/* gcc ship.c -lm -o ship */
/* by Viola Schmidt schmidt@securitas.net */
/*
setenv DRIVE 1
setenv WEAPONS 1
setenv CARGO 1
setenv SHIELDS 1
*/
#include <stdio.h>
#include  <stdlib.h>
#include <math.h>

float drivetech=10.00;
float weapontech=10.00;
float shieldtech=10.00;
float cargotech=1.00;

float production=99;

double pow();

void shields(float shields, float size)
{
	float ShiStr;
	ShiStr = shieldtech*(shields / pow (size,1.0/3.0) * pow (30.0,1.0/3.0));
	if(ShiStr)
	printf("Shields: %2.2f (Safe %2.2f, doomed %2.2f)\n",ShiStr,ShiStr /4, ShiStr * 4 );
	return;
}


void main(int argc, char *argv[])
{
	int i,fanz;
	float cargo,mass,speed,lspeed,anz;
	float ship[5];
	char *zwischenwert;

	if(argc!=6 && argc!=7) {
		printf("Usage: %s [production] drive nr weapon shield cargo\n",
		argv[0]);
		printf("The program uses the environment variables DRIVE, CARGO, WEAPONS and SHIELDS.\n");
		exit(-1);
	}
	if(argc==7)
	{
	       production=atof(argv[1]);
	       ship[0]=atof(argv[2]);
	       ship[1]=atof(argv[3]);
	       ship[2]=atof(argv[4]);
	       ship[3]=atof(argv[5]);
	       ship[4]=atof(argv[6]);
	} else {
		production=99;
		ship[0]=atof(argv[1]);
		ship[1]=atof(argv[2]);
		ship[2]=atof(argv[3]);
		ship[3]=atof(argv[4]);
		ship[4]=atof(argv[5]);
	}

	if(zwischenwert=getenv("DRIVE")) 
		drivetech=atof(zwischenwert);
	if(zwischenwert=getenv("CARGO")) 
		cargotech=atof(zwischenwert);
	if(zwischenwert=getenv("SHIELDS")) 
		shieldtech=atof(zwischenwert);
	if(zwischenwert=getenv("WEAPONS")) 
		weapontech=atof(zwischenwert);

	cargo=cargotech*(ship[4]+(ship[4]*ship[4])/10);
	mass=ship[0]+(ship[1]==0?0:ship[2])+ship[3]+ship[4];
	if(ship[1]>1) mass+=ship[2]*(ship[1]-1)/2;
	speed=drivetech*20*ship[0]/mass;
	lspeed=drivetech*20*ship[0]/(mass+cargo/cargotech);
	if(ship[2])
	printf("Firepower: %2.2f * %2.2f\n",ship[1],ship[2]*weapontech); 

	if (cargo>0) {
	printf("Mass %2.2f - Speed %2.2f  (or %2.2f) Cargo %2.2f  \n",mass,speed,lspeed,cargo); } else
	{
	if (speed) printf("Mass %2.2f - Speed %2.2f\n",mass,speed);
	else printf("Mass %2.2f - No Drive \n",mass); }
	anz=production/mass;
	fanz=(int)anz;
	shields(ship[3],mass);
	if(cargo>0 && ship[3]>0) {
		printf("The same while loaded:\n");
		shields(ship[3],mass+cargo/cargotech);
	}
	printf("# %2.2f       Suggested mass: %2.2f or %2.2f or %2.2f\n",anz,(production/((float)fanz-1)),(production/(float)fanz),(production/((float)fanz+1)));
}

