ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/recipe.C
(Generate patch)

Comparing deliantra/server/common/recipe.C (file contents):
Revision 1.6 by root, Thu Sep 14 21:16:11 2006 UTC vs.
Revision 1.19 by root, Mon Apr 16 06:23:40 2007 UTC

1
2
3/* Basic stuff for use with the alchemy code. Clearly some of this stuff 1/* Basic stuff for use with the alchemy code. Clearly some of this stuff
4 * could go into server/alchemy, but I left it here just in case it proves 2 * could go into server/alchemy, but I left it here just in case it proves
5 * more generally useful. 3 * more generally useful.
6 * 4 *
7 * Nov 1995 - file created by b.t. thomas@astro.psu.edu 5 * Nov 1995 - file created by b.t. thomas@astro.psu.edu
12 * Ingredients are just comma delimited list of archetype (or object) 10 * Ingredients are just comma delimited list of archetype (or object)
13 * names. 11 * names.
14 */ 12 */
15 13
16/* Example 'formula' entry in libdir/formulae: 14/* Example 'formula' entry in libdir/formulae:
17 * Object transparency 15 * object transparency
18 * chance 10 16 * chance 10
19 * ingred dust of beholdereye,gem 17 * ingred dust of beholdereye,gem
20 * arch potion_generic 18 * arch potion_generic
21 */ 19 */
22 20
21#include <cctype>
22
23#include <global.h> 23#include <global.h>
24#include <object.h> 24#include <object.h>
25#include <ctype.h>
26 25
27static void build_stringlist (const char *str, char ***result_list, size_t * result_size); 26static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
28 27
29static recipelist *formulalist; 28static recipelist *formulalist;
30 29
33{ 32{
34 recipelist *tl = new recipelist; 33 recipelist *tl = new recipelist;
35 34
36 tl->total_chance = 0; 35 tl->total_chance = 0;
37 tl->number = 0; 36 tl->number = 0;
38 tl->items = NULL; 37 tl->items = 0;
39 tl->next = NULL; 38 tl->next = 0;
39
40 return tl; 40 return tl;
41} 41}
42 42
43static recipe * 43static recipe *
44get_empty_formula (void) 44get_empty_formula (void)
61 t->next = NULL; 61 t->next = NULL;
62 return t; 62 return t;
63} 63}
64 64
65/* get_formulalist() - returns pointer to the formula list */ 65/* get_formulalist() - returns pointer to the formula list */
66
67recipelist * 66recipelist *
68get_formulalist (int i) 67get_formulalist (int i)
69{ 68{
70 recipelist *fl = formulalist; 69 recipelist *fl = formulalist;
71 int number = i; 70 int number = i;
74 { 73 {
75 if (!(fl = fl->next)) 74 if (!(fl = fl->next))
76 break; 75 break;
77 number--; 76 number--;
78 } 77 }
78
79 return fl; 79 return fl;
80} 80}
81 81
82/* check_recipe() - makes sure we actually have the requested artifact 82/* check_recipe() - makes sure we actually have the requested artifact
83 * and archetype. */ 83 * and archetype. */
84
85static int 84static int
86check_recipe (const recipe *rp) 85check_recipe (const recipe *rp)
87{ 86{
88 size_t i; 87 size_t i;
89 int result; 88 int result = 1;
90 89
91 result = 1;
92 for (i = 0; i < rp->arch_names; i++) 90 for (i = 0; i < rp->arch_names; i++)
93 { 91 {
94 if (archetype::find (rp->arch_name[i]) != NULL) 92 if (archetype::find (rp->arch_name[i]) != NULL)
95 { 93 {
96 artifact *art = locate_recipe_artifact (rp, i); 94 artifact *art = locate_recipe_artifact (rp, i);
97 95
98 if (!art && strcmp (rp->title, "NONE") != 0) 96 if (!art && strcmp (rp->title, "NONE") != 0)
99 { 97 {
100 LOG (llevError, "\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); 98 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
101 result = 0; 99 result = 0;
102 } 100 }
103 } 101 }
104 else 102 else
105 { 103 {
106 LOG (llevError, "\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); 104 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
107 result = 0; 105 result = 0;
108 } 106 }
109 } 107 }
110 108
111 return result; 109 return result;
112} 110}
113
114 111
115/* 112/*
116 * init_formulae() - Builds up the lists of formula from the file in 113 * init_formulae() - Builds up the lists of formula from the file in
117 * the libdir. -b.t. 114 * the libdir. -b.t.
118 */ 115 */
119
120void 116void
121init_formulae (void) 117init_formulae (void)
122{ 118{
123 static int has_been_done = 0; 119 static int has_been_done = 0;
124 FILE *fp; 120 FILE *fp;
135 return; 131 return;
136 else 132 else
137 has_been_done = 1; 133 has_been_done = 1;
138 134
139 sprintf (filename, "%s/formulae", settings.datadir); 135 sprintf (filename, "%s/formulae", settings.datadir);
140 LOG (llevDebug, "Reading alchemical formulae from %s...", filename); 136 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
141 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) 137 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
142 { 138 {
143 LOG (llevError, "Can't open %s.\n", filename); 139 LOG (llevError, "Can't open %s.\n", filename);
144 return; 140 return;
145 } 141 }
152 *cp = '\0'; 148 *cp = '\0';
153 cp = buf; 149 cp = buf;
154 while (*cp == ' ') /* Skip blanks */ 150 while (*cp == ' ') /* Skip blanks */
155 cp++; 151 cp++;
156 152
157 if (!strncmp (cp, "Object", 6)) 153 if (!strncmp (cp, "object", 6))
158 { 154 {
159 formula = get_empty_formula (); 155 formula = get_empty_formula ();
160 formula->title = strchr (cp, ' ') + 1; 156 formula->title = strchr (cp, ' ') + 1;
161 } 157 }
162 else if (!strncmp (cp, "keycode", 7)) 158 else if (!strncmp (cp, "keycode", 7))
163 {
164 formula->keycode = strchr (cp, ' ') + 1; 159 formula->keycode = strchr (cp, ' ') + 1;
165 }
166 else if (sscanf (cp, "trans %d", &value)) 160 else if (sscanf (cp, "trans %d", &value))
167 {
168 formula->transmute = (uint16) value; 161 formula->transmute = (uint16) value;
169 }
170 else if (sscanf (cp, "yield %d", &value)) 162 else if (sscanf (cp, "yield %d", &value))
171 {
172 formula->yield = (uint16) value; 163 formula->yield = (uint16) value;
173 }
174 else if (sscanf (cp, "chance %d", &value)) 164 else if (sscanf (cp, "chance %d", &value))
175 {
176 formula->chance = (uint16) value; 165 formula->chance = (uint16) value;
177 }
178 else if (sscanf (cp, "exp %d", &value)) 166 else if (sscanf (cp, "exp %d", &value))
179 {
180 formula->exp = (uint16) value; 167 formula->exp = (uint16) value;
181 }
182 else if (sscanf (cp, "diff %d", &value)) 168 else if (sscanf (cp, "diff %d", &value))
183 {
184 formula->diff = (uint16) value; 169 formula->diff = (uint16) value;
185 }
186 else if (!strncmp (cp, "ingred", 6)) 170 else if (!strncmp (cp, "ingred", 6))
187 { 171 {
188 int numb_ingred = 1; 172 int numb_ingred = 1;
189 173
190 cp = strchr (cp, ' ') + 1; 174 cp = strchr (cp, ' ') + 1;
193 if ((next = strchr (cp, ',')) != NULL) 177 if ((next = strchr (cp, ',')) != NULL)
194 { 178 {
195 *(next++) = '\0'; 179 *(next++) = '\0';
196 numb_ingred++; 180 numb_ingred++;
197 } 181 }
182
198 tmp = new linked_char; 183 tmp = new linked_char;
199 184
200 tmp->name = cp; 185 tmp->name = cp;
201 tmp->next = formula->ingred; 186 tmp->next = formula->ingred;
202 formula->ingred = tmp; 187 formula->ingred = tmp;
205 * quickly for the right recipe. 190 * quickly for the right recipe.
206 */ 191 */
207 formula->index += strtoint (cp); 192 formula->index += strtoint (cp);
208 } 193 }
209 while ((cp = next) != NULL); 194 while ((cp = next) != NULL);
195
210 /* now find the correct (# of ingred ordered) formulalist */ 196 /* now find the correct (# of ingred ordered) formulalist */
211 fl = formulalist; 197 fl = formulalist;
212 while (numb_ingred != 1) 198 while (numb_ingred != 1)
213 { 199 {
214 if (!fl->next) 200 if (!fl->next)
215 fl->next = init_recipelist (); 201 fl->next = init_recipelist ();
202
216 fl = fl->next; 203 fl = fl->next;
217 numb_ingred--; 204 numb_ingred--;
218 } 205 }
206
219 fl->total_chance += formula->chance; 207 fl->total_chance += formula->chance;
220 fl->number++; 208 fl->number++;
221 formula->next = fl->items; 209 formula->next = fl->items;
222 fl->items = formula; 210 fl->items = formula;
223 } 211 }
225 { 213 {
226 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); 214 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names);
227 check_recipe (formula); 215 check_recipe (formula);
228 } 216 }
229 else if (!strncmp (cp, "skill", 5)) 217 else if (!strncmp (cp, "skill", 5))
230 {
231 formula->skill = strchr (cp, ' ') + 1; 218 formula->skill = strchr (cp, ' ') + 1;
232 }
233 else if (!strncmp (cp, "cauldron", 8)) 219 else if (!strncmp (cp, "cauldron", 8))
234 {
235 formula->cauldron = strchr (cp, ' ') + 1; 220 formula->cauldron = strchr (cp, ' ') + 1;
236 }
237 else 221 else
238 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); 222 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
239 } 223 }
224
240 LOG (llevDebug, "done.\n"); 225 LOG (llevDebug, "done.\n");
241 close_and_delete (fp, comp); 226 close_and_delete (fp, comp);
242 /* Lastly, lets check for problems in formula we got */ 227 /* Lastly, lets check for problems in formula we got */
243 check_formulae (); 228 check_formulae ();
244} 229}
255{ 240{
256 recipelist *fl; 241 recipelist *fl;
257 recipe *check, *formula; 242 recipe *check, *formula;
258 int numb = 1; 243 int numb = 1;
259 244
260 LOG (llevDebug, "Checking formulae lists..."); 245 LOG (llevDebug, "Checking formulae lists...\n");
261 246
262 for (fl = formulalist; fl != NULL; fl = fl->next) 247 for (fl = formulalist; fl; fl = fl->next)
263 { 248 {
264 for (formula = fl->items; formula != NULL; formula = formula->next) 249 for (formula = fl->items; formula; formula = formula->next)
265 for (check = formula->next; check != NULL; check = check->next) 250 for (check = formula->next; check; check = check->next)
266 if (check->index == formula->index) 251 if (check->index == formula->index)
267 { 252 {
268 LOG (llevError, " ERROR: On %d ingred list: ", numb); 253 LOG (llevError, " ERROR: On %d ingred list: ", numb);
269 LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n", 254 LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n",
270 formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index); 255 formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index);
289 fprintf (logfile, "\n"); 274 fprintf (logfile, "\n");
290 while (fl) 275 while (fl)
291 { 276 {
292 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", 277 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
293 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); 278 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
294 for (formula = fl->items; formula != NULL; formula = formula->next) 279 for (formula = fl->items; formula; formula = formula->next)
295 { 280 {
296 artifact *art = NULL; 281 artifact *art = NULL;
297 char buf[MAX_BUF]; 282 char buf[MAX_BUF];
298 size_t i; 283 size_t i;
299 284
303 288
304 if (archetype::find (string) != NULL) 289 if (archetype::find (string) != NULL)
305 { 290 {
306 art = locate_recipe_artifact (formula, i); 291 art = locate_recipe_artifact (formula, i);
307 if (!art && strcmp (formula->title, "NONE")) 292 if (!art && strcmp (formula->title, "NONE"))
308 LOG (llevError, "Formula %s has no artifact\n", &formula->title); 293 LOG (llevError, "Formula %s has no artifact!\n", &formula->title);
309 else 294 else
310 { 295 {
311 if (strcmp (formula->title, "NONE")) 296 if (strcmp (formula->title, "NONE"))
312 sprintf (buf, "%s of %s", string, &formula->title); 297 sprintf (buf, "%s of %s", string, &formula->title);
313 else 298 else
352 * only there to prevent infinite loops in treasure lists (a list 337 * only there to prevent infinite loops in treasure lists (a list
353 * referencing another list pointing back to the first one). */ 338 * referencing another list pointing back to the first one). */
354archetype * 339archetype *
355find_treasure_by_name (const treasure *t, const char *name, int depth) 340find_treasure_by_name (const treasure *t, const char *name, int depth)
356{ 341{
357 treasurelist *tl;
358 archetype *at;
359
360 if (depth > 10) 342 if (depth > 10)
361 return NULL; 343 return 0;
362 while (t != NULL) 344
345 while (t)
363 { 346 {
364 if (t->name != NULL) 347 if (t->name)
365 { 348 {
366 tl = find_treasurelist (t->name); 349 if (treasurelist *tl = treasurelist::find (t->name))
350 if (tl->items)
367 at = find_treasure_by_name (tl->items, name, depth + 1); 351 if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1))
368 if (at != NULL)
369 return at; 352 return at;
370 } 353 }
371 else 354 else
372 { 355 {
373 if (!strcasecmp (t->item->clone.name, name)) 356 if (t->item && !strcasecmp (t->item->clone.name, name))
374 return t->item; 357 return t->item;
375 } 358 }
359
376 if (t->next_yes != NULL) 360 if (t->next_yes)
377 {
378 at = find_treasure_by_name (t->next_yes, name, depth); 361 if (archetype *at = find_treasure_by_name (t->next_yes, name, depth))
379 if (at != NULL)
380 return at; 362 return at;
381 } 363
382 if (t->next_no != NULL) 364 if (t->next_no)
383 {
384 at = find_treasure_by_name (t->next_no, name, depth); 365 if (archetype *at = find_treasure_by_name (t->next_no, name, depth))
385 if (at != NULL)
386 return at; 366 return at;
387 } 367
388 t = t->next; 368 t = t->next;
389 } 369 }
370
390 return NULL; 371 return 0;
391} 372}
392 373
393/* If several archetypes have the same name, the value of the first 374/* If several archetypes have the same name, the value of the first
394 * one with that name will be returned. This happens for the 375 * one with that name will be returned. This happens for the
395 * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the 376 * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the
415 while (isdigit (*name)) 396 while (isdigit (*name))
416 { 397 {
417 mult = 10 * mult + (*name - '0'); 398 mult = 10 * mult + (*name - '0');
418 name++; 399 name++;
419 } 400 }
401
420 if (mult > 0) 402 if (mult > 0)
421 name++; 403 name++;
422 else 404 else
423 mult = 1; 405 mult = 1;
406
424 /* first, try to match the name of an archetype */ 407 /* first, try to match the name of an archetype */
425 for (at = first_archetype; at != NULL; at = at->next) 408 for (at = first_archetype; at != NULL; at = at->next)
426 { 409 {
427 if (at->clone.title != NULL) 410 if (at->clone.title != NULL)
428 { 411 {
432 return mult * at->clone.value; 415 return mult * at->clone.value;
433 } 416 }
434 if (!strcasecmp (at->clone.name, name)) 417 if (!strcasecmp (at->clone.name, name))
435 return mult * at->clone.value; 418 return mult * at->clone.value;
436 } 419 }
420
437 /* second, try to match an artifact ("arch of something") */ 421 /* second, try to match an artifact ("arch of something") */
438 cp = strstr (name, " of "); 422 cp = strstr (name, " of ");
439 if (cp != NULL) 423 if (cp != NULL)
440 { 424 {
441 strcpy (part1, name); 425 strcpy (part1, name);
442 part1[cp - name] = '\0'; 426 part1[cp - name] = '\0';
443 strcpy (part2, cp + 4); 427 strcpy (part2, cp + 4);
444 /* find the first archetype matching the first part of the name */ 428 /* find the first archetype matching the first part of the name */
445 for (at = first_archetype; at != NULL; at = at->next) 429 for (at = first_archetype; at; at = at->next)
446 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) 430 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
447 break; 431 break;
448 if (at != NULL) 432 if (at != NULL)
449 { 433 {
450 /* find the first artifact derived from that archetype (same type) */ 434 /* find the first artifact derived from that archetype (same type) */
451 for (al = first_artifactlist; al != NULL; al = al->next) 435 for (al = first_artifactlist; al; al = al->next)
452 if (al->type == at->clone.type) 436 if (al->type == at->clone.type)
453 { 437 {
454 for (art = al->items; art != NULL; art = art->next) 438 for (art = al->items; art; art = art->next)
455 if (!strcasecmp (art->item->name, part2)) 439 if (!strcasecmp (art->item->name, part2))
456 return mult * at->clone.value * art->item->value; 440 return mult * at->clone.value * art->item->value;
457 } 441 }
458 } 442 }
459 } 443 }
444
460 /* third, try to match a body part ("arch's something") */ 445 /* third, try to match a body part ("arch's something") */
461 cp = strstr (name, "'s "); 446 cp = strstr (name, "'s ");
462 if (cp != NULL) 447 if (cp)
463 { 448 {
464 strcpy (part1, name); 449 strcpy (part1, name);
465 part1[cp - name] = '\0'; 450 part1[cp - name] = '\0';
466 strcpy (part2, cp + 3); 451 strcpy (part2, cp + 3);
467 /* examine all archetypes matching the first part of the name */ 452 /* examine all archetypes matching the first part of the name */
468 for (at = first_archetype; at != NULL; at = at->next) 453 for (at = first_archetype; at; at = at->next)
469 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) 454 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
470 { 455 {
471 if (at->clone.randomitems != NULL) 456 if (at->clone.randomitems)
472 { 457 {
473 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); 458 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0);
474 if (at2 != NULL) 459 if (at2)
475 return mult * at2->clone.value * isqrt (at->clone.level * 2); 460 return mult * at2->clone.value * isqrt (at->clone.level * 2);
476 } 461 }
477 } 462 }
478 } 463 }
464
479 /* failed to find any matching items -- formula should be checked */ 465 /* failed to find any matching items -- formula should be checked */
480 return -1; 466 return -1;
481} 467}
482 468
483/* code copied from dump_alchemy() and modified by Raphael Quinet */ 469/* code copied from dump_alchemy() and modified by Raphael Quinet */
495 fprintf (logfile, "\n"); 481 fprintf (logfile, "\n");
496 while (fl) 482 while (fl)
497 { 483 {
498 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", 484 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
499 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); 485 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
500 for (formula = fl->items; formula != NULL; formula = formula->next) 486 for (formula = fl->items; formula; formula = formula->next)
501 { 487 {
502 artifact *art = NULL; 488 artifact *art = NULL;
503 archetype *at = NULL; 489 archetype *at = NULL;
504 char buf[MAX_BUF]; 490 char buf[MAX_BUF];
505 size_t i; 491 size_t i;
603} 589}
604 590
605artifact * 591artifact *
606locate_recipe_artifact (const recipe *rp, size_t idx) 592locate_recipe_artifact (const recipe *rp, size_t idx)
607{ 593{
608 object *item = get_archetype (rp->arch_name[idx]); 594 archetype *at = archetype::find (rp->arch_name [idx]);
609 artifactlist *at = NULL;
610 artifact *art = NULL;
611 595
612 if (!item) 596 if (at)
613 return (artifact *) NULL; 597 if (artifactlist *al = find_artifactlist (at->clone.type))
614
615 if ((at = find_artifactlist (item->type)))
616 for (art = at->items; art; art = art->next) 598 for (artifact *art = al->items; art; art = art->next)
617 if (!strcmp (art->item->name, rp->title)) 599 if (art->item->name == rp->title)
618 break; 600 return art;
619 601
620 free_object (item);
621
622 return art; 602 return 0;
623} 603}
624 604
625int 605int
626numb_ingred (const char *buf) 606numb_ingred (const char *buf)
627{ 607{
643 for (fl = get_formulalist (1); fl; fl = fl->next) 623 for (fl = get_formulalist (1); fl; fl = fl->next)
644 number++; 624 number++;
645 625
646 /* now, randomly choose one */ 626 /* now, randomly choose one */
647 if (number > 0) 627 if (number > 0)
648 roll = RANDOM () % number; 628 roll = rndm (number);
649 629
650 fl = get_formulalist (1); 630 fl = get_formulalist (1);
651 while (roll && fl) 631 while (roll && fl)
652 { 632 {
653 if (fl->next) 633 if (fl->next)
676 if ((fl = get_random_recipelist ()) == NULL) 656 if ((fl = get_random_recipelist ()) == NULL)
677 return rp; 657 return rp;
678 658
679 if (fl->total_chance > 0) 659 if (fl->total_chance > 0)
680 { 660 {
681 r = RANDOM () % fl->total_chance; 661 r = rndm (fl->total_chance);
682 for (rp = fl->items; rp; rp = rp->next) 662 for (rp = fl->items; rp; rp = rp->next)
683 { 663 {
684 r -= rp->chance; 664 r -= rp->chance;
685 if (r < 0) 665 if (r < 0)
686 break; 666 break;
737 char *dup; 717 char *dup;
738 char *p; 718 char *p;
739 size_t size; 719 size_t size;
740 size_t i; 720 size_t i;
741 721
742 dup = strdup_local (str); 722 dup = strdup (str);
743 if (dup == NULL) 723 if (dup == NULL)
744 fatal (OUT_OF_MEMORY); 724 fatal (OUT_OF_MEMORY);
745 725
746 size = 0; 726 size = 0;
747 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 727 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines