Crossfire Server, Trunk  1.75.0
recipe.cpp File Reference
#include "global.h"
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include "object.h"
#include "assets.h"
#include "AssetsManager.h"
+ Include dependency graph for recipe.cpp:

Go to the source code of this file.

Functions

static void build_stringlist (const char *str, char ***result_list, size_t *result_size)
 Split a comma separated string list into words. More...
 
bool check_formulae (void)
 Check if formula don't have the same index. More...
 
static int check_recipe (const recipe *rp)
 Makes sure we actually have the requested artifact and archetype. More...
 
bool check_recipes ()
 Ensure that all recipes have a valid artifact, and that archetypes are correct. More...
 
void dump_alchemy (void)
 Dumps alchemy recipes to output. More...
 
void dump_alchemy_costs (void)
 Dumps to output all costs of recipes. More...
 
recipefind_recipe_for_tool (const char *tool, recipe *from)
 Find a recipe for a specified tool. More...
 
archetypefind_treasure_by_name (const treasure *t, const char *name, int depth)
 Find a treasure with a matching name. More...
 
void free_all_recipes (void)
 Frees all memory allocated to recipes and recipes lists. More...
 
static recipeget_empty_formula (void)
 Allocates a new recipe. More...
 
recipelistget_formulalist (int i)
 Gets a formula list by ingredients count. More...
 
recipeget_random_recipe (recipelist *rpl)
 Gets a random recipe from a list, based on chance. More...
 
static recipelistget_random_recipelist (void)
 Gets a random recipe list. More...
 
static const char * ingred_name (const char *name)
 Extracts the name from an ingredient. More...
 
void init_formulae (BufferReader *reader, const char *filename)
 Builds up the lists of formula from the file in the libdir. More...
 
static recipelistinit_recipelist (void)
 Allocates a new recipelist. More...
 
const artifactlocate_recipe_artifact (const recipe *rp, size_t idx)
 Finds an artifact for a recipe. More...
 
static int numb_ingred (const char *buf)
 Extracts the number part of an ingredient. More...
 
static long recipe_find_ingredient_cost (const char *name)
 Try to find an ingredient with specified name. More...
 
const char * recipe_get_difficulty_string (int difficulty)
 A method to produce a difficulty adjective to describe alchemy projects. More...
 
const Facerecipe_get_face (const recipe *rp)
 Return the best face associated with a recipe. More...
 
int strtoint (const char *buf)
 Convert buf into an integer equal to the coadded sum of the (lowercase) character. More...
 

Variables

static recipelistformulalist
 Pointer to first recipelist. More...
 

Detailed Description

Basic stuff for use with the alchemy code. Clearly some of this stuff could go into server/alchemy, but I left it here just in case it proves more generally useful.

Nov 1995 - file created by b.t. thoma.nosp@m.s@as.nosp@m.tro.p.nosp@m.su.e.nosp@m.du

Our definition of 'formula' is any product of an alchemical process. Ingredients are just comma delimited list of archetype (or object) names.

Example 'formula' entry in libdir/formulae: Object transparency chance 10 ingred dust of beholdereye,gem arch potion_generic

An ingredient is a name, which can contain an initial number for how many are needed.

Definition in file recipe.cpp.

Function Documentation

◆ build_stringlist()

static void build_stringlist ( const char *  str,
char ***  result_list,
size_t *  result_size 
)
static

Split a comma separated string list into words.

Parameters
strthe string to split
[out]result_listpointer to return value for the newly created list; the caller is responsible for freeing both *result_list and **result_list.
[out]result_sizepointer to return value for the size of the newly created list

Definition at line 864 of file recipe.cpp.

References fatal(), OUT_OF_MEMORY, and strdup_local.

Referenced by init_formulae().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ check_formulae()

bool check_formulae ( void  )

Check if formula don't have the same index.

Since we are doing a squential search on the formulae lists now, we have to be carefull that we dont have 2 formula with the exact same index value. Under the new nbatches code, it is possible to have multiples of ingredients in a cauldron which could result in an index formula mismatch. We *don't *check for that possibility here. -b.t.

LOG() to error level.

Todo:
check archetypes exist, check coherence (skill present, cauldron ok, and such things), set chance to 0 for combinations

Definition at line 292 of file recipe.cpp.

References recipe::arch_name, recipe::arch_names, recipe::cauldron, formulalist, recipe::index, recipelist::items, llevDebug, llevError, LOG(), recipe::next, recipelist::next, recipe::title, recipe::tool, recipe::tool_size, and try_find_archetype().

Referenced by assets_end_load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ check_recipe()

static int check_recipe ( const recipe rp)
static

Makes sure we actually have the requested artifact and archetype.

Parameters
rprecipe we want to check.
Returns
1 if recipe is ok, 0 if missing something (and LOG() to error).

Definition at line 120 of file recipe.cpp.

References recipe::arch_name, recipe::arch_names, llevError, locate_recipe_artifact(), LOG(), recipe::title, and try_find_archetype().

Referenced by check_recipes().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ check_recipes()

bool check_recipes ( )

Ensure that all recipes have a valid artifact, and that archetypes are correct.

Definition at line 147 of file recipe.cpp.

References check_recipe(), formulalist, recipelist::items, recipe::next, and recipelist::next.

Referenced by assets_end_load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dump_alchemy()

void dump_alchemy ( void  )

Dumps alchemy recipes to output.

Borrowed (again) from the artifacts code for this.

Todo:
use LOG() instead of fprintf?

Definition at line 352 of file recipe.cpp.

References recipe::arch_name, recipe::arch_names, buf, recipe::cauldron, recipe::chance, archetype::clone, recipe::diff, recipe::exp, formulalist, recipe::index, recipe::ingred, artifact::item, recipelist::items, llevError, locate_recipe_artifact(), LOG(), MAX_BUF, linked_char::name, recipe::next, recipelist::next, linked_char::next, price_base(), recipe_find_ingredient_cost(), recipe::skill, strlcpy(), strtoint(), recipe::title, try_find_archetype(), object::value, and recipe::yield.

Referenced by init_beforeplay().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dump_alchemy_costs()

void dump_alchemy_costs ( void  )

◆ find_recipe_for_tool()

recipe* find_recipe_for_tool ( const char *  tool,
recipe from 
)

Find a recipe for a specified tool.

This function can be called multiple times to browse the whole list, using the 'from' parameter.

Parameters
tooltool's archetype name.
fromformula to search from, if NULL from the first one.
Returns
matching formula, NULL if none matching.

Definition at line 897 of file recipe.cpp.

References formulalist, get_formulalist(), recipe::ingred_count, list, recipe::next, t, recipe::tool, and recipe::tool_size.

Referenced by command_use().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ find_treasure_by_name()

archetype* find_treasure_by_name ( const treasure t,
const char *  name,
int  depth 
)

Find a treasure with a matching name.

The 'depth' parameter is only there to prevent infinite loops in treasure lists (a list referencing another list pointing back to the first one).

Parameters
titem of treasure list to search from
namename we're trying to find. Doesn't need to be a shared string.
depthcurrent depth. Code will exit if greater than 10.
Returns
archetype with name, or NULL if nothing found.

Definition at line 425 of file recipe.cpp.

References find_treasure_by_name(), find_treasurelist(), treasurelist::items, name, strcasecmp(), and t.

Referenced by find_treasure_by_name().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ free_all_recipes()

void free_all_recipes ( void  )

Frees all memory allocated to recipes and recipes lists.

Definition at line 814 of file recipe.cpp.

References recipe::arch_name, recipe::cauldron, recipe::failure_arch, recipe::failure_message, formulalist, free_string(), recipe::ingred, recipelist::items, llevDebug, LOG(), linked_char::name, recipe::next, recipelist::next, linked_char::next, recipe::skill, recipe::title, and recipe::tool.

Referenced by cleanup(), and init_formulae().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_empty_formula()

static recipe* get_empty_formula ( void  )
static

Allocates a new recipe.

Will call fatal() if memory allocation error.

Returns
new structure initialized. Never NULL.

Definition at line 79 of file recipe.cpp.

References fatal(), OUT_OF_MEMORY, and t.

Referenced by init_formulae().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_formulalist()

recipelist* get_formulalist ( int  i)

Gets a formula list by ingredients count.

Parameters
inumber of ingredients.
Returns
pointer to the formula list, or NULL if it doesn't exist.

Definition at line 98 of file recipe.cpp.

References formulalist, and recipelist::next.

Referenced by alchemy_failure_effect(), attempt_do_alchemy(), find_recipe_for_tool(), get_random_recipelist(), knowledge_alchemy_get_recipe(), make_formula_book(), and pack_formulae().

+ Here is the caller graph for this function:

◆ get_random_recipe()

recipe* get_random_recipe ( recipelist rpl)

Gets a random recipe from a list, based on chance.

Parameters
rplrecipelist we want a recipe from. Can be NULL in which case a random one is selected.
Returns
random recipe. Can be NULL if recipelist has a total_chance of 0.

Definition at line 790 of file recipe.cpp.

References recipe::chance, get_random_recipelist(), recipelist::items, recipe::next, RANDOM, and recipelist::total_chance.

Referenced by alchemy_failure_effect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_random_recipelist()

static recipelist* get_random_recipelist ( void  )
static

Gets a random recipe list.

Returns
random recipe list.

Definition at line 754 of file recipe.cpp.

References get_formulalist(), llevError, LOG(), recipelist::next, RANDOM, and recipelist::total_chance.

Referenced by get_random_recipe().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ingred_name()

static const char* ingred_name ( const char *  name)
static

Extracts the name from an ingredient.

Parameters
nameingredient to extract from. Can contain a number at start.
Returns
pointer in name to the first character of the ingredient's name.

Definition at line 677 of file recipe.cpp.

References name.

Referenced by strtoint().

+ Here is the caller graph for this function:

◆ init_formulae()

◆ init_recipelist()

static recipelist* init_recipelist ( void  )
static

Allocates a new recipelist.

Will call fatal() if memory allocation error.

Returns
new structure initialized. Never NULL.

Definition at line 60 of file recipe.cpp.

References fatal(), recipelist::items, recipelist::next, recipelist::number, OUT_OF_MEMORY, and recipelist::total_chance.

Referenced by init_formulae().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ locate_recipe_artifact()

const artifact* locate_recipe_artifact ( const recipe rp,
size_t  idx 
)

Finds an artifact for a recipe.

Parameters
rprecipe
idxindex of ingredient in recipe.
Returns
artifact, or NULL if not found.

Definition at line 733 of file recipe.cpp.

References recipe::arch_name, create_archetype(), find_artifactlist(), legal_artifact_combination(), object_free_drop_inventory(), and recipe::title.

Referenced by check_recipe(), dump_alchemy(), dump_alchemy_costs(), make_item_from_recipe(), and recipe_get_face().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ numb_ingred()

static int numb_ingred ( const char *  buf)
static

Extracts the number part of an ingredient.

Parameters
bufingredient.
Returns
number part of an ingredient.

Definition at line 693 of file recipe.cpp.

References buf.

Referenced by init_formulae(), and strtoint().

+ Here is the caller graph for this function:

◆ recipe_find_ingredient_cost()

static long recipe_find_ingredient_cost ( const char *  name)
static

Try to find an ingredient with specified name.

If several archetypes have the same name, the value of the first one with that name will be returned. This happens for the mushrooms (mushroom_1, mushroom_2 and mushroom_3). For the monsters' body parts, there may be several monsters with the same name. This is not a problem if these monsters have the same level (e.g. sage & c_sage) or if only one of the monsters generates the body parts that we are looking for (e.g. big_dragon and big_dragon_worthless).

Will also search in artifacts.

Parameters
nameingredient we're searching for. Can start with a number.
Returns
cost of ingredient, -1 if wasn't found.

Definition at line 476 of file recipe.cpp.

References AssetsManager::archetypes(), archetype::clone, AssetsCollection< T, Key >::each(), is_valid_types_gen::found, getManager(), name, object::name, strcasecmp(), and object::title.

Referenced by dump_alchemy(), and dump_alchemy_costs().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ recipe_get_difficulty_string()

const char* recipe_get_difficulty_string ( int  difficulty)

A method to produce a difficulty adjective to describe alchemy projects.

Parameters
difficultythe base difficulty of the recipe
Returns
A string that approximates the difficulty of the recipe, meant for printing in the recipe books and the recipe knowledge descriptor.

Definition at line 965 of file recipe.cpp.

Referenced by knowledge_alchemy_detail(), and make_formula_book().

+ Here is the caller graph for this function:

◆ recipe_get_face()

const Face* recipe_get_face ( const recipe rp)

Return the best face associated with a recipe.

Parameters
rprecipe to get the face of.
Returns
best face, may be NULL if none applicable.

Definition at line 923 of file recipe.cpp.

References recipe::arch_name, recipe::arch_names, arch_to_object(), blank_face, archetype::clone, face(), object::face, FREE_OBJ_FREE_INVENTORY, FREE_OBJ_NO_DESTROY_CALLBACK, give_artifact_abilities(), artifact::item, locate_recipe_artifact(), object_free(), object_give_identified_properties(), recipe::title, and try_find_archetype().

Referenced by knowledge_alchemy_face().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ strtoint()

int strtoint ( const char *  buf)

Convert buf into an integer equal to the coadded sum of the (lowercase) character.

ASCII values in buf (times prepended integers).

Parameters
bufbuffer we want to convert. Can contain an initial number.
Returns
sum of lowercase characters of the ingredient's name.

Definition at line 712 of file recipe.cpp.

References buf, ingred_name(), numb_ingred(), and tolower.

Referenced by add_book_to_list(), content_recipe_value(), dump_alchemy(), find_title(), and init_formulae().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ formulalist

recipelist* formulalist
static