-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathg_turret.cpp
603 lines (485 loc) · 17.3 KB
/
g_turret.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright (c) ZeniMax Media Inc.
// Licensed under the GNU General Public License 2.0.
// g_turret.c
#include "g_local.h"
constexpr spawnflags_t SPAWNFLAG_TURRET_BREACH_FIRE = 65536_spawnflag;
static void AnglesNormalize(vec3_t &vec) {
while (vec[0] > 360)
vec[0] -= 360;
while (vec[0] < 0)
vec[0] += 360;
while (vec[1] > 360)
vec[1] -= 360;
while (vec[1] < 0)
vec[1] += 360;
}
MOVEINFO_BLOCKED(turret_blocked) (gentity_t *self, gentity_t *other) -> void {
gentity_t *attacker;
if (other->takedamage) {
if (self->teammaster->owner)
attacker = self->teammaster->owner;
else
attacker = self->teammaster;
T_Damage(other, self, attacker, vec3_origin, other->s.origin, vec3_origin, self->teammaster->dmg, 10, DAMAGE_NONE, MOD_CRUSH);
}
}
/*QUAKED turret_breach (0 0 0) ? x x x x x x x x NOT_EASY NOT_MEDIUM NOT_HARD NOT_DM NOT_COOP
This portion of the turret can change both pitch and yaw.
The model should be made with a flat pitch.
It (and the associated base) need to be oriented towards 0.
Use "angle" to set the starting angle.
"speed" default 50
"dmg" default 10
"angle" point this forward
"target" point this at an info_notnull at the muzzle tip
"minpitch" min acceptable pitch angle : default -30
"maxpitch" max acceptable pitch angle : default 30
"minyaw" min acceptable yaw angle : default 0
"maxyaw" max acceptable yaw angle : default 360
*/
static void turret_breach_fire(gentity_t *self) {
vec3_t f, r, u;
vec3_t start;
int damage;
int speed;
AngleVectors(self->s.angles, f, r, u);
start = self->s.origin + (f * self->move_origin[0]);
start += (r * self->move_origin[1]);
start += (u * self->move_origin[2]);
if (self->count)
damage = self->count;
else
damage = (int)frandom(100, 150);
speed = 550 + 50 * skill->integer;
gentity_t *rocket = fire_rocket(self->teammaster->owner->activator ? self->teammaster->owner->activator : self->teammaster->owner, start, f, damage, speed, 150, damage);
rocket->s.scale = self->teammaster->splash_radius;
gi.positioned_sound(start, self, CHAN_WEAPON, gi.soundindex("weapons/rocklf1a.wav"), 1, ATTN_NORM, 0);
}
static THINK(turret_breach_think) (gentity_t *self) -> void {
gentity_t *ent;
vec3_t current_angles;
vec3_t delta;
current_angles = self->s.angles;
AnglesNormalize(current_angles);
AnglesNormalize(self->move_angles);
if (self->move_angles[PITCH] > 180)
self->move_angles[PITCH] -= 360;
// clamp angles to mins & maxs
if (self->move_angles[PITCH] > self->pos1[PITCH])
self->move_angles[PITCH] = self->pos1[PITCH];
else if (self->move_angles[PITCH] < self->pos2[PITCH])
self->move_angles[PITCH] = self->pos2[PITCH];
if ((self->move_angles[YAW] < self->pos1[YAW]) || (self->move_angles[YAW] > self->pos2[YAW])) {
float dmin, dmax;
dmin = fabsf(self->pos1[YAW] - self->move_angles[YAW]);
if (dmin < -180)
dmin += 360;
else if (dmin > 180)
dmin -= 360;
dmax = fabsf(self->pos2[YAW] - self->move_angles[YAW]);
if (dmax < -180)
dmax += 360;
else if (dmax > 180)
dmax -= 360;
if (fabsf(dmin) < fabsf(dmax))
self->move_angles[YAW] = self->pos1[YAW];
else
self->move_angles[YAW] = self->pos2[YAW];
}
delta = self->move_angles - current_angles;
if (delta[0] < -180)
delta[0] += 360;
else if (delta[0] > 180)
delta[0] -= 360;
if (delta[1] < -180)
delta[1] += 360;
else if (delta[1] > 180)
delta[1] -= 360;
delta[2] = 0;
if (delta[0] > self->speed * gi.frame_time_s)
delta[0] = self->speed * gi.frame_time_s;
if (delta[0] < -1 * self->speed * gi.frame_time_s)
delta[0] = -1 * self->speed * gi.frame_time_s;
if (delta[1] > self->speed * gi.frame_time_s)
delta[1] = self->speed * gi.frame_time_s;
if (delta[1] < -1 * self->speed * gi.frame_time_s)
delta[1] = -1 * self->speed * gi.frame_time_s;
for (ent = self->teammaster; ent; ent = ent->teamchain) {
if (ent->noise_index) {
if (delta[0] || delta[1]) {
ent->s.sound = ent->noise_index;
ent->s.loop_attenuation = ATTN_NORM;
} else
ent->s.sound = 0;
}
}
self->avelocity = delta * (1.0f / gi.frame_time_s);
self->nextthink = level.time + FRAME_TIME_S;
for (ent = self->teammaster; ent; ent = ent->teamchain)
ent->avelocity[1] = self->avelocity[1];
// if we have a driver, adjust his velocities
if (self->owner) {
float angle;
float target_z;
float diff;
vec3_t target;
vec3_t dir;
// angular is easy, just copy ours
self->owner->avelocity[0] = self->avelocity[0];
self->owner->avelocity[1] = self->avelocity[1];
// x & y
angle = self->s.angles[YAW] + self->owner->move_origin[1];
angle *= (float)(PI * 2 / 360);
target[0] = self->s.origin[0] + cosf(angle) * self->owner->move_origin[0];
target[1] = self->s.origin[1] + sinf(angle) * self->owner->move_origin[0];
target[2] = self->owner->s.origin[2];
dir = target - self->owner->s.origin;
self->owner->velocity[0] = dir[0] * 1.0f / gi.frame_time_s;
self->owner->velocity[1] = dir[1] * 1.0f / gi.frame_time_s;
// z
angle = self->s.angles[PITCH] * (float)(PI * 2 / 360);
target_z = self->s.origin[2] + self->owner->move_origin[0] * tan(angle) + self->owner->move_origin[2];
diff = target_z - self->owner->s.origin[2];
self->owner->velocity[2] = diff * 1.0f / gi.frame_time_s;
if (self->spawnflags.has(SPAWNFLAG_TURRET_BREACH_FIRE)) {
turret_breach_fire(self);
self->spawnflags &= ~SPAWNFLAG_TURRET_BREACH_FIRE;
}
}
}
static THINK(turret_breach_finish_init) (gentity_t *self) -> void {
// get and save info for muzzle location
if (!self->target) {
gi.Com_PrintFmt("{}: needs a target\n", *self);
} else {
self->target_ent = G_PickTarget(self->target);
if (self->target_ent) {
self->move_origin = self->target_ent->s.origin - self->s.origin;
G_FreeEntity(self->target_ent);
} else
gi.Com_PrintFmt("{}: could not find target entity \"{}\"\n", *self, self->target);
}
self->teammaster->dmg = self->dmg;
self->teammaster->splash_radius = self->splash_radius; // scale
self->think = turret_breach_think;
self->think(self);
}
void SP_turret_breach(gentity_t *self) {
self->solid = SOLID_BSP;
self->movetype = MOVETYPE_PUSH;
if (st.noise)
self->noise_index = gi.soundindex(st.noise);
gi.setmodel(self, self->model);
if (!self->speed)
self->speed = 50;
if (!self->dmg)
self->dmg = 10;
if (!st.minpitch)
st.minpitch = -30;
if (!st.maxpitch)
st.maxpitch = 30;
if (!st.maxyaw)
st.maxyaw = 360;
self->pos1[PITCH] = -1 * st.minpitch;
self->pos1[YAW] = st.minyaw;
self->pos2[PITCH] = -1 * st.maxpitch;
self->pos2[YAW] = st.maxyaw;
// scale used for rocket scale
self->splash_radius = self->s.scale;
self->s.scale = 0;
self->ideal_yaw = self->s.angles[YAW];
self->move_angles[YAW] = self->ideal_yaw;
self->moveinfo.blocked = turret_blocked;
self->think = turret_breach_finish_init;
self->nextthink = level.time + FRAME_TIME_S;
gi.linkentity(self);
}
/*QUAKED turret_base (0 0 0) ? x x x x x x x x NOT_EASY NOT_MEDIUM NOT_HARD NOT_DM NOT_COOP
This portion of the turret changes yaw only.
MUST be teamed with a turret_breach.
*/
void SP_turret_base(gentity_t *self) {
self->solid = SOLID_BSP;
self->movetype = MOVETYPE_PUSH;
if (st.noise)
self->noise_index = gi.soundindex(st.noise);
gi.setmodel(self, self->model);
self->moveinfo.blocked = turret_blocked;
gi.linkentity(self);
}
/*QUAKED turret_driver (1 .5 0) (-16 -16 -24) (16 16 32) x x x x x x x x NOT_EASY NOT_MEDIUM NOT_HARD NOT_DM NOT_COOP
Must NOT be on the team with the rest of the turret parts.
Instead it must target the turret_breach.
*/
void infantry_die(gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, const vec3_t &point, const mod_t &mod);
void infantry_stand(gentity_t *self);
void infantry_pain(gentity_t *self, gentity_t *other, float kick, int damage, const mod_t &mod);
void infantry_setskin(gentity_t *self);
static DIE(turret_driver_die) (gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, const vec3_t &point, const mod_t &mod) -> void {
if (!self->deadflag) {
gentity_t *ent;
// level the gun
self->target_ent->move_angles[0] = 0;
// remove the driver from the end of them team chain
for (ent = self->target_ent->teammaster; ent->teamchain != self; ent = ent->teamchain)
;
ent->teamchain = nullptr;
self->teammaster = nullptr;
self->flags &= ~FL_TEAMSLAVE;
self->target_ent->owner = nullptr;
self->target_ent->teammaster->owner = nullptr;
self->target_ent->moveinfo.blocked = nullptr;
// clear pitch
self->s.angles[PITCH] = 0;
self->movetype = MOVETYPE_STEP;
self->think = monster_think;
}
infantry_die(self, inflictor, attacker, damage, point, mod);
G_FixStuckObject(self, self->s.origin);
AngleVectors(self->s.angles, self->velocity, nullptr, nullptr);
self->velocity *= -50;
self->velocity.z += 110.f;
}
bool FindTarget(gentity_t *self);
static THINK(turret_driver_think) (gentity_t *self) -> void {
vec3_t target;
vec3_t dir;
self->nextthink = level.time + FRAME_TIME_S;
if (self->enemy && (!self->enemy->inuse || self->enemy->health <= 0))
self->enemy = nullptr;
if (!self->enemy) {
if (!FindTarget(self))
return;
self->monsterinfo.trail_time = level.time;
self->monsterinfo.aiflags &= ~AI_LOST_SIGHT;
} else {
if (visible(self, self->enemy)) {
if (self->monsterinfo.aiflags & AI_LOST_SIGHT) {
self->monsterinfo.trail_time = level.time;
self->monsterinfo.aiflags &= ~AI_LOST_SIGHT;
}
} else {
self->monsterinfo.aiflags |= AI_LOST_SIGHT;
return;
}
}
// let the turret know where we want it to aim
target = self->enemy->s.origin;
target[2] += self->enemy->viewheight;
dir = target - self->target_ent->s.origin;
self->target_ent->move_angles = vectoangles(dir);
// decide if we should shoot
if (level.time < self->monsterinfo.attack_finished)
return;
gtime_t reaction_time = gtime_t::from_sec(3 - skill->integer);
if ((level.time - self->monsterinfo.trail_time) < reaction_time)
return;
self->monsterinfo.attack_finished = level.time + reaction_time + 1_sec;
// FIXME how do we really want to pass this along?
self->target_ent->spawnflags |= SPAWNFLAG_TURRET_BREACH_FIRE;
}
static THINK(turret_driver_link) (gentity_t *self) -> void {
vec3_t vec;
gentity_t *ent;
self->think = turret_driver_think;
self->nextthink = level.time + FRAME_TIME_S;
self->target_ent = G_PickTarget(self->target);
self->target_ent->owner = self;
self->target_ent->teammaster->owner = self;
self->s.angles = self->target_ent->s.angles;
vec[0] = self->target_ent->s.origin[0] - self->s.origin[0];
vec[1] = self->target_ent->s.origin[1] - self->s.origin[1];
vec[2] = 0;
self->move_origin[0] = vec.length();
vec = self->s.origin - self->target_ent->s.origin;
vec = vectoangles(vec);
AnglesNormalize(vec);
self->move_origin[1] = vec[1];
self->move_origin[2] = self->s.origin[2] - self->target_ent->s.origin[2];
// add the driver to the end of them team chain
for (ent = self->target_ent->teammaster; ent->teamchain; ent = ent->teamchain)
;
ent->teamchain = self;
self->teammaster = self->target_ent->teammaster;
self->flags |= FL_TEAMSLAVE;
}
void InfantryPrecache();
void SP_turret_driver(gentity_t *self) {
if (deathmatch->integer) {
G_FreeEntity(self);
return;
}
InfantryPrecache();
self->movetype = MOVETYPE_PUSH;
self->solid = SOLID_BBOX;
self->s.modelindex = gi.modelindex("models/monsters/infantry/tris.md2");
self->mins = { -16, -16, -24 };
self->maxs = { 16, 16, 32 };
self->health = self->max_health = 100;
self->gib_health = GIB_HEALTH;
self->mass = 200;
self->viewheight = 24;
self->pain = infantry_pain;
self->die = turret_driver_die;
self->monsterinfo.stand = infantry_stand;
self->flags |= FL_NO_KNOCKBACK;
if (g_debug_monster_kills->integer)
level.monsters_registered[level.total_monsters] = self;
level.total_monsters++;
self->svflags |= SVF_MONSTER;
self->takedamage = true;
self->use = monster_use;
self->clipmask = MASK_MONSTERSOLID;
self->s.old_origin = self->s.origin;
self->monsterinfo.aiflags |= AI_STAND_GROUND;
self->monsterinfo.setskin = infantry_setskin;
if (st.item) {
self->item = FindItemByClassname(st.item);
if (!self->item)
gi.Com_PrintFmt("{}: bad item: {}\n", *self, st.item);
}
self->think = turret_driver_link;
self->nextthink = level.time + FRAME_TIME_S;
gi.linkentity(self);
}
// invisible turret drivers so we can have unmanned turrets.
// originally designed to shoot at func_trains and such, so they
// fire at the center of the bounding box, rather than the entity's
// origin.
constexpr spawnflags_t SPAWNFLAG_TURRET_BRAIN_IGNORE_SIGHT = 1_spawnflag;
static THINK(turret_brain_think) (gentity_t *self) -> void {
vec3_t target;
vec3_t dir;
vec3_t endpos;
trace_t trace;
self->nextthink = level.time + FRAME_TIME_S;
if (self->enemy) {
if (!self->enemy->inuse)
self->enemy = nullptr;
else if (self->enemy->takedamage && self->enemy->health <= 0)
self->enemy = nullptr;
}
if (!self->enemy) {
if (!FindTarget(self))
return;
self->monsterinfo.trail_time = level.time;
self->monsterinfo.aiflags &= ~AI_LOST_SIGHT;
}
endpos = self->enemy->absmax + self->enemy->absmin;
endpos *= 0.5f;
if (!self->spawnflags.has(SPAWNFLAG_TURRET_BRAIN_IGNORE_SIGHT)) {
trace = gi.traceline(self->target_ent->s.origin, endpos, self->target_ent, MASK_SHOT);
if (trace.fraction == 1 || trace.ent == self->enemy) {
if (self->monsterinfo.aiflags & AI_LOST_SIGHT) {
self->monsterinfo.trail_time = level.time;
self->monsterinfo.aiflags &= ~AI_LOST_SIGHT;
}
} else {
self->monsterinfo.aiflags |= AI_LOST_SIGHT;
return;
}
}
// let the turret know where we want it to aim
target = endpos;
dir = target - self->target_ent->s.origin;
self->target_ent->move_angles = vectoangles(dir);
// decide if we should shoot
if (level.time < self->monsterinfo.attack_finished)
return;
gtime_t reaction_time;
if (self->delay)
reaction_time = gtime_t::from_sec(self->delay);
else
reaction_time = gtime_t::from_sec(3 - skill->integer);
if ((level.time - self->monsterinfo.trail_time) < reaction_time)
return;
self->monsterinfo.attack_finished = level.time + reaction_time + 1_sec;
// FIXME how do we really want to pass this along?
self->target_ent->spawnflags |= SPAWNFLAG_TURRET_BREACH_FIRE;
}
// =================
// =================
static THINK(turret_brain_link) (gentity_t *self) -> void {
vec3_t vec;
gentity_t *ent;
if (self->killtarget) {
self->enemy = G_PickTarget(self->killtarget);
}
self->think = turret_brain_think;
self->nextthink = level.time + FRAME_TIME_S;
self->target_ent = G_PickTarget(self->target);
self->target_ent->owner = self;
self->target_ent->teammaster->owner = self;
self->s.angles = self->target_ent->s.angles;
vec[0] = self->target_ent->s.origin[0] - self->s.origin[0];
vec[1] = self->target_ent->s.origin[1] - self->s.origin[1];
vec[2] = 0;
self->move_origin[0] = vec.length();
vec = self->s.origin - self->target_ent->s.origin;
vec = vectoangles(vec);
AnglesNormalize(vec);
self->move_origin[1] = vec[1];
self->move_origin[2] = self->s.origin[2] - self->target_ent->s.origin[2];
// add the driver to the end of them team chain
for (ent = self->target_ent->teammaster; ent->teamchain; ent = ent->teamchain)
ent->activator = self->activator; // pass along activator to breach, etc
ent->teamchain = self;
self->teammaster = self->target_ent->teammaster;
self->flags |= FL_TEAMSLAVE;
}
// =================
// =================
static USE(turret_brain_deactivate) (gentity_t *self, gentity_t *other, gentity_t *activator) -> void {
self->think = nullptr;
self->nextthink = 0_ms;
}
// =================
// =================
static USE(turret_brain_activate) (gentity_t *self, gentity_t *other, gentity_t *activator) -> void {
if (!self->enemy)
self->enemy = activator;
// wait at least 3 seconds to fire.
if (self->wait)
self->monsterinfo.attack_finished = level.time + gtime_t::from_sec(self->wait);
else
self->monsterinfo.attack_finished = level.time + 3_sec;
self->use = turret_brain_deactivate;
// Paril NOTE: rhangar1 has a turret_invisible_brain that breaks the
// hangar ceiling; once the final rocket explodes the barrier,
// it attempts to print "Barrier neutralized." to the rocket owner
// who happens to be this brain rather than the player that activated
// the turret. this resolves this by passing it along to fire_rocket.
self->activator = activator;
self->think = turret_brain_link;
self->nextthink = level.time + FRAME_TIME_S;
}
/*QUAKED turret_invisible_brain (1 .5 0) (-16 -16 -16) (16 16 16) x x x x x x x x NOT_EASY NOT_MEDIUM NOT_HARD NOT_DM NOT_COOP
Invisible brain to drive the turret.
Does not search for targets. If targeted, can only be turned on once
and then off once. After that they are completely disabled.
"delay" the delay between firing (default ramps for skill level)
"Target" the turret breach
"Killtarget" the item you want it to attack.
Target the brain if you want it activated later, instead of immediately. It will wait 3 seconds
before firing to acquire the target.
*/
void SP_turret_invisible_brain(gentity_t *self) {
if (!self->killtarget) {
gi.Com_Print("turret_invisible_brain with no killtarget!\n");
G_FreeEntity(self);
return;
}
if (!self->target) {
gi.Com_Print("turret_invisible_brain with no target!\n");
G_FreeEntity(self);
return;
}
if (self->targetname) {
self->use = turret_brain_activate;
} else {
self->think = turret_brain_link;
self->nextthink = level.time + FRAME_TIME_S;
}
self->movetype = MOVETYPE_PUSH;
gi.linkentity(self);
}