Skip to content

Commit 396cb3d

Browse files
authored
Merge pull request #82728 from ehughsbaird/some-iuse-actor
optional/mandatory for some iuse actors
2 parents 573e56c + 78994ac commit 396cb3d

File tree

2 files changed

+51
-65
lines changed

2 files changed

+51
-65
lines changed

src/iuse_actor.cpp

Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -213,66 +213,55 @@ std::unique_ptr<iuse_actor> iuse_transform::clone() const
213213

214214
void iuse_transform::load( const JsonObject &obj, const std::string & )
215215
{
216-
obj.read( "target", target, true );
217-
obj.read( "target_group", target_group, true );
216+
optional( obj, false, "target", target );
217+
optional( obj, false, "target_group", target_group );
218218

219219
if( !target.is_empty() && !target_group.is_empty() ) {
220220
obj.throw_error_at( "target_group", "Cannot use both target and target_group at once" );
221221
}
222222

223-
obj.read( "msg", msg_transform );
224-
obj.read( "variant_type", variant_type );
225-
obj.read( "container", container );
226-
obj.read( "sealed", sealed );
223+
optional( obj, false, "msg", msg_transform );
224+
optional( obj, false, "msg", msg_transform );
225+
optional( obj, false, "variant_type", variant_type, "<any>" );
226+
optional( obj, false, "container", container );
227+
optional( obj, false, "sealed", sealed, true );
227228
if( obj.has_member( "target_charges" ) && obj.has_member( "rand_target_charges" ) ) {
228229
obj.throw_error_at( "target_charges",
229230
"Transform actor specified both fixed and random target charges" );
230231
}
231-
obj.read( "target_charges", ammo_qty );
232-
if( obj.has_array( "rand_target_charges" ) ) {
233-
for( const int charge : obj.get_array( "rand_target_charges" ) ) {
234-
random_ammo_qty.push_back( charge );
235-
}
236-
if( random_ammo_qty.size() < 2 ) {
237-
obj.throw_error_at( "rand_target_charges",
238-
"You must specify two or more values to choose between" );
239-
}
232+
optional( obj, false, "target_charges", ammo_qty, -1 );
233+
optional( obj, false, "rand_target_charges", random_ammo_qty );
234+
if( random_ammo_qty.size() == 1 ) {
235+
obj.throw_error_at( "rand_target_charges",
236+
"You must specify two or more values to choose between" );
240237
}
241-
obj.read( "target_ammo", ammo_type );
238+
optional( obj, false, "target_ammo", ammo_type );
242239

243-
obj.read( "target_timer", target_timer );
240+
optional( obj, false, "target_timer", target_timer, 0_seconds );
244241

245242
if( !ammo_type.is_empty() && !container.is_empty() ) {
246243
obj.throw_error_at( "target_ammo", "Transform actor specified both ammo type and container type" );
247244
}
248245

249-
obj.read( "active", active );
246+
optional( obj, false, "active", active, false );
250247

251-
obj.read( "moves", moves );
252-
if( moves < 0 ) {
253-
obj.throw_error_at( "moves", "transform actor specified negative moves" );
254-
}
248+
optional( obj, false, "moves", moves, numeric_bound_reader<int> { 0 }, 0 );
255249

256-
obj.read( "need_fire", need_fire );
257-
need_fire = std::max( need_fire, 0 );
258-
if( !obj.read( "need_charges_msg", need_charges_msg ) ) {
259-
need_charges_msg = to_translation( "The %s is empty!" );
260-
}
250+
optional( obj, false, "need_fire", need_fire, numeric_bound_reader<int> { 0 }, 0 );
251+
optional( obj, false, "need_charges_msg", need_charges_msg, to_translation( "The %s is empty!" ) );
261252

262-
obj.read( "need_charges", need_charges );
263-
need_charges = std::max( need_charges, 0 );
264-
if( !obj.read( "need_fire_msg", need_fire_msg ) ) {
265-
need_fire_msg = to_translation( "You need a source of fire!" );
266-
}
253+
optional( obj, false, "need_charges", need_charges, numeric_bound_reader<int> { 0 }, 0 );
254+
optional( obj, false, "need_fire_msg", need_fire_msg,
255+
to_translation( "You need a source of fire!" ) );
267256

268-
obj.read( "need_worn", need_worn );
269-
obj.read( "need_wielding", need_wielding );
257+
optional( obj, false, "need_worn", need_worn, false );
258+
optional( obj, false, "need_wielding", need_wielding, false );
270259

271-
obj.read( "need_empty", need_empty );
260+
optional( obj, false, "need_empty", need_empty, false );
272261

273-
obj.read( "qualities_needed", qualities_needed );
262+
optional( obj, false, "qualities_needed", qualities_needed );
274263

275-
obj.read( "menu_text", menu_text );
264+
optional( obj, false, "menu_text", menu_text );
276265
}
277266

278267
std::optional<int> iuse_transform::use( Character *p, item &it, const tripoint_bub_ms &pos ) const
@@ -572,9 +561,9 @@ std::unique_ptr<iuse_actor> unpack_actor::clone() const
572561

573562
void unpack_actor::load( const JsonObject &obj, const std::string & )
574563
{
575-
obj.read( "group", unpack_group );
576-
obj.read( "items_fit", items_fit );
577-
assign( obj, "filthy_volume_threshold", filthy_vol_threshold );
564+
optional( obj, false, "group", unpack_group );
565+
optional( obj, false, "items_fit", items_fit, false );
566+
optional( obj, false, "filthy_volume_threshold", filthy_vol_threshold, 0_ml );
578567
}
579568

580569
std::optional<int> unpack_actor::use( Character *p, item &it, const tripoint_bub_ms &pos ) const
@@ -629,8 +618,8 @@ std::unique_ptr<iuse_actor> message_iuse::clone() const
629618

630619
void message_iuse::load( const JsonObject &obj, const std::string & )
631620
{
632-
obj.read( "name", name );
633-
obj.read( "message", message );
621+
optional( obj, false, "name", name );
622+
optional( obj, false, "message", message );
634623
}
635624

636625
std::optional<int> message_iuse::use( Character *p, item &it,
@@ -669,11 +658,11 @@ std::unique_ptr<iuse_actor> sound_iuse::clone() const
669658

670659
void sound_iuse::load( const JsonObject &obj, const std::string & )
671660
{
672-
obj.read( "name", name );
673-
obj.read( "sound_message", sound_message );
674-
obj.read( "sound_volume", sound_volume );
675-
obj.read( "sound_id", sound_id );
676-
obj.read( "sound_variant", sound_variant );
661+
optional( obj, false, "name", name );
662+
optional( obj, false, "sound_message", sound_message );
663+
optional( obj, false, "sound_volume", sound_volume, 0 );
664+
optional( obj, false, "sound_id", sound_id, "misc" );
665+
optional( obj, false, "sound_variant", sound_variant, "default" );
677666
}
678667

679668
std::optional<int> sound_iuse::use( Character *, item &,
@@ -738,23 +727,20 @@ static std::vector<tripoint_bub_ms> points_for_gas_cloud( map *here, const tripo
738727
void explosion_iuse::load( const JsonObject &obj, const std::string & )
739728
{
740729
optional( obj, false, "explosion", explosion );
741-
obj.read( "draw_explosion_radius", draw_explosion_radius );
742-
if( obj.has_member( "draw_explosion_color" ) ) {
743-
draw_explosion_color = color_from_string( obj.get_string( "draw_explosion_color" ) );
744-
}
745-
obj.read( "do_flashbang", do_flashbang );
746-
obj.read( "flashbang_player_immune", flashbang_player_immune );
747-
obj.read( "fields_radius", fields_radius );
730+
optional( obj, false, "draw_explosion_radius", draw_explosion_radius, -1 );
731+
optional( obj, false, "draw_explosion_color", draw_explosion_color, nc_color_reader{}, c_white );
732+
optional( obj, false, "do_flashbang", do_flashbang, false );
733+
optional( obj, false, "flashbang_player_immune", flashbang_player_immune, false );
734+
optional( obj, false, "fields_radius", fields_radius, -1 );
748735
if( obj.has_member( "fields_type" ) || fields_radius > 0 ) {
749-
fields_type = field_type_id( obj.get_string( "fields_type" ) );
750-
}
751-
obj.read( "fields_min_intensity", fields_min_intensity );
752-
obj.read( "fields_max_intensity", fields_max_intensity );
753-
if( fields_max_intensity == 0 ) {
754-
fields_max_intensity = fields_type.obj().get_max_intensity();
755-
}
756-
obj.read( "emp_blast_radius", emp_blast_radius );
757-
obj.read( "scrambler_blast_radius", scrambler_blast_radius );
736+
mandatory( obj, false, "fields_type", fields_type );
737+
}
738+
optional( obj, false, "fields_min_intensity", fields_min_intensity, 1 );
739+
// this should happen in finalize, fields_type is not necessarily loaded
740+
optional( obj, false, "fields_max_intensity", fields_max_intensity,
741+
fields_type->get_max_intensity() );
742+
optional( obj, false, "emp_blast_radius", emp_blast_radius, -1 );
743+
optional( obj, false, "scrambler_blast_radius", scrambler_blast_radius, -1 );
758744
}
759745

760746
std::optional<int> explosion_iuse::use( Character *p, item &it, const tripoint_bub_ms &pos ) const

src/iuse_actor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ class sound_iuse : public iuse_actor
179179
translation sound_message;
180180

181181
int sound_volume = 0;
182-
std::string sound_id = "misc";
183-
std::string sound_variant = "default";
182+
std::string sound_id;
183+
std::string sound_variant;
184184

185185
~sound_iuse() override = default;
186186
void load( const JsonObject &obj, const std::string & ) override;

0 commit comments

Comments
 (0)