🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.games.gb
Article #376

Small utility

#376
From: John Deragon
Date: Sat, 02 Jan 1993 03:20
418 lines
12583 bytes

	Howdy....  enclosed is a small utility progammed called exship
	(examine ship).  It will basically open the "ship" database, and 
	check for problems with it. (ie: ships with cargo greater than 
	ships capacity). 

	Also you can specify a ship number, and the program will print out
	various details of the specified ship.

	See the README file for more info.   Be warned, this was only something
	I hacked together in about 30 minutes, its not in anyway a work or art,
	but it is useful.

#!/bin/sh
# This is a shell archive (shar 3.43)
# made 01/02/1993 03:14 UTC by deragon@jethro
# Source directory /work/devel/GB+/utils
#
# existing files will NOT be overwritten unless -c is specified
#
# This shar contains:
# length  mode       name
# ------ ---------- ------------------------------------------
#   6927 -r--r--r-- exship.c
#   2528 -rw-rw-r-- README
#
# ============= exship.c ==============
if test -f 'exship.c' -a X"$1" != X"-c"; then
	echo 'x - skipping exship.c (File already exists)'
else
echo 'x - extracting exship.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'exship.c' &&
#ident  "@(#)exship.c	1.2 1/1/93 "
/***********************************************
X * exship.c
X *
X * Created: Fri Jan  1 19:34:23 EST 1993
X * Author:  J. Deragon (deragon@jethro.nyu.edu)
X *
X * Version: 1.2 22:05:01
X *
X *	This software is provided "as is" and without any express or
X *	implied warranties, including, without limitation, the implied
X *	warranties of merchantibility and fitness for a particular
X *	purpose.
X *
X ***********************************************/
X
/*
X * includes
X */
#include <strings.h>
#include <signal.h>
#include <sys/file.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#define EXTERN extern
#include "ships.h"
#include "shipdata.h"
X
X
#define DATAFILE "ship"
X
/*
X * Prototypes
X */
X
void            check_ship();
void            check_size();
void            display_ship();
#ifdef __STDC__
void            main(int, char **);
void            readship(shiptype **, int);
#else
void            main();
void            readship();
#endif
X
X
/*
X * Global vars
X */
X
static int      ship_fd;	/* the file descriptor of the datafile     */
struct stat     buffer;		/* used for getting the size of the file   */
int             num_ships;	/* number of total ships in database       */
int             bad_ship_count = 0;	/* the number of bad ships in the
X					 * database */
shiptype       *display;	/* this ship we are currently working on   */
X
/*
X * main:
X * 
X * arguments: shipnumber
X * 
X * called by:
X * 
X * description:  If invoked with zero arguments, it will open the ship datafile
X * and go through looking for obvious errors. If invoked with a ship number
X * it will print out a very verbose listing of the requested ship.
X * 
X */
void
main(argc, argv)
X	int             argc;
X	char           *argv[];
{
X	int             i;
X
X	if ((ship_fd = open(DATAFILE, 000, 0777)) < 0) {
X		perror(main);
X		printf("unable to open %s\n", DATAFILE);
X		exit(-1);
X	}
X	check_size();
X
X	if (argc == 1) {	/* check the whole database for errors */
X		for (i = 1; i <= num_ships; i++) {
X			readship(&display, i);
X			check_ship();
X			free(display);
X		}
X		printf("I found a total of %d bad ships out of %d\n",
X		       bad_ship_count, num_ships);
X	} else {		/* we want a specific ship display */
X		readship(&display, atoi(argv[1]));
X		display_ship();
X	}
X	printf("All Done \n");
X
X	exit(3);
}
X
/*
X * readship:
X * 
X * arguments: shiptype structure, shipnumber
X * 
X * called by:
X * 
X * description:  This funtion reads the actual data from the file.
X * 
X */
X
void
readship(s, shipnum)
X	shiptype      **s;
X	int             shipnum;
{
X	int             n;
X
X	if (shipnum <= 0)
X		exit(1);
X
X	if ((*s = (shiptype *) malloc(sizeof(shiptype))) == 0) {
X		printf("getship:Malloc() error\n");
X		exit(0);
X	}
X	if (lseek(ship_fd, (shipnum - 1) * sizeof(shiptype), L_SET) < 0) {
X		perror(lseek);
X		exit(1);
X	}
X	if ((n = read(ship_fd, (char *) *s, sizeof(shiptype))) != sizeof(shiptype))
X		perror(read);
X
X
X
}
X
/*
X * check_size:
X * 
X * arguments: none
X * 
X * called by: main
X * 
X * description:  gets the number of ships in the current database
X * 
X */
void
check_size()
{
X
X
X	fstat(ship_fd, &buffer);
X	num_ships = buffer.st_size / sizeof(shiptype);
X	printf("Number of ships in database is %d\n", num_ships);
}
X
/*
X * check_ship:
X * 
X * arguments: none
X * 
X * called by: main
X * 
X * description:  checks basic cargo to make sure its within limits of ships
X * ability.
X * 
X */
void
check_ship()
{
X	int             ship_ok = 1;
X	int             pop, troops, res, des, fu, speed, hanger;
X
X	pop = troops = res = des = fu = speed = hanger = 1;
X
X	if (display->type == OTYPE_FACTORY) {
X		if (display->popn > Shipdata[(display)->type][ABIL_MAXCREW])
X			ship_ok = pop = 0;
X		if (display->troops > Shipdata[(display)->type][ABIL_MAXCREW])
X			ship_ok = troops = 0;
X		if ((display->popn + display->troops) >
X		    Shipdata[(display)->type][ABIL_MAXCREW])
X			ship_ok = pop = troops = 0;
X	} else {
X		if (display->popn > display->max_crew)
X			ship_ok = pop = 0;
X		if (display->troops > display->max_crew)
X			ship_ok = troops = 0;
X		if ((display->popn + display->troops) > display->max_crew)
X			ship_ok = pop = troops = 0;
X	}
X	if (display->resource > Max_resource(display))
X		ship_ok = res = 0;
X	if (display->destruct > Max_destruct(display))
X		ship_ok = des = 0;
X	if ((int) display->fuel > Max_fuel(display))
X		ship_ok = fu = 0;
X	if (display->speed > Max_speed(display))
X		ship_ok = speed = 0;
X	if (display->hanger > display->max_hanger)
X		ship_ok = hanger = 0;
X
X	if (!ship_ok) {
X		bad_ship_count++;
X
X		printf("Problem with ship number %d\n", display->number);
X		printf("\t\tOwner: %d\n", display->owner);
X		printf("\t\tGovernor: %d\n", display->governor);
X		printf("\t\tName: %s\n", display->name);
X		printf("\t\tType: %c\n", Shipltrs[display->type]);
X		printf("\n");
X
X		printf("\t %s popn: %d\t max_popn: %d:\n",
X		pop ? "      " : "----->", display->popn, display->max_crew);
X		printf("\t %s troops: %d\t max_troops: %d\n",
X		       troops ? "      " : "----->", display->troops, display->max_crew);
X		printf("\t %s resources: %d\t max_resources: %d\n",
X		       res ? "      " : "----->", display->resource,
X		       display->max_resource);
X		printf("\t %s destruct: %d\t max_destruct: %d\n",
X		       des ? "      " : "----->", display->destruct,
X		       display->max_destruct);
X		printf("\t %s fuel: %d\t max_fuel: %d\n",
X		       fu ? "      " : "----->", (int) display->fuel,
X		       (int) display->max_fuel);
X		printf("\t %s speed: %d\t max_speed: %d\n",
X		       speed ? "      " : "----->", display->speed, display->max_speed);
X		printf("\t %s hanger: %d\t max_hanger: %d\n",
X		       hanger ? "      " : "----->", display->hanger,
X		       display->max_hanger);
X	}
}
X
/*
X * display_ship:
X * 
X * arguments: none
X * 
X * called by: main
X * 
X * description:  Prints a _long_ description a a specific ship.
X * 
X */
void
display_ship()
{
X	printf("Ship Number: %d\tShip Type: %c\tShip Owner: %d\tShip Governor %d\n",
X	       display->number, Shipltrs[display->type],
X	       display->owner, display->governor);
X	printf("Ship Name: %s\n", display->name);
X
X	printf("\nCrew: %-9d\t  Troops: %-6d\tArmor: %d\n",
X	       display->popn, display->troops, display->armor);
X	printf("Size: %-9d\t  Base Mass: %-4.1f\tBase Tech: %4.1f\n",
X	       display->size, display->base_mass, display->tech);
X	printf("Destruct: %-6d  Resources: %-6d\tCrystals: %d\n",
X	       display->destruct, display->resource, display->crystals);
X	printf("Fuel: 4.1f\n", display->fuel);
X	printf("\n");
X	printf("Guns:\tPrimary: %-3d%c\n", display->primary,
X	       (display->primtype == LIGHT ? 'L' :
X		display->primtype == MEDIUM ? 'M' :
X		display->primtype == HEAVY ? 'H' : 'N'));
X	printf("     \tSecondary:  %-3d%c\n", display->primary,
X	       (display->primtype == LIGHT ? 'L' :
X		display->primtype == MEDIUM ? 'M' :
X		display->primtype == HEAVY ? 'H' : 'N'));
X
X	printf("\nNextship: %-6d\t (%s)\n", display->nextship,
X	       display->alive ? "ALIVE" : "DEAD");
}
SHAR_EOF
chmod 0444 exship.c ||
echo 'restore of exship.c failed'
Wc_c="`wc -c < 'exship.c'`"
test 6927 -eq "$Wc_c" ||
	echo 'exship.c: original size 6927, current size' "$Wc_c"
fi
# ============= README ==============
if test -f 'README' -a X"$1" != X"-c"; then
	echo 'x - skipping README (File already exists)'
else
echo 'x - extracting README (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'README' &&
X	This is a small utility to examine the ship database for Galactic
Bloodshed.  It can be used two ways:
X
X	a: run exship with no arguments, and it will scan through the entire
X	   database, looking for ships whose cargo exceeds the maximum allowed.
X
X	b: run exship with a ship_number and it will display a more verbose
X	   listing of the ships.  
X
X	The following are sample outputs from both cases...
X
X	tensha%: exship
X	Number of ships in database is 5
X	Problem with ship number 1
X                Owner: 1
X                Governor: 0
X                Name: Tester
X                Type: @
X
X                popn: 10         max_popn: 10:
X                troops: 0        max_troops: 10
X         -----> resources: 600   max_resources: 500
X                destruct: 100    max_destruct: 100
X                fuel: 1000       max_fuel: 1000
X                speed: 0         max_speed: 0
X                hanger: 0        max_hanger: 0
X	I found a total of 1 bad ships out of 5
X	All Done
-----
X	
X  As you can see there is a problem with the ship [denoted by the ----->]
X  in which the ship has more resources loaded then it should have.  It also
X  at the top of the screen will display the number of ships in the database.
X
X
X  When run with a argument, the following is the output:
X
X	tensha%: exship 1
X	Number of ships in database is 5
X	Ship Number: 1  Ship Type: @    Ship Owner: 1   Ship Governor 0
X	Ship Name: Tester
X
X	Crew: 10          Troops: 0             Armor: 20
X	Size: 25          Base Mass: 100.0      Base Tech: 100.0
X	Destruct: 100     Resources: 600        Crystals: 0
X	Fuel: 4.1f
X
X	Guns:   Primary: 10 L
X        Secondary:  10 L
X
X	Nextship: 2              (ALIVE)
X	All Done
X
-----
X
X	Compliling information:
X
X	This program has only been tested on a Suns running Sun OS 4.1.1, and
X	compiled succesfully with the Sun compilier and gcc 1.42.
X
X	compile with the following line:
X
X	gcc -I/work/GB/hdrs -g -o exship exship.c	
X
X	but change /work/GB/hdrs to be the location where the compiler can
X	find the ships.h and shipdata.h files. (these files are part of the GB
X	release)
X
X
X	This is something I hacked together very very quickly to just fix
X	a few problems with the database.  It is not meant to be a catch all
X	for database problems.   If I have some time in the future, I will
X	add new bells and whistles.  Feel free to modify this source, and if
X	you make any improvements, please email them back to me, so I can 
X	merge them into the next version.
X
X
X	John Paul Deragon
X	deragon@jethro.nyu.edu
X	Fri Jan  1 21:58:49 EST 1993
SHAR_EOF
chmod 0664 README ||
echo 'restore of README failed'
Wc_c="`wc -c < 'README'`"
test 2528 -eq "$Wc_c" ||
	echo 'README: original size 2528, current size' "$Wc_c"
fi
exit 0
-- 



-----
John Deragon		   deragon@cs.nyu.edu | deragon@jethro.cs.nyu.edu
-----
Space is big.  You just won't believe how vastly, hugely, mind-bogglingly
big it is.   I mean, you  may think it's a long  way down the road to the 
drug store, but that's just peanuts to space.
-- "The Hitchhiker's Guide to the Galaxy"
Technical Details
Message-ID: <9301020318.AA01177@JETHRO.CS.NYU.EDU>
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!malgudi.oar.net!caen!sdd.hp.com!elroy.jpl.nasa.gov!swrinde!cs.utexas.edu!gateway

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!malgudi.oar.net!caen!sdd.hp.com!elroy.jpl.nasa.gov!swrinde!cs.utexas.edu!gateway
From: deragon@JETHRO.CS.NYU.EDU (John Deragon)
Newsgroups: alt.games.gb
Subject: Small utility
Date: 1 Jan 1993 21:20:45 -0600
Organization: UTexas Mail-to-News Gateway
Lines: 419
Sender: daemon@cs.utexas.edu
Message-ID: <9301020318.AA01177@JETHRO.CS.NYU.EDU>
NNTP-Posting-Host: cs.utexas.edu