From 5d15565b93e8d0e3786583f46da7e7984eb1e71a Mon Sep 17 00:00:00 2001 From: irwiss Date: Sun, 25 Jun 2023 17:32:46 +0300 Subject: [PATCH] Make vpart_info store time_durations --- data/json/vehicleparts/vehicle_parts.json | 2 +- src/veh_type.cpp | 37 +++++++++++++---------- src/veh_type.h | 13 ++++---- src/veh_utils.cpp | 2 +- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/data/json/vehicleparts/vehicle_parts.json b/data/json/vehicleparts/vehicle_parts.json index 7daf83c7ecf64..1d0993ca21983 100644 --- a/data/json/vehicleparts/vehicle_parts.json +++ b/data/json/vehicleparts/vehicle_parts.json @@ -2450,7 +2450,7 @@ "durability": 120, "description": "A heavy-duty tow cable, if the other end was attached to another vehicle, it could pull it.", "item": "hd_tow_cable", - "requirements": { "removal": { "time": 500 } }, + "requirements": { "removal": { "time": "5 s" } }, "flags": [ "NO_INSTALL_HIDDEN", "UNMOUNT_ON_DAMAGE", "TOW_CABLE" ], "breaks_into": [ { "item": "scrap", "count": [ 4, 8 ] }, { "item": "grip_hook", "count": 1 }, { "item": "cable", "count": [ 1, 4 ] } ], "variants": [ { "symbols": "{", "symbols_broken": "*" } ] diff --git a/src/veh_type.cpp b/src/veh_type.cpp index be4140120211b..338a111328d2d 100644 --- a/src/veh_type.cpp +++ b/src/veh_type.cpp @@ -152,7 +152,7 @@ const vpart_info &string_id::obj() const static void parse_vp_reqs( const JsonObject &obj, const vpart_id &id, const std::string &key, std::vector> &reqs, - std::map &skills, int &moves ) + std::map &skills, time_duration &time ) { if( !obj.has_object( key ) ) { @@ -164,15 +164,19 @@ static void parse_vp_reqs( const JsonObject &obj, const vpart_id &id, const std: if( !sk.empty() ) { skills.clear(); for( JsonArray cur : sk ) { - skills.emplace( skill_id( cur.get_string( 0 ) ), cur.size() >= 2 ? cur.get_int( 1 ) : 1 ); + if( cur.size() != 2 ) { + debugmsg( "vpart '%s' has requirement with invalid skill entry", id.str() ); + continue; + } + skills.emplace( skill_id( cur.get_string( 0 ) ), cur.get_int( 1 ) ); } } - if( src.has_int( "time" ) ) { - moves = src.get_int( "time" ); - } else if( src.has_string( "time" ) ) { - moves = to_moves( read_from_json_string( src.get_member( "time" ), - time_duration::units ) ); + if( src.has_string( "time" ) ) { + assign( src, "time", time, /* strict = */ false ); + } else if( src.has_int( "time" ) ) { // remove in 0.H + time = time_duration::from_moves( src.get_int( "time" ) ); + debugmsg( "vpart '%s' defines requirement time as integer, use time units string", id.str() ); } if( src.has_string( "using" ) ) { @@ -558,8 +562,8 @@ void vehicles::parts::finalize() time_duration install_time = 10_minutes * difficulty; time_duration removal_time = 10_minutes * difficulty / 2; - new_part.install_moves = to_moves( install_time ); - new_part.removal_moves = to_moves( removal_time ); + new_part.install_moves = install_time; + new_part.removal_moves = removal_time; new_part.looks_like = get_looks_like( new_part, *item ); @@ -699,7 +703,7 @@ void vpart_info::finalize() requirement_data::save_requirement( ins, ins_id ); install_reqs.emplace_back( ins_id, 1 ); - if( removal_moves < 0 ) { + if( removal_moves < 0_seconds ) { removal_moves = install_moves / 2; } @@ -781,11 +785,11 @@ void vpart_info::check() const } } - if( install_moves < 0 ) { + if( install_moves < 0_seconds ) { debugmsg( "vehicle part %s has negative installation time", id.str() ); } - if( removal_moves < 0 ) { + if( removal_moves < 0_seconds ) { debugmsg( "vehicle part %s has negative removal time", id.str() ); } @@ -1030,7 +1034,8 @@ bool vpart_info::is_repairable() const return !has_flag( "NO_REPAIR" ) && !repair_requirements().is_empty(); } -static int scale_time( const std::map &sk, int mv, const Character &you ) +static time_duration scale_time( const std::map &sk, time_duration mv, + const Character &you ) { if( sk.empty() ) { return mv; @@ -1051,17 +1056,17 @@ static int scale_time( const std::map &sk, int mv, const Characte int vpart_info::install_time( const Character &you ) const { - return scale_time( install_skills, install_moves, you ); + return to_moves( scale_time( install_skills, install_moves, you ) ); } int vpart_info::removal_time( const Character &you ) const { - return scale_time( removal_skills, removal_moves, you ); + return to_moves( scale_time( removal_skills, removal_moves, you ) ); } int vpart_info::repair_time( const Character &you ) const { - return scale_time( repair_skills, repair_moves, you ); + return to_moves( scale_time( repair_skills, repair_moves, you ) ); } /** diff --git a/src/veh_type.h b/src/veh_type.h index 867bf222197e8..abe470ab4123c 100644 --- a/src/veh_type.h +++ b/src/veh_type.h @@ -436,13 +436,12 @@ class vpart_info */ units::power power = 0_W; - /** Installation time (in moves) for component (@see install_time), default 1 hour */ - int install_moves = to_moves( 1_hours ); - /** Repair time (in moves) to fully repair a component (@see repair_time) */ - int repair_moves = to_moves( 1_hours ); - /** Removal time (in moves) for component (@see removal_time), - * default is half @ref install_moves */ - int removal_moves = -1; + /** Installation for component (@see install_time) */ + time_duration install_moves = 1_hours; + /** Repair time to fully repair a component (@see repair_time) */ + time_duration repair_moves = 1_hours; + /** Removal time for component (@see removal_time), default is half \p install_moves */ + time_duration removal_moves = -1_seconds; // seatbelt (str, currently non-functional #30239) // muffler (% noise reduction) diff --git a/src/veh_utils.cpp b/src/veh_utils.cpp index b07b1b126041b..1e23a58d1a47f 100644 --- a/src/veh_utils.cpp +++ b/src/veh_utils.cpp @@ -49,7 +49,7 @@ int calc_xp_gain( const vpart_info &vp, const skill_id &sk, const Character &who // 5: 3 xp /h // 6: 2 xp /h // 7+: 1 xp /h - return std::ceil( static_cast( vp.install_moves ) / + return std::ceil( to_moves( vp.install_moves ) / to_moves( 1_minutes * std::pow( lvl, 2 ) ) ); }