chiark / gitweb /
Buttload of hacking.
[newkind] / planet.c
1 /*
2  * Elite - The New Kind.
3  *
4  * Reverse engineered from the BBC disk version of Elite.
5  * Additional material by C.J.Pinder.
6  *
7  * The original Elite code is (C) I.Bell & D.Braben 1984.
8  * This version re-engineered in C by C.J.Pinder 1999-2001.
9  *
10  * email: <christian@newkind.co.uk>
11  *
12  */
13
14 /*
15  *
16  * Handle the generation of planet info...
17  */
18
19 #include <ctype.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23
24 #include "config.h"
25 #include "gfx.h"
26 #include "elite.h"
27 #include "planet.h"
28 #include "missions.h"
29
30
31 extern struct galaxy_seed hyperspace_planet;
32
33 struct random_seed
34 {
35         int a;
36         int b;
37         int c;
38         int d;
39 };
40
41
42 static struct random_seed rnd_seed;
43
44 static char *digrams="ABOUSEITILETSTONLONUTHNOALLEXEGEZACEBISOUSESARMAINDIREA?ERATENBERALAVETIEDORQUANTEISRION";
45
46 static char *inhabitant_desc1[] = {"Large ", "Fierce ", "Small "};
47
48 static char *inhabitant_desc2[] = {"Green ", "Red ", "Yellow ", "Blue ", "Black ", "Harmless "};
49
50 static char *inhabitant_desc3[] = {"Slimy ", "Bug-Eyed ", "Horned ", "Bony ", "Fat ", "Furry "};
51
52 static char *inhabitant_desc4[] = {"Rodent", "Frog", "Lizard", "Lobster", "Bird", "Humanoid", "Feline", "Insect"};
53
54
55 static char planet_description[300];
56 static char *desc_ptr;
57
58
59
60
61 static char *desc_list[36][5] =
62 {
63 /*  0   */      {"fabled", "notable", "well known", "famous", "noted"},
64 /*  1   */      {"very", "mildly", "most", "reasonably", ""},
65 /*  2   */      {"ancient", "<20>", "great", "vast", "pink"},
66 /*  3   */      {"<29> <28> plantations", "mountains", "<27>", "<19> forests", "oceans"},
67 /*  4   */      {"shyness", "silliness", "mating traditions", "loathing of <5>", "love for <5>"},
68 /*  5   */      {"food blenders", "tourists", "poetry", "discos", "<13>"},
69 /*  6   */      {"talking tree", "crab", "bat", "lobst", "%R"},
70 /*  7   */      {"beset", "plagued", "ravaged", "cursed", "scourged"},
71 /*  8   */      {"<21> civil war", "<26> <23> <24>s", "a <26> disease", "<21> earthquakes", "<21> solar activity"},
72 /*  9   */      {"its <2> <3>", "the %I <23> <24>","its inhabitants' <25> <4>", "<32>", "its <12> <13>"},
73 /* 10   */      {"juice", "brandy", "water", "brew", "gargle blasters"},
74 /* 11   */      {"%R", "%I <24>", "%I %R", "%I <26>", "<26> %R"},
75 /* 12   */      {"fabulous", "exotic", "hoopy", "unusual", "exciting"},
76 /* 13   */      {"cuisine", "night life", "casinos", "sit coms", " <32> "},
77 /* 14   */      {"%H", "The planet %H", "The world %H", "This planet", "This world"},
78 /* 15   */      {"n unremarkable", " boring", " dull", " tedious", " revolting"},
79 /* 16   */      {"planet", "world", "place", "little planet", "dump"},
80 /* 17   */      {"wasp", "moth", "grub", "ant", "%R"},
81 /* 18   */      {"poet", "arts graduate", "yak", "snail", "slug"},
82 /* 19   */      {"tropical", "dense", "rain", "impenetrable", "exuberant"},
83 /* 20   */      {"funny", "wierd", "unusual", "strange", "peculiar"},
84 /* 21   */      {"frequent", "occasional", "unpredictable", "dreadful", "deadly"},
85 /* 22   */      {"<1> <0> for <9>", "<1> <0> for <9> and <9>", "<7> by <8>", "<1> <0> for <9> but <7> by <8>"," a<15> <16>"},
86 /* 23   */      {"<26>", "mountain", "edible", "tree", "spotted"},
87 /* 24   */      {"<30>", "<31>", "<6>oid", "<18>", "<17>"},
88 /* 25   */      {"ancient", "exceptional", "eccentric", "ingrained", "<20>"},
89 /* 26   */      {"killer", "deadly", "evil", "lethal", "vicious"},
90 /* 27   */      {"parking meters", "dust clouds", "ice bergs", "rock formations", "volcanoes"},
91 /* 28   */      {"plant", "tulip", "banana", "corn", "%Rweed"},
92 /* 29   */      {"%R", "%I %R", "%I <26>", "inhabitant", "%I %R"},
93 /* 30   */      {"shrew", "beast", "bison", "snake", "wolf"},
94 /* 31   */      {"leopard", "cat", "monkey", "goat", "fish"},
95 /* 32   */      {"<11> <10>", "%I <30> <33>","its <12> <31> <33>", "<34> <35>", "<11> <10>"},
96 /* 33   */      {"meat", "cutlet", "steak", "burgers", "soup"},
97 /* 34   */      {"ice", "mud", "Zero-G", "vacuum", "%I ultra"},
98 /* 35   */      {"hockey", "cricket", "karate", "polo", "tennis"}
99 };
100
101
102
103
104
105
106
107
108 /*
109  * Generate a random number between 0 and 255.
110  * This is the version used in the 6502 Elites.
111  */
112
113 int gen_rnd_number (void)
114 {
115         int a,x;
116
117         x = (rnd_seed.a * 2) & 0xFF;
118         a = x + rnd_seed.c;
119         if (rnd_seed.a > 127)
120                 a++;
121         rnd_seed.a = a & 0xFF;
122         rnd_seed.c = x;
123
124         a = a / 256;    /* a = any carry left from above */
125         x = rnd_seed.b;
126         a = (a + x + rnd_seed.d) & 0xFF;
127         rnd_seed.b = a;
128         rnd_seed.d = x;
129         return a;
130 }
131
132
133 /*
134  * Generate a random number between 0 and 255.
135  * This is the version used in the MSX and 16bit Elites.
136  */
137
138
139 int gen_msx_rnd_number (void)
140 {
141     int a,b;
142
143         a = rnd_seed.a;
144         b = rnd_seed.b;
145         
146         rnd_seed.a = rnd_seed.c;
147         rnd_seed.b = rnd_seed.d;
148         
149         a += rnd_seed.c;
150         b = (b + rnd_seed.d) & 255;
151         if (a > 255)
152         {
153                 a &= 255;
154                 b++;
155         }
156         
157         rnd_seed.c = a;
158         rnd_seed.d = b;
159         
160         return rnd_seed.c / 0x34;
161 }
162
163
164 void waggle_galaxy (struct galaxy_seed *glx_ptr)
165 {
166     unsigned int x;
167         unsigned int y;
168         extern int carry_flag;
169
170         x = glx_ptr->a + glx_ptr->c;
171     y = glx_ptr->b + glx_ptr->d;
172
173
174         if (x > 0xFF)
175             y++;
176
177         x &= 0xFF;
178         y &= 0xFF;
179
180         glx_ptr->a = glx_ptr->c;
181         glx_ptr->b = glx_ptr->d;
182         glx_ptr->c = glx_ptr->e;
183         glx_ptr->d = glx_ptr->f;
184
185     x += glx_ptr->c;
186         y += glx_ptr->d;
187
188
189         if (x > 0xFF)
190                 y++;
191
192         if (y > 0xFF)
193                 carry_flag = 1;
194         else
195                 carry_flag = 0;
196
197     x &= 0xFF;
198         y &= 0xFF;
199
200         glx_ptr->e = x;
201         glx_ptr->f = y;
202 }
203
204
205
206
207 struct galaxy_seed find_planet (int cx, int cy)
208 {
209     int min_dist = 10000;
210         struct galaxy_seed glx;
211         struct galaxy_seed planet;
212         int distance;
213         int dx, dy;
214         int i;
215
216         glx = cmdr.galaxy;
217
218         for (i = 0; i < 256; i++)
219         {
220
221                 dx = abs(cx - glx.d);
222                 dy = abs(cy - glx.b);
223
224                 if (dx > dy)
225                         distance = (dx + dx + dy) / 2;
226                 else
227                         distance = (dx + dy + dy) / 2;
228
229                 if (distance < min_dist)
230                 {
231                         min_dist = distance;
232                         planet = glx;
233                 }
234
235                 waggle_galaxy (&glx);
236                 waggle_galaxy (&glx);
237                 waggle_galaxy (&glx);
238                 waggle_galaxy (&glx);
239         }
240
241         return planet;
242 }
243
244
245 int find_planet_number (struct galaxy_seed planet)
246 {
247         struct galaxy_seed glx;
248         int i;
249
250         glx = cmdr.galaxy;
251
252         for (i = 0; i < 256; i++)
253         {
254
255                 if ((planet.a == glx.a) &&
256                         (planet.b == glx.b) &&
257                         (planet.c == glx.c) &&
258                         (planet.d == glx.d) &&
259                         (planet.e == glx.e) &&
260                         (planet.f == glx.f))
261                         return i;
262         
263                 waggle_galaxy (&glx);
264                 waggle_galaxy (&glx);
265                 waggle_galaxy (&glx);
266                 waggle_galaxy (&glx);
267         }
268
269         return -1;
270 }
271
272
273
274 void name_planet (char *gname, struct galaxy_seed glx)
275 {
276     int size;
277         int i;
278         char *gp;
279         unsigned int x;
280
281
282         gp = gname;
283
284         if ((glx.a & 0x40) == 0)
285                 size = 3;
286         else
287                 size = 4;
288
289         for (i = 0; i < size; i++)
290         {
291                 x = glx.f & 0x1F;
292                 if (x != 0)
293                 {
294                         x += 12;
295                         x *= 2;
296                         *gp++ = digrams[x];
297                         if (digrams[x+1] != '?')
298                                 *gp++ = digrams[x+1];
299                 }
300
301                 waggle_galaxy (&glx);
302         }
303
304         *gp = '\0';
305 }
306
307
308 void capitalise_name (char *name)
309 {
310     char *ptr = name;
311
312         if (*ptr == '\0')
313                 return;
314
315         *ptr = toupper(*ptr);
316         ptr++;
317
318         while (*ptr != '\0')
319         {
320             *ptr = tolower(*ptr);
321                 ptr++;
322         }
323 }
324
325
326 void describe_inhabitants (char *str, struct galaxy_seed planet)
327 {
328         int inhab;
329
330         strcpy (str, "(");
331
332         if (planet.e < 128)
333         {
334                 strcat (str, "Human Colonial");
335         }
336         else
337         {
338                 inhab = (planet.f / 4) & 7;
339                 if (inhab < 3)
340                         strcat (str, inhabitant_desc1[inhab]);
341
342                 inhab = planet.f / 32;
343                 if (inhab < 6)
344                         strcat (str, inhabitant_desc2[inhab]);
345
346                 inhab = (planet.d ^ planet.b) & 7;
347                 if (inhab < 6)
348                         strcat (str, inhabitant_desc3[inhab]);
349
350                 inhab = (inhab + (planet.f & 3)) & 7;
351                 strcat (str, inhabitant_desc4[inhab]);
352         }
353
354         strcat (str, "s)");
355 }
356
357
358
359 void expand_description (char *source)
360 {
361         char str[32];
362         char *ptr;
363         int num;
364         int rnd;
365         int option;
366         int i, len, x;
367
368         while (*source != '\0')
369         {
370                 if (*source == '<')
371                 {
372                         source++;
373                         ptr = str;
374                         while (*source != '>')
375                                 *ptr++ = *source++;
376                         *ptr = '\0';
377                         source++;
378                         num = atoi(str);
379                         
380                         if (hoopy_casinos)
381                         {
382                                 option = gen_msx_rnd_number();
383                         }
384                         else
385                         {
386                                 rnd = gen_rnd_number();
387                                 option = 0;
388                                 if (rnd >= 0x33) option++;
389                                 if (rnd >= 0x66) option++;
390                                 if (rnd >= 0x99) option++;
391                                 if (rnd >= 0xCC) option++;
392                         }
393                         
394                         expand_description (desc_list[num][option]);
395                         continue;
396                 }
397
398                 if (*source == '%')
399                 {
400                         source++;
401                         switch (*source)
402                         {
403                                 case 'H':
404                                         name_planet (str, hyperspace_planet);
405                                         capitalise_name (str);
406                                         for (ptr = str; *ptr != '\0';)
407                                                 *desc_ptr++ = *ptr++;
408                                         break;
409
410                                 case 'I':
411                                         name_planet (str, hyperspace_planet);
412                                         capitalise_name (str);
413                                         for (ptr = str; *ptr != '\0';)
414                                                 *desc_ptr++ = *ptr++;
415                                                 desc_ptr--;
416                                         strcpy (desc_ptr, "ian");
417                                         desc_ptr += 3;
418                                         break;
419
420                                 case 'R':
421                                         len = gen_rnd_number() & 3;
422                                         for (i = 0; i <= len; i++)
423                                         {
424                                                 x = gen_rnd_number() & 0x3e;
425                                                 if (i == 0)
426                                                     *desc_ptr++ = digrams[x];
427                                                 else
428                                                         *desc_ptr++ = tolower(digrams[x]);
429                                                 *desc_ptr++ = tolower(digrams[x+1]);
430                                         }
431
432                         }
433
434                         source++;
435                         continue;
436                 }
437
438                 *desc_ptr++ = *source++;
439         }
440
441
442
443         *desc_ptr = '\0';
444 }
445
446
447
448 char *describe_planet (struct galaxy_seed planet)
449 {
450         char *mission_text;
451         
452         if (cmdr.mission == 1)
453         {
454                 mission_text = mission_planet_desc (planet);
455                 if (mission_text != NULL)
456                         return mission_text;
457         }
458         
459         rnd_seed.a = planet.c;
460         rnd_seed.b = planet.d;
461         rnd_seed.c = planet.e;
462         rnd_seed.d = planet.f;
463
464         if (hoopy_casinos)
465         {
466                 rnd_seed.a ^= planet.a;
467                 rnd_seed.b ^= planet.b;
468                 rnd_seed.c ^= rnd_seed.a;
469                 rnd_seed.d ^= rnd_seed.b;
470         }
471         
472         desc_ptr = planet_description;
473
474         expand_description ("<14> is <22>.");
475
476         return planet_description;
477 }
478
479
480
481 void generate_planet_data (struct planet_data *pl, struct galaxy_seed planet_seed)
482 {
483
484         pl->government = (planet_seed.c / 8) & 7;
485
486         pl->economy = planet_seed.b & 7;
487
488         if (pl->government < 2)
489                 pl->economy = pl->economy | 2;
490
491         pl->techlevel = pl->economy ^ 7;
492         pl->techlevel += planet_seed.d & 3;
493         pl->techlevel += (pl->government / 2) + (pl->government & 1);
494
495
496         pl->population = pl->techlevel * 4;
497         pl->population += pl->government;
498         pl->population += pl->economy;
499         pl->population++;
500
501         pl->productivity = (pl->economy ^ 7) + 3;
502         pl->productivity *= pl->government + 4;
503         pl->productivity *= pl->population;
504         pl->productivity *= 8;
505
506         pl->radius = (((planet_seed.f & 15) + 11) * 256) + planet_seed.d;
507 }
508
509
510