Allow replacements in cooking and fuel recipes

master
Kahrl 2012-02-11 18:10:13 +01:00 committed by Perttu Ahola
parent 430d6e1cca
commit e070f1e525
3 changed files with 69 additions and 38 deletions

View File

@ -260,6 +260,7 @@ std::string CraftOutput::dump() const
/* /*
CraftReplacements CraftReplacements
*/ */
std::string CraftReplacements::dump() const std::string CraftReplacements::dump() const
{ {
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
@ -276,6 +277,27 @@ std::string CraftReplacements::dump() const
return os.str(); return os.str();
} }
void CraftReplacements::serialize(std::ostream &os) const
{
writeU16(os, pairs.size());
for(u32 i=0; i<pairs.size(); i++)
{
os<<serializeString(pairs[i].first);
os<<serializeString(pairs[i].second);
}
}
void CraftReplacements::deSerialize(std::istream &is)
{
pairs.clear();
u32 count = readU16(is);
for(u32 i=0; i<count; i++)
{
std::string first = deSerializeString(is);
std::string second = deSerializeString(is);
pairs.push_back(std::make_pair(first, second));
}
}
/* /*
CraftDefinition CraftDefinition
@ -418,12 +440,7 @@ void CraftDefinitionShaped::serializeBody(std::ostream &os) const
writeU16(os, recipe.size()); writeU16(os, recipe.size());
for(u32 i=0; i<recipe.size(); i++) for(u32 i=0; i<recipe.size(); i++)
os<<serializeString(recipe[i]); os<<serializeString(recipe[i]);
writeU16(os, replacements.pairs.size()); replacements.serialize(os);
for(u32 i=0; i<replacements.pairs.size(); i++)
{
os<<serializeString(replacements.pairs[i].first);
os<<serializeString(replacements.pairs[i].second);
}
} }
void CraftDefinitionShaped::deSerializeBody(std::istream &is, int version) void CraftDefinitionShaped::deSerializeBody(std::istream &is, int version)
@ -436,14 +453,7 @@ void CraftDefinitionShaped::deSerializeBody(std::istream &is, int version)
u32 count = readU16(is); u32 count = readU16(is);
for(u32 i=0; i<count; i++) for(u32 i=0; i<count; i++)
recipe.push_back(deSerializeString(is)); recipe.push_back(deSerializeString(is));
replacements.pairs.clear(); replacements.deSerialize(is);
count = readU16(is);
for(u32 i=0; i<count; i++)
{
std::string first = deSerializeString(is);
std::string second = deSerializeString(is);
replacements.pairs.push_back(std::make_pair(first, second));
}
} }
/* /*
@ -497,12 +507,7 @@ void CraftDefinitionShapeless::serializeBody(std::ostream &os) const
writeU16(os, recipe.size()); writeU16(os, recipe.size());
for(u32 i=0; i<recipe.size(); i++) for(u32 i=0; i<recipe.size(); i++)
os<<serializeString(recipe[i]); os<<serializeString(recipe[i]);
writeU16(os, replacements.pairs.size()); replacements.serialize(os);
for(u32 i=0; i<replacements.pairs.size(); i++)
{
os<<serializeString(replacements.pairs[i].first);
os<<serializeString(replacements.pairs[i].second);
}
} }
void CraftDefinitionShapeless::deSerializeBody(std::istream &is, int version) void CraftDefinitionShapeless::deSerializeBody(std::istream &is, int version)
@ -514,14 +519,7 @@ void CraftDefinitionShapeless::deSerializeBody(std::istream &is, int version)
u32 count = readU16(is); u32 count = readU16(is);
for(u32 i=0; i<count; i++) for(u32 i=0; i<count; i++)
recipe.push_back(deSerializeString(is)); recipe.push_back(deSerializeString(is));
replacements.pairs.clear(); replacements.deSerialize(is);
count = readU16(is);
for(u32 i=0; i<count; i++)
{
std::string first = deSerializeString(is);
std::string second = deSerializeString(is);
replacements.pairs.push_back(std::make_pair(first, second));
}
} }
/* /*
@ -664,7 +662,7 @@ CraftOutput CraftDefinitionCooking::getOutput(const CraftInput &input, IGameDef
void CraftDefinitionCooking::decrementInput(CraftInput &input, IGameDef *gamedef) const void CraftDefinitionCooking::decrementInput(CraftInput &input, IGameDef *gamedef) const
{ {
craftDecrementInput(input, gamedef); craftDecrementOrReplaceInput(input, replacements, gamedef);
} }
std::string CraftDefinitionCooking::dump() const std::string CraftDefinitionCooking::dump() const
@ -672,7 +670,8 @@ std::string CraftDefinitionCooking::dump() const
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
os<<"(cooking, output=\""<<output os<<"(cooking, output=\""<<output
<<"\", recipe=\""<<recipe <<"\", recipe=\""<<recipe
<<"\", cooktime="<<cooktime<<")"; <<"\", cooktime="<<cooktime<<")"
<<", replacements="<<replacements.dump()<<")";
return os.str(); return os.str();
} }
@ -681,6 +680,7 @@ void CraftDefinitionCooking::serializeBody(std::ostream &os) const
os<<serializeString(output); os<<serializeString(output);
os<<serializeString(recipe); os<<serializeString(recipe);
writeF1000(os, cooktime); writeF1000(os, cooktime);
replacements.serialize(os);
} }
void CraftDefinitionCooking::deSerializeBody(std::istream &is, int version) void CraftDefinitionCooking::deSerializeBody(std::istream &is, int version)
@ -690,6 +690,7 @@ void CraftDefinitionCooking::deSerializeBody(std::istream &is, int version)
output = deSerializeString(is); output = deSerializeString(is);
recipe = deSerializeString(is); recipe = deSerializeString(is);
cooktime = readF1000(is); cooktime = readF1000(is);
replacements.deSerialize(is);
} }
/* /*
@ -725,14 +726,15 @@ CraftOutput CraftDefinitionFuel::getOutput(const CraftInput &input, IGameDef *ga
void CraftDefinitionFuel::decrementInput(CraftInput &input, IGameDef *gamedef) const void CraftDefinitionFuel::decrementInput(CraftInput &input, IGameDef *gamedef) const
{ {
craftDecrementInput(input, gamedef); craftDecrementOrReplaceInput(input, replacements, gamedef);
} }
std::string CraftDefinitionFuel::dump() const std::string CraftDefinitionFuel::dump() const
{ {
std::ostringstream os(std::ios::binary); std::ostringstream os(std::ios::binary);
os<<"(fuel, recipe=\""<<recipe os<<"(fuel, recipe=\""<<recipe
<<"\", burntime="<<burntime<<")"; <<"\", burntime="<<burntime<<")"
<<", replacements="<<replacements.dump()<<")";
return os.str(); return os.str();
} }
@ -740,6 +742,7 @@ void CraftDefinitionFuel::serializeBody(std::ostream &os) const
{ {
os<<serializeString(recipe); os<<serializeString(recipe);
writeF1000(os, burntime); writeF1000(os, burntime);
replacements.serialize(os);
} }
void CraftDefinitionFuel::deSerializeBody(std::istream &is, int version) void CraftDefinitionFuel::deSerializeBody(std::istream &is, int version)
@ -748,6 +751,7 @@ void CraftDefinitionFuel::deSerializeBody(std::istream &is, int version)
"unsupported CraftDefinitionFuel version"); "unsupported CraftDefinitionFuel version");
recipe = deSerializeString(is); recipe = deSerializeString(is);
burntime = readF1000(is); burntime = readF1000(is);
replacements.deSerialize(is);
} }
/* /*

View File

@ -106,6 +106,8 @@ struct CraftReplacements
pairs(pairs_) pairs(pairs_)
{} {}
std::string dump() const; std::string dump() const;
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is);
}; };
/* /*
@ -270,8 +272,9 @@ public:
CraftDefinitionCooking( CraftDefinitionCooking(
const std::string &output_, const std::string &output_,
const std::string &recipe_, const std::string &recipe_,
float cooktime_): float cooktime_,
output(output_), recipe(recipe_), cooktime(cooktime_) const CraftReplacements &replacements_):
output(output_), recipe(recipe_), cooktime(cooktime_), replacements(replacements_)
{} {}
virtual ~CraftDefinitionCooking(){} virtual ~CraftDefinitionCooking(){}
@ -293,6 +296,8 @@ private:
std::string recipe; std::string recipe;
// Time in seconds // Time in seconds
float cooktime; float cooktime;
// Replacement items for decrementInput()
CraftReplacements replacements;
}; };
/* /*
@ -305,8 +310,10 @@ public:
CraftDefinitionFuel(): CraftDefinitionFuel():
recipe(""), burntime() recipe(""), burntime()
{} {}
CraftDefinitionFuel(std::string recipe_, float burntime_): CraftDefinitionFuel(std::string recipe_,
recipe(recipe_), burntime(burntime_) float burntime_,
const CraftReplacements &replacements_):
recipe(recipe_), burntime(burntime_), replacements(replacements_)
{} {}
virtual ~CraftDefinitionFuel(){} virtual ~CraftDefinitionFuel(){}
@ -326,6 +333,8 @@ private:
std::string recipe; std::string recipe;
// Time in seconds // Time in seconds
float burntime; float burntime;
// Replacement items for decrementInput()
CraftReplacements replacements;
}; };
/* /*

View File

@ -4129,8 +4129,17 @@ static int l_register_craft(lua_State *L)
float cooktime = getfloatfield_default(L, table, "cooktime", 3.0); float cooktime = getfloatfield_default(L, table, "cooktime", 3.0);
CraftReplacements replacements;
lua_getfield(L, table, "replacements");
if(!lua_isnil(L, -1))
{
if(!read_craft_replacements(L, -1, replacements))
throw LuaError(L, "Invalid replacements"
" (cooking output=\"" + output + "\")");
}
CraftDefinition *def = new CraftDefinitionCooking( CraftDefinition *def = new CraftDefinitionCooking(
output, recipe, cooktime); output, recipe, cooktime, replacements);
craftdef->registerCraft(def); craftdef->registerCraft(def);
} }
/* /*
@ -4144,8 +4153,17 @@ static int l_register_craft(lua_State *L)
float burntime = getfloatfield_default(L, table, "burntime", 1.0); float burntime = getfloatfield_default(L, table, "burntime", 1.0);
CraftReplacements replacements;
lua_getfield(L, table, "replacements");
if(!lua_isnil(L, -1))
{
if(!read_craft_replacements(L, -1, replacements))
throw LuaError(L, "Invalid replacements"
" (fuel recipe=\"" + recipe + "\")");
}
CraftDefinition *def = new CraftDefinitionFuel( CraftDefinition *def = new CraftDefinitionFuel(
recipe, burntime); recipe, burntime, replacements);
craftdef->registerCraft(def); craftdef->registerCraft(def);
} }
else else