diff options
Diffstat (limited to 'src/script/common/c_content.cpp')
-rw-r--r-- | src/script/common/c_content.cpp | 161 |
1 files changed, 136 insertions, 25 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 52baeae9d..897ca2862 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "object_properties.h" #include "collision.h" #include "cpp_api/s_node.h" +#include "lua_api/l_clientobject.h" #include "lua_api/l_object.h" #include "lua_api/l_item.h" #include "common/c_internal.h" @@ -174,10 +175,12 @@ void push_item_definition_full(lua_State *L, const ItemDefinition &i) } push_groups(L, i.groups); lua_setfield(L, -2, "groups"); + lua_newtable(L); push_soundspec(L, i.sound_place); - lua_setfield(L, -2, "sound_place"); + lua_setfield(L, -2, "place"); push_soundspec(L, i.sound_place_failed); - lua_setfield(L, -2, "sound_place_failed"); + lua_setfield(L, -2, "place_failed"); + lua_setfield(L, -2, "sounds"); lua_pushstring(L, i.node_placement_prediction.c_str()); lua_setfield(L, -2, "node_placement_prediction"); } @@ -197,7 +200,7 @@ void read_object_properties(lua_State *L, int index, if (getintfield(L, -1, "hp_max", hp_max)) { prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX); - if (prop->hp_max < sao->getHP()) { + if (sao && prop->hp_max < sao->getHP()) { PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP); sao->setHP(prop->hp_max, reason); if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) @@ -206,7 +209,7 @@ void read_object_properties(lua_State *L, int index, } if (getintfield(L, -1, "breath_max", prop->breath_max)) { - if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) { + if (sao && sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) { PlayerSAO *player = (PlayerSAO *)sao; if (prop->breath_max < player->getBreath()) player->setBreath(prop->breath_max); @@ -513,6 +516,35 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype) } /******************************************************************************/ +void push_tiledef(lua_State *L, TileDef tiledef) +{ + lua_newtable(L); + setstringfield(L, -1, "name", tiledef.name); + setboolfield(L, -1, "backface_culling", tiledef.backface_culling); + setboolfield(L, -1, "tileable_horizontal", tiledef.tileable_horizontal); + setboolfield(L, -1, "tileable_vertical", tiledef.tileable_vertical); + std::string align_style; + switch (tiledef.align_style) { + case ALIGN_STYLE_USER_DEFINED: + align_style = "user"; + break; + case ALIGN_STYLE_WORLD: + align_style = "world"; + break; + default: + align_style = "node"; + } + setstringfield(L, -1, "align_style", align_style); + setintfield(L, -1, "scale", tiledef.scale); + if (tiledef.has_color) { + push_ARGB8(L, tiledef.color); + lua_setfield(L, -2, "color"); + } + push_animation_definition(L, tiledef.animation); + lua_setfield(L, -2, "animation"); +} + +/******************************************************************************/ void read_content_features(lua_State *L, ContentFeatures &f, int index) { if(index < 0) @@ -833,9 +865,32 @@ void push_content_features(lua_State *L, const ContentFeatures &c) std::string drawtype(ScriptApiNode::es_DrawType[(int)c.drawtype].str); std::string liquid_type(ScriptApiNode::es_LiquidType[(int)c.liquid_type].str); - /* Missing "tiles" because I don't see a usecase (at least not yet). */ + lua_newtable(L); + + // tiles + lua_newtable(L); + for (int i = 0; i < 6; i++) { + push_tiledef(L, c.tiledef[i]); + lua_rawseti(L, -2, i + 1); + } + lua_setfield(L, -2, "tiles"); + // overlay_tiles lua_newtable(L); + for (int i = 0; i < 6; i++) { + push_tiledef(L, c.tiledef_overlay[i]); + lua_rawseti(L, -2, i + 1); + } + lua_setfield(L, -2, "overlay_tiles"); + + // special_tiles + lua_newtable(L); + for (int i = 0; i < CF_SPECIAL_COUNT; i++) { + push_tiledef(L, c.tiledef_special[i]); + lua_rawseti(L, -2, i + 1); + } + lua_setfield(L, -2, "special_tiles"); + lua_pushboolean(L, c.has_on_construct); lua_setfield(L, -2, "has_on_construct"); lua_pushboolean(L, c.has_on_destruct); @@ -939,11 +994,11 @@ void push_content_features(lua_State *L, const ContentFeatures &c) lua_setfield(L, -2, "collision_box"); lua_newtable(L); push_soundspec(L, c.sound_footstep); - lua_setfield(L, -2, "sound_footstep"); + lua_setfield(L, -2, "footstep"); push_soundspec(L, c.sound_dig); - lua_setfield(L, -2, "sound_dig"); + lua_setfield(L, -2, "dig"); push_soundspec(L, c.sound_dug); - lua_setfield(L, -2, "sound_dug"); + lua_setfield(L, -2, "dug"); lua_setfield(L, -2, "sounds"); lua_pushboolean(L, c.legacy_facedir_simple); lua_setfield(L, -2, "legacy_facedir_simple"); @@ -1331,17 +1386,19 @@ void push_tool_capabilities(lua_State *L, } /******************************************************************************/ -void push_inventory_list(lua_State *L, Inventory *inv, const char *name) +void push_inventory(lua_State *L, Inventory *inventory) { - InventoryList *invlist = inv->getList(name); - if(invlist == NULL){ - lua_pushnil(L); - return; + if (! inventory) + throw SerializationError("Attempt to push nonexistant inventory"); + 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(); + lua_pushstring(L, name); + push_inventory_list(L, inventory, name); + lua_rawset(L, -3); } - std::vector<ItemStack> items; - for(u32 i=0; i<invlist->getSize(); i++) - items.push_back(invlist->getItem(i)); - push_items(L, items); } /******************************************************************************/ @@ -1373,6 +1430,19 @@ void read_inventory_list(lua_State *L, int tableindex, } } +void push_inventory_list(lua_State *L, Inventory *inv, const char *name) +{ + InventoryList *invlist = inv->getList(name); + if(invlist == NULL){ + lua_pushnil(L); + return; + } + std::vector<ItemStack> items; + for(u32 i=0; i<invlist->getSize(); i++) + items.push_back(invlist->getItem(i)); + push_items(L, items); +} + /******************************************************************************/ struct TileAnimationParams read_animation_definition(lua_State *L, int index) { @@ -1408,6 +1478,29 @@ struct TileAnimationParams read_animation_definition(lua_State *L, int index) return anim; } +void push_animation_definition(lua_State *L, struct TileAnimationParams anim) +{ + switch (anim.type) { + case TAT_NONE: + lua_pushnil(L); + break; + case TAT_VERTICAL_FRAMES: + lua_newtable(L); + setstringfield(L, -1, "type", "vertical_frames"); + setfloatfield(L, -1, "aspect_w", anim.vertical_frames.aspect_w); + setfloatfield(L, -1, "aspect_h", anim.vertical_frames.aspect_h); + setfloatfield(L, -1, "length", anim.vertical_frames.length); + break; + case TAT_SHEET_2D: + lua_newtable(L); + setstringfield(L, -1, "type", "sheet_2d"); + setintfield(L, -1, "frames_w", anim.sheet_2d.frames_w); + setintfield(L, -1, "frames_h", anim.sheet_2d.frames_h); + setintfield(L, -1, "frame_length", anim.sheet_2d.frame_length); + break; + } +} + /******************************************************************************/ ToolCapabilities read_tool_capabilities( lua_State *L, int table) @@ -1846,14 +1939,8 @@ void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm, } else if (pointed.type == POINTEDTHING_OBJECT) { lua_pushstring(L, "object"); lua_setfield(L, -2, "type"); - - if (csm) { - lua_pushinteger(L, pointed.object_id); - lua_setfield(L, -2, "id"); - } else { - push_objectRef(L, pointed.object_id); - lua_setfield(L, -2, "ref"); - } + push_objectRef(L, pointed.object_id); + lua_setfield(L, -2, "ref"); } else { lua_pushstring(L, "nothing"); lua_setfield(L, -2, "type"); @@ -2107,3 +2194,27 @@ void push_collision_move_result(lua_State *L, const collisionMoveResult &res) lua_setfield(L, -2, "collisions"); /**/ } + +/******************************************************************************/ +void push_physics_override(lua_State *L, float speed, float jump, float gravity, bool sneak, bool sneak_glitch, bool new_move) +{ + lua_createtable(L, 0, 6); + + lua_pushnumber(L, speed); + lua_setfield(L, -2, "speed"); + + lua_pushnumber(L, jump); + lua_setfield(L, -2, "jump"); + + lua_pushnumber(L, gravity); + lua_setfield(L, -2, "gravity"); + + lua_pushboolean(L, sneak); + lua_setfield(L, -2, "sneak"); + + lua_pushboolean(L, sneak_glitch); + lua_setfield(L, -2, "sneak_glitch"); + + lua_pushboolean(L, new_move); + lua_setfield(L, -2, "new_move"); +} |