/
StandardGun.cpp
425 lines (352 loc) · 12.3 KB
/
StandardGun.cpp
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* common/StandardGun.cpp
*
* This file is part of Leges Motus, a networked, 2D shooter set in zero gravity.
*
* Copyright 2009-2011 Andrew Ayer, Nathan Partlan, Jeffrey Pfau
*
* Leges Motus is free and open source software. You may redistribute it and/or
* modify it under the terms of version 2, or (at your option) version 3, of the
* GNU General Public License (GPL), as published by the Free Software Foundation.
*
* Leges Motus 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 full text of the GNU General Public License for
* further detail.
*
* For a full copy of the GNU General Public License, please see the COPYING file
* in the root of the source code tree. You may also retrieve a copy from
* <http://www.gnu.org/licenses/gpl-2.0.txt>, or request a copy by writing to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
*/
#include "StandardGun.hpp"
#include "Player.hpp"
#include "StringTokenizer.hpp"
#include "PacketWriter.hpp"
#include "timer.hpp"
#include "math.hpp"
#include "TypeWrapper.hpp"
#include "misc.hpp"
#include "common/misc.hpp"
#include "common/MapObject.hpp"
#include <algorithm>
#include <stdlib.h>
#include <iostream>
#include "common/physics.hpp"
using namespace LM;
using namespace std;
static bool compare_hit_data(HitData first, HitData second) { return first.fraction > second.fraction; }
StandardGun::StandardGun(uint32_t id) : Weapon(id) {
m_last_fired_time = 0;
m_freeze_time = 10000;
m_damage = 100;
m_cooldown = 700;
m_recoil = 1.5;
m_inaccuracy = 0;
m_is_continuous = false;
m_total_ammo = 0;
m_ammo_recharge = 500;
m_last_fired_dir = 0;
m_penetrates_players = 0.0;
m_penetrates_walls = 0.0;
m_max_penetration = 0;
m_damage_degradation = 0;
m_nbr_projectiles = 1;
m_angle = 0;
m_freeze_reduction = 0;
m_energy_cost = 0;
// TODO: FOR NOW, HARDCODE MAX RANGE.
m_max_range = 10000;
}
bool StandardGun::parse_param(const char* param_string) {
if (strncmp(param_string, "freeze=", 7) == 0) {
m_freeze_time = atol(param_string + 7);
} else if (strncmp(param_string, "damage=", 7) == 0) {
m_damage = atoi(param_string + 7);
} else if (strncmp(param_string, "cooldown=", 9) == 0) {
m_cooldown = atol(param_string + 9);
} else if (strncmp(param_string, "recoil=", 7) == 0) {
m_recoil = atof(param_string + 7);
} else if (strncmp(param_string, "inaccuracy=", 11) == 0) {
m_inaccuracy = to_radians(atof(param_string + 11));
} else if (strcmp(param_string, "ammo=unlimited") == 0) {
m_total_ammo = 0;
} else if (strncmp(param_string, "ammo=", 5) == 0) {
m_total_ammo = atoi(param_string + 5);
} else if (strncmp(param_string, "ammo_recharge=", 14) == 0) {
m_ammo_recharge = atol(param_string + 14);
} else if (strcmp(param_string, "continuous") == 0) {
m_is_continuous = true;
} else if (strcmp(param_string, "notcontinuous") == 0) {
m_is_continuous = false;
} else if (strncmp(param_string, "penetrate_players=", 18) == 0) {
m_penetrates_players = atof(param_string + 18);
} else if (strncmp(param_string, "penetrate_walls=", 16) == 0) {
m_penetrates_walls = atof(param_string + 16);
} else if (strncmp(param_string, "max_penetration=", 16) == 0) {
m_max_penetration = atof(param_string + 16);
} else if (strncmp(param_string, "degradation=", 12) == 0) {
m_damage_degradation = atof(param_string + 12);
} else if (strncmp(param_string, "projectiles=", 12) == 0) {
m_nbr_projectiles = atoi(param_string + 12);
} else if (strncmp(param_string, "angle=", 6) == 0) {
m_angle = to_radians(atof(param_string + 6));
} else if (strncmp(param_string, "reduction=", 10) == 0) {
m_freeze_reduction = atol(param_string + 10);
} else if (strncmp(param_string, "cost=", 5) == 0) {
m_energy_cost = atoi(param_string + 5);
} else {
return Weapon::parse_param(param_string);
}
return true;
}
Packet::WeaponDischarged* StandardGun::fire(b2World* physics, Player& player, Point start, float direction, Packet::WeaponDischarged* packet) {
m_last_fired_time = get_ticks();
m_last_fired_dir = direction;
b2Vec2 end_point;
m_hit_data.clear();
// Recharge ammo, if applicable
if (m_total_ammo) {
m_current_ammo = get_current_ammo();
if (m_current_ammo == 0) {
// Out of ammo
return NULL;
}
--m_current_ammo;
}
float currdirection = direction - m_angle/2.0f;
for (int i = 0; i < m_nbr_projectiles; i++) {
float endx = start.x + m_max_range * cos(currdirection);
float endy = start.y + m_max_range * sin(currdirection);
m_objects_penetrated = 0;
m_current_damage = m_damage;
m_hit_data.push_back(vector<HitData>());
physics->RayCast(this, b2Vec2(to_physics(start.x), to_physics(start.y)), b2Vec2(to_physics(endx), to_physics(endy)));
if (!m_hit_data.back().empty()) {
sort(m_hit_data.back().begin(), m_hit_data.back().end(), compare_hit_data);
}
// Find the end point of the middle projectile
if (i == m_nbr_projectiles/2) {
bool found = false;
vector<HitData>::iterator thishit = m_hit_data.back().end();
// Discard invalid collisions until we find the closest good collision
while (!found) {
thishit--;
b2Body* body = (*thishit).fixture->GetBody();
if (body->GetUserData() == NULL) {
continue;
}
PhysicsObject* hitobj = static_cast<PhysicsObject*>(body->GetUserData());
if ((*thishit).fixture->IsSensor()) {
continue;
}
if (hitobj->get_type() == PhysicsObject::MAP_OBJECT) {
MapObject* object = static_cast<MapObject*>(hitobj);
if (!object->is_shootable()) {
continue;
}
}
found = true;
}
end_point = (*thishit).point;
}
currdirection += m_angle/(m_nbr_projectiles - 1.0);
}
packet->player_id = player.get_id();
packet->weapon_id = get_id();
packet->start_x = start.x;
packet->start_y = start.y;
packet->end_x = to_game(end_point.x);
packet->end_y = to_game(end_point.y);
packet->direction = direction;
was_fired(physics, player, direction);
return packet;
}
void StandardGun::was_fired(b2World* physics, Player& player, float direction) {
// Apply recoil and energy cost if necessary.
// FIXME this appears to be repeated code in all Weapons
player.apply_force(b2Vec2(m_recoil * 100 * cos(M_PI + direction), m_recoil * 100 * sin(M_PI + direction)));
player.change_energy(-1 * m_energy_cost);
if (player.get_energy() <= 0) {
player.set_is_frozen(true, m_freeze_time, &player);
}
}
void StandardGun::hit(Player* hit_player, Player* firing_player, const Packet::PlayerHit* p) {
string extradata = *p->extradata.item;
stringstream s(extradata);
float direction;
float hit_point_x;
float hit_point_y;
float damage;
s >> direction >> hit_point_x >> hit_point_y >> damage;
// Calculate actual recoil.
float recoil = (m_recoil / m_nbr_projectiles);
// Apply force to the player.
hit_player->apply_force(b2Vec2(recoil * 100 * cos(direction), recoil * 100 * sin(direction)), b2Vec2(hit_point_x, hit_point_y));
// Change the freeze time on the hit player.
uint64_t curr_freeze_time = hit_player->get_freeze_time();
if (curr_freeze_time > 0) {
if (m_freeze_reduction > curr_freeze_time) {
hit_player->set_freeze_time(0);
} else {
hit_player->set_freeze_time(curr_freeze_time - m_freeze_reduction);
}
}
// Do damage if the gun has effect and they're not frozen.
if (p->has_effect && !hit_player->is_frozen()) {
// Apply damage to the player.
hit_player->change_energy(-damage);
// If player is damaged sufficiently, freeze them.
if (hit_player->get_energy() <= 0) {
hit_player->set_is_frozen(true, m_freeze_time, firing_player);
}
}
}
void StandardGun::reset() {
m_last_fired_time = 0;
m_current_ammo = m_total_ammo;
}
uint64_t StandardGun::get_remaining_cooldown() const {
if (m_last_fired_time) {
uint64_t time_since_fire = get_ticks() - m_last_fired_time;
if (time_since_fire < m_cooldown) {
return m_cooldown - time_since_fire;
}
}
return 0;
}
int StandardGun::get_current_ammo() const {
if (m_last_fired_time && m_ammo_recharge) {
// Take into account the ammo recharge that has occurred since last firing
uint64_t time_since_fire = get_ticks() - m_last_fired_time;
return min<int>(m_total_ammo, m_current_ammo + time_since_fire / m_ammo_recharge);
}
return m_current_ammo;
}
int StandardGun::get_damage() const {
return m_damage;
}
int StandardGun::get_damage_at_point(int start_x, int start_y, int target_x, int target_y) const {
float distance = sqrt((target_y - start_y) * (target_y - start_y) + (target_x - start_x) * (target_x - start_x));
float fraction = distance/m_max_range;
// Estimate the actual damage at this range.
float actualdamage = m_damage - m_damage_degradation * m_max_range * fraction;
if (distance > m_max_range) {
return 0;
}
if (actualdamage <= 0) {
actualdamage = 0;
}
// Estimate the number of projectiles that will hit.
if (m_nbr_projectiles > 1) {
int num_missed = 0;
float spread = m_angle * distance;
if (spread > Player::PLAYER_HEIGHT) {
num_missed = 2 * spread/Player::PLAYER_HEIGHT;
if (num_missed >= m_nbr_projectiles) {
num_missed = m_nbr_projectiles-1;
}
}
actualdamage *= m_nbr_projectiles - num_missed;
}
return actualdamage;
}
float StandardGun::get_base_force() const {
return m_recoil;
}
float StandardGun::get_force(int start_x, int start_y, int target_x, int target_y) const {
if (m_nbr_projectiles <= 1) {
return m_recoil;
}
float distance = sqrt((target_y - start_y) * (target_y - start_y) + (target_x - start_x) * (target_x - start_x));
// Estimate the number of projectiles that will hit.
int num_missed = 0;
float spread = m_angle * distance;
if (spread > Player::PLAYER_HEIGHT) {
num_missed = 2 * spread/Player::PLAYER_HEIGHT;
if (num_missed >= m_nbr_projectiles) {
num_missed = m_nbr_projectiles-1;
}
}
return (m_recoil * (m_nbr_projectiles - num_missed)) / m_nbr_projectiles;
}
float StandardGun::get_freeze_time() const {
return m_freeze_time;
}
Packet::PlayerHit* StandardGun::generate_next_hit_packet(Packet::PlayerHit* p, Player* shooter) {
if (m_hit_data.empty()) {
return NULL;
}
bool found_player = false;
while (!m_hit_data.empty()) {
vector<HitData> nextshot = m_hit_data.back();
m_hit_data.pop_back();
if (nextshot.empty()) {
continue;
}
m_objects_penetrated = 0;
m_current_damage = m_damage;
while (!found_player && !nextshot.empty()) {
// Don't go over max penetration depth.
if (m_objects_penetrated > m_max_penetration) {
return NULL;
}
if (m_current_damage <= 0) {
return NULL;
}
HitData nextdata = nextshot.back();
nextshot.pop_back();
b2Body* body = nextdata.fixture->GetBody();
PhysicsObject* userdata = static_cast<PhysicsObject*>(body->GetUserData());
if (userdata->get_type() == PhysicsObject::MAP_OBJECT) {
MapObject* mapobj = static_cast<MapObject*>(userdata);
if (!mapobj->is_shootable()) {
// Just ignore this object and keep going - it's not actually hitting it.
continue;
}
}
m_objects_penetrated++;
if (userdata->get_type() != PhysicsObject::PLAYER) {
m_current_damage = m_current_damage * m_penetrates_walls;
continue;
}
float actualdamage = m_current_damage - m_damage_degradation * (m_max_range * nextdata.fraction);
if (actualdamage <= 0) {
actualdamage = 0;
}
p->shooter_id = shooter->get_id();
p->weapon_id = get_id();
Player* hit_player = static_cast<Player*>(userdata);
p->shot_player_id = hit_player->get_id();
p->has_effect = hit_player->is_frozen() ? false : true;
std::stringstream out;
out << m_last_fired_dir << " " << nextdata.point.x << " " << nextdata.point.y << " " << actualdamage;
p->extradata = out.str();
hit(static_cast<Player*>(userdata), shooter, p);
m_current_damage = m_current_damage * m_penetrates_players;
found_player = true;
return p;
}
}
ASSERT(!found_player);
return NULL;
}
float32 StandardGun::ReportFixture(b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, float32 fraction) {
b2Body* body = fixture->GetBody();
if (body->GetUserData() == NULL) {
WARN("Body has no user data!");
return 1;
}
if (fraction < 0) {
return 0;
}
HitData hit_data;
hit_data.fixture = fixture;
hit_data.point = point;
hit_data.normal = normal;
hit_data.fraction = fraction;
m_hit_data.back().push_back(hit_data);
return 1;
}