Players stay in environment even when dead, damage flash and fall damage fixes
[oweals/minetest.git] / src / serverremoteplayer.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "serverremoteplayer.h"
21 #include "main.h" // For g_settings
22 #include "settings.h"
23 #include "log.h"
24 #include "gamedef.h"
25 #include "inventory.h"
26 #include "environment.h"
27 #include "materials.h"
28
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),
34         m_wield_index(0),
35         m_inventory_not_sent(false),
36         m_hp_not_sent(false),
37         m_is_in_environment(false),
38         m_time_from_last_punch(0),
39         m_position_not_sent(false)
40 {
41 }
42 ServerRemotePlayer::ServerRemotePlayer(ServerEnvironment *env, v3f pos_, u16 peer_id_,
43                 const char *name_):
44         Player(env->getGameDef()),
45         ServerActiveObject(env, pos_),
46         m_last_good_position(0,0,0),
47         m_last_good_position_age(0),
48         m_wield_index(0),
49         m_inventory_not_sent(false),
50         m_hp_not_sent(false),
51         m_is_in_environment(false),
52         m_time_from_last_punch(0),
53         m_position_not_sent(false)
54 {
55         setPosition(pos_);
56         peer_id = peer_id_;
57         updateName(name_);
58 }
59 ServerRemotePlayer::~ServerRemotePlayer()
60 {
61 }
62
63 void ServerRemotePlayer::setPosition(const v3f &position)
64 {
65         Player::setPosition(position);
66         ServerActiveObject::setBasePosition(position);
67         m_position_not_sent = true;
68 }
69
70 Inventory* ServerRemotePlayer::getInventory()
71 {
72         return &inventory;
73 }
74
75 const Inventory* ServerRemotePlayer::getInventory() const
76 {
77         return &inventory;
78 }
79
80 InventoryLocation ServerRemotePlayer::getInventoryLocation() const
81 {
82         InventoryLocation loc;
83         loc.setPlayer(getName());
84         return loc;
85 }
86
87 void ServerRemotePlayer::setInventoryModified()
88 {
89         m_inventory_not_sent = true;
90 }
91
92 std::string ServerRemotePlayer::getWieldList() const
93 {
94         return "main";
95 }
96
97 int ServerRemotePlayer::getWieldIndex() const
98 {
99         return m_wield_index;
100 }
101
102 void ServerRemotePlayer::setWieldIndex(int i)
103 {
104         m_wield_index = i;
105 }
106
107 /* ServerActiveObject interface */
108
109 void ServerRemotePlayer::addedToEnvironment()
110 {
111         assert(!m_is_in_environment);
112         m_is_in_environment = true;
113 }
114
115 void ServerRemotePlayer::removingFromEnvironment()
116 {
117         assert(m_is_in_environment);
118         m_is_in_environment = false;
119 }
120
121 bool ServerRemotePlayer::unlimitedTransferDistance() const
122 {
123         return g_settings->getBool("unlimited_player_transfer_distance");
124 }
125
126 void ServerRemotePlayer::step(float dtime, bool send_recommended)
127 {
128         m_time_from_last_punch += dtime;
129         
130         if(send_recommended == false)
131                 return;
132         
133         if(m_position_not_sent)
134         {
135                 m_position_not_sent = false;
136
137                 std::ostringstream os(std::ios::binary);
138                 // command (0 = update position)
139                 writeU8(os, 0);
140                 // pos
141                 writeV3F1000(os, getPosition());
142                 // yaw
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);
147         }
148 }
149
150 std::string ServerRemotePlayer::getClientInitializationData()
151 {
152         std::ostringstream os(std::ios::binary);
153         // version
154         writeU8(os, 0);
155         // name
156         os<<serializeString(getName());
157         // pos
158         writeV3F1000(os, getPosition());
159         // yaw
160         writeF1000(os, getYaw());
161         // dead
162         writeU8(os, getHP() == 0);
163         return os.str();
164 }
165
166 std::string ServerRemotePlayer::getStaticData()
167 {
168         assert(0);
169         return "";
170 }
171
172 void ServerRemotePlayer::punch(ServerActiveObject *puncher,
173                 float time_from_last_punch)
174 {
175         if(!puncher)
176                 return;
177         
178         // No effect if PvP disabled
179         if(g_settings->getBool("enable_pvp") == false){
180                 if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER)
181                         return;
182         }
183         
184         // "Material" properties of a player
185         MaterialProperties mp;
186         mp.diggability = DIGGABLE_NORMAL;
187         mp.crackiness = -0.5;
188         mp.cuttability = 0.5;
189
190         IItemDefManager *idef = m_env->getGameDef()->idef();
191         ItemStack punchitem = puncher->getWieldedItem();
192         ToolDiggingProperties tp =
193                 punchitem.getToolDiggingProperties(idef);
194
195         HittingProperties hitprop = getHittingProperties(&mp, &tp,
196                         time_from_last_punch);
197         
198         actionstream<<"Player "<<getName()<<" punched by "
199                         <<puncher->getDescription()<<", damage "<<hitprop.hp
200                         <<" HP"<<std::endl;
201         
202         setHP(getHP() - hitprop.hp);
203         punchitem.addWear(hitprop.wear, idef);
204         puncher->setWieldedItem(punchitem);
205         
206         if(hitprop.hp != 0)
207         {
208                 std::ostringstream os(std::ios::binary);
209                 // command (1 = punched)
210                 writeU8(os, 1);
211                 // damage
212                 writeS16(os, hitprop.hp);
213                 // create message and add to list
214                 ActiveObjectMessage aom(getId(), false, os.str());
215                 m_messages_out.push_back(aom);
216         }
217 }
218
219 void ServerRemotePlayer::rightClick(ServerActiveObject *clicker)
220 {
221 }
222
223 void ServerRemotePlayer::setPos(v3f pos)
224 {
225         setPosition(pos);
226         // Movement caused by this command is always valid
227         m_last_good_position = pos;
228         m_last_good_position_age = 0;
229 }
230 void ServerRemotePlayer::moveTo(v3f pos, bool continuous)
231 {
232         setPosition(pos);
233         // Movement caused by this command is always valid
234         m_last_good_position = pos;
235         m_last_good_position_age = 0;
236 }
237
238 void ServerRemotePlayer::setHP(s16 hp_)
239 {
240         s16 oldhp = hp;
241
242         // FIXME: don't hardcode maximum HP, make configurable per object
243         if(hp_ < 0)
244                 hp_ = 0;
245         else if(hp_ > 20)
246                 hp_ = 20;
247         hp = hp_;
248
249         if(hp != oldhp)
250                 m_hp_not_sent = true;
251
252         // On death or reincarnation send an active object message
253         if((hp == 0) != (oldhp == 0))
254         {
255                 std::ostringstream os(std::ios::binary);
256                 // command (2 = update death state)
257                 writeU8(os, 2);
258                 // dead?
259                 writeU8(os, hp == 0);
260                 // create message and add to list
261                 ActiveObjectMessage aom(getId(), false, os.str());
262                 m_messages_out.push_back(aom);
263         }
264 }
265 s16 ServerRemotePlayer::getHP()
266 {
267         return hp;
268 }
269
270