summaryrefslogtreecommitdiff
path: root/src/serverremoteplayer.cpp
blob: b4dbbdb1b0663f7d9abd45b972079c247e8252ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Minetest-c55
Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "serverremoteplayer.h"
#include "main.h" // For g_settings
#include "settings.h"
#include "log.h"
#include "gamedef.h"
#include "inventory.h"
#include "environment.h"
#include "materials.h"

ServerRemotePlayer::ServerRemotePlayer(ServerEnvironment *env):
	Player(env->getGameDef()),
	ServerActiveObject(env, v3f(0,0,0)),
	m_last_good_position(0,0,0),
	m_last_good_position_age(0),
	m_wield_index(0),
	m_inventory_not_sent(false),
	m_hp_not_sent(false),
	m_respawn_active(false),
	m_is_in_environment(false),
	m_time_from_last_punch(0),
	m_position_not_sent(false)
{
}
ServerRemotePlayer::ServerRemotePlayer(ServerEnvironment *env, v3f pos_, u16 peer_id_,
		const char *name_):
	Player(env->getGameDef()),
	ServerActiveObject(env, pos_),
	m_last_good_position(0,0,0),
	m_last_good_position_age(0),
	m_wield_index(0),
	m_inventory_not_sent(false),
	m_hp_not_sent(false),
	m_is_in_environment(false),
	m_time_from_last_punch(0),
	m_position_not_sent(false)
{
	setPosition(pos_);
	peer_id = peer_id_;
	updateName(name_);
}
ServerRemotePlayer::~ServerRemotePlayer()
{
}

void ServerRemotePlayer::setPosition(const v3f &position)
{
	Player::setPosition(position);
	ServerActiveObject::setBasePosition(position);
	m_position_not_sent = true;
}

Inventory* ServerRemotePlayer::getInventory()
{
	return &inventory;
}

const Inventory* ServerRemotePlayer::getInventory() const
{
	return &inventory;
}

InventoryLocation ServerRemotePlayer::getInventoryLocation() const
{
	InventoryLocation loc;
	loc.setPlayer(getName());
	return loc;
}

void ServerRemotePlayer::setInventoryModified()
{
	m_inventory_not_sent = true;
}

std::string ServerRemotePlayer::getWieldList() const
{
	return "main";
}

int ServerRemotePlayer::getWieldIndex() const
{
	return m_wield_index;
}

void ServerRemotePlayer::setWieldIndex(int i)
{
	m_wield_index = i;
}

/* ServerActiveObject interface */

void ServerRemotePlayer::addedToEnvironment()
{
	assert(!m_is_in_environment);
	m_is_in_environment = true;
}

void ServerRemotePlayer::removingFromEnvironment()
{
	assert(m_is_in_environment);
	m_is_in_environment = false;
}

bool ServerRemotePlayer::unlimitedTransferDistance() const
{
	return g_settings->getBool("unlimited_player_transfer_distance");
}

void ServerRemotePlayer::step(float dtime, bool send_recommended)
{
	m_time_from_last_punch += dtime;
	
	if(send_recommended == false)
		return;
	
	if(m_position_not_sent)
	{
		m_position_not_sent = false;

		std::ostringstream os(std::ios::binary);
		// command (0 = update position)
		writeU8(os, 0);
		// pos
		writeV3F1000(os, getPosition());
		// yaw
		writeF1000(os, getYaw());
		// create message and add to list
		ActiveObjectMessage aom(getId(), false, os.str());
		m_messages_out.push_back(aom);
	}
}

std::string ServerRemotePlayer::getClientInitializationData()
{
	std::ostringstream os(std::ios::binary);
	// version
	writeU8(os, 0);
	// name
	os<<serializeString(getName());
	// pos
	writeV3F1000(os, getPosition());
	// yaw
	writeF1000(os, getYaw());
	return os.str();
}

std::string ServerRemotePlayer::getStaticData()
{
	assert(0);
	return "";
}

void ServerRemotePlayer::punch(ServerActiveObject *puncher,
		float time_from_last_punch)
{
	if(!puncher)
		return;
	
	// No effect if PvP disabled
	if(g_settings->getBool("enable_pvp") == false){
		if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER)
			return;
	}
	
	// "Material" properties of a player
	MaterialProperties mp;
	mp.diggability = DIGGABLE_NORMAL;
	mp.crackiness = -0.5;
	mp.cuttability = 0.5;

	IItemDefManager *idef = m_env->getGameDef()->idef();
	ItemStack punchitem = puncher->getWieldedItem();
	ToolDiggingProperties tp =
		punchitem.getToolDiggingProperties(idef);

	HittingProperties hitprop = getHittingProperties(&mp, &tp,
			time_from_last_punch);
	
	actionstream<<"Player "<<getName()<<" punched by "
			<<puncher->getDescription()<<", damage "<<hitprop.hp
			<<" HP"<<std::endl;
	
	setHP(getHP() - hitprop.hp);
	punchitem.addWear(hitprop.wear, idef);
	puncher->setWieldedItem(punchitem);
	
	if(hitprop.hp != 0)
	{
		std::ostringstream os(std::ios::binary);
		// command (1 = punched)
		writeU8(os, 1);
		// damage
		writeS16(os, hitprop.hp);
		// create message and add to list
		ActiveObjectMessage aom(getId(), false, os.str());
		m_messages_out.push_back(aom);
	}
}

void ServerRemotePlayer::rightClick(ServerActiveObject *clicker)
{
}

void ServerRemotePlayer::setPos(v3f pos)
{
	setPosition(pos);
	// Movement caused by this command is always valid
	m_last_good_position = pos;
	m_last_good_position_age = 0;
}
void ServerRemotePlayer::moveTo(v3f pos, bool continuous)
{
	setPosition(pos);
	// Movement caused by this command is always valid
	m_last_good_position = pos;
	m_last_good_position_age = 0;
}

void ServerRemotePlayer::setHP(s16 hp_)
{
	s16 oldhp = hp;

	// FIXME: don't hardcode maximum HP, make configurable per object
	if(hp_ < 0)
		hp_ = 0;
	else if(hp_ > 20)
		hp_ = 20;
	hp = hp_;

	if(hp != oldhp)
		m_hp_not_sent = true;
}
s16 ServerRemotePlayer::getHP()
{
	return hp;
}