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"
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_is_in_environment(false),
38 m_time_from_last_punch(0),
39 m_position_not_sent(false)
42 ServerRemotePlayer::ServerRemotePlayer(ServerEnvironment *env, v3f pos_, u16 peer_id_,
44 Player(env->getGameDef()),
45 ServerActiveObject(env, pos_),
46 m_last_good_position(0,0,0),
47 m_last_good_position_age(0),
49 m_inventory_not_sent(false),
51 m_is_in_environment(false),
52 m_time_from_last_punch(0),
53 m_position_not_sent(false)
59 ServerRemotePlayer::~ServerRemotePlayer()
63 void ServerRemotePlayer::setPosition(const v3f &position)
65 Player::setPosition(position);
66 ServerActiveObject::setBasePosition(position);
67 m_position_not_sent = true;
70 Inventory* ServerRemotePlayer::getInventory()
75 const Inventory* ServerRemotePlayer::getInventory() const
80 InventoryLocation ServerRemotePlayer::getInventoryLocation() const
82 InventoryLocation loc;
83 loc.setPlayer(getName());
87 void ServerRemotePlayer::setInventoryModified()
89 m_inventory_not_sent = true;
92 std::string ServerRemotePlayer::getWieldList() const
97 int ServerRemotePlayer::getWieldIndex() const
102 void ServerRemotePlayer::setWieldIndex(int i)
107 /* ServerActiveObject interface */
109 void ServerRemotePlayer::addedToEnvironment()
111 assert(!m_is_in_environment);
112 m_is_in_environment = true;
115 void ServerRemotePlayer::removingFromEnvironment()
117 assert(m_is_in_environment);
118 m_is_in_environment = false;
121 bool ServerRemotePlayer::unlimitedTransferDistance() const
123 return g_settings->getBool("unlimited_player_transfer_distance");
126 void ServerRemotePlayer::step(float dtime, bool send_recommended)
128 m_time_from_last_punch += dtime;
130 if(send_recommended == false)
133 if(m_position_not_sent)
135 m_position_not_sent = false;
137 std::ostringstream os(std::ios::binary);
138 // command (0 = update position)
141 writeV3F1000(os, getPosition());
143 writeF1000(os, getYaw());
144 // create message and add to list
145 ActiveObjectMessage aom(getId(), false, os.str());
146 m_messages_out.push_back(aom);
150 std::string ServerRemotePlayer::getClientInitializationData()
152 std::ostringstream os(std::ios::binary);
156 os<<serializeString(getName());
158 writeV3F1000(os, getPosition());
160 writeF1000(os, getYaw());
162 writeU8(os, getHP() == 0);
166 std::string ServerRemotePlayer::getStaticData()
172 void ServerRemotePlayer::punch(ServerActiveObject *puncher,
173 float time_from_last_punch)
178 // No effect if PvP disabled
179 if(g_settings->getBool("enable_pvp") == false){
180 if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER)
184 // "Material" groups of the player
185 std::map<std::string, int> groups;
186 groups["choppy"] = 2;
187 groups["fleshy"] = 3;
189 IItemDefManager *idef = m_env->getGameDef()->idef();
190 ItemStack punchitem = puncher->getWieldedItem();
191 ToolCapabilities tp = punchitem.getToolCapabilities(idef);
193 HitParams hitparams = getHitParams(groups, &tp, time_from_last_punch);
195 actionstream<<"Player "<<getName()<<" punched by "
196 <<puncher->getDescription()<<", damage "<<hitparams.hp
199 setHP(getHP() - hitparams.hp);
200 punchitem.addWear(hitparams.wear, idef);
201 puncher->setWieldedItem(punchitem);
203 if(hitparams.hp != 0)
205 std::ostringstream os(std::ios::binary);
206 // command (1 = punched)
209 writeS16(os, hitparams.hp);
210 // create message and add to list
211 ActiveObjectMessage aom(getId(), false, os.str());
212 m_messages_out.push_back(aom);
216 void ServerRemotePlayer::rightClick(ServerActiveObject *clicker)
220 void ServerRemotePlayer::setPos(v3f pos)
223 // Movement caused by this command is always valid
224 m_last_good_position = pos;
225 m_last_good_position_age = 0;
227 void ServerRemotePlayer::moveTo(v3f pos, bool continuous)
230 // Movement caused by this command is always valid
231 m_last_good_position = pos;
232 m_last_good_position_age = 0;
235 void ServerRemotePlayer::setHP(s16 hp_)
239 // FIXME: don't hardcode maximum HP, make configurable per object
247 m_hp_not_sent = true;
249 // On death or reincarnation send an active object message
250 if((hp == 0) != (oldhp == 0))
252 std::ostringstream os(std::ios::binary);
253 // command (2 = update death state)
256 writeU8(os, hp == 0);
257 // create message and add to list
258 ActiveObjectMessage aom(getId(), false, os.str());
259 m_messages_out.push_back(aom);
262 s16 ServerRemotePlayer::getHP()