forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turret.cpp
621 lines (527 loc) · 17.5 KB
/
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "vehicle.h" // IWYU pragma: associated
#include <algorithm>
#include <memory>
#include "avatar.h"
#include "creature.h"
#include "debug.h"
#include "enums.h"
#include "game.h"
#include "gun_mode.h"
#include "item.h"
#include "itype.h"
#include "messages.h"
#include "npc.h"
#include "player.h"
#include "projectile.h"
#include "ranged.h"
#include "string_formatter.h"
#include "translations.h"
#include "ui.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vehicle_selector.h"
#include "vpart_position.h"
#include "vpart_range.h"
static const itype_id fuel_type_battery( "battery" );
static const efftype_id effect_on_roof( "on_roof" );
std::vector<vehicle_part *> vehicle::turrets()
{
std::vector<vehicle_part *> res;
for( auto &e : parts ) {
if( !e.is_broken() && e.base.is_gun() ) {
res.push_back( &e );
}
}
return res;
}
std::vector<vehicle_part *> vehicle::turrets( const tripoint &target )
{
std::vector<vehicle_part *> res = turrets();
// exclude turrets not ready to fire or where target is out of range
res.erase( std::remove_if( res.begin(), res.end(), [&]( const vehicle_part * e ) {
return turret_query( *e ).query() != turret_data::status::ready ||
rl_dist( global_part_pos3( *e ), target ) > e->base.gun_range();
} ), res.end() );
return res;
}
turret_data vehicle::turret_query( vehicle_part &pt )
{
if( !pt.is_turret() || pt.removed || pt.is_broken() ) {
return turret_data();
}
return turret_data( this, &pt );
}
turret_data vehicle::turret_query( const vehicle_part &pt ) const
{
return const_cast<vehicle *>( this )->turret_query( const_cast<vehicle_part &>( pt ) );
}
turret_data vehicle::turret_query( const tripoint &pos )
{
auto res = get_parts_at( pos, "TURRET", part_status_flag::any );
return !res.empty() ? turret_query( *res.front() ) : turret_data();
}
turret_data vehicle::turret_query( const tripoint &pos ) const
{
return const_cast<vehicle *>( this )->turret_query( pos );
}
std::string turret_data::name() const
{
return part->name();
}
item_location turret_data::base()
{
return item_location( vehicle_cursor( *veh, veh->index_of_part( part ) ), &part->base );
}
item_location turret_data::base() const
{
return item_location( vehicle_cursor( *veh, veh->index_of_part( part ) ), &part->base );
}
int turret_data::ammo_remaining() const
{
if( !veh || !part ) {
return 0;
}
if( part->info().has_flag( "USE_TANKS" ) ) {
return veh->fuel_left( ammo_current() );
}
return part->base.ammo_remaining();
}
int turret_data::ammo_capacity() const
{
if( !veh || !part || part->info().has_flag( "USE_TANKS" ) ) {
return 0;
}
return part->base.ammo_capacity();
}
const itype *turret_data::ammo_data() const
{
if( !veh || !part ) {
return nullptr;
}
if( part->info().has_flag( "USE_TANKS" ) ) {
return ammo_current().is_null() ? nullptr : &*ammo_current();
}
return part->base.ammo_data();
}
itype_id turret_data::ammo_current() const
{
auto opts = ammo_options();
if( opts.count( part->ammo_pref ) ) {
return part->ammo_pref;
}
if( opts.count( part->info().default_ammo ) ) {
return part->info().default_ammo;
}
if( opts.count( part->base.ammo_default() ) ) {
return part->base.ammo_default();
}
return opts.empty() ? itype_id::NULL_ID() : *opts.begin();
}
std::set<itype_id> turret_data::ammo_options() const
{
std::set<itype_id> opts;
if( !veh || !part ) {
return opts;
}
if( !part->info().has_flag( "USE_TANKS" ) ) {
if( !part->base.ammo_current().is_null() ) {
opts.insert( part->base.ammo_current() );
}
} else {
for( const auto &e : veh->fuels_left() ) {
const itype *fuel = &*e.first;
if( fuel->ammo && part->base.ammo_types().count( fuel->ammo->type ) &&
e.second >= part->base.ammo_required() ) {
opts.insert( fuel->get_id() );
}
}
}
return opts;
}
bool turret_data::ammo_select( const itype_id &ammo )
{
if( !ammo_options().count( ammo ) ) {
return false;
}
part->ammo_pref = ammo;
return true;
}
std::set<ammo_effect_str_id> turret_data::ammo_effects() const
{
if( !veh || !part ) {
return std::set<ammo_effect_str_id>();
}
auto res = part->base.ammo_effects();
if( part->info().has_flag( "USE_TANKS" ) && ammo_data() ) {
res.insert( ammo_data()->ammo->ammo_effects.begin(), ammo_data()->ammo->ammo_effects.end() );
}
return res;
}
int turret_data::range() const
{
if( !veh || !part ) {
return 0;
}
if( part->info().has_flag( "USE_TANKS" ) && ammo_data() ) {
if( ammo_data()->ammo->shape ) {
return ammo_data()->ammo->shape->get_range();
}
return part->base.gun_range() + ammo_data()->ammo->range;
}
return part->base.gun_range();
}
bool turret_data::in_range( const tripoint &target ) const
{
if( !veh || !part ) {
return false;
}
int range = veh->turret_query( *part ).range();
int dist = rl_dist( veh->global_part_pos3( *part ), target );
return range >= dist;
}
bool turret_data::can_reload() const
{
if( !veh || !part || part->info().has_flag( "USE_TANKS" ) ) {
return false;
}
if( !part->base.magazine_integral() ) {
// always allow changing of magazines
return true;
}
return part->base.ammo_remaining() < part->base.ammo_capacity();
}
bool turret_data::can_unload() const
{
if( !veh || !part || part->info().has_flag( "USE_TANKS" ) ) {
return false;
}
return part->base.ammo_remaining() || part->base.magazine_current();
}
turret_data::status turret_data::query() const
{
if( !veh || !part ) {
return status::invalid;
}
if( part->info().has_flag( "USE_TANKS" ) ) {
if( veh->fuel_left( ammo_current() ) < part->base.ammo_required() ) {
return status::no_ammo;
}
} else {
if( !part->base.ammo_sufficient() ) {
return status::no_ammo;
}
}
auto ups = part->base.get_gun_ups_drain() * part->base.gun_current_mode().qty;
if( ups > veh->fuel_left( fuel_type_battery, true ) ) {
return status::no_power;
}
return status::ready;
}
void turret_data::prepare_fire( player &p )
{
// prevent turrets from shooting their own vehicles
p.add_effect( effect_on_roof, 1_turns );
// turrets are subject only to recoil_vehicle()
cached_recoil = p.recoil;
p.recoil = 0;
// set fuel tank fluid as ammo, if appropriate
if( part->info().has_flag( "USE_TANKS" ) ) {
auto mode = base()->gun_current_mode();
int qty = mode->ammo_required();
int fuel_left = veh->fuel_left( ammo_current() );
mode->ammo_set( ammo_current(), std::min( qty * mode.qty, fuel_left ) );
}
}
void turret_data::post_fire( player &p, int shots )
{
// remove any temporary recoil adjustments
p.remove_effect( effect_on_roof );
p.recoil = cached_recoil;
auto mode = base()->gun_current_mode();
// handle draining of vehicle tanks and UPS charges, if applicable
if( part->info().has_flag( "USE_TANKS" ) ) {
veh->drain( ammo_current(), mode->ammo_required() * shots );
mode->ammo_unset();
}
veh->drain( fuel_type_battery, mode->get_gun_ups_drain() * shots );
}
int turret_data::fire( player &p, const tripoint &target )
{
if( !veh || !part ) {
return 0;
}
int shots = 0;
auto mode = base()->gun_current_mode();
prepare_fire( p );
shots = ranged::fire_gun( p, target, mode.qty, *mode, item_location() );
post_fire( p, shots );
return shots;
}
void vehicle::turrets_aim_and_fire_single()
{
std::vector<std::string> option_names;
std::vector<vehicle_part *> options;
// Find all turrets that are ready to fire
for( auto &t : turrets() ) {
turret_data data = turret_query( *t );
if( data.query() == turret_data::status::ready ) {
option_names.push_back( t->name() );
options.push_back( t );
}
}
// Select one
if( options.empty() ) {
add_msg( m_warning, _( "None of the turrets are ready to fire." ) );
return;
}
const int idx = uilist( _( "Aim which turret?" ), option_names );
if( idx < 0 ) {
return;
}
vehicle_part *turret = options[idx];
std::vector<vehicle_part *> turrets;
turrets.push_back( turret );
turrets_aim_and_fire( turrets );
}
bool vehicle::turrets_aim_and_fire_all_manual( bool show_msg )
{
std::vector<vehicle_part *> turrets = find_all_ready_turrets( true, false );
if( turrets.empty() ) {
if( show_msg ) {
add_msg( m_warning,
_( "Can't aim turrets: all turrets are offline or set to automatic targeting mode." ) );
}
return false;
}
turrets_aim_and_fire( turrets );
return true;
}
void vehicle::turrets_override_automatic_aim()
{
std::vector<vehicle_part *> turrets = find_all_ready_turrets( false, true );
if( turrets.empty() ) {
add_msg( m_warning,
_( "Can't aim turrets: all turrets are offline or set to manual targeting mode." ) );
return;
}
turrets_aim( turrets );
}
int vehicle::turrets_aim_and_fire( std::vector<vehicle_part *> &turrets )
{
int shots = 0;
if( turrets_aim( turrets ) ) {
for( vehicle_part *t : turrets ) {
bool has_target = t->target.first != t->target.second;
if( has_target ) {
turret_data turret = turret_query( *t );
npc cpu = get_targeting_npc( *t );
shots += turret.fire( cpu, t->target.second );
t->reset_target( global_part_pos3( *t ) );
}
}
}
return shots;
}
bool vehicle::turrets_aim( std::vector<vehicle_part *> &turrets )
{
// Clear existing targets
for( vehicle_part *t : turrets ) {
if( !turret_query( *t ) ) {
debugmsg( "Expected a valid vehicle turret" );
return false;
}
t->reset_target( global_part_pos3( *t ) );
}
// Get target
target_handler::trajectory trajectory = target_handler::mode_turrets( g->u, *this, turrets );
bool got_target = !trajectory.empty();
if( got_target ) {
tripoint target = trajectory.back();
// Set target for any turret in range
for( vehicle_part *t : turrets ) {
if( turret_query( *t ).in_range( target ) ) {
t->target.second = target;
}
}
///\EFFECT_INT speeds up aiming of vehicle turrets
g->u.moves = std::min( 0, g->u.moves - 100 + ( 5 * g->u.int_cur ) );
}
return got_target;
}
std::vector<vehicle_part *> vehicle::find_all_ready_turrets( bool manual, bool automatic )
{
std::vector<vehicle_part *> res;
for( vehicle_part *t : turrets() ) {
if( ( t->enabled && automatic ) || ( !t->enabled && manual ) ) {
if( turret_query( *t ).query() == turret_data::status::ready ) {
res.push_back( t );
}
}
}
return res;
}
void vehicle::turrets_set_targeting()
{
std::vector<vehicle_part *> turrets;
std::vector<tripoint> locations;
for( auto &p : parts ) {
if( p.is_turret() ) {
turrets.push_back( &p );
locations.push_back( global_part_pos3( p ) );
}
}
pointmenu_cb callback( locations );
int sel = 0;
while( true ) {
uilist menu;
menu.text = _( "Set turret targeting" );
menu.callback = &callback;
menu.selected = sel;
menu.fselected = sel;
menu.w_y_setup = 2;
for( auto &p : turrets ) {
menu.addentry( -1, has_part( global_part_pos3( *p ), "TURRET_CONTROLS" ), MENU_AUTOASSIGN,
"%s [%s]", p->name(), p->enabled ?
_( "auto -> manual" ) : has_part( global_part_pos3( *p ), "TURRET_CONTROLS" ) ?
_( "manual -> auto" ) :
_( "manual (turret control unit required for auto mode)" ) );
}
menu.query();
if( menu.ret < 0 || static_cast<size_t>( menu.ret ) >= turrets.size() ) {
break;
}
sel = menu.ret;
if( has_part( locations[ sel ], "TURRET_CONTROLS" ) ) {
turrets[sel]->enabled = !turrets[sel]->enabled;
} else {
turrets[sel]->enabled = false;
}
for( const vpart_reference &vp : get_avail_parts( "TURRET_CONTROLS" ) ) {
vehicle_part &e = vp.part();
if( e.mount == turrets[sel]->mount ) {
e.enabled = turrets[sel]->enabled;
}
}
// clear the turret's current targets to prevent unwanted auto-firing
tripoint pos = locations[ sel ];
turrets[ sel ]->reset_target( pos );
}
}
void vehicle::turrets_set_mode()
{
std::vector<vehicle_part *> turrets;
std::vector<tripoint> locations;
for( auto &p : parts ) {
if( p.base.is_gun() ) {
turrets.push_back( &p );
locations.push_back( global_part_pos3( p ) );
}
}
pointmenu_cb callback( locations );
int sel = 0;
while( true ) {
uilist menu;
menu.text = _( "Set turret firing modes" );
menu.callback = &callback;
menu.selected = sel;
menu.fselected = sel;
menu.w_y_setup = 2;
for( auto &p : turrets ) {
menu.addentry( -1, true, MENU_AUTOASSIGN, "%s [%s]",
p->name(), p->base.gun_current_mode().tname() );
}
menu.query();
if( menu.ret < 0 || static_cast<size_t>( menu.ret ) >= turrets.size() ) {
break;
}
sel = menu.ret;
turrets[ sel ]->base.gun_cycle_mode();
}
}
npc vehicle::get_targeting_npc( const vehicle_part &pt )
{
// Make a fake NPC to represent the targeting system
npc cpu;
cpu.set_fake( true );
cpu.name = string_format( _( "The %s turret" ), pt.get_base().tname( 1 ) );
// turrets are subject only to recoil_vehicle()
cpu.recoil = 0;
// These might all be affected by vehicle part damage, weather effects, etc.
cpu.set_skill_level( pt.get_base().gun_skill(), 8 );
cpu.set_skill_level( skill_id( "gun" ), 4 );
cpu.str_cur = 16;
cpu.dex_cur = 8;
cpu.per_cur = 12;
cpu.setpos( global_part_pos3( pt ) );
// Assume vehicle turrets are friendly to the player.
cpu.set_attitude( NPCATT_FOLLOW );
cpu.set_fac( get_owner() );
return cpu;
}
int vehicle::automatic_fire_turret( vehicle_part &pt )
{
turret_data gun = turret_query( pt );
int shots = 0;
if( gun.query() != turret_data::status::ready ) {
return shots;
}
// The position of the vehicle part.
tripoint pos = global_part_pos3( pt );
// Create the targeting computer's npc
npc cpu = get_targeting_npc( pt );
int area = max_aoe_size( gun.ammo_effects() );
if( area > 0 ) {
// Pad a bit for less friendly fire
area += area == 1 ? 1 : 2;
}
const bool u_see = g->u.sees( pos );
const bool u_hear = !g->u.is_deaf();
// The current target of the turret.
auto &target = pt.target;
if( target.first == target.second ) {
// Manual target not set, find one automatically.
// BEWARE: Calling turret_data.fire on tripoint min coordinates starts a crash
// triggered at `trajectory.insert( trajectory.begin(), source )` at ranged.cpp:236
pt.reset_target( pos );
int boo_hoo;
// TODO: calculate chance to hit and cap range based upon this
int max_range = 20;
int range = std::min( gun.range(), max_range );
Creature *auto_target = cpu.auto_find_hostile_target( range, boo_hoo, area );
if( auto_target == nullptr ) {
if( boo_hoo ) {
cpu.name = string_format( pgettext( "vehicle turret", "The %s" ), pt.name() );
// check if the player can see or hear then print chooses a message accordingly
if( u_see && u_hear ) {
add_msg( m_warning, vgettext( "%s points in your direction and emits an IFF warning beep.",
"%s points in your direction and emits %d annoyed sounding beeps.",
boo_hoo ),
cpu.name, boo_hoo );
} else if( u_hear ) {
add_msg( m_warning, vgettext( "You hear a warning beep.",
"You hear %d annoyed sounding beeps.",
boo_hoo ), boo_hoo );
} else if( u_see ) {
add_msg( m_warning, _( "%s points in your direction." ), cpu.name );
}
}
return shots;
}
target.second = auto_target->pos();
} else {
// Target is already set, make sure we didn't move after aiming (it's a bug if we did).
if( pos != target.first ) {
target.second = target.first;
debugmsg( "%s moved after aiming but before it could fire.", cpu.name );
return shots;
}
}
// Get the turret's target and reset it
tripoint targ = target.second;
pt.reset_target( pos );
shots = gun.fire( cpu, targ );
if( shots && u_see && !g->u.sees( targ ) ) {
add_msg( _( "The %1$s fires its %2$s!" ), name, pt.name() );
}
return shots;
}