]> git.lizzy.rs Git - minetest.git/blob - src/script/common/c_content.cpp
Raycast: export exact pointing location (#6304)
[minetest.git] / src / script / common / c_content.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19 #include "common/c_content.h"
20 #include "common/c_converter.h"
21 #include "common/c_types.h"
22 #include "nodedef.h"
23 #include "object_properties.h"
24 #include "cpp_api/s_node.h"
25 #include "lua_api/l_object.h"
26 #include "lua_api/l_item.h"
27 #include "common/c_internal.h"
28 #include "server.h"
29 #include "log.h"
30 #include "tool.h"
31 #include "serverobject.h"
32 #include "porting.h"
33 #include "mapgen/mg_schematic.h"
34 #include "noise.h"
35 #include "util/pointedthing.h"
36 #include "debug.h" // For FATAL_ERROR
37 #include <json/json.h>
38
39 struct EnumString es_TileAnimationType[] =
40 {
41         {TAT_NONE, "none"},
42         {TAT_VERTICAL_FRAMES, "vertical_frames"},
43         {TAT_SHEET_2D, "sheet_2d"},
44         {0, NULL},
45 };
46
47 /******************************************************************************/
48 void read_item_definition(lua_State* L, int index,
49                 const ItemDefinition &default_def, ItemDefinition &def)
50 {
51         if (index < 0)
52                 index = lua_gettop(L) + 1 + index;
53
54         def.type = (ItemType)getenumfield(L, index, "type",
55                         es_ItemType, ITEM_NONE);
56         getstringfield(L, index, "name", def.name);
57         getstringfield(L, index, "description", def.description);
58         getstringfield(L, index, "inventory_image", def.inventory_image);
59         getstringfield(L, index, "inventory_overlay", def.inventory_overlay);
60         getstringfield(L, index, "wield_image", def.wield_image);
61         getstringfield(L, index, "wield_overlay", def.wield_overlay);
62         getstringfield(L, index, "palette", def.palette_image);
63
64         // Read item color.
65         lua_getfield(L, index, "color");
66         read_color(L, -1, &def.color);
67         lua_pop(L, 1);
68
69         lua_getfield(L, index, "wield_scale");
70         if(lua_istable(L, -1)){
71                 def.wield_scale = check_v3f(L, -1);
72         }
73         lua_pop(L, 1);
74
75         int stack_max = getintfield_default(L, index, "stack_max", def.stack_max);
76         def.stack_max = rangelim(stack_max, 1, U16_MAX);
77
78         lua_getfield(L, index, "on_use");
79         def.usable = lua_isfunction(L, -1);
80         lua_pop(L, 1);
81
82         getboolfield(L, index, "liquids_pointable", def.liquids_pointable);
83
84         warn_if_field_exists(L, index, "tool_digging_properties",
85                         "Deprecated; use tool_capabilities");
86
87         lua_getfield(L, index, "tool_capabilities");
88         if(lua_istable(L, -1)){
89                 def.tool_capabilities = new ToolCapabilities(
90                                 read_tool_capabilities(L, -1));
91         }
92
93         // If name is "" (hand), ensure there are ToolCapabilities
94         // because it will be looked up there whenever any other item has
95         // no ToolCapabilities
96         if (def.name.empty() && def.tool_capabilities == NULL){
97                 def.tool_capabilities = new ToolCapabilities();
98         }
99
100         lua_getfield(L, index, "groups");
101         read_groups(L, -1, def.groups);
102         lua_pop(L, 1);
103
104         lua_getfield(L, index, "sounds");
105         if(lua_istable(L, -1)){
106                 lua_getfield(L, -1, "place");
107                 read_soundspec(L, -1, def.sound_place);
108                 lua_pop(L, 1);
109                 lua_getfield(L, -1, "place_failed");
110                 read_soundspec(L, -1, def.sound_place_failed);
111                 lua_pop(L, 1);
112         }
113         lua_pop(L, 1);
114
115         def.range = getfloatfield_default(L, index, "range", def.range);
116
117         // Client shall immediately place this node when player places the item.
118         // Server will update the precise end result a moment later.
119         // "" = no prediction
120         getstringfield(L, index, "node_placement_prediction",
121                         def.node_placement_prediction);
122 }
123
124 /******************************************************************************/
125 void push_item_definition(lua_State *L, const ItemDefinition &i)
126 {
127         lua_newtable(L);
128         lua_pushstring(L, i.name.c_str());
129         lua_setfield(L, -2, "name");
130         lua_pushstring(L, i.description.c_str());
131         lua_setfield(L, -2, "description");
132 }
133
134 void push_item_definition_full(lua_State *L, const ItemDefinition &i)
135 {
136         std::string type(es_ItemType[(int)i.type].str);
137
138         lua_newtable(L);
139         lua_pushstring(L, i.name.c_str());
140         lua_setfield(L, -2, "name");
141         lua_pushstring(L, i.description.c_str());
142         lua_setfield(L, -2, "description");
143         lua_pushstring(L, type.c_str());
144         lua_setfield(L, -2, "type");
145         lua_pushstring(L, i.inventory_image.c_str());
146         lua_setfield(L, -2, "inventory_image");
147         lua_pushstring(L, i.inventory_overlay.c_str());
148         lua_setfield(L, -2, "inventory_overlay");
149         lua_pushstring(L, i.wield_image.c_str());
150         lua_setfield(L, -2, "wield_image");
151         lua_pushstring(L, i.wield_overlay.c_str());
152         lua_setfield(L, -2, "wield_overlay");
153         lua_pushstring(L, i.palette_image.c_str());
154         lua_setfield(L, -2, "palette_image");
155         push_ARGB8(L, i.color);
156         lua_setfield(L, -2, "color");
157         push_v3f(L, i.wield_scale);
158         lua_setfield(L, -2, "wield_scale");
159         lua_pushinteger(L, i.stack_max);
160         lua_setfield(L, -2, "stack_max");
161         lua_pushboolean(L, i.usable);
162         lua_setfield(L, -2, "usable");
163         lua_pushboolean(L, i.liquids_pointable);
164         lua_setfield(L, -2, "liquids_pointable");
165         if (i.type == ITEM_TOOL) {
166                 push_tool_capabilities(L, ToolCapabilities(
167                         i.tool_capabilities->full_punch_interval,
168                         i.tool_capabilities->max_drop_level,
169                         i.tool_capabilities->groupcaps,
170                         i.tool_capabilities->damageGroups));
171                 lua_setfield(L, -2, "tool_capabilities");
172         }
173         push_groups(L, i.groups);
174         lua_setfield(L, -2, "groups");
175         push_soundspec(L, i.sound_place);
176         lua_setfield(L, -2, "sound_place");
177         push_soundspec(L, i.sound_place_failed);
178         lua_setfield(L, -2, "sound_place_failed");
179         lua_pushstring(L, i.node_placement_prediction.c_str());
180         lua_setfield(L, -2, "node_placement_prediction");
181 }
182
183 /******************************************************************************/
184 void read_object_properties(lua_State *L, int index,
185                 ObjectProperties *prop, IItemDefManager *idef)
186 {
187         if(index < 0)
188                 index = lua_gettop(L) + 1 + index;
189         if(!lua_istable(L, index))
190                 return;
191
192         int hp_max = 0;
193         if (getintfield(L, -1, "hp_max", hp_max))
194                 prop->hp_max = (s16)rangelim(hp_max, 0, S16_MAX);
195
196         getintfield(L, -1, "breath_max", prop->breath_max);
197         getboolfield(L, -1, "physical", prop->physical);
198         getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
199
200         getfloatfield(L, -1, "weight", prop->weight);
201
202         lua_getfield(L, -1, "collisionbox");
203         bool collisionbox_defined = lua_istable(L, -1);
204         if (collisionbox_defined)
205                 prop->collisionbox = read_aabb3f(L, -1, 1.0);
206         lua_pop(L, 1);
207
208         lua_getfield(L, -1, "selectionbox");
209         if (lua_istable(L, -1))
210                 prop->selectionbox = read_aabb3f(L, -1, 1.0);
211         else if (collisionbox_defined)
212                 prop->selectionbox = prop->collisionbox;
213         lua_pop(L, 1);
214
215         getboolfield(L, -1, "pointable", prop->pointable);
216         getstringfield(L, -1, "visual", prop->visual);
217
218         getstringfield(L, -1, "mesh", prop->mesh);
219
220         lua_getfield(L, -1, "visual_size");
221         if(lua_istable(L, -1))
222                 prop->visual_size = read_v2f(L, -1);
223         lua_pop(L, 1);
224
225         lua_getfield(L, -1, "textures");
226         if(lua_istable(L, -1)){
227                 prop->textures.clear();
228                 int table = lua_gettop(L);
229                 lua_pushnil(L);
230                 while(lua_next(L, table) != 0){
231                         // key at index -2 and value at index -1
232                         if(lua_isstring(L, -1))
233                                 prop->textures.emplace_back(lua_tostring(L, -1));
234                         else
235                                 prop->textures.emplace_back("");
236                         // removes value, keeps key for next iteration
237                         lua_pop(L, 1);
238                 }
239         }
240         lua_pop(L, 1);
241
242         lua_getfield(L, -1, "colors");
243         if (lua_istable(L, -1)) {
244                 int table = lua_gettop(L);
245                 prop->colors.clear();
246                 for (lua_pushnil(L); lua_next(L, table); lua_pop(L, 1)) {
247                         video::SColor color(255, 255, 255, 255);
248                         read_color(L, -1, &color);
249                         prop->colors.push_back(color);
250                 }
251         }
252         lua_pop(L, 1);
253
254         lua_getfield(L, -1, "spritediv");
255         if(lua_istable(L, -1))
256                 prop->spritediv = read_v2s16(L, -1);
257         lua_pop(L, 1);
258
259         lua_getfield(L, -1, "initial_sprite_basepos");
260         if(lua_istable(L, -1))
261                 prop->initial_sprite_basepos = read_v2s16(L, -1);
262         lua_pop(L, 1);
263
264         getboolfield(L, -1, "is_visible", prop->is_visible);
265         getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
266         if (getfloatfield(L, -1, "stepheight", prop->stepheight))
267                 prop->stepheight *= BS;
268         getfloatfield(L, -1, "eye_height", prop->eye_height);
269
270         getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
271         lua_getfield(L, -1, "automatic_face_movement_dir");
272         if (lua_isnumber(L, -1)) {
273                 prop->automatic_face_movement_dir = true;
274                 prop->automatic_face_movement_dir_offset = luaL_checknumber(L, -1);
275         } else if (lua_isboolean(L, -1)) {
276                 prop->automatic_face_movement_dir = lua_toboolean(L, -1);
277                 prop->automatic_face_movement_dir_offset = 0.0;
278         }
279         lua_pop(L, 1);
280         getboolfield(L, -1, "backface_culling", prop->backface_culling);
281         getintfield(L, -1, "glow", prop->glow);
282
283         getstringfield(L, -1, "nametag", prop->nametag);
284         lua_getfield(L, -1, "nametag_color");
285         if (!lua_isnil(L, -1)) {
286                 video::SColor color = prop->nametag_color;
287                 if (read_color(L, -1, &color))
288                         prop->nametag_color = color;
289         }
290         lua_pop(L, 1);
291
292         lua_getfield(L, -1, "automatic_face_movement_max_rotation_per_sec");
293         if (lua_isnumber(L, -1)) {
294                 prop->automatic_face_movement_max_rotation_per_sec = luaL_checknumber(L, -1);
295         }
296         lua_pop(L, 1);
297
298         getstringfield(L, -1, "infotext", prop->infotext);
299         getboolfield(L, -1, "static_save", prop->static_save);
300
301         lua_getfield(L, -1, "wield_item");
302         if (!lua_isnil(L, -1))
303                 prop->wield_item = read_item(L, -1, idef).getItemString();
304         lua_pop(L, 1);
305
306         getfloatfield(L, -1, "zoom_fov", prop->zoom_fov);
307         getboolfield(L, -1, "use_texture_alpha", prop->use_texture_alpha);
308 }
309
310 /******************************************************************************/
311 void push_object_properties(lua_State *L, ObjectProperties *prop)
312 {
313         lua_newtable(L);
314         lua_pushnumber(L, prop->hp_max);
315         lua_setfield(L, -2, "hp_max");
316         lua_pushnumber(L, prop->breath_max);
317         lua_setfield(L, -2, "breath_max");
318         lua_pushboolean(L, prop->physical);
319         lua_setfield(L, -2, "physical");
320         lua_pushboolean(L, prop->collideWithObjects);
321         lua_setfield(L, -2, "collide_with_objects");
322         lua_pushnumber(L, prop->weight);
323         lua_setfield(L, -2, "weight");
324         push_aabb3f(L, prop->collisionbox);
325         lua_setfield(L, -2, "collisionbox");
326         push_aabb3f(L, prop->selectionbox);
327         lua_setfield(L, -2, "selectionbox");
328         lua_pushboolean(L, prop->pointable);
329         lua_setfield(L, -2, "pointable");
330         lua_pushlstring(L, prop->visual.c_str(), prop->visual.size());
331         lua_setfield(L, -2, "visual");
332         lua_pushlstring(L, prop->mesh.c_str(), prop->mesh.size());
333         lua_setfield(L, -2, "mesh");
334         push_v2f(L, prop->visual_size);
335         lua_setfield(L, -2, "visual_size");
336
337         lua_newtable(L);
338         u16 i = 1;
339         for (const std::string &texture : prop->textures) {
340                 lua_pushlstring(L, texture.c_str(), texture.size());
341                 lua_rawseti(L, -2, i);
342         }
343         lua_setfield(L, -2, "textures");
344
345         lua_newtable(L);
346         i = 1;
347         for (const video::SColor &color : prop->colors) {
348                 push_ARGB8(L, color);
349                 lua_rawseti(L, -2, i);
350         }
351         lua_setfield(L, -2, "colors");
352
353         push_v2s16(L, prop->spritediv);
354         lua_setfield(L, -2, "spritediv");
355         push_v2s16(L, prop->initial_sprite_basepos);
356         lua_setfield(L, -2, "initial_sprite_basepos");
357         lua_pushboolean(L, prop->is_visible);
358         lua_setfield(L, -2, "is_visible");
359         lua_pushboolean(L, prop->makes_footstep_sound);
360         lua_setfield(L, -2, "makes_footstep_sound");
361         lua_pushnumber(L, prop->stepheight / BS);
362         lua_setfield(L, -2, "stepheight");
363         lua_pushnumber(L, prop->eye_height);
364         lua_setfield(L, -2, "eye_height");
365         lua_pushnumber(L, prop->automatic_rotate);
366         lua_setfield(L, -2, "automatic_rotate");
367         if (prop->automatic_face_movement_dir)
368                 lua_pushnumber(L, prop->automatic_face_movement_dir_offset);
369         else
370                 lua_pushboolean(L, false);
371         lua_setfield(L, -2, "automatic_face_movement_dir");
372         lua_pushboolean(L, prop->backface_culling);
373         lua_setfield(L, -2, "backface_culling");
374         lua_pushnumber(L, prop->glow);
375         lua_setfield(L, -2, "glow");
376         lua_pushlstring(L, prop->nametag.c_str(), prop->nametag.size());
377         lua_setfield(L, -2, "nametag");
378         push_ARGB8(L, prop->nametag_color);
379         lua_setfield(L, -2, "nametag_color");
380         lua_pushnumber(L, prop->automatic_face_movement_max_rotation_per_sec);
381         lua_setfield(L, -2, "automatic_face_movement_max_rotation_per_sec");
382         lua_pushlstring(L, prop->infotext.c_str(), prop->infotext.size());
383         lua_setfield(L, -2, "infotext");
384         lua_pushboolean(L, prop->static_save);
385         lua_setfield(L, -2, "static_save");
386         lua_pushlstring(L, prop->wield_item.c_str(), prop->wield_item.size());
387         lua_setfield(L, -2, "wield_item");
388         lua_pushnumber(L, prop->zoom_fov);
389         lua_setfield(L, -2, "zoom_fov");
390         lua_pushboolean(L, prop->use_texture_alpha);
391         lua_setfield(L, -2, "use_texture_alpha");
392 }
393
394 /******************************************************************************/
395 TileDef read_tiledef(lua_State *L, int index, u8 drawtype)
396 {
397         if(index < 0)
398                 index = lua_gettop(L) + 1 + index;
399
400         TileDef tiledef;
401
402         bool default_tiling = true;
403         bool default_culling = true;
404         switch (drawtype) {
405                 case NDT_PLANTLIKE:
406                 case NDT_PLANTLIKE_ROOTED:
407                 case NDT_FIRELIKE:
408                         default_tiling = false;
409                         // "break" is omitted here intentionaly, as PLANTLIKE
410                         // FIRELIKE drawtype both should default to having
411                         // backface_culling to false.
412                 case NDT_MESH:
413                 case NDT_LIQUID:
414                         default_culling = false;
415                         break;
416                 default:
417                         break;
418         }
419
420         // key at index -2 and value at index
421         if(lua_isstring(L, index)){
422                 // "default_lava.png"
423                 tiledef.name = lua_tostring(L, index);
424                 tiledef.tileable_vertical = default_tiling;
425                 tiledef.tileable_horizontal = default_tiling;
426                 tiledef.backface_culling = default_culling;
427         }
428         else if(lua_istable(L, index))
429         {
430                 // name="default_lava.png"
431                 tiledef.name = "";
432                 getstringfield(L, index, "name", tiledef.name);
433                 getstringfield(L, index, "image", tiledef.name); // MaterialSpec compat.
434                 tiledef.backface_culling = getboolfield_default(
435                         L, index, "backface_culling", default_culling);
436                 tiledef.tileable_horizontal = getboolfield_default(
437                         L, index, "tileable_horizontal", default_tiling);
438                 tiledef.tileable_vertical = getboolfield_default(
439                         L, index, "tileable_vertical", default_tiling);
440                 std::string align_style;
441                 if (getstringfield(L, index, "align_style", align_style)) {
442                         if (align_style == "user")
443                                 tiledef.align_style = ALIGN_STYLE_USER_DEFINED;
444                         else if (align_style == "world")
445                                 tiledef.align_style = ALIGN_STYLE_WORLD;
446                         else
447                                 tiledef.align_style = ALIGN_STYLE_NODE;
448                 }
449                 tiledef.scale = getintfield_default(L, index, "scale", 0);
450                 // color = ...
451                 lua_getfield(L, index, "color");
452                 tiledef.has_color = read_color(L, -1, &tiledef.color);
453                 lua_pop(L, 1);
454                 // animation = {}
455                 lua_getfield(L, index, "animation");
456                 tiledef.animation = read_animation_definition(L, -1);
457                 lua_pop(L, 1);
458         }
459
460         return tiledef;
461 }
462
463 /******************************************************************************/
464 ContentFeatures read_content_features(lua_State *L, int index)
465 {
466         if(index < 0)
467                 index = lua_gettop(L) + 1 + index;
468
469         ContentFeatures f;
470
471         /* Cache existence of some callbacks */
472         lua_getfield(L, index, "on_construct");
473         if(!lua_isnil(L, -1)) f.has_on_construct = true;
474         lua_pop(L, 1);
475         lua_getfield(L, index, "on_destruct");
476         if(!lua_isnil(L, -1)) f.has_on_destruct = true;
477         lua_pop(L, 1);
478         lua_getfield(L, index, "after_destruct");
479         if(!lua_isnil(L, -1)) f.has_after_destruct = true;
480         lua_pop(L, 1);
481
482         lua_getfield(L, index, "on_rightclick");
483         f.rightclickable = lua_isfunction(L, -1);
484         lua_pop(L, 1);
485
486         /* Name */
487         getstringfield(L, index, "name", f.name);
488
489         /* Groups */
490         lua_getfield(L, index, "groups");
491         read_groups(L, -1, f.groups);
492         lua_pop(L, 1);
493
494         /* Visual definition */
495
496         f.drawtype = (NodeDrawType)getenumfield(L, index, "drawtype",
497                         ScriptApiNode::es_DrawType,NDT_NORMAL);
498         getfloatfield(L, index, "visual_scale", f.visual_scale);
499
500         /* Meshnode model filename */
501         getstringfield(L, index, "mesh", f.mesh);
502
503         // tiles = {}
504         lua_getfield(L, index, "tiles");
505         // If nil, try the deprecated name "tile_images" instead
506         if(lua_isnil(L, -1)){
507                 lua_pop(L, 1);
508                 warn_if_field_exists(L, index, "tile_images",
509                                 "Deprecated; new name is \"tiles\".");
510                 lua_getfield(L, index, "tile_images");
511         }
512         if(lua_istable(L, -1)){
513                 int table = lua_gettop(L);
514                 lua_pushnil(L);
515                 int i = 0;
516                 while(lua_next(L, table) != 0){
517                         // Read tiledef from value
518                         f.tiledef[i] = read_tiledef(L, -1, f.drawtype);
519                         // removes value, keeps key for next iteration
520                         lua_pop(L, 1);
521                         i++;
522                         if(i==6){
523                                 lua_pop(L, 1);
524                                 break;
525                         }
526                 }
527                 // Copy last value to all remaining textures
528                 if(i >= 1){
529                         TileDef lasttile = f.tiledef[i-1];
530                         while(i < 6){
531                                 f.tiledef[i] = lasttile;
532                                 i++;
533                         }
534                 }
535         }
536         lua_pop(L, 1);
537
538         // overlay_tiles = {}
539         lua_getfield(L, index, "overlay_tiles");
540         if (lua_istable(L, -1)) {
541                 int table = lua_gettop(L);
542                 lua_pushnil(L);
543                 int i = 0;
544                 while (lua_next(L, table) != 0) {
545                         // Read tiledef from value
546                         f.tiledef_overlay[i] = read_tiledef(L, -1, f.drawtype);
547                         // removes value, keeps key for next iteration
548                         lua_pop(L, 1);
549                         i++;
550                         if (i == 6) {
551                                 lua_pop(L, 1);
552                                 break;
553                         }
554                 }
555                 // Copy last value to all remaining textures
556                 if (i >= 1) {
557                         TileDef lasttile = f.tiledef_overlay[i - 1];
558                         while (i < 6) {
559                                 f.tiledef_overlay[i] = lasttile;
560                                 i++;
561                         }
562                 }
563         }
564         lua_pop(L, 1);
565
566         // special_tiles = {}
567         lua_getfield(L, index, "special_tiles");
568         // If nil, try the deprecated name "special_materials" instead
569         if(lua_isnil(L, -1)){
570                 lua_pop(L, 1);
571                 warn_if_field_exists(L, index, "special_materials",
572                                 "Deprecated; new name is \"special_tiles\".");
573                 lua_getfield(L, index, "special_materials");
574         }
575         if(lua_istable(L, -1)){
576                 int table = lua_gettop(L);
577                 lua_pushnil(L);
578                 int i = 0;
579                 while(lua_next(L, table) != 0){
580                         // Read tiledef from value
581                         f.tiledef_special[i] = read_tiledef(L, -1, f.drawtype);
582                         // removes value, keeps key for next iteration
583                         lua_pop(L, 1);
584                         i++;
585                         if(i==CF_SPECIAL_COUNT){
586                                 lua_pop(L, 1);
587                                 break;
588                         }
589                 }
590         }
591         lua_pop(L, 1);
592
593         f.alpha = getintfield_default(L, index, "alpha", 255);
594
595         bool usealpha = getboolfield_default(L, index,
596                         "use_texture_alpha", false);
597         if (usealpha)
598                 f.alpha = 0;
599
600         // Read node color.
601         lua_getfield(L, index, "color");
602         read_color(L, -1, &f.color);
603         lua_pop(L, 1);
604
605         getstringfield(L, index, "palette", f.palette_name);
606
607         /* Other stuff */
608
609         lua_getfield(L, index, "post_effect_color");
610         read_color(L, -1, &f.post_effect_color);
611         lua_pop(L, 1);
612
613         f.param_type = (ContentParamType)getenumfield(L, index, "paramtype",
614                         ScriptApiNode::es_ContentParamType, CPT_NONE);
615         f.param_type_2 = (ContentParamType2)getenumfield(L, index, "paramtype2",
616                         ScriptApiNode::es_ContentParamType2, CPT2_NONE);
617
618         if (!f.palette_name.empty() &&
619                         !(f.param_type_2 == CPT2_COLOR ||
620                         f.param_type_2 == CPT2_COLORED_FACEDIR ||
621                         f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
622                 warningstream << "Node " << f.name.c_str()
623                         << " has a palette, but not a suitable paramtype2." << std::endl;
624
625         // Warn about some deprecated fields
626         warn_if_field_exists(L, index, "wall_mounted",
627                         "Deprecated; use paramtype2 = 'wallmounted'");
628         warn_if_field_exists(L, index, "light_propagates",
629                         "Deprecated; determined from paramtype");
630         warn_if_field_exists(L, index, "dug_item",
631                         "Deprecated; use 'drop' field");
632         warn_if_field_exists(L, index, "extra_dug_item",
633                         "Deprecated; use 'drop' field");
634         warn_if_field_exists(L, index, "extra_dug_item_rarity",
635                         "Deprecated; use 'drop' field");
636         warn_if_field_exists(L, index, "metadata_name",
637                         "Deprecated; use on_add and metadata callbacks");
638
639         // True for all ground-like things like stone and mud, false for eg. trees
640         getboolfield(L, index, "is_ground_content", f.is_ground_content);
641         f.light_propagates = (f.param_type == CPT_LIGHT);
642         getboolfield(L, index, "sunlight_propagates", f.sunlight_propagates);
643         // This is used for collision detection.
644         // Also for general solidness queries.
645         getboolfield(L, index, "walkable", f.walkable);
646         // Player can point to these
647         getboolfield(L, index, "pointable", f.pointable);
648         // Player can dig these
649         getboolfield(L, index, "diggable", f.diggable);
650         // Player can climb these
651         getboolfield(L, index, "climbable", f.climbable);
652         // Player can build on these
653         getboolfield(L, index, "buildable_to", f.buildable_to);
654         // Liquids flow into and replace node
655         getboolfield(L, index, "floodable", f.floodable);
656         // Whether the node is non-liquid, source liquid or flowing liquid
657         f.liquid_type = (LiquidType)getenumfield(L, index, "liquidtype",
658                         ScriptApiNode::es_LiquidType, LIQUID_NONE);
659         // If the content is liquid, this is the flowing version of the liquid.
660         getstringfield(L, index, "liquid_alternative_flowing",
661                         f.liquid_alternative_flowing);
662         // If the content is liquid, this is the source version of the liquid.
663         getstringfield(L, index, "liquid_alternative_source",
664                         f.liquid_alternative_source);
665         // Viscosity for fluid flow, ranging from 1 to 7, with
666         // 1 giving almost instantaneous propagation and 7 being
667         // the slowest possible
668         f.liquid_viscosity = getintfield_default(L, index,
669                         "liquid_viscosity", f.liquid_viscosity);
670         f.liquid_range = getintfield_default(L, index,
671                         "liquid_range", f.liquid_range);
672         f.leveled = getintfield_default(L, index, "leveled", f.leveled);
673
674         getboolfield(L, index, "liquid_renewable", f.liquid_renewable);
675         f.drowning = getintfield_default(L, index,
676                         "drowning", f.drowning);
677         // Amount of light the node emits
678         f.light_source = getintfield_default(L, index,
679                         "light_source", f.light_source);
680         if (f.light_source > LIGHT_MAX) {
681                 warningstream << "Node " << f.name.c_str()
682                         << " had greater light_source than " << LIGHT_MAX
683                         << ", it was reduced." << std::endl;
684                 f.light_source = LIGHT_MAX;
685         }
686         f.damage_per_second = getintfield_default(L, index,
687                         "damage_per_second", f.damage_per_second);
688
689         lua_getfield(L, index, "node_box");
690         if(lua_istable(L, -1))
691                 f.node_box = read_nodebox(L, -1);
692         lua_pop(L, 1);
693
694         lua_getfield(L, index, "connects_to");
695         if (lua_istable(L, -1)) {
696                 int table = lua_gettop(L);
697                 lua_pushnil(L);
698                 while (lua_next(L, table) != 0) {
699                         // Value at -1
700                         f.connects_to.emplace_back(lua_tostring(L, -1));
701                         lua_pop(L, 1);
702                 }
703         }
704         lua_pop(L, 1);
705
706         lua_getfield(L, index, "connect_sides");
707         if (lua_istable(L, -1)) {
708                 int table = lua_gettop(L);
709                 lua_pushnil(L);
710                 while (lua_next(L, table) != 0) {
711                         // Value at -1
712                         std::string side(lua_tostring(L, -1));
713                         // Note faces are flipped to make checking easier
714                         if (side == "top")
715                                 f.connect_sides |= 2;
716                         else if (side == "bottom")
717                                 f.connect_sides |= 1;
718                         else if (side == "front")
719                                 f.connect_sides |= 16;
720                         else if (side == "left")
721                                 f.connect_sides |= 32;
722                         else if (side == "back")
723                                 f.connect_sides |= 4;
724                         else if (side == "right")
725                                 f.connect_sides |= 8;
726                         else
727                                 warningstream << "Unknown value for \"connect_sides\": "
728                                         << side << std::endl;
729                         lua_pop(L, 1);
730                 }
731         }
732         lua_pop(L, 1);
733
734         lua_getfield(L, index, "selection_box");
735         if(lua_istable(L, -1))
736                 f.selection_box = read_nodebox(L, -1);
737         lua_pop(L, 1);
738
739         lua_getfield(L, index, "collision_box");
740         if(lua_istable(L, -1))
741                 f.collision_box = read_nodebox(L, -1);
742         lua_pop(L, 1);
743
744         f.waving = getintfield_default(L, index,
745                         "waving", f.waving);
746
747         // Set to true if paramtype used to be 'facedir_simple'
748         getboolfield(L, index, "legacy_facedir_simple", f.legacy_facedir_simple);
749         // Set to true if wall_mounted used to be set to true
750         getboolfield(L, index, "legacy_wallmounted", f.legacy_wallmounted);
751
752         // Sound table
753         lua_getfield(L, index, "sounds");
754         if(lua_istable(L, -1)){
755                 lua_getfield(L, -1, "footstep");
756                 read_soundspec(L, -1, f.sound_footstep);
757                 lua_pop(L, 1);
758                 lua_getfield(L, -1, "dig");
759                 read_soundspec(L, -1, f.sound_dig);
760                 lua_pop(L, 1);
761                 lua_getfield(L, -1, "dug");
762                 read_soundspec(L, -1, f.sound_dug);
763                 lua_pop(L, 1);
764         }
765         lua_pop(L, 1);
766
767         // Node immediately placed by client when node is dug
768         getstringfield(L, index, "node_dig_prediction",
769                 f.node_dig_prediction);
770
771         return f;
772 }
773
774 void push_content_features(lua_State *L, const ContentFeatures &c)
775 {
776         std::string paramtype(ScriptApiNode::es_ContentParamType[(int)c.param_type].str);
777         std::string paramtype2(ScriptApiNode::es_ContentParamType2[(int)c.param_type_2].str);
778         std::string drawtype(ScriptApiNode::es_DrawType[(int)c.drawtype].str);
779         std::string liquid_type(ScriptApiNode::es_LiquidType[(int)c.liquid_type].str);
780
781         /* Missing "tiles" because I don't see a usecase (at least not yet). */
782
783         lua_newtable(L);
784         lua_pushboolean(L, c.has_on_construct);
785         lua_setfield(L, -2, "has_on_construct");
786         lua_pushboolean(L, c.has_on_destruct);
787         lua_setfield(L, -2, "has_on_destruct");
788         lua_pushboolean(L, c.has_after_destruct);
789         lua_setfield(L, -2, "has_after_destruct");
790         lua_pushstring(L, c.name.c_str());
791         lua_setfield(L, -2, "name");
792         push_groups(L, c.groups);
793         lua_setfield(L, -2, "groups");
794         lua_pushstring(L, paramtype.c_str());
795         lua_setfield(L, -2, "paramtype");
796         lua_pushstring(L, paramtype2.c_str());
797         lua_setfield(L, -2, "paramtype2");
798         lua_pushstring(L, drawtype.c_str());
799         lua_setfield(L, -2, "drawtype");
800         if (!c.mesh.empty()) {
801                 lua_pushstring(L, c.mesh.c_str());
802                 lua_setfield(L, -2, "mesh");
803         }
804 #ifndef SERVER
805         push_ARGB8(L, c.minimap_color);       // I know this is not set-able w/ register_node,
806         lua_setfield(L, -2, "minimap_color"); // but the people need to know!
807 #endif
808         lua_pushnumber(L, c.visual_scale);
809         lua_setfield(L, -2, "visual_scale");
810         lua_pushnumber(L, c.alpha);
811         lua_setfield(L, -2, "alpha");
812         if (!c.palette_name.empty()) {
813                 push_ARGB8(L, c.color);
814                 lua_setfield(L, -2, "color");
815
816                 lua_pushstring(L, c.palette_name.c_str());
817                 lua_setfield(L, -2, "palette_name");
818
819                 push_palette(L, c.palette);
820                 lua_setfield(L, -2, "palette");
821         }
822         lua_pushnumber(L, c.waving);
823         lua_setfield(L, -2, "waving");
824         lua_pushnumber(L, c.connect_sides);
825         lua_setfield(L, -2, "connect_sides");
826
827         lua_newtable(L);
828         u16 i = 1;
829         for (const std::string &it : c.connects_to) {
830                 lua_pushlstring(L, it.c_str(), it.size());
831                 lua_rawseti(L, -2, i);
832         }
833         lua_setfield(L, -2, "connects_to");
834
835         push_ARGB8(L, c.post_effect_color);
836         lua_setfield(L, -2, "post_effect_color");
837         lua_pushnumber(L, c.leveled);
838         lua_setfield(L, -2, "leveled");
839         lua_pushboolean(L, c.sunlight_propagates);
840         lua_setfield(L, -2, "sunlight_propagates");
841         lua_pushnumber(L, c.light_source);
842         lua_setfield(L, -2, "light_source");
843         lua_pushboolean(L, c.is_ground_content);
844         lua_setfield(L, -2, "is_ground_content");
845         lua_pushboolean(L, c.walkable);
846         lua_setfield(L, -2, "walkable");
847         lua_pushboolean(L, c.pointable);
848         lua_setfield(L, -2, "pointable");
849         lua_pushboolean(L, c.diggable);
850         lua_setfield(L, -2, "diggable");
851         lua_pushboolean(L, c.climbable);
852         lua_setfield(L, -2, "climbable");
853         lua_pushboolean(L, c.buildable_to);
854         lua_setfield(L, -2, "buildable_to");
855         lua_pushboolean(L, c.rightclickable);
856         lua_setfield(L, -2, "rightclickable");
857         lua_pushnumber(L, c.damage_per_second);
858         lua_setfield(L, -2, "damage_per_second");
859         if (c.isLiquid()) {
860                 lua_pushstring(L, liquid_type.c_str());
861                 lua_setfield(L, -2, "liquid_type");
862                 lua_pushstring(L, c.liquid_alternative_flowing.c_str());
863                 lua_setfield(L, -2, "liquid_alternative_flowing");
864                 lua_pushstring(L, c.liquid_alternative_source.c_str());
865                 lua_setfield(L, -2, "liquid_alternative_source");
866                 lua_pushnumber(L, c.liquid_viscosity);
867                 lua_setfield(L, -2, "liquid_viscosity");
868                 lua_pushboolean(L, c.liquid_renewable);
869                 lua_setfield(L, -2, "liquid_renewable");
870                 lua_pushnumber(L, c.liquid_range);
871                 lua_setfield(L, -2, "liquid_range");
872         }
873         lua_pushnumber(L, c.drowning);
874         lua_setfield(L, -2, "drowning");
875         lua_pushboolean(L, c.floodable);
876         lua_setfield(L, -2, "floodable");
877         push_nodebox(L, c.node_box);
878         lua_setfield(L, -2, "node_box");
879         push_nodebox(L, c.selection_box);
880         lua_setfield(L, -2, "selection_box");
881         push_nodebox(L, c.collision_box);
882         lua_setfield(L, -2, "collision_box");
883         lua_newtable(L);
884         push_soundspec(L, c.sound_footstep);
885         lua_setfield(L, -2, "sound_footstep");
886         push_soundspec(L, c.sound_dig);
887         lua_setfield(L, -2, "sound_dig");
888         push_soundspec(L, c.sound_dug);
889         lua_setfield(L, -2, "sound_dug");
890         lua_setfield(L, -2, "sounds");
891         lua_pushboolean(L, c.legacy_facedir_simple);
892         lua_setfield(L, -2, "legacy_facedir_simple");
893         lua_pushboolean(L, c.legacy_wallmounted);
894         lua_setfield(L, -2, "legacy_wallmounted");
895         lua_pushstring(L, c.node_dig_prediction.c_str());
896         lua_setfield(L, -2, "node_dig_prediction");
897 }
898
899 /******************************************************************************/
900 void push_nodebox(lua_State *L, const NodeBox &box)
901 {
902         lua_newtable(L);
903         switch (box.type)
904         {
905                 case NODEBOX_REGULAR:
906                         lua_pushstring(L, "regular");
907                         lua_setfield(L, -2, "type");
908                         break;
909                 case NODEBOX_LEVELED:
910                 case NODEBOX_FIXED:
911                         lua_pushstring(L, "fixed");
912                         lua_setfield(L, -2, "type");
913                         push_box(L, box.fixed);
914                         lua_setfield(L, -2, "fixed");
915                         break;
916                 case NODEBOX_WALLMOUNTED:
917                         lua_pushstring(L, "wallmounted");
918                         lua_setfield(L, -2, "type");
919                         push_aabb3f(L, box.wall_top);
920                         lua_setfield(L, -2, "wall_top");
921                         push_aabb3f(L, box.wall_bottom);
922                         lua_setfield(L, -2, "wall_bottom");
923                         push_aabb3f(L, box.wall_side);
924                         lua_setfield(L, -2, "wall_side");
925                         break;
926                 case NODEBOX_CONNECTED:
927                         lua_pushstring(L, "connected");
928                         lua_setfield(L, -2, "type");
929                         push_box(L, box.connect_top);
930                         lua_setfield(L, -2, "connect_top");
931                         push_box(L, box.connect_bottom);
932                         lua_setfield(L, -2, "connect_bottom");
933                         push_box(L, box.connect_front);
934                         lua_setfield(L, -2, "connect_front");
935                         push_box(L, box.connect_back);
936                         lua_setfield(L, -2, "connect_back");
937                         push_box(L, box.connect_left);
938                         lua_setfield(L, -2, "connect_left");
939                         push_box(L, box.connect_right);
940                         lua_setfield(L, -2, "connect_right");
941                         break;
942                 default:
943                         FATAL_ERROR("Invalid box.type");
944                         break;
945         }
946 }
947
948 void push_box(lua_State *L, const std::vector<aabb3f> &box)
949 {
950         lua_newtable(L);
951         u8 i = 1;
952         for (const aabb3f &it : box) {
953                 push_aabb3f(L, it);
954                 lua_rawseti(L, -2, i);
955         }
956 }
957
958 /******************************************************************************/
959 void push_palette(lua_State *L, const std::vector<video::SColor> *palette)
960 {
961         lua_createtable(L, palette->size(), 0);
962         int newTable = lua_gettop(L);
963         int index = 1;
964         std::vector<video::SColor>::const_iterator iter;
965         for (iter = palette->begin(); iter != palette->end(); ++iter) {
966                 push_ARGB8(L, (*iter));
967                 lua_rawseti(L, newTable, index);
968                 index++;
969         }
970 }
971
972 /******************************************************************************/
973 void read_server_sound_params(lua_State *L, int index,
974                 ServerSoundParams &params)
975 {
976         if(index < 0)
977                 index = lua_gettop(L) + 1 + index;
978         // Clear
979         params = ServerSoundParams();
980         if(lua_istable(L, index)){
981                 getfloatfield(L, index, "gain", params.gain);
982                 getstringfield(L, index, "to_player", params.to_player);
983                 getfloatfield(L, index, "fade", params.fade);
984                 getfloatfield(L, index, "pitch", params.pitch);
985                 lua_getfield(L, index, "pos");
986                 if(!lua_isnil(L, -1)){
987                         v3f p = read_v3f(L, -1)*BS;
988                         params.pos = p;
989                         params.type = ServerSoundParams::SSP_POSITIONAL;
990                 }
991                 lua_pop(L, 1);
992                 lua_getfield(L, index, "object");
993                 if(!lua_isnil(L, -1)){
994                         ObjectRef *ref = ObjectRef::checkobject(L, -1);
995                         ServerActiveObject *sao = ObjectRef::getobject(ref);
996                         if(sao){
997                                 params.object = sao->getId();
998                                 params.type = ServerSoundParams::SSP_OBJECT;
999                         }
1000                 }
1001                 lua_pop(L, 1);
1002                 params.max_hear_distance = BS*getfloatfield_default(L, index,
1003                                 "max_hear_distance", params.max_hear_distance/BS);
1004                 getboolfield(L, index, "loop", params.loop);
1005         }
1006 }
1007
1008 /******************************************************************************/
1009 void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
1010 {
1011         if(index < 0)
1012                 index = lua_gettop(L) + 1 + index;
1013         if(lua_isnil(L, index)){
1014         } else if(lua_istable(L, index)){
1015                 getstringfield(L, index, "name", spec.name);
1016                 getfloatfield(L, index, "gain", spec.gain);
1017                 getfloatfield(L, index, "fade", spec.fade);
1018                 getfloatfield(L, index, "pitch", spec.pitch);
1019         } else if(lua_isstring(L, index)){
1020                 spec.name = lua_tostring(L, index);
1021         }
1022 }
1023
1024 void push_soundspec(lua_State *L, const SimpleSoundSpec &spec)
1025 {
1026         lua_newtable(L);
1027         lua_pushstring(L, spec.name.c_str());
1028         lua_setfield(L, -2, "name");
1029         lua_pushnumber(L, spec.gain);
1030         lua_setfield(L, -2, "gain");
1031         lua_pushnumber(L, spec.fade);
1032         lua_setfield(L, -2, "fade");
1033         lua_pushnumber(L, spec.pitch);
1034         lua_setfield(L, -2, "pitch");
1035 }
1036
1037 /******************************************************************************/
1038 NodeBox read_nodebox(lua_State *L, int index)
1039 {
1040         NodeBox nodebox;
1041         if(lua_istable(L, -1)){
1042                 nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
1043                                 ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
1044
1045 #define NODEBOXREAD(n, s){ \
1046                 lua_getfield(L, index, (s)); \
1047                 if (lua_istable(L, -1)) \
1048                         (n) = read_aabb3f(L, -1, BS); \
1049                 lua_pop(L, 1); \
1050         }
1051
1052 #define NODEBOXREADVEC(n, s) \
1053                 lua_getfield(L, index, (s)); \
1054                 if (lua_istable(L, -1)) \
1055                         (n) = read_aabb3f_vector(L, -1, BS); \
1056                 lua_pop(L, 1);
1057
1058                 NODEBOXREADVEC(nodebox.fixed, "fixed");
1059                 NODEBOXREAD(nodebox.wall_top, "wall_top");
1060                 NODEBOXREAD(nodebox.wall_bottom, "wall_bottom");
1061                 NODEBOXREAD(nodebox.wall_side, "wall_side");
1062                 NODEBOXREADVEC(nodebox.connect_top, "connect_top");
1063                 NODEBOXREADVEC(nodebox.connect_bottom, "connect_bottom");
1064                 NODEBOXREADVEC(nodebox.connect_front, "connect_front");
1065                 NODEBOXREADVEC(nodebox.connect_left, "connect_left");
1066                 NODEBOXREADVEC(nodebox.connect_back, "connect_back");
1067                 NODEBOXREADVEC(nodebox.connect_right, "connect_right");
1068                 NODEBOXREADVEC(nodebox.disconnected_top, "disconnected_top");
1069                 NODEBOXREADVEC(nodebox.disconnected_bottom, "disconnected_bottom");
1070                 NODEBOXREADVEC(nodebox.disconnected_front, "disconnected_front");
1071                 NODEBOXREADVEC(nodebox.disconnected_left, "disconnected_left");
1072                 NODEBOXREADVEC(nodebox.disconnected_back, "disconnected_back");
1073                 NODEBOXREADVEC(nodebox.disconnected_right, "disconnected_right");
1074                 NODEBOXREADVEC(nodebox.disconnected, "disconnected");
1075                 NODEBOXREADVEC(nodebox.disconnected_sides, "disconnected_sides");
1076         }
1077         return nodebox;
1078 }
1079
1080 /******************************************************************************/
1081 MapNode readnode(lua_State *L, int index, const NodeDefManager *ndef)
1082 {
1083         lua_getfield(L, index, "name");
1084         if (!lua_isstring(L, -1))
1085                 throw LuaError("Node name is not set or is not a string!");
1086         const char *name = lua_tostring(L, -1);
1087         lua_pop(L, 1);
1088
1089         u8 param1 = 0;
1090         lua_getfield(L, index, "param1");
1091         if (!lua_isnil(L, -1))
1092                 param1 = lua_tonumber(L, -1);
1093         lua_pop(L, 1);
1094
1095         u8 param2 = 0;
1096         lua_getfield(L, index, "param2");
1097         if (!lua_isnil(L, -1))
1098                 param2 = lua_tonumber(L, -1);
1099         lua_pop(L, 1);
1100
1101         return {ndef, name, param1, param2};
1102 }
1103
1104 /******************************************************************************/
1105 void pushnode(lua_State *L, const MapNode &n, const NodeDefManager *ndef)
1106 {
1107         lua_newtable(L);
1108         lua_pushstring(L, ndef->get(n).name.c_str());
1109         lua_setfield(L, -2, "name");
1110         lua_pushnumber(L, n.getParam1());
1111         lua_setfield(L, -2, "param1");
1112         lua_pushnumber(L, n.getParam2());
1113         lua_setfield(L, -2, "param2");
1114 }
1115
1116 /******************************************************************************/
1117 void warn_if_field_exists(lua_State *L, int table,
1118                 const char *name, const std::string &message)
1119 {
1120         lua_getfield(L, table, name);
1121         if (!lua_isnil(L, -1)) {
1122                 warningstream << "Field \"" << name << "\": "
1123                                 << message << std::endl;
1124                 infostream << script_get_backtrace(L) << std::endl;
1125         }
1126         lua_pop(L, 1);
1127 }
1128
1129 /******************************************************************************/
1130 int getenumfield(lua_State *L, int table,
1131                 const char *fieldname, const EnumString *spec, int default_)
1132 {
1133         int result = default_;
1134         string_to_enum(spec, result,
1135                         getstringfield_default(L, table, fieldname, ""));
1136         return result;
1137 }
1138
1139 /******************************************************************************/
1140 bool string_to_enum(const EnumString *spec, int &result,
1141                 const std::string &str)
1142 {
1143         const EnumString *esp = spec;
1144         while(esp->str){
1145                 if(str == std::string(esp->str)){
1146                         result = esp->num;
1147                         return true;
1148                 }
1149                 esp++;
1150         }
1151         return false;
1152 }
1153
1154 /******************************************************************************/
1155 ItemStack read_item(lua_State* L, int index, IItemDefManager *idef)
1156 {
1157         if(index < 0)
1158                 index = lua_gettop(L) + 1 + index;
1159
1160         if (lua_isnil(L, index)) {
1161                 return ItemStack();
1162         }
1163
1164         if (lua_isuserdata(L, index)) {
1165                 // Convert from LuaItemStack
1166                 LuaItemStack *o = LuaItemStack::checkobject(L, index);
1167                 return o->getItem();
1168         }
1169
1170         if (lua_isstring(L, index)) {
1171                 // Convert from itemstring
1172                 std::string itemstring = lua_tostring(L, index);
1173                 try
1174                 {
1175                         ItemStack item;
1176                         item.deSerialize(itemstring, idef);
1177                         return item;
1178                 }
1179                 catch(SerializationError &e)
1180                 {
1181                         warningstream<<"unable to create item from itemstring"
1182                                         <<": "<<itemstring<<std::endl;
1183                         return ItemStack();
1184                 }
1185         }
1186         else if(lua_istable(L, index))
1187         {
1188                 // Convert from table
1189                 std::string name = getstringfield_default(L, index, "name", "");
1190                 int count = getintfield_default(L, index, "count", 1);
1191                 int wear = getintfield_default(L, index, "wear", 0);
1192
1193                 ItemStack istack(name, count, wear, idef);
1194
1195                 // BACKWARDS COMPATIBLITY
1196                 std::string value = getstringfield_default(L, index, "metadata", "");
1197                 istack.metadata.setString("", value);
1198
1199                 // Get meta
1200                 lua_getfield(L, index, "meta");
1201                 int fieldstable = lua_gettop(L);
1202                 if (lua_istable(L, fieldstable)) {
1203                         lua_pushnil(L);
1204                         while (lua_next(L, fieldstable) != 0) {
1205                                 // key at index -2 and value at index -1
1206                                 std::string key = lua_tostring(L, -2);
1207                                 size_t value_len;
1208                                 const char *value_cs = lua_tolstring(L, -1, &value_len);
1209                                 std::string value(value_cs, value_len);
1210                                 istack.metadata.setString(key, value);
1211                                 lua_pop(L, 1); // removes value, keeps key for next iteration
1212                         }
1213                 }
1214
1215                 return istack;
1216         } else {
1217                 throw LuaError("Expecting itemstack, itemstring, table or nil");
1218         }
1219 }
1220
1221 /******************************************************************************/
1222 void push_tool_capabilities(lua_State *L,
1223                 const ToolCapabilities &toolcap)
1224 {
1225         lua_newtable(L);
1226         setfloatfield(L, -1, "full_punch_interval", toolcap.full_punch_interval);
1227                 setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
1228                 // Create groupcaps table
1229                 lua_newtable(L);
1230                 // For each groupcap
1231                 for (const auto &gc_it : toolcap.groupcaps) {
1232                         // Create groupcap table
1233                         lua_newtable(L);
1234                         const std::string &name = gc_it.first;
1235                         const ToolGroupCap &groupcap = gc_it.second;
1236                         // Create subtable "times"
1237                         lua_newtable(L);
1238                         for (auto time : groupcap.times) {
1239                                 lua_pushinteger(L, time.first);
1240                                 lua_pushnumber(L, time.second);
1241                                 lua_settable(L, -3);
1242                         }
1243                         // Set subtable "times"
1244                         lua_setfield(L, -2, "times");
1245                         // Set simple parameters
1246                         setintfield(L, -1, "maxlevel", groupcap.maxlevel);
1247                         setintfield(L, -1, "uses", groupcap.uses);
1248                         // Insert groupcap table into groupcaps table
1249                         lua_setfield(L, -2, name.c_str());
1250                 }
1251                 // Set groupcaps table
1252                 lua_setfield(L, -2, "groupcaps");
1253                 //Create damage_groups table
1254                 lua_newtable(L);
1255                 // For each damage group
1256                 for (const auto &damageGroup : toolcap.damageGroups) {
1257                         // Create damage group table
1258                         lua_pushinteger(L, damageGroup.second);
1259                         lua_setfield(L, -2, damageGroup.first.c_str());
1260                 }
1261                 lua_setfield(L, -2, "damage_groups");
1262 }
1263
1264 /******************************************************************************/
1265 void push_inventory_list(lua_State *L, Inventory *inv, const char *name)
1266 {
1267         InventoryList *invlist = inv->getList(name);
1268         if(invlist == NULL){
1269                 lua_pushnil(L);
1270                 return;
1271         }
1272         std::vector<ItemStack> items;
1273         for(u32 i=0; i<invlist->getSize(); i++)
1274                 items.push_back(invlist->getItem(i));
1275         push_items(L, items);
1276 }
1277
1278 /******************************************************************************/
1279 void read_inventory_list(lua_State *L, int tableindex,
1280                 Inventory *inv, const char *name, Server* srv, int forcesize)
1281 {
1282         if(tableindex < 0)
1283                 tableindex = lua_gettop(L) + 1 + tableindex;
1284         // If nil, delete list
1285         if(lua_isnil(L, tableindex)){
1286                 inv->deleteList(name);
1287                 return;
1288         }
1289         // Otherwise set list
1290         std::vector<ItemStack> items = read_items(L, tableindex,srv);
1291         int listsize = (forcesize != -1) ? forcesize : items.size();
1292         InventoryList *invlist = inv->addList(name, listsize);
1293         int index = 0;
1294         for(std::vector<ItemStack>::const_iterator
1295                         i = items.begin(); i != items.end(); ++i){
1296                 if(forcesize != -1 && index == forcesize)
1297                         break;
1298                 invlist->changeItem(index, *i);
1299                 index++;
1300         }
1301         while(forcesize != -1 && index < forcesize){
1302                 invlist->deleteItem(index);
1303                 index++;
1304         }
1305 }
1306
1307 /******************************************************************************/
1308 struct TileAnimationParams read_animation_definition(lua_State *L, int index)
1309 {
1310         if(index < 0)
1311                 index = lua_gettop(L) + 1 + index;
1312
1313         struct TileAnimationParams anim;
1314         anim.type = TAT_NONE;
1315         if (!lua_istable(L, index))
1316                 return anim;
1317
1318         anim.type = (TileAnimationType)
1319                 getenumfield(L, index, "type", es_TileAnimationType,
1320                 TAT_NONE);
1321         if (anim.type == TAT_VERTICAL_FRAMES) {
1322                 // {type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0}
1323                 anim.vertical_frames.aspect_w =
1324                         getintfield_default(L, index, "aspect_w", 16);
1325                 anim.vertical_frames.aspect_h =
1326                         getintfield_default(L, index, "aspect_h", 16);
1327                 anim.vertical_frames.length =
1328                         getfloatfield_default(L, index, "length", 1.0);
1329         } else if (anim.type == TAT_SHEET_2D) {
1330                 // {type="sheet_2d", frames_w=5, frames_h=3, frame_length=0.5}
1331                 getintfield(L, index, "frames_w",
1332                         anim.sheet_2d.frames_w);
1333                 getintfield(L, index, "frames_h",
1334                         anim.sheet_2d.frames_h);
1335                 getfloatfield(L, index, "frame_length",
1336                         anim.sheet_2d.frame_length);
1337         }
1338
1339         return anim;
1340 }
1341
1342 /******************************************************************************/
1343 ToolCapabilities read_tool_capabilities(
1344                 lua_State *L, int table)
1345 {
1346         ToolCapabilities toolcap;
1347         getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
1348         getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
1349         lua_getfield(L, table, "groupcaps");
1350         if(lua_istable(L, -1)){
1351                 int table_groupcaps = lua_gettop(L);
1352                 lua_pushnil(L);
1353                 while(lua_next(L, table_groupcaps) != 0){
1354                         // key at index -2 and value at index -1
1355                         std::string groupname = luaL_checkstring(L, -2);
1356                         if(lua_istable(L, -1)){
1357                                 int table_groupcap = lua_gettop(L);
1358                                 // This will be created
1359                                 ToolGroupCap groupcap;
1360                                 // Read simple parameters
1361                                 getintfield(L, table_groupcap, "maxlevel", groupcap.maxlevel);
1362                                 getintfield(L, table_groupcap, "uses", groupcap.uses);
1363                                 // DEPRECATED: maxwear
1364                                 float maxwear = 0;
1365                                 if (getfloatfield(L, table_groupcap, "maxwear", maxwear)){
1366                                         if (maxwear != 0)
1367                                                 groupcap.uses = 1.0/maxwear;
1368                                         else
1369                                                 groupcap.uses = 0;
1370                                         warningstream << "Field \"maxwear\" is deprecated; "
1371                                                         << "replace with uses=1/maxwear" << std::endl;
1372                                         infostream << script_get_backtrace(L) << std::endl;
1373                                 }
1374                                 // Read "times" table
1375                                 lua_getfield(L, table_groupcap, "times");
1376                                 if(lua_istable(L, -1)){
1377                                         int table_times = lua_gettop(L);
1378                                         lua_pushnil(L);
1379                                         while(lua_next(L, table_times) != 0){
1380                                                 // key at index -2 and value at index -1
1381                                                 int rating = luaL_checkinteger(L, -2);
1382                                                 float time = luaL_checknumber(L, -1);
1383                                                 groupcap.times[rating] = time;
1384                                                 // removes value, keeps key for next iteration
1385                                                 lua_pop(L, 1);
1386                                         }
1387                                 }
1388                                 lua_pop(L, 1);
1389                                 // Insert groupcap into toolcap
1390                                 toolcap.groupcaps[groupname] = groupcap;
1391                         }
1392                         // removes value, keeps key for next iteration
1393                         lua_pop(L, 1);
1394                 }
1395         }
1396         lua_pop(L, 1);
1397
1398         lua_getfield(L, table, "damage_groups");
1399         if(lua_istable(L, -1)){
1400                 int table_damage_groups = lua_gettop(L);
1401                 lua_pushnil(L);
1402                 while(lua_next(L, table_damage_groups) != 0){
1403                         // key at index -2 and value at index -1
1404                         std::string groupname = luaL_checkstring(L, -2);
1405                         u16 value = luaL_checkinteger(L, -1);
1406                         toolcap.damageGroups[groupname] = value;
1407                         // removes value, keeps key for next iteration
1408                         lua_pop(L, 1);
1409                 }
1410         }
1411         lua_pop(L, 1);
1412         return toolcap;
1413 }
1414
1415 /******************************************************************************/
1416 void push_dig_params(lua_State *L,const DigParams &params)
1417 {
1418         lua_newtable(L);
1419         setboolfield(L, -1, "diggable", params.diggable);
1420         setfloatfield(L, -1, "time", params.time);
1421         setintfield(L, -1, "wear", params.wear);
1422 }
1423
1424 /******************************************************************************/
1425 void push_hit_params(lua_State *L,const HitParams &params)
1426 {
1427         lua_newtable(L);
1428         setintfield(L, -1, "hp", params.hp);
1429         setintfield(L, -1, "wear", params.wear);
1430 }
1431
1432 /******************************************************************************/
1433
1434 bool getflagsfield(lua_State *L, int table, const char *fieldname,
1435         FlagDesc *flagdesc, u32 *flags, u32 *flagmask)
1436 {
1437         lua_getfield(L, table, fieldname);
1438
1439         bool success = read_flags(L, -1, flagdesc, flags, flagmask);
1440
1441         lua_pop(L, 1);
1442
1443         return success;
1444 }
1445
1446 bool read_flags(lua_State *L, int index, FlagDesc *flagdesc,
1447         u32 *flags, u32 *flagmask)
1448 {
1449         if (lua_isstring(L, index)) {
1450                 std::string flagstr = lua_tostring(L, index);
1451                 *flags = readFlagString(flagstr, flagdesc, flagmask);
1452         } else if (lua_istable(L, index)) {
1453                 *flags = read_flags_table(L, index, flagdesc, flagmask);
1454         } else {
1455                 return false;
1456         }
1457
1458         return true;
1459 }
1460
1461 u32 read_flags_table(lua_State *L, int table, FlagDesc *flagdesc, u32 *flagmask)
1462 {
1463         u32 flags = 0, mask = 0;
1464         char fnamebuf[64] = "no";
1465
1466         for (int i = 0; flagdesc[i].name; i++) {
1467                 bool result;
1468
1469                 if (getboolfield(L, table, flagdesc[i].name, result)) {
1470                         mask |= flagdesc[i].flag;
1471                         if (result)
1472                                 flags |= flagdesc[i].flag;
1473                 }
1474
1475                 strlcpy(fnamebuf + 2, flagdesc[i].name, sizeof(fnamebuf) - 2);
1476                 if (getboolfield(L, table, fnamebuf, result))
1477                         mask |= flagdesc[i].flag;
1478         }
1479
1480         if (flagmask)
1481                 *flagmask = mask;
1482
1483         return flags;
1484 }
1485
1486 void push_flags_string(lua_State *L, FlagDesc *flagdesc, u32 flags, u32 flagmask)
1487 {
1488         std::string flagstring = writeFlagString(flags, flagdesc, flagmask);
1489         lua_pushlstring(L, flagstring.c_str(), flagstring.size());
1490 }
1491
1492 /******************************************************************************/
1493 /* Lua Stored data!                                                           */
1494 /******************************************************************************/
1495
1496 /******************************************************************************/
1497 void read_groups(lua_State *L, int index, ItemGroupList &result)
1498 {
1499         if (!lua_istable(L,index))
1500                 return;
1501         result.clear();
1502         lua_pushnil(L);
1503         if(index < 0)
1504                 index -= 1;
1505         while(lua_next(L, index) != 0){
1506                 // key at index -2 and value at index -1
1507                 std::string name = luaL_checkstring(L, -2);
1508                 int rating = luaL_checkinteger(L, -1);
1509                 result[name] = rating;
1510                 // removes value, keeps key for next iteration
1511                 lua_pop(L, 1);
1512         }
1513 }
1514
1515 /******************************************************************************/
1516 void push_groups(lua_State *L, const ItemGroupList &groups)
1517 {
1518         lua_newtable(L);
1519         for (const auto &group : groups) {
1520                 lua_pushnumber(L, group.second);
1521                 lua_setfield(L, -2, group.first.c_str());
1522         }
1523 }
1524
1525 /******************************************************************************/
1526 void push_items(lua_State *L, const std::vector<ItemStack> &items)
1527 {
1528         lua_createtable(L, items.size(), 0);
1529         for (u32 i = 0; i != items.size(); i++) {
1530                 LuaItemStack::create(L, items[i]);
1531                 lua_rawseti(L, -2, i + 1);
1532         }
1533 }
1534
1535 /******************************************************************************/
1536 std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
1537 {
1538         if(index < 0)
1539                 index = lua_gettop(L) + 1 + index;
1540
1541         std::vector<ItemStack> items;
1542         luaL_checktype(L, index, LUA_TTABLE);
1543         lua_pushnil(L);
1544         while (lua_next(L, index)) {
1545                 s32 key = luaL_checkinteger(L, -2);
1546                 if (key < 1) {
1547                         throw LuaError("Invalid inventory list index");
1548                 }
1549                 if (items.size() < (u32) key) {
1550                         items.resize(key);
1551                 }
1552                 items[key - 1] = read_item(L, -1, srv->idef());
1553                 lua_pop(L, 1);
1554         }
1555         return items;
1556 }
1557
1558 /******************************************************************************/
1559 void luaentity_get(lua_State *L, u16 id)
1560 {
1561         // Get luaentities[i]
1562         lua_getglobal(L, "core");
1563         lua_getfield(L, -1, "luaentities");
1564         luaL_checktype(L, -1, LUA_TTABLE);
1565         lua_pushnumber(L, id);
1566         lua_gettable(L, -2);
1567         lua_remove(L, -2); // Remove luaentities
1568         lua_remove(L, -2); // Remove core
1569 }
1570
1571 /******************************************************************************/
1572 bool read_noiseparams(lua_State *L, int index, NoiseParams *np)
1573 {
1574         if (index < 0)
1575                 index = lua_gettop(L) + 1 + index;
1576
1577         if (!lua_istable(L, index))
1578                 return false;
1579
1580         getfloatfield(L, index, "offset",      np->offset);
1581         getfloatfield(L, index, "scale",       np->scale);
1582         getfloatfield(L, index, "persist",     np->persist);
1583         getfloatfield(L, index, "persistence", np->persist);
1584         getfloatfield(L, index, "lacunarity",  np->lacunarity);
1585         getintfield(L,   index, "seed",        np->seed);
1586         getintfield(L,   index, "octaves",     np->octaves);
1587
1588         u32 flags    = 0;
1589         u32 flagmask = 0;
1590         np->flags = getflagsfield(L, index, "flags", flagdesc_noiseparams,
1591                 &flags, &flagmask) ? flags : NOISE_FLAG_DEFAULTS;
1592
1593         lua_getfield(L, index, "spread");
1594         np->spread  = read_v3f(L, -1);
1595         lua_pop(L, 1);
1596
1597         return true;
1598 }
1599
1600 void push_noiseparams(lua_State *L, NoiseParams *np)
1601 {
1602         lua_newtable(L);
1603         push_float_string(L, np->offset);
1604         lua_setfield(L, -2, "offset");
1605         push_float_string(L, np->scale);
1606         lua_setfield(L, -2, "scale");
1607         push_float_string(L, np->persist);
1608         lua_setfield(L, -2, "persistence");
1609         push_float_string(L, np->lacunarity);
1610         lua_setfield(L, -2, "lacunarity");
1611         lua_pushnumber(L, np->seed);
1612         lua_setfield(L, -2, "seed");
1613         lua_pushnumber(L, np->octaves);
1614         lua_setfield(L, -2, "octaves");
1615
1616         push_flags_string(L, flagdesc_noiseparams, np->flags,
1617                 np->flags);
1618         lua_setfield(L, -2, "flags");
1619
1620         push_v3_float_string(L, np->spread);
1621         lua_setfield(L, -2, "spread");
1622 }
1623
1624 /******************************************************************************/
1625 // Returns depth of json value tree
1626 static int push_json_value_getdepth(const Json::Value &value)
1627 {
1628         if (!value.isArray() && !value.isObject())
1629                 return 1;
1630
1631         int maxdepth = 0;
1632         for (const auto &it : value) {
1633                 int elemdepth = push_json_value_getdepth(it);
1634                 if (elemdepth > maxdepth)
1635                         maxdepth = elemdepth;
1636         }
1637         return maxdepth + 1;
1638 }
1639 // Recursive function to convert JSON --> Lua table
1640 static bool push_json_value_helper(lua_State *L, const Json::Value &value,
1641                 int nullindex)
1642 {
1643         switch(value.type()) {
1644                 case Json::nullValue:
1645                 default:
1646                         lua_pushvalue(L, nullindex);
1647                         break;
1648                 case Json::intValue:
1649                         lua_pushinteger(L, value.asInt());
1650                         break;
1651                 case Json::uintValue:
1652                         lua_pushinteger(L, value.asUInt());
1653                         break;
1654                 case Json::realValue:
1655                         lua_pushnumber(L, value.asDouble());
1656                         break;
1657                 case Json::stringValue:
1658                         {
1659                                 const char *str = value.asCString();
1660                                 lua_pushstring(L, str ? str : "");
1661                         }
1662                         break;
1663                 case Json::booleanValue:
1664                         lua_pushboolean(L, value.asInt());
1665                         break;
1666                 case Json::arrayValue:
1667                         lua_newtable(L);
1668                         for (Json::Value::const_iterator it = value.begin();
1669                                         it != value.end(); ++it) {
1670                                 push_json_value_helper(L, *it, nullindex);
1671                                 lua_rawseti(L, -2, it.index() + 1);
1672                         }
1673                         break;
1674                 case Json::objectValue:
1675                         lua_newtable(L);
1676                         for (Json::Value::const_iterator it = value.begin();
1677                                         it != value.end(); ++it) {
1678 #ifndef JSONCPP_STRING
1679                                 const char *str = it.memberName();
1680                                 lua_pushstring(L, str ? str : "");
1681 #else
1682                                 std::string str = it.name();
1683                                 lua_pushstring(L, str.c_str());
1684 #endif
1685                                 push_json_value_helper(L, *it, nullindex);
1686                                 lua_rawset(L, -3);
1687                         }
1688                         break;
1689         }
1690         return true;
1691 }
1692 // converts JSON --> Lua table; returns false if lua stack limit exceeded
1693 // nullindex: Lua stack index of value to use in place of JSON null
1694 bool push_json_value(lua_State *L, const Json::Value &value, int nullindex)
1695 {
1696         if(nullindex < 0)
1697                 nullindex = lua_gettop(L) + 1 + nullindex;
1698
1699         int depth = push_json_value_getdepth(value);
1700
1701         // The maximum number of Lua stack slots used at each recursion level
1702         // of push_json_value_helper is 2, so make sure there a depth * 2 slots
1703         if (lua_checkstack(L, depth * 2))
1704                 return push_json_value_helper(L, value, nullindex);
1705
1706         return false;
1707 }
1708
1709 // Converts Lua table --> JSON
1710 void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion)
1711 {
1712         if (recursion > 16) {
1713                 throw SerializationError("Maximum recursion depth exceeded");
1714         }
1715         int type = lua_type(L, index);
1716         if (type == LUA_TBOOLEAN) {
1717                 root = (bool) lua_toboolean(L, index);
1718         } else if (type == LUA_TNUMBER) {
1719                 root = lua_tonumber(L, index);
1720         } else if (type == LUA_TSTRING) {
1721                 size_t len;
1722                 const char *str = lua_tolstring(L, index, &len);
1723                 root = std::string(str, len);
1724         } else if (type == LUA_TTABLE) {
1725                 lua_pushnil(L);
1726                 while (lua_next(L, index)) {
1727                         // Key is at -2 and value is at -1
1728                         Json::Value value;
1729                         read_json_value(L, value, lua_gettop(L), recursion + 1);
1730
1731                         Json::ValueType roottype = root.type();
1732                         int keytype = lua_type(L, -1);
1733                         if (keytype == LUA_TNUMBER) {
1734                                 lua_Number key = lua_tonumber(L, -1);
1735                                 if (roottype != Json::nullValue && roottype != Json::arrayValue) {
1736                                         throw SerializationError("Can't mix array and object values in JSON");
1737                                 } else if (key < 1) {
1738                                         throw SerializationError("Can't use zero-based or negative indexes in JSON");
1739                                 } else if (floor(key) != key) {
1740                                         throw SerializationError("Can't use indexes with a fractional part in JSON");
1741                                 }
1742                                 root[(Json::ArrayIndex) key - 1] = value;
1743                         } else if (keytype == LUA_TSTRING) {
1744                                 if (roottype != Json::nullValue && roottype != Json::objectValue) {
1745                                         throw SerializationError("Can't mix array and object values in JSON");
1746                                 }
1747                                 root[lua_tostring(L, -1)] = value;
1748                         } else {
1749                                 throw SerializationError("Lua key to convert to JSON is not a string or number");
1750                         }
1751                 }
1752         } else if (type == LUA_TNIL) {
1753                 root = Json::nullValue;
1754         } else {
1755                 throw SerializationError("Can only store booleans, numbers, strings, objects, arrays, and null in JSON");
1756         }
1757         lua_pop(L, 1); // Pop value
1758 }
1759
1760 void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm,
1761         bool hitpoint)
1762 {
1763         lua_newtable(L);
1764         if (pointed.type == POINTEDTHING_NODE) {
1765                 lua_pushstring(L, "node");
1766                 lua_setfield(L, -2, "type");
1767                 push_v3s16(L, pointed.node_undersurface);
1768                 lua_setfield(L, -2, "under");
1769                 push_v3s16(L, pointed.node_abovesurface);
1770                 lua_setfield(L, -2, "above");
1771         } else if (pointed.type == POINTEDTHING_OBJECT) {
1772                 lua_pushstring(L, "object");
1773                 lua_setfield(L, -2, "type");
1774
1775                 if (csm) {
1776                         lua_pushinteger(L, pointed.object_id);
1777                         lua_setfield(L, -2, "id");
1778                 } else {
1779                         push_objectRef(L, pointed.object_id);
1780                         lua_setfield(L, -2, "ref");
1781                 }
1782         } else {
1783                 lua_pushstring(L, "nothing");
1784                 lua_setfield(L, -2, "type");
1785         }
1786         if (hitpoint && (pointed.type != POINTEDTHING_NOTHING)) {
1787                 push_v3f(L, pointed.intersection_point / BS); // convert to node coords
1788                 lua_setfield(L, -2, "intersection_point");
1789                 push_v3s16(L, pointed.intersection_normal);
1790                 lua_setfield(L, -2, "intersection_normal");
1791                 lua_pushinteger(L, pointed.box_id + 1); // change to Lua array index
1792                 lua_setfield(L, -2, "box_id");
1793         }
1794 }
1795
1796 void push_objectRef(lua_State *L, const u16 id)
1797 {
1798         // Get core.object_refs[i]
1799         lua_getglobal(L, "core");
1800         lua_getfield(L, -1, "object_refs");
1801         luaL_checktype(L, -1, LUA_TTABLE);
1802         lua_pushnumber(L, id);
1803         lua_gettable(L, -2);
1804         lua_remove(L, -2); // object_refs
1805         lua_remove(L, -2); // core
1806 }
1807
1808 void read_hud_element(lua_State *L, HudElement *elem)
1809 {
1810         elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
1811                                                                         es_HudElementType, HUD_ELEM_TEXT);
1812
1813         lua_getfield(L, 2, "position");
1814         elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1815         lua_pop(L, 1);
1816
1817         lua_getfield(L, 2, "scale");
1818         elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1819         lua_pop(L, 1);
1820
1821         lua_getfield(L, 2, "size");
1822         elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
1823         lua_pop(L, 1);
1824
1825         elem->name   = getstringfield_default(L, 2, "name", "");
1826         elem->text   = getstringfield_default(L, 2, "text", "");
1827         elem->number = getintfield_default(L, 2, "number", 0);
1828         elem->item   = getintfield_default(L, 2, "item", 0);
1829         elem->dir    = getintfield_default(L, 2, "direction", 0);
1830
1831         // Deprecated, only for compatibility's sake
1832         if (elem->dir == 0)
1833                 elem->dir = getintfield_default(L, 2, "dir", 0);
1834
1835         lua_getfield(L, 2, "alignment");
1836         elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1837         lua_pop(L, 1);
1838
1839         lua_getfield(L, 2, "offset");
1840         elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1841         lua_pop(L, 1);
1842
1843         lua_getfield(L, 2, "world_pos");
1844         elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
1845         lua_pop(L, 1);
1846
1847         /* check for known deprecated element usage */
1848         if ((elem->type  == HUD_ELEM_STATBAR) && (elem->size == v2s32()))
1849                 log_deprecated(L,"Deprecated usage of statbar without size!");
1850 }
1851
1852 void push_hud_element(lua_State *L, HudElement *elem)
1853 {
1854         lua_newtable(L);
1855
1856         lua_pushstring(L, es_HudElementType[(u8)elem->type].str);
1857         lua_setfield(L, -2, "type");
1858
1859         push_v2f(L, elem->pos);
1860         lua_setfield(L, -2, "position");
1861
1862         lua_pushstring(L, elem->name.c_str());
1863         lua_setfield(L, -2, "name");
1864
1865         push_v2f(L, elem->scale);
1866         lua_setfield(L, -2, "scale");
1867
1868         lua_pushstring(L, elem->text.c_str());
1869         lua_setfield(L, -2, "text");
1870
1871         lua_pushnumber(L, elem->number);
1872         lua_setfield(L, -2, "number");
1873
1874         lua_pushnumber(L, elem->item);
1875         lua_setfield(L, -2, "item");
1876
1877         lua_pushnumber(L, elem->dir);
1878         lua_setfield(L, -2, "direction");
1879
1880         push_v2f(L, elem->offset);
1881         lua_setfield(L, -2, "offset");
1882
1883         push_v2f(L, elem->align);
1884         lua_setfield(L, -2, "alignment");
1885
1886         push_v2s32(L, elem->size);
1887         lua_setfield(L, -2, "size");
1888
1889         // Deprecated, only for compatibility's sake
1890         lua_pushnumber(L, elem->dir);
1891         lua_setfield(L, -2, "dir");
1892
1893         push_v3f(L, elem->world_pos);
1894         lua_setfield(L, -2, "world_pos");
1895 }
1896
1897 HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
1898 {
1899         HudElementStat stat = HUD_STAT_NUMBER;
1900         if (lua_isstring(L, 3)) {
1901                 int statint;
1902                 std::string statstr = lua_tostring(L, 3);
1903                 stat = string_to_enum(es_HudElementStat, statint, statstr) ?
1904                                 (HudElementStat)statint : stat;
1905         }
1906
1907         switch (stat) {
1908                 case HUD_STAT_POS:
1909                         elem->pos = read_v2f(L, 4);
1910                         *value = &elem->pos;
1911                         break;
1912                 case HUD_STAT_NAME:
1913                         elem->name = luaL_checkstring(L, 4);
1914                         *value = &elem->name;
1915                         break;
1916                 case HUD_STAT_SCALE:
1917                         elem->scale = read_v2f(L, 4);
1918                         *value = &elem->scale;
1919                         break;
1920                 case HUD_STAT_TEXT:
1921                         elem->text = luaL_checkstring(L, 4);
1922                         *value = &elem->text;
1923                         break;
1924                 case HUD_STAT_NUMBER:
1925                         elem->number = luaL_checknumber(L, 4);
1926                         *value = &elem->number;
1927                         break;
1928                 case HUD_STAT_ITEM:
1929                         elem->item = luaL_checknumber(L, 4);
1930                         *value = &elem->item;
1931                         break;
1932                 case HUD_STAT_DIR:
1933                         elem->dir = luaL_checknumber(L, 4);
1934                         *value = &elem->dir;
1935                         break;
1936                 case HUD_STAT_ALIGN:
1937                         elem->align = read_v2f(L, 4);
1938                         *value = &elem->align;
1939                         break;
1940                 case HUD_STAT_OFFSET:
1941                         elem->offset = read_v2f(L, 4);
1942                         *value = &elem->offset;
1943                         break;
1944                 case HUD_STAT_WORLD_POS:
1945                         elem->world_pos = read_v3f(L, 4);
1946                         *value = &elem->world_pos;
1947                         break;
1948                 case HUD_STAT_SIZE:
1949                         elem->size = read_v2s32(L, 4);
1950                         *value = &elem->size;
1951                         break;
1952         }
1953         return stat;
1954 }