/*
CraftReplacements
*/
+
std::string CraftReplacements::dump() const
{
std::ostringstream os(std::ios::binary);
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
writeU16(os, recipe.size());
for(u32 i=0; i<recipe.size(); i++)
os<<serializeString(recipe[i]);
- writeU16(os, replacements.pairs.size());
- for(u32 i=0; i<replacements.pairs.size(); i++)
- {
- os<<serializeString(replacements.pairs[i].first);
- os<<serializeString(replacements.pairs[i].second);
- }
+ replacements.serialize(os);
}
void CraftDefinitionShaped::deSerializeBody(std::istream &is, int version)
u32 count = readU16(is);
for(u32 i=0; i<count; i++)
recipe.push_back(deSerializeString(is));
- replacements.pairs.clear();
- 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));
- }
+ replacements.deSerialize(is);
}
/*
writeU16(os, recipe.size());
for(u32 i=0; i<recipe.size(); i++)
os<<serializeString(recipe[i]);
- writeU16(os, replacements.pairs.size());
- for(u32 i=0; i<replacements.pairs.size(); i++)
- {
- os<<serializeString(replacements.pairs[i].first);
- os<<serializeString(replacements.pairs[i].second);
- }
+ replacements.serialize(os);
}
void CraftDefinitionShapeless::deSerializeBody(std::istream &is, int version)
u32 count = readU16(is);
for(u32 i=0; i<count; i++)
recipe.push_back(deSerializeString(is));
- replacements.pairs.clear();
- 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));
- }
+ replacements.deSerialize(is);
}
/*
void CraftDefinitionCooking::decrementInput(CraftInput &input, IGameDef *gamedef) const
{
- craftDecrementInput(input, gamedef);
+ craftDecrementOrReplaceInput(input, replacements, gamedef);
}
std::string CraftDefinitionCooking::dump() const
std::ostringstream os(std::ios::binary);
os<<"(cooking, output=\""<<output
<<"\", recipe=\""<<recipe
- <<"\", cooktime="<<cooktime<<")";
+ <<"\", cooktime="<<cooktime<<")"
+ <<", replacements="<<replacements.dump()<<")";
return os.str();
}
os<<serializeString(output);
os<<serializeString(recipe);
writeF1000(os, cooktime);
+ replacements.serialize(os);
}
void CraftDefinitionCooking::deSerializeBody(std::istream &is, int version)
output = deSerializeString(is);
recipe = deSerializeString(is);
cooktime = readF1000(is);
+ replacements.deSerialize(is);
}
/*
void CraftDefinitionFuel::decrementInput(CraftInput &input, IGameDef *gamedef) const
{
- craftDecrementInput(input, gamedef);
+ craftDecrementOrReplaceInput(input, replacements, gamedef);
}
std::string CraftDefinitionFuel::dump() const
{
std::ostringstream os(std::ios::binary);
os<<"(fuel, recipe=\""<<recipe
- <<"\", burntime="<<burntime<<")";
+ <<"\", burntime="<<burntime<<")"
+ <<", replacements="<<replacements.dump()<<")";
return os.str();
}
{
os<<serializeString(recipe);
writeF1000(os, burntime);
+ replacements.serialize(os);
}
void CraftDefinitionFuel::deSerializeBody(std::istream &is, int version)
"unsupported CraftDefinitionFuel version");
recipe = deSerializeString(is);
burntime = readF1000(is);
+ replacements.deSerialize(is);
}
/*
pairs(pairs_)
{}
std::string dump() const;
+ void serialize(std::ostream &os) const;
+ void deSerialize(std::istream &is);
};
/*
CraftDefinitionCooking(
const std::string &output_,
const std::string &recipe_,
- float cooktime_):
- output(output_), recipe(recipe_), cooktime(cooktime_)
+ float cooktime_,
+ const CraftReplacements &replacements_):
+ output(output_), recipe(recipe_), cooktime(cooktime_), replacements(replacements_)
{}
virtual ~CraftDefinitionCooking(){}
std::string recipe;
// Time in seconds
float cooktime;
+ // Replacement items for decrementInput()
+ CraftReplacements replacements;
};
/*
CraftDefinitionFuel():
recipe(""), burntime()
{}
- CraftDefinitionFuel(std::string recipe_, float burntime_):
- recipe(recipe_), burntime(burntime_)
+ CraftDefinitionFuel(std::string recipe_,
+ float burntime_,
+ const CraftReplacements &replacements_):
+ recipe(recipe_), burntime(burntime_), replacements(replacements_)
{}
virtual ~CraftDefinitionFuel(){}
std::string recipe;
// Time in seconds
float burntime;
+ // Replacement items for decrementInput()
+ CraftReplacements replacements;
};
/*
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(
- output, recipe, cooktime);
+ output, recipe, cooktime, replacements);
craftdef->registerCraft(def);
}
/*
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(
- recipe, burntime);
+ recipe, burntime, replacements);
craftdef->registerCraft(def);
}
else