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, getYaw());
145 // create message and add to list
146 ActiveObjectMessage aom(getId(), false, os.str());
147 m_messages_out.push_back(aom);
151 std::string ServerRemotePlayer::getClientInitializationData()
153 std::ostringstream os(std::ios::binary);
157 os<<serializeString(getName());
159 writeV3F1000(os, getPosition());
161 writeF1000(os, getYaw());
165 std::string ServerRemotePlayer::getStaticData()
171 void ServerRemotePlayer::punch(ServerActiveObject *puncher,
172 float time_from_last_punch)
177 // No effect if PvP disabled
178 if(g_settings->getBool("enable_pvp") == false){
179 if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER)
183 // "Material" properties of a player
184 MaterialProperties mp;
185 mp.diggability = DIGGABLE_NORMAL;
186 mp.crackiness = -0.5;
187 mp.cuttability = 0.5;
189 IItemDefManager *idef = m_env->getGameDef()->idef();
190 ItemStack punchitem = puncher->getWieldedItem();
191 ToolDiggingProperties tp =
192 punchitem.getToolDiggingProperties(idef);
194 HittingProperties hitprop = getHittingProperties(&mp, &tp,
195 time_from_last_punch);
197 actionstream<<"Player "<<getName()<<" punched by "
198 <<puncher->getDescription()<<", damage "<<hitprop.hp
201 setHP(getHP() - hitprop.hp);
202 punchitem.addWear(hitprop.wear, idef);
203 puncher->setWieldedItem(punchitem);
207 std::ostringstream os(std::ios::binary);
208 // command (1 = punched)
211 writeS16(os, hitprop.hp);
212 // create message and add to list
213 ActiveObjectMessage aom(getId(), false, os.str());
214 m_messages_out.push_back(aom);
218 void ServerRemotePlayer::rightClick(ServerActiveObject *clicker)
222 void ServerRemotePlayer::setPos(v3f pos)
225 // Movement caused by this command is always valid
226 m_last_good_position = pos;
227 m_last_good_position_age = 0;
229 void ServerRemotePlayer::moveTo(v3f pos, bool continuous)
232 // Movement caused by this command is always valid
233 m_last_good_position = pos;
234 m_last_good_position_age = 0;
237 void ServerRemotePlayer::setHP(s16 hp_)
241 // FIXME: don't hardcode maximum HP, make configurable per object
249 m_hp_not_sent = true;
251 s16 ServerRemotePlayer::getHP()