Skip to content

Commit 5a159f4

Browse files
authored
Merge pull request #82183 from ehughsbaird/body-bionic-art-optional
Use optional/mandatory for ascii_art, bionic_data, bodypart
2 parents a16423a + 74d8aef commit 5a159f4

File tree

4 files changed

+34
-32
lines changed

4 files changed

+34
-32
lines changed

data/json/bionics.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@
14521452
"description": "A small module connected to your brain allows you to interface with nearby devices with wireless capabilities.",
14531453
"occupied_bodyparts": [ [ "head", 2 ] ],
14541454
"flags": [ "BIONIC_TOGGLED" ],
1455-
"react_cost": 1,
1455+
"react_cost": "1 kJ",
14561456
"time": "24 s"
14571457
},
14581458
{
@@ -1461,7 +1461,7 @@
14611461
"name": { "str": "Sonic Resonator" },
14621462
"description": "Your entire body may resonate at very high power, creating a short-range shockwave. While it will not do much damage to creatures, solid objects such as walls and doors will be damaged.",
14631463
"occupied_bodyparts": [ [ "torso", 15 ], [ "head", 1 ], [ "arm_l", 2 ], [ "arm_r", 2 ], [ "leg_l", 4 ], [ "leg_r", 4 ] ],
1464-
"act_cost": 100
1464+
"act_cost": "100 kJ"
14651465
},
14661466
{
14671467
"id": "bio_scent_mask",
@@ -1471,8 +1471,8 @@
14711471
"occupied_bodyparts": [ [ "torso", 3 ], [ "head", 1 ], [ "arm_l", 1 ], [ "arm_r", 1 ], [ "leg_l", 1 ], [ "leg_r", 1 ] ],
14721472
"flags": [ "BIONIC_TOGGLED" ],
14731473
"active_flags": [ "NO_SCENT" ],
1474-
"act_cost": 1,
1475-
"react_cost": 1,
1474+
"act_cost": "1 kJ",
1475+
"react_cost": "1 kJ",
14761476
"time": "1 m"
14771477
},
14781478
{
@@ -1492,7 +1492,6 @@
14921492
"flags": [ "BIONIC_TOGGLED" ],
14931493
"active_flags": [ "STOP_SLEEP_DEPRIVATION" ],
14941494
"occupied_bodyparts": [ [ "head", 1 ], [ "torso", 1 ] ],
1495-
"act_cost": 0,
14961495
"react_cost": "1 kJ",
14971496
"time": "100 s"
14981497
},
@@ -1511,7 +1510,7 @@
15111510
"name": { "str": "Shockwave Generator" },
15121511
"description": "You generate a powerful shockwave, knocking back all nearby creatures. Targets are stunned briefly, take damage and additional stun upon impact with impassable terrain, and knockback any creatures they collide with.",
15131512
"occupied_bodyparts": [ [ "torso", 20 ] ],
1514-
"act_cost": 250
1513+
"act_cost": "250 kJ"
15151514
},
15161515
{
15171516
"id": "bio_sleepy",
@@ -1648,7 +1647,7 @@
16481647
"name": { "str": "Teleportation Unit" },
16491648
"description": "This highly experimental unit folds space over short distances, instantly transporting your body up to 25 feet at the cost of much power. Note that prolonged or frequent use may have dangerous side effects.",
16501649
"occupied_bodyparts": [ [ "torso", 16 ], [ "arm_l", 3 ], [ "arm_r", 3 ], [ "leg_l", 4 ], [ "leg_r", 4 ] ],
1651-
"act_cost": 250
1650+
"act_cost": "250 kJ"
16521651
},
16531652
{
16541653
"id": "bio_thumbs",
@@ -1677,7 +1676,7 @@
16771676
[ "foot_l", 1 ],
16781677
[ "foot_r", 1 ]
16791678
],
1680-
"act_cost": 50
1679+
"act_cost": "50 kJ"
16811680
},
16821681
{
16831682
"id": "bio_tools",

src/ascii_art.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <string>
44

5-
#include "assign.h"
65
#include "catacharset.h"
76
#include "debug.h"
87
#include "generic_factory.h"
@@ -34,9 +33,7 @@ void ascii_art::load_ascii_art( const JsonObject &jo, const std::string &src )
3433

3534
void ascii_art::load( const JsonObject &jo, std::string_view )
3635
{
37-
assign( jo, "id", id );
38-
39-
assign( jo, "picture", picture );
36+
mandatory( jo, was_loaded, "picture", picture );
4037
for( std::string &line : picture ) {
4138
if( utf8_width( remove_color_tags( line ) ) > ascii_art_width ) {
4239
line = trim_by_length( line, ascii_art_width );

src/bionics.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
#include "action.h"
2020
#include "activity_actor_definitions.h"
21-
#include "assign.h"
2221
#include "avatar.h"
2322
#include "avatar_action.h"
2423
#include "ballistics.h"
@@ -313,12 +312,15 @@ void bionic_data::load( const JsonObject &jsobj, std::string_view src )
313312
mandatory( jsobj, was_loaded, "description", description );
314313

315314
optional( jsobj, was_loaded, "cant_remove_reason", cant_remove_reason );
316-
// uses assign because optional doesn't handle loading units as strings
317-
assign( jsobj, "react_cost", power_over_time, false, 0_kJ );
318-
assign( jsobj, "capacity", capacity, false );
319-
assign( jsobj, "act_cost", power_activate, false, 0_kJ );
320-
assign( jsobj, "deact_cost", power_deactivate, false, 0_kJ );
321-
assign( jsobj, "trigger_cost", power_trigger, false, 0_kJ );
315+
optional( jsobj, was_loaded, "react_cost", power_over_time,
316+
units_bound_reader<units::energy>( 0_kJ ), 0_kJ );
317+
optional( jsobj, was_loaded, "capacity", capacity, 0_kJ );
318+
optional( jsobj, was_loaded, "act_cost", power_activate,
319+
units_bound_reader<units::energy>( 0_kJ ), 0_kJ );
320+
optional( jsobj, was_loaded, "deact_cost", power_deactivate,
321+
units_bound_reader<units::energy>( 0_kJ ), 0_kJ );
322+
optional( jsobj, was_loaded, "trigger_cost", power_trigger,
323+
units_bound_reader<units::energy>( 0_kJ ), 0_kJ );
322324

323325
optional( jsobj, was_loaded, "time", charge_time, 0_turns );
324326

src/bodypart.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <utility>
99
#include <vector>
1010

11-
#include "assign.h"
1211
#include "body_part_set.h"
1312
#include "creature.h"
1413
#include "debug.h"
@@ -285,6 +284,21 @@ const std::vector<body_part_type> &body_part_type::get_all()
285284
return body_part_factory.get_all();
286285
}
287286

287+
class encumbrance_per_weight_reader : public generic_typed_reader<encumbrance_per_weight_reader>
288+
{
289+
public:
290+
std::pair<units::mass, int> get_next( const JsonValue &jv ) const {
291+
std::pair<units::mass, int> ret;
292+
if( !jv.test_object() ) {
293+
jv.throw_error( "Invalid format" );
294+
}
295+
JsonObject entry = jv.get_object();
296+
entry.read( "weight", ret.first, true );
297+
entry.read( "encumbrance", ret.second, true );
298+
return ret;
299+
}
300+
};
301+
288302
void body_part_type::load( const JsonObject &jo, std::string_view )
289303
{
290304
mandatory( jo, was_loaded, "id", id );
@@ -459,18 +473,8 @@ void body_part_type::load( const JsonObject &jo, std::string_view )
459473

460474
mandatory( jo, was_loaded, "sub_parts", sub_parts );
461475

462-
if( jo.has_array( "encumbrance_per_weight" ) ) {
463-
const JsonArray &jarr = jo.get_array( "encumbrance_per_weight" );
464-
for( const JsonObject jval : jarr ) {
465-
units::mass weight = 0_gram;
466-
int encumbrance = 0;
467-
468-
assign( jval, "weight", weight, true );
469-
mandatory( jval, was_loaded, "encumbrance", encumbrance );
470-
471-
encumbrance_per_weight.insert( std::pair<units::mass, int>( weight, encumbrance ) );
472-
}
473-
}
476+
optional( jo, was_loaded, "encumbrance_per_weight", encumbrance_per_weight,
477+
encumbrance_per_weight_reader{} );
474478
}
475479

476480
void bp_onhit_effect::load( const JsonObject &jo )

0 commit comments

Comments
 (0)