Skip to content
Permalink
Browse files

Optimize minetest.get_(all)_craft_recipe(s)

Signed off by: ShadowNinja, kwolekr
  • Loading branch information
gregorycu authored and kwolekr committed Mar 21, 2015
1 parent 1e4fb80 commit 03e0dd33a847a83d975282c6caf6b926306e7b57
Showing with 107 additions and 180 deletions.
  1. +28 −75 src/craftdef.cpp
  2. +4 −8 src/craftdef.h
  3. +75 −97 src/script/lua_api/l_craft.cpp
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gamedef.h"
#include "inventory.h"
#include "util/serialize.h"
#include "util/numeric.h"
#include "strfnd.h"
#include "exceptions.h"

@@ -931,85 +932,30 @@ class CCraftDefManager: public IWritableCraftDefManager
}
return false;
}
virtual bool getCraftRecipe(CraftInput &input, CraftOutput &output,
IGameDef *gamedef) const
{
CraftOutput tmpout;
tmpout.item = "";
tmpout.time = 0;

// If output item is empty, abort.
if(output.item.empty())
return false;

// Walk crafting definitions from back to front, so that later
// definitions can override earlier ones.
for(std::vector<CraftDefinition*>::const_reverse_iterator
i = m_craft_definitions.rbegin();
i != m_craft_definitions.rend(); i++)
{
CraftDefinition *def = *i;

/*infostream<<"Checking "<<input.dump()<<std::endl
<<" against "<<def->dump()<<std::endl;*/

try {
tmpout = def->getOutput(input, gamedef);
if((tmpout.item.substr(0,output.item.length()) == output.item) &&
((tmpout.item[output.item.length()] == 0) ||
(tmpout.item[output.item.length()] == ' ')))
{
// Get output, then decrement input (if requested)
input = def->getInput(output, gamedef);
return true;
}
}
catch(SerializationError &e)
{
errorstream<<"getCraftResult: ERROR: "
<<"Serialization error in recipe "
<<def->dump()<<std::endl;
// then go on with the next craft definition
}
}
return false;
}
virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
IGameDef *gamedef) const
IGameDef *gamedef, unsigned limit=0) const
{
std::vector<CraftDefinition*> recipes_list;
CraftInput input;
CraftOutput tmpout;
tmpout.item = "";
tmpout.time = 0;
std::vector<CraftDefinition*> recipes;

for(std::vector<CraftDefinition*>::const_reverse_iterator
i = m_craft_definitions.rbegin();
i != m_craft_definitions.rend(); i++)
{
CraftDefinition *def = *i;
std::map<std::string, std::vector<CraftDefinition*> >::const_iterator
vec_iter = m_output_craft_definitions.find(output.item);

/*infostream<<"Checking "<<input.dump()<<std::endl
<<" against "<<def->dump()<<std::endl;*/
if (vec_iter == m_output_craft_definitions.end())
return recipes;

try {
tmpout = def->getOutput(input, gamedef);
if(tmpout.item.substr(0,output.item.length()) == output.item)
{
// Get output, then decrement input (if requested)
input = def->getInput(output, gamedef);
recipes_list.push_back(*i);
}
}
catch(SerializationError &e)
{
errorstream<<"getCraftResult: ERROR: "
<<"Serialization error in recipe "
<<def->dump()<<std::endl;
// then go on with the next craft definition
}
const std::vector<CraftDefinition*> &vec = vec_iter->second;

recipes.reserve(limit ? MYMIN(limit, vec.size()) : vec.size());

for (std::vector<CraftDefinition*>::const_reverse_iterator
it = vec.rbegin(); it != vec.rend(); ++it) {
if (limit && recipes.size() >= limit)
break;
recipes.push_back(*it);
}
return recipes_list;

return recipes;
}
virtual std::string dump() const
{
@@ -1023,11 +969,16 @@ class CCraftDefManager: public IWritableCraftDefManager
}
return os.str();
}
virtual void registerCraft(CraftDefinition *def)
virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef)
{
verbosestream<<"registerCraft: registering craft definition: "
<<def->dump()<<std::endl;
m_craft_definitions.push_back(def);

CraftInput input;
std::string output_name = craftGetItemName(
def->getOutput(input, gamedef).item, gamedef);
m_output_craft_definitions[output_name].push_back(def);
}
virtual void clear()
{
@@ -1037,6 +988,7 @@ class CCraftDefManager: public IWritableCraftDefManager
delete *i;
}
m_craft_definitions.clear();
m_output_craft_definitions.clear();
}
virtual void serialize(std::ostream &os) const
{
@@ -1053,7 +1005,7 @@ class CCraftDefManager: public IWritableCraftDefManager
os<<serializeString(tmp_os.str());
}
}
virtual void deSerialize(std::istream &is)
virtual void deSerialize(std::istream &is, IGameDef *gamedef)
{
// Clear everything
clear();
@@ -1067,11 +1019,12 @@ class CCraftDefManager: public IWritableCraftDefManager
std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
CraftDefinition *def = CraftDefinition::deSerialize(tmp_is);
// Register
registerCraft(def);
registerCraft(def, gamedef);
}
}
private:
std::vector<CraftDefinition*> m_craft_definitions;
std::map<std::string, std::vector<CraftDefinition*> > m_output_craft_definitions;
};

IWritableCraftDefManager* createCraftDefManager()
@@ -356,10 +356,8 @@ class ICraftDefManager
// The main crafting function
virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
bool decrementInput, IGameDef *gamedef) const=0;
virtual bool getCraftRecipe(CraftInput &input, CraftOutput &output,
IGameDef *gamedef) const=0;
virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
IGameDef *gamedef) const=0;
IGameDef *gamedef, unsigned limit=0) const=0;

// Print crafting recipes for debugging
virtual std::string dump() const=0;
@@ -376,22 +374,20 @@ class IWritableCraftDefManager : public ICraftDefManager
// The main crafting function
virtual bool getCraftResult(CraftInput &input, CraftOutput &output,
bool decrementInput, IGameDef *gamedef) const=0;
virtual bool getCraftRecipe(CraftInput &input, CraftOutput &output,
IGameDef *gamedef) const=0;
virtual std::vector<CraftDefinition*> getCraftRecipes(CraftOutput &output,
IGameDef *gamedef) const=0;
IGameDef *gamedef, unsigned limit=0) const=0;

// Print crafting recipes for debugging
virtual std::string dump() const=0;

// Add a crafting definition.
// After calling this, the pointer belongs to the manager.
virtual void registerCraft(CraftDefinition *def)=0;
virtual void registerCraft(CraftDefinition *def, IGameDef *gamedef) = 0;
// Delete all crafting definitions
virtual void clear()=0;

virtual void serialize(std::ostream &os) const=0;
virtual void deSerialize(std::istream &is)=0;
virtual void deSerialize(std::istream &is, IGameDef *gamedef) = 0;
};

IWritableCraftDefManager* createCraftDefManager();

0 comments on commit 03e0dd3

Please sign in to comment.