Skip to content

Commit

Permalink
Test crafting hash type only once for a recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
HybridDog authored and SmallJoker committed May 20, 2019
1 parent b1b40fe commit 1604b94
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 55 deletions.
77 changes: 29 additions & 48 deletions src/craftdef.cpp
Expand Up @@ -407,22 +407,6 @@ void CraftDefinitionShaped::decrementInput(CraftInput &input, std::vector<ItemSt
craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
}

CraftHashType CraftDefinitionShaped::getHashType() const
{
assert(hash_inited); // Pre-condition
bool has_group = false;
for (const auto &recipe_name : recipe_names) {
if (isGroupRecipeStr(recipe_name)) {
has_group = true;
break;
}
}
if (has_group)
return CRAFT_HASH_TYPE_COUNT;

return CRAFT_HASH_TYPE_ITEM_NAMES;
}

u64 CraftDefinitionShaped::getHash(CraftHashType type) const
{
assert(hash_inited); // Pre-condition
Expand All @@ -440,6 +424,15 @@ void CraftDefinitionShaped::initHash(IGameDef *gamedef)
return;
hash_inited = true;
recipe_names = craftGetItemNames(recipe, gamedef);

bool has_group = false;
for (const auto &recipe_name : recipe_names) {
if (isGroupRecipeStr(recipe_name)) {
has_group = true;
break;
}
}
hash_type = has_group ? CRAFT_HASH_TYPE_COUNT : CRAFT_HASH_TYPE_ITEM_NAMES;
}

std::string CraftDefinitionShaped::dump() const
Expand Down Expand Up @@ -527,22 +520,6 @@ void CraftDefinitionShapeless::decrementInput(CraftInput &input, std::vector<Ite
craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
}

CraftHashType CraftDefinitionShapeless::getHashType() const
{
assert(hash_inited); // Pre-condition
bool has_group = false;
for (const auto &recipe_name : recipe_names) {
if (isGroupRecipeStr(recipe_name)) {
has_group = true;
break;
}
}
if (has_group)
return CRAFT_HASH_TYPE_COUNT;

return CRAFT_HASH_TYPE_ITEM_NAMES;
}

u64 CraftDefinitionShapeless::getHash(CraftHashType type) const
{
assert(hash_inited); // Pre-condition
Expand All @@ -558,6 +535,15 @@ void CraftDefinitionShapeless::initHash(IGameDef *gamedef)
hash_inited = true;
recipe_names = craftGetItemNames(recipe, gamedef);
std::sort(recipe_names.begin(), recipe_names.end());

bool has_group = false;
for (const auto &recipe_name : recipe_names) {
if (isGroupRecipeStr(recipe_name)) {
has_group = true;
break;
}
}
hash_type = has_group ? CRAFT_HASH_TYPE_COUNT : CRAFT_HASH_TYPE_ITEM_NAMES;
}

std::string CraftDefinitionShapeless::dump() const
Expand Down Expand Up @@ -715,14 +701,6 @@ void CraftDefinitionCooking::decrementInput(CraftInput &input, std::vector<ItemS
craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
}

CraftHashType CraftDefinitionCooking::getHashType() const
{
if (isGroupRecipeStr(recipe_name))
return CRAFT_HASH_TYPE_COUNT;

return CRAFT_HASH_TYPE_ITEM_NAMES;
}

u64 CraftDefinitionCooking::getHash(CraftHashType type) const
{
if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
Expand All @@ -744,6 +722,11 @@ void CraftDefinitionCooking::initHash(IGameDef *gamedef)
return;
hash_inited = true;
recipe_name = craftGetItemName(recipe, gamedef);

if (isGroupRecipeStr(recipe_name))
hash_type = CRAFT_HASH_TYPE_COUNT;
else
hash_type = CRAFT_HASH_TYPE_ITEM_NAMES;
}

std::string CraftDefinitionCooking::dump() const
Expand Down Expand Up @@ -808,14 +791,6 @@ void CraftDefinitionFuel::decrementInput(CraftInput &input, std::vector<ItemStac
craftDecrementOrReplaceInput(input, output_replacements, replacements, gamedef);
}

CraftHashType CraftDefinitionFuel::getHashType() const
{
if (isGroupRecipeStr(recipe_name))
return CRAFT_HASH_TYPE_COUNT;

return CRAFT_HASH_TYPE_ITEM_NAMES;
}

u64 CraftDefinitionFuel::getHash(CraftHashType type) const
{
if (type == CRAFT_HASH_TYPE_ITEM_NAMES) {
Expand All @@ -837,7 +812,13 @@ void CraftDefinitionFuel::initHash(IGameDef *gamedef)
return;
hash_inited = true;
recipe_name = craftGetItemName(recipe, gamedef);

if (isGroupRecipeStr(recipe_name))
hash_type = CRAFT_HASH_TYPE_COUNT;
else
hash_type = CRAFT_HASH_TYPE_ITEM_NAMES;
}

std::string CraftDefinitionFuel::dump() const
{
std::ostringstream os(std::ios::binary);
Expand Down
18 changes: 11 additions & 7 deletions src/craftdef.h
Expand Up @@ -149,13 +149,19 @@ class CraftDefinition
virtual void decrementInput(CraftInput &input,
std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const=0;

virtual CraftHashType getHashType() const = 0;
CraftHashType getHashType() const
{
return hash_type;
}
virtual u64 getHash(CraftHashType type) const = 0;

// to be called after all mods are loaded, so that we catch all aliases
virtual void initHash(IGameDef *gamedef) = 0;

virtual std::string dump() const=0;

protected:
CraftHashType hash_type;
};

/*
Expand Down Expand Up @@ -186,7 +192,6 @@ class CraftDefinitionShaped: public CraftDefinition
virtual void decrementInput(CraftInput &input,
std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;

virtual CraftHashType getHashType() const;
virtual u64 getHash(CraftHashType type) const;

virtual void initHash(IGameDef *gamedef);
Expand Down Expand Up @@ -232,7 +237,6 @@ class CraftDefinitionShapeless: public CraftDefinition
virtual void decrementInput(CraftInput &input,
std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;

virtual CraftHashType getHashType() const;
virtual u64 getHash(CraftHashType type) const;

virtual void initHash(IGameDef *gamedef);
Expand Down Expand Up @@ -274,10 +278,12 @@ class CraftDefinitionToolRepair: public CraftDefinition
virtual void decrementInput(CraftInput &input,
std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;

virtual CraftHashType getHashType() const { return CRAFT_HASH_TYPE_COUNT; }
virtual u64 getHash(CraftHashType type) const { return 2; }

virtual void initHash(IGameDef *gamedef) {}
virtual void initHash(IGameDef *gamedef)
{
hash_type = CRAFT_HASH_TYPE_COUNT;
}

virtual std::string dump() const;

Expand Down Expand Up @@ -314,7 +320,6 @@ class CraftDefinitionCooking: public CraftDefinition
virtual void decrementInput(CraftInput &input,
std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;

virtual CraftHashType getHashType() const;
virtual u64 getHash(CraftHashType type) const;

virtual void initHash(IGameDef *gamedef);
Expand Down Expand Up @@ -358,7 +363,6 @@ class CraftDefinitionFuel: public CraftDefinition
virtual void decrementInput(CraftInput &input,
std::vector<ItemStack> &output_replacements, IGameDef *gamedef) const;

virtual CraftHashType getHashType() const;
virtual u64 getHash(CraftHashType type) const;

virtual void initHash(IGameDef *gamedef);
Expand Down

0 comments on commit 1604b94

Please sign in to comment.