3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "serverremoteplayer.h"
21 #include "main.h" // For g_settings
25 #include "inventory.h"
26 #include "environment.h"
27 #include "materials.h"
29 ServerRemotePlayer::ServerRemotePlayer(ServerEnvironment *env):
30 Player(env->getGameDef()),
31 ServerActiveObject(env, v3f(0,0,0)),
32 m_last_good_position(0,0,0),
33 m_last_good_position_age(0),
35 m_inventory_not_sent(false),
37 m_respawn_active(false),
38 m_is_in_environment(false),
39 m_time_from_last_punch(0),
40 m_position_not_sent(false)
43 ServerRemotePlayer::ServerRemotePlayer(ServerEnvironment *env, v3f pos_, u16 peer_id_,
45 Player(env->getGameDef()),
46 ServerActiveObject(env, pos_),
47 m_last_good_position(0,0,0),
48 m_last_good_position_age(0),
50 m_inventory_not_sent(false),
52 m_is_in_environment(false),
53 m_time_from_last_punch(0),
54 m_position_not_sent(false)
60 ServerRemotePlayer::~ServerRemotePlayer()
64 void ServerRemotePlayer::setPosition(const v3f &position)
66 Player::setPosition(position);
67 ServerActiveObject::setBasePosition(position);
68 m_position_not_sent = true;
71 Inventory* ServerRemotePlayer::getInventory()
76 const Inventory* ServerRemotePlayer::getInventory() const
81 InventoryLocation ServerRemotePlayer::getInventoryLocation() const
83 InventoryLocation loc;
84 loc.setPlayer(getName());
88 void ServerRemotePlayer::setInventoryModified()
90 m_inventory_not_sent = true;
93 std::string ServerRemotePlayer::getWieldList() const
98 int ServerRemotePlayer::getWieldIndex() const
100 return m_wield_index;
103 void ServerRemotePlayer::setWieldIndex(int i)
108 /* ServerActiveObject interface */
110 void ServerRemotePlayer::addedToEnvironment()
112 assert(!m_is_in_environment);
113 m_is_in_environment = true;
116 void ServerRemotePlayer::removingFromEnvironment()
118 assert(m_is_in_environment);
119 m_is_in_environment = false;
122 bool ServerRemotePlayer::unlimitedTransferDistance() const
124 return g_settings->getBool("unlimited_player_transfer_distance");
127 void ServerRemotePlayer::step(float dtime, bool send_recommended)
129 m_time_from_last_punch += dtime;
131 if(send_recommended == false)
134 if(m_position_not_sent)
136 m_position_not_sent = false;
138 std::ostringstream os(std::ios::binary);
139 // command (0 = update position)
142 writeV3F1000(os, getPosition());
144 writeF1000(os, getPitch());
146 writeF1000(os, getYaw());
147 // create message and add to list
148 ActiveObjectMessage aom(getId(), false, os.str());
149 m_messages_out.push_back(aom);
153 std::string ServerRemotePlayer::getClientInitializationData()
155 std::ostringstream os(std::ios::binary);
159 os<<serializeString(getName());
161 writeV3F1000(os, getPosition());
163 writeF1000(os, getPitch());
165 writeF1000(os, getYaw());
169 std::string ServerRemotePlayer::getStaticData()
175 void ServerRemotePlayer::punch(ServerActiveObject *puncher,
176 float time_from_last_punch)
181 // No effect if PvP disabled
182 if(g_settings->getBool("enable_pvp") == false){
183 if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER)
187 // "Material" properties of a player
188 MaterialProperties mp;
189 mp.diggability = DIGGABLE_NORMAL;
190 mp.crackiness = -0.5;
191 mp.cuttability = 0.5;
193 IItemDefManager *idef = m_env->getGameDef()->idef();
194 ItemStack punchitem = puncher->getWieldedItem();
195 ToolDiggingProperties tp =
196 punchitem.getToolDiggingProperties(idef);
198 HittingProperties hitprop = getHittingProperties(&mp, &tp,
199 time_from_last_punch);
201 actionstream<<"Player "<<getName()<<" punched by "
202 <<puncher->getDescription()<<", damage "<<hitprop.hp
205 setHP(getHP() - hitprop.hp);
206 punchitem.addWear(hitprop.wear, idef);
207 puncher->setWieldedItem(punchitem);
211 std::ostringstream os(std::ios::binary);
212 // command (1 = punched)
215 writeS16(os, hitprop.hp);
216 // create message and add to list
217 ActiveObjectMessage aom(getId(), false, os.str());
218 m_messages_out.push_back(aom);
222 void ServerRemotePlayer::rightClick(ServerActiveObject *clicker)
226 void ServerRemotePlayer::setPos(v3f pos)
229 // Movement caused by this command is always valid
230 m_last_good_position = pos;
231 m_last_good_position_age = 0;
233 void ServerRemotePlayer::moveTo(v3f pos, bool continuous)
236 // Movement caused by this command is always valid
237 m_last_good_position = pos;
238 m_last_good_position_age = 0;
241 void ServerRemotePlayer::setHP(s16 hp_)
245 // FIXME: don't hardcode maximum HP, make configurable per object
253 m_hp_not_sent = true;
255 s16 ServerRemotePlayer::getHP()