From 9e7ccedba4d9a94c7f2d20013c55e82aac3fe6a3 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 30 Mar 2012 12:51:51 +0300 Subject: [PATCH] ObjectProperties --- src/CMakeLists.txt | 2 +- src/content_cao.cpp | 107 +++++++----------- src/content_sao.cpp | 61 ++++------ src/content_sao.h | 6 +- src/genericobject.cpp | 63 +++++++---- src/genericobject.h | 16 +-- src/luaentity_common.h | 23 ---- ...ntity_common.cpp => object_properties.cpp} | 25 ++-- src/object_properties.h | 49 ++++++++ src/scriptapi.cpp | 5 +- src/scriptapi.h | 4 +- 11 files changed, 175 insertions(+), 186 deletions(-) rename src/{luaentity_common.cpp => object_properties.cpp} (80%) create mode 100644 src/object_properties.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4e7ac315..fce88704 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -165,7 +165,7 @@ set(common_SRCS nameidmapping.cpp itemdef.cpp nodedef.cpp - luaentity_common.cpp + object_properties.cpp scriptapi.cpp script.cpp log.cpp diff --git a/src/content_cao.cpp b/src/content_cao.cpp index ba2011a5..90e99dc0 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -548,16 +548,7 @@ private: bool m_is_player; bool m_is_local_player; // determined locally // Property-ish things - s16 m_hp_max; - bool m_physical; - float m_weight; - core::aabbox3d m_collisionbox; - std::string m_visual; - v2f m_visual_size; - core::array m_textures; - v2s16 m_spritediv; - bool m_is_visible; - bool m_makes_footstep_sound; + ObjectProperties m_prop; // scene::ISceneManager *m_smgr; IrrlichtDevice *m_irr; @@ -574,6 +565,7 @@ private: // Spritesheet/animation stuff v2f m_tx_size; v2s16 m_tx_basepos; + bool m_initial_tx_basepos_set; bool m_tx_select_horiz_by_yawpitch; int m_anim_frame; int m_anim_num_frames; @@ -591,16 +583,6 @@ public: m_is_player(false), m_is_local_player(false), // - m_hp_max(1), - m_physical(false), - m_weight(5), - m_collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5), - m_visual("sprite"), - m_visual_size(1,1), - m_spritediv(1,1), - m_is_visible(true), - m_makes_footstep_sound(false), - // m_smgr(NULL), m_irr(NULL), m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.), @@ -614,6 +596,7 @@ public: m_hp(1), m_tx_size(1,1), m_tx_basepos(0,0), + m_initial_tx_basepos_set(false), m_tx_select_horiz_by_yawpitch(false), m_anim_frame(0), m_anim_num_frames(1), @@ -623,7 +606,6 @@ public: m_visuals_expired(false), m_step_distance_counter(0) { - m_textures.push_back("unknown_object.png"); if(gamedef == NULL) ClientActiveObject::registerType(getType(), create); } @@ -678,7 +660,7 @@ public: } core::aabbox3d* getSelectionBox() { - if(!m_is_visible || m_is_local_player) + if(!m_prop.is_visible || m_is_local_player) return NULL; return &m_selection_box; } @@ -710,12 +692,12 @@ public: m_visuals_expired = false; - if(!m_is_visible || m_is_local_player) + if(!m_prop.is_visible || m_is_local_player) return; //video::IVideoDriver* driver = smgr->getVideoDriver(); - if(m_visual == "sprite"){ + if(m_prop.visual == "sprite"){ infostream<<"GenericCAO::addToScene(): single_sprite"<addBillboardSceneNode( NULL, v2f(1, 1), v3f(0,0,0), -1); @@ -727,7 +709,7 @@ public: m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true); m_spritenode->setColor(video::SColor(255,0,0,0)); m_spritenode->setVisible(false); /* Set visible when brightness is known */ - m_spritenode->setSize(m_visual_size*BS); + m_spritenode->setSize(m_prop.visual_size*BS); { const float txs = 1.0 / 1; const float tys = 1.0 / 1; @@ -735,11 +717,11 @@ public: txs, tys, 0, 0); } } - else if(m_visual == "upright_sprite") + else if(m_prop.visual == "upright_sprite") { scene::SMesh *mesh = new scene::SMesh(); - double dx = BS*m_visual_size.X/2; - double dy = BS*m_visual_size.Y/2; + double dx = BS*m_prop.visual_size.X/2; + double dy = BS*m_prop.visual_size.Y/2; { // Front scene::IMeshBuffer *buf = new scene::SMeshBuffer(); video::SColor c(255,255,255,255); @@ -788,7 +770,7 @@ public: // This is needed for changing the texture in the future m_meshnode->setReadOnlyMaterials(true); } - else if(m_visual == "cube"){ + else if(m_prop.visual == "cube"){ infostream<<"GenericCAO::addToScene(): cube"<addMeshSceneNode(mesh, NULL); @@ -798,7 +780,7 @@ public: // Will be shown when we know the brightness m_meshnode->setVisible(false); } else { - infostream<<"GenericCAO::addToScene(): \""<tsrc(), m_irr); } - if(m_physical){ - core::aabbox3d box = m_collisionbox; + if(m_prop.physical){ + core::aabbox3d box = m_prop.collisionbox; box.MinEdge *= BS; box.MaxEdge *= BS; collisionMoveResult moveresult; @@ -901,7 +883,7 @@ public: m_step_distance_counter += moved; if(m_step_distance_counter > 1.5*BS){ m_step_distance_counter = 0; - if(!m_is_local_player && m_makes_footstep_sound){ + if(!m_is_local_player && m_prop.makes_footstep_sound){ INodeDefManager *ndef = m_gamedef->ndef(); v3s16 p = floatToInt(getPosition()+v3f(0,-0.5*BS, 0), BS); MapNode n = m_env->getMap().getNodeNoEx(p); @@ -983,11 +965,11 @@ public: if(m_spritenode) { - if(m_visual == "sprite") + if(m_prop.visual == "sprite") { std::string texturestring = "unknown_block.png"; - if(m_textures.size() >= 1) - texturestring = m_textures[0]; + if(m_prop.textures.size() >= 1) + texturestring = m_prop.textures[0]; texturestring += mod; m_spritenode->setMaterialTexture(0, tsrc->getTextureRaw(texturestring)); @@ -995,13 +977,13 @@ public: } if(m_meshnode) { - if(m_visual == "cube") + if(m_prop.visual == "cube") { for (u32 i = 0; i < 6; ++i) { std::string texturestring = "unknown_block.png"; - if(m_textures.size() > i) - texturestring = m_textures[i]; + if(m_prop.textures.size() > i) + texturestring = m_prop.textures[i]; texturestring += mod; AtlasPointer ap = tsrc->getTexture(texturestring); @@ -1019,13 +1001,13 @@ public: material.getTextureMatrix(0).setTextureScale(size.X, size.Y); } } - else if(m_visual == "upright_sprite") + else if(m_prop.visual == "upright_sprite") { scene::IMesh *mesh = m_meshnode->getMesh(); { std::string tname = "unknown_object.png"; - if(m_textures.size() >= 1) - tname = m_textures[0]; + if(m_prop.textures.size() >= 1) + tname = m_prop.textures[0]; tname += mod; scene::IMeshBuffer *buf = mesh->getMeshBuffer(0); buf->getMaterial().setTexture(0, @@ -1033,10 +1015,10 @@ public: } { std::string tname = "unknown_object.png"; - if(m_textures.size() >= 2) - tname = m_textures[1]; - else if(m_textures.size() >= 1) - tname = m_textures[0]; + if(m_prop.textures.size() >= 2) + tname = m_prop.textures[1]; + else if(m_prop.textures.size() >= 1) + tname = m_prop.textures[0]; tname += mod; scene::IMeshBuffer *buf = mesh->getMeshBuffer(1); buf->getMaterial().setTexture(0, @@ -1054,28 +1036,19 @@ public: u8 cmd = readU8(is); if(cmd == GENERIC_CMD_SET_PROPERTIES) { - m_hp_max = readS16(is); - m_physical = readU8(is); - m_weight = readF1000(is); - m_collisionbox.MinEdge = readV3F1000(is); - m_collisionbox.MaxEdge = readV3F1000(is); - m_visual = deSerializeString(is); - m_visual_size = readV2F1000(is); - m_textures.clear(); - u32 texture_count = readU16(is); - for(u32 i=0; igetLua(); scriptapi_luaentity_rm(L, m_id); } - delete m_prop; } void LuaEntitySAO::addedToEnvironment() @@ -390,9 +386,9 @@ void LuaEntitySAO::addedToEnvironment() if(m_registered){ // Get properties - scriptapi_luaentity_get_properties(L, m_id, m_prop); + scriptapi_luaentity_get_properties(L, m_id, &m_prop); // Initialize HP from properties - m_hp = m_prop->hp_max; + m_hp = m_prop.hp_max; } // Activate entity, supplying serialized state @@ -447,8 +443,8 @@ void LuaEntitySAO::step(float dtime, bool send_recommended) m_last_sent_position_timer += dtime; - if(m_prop->physical){ - core::aabbox3d box = m_prop->collisionbox; + if(m_prop.physical){ + core::aabbox3d box = m_prop.collisionbox; box.MinEdge *= BS; box.MaxEdge *= BS; collisionMoveResult moveresult; @@ -511,13 +507,9 @@ std::string LuaEntitySAO::getClientInitializationData() writeV3F1000(os, m_base_position); writeF1000(os, m_yaw); writeS16(os, m_hp); - writeU8(os, 3); // number of messages stuffed in here + writeU8(os, 2); // number of messages stuffed in here os<initial_sprite_basepos, - 1, 1.0, false - )); // return result return os.str(); } @@ -710,18 +702,7 @@ std::string LuaEntitySAO::getName() std::string LuaEntitySAO::getPropertyPacket() { - return gob_cmd_set_properties( - m_prop->hp_max, - m_prop->physical, - m_prop->weight, - m_prop->collisionbox, - m_prop->visual, - m_prop->visual_size, - m_prop->textures, - m_prop->spritediv, - true, // is_visible - false // makes_footstep_sound - ); + return gob_cmd_set_properties(m_prop); } void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end) @@ -778,6 +759,19 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_): m_inventory = &m_player->inventory; m_armor_groups["choppy"] = 2; m_armor_groups["fleshy"] = 3; + + m_prop.hp_max = PLAYER_MAX_HP; + m_prop.physical = false; + m_prop.weight = 75; + m_prop.collisionbox = core::aabbox3d(-1/3.,-1.0,-1/3., 1/3.,1.0,1/3.); + m_prop.visual = "upright_sprite"; + m_prop.visual_size = v2f(1, 2); + m_prop.textures.clear(); + m_prop.textures.push_back("player.png"); + m_prop.textures.push_back("player_back.png"); + m_prop.spritediv = v2s16(1,1); + m_prop.is_visible = (getHP() != 0); + m_prop.makes_footstep_sound = true; } PlayerSAO::~PlayerSAO() @@ -1107,20 +1101,7 @@ void PlayerSAO::createCreativeInventory() std::string PlayerSAO::getPropertyPacket() { - core::array textures; - textures.push_back("player.png"); - textures.push_back("player_back.png"); - return gob_cmd_set_properties( - PLAYER_MAX_HP, - false, - 75, - core::aabbox3d(-1/3.,-1.0,-1/3., 1/3.,1.0,1/3.), - "upright_sprite", - v2f(1, 2), - textures, - v2s16(1,1), - (getHP() != 0), // is_visible - true // makes_footstep_sound - ); + m_prop.is_visible = (getHP() != 0); + return gob_cmd_set_properties(m_prop); } diff --git a/src/content_sao.h b/src/content_sao.h index 48a5078b..f0788cbd 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "content_object.h" #include "itemgroup.h" #include "player.h" +#include "object_properties.h" ServerActiveObject* createItemSAO(ServerEnvironment *env, v3f pos, const std::string itemstring); @@ -32,8 +33,6 @@ ServerActiveObject* createItemSAO(ServerEnvironment *env, v3f pos, LuaEntitySAO needs some internals exposed. */ -struct LuaEntityProperties; - class LuaEntitySAO : public ServerActiveObject { public: @@ -80,7 +79,7 @@ private: std::string m_init_name; std::string m_init_state; bool m_registered; - struct LuaEntityProperties *m_prop; + struct ObjectProperties m_prop; s16 m_hp; v3f m_velocity; @@ -193,6 +192,7 @@ private: ItemGroupList m_armor_groups; bool m_armor_groups_sent; bool m_properties_sent; + struct ObjectProperties m_prop; public: // Some flags used by Server diff --git a/src/genericobject.cpp b/src/genericobject.cpp index 3538f0bf..563b7f7f 100644 --- a/src/genericobject.cpp +++ b/src/genericobject.cpp @@ -21,37 +21,50 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "utility.h" #include -std::string gob_cmd_set_properties( - s16 hp_max, - bool physical, - float weight, - core::aabbox3d collisionbox, - std::string visual, - v2f visual_size, - core::array textures, - v2s16 spritediv, - bool is_visible, - bool makes_footstep_sound -){ +std::string gob_cmd_set_properties(const ObjectProperties &prop) +{ std::ostringstream os(std::ios::binary); writeU8(os, GENERIC_CMD_SET_PROPERTIES); - writeS16(os, hp_max); - writeU8(os, physical); - writeF1000(os, weight); - writeV3F1000(os, collisionbox.MinEdge); - writeV3F1000(os, collisionbox.MaxEdge); - os< #include "irrlichttypes.h" +#include #define GENERIC_CMD_SET_PROPERTIES 0 #define GENERIC_CMD_UPDATE_POSITION 1 @@ -30,18 +31,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #define GENERIC_CMD_PUNCHED 4 #define GENERIC_CMD_UPDATE_ARMOR_GROUPS 5 -std::string gob_cmd_set_properties( - s16 hp_max, - bool physical, - float weight, - core::aabbox3d collisionbox, - std::string visual, - v2f visual_size, - core::array textures, - v2s16 spritediv, - bool is_visible, - bool makes_footstep_sound -); +#include "object_properties.h" +std::string gob_cmd_set_properties(const ObjectProperties &prop); +ObjectProperties gob_read_set_properties(std::istream &is); std::string gob_cmd_update_position( v3f position, diff --git a/src/luaentity_common.h b/src/luaentity_common.h index d12ec9f0..e769cf3d 100644 --- a/src/luaentity_common.h +++ b/src/luaentity_common.h @@ -20,29 +20,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef LUAENTITY_COMMON_HEADER #define LUAENTITY_COMMON_HEADER -#include -#include "irrlichttypes.h" -#include - -struct LuaEntityProperties -{ - // Values are BS=1 - s16 hp_max; - bool physical; - float weight; - core::aabbox3d collisionbox; - std::string visual; - v2f visual_size; - core::array textures; - v2s16 spritediv; - v2s16 initial_sprite_basepos; - - LuaEntityProperties(); - std::string dump(); - void serialize(std::ostream &os); - void deSerialize(std::istream &is); -}; - #define LUAENTITY_CMD_UPDATE_POSITION 0 #define LUAENTITY_CMD_SET_TEXTURE_MOD 1 #define LUAENTITY_CMD_SET_SPRITE 2 diff --git a/src/luaentity_common.cpp b/src/object_properties.cpp similarity index 80% rename from src/luaentity_common.cpp rename to src/object_properties.cpp index 63c3f2d6..edd50c30 100644 --- a/src/luaentity_common.cpp +++ b/src/object_properties.cpp @@ -1,6 +1,6 @@ /* Minetest-c55 -Copyright (C) 2011 celeron55, Perttu Ahola +Copyright (C) 2012 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,14 +17,13 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "luaentity_common.h" - +#include "object_properties.h" #include "utility.h" #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" #define PP2(x) "("<<(x).X<<","<<(x).Y<<")" -LuaEntityProperties::LuaEntityProperties(): +ObjectProperties::ObjectProperties(): hp_max(1), physical(false), weight(5), @@ -32,12 +31,14 @@ LuaEntityProperties::LuaEntityProperties(): visual("sprite"), visual_size(1,1), spritediv(1,1), - initial_sprite_basepos(0,0) + initial_sprite_basepos(0,0), + is_visible(true), + makes_footstep_sound(false) { textures.push_back("unknown_object.png"); } -std::string LuaEntityProperties::dump() +std::string ObjectProperties::dump() { std::ostringstream os(std::ios::binary); os<<"hp_max="< + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef OBJECT_PROPERTIES_HEADER +#define OBJECT_PROPERTIES_HEADER + +#include +#include "irrlichttypes.h" +#include + +struct ObjectProperties +{ + // Values are BS=1 + s16 hp_max; + bool physical; + float weight; + core::aabbox3d collisionbox; + std::string visual; + v2f visual_size; + core::array textures; + v2s16 spritediv; + v2s16 initial_sprite_basepos; + bool is_visible; + bool makes_footstep_sound; + + ObjectProperties(); + std::string dump(); + void serialize(std::ostream &os); + void deSerialize(std::istream &is); +}; + +#endif + diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 4c40fa58..1ef6d0e7 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -33,8 +33,7 @@ extern "C" { #include "filesys.h" #include "serverobject.h" #include "script.h" -//#include "luna.h" -#include "luaentity_common.h" +#include "object_properties.h" #include "content_sao.h" // For LuaEntitySAO and PlayerSAO #include "itemdef.h" #include "nodedef.h" @@ -4679,7 +4678,7 @@ std::string scriptapi_luaentity_get_staticdata(lua_State *L, u16 id) } void scriptapi_luaentity_get_properties(lua_State *L, u16 id, - LuaEntityProperties *prop) + ObjectProperties *prop) { realitycheck(L); assert(lua_checkstack(L, 20)); diff --git a/src/scriptapi.h b/src/scriptapi.h index 84d3756c..b80039f9 100644 --- a/src/scriptapi.h +++ b/src/scriptapi.h @@ -28,7 +28,7 @@ class Server; class ServerEnvironment; class ServerActiveObject; typedef struct lua_State lua_State; -struct LuaEntityProperties; +struct ObjectProperties; struct ItemStack; struct PointedThing; //class IGameDef; @@ -83,7 +83,7 @@ void scriptapi_luaentity_activate(lua_State *L, u16 id, void scriptapi_luaentity_rm(lua_State *L, u16 id); std::string scriptapi_luaentity_get_staticdata(lua_State *L, u16 id); void scriptapi_luaentity_get_properties(lua_State *L, u16 id, - LuaEntityProperties *prop); + ObjectProperties *prop); void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime); void scriptapi_luaentity_punch(lua_State *L, u16 id, ServerActiveObject *puncher, float time_from_last_punch,