From 52996cd5eb8939686b8a3eb9f075043c67d1355c Mon Sep 17 00:00:00 2001 From: sapier Date: Tue, 7 Feb 2012 21:37:42 +0100 Subject: [PATCH] maualy merged object linking interface from cao_sao_split patch --- src/clientlinkableobject.cpp | 4 +- src/clientlinkableobject.h | 15 ---- src/content_cao.cpp | 112 +++++++++++++++++++++--------- src/content_cao.h | 24 ------- src/content_sao.cpp | 21 ++++++ src/content_sao.h | 5 +- src/environment.cpp | 130 ++++++++++++++++++----------------- src/player.cpp | 3 +- src/player.h | 4 ++ src/serverremoteplayer.cpp | 23 +++++++ src/serverremoteplayer.h | 5 +- 11 files changed, 205 insertions(+), 141 deletions(-) delete mode 100644 src/content_cao.h diff --git a/src/clientlinkableobject.cpp b/src/clientlinkableobject.cpp index 917edbd53..8143a378c 100644 --- a/src/clientlinkableobject.cpp +++ b/src/clientlinkableobject.cpp @@ -49,7 +49,7 @@ void ClientLinkableObject::stepLinkedObjects(v3f pos,float dtime) { } bool ClientLinkableObject::handleLinkUnlinkMessages(u8 cmd,std::istringstream* is,ClientEnvironment *m_env) { - if(cmd == AO_Message_type::Link) // Link entity + if(cmd == 3) // Link entity { //Object to link entity to u16 object_id = readU16(*is); @@ -69,7 +69,7 @@ bool ClientLinkableObject::handleLinkUnlinkMessages(u8 cmd,std::istringstream* i return true; } - else if(cmd == AO_Message_type::UnLink) // UnLink entity + else if(cmd == 4) // UnLink entity { if (this->m_Parent == NULL) { errorstream << "Unlinking object not linked!" << std::endl; diff --git a/src/clientlinkableobject.h b/src/clientlinkableobject.h index a6192d58c..c3aba4ae5 100644 --- a/src/clientlinkableobject.h +++ b/src/clientlinkableobject.h @@ -30,21 +30,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "utility.h" #include "log.h" - -//this ain't the right place to define this but until cao/sao split -//is decided it'll have to stay here -struct AO_Message_type { - static const u8 SetPosition = 0x00; - static const u8 SetTextureMod = 0x01; - static const u8 SetSprite = 0x02; - static const u8 Punched = 0x03; - static const u8 TakeDamage = 0x04; - static const u8 Shoot = 0x05; - static const u8 Link = 0x06; - static const u8 UnLink = 0x07; -}; - - class ClientLinkableObject { public: ClientLinkableObject(); diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 4157cf68d..19aa14c12 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -17,11 +17,11 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "content_cao.h" #include "tile.h" #include "environment.h" #include "collision.h" #include "collidableobject.h" +#include "clientlinkableobject.h" #include "settings.h" #include #include @@ -1682,7 +1682,7 @@ void MobV2CAO::setLooks(const std::string &looks) #include "luaentity_common.h" -class LuaEntityCAO : public ClientActiveObject , public CollidableObject +class LuaEntityCAO : public ClientActiveObject , public CollidableObject , public ClientLinkableObject { private: core::aabbox3d m_selection_box; @@ -1897,36 +1897,40 @@ public: void step(float dtime, ClientEnvironment *env) { - if(m_prop->physical){ - core::aabbox3d box = m_prop->collisionbox; - box.MinEdge *= BS; - box.MaxEdge *= BS; - collisionMoveResult moveresult; - f32 pos_max_d = BS*0.125; // Distance per iteration - f32 stepheight = 0; - v3f p_pos = m_position; - v3f p_velocity = m_velocity; - v3f p_acceleration = m_acceleration; - moveresult = collisionMovePrecise(env, - pos_max_d, box, stepheight, dtime, - p_pos, p_velocity, p_acceleration); - // Apply results - m_position = p_pos; - m_velocity = p_velocity; - m_acceleration = p_acceleration; - - bool is_end_position = moveresult.collides; - pos_translator.update(m_position, is_end_position, dtime); - pos_translator.translate(dtime); - updateNodePos(); - } else { - m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration; - m_velocity += dtime * m_acceleration; - pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time); - pos_translator.translate(dtime); - updateNodePos(); + if(!this->isLinked()) { + if(m_prop->physical){ + core::aabbox3d box = m_prop->collisionbox; + box.MinEdge *= BS; + box.MaxEdge *= BS; + collisionMoveResult moveresult; + f32 pos_max_d = BS*0.125; // Distance per iteration + f32 stepheight = 0; + v3f p_pos = m_position; + v3f p_velocity = m_velocity; + v3f p_acceleration = m_acceleration; + moveresult = collisionMovePrecise(env, + pos_max_d, box, stepheight, dtime, + p_pos, p_velocity, p_acceleration); + // Apply results + m_position = p_pos; + m_velocity = p_velocity; + m_acceleration = p_acceleration; + + bool is_end_position = moveresult.collides; + pos_translator.update(m_position, is_end_position, dtime); + pos_translator.translate(dtime); + updateNodePos(); + } else { + m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration; + m_velocity += dtime * m_acceleration; + pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time); + pos_translator.translate(dtime); + updateNodePos(); + } } + stepLinkedObjects(this->m_position,dtime); + m_anim_timer += dtime; if(m_anim_timer >= m_anim_framelength){ m_anim_timer -= m_anim_framelength; @@ -2098,6 +2102,10 @@ public: updateTexturePos(); } + else if (handleLinkUnlinkMessages(cmd,&is,this->m_env)) + { + //Link unlink already done in handleLinkUnlinkMessages! + } } aabb3f* getCollisionBox() { @@ -2114,6 +2122,17 @@ public: return NULL; } + + void setPosition(v3f toset, float dtime){ + if (this->isLinked()) { + this->m_position = toset + this->m_linkOffset; + pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time); + pos_translator.translate(dtime); + updateNodePos(); + } + } + + void updateLinkState(bool value) {} }; // Prototype @@ -2123,7 +2142,7 @@ LuaEntityCAO proto_LuaEntityCAO(NULL, NULL); PlayerCAO */ -class PlayerCAO : public ClientActiveObject, public CollidableObject +class PlayerCAO : public ClientActiveObject, public CollidableObject , public ClientLinkableObject { private: core::aabbox3d m_selection_box; @@ -2342,9 +2361,12 @@ public: void step(float dtime, ClientEnvironment *env) { - pos_translator.translate(dtime); + if(!isLinked()) { + pos_translator.translate(dtime); + updateNodePos(); + } + updateVisibility(); - updateNodePos(); if(m_damage_visual_timer > 0){ m_damage_visual_timer -= dtime; @@ -2385,6 +2407,10 @@ public: m_dead = readU8(is); updateVisibility(); } + else if (handleLinkUnlinkMessages(cmd,&is,this->m_env)) + { + //Link unlink already done in handleLinkUnlinkMessages! + } } void updateTextures(const std::string &mod) @@ -2410,6 +2436,26 @@ public: } } } + void setPosition(v3f toset, float dtime){ + if (isLinked()) { + this->m_position = toset + this->m_linkOffset; + pos_translator.update(this->m_position,false); + updateNodePos(); + + if(m_is_local_player) { + m_local_player->setPosition(this->m_position); + } + } + else { + errorstream<<"Got linked position update but not linked"<< std::endl; + } + } + + void updateLinkState(bool value) { + if(m_is_local_player) { + m_local_player->Link(value); + } + } }; // Prototype diff --git a/src/content_cao.h b/src/content_cao.h deleted file mode 100644 index 6394bedbe..000000000 --- a/src/content_cao.h +++ /dev/null @@ -1,24 +0,0 @@ -/* -Minetest-c55 -Copyright (C) 2010-2011 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 -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 CONTENT_CAO_HEADER -#define CONTENT_CAO_HEADER - -#endif - diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 6c1b4c05b..d9bc80744 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -1852,3 +1852,24 @@ aabb3f* LuaEntitySAO::getCollisionBox() { return NULL; } +bool LuaEntitySAO::sendLinkMsg(ServerActiveObject* parent,v3f offset) { + std::ostringstream os(std::ios::binary); + writeU8(os, 3); + // parameters + writeU16(os, parent->getId()); + writeV3F1000(os, offset); + // create message and add to list + ActiveObjectMessage aom(getId(), false, os.str()); + m_messages_out.push_back(aom); + return true; +} + +bool LuaEntitySAO::sendUnlinkMsg() { + std::ostringstream os(std::ios::binary); + writeU8(os, 4); + // create message and add to list + ActiveObjectMessage aom(getId(), false, os.str()); + m_messages_out.push_back(aom); + return true; +} + diff --git a/src/content_sao.h b/src/content_sao.h index 6db320ab4..1036bdcd4 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serverobject.h" #include "content_object.h" #include "collidableobject.h" +#include "serverlinkableobject.h" class TestSAO : public ServerActiveObject { @@ -193,7 +194,7 @@ private: struct LuaEntityProperties; -class LuaEntitySAO : public ServerActiveObject, public CollidableObject +class LuaEntitySAO : public ServerActiveObject, public CollidableObject , public ServerLinkableObject { public: LuaEntitySAO(ServerEnvironment *env, v3f pos, @@ -224,6 +225,8 @@ public: bool select_horiz_by_yawpitch); std::string getName(); aabb3f* getCollisionBox(); + bool sendLinkMsg(ServerActiveObject* parent,v3f offset); + bool sendUnlinkMsg(); private: void sendPosition(bool do_interpolate, bool is_movement_end); diff --git a/src/environment.cpp b/src/environment.cpp index 05e56bfb5..1f8dfb7cc 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1908,6 +1908,7 @@ void ClientEnvironment::step(float dtime) Get the speed the player is going */ bool is_climbing = lplayer->is_climbing; + bool linked = lplayer->m_linked; f32 player_speed = lplayer->getSpeed().getLength(); @@ -1936,90 +1937,91 @@ void ClientEnvironment::step(float dtime) /* Stuff that has a maximum time increment */ + if (!linked) { + u32 loopcount = 0; + do + { + loopcount++; - u32 loopcount = 0; - do - { - loopcount++; + f32 dtime_part; + if(dtime_downcount > dtime_max_increment) + { + dtime_part = dtime_max_increment; + dtime_downcount -= dtime_part; + } + else + { + dtime_part = dtime_downcount; + /* + Setting this to 0 (no -=dtime_part) disables an infinite loop + when dtime_part is so small that dtime_downcount -= dtime_part + does nothing + */ + dtime_downcount = 0; + } - f32 dtime_part; - if(dtime_downcount > dtime_max_increment) - { - dtime_part = dtime_max_increment; - dtime_downcount -= dtime_part; - } - else - { - dtime_part = dtime_downcount; /* - Setting this to 0 (no -=dtime_part) disables an infinite loop - when dtime_part is so small that dtime_downcount -= dtime_part - does nothing + Handle local player */ - dtime_downcount = 0; - } - - /* - Handle local player - */ - - { - v3f lplayerpos = lplayer->getPosition(); - // Apply physics - if(free_move == false && is_climbing == false) { - // Gravity - v3f speed = lplayer->getSpeed(); - if(lplayer->swimming_up == false) - speed.Y -= 9.81 * BS * dtime_part * 2; + v3f lplayerpos = lplayer->getPosition(); - // Water resistance - if(lplayer->in_water_stable || lplayer->in_water) + // Apply physics + if(free_move == false && is_climbing == false) { - f32 max_down = 2.0*BS; - if(speed.Y < -max_down) speed.Y = -max_down; + // Gravity + v3f speed = lplayer->getSpeed(); + if(lplayer->swimming_up == false) + speed.Y -= 9.81 * BS * dtime_part * 2; - f32 max = 2.5*BS; - if(speed.getLength() > max) + // Water resistance + if(lplayer->in_water_stable || lplayer->in_water) { - speed = speed / speed.getLength() * max; + f32 max_down = 2.0*BS; + if(speed.Y < -max_down) speed.Y = -max_down; + + f32 max = 2.5*BS; + if(speed.getLength() > max) + { + speed = speed / speed.getLength() * max; + } } + + lplayer->setSpeed(speed); } - lplayer->setSpeed(speed); + /* + Move the lplayer. + This also does collision detection. + */ + lplayer->move(dtime_part, this, position_max_increment, + &player_collisions); } - - /* - Move the lplayer. - This also does collision detection. - */ - lplayer->move(dtime_part, this, position_max_increment, - &player_collisions); } - } - while(dtime_downcount > 0.001); + while(dtime_downcount > 0.001); + + //std::cout<<"Looped "<::Iterator - i = player_collisions.begin(); - i != player_collisions.end(); i++) - { - CollisionInfo &info = *i; - if(info.t == COLLISION_FALL) + for(core::list::Iterator + i = player_collisions.begin(); + i != player_collisions.end(); i++) { - //f32 tolerance = BS*10; // 2 without damage - f32 tolerance = BS*12; // 3 without damage - f32 factor = 1; - if(info.speed > tolerance) + CollisionInfo &info = *i; + if(info.t == COLLISION_FALL) { - f32 damage_f = (info.speed - tolerance)/BS*factor; - u16 damage = (u16)(damage_f+0.5); - damageLocalPlayer(damage, true); + //f32 tolerance = BS*10; // 2 without damage + f32 tolerance = BS*12; // 3 without damage + f32 factor = 1; + if(info.speed > tolerance) + { + f32 damage_f = (info.speed - tolerance)/BS*factor; + u16 damage = (u16)(damage_f+0.5); + damageLocalPlayer(damage, true); + } } } - } + } /* A quick draft of lava damage diff --git a/src/player.cpp b/src/player.cpp index 61cc2de7e..28ffa1679 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -188,7 +188,8 @@ void Player::deSerialize(std::istream &is) LocalPlayer::LocalPlayer(IGameDef *gamedef): Player(gamedef), m_sneak_node(32767,32767,32767), - m_sneak_node_exists(false) + m_sneak_node_exists(false), + m_linked(false) { // Initialize hp to 0, so that no hearts will be shown if server // doesn't support health points diff --git a/src/player.h b/src/player.h index 69db8ac51..d306cd63e 100644 --- a/src/player.h +++ b/src/player.h @@ -240,6 +240,10 @@ public: PlayerControl control; + inline void Link(bool value) { + m_linked = value; + } + bool m_linked; private: // This is used for determining the sneaking range v3s16 m_sneak_node; diff --git a/src/serverremoteplayer.cpp b/src/serverremoteplayer.cpp index 6663d22f8..832f15dc7 100644 --- a/src/serverremoteplayer.cpp +++ b/src/serverremoteplayer.cpp @@ -130,6 +130,9 @@ void ServerRemotePlayer::step(float dtime, bool send_recommended) if(send_recommended == false) return; + if(isLinked()) + return; + if(m_position_not_sent) { m_position_not_sent = false; @@ -278,4 +281,24 @@ aabb3f* ServerRemotePlayer::getCollisionBox() { return &m_collisionbox; } +bool ServerRemotePlayer::sendLinkMsg(ServerActiveObject* parent,v3f offset) { + std::ostringstream os(std::ios::binary); + writeU8(os, 3); + // parameters + writeU16(os, parent->getId()); + writeV3F1000(os, offset); + // create message and add to list + ActiveObjectMessage aom(getId(), true, os.str()); + m_messages_out.push_back(aom); + return true; +} + +bool ServerRemotePlayer::sendUnlinkMsg() { + std::ostringstream os(std::ios::binary); + writeU8(os, 4); + // create message and add to list + ActiveObjectMessage aom(getId(), true, os.str()); + m_messages_out.push_back(aom); + return true; +} diff --git a/src/serverremoteplayer.h b/src/serverremoteplayer.h index b67ba9c7f..c65f5530c 100644 --- a/src/serverremoteplayer.h +++ b/src/serverremoteplayer.h @@ -23,13 +23,14 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "player.h" #include "serverobject.h" #include "collidableobject.h" +#include "serverlinkableobject.h" #include "content_object.h" // Object type IDs /* Player on the server */ -class ServerRemotePlayer : public Player, public ServerActiveObject , public CollidableObject +class ServerRemotePlayer : public Player, public ServerActiveObject , public CollidableObject , public ServerLinkableObject { public: ServerRemotePlayer(ServerEnvironment *env); @@ -95,6 +96,8 @@ public: // Incremented by step(), read and reset by Server float m_time_from_last_punch; aabb3f* getCollisionBox(); + bool sendLinkMsg(ServerActiveObject* parent,v3f offset); + bool sendUnlinkMsg(); private: bool m_position_not_sent; }; -- 2.25.1