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