forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vehicle_part.cpp
611 lines (515 loc) · 15.3 KB
/
vehicle_part.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
#include "vehicle.h" // IWYU pragma: associated
#include <algorithm>
#include <cassert>
#include <cmath>
#include <memory>
#include <set>
#include "avatar.h"
#include "color.h"
#include "debug.h"
#include "enums.h"
#include "flat_set.h"
#include "game.h"
#include "item.h"
#include "item_contents.h"
#include "itype.h"
#include "map.h"
#include "messages.h"
#include "npc.h"
#include "string_formatter.h"
#include "translations.h"
#include "value_ptr.h"
#include "veh_type.h"
#include "vpart_position.h"
#include "weather.h"
static const itype_id fuel_type_battery( "battery" );
static const itype_id fuel_type_none( "null" );
static const itype_id itype_battery( "battery" );
static const itype_id itype_muscle( "muscle" );
/*-----------------------------------------------------------------------------
* VEHICLE_PART
*-----------------------------------------------------------------------------*/
vehicle_part::vehicle_part()
: id( vpart_id::NULL_ID() ) {}
vehicle_part::vehicle_part( const vpart_id &vp, point dp, item &&obj )
: mount( dp ), id( vp ), base( std::move( obj ) )
{
// Mark base item as being installed as a vehicle part
base.set_flag( "VEHICLE" );
if( base.typeId() != vp->item ) {
debugmsg( "incorrect vehicle part item, expected: %s, received: %s",
vp->item.c_str(), base.typeId().c_str() );
}
}
vehicle_part::operator bool() const
{
return id != vpart_id::NULL_ID();
}
const item &vehicle_part::get_base() const
{
return base;
}
void vehicle_part::set_base( const item &new_base )
{
base = new_base;
}
item vehicle_part::properties_to_item() const
{
map &here = get_map();
item tmp = base;
tmp.unset_flag( "VEHICLE" );
// Cables get special handling: their target coordinates need to remain
// stored, and if a cable actually drops, it should be half-connected.
// Except grid-connected ones, for now.
if( tmp.has_flag( "CABLE_SPOOL" ) && !tmp.has_flag( "TOW_CABLE" ) ) {
if( has_flag( targets_grid ) ) {
// Ideally, we'd drop the cable on the charger instead
tmp.erase_var( "source_x" );
tmp.erase_var( "source_y" );
tmp.erase_var( "source_z" );
tmp.erase_var( "state" );
tmp.active = false;
tmp.charges = tmp.type->maximum_charges();
} else {
const tripoint local_pos = here.getlocal( target.first );
if( !here.veh_at( local_pos ) ) {
// That vehicle ain't there no more.
tmp.set_flag( "NO_DROP" );
}
tmp.set_var( "source_x", target.first.x );
tmp.set_var( "source_y", target.first.y );
tmp.set_var( "source_z", target.first.z );
tmp.set_var( "state", "pay_out_cable" );
tmp.active = true;
}
}
// force rationalization of damage values to the middle value of each damage level so
// that parts will stack nicely
tmp.set_damage( tmp.damage_level( 4 ) * itype::damage_scale );
return tmp;
}
std::string vehicle_part::name( bool with_prefix ) const
{
auto res = info().name();
if( base.engine_displacement() > 0 ) {
res.insert( 0, string_format( _( "%2.1fL " ), base.engine_displacement() / 100.0 ) );
} else if( wheel_diameter() > 0 ) {
res.insert( 0, string_format( _( "%d\" " ), wheel_diameter() ) );
}
if( base.is_faulty() ) {
res += _( " (faulty)" );
}
if( base.has_var( "contained_name" ) ) {
res += string_format( _( " holding %s" ), base.get_var( "contained_name" ) );
}
if( is_leaking() ) {
res += _( " (draining)" );
}
if( with_prefix ) {
res.insert( 0, colorize( base.damage_symbol(), base.damage_color() ) + " " );
}
return res;
}
int vehicle_part::hp() const
{
const int dur = info().durability;
if( base.max_damage() > 0 ) {
return dur - dur * base.damage() / base.max_damage();
} else {
return dur;
}
}
int vehicle_part::damage() const
{
return base.damage();
}
int vehicle_part::max_damage() const
{
return base.max_damage();
}
int vehicle_part::damage_level( int max ) const
{
return base.damage_level( max );
}
double vehicle_part::health_percent() const
{
return 1.0 - static_cast<double>( base.damage() ) / base.max_damage();
}
double vehicle_part::damage_percent() const
{
return static_cast<double>( base.damage() ) / base.max_damage();
}
/** parts are considered broken at zero health */
bool vehicle_part::is_broken() const
{
return base.damage() >= base.max_damage();
}
bool vehicle_part::is_unavailable( const bool carried ) const
{
return is_broken() || ( has_flag( carried_flag ) && carried );
}
bool vehicle_part::is_available( const bool carried ) const
{
return !is_unavailable( carried );
}
itype_id vehicle_part::fuel_current() const
{
if( is_engine() ) {
if( ammo_pref.is_null() ) {
return info().fuel_type != itype_muscle ? info().fuel_type : itype_id::NULL_ID();
} else {
return ammo_pref;
}
}
return itype_id::NULL_ID();
}
bool vehicle_part::fuel_set( const itype_id &fuel )
{
if( is_engine() ) {
for( const itype_id &avail : info().engine_fuel_opts() ) {
if( fuel == avail ) {
ammo_pref = fuel;
return true;
}
}
}
return false;
}
itype_id vehicle_part::ammo_current() const
{
if( is_battery() ) {
return itype_battery;
}
if( is_tank() && !base.contents.empty() ) {
return base.contents.front().typeId();
}
if( is_fuel_store( false ) || is_turret() ) {
return base.ammo_current();
}
return itype_id::NULL_ID();
}
int vehicle_part::ammo_capacity() const
{
if( is_tank() ) {
return ammo_current()->charges_per_volume( base.get_container_capacity() );
}
if( is_fuel_store( false ) || is_turret() ) {
return base.ammo_capacity();
}
return 0;
}
int vehicle_part::ammo_remaining() const
{
if( is_tank() ) {
return base.contents.empty() ? 0 : base.contents.back().charges;
}
if( is_fuel_store( false ) || is_turret() ) {
return base.ammo_remaining();
}
return 0;
}
int vehicle_part::ammo_set( const itype_id &ammo, int qty )
{
const itype *liquid = &*ammo;
// We often check if ammo is set to see if tank is empty, if qty == 0 don't set ammo
if( is_tank() && liquid->phase >= LIQUID && qty != 0 ) {
base.contents.clear_items();
const auto stack = units::legacy_volume_factor / std::max( liquid->stack_size, 1 );
const int limit = units::from_milliliter( ammo_capacity() ) / stack;
base.put_in( item( ammo, calendar::turn, qty > 0 ? std::min( qty, limit ) : limit ) );
return qty;
}
if( is_turret() ) {
return base.ammo_set( ammo, qty ).ammo_remaining();
}
if( is_fuel_store() ) {
base.ammo_set( ammo, qty >= 0 ? qty : ammo_capacity() );
return base.ammo_remaining();
}
return -1;
}
void vehicle_part::ammo_unset()
{
if( is_tank() ) {
base.contents.clear_items();
} else if( is_fuel_store() ) {
base.ammo_unset();
}
}
int vehicle_part::ammo_consume( int qty, const tripoint &pos )
{
if( is_tank() && !base.contents.empty() ) {
const int res = std::min( ammo_remaining(), qty );
item &liquid = base.contents.back();
liquid.charges -= res;
if( liquid.charges == 0 ) {
base.contents.clear_items();
}
return res;
}
return base.ammo_consume( qty, pos );
}
double vehicle_part::consume_energy( const itype_id &ftype, double energy_j )
{
if( base.contents.empty() || !is_fuel_store() ) {
return 0.0f;
}
item &fuel = base.contents.back();
if( fuel.typeId() == ftype ) {
assert( fuel.is_fuel() );
// convert energy density in MJ/L to J/ml
const double energy_p_mL = fuel.fuel_energy() * 1000;
const int ml_to_use = static_cast<int>( std::floor( energy_j / energy_p_mL ) );
int charges_to_use = fuel.charges_per_volume( ml_to_use * 1_ml );
if( !charges_to_use ) {
return 0.0;
}
if( charges_to_use >= fuel.charges ) {
charges_to_use = fuel.charges;
base.contents.clear_items();
} else {
fuel.charges -= charges_to_use;
}
item fuel_consumed( ftype, calendar::turn, charges_to_use );
return energy_p_mL * units::to_milliliter<int>( fuel_consumed.volume( true ) );
}
return 0.0;
}
bool vehicle_part::can_reload( const item &obj ) const
{
// first check part is not destroyed and can contain ammo
if( !is_fuel_store() ) {
return false;
}
if( !obj.is_null() ) {
const itype_id obj_type = obj.typeId();
if( is_reactor() ) {
return base.is_reloadable_with( obj_type );
}
// forbid filling tanks with solids or non-material things
if( is_tank() && ( obj.made_of( SOLID ) || obj.made_of( PNULL ) ) ) {
return false;
}
// forbid putting liquids, gasses, and plasma in things that aren't tanks
else if( !obj.made_of( SOLID ) && !is_tank() ) {
return false;
}
// prevent mixing of different ammo
if( !ammo_current().is_null() && ammo_current() != obj_type ) {
return false;
}
// For storage with set type, prevent filling with different types
if( info().fuel_type != fuel_type_none && info().fuel_type != obj_type ) {
return false;
}
// don't fill magazines with inappropriate fuel
if( !is_tank() && !base.is_reloadable_with( obj_type ) ) {
return false;
}
}
return ammo_remaining() < ammo_capacity();
}
void vehicle_part::process_contents( const tripoint &pos, const bool e_heater )
{
// for now we only care about processing food containers since things like
// fuel don't care about temperature yet
if( base.is_food_container() ) {
temperature_flag flag = temperature_flag::TEMP_NORMAL;
if( e_heater ) {
flag = temperature_flag::TEMP_HEATER;
}
base.process( nullptr, pos, false, flag );
}
}
bool vehicle_part::fill_with( item &liquid, int qty )
{
if( !is_tank() || !can_reload( liquid ) ) {
return false;
}
base.fill_with( liquid, qty );
return true;
}
const std::set<fault_id> &vehicle_part::faults() const
{
return base.faults;
}
std::set<fault_id> vehicle_part::faults_potential() const
{
return base.faults_potential();
}
bool vehicle_part::fault_set( const fault_id &f )
{
if( !faults_potential().count( f ) ) {
return false;
}
base.faults.insert( f );
return true;
}
int vehicle_part::wheel_area() const
{
return info().wheel_area();
}
/** Get wheel diameter (inches) or return 0 if part is not wheel */
int vehicle_part::wheel_diameter() const
{
return base.is_wheel() ? base.type->wheel->diameter : 0;
}
/** Get wheel width (inches) or return 0 if part is not wheel */
int vehicle_part::wheel_width() const
{
return base.is_wheel() ? base.type->wheel->width : 0;
}
npc *vehicle_part::crew() const
{
if( is_broken() || !crew_id.is_valid() ) {
return nullptr;
}
npc *const res = g->critter_by_id<npc>( crew_id );
if( !res ) {
return nullptr;
}
return res->is_player_ally() ? res : nullptr;
}
bool vehicle_part::set_crew( const npc &who )
{
if( who.is_dead_state() || !( who.is_walking_with() || who.is_player_ally() ) ) {
return false;
}
if( is_broken() || ( !is_seat() && !is_turret() ) ) {
return false;
}
crew_id = who.getID();
return true;
}
void vehicle_part::unset_crew()
{
crew_id = character_id();
}
void vehicle_part::reset_target( const tripoint &pos )
{
target.first = pos;
target.second = pos;
}
bool vehicle_part::is_engine() const
{
return info().has_flag( VPFLAG_ENGINE );
}
bool vehicle_part::is_light() const
{
const auto &vp = info();
return vp.has_flag( VPFLAG_CONE_LIGHT ) ||
vp.has_flag( VPFLAG_WIDE_CONE_LIGHT ) ||
vp.has_flag( VPFLAG_HALF_CIRCLE_LIGHT ) ||
vp.has_flag( VPFLAG_CIRCLE_LIGHT ) ||
vp.has_flag( VPFLAG_AISLE_LIGHT ) ||
vp.has_flag( VPFLAG_DOME_LIGHT ) ||
vp.has_flag( VPFLAG_ATOMIC_LIGHT );
}
bool vehicle_part::is_fuel_store( bool skip_broke ) const
{
if( skip_broke && is_broken() ) {
return false;
}
return is_tank() || base.is_magazine() || is_reactor();
}
bool vehicle_part::is_tank() const
{
return base.is_watertight_container();
}
bool vehicle_part::is_battery() const
{
return base.is_magazine() && base.ammo_types().count( ammotype( "battery" ) );
}
bool vehicle_part::is_reactor() const
{
return info().has_flag( VPFLAG_REACTOR );
}
bool vehicle_part::is_leaking() const
{
return health_percent() <= 0.5 && ( is_tank() || is_battery() || is_reactor() );
}
bool vehicle_part::is_turret() const
{
return base.is_gun();
}
bool vehicle_part::is_seat() const
{
return info().has_flag( "SEAT" );
}
const vpart_info &vehicle_part::info() const
{
if( !info_cache ) {
info_cache = &id.obj();
}
return *info_cache;
}
void vehicle::set_hp( vehicle_part &pt, int qty )
{
if( qty == pt.info().durability || pt.info().durability <= 0 ) {
pt.base.set_damage( 0 );
} else if( qty == 0 ) {
pt.base.set_damage( pt.base.max_damage() );
} else {
pt.base.set_damage( pt.base.max_damage() - pt.base.max_damage() * qty / pt.info().durability );
}
}
bool vehicle::mod_hp( vehicle_part &pt, int qty, damage_type dt )
{
if( pt.info().durability > 0 ) {
return pt.base.mod_damage( -( pt.base.max_damage() * qty / pt.info().durability ), dt );
} else {
return false;
}
}
bool vehicle::can_enable( const vehicle_part &pt, bool alert ) const
{
if( std::none_of( parts.begin(), parts.end(), [&pt]( const vehicle_part & e ) {
return &e == &pt;
} ) || pt.removed ) {
debugmsg( "Cannot enable removed or non-existent part" );
}
if( pt.is_broken() ) {
return false;
}
if( pt.info().has_flag( "PLANTER" ) && !warm_enough_to_plant( g->u.pos() ) ) {
if( alert ) {
add_msg( m_bad, _( "It is too cold to plant anything now." ) );
}
return false;
}
// TODO: check fuel for combustion engines
if( pt.info().epower < 0 && fuel_left( fuel_type_battery, true ) <= 0 ) {
if( alert ) {
add_msg( m_bad, _( "Insufficient power to enable %s" ), pt.name() );
}
return false;
}
return true;
}
bool vehicle::assign_seat( vehicle_part &pt, const npc &who )
{
if( !pt.is_seat() || !pt.set_crew( who ) ) {
return false;
}
// NPC's can only be assigned to one seat in the vehicle
for( auto &e : parts ) {
if( &e == &pt ) {
// skip this part
continue;
}
if( e.is_seat() ) {
const npc *n = e.crew();
if( n && n->getID() == who.getID() ) {
e.unset_crew();
}
}
}
return true;
}
std::string vehicle_part::carried_name() const
{
if( carry_names.empty() ) {
return std::string();
}
return carry_names.top().substr( name_offset );
}