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.37 by root, Thu Apr 15 03:34:30 2010 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,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992 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
49static void build_stringlist (const char *str, char ***result_list, size_t * result_size); 50static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
50 51
51static recipelist *formulalist; 52static recipelist *formulalist;
52 53
53static recipelist * 54static recipelist *
54init_recipelist (void) 55init_recipelist ()
55{ 56{
56 recipelist *tl = new recipelist; 57 recipelist *tl = new recipelist;
57 58
58 tl->total_chance = 0; 59 tl->total_chance = 0;
59 tl->number = 0; 60 tl->number = 0;
62 63
63 return tl; 64 return tl;
64} 65}
65 66
66static recipe * 67static recipe *
67get_empty_formula (void) 68get_empty_formula ()
68{ 69{
69 recipe *t = new recipe; 70 recipe *t = new recipe;
70 71
71 t->chance = 0; 72 t->chance = 0;
72 t->index = 0; 73 t->index = 0;
73 t->transmute = 0; 74 t->transmute = 0;
74 t->yield = 0; 75 t->yield = 0;
75 t->diff = 0; 76 t->diff = 0;
76 t->exp = 0; 77 t->exp = 0;
77 t->keycode = 0; 78 t->keycode = 0;
78 t->title = NULL; 79 t->title = 0;
79 t->arch_names = 0; 80 t->arch_names = 0;
80 t->arch_name = NULL; 81 t->arch_name = 0;
81 t->skill = NULL; 82 t->skill = 0;
82 t->cauldron = NULL; 83 t->cauldron = 0;
83 t->ingred = NULL; 84 t->ingred = 0;
84 t->next = NULL; 85 t->next = 0;
86
85 return t; 87 return t;
86} 88}
87 89
88/* get_formulalist() - returns pointer to the formula list */ 90/* get_formulalist() - returns pointer to the formula list */
89recipelist * 91recipelist *
110 size_t i; 112 size_t i;
111 int result = 1; 113 int result = 1;
112 114
113 for (i = 0; i < rp->arch_names; i++) 115 for (i = 0; i < rp->arch_names; i++)
114 { 116 {
115 if (archetype::find (rp->arch_name[i]) != NULL) 117 if (archetype::find (rp->arch_name[i]))
116 { 118 {
117 artifact *art = locate_recipe_artifact (rp, i); 119 artifact *art = locate_recipe_artifact (rp, i);
118 120
119 if (!art && strcmp (rp->title, "NONE") != 0) 121 if (!art && rp->title != shstr_NONE)
120 { 122 {
121 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); 123 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
122 result = 0; 124 result = 0;
123 } 125 }
124 } 126 }
128 result = 0; 130 result = 0;
129 } 131 }
130 } 132 }
131 133
132 return result; 134 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} 135}
253 136
254/* check_formulae()- since we are doing a squential search on the 137/* 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 138 * 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 139 * formula with the exact same index value. Under the new nbatches
257 * code, it is possible to have multiples of ingredients in a cauldron 140 * 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 141 * which could result in an index formula mismatch. We *don't* check for
259 * that possibility here. -b.t. 142 * that possibility here. -b.t.
260 */ 143 */
261void 144static void
262check_formulae (void) 145check_formulae ()
263{ 146{
264 recipelist *fl; 147 recipelist *fl;
265 recipe *check, *formula; 148 recipe *check, *formula;
266 int numb = 1; 149 int numb = 1;
267 150
282 165
283 LOG (llevDebug, "done.\n"); 166 LOG (llevDebug, "done.\n");
284 167
285} 168}
286 169
170/*
171 * init_formulae() - Builds up the lists of formula from the file in
172 * the libdir. -b.t.
173 */
174void
175init_formulae ()
176{
177 static int has_been_done = 0;
178 FILE *fp;
179 char filename[MAX_BUF], buf[MAX_BUF], *cp, *next;
180 recipelist *fl = init_recipelist ();
181 linked_char *tmp;
182 int value, comp;
183
184 if (!formulalist)
185 formulalist = fl;
186
187 if (has_been_done)
188 return;
189 else
190 has_been_done = 1;
191
192 sprintf (filename, "%s/formulae", settings.datadir);
193 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
194
195 object_thawer thawer (filename);
196
197 if (!thawer)
198 {
199 LOG (llevError, "Can't open %s.\n", filename);
200 return;
201 }
202
203 while (thawer.kw)
204 {
205 if (thawer.kw != KW_object)
206 if (!thawer.parse_error ("formulae file", "formulae"))
207 break;
208
209 recipe *formula = get_empty_formula ();
210 thawer.get (formula->title);
211
212 for (;;)
213 {
214 thawer.next ();
215
216 switch (thawer.kw)
217 {
218 case KW_keycode: thawer.get (formula->keycode ); break;
219 case KW_trans: thawer.get (formula->transmute); break;
220 case KW_yield: thawer.get (formula->yield ); break;
221 case KW_chance: thawer.get (formula->chance ); break;
222 case KW_exp: thawer.get (formula->exp ); break;
223 case KW_diff: thawer.get (formula->diff ); break;
224 case KW_skill: thawer.get (formula->skill ); break;
225 case KW_cauldron: thawer.get (formula->cauldron ); break;
226
227 case KW_arch:
228 {
229 build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names);
230 check_recipe (formula);
231 }
232 break;
233
234 case KW_ingred:
235 if (thawer.value)
236 {
237 int numb_ingred = 1;
238 char *cp = thawer.value;
239
240 do
241 {
242 if ((next = strchr (cp, ',')))
243 {
244 *next++ = '\0';
245 ++numb_ingred;
246 }
247
248 tmp = new linked_char;
249
250 tmp->name = cp;
251 tmp->next = formula->ingred;
252 formula->ingred = tmp;
253 /* each ingredient's ASCII value is coadded. Later on this
254 * value will be used allow us to search the formula lists
255 * quickly for the right recipe.
256 */
257 formula->index += strtoint (cp);
258 }
259 while ((cp = next));
260
261 /* now find the correct (# of ingred ordered) formulalist */
262 fl = formulalist;
263 while (numb_ingred != 1)
264 {
265 if (!fl->next)
266 fl->next = init_recipelist ();
267
268 fl = fl->next;
269 numb_ingred--;
270 }
271
272 fl->total_chance += formula->chance;
273 fl->number++;
274 formula->next = fl->items;
275 fl->items = formula;
276 }
277 break;
278
279 default:
280 goto next_object;
281 }
282 }
283
284next_object: ;
285 }
286
287 LOG (llevDebug, "done.\n");
288 /* Lastly, lets check for problems in formula we got */
289 check_formulae ();
290}
291
287/* Find a treasure with a matching name. The 'depth' parameter is 292/* Find a treasure with a matching name. The 'depth' parameter is
288 * only there to prevent infinite loops in treasure lists (a list 293 * only there to prevent infinite loops in treasure lists (a list
289 * referencing another list pointing back to the first one). */ 294 * referencing another list pointing back to the first one). */
290archetype * 295static archetype *
291find_treasure_by_name (const treasure *t, const char *name, int depth) 296find_treasure_by_name (const treasure *t, const char *name, int depth)
292{ 297{
293 if (depth > 10) 298 if (depth > 10)
294 return 0; 299 return 0;
295 300
320 } 325 }
321 326
322 return 0; 327 return 0;
323} 328}
324 329
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 * 330static const char *
419ingred_name (const char *name) 331ingred_name (const char *name)
420{ 332{
421 const char *cp = name; 333 const char *cp = name;
422 334
423 if (atoi (cp)) 335 if (atoi (cp))
424 cp = strchr (cp, ' ') + 1; 336 cp = strchr (cp, ' ') + 1;
425 337
426 return cp; 338 return cp;
339}
340
341static int
342numb_ingred (const char *buf)
343{
344 int numb;
345
346 if ((numb = atoi (buf)))
347 return numb;
348 else
349 return 1;
427} 350}
428 351
429/* strtoint() - we use this to convert buf into an integer 352/* strtoint() - we use this to convert buf into an integer
430 * equal to the coadded sum of the (lowercase) character 353 * equal to the coadded sum of the (lowercase) character
431 * ASCII values in buf (times prepended integers). 354 * ASCII values in buf (times prepended integers).
355 * Some kind of hashing.
432 */ 356 */
433
434int 357int
435strtoint (const char *buf) 358strtoint (const char *buf)
436{ 359{
437 const char *cp = ingred_name (buf); 360 const char *cp = ingred_name (buf);
438 int val = 0, len = strlen (cp), mult = numb_ingred (buf); 361 int val = 0, len = strlen (cp), mult = numb_ingred (buf);
441 { 364 {
442 val += tolower (*cp); 365 val += tolower (*cp);
443 cp++; 366 cp++;
444 len--; 367 len--;
445 } 368 }
369
446 return val * mult; 370 return val * mult;
447} 371}
448 372
449artifact * 373artifact *
450locate_recipe_artifact (const recipe *rp, size_t idx) 374locate_recipe_artifact (const recipe *rp, size_t idx)
458 return art; 382 return art;
459 383
460 return 0; 384 return 0;
461} 385}
462 386
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 * 387static recipelist *
475get_random_recipelist (void) 388get_random_recipelist ()
476{ 389{
477 recipelist *fl = NULL; 390 recipelist *fl = NULL;
478 int number = 0, roll = 0; 391 int number = 0, roll = 0;
479 392
480 /* first, determine # of recipelist we have */ 393 /* first, determine # of recipelist we have */
523 if (r < 0) 436 if (r < 0)
524 break; 437 break;
525 } 438 }
526 } 439 }
527 return rp; 440 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} 441}
560 442
561/** 443/**
562 * Split a comma separated string list into words. 444 * Split a comma separated string list into words.
563 * 445 *
577 size_t size; 459 size_t size;
578 size_t i; 460 size_t i;
579 461
580 dup = strdup (str); 462 dup = strdup (str);
581 if (dup == NULL) 463 if (dup == NULL)
582 fatal (OUT_OF_MEMORY); 464 fatal ("out of memory");
583 465
584 size = 0; 466 size = 0;
585 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 467 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))
586 size++; 468 size++;
587 469
588 *result_list = (char **) malloc (size * sizeof (*result_list)); 470 *result_list = (char **)malloc (size * sizeof (*result_list));
589 471
590 *result_size = size; 472 *result_size = size;
591 473
592 for (i = 0; i < size; i++) 474 for (i = 0; i < size; i++)
593 { 475 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines