Skip to content

Commit

Permalink
Mode node definition loading from Lua (still not finished), fix metad…
Browse files Browse the repository at this point in the history
…ata creation from name
  • Loading branch information
celeron55 committed Nov 29, 2011
1 parent 6a8f913 commit 18bb0ea
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 51 deletions.
50 changes: 41 additions & 9 deletions data/mods/default/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ end

-- print("minetest dump: "..dump(minetest))

--
-- Tool definition
--

minetest.register_tool("WPick", {
image = "tool_woodpick.png",
basetime = 2.0,
Expand Down Expand Up @@ -330,15 +334,9 @@ minetest.register_tool("horribletool", {
})
--]]

minetest.register_node("somenode", {
tile_images = {"lava.png", "mese.png", "stone.png", "grass.png", "cobble.png", "tree_top.png"},
inventory_image = "treeprop.png"
})

minetest.register_node("TNT", {
tile_images = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png", "tnt_side.png", "tnt_side.png", "tnt_side.png"},
inventory_image = "tnt_side.png"
})
--
-- Crafting definition
--

minetest.register_craft({
output = 'NodeItem "wood" 4',
Expand Down Expand Up @@ -617,6 +615,40 @@ minetest.register_craft({
}
})

minetest.register_craft({
output = 'NodeItem "somenode" 4',
recipe = {
{'CraftItem "Stick" 1'},
}
})

--
-- Node definitions
--

minetest.register_node("somenode", {
tile_images = {"lava.png", "mese.png", "stone.png", "grass.png", "cobble.png", "tree_top.png"},
inventory_image = "treeprop.png",
material = {
diggability = "normal",
weight = 0,
crackiness = 0,
crumbliness = 0,
cuttability = 0,
flammability = 0
},
metadata_name = "chest",
})

minetest.register_node("TNT", {
tile_images = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png", "tnt_side.png", "tnt_side.png", "tnt_side.png"},
inventory_image = "tnt_side.png",
dug_item = '', -- Get nothing
material = {
diggability = "not",
},
})

--
-- Some common functions
--
Expand Down
27 changes: 23 additions & 4 deletions src/content_nodemeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SignNodeMetadata::SignNodeMetadata(IGameDef *gamedef, std::string text):
NodeMetadata(gamedef),
m_text(text)
{
NodeMetadata::registerType(typeId(), typeName(), create);
NodeMetadata::registerType(typeId(), typeName(), create, create);
}
u16 SignNodeMetadata::typeId() const
{
Expand All @@ -49,6 +49,10 @@ NodeMetadata* SignNodeMetadata::create(std::istream &is, IGameDef *gamedef)
std::string text = deSerializeString(is);
return new SignNodeMetadata(gamedef, text);
}
NodeMetadata* SignNodeMetadata::create(IGameDef *gamedef)
{
return new SignNodeMetadata(gamedef, "");
}
NodeMetadata* SignNodeMetadata::clone(IGameDef *gamedef)
{
return new SignNodeMetadata(gamedef, m_text);
Expand All @@ -72,7 +76,7 @@ ChestNodeMetadata proto_ChestNodeMetadata(NULL);
ChestNodeMetadata::ChestNodeMetadata(IGameDef *gamedef):
NodeMetadata(gamedef)
{
NodeMetadata::registerType(typeId(), typeName(), create);
NodeMetadata::registerType(typeId(), typeName(), create, create);

m_inventory = new Inventory();
m_inventory->addList("0", 8*4);
Expand All @@ -91,6 +95,11 @@ NodeMetadata* ChestNodeMetadata::create(std::istream &is, IGameDef *gamedef)
d->m_inventory->deSerialize(is, gamedef);
return d;
}
NodeMetadata* ChestNodeMetadata::create(IGameDef *gamedef)
{
ChestNodeMetadata *d = new ChestNodeMetadata(gamedef);
return d;
}
NodeMetadata* ChestNodeMetadata::clone(IGameDef *gamedef)
{
ChestNodeMetadata *d = new ChestNodeMetadata(gamedef);
Expand Down Expand Up @@ -135,7 +144,7 @@ LockingChestNodeMetadata proto_LockingChestNodeMetadata(NULL);
LockingChestNodeMetadata::LockingChestNodeMetadata(IGameDef *gamedef):
NodeMetadata(gamedef)
{
NodeMetadata::registerType(typeId(), typeName(), create);
NodeMetadata::registerType(typeId(), typeName(), create, create);

m_inventory = new Inventory();
m_inventory->addList("0", 8*4);
Expand All @@ -155,6 +164,11 @@ NodeMetadata* LockingChestNodeMetadata::create(std::istream &is, IGameDef *gamed
d->m_inventory->deSerialize(is, gamedef);
return d;
}
NodeMetadata* LockingChestNodeMetadata::create(IGameDef *gamedef)
{
LockingChestNodeMetadata *d = new LockingChestNodeMetadata(gamedef);
return d;
}
NodeMetadata* LockingChestNodeMetadata::clone(IGameDef *gamedef)
{
LockingChestNodeMetadata *d = new LockingChestNodeMetadata(gamedef);
Expand Down Expand Up @@ -200,7 +214,7 @@ FurnaceNodeMetadata proto_FurnaceNodeMetadata(NULL);
FurnaceNodeMetadata::FurnaceNodeMetadata(IGameDef *gamedef):
NodeMetadata(gamedef)
{
NodeMetadata::registerType(typeId(), typeName(), create);
NodeMetadata::registerType(typeId(), typeName(), create, create);

m_inventory = new Inventory();
m_inventory->addList("fuel", 1);
Expand Down Expand Up @@ -241,6 +255,11 @@ NodeMetadata* FurnaceNodeMetadata::create(std::istream &is, IGameDef *gamedef)

return d;
}
NodeMetadata* FurnaceNodeMetadata::create(IGameDef *gamedef)
{
FurnaceNodeMetadata *d = new FurnaceNodeMetadata(gamedef);
return d;
}
void FurnaceNodeMetadata::serializeBody(std::ostream &os)
{
m_inventory->serialize(os);
Expand Down
4 changes: 4 additions & 0 deletions src/content_nodemeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SignNodeMetadata : public NodeMetadata
virtual const char* typeName() const
{ return "sign"; }
static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
static NodeMetadata* create(IGameDef *gamedef);
virtual NodeMetadata* clone(IGameDef *gamedef);
virtual void serializeBody(std::ostream &os);
virtual std::string infoText();
Expand All @@ -56,6 +57,7 @@ class ChestNodeMetadata : public NodeMetadata
virtual const char* typeName() const
{ return "chest"; }
static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
static NodeMetadata* create(IGameDef *gamedef);
virtual NodeMetadata* clone(IGameDef *gamedef);
virtual void serializeBody(std::ostream &os);
virtual std::string infoText();
Expand All @@ -77,6 +79,7 @@ class LockingChestNodeMetadata : public NodeMetadata
virtual const char* typeName() const
{ return "locked_chest"; }
static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
static NodeMetadata* create(IGameDef *gamedef);
virtual NodeMetadata* clone(IGameDef *gamedef);
virtual void serializeBody(std::ostream &os);
virtual std::string infoText();
Expand All @@ -103,6 +106,7 @@ class FurnaceNodeMetadata : public NodeMetadata
{ return "furnace"; }
virtual NodeMetadata* clone(IGameDef *gamedef);
static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
static NodeMetadata* create(IGameDef *gamedef);
virtual void serializeBody(std::ostream &os);
virtual std::string infoText();
virtual Inventory* getInventory() {return m_inventory;}
Expand Down
17 changes: 8 additions & 9 deletions src/nodemetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/

core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
core::map<std::string, NodeMetadata::Factory> NodeMetadata::m_names;
core::map<std::string, NodeMetadata::Factory2> NodeMetadata::m_names;

NodeMetadata::NodeMetadata(IGameDef *gamedef):
m_gamedef(gamedef)
Expand All @@ -45,7 +45,7 @@ NodeMetadata::~NodeMetadata()
NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
{
// Find factory function
core::map<std::string, Factory>::Node *n;
core::map<std::string, Factory2>::Node *n;
n = m_names.find(name);
if(n == NULL)
{
Expand All @@ -58,10 +58,8 @@ NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
// Try to load the metadata. If it fails, just return.
try
{
std::istringstream iss("", std::ios_base::binary);

Factory f = n->getValue();
NodeMetadata *meta = (*f)(iss, gamedef);
Factory2 f2 = n->getValue();
NodeMetadata *meta = (*f2)(gamedef);
return meta;
}
catch(SerializationError &e)
Expand Down Expand Up @@ -120,7 +118,8 @@ void NodeMetadata::serialize(std::ostream &os)
os<<serializeString(oss.str());
}

void NodeMetadata::registerType(u16 id, const std::string &name, Factory f)
void NodeMetadata::registerType(u16 id, const std::string &name, Factory f,
Factory2 f2)
{
{ // typeId
core::map<u16, Factory>::Node *n;
Expand All @@ -129,10 +128,10 @@ void NodeMetadata::registerType(u16 id, const std::string &name, Factory f)
m_types.insert(id, f);
}
{ // typeName
core::map<std::string, Factory>::Node *n;
core::map<std::string, Factory2>::Node *n;
n = m_names.find(name);
if(!n)
m_names.insert(name, f);
m_names.insert(name, f2);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/nodemetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class NodeMetadata
{
public:
typedef NodeMetadata* (*Factory)(std::istream&, IGameDef *gamedef);
typedef NodeMetadata* (*Factory2)(IGameDef *gamedef);

NodeMetadata(IGameDef *gamedef);
virtual ~NodeMetadata();
Expand Down Expand Up @@ -70,11 +71,12 @@ class NodeMetadata
virtual std::string getText(){ return ""; }
virtual void setText(const std::string &t){}
protected:
static void registerType(u16 id, const std::string &name, Factory f);
static void registerType(u16 id, const std::string &name, Factory f,
Factory2 f2);
IGameDef *m_gamedef;
private:
static core::map<u16, Factory> m_types;
static core::map<std::string, Factory> m_names;
static core::map<std::string, Factory2> m_names;
};

/*
Expand Down
Loading

0 comments on commit 18bb0ea

Please sign in to comment.