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.26 by root, Tue May 6 16:55:25 2008 UTC vs.
Revision 1.32 by root, Fri Nov 6 13:31:47 2009 UTC

3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 11 * option) any later version.
12 * 12 *
13 * This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
23 24
24/* 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
110 size_t i; 111 size_t i;
111 int result = 1; 112 int result = 1;
112 113
113 for (i = 0; i < rp->arch_names; i++) 114 for (i = 0; i < rp->arch_names; i++)
114 { 115 {
115 if (archetype::find (rp->arch_name[i]) != NULL) 116 if (archetype::find (rp->arch_name[i]))
116 { 117 {
117 artifact *art = locate_recipe_artifact (rp, i); 118 artifact *art = locate_recipe_artifact (rp, i);
118 119
119 if (!art && strcmp (rp->title, "NONE") != 0) 120 if (!art && rp->title != shstr_NONE)
120 { 121 {
121 LOG (llevError, "WARNING: 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);
122 result = 0; 123 result = 0;
123 } 124 }
124 } 125 }
128 result = 0; 129 result = 0;
129 } 130 }
130 } 131 }
131 132
132 return result; 133 return result;
134}
135
136/* check_formulae()- since we are doing a squential search on the
137 * formulae lists now, we have to be carefull that we dont have 2
138 * formula with the exact same index value. Under the new nbatches
139 * code, it is possible to have multiples of ingredients in a cauldron
140 * which could result in an index formula mismatch. We *don't* check for
141 * that possibility here. -b.t.
142 */
143static void
144check_formulae (void)
145{
146 recipelist *fl;
147 recipe *check, *formula;
148 int numb = 1;
149
150 LOG (llevDebug, "Checking formulae lists...\n");
151
152 for (fl = formulalist; fl; fl = fl->next)
153 {
154 for (formula = fl->items; formula; formula = formula->next)
155 for (check = formula->next; check; check = check->next)
156 if (check->index == formula->index)
157 {
158 LOG (llevError, " ERROR: On %d ingred list: ", numb);
159 LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n",
160 formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index);
161 }
162 numb++;
163 }
164
165 LOG (llevDebug, "done.\n");
166
133} 167}
134 168
135/* 169/*
136 * init_formulae() - Builds up the lists of formula from the file in 170 * init_formulae() - Builds up the lists of formula from the file in
137 * the libdir. -b.t. 171 * the libdir. -b.t.
249 close_and_delete (fp, comp); 283 close_and_delete (fp, comp);
250 /* Lastly, lets check for problems in formula we got */ 284 /* Lastly, lets check for problems in formula we got */
251 check_formulae (); 285 check_formulae ();
252} 286}
253 287
254/* check_formulae()- since we are doing a squential search on the
255 * formulae lists now, we have to be carefull that we dont have 2
256 * formula with the exact same index value. Under the new nbatches
257 * code, it is possible to have multiples of ingredients in a cauldron
258 * which could result in an index formula mismatch. We *don't* check for
259 * that possibility here. -b.t.
260 */
261void
262check_formulae (void)
263{
264 recipelist *fl;
265 recipe *check, *formula;
266 int numb = 1;
267
268 LOG (llevDebug, "Checking formulae lists...\n");
269
270 for (fl = formulalist; fl; fl = fl->next)
271 {
272 for (formula = fl->items; formula; formula = formula->next)
273 for (check = formula->next; check; check = check->next)
274 if (check->index == formula->index)
275 {
276 LOG (llevError, " ERROR: On %d ingred list: ", numb);
277 LOG (llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n",
278 formula->arch_name[0], &formula->title, check->arch_name[0], &check->title, formula->index);
279 }
280 numb++;
281 }
282
283 LOG (llevDebug, "done.\n");
284
285}
286
287/* Find a treasure with a matching name. The 'depth' parameter is 288/* Find a treasure with a matching name. The 'depth' parameter is
288 * only there to prevent infinite loops in treasure lists (a list 289 * only there to prevent infinite loops in treasure lists (a list
289 * referencing another list pointing back to the first one). */ 290 * referencing another list pointing back to the first one). */
290archetype * 291static archetype *
291find_treasure_by_name (const treasure *t, const char *name, int depth) 292find_treasure_by_name (const treasure *t, const char *name, int depth)
292{ 293{
293 if (depth > 10) 294 if (depth > 10)
294 return 0; 295 return 0;
295 296
320 } 321 }
321 322
322 return 0; 323 return 0;
323} 324}
324 325
325/* If several archetypes have the same name, the value of the first
326 * one with that name will be returned. This happens for the
327 * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the
328 * monsters' body parts, there may be several monsters with the same
329 * name. This is not a problem if these monsters have the same level
330 * (e.g. sage & c_sage) or if only one of the monsters generates the
331 * body parts that we are looking for (e.g. big_dragon and
332 * big_dragon_worthless). */
333long
334find_ingred_cost (const char *name)
335{
336 archetype *at2;
337 artifactlist *al;
338 artifact *art;
339 long mult;
340 char *cp;
341 char part1[100];
342 char part2[100];
343
344 /* same as atoi(), but skip number */
345 mult = 0;
346 while (isdigit (*name))
347 {
348 mult = 10 * mult + (*name - '0');
349 name++;
350 }
351
352 if (mult > 0)
353 name++;
354 else
355 mult = 1;
356
357 /* first, try to match the name of an archetype */
358 for_all_archetypes (at)
359 {
360 if (at->title != NULL)
361 {
362 /* inefficient, but who cares? */
363 sprintf (part1, "%s %s", &at->object::name, &at->title);
364 if (!strcasecmp (part1, name))
365 return mult * at->value;
366 }
367 if (!strcasecmp (at->object::name, name))
368 return mult * at->value;
369 }
370
371 /* second, try to match an artifact ("arch of something") */
372 cp = strstr (name, " of ");
373 if (cp != NULL)
374 {
375 strcpy (part1, name);
376 part1[cp - name] = '\0';
377 strcpy (part2, cp + 4);
378
379 /* find the first archetype matching the first part of the name */
380 for_all_archetypes (at)
381 if (!strcasecmp (at->object::name, part1) && at->title == NULL)
382 {
383 /* find the first artifact derived from that archetype (same type) */
384 for (al = first_artifactlist; al; al = al->next)
385 if (al->type == at->type)
386 {
387 for (art = al->items; art; art = art->next)
388 if (!strcasecmp (art->item->name, part2))
389 return mult * at->value * art->item->value;
390 }
391 }
392 }
393
394 /* third, try to match a body part ("arch's something") */
395 cp = strstr (name, "'s ");
396 if (cp)
397 {
398 strcpy (part1, name);
399 part1[cp - name] = '\0';
400 strcpy (part2, cp + 3);
401 /* examine all archetypes matching the first part of the name */
402 for_all_archetypes (at)
403 if (!strcasecmp (at->object::name, part1) && at->title == NULL)
404 {
405 if (at->randomitems)
406 {
407 at2 = find_treasure_by_name (at->randomitems->items, part2, 0);
408 if (at2)
409 return mult * at2->value * isqrt (at->level * 2);
410 }
411 }
412 }
413
414 /* failed to find any matching items -- formula should be checked */
415 return -1;
416}
417
418const char * 326static const char *
419ingred_name (const char *name) 327ingred_name (const char *name)
420{ 328{
421 const char *cp = name; 329 const char *cp = name;
422 330
423 if (atoi (cp)) 331 if (atoi (cp))
424 cp = strchr (cp, ' ') + 1; 332 cp = strchr (cp, ' ') + 1;
425 333
426 return cp; 334 return cp;
335}
336
337static int
338numb_ingred (const char *buf)
339{
340 int numb;
341
342 if ((numb = atoi (buf)))
343 return numb;
344 else
345 return 1;
427} 346}
428 347
429/* strtoint() - we use this to convert buf into an integer 348/* strtoint() - we use this to convert buf into an integer
430 * equal to the coadded sum of the (lowercase) character 349 * equal to the coadded sum of the (lowercase) character
431 * ASCII values in buf (times prepended integers). 350 * ASCII values in buf (times prepended integers).
351 * Some kind of hashing.
432 */ 352 */
433
434int 353int
435strtoint (const char *buf) 354strtoint (const char *buf)
436{ 355{
437 const char *cp = ingred_name (buf); 356 const char *cp = ingred_name (buf);
438 int val = 0, len = strlen (cp), mult = numb_ingred (buf); 357 int val = 0, len = strlen (cp), mult = numb_ingred (buf);
441 { 360 {
442 val += tolower (*cp); 361 val += tolower (*cp);
443 cp++; 362 cp++;
444 len--; 363 len--;
445 } 364 }
365
446 return val * mult; 366 return val * mult;
447} 367}
448 368
449artifact * 369artifact *
450locate_recipe_artifact (const recipe *rp, size_t idx) 370locate_recipe_artifact (const recipe *rp, size_t idx)
458 return art; 378 return art;
459 379
460 return 0; 380 return 0;
461} 381}
462 382
463int
464numb_ingred (const char *buf)
465{
466 int numb;
467
468 if ((numb = atoi (buf)))
469 return numb;
470 else
471 return 1;
472}
473
474recipelist * 383static recipelist *
475get_random_recipelist (void) 384get_random_recipelist (void)
476{ 385{
477 recipelist *fl = NULL; 386 recipelist *fl = NULL;
478 int number = 0, roll = 0; 387 int number = 0, roll = 0;
479 388
523 if (r < 0) 432 if (r < 0)
524 break; 433 break;
525 } 434 }
526 } 435 }
527 return rp; 436 return rp;
528}
529
530void
531free_all_recipes (void)
532{
533 recipelist *fl = formulalist, *flnext;
534 recipe *formula = NULL, *next;
535 linked_char *lchar, *charnext;
536
537 LOG (llevDebug, "Freeing all the recipes\n");
538 for (fl = formulalist; fl != NULL; fl = flnext)
539 {
540 flnext = fl->next;
541
542 for (formula = fl->items; formula != NULL; formula = next)
543 {
544 next = formula->next;
545
546 free (formula->arch_name[0]);
547 free (formula->arch_name);
548
549 for (lchar = formula->ingred; lchar; lchar = charnext)
550 {
551 charnext = lchar->next;
552 delete lchar;
553 }
554 delete formula;
555 }
556
557 delete fl;
558 }
559} 437}
560 438
561/** 439/**
562 * Split a comma separated string list into words. 440 * Split a comma separated string list into words.
563 * 441 *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines