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.11 by root, Thu Dec 14 22:45:40 2006 UTC vs.
Revision 1.21 by root, Mon May 28 21:21:40 2007 UTC

1/*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Crossfire TRT is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * The authors can be reached via e-mail to <crossfire@schmorp.de>
23 */
24
1/* Basic stuff for use with the alchemy code. Clearly some of this stuff 25/* Basic stuff for use with the alchemy code. Clearly some of this stuff
2 * could go into server/alchemy, but I left it here just in case it proves 26 * could go into server/alchemy, but I left it here just in case it proves
3 * more generally useful. 27 * more generally useful.
4 * 28 *
5 * Nov 1995 - file created by b.t. thomas@astro.psu.edu 29 * Nov 1995 - file created by b.t. thomas@astro.psu.edu
10 * Ingredients are just comma delimited list of archetype (or object) 34 * Ingredients are just comma delimited list of archetype (or object)
11 * names. 35 * names.
12 */ 36 */
13 37
14/* Example 'formula' entry in libdir/formulae: 38/* Example 'formula' entry in libdir/formulae:
15 * Object transparency 39 * object transparency
16 * chance 10 40 * chance 10
17 * ingred dust of beholdereye,gem 41 * ingred dust of beholdereye,gem
18 * arch potion_generic 42 * arch potion_generic
19 */ 43 */
20 44
32{ 56{
33 recipelist *tl = new recipelist; 57 recipelist *tl = new recipelist;
34 58
35 tl->total_chance = 0; 59 tl->total_chance = 0;
36 tl->number = 0; 60 tl->number = 0;
37 tl->items = NULL; 61 tl->items = 0;
38 tl->next = NULL; 62 tl->next = 0;
63
39 return tl; 64 return tl;
40} 65}
41 66
42static recipe * 67static recipe *
43get_empty_formula (void) 68get_empty_formula (void)
60 t->next = NULL; 85 t->next = NULL;
61 return t; 86 return t;
62} 87}
63 88
64/* get_formulalist() - returns pointer to the formula list */ 89/* get_formulalist() - returns pointer to the formula list */
65
66recipelist * 90recipelist *
67get_formulalist (int i) 91get_formulalist (int i)
68{ 92{
69 recipelist *fl = formulalist; 93 recipelist *fl = formulalist;
70 int number = i; 94 int number = i;
73 { 97 {
74 if (!(fl = fl->next)) 98 if (!(fl = fl->next))
75 break; 99 break;
76 number--; 100 number--;
77 } 101 }
102
78 return fl; 103 return fl;
79} 104}
80 105
81/* check_recipe() - makes sure we actually have the requested artifact 106/* check_recipe() - makes sure we actually have the requested artifact
82 * and archetype. */ 107 * and archetype. */
83
84static int 108static int
85check_recipe (const recipe *rp) 109check_recipe (const recipe *rp)
86{ 110{
87 size_t i; 111 size_t i;
88 int result; 112 int result = 1;
89 113
90 result = 1;
91 for (i = 0; i < rp->arch_names; i++) 114 for (i = 0; i < rp->arch_names; i++)
92 { 115 {
93 if (archetype::find (rp->arch_name[i]) != NULL) 116 if (archetype::find (rp->arch_name[i]) != NULL)
94 { 117 {
95 artifact *art = locate_recipe_artifact (rp, i); 118 artifact *art = locate_recipe_artifact (rp, i);
96 119
97 if (!art && strcmp (rp->title, "NONE") != 0) 120 if (!art && strcmp (rp->title, "NONE") != 0)
98 { 121 {
99 LOG (llevError, "\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); 122 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
100 result = 0; 123 result = 0;
101 } 124 }
102 } 125 }
103 else 126 else
104 { 127 {
105 LOG (llevError, "\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); 128 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
106 result = 0; 129 result = 0;
107 } 130 }
108 } 131 }
109 132
110 return result; 133 return result;
111} 134}
112
113 135
114/* 136/*
115 * init_formulae() - Builds up the lists of formula from the file in 137 * init_formulae() - Builds up the lists of formula from the file in
116 * the libdir. -b.t. 138 * the libdir. -b.t.
117 */ 139 */
118
119void 140void
120init_formulae (void) 141init_formulae (void)
121{ 142{
122 static int has_been_done = 0; 143 static int has_been_done = 0;
123 FILE *fp; 144 FILE *fp;
134 return; 155 return;
135 else 156 else
136 has_been_done = 1; 157 has_been_done = 1;
137 158
138 sprintf (filename, "%s/formulae", settings.datadir); 159 sprintf (filename, "%s/formulae", settings.datadir);
139 LOG (llevDebug, "Reading alchemical formulae from %s...", filename); 160 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
140 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) 161 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
141 { 162 {
142 LOG (llevError, "Can't open %s.\n", filename); 163 LOG (llevError, "Can't open %s.\n", filename);
143 return; 164 return;
144 } 165 }
151 *cp = '\0'; 172 *cp = '\0';
152 cp = buf; 173 cp = buf;
153 while (*cp == ' ') /* Skip blanks */ 174 while (*cp == ' ') /* Skip blanks */
154 cp++; 175 cp++;
155 176
156 if (!strncmp (cp, "Object", 6)) 177 if (!strncmp (cp, "object", 6))
157 { 178 {
158 formula = get_empty_formula (); 179 formula = get_empty_formula ();
159 formula->title = strchr (cp, ' ') + 1; 180 formula->title = strchr (cp, ' ') + 1;
160 } 181 }
161 else if (!strncmp (cp, "keycode", 7)) 182 else if (!strncmp (cp, "keycode", 7))
162 {
163 formula->keycode = strchr (cp, ' ') + 1; 183 formula->keycode = strchr (cp, ' ') + 1;
164 }
165 else if (sscanf (cp, "trans %d", &value)) 184 else if (sscanf (cp, "trans %d", &value))
166 {
167 formula->transmute = (uint16) value; 185 formula->transmute = (uint16) value;
168 }
169 else if (sscanf (cp, "yield %d", &value)) 186 else if (sscanf (cp, "yield %d", &value))
170 {
171 formula->yield = (uint16) value; 187 formula->yield = (uint16) value;
172 }
173 else if (sscanf (cp, "chance %d", &value)) 188 else if (sscanf (cp, "chance %d", &value))
174 {
175 formula->chance = (uint16) value; 189 formula->chance = (uint16) value;
176 }
177 else if (sscanf (cp, "exp %d", &value)) 190 else if (sscanf (cp, "exp %d", &value))
178 {
179 formula->exp = (uint16) value; 191 formula->exp = (uint16) value;
180 }
181 else if (sscanf (cp, "diff %d", &value)) 192 else if (sscanf (cp, "diff %d", &value))
182 {
183 formula->diff = (uint16) value; 193 formula->diff = (uint16) value;
184 }
185 else if (!strncmp (cp, "ingred", 6)) 194 else if (!strncmp (cp, "ingred", 6))
186 { 195 {
187 int numb_ingred = 1; 196 int numb_ingred = 1;
188 197
189 cp = strchr (cp, ' ') + 1; 198 cp = strchr (cp, ' ') + 1;
192 if ((next = strchr (cp, ',')) != NULL) 201 if ((next = strchr (cp, ',')) != NULL)
193 { 202 {
194 *(next++) = '\0'; 203 *(next++) = '\0';
195 numb_ingred++; 204 numb_ingred++;
196 } 205 }
206
197 tmp = new linked_char; 207 tmp = new linked_char;
198 208
199 tmp->name = cp; 209 tmp->name = cp;
200 tmp->next = formula->ingred; 210 tmp->next = formula->ingred;
201 formula->ingred = tmp; 211 formula->ingred = tmp;
204 * quickly for the right recipe. 214 * quickly for the right recipe.
205 */ 215 */
206 formula->index += strtoint (cp); 216 formula->index += strtoint (cp);
207 } 217 }
208 while ((cp = next) != NULL); 218 while ((cp = next) != NULL);
219
209 /* now find the correct (# of ingred ordered) formulalist */ 220 /* now find the correct (# of ingred ordered) formulalist */
210 fl = formulalist; 221 fl = formulalist;
211 while (numb_ingred != 1) 222 while (numb_ingred != 1)
212 { 223 {
213 if (!fl->next) 224 if (!fl->next)
214 fl->next = init_recipelist (); 225 fl->next = init_recipelist ();
226
215 fl = fl->next; 227 fl = fl->next;
216 numb_ingred--; 228 numb_ingred--;
217 } 229 }
230
218 fl->total_chance += formula->chance; 231 fl->total_chance += formula->chance;
219 fl->number++; 232 fl->number++;
220 formula->next = fl->items; 233 formula->next = fl->items;
221 fl->items = formula; 234 fl->items = formula;
222 } 235 }
224 { 237 {
225 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); 238 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names);
226 check_recipe (formula); 239 check_recipe (formula);
227 } 240 }
228 else if (!strncmp (cp, "skill", 5)) 241 else if (!strncmp (cp, "skill", 5))
229 {
230 formula->skill = strchr (cp, ' ') + 1; 242 formula->skill = strchr (cp, ' ') + 1;
231 }
232 else if (!strncmp (cp, "cauldron", 8)) 243 else if (!strncmp (cp, "cauldron", 8))
233 {
234 formula->cauldron = strchr (cp, ' ') + 1; 244 formula->cauldron = strchr (cp, ' ') + 1;
235 }
236 else 245 else
237 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); 246 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
238 } 247 }
248
239 LOG (llevDebug, "done.\n"); 249 LOG (llevDebug, "done.\n");
240 close_and_delete (fp, comp); 250 close_and_delete (fp, comp);
241 /* Lastly, lets check for problems in formula we got */ 251 /* Lastly, lets check for problems in formula we got */
242 check_formulae (); 252 check_formulae ();
243} 253}
254{ 264{
255 recipelist *fl; 265 recipelist *fl;
256 recipe *check, *formula; 266 recipe *check, *formula;
257 int numb = 1; 267 int numb = 1;
258 268
259 LOG (llevDebug, "Checking formulae lists..."); 269 LOG (llevDebug, "Checking formulae lists...\n");
260 270
261 for (fl = formulalist; fl != NULL; fl = fl->next) 271 for (fl = formulalist; fl; fl = fl->next)
262 { 272 {
263 for (formula = fl->items; formula != NULL; formula = formula->next) 273 for (formula = fl->items; formula; formula = formula->next)
264 for (check = formula->next; check != NULL; check = check->next) 274 for (check = formula->next; check; check = check->next)
265 if (check->index == formula->index) 275 if (check->index == formula->index)
266 { 276 {
267 LOG (llevError, " ERROR: On %d ingred list: ", numb); 277 LOG (llevError, " ERROR: On %d ingred list: ", numb);
268 LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n", 278 LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n",
269 formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index); 279 formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index);
288 fprintf (logfile, "\n"); 298 fprintf (logfile, "\n");
289 while (fl) 299 while (fl)
290 { 300 {
291 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", 301 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
292 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); 302 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
293 for (formula = fl->items; formula != NULL; formula = formula->next) 303 for (formula = fl->items; formula; formula = formula->next)
294 { 304 {
295 artifact *art = NULL; 305 artifact *art = NULL;
296 char buf[MAX_BUF]; 306 char buf[MAX_BUF];
297 size_t i; 307 size_t i;
298 308
302 312
303 if (archetype::find (string) != NULL) 313 if (archetype::find (string) != NULL)
304 { 314 {
305 art = locate_recipe_artifact (formula, i); 315 art = locate_recipe_artifact (formula, i);
306 if (!art && strcmp (formula->title, "NONE")) 316 if (!art && strcmp (formula->title, "NONE"))
307 LOG (llevError, "Formula %s has no artifact\n", &formula->title); 317 LOG (llevError, "Formula %s has no artifact!\n", &formula->title);
308 else 318 else
309 { 319 {
310 if (strcmp (formula->title, "NONE")) 320 if (strcmp (formula->title, "NONE"))
311 sprintf (buf, "%s of %s", string, &formula->title); 321 sprintf (buf, "%s of %s", string, &formula->title);
312 else 322 else
351 * only there to prevent infinite loops in treasure lists (a list 361 * only there to prevent infinite loops in treasure lists (a list
352 * referencing another list pointing back to the first one). */ 362 * referencing another list pointing back to the first one). */
353archetype * 363archetype *
354find_treasure_by_name (const treasure *t, const char *name, int depth) 364find_treasure_by_name (const treasure *t, const char *name, int depth)
355{ 365{
356 treasurelist *tl;
357 archetype *at;
358
359 if (depth > 10) 366 if (depth > 10)
360 return 0; 367 return 0;
361 368
362 while (t) 369 while (t)
363 { 370 {
364 if (t->name) 371 if (t->name)
365 { 372 {
366 tl = find_treasurelist (t->name); 373 if (treasurelist *tl = treasurelist::find (t->name))
367
368 if (tl) 374 if (tl->items)
369 {
370 at = find_treasure_by_name (tl->items, name, depth + 1); 375 if (archetype *at = find_treasure_by_name (tl->items, name, depth + 1))
371
372 if (at)
373 return at; 376 return at;
374 }
375 } 377 }
376 else 378 else
377 { 379 {
378 if (!strcasecmp (t->item->clone.name, name)) 380 if (t->item && !strcasecmp (t->item->clone.name, name))
379 return t->item; 381 return t->item;
380 } 382 }
381 383
382 if (t->next_yes) 384 if (t->next_yes)
383 {
384 at = find_treasure_by_name (t->next_yes, name, depth); 385 if (archetype *at = find_treasure_by_name (t->next_yes, name, depth))
385 if (at)
386 return at; 386 return at;
387 }
388 387
389 if (t->next_no) 388 if (t->next_no)
390 {
391 at = find_treasure_by_name (t->next_no, name, depth); 389 if (archetype *at = find_treasure_by_name (t->next_no, name, depth))
392 if (at)
393 return at; 390 return at;
394 } 391
395 t = t->next; 392 t = t->next;
396 } 393 }
394
397 return 0; 395 return 0;
398} 396}
399 397
400/* If several archetypes have the same name, the value of the first 398/* If several archetypes have the same name, the value of the first
401 * one with that name will be returned. This happens for the 399 * one with that name will be returned. This happens for the
422 while (isdigit (*name)) 420 while (isdigit (*name))
423 { 421 {
424 mult = 10 * mult + (*name - '0'); 422 mult = 10 * mult + (*name - '0');
425 name++; 423 name++;
426 } 424 }
425
427 if (mult > 0) 426 if (mult > 0)
428 name++; 427 name++;
429 else 428 else
430 mult = 1; 429 mult = 1;
430
431 /* first, try to match the name of an archetype */ 431 /* first, try to match the name of an archetype */
432 for (at = first_archetype; at != NULL; at = at->next) 432 for (at = first_archetype; at != NULL; at = at->next)
433 { 433 {
434 if (at->clone.title != NULL) 434 if (at->clone.title != NULL)
435 { 435 {
439 return mult * at->clone.value; 439 return mult * at->clone.value;
440 } 440 }
441 if (!strcasecmp (at->clone.name, name)) 441 if (!strcasecmp (at->clone.name, name))
442 return mult * at->clone.value; 442 return mult * at->clone.value;
443 } 443 }
444
444 /* second, try to match an artifact ("arch of something") */ 445 /* second, try to match an artifact ("arch of something") */
445 cp = strstr (name, " of "); 446 cp = strstr (name, " of ");
446 if (cp != NULL) 447 if (cp != NULL)
447 { 448 {
448 strcpy (part1, name); 449 strcpy (part1, name);
449 part1[cp - name] = '\0'; 450 part1[cp - name] = '\0';
450 strcpy (part2, cp + 4); 451 strcpy (part2, cp + 4);
451 /* find the first archetype matching the first part of the name */ 452 /* find the first archetype matching the first part of the name */
452 for (at = first_archetype; at != NULL; at = at->next) 453 for (at = first_archetype; at; at = at->next)
453 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) 454 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
454 break; 455 break;
455 if (at != NULL) 456 if (at != NULL)
456 { 457 {
457 /* find the first artifact derived from that archetype (same type) */ 458 /* find the first artifact derived from that archetype (same type) */
458 for (al = first_artifactlist; al != NULL; al = al->next) 459 for (al = first_artifactlist; al; al = al->next)
459 if (al->type == at->clone.type) 460 if (al->type == at->clone.type)
460 { 461 {
461 for (art = al->items; art != NULL; art = art->next) 462 for (art = al->items; art; art = art->next)
462 if (!strcasecmp (art->item->name, part2)) 463 if (!strcasecmp (art->item->name, part2))
463 return mult * at->clone.value * art->item->value; 464 return mult * at->clone.value * art->item->value;
464 } 465 }
465 } 466 }
466 } 467 }
468
467 /* third, try to match a body part ("arch's something") */ 469 /* third, try to match a body part ("arch's something") */
468 cp = strstr (name, "'s "); 470 cp = strstr (name, "'s ");
469 if (cp != NULL) 471 if (cp)
470 { 472 {
471 strcpy (part1, name); 473 strcpy (part1, name);
472 part1[cp - name] = '\0'; 474 part1[cp - name] = '\0';
473 strcpy (part2, cp + 3); 475 strcpy (part2, cp + 3);
474 /* examine all archetypes matching the first part of the name */ 476 /* examine all archetypes matching the first part of the name */
475 for (at = first_archetype; at != NULL; at = at->next) 477 for (at = first_archetype; at; at = at->next)
476 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL) 478 if (!strcasecmp (at->clone.name, part1) && at->clone.title == NULL)
477 { 479 {
478 if (at->clone.randomitems != NULL) 480 if (at->clone.randomitems)
479 { 481 {
480 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0); 482 at2 = find_treasure_by_name (at->clone.randomitems->items, part2, 0);
481 if (at2) 483 if (at2)
482 return mult * at2->clone.value * isqrt (at->clone.level * 2); 484 return mult * at2->clone.value * isqrt (at->clone.level * 2);
483 } 485 }
484 } 486 }
485 } 487 }
488
486 /* failed to find any matching items -- formula should be checked */ 489 /* failed to find any matching items -- formula should be checked */
487 return -1; 490 return -1;
488} 491}
489 492
490/* code copied from dump_alchemy() and modified by Raphael Quinet */ 493/* code copied from dump_alchemy() and modified by Raphael Quinet */
502 fprintf (logfile, "\n"); 505 fprintf (logfile, "\n");
503 while (fl) 506 while (fl)
504 { 507 {
505 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", 508 fprintf (logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n",
506 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance); 509 num_ingred, num_ingred > 1 ? "s." : ".", fl->number, fl->total_chance);
507 for (formula = fl->items; formula != NULL; formula = formula->next) 510 for (formula = fl->items; formula; formula = formula->next)
508 { 511 {
509 artifact *art = NULL; 512 artifact *art = NULL;
510 archetype *at = NULL; 513 archetype *at = NULL;
511 char buf[MAX_BUF]; 514 char buf[MAX_BUF];
512 size_t i; 515 size_t i;
610} 613}
611 614
612artifact * 615artifact *
613locate_recipe_artifact (const recipe *rp, size_t idx) 616locate_recipe_artifact (const recipe *rp, size_t idx)
614{ 617{
615 object *item = get_archetype (rp->arch_name[idx]); 618 archetype *at = archetype::find (rp->arch_name [idx]);
616 artifactlist *at = NULL;
617 artifact *art = NULL;
618 619
619 if (!item) 620 if (at)
620 return (artifact *) NULL; 621 if (artifactlist *al = find_artifactlist (at->clone.type))
621
622 if ((at = find_artifactlist (item->type)))
623 for (art = at->items; art; art = art->next) 622 for (artifact *art = al->items; art; art = art->next)
624 if (!strcmp (art->item->name, rp->title)) 623 if (art->item->name == rp->title)
625 break; 624 return art;
626 625
627 item->destroy ();
628
629 return art; 626 return 0;
630} 627}
631 628
632int 629int
633numb_ingred (const char *buf) 630numb_ingred (const char *buf)
634{ 631{
650 for (fl = get_formulalist (1); fl; fl = fl->next) 647 for (fl = get_formulalist (1); fl; fl = fl->next)
651 number++; 648 number++;
652 649
653 /* now, randomly choose one */ 650 /* now, randomly choose one */
654 if (number > 0) 651 if (number > 0)
655 roll = RANDOM () % number; 652 roll = rndm (number);
656 653
657 fl = get_formulalist (1); 654 fl = get_formulalist (1);
658 while (roll && fl) 655 while (roll && fl)
659 { 656 {
660 if (fl->next) 657 if (fl->next)
683 if ((fl = get_random_recipelist ()) == NULL) 680 if ((fl = get_random_recipelist ()) == NULL)
684 return rp; 681 return rp;
685 682
686 if (fl->total_chance > 0) 683 if (fl->total_chance > 0)
687 { 684 {
688 r = RANDOM () % fl->total_chance; 685 r = rndm (fl->total_chance);
689 for (rp = fl->items; rp; rp = rp->next) 686 for (rp = fl->items; rp; rp = rp->next)
690 { 687 {
691 r -= rp->chance; 688 r -= rp->chance;
692 if (r < 0) 689 if (r < 0)
693 break; 690 break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines