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.8 by root, Tue Dec 12 20:53:02 2006 UTC vs.
Revision 1.19 by root, Mon Apr 16 06:23:40 2007 UTC

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