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.10 by elmex, Thu Dec 14 00:08:52 2006 UTC vs.
Revision 1.18 by root, Fri Feb 16 19:43:41 2007 UTC

10 * Ingredients are just comma delimited list of archetype (or object) 10 * Ingredients are just comma delimited list of archetype (or object)
11 * names. 11 * names.
12 */ 12 */
13 13
14/* Example 'formula' entry in libdir/formulae: 14/* Example 'formula' entry in libdir/formulae:
15 * Object transparency 15 * object transparency
16 * chance 10 16 * chance 10
17 * ingred dust of beholdereye,gem 17 * ingred dust of beholdereye,gem
18 * arch potion_generic 18 * arch potion_generic
19 */ 19 */
20 20
21#include <cctype>
22
21#include <global.h> 23#include <global.h>
22#include <object.h> 24#include <object.h>
23#include <ctype.h>
24 25
25static void build_stringlist (const char *str, char ***result_list, size_t * result_size); 26static void build_stringlist (const char *str, char ***result_list, size_t * result_size);
26 27
27static recipelist *formulalist; 28static recipelist *formulalist;
28 29
59 t->next = NULL; 60 t->next = NULL;
60 return t; 61 return t;
61} 62}
62 63
63/* get_formulalist() - returns pointer to the formula list */ 64/* get_formulalist() - returns pointer to the formula list */
64
65recipelist * 65recipelist *
66get_formulalist (int i) 66get_formulalist (int i)
67{ 67{
68 recipelist *fl = formulalist; 68 recipelist *fl = formulalist;
69 int number = i; 69 int number = i;
72 { 72 {
73 if (!(fl = fl->next)) 73 if (!(fl = fl->next))
74 break; 74 break;
75 number--; 75 number--;
76 } 76 }
77
77 return fl; 78 return fl;
78} 79}
79 80
80/* check_recipe() - makes sure we actually have the requested artifact 81/* check_recipe() - makes sure we actually have the requested artifact
81 * and archetype. */ 82 * and archetype. */
82
83static int 83static int
84check_recipe (const recipe *rp) 84check_recipe (const recipe *rp)
85{ 85{
86 size_t i; 86 size_t i;
87 int result; 87 int result = 1;
88 88
89 result = 1;
90 for (i = 0; i < rp->arch_names; i++) 89 for (i = 0; i < rp->arch_names; i++)
91 { 90 {
92 if (archetype::find (rp->arch_name[i]) != NULL) 91 if (archetype::find (rp->arch_name[i]) != NULL)
93 { 92 {
94 artifact *art = locate_recipe_artifact (rp, i); 93 artifact *art = locate_recipe_artifact (rp, i);
95 94
96 if (!art && strcmp (rp->title, "NONE") != 0) 95 if (!art && strcmp (rp->title, "NONE") != 0)
97 { 96 {
98 LOG (llevError, "\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); 97 LOG (llevError, "WARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title);
99 result = 0; 98 result = 0;
100 } 99 }
101 } 100 }
102 else 101 else
103 { 102 {
104 LOG (llevError, "\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); 103 LOG (llevError, "WARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title);
105 result = 0; 104 result = 0;
106 } 105 }
107 } 106 }
108 107
109 return result; 108 return result;
110} 109}
111
112 110
113/* 111/*
114 * init_formulae() - Builds up the lists of formula from the file in 112 * init_formulae() - Builds up the lists of formula from the file in
115 * the libdir. -b.t. 113 * the libdir. -b.t.
116 */ 114 */
117
118void 115void
119init_formulae (void) 116init_formulae (void)
120{ 117{
121 static int has_been_done = 0; 118 static int has_been_done = 0;
122 FILE *fp; 119 FILE *fp;
133 return; 130 return;
134 else 131 else
135 has_been_done = 1; 132 has_been_done = 1;
136 133
137 sprintf (filename, "%s/formulae", settings.datadir); 134 sprintf (filename, "%s/formulae", settings.datadir);
138 LOG (llevDebug, "Reading alchemical formulae from %s...", filename); 135 LOG (llevDebug, "Reading alchemical formulae from %s...\n", filename);
139 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL) 136 if ((fp = open_and_uncompress (filename, 0, &comp)) == NULL)
140 { 137 {
141 LOG (llevError, "Can't open %s.\n", filename); 138 LOG (llevError, "Can't open %s.\n", filename);
142 return; 139 return;
143 } 140 }
150 *cp = '\0'; 147 *cp = '\0';
151 cp = buf; 148 cp = buf;
152 while (*cp == ' ') /* Skip blanks */ 149 while (*cp == ' ') /* Skip blanks */
153 cp++; 150 cp++;
154 151
155 if (!strncmp (cp, "Object", 6)) 152 if (!strncmp (cp, "object", 6))
156 { 153 {
157 formula = get_empty_formula (); 154 formula = get_empty_formula ();
158 formula->title = strchr (cp, ' ') + 1; 155 formula->title = strchr (cp, ' ') + 1;
159 } 156 }
160 else if (!strncmp (cp, "keycode", 7)) 157 else if (!strncmp (cp, "keycode", 7))
161 {
162 formula->keycode = strchr (cp, ' ') + 1; 158 formula->keycode = strchr (cp, ' ') + 1;
163 }
164 else if (sscanf (cp, "trans %d", &value)) 159 else if (sscanf (cp, "trans %d", &value))
165 {
166 formula->transmute = (uint16) value; 160 formula->transmute = (uint16) value;
167 }
168 else if (sscanf (cp, "yield %d", &value)) 161 else if (sscanf (cp, "yield %d", &value))
169 {
170 formula->yield = (uint16) value; 162 formula->yield = (uint16) value;
171 }
172 else if (sscanf (cp, "chance %d", &value)) 163 else if (sscanf (cp, "chance %d", &value))
173 {
174 formula->chance = (uint16) value; 164 formula->chance = (uint16) value;
175 }
176 else if (sscanf (cp, "exp %d", &value)) 165 else if (sscanf (cp, "exp %d", &value))
177 {
178 formula->exp = (uint16) value; 166 formula->exp = (uint16) value;
179 }
180 else if (sscanf (cp, "diff %d", &value)) 167 else if (sscanf (cp, "diff %d", &value))
181 {
182 formula->diff = (uint16) value; 168 formula->diff = (uint16) value;
183 }
184 else if (!strncmp (cp, "ingred", 6)) 169 else if (!strncmp (cp, "ingred", 6))
185 { 170 {
186 int numb_ingred = 1; 171 int numb_ingred = 1;
187 172
188 cp = strchr (cp, ' ') + 1; 173 cp = strchr (cp, ' ') + 1;
191 if ((next = strchr (cp, ',')) != NULL) 176 if ((next = strchr (cp, ',')) != NULL)
192 { 177 {
193 *(next++) = '\0'; 178 *(next++) = '\0';
194 numb_ingred++; 179 numb_ingred++;
195 } 180 }
181
196 tmp = new linked_char; 182 tmp = new linked_char;
197 183
198 tmp->name = cp; 184 tmp->name = cp;
199 tmp->next = formula->ingred; 185 tmp->next = formula->ingred;
200 formula->ingred = tmp; 186 formula->ingred = tmp;
203 * quickly for the right recipe. 189 * quickly for the right recipe.
204 */ 190 */
205 formula->index += strtoint (cp); 191 formula->index += strtoint (cp);
206 } 192 }
207 while ((cp = next) != NULL); 193 while ((cp = next) != NULL);
194
208 /* now find the correct (# of ingred ordered) formulalist */ 195 /* now find the correct (# of ingred ordered) formulalist */
209 fl = formulalist; 196 fl = formulalist;
210 while (numb_ingred != 1) 197 while (numb_ingred != 1)
211 { 198 {
212 if (!fl->next) 199 if (!fl->next)
213 fl->next = init_recipelist (); 200 fl->next = init_recipelist ();
201
214 fl = fl->next; 202 fl = fl->next;
215 numb_ingred--; 203 numb_ingred--;
216 } 204 }
205
217 fl->total_chance += formula->chance; 206 fl->total_chance += formula->chance;
218 fl->number++; 207 fl->number++;
219 formula->next = fl->items; 208 formula->next = fl->items;
220 fl->items = formula; 209 fl->items = formula;
221 } 210 }
223 { 212 {
224 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names); 213 build_stringlist (strchr (cp, ' ') + 1, &formula->arch_name, &formula->arch_names);
225 check_recipe (formula); 214 check_recipe (formula);
226 } 215 }
227 else if (!strncmp (cp, "skill", 5)) 216 else if (!strncmp (cp, "skill", 5))
228 {
229 formula->skill = strchr (cp, ' ') + 1; 217 formula->skill = strchr (cp, ' ') + 1;
230 }
231 else if (!strncmp (cp, "cauldron", 8)) 218 else if (!strncmp (cp, "cauldron", 8))
232 {
233 formula->cauldron = strchr (cp, ' ') + 1; 219 formula->cauldron = strchr (cp, ' ') + 1;
234 }
235 else 220 else
236 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf); 221 LOG (llevError, "Unknown input in file %s: %s\n", filename, buf);
237 } 222 }
223
238 LOG (llevDebug, "done.\n"); 224 LOG (llevDebug, "done.\n");
239 close_and_delete (fp, comp); 225 close_and_delete (fp, comp);
240 /* Lastly, lets check for problems in formula we got */ 226 /* Lastly, lets check for problems in formula we got */
241 check_formulae (); 227 check_formulae ();
242} 228}
253{ 239{
254 recipelist *fl; 240 recipelist *fl;
255 recipe *check, *formula; 241 recipe *check, *formula;
256 int numb = 1; 242 int numb = 1;
257 243
258 LOG (llevDebug, "Checking formulae lists..."); 244 LOG (llevDebug, "Checking formulae lists...\n");
259 245
260 for (fl = formulalist; fl != NULL; fl = fl->next) 246 for (fl = formulalist; fl != NULL; fl = fl->next)
261 { 247 {
262 for (formula = fl->items; formula != NULL; formula = formula->next) 248 for (formula = fl->items; formula != NULL; formula = formula->next)
263 for (check = formula->next; check != NULL; check = check->next) 249 for (check = formula->next; check != NULL; check = check->next)
301 287
302 if (archetype::find (string) != NULL) 288 if (archetype::find (string) != NULL)
303 { 289 {
304 art = locate_recipe_artifact (formula, i); 290 art = locate_recipe_artifact (formula, i);
305 if (!art && strcmp (formula->title, "NONE")) 291 if (!art && strcmp (formula->title, "NONE"))
306 LOG (llevError, "Formula %s has no artifact\n", &formula->title); 292 LOG (llevError, "Formula %s has no artifact!\n", &formula->title);
307 else 293 else
308 { 294 {
309 if (strcmp (formula->title, "NONE")) 295 if (strcmp (formula->title, "NONE"))
310 sprintf (buf, "%s of %s", string, &formula->title); 296 sprintf (buf, "%s of %s", string, &formula->title);
311 else 297 else
372 return at; 358 return at;
373 } 359 }
374 } 360 }
375 else 361 else
376 { 362 {
377 if (!strcasecmp (t->item->clone.name, name)) 363 if (t->item && !strcasecmp (t->item->clone.name, name))
378 return t->item; 364 return t->item;
379 } 365 }
380 366
381 if (t->next_yes) 367 if (t->next_yes)
382 { 368 {
421 while (isdigit (*name)) 407 while (isdigit (*name))
422 { 408 {
423 mult = 10 * mult + (*name - '0'); 409 mult = 10 * mult + (*name - '0');
424 name++; 410 name++;
425 } 411 }
412
426 if (mult > 0) 413 if (mult > 0)
427 name++; 414 name++;
428 else 415 else
429 mult = 1; 416 mult = 1;
417
430 /* first, try to match the name of an archetype */ 418 /* first, try to match the name of an archetype */
431 for (at = first_archetype; at != NULL; at = at->next) 419 for (at = first_archetype; at != NULL; at = at->next)
432 { 420 {
433 if (at->clone.title != NULL) 421 if (at->clone.title != NULL)
434 { 422 {
438 return mult * at->clone.value; 426 return mult * at->clone.value;
439 } 427 }
440 if (!strcasecmp (at->clone.name, name)) 428 if (!strcasecmp (at->clone.name, name))
441 return mult * at->clone.value; 429 return mult * at->clone.value;
442 } 430 }
431
443 /* second, try to match an artifact ("arch of something") */ 432 /* second, try to match an artifact ("arch of something") */
444 cp = strstr (name, " of "); 433 cp = strstr (name, " of ");
445 if (cp != NULL) 434 if (cp != NULL)
446 { 435 {
447 strcpy (part1, name); 436 strcpy (part1, name);
461 if (!strcasecmp (art->item->name, part2)) 450 if (!strcasecmp (art->item->name, part2))
462 return mult * at->clone.value * art->item->value; 451 return mult * at->clone.value * art->item->value;
463 } 452 }
464 } 453 }
465 } 454 }
455
466 /* third, try to match a body part ("arch's something") */ 456 /* third, try to match a body part ("arch's something") */
467 cp = strstr (name, "'s "); 457 cp = strstr (name, "'s ");
468 if (cp != NULL) 458 if (cp)
469 { 459 {
470 strcpy (part1, name); 460 strcpy (part1, name);
471 part1[cp - name] = '\0'; 461 part1[cp - name] = '\0';
472 strcpy (part2, cp + 3); 462 strcpy (part2, cp + 3);
473 /* examine all archetypes matching the first part of the name */ 463 /* examine all archetypes matching the first part of the name */
480 if (at2) 470 if (at2)
481 return mult * at2->clone.value * isqrt (at->clone.level * 2); 471 return mult * at2->clone.value * isqrt (at->clone.level * 2);
482 } 472 }
483 } 473 }
484 } 474 }
475
485 /* failed to find any matching items -- formula should be checked */ 476 /* failed to find any matching items -- formula should be checked */
486 return -1; 477 return -1;
487} 478}
488 479
489/* code copied from dump_alchemy() and modified by Raphael Quinet */ 480/* code copied from dump_alchemy() and modified by Raphael Quinet */
609} 600}
610 601
611artifact * 602artifact *
612locate_recipe_artifact (const recipe *rp, size_t idx) 603locate_recipe_artifact (const recipe *rp, size_t idx)
613{ 604{
614 object *item = get_archetype (rp->arch_name[idx]); 605 archetype *at = archetype::find (rp->arch_name [idx]);
615 artifactlist *at = NULL;
616 artifact *art = NULL;
617 606
618 if (!item) 607 if (at)
619 return (artifact *) NULL; 608 if (artifactlist *al = find_artifactlist (at->clone.type))
620
621 if ((at = find_artifactlist (item->type)))
622 for (art = at->items; art; art = art->next) 609 for (artifact *art = al->items; art; art = art->next)
623 if (!strcmp (art->item->name, rp->title)) 610 if (art->item->name == rp->title)
624 break; 611 return art;
625 612
626 item->destroy ();
627
628 return art; 613 return 0;
629} 614}
630 615
631int 616int
632numb_ingred (const char *buf) 617numb_ingred (const char *buf)
633{ 618{
649 for (fl = get_formulalist (1); fl; fl = fl->next) 634 for (fl = get_formulalist (1); fl; fl = fl->next)
650 number++; 635 number++;
651 636
652 /* now, randomly choose one */ 637 /* now, randomly choose one */
653 if (number > 0) 638 if (number > 0)
654 roll = RANDOM () % number; 639 roll = rndm (number);
655 640
656 fl = get_formulalist (1); 641 fl = get_formulalist (1);
657 while (roll && fl) 642 while (roll && fl)
658 { 643 {
659 if (fl->next) 644 if (fl->next)
682 if ((fl = get_random_recipelist ()) == NULL) 667 if ((fl = get_random_recipelist ()) == NULL)
683 return rp; 668 return rp;
684 669
685 if (fl->total_chance > 0) 670 if (fl->total_chance > 0)
686 { 671 {
687 r = RANDOM () % fl->total_chance; 672 r = rndm (fl->total_chance);
688 for (rp = fl->items; rp; rp = rp->next) 673 for (rp = fl->items; rp; rp = rp->next)
689 { 674 {
690 r -= rp->chance; 675 r -= rp->chance;
691 if (r < 0) 676 if (r < 0)
692 break; 677 break;
743 char *dup; 728 char *dup;
744 char *p; 729 char *p;
745 size_t size; 730 size_t size;
746 size_t i; 731 size_t i;
747 732
748 dup = strdup_local (str); 733 dup = strdup (str);
749 if (dup == NULL) 734 if (dup == NULL)
750 fatal (OUT_OF_MEMORY); 735 fatal (OUT_OF_MEMORY);
751 736
752 size = 0; 737 size = 0;
753 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ",")) 738 for (p = strtok (dup, ","); p != NULL; p = strtok (NULL, ","))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines