Skip to content

Commit

Permalink
Make vpart_info store time_durations
Browse files Browse the repository at this point in the history
  • Loading branch information
irwiss committed Jun 28, 2023
1 parent fabc842 commit 4840a25
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion data/json/vehicleparts/vehicle_parts.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*" } ]
Expand Down
37 changes: 21 additions & 16 deletions src/veh_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const vpart_info &string_id<vpart_info>::obj() const

static void parse_vp_reqs( const JsonObject &obj, const vpart_id &id, const std::string &key,
std::vector<std::pair<requirement_id, int>> &reqs,
std::map<skill_id, int> &skills, int &moves )
std::map<skill_id, int> &skills, time_duration &time )
{

if( !obj.has_object( key ) ) {
Expand All @@ -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<int>( read_from_json_string<time_duration>( 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" ) ) {
Expand Down Expand Up @@ -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<int>( install_time );
new_part.removal_moves = to_moves<int>( removal_time );
new_part.install_moves = install_time;
new_part.removal_moves = removal_time;

new_part.looks_like = get_looks_like( new_part, *item );

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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() );
}

Expand Down Expand Up @@ -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<skill_id, int> &sk, int mv, const Character &you )
static time_duration scale_time( const std::map<skill_id, int> &sk, time_duration mv,
const Character &you )
{
if( sk.empty() ) {
return mv;
Expand All @@ -1051,17 +1056,17 @@ static int scale_time( const std::map<skill_id, int> &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<int>( 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<int>( 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<int>( scale_time( repair_skills, repair_moves, you ) );
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/veh_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>( 1_hours );
/** Repair time (in moves) to fully repair a component (@see repair_time) */
int repair_moves = to_moves<int>( 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)
Expand Down
2 changes: 1 addition & 1 deletion src/veh_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>( vp.install_moves ) /
return std::ceil( to_moves<double>( vp.install_moves ) /
to_moves<int>( 1_minutes * std::pow( lvl, 2 ) ) );
}

Expand Down

0 comments on commit 4840a25

Please sign in to comment.