home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / alt / games / gb / 1393 < prev    next >
Encoding:
Text File  |  1993-01-01  |  11.9 KB  |  431 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!malgudi.oar.net!caen!sdd.hp.com!elroy.jpl.nasa.gov!swrinde!cs.utexas.edu!gateway
  2. From: deragon@JETHRO.CS.NYU.EDU (John Deragon)
  3. Newsgroups: alt.games.gb
  4. Subject: Small utility
  5. Date: 1 Jan 1993 21:20:45 -0600
  6. Organization: UTexas Mail-to-News Gateway
  7. Lines: 419
  8. Sender: daemon@cs.utexas.edu
  9. Message-ID: <9301020318.AA01177@JETHRO.CS.NYU.EDU>
  10. NNTP-Posting-Host: cs.utexas.edu
  11.  
  12.  
  13.  
  14.     Howdy....  enclosed is a small utility progammed called exship
  15.     (examine ship).  It will basically open the "ship" database, and 
  16.     check for problems with it. (ie: ships with cargo greater than 
  17.     ships capacity). 
  18.  
  19.     Also you can specify a ship number, and the program will print out
  20.     various details of the specified ship.
  21.  
  22.     See the README file for more info.   Be warned, this was only something
  23.     I hacked together in about 30 minutes, its not in anyway a work or art,
  24.     but it is useful.
  25.  
  26. #!/bin/sh
  27. # This is a shell archive (shar 3.43)
  28. # made 01/02/1993 03:14 UTC by deragon@jethro
  29. # Source directory /work/devel/GB+/utils
  30. #
  31. # existing files will NOT be overwritten unless -c is specified
  32. #
  33. # This shar contains:
  34. # length  mode       name
  35. # ------ ---------- ------------------------------------------
  36. #   6927 -r--r--r-- exship.c
  37. #   2528 -rw-rw-r-- README
  38. #
  39. # ============= exship.c ==============
  40. if test -f 'exship.c' -a X"$1" != X"-c"; then
  41.     echo 'x - skipping exship.c (File already exists)'
  42. else
  43. echo 'x - extracting exship.c (Text)'
  44. sed 's/^X//' << 'SHAR_EOF' > 'exship.c' &&
  45. #ident  "@(#)exship.c    1.2 1/1/93 "
  46. /***********************************************
  47. X * exship.c
  48. X *
  49. X * Created: Fri Jan  1 19:34:23 EST 1993
  50. X * Author:  J. Deragon (deragon@jethro.nyu.edu)
  51. X *
  52. X * Version: 1.2 22:05:01
  53. X *
  54. X *    This software is provided "as is" and without any express or
  55. X *    implied warranties, including, without limitation, the implied
  56. X *    warranties of merchantibility and fitness for a particular
  57. X *    purpose.
  58. X *
  59. X ***********************************************/
  60. X
  61. /*
  62. X * includes
  63. X */
  64. #include <strings.h>
  65. #include <signal.h>
  66. #include <sys/file.h>
  67. #include <errno.h>
  68. #include <sys/types.h>
  69. #include <sys/stat.h>
  70. #define EXTERN extern
  71. #include "ships.h"
  72. #include "shipdata.h"
  73. X
  74. X
  75. #define DATAFILE "ship"
  76. X
  77. /*
  78. X * Prototypes
  79. X */
  80. X
  81. void            check_ship();
  82. void            check_size();
  83. void            display_ship();
  84. #ifdef __STDC__
  85. void            main(int, char **);
  86. void            readship(shiptype **, int);
  87. #else
  88. void            main();
  89. void            readship();
  90. #endif
  91. X
  92. X
  93. /*
  94. X * Global vars
  95. X */
  96. X
  97. static int      ship_fd;    /* the file descriptor of the datafile     */
  98. struct stat     buffer;        /* used for getting the size of the file   */
  99. int             num_ships;    /* number of total ships in database       */
  100. int             bad_ship_count = 0;    /* the number of bad ships in the
  101. X                     * database */
  102. shiptype       *display;    /* this ship we are currently working on   */
  103. X
  104. /*
  105. X * main:
  106. X * 
  107. X * arguments: shipnumber
  108. X * 
  109. X * called by:
  110. X * 
  111. X * description:  If invoked with zero arguments, it will open the ship datafile
  112. X * and go through looking for obvious errors. If invoked with a ship number
  113. X * it will print out a very verbose listing of the requested ship.
  114. X * 
  115. X */
  116. void
  117. main(argc, argv)
  118. X    int             argc;
  119. X    char           *argv[];
  120. {
  121. X    int             i;
  122. X
  123. X    if ((ship_fd = open(DATAFILE, 000, 0777)) < 0) {
  124. X        perror(main);
  125. X        printf("unable to open %s\n", DATAFILE);
  126. X        exit(-1);
  127. X    }
  128. X    check_size();
  129. X
  130. X    if (argc == 1) {    /* check the whole database for errors */
  131. X        for (i = 1; i <= num_ships; i++) {
  132. X            readship(&display, i);
  133. X            check_ship();
  134. X            free(display);
  135. X        }
  136. X        printf("I found a total of %d bad ships out of %d\n",
  137. X               bad_ship_count, num_ships);
  138. X    } else {        /* we want a specific ship display */
  139. X        readship(&display, atoi(argv[1]));
  140. X        display_ship();
  141. X    }
  142. X    printf("All Done \n");
  143. X
  144. X    exit(3);
  145. }
  146. X
  147. /*
  148. X * readship:
  149. X * 
  150. X * arguments: shiptype structure, shipnumber
  151. X * 
  152. X * called by:
  153. X * 
  154. X * description:  This funtion reads the actual data from the file.
  155. X * 
  156. X */
  157. X
  158. void
  159. readship(s, shipnum)
  160. X    shiptype      **s;
  161. X    int             shipnum;
  162. {
  163. X    int             n;
  164. X
  165. X    if (shipnum <= 0)
  166. X        exit(1);
  167. X
  168. X    if ((*s = (shiptype *) malloc(sizeof(shiptype))) == 0) {
  169. X        printf("getship:Malloc() error\n");
  170. X        exit(0);
  171. X    }
  172. X    if (lseek(ship_fd, (shipnum - 1) * sizeof(shiptype), L_SET) < 0) {
  173. X        perror(lseek);
  174. X        exit(1);
  175. X    }
  176. X    if ((n = read(ship_fd, (char *) *s, sizeof(shiptype))) != sizeof(shiptype))
  177. X        perror(read);
  178. X
  179. X
  180. X
  181. }
  182. X
  183. /*
  184. X * check_size:
  185. X * 
  186. X * arguments: none
  187. X * 
  188. X * called by: main
  189. X * 
  190. X * description:  gets the number of ships in the current database
  191. X * 
  192. X */
  193. void
  194. check_size()
  195. {
  196. X
  197. X
  198. X    fstat(ship_fd, &buffer);
  199. X    num_ships = buffer.st_size / sizeof(shiptype);
  200. X    printf("Number of ships in database is %d\n", num_ships);
  201. }
  202. X
  203. /*
  204. X * check_ship:
  205. X * 
  206. X * arguments: none
  207. X * 
  208. X * called by: main
  209. X * 
  210. X * description:  checks basic cargo to make sure its within limits of ships
  211. X * ability.
  212. X * 
  213. X */
  214. void
  215. check_ship()
  216. {
  217. X    int             ship_ok = 1;
  218. X    int             pop, troops, res, des, fu, speed, hanger;
  219. X
  220. X    pop = troops = res = des = fu = speed = hanger = 1;
  221. X
  222. X    if (display->type == OTYPE_FACTORY) {
  223. X        if (display->popn > Shipdata[(display)->type][ABIL_MAXCREW])
  224. X            ship_ok = pop = 0;
  225. X        if (display->troops > Shipdata[(display)->type][ABIL_MAXCREW])
  226. X            ship_ok = troops = 0;
  227. X        if ((display->popn + display->troops) >
  228. X            Shipdata[(display)->type][ABIL_MAXCREW])
  229. X            ship_ok = pop = troops = 0;
  230. X    } else {
  231. X        if (display->popn > display->max_crew)
  232. X            ship_ok = pop = 0;
  233. X        if (display->troops > display->max_crew)
  234. X            ship_ok = troops = 0;
  235. X        if ((display->popn + display->troops) > display->max_crew)
  236. X            ship_ok = pop = troops = 0;
  237. X    }
  238. X    if (display->resource > Max_resource(display))
  239. X        ship_ok = res = 0;
  240. X    if (display->destruct > Max_destruct(display))
  241. X        ship_ok = des = 0;
  242. X    if ((int) display->fuel > Max_fuel(display))
  243. X        ship_ok = fu = 0;
  244. X    if (display->speed > Max_speed(display))
  245. X        ship_ok = speed = 0;
  246. X    if (display->hanger > display->max_hanger)
  247. X        ship_ok = hanger = 0;
  248. X
  249. X    if (!ship_ok) {
  250. X        bad_ship_count++;
  251. X
  252. X        printf("Problem with ship number %d\n", display->number);
  253. X        printf("\t\tOwner: %d\n", display->owner);
  254. X        printf("\t\tGovernor: %d\n", display->governor);
  255. X        printf("\t\tName: %s\n", display->name);
  256. X        printf("\t\tType: %c\n", Shipltrs[display->type]);
  257. X        printf("\n");
  258. X
  259. X        printf("\t %s popn: %d\t max_popn: %d:\n",
  260. X        pop ? "      " : "----->", display->popn, display->max_crew);
  261. X        printf("\t %s troops: %d\t max_troops: %d\n",
  262. X               troops ? "      " : "----->", display->troops, display->max_crew);
  263. X        printf("\t %s resources: %d\t max_resources: %d\n",
  264. X               res ? "      " : "----->", display->resource,
  265. X               display->max_resource);
  266. X        printf("\t %s destruct: %d\t max_destruct: %d\n",
  267. X               des ? "      " : "----->", display->destruct,
  268. X               display->max_destruct);
  269. X        printf("\t %s fuel: %d\t max_fuel: %d\n",
  270. X               fu ? "      " : "----->", (int) display->fuel,
  271. X               (int) display->max_fuel);
  272. X        printf("\t %s speed: %d\t max_speed: %d\n",
  273. X               speed ? "      " : "----->", display->speed, display->max_speed);
  274. X        printf("\t %s hanger: %d\t max_hanger: %d\n",
  275. X               hanger ? "      " : "----->", display->hanger,
  276. X               display->max_hanger);
  277. X    }
  278. }
  279. X
  280. /*
  281. X * display_ship:
  282. X * 
  283. X * arguments: none
  284. X * 
  285. X * called by: main
  286. X * 
  287. X * description:  Prints a _long_ description a a specific ship.
  288. X * 
  289. X */
  290. void
  291. display_ship()
  292. {
  293. X    printf("Ship Number: %d\tShip Type: %c\tShip Owner: %d\tShip Governor %d\n",
  294. X           display->number, Shipltrs[display->type],
  295. X           display->owner, display->governor);
  296. X    printf("Ship Name: %s\n", display->name);
  297. X
  298. X    printf("\nCrew: %-9d\t  Troops: %-6d\tArmor: %d\n",
  299. X           display->popn, display->troops, display->armor);
  300. X    printf("Size: %-9d\t  Base Mass: %-4.1f\tBase Tech: %4.1f\n",
  301. X           display->size, display->base_mass, display->tech);
  302. X    printf("Destruct: %-6d  Resources: %-6d\tCrystals: %d\n",
  303. X           display->destruct, display->resource, display->crystals);
  304. X    printf("Fuel: 4.1f\n", display->fuel);
  305. X    printf("\n");
  306. X    printf("Guns:\tPrimary: %-3d%c\n", display->primary,
  307. X           (display->primtype == LIGHT ? 'L' :
  308. X        display->primtype == MEDIUM ? 'M' :
  309. X        display->primtype == HEAVY ? 'H' : 'N'));
  310. X    printf("     \tSecondary:  %-3d%c\n", display->primary,
  311. X           (display->primtype == LIGHT ? 'L' :
  312. X        display->primtype == MEDIUM ? 'M' :
  313. X        display->primtype == HEAVY ? 'H' : 'N'));
  314. X
  315. X    printf("\nNextship: %-6d\t (%s)\n", display->nextship,
  316. X           display->alive ? "ALIVE" : "DEAD");
  317. }
  318. SHAR_EOF
  319. chmod 0444 exship.c ||
  320. echo 'restore of exship.c failed'
  321. Wc_c="`wc -c < 'exship.c'`"
  322. test 6927 -eq "$Wc_c" ||
  323.     echo 'exship.c: original size 6927, current size' "$Wc_c"
  324. fi
  325. # ============= README ==============
  326. if test -f 'README' -a X"$1" != X"-c"; then
  327.     echo 'x - skipping README (File already exists)'
  328. else
  329. echo 'x - extracting README (Text)'
  330. sed 's/^X//' << 'SHAR_EOF' > 'README' &&
  331. X    This is a small utility to examine the ship database for Galactic
  332. Bloodshed.  It can be used two ways:
  333. X
  334. X    a: run exship with no arguments, and it will scan through the entire
  335. X       database, looking for ships whose cargo exceeds the maximum allowed.
  336. X
  337. X    b: run exship with a ship_number and it will display a more verbose
  338. X       listing of the ships.  
  339. X
  340. X    The following are sample outputs from both cases...
  341. X
  342. X    tensha%: exship
  343. X    Number of ships in database is 5
  344. X    Problem with ship number 1
  345. X                Owner: 1
  346. X                Governor: 0
  347. X                Name: Tester
  348. X                Type: @
  349. X
  350. X                popn: 10         max_popn: 10:
  351. X                troops: 0        max_troops: 10
  352. X         -----> resources: 600   max_resources: 500
  353. X                destruct: 100    max_destruct: 100
  354. X                fuel: 1000       max_fuel: 1000
  355. X                speed: 0         max_speed: 0
  356. X                hanger: 0        max_hanger: 0
  357. X    I found a total of 1 bad ships out of 5
  358. X    All Done
  359. -----
  360. X    
  361. X  As you can see there is a problem with the ship [denoted by the ----->]
  362. X  in which the ship has more resources loaded then it should have.  It also
  363. X  at the top of the screen will display the number of ships in the database.
  364. X
  365. X
  366. X  When run with a argument, the following is the output:
  367. X
  368. X    tensha%: exship 1
  369. X    Number of ships in database is 5
  370. X    Ship Number: 1  Ship Type: @    Ship Owner: 1   Ship Governor 0
  371. X    Ship Name: Tester
  372. X
  373. X    Crew: 10          Troops: 0             Armor: 20
  374. X    Size: 25          Base Mass: 100.0      Base Tech: 100.0
  375. X    Destruct: 100     Resources: 600        Crystals: 0
  376. X    Fuel: 4.1f
  377. X
  378. X    Guns:   Primary: 10 L
  379. X        Secondary:  10 L
  380. X
  381. X    Nextship: 2              (ALIVE)
  382. X    All Done
  383. X
  384. -----
  385. X
  386. X    Compliling information:
  387. X
  388. X    This program has only been tested on a Suns running Sun OS 4.1.1, and
  389. X    compiled succesfully with the Sun compilier and gcc 1.42.
  390. X
  391. X    compile with the following line:
  392. X
  393. X    gcc -I/work/GB/hdrs -g -o exship exship.c    
  394. X
  395. X    but change /work/GB/hdrs to be the location where the compiler can
  396. X    find the ships.h and shipdata.h files. (these files are part of the GB
  397. X    release)
  398. X
  399. X
  400. X    This is something I hacked together very very quickly to just fix
  401. X    a few problems with the database.  It is not meant to be a catch all
  402. X    for database problems.   If I have some time in the future, I will
  403. X    add new bells and whistles.  Feel free to modify this source, and if
  404. X    you make any improvements, please email them back to me, so I can 
  405. X    merge them into the next version.
  406. X
  407. X
  408. X    John Paul Deragon
  409. X    deragon@jethro.nyu.edu
  410. X    Fri Jan  1 21:58:49 EST 1993
  411. SHAR_EOF
  412. chmod 0664 README ||
  413. echo 'restore of README failed'
  414. Wc_c="`wc -c < 'README'`"
  415. test 2528 -eq "$Wc_c" ||
  416.     echo 'README: original size 2528, current size' "$Wc_c"
  417. fi
  418. exit 0
  419. -- 
  420.  
  421.  
  422.  
  423. -----
  424. John Deragon           deragon@cs.nyu.edu | deragon@jethro.cs.nyu.edu
  425. -----
  426. Space is big.  You just won't believe how vastly, hugely, mind-bogglingly
  427. big it is.   I mean, you  may think it's a long  way down the road to the 
  428. drug store, but that's just peanuts to space.
  429. -- "The Hitchhiker's Guide to the Galaxy"
  430.  
  431.