aboutsummaryrefslogtreecommitdiff
path: root/src/script/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/script/common')
-rw-r--r--src/script/common/c_content.cpp824
-rw-r--r--src/script/common/c_content.h188
-rw-r--r--src/script/common/c_converter.cpp150
-rw-r--r--src/script/common/c_converter.h136
-rw-r--r--src/script/common/c_internal.cpp24
-rw-r--r--src/script/common/c_internal.h35
-rw-r--r--src/script/common/c_types.cpp6
-rw-r--r--src/script/common/c_types.h6
8 files changed, 727 insertions, 642 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index 9e99d5673..7d4c1e748 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -38,21 +38,23 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "debug.h" // For FATAL_ERROR
#include <json/json.h>
-struct EnumString es_TileAnimationType[] = {
- {TAT_NONE, "none"},
- {TAT_VERTICAL_FRAMES, "vertical_frames"},
- {TAT_SHEET_2D, "sheet_2d"},
- {0, NULL},
+struct EnumString es_TileAnimationType[] =
+{
+ {TAT_NONE, "none"},
+ {TAT_VERTICAL_FRAMES, "vertical_frames"},
+ {TAT_SHEET_2D, "sheet_2d"},
+ {0, NULL},
};
/******************************************************************************/
-void read_item_definition(lua_State *L, int index, const ItemDefinition &default_def,
- ItemDefinition &def)
+void read_item_definition(lua_State* L, int index,
+ const ItemDefinition &default_def, ItemDefinition &def)
{
if (index < 0)
index = lua_gettop(L) + 1 + index;
- def.type = (ItemType)getenumfield(L, index, "type", es_ItemType, ITEM_NONE);
+ def.type = (ItemType)getenumfield(L, index, "type",
+ es_ItemType, ITEM_NONE);
getstringfield(L, index, "name", def.name);
getstringfield(L, index, "description", def.description);
getstringfield(L, index, "inventory_image", def.inventory_image);
@@ -67,7 +69,7 @@ void read_item_definition(lua_State *L, int index, const ItemDefinition &default
lua_pop(L, 1);
lua_getfield(L, index, "wield_scale");
- if (lua_istable(L, -1)) {
+ if(lua_istable(L, -1)){
def.wield_scale = check_v3f(L, -1);
}
lua_pop(L, 1);
@@ -85,15 +87,15 @@ void read_item_definition(lua_State *L, int index, const ItemDefinition &default
"Obsolete; use tool_capabilities");
lua_getfield(L, index, "tool_capabilities");
- if (lua_istable(L, -1)) {
- def.tool_capabilities =
- new ToolCapabilities(read_tool_capabilities(L, -1));
+ if(lua_istable(L, -1)){
+ def.tool_capabilities = new ToolCapabilities(
+ read_tool_capabilities(L, -1));
}
// If name is "" (hand), ensure there are ToolCapabilities
// because it will be looked up there whenever any other item has
// no ToolCapabilities
- if (def.name.empty() && def.tool_capabilities == NULL) {
+ if (def.name.empty() && def.tool_capabilities == NULL){
def.tool_capabilities = new ToolCapabilities();
}
@@ -180,10 +182,10 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i)
}
/******************************************************************************/
-void read_object_properties(lua_State *L, int index, ServerActiveObject *sao,
- ObjectProperties *prop, IItemDefManager *idef)
+void read_object_properties(lua_State *L, int index,
+ ServerActiveObject *sao, ObjectProperties *prop, IItemDefManager *idef)
{
- if (index < 0)
+ if(index < 0)
index = lua_gettop(L) + 1 + index;
if (lua_isnil(L, index))
return;
@@ -198,8 +200,7 @@ void read_object_properties(lua_State *L, int index, ServerActiveObject *sao,
PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
sao->setHP(prop->hp_max, reason);
if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER)
- sao->getEnv()->getGameDef()->SendPlayerHPOrDie(
- (PlayerSAO *)sao, reason);
+ sao->getEnv()->getGameDef()->SendPlayerHPOrDie((PlayerSAO *)sao, reason);
}
}
@@ -247,13 +248,13 @@ void read_object_properties(lua_State *L, int index, ServerActiveObject *sao,
lua_pop(L, 1);
lua_getfield(L, -1, "textures");
- if (lua_istable(L, -1)) {
+ if(lua_istable(L, -1)){
prop->textures.clear();
int table = lua_gettop(L);
lua_pushnil(L);
- while (lua_next(L, table) != 0) {
+ while(lua_next(L, table) != 0){
// key at index -2 and value at index -1
- if (lua_isstring(L, -1))
+ if(lua_isstring(L, -1))
prop->textures.emplace_back(lua_tostring(L, -1));
else
prop->textures.emplace_back("");
@@ -276,12 +277,12 @@ void read_object_properties(lua_State *L, int index, ServerActiveObject *sao,
lua_pop(L, 1);
lua_getfield(L, -1, "spritediv");
- if (lua_istable(L, -1))
+ if(lua_istable(L, -1))
prop->spritediv = read_v2s16(L, -1);
lua_pop(L, 1);
lua_getfield(L, -1, "initial_sprite_basepos");
- if (lua_istable(L, -1))
+ if(lua_istable(L, -1))
prop->initial_sprite_basepos = read_v2s16(L, -1);
lua_pop(L, 1);
@@ -315,8 +316,7 @@ void read_object_properties(lua_State *L, int index, ServerActiveObject *sao,
lua_getfield(L, -1, "automatic_face_movement_max_rotation_per_sec");
if (lua_isnumber(L, -1)) {
- prop->automatic_face_movement_max_rotation_per_sec =
- luaL_checknumber(L, -1);
+ prop->automatic_face_movement_max_rotation_per_sec = luaL_checknumber(L, -1);
}
lua_pop(L, 1);
@@ -417,15 +417,14 @@ void push_object_properties(lua_State *L, ObjectProperties *prop)
lua_setfield(L, -2, "use_texture_alpha");
lua_pushboolean(L, prop->shaded);
lua_setfield(L, -2, "shaded");
- lua_pushlstring(L, prop->damage_texture_modifier.c_str(),
- prop->damage_texture_modifier.size());
+ lua_pushlstring(L, prop->damage_texture_modifier.c_str(), prop->damage_texture_modifier.size());
lua_setfield(L, -2, "damage_texture_modifier");
}
/******************************************************************************/
TileDef read_tiledef(lua_State *L, int index, u8 drawtype)
{
- if (index < 0)
+ if(index < 0)
index = lua_gettop(L) + 1 + index;
TileDef tiledef;
@@ -433,39 +432,41 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype)
bool default_tiling = true;
bool default_culling = true;
switch (drawtype) {
- case NDT_PLANTLIKE:
- case NDT_PLANTLIKE_ROOTED:
- case NDT_FIRELIKE:
- default_tiling = false;
- // "break" is omitted here intentionaly, as PLANTLIKE
- // FIRELIKE drawtype both should default to having
- // backface_culling to false.
- case NDT_MESH:
- case NDT_LIQUID:
- default_culling = false;
- break;
- default:
- break;
+ case NDT_PLANTLIKE:
+ case NDT_PLANTLIKE_ROOTED:
+ case NDT_FIRELIKE:
+ default_tiling = false;
+ // "break" is omitted here intentionaly, as PLANTLIKE
+ // FIRELIKE drawtype both should default to having
+ // backface_culling to false.
+ case NDT_MESH:
+ case NDT_LIQUID:
+ default_culling = false;
+ break;
+ default:
+ break;
}
// key at index -2 and value at index
- if (lua_isstring(L, index)) {
+ if(lua_isstring(L, index)){
// "default_lava.png"
tiledef.name = lua_tostring(L, index);
tiledef.tileable_vertical = default_tiling;
tiledef.tileable_horizontal = default_tiling;
tiledef.backface_culling = default_culling;
- } else if (lua_istable(L, index)) {
+ }
+ else if(lua_istable(L, index))
+ {
// name="default_lava.png"
tiledef.name = "";
getstringfield(L, index, "name", tiledef.name);
getstringfield(L, index, "image", tiledef.name); // MaterialSpec compat.
tiledef.backface_culling = getboolfield_default(
- L, index, "backface_culling", default_culling);
+ L, index, "backface_culling", default_culling);
tiledef.tileable_horizontal = getboolfield_default(
- L, index, "tileable_horizontal", default_tiling);
+ L, index, "tileable_horizontal", default_tiling);
tiledef.tileable_vertical = getboolfield_default(
- L, index, "tileable_vertical", default_tiling);
+ L, index, "tileable_vertical", default_tiling);
std::string align_style;
if (getstringfield(L, index, "align_style", align_style)) {
if (align_style == "user")
@@ -492,23 +493,20 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype)
/******************************************************************************/
ContentFeatures read_content_features(lua_State *L, int index)
{
- if (index < 0)
+ if(index < 0)
index = lua_gettop(L) + 1 + index;
ContentFeatures f;
/* Cache existence of some callbacks */
lua_getfield(L, index, "on_construct");
- if (!lua_isnil(L, -1))
- f.has_on_construct = true;
+ if(!lua_isnil(L, -1)) f.has_on_construct = true;
lua_pop(L, 1);
lua_getfield(L, index, "on_destruct");
- if (!lua_isnil(L, -1))
- f.has_on_destruct = true;
+ if(!lua_isnil(L, -1)) f.has_on_destruct = true;
lua_pop(L, 1);
lua_getfield(L, index, "after_destruct");
- if (!lua_isnil(L, -1))
- f.has_after_destruct = true;
+ if(!lua_isnil(L, -1)) f.has_after_destruct = true;
lua_pop(L, 1);
lua_getfield(L, index, "on_rightclick");
@@ -525,8 +523,8 @@ ContentFeatures read_content_features(lua_State *L, int index)
/* Visual definition */
- f.drawtype = (NodeDrawType)getenumfield(
- L, index, "drawtype", ScriptApiNode::es_DrawType, NDT_NORMAL);
+ f.drawtype = (NodeDrawType)getenumfield(L, index, "drawtype",
+ ScriptApiNode::es_DrawType,NDT_NORMAL);
getfloatfield(L, index, "visual_scale", f.visual_scale);
/* Meshnode model filename */
@@ -535,31 +533,31 @@ ContentFeatures read_content_features(lua_State *L, int index)
// tiles = {}
lua_getfield(L, index, "tiles");
// If nil, try the deprecated name "tile_images" instead
- if (lua_isnil(L, -1)) {
+ if(lua_isnil(L, -1)){
lua_pop(L, 1);
warn_if_field_exists(L, index, "tile_images",
"Deprecated; new name is \"tiles\".");
lua_getfield(L, index, "tile_images");
}
- if (lua_istable(L, -1)) {
+ if(lua_istable(L, -1)){
int table = lua_gettop(L);
lua_pushnil(L);
int i = 0;
- while (lua_next(L, table) != 0) {
+ while(lua_next(L, table) != 0){
// Read tiledef from value
f.tiledef[i] = read_tiledef(L, -1, f.drawtype);
// removes value, keeps key for next iteration
lua_pop(L, 1);
i++;
- if (i == 6) {
+ if(i==6){
lua_pop(L, 1);
break;
}
}
// Copy last value to all remaining textures
- if (i >= 1) {
- TileDef lasttile = f.tiledef[i - 1];
- while (i < 6) {
+ if(i >= 1){
+ TileDef lasttile = f.tiledef[i-1];
+ while(i < 6){
f.tiledef[i] = lasttile;
i++;
}
@@ -598,23 +596,23 @@ ContentFeatures read_content_features(lua_State *L, int index)
// special_tiles = {}
lua_getfield(L, index, "special_tiles");
// If nil, try the deprecated name "special_materials" instead
- if (lua_isnil(L, -1)) {
+ if(lua_isnil(L, -1)){
lua_pop(L, 1);
warn_if_field_exists(L, index, "special_materials",
"Deprecated; new name is \"special_tiles\".");
lua_getfield(L, index, "special_materials");
}
- if (lua_istable(L, -1)) {
+ if(lua_istable(L, -1)){
int table = lua_gettop(L);
lua_pushnil(L);
int i = 0;
- while (lua_next(L, table) != 0) {
+ while(lua_next(L, table) != 0){
// Read tiledef from value
f.tiledef_special[i] = read_tiledef(L, -1, f.drawtype);
// removes value, keeps key for next iteration
lua_pop(L, 1);
i++;
- if (i == CF_SPECIAL_COUNT) {
+ if(i==CF_SPECIAL_COUNT){
lua_pop(L, 1);
break;
}
@@ -624,7 +622,8 @@ ContentFeatures read_content_features(lua_State *L, int index)
f.alpha = getintfield_default(L, index, "alpha", 255);
- bool usealpha = getboolfield_default(L, index, "use_texture_alpha", false);
+ bool usealpha = getboolfield_default(L, index,
+ "use_texture_alpha", false);
if (usealpha)
f.alpha = 0;
@@ -648,21 +647,22 @@ ContentFeatures read_content_features(lua_State *L, int index)
if (!f.palette_name.empty() &&
!(f.param_type_2 == CPT2_COLOR ||
- f.param_type_2 == CPT2_COLORED_FACEDIR ||
- f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
+ f.param_type_2 == CPT2_COLORED_FACEDIR ||
+ f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
warningstream << "Node " << f.name.c_str()
- << " has a palette, but not a suitable paramtype2."
- << std::endl;
+ << " has a palette, but not a suitable paramtype2." << std::endl;
// Warn about some obsolete fields
warn_if_field_exists(L, index, "wall_mounted",
"Obsolete; use paramtype2 = 'wallmounted'");
warn_if_field_exists(L, index, "light_propagates",
"Obsolete; determined from paramtype");
- warn_if_field_exists(L, index, "dug_item", "Obsolete; use 'drop' field");
- warn_if_field_exists(L, index, "extra_dug_item", "Obsolete; use 'drop' field");
- warn_if_field_exists(
- L, index, "extra_dug_item_rarity", "Obsolete; use 'drop' field");
+ warn_if_field_exists(L, index, "dug_item",
+ "Obsolete; use 'drop' field");
+ warn_if_field_exists(L, index, "extra_dug_item",
+ "Obsolete; use 'drop' field");
+ warn_if_field_exists(L, index, "extra_dug_item_rarity",
+ "Obsolete; use 'drop' field");
warn_if_field_exists(L, index, "metadata_name",
"Obsolete; use on_add and metadata callbacks");
@@ -695,27 +695,31 @@ ContentFeatures read_content_features(lua_State *L, int index)
// Viscosity for fluid flow, ranging from 1 to 7, with
// 1 giving almost instantaneous propagation and 7 being
// the slowest possible
- f.liquid_viscosity = getintfield_default(
- L, index, "liquid_viscosity", f.liquid_viscosity);
- f.liquid_range = getintfield_default(L, index, "liquid_range", f.liquid_range);
+ f.liquid_viscosity = getintfield_default(L, index,
+ "liquid_viscosity", f.liquid_viscosity);
+ f.liquid_range = getintfield_default(L, index,
+ "liquid_range", f.liquid_range);
f.leveled = getintfield_default(L, index, "leveled", f.leveled);
- f.leveled_max = getintfield_default(L, index, "leveled_max", f.leveled_max);
+ f.leveled_max = getintfield_default(L, index,
+ "leveled_max", f.leveled_max);
getboolfield(L, index, "liquid_renewable", f.liquid_renewable);
- f.drowning = getintfield_default(L, index, "drowning", f.drowning);
+ f.drowning = getintfield_default(L, index,
+ "drowning", f.drowning);
// Amount of light the node emits
- f.light_source = getintfield_default(L, index, "light_source", f.light_source);
+ f.light_source = getintfield_default(L, index,
+ "light_source", f.light_source);
if (f.light_source > LIGHT_MAX) {
warningstream << "Node " << f.name.c_str()
- << " had greater light_source than " << LIGHT_MAX
- << ", it was reduced." << std::endl;
+ << " had greater light_source than " << LIGHT_MAX
+ << ", it was reduced." << std::endl;
f.light_source = LIGHT_MAX;
}
- f.damage_per_second = getintfield_default(
- L, index, "damage_per_second", f.damage_per_second);
+ f.damage_per_second = getintfield_default(L, index,
+ "damage_per_second", f.damage_per_second);
lua_getfield(L, index, "node_box");
- if (lua_istable(L, -1))
+ if(lua_istable(L, -1))
f.node_box = read_nodebox(L, -1);
lua_pop(L, 1);
@@ -753,23 +757,24 @@ ContentFeatures read_content_features(lua_State *L, int index)
f.connect_sides |= 8;
else
warningstream << "Unknown value for \"connect_sides\": "
- << side << std::endl;
+ << side << std::endl;
lua_pop(L, 1);
}
}
lua_pop(L, 1);
lua_getfield(L, index, "selection_box");
- if (lua_istable(L, -1))
+ if(lua_istable(L, -1))
f.selection_box = read_nodebox(L, -1);
- lua_pop(L, 1);
+ lua_pop(L, 1);
lua_getfield(L, index, "collision_box");
- if (lua_istable(L, -1))
+ if(lua_istable(L, -1))
f.collision_box = read_nodebox(L, -1);
lua_pop(L, 1);
- f.waving = getintfield_default(L, index, "waving", f.waving);
+ f.waving = getintfield_default(L, index,
+ "waving", f.waving);
// Set to true if paramtype used to be 'facedir_simple'
getboolfield(L, index, "legacy_facedir_simple", f.legacy_facedir_simple);
@@ -778,7 +783,7 @@ ContentFeatures read_content_features(lua_State *L, int index)
// Sound table
lua_getfield(L, index, "sounds");
- if (lua_istable(L, -1)) {
+ if(lua_istable(L, -1)){
lua_getfield(L, -1, "footstep");
read_soundspec(L, -1, f.sound_footstep);
lua_pop(L, 1);
@@ -792,7 +797,8 @@ ContentFeatures read_content_features(lua_State *L, int index)
lua_pop(L, 1);
// Node immediately placed by client when node is dug
- getstringfield(L, index, "node_dig_prediction", f.node_dig_prediction);
+ getstringfield(L, index, "node_dig_prediction",
+ f.node_dig_prediction);
return f;
}
@@ -800,8 +806,7 @@ ContentFeatures read_content_features(lua_State *L, int index)
void push_content_features(lua_State *L, const ContentFeatures &c)
{
std::string paramtype(ScriptApiNode::es_ContentParamType[(int)c.param_type].str);
- std::string paramtype2(
- ScriptApiNode::es_ContentParamType2[(int)c.param_type_2].str);
+ std::string paramtype2(ScriptApiNode::es_ContentParamType2[(int)c.param_type_2].str);
std::string drawtype(ScriptApiNode::es_DrawType[(int)c.drawtype].str);
std::string liquid_type(ScriptApiNode::es_LiquidType[(int)c.liquid_type].str);
@@ -829,7 +834,7 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
lua_setfield(L, -2, "mesh");
}
#ifndef SERVER
- push_ARGB8(L, c.minimap_color); // I know this is not set-able w/ register_node,
+ push_ARGB8(L, c.minimap_color); // I know this is not set-able w/ register_node,
lua_setfield(L, -2, "minimap_color"); // but the people need to know!
#endif
lua_pushnumber(L, c.visual_scale);
@@ -929,47 +934,48 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
void push_nodebox(lua_State *L, const NodeBox &box)
{
lua_newtable(L);
- switch (box.type) {
- case NODEBOX_REGULAR:
- lua_pushstring(L, "regular");
- lua_setfield(L, -2, "type");
- break;
- case NODEBOX_LEVELED:
- case NODEBOX_FIXED:
- lua_pushstring(L, "fixed");
- lua_setfield(L, -2, "type");
- push_box(L, box.fixed);
- lua_setfield(L, -2, "fixed");
- break;
- case NODEBOX_WALLMOUNTED:
- lua_pushstring(L, "wallmounted");
- lua_setfield(L, -2, "type");
- push_aabb3f(L, box.wall_top);
- lua_setfield(L, -2, "wall_top");
- push_aabb3f(L, box.wall_bottom);
- lua_setfield(L, -2, "wall_bottom");
- push_aabb3f(L, box.wall_side);
- lua_setfield(L, -2, "wall_side");
- break;
- case NODEBOX_CONNECTED:
- lua_pushstring(L, "connected");
- lua_setfield(L, -2, "type");
- push_box(L, box.connect_top);
- lua_setfield(L, -2, "connect_top");
- push_box(L, box.connect_bottom);
- lua_setfield(L, -2, "connect_bottom");
- push_box(L, box.connect_front);
- lua_setfield(L, -2, "connect_front");
- push_box(L, box.connect_back);
- lua_setfield(L, -2, "connect_back");
- push_box(L, box.connect_left);
- lua_setfield(L, -2, "connect_left");
- push_box(L, box.connect_right);
- lua_setfield(L, -2, "connect_right");
- break;
- default:
- FATAL_ERROR("Invalid box.type");
- break;
+ switch (box.type)
+ {
+ case NODEBOX_REGULAR:
+ lua_pushstring(L, "regular");
+ lua_setfield(L, -2, "type");
+ break;
+ case NODEBOX_LEVELED:
+ case NODEBOX_FIXED:
+ lua_pushstring(L, "fixed");
+ lua_setfield(L, -2, "type");
+ push_box(L, box.fixed);
+ lua_setfield(L, -2, "fixed");
+ break;
+ case NODEBOX_WALLMOUNTED:
+ lua_pushstring(L, "wallmounted");
+ lua_setfield(L, -2, "type");
+ push_aabb3f(L, box.wall_top);
+ lua_setfield(L, -2, "wall_top");
+ push_aabb3f(L, box.wall_bottom);
+ lua_setfield(L, -2, "wall_bottom");
+ push_aabb3f(L, box.wall_side);
+ lua_setfield(L, -2, "wall_side");
+ break;
+ case NODEBOX_CONNECTED:
+ lua_pushstring(L, "connected");
+ lua_setfield(L, -2, "type");
+ push_box(L, box.connect_top);
+ lua_setfield(L, -2, "connect_top");
+ push_box(L, box.connect_bottom);
+ lua_setfield(L, -2, "connect_bottom");
+ push_box(L, box.connect_front);
+ lua_setfield(L, -2, "connect_front");
+ push_box(L, box.connect_back);
+ lua_setfield(L, -2, "connect_back");
+ push_box(L, box.connect_left);
+ lua_setfield(L, -2, "connect_left");
+ push_box(L, box.connect_right);
+ lua_setfield(L, -2, "connect_right");
+ break;
+ default:
+ FATAL_ERROR("Invalid box.type");
+ break;
}
}
@@ -998,37 +1004,37 @@ void push_palette(lua_State *L, const std::vector<video::SColor> *palette)
}
/******************************************************************************/
-void read_server_sound_params(lua_State *L, int index, ServerSoundParams &params)
+void read_server_sound_params(lua_State *L, int index,
+ ServerSoundParams &params)
{
- if (index < 0)
+ if(index < 0)
index = lua_gettop(L) + 1 + index;
// Clear
params = ServerSoundParams();
- if (lua_istable(L, index)) {
+ if(lua_istable(L, index)){
getfloatfield(L, index, "gain", params.gain);
getstringfield(L, index, "to_player", params.to_player);
getfloatfield(L, index, "fade", params.fade);
getfloatfield(L, index, "pitch", params.pitch);
lua_getfield(L, index, "pos");
- if (!lua_isnil(L, -1)) {
- v3f p = read_v3f(L, -1) * BS;
+ if(!lua_isnil(L, -1)){
+ v3f p = read_v3f(L, -1)*BS;
params.pos = p;
params.type = ServerSoundParams::SSP_POSITIONAL;
}
lua_pop(L, 1);
lua_getfield(L, index, "object");
- if (!lua_isnil(L, -1)) {
+ if(!lua_isnil(L, -1)){
ObjectRef *ref = ObjectRef::checkobject(L, -1);
ServerActiveObject *sao = ObjectRef::getobject(ref);
- if (sao) {
+ if(sao){
params.object = sao->getId();
params.type = ServerSoundParams::SSP_OBJECT;
}
}
lua_pop(L, 1);
- params.max_hear_distance =
- BS * getfloatfield_default(L, index, "max_hear_distance",
- params.max_hear_distance / BS);
+ params.max_hear_distance = BS*getfloatfield_default(L, index,
+ "max_hear_distance", params.max_hear_distance/BS);
getboolfield(L, index, "loop", params.loop);
getstringfield(L, index, "exclude_player", params.exclude_player);
}
@@ -1037,7 +1043,7 @@ void read_server_sound_params(lua_State *L, int index, ServerSoundParams &params
/******************************************************************************/
void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
{
- if (index < 0)
+ if(index < 0)
index = lua_gettop(L) + 1 + index;
if (lua_isnil(L, index))
return;
@@ -1074,21 +1080,20 @@ NodeBox read_nodebox(lua_State *L, int index)
luaL_checktype(L, -1, LUA_TTABLE);
- nodebox.type = (NodeBoxType)getenumfield(
- L, index, "type", ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
+ nodebox.type = (NodeBoxType)getenumfield(L, index, "type",
+ ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR);
-#define NODEBOXREAD(n, s) \
- { \
- lua_getfield(L, index, (s)); \
- if (lua_istable(L, -1)) \
- (n) = read_aabb3f(L, -1, BS); \
- lua_pop(L, 1); \
+#define NODEBOXREAD(n, s){ \
+ lua_getfield(L, index, (s)); \
+ if (lua_istable(L, -1)) \
+ (n) = read_aabb3f(L, -1, BS); \
+ lua_pop(L, 1); \
}
-#define NODEBOXREADVEC(n, s) \
- lua_getfield(L, index, (s)); \
- if (lua_istable(L, -1)) \
- (n) = read_aabb3f_vector(L, -1, BS); \
+#define NODEBOXREADVEC(n, s) \
+ lua_getfield(L, index, (s)); \
+ if (lua_istable(L, -1)) \
+ (n) = read_aabb3f_vector(L, -1, BS); \
lua_pop(L, 1);
NODEBOXREADVEC(nodebox.fixed, "fixed");
@@ -1154,31 +1159,34 @@ void pushnode(lua_State *L, const MapNode &n, const NodeDefManager *ndef)
}
/******************************************************************************/
-void warn_if_field_exists(
- lua_State *L, int table, const char *name, const std::string &message)
+void warn_if_field_exists(lua_State *L, int table,
+ const char *name, const std::string &message)
{
lua_getfield(L, table, name);
if (!lua_isnil(L, -1)) {
- warningstream << "Field \"" << name << "\": " << message << std::endl;
+ warningstream << "Field \"" << name << "\": "
+ << message << std::endl;
infostream << script_get_backtrace(L) << std::endl;
}
lua_pop(L, 1);
}
/******************************************************************************/
-int getenumfield(lua_State *L, int table, const char *fieldname, const EnumString *spec,
- int default_)
+int getenumfield(lua_State *L, int table,
+ const char *fieldname, const EnumString *spec, int default_)
{
int result = default_;
- string_to_enum(spec, result, getstringfield_default(L, table, fieldname, ""));
+ string_to_enum(spec, result,
+ getstringfield_default(L, table, fieldname, ""));
return result;
}
/******************************************************************************/
-bool string_to_enum(const EnumString *spec, int &result, const std::string &str)
+bool string_to_enum(const EnumString *spec, int &result,
+ const std::string &str)
{
const EnumString *esp = spec;
- while (esp->str) {
+ while(esp->str){
if (!strcmp(str.c_str(), esp->str)) {
result = esp->num;
return true;
@@ -1189,9 +1197,9 @@ bool string_to_enum(const EnumString *spec, int &result, const std::string &str)
}
/******************************************************************************/
-ItemStack read_item(lua_State *L, int index, IItemDefManager *idef)
+ItemStack read_item(lua_State* L, int index, IItemDefManager *idef)
{
- if (index < 0)
+ if(index < 0)
index = lua_gettop(L) + 1 + index;
if (lua_isnil(L, index)) {
@@ -1207,16 +1215,21 @@ ItemStack read_item(lua_State *L, int index, IItemDefManager *idef)
if (lua_isstring(L, index)) {
// Convert from itemstring
std::string itemstring = lua_tostring(L, index);
- try {
+ try
+ {
ItemStack item;
item.deSerialize(itemstring, idef);
return item;
- } catch (SerializationError &e) {
- warningstream << "unable to create item from itemstring"
- << ": " << itemstring << std::endl;
+ }
+ catch(SerializationError &e)
+ {
+ warningstream<<"unable to create item from itemstring"
+ <<": "<<itemstring<<std::endl;
return ItemStack();
}
- } else if (lua_istable(L, index)) {
+ }
+ else if(lua_istable(L, index))
+ {
// Convert from table
std::string name = getstringfield_default(L, index, "name", "");
int count = getintfield_default(L, index, "count", 1);
@@ -1240,8 +1253,7 @@ ItemStack read_item(lua_State *L, int index, IItemDefManager *idef)
const char *value_cs = lua_tolstring(L, -1, &value_len);
std::string value(value_cs, value_len);
istack.metadata.setString(key, value);
- lua_pop(L, 1); // removes value, keeps key for next
- // iteration
+ lua_pop(L, 1); // removes value, keeps key for next iteration
}
}
@@ -1252,56 +1264,57 @@ ItemStack read_item(lua_State *L, int index, IItemDefManager *idef)
}
/******************************************************************************/
-void push_tool_capabilities(lua_State *L, const ToolCapabilities &toolcap)
+void push_tool_capabilities(lua_State *L,
+ const ToolCapabilities &toolcap)
{
lua_newtable(L);
setfloatfield(L, -1, "full_punch_interval", toolcap.full_punch_interval);
setintfield(L, -1, "max_drop_level", toolcap.max_drop_level);
setintfield(L, -1, "punch_attack_uses", toolcap.punch_attack_uses);
- // Create groupcaps table
- lua_newtable(L);
- // For each groupcap
- for (const auto &gc_it : toolcap.groupcaps) {
- // Create groupcap table
+ // Create groupcaps table
lua_newtable(L);
- const std::string &name = gc_it.first;
- const ToolGroupCap &groupcap = gc_it.second;
- // Create subtable "times"
+ // For each groupcap
+ for (const auto &gc_it : toolcap.groupcaps) {
+ // Create groupcap table
+ lua_newtable(L);
+ const std::string &name = gc_it.first;
+ const ToolGroupCap &groupcap = gc_it.second;
+ // Create subtable "times"
+ lua_newtable(L);
+ for (auto time : groupcap.times) {
+ lua_pushinteger(L, time.first);
+ lua_pushnumber(L, time.second);
+ lua_settable(L, -3);
+ }
+ // Set subtable "times"
+ lua_setfield(L, -2, "times");
+ // Set simple parameters
+ setintfield(L, -1, "maxlevel", groupcap.maxlevel);
+ setintfield(L, -1, "uses", groupcap.uses);
+ // Insert groupcap table into groupcaps table
+ lua_setfield(L, -2, name.c_str());
+ }
+ // Set groupcaps table
+ lua_setfield(L, -2, "groupcaps");
+ //Create damage_groups table
lua_newtable(L);
- for (auto time : groupcap.times) {
- lua_pushinteger(L, time.first);
- lua_pushnumber(L, time.second);
- lua_settable(L, -3);
+ // For each damage group
+ for (const auto &damageGroup : toolcap.damageGroups) {
+ // Create damage group table
+ lua_pushinteger(L, damageGroup.second);
+ lua_setfield(L, -2, damageGroup.first.c_str());
}
- // Set subtable "times"
- lua_setfield(L, -2, "times");
- // Set simple parameters
- setintfield(L, -1, "maxlevel", groupcap.maxlevel);
- setintfield(L, -1, "uses", groupcap.uses);
- // Insert groupcap table into groupcaps table
- lua_setfield(L, -2, name.c_str());
- }
- // Set groupcaps table
- lua_setfield(L, -2, "groupcaps");
- // Create damage_groups table
- lua_newtable(L);
- // For each damage group
- for (const auto &damageGroup : toolcap.damageGroups) {
- // Create damage group table
- lua_pushinteger(L, damageGroup.second);
- lua_setfield(L, -2, damageGroup.first.c_str());
- }
- lua_setfield(L, -2, "damage_groups");
+ lua_setfield(L, -2, "damage_groups");
}
/******************************************************************************/
void push_inventory(lua_State *L, Inventory *inventory)
{
- std::vector<const InventoryList *> lists = inventory->getLists();
- std::vector<const InventoryList *>::iterator iter = lists.begin();
+ std::vector<const InventoryList*> lists = inventory->getLists();
+ std::vector<const InventoryList*>::iterator iter = lists.begin();
lua_createtable(L, 0, lists.size());
for (; iter != lists.end(); iter++) {
- const char *name = (*iter)->getName().c_str();
+ const char* name = (*iter)->getName().c_str();
lua_pushstring(L, name);
push_inventory_list(L, inventory, name);
lua_rawset(L, -3);
@@ -1312,40 +1325,40 @@ void push_inventory(lua_State *L, Inventory *inventory)
void push_inventory_list(lua_State *L, Inventory *inv, const char *name)
{
InventoryList *invlist = inv->getList(name);
- if (invlist == NULL) {
+ if(invlist == NULL){
lua_pushnil(L);
return;
}
std::vector<ItemStack> items;
- for (u32 i = 0; i < invlist->getSize(); i++)
+ for(u32 i=0; i<invlist->getSize(); i++)
items.push_back(invlist->getItem(i));
push_items(L, items);
}
/******************************************************************************/
-void read_inventory_list(lua_State *L, int tableindex, Inventory *inv, const char *name,
- Server *srv, int forcesize)
+void read_inventory_list(lua_State *L, int tableindex,
+ Inventory *inv, const char *name, Server* srv, int forcesize)
{
- if (tableindex < 0)
+ if(tableindex < 0)
tableindex = lua_gettop(L) + 1 + tableindex;
// If nil, delete list
- if (lua_isnil(L, tableindex)) {
+ if(lua_isnil(L, tableindex)){
inv->deleteList(name);
return;
}
// Otherwise set list
- std::vector<ItemStack> items = read_items(L, tableindex, srv);
+ std::vector<ItemStack> items = read_items(L, tableindex,srv);
int listsize = (forcesize != -1) ? forcesize : items.size();
InventoryList *invlist = inv->addList(name, listsize);
int index = 0;
- for (std::vector<ItemStack>::const_iterator i = items.begin(); i != items.end();
- ++i) {
- if (forcesize != -1 && index == forcesize)
+ for(std::vector<ItemStack>::const_iterator
+ i = items.begin(); i != items.end(); ++i){
+ if(forcesize != -1 && index == forcesize)
break;
invlist->changeItem(index, *i);
index++;
}
- while (forcesize != -1 && index < forcesize) {
+ while(forcesize != -1 && index < forcesize){
invlist->deleteItem(index);
index++;
}
@@ -1354,7 +1367,7 @@ void read_inventory_list(lua_State *L, int tableindex, Inventory *inv, const cha
/******************************************************************************/
struct TileAnimationParams read_animation_definition(lua_State *L, int index)
{
- if (index < 0)
+ if(index < 0)
index = lua_gettop(L) + 1 + index;
struct TileAnimationParams anim;
@@ -1362,75 +1375,74 @@ struct TileAnimationParams read_animation_definition(lua_State *L, int index)
if (!lua_istable(L, index))
return anim;
- anim.type = (TileAnimationType)getenumfield(
- L, index, "type", es_TileAnimationType, TAT_NONE);
+ anim.type = (TileAnimationType)
+ getenumfield(L, index, "type", es_TileAnimationType,
+ TAT_NONE);
if (anim.type == TAT_VERTICAL_FRAMES) {
// {type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0}
anim.vertical_frames.aspect_w =
- getintfield_default(L, index, "aspect_w", 16);
+ getintfield_default(L, index, "aspect_w", 16);
anim.vertical_frames.aspect_h =
- getintfield_default(L, index, "aspect_h", 16);
+ getintfield_default(L, index, "aspect_h", 16);
anim.vertical_frames.length =
- getfloatfield_default(L, index, "length", 1.0);
+ getfloatfield_default(L, index, "length", 1.0);
} else if (anim.type == TAT_SHEET_2D) {
// {type="sheet_2d", frames_w=5, frames_h=3, frame_length=0.5}
- getintfield(L, index, "frames_w", anim.sheet_2d.frames_w);
- getintfield(L, index, "frames_h", anim.sheet_2d.frames_h);
- getfloatfield(L, index, "frame_length", anim.sheet_2d.frame_length);
+ getintfield(L, index, "frames_w",
+ anim.sheet_2d.frames_w);
+ getintfield(L, index, "frames_h",
+ anim.sheet_2d.frames_h);
+ getfloatfield(L, index, "frame_length",
+ anim.sheet_2d.frame_length);
}
return anim;
}
/******************************************************************************/
-ToolCapabilities read_tool_capabilities(lua_State *L, int table)
+ToolCapabilities read_tool_capabilities(
+ lua_State *L, int table)
{
ToolCapabilities toolcap;
getfloatfield(L, table, "full_punch_interval", toolcap.full_punch_interval);
getintfield(L, table, "max_drop_level", toolcap.max_drop_level);
getintfield(L, table, "punch_attack_uses", toolcap.punch_attack_uses);
lua_getfield(L, table, "groupcaps");
- if (lua_istable(L, -1)) {
+ if(lua_istable(L, -1)){
int table_groupcaps = lua_gettop(L);
lua_pushnil(L);
- while (lua_next(L, table_groupcaps) != 0) {
+ while(lua_next(L, table_groupcaps) != 0){
// key at index -2 and value at index -1
std::string groupname = luaL_checkstring(L, -2);
- if (lua_istable(L, -1)) {
+ if(lua_istable(L, -1)){
int table_groupcap = lua_gettop(L);
// This will be created
ToolGroupCap groupcap;
// Read simple parameters
- getintfield(L, table_groupcap, "maxlevel",
- groupcap.maxlevel);
+ getintfield(L, table_groupcap, "maxlevel", groupcap.maxlevel);
getintfield(L, table_groupcap, "uses", groupcap.uses);
// DEPRECATED: maxwear
float maxwear = 0;
- if (getfloatfield(L, table_groupcap, "maxwear",
- maxwear)) {
+ if (getfloatfield(L, table_groupcap, "maxwear", maxwear)){
if (maxwear != 0)
- groupcap.uses = 1.0 / maxwear;
+ groupcap.uses = 1.0/maxwear;
else
groupcap.uses = 0;
- warningstream << "Field \"maxwear\" is "
- "deprecated; "
- << "replace with uses=1/maxwear"
- << std::endl;
- infostream << script_get_backtrace(L)
- << std::endl;
+ warningstream << "Field \"maxwear\" is deprecated; "
+ << "replace with uses=1/maxwear" << std::endl;
+ infostream << script_get_backtrace(L) << std::endl;
}
// Read "times" table
lua_getfield(L, table_groupcap, "times");
- if (lua_istable(L, -1)) {
+ if(lua_istable(L, -1)){
int table_times = lua_gettop(L);
lua_pushnil(L);
- while (lua_next(L, table_times) != 0) {
+ while(lua_next(L, table_times) != 0){
// key at index -2 and value at index -1
int rating = luaL_checkinteger(L, -2);
float time = luaL_checknumber(L, -1);
groupcap.times[rating] = time;
- // removes value, keeps key for next
- // iteration
+ // removes value, keeps key for next iteration
lua_pop(L, 1);
}
}
@@ -1445,10 +1457,10 @@ ToolCapabilities read_tool_capabilities(lua_State *L, int table)
lua_pop(L, 1);
lua_getfield(L, table, "damage_groups");
- if (lua_istable(L, -1)) {
+ if(lua_istable(L, -1)){
int table_damage_groups = lua_gettop(L);
lua_pushnil(L);
- while (lua_next(L, table_damage_groups) != 0) {
+ while(lua_next(L, table_damage_groups) != 0){
// key at index -2 and value at index -1
std::string groupname = luaL_checkstring(L, -2);
u16 value = luaL_checkinteger(L, -1);
@@ -1462,7 +1474,7 @@ ToolCapabilities read_tool_capabilities(lua_State *L, int table)
}
/******************************************************************************/
-void push_dig_params(lua_State *L, const DigParams &params)
+void push_dig_params(lua_State *L,const DigParams &params)
{
lua_createtable(L, 0, 3);
setboolfield(L, -1, "diggable", params.diggable);
@@ -1471,7 +1483,7 @@ void push_dig_params(lua_State *L, const DigParams &params)
}
/******************************************************************************/
-void push_hit_params(lua_State *L, const HitParams &params)
+void push_hit_params(lua_State *L,const HitParams &params)
{
lua_createtable(L, 0, 3);
setintfield(L, -1, "hp", params.hp);
@@ -1480,8 +1492,8 @@ void push_hit_params(lua_State *L, const HitParams &params)
/******************************************************************************/
-bool getflagsfield(lua_State *L, int table, const char *fieldname, FlagDesc *flagdesc,
- u32 *flags, u32 *flagmask)
+bool getflagsfield(lua_State *L, int table, const char *fieldname,
+ FlagDesc *flagdesc, u32 *flags, u32 *flagmask)
{
lua_getfield(L, table, fieldname);
@@ -1492,7 +1504,8 @@ bool getflagsfield(lua_State *L, int table, const char *fieldname, FlagDesc *fla
return success;
}
-bool read_flags(lua_State *L, int index, FlagDesc *flagdesc, u32 *flags, u32 *flagmask)
+bool read_flags(lua_State *L, int index, FlagDesc *flagdesc,
+ u32 *flags, u32 *flagmask)
{
if (lua_isstring(L, index)) {
std::string flagstr = lua_tostring(L, index);
@@ -1588,7 +1601,7 @@ void push_items(lua_State *L, const std::vector<ItemStack> &items)
/******************************************************************************/
std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
{
- if (index < 0)
+ if(index < 0)
index = lua_gettop(L) + 1 + index;
std::vector<ItemStack> items;
@@ -1599,7 +1612,7 @@ std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
if (key < 1) {
throw LuaError("Invalid inventory list index");
}
- if (items.size() < (u32)key) {
+ if (items.size() < (u32) key) {
items.resize(key);
}
items[key - 1] = read_item(L, -1, srv->idef());
@@ -1630,23 +1643,21 @@ bool read_noiseparams(lua_State *L, int index, NoiseParams *np)
if (!lua_istable(L, index))
return false;
- getfloatfield(L, index, "offset", np->offset);
- getfloatfield(L, index, "scale", np->scale);
- getfloatfield(L, index, "persist", np->persist);
+ getfloatfield(L, index, "offset", np->offset);
+ getfloatfield(L, index, "scale", np->scale);
+ getfloatfield(L, index, "persist", np->persist);
getfloatfield(L, index, "persistence", np->persist);
- getfloatfield(L, index, "lacunarity", np->lacunarity);
- getintfield(L, index, "seed", np->seed);
- getintfield(L, index, "octaves", np->octaves);
+ getfloatfield(L, index, "lacunarity", np->lacunarity);
+ getintfield(L, index, "seed", np->seed);
+ getintfield(L, index, "octaves", np->octaves);
- u32 flags = 0;
+ u32 flags = 0;
u32 flagmask = 0;
- np->flags = getflagsfield(L, index, "flags", flagdesc_noiseparams, &flags,
- &flagmask)
- ? flags
- : NOISE_FLAG_DEFAULTS;
+ np->flags = getflagsfield(L, index, "flags", flagdesc_noiseparams,
+ &flags, &flagmask) ? flags : NOISE_FLAG_DEFAULTS;
lua_getfield(L, index, "spread");
- np->spread = read_v3f(L, -1);
+ np->spread = read_v3f(L, -1);
lua_pop(L, 1);
return true;
@@ -1668,7 +1679,8 @@ void push_noiseparams(lua_State *L, NoiseParams *np)
lua_pushnumber(L, np->octaves);
lua_setfield(L, -2, "octaves");
- push_flags_string(L, flagdesc_noiseparams, np->flags, np->flags);
+ push_flags_string(L, flagdesc_noiseparams, np->flags,
+ np->flags);
lua_setfield(L, -2, "flags");
push_v3_float_string(L, np->spread);
@@ -1691,52 +1703,55 @@ static int push_json_value_getdepth(const Json::Value &value)
return maxdepth + 1;
}
// Recursive function to convert JSON --> Lua table
-static bool push_json_value_helper(lua_State *L, const Json::Value &value, int nullindex)
+static bool push_json_value_helper(lua_State *L, const Json::Value &value,
+ int nullindex)
{
- switch (value.type()) {
- case Json::nullValue:
- default:
- lua_pushvalue(L, nullindex);
- break;
- case Json::intValue:
- lua_pushinteger(L, value.asLargestInt());
- break;
- case Json::uintValue:
- lua_pushinteger(L, value.asLargestUInt());
- break;
- case Json::realValue:
- lua_pushnumber(L, value.asDouble());
- break;
- case Json::stringValue: {
- const char *str = value.asCString();
- lua_pushstring(L, str ? str : "");
- } break;
- case Json::booleanValue:
- lua_pushboolean(L, value.asInt());
- break;
- case Json::arrayValue:
- lua_createtable(L, value.size(), 0);
- for (Json::Value::const_iterator it = value.begin(); it != value.end();
- ++it) {
- push_json_value_helper(L, *it, nullindex);
- lua_rawseti(L, -2, it.index() + 1);
- }
- break;
- case Json::objectValue:
- lua_createtable(L, 0, value.size());
- for (Json::Value::const_iterator it = value.begin(); it != value.end();
- ++it) {
+ switch(value.type()) {
+ case Json::nullValue:
+ default:
+ lua_pushvalue(L, nullindex);
+ break;
+ case Json::intValue:
+ lua_pushinteger(L, value.asLargestInt());
+ break;
+ case Json::uintValue:
+ lua_pushinteger(L, value.asLargestUInt());
+ break;
+ case Json::realValue:
+ lua_pushnumber(L, value.asDouble());
+ break;
+ case Json::stringValue:
+ {
+ const char *str = value.asCString();
+ lua_pushstring(L, str ? str : "");
+ }
+ break;
+ case Json::booleanValue:
+ lua_pushboolean(L, value.asInt());
+ break;
+ case Json::arrayValue:
+ lua_createtable(L, value.size(), 0);
+ for (Json::Value::const_iterator it = value.begin();
+ it != value.end(); ++it) {
+ push_json_value_helper(L, *it, nullindex);
+ lua_rawseti(L, -2, it.index() + 1);
+ }
+ break;
+ case Json::objectValue:
+ lua_createtable(L, 0, value.size());
+ for (Json::Value::const_iterator it = value.begin();
+ it != value.end(); ++it) {
#if !defined(JSONCPP_STRING) && (JSONCPP_VERSION_MAJOR < 1 || JSONCPP_VERSION_MINOR < 9)
- const char *str = it.memberName();
- lua_pushstring(L, str ? str : "");
+ const char *str = it.memberName();
+ lua_pushstring(L, str ? str : "");
#else
- std::string str = it.name();
- lua_pushstring(L, str.c_str());
+ std::string str = it.name();
+ lua_pushstring(L, str.c_str());
#endif
- push_json_value_helper(L, *it, nullindex);
- lua_rawset(L, -3);
- }
- break;
+ push_json_value_helper(L, *it, nullindex);
+ lua_rawset(L, -3);
+ }
+ break;
}
return true;
}
@@ -1744,7 +1759,7 @@ static bool push_json_value_helper(lua_State *L, const Json::Value &value, int n
// nullindex: Lua stack index of value to use in place of JSON null
bool push_json_value(lua_State *L, const Json::Value &value, int nullindex)
{
- if (nullindex < 0)
+ if(nullindex < 0)
nullindex = lua_gettop(L) + 1 + nullindex;
int depth = push_json_value_getdepth(value);
@@ -1765,7 +1780,7 @@ void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion)
}
int type = lua_type(L, index);
if (type == LUA_TBOOLEAN) {
- root = (bool)lua_toboolean(L, index);
+ root = (bool) lua_toboolean(L, index);
} else if (type == LUA_TNUMBER) {
root = lua_tonumber(L, index);
} else if (type == LUA_TSTRING) {
@@ -1783,43 +1798,33 @@ void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion)
int keytype = lua_type(L, -1);
if (keytype == LUA_TNUMBER) {
lua_Number key = lua_tonumber(L, -1);
- if (roottype != Json::nullValue &&
- roottype != Json::arrayValue) {
- throw SerializationError("Can't mix array and "
- "object values in JSON");
+ if (roottype != Json::nullValue && roottype != Json::arrayValue) {
+ throw SerializationError("Can't mix array and object values in JSON");
} else if (key < 1) {
- throw SerializationError(
- "Can't use zero-based or "
- "negative indexes in JSON");
+ throw SerializationError("Can't use zero-based or negative indexes in JSON");
} else if (floor(key) != key) {
- throw SerializationError(
- "Can't use indexes with a "
- "fractional part in JSON");
+ throw SerializationError("Can't use indexes with a fractional part in JSON");
}
- root[(Json::ArrayIndex)key - 1] = value;
+ root[(Json::ArrayIndex) key - 1] = value;
} else if (keytype == LUA_TSTRING) {
- if (roottype != Json::nullValue &&
- roottype != Json::objectValue) {
- throw SerializationError("Can't mix array and "
- "object values in JSON");
+ if (roottype != Json::nullValue && roottype != Json::objectValue) {
+ throw SerializationError("Can't mix array and object values in JSON");
}
root[lua_tostring(L, -1)] = value;
} else {
- throw SerializationError("Lua key to convert to JSON is "
- "not a string or number");
+ throw SerializationError("Lua key to convert to JSON is not a string or number");
}
}
} else if (type == LUA_TNIL) {
root = Json::nullValue;
} else {
- throw SerializationError("Can only store booleans, numbers, strings, "
- "objects, arrays, and null in JSON");
+ throw SerializationError("Can only store booleans, numbers, strings, objects, arrays, and null in JSON");
}
lua_pop(L, 1); // Pop value
}
-void push_pointed_thing(
- lua_State *L, const PointedThing &pointed, bool csm, bool hitpoint)
+void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm,
+ bool hitpoint)
{
lua_newtable(L);
if (pointed.type == POINTEDTHING_NODE) {
@@ -1838,7 +1843,7 @@ void push_pointed_thing(
} else {
push_objectRef(L, pointed.object_id);
}
-
+
lua_setfield(L, -2, "ref");
} else {
lua_pushstring(L, "nothing");
@@ -1868,8 +1873,8 @@ void push_objectRef(lua_State *L, const u16 id)
void read_hud_element(lua_State *L, HudElement *elem)
{
- elem->type = (HudElementType)getenumfield(
- L, 2, "hud_elem_type", es_HudElementType, HUD_ELEM_TEXT);
+ elem->type = (HudElementType)getenumfield(L, 2, "hud_elem_type",
+ es_HudElementType, HUD_ELEM_TEXT);
lua_getfield(L, 2, "position");
elem->pos = lua_istable(L, -1) ? read_v2f(L, -1) : v2f();
@@ -1883,18 +1888,18 @@ void read_hud_element(lua_State *L, HudElement *elem)
elem->size = lua_istable(L, -1) ? read_v2s32(L, -1) : v2s32();
lua_pop(L, 1);
- elem->name = getstringfield_default(L, 2, "name", "");
- elem->text = getstringfield_default(L, 2, "text", "");
- elem->number = getintfield_default(L, 2, "number", 0);
+ elem->name = getstringfield_default(L, 2, "name", "");
+ elem->text = getstringfield_default(L, 2, "text", "");
+ elem->number = getintfield_default(L, 2, "number", 0);
if (elem->type == HUD_ELEM_WAYPOINT)
// waypoints reuse the item field to store precision, item = precision + 1
elem->item = getintfield_default(L, 2, "precision", -1) + 1;
else
elem->item = getintfield_default(L, 2, "item", 0);
- elem->dir = getintfield_default(L, 2, "direction", 0);
- elem->z_index = MYMAX(
- S16_MIN, MYMIN(S16_MAX, getintfield_default(L, 2, "z_index", 0)));
- elem->text2 = getstringfield_default(L, 2, "text2", "");
+ elem->dir = getintfield_default(L, 2, "direction", 0);
+ elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX,
+ getintfield_default(L, 2, "z_index", 0)));
+ elem->text2 = getstringfield_default(L, 2, "text2", "");
// Deprecated, only for compatibility's sake
if (elem->dir == 0)
@@ -1913,8 +1918,8 @@ void read_hud_element(lua_State *L, HudElement *elem)
lua_pop(L, 1);
/* check for known deprecated element usage */
- if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32()))
- log_deprecated(L, "Deprecated usage of statbar without size!");
+ if ((elem->type == HUD_ELEM_STATBAR) && (elem->size == v2s32()))
+ log_deprecated(L,"Deprecated usage of statbar without size!");
}
void push_hud_element(lua_State *L, HudElement *elem)
@@ -1974,64 +1979,63 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
if (lua_isstring(L, 3)) {
int statint;
std::string statstr = lua_tostring(L, 3);
- stat = string_to_enum(es_HudElementStat, statint, statstr)
- ? (HudElementStat)statint
- : stat;
+ stat = string_to_enum(es_HudElementStat, statint, statstr) ?
+ (HudElementStat)statint : stat;
}
switch (stat) {
- case HUD_STAT_POS:
- elem->pos = read_v2f(L, 4);
- *value = &elem->pos;
- break;
- case HUD_STAT_NAME:
- elem->name = luaL_checkstring(L, 4);
- *value = &elem->name;
- break;
- case HUD_STAT_SCALE:
- elem->scale = read_v2f(L, 4);
- *value = &elem->scale;
- break;
- case HUD_STAT_TEXT:
- elem->text = luaL_checkstring(L, 4);
- *value = &elem->text;
- break;
- case HUD_STAT_NUMBER:
- elem->number = luaL_checknumber(L, 4);
- *value = &elem->number;
- break;
- case HUD_STAT_ITEM:
- elem->item = luaL_checknumber(L, 4);
- *value = &elem->item;
- break;
- case HUD_STAT_DIR:
- elem->dir = luaL_checknumber(L, 4);
- *value = &elem->dir;
- break;
- case HUD_STAT_ALIGN:
- elem->align = read_v2f(L, 4);
- *value = &elem->align;
- break;
- case HUD_STAT_OFFSET:
- elem->offset = read_v2f(L, 4);
- *value = &elem->offset;
- break;
- case HUD_STAT_WORLD_POS:
- elem->world_pos = read_v3f(L, 4);
- *value = &elem->world_pos;
- break;
- case HUD_STAT_SIZE:
- elem->size = read_v2s32(L, 4);
- *value = &elem->size;
- break;
- case HUD_STAT_Z_INDEX:
- elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX, luaL_checknumber(L, 4)));
- *value = &elem->z_index;
- break;
- case HUD_STAT_TEXT2:
- elem->text2 = luaL_checkstring(L, 4);
- *value = &elem->text2;
- break;
+ case HUD_STAT_POS:
+ elem->pos = read_v2f(L, 4);
+ *value = &elem->pos;
+ break;
+ case HUD_STAT_NAME:
+ elem->name = luaL_checkstring(L, 4);
+ *value = &elem->name;
+ break;
+ case HUD_STAT_SCALE:
+ elem->scale = read_v2f(L, 4);
+ *value = &elem->scale;
+ break;
+ case HUD_STAT_TEXT:
+ elem->text = luaL_checkstring(L, 4);
+ *value = &elem->text;
+ break;
+ case HUD_STAT_NUMBER:
+ elem->number = luaL_checknumber(L, 4);
+ *value = &elem->number;
+ break;
+ case HUD_STAT_ITEM:
+ elem->item = luaL_checknumber(L, 4);
+ *value = &elem->item;
+ break;
+ case HUD_STAT_DIR:
+ elem->dir = luaL_checknumber(L, 4);
+ *value = &elem->dir;
+ break;
+ case HUD_STAT_ALIGN:
+ elem->align = read_v2f(L, 4);
+ *value = &elem->align;
+ break;
+ case HUD_STAT_OFFSET:
+ elem->offset = read_v2f(L, 4);
+ *value = &elem->offset;
+ break;
+ case HUD_STAT_WORLD_POS:
+ elem->world_pos = read_v3f(L, 4);
+ *value = &elem->world_pos;
+ break;
+ case HUD_STAT_SIZE:
+ elem->size = read_v2s32(L, 4);
+ *value = &elem->size;
+ break;
+ case HUD_STAT_Z_INDEX:
+ elem->z_index = MYMAX(S16_MIN, MYMIN(S16_MAX, luaL_checknumber(L, 4)));
+ *value = &elem->z_index;
+ break;
+ case HUD_STAT_TEXT2:
+ elem->text2 = luaL_checkstring(L, 4);
+ *value = &elem->text2;
+ break;
}
return stat;
}
@@ -2040,15 +2044,15 @@ HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value)
// Indices must match values in `enum CollisionType` exactly!!
static const char *collision_type_str[] = {
- "node",
- "object",
+ "node",
+ "object",
};
// Indices must match values in `enum CollisionAxis` exactly!!
static const char *collision_axis_str[] = {
- "x",
- "y",
- "z",
+ "x",
+ "y",
+ "z",
};
void push_collision_move_result(lua_State *L, const collisionMoveResult &res)
diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h
index 49ab96fd5..5a8bf6700 100644
--- a/src/script/common/c_content.h
+++ b/src/script/common/c_content.h
@@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+
/******************************************************************************/
/******************************************************************************/
/* WARNING!!!! do NOT add this header in any include file or any code file */
@@ -40,10 +41,7 @@ extern "C" {
#include "c_types.h"
#include "hud.h"
-namespace Json
-{
-class Value;
-}
+namespace Json { class Value; }
struct MapNode;
class NodeDefManager;
@@ -69,96 +67,138 @@ struct collisionMoveResult;
extern struct EnumString es_TileAnimationType[];
-ContentFeatures read_content_features(lua_State *L, int index);
-void push_content_features(lua_State *L, const ContentFeatures &c);
+ContentFeatures read_content_features (lua_State *L, int index);
+void push_content_features (lua_State *L,
+ const ContentFeatures &c);
-void push_nodebox(lua_State *L, const NodeBox &box);
-void push_box(lua_State *L, const std::vector<aabb3f> &box);
+void push_nodebox (lua_State *L,
+ const NodeBox &box);
+void push_box (lua_State *L,
+ const std::vector<aabb3f> &box);
-void push_palette(lua_State *L, const std::vector<video::SColor> *palette);
+void push_palette (lua_State *L,
+ const std::vector<video::SColor> *palette);
-TileDef read_tiledef(lua_State *L, int index, u8 drawtype);
+TileDef read_tiledef (lua_State *L, int index,
+ u8 drawtype);
-void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec);
-NodeBox read_nodebox(lua_State *L, int index);
+void read_soundspec (lua_State *L, int index,
+ SimpleSoundSpec &spec);
+NodeBox read_nodebox (lua_State *L, int index);
-void read_server_sound_params(lua_State *L, int index, ServerSoundParams &params);
+void read_server_sound_params (lua_State *L, int index,
+ ServerSoundParams &params);
-void push_dig_params(lua_State *L, const DigParams &params);
-void push_hit_params(lua_State *L, const HitParams &params);
+void push_dig_params (lua_State *L,
+ const DigParams &params);
+void push_hit_params (lua_State *L,
+ const HitParams &params);
-ItemStack read_item(lua_State *L, int index, IItemDefManager *idef);
+ItemStack read_item (lua_State *L, int index, IItemDefManager *idef);
struct TileAnimationParams read_animation_definition(lua_State *L, int index);
-ToolCapabilities read_tool_capabilities(lua_State *L, int table);
-void push_tool_capabilities(lua_State *L, const ToolCapabilities &prop);
+ToolCapabilities read_tool_capabilities (lua_State *L, int table);
+void push_tool_capabilities (lua_State *L,
+ const ToolCapabilities &prop);
-void read_item_definition(lua_State *L, int index, const ItemDefinition &default_def,
+void read_item_definition (lua_State *L, int index, const ItemDefinition &default_def,
ItemDefinition &def);
-void push_item_definition(lua_State *L, const ItemDefinition &i);
-void push_item_definition_full(lua_State *L, const ItemDefinition &i);
-
-void read_object_properties(lua_State *L, int index, ServerActiveObject *sao,
- ObjectProperties *prop, IItemDefManager *idef);
-void push_object_properties(lua_State *L, ObjectProperties *prop);
-
-void push_inventory(lua_State *L, Inventory *inventory);
-
-void push_inventory_list(lua_State *L, Inventory *inv, const char *name);
-void read_inventory_list(lua_State *L, int tableindex, Inventory *inv, const char *name,
- Server *srv, int forcesize = -1);
-
-MapNode readnode(lua_State *L, int index, const NodeDefManager *ndef);
-void pushnode(lua_State *L, const MapNode &n, const NodeDefManager *ndef);
-
-void read_groups(lua_State *L, int index, ItemGroupList &result);
-
-void push_groups(lua_State *L, const ItemGroupList &groups);
-
-// TODO rename to "read_enum_field"
-int getenumfield(lua_State *L, int table, const char *fieldname, const EnumString *spec,
- int default_);
-
-bool getflagsfield(lua_State *L, int table, const char *fieldname, FlagDesc *flagdesc,
- u32 *flags, u32 *flagmask);
-
-bool read_flags(lua_State *L, int index, FlagDesc *flagdesc, u32 *flags, u32 *flagmask);
-
-void push_flags_string(lua_State *L, FlagDesc *flagdesc, u32 flags, u32 flagmask);
-
-u32 read_flags_table(lua_State *L, int table, FlagDesc *flagdesc, u32 *flagmask);
-
-void push_items(lua_State *L, const std::vector<ItemStack> &items);
-
-std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv);
-
-void push_soundspec(lua_State *L, const SimpleSoundSpec &spec);
-
-bool string_to_enum(const EnumString *spec, int &result, const std::string &str);
-
-bool read_noiseparams(lua_State *L, int index, NoiseParams *np);
-void push_noiseparams(lua_State *L, NoiseParams *np);
-
-void luaentity_get(lua_State *L, u16 id);
-
-bool push_json_value(lua_State *L, const Json::Value &value, int nullindex);
-void read_json_value(lua_State *L, Json::Value &root, int index, u8 recursion = 0);
+void push_item_definition (lua_State *L,
+ const ItemDefinition &i);
+void push_item_definition_full (lua_State *L,
+ const ItemDefinition &i);
+
+void read_object_properties (lua_State *L, int index,
+ ServerActiveObject *sao,
+ ObjectProperties *prop,
+ IItemDefManager *idef);
+void push_object_properties (lua_State *L,
+ ObjectProperties *prop);
+
+void push_inventory (lua_State *L,
+ Inventory *inventory);
+
+void push_inventory_list (lua_State *L,
+ Inventory *inv,
+ const char *name);
+void read_inventory_list (lua_State *L, int tableindex,
+ Inventory *inv, const char *name,
+ Server *srv, int forcesize=-1);
+
+MapNode readnode (lua_State *L, int index,
+ const NodeDefManager *ndef);
+void pushnode (lua_State *L, const MapNode &n,
+ const NodeDefManager *ndef);
+
+
+void read_groups (lua_State *L, int index,
+ ItemGroupList &result);
+
+void push_groups (lua_State *L,
+ const ItemGroupList &groups);
+
+//TODO rename to "read_enum_field"
+int getenumfield (lua_State *L, int table,
+ const char *fieldname,
+ const EnumString *spec,
+ int default_);
+
+bool getflagsfield (lua_State *L, int table,
+ const char *fieldname,
+ FlagDesc *flagdesc,
+ u32 *flags, u32 *flagmask);
+
+bool read_flags (lua_State *L, int index,
+ FlagDesc *flagdesc,
+ u32 *flags, u32 *flagmask);
+
+void push_flags_string (lua_State *L, FlagDesc *flagdesc,
+ u32 flags, u32 flagmask);
+
+u32 read_flags_table (lua_State *L, int table,
+ FlagDesc *flagdesc, u32 *flagmask);
+
+void push_items (lua_State *L,
+ const std::vector<ItemStack> &items);
+
+std::vector<ItemStack> read_items (lua_State *L,
+ int index,
+ Server* srv);
+
+void push_soundspec (lua_State *L,
+ const SimpleSoundSpec &spec);
+
+bool string_to_enum (const EnumString *spec,
+ int &result,
+ const std::string &str);
+
+bool read_noiseparams (lua_State *L, int index,
+ NoiseParams *np);
+void push_noiseparams (lua_State *L, NoiseParams *np);
+
+void luaentity_get (lua_State *L,u16 id);
+
+bool push_json_value (lua_State *L,
+ const Json::Value &value,
+ int nullindex);
+void read_json_value (lua_State *L, Json::Value &root,
+ int index, u8 recursion = 0);
/*!
* Pushes a Lua `pointed_thing` to the given Lua stack.
* \param csm If true, a client side pointed thing is pushed
* \param hitpoint If true, the exact pointing location is also pushed
*/
-void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm = false,
- bool hitpoint = false);
+void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm =
+ false, bool hitpoint = false);
-void push_objectRef(lua_State *L, const u16 id);
+void push_objectRef (lua_State *L, const u16 id);
-void read_hud_element(lua_State *L, HudElement *elem);
+void read_hud_element (lua_State *L, HudElement *elem);
-void push_hud_element(lua_State *L, HudElement *elem);
+void push_hud_element (lua_State *L, HudElement *elem);
-HudElementStat read_hud_change(lua_State *L, HudElement *elem, void **value);
+HudElementStat read_hud_change (lua_State *L, HudElement *elem, void **value);
-void push_collision_move_result(lua_State *L, const collisionMoveResult &res);
+void push_collision_move_result(lua_State *L, const collisionMoveResult &res);
diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp
index 349f148d3..eb6ab5331 100644
--- a/src/script/common/c_converter.cpp
+++ b/src/script/common/c_converter.cpp
@@ -30,27 +30,27 @@ extern "C" {
#include "constants.h"
#include <set>
-#define CHECK_TYPE(index, name, type) \
- { \
- int t = lua_type(L, (index)); \
- if (t != (type)) { \
- throw LuaError(std::string("Invalid ") + (name) + \
- " (expected " + lua_typename(L, (type)) + \
- " got " + lua_typename(L, t) + ")."); \
- } \
- }
-#define CHECK_POS_COORD(name) \
- CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
-#define CHECK_FLOAT_RANGE(value, name) \
- if (value < F1000_MIN || value > F1000_MAX) { \
- std::ostringstream error_text; \
- error_text << "Invalid float vector dimension range '" name "' " \
- << "(expected " << F1000_MIN << " < " name " < " << F1000_MAX \
- << " got " << value << ")." << std::endl; \
- throw LuaError(error_text.str()); \
+
+#define CHECK_TYPE(index, name, type) { \
+ int t = lua_type(L, (index)); \
+ if (t != (type)) { \
+ throw LuaError(std::string("Invalid ") + (name) + \
+ " (expected " + lua_typename(L, (type)) + \
+ " got " + lua_typename(L, t) + ")."); \
+ } \
}
+#define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
+#define CHECK_FLOAT_RANGE(value, name) \
+if (value < F1000_MIN || value > F1000_MAX) { \
+ std::ostringstream error_text; \
+ error_text << "Invalid float vector dimension range '" name "' " << \
+ "(expected " << F1000_MIN << " < " name " < " << F1000_MAX << \
+ " got " << value << ")." << std::endl; \
+ throw LuaError(error_text.str()); \
+}
#define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE)
+
void push_float_string(lua_State *L, float value)
{
std::stringstream ss;
@@ -360,7 +360,7 @@ bool is_color_table(lua_State *L, int index)
aabb3f read_aabb3f(lua_State *L, int index, f32 scale)
{
aabb3f box;
- if (lua_istable(L, index)) {
+ if(lua_istable(L, index)){
lua_rawgeti(L, index, 1);
box.MinEdge.X = lua_tonumber(L, -1) * scale;
lua_pop(L, 1);
@@ -404,22 +404,22 @@ void push_aabb3f(lua_State *L, aabb3f box)
std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale)
{
std::vector<aabb3f> boxes;
- if (lua_istable(L, index)) {
+ if(lua_istable(L, index)){
int n = lua_objlen(L, index);
// Check if it's a single box or a list of boxes
bool possibly_single_box = (n == 6);
- for (int i = 1; i <= n && possibly_single_box; i++) {
+ for(int i = 1; i <= n && possibly_single_box; i++){
lua_rawgeti(L, index, i);
- if (!lua_isnumber(L, -1))
+ if(!lua_isnumber(L, -1))
possibly_single_box = false;
lua_pop(L, 1);
}
- if (possibly_single_box) {
+ if(possibly_single_box){
// Read a single box
boxes.push_back(read_aabb3f(L, index, scale));
} else {
// Read a list of boxes
- for (int i = 1; i <= n; i++) {
+ for(int i = 1; i <= n; i++){
lua_rawgeti(L, index, i);
boxes.push_back(read_aabb3f(L, -1, scale));
lua_pop(L, 1);
@@ -487,8 +487,8 @@ bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname
// Types mismatch. Log unique line.
std::string backtrace = std::string("Invalid field ") + fieldname +
- " (expected " + lua_typename(L, type) + " got " +
- lua_typename(L, t) + ").\n" + script_get_backtrace(L);
+ " (expected " + lua_typename(L, type) +
+ " got " + lua_typename(L, t) + ").\n" + script_get_backtrace(L);
u64 hash = murmur_hash_64_ua(backtrace.data(), backtrace.length(), 0xBADBABE);
if (warned_msgs.find(hash) == warned_msgs.end()) {
@@ -499,7 +499,8 @@ bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname
return false;
}
-bool getstringfield(lua_State *L, int table, const char *fieldname, std::string &result)
+bool getstringfield(lua_State *L, int table,
+ const char *fieldname, std::string &result)
{
lua_getfield(L, table, fieldname);
bool got = false;
@@ -516,7 +517,8 @@ bool getstringfield(lua_State *L, int table, const char *fieldname, std::string
return got;
}
-bool getfloatfield(lua_State *L, int table, const char *fieldname, float &result)
+bool getfloatfield(lua_State *L, int table,
+ const char *fieldname, float &result)
{
lua_getfield(L, table, fieldname);
bool got = false;
@@ -529,12 +531,13 @@ bool getfloatfield(lua_State *L, int table, const char *fieldname, float &result
return got;
}
-bool getboolfield(lua_State *L, int table, const char *fieldname, bool &result)
+bool getboolfield(lua_State *L, int table,
+ const char *fieldname, bool &result)
{
lua_getfield(L, table, fieldname);
bool got = false;
- if (check_field_or_nil(L, -1, LUA_TBOOLEAN, fieldname)) {
+ if (check_field_or_nil(L, -1, LUA_TBOOLEAN, fieldname)){
result = lua_toboolean(L, -1);
got = true;
}
@@ -553,82 +556,93 @@ size_t getstringlistfield(lua_State *L, int table, const char *fieldname,
return num_strings_read;
}
-std::string getstringfield_default(lua_State *L, int table, const char *fieldname,
- const std::string &default_)
+std::string getstringfield_default(lua_State *L, int table,
+ const char *fieldname, const std::string &default_)
{
std::string result = default_;
getstringfield(L, table, fieldname, result);
return result;
}
-int getintfield_default(lua_State *L, int table, const char *fieldname, int default_)
+int getintfield_default(lua_State *L, int table,
+ const char *fieldname, int default_)
{
int result = default_;
getintfield(L, table, fieldname, result);
return result;
}
-float getfloatfield_default(
- lua_State *L, int table, const char *fieldname, float default_)
+float getfloatfield_default(lua_State *L, int table,
+ const char *fieldname, float default_)
{
float result = default_;
getfloatfield(L, table, fieldname, result);
return result;
}
-bool getboolfield_default(lua_State *L, int table, const char *fieldname, bool default_)
+bool getboolfield_default(lua_State *L, int table,
+ const char *fieldname, bool default_)
{
bool result = default_;
getboolfield(L, table, fieldname, result);
return result;
}
-v3s16 getv3s16field_default(
- lua_State *L, int table, const char *fieldname, v3s16 default_)
+v3s16 getv3s16field_default(lua_State *L, int table,
+ const char *fieldname, v3s16 default_)
{
getv3intfield(L, table, fieldname, default_);
return default_;
}
-void setstringfield(
- lua_State *L, int table, const char *fieldname, const std::string &value)
+void setstringfield(lua_State *L, int table,
+ const char *fieldname, const std::string &value)
{
lua_pushlstring(L, value.c_str(), value.length());
- if (table < 0)
+ if(table < 0)
table -= 1;
lua_setfield(L, table, fieldname);
}
-void setintfield(lua_State *L, int table, const char *fieldname, int value)
+void setintfield(lua_State *L, int table,
+ const char *fieldname, int value)
{
lua_pushinteger(L, value);
- if (table < 0)
+ if(table < 0)
table -= 1;
lua_setfield(L, table, fieldname);
}
-void setfloatfield(lua_State *L, int table, const char *fieldname, float value)
+void setfloatfield(lua_State *L, int table,
+ const char *fieldname, float value)
{
lua_pushnumber(L, value);
- if (table < 0)
+ if(table < 0)
table -= 1;
lua_setfield(L, table, fieldname);
}
-void setboolfield(lua_State *L, int table, const char *fieldname, bool value)
+void setboolfield(lua_State *L, int table,
+ const char *fieldname, bool value)
{
lua_pushboolean(L, value);
- if (table < 0)
+ if(table < 0)
table -= 1;
lua_setfield(L, table, fieldname);
}
+
////
//// Array table slices
////
-size_t write_array_slice_float(lua_State *L, int table_index, float *data,
- v3u16 data_size, v3u16 slice_offset, v3u16 slice_size)
+size_t write_array_slice_float(
+ lua_State *L,
+ int table_index,
+ float *data,
+ v3u16 data_size,
+ v3u16 slice_offset,
+ v3u16 slice_size)
{
v3u16 pmin, pmax(data_size);
@@ -655,19 +669,25 @@ size_t write_array_slice_float(lua_State *L, int table_index, float *data,
u32 elem_index = 1;
for (u32 z = pmin.Z; z != pmax.Z; z++)
- for (u32 y = pmin.Y; y != pmax.Y; y++)
- for (u32 x = pmin.X; x != pmax.X; x++) {
- u32 i = z * zstride + y * ystride + x;
- lua_pushnumber(L, data[i]);
- lua_rawseti(L, table_index, elem_index);
- elem_index++;
- }
+ for (u32 y = pmin.Y; y != pmax.Y; y++)
+ for (u32 x = pmin.X; x != pmax.X; x++) {
+ u32 i = z * zstride + y * ystride + x;
+ lua_pushnumber(L, data[i]);
+ lua_rawseti(L, table_index, elem_index);
+ elem_index++;
+ }
return elem_index - 1;
}
-size_t write_array_slice_u16(lua_State *L, int table_index, u16 *data, v3u16 data_size,
- v3u16 slice_offset, v3u16 slice_size)
+
+size_t write_array_slice_u16(
+ lua_State *L,
+ int table_index,
+ u16 *data,
+ v3u16 data_size,
+ v3u16 slice_offset,
+ v3u16 slice_size)
{
v3u16 pmin, pmax(data_size);
@@ -694,13 +714,13 @@ size_t write_array_slice_u16(lua_State *L, int table_index, u16 *data, v3u16 dat
u32 elem_index = 1;
for (u32 z = pmin.Z; z != pmax.Z; z++)
- for (u32 y = pmin.Y; y != pmax.Y; y++)
- for (u32 x = pmin.X; x != pmax.X; x++) {
- u32 i = z * zstride + y * ystride + x;
- lua_pushinteger(L, data[i]);
- lua_rawseti(L, table_index, elem_index);
- elem_index++;
- }
+ for (u32 y = pmin.Y; y != pmax.Y; y++)
+ for (u32 x = pmin.X; x != pmax.X; x++) {
+ u32 i = z * zstride + y * ystride + x;
+ lua_pushinteger(L, data[i]);
+ lua_rawseti(L, table_index, elem_index);
+ elem_index++;
+ }
return elem_index - 1;
}
diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h
index 16d21db3b..a4a7079fd 100644
--- a/src/script/common/c_converter.h
+++ b/src/script/common/c_converter.h
@@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+
/******************************************************************************/
/******************************************************************************/
/* WARNING!!!! do NOT add this header in any include file or any code file */
@@ -35,21 +36,24 @@ extern "C" {
#include <lua.h>
}
-std::string getstringfield_default(lua_State *L, int table, const char *fieldname,
- const std::string &default_);
-bool getboolfield_default(lua_State *L, int table, const char *fieldname, bool default_);
-float getfloatfield_default(
- lua_State *L, int table, const char *fieldname, float default_);
-int getintfield_default(lua_State *L, int table, const char *fieldname, int default_);
+std::string getstringfield_default(lua_State *L, int table,
+ const char *fieldname, const std::string &default_);
+bool getboolfield_default(lua_State *L, int table,
+ const char *fieldname, bool default_);
+float getfloatfield_default(lua_State *L, int table,
+ const char *fieldname, float default_);
+int getintfield_default(lua_State *L, int table,
+ const char *fieldname, int default_);
bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname);
-template <typename T>
-bool getintfield(lua_State *L, int table, const char *fieldname, T &result)
+template<typename T>
+bool getintfield(lua_State *L, int table,
+ const char *fieldname, T &result)
{
lua_getfield(L, table, fieldname);
bool got = false;
- if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)) {
+ if (check_field_or_nil(L, -1, LUA_TNUMBER, fieldname)){
result = lua_tointeger(L, -1);
got = true;
}
@@ -57,8 +61,9 @@ bool getintfield(lua_State *L, int table, const char *fieldname, T &result)
return got;
}
-template <class T>
-bool getv3intfield(lua_State *L, int index, const char *fieldname, T &result)
+template<class T>
+bool getv3intfield(lua_State *L, int index,
+ const char *fieldname, T &result)
{
lua_getfield(L, index, fieldname);
bool got = false;
@@ -71,54 +76,65 @@ bool getv3intfield(lua_State *L, int index, const char *fieldname, T &result)
return got;
}
-v3s16 getv3s16field_default(
- lua_State *L, int table, const char *fieldname, v3s16 default_);
-bool getstringfield(lua_State *L, int table, const char *fieldname, std::string &result);
-size_t getstringlistfield(lua_State *L, int table, const char *fieldname,
- std::vector<std::string> *result);
-void read_groups(lua_State *L, int index, std::unordered_map<std::string, int> &result);
-bool getboolfield(lua_State *L, int table, const char *fieldname, bool &result);
-bool getfloatfield(lua_State *L, int table, const char *fieldname, float &result);
-
-void setstringfield(
- lua_State *L, int table, const char *fieldname, const std::string &value);
-void setintfield(lua_State *L, int table, const char *fieldname, int value);
-void setfloatfield(lua_State *L, int table, const char *fieldname, float value);
-void setboolfield(lua_State *L, int table, const char *fieldname, bool value);
-
-v3f checkFloatPos(lua_State *L, int index);
-v3f check_v3f(lua_State *L, int index);
-v3s16 check_v3s16(lua_State *L, int index);
-
-v3f read_v3f(lua_State *L, int index);
-v2f read_v2f(lua_State *L, int index);
-v2s16 read_v2s16(lua_State *L, int index);
-v2s32 read_v2s32(lua_State *L, int index);
-video::SColor read_ARGB8(lua_State *L, int index);
-bool read_color(lua_State *L, int index, video::SColor *color);
-bool is_color_table(lua_State *L, int index);
-
-aabb3f read_aabb3f(lua_State *L, int index, f32 scale);
-v3s16 read_v3s16(lua_State *L, int index);
-std::vector<aabb3f> read_aabb3f_vector(lua_State *L, int index, f32 scale);
-size_t read_stringlist(lua_State *L, int index, std::vector<std::string> *result);
-
-void push_float_string(lua_State *L, float value);
-void push_v3_float_string(lua_State *L, v3f p);
-void push_v2_float_string(lua_State *L, v2f p);
-void push_v2s16(lua_State *L, v2s16 p);
-void push_v2s32(lua_State *L, v2s32 p);
-void push_v3s16(lua_State *L, v3s16 p);
-void push_aabb3f(lua_State *L, aabb3f box);
-void push_ARGB8(lua_State *L, video::SColor color);
-void pushFloatPos(lua_State *L, v3f p);
-void push_v3f(lua_State *L, v3f p);
-void push_v2f(lua_State *L, v2f p);
-
-void warn_if_field_exists(lua_State *L, int table, const char *fieldname,
- const std::string &message);
+v3s16 getv3s16field_default(lua_State *L, int table,
+ const char *fieldname, v3s16 default_);
+bool getstringfield(lua_State *L, int table,
+ const char *fieldname, std::string &result);
+size_t getstringlistfield(lua_State *L, int table,
+ const char *fieldname,
+ std::vector<std::string> *result);
+void read_groups(lua_State *L, int index,
+ std::unordered_map<std::string, int> &result);
+bool getboolfield(lua_State *L, int table,
+ const char *fieldname, bool &result);
+bool getfloatfield(lua_State *L, int table,
+ const char *fieldname, float &result);
+
+void setstringfield(lua_State *L, int table,
+ const char *fieldname, const std::string &value);
+void setintfield(lua_State *L, int table,
+ const char *fieldname, int value);
+void setfloatfield(lua_State *L, int table,
+ const char *fieldname, float value);
+void setboolfield(lua_State *L, int table,
+ const char *fieldname, bool value);
+
+v3f checkFloatPos (lua_State *L, int index);
+v3f check_v3f (lua_State *L, int index);
+v3s16 check_v3s16 (lua_State *L, int index);
+
+v3f read_v3f (lua_State *L, int index);
+v2f read_v2f (lua_State *L, int index);
+v2s16 read_v2s16 (lua_State *L, int index);
+v2s32 read_v2s32 (lua_State *L, int index);
+video::SColor read_ARGB8 (lua_State *L, int index);
+bool read_color (lua_State *L, int index,
+ video::SColor *color);
+bool is_color_table (lua_State *L, int index);
+
+aabb3f read_aabb3f (lua_State *L, int index, f32 scale);
+v3s16 read_v3s16 (lua_State *L, int index);
+std::vector<aabb3f> read_aabb3f_vector (lua_State *L, int index, f32 scale);
+size_t read_stringlist (lua_State *L, int index,
+ std::vector<std::string> *result);
+
+void push_float_string (lua_State *L, float value);
+void push_v3_float_string(lua_State *L, v3f p);
+void push_v2_float_string(lua_State *L, v2f p);
+void push_v2s16 (lua_State *L, v2s16 p);
+void push_v2s32 (lua_State *L, v2s32 p);
+void push_v3s16 (lua_State *L, v3s16 p);
+void push_aabb3f (lua_State *L, aabb3f box);
+void push_ARGB8 (lua_State *L, video::SColor color);
+void pushFloatPos (lua_State *L, v3f p);
+void push_v3f (lua_State *L, v3f p);
+void push_v2f (lua_State *L, v2f p);
+
+void warn_if_field_exists(lua_State *L, int table,
+ const char *fieldname,
+ const std::string &message);
size_t write_array_slice_float(lua_State *L, int table_index, float *data,
- v3u16 data_size, v3u16 slice_offset, v3u16 slice_size);
-size_t write_array_slice_u16(lua_State *L, int table_index, u16 *data, v3u16 data_size,
- v3u16 slice_offset, v3u16 slice_size);
+ v3u16 data_size, v3u16 slice_offset, v3u16 slice_size);
+size_t write_array_slice_u16(lua_State *L, int table_index, u16 *data,
+ v3u16 data_size, v3u16 slice_offset, v3u16 slice_size);
diff --git a/src/script/common/c_internal.cpp b/src/script/common/c_internal.cpp
index 19a874f63..6df1f8b7b 100644
--- a/src/script/common/c_internal.cpp
+++ b/src/script/common/c_internal.cpp
@@ -35,13 +35,13 @@ std::string script_get_backtrace(lua_State *L)
int script_exception_wrapper(lua_State *L, lua_CFunction f)
{
try {
- return f(L); // Call wrapped function and return result.
- } catch (const char *s) { // Catch and convert exceptions.
+ return f(L); // Call wrapped function and return result.
+ } catch (const char *s) { // Catch and convert exceptions.
lua_pushstring(L, s);
} catch (std::exception &e) {
lua_pushstring(L, e.what());
}
- return lua_error(L); // Rethrow as a Lua error.
+ return lua_error(L); // Rethrow as a Lua error.
}
/*
@@ -85,15 +85,15 @@ void script_error(lua_State *L, int pcall_result, const char *mod, const char *f
err_descr = "<no description>";
char buf[256];
- porting::mt_snprintf(buf, sizeof(buf),
- "%s error from mod '%s' in callback %s(): ", err_type, mod, fxn);
+ porting::mt_snprintf(buf, sizeof(buf), "%s error from mod '%s' in callback %s(): ",
+ err_type, mod, fxn);
std::string err_msg(buf);
err_msg += err_descr;
if (pcall_result == LUA_ERRMEM) {
- err_msg += "\nCurrent Lua memory usage: " +
- itos(lua_gc(L, LUA_GCCOUNT, 0) >> 10) + " MB";
+ err_msg += "\nCurrent Lua memory usage: "
+ + itos(lua_gc(L, LUA_GCCOUNT, 0) >> 10) + " MB";
}
throw LuaError(err_msg);
@@ -105,8 +105,8 @@ void script_error(lua_State *L, int pcall_result, const char *mod, const char *f
// - runs the callbacks
// - replaces the table and arguments with the return value,
// computed depending on mode
-void script_run_callbacks_f(
- lua_State *L, int nargs, RunCallbacksMode mode, const char *fxn)
+void script_run_callbacks_f(lua_State *L, int nargs,
+ RunCallbacksMode mode, const char *fxn)
{
FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");
@@ -122,7 +122,7 @@ void script_run_callbacks_f(
lua_insert(L, error_handler + 1);
// Insert mode after table
- lua_pushnumber(L, (int)mode);
+ lua_pushnumber(L, (int) mode);
lua_insert(L, error_handler + 3);
// Stack now looks like this:
@@ -135,8 +135,8 @@ void script_run_callbacks_f(
lua_remove(L, error_handler);
}
-static void script_log(lua_State *L, const std::string &message, std::ostream &log_to,
- bool do_error, int stack_depth)
+static void script_log(lua_State *L, const std::string &message,
+ std::ostream &log_to, bool do_error, int stack_depth)
{
lua_Debug ar;
diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h
index 677194453..442546332 100644
--- a/src/script/common/c_internal.h
+++ b/src/script/common/c_internal.h
@@ -34,6 +34,7 @@ extern "C" {
#include "config.h"
#include "common/c_types.h"
+
/*
Define our custom indices into the Lua registry table.
@@ -44,15 +45,15 @@ extern "C" {
so we can use numeric indices freely.
*/
#ifdef LUA_RIDX_LAST
-#define CUSTOM_RIDX_BASE ((LUA_RIDX_LAST) + 1)
+#define CUSTOM_RIDX_BASE ((LUA_RIDX_LAST)+1)
#else
#define CUSTOM_RIDX_BASE 1
#endif
-#define CUSTOM_RIDX_SCRIPTAPI (CUSTOM_RIDX_BASE)
-#define CUSTOM_RIDX_GLOBALS_BACKUP (CUSTOM_RIDX_BASE + 1)
-#define CUSTOM_RIDX_CURRENT_MOD_NAME (CUSTOM_RIDX_BASE + 2)
-#define CUSTOM_RIDX_BACKTRACE (CUSTOM_RIDX_BASE + 3)
+#define CUSTOM_RIDX_SCRIPTAPI (CUSTOM_RIDX_BASE)
+#define CUSTOM_RIDX_GLOBALS_BACKUP (CUSTOM_RIDX_BASE + 1)
+#define CUSTOM_RIDX_CURRENT_MOD_NAME (CUSTOM_RIDX_BASE + 2)
+#define CUSTOM_RIDX_BACKTRACE (CUSTOM_RIDX_BASE + 3)
// Determine if CUSTOM_RIDX_SCRIPTAPI will hold a light or full userdata
#if defined(__aarch64__) && USE_LUAJIT
@@ -64,18 +65,17 @@ extern "C" {
#endif
// Pushes the error handler onto the stack and returns its index
-#define PUSH_ERROR_HANDLER(L) \
+#define PUSH_ERROR_HANDLER(L) \
(lua_rawgeti((L), LUA_REGISTRYINDEX, CUSTOM_RIDX_BACKTRACE), lua_gettop((L)))
-#define PCALL_RESL(L, RES) \
- { \
- int result_ = (RES); \
- if (result_ != 0) { \
- script_error((L), result_, NULL, __FUNCTION__); \
- } \
- }
+#define PCALL_RESL(L, RES) { \
+ int result_ = (RES); \
+ if (result_ != 0) { \
+ script_error((L), result_, NULL, __FUNCTION__); \
+ } \
+}
-#define script_run_callbacks(L, nargs, mode) \
+#define script_run_callbacks(L, nargs, mode) \
script_run_callbacks_f((L), (nargs), (mode), __FUNCTION__)
// What script_run_callbacks does with the return values of callbacks.
@@ -111,7 +111,8 @@ enum RunCallbacksMode
std::string script_get_backtrace(lua_State *L);
int script_exception_wrapper(lua_State *L, lua_CFunction f);
void script_error(lua_State *L, int pcall_result, const char *mod, const char *fxn);
-void script_run_callbacks_f(
- lua_State *L, int nargs, RunCallbacksMode mode, const char *fxn);
+void script_run_callbacks_f(lua_State *L, int nargs,
+ RunCallbacksMode mode, const char *fxn);
-void log_deprecated(lua_State *L, const std::string &message, int stack_depth = 1);
+void log_deprecated(lua_State *L, const std::string &message,
+ int stack_depth=1);
diff --git a/src/script/common/c_types.cpp b/src/script/common/c_types.cpp
index 93bb5bd70..e832ff2ab 100644
--- a/src/script/common/c_types.cpp
+++ b/src/script/common/c_types.cpp
@@ -23,10 +23,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_internal.h"
#include "itemdef.h"
-struct EnumString es_ItemType[] = {
+
+struct EnumString es_ItemType[] =
+ {
{ITEM_NONE, "none"},
{ITEM_NODE, "node"},
{ITEM_CRAFT, "craft"},
{ITEM_TOOL, "tool"},
{0, NULL},
-};
+ };
diff --git a/src/script/common/c_types.h b/src/script/common/c_types.h
index 860bfaaa8..86bfb0b6b 100644
--- a/src/script/common/c_types.h
+++ b/src/script/common/c_types.h
@@ -38,9 +38,10 @@ class StackUnroller
private:
lua_State *m_lua;
int m_original_top;
-
public:
- StackUnroller(lua_State *L) : m_lua(L), m_original_top(-1)
+ StackUnroller(lua_State *L):
+ m_lua(L),
+ m_original_top(-1)
{
m_original_top = lua_gettop(m_lua); // store stack height
}
@@ -56,4 +57,5 @@ public:
LuaError(const std::string &s) : ModError(s) {}
};
+
extern EnumString es_ItemType[];