-
Notifications
You must be signed in to change notification settings - Fork 280
/
electric_grid_test.cpp
445 lines (378 loc) · 15.9 KB
/
electric_grid_test.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
#include "catch/catch.hpp"
#include <vector>
#include "active_tile_data.h"
#include "active_tile_data_def.h"
#include "cata_utility.h"
#include "coordinate_conversions.h"
#include "distribution_grid.h"
#include "map.h"
#include "mapbuffer.h"
#include "map_helpers.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "submap.h"
#include "state_helpers.h"
#include "stringmaker.h"
#include "vehicle.h"
#include "vehicle_part.h"
static furn_str_id f_battery( "f_battery" );
static furn_str_id f_cable_connector( "f_cable_connector" );
static furn_str_id f_floor_lamp( "f_floor_lamp" );
static furn_str_id f_floor_lamp_on( "f_floor_lamp_on" );
static itype_id itype_battery( "battery" );
static inline void test_grid_veh( distribution_grid &grid, vehicle &veh, battery_tile &battery )
{
CAPTURE( veh.fuel_capacity( itype_battery ) );
CAPTURE( battery.max_stored );
WHEN( "the vehicle is fully charged and battery is discharged" ) {
veh.charge_battery( veh.fuel_capacity( itype_battery ), false );
REQUIRE( veh.fuel_left( itype_battery, false ) ==
veh.fuel_capacity( itype_battery ) );
REQUIRE( battery.get_resource() == 0 );
REQUIRE( grid.get_resource() == veh.fuel_capacity( itype_battery ) );
AND_WHEN( "the grid is discharged without energy in battery" ) {
int deficit = grid.mod_resource( -( grid.get_resource() - 10 ) );
CHECK( deficit == 0 );
THEN( "the power is drained from vehicle" ) {
CHECK( grid.get_resource() == 10 );
CHECK( veh.fuel_left( itype_battery, false ) == 10 );
}
}
AND_WHEN( "the vehicle is charged despite being full" ) {
int excess = veh.charge_battery( 10 );
THEN( "the grid contains the added energy" ) {
CHECK( excess == 0 );
CHECK( grid.get_resource() == veh.fuel_left( itype_battery, false ) + 10 );
AND_THEN( "the added energy is in the battery" ) {
CHECK( battery.get_resource() == 10 );
}
}
}
}
WHEN( "the battery is fully charged and vehicle is discharged" ) {
int excess = battery.mod_resource( battery.max_stored );
REQUIRE( excess == 0 );
REQUIRE( battery.get_resource() == battery.max_stored );
REQUIRE( veh.fuel_left( itype_battery, false ) == 0 );
REQUIRE( grid.get_resource() == battery.get_resource() );
AND_WHEN( "the vehicle is discharged despite being empty" ) {
int deficit = veh.discharge_battery( 10, true );
THEN( "the grid provides the needed power" ) {
CHECK( deficit == 0 );
AND_THEN( "this power comes from the battery" ) {
CHECK( battery.get_resource() == battery.max_stored - 10 );
}
}
}
AND_WHEN( "the grid is charged some more" ) {
int excess = grid.mod_resource( 10 );
THEN( "the grid contains the added energy" ) {
CHECK( excess == 0 );
CHECK( grid.get_resource() == battery.max_stored + 10 );
AND_THEN( "the added energy is in the vehicle" ) {
CHECK( veh.fuel_left( itype_battery, false ) == 10 );
}
}
}
}
}
static void connect_grid_vehicle( map &m, vehicle &veh, vehicle_connector_tile &connector,
const tripoint_abs_ms &connector_abs_pos )
{
const point cable_part_pos;
vehicle_part source_part( vpart_id( "jumper_cable" ), cable_part_pos,
item::spawn( "jumper_cable" ), &veh );
source_part.target.first = connector_abs_pos.raw();
source_part.target.second = connector_abs_pos.raw();
source_part.set_flag( vehicle_part::targets_grid );
connector.connected_vehicles.clear();
connector.connected_vehicles.emplace_back( m.getabs( veh.global_pos3() ) );
int part_index = veh.install_part( cable_part_pos, std::move( source_part ) );
REQUIRE( part_index >= 0 );
}
struct grid_setup {
distribution_grid &grid;
vehicle &veh;
battery_tile &battery;
};
static void clear_grid_connections( map &m )
{
// TODO: fix point types
auto om = overmap_buffer.get_om_global( project_to<coords::omt>( tripoint_abs_sm(
m.get_abs_sub() ) ) );
om.om->set_electric_grid_connections( om.local, {} );
}
static grid_setup set_up_grid( map &m )
{
// TODO: clear_grids()
clear_grid_connections( m );
const tripoint vehicle_local_pos = tripoint( 10, 10, 0 );
const tripoint connector_local_pos = tripoint( 13, 10, 0 );
const tripoint battery_local_pos = tripoint( 14, 10, 0 );
const tripoint_abs_ms connector_abs_pos( m.getabs( connector_local_pos ) );
const tripoint_abs_ms battery_abs_pos( m.getabs( battery_local_pos ) );
m.furn_set( connector_local_pos, f_cable_connector );
m.furn_set( battery_local_pos, f_battery );
vehicle *veh = m.add_vehicle( vproto_id( "car" ), vehicle_local_pos, 0_degrees, 0, 0, false );
vehicle_connector_tile *grid_connector =
active_tiles::furn_at<vehicle_connector_tile>( connector_abs_pos );
battery_tile *battery = active_tiles::furn_at<battery_tile>( battery_abs_pos );
CAPTURE( connector_abs_pos );
CAPTURE( battery_abs_pos );
REQUIRE( veh );
REQUIRE( grid_connector );
REQUIRE( battery );
connect_grid_vehicle( m, *veh, *grid_connector, connector_abs_pos );
distribution_grid &grid = get_distribution_grid_tracker().grid_at( connector_abs_pos );
REQUIRE( !grid.empty() );
REQUIRE( &grid == &get_distribution_grid_tracker().grid_at( battery_abs_pos ) );
return grid_setup{grid, *veh, *battery};
}
TEST_CASE( "grid_and_vehicle_in_bubble", "[grids][vehicle]" )
{
clear_all_state();
put_player_underground();
GIVEN( "vehicle and battery are on one grid" ) {
auto setup = set_up_grid( get_map() );
test_grid_veh( setup.grid, setup.veh, setup.battery );
}
}
TEST_CASE( "grid_and_vehicle_outside_bubble", "[grids][vehicle]" )
{
clear_all_state();
put_player_underground();
map &m = get_map();
const tripoint old_abs_sub = m.get_abs_sub();
// Ugly: we move the real map instead of the tinymap to reuse clear_map() results
m.load( m.get_abs_sub() + point( m.getmapsize(), 0 ), true );
GIVEN( "vehicle and battery are on one grid" ) {
tinymap tm;
tm.load( old_abs_sub, false );
auto setup = set_up_grid( tm );
tm.save();
test_grid_veh( setup.grid, setup.veh, setup.battery );
}
}
struct grid_setup_consumer {
distribution_grid &grid;
steady_consumer_tile &consumer;
battery_tile &battery;
tripoint_abs_ms consumer_pos;
};
struct grid_setup_watcher {
distribution_grid &grid;
charge_watcher_tile &watcher;
battery_tile &battery;
tripoint_abs_ms watcher_pos;
};
template<typename T, typename S>
static S set_up_grid_with_consumer( map &m, const furn_str_id &act_tile_id )
{
// TODO: clear_grids()
clear_grid_connections( m );
const tripoint act_local_pos = tripoint( 13, 10, 0 );
const tripoint battery_local_pos = tripoint( 14, 10, 0 );
const tripoint_abs_ms act_abs_pos( m.getabs( act_local_pos ) );
const tripoint_abs_ms battery_abs_pos( m.getabs( battery_local_pos ) );
m.furn_set( act_local_pos, act_tile_id );
m.furn_set( battery_local_pos, f_battery );
T *act_tile = active_tiles::furn_at<T>( act_abs_pos );
battery_tile *battery = active_tiles::furn_at<battery_tile>( battery_abs_pos );
CAPTURE( act_abs_pos );
CAPTURE( battery_abs_pos );
REQUIRE( act_tile );
REQUIRE( battery );
distribution_grid &grid = get_distribution_grid_tracker().grid_at( act_abs_pos );
REQUIRE( !grid.empty() );
REQUIRE( &grid == &get_distribution_grid_tracker().grid_at( battery_abs_pos ) );
return S{grid, *act_tile, *battery, act_abs_pos};
}
static void require_empty_queue( const grid_furn_transform_queue &q )
{
static const grid_furn_transform_queue empty_queue;
REQUIRE( q == empty_queue );
}
static void test_steady_consumer( grid_setup_consumer &setup )
{
grid_furn_transform_queue &tf_queue = get_distribution_grid_tracker().get_transform_queue();
auto _cleanup = on_out_of_scope( [&]() {
tf_queue.clear();
} );
distribution_grid &grid = setup.grid;
steady_consumer_tile &consumer = setup.consumer;
battery_tile &battery = setup.battery;
CAPTURE( battery.max_stored );
CAPTURE( consumer.power );
CAPTURE( consumer.consume_every );
CAPTURE( consumer.transform.id );
REQUIRE( consumer.consume_every > 1_seconds );
WHEN( "the battery is fully charged" ) {
int excess = battery.mod_resource( battery.max_stored );
REQUIRE( excess == 0 );
REQUIRE( battery.get_resource() == battery.max_stored );
REQUIRE( grid.get_resource() == battery.get_resource() );
AND_WHEN( "1 consumer tick passes" ) {
time_point to = calendar::turn + consumer.consume_every;
grid.update( to );
THEN( "the battery has been drained by specified amount per tick" ) {
REQUIRE( grid.get_resource() == battery.max_stored - consumer.power );
require_empty_queue( tf_queue );
}
}
AND_WHEN( "less than 1 consumer tick passes" ) {
time_point to = calendar::turn + consumer.consume_every - 1_seconds;
grid.update( to );
THEN( "no changes" ) {
REQUIRE( grid.get_resource() == battery.max_stored );
require_empty_queue( tf_queue );
}
AND_WHEN( "3 consumer ticks pass" ) {
to += consumer.consume_every * 3;
grid.update( to );
THEN( "the battery has been drained by 3x specified amount per tick" ) {
REQUIRE( grid.get_resource() == battery.max_stored - consumer.power * 3 );
require_empty_queue( tf_queue );
}
}
}
}
WHEN( "the battery has power for 1 consumer tick" ) {
int excess = battery.mod_resource( 3 );
REQUIRE( excess == 0 );
REQUIRE( battery.get_resource() == 3 );
REQUIRE( grid.get_resource() == battery.get_resource() );
AND_WHEN( "1 consumer tick passes" ) {
time_point to = calendar::turn + consumer.consume_every;
grid.update( to );
THEN( "the battery has been fully drained" ) {
REQUIRE( grid.get_resource() == 0 );
require_empty_queue( tf_queue );
}
}
AND_WHEN( "less than 1 consumer tick passes" ) {
time_point to = calendar::turn + consumer.consume_every - 1_seconds;
grid.update( to );
THEN( "no changes" ) {
REQUIRE( grid.get_resource() == 3 );
require_empty_queue( tf_queue );
}
AND_WHEN( "3 consumer ticks pass" ) {
to += consumer.consume_every * 3;
grid.update( to );
THEN( "the battery has been fully drained, and transform has been queued" ) {
REQUIRE( grid.get_resource() == 0 );
grid_furn_transform_queue single_dead_lamp;
single_dead_lamp.add( setup.consumer_pos, f_floor_lamp, "The lamp flickers and dies." );
REQUIRE( tf_queue == single_dead_lamp );
}
}
}
}
}
static void test_charge_watcher( grid_setup_watcher &setup )
{
grid_furn_transform_queue &tf_queue = get_distribution_grid_tracker().get_transform_queue();
auto _cleanup = on_out_of_scope( [&]() {
tf_queue.clear();
} );
distribution_grid &grid = setup.grid;
charge_watcher_tile &watcher = setup.watcher;
battery_tile &battery = setup.battery;
CAPTURE( battery.max_stored );
CAPTURE( watcher.min_power );
CAPTURE( watcher.transform.id );
WHEN( "battery charge < watcher limit" ) {
int excess = battery.mod_resource( 3 );
REQUIRE( excess == 0 );
REQUIRE( battery.get_resource() == 3 );
REQUIRE( grid.get_resource() == battery.get_resource() );
AND_WHEN( "1 turn passes" ) {
time_point to = calendar::turn + 1_seconds;
grid.update( to );
THEN( "nothing happens" ) {
require_empty_queue( tf_queue );
}
}
}
WHEN( "battery charge >= watcher limit" ) {
int excess = battery.mod_resource( 5 );
REQUIRE( excess == 0 );
REQUIRE( battery.get_resource() == 5 );
REQUIRE( grid.get_resource() == battery.get_resource() );
AND_WHEN( "1 turn passes" ) {
time_point to = calendar::turn + 1_seconds;
grid.update( to );
THEN( "transform has been queued" ) {
grid_furn_transform_queue single_lit_lamp;
single_lit_lamp.add( setup.watcher_pos, f_floor_lamp_on, "The lamp lights up." );
REQUIRE( tf_queue == single_lit_lamp );
}
}
}
}
TEST_CASE( "steady_consumer_in_bubble", "[grids]" )
{
clear_all_state();
calendar::turn = calendar::turn_zero;
put_player_underground();
GIVEN( "consumer and battery are on one grid" ) {
grid_setup_consumer setup = set_up_grid_with_consumer<steady_consumer_tile, grid_setup_consumer>
( get_map(), f_floor_lamp_on );
test_steady_consumer( setup );
}
}
TEST_CASE( "charge_watcher_in_bubble", "[grids]" )
{
clear_all_state();
calendar::turn = calendar::turn_zero;
put_player_underground();
GIVEN( "watcher and battery are on one grid" ) {
grid_setup_watcher setup = set_up_grid_with_consumer<charge_watcher_tile, grid_setup_watcher>
( get_map(), f_floor_lamp );
test_charge_watcher( setup );
}
}
TEST_CASE( "grid_furn_transform_queue_in_bubble", "[grids]" )
{
clear_all_state();
calendar::turn = calendar::turn_zero;
put_player_underground();
tripoint pos_local( 22, 7, 0 );
tripoint_abs_ms pos_abs( get_map().getabs( pos_local ) );
grid_furn_transform_queue tf_queue;
tf_queue.add( pos_abs, f_floor_lamp_on, "" );
CAPTURE( pos_abs );
REQUIRE( get_map().furn( pos_local ).id() != f_floor_lamp_on );
REQUIRE( active_tiles::furn_at<active_tile_data>( pos_abs ) == nullptr );
tf_queue.apply( MAPBUFFER, get_distribution_grid_tracker(), get_player_character(), get_map() );
REQUIRE( get_map().furn( pos_local ).id() == f_floor_lamp_on );
REQUIRE( active_tiles::furn_at<steady_consumer_tile>( pos_abs ) != nullptr );
}
TEST_CASE( "grid_furn_transform_queue_outside_bubble", "[grids]" )
{
clear_all_state();
calendar::turn = calendar::turn_zero;
put_player_underground();
tripoint pos_local( 22, 7, 0 );
tripoint_abs_ms pos_abs( get_map().getabs( pos_local ) );
tripoint_abs_sm pos_abs_sm;
point_sm_ms pos_in_sm;
std::tie( pos_abs_sm, pos_in_sm ) = project_remain<coords::sm>( pos_abs );
// Ugly: we move the real map to have submap exist in mapbuffer only
map &m = get_map();
m.load( m.get_abs_sub() + point( m.getmapsize(), 0 ), true );
grid_furn_transform_queue tf_queue;
tf_queue.add( pos_abs, f_floor_lamp_on, "" );
submap *sm = nullptr;
CAPTURE( pos_abs );
CAPTURE( pos_abs_sm );
sm = MAPBUFFER.lookup_submap( pos_abs_sm );
REQUIRE( sm );
REQUIRE( sm->get_furn( pos_in_sm.raw() ).id() != f_floor_lamp_on );
REQUIRE( active_tiles::furn_at<active_tile_data>( pos_abs ) == nullptr );
tf_queue.apply( MAPBUFFER, get_distribution_grid_tracker(), get_player_character(), get_map() );
sm = MAPBUFFER.lookup_submap( pos_abs_sm );
REQUIRE( sm );
REQUIRE( sm->get_furn( pos_in_sm.raw() ).id() == f_floor_lamp_on );
REQUIRE( active_tiles::furn_at<steady_consumer_tile>( pos_abs ) != nullptr );
}