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.25 by root, Tue Apr 15 03:16:02 2008 UTC vs.
Revision 1.48 by root, Sat Nov 17 23:40:00 2018 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 7 * Copyright (©) 1992 Frank Tore Johansen
7 * 8 *
8 * Deliantra is free software: you can redistribute it and/or modify 9 * 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 10 * 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 11 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 12 * option) any later version.
12 * 13 *
13 * This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 17 * GNU General Public License for more details.
17 * 18 *
18 * You should have received a copy of the GNU General Public License 19 * 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/>. 20 * and the GNU General Public License along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
20 * 22 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 23 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 24 */
23 25
24/* Basic stuff for use with the alchemy code. Clearly some of this stuff 26/* Basic stuff for use with the alchemy code. Clearly some of this stuff
25 * could go into server/alchemy, but I left it here just in case it proves 27 * could go into server/alchemy, but I left it here just in case it proves
26 * more generally useful. 28 * more generally useful.
27 * 29 *
28 * Nov 1995 - file created by b.t. thomas@astro.psu.edu 30 * Nov 1995 - file created by b.t. thomas@astro.psu.edu
29 */ 31 */
30 32
31 33
32/* Our definition of 'formula' is any product of an alchemical process. 34/* Our definition of 'formula' is any product of an alchemical process.
33 * Ingredients are just comma delimited list of archetype (or object) 35 * Ingredients are just comma delimited list of archetype (or object)
34 * names. 36 * names.
35 */ 37 */
36 38
37/* Example 'formula' entry in libdir/formulae: 39/* Example 'formula' entry in libdir/formulae:
38 * object transparency 40 * object transparency
39 * chance 10 41 * chance 10
49static void build_stringlist (const char *str, char ***result_list, size_t * result_size); 51static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
50 52
51static recipelist *formulalist; 53static recipelist *formulalist;
52 54
53static recipelist * 55static recipelist *
54init_recipelist (void) 56init_recipelist ()
55{ 57{
56 recipelist *tl = new recipelist; 58 recipelist *tl = new recipelist;
57 59
58 tl->total_chance = 0; 60 tl->total_chance = 0;
59 tl->number = 0; 61 tl->number = 0;
62 64
63 return tl; 65 return tl;
64} 66}
65 67
66static recipe * 68static recipe *
67get_empty_formula (void) 69get_empty_formula ()
68{ 70{
69 recipe *t = new recipe; 71 recipe *t = new recipe;
70 72
71 t->chance = 0; 73 t->chance = 0;
72 t->index = 0; 74 t->index = 0;
73 t->transmute = 0; 75 t->transmute = 0;
74 t->yield = 0; 76 t->yield = 0;
75 t->diff = 0; 77 t->diff = 0;
76 t->exp = 0; 78 t->exp = 0;
77 t->keycode = 0; 79 t->keycode = 0;
78 t->title = NULL; 80 t->title = 0;
79 t->arch_names = 0; 81 t->arch_names = 0;
80 t->arch_name = NULL; 82 t->arch_name = 0;
81 t->skill = NULL; 83 t->skill = 0;
82 t->cauldron = NULL; 84 t->cauldron = 0;
83 t->ingred = NULL; 85 t->ingred = 0;
84 t->next = NULL; 86 t->next = 0;
87
85 return t; 88 return t;
86} 89}
87 90
88/* get_formulalist() - returns pointer to the formula list */ 91/* get_formulalist() - returns pointer to the formula list */
89recipelist * 92recipelist *
100 } 103 }
101 104
102 return fl; 105 return fl;
103} 106}
104 107
105/* check_recipe() - makes sure we actually have the requested artifact 108/* check_recipe() - makes sure we actually have the requested artifact
106 * and archetype. */ 109 * and archetype. */
107static int 110static int
108check_recipe (const recipe *rp) 111check_recipe (const recipe *rp)
109{ 112{
110 size_t i; 113 size_t i;
111 int result = 1; 114 int result = 1;
112 115
113 for (i = 0; i < rp->arch_names; i++) 116 for (i = 0; i < rp->arch_names; i++)
114 { 117 {
115 if (archetype::find (rp->arch_name[i]) != NULL) 118 if (archetype::find (rp->arch_name[i]))
116 { 119 {
117 artifact *art = locate_recipe_artifact (rp, i); 120 artifact *art = locate_recipe_artifact (rp, i);
118 121
119 if (!art && strcmp (rp->title, "NONE") != 0) 122 if (!art && rp->title != shstr_NONE)
120 { 123 {
121 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); 124 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
122 result = 0; 125 result = 0;
123 } 126 }
124 } 127 }
128 result = 0; 131 result = 0;
129 } 132 }
130 } 133 }
131 134
132 return result; 135 return result;
133}
134
135/*
136 * init_formulae() - Builds up the lists of formula from the file in
137 * the libdir. -b.t.
138 */
139void
140init_formulae (void)
141{
142 static int has_been_done = 0;
143 FILE *fp;
144 char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
145 recipe *formula = NULL;
146 recipelist *fl = init_recipelist ();
147 linked_char *tmp;
148 int value, comp;
149
150 if (!formulalist)
151 formulalist = fl;
152
153 if (has_been_done)
154 return;
155 else
156 has_been_done = 1;
157
158 sprintf (filename, "%s/formulae", settings.datadir);
159 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
160 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
161 {
162 LOG (llevError, "Can't open %s.\n", filename);
163 return;
164 }
165
166 while (fgets (buf, MAX_BUF, fp) != NULL)
167 {
168 if (*buf == '#')
169 continue;
170 if ((cp = strchr (buf, '\n')) != NULL)
171 *cp = '\0';
172 cp = buf;
173 while (*cp == ' ') /* Skip blanks */
174 cp++;
175
176 if (!strncmp (cp, "object", 6))
177 {
178 formula = get_empty_formula ();
179 formula->title = strchr (cp, ' ') + 1;
180 }
181 else if (!strncmp (cp, "keycode", 7))
182 formula->keycode = strchr (cp, ' ') + 1;
183 else if (sscanf (cp, "trans %d", &value))
184 formula->transmute = (uint16) value;
185 else if (sscanf (cp, "yield %d", &value))
186 formula->yield = (uint16) value;
187 else if (sscanf (cp, "chance %d", &value))
188 formula->chance = (uint16) value;
189 else if (sscanf (cp, "exp %d", &value))
190 formula->exp = (uint16) value;
191 else if (sscanf (cp, "diff %d", &value))
192 formula->diff = (uint16) value;
193 else if (!strncmp (cp, "ingred", 6))
194 {
195 int numb_ingred = 1;
196
197 cp = strchr (cp, ' ') + 1;
198 do
199 {
200 if ((next = strchr (cp, ',')) != NULL)
201 {
202 *(next++) = '\0';
203 numb_ingred++;
204 }
205
206 tmp = new linked_char;
207
208 tmp->name = cp;
209 tmp->next = formula->ingred;
210 formula->ingred = tmp;
211 /* each ingredient's ASCII value is coadded. Later on this
212 * value will be used allow us to search the formula lists
213 * quickly for the right recipe.
214 */
215 formula->index += strtoint (cp);
216 }
217 while ((cp = next) != NULL);
218
219 /* now find the correct (# of ingred ordered) formulalist */
220 fl = formulalist;
221 while (numb_ingred != 1)
222 {
223 if (!fl->next)
224 fl->next = init_recipelist ();
225
226 fl = fl->next;
227 numb_ingred--;
228 }
229
230 fl->total_chance += formula->chance;
231 fl->number++;
232 formula->next = fl->items;
233 fl->items = formula;
234 }
235 else if (!strncmp (cp, "arch", 4))
236 {
237 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names);
238 check_recipe (formula);
239 }
240 else if (!strncmp (cp, "skill", 5))
241 formula->skill = strchr (cp, ' ') + 1;
242 else if (!strncmp (cp, "cauldron", 8))
243 formula->cauldron = strchr (cp, ' ') + 1;
244 else
245 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
246 }
247
248 LOG (llevDebug, "done.\n");
249 close_and_delete (fp, comp);
250 /* Lastly, lets check for problems in formula we got */
251 check_formulae ();
252} 136}
253 137
254/* check_formulae()- since we are doing a squential search on the 138/* 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 139 * 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 140 * formula with the exact same index value. Under the new nbatches
257 * code, it is possible to have multiples of ingredients in a cauldron 141 * 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 142 * which could result in an index formula mismatch. We *don't* check for
259 * that possibility here. -b.t. 143 * that possibility here. -b.t.
260 */ 144 */
261void 145static void
262check_formulae (void) 146check_formulae ()
263{ 147{
264 recipelist *fl; 148 recipelist *fl;
265 recipe *check, *formula; 149 recipe *check, *formula;
266 int numb = 1; 150 int numb = 1;
267 151
282 166
283 LOG (llevDebug, "done.\n"); 167 LOG (llevDebug, "done.\n");
284 168
285} 169}
286 170
171/*
172 * init_formulae() - Builds up the lists of formula from the file in
173 * the libdir. -b.t.
174 */
175void
176init_formulae ()
177{
178 static int has_been_done = 0;
179 char *next;
180 recipelist *fl = init_recipelist ();
181 linked_char *tmp;
182
183 if (!formulalist)
184 formulalist = fl;
185
186 if (has_been_done)
187 return;
188 else
189 has_been_done = 1;
190
191 object_thawer thawer (settings.datadir, "formulae");
192
193 if (!thawer)
194 {
195 LOG (llevError, "Can't open %s.\n", thawer.name);
196 return;
197 }
198
199 while (thawer.kw)
200 {
201 if (thawer.kw != KW_object)
202 if (!thawer.parse_error ("formulae file"))
203 break;
204
205 recipe *formula = get_empty_formula ();
206 thawer.get (formula->title);
207
208 for (;;)
209 {
210 thawer.next ();
211
212 switch (thawer.kw)
213 {
214 case KW_keycode: thawer.get (formula->keycode ); break;
215 case KW_trans: thawer.get (formula->transmute); break;
216 case KW_yield: thawer.get (formula->yield ); break;
217 case KW_chance: thawer.get (formula->chance ); break;
218 case KW_exp: thawer.get (formula->exp ); break;
219 case KW_diff: thawer.get (formula->diff ); break;
220 case KW_skill: thawer.get (formula->skill ); break;
221 case KW_cauldron: thawer.get (formula->cauldron ); break;
222
223 case KW_arch:
224 {
225 build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names);
226 check_recipe (formula);
227 }
228 break;
229
230 case KW_ingred:
231 if (thawer.value)
232 {
233 int numb_ingred = 1;
234 char *cp = thawer.value;
235
236 do
237 {
238 if ((next = strchr (cp, ',')))
239 {
240 *next++ = '\0';
241 ++numb_ingred;
242 }
243
244 tmp = new linked_char;
245
246 tmp->name = cp;
247 tmp->next = formula->ingred;
248 formula->ingred = tmp;
249 /* each ingredient's ASCII value is coadded. Later on this
250 * value will be used allow us to search the formula lists
251 * quickly for the right recipe.
252 */
253 formula->index += strtoint (cp);
254 }
255 while ((cp = next));
256
257 /* now find the correct (# of ingred ordered) formulalist */
258 fl = formulalist;
259 while (numb_ingred != 1)
260 {
261 if (!fl->next)
262 fl->next = init_recipelist ();
263
264 fl = fl->next;
265 numb_ingred--;
266 }
267
268 fl->total_chance += formula->chance;
269 fl->number++;
270 formula->next = fl->items;
271 fl->items = formula;
272 }
273 break;
274
275 default:
276 delete formula;
277 case KW_EOF:
278 case KW_object:
279 goto next_object;
280 }
281 }
282
283next_object: ;
284 }
285
286 /* Lastly, lets check for problems in formula we got */
287 check_formulae ();
288}
289
287/* Find a treasure with a matching name. The 'depth' parameter is 290/* Find a treasure with a matching name. The 'depth' parameter is
288 * only there to prevent infinite loops in treasure lists (a list 291 * only there to prevent infinite loops in treasure lists (a list
289 * referencing another list pointing back to the first one). */ 292 * referencing another list pointing back to the first one). */
290archetype * 293static archetype *
291find_treasure_by_name (const treasure *t, const char *name, int depth) 294find_treasure_by_name (const treasure *t, const char *name, int depth)
292{ 295{
293 if (depth > 10) 296 if (depth > 10)
294 return 0; 297 return 0;
295 298
320 } 323 }
321 324
322 return 0; 325 return 0;
323} 326}
324 327
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 * 328static const char *
419ingred_name (const char *name) 329ingred_name (const char *name)
420{ 330{
421 const char *cp = name; 331 const char *cp = name;
422 332
423 if (atoi (cp)) 333 if (atoi (cp))
424 cp = strchr (cp, ' ') + 1; 334 cp = strchr (cp, ' ') + 1;
425 335
426 return cp; 336 return cp;
427} 337}
428 338
339static int
340numb_ingred (const char *buf)
341{
342 int numb;
343
344 if ((numb = atoi (buf)))
345 return numb;
346 else
347 return 1;
348}
349
429/* strtoint() - we use this to convert buf into an integer 350/* strtoint() - we use this to convert buf into an integer
430 * equal to the coadded sum of the (lowercase) character 351 * equal to the coadded sum of the (lowercase) character
431 * ASCII values in buf (times prepended integers). 352 * ASCII values in buf (times prepended integers).
353 * Some kind of hashing.
432 */ 354 */
433
434int 355int
435strtoint (const char *buf) 356strtoint (const char *buf)
436{ 357{
437 const char *cp = ingred_name (buf); 358 const char *cp = ingred_name (buf);
438 int val = 0, len = strlen (cp), mult = numb_ingred (buf); 359 int val = 0, len = strlen (cp), mult = numb_ingred (buf);
441 { 362 {
442 val += tolower (*cp); 363 val += tolower (*cp);
443 cp++; 364 cp++;
444 len--; 365 len--;
445 } 366 }
367
446 return val * mult; 368 return val * mult;
447} 369}
448 370
449artifact * 371artifact *
450locate_recipe_artifact (const recipe *rp, size_t idx) 372locate_recipe_artifact (const recipe *rp, size_t idx)
458 return art; 380 return art;
459 381
460 return 0; 382 return 0;
461} 383}
462 384
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 * 385static recipelist *
475get_random_recipelist (void) 386get_random_recipelist ()
476{ 387{
477 recipelist *fl = NULL; 388 recipelist *fl = NULL;
478 int number = 0, roll = 0; 389 int number = 0, roll = 0;
479 390
480 /* first, determine # of recipelist we have */ 391 /* first, determine # of recipelist we have */
525 } 436 }
526 } 437 }
527 return rp; 438 return rp;
528} 439}
529 440
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}
560
561/** 441/**
562 * Split a comma separated string list into words. 442 * Split a comma separated string list into words.
563 * 443 *
564 * @param str the string to split 444 * @param str the string to split
565 * 445 *
576 char *p; 456 char *p;
577 size_t size; 457 size_t size;
578 size_t i; 458 size_t i;
579 459
580 dup = strdup (str); 460 dup = strdup (str);
581 if (dup == NULL)
582 fatal (OUT_OF_MEMORY);
583 461
584 size = 0; 462 size = 0;
585 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 463 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
586 size++; 464 size++;
587 465
588 *result_list = (char **) malloc (size * sizeof (*result_list)); 466 *result_list = (char **)malloc (size * sizeof (*result_list));
589 467
590 *result_size = size; 468 *result_size = size;
591 469
592 for (i = 0; i < size; i++) 470 for (i = 0; i < size; i++)
593 { 471 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines