3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "common/c_content.h"
20 #include "common/c_converter.h"
21 #include "common/c_types.h"
23 #include "object_properties.h"
24 #include "collision.h"
25 #include "cpp_api/s_node.h"
26 #include "lua_api/l_object.h"
27 #include "lua_api/l_item.h"
28 #include "common/c_internal.h"
33 #include "mapgen/mg_schematic.h"
35 #include "server/player_sao.h"
36 #include "util/pointedthing.h"
37 #include "debug.h" // For FATAL_ERROR
38 #include <json/json.h>
40 struct EnumString es_TileAnimationType[] =
43 {TAT_VERTICAL_FRAMES, "vertical_frames"},
44 {TAT_SHEET_2D, "sheet_2d"},
48 /******************************************************************************/
49 void read_item_definition(lua_State* L, int index,
50 const ItemDefinition &default_def, ItemDefinition &def)
53 index = lua_gettop(L) + 1 + index;
55 def.type = (ItemType)getenumfield(L, index, "type",
56 es_ItemType, ITEM_NONE);
57 getstringfield(L, index, "name", def.name);
58 getstringfield(L, index, "description", def.description);
59 getstringfield(L, index, "short_description", def.short_description);
60 getstringfield(L, index, "inventory_image", def.inventory_image);
61 getstringfield(L, index, "inventory_overlay", def.inventory_overlay);
62 getstringfield(L, index, "wield_image", def.wield_image);
63 getstringfield(L, index, "wield_overlay", def.wield_overlay);
64 getstringfield(L, index, "palette", def.palette_image);
67 lua_getfield(L, index, "color");
68 read_color(L, -1, &def.color);
71 lua_getfield(L, index, "wield_scale");
72 if(lua_istable(L, -1)){
73 def.wield_scale = check_v3f(L, -1);
77 int stack_max = getintfield_default(L, index, "stack_max", def.stack_max);
78 def.stack_max = rangelim(stack_max, 1, U16_MAX);
80 lua_getfield(L, index, "on_use");
81 def.usable = lua_isfunction(L, -1);
84 getboolfield(L, index, "liquids_pointable", def.liquids_pointable);
86 lua_getfield(L, index, "tool_capabilities");
87 if(lua_istable(L, -1)){
88 def.tool_capabilities = new ToolCapabilities(
89 read_tool_capabilities(L, -1));
92 // If name is "" (hand), ensure there are ToolCapabilities
93 // because it will be looked up there whenever any other item has
94 // no ToolCapabilities
95 if (def.name.empty() && def.tool_capabilities == NULL){
96 def.tool_capabilities = new ToolCapabilities();
99 lua_getfield(L, index, "groups");
100 read_groups(L, -1, def.groups);
103 lua_getfield(L, index, "sounds");
104 if (!lua_isnil(L, -1)) {
105 luaL_checktype(L, -1, LUA_TTABLE);
106 lua_getfield(L, -1, "place");
107 read_soundspec(L, -1, def.sound_place);
109 lua_getfield(L, -1, "place_failed");
110 read_soundspec(L, -1, def.sound_place_failed);
115 // No, this is not a mistake. Item sounds are in "sound", node sounds in "sounds".
116 lua_getfield(L, index, "sound");
117 if (!lua_isnil(L, -1)) {
118 luaL_checktype(L, -1, LUA_TTABLE);
119 lua_getfield(L, -1, "punch_use");
120 read_soundspec(L, -1, def.sound_use);
122 lua_getfield(L, -1, "punch_use_air");
123 read_soundspec(L, -1, def.sound_use_air);
128 def.range = getfloatfield_default(L, index, "range", def.range);
130 // Client shall immediately place this node when player places the item.
131 // Server will update the precise end result a moment later.
132 // "" = no prediction
133 getstringfield(L, index, "node_placement_prediction",
134 def.node_placement_prediction);
136 getintfield(L, index, "place_param2", def.place_param2);
139 /******************************************************************************/
140 void push_item_definition(lua_State *L, const ItemDefinition &i)
143 lua_pushstring(L, i.name.c_str());
144 lua_setfield(L, -2, "name");
145 lua_pushstring(L, i.description.c_str());
146 lua_setfield(L, -2, "description");
149 void push_item_definition_full(lua_State *L, const ItemDefinition &i)
151 std::string type(es_ItemType[(int)i.type].str);
154 lua_pushstring(L, i.name.c_str());
155 lua_setfield(L, -2, "name");
156 lua_pushstring(L, i.description.c_str());
157 lua_setfield(L, -2, "description");
158 if (!i.short_description.empty()) {
159 lua_pushstring(L, i.short_description.c_str());
160 lua_setfield(L, -2, "short_description");
162 lua_pushstring(L, type.c_str());
163 lua_setfield(L, -2, "type");
164 lua_pushstring(L, i.inventory_image.c_str());
165 lua_setfield(L, -2, "inventory_image");
166 lua_pushstring(L, i.inventory_overlay.c_str());
167 lua_setfield(L, -2, "inventory_overlay");
168 lua_pushstring(L, i.wield_image.c_str());
169 lua_setfield(L, -2, "wield_image");
170 lua_pushstring(L, i.wield_overlay.c_str());
171 lua_setfield(L, -2, "wield_overlay");
172 lua_pushstring(L, i.palette_image.c_str());
173 lua_setfield(L, -2, "palette_image");
174 push_ARGB8(L, i.color);
175 lua_setfield(L, -2, "color");
176 push_v3f(L, i.wield_scale);
177 lua_setfield(L, -2, "wield_scale");
178 lua_pushinteger(L, i.stack_max);
179 lua_setfield(L, -2, "stack_max");
180 lua_pushboolean(L, i.usable);
181 lua_setfield(L, -2, "usable");
182 lua_pushboolean(L, i.liquids_pointable);
183 lua_setfield(L, -2, "liquids_pointable");
184 if (i.tool_capabilities) {
185 push_tool_capabilities(L, *i.tool_capabilities);
186 lua_setfield(L, -2, "tool_capabilities");
188 push_groups(L, i.groups);
189 lua_setfield(L, -2, "groups");
190 push_soundspec(L, i.sound_place);
191 lua_setfield(L, -2, "sound_place");
192 push_soundspec(L, i.sound_place_failed);
193 lua_setfield(L, -2, "sound_place_failed");
194 lua_pushstring(L, i.node_placement_prediction.c_str());
195 lua_setfield(L, -2, "node_placement_prediction");
198 /******************************************************************************/
199 void read_object_properties(lua_State *L, int index,
200 ServerActiveObject *sao, ObjectProperties *prop, IItemDefManager *idef)
203 index = lua_gettop(L) + 1 + index;
204 if (lua_isnil(L, index))
207 luaL_checktype(L, -1, LUA_TTABLE);
210 if (getintfield(L, -1, "hp_max", hp_max)) {
211 prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);
212 // hp_max = 0 is sometimes used as a hack to keep players dead, only validate for entities
213 if (prop->hp_max == 0 && sao->getType() != ACTIVEOBJECT_TYPE_PLAYER)
214 throw LuaError("The hp_max property may not be 0 for entities!");
216 if (prop->hp_max < sao->getHP()) {
217 PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP_MAX);
218 sao->setHP(prop->hp_max, reason);
222 if (getintfield(L, -1, "breath_max", prop->breath_max)) {
223 if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
224 PlayerSAO *player = (PlayerSAO *)sao;
225 if (prop->breath_max < player->getBreath())
226 player->setBreath(prop->breath_max);
229 getboolfield(L, -1, "physical", prop->physical);
230 getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
232 lua_getfield(L, -1, "collisionbox");
233 bool collisionbox_defined = lua_istable(L, -1);
234 if (collisionbox_defined)
235 prop->collisionbox = read_aabb3f(L, -1, 1.0);
238 lua_getfield(L, -1, "selectionbox");
239 if (lua_istable(L, -1)) {
240 getboolfield(L, -1, "rotate", prop->rotate_selectionbox);
241 prop->selectionbox = read_aabb3f(L, -1, 1.0);
242 } else if (collisionbox_defined) {
243 prop->selectionbox = prop->collisionbox;
247 getboolfield(L, -1, "pointable", prop->pointable);
248 getstringfield(L, -1, "visual", prop->visual);
250 getstringfield(L, -1, "mesh", prop->mesh);
252 lua_getfield(L, -1, "visual_size");
253 if (lua_istable(L, -1)) {
254 // Backwards compatibility: Also accept { x = ?, y = ? }
255 v2f scale_xy = read_v2f(L, -1);
257 f32 scale_z = scale_xy.X;
258 lua_getfield(L, -1, "z");
259 if (lua_isnumber(L, -1))
260 scale_z = lua_tonumber(L, -1);
263 prop->visual_size = v3f(scale_xy.X, scale_xy.Y, scale_z);
267 lua_getfield(L, -1, "textures");
268 if(lua_istable(L, -1)){
269 prop->textures.clear();
270 int table = lua_gettop(L);
272 while(lua_next(L, table) != 0){
273 // key at index -2 and value at index -1
274 if(lua_isstring(L, -1))
275 prop->textures.emplace_back(lua_tostring(L, -1));
277 prop->textures.emplace_back("");
278 // removes value, keeps key for next iteration
284 lua_getfield(L, -1, "colors");
285 if (lua_istable(L, -1)) {
286 int table = lua_gettop(L);
287 prop->colors.clear();
288 for (lua_pushnil(L); lua_next(L, table); lua_pop(L, 1)) {
289 video::SColor color(255, 255, 255, 255);
290 read_color(L, -1, &color);
291 prop->colors.push_back(color);
296 lua_getfield(L, -1, "spritediv");
297 if(lua_istable(L, -1))
298 prop->spritediv = read_v2s16(L, -1);
301 lua_getfield(L, -1, "initial_sprite_basepos");
302 if(lua_istable(L, -1))
303 prop->initial_sprite_basepos = read_v2s16(L, -1);
306 getboolfield(L, -1, "is_visible", prop->is_visible);
307 getboolfield(L, -1, "makes_footstep_sound", prop->makes_footstep_sound);
308 if (getfloatfield(L, -1, "stepheight", prop->stepheight))
309 prop->stepheight *= BS;
310 getfloatfield(L, -1, "eye_height", prop->eye_height);
312 getfloatfield(L, -1, "automatic_rotate", prop->automatic_rotate);
313 lua_getfield(L, -1, "automatic_face_movement_dir");
314 if (lua_isnumber(L, -1)) {
315 prop->automatic_face_movement_dir = true;
316 prop->automatic_face_movement_dir_offset = luaL_checknumber(L, -1);
317 } else if (lua_isboolean(L, -1)) {
318 prop->automatic_face_movement_dir = lua_toboolean(L, -1);
319 prop->automatic_face_movement_dir_offset = 0.0;
322 getboolfield(L, -1, "backface_culling", prop->backface_culling);
323 getintfield(L, -1, "glow", prop->glow);
325 getstringfield(L, -1, "nametag", prop->nametag);
326 lua_getfield(L, -1, "nametag_color");
327 if (!lua_isnil(L, -1)) {
328 video::SColor color = prop->nametag_color;
329 if (read_color(L, -1, &color))
330 prop->nametag_color = color;
333 lua_getfield(L, -1, "nametag_bgcolor");
334 if (!lua_isnil(L, -1)) {
335 if (lua_toboolean(L, -1)) {
337 if (read_color(L, -1, &color))
338 prop->nametag_bgcolor = color;
340 prop->nametag_bgcolor = nullopt;
345 lua_getfield(L, -1, "automatic_face_movement_max_rotation_per_sec");
346 if (lua_isnumber(L, -1)) {
347 prop->automatic_face_movement_max_rotation_per_sec = luaL_checknumber(L, -1);
351 getstringfield(L, -1, "infotext", prop->infotext);
352 getboolfield(L, -1, "static_save", prop->static_save);
354 lua_getfield(L, -1, "wield_item");
355 if (!lua_isnil(L, -1))
356 prop->wield_item = read_item(L, -1, idef).getItemString();
359 getfloatfield(L, -1, "zoom_fov", prop->zoom_fov);
360 getboolfield(L, -1, "use_texture_alpha", prop->use_texture_alpha);
361 getboolfield(L, -1, "shaded", prop->shaded);
362 getboolfield(L, -1, "show_on_minimap", prop->show_on_minimap);
364 getstringfield(L, -1, "damage_texture_modifier", prop->damage_texture_modifier);
367 /******************************************************************************/
368 void push_object_properties(lua_State *L, ObjectProperties *prop)
371 lua_pushnumber(L, prop->hp_max);
372 lua_setfield(L, -2, "hp_max");
373 lua_pushnumber(L, prop->breath_max);
374 lua_setfield(L, -2, "breath_max");
375 lua_pushboolean(L, prop->physical);
376 lua_setfield(L, -2, "physical");
377 lua_pushboolean(L, prop->collideWithObjects);
378 lua_setfield(L, -2, "collide_with_objects");
379 push_aabb3f(L, prop->collisionbox);
380 lua_setfield(L, -2, "collisionbox");
381 push_aabb3f(L, prop->selectionbox);
382 lua_pushboolean(L, prop->rotate_selectionbox);
383 lua_setfield(L, -2, "rotate");
384 lua_setfield(L, -2, "selectionbox");
385 lua_pushboolean(L, prop->pointable);
386 lua_setfield(L, -2, "pointable");
387 lua_pushlstring(L, prop->visual.c_str(), prop->visual.size());
388 lua_setfield(L, -2, "visual");
389 lua_pushlstring(L, prop->mesh.c_str(), prop->mesh.size());
390 lua_setfield(L, -2, "mesh");
391 push_v3f(L, prop->visual_size);
392 lua_setfield(L, -2, "visual_size");
394 lua_createtable(L, prop->textures.size(), 0);
396 for (const std::string &texture : prop->textures) {
397 lua_pushlstring(L, texture.c_str(), texture.size());
398 lua_rawseti(L, -2, i++);
400 lua_setfield(L, -2, "textures");
402 lua_createtable(L, prop->colors.size(), 0);
404 for (const video::SColor &color : prop->colors) {
405 push_ARGB8(L, color);
406 lua_rawseti(L, -2, i++);
408 lua_setfield(L, -2, "colors");
410 push_v2s16(L, prop->spritediv);
411 lua_setfield(L, -2, "spritediv");
412 push_v2s16(L, prop->initial_sprite_basepos);
413 lua_setfield(L, -2, "initial_sprite_basepos");
414 lua_pushboolean(L, prop->is_visible);
415 lua_setfield(L, -2, "is_visible");
416 lua_pushboolean(L, prop->makes_footstep_sound);
417 lua_setfield(L, -2, "makes_footstep_sound");
418 lua_pushnumber(L, prop->stepheight / BS);
419 lua_setfield(L, -2, "stepheight");
420 lua_pushnumber(L, prop->eye_height);
421 lua_setfield(L, -2, "eye_height");
422 lua_pushnumber(L, prop->automatic_rotate);
423 lua_setfield(L, -2, "automatic_rotate");
424 if (prop->automatic_face_movement_dir)
425 lua_pushnumber(L, prop->automatic_face_movement_dir_offset);
427 lua_pushboolean(L, false);
428 lua_setfield(L, -2, "automatic_face_movement_dir");
429 lua_pushboolean(L, prop->backface_culling);
430 lua_setfield(L, -2, "backface_culling");
431 lua_pushnumber(L, prop->glow);
432 lua_setfield(L, -2, "glow");
433 lua_pushlstring(L, prop->nametag.c_str(), prop->nametag.size());
434 lua_setfield(L, -2, "nametag");
435 push_ARGB8(L, prop->nametag_color);
436 lua_setfield(L, -2, "nametag_color");
437 if (prop->nametag_bgcolor) {
438 push_ARGB8(L, prop->nametag_bgcolor.value());
439 lua_setfield(L, -2, "nametag_bgcolor");
441 lua_pushboolean(L, false);
442 lua_setfield(L, -2, "nametag_bgcolor");
444 lua_pushnumber(L, prop->automatic_face_movement_max_rotation_per_sec);
445 lua_setfield(L, -2, "automatic_face_movement_max_rotation_per_sec");
446 lua_pushlstring(L, prop->infotext.c_str(), prop->infotext.size());
447 lua_setfield(L, -2, "infotext");
448 lua_pushboolean(L, prop->static_save);
449 lua_setfield(L, -2, "static_save");
450 lua_pushlstring(L, prop->wield_item.c_str(), prop->wield_item.size());
451 lua_setfield(L, -2, "wield_item");
452 lua_pushnumber(L, prop->zoom_fov);
453 lua_setfield(L, -2, "zoom_fov");
454 lua_pushboolean(L, prop->use_texture_alpha);
455 lua_setfield(L, -2, "use_texture_alpha");
456 lua_pushboolean(L, prop->shaded);
457 lua_setfield(L, -2, "shaded");
458 lua_pushlstring(L, prop->damage_texture_modifier.c_str(), prop->damage_texture_modifier.size());
459 lua_setfield(L, -2, "damage_texture_modifier");
460 lua_pushboolean(L, prop->show_on_minimap);
461 lua_setfield(L, -2, "show_on_minimap");
464 /******************************************************************************/
465 TileDef read_tiledef(lua_State *L, int index, u8 drawtype, bool special)
468 index = lua_gettop(L) + 1 + index;
472 bool default_tiling = true;
473 bool default_culling = true;
477 default_tiling = false;
478 // "break" is omitted here intentionaly, as PLANTLIKE
479 // FIRELIKE drawtype both should default to having
480 // backface_culling to false.
483 default_culling = false;
485 case NDT_PLANTLIKE_ROOTED:
486 default_tiling = !special;
487 default_culling = !special;
493 // key at index -2 and value at index
494 if(lua_isstring(L, index)){
495 // "default_lava.png"
496 tiledef.name = lua_tostring(L, index);
497 tiledef.tileable_vertical = default_tiling;
498 tiledef.tileable_horizontal = default_tiling;
499 tiledef.backface_culling = default_culling;
501 else if(lua_istable(L, index))
503 // name="default_lava.png"
504 tiledef.name.clear();
505 getstringfield(L, index, "name", tiledef.name);
506 getstringfield(L, index, "image", tiledef.name); // MaterialSpec compat.
507 tiledef.backface_culling = getboolfield_default(
508 L, index, "backface_culling", default_culling);
509 tiledef.tileable_horizontal = getboolfield_default(
510 L, index, "tileable_horizontal", default_tiling);
511 tiledef.tileable_vertical = getboolfield_default(
512 L, index, "tileable_vertical", default_tiling);
513 std::string align_style;
514 if (getstringfield(L, index, "align_style", align_style)) {
515 if (align_style == "user")
516 tiledef.align_style = ALIGN_STYLE_USER_DEFINED;
517 else if (align_style == "world")
518 tiledef.align_style = ALIGN_STYLE_WORLD;
520 tiledef.align_style = ALIGN_STYLE_NODE;
522 tiledef.scale = getintfield_default(L, index, "scale", 0);
524 lua_getfield(L, index, "color");
525 tiledef.has_color = read_color(L, -1, &tiledef.color);
528 lua_getfield(L, index, "animation");
529 tiledef.animation = read_animation_definition(L, -1);
536 /******************************************************************************/
537 void read_content_features(lua_State *L, ContentFeatures &f, int index)
540 index = lua_gettop(L) + 1 + index;
542 /* Cache existence of some callbacks */
543 lua_getfield(L, index, "on_construct");
544 if(!lua_isnil(L, -1)) f.has_on_construct = true;
546 lua_getfield(L, index, "on_destruct");
547 if(!lua_isnil(L, -1)) f.has_on_destruct = true;
549 lua_getfield(L, index, "after_destruct");
550 if(!lua_isnil(L, -1)) f.has_after_destruct = true;
553 lua_getfield(L, index, "on_rightclick");
554 f.rightclickable = lua_isfunction(L, -1);
558 getstringfield(L, index, "name", f.name);
561 lua_getfield(L, index, "groups");
562 read_groups(L, -1, f.groups);
565 /* Visual definition */
567 f.drawtype = (NodeDrawType)getenumfield(L, index, "drawtype",
568 ScriptApiNode::es_DrawType,NDT_NORMAL);
569 getfloatfield(L, index, "visual_scale", f.visual_scale);
571 /* Meshnode model filename */
572 getstringfield(L, index, "mesh", f.mesh);
575 lua_getfield(L, index, "tiles");
576 if(lua_istable(L, -1)){
577 int table = lua_gettop(L);
580 while(lua_next(L, table) != 0){
581 // Read tiledef from value
582 f.tiledef[i] = read_tiledef(L, -1, f.drawtype, false);
583 // removes value, keeps key for next iteration
591 // Copy last value to all remaining textures
593 TileDef lasttile = f.tiledef[i-1];
595 f.tiledef[i] = lasttile;
602 // overlay_tiles = {}
603 lua_getfield(L, index, "overlay_tiles");
604 if (lua_istable(L, -1)) {
605 int table = lua_gettop(L);
608 while (lua_next(L, table) != 0) {
609 // Read tiledef from value
610 f.tiledef_overlay[i] = read_tiledef(L, -1, f.drawtype, false);
611 // removes value, keeps key for next iteration
619 // Copy last value to all remaining textures
621 TileDef lasttile = f.tiledef_overlay[i - 1];
623 f.tiledef_overlay[i] = lasttile;
630 // special_tiles = {}
631 lua_getfield(L, index, "special_tiles");
632 if(lua_istable(L, -1)){
633 int table = lua_gettop(L);
636 while(lua_next(L, table) != 0){
637 // Read tiledef from value
638 f.tiledef_special[i] = read_tiledef(L, -1, f.drawtype, true);
639 // removes value, keeps key for next iteration
642 if(i==CF_SPECIAL_COUNT){
650 /* alpha & use_texture_alpha */
651 // This is a bit complicated due to compatibility
653 f.setDefaultAlphaMode();
655 warn_if_field_exists(L, index, "alpha",
656 "Obsolete, only limited compatibility provided; "
657 "replaced by \"use_texture_alpha\"");
658 if (getintfield_default(L, index, "alpha", 255) != 255)
659 f.alpha = ALPHAMODE_BLEND;
661 lua_getfield(L, index, "use_texture_alpha");
662 if (lua_isboolean(L, -1)) {
663 warn_if_field_exists(L, index, "use_texture_alpha",
664 "Boolean values are deprecated; use the new choices");
665 if (lua_toboolean(L, -1))
666 f.alpha = (f.drawtype == NDT_NORMAL) ? ALPHAMODE_CLIP : ALPHAMODE_BLEND;
667 } else if (check_field_or_nil(L, -1, LUA_TSTRING, "use_texture_alpha")) {
668 int result = f.alpha;
669 string_to_enum(ScriptApiNode::es_TextureAlphaMode, result,
670 std::string(lua_tostring(L, -1)));
671 f.alpha = static_cast<enum AlphaMode>(result);
677 lua_getfield(L, index, "color");
678 read_color(L, -1, &f.color);
681 getstringfield(L, index, "palette", f.palette_name);
683 lua_getfield(L, index, "post_effect_color");
684 read_color(L, -1, &f.post_effect_color);
687 f.param_type = (ContentParamType)getenumfield(L, index, "paramtype",
688 ScriptApiNode::es_ContentParamType, CPT_NONE);
689 f.param_type_2 = (ContentParamType2)getenumfield(L, index, "paramtype2",
690 ScriptApiNode::es_ContentParamType2, CPT2_NONE);
692 if (!f.palette_name.empty() &&
693 !(f.param_type_2 == CPT2_COLOR ||
694 f.param_type_2 == CPT2_COLORED_FACEDIR ||
695 f.param_type_2 == CPT2_COLORED_WALLMOUNTED ||
696 f.param_type_2 == CPT2_COLORED_DEGROTATE ||
697 f.param_type_2 == CPT2_COLORED_4DIR))
698 warningstream << "Node " << f.name.c_str()
699 << " has a palette, but not a suitable paramtype2." << std::endl;
701 // True for all ground-like things like stone and mud, false for eg. trees
702 getboolfield(L, index, "is_ground_content", f.is_ground_content);
703 f.light_propagates = (f.param_type == CPT_LIGHT);
704 getboolfield(L, index, "sunlight_propagates", f.sunlight_propagates);
705 // This is used for collision detection.
706 // Also for general solidness queries.
707 getboolfield(L, index, "walkable", f.walkable);
708 // Player can point to these
709 getboolfield(L, index, "pointable", f.pointable);
710 // Player can dig these
711 getboolfield(L, index, "diggable", f.diggable);
712 // Player can climb these
713 getboolfield(L, index, "climbable", f.climbable);
714 // Player can build on these
715 getboolfield(L, index, "buildable_to", f.buildable_to);
716 // Liquids flow into and replace node
717 getboolfield(L, index, "floodable", f.floodable);
718 // Whether the node is non-liquid, source liquid or flowing liquid
719 f.liquid_type = (LiquidType)getenumfield(L, index, "liquidtype",
720 ScriptApiNode::es_LiquidType, LIQUID_NONE);
721 // If the content is liquid, this is the flowing version of the liquid.
722 getstringfield(L, index, "liquid_alternative_flowing",
723 f.liquid_alternative_flowing);
724 // If the content is liquid, this is the source version of the liquid.
725 getstringfield(L, index, "liquid_alternative_source",
726 f.liquid_alternative_source);
727 // Viscosity for fluid flow, ranging from 1 to 7, with
728 // 1 giving almost instantaneous propagation and 7 being
729 // the slowest possible
730 f.liquid_viscosity = getintfield_default(L, index,
731 "liquid_viscosity", f.liquid_viscosity);
732 // If move_resistance is not set explicitly,
733 // move_resistance is equal to liquid_viscosity
734 f.move_resistance = f.liquid_viscosity;
735 f.liquid_range = getintfield_default(L, index,
736 "liquid_range", f.liquid_range);
737 f.leveled = getintfield_default(L, index, "leveled", f.leveled);
738 f.leveled_max = getintfield_default(L, index,
739 "leveled_max", f.leveled_max);
741 getboolfield(L, index, "liquid_renewable", f.liquid_renewable);
742 f.drowning = getintfield_default(L, index,
743 "drowning", f.drowning);
744 // Amount of light the node emits
745 f.light_source = getintfield_default(L, index,
746 "light_source", f.light_source);
747 if (f.light_source > LIGHT_MAX) {
748 warningstream << "Node " << f.name.c_str()
749 << " had greater light_source than " << LIGHT_MAX
750 << ", it was reduced." << std::endl;
751 f.light_source = LIGHT_MAX;
753 f.damage_per_second = getintfield_default(L, index,
754 "damage_per_second", f.damage_per_second);
756 lua_getfield(L, index, "node_box");
757 if(lua_istable(L, -1))
758 f.node_box = read_nodebox(L, -1);
761 lua_getfield(L, index, "connects_to");
762 if (lua_istable(L, -1)) {
763 int table = lua_gettop(L);
765 while (lua_next(L, table) != 0) {
767 f.connects_to.emplace_back(lua_tostring(L, -1));
773 lua_getfield(L, index, "connect_sides");
774 if (lua_istable(L, -1)) {
775 int table = lua_gettop(L);
777 while (lua_next(L, table) != 0) {
779 std::string side(lua_tostring(L, -1));
780 // Note faces are flipped to make checking easier
782 f.connect_sides |= 2;
783 else if (side == "bottom")
784 f.connect_sides |= 1;
785 else if (side == "front")
786 f.connect_sides |= 16;
787 else if (side == "left")
788 f.connect_sides |= 32;
789 else if (side == "back")
790 f.connect_sides |= 4;
791 else if (side == "right")
792 f.connect_sides |= 8;
794 warningstream << "Unknown value for \"connect_sides\": "
795 << side << std::endl;
801 lua_getfield(L, index, "selection_box");
802 if(lua_istable(L, -1))
803 f.selection_box = read_nodebox(L, -1);
806 lua_getfield(L, index, "collision_box");
807 if(lua_istable(L, -1))
808 f.collision_box = read_nodebox(L, -1);
811 f.waving = getintfield_default(L, index,
814 // Set to true if paramtype used to be 'facedir_simple'
815 getboolfield(L, index, "legacy_facedir_simple", f.legacy_facedir_simple);
816 // Set to true if wall_mounted used to be set to true
817 getboolfield(L, index, "legacy_wallmounted", f.legacy_wallmounted);
820 lua_getfield(L, index, "sounds");
821 if(lua_istable(L, -1)){
822 lua_getfield(L, -1, "footstep");
823 read_soundspec(L, -1, f.sound_footstep);
825 lua_getfield(L, -1, "dig");
826 read_soundspec(L, -1, f.sound_dig);
828 lua_getfield(L, -1, "dug");
829 read_soundspec(L, -1, f.sound_dug);
834 // Node immediately placed by client when node is dug
835 getstringfield(L, index, "node_dig_prediction",
836 f.node_dig_prediction);
838 // How much the node slows down players, ranging from 1 to 7,
839 // the higher, the slower.
840 f.move_resistance = getintfield_default(L, index,
841 "move_resistance", f.move_resistance);
843 // Whether e.g. players in this node will have liquid movement physics
844 lua_getfield(L, index, "liquid_move_physics");
845 if(lua_isboolean(L, -1)) {
846 f.liquid_move_physics = lua_toboolean(L, -1);
847 } else if(lua_isnil(L, -1)) {
848 f.liquid_move_physics = f.liquid_type != LIQUID_NONE;
850 errorstream << "Field \"liquid_move_physics\": Invalid type!" << std::endl;
855 void push_content_features(lua_State *L, const ContentFeatures &c)
857 std::string paramtype(ScriptApiNode::es_ContentParamType[(int)c.param_type].str);
858 std::string paramtype2(ScriptApiNode::es_ContentParamType2[(int)c.param_type_2].str);
859 std::string drawtype(ScriptApiNode::es_DrawType[(int)c.drawtype].str);
860 std::string liquid_type(ScriptApiNode::es_LiquidType[(int)c.liquid_type].str);
862 /* Missing "tiles" because I don't see a usecase (at least not yet). */
865 lua_pushboolean(L, c.has_on_construct);
866 lua_setfield(L, -2, "has_on_construct");
867 lua_pushboolean(L, c.has_on_destruct);
868 lua_setfield(L, -2, "has_on_destruct");
869 lua_pushboolean(L, c.has_after_destruct);
870 lua_setfield(L, -2, "has_after_destruct");
871 lua_pushstring(L, c.name.c_str());
872 lua_setfield(L, -2, "name");
873 push_groups(L, c.groups);
874 lua_setfield(L, -2, "groups");
875 lua_pushstring(L, paramtype.c_str());
876 lua_setfield(L, -2, "paramtype");
877 lua_pushstring(L, paramtype2.c_str());
878 lua_setfield(L, -2, "paramtype2");
879 lua_pushstring(L, drawtype.c_str());
880 lua_setfield(L, -2, "drawtype");
881 if (!c.mesh.empty()) {
882 lua_pushstring(L, c.mesh.c_str());
883 lua_setfield(L, -2, "mesh");
886 push_ARGB8(L, c.minimap_color); // I know this is not set-able w/ register_node,
887 lua_setfield(L, -2, "minimap_color"); // but the people need to know!
889 lua_pushnumber(L, c.visual_scale);
890 lua_setfield(L, -2, "visual_scale");
891 lua_pushnumber(L, c.alpha);
892 lua_setfield(L, -2, "alpha");
893 if (!c.palette_name.empty()) {
894 push_ARGB8(L, c.color);
895 lua_setfield(L, -2, "color");
897 lua_pushstring(L, c.palette_name.c_str());
898 lua_setfield(L, -2, "palette_name");
900 push_palette(L, c.palette);
901 lua_setfield(L, -2, "palette");
903 lua_pushnumber(L, c.waving);
904 lua_setfield(L, -2, "waving");
905 lua_pushnumber(L, c.connect_sides);
906 lua_setfield(L, -2, "connect_sides");
908 lua_createtable(L, c.connects_to.size(), 0);
910 for (const std::string &it : c.connects_to) {
911 lua_pushlstring(L, it.c_str(), it.size());
912 lua_rawseti(L, -2, i++);
914 lua_setfield(L, -2, "connects_to");
916 push_ARGB8(L, c.post_effect_color);
917 lua_setfield(L, -2, "post_effect_color");
918 lua_pushnumber(L, c.leveled);
919 lua_setfield(L, -2, "leveled");
920 lua_pushnumber(L, c.leveled_max);
921 lua_setfield(L, -2, "leveled_max");
922 lua_pushboolean(L, c.sunlight_propagates);
923 lua_setfield(L, -2, "sunlight_propagates");
924 lua_pushnumber(L, c.light_source);
925 lua_setfield(L, -2, "light_source");
926 lua_pushboolean(L, c.is_ground_content);
927 lua_setfield(L, -2, "is_ground_content");
928 lua_pushboolean(L, c.walkable);
929 lua_setfield(L, -2, "walkable");
930 lua_pushboolean(L, c.pointable);
931 lua_setfield(L, -2, "pointable");
932 lua_pushboolean(L, c.diggable);
933 lua_setfield(L, -2, "diggable");
934 lua_pushboolean(L, c.climbable);
935 lua_setfield(L, -2, "climbable");
936 lua_pushboolean(L, c.buildable_to);
937 lua_setfield(L, -2, "buildable_to");
938 lua_pushboolean(L, c.rightclickable);
939 lua_setfield(L, -2, "rightclickable");
940 lua_pushnumber(L, c.damage_per_second);
941 lua_setfield(L, -2, "damage_per_second");
943 lua_pushstring(L, liquid_type.c_str());
944 lua_setfield(L, -2, "liquid_type");
945 lua_pushstring(L, c.liquid_alternative_flowing.c_str());
946 lua_setfield(L, -2, "liquid_alternative_flowing");
947 lua_pushstring(L, c.liquid_alternative_source.c_str());
948 lua_setfield(L, -2, "liquid_alternative_source");
949 lua_pushnumber(L, c.liquid_viscosity);
950 lua_setfield(L, -2, "liquid_viscosity");
951 lua_pushboolean(L, c.liquid_renewable);
952 lua_setfield(L, -2, "liquid_renewable");
953 lua_pushnumber(L, c.liquid_range);
954 lua_setfield(L, -2, "liquid_range");
956 lua_pushnumber(L, c.drowning);
957 lua_setfield(L, -2, "drowning");
958 lua_pushboolean(L, c.floodable);
959 lua_setfield(L, -2, "floodable");
960 push_nodebox(L, c.node_box);
961 lua_setfield(L, -2, "node_box");
962 push_nodebox(L, c.selection_box);
963 lua_setfield(L, -2, "selection_box");
964 push_nodebox(L, c.collision_box);
965 lua_setfield(L, -2, "collision_box");
967 push_soundspec(L, c.sound_footstep);
968 lua_setfield(L, -2, "sound_footstep");
969 push_soundspec(L, c.sound_dig);
970 lua_setfield(L, -2, "sound_dig");
971 push_soundspec(L, c.sound_dug);
972 lua_setfield(L, -2, "sound_dug");
973 lua_setfield(L, -2, "sounds");
974 lua_pushboolean(L, c.legacy_facedir_simple);
975 lua_setfield(L, -2, "legacy_facedir_simple");
976 lua_pushboolean(L, c.legacy_wallmounted);
977 lua_setfield(L, -2, "legacy_wallmounted");
978 lua_pushstring(L, c.node_dig_prediction.c_str());
979 lua_setfield(L, -2, "node_dig_prediction");
980 lua_pushnumber(L, c.move_resistance);
981 lua_setfield(L, -2, "move_resistance");
982 lua_pushboolean(L, c.liquid_move_physics);
983 lua_setfield(L, -2, "liquid_move_physics");
986 /******************************************************************************/
987 void push_nodebox(lua_State *L, const NodeBox &box)
992 case NODEBOX_REGULAR:
993 lua_pushstring(L, "regular");
994 lua_setfield(L, -2, "type");
996 case NODEBOX_LEVELED:
998 lua_pushstring(L, "fixed");
999 lua_setfield(L, -2, "type");
1000 push_box(L, box.fixed);
1001 lua_setfield(L, -2, "fixed");
1003 case NODEBOX_WALLMOUNTED:
1004 lua_pushstring(L, "wallmounted");
1005 lua_setfield(L, -2, "type");
1006 push_aabb3f(L, box.wall_top);
1007 lua_setfield(L, -2, "wall_top");
1008 push_aabb3f(L, box.wall_bottom);
1009 lua_setfield(L, -2, "wall_bottom");
1010 push_aabb3f(L, box.wall_side);
1011 lua_setfield(L, -2, "wall_side");
1013 case NODEBOX_CONNECTED: {
1014 lua_pushstring(L, "connected");
1015 lua_setfield(L, -2, "type");
1016 const auto &c = box.getConnected();
1017 push_box(L, c.connect_top);
1018 lua_setfield(L, -2, "connect_top");
1019 push_box(L, c.connect_bottom);
1020 lua_setfield(L, -2, "connect_bottom");
1021 push_box(L, c.connect_front);
1022 lua_setfield(L, -2, "connect_front");
1023 push_box(L, c.connect_back);
1024 lua_setfield(L, -2, "connect_back");
1025 push_box(L, c.connect_left);
1026 lua_setfield(L, -2, "connect_left");
1027 push_box(L, c.connect_right);
1028 lua_setfield(L, -2, "connect_right");
1029 // half the boxes are missing here?
1033 FATAL_ERROR("Invalid box.type");
1038 void push_box(lua_State *L, const std::vector<aabb3f> &box)
1040 lua_createtable(L, box.size(), 0);
1042 for (const aabb3f &it : box) {
1044 lua_rawseti(L, -2, i++);
1048 /******************************************************************************/
1049 void push_palette(lua_State *L, const std::vector<video::SColor> *palette)
1051 lua_createtable(L, palette->size(), 0);
1052 int newTable = lua_gettop(L);
1054 std::vector<video::SColor>::const_iterator iter;
1055 for (iter = palette->begin(); iter != palette->end(); ++iter) {
1056 push_ARGB8(L, (*iter));
1057 lua_rawseti(L, newTable, index);
1062 /******************************************************************************/
1063 void read_server_sound_params(lua_State *L, int index,
1064 ServerPlayingSound ¶ms)
1067 index = lua_gettop(L) + 1 + index;
1069 if(lua_istable(L, index)){
1070 // Functional overlap: this may modify SimpleSoundSpec contents
1071 getfloatfield(L, index, "fade", params.spec.fade);
1072 getfloatfield(L, index, "pitch", params.spec.pitch);
1073 getboolfield(L, index, "loop", params.spec.loop);
1075 getfloatfield(L, index, "gain", params.gain);
1077 // Handle positional information
1078 getstringfield(L, index, "to_player", params.to_player);
1079 lua_getfield(L, index, "pos");
1080 if(!lua_isnil(L, -1)){
1081 v3f p = read_v3f(L, -1)*BS;
1083 params.type = SoundLocation::Position;
1086 lua_getfield(L, index, "object");
1087 if(!lua_isnil(L, -1)){
1088 ObjectRef *ref = ModApiBase::checkObject<ObjectRef>(L, -1);
1089 ServerActiveObject *sao = ObjectRef::getobject(ref);
1091 params.object = sao->getId();
1092 params.type = SoundLocation::Object;
1096 params.max_hear_distance = BS*getfloatfield_default(L, index,
1097 "max_hear_distance", params.max_hear_distance/BS);
1098 getstringfield(L, index, "exclude_player", params.exclude_player);
1102 /******************************************************************************/
1103 void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
1106 index = lua_gettop(L) + 1 + index;
1107 if (lua_isnil(L, index))
1110 if (lua_istable(L, index)) {
1111 getstringfield(L, index, "name", spec.name);
1112 getfloatfield(L, index, "gain", spec.gain);
1113 getfloatfield(L, index, "fade", spec.fade);
1114 getfloatfield(L, index, "pitch", spec.pitch);
1115 } else if (lua_isstring(L, index)) {
1116 spec.name = lua_tostring(L, index);
1120 void push_soundspec(lua_State *L, const SimpleSoundSpec &spec)
1122 lua_createtable(L, 0, 3);
1123 lua_pushstring(L, spec.name.c_str());
1124 lua_setfield(L, -2, "name");
1125 lua_pushnumber(L, spec.gain);
1126 lua_setfield(L, -2, "gain");
1127 lua_pushnumber(L, spec.fade);
1128 lua_setfield(L, -2, "fade");
1129 lua_pushnumber(L, spec.pitch);
1130 lua_setfield(L, -2, "pitch");
1133 /******************************************************************************/
1134 NodeBox read_nodebox(lua_State *L, int index)
1137 if (lua_isnil(L, -1))
1140 luaL_checktype(L, -1, LUA_TTABLE);
1142 nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
1143 ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
1145 #define NODEBOXREAD(n, s){ \
1146 lua_getfield(L, index, (s)); \
1147 if (lua_istable(L, -1)) \
1148 (n) = read_aabb3f(L, -1, BS); \
1152 #define NODEBOXREADVEC(n, s) \
1153 lua_getfield(L, index, (s)); \
1154 if (lua_istable(L, -1)) \
1155 (n) = read_aabb3f_vector(L, -1, BS); \
1158 NODEBOXREADVEC(nodebox.fixed, "fixed");
1159 NODEBOXREAD(nodebox.wall_top, "wall_top");
1160 NODEBOXREAD(nodebox.wall_bottom, "wall_bottom");
1161 NODEBOXREAD(nodebox.wall_side, "wall_side");
1163 if (nodebox.type == NODEBOX_CONNECTED) {
1164 auto &c = nodebox.getConnected();
1165 NODEBOXREADVEC(c.connect_top, "connect_top");
1166 NODEBOXREADVEC(c.connect_bottom, "connect_bottom");
1167 NODEBOXREADVEC(c.connect_front, "connect_front");
1168 NODEBOXREADVEC(c.connect_left, "connect_left");
1169 NODEBOXREADVEC(c.connect_back, "connect_back");
1170 NODEBOXREADVEC(c.connect_right, "connect_right");
1171 NODEBOXREADVEC(c.disconnected_top, "disconnected_top");
1172 NODEBOXREADVEC(c.disconnected_bottom, "disconnected_bottom");
1173 NODEBOXREADVEC(c.disconnected_front, "disconnected_front");
1174 NODEBOXREADVEC(c.disconnected_left, "disconnected_left");
1175 NODEBOXREADVEC(c.disconnected_back, "disconnected_back");
1176 NODEBOXREADVEC(c.disconnected_right, "disconnected_right");
1177 NODEBOXREADVEC(c.disconnected, "disconnected");
1178 NODEBOXREADVEC(c.disconnected_sides, "disconnected_sides");
1184 /******************************************************************************/
1185 MapNode readnode(lua_State *L, int index)
1187 lua_pushvalue(L, index);
1188 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_NODE);
1191 content_t content = lua_tointeger(L, -3);
1192 u8 param1 = lua_tointeger(L, -2);
1193 u8 param2 = lua_tointeger(L, -1);
1195 return MapNode(content, param1, param2);
1198 /******************************************************************************/
1199 void pushnode(lua_State *L, const MapNode &n)
1201 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_NODE);
1202 lua_pushinteger(L, n.getContent());
1203 lua_pushinteger(L, n.getParam1());
1204 lua_pushinteger(L, n.getParam2());
1208 /******************************************************************************/
1209 void warn_if_field_exists(lua_State *L, int table,
1210 const char *name, const std::string &message)
1212 lua_getfield(L, table, name);
1213 if (!lua_isnil(L, -1)) {
1214 warningstream << "Field \"" << name << "\": "
1215 << message << std::endl;
1216 infostream << script_get_backtrace(L) << std::endl;
1221 /******************************************************************************/
1222 int getenumfield(lua_State *L, int table,
1223 const char *fieldname, const EnumString *spec, int default_)
1225 int result = default_;
1226 string_to_enum(spec, result,
1227 getstringfield_default(L, table, fieldname, ""));
1231 /******************************************************************************/
1232 bool string_to_enum(const EnumString *spec, int &result,
1233 const std::string &str)
1235 const EnumString *esp = spec;
1237 if (!strcmp(str.c_str(), esp->str)) {
1246 /******************************************************************************/
1247 ItemStack read_item(lua_State* L, int index, IItemDefManager *idef)
1250 index = lua_gettop(L) + 1 + index;
1252 if (lua_isnil(L, index)) {
1256 if (lua_isuserdata(L, index)) {
1257 // Convert from LuaItemStack
1258 LuaItemStack *o = ModApiBase::checkObject<LuaItemStack>(L, index);
1259 return o->getItem();
1262 if (lua_isstring(L, index)) {
1263 // Convert from itemstring
1264 std::string itemstring = lua_tostring(L, index);
1268 item.deSerialize(itemstring, idef);
1271 catch(SerializationError &e)
1273 warningstream<<"unable to create item from itemstring"
1274 <<": "<<itemstring<<std::endl;
1278 else if(lua_istable(L, index))
1280 // Convert from table
1281 std::string name = getstringfield_default(L, index, "name", "");
1282 int count = getintfield_default(L, index, "count", 1);
1283 int wear = getintfield_default(L, index, "wear", 0);
1285 ItemStack istack(name, count, wear, idef);
1287 // BACKWARDS COMPATIBLITY
1288 std::string value = getstringfield_default(L, index, "metadata", "");
1289 istack.metadata.setString("", value);
1292 lua_getfield(L, index, "meta");
1293 int fieldstable = lua_gettop(L);
1294 if (lua_istable(L, fieldstable)) {
1296 while (lua_next(L, fieldstable) != 0) {
1297 // key at index -2 and value at index -1
1298 std::string key = lua_tostring(L, -2);
1300 const char *value_cs = lua_tolstring(L, -1, &value_len);
1301 std::string value(value_cs, value_len);
1302 istack.metadata.setString(key, value);
1303 lua_pop(L, 1); // removes value, keeps key for next iteration
1309 throw LuaError("Expecting itemstack, itemstring, table or nil");
1313 /******************************************************************************/
1314 void push_tool_capabilities(lua_State *L,
1315 const ToolCapabilities &toolcap)
1318 setfloatfield(L, -1, "full_punch_interval", toolcap.full_punch_interval);
1319 setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
1320 setintfield(L, -1, "punch_attack_uses", toolcap.punch_attack_uses);
1321 // Create groupcaps table
1323 // For each groupcap
1324 for (const auto &gc_it : toolcap.groupcaps) {
1325 // Create groupcap table
1327 const std::string &name = gc_it.first;
1328 const ToolGroupCap &groupcap = gc_it.second;
1329 // Create subtable "times"
1331 for (auto time : groupcap.times) {
1332 lua_pushinteger(L, time.first);
1333 lua_pushnumber(L, time.second);
1334 lua_settable(L, -3);
1336 // Set subtable "times"
1337 lua_setfield(L, -2, "times");
1338 // Set simple parameters
1339 setintfield(L, -1, "maxlevel", groupcap.maxlevel);
1340 setintfield(L, -1, "uses", groupcap.uses);
1341 // Insert groupcap table into groupcaps table
1342 lua_setfield(L, -2, name.c_str());
1344 // Set groupcaps table
1345 lua_setfield(L, -2, "groupcaps");
1346 //Create damage_groups table
1348 // For each damage group
1349 for (const auto &damageGroup : toolcap.damageGroups) {
1350 // Create damage group table
1351 lua_pushinteger(L, damageGroup.second);
1352 lua_setfield(L, -2, damageGroup.first.c_str());
1354 lua_setfield(L, -2, "damage_groups");
1357 /******************************************************************************/
1358 void push_inventory_list(lua_State *L, const InventoryList &invlist)
1360 push_items(L, invlist.getItems());
1363 /******************************************************************************/
1364 void push_inventory_lists(lua_State *L, const Inventory &inv)
1366 const auto &lists = inv.getLists();
1367 lua_createtable(L, 0, lists.size());
1368 for(const InventoryList *list : lists) {
1369 const std::string &name = list->getName();
1370 lua_pushlstring(L, name.c_str(), name.size());
1371 push_inventory_list(L, *list);
1376 /******************************************************************************/
1377 void read_inventory_list(lua_State *L, int tableindex,
1378 Inventory *inv, const char *name, IGameDef *gdef, int forcesize)
1381 tableindex = lua_gettop(L) + 1 + tableindex;
1383 // If nil, delete list
1384 if(lua_isnil(L, tableindex)){
1385 inv->deleteList(name);
1389 // Get Lua-specified items to insert into the list
1390 std::vector<ItemStack> items = read_items(L, tableindex, gdef);
1391 size_t listsize = (forcesize >= 0) ? forcesize : items.size();
1393 // Create or resize/clear list
1394 InventoryList *invlist = inv->addList(name, listsize);
1396 luaL_error(L, "inventory list: cannot create list named '%s'", name);
1400 for (size_t i = 0; i < items.size(); ++i) {
1402 break; // Truncate provided list of items
1403 invlist->changeItem(i, items[i]);
1407 /******************************************************************************/
1408 struct TileAnimationParams read_animation_definition(lua_State *L, int index)
1411 index = lua_gettop(L) + 1 + index;
1413 struct TileAnimationParams anim;
1414 anim.type = TAT_NONE;
1415 if (!lua_istable(L, index))
1418 anim.type = (TileAnimationType)
1419 getenumfield(L, index, "type", es_TileAnimationType,
1421 if (anim.type == TAT_VERTICAL_FRAMES) {
1422 // {type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0}
1423 anim.vertical_frames.aspect_w =
1424 getintfield_default(L, index, "aspect_w", 16);
1425 anim.vertical_frames.aspect_h =
1426 getintfield_default(L, index, "aspect_h", 16);
1427 anim.vertical_frames.length =
1428 getfloatfield_default(L, index, "length", 1.0);
1429 } else if (anim.type == TAT_SHEET_2D) {
1430 // {type="sheet_2d", frames_w=5, frames_h=3, frame_length=0.5}
1431 getintfield(L, index, "frames_w",
1432 anim.sheet_2d.frames_w);
1433 getintfield(L, index, "frames_h",
1434 anim.sheet_2d.frames_h);
1435 getfloatfield(L, index, "frame_length",
1436 anim.sheet_2d.frame_length);
1442 /******************************************************************************/
1443 ToolCapabilities read_tool_capabilities(
1444 lua_State *L, int table)
1446 ToolCapabilities toolcap;
1447 getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
1448 getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
1449 getintfield(L, table, "punch_attack_uses", toolcap.punch_attack_uses);
1450 lua_getfield(L, table, "groupcaps");
1451 if(lua_istable(L, -1)){
1452 int table_groupcaps = lua_gettop(L);
1454 while(lua_next(L, table_groupcaps) != 0){
1455 // key at index -2 and value at index -1
1456 std::string groupname = luaL_checkstring(L, -2);
1457 if(lua_istable(L, -1)){
1458 int table_groupcap = lua_gettop(L);
1459 // This will be created
1460 ToolGroupCap groupcap;
1461 // Read simple parameters
1462 getintfield(L, table_groupcap, "maxlevel", groupcap.maxlevel);
1463 getintfield(L, table_groupcap, "uses", groupcap.uses);
1464 // DEPRECATED: maxwear
1466 if (getfloatfield(L, table_groupcap, "maxwear", maxwear)){
1468 groupcap.uses = 1.0/maxwear;
1471 warningstream << "Field \"maxwear\" is deprecated; "
1472 << "replace with uses=1/maxwear" << std::endl;
1473 infostream << script_get_backtrace(L) << std::endl;
1475 // Read "times" table
1476 lua_getfield(L, table_groupcap, "times");
1477 if(lua_istable(L, -1)){
1478 int table_times = lua_gettop(L);
1480 while(lua_next(L, table_times) != 0){
1481 // key at index -2 and value at index -1
1482 int rating = luaL_checkinteger(L, -2);
1483 float time = luaL_checknumber(L, -1);
1484 groupcap.times[rating] = time;
1485 // removes value, keeps key for next iteration
1490 // Insert groupcap into toolcap
1491 toolcap.groupcaps[groupname] = groupcap;
1493 // removes value, keeps key for next iteration
1499 lua_getfield(L, table, "damage_groups");
1500 if(lua_istable(L, -1)){
1501 int table_damage_groups = lua_gettop(L);
1503 while(lua_next(L, table_damage_groups) != 0){
1504 // key at index -2 and value at index -1
1505 std::string groupname = luaL_checkstring(L, -2);
1506 u16 value = luaL_checkinteger(L, -1);
1507 toolcap.damageGroups[groupname] = value;
1508 // removes value, keeps key for next iteration
1516 /******************************************************************************/
1517 void push_dig_params(lua_State *L,const DigParams ¶ms)
1519 lua_createtable(L, 0, 3);
1520 setboolfield(L, -1, "diggable", params.diggable);
1521 setfloatfield(L, -1, "time", params.time);
1522 setintfield(L, -1, "wear", params.wear);
1525 /******************************************************************************/
1526 void push_hit_params(lua_State *L,const HitParams ¶ms)
1528 lua_createtable(L, 0, 3);
1529 setintfield(L, -1, "hp", params.hp);
1530 setintfield(L, -1, "wear", params.wear);
1533 /******************************************************************************/
1535 bool getflagsfield(lua_State *L, int table, const char *fieldname,
1536 FlagDesc *flagdesc, u32 *flags, u32 *flagmask)
1538 lua_getfield(L, table, fieldname);
1540 bool success = read_flags(L, -1, flagdesc, flags, flagmask);
1547 bool read_flags(lua_State *L, int index, FlagDesc *flagdesc,
1548 u32 *flags, u32 *flagmask)
1550 if (lua_isstring(L, index)) {
1551 std::string flagstr = lua_tostring(L, index);
1552 *flags = readFlagString(flagstr, flagdesc, flagmask);
1553 } else if (lua_istable(L, index)) {
1554 *flags = read_flags_table(L, index, flagdesc, flagmask);
1562 u32 read_flags_table(lua_State *L, int table, FlagDesc *flagdesc, u32 *flagmask)
1564 u32 flags = 0, mask = 0;
1565 char fnamebuf[64] = "no";
1567 for (int i = 0; flagdesc[i].name; i++) {
1570 if (getboolfield(L, table, flagdesc[i].name, result)) {
1571 mask |= flagdesc[i].flag;
1573 flags |= flagdesc[i].flag;
1576 strlcpy(fnamebuf + 2, flagdesc[i].name, sizeof(fnamebuf) - 2);
1577 if (getboolfield(L, table, fnamebuf, result))
1578 mask |= flagdesc[i].flag;
1587 void push_flags_string(lua_State *L, FlagDesc *flagdesc, u32 flags, u32 flagmask)
1589 std::string flagstring = writeFlagString(flags, flagdesc, flagmask);
1590 lua_pushlstring(L, flagstring.c_str(), flagstring.size());
1593 /******************************************************************************/
1594 /* Lua Stored data! */
1595 /******************************************************************************/
1597 /******************************************************************************/
1598 void read_groups(lua_State *L, int index, ItemGroupList &result)
1600 if (lua_isnil(L, index))
1603 luaL_checktype(L, index, LUA_TTABLE);
1609 while (lua_next(L, index) != 0) {
1610 // key at index -2 and value at index -1
1611 std::string name = luaL_checkstring(L, -2);
1612 int rating = luaL_checkinteger(L, -1);
1613 // zero rating indicates not in the group
1615 result[name] = rating;
1616 // removes value, keeps key for next iteration
1621 /******************************************************************************/
1622 void push_groups(lua_State *L, const ItemGroupList &groups)
1624 lua_createtable(L, 0, groups.size());
1625 for (const auto &group : groups) {
1626 lua_pushinteger(L, group.second);
1627 lua_setfield(L, -2, group.first.c_str());
1631 /******************************************************************************/
1632 void push_items(lua_State *L, const std::vector<ItemStack> &items)
1634 lua_createtable(L, items.size(), 0);
1635 for (u32 i = 0; i != items.size(); i++) {
1636 LuaItemStack::create(L, items[i]);
1637 lua_rawseti(L, -2, i + 1);
1641 /******************************************************************************/
1642 std::vector<ItemStack> read_items(lua_State *L, int index, IGameDef *gdef)
1645 index = lua_gettop(L) + 1 + index;
1647 std::vector<ItemStack> items;
1648 luaL_checktype(L, index, LUA_TTABLE);
1650 while (lua_next(L, index)) {
1651 s32 key = luaL_checkinteger(L, -2);
1653 throw LuaError("Invalid inventory list index");
1655 if (items.size() < (u32) key) {
1658 items[key - 1] = read_item(L, -1, gdef->idef());
1664 /******************************************************************************/
1665 void luaentity_get(lua_State *L, u16 id)
1667 // Get luaentities[i]
1668 lua_getglobal(L, "core");
1669 lua_getfield(L, -1, "luaentities");
1670 luaL_checktype(L, -1, LUA_TTABLE);
1671 lua_pushinteger(L, id);
1672 lua_gettable(L, -2);
1673 lua_remove(L, -2); // Remove luaentities
1674 lua_remove(L, -2); // Remove core
1677 /******************************************************************************/
1678 bool read_noiseparams(lua_State *L, int index, NoiseParams *np)
1681 index = lua_gettop(L) + 1 + index;
1683 if (!lua_istable(L, index))
1686 getfloatfield(L, index, "offset", np->offset);
1687 getfloatfield(L, index, "scale", np->scale);
1688 getfloatfield(L, index, "persist", np->persist);
1689 getfloatfield(L, index, "persistence", np->persist);
1690 getfloatfield(L, index, "lacunarity", np->lacunarity);
1691 getintfield(L, index, "seed", np->seed);
1692 getintfield(L, index, "octaves", np->octaves);
1696 np->flags = getflagsfield(L, index, "flags", flagdesc_noiseparams,
1697 &flags, &flagmask) ? flags : NOISE_FLAG_DEFAULTS;
1699 lua_getfield(L, index, "spread");
1700 np->spread = read_v3f(L, -1);
1706 void push_noiseparams(lua_State *L, NoiseParams *np)
1709 setfloatfield(L, -1, "offset", np->offset);
1710 setfloatfield(L, -1, "scale", np->scale);
1711 setfloatfield(L, -1, "persist", np->persist);
1712 setfloatfield(L, -1, "persistence", np->persist);
1713 setfloatfield(L, -1, "lacunarity", np->lacunarity);
1714 setintfield( L, -1, "seed", np->seed);
1715 setintfield( L, -1, "octaves", np->octaves);
1717 push_flags_string(L, flagdesc_noiseparams, np->flags,
1719 lua_setfield(L, -2, "flags");
1721 push_v3f(L, np->spread);
1722 lua_setfield(L, -2, "spread");
1725 /******************************************************************************/
1726 // Returns depth of json value tree
1727 static int push_json_value_getdepth(const Json::Value &value)
1729 if (!value.isArray() && !value.isObject())
1733 for (const auto &it : value) {
1734 int elemdepth = push_json_value_getdepth(it);
1735 if (elemdepth > maxdepth)
1736 maxdepth = elemdepth;
1738 return maxdepth + 1;
1740 // Recursive function to convert JSON --> Lua table
1741 static bool push_json_value_helper(lua_State *L, const Json::Value &value,
1744 switch(value.type()) {
1745 case Json::nullValue:
1747 lua_pushvalue(L, nullindex);
1749 case Json::intValue:
1750 lua_pushinteger(L, value.asLargestInt());
1752 case Json::uintValue:
1753 lua_pushinteger(L, value.asLargestUInt());
1755 case Json::realValue:
1756 lua_pushnumber(L, value.asDouble());
1758 case Json::stringValue:
1760 const char *str = value.asCString();
1761 lua_pushstring(L, str ? str : "");
1764 case Json::booleanValue:
1765 lua_pushboolean(L, value.asInt());
1767 case Json::arrayValue:
1768 lua_createtable(L, value.size(), 0);
1769 for (Json::Value::const_iterator it = value.begin();
1770 it != value.end(); ++it) {
1771 push_json_value_helper(L, *it, nullindex);
1772 lua_rawseti(L, -2, it.index() + 1);
1775 case Json::objectValue:
1776 lua_createtable(L, 0, value.size());
1777 for (Json::Value::const_iterator it = value.begin();
1778 it != value.end(); ++it) {
1779 #if !defined(JSONCPP_STRING) && (JSONCPP_VERSION_MAJOR < 1 || JSONCPP_VERSION_MINOR < 9)
1780 const char *str = it.memberName();
1781 lua_pushstring(L, str ? str : "");
1783 std::string str = it.name();
1784 lua_pushstring(L, str.c_str());
1786 push_json_value_helper(L, *it, nullindex);
1793 // converts JSON --> Lua table; returns false if lua stack limit exceeded
1794 // nullindex: Lua stack index of value to use in place of JSON null
1795 bool push_json_value(lua_State *L, const Json::Value &value, int nullindex)
1798 nullindex = lua_gettop(L) + 1 + nullindex;
1800 int depth = push_json_value_getdepth(value);
1802 // The maximum number of Lua stack slots used at each recursion level
1803 // of push_json_value_helper is 2, so make sure there a depth * 2 slots
1804 if (lua_checkstack(L, depth * 2))
1805 return push_json_value_helper(L, value, nullindex);
1810 // Converts Lua table --> JSON
1811 void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion)
1813 if (recursion > 16) {
1814 throw SerializationError("Maximum recursion depth exceeded");
1816 int type = lua_type(L, index);
1817 if (type == LUA_TBOOLEAN) {
1818 root = (bool) lua_toboolean(L, index);
1819 } else if (type == LUA_TNUMBER) {
1820 root = lua_tonumber(L, index);
1821 } else if (type == LUA_TSTRING) {
1823 const char *str = lua_tolstring(L, index, &len);
1824 root = std::string(str, len);
1825 } else if (type == LUA_TTABLE) {
1827 while (lua_next(L, index)) {
1828 // Key is at -2 and value is at -1
1830 read_json_value(L, value, lua_gettop(L), recursion + 1);
1832 Json::ValueType roottype = root.type();
1833 int keytype = lua_type(L, -1);
1834 if (keytype == LUA_TNUMBER) {
1835 lua_Number key = lua_tonumber(L, -1);
1836 if (roottype != Json::nullValue && roottype != Json::arrayValue) {
1837 throw SerializationError("Can't mix array and object values in JSON");
1838 } else if (key < 1) {
1839 throw SerializationError("Can't use zero-based or negative indexes in JSON");
1840 } else if (floor(key) != key) {
1841 throw SerializationError("Can't use indexes with a fractional part in JSON");
1843 root[(Json::ArrayIndex) key - 1] = value;
1844 } else if (keytype == LUA_TSTRING) {
1845 if (roottype != Json::nullValue && roottype != Json::objectValue) {
1846 throw SerializationError("Can't mix array and object values in JSON");
1848 root[lua_tostring(L, -1)] = value;
1850 throw SerializationError("Lua key to convert to JSON is not a string or number");
1853 } else if (type == LUA_TNIL) {
1854 root = Json::nullValue;
1856 throw SerializationError("Can only store booleans, numbers, strings, objects, arrays, and null in JSON");
1858 lua_pop(L, 1); // Pop value
1861 void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm,
1865 if (pointed.type == POINTEDTHING_NODE) {
1866 lua_pushstring(L, "node");
1867 lua_setfield(L, -2, "type");
1868 push_v3s16(L, pointed.node_undersurface);
1869 lua_setfield(L, -2, "under");
1870 push_v3s16(L, pointed.node_abovesurface);
1871 lua_setfield(L, -2, "above");
1872 } else if (pointed.type == POINTEDTHING_OBJECT) {
1873 lua_pushstring(L, "object");
1874 lua_setfield(L, -2, "type");
1877 lua_pushinteger(L, pointed.object_id);
1878 lua_setfield(L, -2, "id");
1880 push_objectRef(L, pointed.object_id);
1881 lua_setfield(L, -2, "ref");
1884 lua_pushstring(L, "nothing");
1885 lua_setfield(L, -2, "type");
1887 if (hitpoint && (pointed.type != POINTEDTHING_NOTHING)) {
1888 push_v3f(L, pointed.intersection_point / BS); // convert to node coords
1889 lua_setfield(L, -2, "intersection_point");
1890 push_v3f(L, pointed.intersection_normal);
1891 lua_setfield(L, -2, "intersection_normal");
1892 lua_pushinteger(L, pointed.box_id + 1); // change to Lua array index
1893 lua_setfield(L, -2, "box_id");
1897 void push_objectRef(lua_State *L, const u16 id)
1899 // Get core.object_refs[i]
1900 lua_getglobal(L, "core");
1901 lua_getfield(L, -1, "object_refs");
1902 luaL_checktype(L, -1, LUA_TTABLE);
1903 lua_pushinteger(L, id);
1904 lua_gettable(L, -2);
1905 lua_remove(L, -2); // object_refs
1906 lua_remove(L, -2); // core
1909 void read_hud_element(lua_State *L, HudElement *elem)
1911 elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
1912 es_HudElementType, HUD_ELEM_TEXT);
1914 lua_getfield(L, 2, "position");
1915 elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1918 lua_getfield(L, 2, "scale");
1919 elem->scale = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1922 lua_getfield(L, 2, "size");
1923 elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
1926 elem->name = getstringfield_default(L, 2, "name", "");
1927 elem->text = getstringfield_default(L, 2, "text", "");
1928 elem->number = getintfield_default(L, 2, "number", 0);
1929 if (elem->type == HUD_ELEM_WAYPOINT)
1930 // waypoints reuse the item field to store precision, item = precision + 1
1931 elem->item = getintfield_default(L, 2, "precision", -1) + 1;
1933 elem->item = getintfield_default(L, 2, "item", 0);
1934 elem->dir = getintfield_default(L, 2, "direction", 0);
1935 elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX,
1936 getintfield_default(L, 2, "z_index", 0)));
1937 elem->text2 = getstringfield_default(L, 2, "text2", "");
1939 // Deprecated, only for compatibility's sake
1941 elem->dir = getintfield_default(L, 2, "dir", 0);
1943 lua_getfield(L, 2, "alignment");
1944 elem->align = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1947 lua_getfield(L, 2, "offset");
1948 elem->offset = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
1951 lua_getfield(L, 2, "world_pos");
1952 elem->world_pos = lua_istable(L, -1) ? read_v3f(L, -1) : v3f();
1955 elem->style = getintfield_default(L, 2, "style", 0);
1957 /* check for known deprecated element usage */
1958 if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32()))
1959 log_deprecated(L,"Deprecated usage of statbar without size!");
1962 void push_hud_element(lua_State *L, HudElement *elem)
1966 lua_pushstring(L, es_HudElementType[(u8)elem->type].str);
1967 lua_setfield(L, -2, "type");
1969 push_v2f(L, elem->pos);
1970 lua_setfield(L, -2, "position");
1972 lua_pushstring(L, elem->name.c_str());
1973 lua_setfield(L, -2, "name");
1975 push_v2f(L, elem->scale);
1976 lua_setfield(L, -2, "scale");
1978 lua_pushstring(L, elem->text.c_str());
1979 lua_setfield(L, -2, "text");
1981 lua_pushnumber(L, elem->number);
1982 lua_setfield(L, -2, "number");
1984 if (elem->type == HUD_ELEM_WAYPOINT) {
1985 // waypoints reuse the item field to store precision, precision = item - 1
1986 lua_pushnumber(L, elem->item - 1);
1987 lua_setfield(L, -2, "precision");
1989 // push the item field for waypoints as well for backwards compatibility
1990 lua_pushnumber(L, elem->item);
1991 lua_setfield(L, -2, "item");
1993 lua_pushnumber(L, elem->dir);
1994 lua_setfield(L, -2, "direction");
1996 push_v2f(L, elem->offset);
1997 lua_setfield(L, -2, "offset");
1999 push_v2f(L, elem->align);
2000 lua_setfield(L, -2, "alignment");
2002 push_v2s32(L, elem->size);
2003 lua_setfield(L, -2, "size");
2005 // Deprecated, only for compatibility's sake
2006 lua_pushnumber(L, elem->dir);
2007 lua_setfield(L, -2, "dir");
2009 push_v3f(L, elem->world_pos);
2010 lua_setfield(L, -2, "world_pos");
2012 lua_pushnumber(L, elem->z_index);
2013 lua_setfield(L, -2, "z_index");
2015 lua_pushstring(L, elem->text2.c_str());
2016 lua_setfield(L, -2, "text2");
2018 lua_pushinteger(L, elem->style);
2019 lua_setfield(L, -2, "style");
2022 bool read_hud_change(lua_State *L, HudElementStat &stat, HudElement *elem, void **value)
2024 std::string statstr = lua_tostring(L, 3);
2027 if (!string_to_enum(es_HudElementStat, statint, statstr)) {
2028 script_log_unique(L, "Unknown HUD stat type: " + statstr, warningstream);
2032 stat = (HudElementStat)statint;
2037 elem->pos = read_v2f(L, 4);
2038 *value = &elem->pos;
2041 elem->name = luaL_checkstring(L, 4);
2042 *value = &elem->name;
2044 case HUD_STAT_SCALE:
2045 elem->scale = read_v2f(L, 4);
2046 *value = &elem->scale;
2049 elem->text = luaL_checkstring(L, 4);
2050 *value = &elem->text;
2052 case HUD_STAT_NUMBER:
2053 elem->number = luaL_checknumber(L, 4);
2054 *value = &elem->number;
2057 elem->item = luaL_checknumber(L, 4);
2058 if (elem->type == HUD_ELEM_WAYPOINT && statstr == "precision")
2060 *value = &elem->item;
2063 elem->dir = luaL_checknumber(L, 4);
2064 *value = &elem->dir;
2066 case HUD_STAT_ALIGN:
2067 elem->align = read_v2f(L, 4);
2068 *value = &elem->align;
2070 case HUD_STAT_OFFSET:
2071 elem->offset = read_v2f(L, 4);
2072 *value = &elem->offset;
2074 case HUD_STAT_WORLD_POS:
2075 elem->world_pos = read_v3f(L, 4);
2076 *value = &elem->world_pos;
2079 elem->size = read_v2s32(L, 4);
2080 *value = &elem->size;
2082 case HUD_STAT_Z_INDEX:
2083 elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX, luaL_checknumber(L, 4)));
2084 *value = &elem->z_index;
2086 case HUD_STAT_TEXT2:
2087 elem->text2 = luaL_checkstring(L, 4);
2088 *value = &elem->text2;
2090 case HUD_STAT_STYLE:
2091 elem->style = luaL_checknumber(L, 4);
2092 *value = &elem->style;
2099 /******************************************************************************/
2101 // Indices must match values in `enum CollisionType` exactly!!
2102 static const char *collision_type_str[] = {
2107 // Indices must match values in `enum CollisionAxis` exactly!!
2108 static const char *collision_axis_str[] = {
2114 void push_collision_move_result(lua_State *L, const collisionMoveResult &res)
2116 lua_createtable(L, 0, 4);
2118 setboolfield(L, -1, "touching_ground", res.touching_ground);
2119 setboolfield(L, -1, "collides", res.collides);
2120 setboolfield(L, -1, "standing_on_object", res.standing_on_object);
2123 lua_createtable(L, res.collisions.size(), 0);
2125 for (const auto &c : res.collisions) {
2126 lua_createtable(L, 0, 5);
2128 lua_pushstring(L, collision_type_str[c.type]);
2129 lua_setfield(L, -2, "type");
2131 assert(c.axis != COLLISION_AXIS_NONE);
2132 lua_pushstring(L, collision_axis_str[c.axis]);
2133 lua_setfield(L, -2, "axis");
2135 if (c.type == COLLISION_NODE) {
2136 push_v3s16(L, c.node_p);
2137 lua_setfield(L, -2, "node_pos");
2138 } else if (c.type == COLLISION_OBJECT) {
2139 push_objectRef(L, c.object->getId());
2140 lua_setfield(L, -2, "object");
2143 push_v3f(L, c.old_speed / BS);
2144 lua_setfield(L, -2, "old_velocity");
2146 push_v3f(L, c.new_speed / BS);
2147 lua_setfield(L, -2, "new_velocity");
2149 lua_rawseti(L, -2, i++);
2151 lua_setfield(L, -2, "collisions");
2156 void push_mod_spec(lua_State *L, const ModSpec &spec, bool include_unsatisfied)
2160 lua_pushstring(L, spec.name.c_str());
2161 lua_setfield(L, -2, "name");
2163 lua_pushstring(L, spec.author.c_str());
2164 lua_setfield(L, -2, "author");
2166 lua_pushinteger(L, spec.release);
2167 lua_setfield(L, -2, "release");
2169 lua_pushstring(L, spec.desc.c_str());
2170 lua_setfield(L, -2, "description");
2172 lua_pushstring(L, spec.path.c_str());
2173 lua_setfield(L, -2, "path");
2175 lua_pushstring(L, spec.virtual_path.c_str());
2176 lua_setfield(L, -2, "virtual_path");
2180 for (const auto &dep : spec.unsatisfied_depends) {
2181 lua_pushstring(L, dep.c_str());
2182 lua_rawseti(L, -2, i++);
2184 lua_setfield(L, -2, "unsatisfied_depends");