3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "common/c_content.h"
20 #include "common/c_converter.h"
21 #include "common/c_types.h"
24 #include "object_properties.h"
25 #include "cpp_api/s_node.h"
26 #include "lua_api/l_object.h"
27 #include "lua_api/l_item.h"
28 #include "common/c_internal.h"
32 #include "serverobject.h"
35 struct EnumString es_TileAnimationType[] =
38 {TAT_VERTICAL_FRAMES, "vertical_frames"},
42 /******************************************************************************/
43 ItemDefinition read_item_definition(lua_State* L,int index,
44 ItemDefinition default_def)
47 index = lua_gettop(L) + 1 + index;
49 // Read the item definition
50 ItemDefinition def = default_def;
52 def.type = (ItemType)getenumfield(L, index, "type",
53 es_ItemType, ITEM_NONE);
54 getstringfield(L, index, "name", def.name);
55 getstringfield(L, index, "description", def.description);
56 getstringfield(L, index, "inventory_image", def.inventory_image);
57 getstringfield(L, index, "wield_image", def.wield_image);
59 lua_getfield(L, index, "wield_scale");
60 if(lua_istable(L, -1)){
61 def.wield_scale = check_v3f(L, -1);
65 def.stack_max = getintfield_default(L, index, "stack_max", def.stack_max);
66 if(def.stack_max == 0)
69 lua_getfield(L, index, "on_use");
70 def.usable = lua_isfunction(L, -1);
73 getboolfield(L, index, "liquids_pointable", def.liquids_pointable);
75 warn_if_field_exists(L, index, "tool_digging_properties",
76 "deprecated: use tool_capabilities");
78 lua_getfield(L, index, "tool_capabilities");
79 if(lua_istable(L, -1)){
80 def.tool_capabilities = new ToolCapabilities(
81 read_tool_capabilities(L, -1));
84 // If name is "" (hand), ensure there are ToolCapabilities
85 // because it will be looked up there whenever any other item has
86 // no ToolCapabilities
87 if(def.name == "" && def.tool_capabilities == NULL){
88 def.tool_capabilities = new ToolCapabilities();
91 lua_getfield(L, index, "groups");
92 read_groups(L, -1, def.groups);
95 lua_getfield(L, index, "sounds");
96 if(lua_istable(L, -1)){
97 lua_getfield(L, -1, "place");
98 read_soundspec(L, -1, def.sound_place);
103 def.range = getfloatfield_default(L, index, "range", def.range);
105 // Client shall immediately place this node when player places the item.
106 // Server will update the precise end result a moment later.
107 // "" = no prediction
108 getstringfield(L, index, "node_placement_prediction",
109 def.node_placement_prediction);
114 /******************************************************************************/
115 void read_object_properties(lua_State *L, int index,
116 ObjectProperties *prop)
119 index = lua_gettop(L) + 1 + index;
120 if(!lua_istable(L, index))
123 prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
125 getboolfield(L, -1, "physical", prop->physical);
126 getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
128 getfloatfield(L, -1, "weight", prop->weight);
130 lua_getfield(L, -1, "collisionbox");
131 if(lua_istable(L, -1))
132 prop->collisionbox = read_aabb3f(L, -1, 1.0);
135 getstringfield(L, -1, "visual", prop->visual);
137 getstringfield(L, -1, "mesh", prop->mesh);
139 lua_getfield(L, -1, "visual_size");
140 if(lua_istable(L, -1))
141 prop->visual_size = read_v2f(L, -1);
144 lua_getfield(L, -1, "textures");
145 if(lua_istable(L, -1)){
146 prop->textures.clear();
147 int table = lua_gettop(L);
149 while(lua_next(L, table) != 0){
150 // key at index -2 and value at index -1
151 if(lua_isstring(L, -1))
152 prop->textures.push_back(lua_tostring(L, -1));
154 prop->textures.push_back("");
155 // removes value, keeps key for next iteration
161 lua_getfield(L, -1, "colors");
162 if(lua_istable(L, -1)){
163 prop->colors.clear();
164 int table = lua_gettop(L);
166 while(lua_next(L, table) != 0){
167 // key at index -2 and value at index -1
168 if(lua_isstring(L, -1))
169 prop->colors.push_back(readARGB8(L, -1));
171 prop->colors.push_back(video::SColor(255, 255, 255, 255));
172 // removes value, keeps key for next iteration
178 lua_getfield(L, -1, "spritediv");
179 if(lua_istable(L, -1))
180 prop->spritediv = read_v2s16(L, -1);
183 lua_getfield(L, -1, "initial_sprite_basepos");
184 if(lua_istable(L, -1))
185 prop->initial_sprite_basepos = read_v2s16(L, -1);
188 getboolfield(L, -1, "is_visible", prop->is_visible);
189 getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
190 getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
191 getfloatfield(L, -1, "stepheight", prop->stepheight);
192 prop->stepheight*=BS;
193 getboolfield(L, -1, "automatic_face_movement_dir", prop->automatic_face_movement_dir);
196 /******************************************************************************/
197 TileDef read_tiledef(lua_State *L, int index)
200 index = lua_gettop(L) + 1 + index;
204 // key at index -2 and value at index
205 if(lua_isstring(L, index)){
206 // "default_lava.png"
207 tiledef.name = lua_tostring(L, index);
209 else if(lua_istable(L, index))
211 // {name="default_lava.png", animation={}}
213 getstringfield(L, index, "name", tiledef.name);
214 getstringfield(L, index, "image", tiledef.name); // MaterialSpec compat.
215 tiledef.backface_culling = getboolfield_default(
216 L, index, "backface_culling", true);
218 lua_getfield(L, index, "animation");
219 if(lua_istable(L, -1)){
220 // {type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0}
221 tiledef.animation.type = (TileAnimationType)
222 getenumfield(L, -1, "type", es_TileAnimationType,
224 tiledef.animation.aspect_w =
225 getintfield_default(L, -1, "aspect_w", 16);
226 tiledef.animation.aspect_h =
227 getintfield_default(L, -1, "aspect_h", 16);
228 tiledef.animation.length =
229 getfloatfield_default(L, -1, "length", 1.0);
237 /******************************************************************************/
238 ContentFeatures read_content_features(lua_State *L, int index)
241 index = lua_gettop(L) + 1 + index;
245 /* Cache existence of some callbacks */
246 lua_getfield(L, index, "on_construct");
247 if(!lua_isnil(L, -1)) f.has_on_construct = true;
249 lua_getfield(L, index, "on_destruct");
250 if(!lua_isnil(L, -1)) f.has_on_destruct = true;
252 lua_getfield(L, index, "after_destruct");
253 if(!lua_isnil(L, -1)) f.has_after_destruct = true;
256 lua_getfield(L, index, "on_rightclick");
257 f.rightclickable = lua_isfunction(L, -1);
261 getstringfield(L, index, "name", f.name);
264 lua_getfield(L, index, "groups");
265 read_groups(L, -1, f.groups);
268 /* Visual definition */
270 f.drawtype = (NodeDrawType)getenumfield(L, index, "drawtype",
271 ScriptApiNode::es_DrawType,NDT_NORMAL);
272 getfloatfield(L, index, "visual_scale", f.visual_scale);
275 lua_getfield(L, index, "tiles");
276 // If nil, try the deprecated name "tile_images" instead
277 if(lua_isnil(L, -1)){
279 warn_if_field_exists(L, index, "tile_images",
280 "Deprecated; new name is \"tiles\".");
281 lua_getfield(L, index, "tile_images");
283 if(lua_istable(L, -1)){
284 int table = lua_gettop(L);
287 while(lua_next(L, table) != 0){
288 // Read tiledef from value
289 f.tiledef[i] = read_tiledef(L, -1);
290 // removes value, keeps key for next iteration
298 // Copy last value to all remaining textures
300 TileDef lasttile = f.tiledef[i-1];
302 f.tiledef[i] = lasttile;
309 // special_tiles = {}
310 lua_getfield(L, index, "special_tiles");
311 // If nil, try the deprecated name "special_materials" instead
312 if(lua_isnil(L, -1)){
314 warn_if_field_exists(L, index, "special_materials",
315 "Deprecated; new name is \"special_tiles\".");
316 lua_getfield(L, index, "special_materials");
318 if(lua_istable(L, -1)){
319 int table = lua_gettop(L);
322 while(lua_next(L, table) != 0){
323 // Read tiledef from value
324 f.tiledef_special[i] = read_tiledef(L, -1);
325 // removes value, keeps key for next iteration
336 f.alpha = getintfield_default(L, index, "alpha", 255);
338 bool usealpha = getboolfield_default(L, index,
339 "use_texture_alpha", false);
345 lua_getfield(L, index, "post_effect_color");
346 if(!lua_isnil(L, -1))
347 f.post_effect_color = readARGB8(L, -1);
350 f.param_type = (ContentParamType)getenumfield(L, index, "paramtype",
351 ScriptApiNode::es_ContentParamType, CPT_NONE);
352 f.param_type_2 = (ContentParamType2)getenumfield(L, index, "paramtype2",
353 ScriptApiNode::es_ContentParamType2, CPT2_NONE);
355 // Warn about some deprecated fields
356 warn_if_field_exists(L, index, "wall_mounted",
357 "deprecated: use paramtype2 = 'wallmounted'");
358 warn_if_field_exists(L, index, "light_propagates",
359 "deprecated: determined from paramtype");
360 warn_if_field_exists(L, index, "dug_item",
361 "deprecated: use 'drop' field");
362 warn_if_field_exists(L, index, "extra_dug_item",
363 "deprecated: use 'drop' field");
364 warn_if_field_exists(L, index, "extra_dug_item_rarity",
365 "deprecated: use 'drop' field");
366 warn_if_field_exists(L, index, "metadata_name",
367 "deprecated: use on_add and metadata callbacks");
369 // True for all ground-like things like stone and mud, false for eg. trees
370 getboolfield(L, index, "is_ground_content", f.is_ground_content);
371 f.light_propagates = (f.param_type == CPT_LIGHT);
372 getboolfield(L, index, "sunlight_propagates", f.sunlight_propagates);
373 // This is used for collision detection.
374 // Also for general solidness queries.
375 getboolfield(L, index, "walkable", f.walkable);
376 // Player can point to these
377 getboolfield(L, index, "pointable", f.pointable);
378 // Player can dig these
379 getboolfield(L, index, "diggable", f.diggable);
380 // Player can climb these
381 getboolfield(L, index, "climbable", f.climbable);
382 // Player can build on these
383 getboolfield(L, index, "buildable_to", f.buildable_to);
384 // Whether the node is non-liquid, source liquid or flowing liquid
385 f.liquid_type = (LiquidType)getenumfield(L, index, "liquidtype",
386 ScriptApiNode::es_LiquidType, LIQUID_NONE);
387 // If the content is liquid, this is the flowing version of the liquid.
388 getstringfield(L, index, "liquid_alternative_flowing",
389 f.liquid_alternative_flowing);
390 // If the content is liquid, this is the source version of the liquid.
391 getstringfield(L, index, "liquid_alternative_source",
392 f.liquid_alternative_source);
393 // Viscosity for fluid flow, ranging from 1 to 7, with
394 // 1 giving almost instantaneous propagation and 7 being
395 // the slowest possible
396 f.liquid_viscosity = getintfield_default(L, index,
397 "liquid_viscosity", f.liquid_viscosity);
398 f.liquid_range = getintfield_default(L, index,
399 "liquid_range", f.liquid_range);
400 f.leveled = getintfield_default(L, index, "leveled", f.leveled);
402 getboolfield(L, index, "liquid_renewable", f.liquid_renewable);
403 getstringfield(L, index, "freezemelt", f.freezemelt);
404 f.drowning = getintfield_default(L, index,
405 "drowning", f.drowning);
406 // Amount of light the node emits
407 f.light_source = getintfield_default(L, index,
408 "light_source", f.light_source);
409 f.damage_per_second = getintfield_default(L, index,
410 "damage_per_second", f.damage_per_second);
412 lua_getfield(L, index, "node_box");
413 if(lua_istable(L, -1))
414 f.node_box = read_nodebox(L, -1);
417 lua_getfield(L, index, "selection_box");
418 if(lua_istable(L, -1))
419 f.selection_box = read_nodebox(L, -1);
422 // Set to true if paramtype used to be 'facedir_simple'
423 getboolfield(L, index, "legacy_facedir_simple", f.legacy_facedir_simple);
424 // Set to true if wall_mounted used to be set to true
425 getboolfield(L, index, "legacy_wallmounted", f.legacy_wallmounted);
428 lua_getfield(L, index, "sounds");
429 if(lua_istable(L, -1)){
430 lua_getfield(L, -1, "footstep");
431 read_soundspec(L, -1, f.sound_footstep);
433 lua_getfield(L, -1, "dig");
434 read_soundspec(L, -1, f.sound_dig);
436 lua_getfield(L, -1, "dug");
437 read_soundspec(L, -1, f.sound_dug);
445 /******************************************************************************/
446 void read_server_sound_params(lua_State *L, int index,
447 ServerSoundParams ¶ms)
450 index = lua_gettop(L) + 1 + index;
452 params = ServerSoundParams();
453 if(lua_istable(L, index)){
454 getfloatfield(L, index, "gain", params.gain);
455 getstringfield(L, index, "to_player", params.to_player);
456 lua_getfield(L, index, "pos");
457 if(!lua_isnil(L, -1)){
458 v3f p = read_v3f(L, -1)*BS;
460 params.type = ServerSoundParams::SSP_POSITIONAL;
463 lua_getfield(L, index, "object");
464 if(!lua_isnil(L, -1)){
465 ObjectRef *ref = ObjectRef::checkobject(L, -1);
466 ServerActiveObject *sao = ObjectRef::getobject(ref);
468 params.object = sao->getId();
469 params.type = ServerSoundParams::SSP_OBJECT;
473 params.max_hear_distance = BS*getfloatfield_default(L, index,
474 "max_hear_distance", params.max_hear_distance/BS);
475 getboolfield(L, index, "loop", params.loop);
479 /******************************************************************************/
480 void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
483 index = lua_gettop(L) + 1 + index;
484 if(lua_isnil(L, index)){
485 } else if(lua_istable(L, index)){
486 getstringfield(L, index, "name", spec.name);
487 getfloatfield(L, index, "gain", spec.gain);
488 } else if(lua_isstring(L, index)){
489 spec.name = lua_tostring(L, index);
493 /******************************************************************************/
494 NodeBox read_nodebox(lua_State *L, int index)
497 if(lua_istable(L, -1)){
498 nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
499 ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
501 lua_getfield(L, index, "fixed");
502 if(lua_istable(L, -1))
503 nodebox.fixed = read_aabb3f_vector(L, -1, BS);
506 lua_getfield(L, index, "wall_top");
507 if(lua_istable(L, -1))
508 nodebox.wall_top = read_aabb3f(L, -1, BS);
511 lua_getfield(L, index, "wall_bottom");
512 if(lua_istable(L, -1))
513 nodebox.wall_bottom = read_aabb3f(L, -1, BS);
516 lua_getfield(L, index, "wall_side");
517 if(lua_istable(L, -1))
518 nodebox.wall_side = read_aabb3f(L, -1, BS);
524 /******************************************************************************/
525 MapNode readnode(lua_State *L, int index, INodeDefManager *ndef)
527 lua_getfield(L, index, "name");
528 const char *name = luaL_checkstring(L, -1);
531 lua_getfield(L, index, "param1");
535 param1 = lua_tonumber(L, -1);
538 lua_getfield(L, index, "param2");
542 param2 = lua_tonumber(L, -1);
544 return MapNode(ndef, name, param1, param2);
547 /******************************************************************************/
548 void pushnode(lua_State *L, const MapNode &n, INodeDefManager *ndef)
551 lua_pushstring(L, ndef->get(n).name.c_str());
552 lua_setfield(L, -2, "name");
553 lua_pushnumber(L, n.getParam1());
554 lua_setfield(L, -2, "param1");
555 lua_pushnumber(L, n.getParam2());
556 lua_setfield(L, -2, "param2");
559 /******************************************************************************/
560 void warn_if_field_exists(lua_State *L, int table,
561 const char *fieldname, const std::string &message)
563 lua_getfield(L, table, fieldname);
564 if(!lua_isnil(L, -1)){
565 //TODO find way to access backtrace fct from here
566 // infostream<<script_get_backtrace(L)<<std::endl;
567 infostream<<"WARNING: field \""<<fieldname<<"\": "
568 <<message<<std::endl;
573 /******************************************************************************/
574 int getenumfield(lua_State *L, int table,
575 const char *fieldname, const EnumString *spec, int default_)
577 int result = default_;
578 string_to_enum(spec, result,
579 getstringfield_default(L, table, fieldname, ""));
583 /******************************************************************************/
584 bool string_to_enum(const EnumString *spec, int &result,
585 const std::string &str)
587 const EnumString *esp = spec;
589 if(str == std::string(esp->str)){
598 /******************************************************************************/
599 ItemStack read_item(lua_State* L, int index,Server* srv)
602 index = lua_gettop(L) + 1 + index;
604 if(lua_isnil(L, index))
608 else if(lua_isuserdata(L, index))
610 // Convert from LuaItemStack
611 LuaItemStack *o = LuaItemStack::checkobject(L, index);
614 else if(lua_isstring(L, index))
616 // Convert from itemstring
617 std::string itemstring = lua_tostring(L, index);
618 IItemDefManager *idef = srv->idef();
622 item.deSerialize(itemstring, idef);
625 catch(SerializationError &e)
627 infostream<<"WARNING: unable to create item from itemstring"
628 <<": "<<itemstring<<std::endl;
632 else if(lua_istable(L, index))
634 // Convert from table
635 IItemDefManager *idef = srv->idef();
636 std::string name = getstringfield_default(L, index, "name", "");
637 int count = getintfield_default(L, index, "count", 1);
638 int wear = getintfield_default(L, index, "wear", 0);
639 std::string metadata = getstringfield_default(L, index, "metadata", "");
640 return ItemStack(name, count, wear, metadata, idef);
644 throw LuaError(L, "Expecting itemstack, itemstring, table or nil");
648 /******************************************************************************/
649 void push_tool_capabilities(lua_State *L,
650 const ToolCapabilities &toolcap)
653 setfloatfield(L, -1, "full_punch_interval", toolcap.full_punch_interval);
654 setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
655 // Create groupcaps table
658 for(std::map<std::string, ToolGroupCap>::const_iterator
659 i = toolcap.groupcaps.begin(); i != toolcap.groupcaps.end(); i++){
660 // Create groupcap table
662 const std::string &name = i->first;
663 const ToolGroupCap &groupcap = i->second;
664 // Create subtable "times"
666 for(std::map<int, float>::const_iterator
667 i = groupcap.times.begin(); i != groupcap.times.end(); i++){
668 int rating = i->first;
669 float time = i->second;
670 lua_pushinteger(L, rating);
671 lua_pushnumber(L, time);
674 // Set subtable "times"
675 lua_setfield(L, -2, "times");
676 // Set simple parameters
677 setintfield(L, -1, "maxlevel", groupcap.maxlevel);
678 setintfield(L, -1, "uses", groupcap.uses);
679 // Insert groupcap table into groupcaps table
680 lua_setfield(L, -2, name.c_str());
682 // Set groupcaps table
683 lua_setfield(L, -2, "groupcaps");
684 //Create damage_groups table
686 // For each damage group
687 for(std::map<std::string, s16>::const_iterator
688 i = toolcap.damageGroups.begin(); i != toolcap.damageGroups.end(); i++){
689 // Create damage group table
690 lua_pushinteger(L, i->second);
691 lua_setfield(L, -2, i->first.c_str());
693 lua_setfield(L, -2, "damage_groups");
696 /******************************************************************************/
697 void push_inventory_list(lua_State *L, Inventory *inv, const char *name)
699 InventoryList *invlist = inv->getList(name);
704 std::vector<ItemStack> items;
705 for(u32 i=0; i<invlist->getSize(); i++)
706 items.push_back(invlist->getItem(i));
707 push_items(L, items);
710 /******************************************************************************/
711 void read_inventory_list(lua_State *L, int tableindex,
712 Inventory *inv, const char *name, Server* srv, int forcesize)
715 tableindex = lua_gettop(L) + 1 + tableindex;
716 // If nil, delete list
717 if(lua_isnil(L, tableindex)){
718 inv->deleteList(name);
721 // Otherwise set list
722 std::vector<ItemStack> items = read_items(L, tableindex,srv);
723 int listsize = (forcesize != -1) ? forcesize : items.size();
724 InventoryList *invlist = inv->addList(name, listsize);
726 for(std::vector<ItemStack>::const_iterator
727 i = items.begin(); i != items.end(); i++){
728 if(forcesize != -1 && index == forcesize)
730 invlist->changeItem(index, *i);
733 while(forcesize != -1 && index < forcesize){
734 invlist->deleteItem(index);
739 /******************************************************************************/
740 ToolCapabilities read_tool_capabilities(
741 lua_State *L, int table)
743 ToolCapabilities toolcap;
744 getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
745 getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
746 lua_getfield(L, table, "groupcaps");
747 if(lua_istable(L, -1)){
748 int table_groupcaps = lua_gettop(L);
750 while(lua_next(L, table_groupcaps) != 0){
751 // key at index -2 and value at index -1
752 std::string groupname = luaL_checkstring(L, -2);
753 if(lua_istable(L, -1)){
754 int table_groupcap = lua_gettop(L);
755 // This will be created
756 ToolGroupCap groupcap;
757 // Read simple parameters
758 getintfield(L, table_groupcap, "maxlevel", groupcap.maxlevel);
759 getintfield(L, table_groupcap, "uses", groupcap.uses);
760 // DEPRECATED: maxwear
762 if(getfloatfield(L, table_groupcap, "maxwear", maxwear)){
764 groupcap.uses = 1.0/maxwear;
767 infostream<<script_get_backtrace(L)<<std::endl;
768 infostream<<"WARNING: field \"maxwear\" is deprecated; "
769 <<"should replace with uses=1/maxwear"<<std::endl;
771 // Read "times" table
772 lua_getfield(L, table_groupcap, "times");
773 if(lua_istable(L, -1)){
774 int table_times = lua_gettop(L);
776 while(lua_next(L, table_times) != 0){
777 // key at index -2 and value at index -1
778 int rating = luaL_checkinteger(L, -2);
779 float time = luaL_checknumber(L, -1);
780 groupcap.times[rating] = time;
781 // removes value, keeps key for next iteration
786 // Insert groupcap into toolcap
787 toolcap.groupcaps[groupname] = groupcap;
789 // removes value, keeps key for next iteration
795 lua_getfield(L, table, "damage_groups");
796 if(lua_istable(L, -1)){
797 int table_damage_groups = lua_gettop(L);
799 while(lua_next(L, table_damage_groups) != 0){
800 // key at index -2 and value at index -1
801 std::string groupname = luaL_checkstring(L, -2);
802 u16 value = luaL_checkinteger(L, -1);
803 toolcap.damageGroups[groupname] = value;
804 // removes value, keeps key for next iteration
812 /******************************************************************************/
813 void push_dig_params(lua_State *L,const DigParams ¶ms)
816 setboolfield(L, -1, "diggable", params.diggable);
817 setfloatfield(L, -1, "time", params.time);
818 setintfield(L, -1, "wear", params.wear);
821 /******************************************************************************/
822 void push_hit_params(lua_State *L,const HitParams ¶ms)
825 setintfield(L, -1, "hp", params.hp);
826 setintfield(L, -1, "wear", params.wear);
829 /******************************************************************************/
830 u32 getflagsfield(lua_State *L, int table,
831 const char *fieldname, FlagDesc *flagdesc) {
832 std::string flagstring;
834 flagstring = getstringfield_default(L, table, fieldname, "");
835 return readFlagString(flagstring, flagdesc);
838 /******************************************************************************/
839 /* Lua Stored data! */
840 /******************************************************************************/
842 /******************************************************************************/
843 void read_groups(lua_State *L, int index,
844 std::map<std::string, int> &result)
846 if (!lua_istable(L,index))
852 while(lua_next(L, index) != 0){
853 // key at index -2 and value at index -1
854 std::string name = luaL_checkstring(L, -2);
855 int rating = luaL_checkinteger(L, -1);
856 result[name] = rating;
857 // removes value, keeps key for next iteration
862 /******************************************************************************/
863 void push_items(lua_State *L, const std::vector<ItemStack> &items)
865 // Get the table insert function
866 lua_getglobal(L, "table");
867 lua_getfield(L, -1, "insert");
868 int table_insert = lua_gettop(L);
869 // Create and fill table
871 int table = lua_gettop(L);
872 for(u32 i=0; i<items.size(); i++){
873 ItemStack item = items[i];
874 lua_pushvalue(L, table_insert);
875 lua_pushvalue(L, table);
876 LuaItemStack::create(L, item);
877 if(lua_pcall(L, 2, 0, 0))
878 script_error(L, "error: %s", lua_tostring(L, -1));
880 lua_remove(L, -2); // Remove table
881 lua_remove(L, -2); // Remove insert
884 /******************************************************************************/
885 std::vector<ItemStack> read_items(lua_State *L, int index,Server* srv)
888 index = lua_gettop(L) + 1 + index;
890 std::vector<ItemStack> items;
891 luaL_checktype(L, index, LUA_TTABLE);
893 while(lua_next(L, index) != 0){
894 // key at index -2 and value at index -1
895 items.push_back(read_item(L, -1, srv));
896 // removes value, keeps key for next iteration
902 /******************************************************************************/
903 void luaentity_get(lua_State *L, u16 id)
905 // Get minetest.luaentities[i]
906 lua_getglobal(L, "minetest");
907 lua_getfield(L, -1, "luaentities");
908 luaL_checktype(L, -1, LUA_TTABLE);
909 lua_pushnumber(L, id);
911 lua_remove(L, -2); // luaentities
912 lua_remove(L, -2); // minetest
915 /******************************************************************************/
916 NoiseParams *read_noiseparams(lua_State *L, int index)
919 index = lua_gettop(L) + 1 + index;
921 if (!lua_istable(L, index))
924 NoiseParams *np = new NoiseParams;
926 np->offset = getfloatfield_default(L, index, "offset", 0.0);
927 np->scale = getfloatfield_default(L, index, "scale", 0.0);
928 lua_getfield(L, index, "spread");
929 np->spread = read_v3f(L, -1);
931 np->seed = getintfield_default(L, index, "seed", 0);
932 np->octaves = getintfield_default(L, index, "octaves", 0);
933 np->persist = getfloatfield_default(L, index, "persist", 0.0);
938 /******************************************************************************/
939 bool read_schematic(lua_State *L, int index, DecoSchematic *dschem, Server *server) {
941 index = lua_gettop(L) + 1 + index;
943 INodeDefManager *ndef = server->getNodeDefManager();
945 if (lua_istable(L, index)) {
946 lua_getfield(L, index, "size");
947 v3s16 size = read_v3s16(L, -1);
950 int numnodes = size.X * size.Y * size.Z;
951 MapNode *schemdata = new MapNode[numnodes];
954 lua_getfield(L, index, "data");
955 luaL_checktype(L, -1, LUA_TTABLE);
958 while (lua_next(L, -2)) {
960 // same as readnode, except param1 default is MTSCHEM_PROB_CONST
961 lua_getfield(L, -1, "name");
962 const char *name = luaL_checkstring(L, -1);
966 lua_getfield(L, -1, "param1");
967 param1 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : MTSCHEM_PROB_ALWAYS;
971 lua_getfield(L, -1, "param2");
972 param2 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : 0;
975 schemdata[i] = MapNode(ndef, name, param1, param2);
983 dschem->schematic = schemdata;
986 errorstream << "read_schematic: incorrect number of "
987 "nodes provided in raw schematic data (got " << i <<
988 ", expected " << numnodes << ")." << std::endl;
991 } else if (lua_isstring(L, index)) {
992 dschem->filename = std::string(lua_tostring(L, index));
994 errorstream << "read_schematic: missing schematic "
995 "filename or raw schematic data" << std::endl;