TXTELITE.C 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /* txtelite.c 1.5 */
  2. /* Textual version of Elite trading (C implementation) */
  3. /* Converted by Ian Bell from 6502 Elite sources.
  4. Original 6502 Elite by Ian Bell & David Braben. */
  5. /* ----------------------------------------------------------------------
  6. The nature of basic mechanisms used to generate the Elite socio-economic
  7. universe are now widely known. A competant games programmer should be able to
  8. produce equivalent functionality. A competant hacker should be able to lift
  9. the exact system from the object code base of official conversions.
  10. This file may be regarded as defining the Classic Elite universe.
  11. It contains a C implementation of the precise 6502 algorithms used in the
  12. original BBC Micro version of Acornsoft Elite (apart from Galctic Hyperspace
  13. target systems) together with a parsed textual command testbed.
  14. Note that this is not the universe of David Braben's 'Frontier' series.
  15. ICGB 13/10/99 ; 21/07/15
  16. ian@ianbell.me
  17. www.ianbellelite.com
  18. ---------------------------------------------------------------------- */
  19. /* Note that this program is "quick-hack" text parser-driven version
  20. of Elite with no combat or missions.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <conio.h>
  27. #include <graph.h>
  28. #include <math.h>
  29. #include <malloc.h>
  30. #define true (-1)
  31. #define false (0)
  32. #define tonnes (0)
  33. #define maxlen (20) /* Length of strings */
  34. typedef int boolean;
  35. typedef unsigned char uint8;
  36. typedef unsigned short uint16;
  37. typedef signed short int16;
  38. typedef signed long int32;
  39. typedef uint16 uint;
  40. typedef int planetnum;
  41. typedef struct
  42. { uint8 a,b,c,d;
  43. } fastseedtype; /* four byte random number used for planet description */
  44. typedef struct
  45. { uint16 w0;
  46. uint16 w1;
  47. uint16 w2;
  48. } seedtype; /* six byte random number used as seed for planets */
  49. typedef struct
  50. { uint x;
  51. uint y; /* One byte unsigned */
  52. uint economy; /* These two are actually only 0-7 */
  53. uint govtype;
  54. uint techlev; /* 0-16 i think */
  55. uint population; /* One byte */
  56. uint productivity; /* Two byte */
  57. uint radius; /* Two byte (not used by game at all) */
  58. fastseedtype goatsoupseed;
  59. char name[12];
  60. } plansys ;
  61. #define galsize (256)
  62. #define AlienItems (16)
  63. #define lasttrade AlienItems
  64. #define numforLave 7 /* Lave is 7th generated planet in galaxy one */
  65. #define numforZaonce 129
  66. #define numforDiso 147
  67. #define numforRied 46
  68. plansys galaxy[galsize]; /* Need 0 to galsize-1 inclusive */
  69. seedtype seed;
  70. fastseedtype rnd_seed;
  71. boolean nativerand;
  72. typedef struct
  73. { /* In 6502 version these were: */
  74. uint baseprice; /* one byte */
  75. int16 gradient; /* five bits plus sign */
  76. uint basequant; /* one byte */
  77. uint maskbyte; /* one byte */
  78. uint units; /* two bits */
  79. char name[20]; /* longest="Radioactives" */
  80. } tradegood ;
  81. typedef struct
  82. { uint quantity[lasttrade+1];
  83. uint price[lasttrade+1];
  84. } markettype ;
  85. /* Player workspace */
  86. uint shipshold[lasttrade+1]; /* Contents of cargo bay */
  87. planetnum currentplanet; /* Current planet */
  88. uint galaxynum; /* Galaxy number (1-8) */
  89. int32 cash;
  90. uint fuel;
  91. markettype localmarket;
  92. uint holdspace;
  93. int fuelcost =2; /* 0.2 CR/Light year */
  94. int maxfuel =70; /* 7.0 LY tank */
  95. const uint16 base0=0x5A4A;
  96. const uint16 base1=0x0248;
  97. const uint16 base2=0xB753; /* Base seed for galaxy 1 */
  98. //static const char *digrams=
  99. // "ABOUSEITILETSTONLONUTHNO"
  100. // "ALLEXEGEZACEBISO"
  101. // "USESARMAINDIREA?"
  102. // "ERATENBERALAVETI"
  103. // "EDORQUANTEISRION";
  104. #if 0 // 1.4-
  105. char pairs0[]="ABOUSEITILETSTONLONUTHNO";
  106. /* must continue into .. */
  107. char pairs[] = "..LEXEGEZACEBISO"
  108. "USESARMAINDIREA."
  109. "ERATENBERALAVETI"
  110. "EDORQUANTEISRION"; /* Dots should be nullprint characters */
  111. #else // 1.5 planet names fix
  112. char pairs0[]=
  113. "ABOUSEITILETSTONLONUTHNOALLEXEGEZACEBISOUSESARMAINDIREA.ERATENBERALAVETIEDORQUANTEISRION";
  114. char pairs[] = "..LEXEGEZACEBISO"
  115. "USESARMAINDIREA."
  116. "ERATENBERALAVETI"
  117. "EDORQUANTEISRION"; /* Dots should be nullprint characters */
  118. #endif
  119. char govnames[][maxlen]={"Anarchy","Feudal","Multi-gov","Dictatorship",
  120. "Communist","Confederacy","Democracy","Corporate State"};
  121. char econnames[][maxlen]={"Rich Ind","Average Ind","Poor Ind","Mainly Ind",
  122. "Mainly Agri","Rich Agri","Average Agri","Poor Agri"};
  123. char unitnames[][5] ={"t","kg","g"};
  124. /* Data for DB's price/availability generation system */
  125. /* Base Grad Base Mask Un Name
  126. price ient quant it */
  127. #define POLITICALLY_CORRECT 0
  128. /* Set to 1 for NES-sanitised trade goods */
  129. tradegood commodities[]=
  130. {
  131. {0x13,-0x02,0x06,0x01,0,"Food "},
  132. {0x14,-0x01,0x0A,0x03,0,"Textiles "},
  133. {0x41,-0x03,0x02,0x07,0,"Radioactives"},
  134. #if POLITICALLY_CORRECT
  135. {0x28,-0x05,0xE2,0x1F,0,"Robot Slaves"},
  136. {0x53,-0x05,0xFB,0x0F,0,"Beverages "},
  137. #else
  138. {0x28,-0x05,0xE2,0x1F,0,"Slaves "},
  139. {0x53,-0x05,0xFB,0x0F,0,"Liquor/Wines"},
  140. #endif
  141. {0xC4,+0x08,0x36,0x03,0,"Luxuries "},
  142. #if POLITICALLY_CORRECT
  143. {0xEB,+0x1D,0x08,0x78,0,"Rare Species"},
  144. #else
  145. {0xEB,+0x1D,0x08,0x78,0,"Narcotics "},
  146. #endif
  147. {0x9A,+0x0E,0x38,0x03,0,"Computers "},
  148. {0x75,+0x06,0x28,0x07,0,"Machinery "},
  149. {0x4E,+0x01,0x11,0x1F,0,"Alloys "},
  150. {0x7C,+0x0d,0x1D,0x07,0,"Firearms "},
  151. {0xB0,-0x09,0xDC,0x3F,0,"Furs "},
  152. {0x20,-0x01,0x35,0x03,0,"Minerals "},
  153. {0x61,-0x01,0x42,0x07,1,"Gold "},
  154. {0xAB,-0x02,0x37,0x1F,1,"Platinum "},
  155. {0x2D,-0x01,0xFA,0x0F,2,"Gem-Strones "},
  156. {0x35,+0x0F,0xC0,0x07,0,"Alien Items "},
  157. };
  158. /**-Required data for text interface **/
  159. char tradnames[lasttrade][maxlen]; /* Tradegood names used in text commands
  160. Set using commodities array */
  161. void goat_soup(const char *source,plansys * psy);
  162. #define nocomms (14)
  163. boolean dobuy(char *);
  164. boolean dosell(char *);
  165. boolean dofuel(char *);
  166. boolean dojump(char *);
  167. boolean docash(char *);
  168. boolean domkt(char *);
  169. boolean dohelp(char *);
  170. boolean dohold(char *);
  171. boolean dosneak(char *);
  172. boolean dolocal(char *);
  173. boolean doinfo(char *);
  174. boolean dogalhyp(char *);
  175. boolean doquit(char *);
  176. boolean dotweakrand(char *);
  177. char commands[nocomms][maxlen]=
  178. {"buy", "sell", "fuel", "jump",
  179. "cash", "mkt", "help", "hold",
  180. "sneak", "local", "info", "galhyp",
  181. "quit", "rand"
  182. };
  183. boolean (*comfuncs[nocomms])(char *)=
  184. {dobuy, dosell, dofuel, dojump,
  185. docash, domkt, dohelp, dohold,
  186. dosneak, dolocal, doinfo, dogalhyp,
  187. doquit, dotweakrand
  188. };
  189. /**- General functions **/
  190. void port_srand(unsigned int);
  191. int port_rand(void);
  192. static unsigned int lastrand = 0;
  193. void mysrand(unsigned int seed)
  194. { srand(seed);
  195. lastrand = seed - 1;
  196. }
  197. int myrand(void)
  198. { int r;
  199. if(nativerand) r=rand();
  200. else
  201. { // As supplied by D McDonnell from SAS Insititute C
  202. r = (((((((((((lastrand << 3) - lastrand) << 3)
  203. + lastrand) << 1) + lastrand) << 4)
  204. - lastrand) << 1) - lastrand) + 0xe60)
  205. & 0x7fffffff;
  206. lastrand = r - 1;
  207. }
  208. return(r);
  209. }
  210. char randbyte(void) { return (char)(myrand()&0xFF);}
  211. uint mymin(uint a,uint b) { if(a<b) return(a); else return(b);}
  212. void stop(char * string)
  213. { printf("\n%s",string);
  214. exit(1);
  215. }
  216. /**+ ftoi **/
  217. signed int ftoi(double value)
  218. { return ((signed int)floor(value+0.5));
  219. }
  220. /**+ ftoi2 **/
  221. signed int ftoi2(double value)
  222. { return ((signed int)floor(value));
  223. }
  224. void tweakseed(seedtype *s)
  225. { uint16 temp;
  226. temp = ((*s).w0)+((*s).w1)+((*s).w2); /* 2 byte aritmetic */
  227. (*s).w0 = (*s).w1;
  228. (*s).w1 = (*s).w2;
  229. (*s).w2 = temp;
  230. }
  231. /**-String functions for text interface **/
  232. void stripout(char *s,const char c) /* Remove all c's from string s */
  233. { size_t i=0,j=0;
  234. while(i<strlen(s))
  235. { if(s[i]!=c) { s[j]=s[i]; j++;}
  236. i++;
  237. }
  238. s[j]=0;
  239. }
  240. int toupper(char c)
  241. { if((c>='a')&&(c<='z')) return(c+'A'-'a');
  242. return((int)c);
  243. }
  244. int tolower(char c)
  245. { if((c>='A')&&(c<='Z')) return(c+'a'-'A');
  246. return((int)c);
  247. }
  248. int stringbeg(char *s,char *t)
  249. /* Return nonzero iff string t begins with non-empty string s */
  250. { size_t i=0;
  251. size_t l=strlen(s);
  252. if(l>0)
  253. { while((i<l)&(toupper(s[i])==toupper(t[i]))) i++;
  254. if(i==l) return true;
  255. }
  256. return false;
  257. }
  258. uint stringmatch(char *s,char a[][20],uint n)
  259. /* Check string s against n options in string array a
  260. If matches ith element return i+1 else return 0 */
  261. { uint i=0;
  262. while(i<n)
  263. { if(stringbeg(s,a[i])) return i+1;
  264. i++;
  265. }
  266. return 0;
  267. }
  268. void spacesplit(char *s,char *t)
  269. /* Split string s at first space, returning first 'word' in t & shortening s
  270. */
  271. { size_t i=0,j=0;
  272. size_t l=strlen(s);
  273. while((i<l)&(s[i]==' ')) i++;; /* Strip leading spaces */
  274. if(i==l) {s[0]=0; t[0]=0; return;};
  275. while((i<l)&(s[i]!=' ')) t[j++]=s[i++];
  276. t[j]=0; i++; j=0;
  277. while(i<l) s[j++]=s[i++];
  278. s[j]=0;
  279. }
  280. /**-Functions for stock market **/
  281. uint gamebuy(uint i, uint a)
  282. /* Try to buy ammount a of good i Return ammount bought */
  283. /* Cannot buy more than is availble, can afford, or will fit in hold */
  284. { uint t;
  285. if(cash<0) t=0;
  286. else
  287. { t=mymin(localmarket.quantity[i],a);
  288. if ((commodities[i].units)==tonnes) {t = mymin(holdspace,t);}
  289. t = mymin(t, (uint)floor((double)cash/(localmarket.price[i])));
  290. }
  291. shipshold[i]+=t;
  292. localmarket.quantity[i]-=t;
  293. cash-=t*(localmarket.price[i]);
  294. if ((commodities[i].units)==tonnes) {holdspace-=t;}
  295. return t;
  296. }
  297. uint gamesell(uint i,uint a) /* As gamebuy but selling */
  298. { uint t=mymin(shipshold[i],a);
  299. shipshold[i]-=t;
  300. localmarket.quantity[i]+=t;
  301. if ((commodities[i].units)==tonnes) {holdspace+=t;}
  302. cash+=t*(localmarket.price[i]);
  303. return t;
  304. }
  305. markettype genmarket(uint fluct, plansys p)
  306. /* Prices and availabilities are influenced by the planet's economy type
  307. (0-7) and a random "fluctuation" byte that was kept within the saved
  308. commander position to keep the market prices constant over gamesaves.
  309. Availabilities must be saved with the game since the player alters them
  310. by buying (and selling(?))
  311. Almost all operations are one byte only and overflow "errors" are
  312. extremely frequent and exploited.
  313. Trade Item prices are held internally in a single byte=true value/4.
  314. The decimal point in prices is introduced only when printing them.
  315. Internally, all prices are integers.
  316. The player's cash is held in four bytes.
  317. */
  318. { markettype market;
  319. unsigned short i;
  320. for(i=0;i<=lasttrade;i++)
  321. { signed int q;
  322. signed int product = (p.economy)*(commodities[i].gradient);
  323. signed int changing = fluct & (commodities[i].maskbyte);
  324. q = (commodities[i].basequant) + changing - product;
  325. q = q&0xFF;
  326. if(q&0x80) {q=0;}; /* Clip to positive 8-bit */
  327. market.quantity[i] = (uint16)(q & 0x3F); /* Mask to 6 bits */
  328. q = (commodities[i].baseprice) + changing + product;
  329. q = q & 0xFF;
  330. market.price[i] = (uint16) (q*4);
  331. }
  332. market.quantity[AlienItems] = 0; /* Override to force nonavailability */
  333. return market;
  334. }
  335. void displaymarket(markettype m)
  336. { unsigned short i;
  337. for(i=0;i<=lasttrade;i++)
  338. { printf("\n");
  339. printf(commodities[i].name);
  340. printf(" %.1f",((float)(m.price[i])/10));
  341. printf(" %u",m.quantity[i]);
  342. printf(unitnames[commodities[i].units]);
  343. printf(" %u",shipshold[i]);
  344. }
  345. }
  346. /**-Generate system info from seed **/
  347. plansys makesystem(seedtype *s)
  348. { plansys thissys;
  349. uint pair1,pair2,pair3,pair4;
  350. uint16 longnameflag=((*s).w0)&64;
  351. thissys.x=(((*s).w1)>>8);
  352. thissys.y=(((*s).w0)>>8);
  353. thissys.govtype =((((*s).w1)>>3)&7); /* bits 3,4 &5 of w1 */
  354. thissys.economy =((((*s).w0)>>8)&7); /* bits 8,9 &A of w0 */
  355. if (thissys.govtype <=1)
  356. { thissys.economy = ((thissys.economy)|2);
  357. }
  358. thissys.techlev =((((*s).w1)>>8)&3)+((thissys.economy)^7);
  359. thissys.techlev +=((thissys.govtype)>>1);
  360. if (((thissys.govtype)&1)==1) thissys.techlev+=1;
  361. /* C simulation of 6502's LSR then ADC */
  362. thissys.population = 4*(thissys.techlev) + (thissys.economy);
  363. thissys.population += (thissys.govtype) + 1;
  364. thissys.productivity = (((thissys.economy)^7)+3)*((thissys.govtype)+4);
  365. thissys.productivity *= (thissys.population)*8;
  366. thissys.radius = 256*(((((*s).w2)>>8)&15)+11) + thissys.x;
  367. thissys.goatsoupseed.a = (*s).w1 & 0xFF;;
  368. thissys.goatsoupseed.b = (*s).w1 >>8;
  369. thissys.goatsoupseed.c = (*s).w2 & 0xFF;
  370. thissys.goatsoupseed.d = (*s).w2 >> 8;
  371. pair1=2*((((*s).w2)>>8)&31); tweakseed(s);
  372. pair2=2*((((*s).w2)>>8)&31); tweakseed(s);
  373. pair3=2*((((*s).w2)>>8)&31); tweakseed(s);
  374. pair4=2*((((*s).w2)>>8)&31); tweakseed(s);
  375. /* Always four iterations of random number */
  376. (thissys.name)[0]=pairs[pair1];
  377. (thissys.name)[1]=pairs[pair1+1];
  378. (thissys.name)[2]=pairs[pair2];
  379. (thissys.name)[3]=pairs[pair2+1];
  380. (thissys.name)[4]=pairs[pair3];
  381. (thissys.name)[5]=pairs[pair3+1];
  382. if(longnameflag) /* bit 6 of ORIGINAL w0 flags a four-pair name */
  383. {
  384. (thissys.name)[6]=pairs[pair4];
  385. (thissys.name)[7]=pairs[pair4+1];
  386. (thissys.name)[8]=0;
  387. }
  388. else (thissys.name)[6]=0;
  389. stripout(thissys.name,'.');
  390. return thissys;
  391. }
  392. /**+Generate galaxy **/
  393. /* Functions for galactic hyperspace */
  394. uint16 rotatel(uint16 x) /* rotate 8 bit number leftwards */
  395. /* (tried to use chars but too much effort persuading this braindead
  396. language to do bit operations on bytes!) */
  397. { uint16 temp = x&128;
  398. return (2*(x&127))+(temp>>7);
  399. }
  400. uint16 twist(uint16 x)
  401. { return (uint16)((256*rotatel(x>>8))+rotatel(x&255));
  402. }
  403. void nextgalaxy(seedtype *s) /* Apply to base seed; once for galaxy 2 */
  404. { (*s).w0 = twist((*s).w0); /* twice for galaxy 3, etc. */
  405. (*s).w1 = twist((*s).w1); /* Eighth application gives galaxy 1 again*/
  406. (*s).w2 = twist((*s).w2);
  407. }
  408. /* Original game generated from scratch each time info needed */
  409. void buildgalaxy(uint galaxynum)
  410. { uint syscount,galcount;
  411. seed.w0=base0; seed.w1=base1; seed.w2=base2; /* Initialise seed for galaxy 1 */
  412. for(galcount=1;galcount<galaxynum;++galcount) nextgalaxy(&seed);
  413. /* Put galaxy data into array of structures */
  414. for(syscount=0;syscount<galsize;++syscount) galaxy[syscount]=makesystem(&seed);
  415. }
  416. /**-Functions for navigation **/
  417. void gamejump(planetnum i) /* Move to system i */
  418. { currentplanet=i;
  419. localmarket = genmarket(randbyte(),galaxy[i]);
  420. }
  421. uint distance(plansys a,plansys b)
  422. /* Seperation between two planets (4*sqrt(X*X+Y*Y/4)) */
  423. { return (uint)ftoi(4*sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)/4));
  424. }
  425. planetnum matchsys(char *s)
  426. /* Return id of the planet whose name matches passed strinmg
  427. closest to currentplanet - if none return currentplanet */
  428. { planetnum syscount;
  429. planetnum p=currentplanet;
  430. uint d=9999;
  431. for(syscount=0;syscount<galsize;++syscount)
  432. { if (stringbeg(s,galaxy[syscount].name))
  433. { if (distance(galaxy[syscount],galaxy[currentplanet])<d)
  434. { d=distance(galaxy[syscount],galaxy[currentplanet]);
  435. p=syscount;
  436. }
  437. }
  438. }
  439. return p;
  440. }
  441. /**-Print data for given system **/
  442. void prisys(plansys plsy,boolean compressed)
  443. { if (compressed)
  444. {
  445. // printf("\n ");
  446. printf("%10s",plsy.name);
  447. printf(" TL: %2i ",(plsy.techlev)+1);
  448. printf("%12s",econnames[plsy.economy]);
  449. printf(" %15s",govnames[plsy.govtype]);
  450. }
  451. else
  452. { printf("\n\nSystem: ");
  453. printf(plsy.name);
  454. printf("\nPosition (%i,",plsy.x);
  455. printf("%i)",plsy.y);
  456. printf("\nEconomy: (%i) ",plsy.economy);
  457. printf(econnames[plsy.economy]);
  458. printf("\nGovernment: (%i) ",plsy.govtype);
  459. printf(govnames[plsy.govtype]);
  460. printf("\nTech Level: %2i",(plsy.techlev)+1);
  461. printf("\nTurnover: %u",(plsy.productivity));
  462. printf("\nRadius: %u",plsy.radius);
  463. printf("\nPopulation: %u Billion",(plsy.population)>>3);
  464. rnd_seed = plsy.goatsoupseed;
  465. printf("\n");goat_soup("\x8F is \x97.",&plsy);
  466. }
  467. }
  468. /**-Various command functions **/
  469. boolean dotweakrand(char * s)
  470. { (void *)&s;
  471. nativerand ^=1;
  472. return true;
  473. }
  474. boolean dolocal(char *s)
  475. { planetnum syscount;
  476. uint d;
  477. atoi(s);
  478. printf("Galaxy number %i",galaxynum);
  479. for(syscount=0;syscount<galsize;++syscount)
  480. { d=distance(galaxy[syscount],galaxy[currentplanet]);
  481. if(d<=maxfuel)
  482. { if(d<=fuel) printf("\n * "); else printf("\n - ");
  483. prisys(galaxy[syscount],true);
  484. printf(" (%.1f LY)",(float)d/10);
  485. }
  486. }
  487. return true;
  488. }
  489. boolean dojump(char *s) /* Jump to planet name s */
  490. { uint d;
  491. planetnum dest=matchsys(s);
  492. if(dest==currentplanet) { printf("\nBad jump"); return false; }
  493. d=distance(galaxy[dest],galaxy[currentplanet]);
  494. if (d>fuel) { printf("\nJump to far"); return false; }
  495. fuel-=d;
  496. gamejump(dest);
  497. prisys(galaxy[currentplanet],false);
  498. return true;
  499. }
  500. boolean dosneak(char *s) /* As dojump but no fuel cost */
  501. { uint fuelkeep=fuel;
  502. boolean b;
  503. fuel=666;
  504. b=dojump(s);
  505. fuel=fuelkeep;
  506. return b;
  507. }
  508. boolean dogalhyp(char *s) /* Jump to next galaxy */
  509. /* Preserve planetnum (eg. if leave 7th planet
  510. arrive at 7th planet)
  511. Classic Elite always jumped to planet nearest (0x60,0x60)
  512. */
  513. { (void)(&s); /* Discard s */
  514. galaxynum++;
  515. if(galaxynum==9) {galaxynum=1;}
  516. buildgalaxy(galaxynum);
  517. return true;
  518. }
  519. boolean doinfo(char *s) /* Info on planet */
  520. { planetnum dest=matchsys(s);
  521. prisys(galaxy[dest],false);
  522. return true;
  523. }
  524. boolean dohold(char *s)
  525. { uint a=(uint)atoi(s),t=0,i;
  526. for(i=0;i<=lasttrade;++i)
  527. { if ((commodities[i].units)==tonnes) t+=shipshold[i];
  528. }
  529. if(t>a) {printf("\nHold too full"); return false;}
  530. holdspace=a-t;
  531. return true;
  532. }
  533. boolean dosell(char *s) /* Sell ammount S(2) of good S(1) */
  534. { uint i,a,t;
  535. char s2[maxlen];
  536. spacesplit(s,s2);
  537. a=(uint)atoi(s);
  538. if (a==0) {a=1;}
  539. i=stringmatch(s2,tradnames,lasttrade+1);
  540. if(i==0) { printf("\nUnknown trade good"); return false; }
  541. i-=1;
  542. t=gamesell(i,a);
  543. if(t==0) { printf("Cannot sell any "); }
  544. else
  545. { printf("\nSelling %i",t);
  546. printf(unitnames[commodities[i].units]);
  547. printf(" of ");
  548. }
  549. printf(tradnames[i]);
  550. return true;
  551. }
  552. boolean dobuy(char *s) /* Buy ammount S(2) of good S(1) */
  553. { uint i,a,t;
  554. char s2[maxlen];
  555. spacesplit(s,s2);
  556. a=(uint)atoi(s);
  557. if (a==0) a=1;
  558. i=stringmatch(s2,tradnames,lasttrade+1);
  559. if(i==0) { printf("\nUnknown trade good"); return false; }
  560. i-=1;
  561. t=gamebuy(i,a);
  562. if(t==0) printf("Cannot buy any ");
  563. else
  564. { printf("\nBuying %i",t);
  565. printf(unitnames[commodities[i].units]);
  566. printf(" of ");
  567. }
  568. printf(tradnames[i]);
  569. return true;
  570. }
  571. uint gamefuel(uint f) /* Attempt to buy f tonnes of fuel */
  572. { if(f+fuel>maxfuel) f=maxfuel-fuel;
  573. if(fuelcost>0)
  574. { if((int)f*fuelcost>cash) f=(uint)(cash/fuelcost);
  575. }
  576. fuel+=f;
  577. cash-=fuelcost*f;
  578. return f;
  579. }
  580. boolean dofuel(char *s)
  581. /* Buy ammount S of fuel */
  582. { uint f=gamefuel((uint)floor(10*atof(s)));
  583. if(f==0) { printf("\nCan't buy any fuel");}
  584. printf("\nBuying %.1fLY fuel",(float)f/10);
  585. return true;
  586. }
  587. boolean docash(char *s) /* Cheat alter cash by S */
  588. { int a=(int)(10*atof(s));
  589. cash+=(long)a;
  590. if(a!=0) return true;
  591. printf("Number not understood");
  592. return false;
  593. }
  594. boolean domkt(char *s) /* Show stock market */
  595. { atoi(s);
  596. displaymarket(localmarket);
  597. printf("\nFuel :%.1f",(float)fuel/10);
  598. printf(" Holdspace :%it",holdspace);
  599. return true;
  600. }
  601. boolean parser(char *s) /* Obey command s */
  602. { uint i;
  603. char c[maxlen];
  604. spacesplit(s,c);
  605. i=stringmatch(c,commands,nocomms);
  606. if(i)return (*comfuncs[i-1])(s) ;
  607. printf("\n Bad command (");
  608. printf(c);
  609. printf(")");
  610. return false;
  611. }
  612. boolean doquit(char *s)
  613. { (void)(&s);
  614. exit(0);
  615. return(0);
  616. }
  617. boolean dohelp(char *s)
  618. {
  619. (void)(&s);
  620. printf("\nCommands are:");
  621. printf("\nBuy tradegood ammount");
  622. printf("\nSell tradegood ammount");
  623. printf("\nFuel ammount (buy ammount LY of fuel)");
  624. printf("\nJump planetname (limited by fuel)");
  625. printf("\nSneak planetname (any distance - no fuel cost)");
  626. printf("\nGalhyp (jumps to next galaxy)");
  627. printf("\nInfo planetname (prints info on system");
  628. printf("\nMkt (shows market prices)");
  629. printf("\nLocal (lists systems within 7 light years)");
  630. printf("\nCash number (alters cash - cheating!)");
  631. printf("\nHold number (change cargo bay)");
  632. printf("\nQuit or ^C (exit)");
  633. printf("\nHelp (display this text)");
  634. printf("\nRand (toggle RNG)");
  635. printf("\n\nAbbreviations allowed eg. b fo 5 = Buy Food 5, m= Mkt");
  636. return true;
  637. }
  638. /**+main **/
  639. int main()
  640. { uint i;
  641. char getcommand[maxlen];
  642. nativerand=1;
  643. printf("\nWelcome to Text Elite 1.5.\n");
  644. for(i=0;i<=lasttrade;i++) strcpy(tradnames[i],commodities[i].name);
  645. mysrand(12345);/* Ensure repeatability */
  646. galaxynum=1; buildgalaxy(galaxynum);
  647. currentplanet=numforLave; /* Don't use jump */
  648. localmarket = genmarket(0x00,galaxy[numforLave]);/* Since want seed=0 */
  649. fuel=maxfuel;
  650. #define PARSER(S) { char buf[0x10];strcpy(buf,S);parser(buf);}
  651. PARSER("hold 20"); /* Small cargo bay */
  652. PARSER("cash +100"); /* 100 CR */
  653. PARSER("help");
  654. #undef PARSER
  655. for(;;)
  656. { printf("\n\nCash :%.1f>",((float)cash)/10);
  657. gets(getcommand);
  658. parser(getcommand);
  659. }
  660. /* Unreachable */
  661. /* 6502 Elite fires up at Lave with fluctuation=00
  662. and these prices tally with the NES ones.
  663. However, the availabilities reside in the saved game data.
  664. Availabilities are calculated (and fluctuation randomised)
  665. on hyperspacing
  666. I have checked with this code for Zaonce with fluctaution &AB
  667. against the SuperVision 6502 code and both prices and availabilities tally.
  668. */
  669. return(0);
  670. }
  671. /* "Goat Soup" planetary description string code - adapted from Christian Pinder's
  672. reverse engineered sources. */
  673. struct desc_choice { const char *option[5];};
  674. static struct desc_choice desc_list[] =
  675. {
  676. /* 81 */ {"fabled", "notable", "well known", "famous", "noted"},
  677. /* 82 */ {"very", "mildly", "most", "reasonably", ""},
  678. /* 83 */ {"ancient", "\x95", "great", "vast", "pink"},
  679. /* 84 */ {"\x9E \x9D plantations", "mountains", "\x9C", "\x94 forests", "oceans"},
  680. /* 85 */ {"shyness", "silliness", "mating traditions", "loathing of \x86", "love for \x86"},
  681. /* 86 */ {"food blenders", "tourists", "poetry", "discos", "\x8E"},
  682. /* 87 */ {"talking tree", "crab", "bat", "lobst", "\xB2"},
  683. /* 88 */ {"beset", "plagued", "ravaged", "cursed", "scourged"},
  684. /* 89 */ {"\x96 civil war", "\x9B \x98 \x99s", "a \x9B disease", "\x96 earthquakes", "\x96 solar activity"},
  685. /* 8A */ {"its \x83 \x84", "the \xB1 \x98 \x99","its inhabitants' \x9A \x85", "\xA1", "its \x8D \x8E"},
  686. /* 8B */ {"juice", "brandy", "water", "brew", "gargle blasters"},
  687. /* 8C */ {"\xB2", "\xB1 \x99", "\xB1 \xB2", "\xB1 \x9B", "\x9B \xB2"},
  688. /* 8D */ {"fabulous", "exotic", "hoopy", "unusual", "exciting"},
  689. /* 8E */ {"cuisine", "night life", "casinos", "sit coms", " \xA1 "},
  690. /* 8F */ {"\xB0", "The planet \xB0", "The world \xB0", "This planet", "This world"},
  691. /* 90 */ {"n unremarkable", " boring", " dull", " tedious", " revolting"},
  692. /* 91 */ {"planet", "world", "place", "little planet", "dump"},
  693. /* 92 */ {"wasp", "moth", "grub", "ant", "\xB2"},
  694. /* 93 */ {"poet", "arts graduate", "yak", "snail", "slug"},
  695. /* 94 */ {"tropical", "dense", "rain", "impenetrable", "exuberant"},
  696. /* 95 */ {"funny", "wierd", "unusual", "strange", "peculiar"},
  697. /* 96 */ {"frequent", "occasional", "unpredictable", "dreadful", "deadly"},
  698. /* 97 */ {"\x82 \x81 for \x8A", "\x82 \x81 for \x8A and \x8A", "\x88 by \x89", "\x82 \x81 for \x8A but \x88 by \x89","a\x90 \x91"},
  699. /* 98 */ {"\x9B", "mountain", "edible", "tree", "spotted"},
  700. /* 99 */ {"\x9F", "\xA0", "\x87oid", "\x93", "\x92"},
  701. /* 9A */ {"ancient", "exceptional", "eccentric", "ingrained", "\x95"},
  702. /* 9B */ {"killer", "deadly", "evil", "lethal", "vicious"},
  703. /* 9C */ {"parking meters", "dust clouds", "ice bergs", "rock formations", "volcanoes"},
  704. /* 9D */ {"plant", "tulip", "banana", "corn", "\xB2weed"},
  705. /* 9E */ {"\xB2", "\xB1 \xB2", "\xB1 \x9B", "inhabitant", "\xB1 \xB2"},
  706. /* 9F */ {"shrew", "beast", "bison", "snake", "wolf"},
  707. /* A0 */ {"leopard", "cat", "monkey", "goat", "fish"},
  708. /* A1 */ {"\x8C \x8B", "\xB1 \x9F \xA2","its \x8D \xA0 \xA2", "\xA3 \xA4", "\x8C \x8B"},
  709. /* A2 */ {"meat", "cutlet", "steak", "burgers", "soup"},
  710. /* A3 */ {"ice", "mud", "Zero-G", "vacuum", "\xB1 ultra"},
  711. /* A4 */ {"hockey", "cricket", "karate", "polo", "tennis"}
  712. };
  713. /* B0 = <planet name>
  714. B1 = <planet name>ian
  715. B2 = <random name>
  716. */
  717. int gen_rnd_number (void)
  718. { int a,x;
  719. x = (rnd_seed.a * 2) & 0xFF;
  720. a = x + rnd_seed.c;
  721. if (rnd_seed.a > 127) a++;
  722. rnd_seed.a = a & 0xFF;
  723. rnd_seed.c = x;
  724. a = a / 256; /* a = any carry left from above */
  725. x = rnd_seed.b;
  726. a = (a + x + rnd_seed.d) & 0xFF;
  727. rnd_seed.b = a;
  728. rnd_seed.d = x;
  729. return a;
  730. }
  731. void goat_soup(const char *source,plansys * psy)
  732. { for(;;)
  733. { int c=*(source++);
  734. if(c=='\0') break;
  735. if(c<0x80) printf("%c",c);
  736. else
  737. { if (c <=0xA4)
  738. { int rnd = gen_rnd_number();
  739. goat_soup(desc_list[c-0x81].option[(rnd >= 0x33)+(rnd >= 0x66)+(rnd >= 0x99)+(rnd >= 0xCC)],psy);
  740. }
  741. else switch(c)
  742. { case 0xB0: /* planet name */
  743. { int i=1;
  744. printf("%c",psy->name[0]);
  745. while(psy->name[i]!='\0') printf("%c",tolower(psy->name[i++]));
  746. } break;
  747. case 0xB1: /* <planet name>ian */
  748. { int i=1;
  749. printf("%c",psy->name[0]);
  750. while(psy->name[i]!='\0')
  751. { if((psy->name[i+1]!='\0') || ((psy->name[i]!='E') && (psy->name[i]!='I')))
  752. printf("%c",tolower(psy->name[i]));
  753. i++;
  754. }
  755. printf("ian");
  756. } break;
  757. case 0xB2: /* random name */
  758. #if 1 // 1.5
  759. {
  760. int i;
  761. int len = gen_rnd_number() & 3;
  762. for (i = 0; i <= len; i++)
  763. {
  764. int x = gen_rnd_number() & 0x3e;
  765. if (i == 0)
  766. {
  767. printf("%c",pairs0[x]);
  768. }
  769. else
  770. {
  771. printf("%c",tolower(pairs0[x]));
  772. }
  773. printf("%c",tolower(pairs0[x+1]));
  774. } // endfor
  775. }
  776. #else // 1.4-
  777. { int i;
  778. int len = gen_rnd_number() & 3;
  779. for(i=0;i<=len;i++)
  780. { int x = gen_rnd_number() & 0x3e;
  781. if(pairs0[x]!='.') printf("%c",pairs0[x]);
  782. if(i && (pairs0[x+1]!='.')) printf("%c",pairs0[x+1]);
  783. }
  784. }
  785. #endif
  786. break;
  787. default: printf("<bad char in data [%X]>",c); return;
  788. } /* endswitch */
  789. } /* endelse */
  790. } /* endwhile */
  791. } /* endfunc */
  792. /**+end **/