--- deliantra/server/common/recipe.C 2006/08/13 17:16:00 1.1 +++ deliantra/server/common/recipe.C 2006/09/03 14:33:48 1.4 @@ -29,9 +29,8 @@ static recipelist *formulalist; static recipelist *init_recipelist(void) { - recipelist *tl = (recipelist *) malloc(sizeof(recipelist)); - if(tl==NULL) - fatal(OUT_OF_MEMORY); + recipelist *tl = new recipelist; + tl->total_chance=0; tl->number=0; tl->items=NULL; @@ -40,9 +39,8 @@ } static recipe *get_empty_formula(void) { - recipe *t = (recipe *) malloc(sizeof(recipe)); - if(t==NULL) - fatal(OUT_OF_MEMORY); + recipe *t = new recipe; + t->chance = 0; t->index = 0; t->transmute = 0; @@ -85,11 +83,11 @@ if (find_archetype(rp->arch_name[i]) != NULL) { artifact *art = locate_recipe_artifact(rp, i); if (!art && strcmp(rp->title, "NONE") != 0) { - LOG(llevError,"\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], rp->title); + LOG(llevError,"\nWARNING: Formula %s of %s has no artifact.\n", rp->arch_name[i], &rp->title); result = 0; } } else { - LOG(llevError,"\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], rp->title); + LOG(llevError,"\nWARNING: Can't find archetype %s for formula %s\n", rp->arch_name[i], &rp->title); result = 0; } } @@ -134,41 +132,41 @@ if (!strncmp(cp, "Object", 6)) { formula=get_empty_formula(); - formula->title = add_string(strchr(cp,' ') + 1); + formula->title = strchr(cp,' ') + 1; } else if (!strncmp(cp, "keycode", 7)) { - formula->keycode = add_string(strchr(cp,' ') + 1); + formula->keycode = strchr (cp,' ') + 1; } else if (sscanf(cp, "trans %d", &value)) { formula->transmute = (uint16)value; } else if (sscanf(cp, "yield %d", &value)) { - formula->yield = (uint16)value; + formula->yield = (uint16)value; } else if (sscanf(cp, "chance %d", &value)) { formula->chance = (uint16)value; } else if (sscanf(cp, "exp %d", &value)) { - formula->exp = (uint16)value; + formula->exp = (uint16)value; } else if (sscanf(cp, "diff %d", &value)) { - formula->diff = (uint16)value; + formula->diff = (uint16)value; } else if (!strncmp(cp, "ingred",6)) { int numb_ingred = 1; cp = strchr(cp,' ') + 1; do { if ((next=strchr(cp,','))!=NULL) {*(next++) = '\0'; numb_ingred++;} - tmp = (linked_char*) malloc(sizeof(linked_char)); - tmp->name = add_string(cp); + tmp = new linked_char; + tmp->name = cp; tmp->next = formula->ingred; formula->ingred = tmp; - /* each ingredient's ASCII value is coadded. Later on this + /* each ingredient's ASCII value is coadded. Later on this * value will be used allow us to search the formula lists - * quickly for the right recipe. - */ - formula->index += strtoint(cp); + * quickly for the right recipe. + */ + formula->index += strtoint(cp); } while ((cp=next)!=NULL); /* now find the correct (# of ingred ordered) formulalist */ fl=formulalist; while(numb_ingred!=1) { - if(!fl->next) - fl->next = init_recipelist(); - fl = fl->next; + if(!fl->next) + fl->next = init_recipelist(); + fl = fl->next; numb_ingred--; } fl->total_chance += formula->chance; @@ -177,11 +175,11 @@ fl->items = formula; } else if (!strncmp(cp, "arch",4)) { build_stringlist(strchr(cp, ' ')+1, &formula->arch_name, &formula->arch_names); - (void) check_recipe(formula); + check_recipe(formula); } else if (!strncmp(cp, "skill", 5)) { - formula->skill = add_string(strchr(cp, ' ')+1); + formula->skill = strchr(cp, ' ') + 1; } else if (!strncmp(cp, "cauldron", 8)) { - formula->cauldron = add_string(strchr(cp, ' ')+1); + formula->cauldron = strchr(cp, ' ') + 1; } else LOG(llevError,"Unknown input in file %s: %s\n", filename, buf); } @@ -211,7 +209,7 @@ if(check->index==formula->index) { LOG(llevError," ERROR: On %d ingred list: ", numb); LOG(llevError, "Formulae [%s] of %s and [%s] of %s have matching index id (%d)\n", - formula->arch_name[0],formula->title,check->arch_name[0],check->title,formula->index); + formula->arch_name[0],&formula->title,check->arch_name[0],&check->title,formula->index); } numb++; } @@ -231,7 +229,7 @@ fprintf(logfile, "\n"); while(fl) { fprintf(logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", - num_ingred, num_ingred>1?"s.":".",fl->number,fl->total_chance); + num_ingred, num_ingred>1?"s.":".",fl->number,fl->total_chance); for (formula=fl->items; formula!=NULL; formula=formula->next) { artifact *art=NULL; char buf[MAX_BUF]; @@ -239,40 +237,40 @@ for (i = 0; i < formula->arch_names; i++) { const char *string = formula->arch_name[i]; - if(find_archetype(string)!=NULL) { + if(find_archetype(string)!=NULL) { art = locate_recipe_artifact(formula, i); if (!art && strcmp(formula->title,"NONE")) - LOG(llevError,"Formula %s has no artifact\n",formula->title); - else { - if(strcmp(formula->title,"NONE")) - sprintf(buf,"%s of %s",string,formula->title); - else + LOG(llevError,"Formula %s has no artifact\n",&formula->title); + else { + if(strcmp(formula->title,"NONE")) + sprintf(buf,"%s of %s",string,&formula->title); + else sprintf(buf,"%s",string); fprintf(logfile,"%-30s(%d) bookchance %3d ",buf,formula->index, - formula->chance); - fprintf(logfile,"skill %s",formula->skill); + formula->chance); + fprintf(logfile,"skill %s",&formula->skill); fprintf(logfile,"\n"); if (formula->ingred !=NULL) { - int nval=0,tval=0; + int nval=0,tval=0; fprintf(logfile,"\tIngred: "); for (next=formula->ingred; next!=NULL; next=next->next) { - if(nval!=0) fprintf(logfile,","); - fprintf(logfile,"%s(%d)",next->name,(nval=strtoint(next->name))); - tval += nval; - } + if(nval!=0) fprintf(logfile,","); + fprintf(logfile,"%s(%d)",&next->name,(nval=strtoint(next->name))); + tval += nval; + } fprintf(logfile,"\n"); - if(tval!=formula->index) fprintf(logfile, "WARNING:ingredient list and formula values not equal.\n"); + if(tval!=formula->index) fprintf(logfile, "WARNING:ingredient list and formula values not equal.\n"); } - if (formula->skill != NULL) - fprintf(logfile, "\tSkill Required: %s", formula->skill); - if (formula->cauldron != NULL) - fprintf(logfile, "\tCauldron: %s\n", formula->cauldron); - fprintf(logfile, "\tDifficulty: %d\t Exp: %d\n", formula->diff, - formula->exp); - } - } else - LOG(llevError,"Can't find archetype:%s for formula %s\n", string, - formula->title); + if (formula->skill != NULL) + fprintf(logfile, "\tSkill Required: %s", &formula->skill); + if (formula->cauldron != NULL) + fprintf(logfile, "\tCauldron: %s\n", &formula->cauldron); + fprintf(logfile, "\tDifficulty: %d\t Exp: %d\n", formula->diff, + formula->exp); + } + } else + LOG(llevError,"Can't find archetype:%s for formula %s\n", string, + &formula->title); } } fprintf(logfile,"\n"); @@ -294,29 +292,29 @@ while (t != NULL) { if (t->name != NULL) - { - tl = find_treasurelist (t->name); - at = find_treasure_by_name (tl->items, name, depth + 1); - if (at != NULL) - return at; - } + { + tl = find_treasurelist (t->name); + at = find_treasure_by_name (tl->items, name, depth + 1); + if (at != NULL) + return at; + } else - { - if (! strcasecmp (t->item->clone.name, name)) - return t->item; - } + { + if (! strcasecmp (t->item->clone.name, name)) + return t->item; + } if (t->next_yes != NULL) - { - at = find_treasure_by_name (t->next_yes, name, depth); - if (at != NULL) - return at; - } + { + at = find_treasure_by_name (t->next_yes, name, depth); + if (at != NULL) + return at; + } if (t->next_no != NULL) - { - at = find_treasure_by_name (t->next_no, name, depth); - if (at != NULL) - return at; - } + { + at = find_treasure_by_name (t->next_no, name, depth); + if (at != NULL) + return at; + } t = t->next; } return NULL; @@ -356,14 +354,14 @@ for (at = first_archetype; at != NULL; at = at->next) { if (at->clone.title != NULL) - { - /* inefficient, but who cares? */ - sprintf (part1, "%s %s", at->clone.name, at->clone.title); - if (! strcasecmp (part1, name)) - return mult * at->clone.value; - } + { + /* inefficient, but who cares? */ + sprintf (part1, "%s %s", &at->clone.name, &at->clone.title); + if (! strcasecmp (part1, name)) + return mult * at->clone.value; + } if (! strcasecmp (at->clone.name, name)) - return mult * at->clone.value; + return mult * at->clone.value; } /* second, try to match an artifact ("arch of something") */ cp = strstr (name, " of "); @@ -374,19 +372,19 @@ strcpy (part2, cp + 4); /* find the first archetype matching the first part of the name */ for (at = first_archetype; at != NULL; at = at->next) - if (! strcasecmp (at->clone.name, part1) && at->clone.title == NULL) - break; + if (! strcasecmp (at->clone.name, part1) && at->clone.title == NULL) + break; if (at != NULL) - { - /* find the first artifact derived from that archetype (same type) */ - for (al = first_artifactlist; al != NULL; al = al->next) - if (al->type == at->clone.type) - { - for (art = al->items; art != NULL; art = art->next) - if (! strcasecmp (art->item->name, part2)) - return mult * at->clone.value * art->item->value; - } - } + { + /* find the first artifact derived from that archetype (same type) */ + for (al = first_artifactlist; al != NULL; al = al->next) + if (al->type == at->clone.type) + { + for (art = al->items; art != NULL; art = art->next) + if (! strcasecmp (art->item->name, part2)) + return mult * at->clone.value * art->item->value; + } + } } /* third, try to match a body part ("arch's something") */ cp = strstr (name, "'s "); @@ -397,16 +395,16 @@ strcpy (part2, cp + 3); /* examine all archetypes matching the first part of the name */ for (at = first_archetype; at != NULL; at = at->next) - if (! strcasecmp (at->clone.name, part1) && at->clone.title == NULL) - { - if (at->clone.randomitems != NULL) - { - at2 = find_treasure_by_name (at->clone.randomitems->items, - part2, 0); - if (at2 != NULL) - return mult * at2->clone.value * isqrt (at->clone.level * 2); - } - } + if (! strcasecmp (at->clone.name, part1) && at->clone.title == NULL) + { + if (at->clone.randomitems != NULL) + { + at2 = find_treasure_by_name (at->clone.randomitems->items, + part2, 0); + if (at2 != NULL) + return mult * at2->clone.value * isqrt (at->clone.level * 2); + } + } } /* failed to find any matching items -- formula should be checked */ return -1; @@ -426,7 +424,7 @@ fprintf (logfile, "\n"); while (fl) { fprintf(logfile, "\n Formulae with %d ingredient%s %d Formulae with total_chance=%d\n", - num_ingred, num_ingred>1?"s.":".",fl->number,fl->total_chance); + num_ingred, num_ingred>1?"s.":".",fl->number,fl->total_chance); for (formula = fl->items; formula != NULL; formula = formula->next) { artifact *art=NULL; archetype *at=NULL; @@ -435,62 +433,62 @@ for (i = 0; i < formula->arch_names; i++) { const char *string = formula->arch_name[i]; - if ((at = find_archetype (string)) != NULL) { + if ((at = find_archetype (string)) != NULL) { art = locate_recipe_artifact (formula, i); if (!art && strcmp (formula->title,"NONE")) - LOG (llevError, "Formula %s has no artifact\n", formula->title); - else - { - if (! strcmp (formula->title, "NONE")) - sprintf (buf, "%s", string); - else - sprintf (buf, "%s of %s", string, formula->title); - fprintf (logfile, "\n%-40s bookchance %3d skill %s\n", - buf, formula->chance, formula->skill); - if (formula->ingred !=NULL) - { - tcost = 0; - for (next = formula->ingred; next != NULL; next = next->next) - { - cost = find_ingred_cost (next->name); - if (cost < 0) - num_errors++; - fprintf (logfile,"\t%-33s%5ld\n", next->name, cost); - if (cost < 0 || tcost < 0) - tcost = -1; - else - tcost += cost; - } - if (art != NULL && art->item != NULL) - cost = at->clone.value * art->item->value; - else - cost = at->clone.value; - fprintf (logfile, "\t\tBuying result costs: %5ld", cost); - if (formula->yield > 1) - { - fprintf (logfile, " to %ld (max %d items)\n", - cost * formula->yield, formula->yield); - cost = cost * (formula->yield + 1L) / 2L; - } - else - fprintf (logfile, "\n"); - fprintf (logfile, "\t\tIngredients cost: %5ld\n\t\tComment: ", tcost); - if (tcost < 0) - fprintf (logfile, "Could not find some ingredients. Check the formula!\n"); - else if (tcost > cost) - fprintf (logfile, "Ingredients are much too expensive. Useless formula.\n"); - else if (tcost * 2L > cost) - fprintf (logfile, "Ingredients are too expensive.\n"); - else if (tcost * 10L < cost) - fprintf (logfile, "Ingredients are too cheap.\n"); - else - fprintf (logfile, "OK.\n"); - } - } - } - else - LOG(llevError, "Can't find archetype:%s for formula %s\n", string, - formula->title); + LOG (llevError, "Formula %s has no artifact\n", &formula->title); + else + { + if (! strcmp (formula->title, "NONE")) + sprintf (buf, "%s", string); + else + sprintf (buf, "%s of %s", string, &formula->title); + fprintf (logfile, "\n%-40s bookchance %3d skill %s\n", + buf, formula->chance, &(formula->skill)); + if (formula->ingred !=NULL) + { + tcost = 0; + for (next = formula->ingred; next != NULL; next = next->next) + { + cost = find_ingred_cost (next->name); + if (cost < 0) + num_errors++; + fprintf (logfile,"\t%-33s%5ld\n", &next->name, cost); + if (cost < 0 || tcost < 0) + tcost = -1; + else + tcost += cost; + } + if (art != NULL && art->item != NULL) + cost = at->clone.value * art->item->value; + else + cost = at->clone.value; + fprintf (logfile, "\t\tBuying result costs: %5ld", cost); + if (formula->yield > 1) + { + fprintf (logfile, " to %ld (max %d items)\n", + cost * formula->yield, formula->yield); + cost = cost * (formula->yield + 1L) / 2L; + } + else + fprintf (logfile, "\n"); + fprintf (logfile, "\t\tIngredients cost: %5ld\n\t\tComment: ", tcost); + if (tcost < 0) + fprintf (logfile, "Could not find some ingredients. Check the formula!\n"); + else if (tcost > cost) + fprintf (logfile, "Ingredients are much too expensive. Useless formula.\n"); + else if (tcost * 2L > cost) + fprintf (logfile, "Ingredients are too expensive.\n"); + else if (tcost * 10L < cost) + fprintf (logfile, "Ingredients are too cheap.\n"); + else + fprintf (logfile, "OK.\n"); + } + } + } + else + LOG(llevError, "Can't find archetype:%s for formula %s\n", string, + &formula->title); } } fprintf (logfile,"\n"); @@ -499,7 +497,7 @@ } if (num_errors > 0) fprintf (logfile, "WARNING: %d objects required by the formulae do not exist in the game.\n", - num_errors); + num_errors); } const char * ingred_name (const char *name) { @@ -597,27 +595,22 @@ LOG(llevDebug,"Freeing all the recipes\n"); for (fl=formulalist; fl!=NULL; fl=flnext) { - flnext=fl->next; + flnext=fl->next; - for (formula=fl->items; formula!=NULL; formula=next) { - next=formula->next; + for (formula=fl->items; formula!=NULL; formula=next) { + next=formula->next; - free(formula->arch_name[0]); - free(formula->arch_name); - if (formula->title) - free_string(formula->title); - if (formula->skill) - free_string(formula->skill); - if (formula->cauldron) - free_string(formula->cauldron); - for (lchar=formula->ingred; lchar; lchar=charnext) { - charnext=lchar->next; - free_string(lchar->name); - free(lchar); - } - free(formula); - } - free(fl); + free (formula->arch_name [0]); + free (formula->arch_name); + + for (lchar=formula->ingred; lchar; lchar=charnext) { + charnext=lchar->next; + delete lchar; + } + delete formula; + } + + delete fl; } } @@ -648,8 +641,7 @@ size++; *result_list = (char **) malloc(size*sizeof(*result_list)); - if (*result_list == NULL) - fatal(OUT_OF_MEMORY); + *result_size = size; for (i = 0; i < size; i++) { @@ -657,3 +649,4 @@ dup = dup+strlen(dup)+1; } } +