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