Skip to content

Commit

Permalink
Migrate ACT_HOTWIRE_CAR to the activity actor system
Browse files Browse the repository at this point in the history
  • Loading branch information
olanti-p authored and kevingranade committed Jun 1, 2020
1 parent 816c20f commit 126e035
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 66 deletions.
75 changes: 75 additions & 0 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "ranged.h"
#include "timed_event.h"
#include "uistate.h"
#include "vehicle.h"
#include "vpart_position.h"

static const bionic_id bio_fingerhack( "bio_fingerhack" );

Expand Down Expand Up @@ -671,6 +673,78 @@ std::unique_ptr<activity_actor> hacking_activity_actor::deserialize( JsonIn & )
return hacking_activity_actor().clone();
}

void hotwire_car_activity_actor::start( player_activity &act, Character & )
{
act.moves_total = moves_total;
act.moves_left = moves_total;
}

void hotwire_car_activity_actor::do_turn( player_activity &act, Character & )
{
if( calendar::once_every( 1_minutes ) ) {
bool lost = !g->m.veh_at( g->m.getlocal( target ) ).has_value();
if( lost ) {
act.set_to_null();
debugmsg( "Lost ACT_HOTWIRE_CAR target vehicle" );
}
}
}

void hotwire_car_activity_actor::finish( player_activity &act, Character &who )
{
act.set_to_null();

const optional_vpart_position vp = g->m.veh_at( g->m.getlocal( target ) );
if( !vp ) {
debugmsg( "Lost ACT_HOTWIRE_CAR target vehicle" );
return;
}
vehicle &veh = vp->vehicle();

int skill = who.get_skill_level( skill_mechanics );
if( skill > rng( 1, 6 ) ) {
// Success
who.add_msg_if_player( _( "You found the wire that starts the engine." ) );
veh.is_locked = false;
} else if( skill > rng( 0, 4 ) ) {
// Soft fail
who.add_msg_if_player( _( "You found a wire that looks like the right one." ) );
veh.is_alarm_on = veh.has_security_working();
veh.is_locked = false;
} else if( !veh.is_alarm_on ) {
// Hard fail
who.add_msg_if_player( _( "The red wire always starts the engine, doesn't it?" ) );
veh.is_alarm_on = veh.has_security_working();
} else {
// Already failed
who.add_msg_if_player(
_( "By process of elimination, you found the wire that starts the engine." ) );
veh.is_locked = false;
}
}

void hotwire_car_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();

jsout.member( "target", target );
jsout.member( "moves_total", moves_total );

jsout.end_object();
}

std::unique_ptr<activity_actor> hotwire_car_activity_actor::deserialize( JsonIn &jsin )
{
hotwire_car_activity_actor actor( 0, tripoint_zero );

JsonObject data = jsin.get_object();

data.read( "target", actor.target );
data.read( "moves_total", actor.moves_total );

return actor.clone();
}

void move_items_activity_actor::do_turn( player_activity &act, Character &who )
{
const tripoint dest = relative_destination + who.pos();
Expand Down Expand Up @@ -1226,6 +1300,7 @@ deserialize_functions = {
{ activity_id( "ACT_DIG" ), &dig_activity_actor::deserialize },
{ activity_id( "ACT_DIG_CHANNEL" ), &dig_channel_activity_actor::deserialize },
{ activity_id( "ACT_HACKING" ), &hacking_activity_actor::deserialize },
{ activity_id( "ACT_HOTWIRE_CAR" ), &hotwire_car_activity_actor::deserialize },
{ activity_id( "ACT_LOCKPICK" ), &lockpick_activity_actor::deserialize },
{ activity_id( "ACT_MIGRATION_CANCEL" ), &migration_cancel_activity_actor::deserialize },
{ activity_id( "ACT_MOVE_ITEMS" ), &move_items_activity_actor::deserialize },
Expand Down
36 changes: 36 additions & 0 deletions src/activity_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,42 @@ class hacking_activity_actor : public activity_actor
static std::unique_ptr<activity_actor> deserialize( JsonIn &jsin );
};

class hotwire_car_activity_actor : public activity_actor
{
private:
int moves_total;

/**
* Position of first vehicle part; used to identify the vehicle
* TODO: find something more reliable (to cover cases when vehicle is moved/damaged)
*/
tripoint target;

bool can_resume_with_internal( const activity_actor &other, const Character & ) const override {
const auto &a = static_cast<const hotwire_car_activity_actor &>( other );
return target == a.target && moves_total == a.moves_total;
}

public:
hotwire_car_activity_actor( int moves_total, const tripoint &target ): moves_total( moves_total ),
target( target ) {}

activity_id get_type() const override {
return activity_id( "ACT_HOTWIRE_CAR" );
}

void start( player_activity &act, Character & ) override;
void do_turn( player_activity &, Character & ) override;
void finish( player_activity &act, Character &who ) override;

std::unique_ptr<activity_actor> clone() const override {
return std::make_unique<hotwire_car_activity_actor>( *this );
}

void serialize( JsonOut &jsout ) const override;
static std::unique_ptr<activity_actor> deserialize( JsonIn &jsin );
};

class move_items_activity_actor : public activity_actor
{
private:
Expand Down
34 changes: 0 additions & 34 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ static const activity_id ACT_HACKSAW( "ACT_HACKSAW" );
static const activity_id ACT_HAIRCUT( "ACT_HAIRCUT" );
static const activity_id ACT_HAND_CRANK( "ACT_HAND_CRANK" );
static const activity_id ACT_HEATING( "ACT_HEATING" );
static const activity_id ACT_HOTWIRE_CAR( "ACT_HOTWIRE_CAR" );
static const activity_id ACT_JACKHAMMER( "ACT_JACKHAMMER" );
static const activity_id ACT_LONGSALVAGE( "ACT_LONGSALVAGE" );
static const activity_id ACT_MEDITATE( "ACT_MEDITATE" );
Expand Down Expand Up @@ -369,7 +368,6 @@ activity_handlers::finish_functions = {
{ ACT_FIRSTAID, firstaid_finish },
{ ACT_FISH, fish_finish },
{ ACT_FORAGE, forage_finish },
{ ACT_HOTWIRE_CAR, hotwire_finish },
{ ACT_LONGSALVAGE, longsalvage_finish },
{ ACT_PICKAXE, pickaxe_finish },
{ ACT_RELOAD, reload_finish },
Expand Down Expand Up @@ -1821,38 +1819,6 @@ void activity_handlers::game_do_turn( player_activity *act, player *p )
}
}

void activity_handlers::hotwire_finish( player_activity *act, player *p )
{
//Grab this now, in case the vehicle gets shifted
if( const optional_vpart_position vp = g->m.veh_at( g->m.getlocal( tripoint( act->values[0],
act->values[1],
p->posz() ) ) ) ) {
vehicle *const veh = &vp->vehicle();
const int mech_skill = act->values[2];
if( mech_skill > static_cast<int>( rng( 1, 6 ) ) ) {
//success
veh->is_locked = false;
add_msg( _( "This wire will start the engine." ) );
} else if( mech_skill > static_cast<int>( rng( 0, 4 ) ) ) {
//soft fail
veh->is_locked = false;
veh->is_alarm_on = veh->has_security_working();
add_msg( _( "This wire will probably start the engine." ) );
} else if( veh->is_alarm_on ) {
veh->is_locked = false;
add_msg( _( "By process of elimination, this wire will start the engine." ) );
} else {
//hard fail
veh->is_alarm_on = veh->has_security_working();
add_msg( _( "The red wire always starts the engine, doesn't it?" ) );
}
} else {
dbg( D_ERROR ) << "game:process_activity: ACT_HOTWIRE_CAR: vehicle not found";
debugmsg( "process_activity ACT_HOTWIRE_CAR: vehicle not found" );
}
act->set_to_null();
}

void activity_handlers::longsalvage_finish( player_activity *act, player *p )
{
static const std::string salvage_string = "salvage";
Expand Down
1 change: 0 additions & 1 deletion src/activity_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ void butcher_finish( player_activity *act, player *p );
void firstaid_finish( player_activity *act, player *p );
void fish_finish( player_activity *act, player *p );
void forage_finish( player_activity *act, player *p );
void hotwire_finish( player_activity *act, player *p );
void longsalvage_finish( player_activity *act, player *p );
void pulp_finish( player_activity *act, player *p );
void pickaxe_finish( player_activity *act, player *p );
Expand Down
50 changes: 19 additions & 31 deletions src/vehicle_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
#include "vpart_range.h"
#include "weather.h"

static const activity_id ACT_HOTWIRE_CAR( "ACT_HOTWIRE_CAR" );
static const activity_id ACT_RELOAD( "ACT_RELOAD" );
static const activity_id ACT_REPAIR_ITEM( "ACT_REPAIR_ITEM" );
static const activity_id ACT_START_ENGINES( "ACT_START_ENGINES" );
Expand Down Expand Up @@ -434,40 +433,29 @@ int vehicle::select_engine()

bool vehicle::interact_vehicle_locked()
{
if( is_locked ) {
const inventory &crafting_inv = g->u.crafting_inventory();
add_msg( _( "You don't find any keys in the %s." ), name );
if( crafting_inv.has_quality( quality_id( "SCREW" ) ) ) {
if( query_yn( _( "You don't find any keys in the %s. Attempt to hotwire vehicle?" ),
name ) ) {
///\EFFECT_MECHANICS speeds up vehicle hotwiring
int mechanics_skill = g->u.get_skill_level( skill_mechanics );
const int hotwire_time = 6000 / ( ( mechanics_skill > 0 ) ? mechanics_skill : 1 );
const int moves = to_moves<int>( time_duration::from_turns( hotwire_time ) );
//assign long activity
g->u.assign_activity( ACT_HOTWIRE_CAR, moves, -1, INT_MIN, _( "Hotwire" ) );
// use part 0 as the reference point
point q = coord_translate( parts[0].mount );
const tripoint abs_veh_pos = g->m.getabs( global_pos3() );
//[0]
g->u.activity.values.push_back( abs_veh_pos.x + q.x );
//[1]
g->u.activity.values.push_back( abs_veh_pos.y + q.y );
//[2]
g->u.activity.values.push_back( g->u.get_skill_level( skill_mechanics ) );
} else {
if( has_security_working() && query_yn( _( "Trigger the %s's Alarm?" ), name ) ) {
is_alarm_on = true;
} else {
add_msg( _( "You leave the controls alone." ) );
}
}
if( !is_locked ) {
return true;
}

add_msg( _( "You don't find any keys in the %s." ), name );
const inventory &inv = g->u.crafting_inventory();
if( inv.has_quality( quality_id( "SCREW" ) ) ) {
if( query_yn( _( "You don't find any keys in the %s. Attempt to hotwire vehicle?" ), name ) ) {
///\EFFECT_MECHANICS speeds up vehicle hotwiring
int skill = g->u.get_skill_level( skill_mechanics );
const int moves = to_moves<int>( 6000_seconds / ( ( skill > 0 ) ? skill : 1 ) );
tripoint target = g->m.getabs( global_pos3() ) + coord_translate( parts[0].mount );
g->u.assign_activity( hotwire_car_activity_actor( moves, target ) );
} else if( has_security_working() && query_yn( _( "Trigger the %s's Alarm?" ), name ) ) {
is_alarm_on = true;
} else {
add_msg( _( "You could use a screwdriver to hotwire it." ) );
add_msg( _( "You leave the controls alone." ) );
}
} else {
add_msg( _( "You could use a screwdriver to hotwire it." ) );
}

return !( is_locked );
return false;
}

void vehicle::smash_security_system()
Expand Down

0 comments on commit 126e035

Please sign in to comment.