Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Mode node definition loading from Lua (still not finished), fix metad…

…ata creation from name
  • Loading branch information...
commit 18bb0ea1ead82406bcfb89ea14908a4d0063209e 1 parent 6a8f913
celeron55 celeron55 authored
50 data/mods/default/init.lua
@@ -131,6 +131,10 @@ end
131 131
132 132 -- print("minetest dump: "..dump(minetest))
133 133
  134 +--
  135 +-- Tool definition
  136 +--
  137 +
134 138 minetest.register_tool("WPick", {
135 139 image = "tool_woodpick.png",
136 140 basetime = 2.0,
@@ -330,15 +334,9 @@ minetest.register_tool("horribletool", {
330 334 })
331 335 --]]
332 336
333   -minetest.register_node("somenode", {
334   - tile_images = {"lava.png", "mese.png", "stone.png", "grass.png", "cobble.png", "tree_top.png"},
335   - inventory_image = "treeprop.png"
336   -})
337   -
338   -minetest.register_node("TNT", {
339   - tile_images = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png", "tnt_side.png", "tnt_side.png", "tnt_side.png"},
340   - inventory_image = "tnt_side.png"
341   -})
  337 +--
  338 +-- Crafting definition
  339 +--
342 340
343 341 minetest.register_craft({
344 342 output = 'NodeItem "wood" 4',
@@ -617,6 +615,40 @@ minetest.register_craft({
617 615 }
618 616 })
619 617
  618 +minetest.register_craft({
  619 + output = 'NodeItem "somenode" 4',
  620 + recipe = {
  621 + {'CraftItem "Stick" 1'},
  622 + }
  623 +})
  624 +
  625 +--
  626 +-- Node definitions
  627 +--
  628 +
  629 +minetest.register_node("somenode", {
  630 + tile_images = {"lava.png", "mese.png", "stone.png", "grass.png", "cobble.png", "tree_top.png"},
  631 + inventory_image = "treeprop.png",
  632 + material = {
  633 + diggability = "normal",
  634 + weight = 0,
  635 + crackiness = 0,
  636 + crumbliness = 0,
  637 + cuttability = 0,
  638 + flammability = 0
  639 + },
  640 + metadata_name = "chest",
  641 +})
  642 +
  643 +minetest.register_node("TNT", {
  644 + tile_images = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png", "tnt_side.png", "tnt_side.png", "tnt_side.png"},
  645 + inventory_image = "tnt_side.png",
  646 + dug_item = '', -- Get nothing
  647 + material = {
  648 + diggability = "not",
  649 + },
  650 +})
  651 +
620 652 --
621 653 -- Some common functions
622 654 --
27 src/content_nodemeta.cpp
@@ -38,7 +38,7 @@ SignNodeMetadata::SignNodeMetadata(IGameDef *gamedef, std::string text):
38 38 NodeMetadata(gamedef),
39 39 m_text(text)
40 40 {
41   - NodeMetadata::registerType(typeId(), typeName(), create);
  41 + NodeMetadata::registerType(typeId(), typeName(), create, create);
42 42 }
43 43 u16 SignNodeMetadata::typeId() const
44 44 {
@@ -49,6 +49,10 @@ NodeMetadata* SignNodeMetadata::create(std::istream &is, IGameDef *gamedef)
49 49 std::string text = deSerializeString(is);
50 50 return new SignNodeMetadata(gamedef, text);
51 51 }
  52 +NodeMetadata* SignNodeMetadata::create(IGameDef *gamedef)
  53 +{
  54 + return new SignNodeMetadata(gamedef, "");
  55 +}
52 56 NodeMetadata* SignNodeMetadata::clone(IGameDef *gamedef)
53 57 {
54 58 return new SignNodeMetadata(gamedef, m_text);
@@ -72,7 +76,7 @@ ChestNodeMetadata proto_ChestNodeMetadata(NULL);
72 76 ChestNodeMetadata::ChestNodeMetadata(IGameDef *gamedef):
73 77 NodeMetadata(gamedef)
74 78 {
75   - NodeMetadata::registerType(typeId(), typeName(), create);
  79 + NodeMetadata::registerType(typeId(), typeName(), create, create);
76 80
77 81 m_inventory = new Inventory();
78 82 m_inventory->addList("0", 8*4);
@@ -91,6 +95,11 @@ NodeMetadata* ChestNodeMetadata::create(std::istream &is, IGameDef *gamedef)
91 95 d->m_inventory->deSerialize(is, gamedef);
92 96 return d;
93 97 }
  98 +NodeMetadata* ChestNodeMetadata::create(IGameDef *gamedef)
  99 +{
  100 + ChestNodeMetadata *d = new ChestNodeMetadata(gamedef);
  101 + return d;
  102 +}
94 103 NodeMetadata* ChestNodeMetadata::clone(IGameDef *gamedef)
95 104 {
96 105 ChestNodeMetadata *d = new ChestNodeMetadata(gamedef);
@@ -135,7 +144,7 @@ LockingChestNodeMetadata proto_LockingChestNodeMetadata(NULL);
135 144 LockingChestNodeMetadata::LockingChestNodeMetadata(IGameDef *gamedef):
136 145 NodeMetadata(gamedef)
137 146 {
138   - NodeMetadata::registerType(typeId(), typeName(), create);
  147 + NodeMetadata::registerType(typeId(), typeName(), create, create);
139 148
140 149 m_inventory = new Inventory();
141 150 m_inventory->addList("0", 8*4);
@@ -155,6 +164,11 @@ NodeMetadata* LockingChestNodeMetadata::create(std::istream &is, IGameDef *gamed
155 164 d->m_inventory->deSerialize(is, gamedef);
156 165 return d;
157 166 }
  167 +NodeMetadata* LockingChestNodeMetadata::create(IGameDef *gamedef)
  168 +{
  169 + LockingChestNodeMetadata *d = new LockingChestNodeMetadata(gamedef);
  170 + return d;
  171 +}
158 172 NodeMetadata* LockingChestNodeMetadata::clone(IGameDef *gamedef)
159 173 {
160 174 LockingChestNodeMetadata *d = new LockingChestNodeMetadata(gamedef);
@@ -200,7 +214,7 @@ FurnaceNodeMetadata proto_FurnaceNodeMetadata(NULL);
200 214 FurnaceNodeMetadata::FurnaceNodeMetadata(IGameDef *gamedef):
201 215 NodeMetadata(gamedef)
202 216 {
203   - NodeMetadata::registerType(typeId(), typeName(), create);
  217 + NodeMetadata::registerType(typeId(), typeName(), create, create);
204 218
205 219 m_inventory = new Inventory();
206 220 m_inventory->addList("fuel", 1);
@@ -241,6 +255,11 @@ NodeMetadata* FurnaceNodeMetadata::create(std::istream &is, IGameDef *gamedef)
241 255
242 256 return d;
243 257 }
  258 +NodeMetadata* FurnaceNodeMetadata::create(IGameDef *gamedef)
  259 +{
  260 + FurnaceNodeMetadata *d = new FurnaceNodeMetadata(gamedef);
  261 + return d;
  262 +}
244 263 void FurnaceNodeMetadata::serializeBody(std::ostream &os)
245 264 {
246 265 m_inventory->serialize(os);
4 src/content_nodemeta.h
@@ -34,6 +34,7 @@ class SignNodeMetadata : public NodeMetadata
34 34 virtual const char* typeName() const
35 35 { return "sign"; }
36 36 static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
  37 + static NodeMetadata* create(IGameDef *gamedef);
37 38 virtual NodeMetadata* clone(IGameDef *gamedef);
38 39 virtual void serializeBody(std::ostream &os);
39 40 virtual std::string infoText();
@@ -56,6 +57,7 @@ class ChestNodeMetadata : public NodeMetadata
56 57 virtual const char* typeName() const
57 58 { return "chest"; }
58 59 static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
  60 + static NodeMetadata* create(IGameDef *gamedef);
59 61 virtual NodeMetadata* clone(IGameDef *gamedef);
60 62 virtual void serializeBody(std::ostream &os);
61 63 virtual std::string infoText();
@@ -77,6 +79,7 @@ class LockingChestNodeMetadata : public NodeMetadata
77 79 virtual const char* typeName() const
78 80 { return "locked_chest"; }
79 81 static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
  82 + static NodeMetadata* create(IGameDef *gamedef);
80 83 virtual NodeMetadata* clone(IGameDef *gamedef);
81 84 virtual void serializeBody(std::ostream &os);
82 85 virtual std::string infoText();
@@ -103,6 +106,7 @@ class FurnaceNodeMetadata : public NodeMetadata
103 106 { return "furnace"; }
104 107 virtual NodeMetadata* clone(IGameDef *gamedef);
105 108 static NodeMetadata* create(std::istream &is, IGameDef *gamedef);
  109 + static NodeMetadata* create(IGameDef *gamedef);
106 110 virtual void serializeBody(std::ostream &os);
107 111 virtual std::string infoText();
108 112 virtual Inventory* getInventory() {return m_inventory;}
17 src/nodemetadata.cpp
@@ -31,7 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
31 31 */
32 32
33 33 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
34   -core::map<std::string, NodeMetadata::Factory> NodeMetadata::m_names;
  34 +core::map<std::string, NodeMetadata::Factory2> NodeMetadata::m_names;
35 35
36 36 NodeMetadata::NodeMetadata(IGameDef *gamedef):
37 37 m_gamedef(gamedef)
@@ -45,7 +45,7 @@ NodeMetadata::~NodeMetadata()
45 45 NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
46 46 {
47 47 // Find factory function
48   - core::map<std::string, Factory>::Node *n;
  48 + core::map<std::string, Factory2>::Node *n;
49 49 n = m_names.find(name);
50 50 if(n == NULL)
51 51 {
@@ -58,10 +58,8 @@ NodeMetadata* NodeMetadata::create(const std::string &name, IGameDef *gamedef)
58 58 // Try to load the metadata. If it fails, just return.
59 59 try
60 60 {
61   - std::istringstream iss("", std::ios_base::binary);
62   -
63   - Factory f = n->getValue();
64   - NodeMetadata *meta = (*f)(iss, gamedef);
  61 + Factory2 f2 = n->getValue();
  62 + NodeMetadata *meta = (*f2)(gamedef);
65 63 return meta;
66 64 }
67 65 catch(SerializationError &e)
@@ -120,7 +118,8 @@ void NodeMetadata::serialize(std::ostream &os)
120 118 os<<serializeString(oss.str());
121 119 }
122 120
123   -void NodeMetadata::registerType(u16 id, const std::string &name, Factory f)
  121 +void NodeMetadata::registerType(u16 id, const std::string &name, Factory f,
  122 + Factory2 f2)
124 123 {
125 124 { // typeId
126 125 core::map<u16, Factory>::Node *n;
@@ -129,10 +128,10 @@ void NodeMetadata::registerType(u16 id, const std::string &name, Factory f)
129 128 m_types.insert(id, f);
130 129 }
131 130 { // typeName
132   - core::map<std::string, Factory>::Node *n;
  131 + core::map<std::string, Factory2>::Node *n;
133 132 n = m_names.find(name);
134 133 if(!n)
135   - m_names.insert(name, f);
  134 + m_names.insert(name, f2);
136 135 }
137 136 }
138 137
6 src/nodemetadata.h
@@ -40,6 +40,7 @@ class NodeMetadata
40 40 {
41 41 public:
42 42 typedef NodeMetadata* (*Factory)(std::istream&, IGameDef *gamedef);
  43 + typedef NodeMetadata* (*Factory2)(IGameDef *gamedef);
43 44
44 45 NodeMetadata(IGameDef *gamedef);
45 46 virtual ~NodeMetadata();
@@ -70,11 +71,12 @@ class NodeMetadata
70 71 virtual std::string getText(){ return ""; }
71 72 virtual void setText(const std::string &t){}
72 73 protected:
73   - static void registerType(u16 id, const std::string &name, Factory f);
  74 + static void registerType(u16 id, const std::string &name, Factory f,
  75 + Factory2 f2);
74 76 IGameDef *m_gamedef;
75 77 private:
76 78 static core::map<u16, Factory> m_types;
77   - static core::map<std::string, Factory> m_names;
  79 + static core::map<std::string, Factory2> m_names;
78 80 };
79 81
80 82 /*
135 src/scriptapi.cpp
@@ -48,7 +48,7 @@ extern "C" {
48 48 - Blink effect
49 49 - Spritesheets and animation
50 50 - LuaNodeMetadata
51   - blockdef.metadata_type =
  51 + blockdef.metadata_name =
52 52 ""
53 53 "sign"
54 54 "furnace"
@@ -212,6 +212,32 @@ static video::SColor readARGB8(lua_State *L, int index)
212 212 return color;
213 213 }
214 214
  215 +static core::aabbox3d<f32> read_aabbox3df32(lua_State *L, int index)
  216 +{
  217 + core::aabbox3d<f32> box;
  218 + if(lua_istable(L, -1)){
  219 + lua_rawgeti(L, -1, 1);
  220 + box.MinEdge.X = lua_tonumber(L, -1);
  221 + lua_pop(L, 1);
  222 + lua_rawgeti(L, -1, 2);
  223 + box.MinEdge.Y = lua_tonumber(L, -1);
  224 + lua_pop(L, 1);
  225 + lua_rawgeti(L, -1, 3);
  226 + box.MinEdge.Z = lua_tonumber(L, -1);
  227 + lua_pop(L, 1);
  228 + lua_rawgeti(L, -1, 4);
  229 + box.MaxEdge.X = lua_tonumber(L, -1);
  230 + lua_pop(L, 1);
  231 + lua_rawgeti(L, -1, 5);
  232 + box.MaxEdge.Y = lua_tonumber(L, -1);
  233 + lua_pop(L, 1);
  234 + lua_rawgeti(L, -1, 6);
  235 + box.MaxEdge.Z = lua_tonumber(L, -1);
  236 + lua_pop(L, 1);
  237 + }
  238 + return box;
  239 +}
  240 +
215 241 static bool getstringfield(lua_State *L, int table,
216 242 const char *fieldname, std::string &result)
217 243 {
@@ -356,6 +382,27 @@ struct EnumString es_ContentParamType[] =
356 382 {CPT_FACEDIR_SIMPLE, "facedir_simple"},
357 383 };
358 384
  385 +struct EnumString es_LiquidType[] =
  386 +{
  387 + {LIQUID_NONE, "none"},
  388 + {LIQUID_FLOWING, "flowing"},
  389 + {LIQUID_SOURCE, "source"},
  390 +};
  391 +
  392 +struct EnumString es_NodeBoxType[] =
  393 +{
  394 + {NODEBOX_REGULAR, "regular"},
  395 + {NODEBOX_FIXED, "fixed"},
  396 + {NODEBOX_WALLMOUNTED, "wallmounted"},
  397 +};
  398 +
  399 +struct EnumString es_Diggability[] =
  400 +{
  401 + {DIGGABLE_NOT, "not"},
  402 + {DIGGABLE_NORMAL, "normal"},
  403 + {DIGGABLE_CONSTANT, "constant"},
  404 +};
  405 +
359 406 /*
360 407 Global functions
361 408 */
@@ -441,17 +488,26 @@ static int l_register_node(lua_State *L)
441 488 // And get the writable node definition manager from the server
442 489 IWritableNodeDefManager *nodedef =
443 490 server->getWritableNodeDefManager();
  491 +
  492 + /*
  493 + Create definition
  494 + */
444 495
445 496 ContentFeatures f;
446 497
447   - f.name = name;
  498 + // Default to getting the corresponding NodeItem when dug
  499 + f.dug_item = std::string("NodeItem \"")+name+"\" 1";
448 500
449 501 /*
450   - Visual definition
  502 + Read definiton from Lua
451 503 */
452 504
  505 + f.name = name;
  506 +
  507 + /* Visual definition */
  508 +
453 509 f.drawtype = (NodeDrawType)getenumfield(L, table0, "drawtype", es_DrawType,
454   - f.drawtype);
  510 + NDT_NORMAL);
455 511 getfloatfield(L, table0, "visual_scale", f.visual_scale);
456 512
457 513 lua_getfield(L, table0, "tile_images");
@@ -505,16 +561,15 @@ static int l_register_node(lua_State *L)
505 561
506 562 f.alpha = getintfield_default(L, table0, "alpha", 255);
507 563
508   - /*
509   - Other stuff
510   - */
  564 + /* Other stuff */
511 565
512 566 lua_getfield(L, table0, "post_effect_color");
513 567 if(!lua_isnil(L, -1))
514 568 f.post_effect_color = readARGB8(L, -1);
  569 + lua_pop(L, 1);
515 570
516 571 f.param_type = (ContentParamType)getenumfield(L, table0, "paramtype",
517   - es_ContentParamType, f.param_type);
  572 + es_ContentParamType, CPT_NONE);
518 573
519 574 // True for all ground-like things like stone and mud, false for eg. trees
520 575 getboolfield(L, table0, "is_ground_content", f.is_ground_content);
@@ -551,8 +606,8 @@ static int l_register_node(lua_State *L)
551 606 // Metadata name of node (eg. "furnace")
552 607 getstringfield(L, table0, "metadata_name", f.metadata_name);
553 608 // Whether the node is non-liquid, source liquid or flowing liquid
554   - // TODO: Enum read
555   - // enum LiquidType liquid_type;
  609 + f.liquid_type = (LiquidType)getenumfield(L, table0, "liquidtype",
  610 + es_LiquidType, LIQUID_NONE);
556 611 // If the content is liquid, this is the flowing version of the liquid.
557 612 // TODO: as name
558 613 // content_t liquid_alternative_flowing;
@@ -569,30 +624,56 @@ static int l_register_node(lua_State *L)
569 624 "light_source", f.light_source);
570 625 f.damage_per_second = getintfield_default(L, table0,
571 626 "damage_per_second", f.damage_per_second);
572   - // TODO
573   - //NodeBox selection_box;
574   - // TODO
575   - //MaterialProperties material;
  627 +
  628 + lua_getfield(L, table0, "selection_box");
  629 + if(lua_istable(L, -1)){
  630 + f.selection_box.type = (NodeBoxType)getenumfield(L, -1, "type",
  631 + es_NodeBoxType, NODEBOX_REGULAR);
  632 +
  633 + lua_getfield(L, -1, "fixed");
  634 + if(lua_istable(L, -1))
  635 + f.selection_box.fixed = read_aabbox3df32(L, -1);
  636 + lua_pop(L, 1);
  637 +
  638 + lua_getfield(L, -1, "wall_top");
  639 + if(lua_istable(L, -1))
  640 + f.selection_box.wall_top = read_aabbox3df32(L, -1);
  641 + lua_pop(L, 1);
  642 +
  643 + lua_getfield(L, -1, "wall_bottom");
  644 + if(lua_istable(L, -1))
  645 + f.selection_box.wall_bottom = read_aabbox3df32(L, -1);
  646 + lua_pop(L, 1);
  647 +
  648 + lua_getfield(L, -1, "wall_side");
  649 + if(lua_istable(L, -1))
  650 + f.selection_box.wall_side = read_aabbox3df32(L, -1);
  651 + lua_pop(L, 1);
  652 + }
  653 + lua_pop(L, 1);
  654 +
  655 + lua_getfield(L, table0, "material");
  656 + if(lua_istable(L, -1)){
  657 + f.material.diggability = (Diggability)getenumfield(L, -1, "diggability",
  658 + es_Diggability, DIGGABLE_NORMAL);
  659 +
  660 + getfloatfield(L, -1, "constant_time", f.material.constant_time);
  661 + getfloatfield(L, -1, "weight", f.material.weight);
  662 + getfloatfield(L, -1, "crackiness", f.material.crackiness);
  663 + getfloatfield(L, -1, "crumbliness", f.material.crumbliness);
  664 + getfloatfield(L, -1, "cuttability", f.material.cuttability);
  665 + getfloatfield(L, -1, "flammability", f.material.flammability);
  666 + }
  667 + lua_pop(L, 1);
  668 +
576 669 getstringfield(L, table0, "cookresult_item", f.cookresult_item);
577 670 getfloatfield(L, table0, "furnace_cooktime", f.furnace_cooktime);
578 671 getfloatfield(L, table0, "furnace_burntime", f.furnace_burntime);
579 672
580 673 /*
581   - Temporary stuff
582   - */
583   -
584   - // TODO: Replace with actual parameter reading
585   - // Temporarily set some sane parameters to allow digging
586   - f.material.diggability = DIGGABLE_NORMAL;
587   - f.material.weight = 0;
588   - f.material.crackiness = 0;
589   - f.material.crumbliness = 0;
590   - f.material.cuttability = 0;
591   - f.dug_item = std::string("NodeItem \"")+name+"\" 1";
592   -
593   - /*
594 674 Register it
595 675 */
  676 +
596 677 nodedef->set(name, f);
597 678
598 679 return 0; /* number of results */

0 comments on commit 18bb0ea

Please sign in to comment.
Something went wrong with that request. Please try again.