Initialize priority in CraftDefinition constructors

The priority is used by getCraftResult, which may be used before
initHash is called.
This commit is contained in:
Paul Ouellette 2019-07-22 01:15:50 -04:00 committed by sfan5
parent b994a35d97
commit 395b1b3346
2 changed files with 94 additions and 62 deletions

@ -37,6 +37,15 @@ inline bool isGroupRecipeStr(const std::string &rec_name)
return str_starts_with(rec_name, std::string("group:")); return str_starts_with(rec_name, std::string("group:"));
} }
static bool hasGroupItem(const std::vector<std::string> &recipe)
{
for (const auto &item : recipe) {
if (isGroupRecipeStr(item))
return true;
}
return false;
}
inline u64 getHashForString(const std::string &recipe_str) inline u64 getHashForString(const std::string &recipe_str)
{ {
/*errorstream << "Hashing craft string \"" << recipe_str << '"';*/ /*errorstream << "Hashing craft string \"" << recipe_str << '"';*/
@ -320,6 +329,19 @@ std::string CraftReplacements::dump() const
CraftDefinitionShaped CraftDefinitionShaped
*/ */
CraftDefinitionShaped::CraftDefinitionShaped(
const std::string &output_,
unsigned int width_,
const std::vector<std::string> &recipe_,
const CraftReplacements &replacements_):
output(output_), width(width_), recipe(recipe_), replacements(replacements_)
{
if (hasGroupItem(recipe))
priority = SHAPED_AND_GROUPS;
else
priority = SHAPED;
}
std::string CraftDefinitionShaped::getName() const std::string CraftDefinitionShaped::getName() const
{ {
return "shaped"; return "shaped";
@ -425,20 +447,10 @@ void CraftDefinitionShaped::initHash(IGameDef *gamedef)
hash_inited = true; hash_inited = true;
recipe_names = craftGetItemNames(recipe, gamedef); recipe_names = craftGetItemNames(recipe, gamedef);
bool has_group = false; if (hasGroupItem(recipe_names))
for (const auto &recipe_name : recipe_names) {
if (isGroupRecipeStr(recipe_name)) {
has_group = true;
break;
}
}
if (has_group) {
hash_type = CRAFT_HASH_TYPE_COUNT; hash_type = CRAFT_HASH_TYPE_COUNT;
priority = SHAPED_AND_GROUPS; else
} else {
hash_type = CRAFT_HASH_TYPE_ITEM_NAMES; hash_type = CRAFT_HASH_TYPE_ITEM_NAMES;
priority = SHAPED;
}
} }
std::string CraftDefinitionShaped::dump() const std::string CraftDefinitionShaped::dump() const
@ -454,6 +466,18 @@ std::string CraftDefinitionShaped::dump() const
CraftDefinitionShapeless CraftDefinitionShapeless
*/ */
CraftDefinitionShapeless::CraftDefinitionShapeless(
const std::string &output_,
const std::vector<std::string> &recipe_,
const CraftReplacements &replacements_):
output(output_), recipe(recipe_), replacements(replacements_)
{
if (hasGroupItem(recipe))
priority = SHAPELESS_AND_GROUPS;
else
priority = SHAPELESS;
}
std::string CraftDefinitionShapeless::getName() const std::string CraftDefinitionShapeless::getName() const
{ {
return "shapeless"; return "shapeless";
@ -542,20 +566,10 @@ void CraftDefinitionShapeless::initHash(IGameDef *gamedef)
recipe_names = craftGetItemNames(recipe, gamedef); recipe_names = craftGetItemNames(recipe, gamedef);
std::sort(recipe_names.begin(), recipe_names.end()); std::sort(recipe_names.begin(), recipe_names.end());
bool has_group = false; if (hasGroupItem(recipe_names))
for (const auto &recipe_name : recipe_names) {
if (isGroupRecipeStr(recipe_name)) {
has_group = true;
break;
}
}
if (has_group) {
hash_type = CRAFT_HASH_TYPE_COUNT; hash_type = CRAFT_HASH_TYPE_COUNT;
priority = SHAPELESS_AND_GROUPS; else
} else {
hash_type = CRAFT_HASH_TYPE_ITEM_NAMES; hash_type = CRAFT_HASH_TYPE_ITEM_NAMES;
priority = SHAPELESS;
}
} }
std::string CraftDefinitionShapeless::dump() const std::string CraftDefinitionShapeless::dump() const
@ -571,6 +585,12 @@ std::string CraftDefinitionShapeless::dump() const
CraftDefinitionToolRepair CraftDefinitionToolRepair
*/ */
CraftDefinitionToolRepair::CraftDefinitionToolRepair(float additional_wear_):
additional_wear(additional_wear_)
{
priority = TOOLREPAIR;
}
static ItemStack craftToolRepair( static ItemStack craftToolRepair(
const ItemStack &item1, const ItemStack &item1,
const ItemStack &item2, const ItemStack &item2,
@ -665,6 +685,19 @@ std::string CraftDefinitionToolRepair::dump() const
CraftDefinitionCooking CraftDefinitionCooking
*/ */
CraftDefinitionCooking::CraftDefinitionCooking(
const std::string &output_,
const std::string &recipe_,
float cooktime_,
const CraftReplacements &replacements_):
output(output_), recipe(recipe_), cooktime(cooktime_), replacements(replacements_)
{
if (isGroupRecipeStr(recipe))
priority = SHAPELESS_AND_GROUPS;
else
priority = SHAPELESS;
}
std::string CraftDefinitionCooking::getName() const std::string CraftDefinitionCooking::getName() const
{ {
return "cooking"; return "cooking";
@ -735,13 +768,10 @@ void CraftDefinitionCooking::initHash(IGameDef *gamedef)
hash_inited = true; hash_inited = true;
recipe_name = craftGetItemName(recipe, gamedef); recipe_name = craftGetItemName(recipe, gamedef);
if (isGroupRecipeStr(recipe_name)) { if (isGroupRecipeStr(recipe_name))
hash_type = CRAFT_HASH_TYPE_COUNT; hash_type = CRAFT_HASH_TYPE_COUNT;
priority = SHAPELESS_AND_GROUPS; else
} else {
hash_type = CRAFT_HASH_TYPE_ITEM_NAMES; hash_type = CRAFT_HASH_TYPE_ITEM_NAMES;
priority = SHAPELESS;
}
} }
std::string CraftDefinitionCooking::dump() const std::string CraftDefinitionCooking::dump() const
@ -758,6 +788,18 @@ std::string CraftDefinitionCooking::dump() const
CraftDefinitionFuel CraftDefinitionFuel
*/ */
CraftDefinitionFuel::CraftDefinitionFuel(
const std::string &recipe_,
float burntime_,
const CraftReplacements &replacements_):
recipe(recipe_), burntime(burntime_), replacements(replacements_)
{
if (isGroupRecipeStr(recipe_name))
priority = SHAPELESS_AND_GROUPS;
else
priority = SHAPELESS;
}
std::string CraftDefinitionFuel::getName() const std::string CraftDefinitionFuel::getName() const
{ {
return "fuel"; return "fuel";
@ -828,13 +870,10 @@ void CraftDefinitionFuel::initHash(IGameDef *gamedef)
hash_inited = true; hash_inited = true;
recipe_name = craftGetItemName(recipe, gamedef); recipe_name = craftGetItemName(recipe, gamedef);
if (isGroupRecipeStr(recipe_name)) { if (isGroupRecipeStr(recipe_name))
hash_type = CRAFT_HASH_TYPE_COUNT; hash_type = CRAFT_HASH_TYPE_COUNT;
priority = SHAPELESS_AND_GROUPS; else
} else {
hash_type = CRAFT_HASH_TYPE_ITEM_NAMES; hash_type = CRAFT_HASH_TYPE_ITEM_NAMES;
priority = SHAPELESS;
}
} }
std::string CraftDefinitionFuel::dump() const std::string CraftDefinitionFuel::dump() const

@ -196,15 +196,12 @@ class CraftDefinitionShaped: public CraftDefinition
{ {
public: public:
CraftDefinitionShaped() = delete; CraftDefinitionShaped() = delete;
CraftDefinitionShaped( CraftDefinitionShaped(
const std::string &output_, const std::string &output_,
unsigned int width_, unsigned int width_,
const std::vector<std::string> &recipe_, const std::vector<std::string> &recipe_,
const CraftReplacements &replacements_): const CraftReplacements &replacements_);
output(output_), width(width_), recipe(recipe_),
replacements(replacements_)
{}
virtual ~CraftDefinitionShaped() = default; virtual ~CraftDefinitionShaped() = default;
virtual std::string getName() const; virtual std::string getName() const;
@ -247,9 +244,8 @@ public:
CraftDefinitionShapeless( CraftDefinitionShapeless(
const std::string &output_, const std::string &output_,
const std::vector<std::string> &recipe_, const std::vector<std::string> &recipe_,
const CraftReplacements &replacements_): const CraftReplacements &replacements_);
output(output_), recipe(recipe_), replacements(replacements_)
{}
virtual ~CraftDefinitionShapeless() = default; virtual ~CraftDefinitionShapeless() = default;
virtual std::string getName() const; virtual std::string getName() const;
@ -288,9 +284,8 @@ class CraftDefinitionToolRepair: public CraftDefinition
{ {
public: public:
CraftDefinitionToolRepair() = delete; CraftDefinitionToolRepair() = delete;
CraftDefinitionToolRepair(float additional_wear_): CraftDefinitionToolRepair(float additional_wear_);
additional_wear(additional_wear_)
{}
virtual ~CraftDefinitionToolRepair() = default; virtual ~CraftDefinitionToolRepair() = default;
virtual std::string getName() const; virtual std::string getName() const;
@ -305,7 +300,6 @@ public:
virtual void initHash(IGameDef *gamedef) virtual void initHash(IGameDef *gamedef)
{ {
hash_type = CRAFT_HASH_TYPE_COUNT; hash_type = CRAFT_HASH_TYPE_COUNT;
priority = TOOLREPAIR;
} }
virtual std::string dump() const; virtual std::string dump() const;
@ -331,9 +325,8 @@ public:
const std::string &output_, const std::string &output_,
const std::string &recipe_, const std::string &recipe_,
float cooktime_, float cooktime_,
const CraftReplacements &replacements_): const CraftReplacements &replacements_);
output(output_), recipe(recipe_), cooktime(cooktime_), replacements(replacements_)
{}
virtual ~CraftDefinitionCooking() = default; virtual ~CraftDefinitionCooking() = default;
virtual std::string getName() const; virtual std::string getName() const;
@ -372,11 +365,11 @@ class CraftDefinitionFuel: public CraftDefinition
{ {
public: public:
CraftDefinitionFuel() = delete; CraftDefinitionFuel() = delete;
CraftDefinitionFuel(const std::string &recipe_, CraftDefinitionFuel(
const std::string &recipe_,
float burntime_, float burntime_,
const CraftReplacements &replacements_): const CraftReplacements &replacements_);
recipe(recipe_), burntime(burntime_), replacements(replacements_)
{}
virtual ~CraftDefinitionFuel() = default; virtual ~CraftDefinitionFuel() = default;
virtual std::string getName() const; virtual std::string getName() const;