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 it under |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * the terms of the Affero GNU General Public License as published by the |
9 | * the terms of the Affero GNU General Public License as published by the |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * option) any later version. |
11 | * option) any later version. |
… | |
… | |
50 | static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
50 | static void build_stringlist (const char *str, char ***result_list, size_t * result_size); |
51 | |
51 | |
52 | static recipelist *formulalist; |
52 | static recipelist *formulalist; |
53 | |
53 | |
54 | static recipelist * |
54 | static recipelist * |
55 | init_recipelist (void) |
55 | init_recipelist () |
56 | { |
56 | { |
57 | recipelist *tl = new recipelist; |
57 | recipelist *tl = new recipelist; |
58 | |
58 | |
59 | tl->total_chance = 0; |
59 | tl->total_chance = 0; |
60 | tl->number = 0; |
60 | tl->number = 0; |
… | |
… | |
63 | |
63 | |
64 | return tl; |
64 | return tl; |
65 | } |
65 | } |
66 | |
66 | |
67 | static recipe * |
67 | static recipe * |
68 | get_empty_formula (void) |
68 | get_empty_formula () |
69 | { |
69 | { |
70 | recipe *t = new recipe; |
70 | recipe *t = new recipe; |
71 | |
71 | |
72 | t->chance = 0; |
72 | t->chance = 0; |
73 | t->index = 0; |
73 | t->index = 0; |
74 | t->transmute = 0; |
74 | t->transmute = 0; |
75 | t->yield = 0; |
75 | t->yield = 0; |
76 | t->diff = 0; |
76 | t->diff = 0; |
77 | t->exp = 0; |
77 | t->exp = 0; |
78 | t->keycode = 0; |
78 | t->keycode = 0; |
79 | t->title = NULL; |
79 | t->title = 0; |
80 | t->arch_names = 0; |
80 | t->arch_names = 0; |
81 | t->arch_name = NULL; |
81 | t->arch_name = 0; |
82 | t->skill = NULL; |
82 | t->skill = 0; |
83 | t->cauldron = NULL; |
83 | t->cauldron = 0; |
84 | t->ingred = NULL; |
84 | t->ingred = 0; |
85 | t->next = NULL; |
85 | t->next = 0; |
|
|
86 | |
86 | return t; |
87 | return t; |
87 | } |
88 | } |
88 | |
89 | |
89 | /* get_formulalist() - returns pointer to the formula list */ |
90 | /* get_formulalist() - returns pointer to the formula list */ |
90 | recipelist * |
91 | recipelist * |
… | |
… | |
138 | * formula with the exact same index value. Under the new nbatches |
139 | * 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 | * 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 | * which could result in an index formula mismatch. We *don't* check for |
141 | * that possibility here. -b.t. |
142 | * that possibility here. -b.t. |
142 | */ |
143 | */ |
143 | void |
144 | static void |
144 | check_formulae (void) |
145 | check_formulae () |
145 | { |
146 | { |
146 | recipelist *fl; |
147 | recipelist *fl; |
147 | recipe *check, *formula; |
148 | recipe *check, *formula; |
148 | int numb = 1; |
149 | int numb = 1; |
149 | |
150 | |
… | |
… | |
169 | /* |
170 | /* |
170 | * init_formulae() - Builds up the lists of formula from the file in |
171 | * init_formulae() - Builds up the lists of formula from the file in |
171 | * the libdir. -b.t. |
172 | * the libdir. -b.t. |
172 | */ |
173 | */ |
173 | void |
174 | void |
174 | init_formulae (void) |
175 | init_formulae () |
175 | { |
176 | { |
176 | static int has_been_done = 0; |
177 | static int has_been_done = 0; |
177 | FILE *fp; |
178 | char *next; |
178 | char filename[MAX_BUF], buf[MAX_BUF], *cp, *next; |
|
|
179 | recipe *formula = NULL; |
|
|
180 | recipelist *fl = init_recipelist (); |
179 | recipelist *fl = init_recipelist (); |
181 | linked_char *tmp; |
180 | linked_char *tmp; |
182 | int value, comp; |
|
|
183 | |
181 | |
184 | if (!formulalist) |
182 | if (!formulalist) |
185 | formulalist = fl; |
183 | formulalist = fl; |
186 | |
184 | |
187 | if (has_been_done) |
185 | if (has_been_done) |
188 | return; |
186 | return; |
189 | else |
187 | else |
190 | has_been_done = 1; |
188 | has_been_done = 1; |
191 | |
189 | |
192 | sprintf (filename, "%s/formulae", settings.datadir); |
190 | object_thawer thawer (settings.datadir, "formulae"); |
193 | LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename); |
191 | |
194 | if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) |
192 | if (!thawer) |
195 | { |
193 | { |
196 | LOG (llevError, "Can't open %s.\n", filename); |
194 | LOG (llevError, "Can't open %s.\n", thawer.name); |
197 | return; |
195 | return; |
198 | } |
196 | } |
199 | |
197 | |
200 | while (fgets (buf, MAX_BUF, fp) != NULL) |
198 | while (thawer.kw) |
201 | { |
199 | { |
202 | if (*buf == '#') |
200 | if (thawer.kw != KW_object) |
203 | continue; |
201 | if (!thawer.parse_error ("formulae file")) |
204 | if ((cp = strchr (buf, '\n')) != NULL) |
202 | break; |
205 | *cp = '\0'; |
|
|
206 | cp = buf; |
|
|
207 | while (*cp == ' ') /* Skip blanks */ |
|
|
208 | cp++; |
|
|
209 | |
203 | |
210 | if (!strncmp (cp, "object", 6)) |
|
|
211 | { |
|
|
212 | formula = get_empty_formula (); |
204 | recipe *formula = get_empty_formula (); |
213 | formula->title = strchr (cp, ' ') + 1; |
205 | thawer.get (formula->title); |
|
|
206 | |
|
|
207 | for (;;) |
214 | } |
208 | { |
215 | else if (!strncmp (cp, "keycode", 7)) |
209 | thawer.next (); |
216 | formula->keycode = strchr (cp, ' ') + 1; |
|
|
217 | else if (sscanf (cp, "trans %d", &value)) |
|
|
218 | formula->transmute = (uint16) value; |
|
|
219 | else if (sscanf (cp, "yield %d", &value)) |
|
|
220 | formula->yield = (uint16) value; |
|
|
221 | else if (sscanf (cp, "chance %d", &value)) |
|
|
222 | formula->chance = (uint16) value; |
|
|
223 | else if (sscanf (cp, "exp %d", &value)) |
|
|
224 | formula->exp = (uint16) value; |
|
|
225 | else if (sscanf (cp, "diff %d", &value)) |
|
|
226 | formula->diff = (uint16) value; |
|
|
227 | else if (!strncmp (cp, "ingred", 6)) |
|
|
228 | { |
|
|
229 | int numb_ingred = 1; |
|
|
230 | |
210 | |
231 | cp = strchr (cp, ' ') + 1; |
211 | switch (thawer.kw) |
232 | do |
|
|
233 | { |
212 | { |
234 | if ((next = strchr (cp, ',')) != NULL) |
213 | case KW_keycode: thawer.get (formula->keycode ); break; |
|
|
214 | case KW_trans: thawer.get (formula->transmute); break; |
|
|
215 | case KW_yield: thawer.get (formula->yield ); break; |
|
|
216 | case KW_chance: thawer.get (formula->chance ); break; |
|
|
217 | case KW_exp: thawer.get (formula->exp ); break; |
|
|
218 | case KW_diff: thawer.get (formula->diff ); break; |
|
|
219 | case KW_skill: thawer.get (formula->skill ); break; |
|
|
220 | case KW_cauldron: thawer.get (formula->cauldron ); break; |
|
|
221 | |
|
|
222 | case KW_arch: |
235 | { |
223 | { |
236 | *(next++) = '\0'; |
224 | build_stringlist (thawer.value_nn, &formula->arch_name, &formula->arch_names); |
237 | numb_ingred++; |
225 | check_recipe (formula); |
238 | } |
226 | } |
|
|
227 | break; |
239 | |
228 | |
|
|
229 | case KW_ingred: |
|
|
230 | if (thawer.value) |
|
|
231 | { |
|
|
232 | int numb_ingred = 1; |
|
|
233 | char *cp = thawer.value; |
|
|
234 | |
|
|
235 | do |
|
|
236 | { |
|
|
237 | if ((next = strchr (cp, ','))) |
|
|
238 | { |
|
|
239 | *next++ = '\0'; |
|
|
240 | ++numb_ingred; |
|
|
241 | } |
|
|
242 | |
240 | tmp = new linked_char; |
243 | tmp = new linked_char; |
241 | |
244 | |
242 | tmp->name = cp; |
245 | tmp->name = cp; |
243 | tmp->next = formula->ingred; |
246 | tmp->next = formula->ingred; |
244 | formula->ingred = tmp; |
247 | formula->ingred = tmp; |
245 | /* each ingredient's ASCII value is coadded. Later on this |
248 | /* each ingredient's ASCII value is coadded. Later on this |
246 | * value will be used allow us to search the formula lists |
249 | * value will be used allow us to search the formula lists |
247 | * quickly for the right recipe. |
250 | * quickly for the right recipe. |
248 | */ |
251 | */ |
249 | formula->index += strtoint (cp); |
252 | formula->index += strtoint (cp); |
|
|
253 | } |
|
|
254 | while ((cp = next)); |
|
|
255 | |
|
|
256 | /* now find the correct (# of ingred ordered) formulalist */ |
|
|
257 | fl = formulalist; |
|
|
258 | while (numb_ingred != 1) |
|
|
259 | { |
|
|
260 | if (!fl->next) |
|
|
261 | fl->next = init_recipelist (); |
|
|
262 | |
|
|
263 | fl = fl->next; |
|
|
264 | numb_ingred--; |
|
|
265 | } |
|
|
266 | |
|
|
267 | fl->total_chance += formula->chance; |
|
|
268 | fl->number++; |
|
|
269 | formula->next = fl->items; |
|
|
270 | fl->items = formula; |
|
|
271 | } |
|
|
272 | break; |
|
|
273 | |
|
|
274 | default: |
|
|
275 | delete formula; |
|
|
276 | case KW_EOF: |
|
|
277 | case KW_object: |
|
|
278 | goto next_object; |
250 | } |
279 | } |
251 | while ((cp = next) != NULL); |
|
|
252 | |
|
|
253 | /* now find the correct (# of ingred ordered) formulalist */ |
|
|
254 | fl = formulalist; |
|
|
255 | while (numb_ingred != 1) |
|
|
256 | { |
|
|
257 | if (!fl->next) |
|
|
258 | fl->next = init_recipelist (); |
|
|
259 | |
|
|
260 | fl = fl->next; |
|
|
261 | numb_ingred--; |
|
|
262 | } |
|
|
263 | |
|
|
264 | fl->total_chance += formula->chance; |
|
|
265 | fl->number++; |
|
|
266 | formula->next = fl->items; |
|
|
267 | fl->items = formula; |
|
|
268 | } |
|
|
269 | else if (!strncmp (cp, "arch", 4)) |
|
|
270 | { |
280 | } |
271 | build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); |
|
|
272 | check_recipe (formula); |
|
|
273 | } |
|
|
274 | else if (!strncmp (cp, "skill", 5)) |
|
|
275 | formula->skill = strchr (cp, ' ') + 1; |
|
|
276 | else if (!strncmp (cp, "cauldron", 8)) |
|
|
277 | formula->cauldron = strchr (cp, ' ') + 1; |
|
|
278 | else |
|
|
279 | LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); |
|
|
280 | } |
|
|
281 | |
281 | |
282 | LOG (llevDebug, "done.\n"); |
282 | next_object: ; |
283 | close_and_delete (fp, comp); |
283 | } |
|
|
284 | |
284 | /* Lastly, lets check for problems in formula we got */ |
285 | /* Lastly, lets check for problems in formula we got */ |
285 | check_formulae (); |
286 | check_formulae (); |
286 | } |
287 | } |
287 | |
288 | |
288 | /* Find a treasure with a matching name. The 'depth' parameter is |
289 | /* Find a treasure with a matching name. The 'depth' parameter is |
289 | * only there to prevent infinite loops in treasure lists (a list |
290 | * only there to prevent infinite loops in treasure lists (a list |
290 | * referencing another list pointing back to the first one). */ |
291 | * referencing another list pointing back to the first one). */ |
291 | archetype * |
292 | static archetype * |
292 | find_treasure_by_name (const treasure *t, const char *name, int depth) |
293 | find_treasure_by_name (const treasure *t, const char *name, int depth) |
293 | { |
294 | { |
294 | if (depth > 10) |
295 | if (depth > 10) |
295 | return 0; |
296 | return 0; |
296 | |
297 | |
… | |
… | |
321 | } |
322 | } |
322 | |
323 | |
323 | return 0; |
324 | return 0; |
324 | } |
325 | } |
325 | |
326 | |
326 | /* If several archetypes have the same name, the value of the first |
|
|
327 | * one with that name will be returned. This happens for the |
|
|
328 | * mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the |
|
|
329 | * monsters' body parts, there may be several monsters with the same |
|
|
330 | * name. This is not a problem if these monsters have the same level |
|
|
331 | * (e.g. sage & c_sage) or if only one of the monsters generates the |
|
|
332 | * body parts that we are looking for (e.g. big_dragon and |
|
|
333 | * big_dragon_worthless). */ |
|
|
334 | long |
|
|
335 | find_ingred_cost (const char *name) |
|
|
336 | { |
|
|
337 | archetype *at2; |
|
|
338 | artifactlist *al; |
|
|
339 | artifact *art; |
|
|
340 | long mult; |
|
|
341 | char *cp; |
|
|
342 | char part1[100]; |
|
|
343 | char part2[100]; |
|
|
344 | |
|
|
345 | /* same as atoi(), but skip number */ |
|
|
346 | mult = 0; |
|
|
347 | while (isdigit (*name)) |
|
|
348 | { |
|
|
349 | mult = 10 * mult + (*name - '0'); |
|
|
350 | name++; |
|
|
351 | } |
|
|
352 | |
|
|
353 | if (mult > 0) |
|
|
354 | name++; |
|
|
355 | else |
|
|
356 | mult = 1; |
|
|
357 | |
|
|
358 | /* first, try to match the name of an archetype */ |
|
|
359 | for_all_archetypes (at) |
|
|
360 | { |
|
|
361 | if (at->title != NULL) |
|
|
362 | { |
|
|
363 | /* inefficient, but who cares? */ |
|
|
364 | sprintf (part1, "%s %s", &at->object::name, &at->title); |
|
|
365 | if (!strcasecmp (part1, name)) |
|
|
366 | return mult * at->value; |
|
|
367 | } |
|
|
368 | if (!strcasecmp (at->object::name, name)) |
|
|
369 | return mult * at->value; |
|
|
370 | } |
|
|
371 | |
|
|
372 | /* second, try to match an artifact ("arch of something") */ |
|
|
373 | cp = strstr (name, " of "); |
|
|
374 | if (cp != NULL) |
|
|
375 | { |
|
|
376 | strcpy (part1, name); |
|
|
377 | part1[cp - name] = '\0'; |
|
|
378 | strcpy (part2, cp + 4); |
|
|
379 | |
|
|
380 | /* find the first archetype matching the first part of the name */ |
|
|
381 | for_all_archetypes (at) |
|
|
382 | if (at->object::name.eq_nc (part1) && !at->title) |
|
|
383 | { |
|
|
384 | /* find the first artifact derived from that archetype (same type) */ |
|
|
385 | for (al = first_artifactlist; al; al = al->next) |
|
|
386 | if (al->type == at->type) |
|
|
387 | { |
|
|
388 | for (art = al->items; art; art = art->next) |
|
|
389 | if (!strcasecmp (art->item->name, part2)) |
|
|
390 | return mult * at->value * art->item->value; |
|
|
391 | } |
|
|
392 | } |
|
|
393 | } |
|
|
394 | |
|
|
395 | /* third, try to match a body part ("arch's something") */ |
|
|
396 | cp = strstr (name, "'s "); |
|
|
397 | if (cp) |
|
|
398 | { |
|
|
399 | strcpy (part1, name); |
|
|
400 | part1[cp - name] = '\0'; |
|
|
401 | strcpy (part2, cp + 3); |
|
|
402 | /* examine all archetypes matching the first part of the name */ |
|
|
403 | for_all_archetypes (at) |
|
|
404 | if (at->object::name.eq_nc (part1) && !at->title) |
|
|
405 | { |
|
|
406 | if (at->randomitems) |
|
|
407 | { |
|
|
408 | at2 = find_treasure_by_name (at->randomitems->items, part2, 0); |
|
|
409 | if (at2) |
|
|
410 | return mult * at2->value * isqrt (at->level * 2); |
|
|
411 | } |
|
|
412 | } |
|
|
413 | } |
|
|
414 | |
|
|
415 | /* failed to find any matching items -- formula should be checked */ |
|
|
416 | return -1; |
|
|
417 | } |
|
|
418 | |
|
|
419 | const char * |
327 | static const char * |
420 | ingred_name (const char *name) |
328 | ingred_name (const char *name) |
421 | { |
329 | { |
422 | const char *cp = name; |
330 | const char *cp = name; |
423 | |
331 | |
424 | if (atoi (cp)) |
332 | if (atoi (cp)) |
425 | cp = strchr (cp, ' ') + 1; |
333 | cp = strchr (cp, ' ') + 1; |
426 | |
334 | |
427 | return cp; |
335 | return cp; |
428 | } |
336 | } |
429 | |
337 | |
430 | int |
338 | static int |
431 | numb_ingred (const char *buf) |
339 | numb_ingred (const char *buf) |
432 | { |
340 | { |
433 | int numb; |
341 | int numb; |
434 | |
342 | |
435 | if ((numb = atoi (buf))) |
343 | if ((numb = atoi (buf))) |
… | |
… | |
471 | return art; |
379 | return art; |
472 | |
380 | |
473 | return 0; |
381 | return 0; |
474 | } |
382 | } |
475 | |
383 | |
476 | recipelist * |
384 | static recipelist * |
477 | get_random_recipelist (void) |
385 | get_random_recipelist () |
478 | { |
386 | { |
479 | recipelist *fl = NULL; |
387 | recipelist *fl = NULL; |
480 | int number = 0, roll = 0; |
388 | int number = 0, roll = 0; |
481 | |
389 | |
482 | /* first, determine # of recipelist we have */ |
390 | /* first, determine # of recipelist we have */ |
… | |
… | |
525 | if (r < 0) |
433 | if (r < 0) |
526 | break; |
434 | break; |
527 | } |
435 | } |
528 | } |
436 | } |
529 | return rp; |
437 | return rp; |
530 | } |
|
|
531 | |
|
|
532 | void |
|
|
533 | free_all_recipes (void) |
|
|
534 | { |
|
|
535 | recipelist *fl = formulalist, *flnext; |
|
|
536 | recipe *formula = NULL, *next; |
|
|
537 | linked_char *lchar, *charnext; |
|
|
538 | |
|
|
539 | LOG (llevDebug, "Freeing all the recipes\n"); |
|
|
540 | for (fl = formulalist; fl != NULL; fl = flnext) |
|
|
541 | { |
|
|
542 | flnext = fl->next; |
|
|
543 | |
|
|
544 | for (formula = fl->items; formula != NULL; formula = next) |
|
|
545 | { |
|
|
546 | next = formula->next; |
|
|
547 | |
|
|
548 | free (formula->arch_name[0]); |
|
|
549 | free (formula->arch_name); |
|
|
550 | |
|
|
551 | for (lchar = formula->ingred; lchar; lchar = charnext) |
|
|
552 | { |
|
|
553 | charnext = lchar->next; |
|
|
554 | delete lchar; |
|
|
555 | } |
|
|
556 | delete formula; |
|
|
557 | } |
|
|
558 | |
|
|
559 | delete fl; |
|
|
560 | } |
|
|
561 | } |
438 | } |
562 | |
439 | |
563 | /** |
440 | /** |
564 | * Split a comma separated string list into words. |
441 | * Split a comma separated string list into words. |
565 | * |
442 | * |
… | |
… | |
578 | char *p; |
455 | char *p; |
579 | size_t size; |
456 | size_t size; |
580 | size_t i; |
457 | size_t i; |
581 | |
458 | |
582 | dup = strdup (str); |
459 | dup = strdup (str); |
583 | if (dup == NULL) |
|
|
584 | fatal (OUT_OF_MEMORY); |
|
|
585 | |
460 | |
586 | size = 0; |
461 | size = 0; |
587 | for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |
462 | for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) |
588 | size++; |
463 | size++; |
589 | |
464 | |
590 | *result_list = (char **) malloc (size * sizeof (*result_list)); |
465 | *result_list = (char **)malloc (size * sizeof (*result_list)); |
591 | |
466 | |
592 | *result_size = size; |
467 | *result_size = size; |
593 | |
468 | |
594 | for (i = 0; i < size; i++) |
469 | for (i = 0; i < size; i++) |
595 | { |
470 | { |