blob: a642371f93cb174ce9396f0ef6e484a7e3ec17cf (
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/*
core/player.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2.
*/
#include <sstream>
#include "auxiliary/functions.h"
#include "sys/sys.h"
#include "core/player.h"
#include "core/cvar.h"
#include "core/application.h"
namespace core
{
Player::Player()
{
player_control = 0;
clear();
}
Player::~Player()
{
clear();
}
void Player::clear()
{
player_id = 0;
player_zone = 0;
player_name.clear();
player_dirty = false;
player_zonechange = false;
player_mute = false;
player_mission_target = 0;
player_view = 0;
clear_assets();
player_control = 0;
player_ping = 0;
player_level = 1;
}
void Player::print() const
{
con_print << "id: ^B" << id() << "^N name: ^B" << name() << "^N" << std::endl;
con_print << " color ^B" << color() << std::endl;
con_print << " ping ^B" << ping() << std::endl;
con_print << " credits ^B" << credits() << std::endl;
con_print << " level ^B" << level() << std::endl;
if (zone()) {
con_print << " zone ^B" << zone()->name() << std::endl;
}
}
void Player::send(const std::string text)
{
message(core::Message::Info, text);
}
void Player::sound(const std::string name)
{
application()->notify_sound(name.c_str());
}
void Player::message(Message::Channel channel, const std::string text)
{
application()->notify_message(channel, text);
}
void Player::set_control(EntityControlable *entitycontrolable)
{
player_control = entitycontrolable;
if (entitycontrolable) {
set_zone(entitycontrolable->zone());
player_view = 0;
}
player_dirty = true;
}
void Player::set_zone(Zone *zone)
{
if (zone != player_zone) {
player_zone = zone;
player_zonechange = true;
}
}
void Player::set_view(Entity *view)
{
player_view = view;
player_dirty = true;
if (player_view) {
set_zone(player_view->zone());
}
}
void Player::set_mission_target(Entity *new_mission_target)
{
if (new_mission_target != player_mission_target) {
player_mission_target = new_mission_target;
player_dirty = true;
}
}
void Player::set_credits(const long amount)
{
player_credits = amount;
}
void Player::add_credits(const long amount)
{
player_credits += amount;
}
void Player::set_ping(const long ping)
{
player_ping = ping;
}
void Player::set_level(const int level) {
player_level = level;
}
void Player::update_info()
{
Cvar *cl_name = Cvar::find("cl_name");
if (cl_name) {
if (cl_name->str().size()) {
player_name = cl_name->str();
aux::strip_quotes(player_name);
(*cl_name) = player_name;
}
}
Cvar *cl_color = Cvar::find("cl_color");
math::Color color(1.0, 1.0, 1.0, 1.0);
if (cl_color) {
std::istringstream is(cl_color->str());
if (is >> color)
player_color.assign(color);
}
Cvar *cl_color_second = Cvar::find("cl_colorsecond");
math::Color color_second(1.0, 1.0, 1.0, 1.0);
if (cl_color_second) {
std::istringstream is(cl_color_second->str());
if (is >> color_second)
player_color_second.assign(color_second);
}
Cvar *passwd = Cvar::find("rconpassword");
if (passwd) {
player_rconpassword.assign(passwd->str());
} else {
player_rconpassword.clear();
}
}
void Player::serialize_client_update(std::ostream & os)
{
os << player_color << " "
<< player_color_second << " "
<< "\"" << player_name << "\" "
<< "\"" << player_rconpassword << "\" ";
}
void Player::receive_client_update(std::istream &is)
{
is >> player_color;
is >> player_color_second;
std::string n;
char c;
while ( (is.get(c)) && (c != '"'));
while ( (is.get(c)) && (c != '"'))
n += c;
if (n.size())
player_name.assign(n);
n.clear();
while ( (is.get(c)) && (c != '"'));
while ( (is.get(c)) && (c != '"'))
n += c;
if (n.size())
player_rconpassword.assign(n);
}
void Player::serialize_server_update(std::ostream & os) const
{
unsigned int zone_id = (zone() ? zone()->id() : 0);
unsigned int view_id = (player_view ? player_view->id() : 0);
unsigned int control_id = (player_control ? player_control->id() : 0);
unsigned int mission_id = (player_mission_target ? player_mission_target->id() : 0);
os << player_id << " "
<< zone_id << " "
<< view_id << " "
<< control_id << " "
<< mission_id << " "
<< player_color << " "
<< player_credits << " "
<< player_level << " "
<< player_ping;
}
void Player::receive_server_update(std::istream &is)
{
int id = 0;
is >> id;
if (!player_id) {
player_id = id;
} else if (player_id != id) {
con_warn << "received inconsistent update for player " << id << "\n";
return;
}
unsigned int zone_id = 0;
is >> zone_id;
set_zone(Zone::find(zone_id));
unsigned int view_id = 0;
is >> view_id;
set_view(Entity::find(view_id));
unsigned int control_id = 0;
is >> control_id;
if (control_id) {
Entity *e = Entity::find(control_id);
if (e && e->type() == Entity::Controlable) {
player_control = static_cast<EntityControlable *>(e);
} else {
player_control = 0;
con_warn << "control set to unknown entity " << control_id << "\n";
}
} else {
player_control = 0;
}
unsigned int mission_id = 0;
is >> mission_id;
if (mission_id) {
player_mission_target = Entity::find(mission_id);
if (!player_mission_target) {
con_warn << "mission target set to unknown entity " << mission_id << "\n";
}
} else {
player_mission_target = 0;
}
is >> player_color;
is >> player_credits;
is >> player_level;
is >> player_ping;
/*
std::string n;
char c;
while ( (is.get(c)) && (c != '"'));
while ( (is.get(c)) && (c != '"'))
n += c;
if (n.size())
player_name = n;
*/
}
void Player::add_asset(EntityControlable *entity)
{
entity->entity_owner = this;
assets.push_back(entity);
//con_debug << " adding asset " << entity->id() << " to player " << id() << std::endl;
}
void Player::remove_asset(EntityControlable *entity)
{
if (!entity)
return;
for (std::list<EntityControlable*>::iterator asset = assets.begin(); asset != assets.end(); asset++) {
if (((*asset) == entity) && (entity->owner() == this)) {
//con_debug << " removing asset " << (*asset)->id() << " from player " << id() << std::endl;
if ((*asset) == player_control)
player_control = 0;
(*asset)->entity_owner = 0;
(*asset)->die();
assets.erase(asset);
player_dirty = true;
return;
}
}
con_warn << "Could not remove asset " << entity->id() << " from player " << this->id() << "\n";
}
void Player::remove_asset(unsigned int id)
{
if (!id)
return;
for (std::list<EntityControlable*>::iterator asset = assets.begin(); asset != assets.end(); asset++) {
if ( ((*asset)->id() == id) && ((*asset)->owner() == this) ) {
//con_debug << " removing asset " << (*asset)->id() << " from player " << this->id() << std::endl;
if ((*asset) == player_control)
player_control = 0;
(*asset)->entity_owner = 0;
(*asset)->die();
assets.erase(asset);
player_dirty = true;
return;
}
}
con_warn << "Could not remove asset " << id << " from player " << this->id() << "\n";
}
void Player::clear_assets()
{
// clear assets
for (std::list<EntityControlable*>::iterator asset = assets.begin(); asset != assets.end(); asset++) {
//con_debug << " removing asset " << (*asset)->id() << " from player " << id() << std::endl;
(*asset)->entity_owner = 0;
(*asset)->die();
}
assets.clear();
player_control = 0;
}
}
|