Skip to content

Commit

Permalink
Migrate ACT_WAIT_STAMINA to the activity_actor system.
Browse files Browse the repository at this point in the history
The dependence on moves was removed as I couldn't see how it was anything
but a placeholder value - wait_stamina_do_turn completely ignored the
moves left. The actor also explicitly states that it can wait until
a given threshold, whereas this functionality was implicit before.
  • Loading branch information
CLIDragon committed Sep 6, 2024
1 parent 1a13be3 commit f60fcb5
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 44 deletions.
49 changes: 49 additions & 0 deletions src/activity_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ static const activity_id ACT_UNLOAD( "ACT_UNLOAD" );
static const activity_id ACT_UNLOAD_LOOT( "ACT_UNLOAD_LOOT" );
static const activity_id ACT_VEHICLE_FOLD( "ACT_VEHICLE_FOLD" );
static const activity_id ACT_VEHICLE_UNFOLD( "ACT_VEHICLE_UNFOLD" );
static const activity_id ACT_WAIT_STAMINA( "ACT_WAIT_STAMINA" );
static const activity_id ACT_WASH( "ACT_WASH" );
static const activity_id ACT_WEAR( "ACT_WEAR" );
static const activity_id ACT_WIELD( "ACT_WIELD" );
Expand Down Expand Up @@ -8122,6 +8123,53 @@ std::unique_ptr<activity_actor> pulp_activity_actor::deserialize( JsonValue &jsi
return actor.clone();
}


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

void wait_stamina_activity_actor::do_turn( player_activity &act, Character &you )
{
if( stamina_threshold != -1 ) {
// Record intial stamina only when waiting until a given threshold.
initial_stamina = you.get_stamina();
}

int stamina_max = stamina_threshold == -1 ? you.get_stamina_max() : stamina_threshold;
if( you.get_stamina() >= stamina_max ) {
finish( act, you );
}
}

void wait_stamina_activity_actor::finish( player_activity &act, Character &you )
{
if( stamina_threshold != -1 ) {
const int stamina_initial = initial_stamina != -1 ? stamina_initial : you.get_stamina();

Check failure on line 8149 in src/activity_actor.cpp

View workflow job for this annotation

GitHub Actions / Basic Build and Test (Clang 10, Ubuntu, Curses)

variable 'stamina_initial' is uninitialized when used within its own initialization [-Werror,-Wuninitialized]

Check failure on line 8149 in src/activity_actor.cpp

View workflow job for this annotation

GitHub Actions / build (src)

variable 'stamina_initial' is uninitialized when used within its own initialization [clang-diagnostic-uninitialized,-warnings-as-errors]
if( you.get_stamina() < stamina_threshold && you.get_stamina() <= stamina_initial ) {
debugmsg( "Failed to wait until stamina threshold %d reached, only at %d. You may not be regaining stamina.",
act.values.front(), you.get_stamina() );
}
} else if( you.get_stamina() < you.get_stamina_max() ) {
you.add_msg_if_player( _( "You are bored of waiting, so you stop." ) );
} else {
you.add_msg_if_player( _( "You finish waiting and feel refreshed." ) );
}
act.set_to_null();
}

void wait_stamina_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.write_null();
}

std::unique_ptr<activity_actor> wait_stamina_activity_actor::deserialize( JsonValue &jsin )

Check failure on line 8167 in src/activity_actor.cpp

View workflow job for this annotation

GitHub Actions / Basic Build and Test (Clang 10, Ubuntu, Curses)

unused parameter 'jsin' [-Werror,-Wunused-parameter]

Check failure on line 8167 in src/activity_actor.cpp

View workflow job for this annotation

GitHub Actions / build (src)

unused parameter 'jsin' [clang-diagnostic-unused-parameter,-warnings-as-errors]

Check failure on line 8167 in src/activity_actor.cpp

View workflow job for this annotation

GitHub Actions / build (src)

parameter 'jsin' is unused [misc-unused-parameters,-warnings-as-errors]
{
return wait_stamina_activity_actor().clone();
}


namespace activity_actors
{

Expand Down Expand Up @@ -8187,6 +8235,7 @@ deserialize_functions = {
{ ACT_UNLOAD, &unload_activity_actor::deserialize },
{ ACT_UNLOAD_LOOT, &unload_loot_activity_actor::deserialize },
{ ACT_VEHICLE_FOLD, &vehicle_folding_activity_actor::deserialize },
{ ACT_WAIT_STAMINA, &wait_stamina_activity_actor::deserialize },
{ ACT_VEHICLE_UNFOLD, &vehicle_unfolding_activity_actor::deserialize },
{ ACT_WASH, &wash_activity_actor::deserialize },
{ ACT_WEAR, &wear_activity_actor::deserialize },
Expand Down
30 changes: 30 additions & 0 deletions src/activity_actor_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2220,4 +2220,34 @@ class pulp_activity_actor : public activity_actor
bool pulp_acid;
};

class wait_stamina_activity_actor : public activity_actor
{

public:
// Wait until stamina is at the maximum.
wait_stamina_activity_actor() = default;

// If we're given a threshold, wait until stamina is at least this value.
wait_stamina_activity_actor( int stamina_threshold ) : stamina_threshold( stamina_threshold ) {};

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (src)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (src)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (src)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (src)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (src)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (src)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (src)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

Check failure on line 2231 in src/activity_actor_definitions.h

View workflow job for this annotation

GitHub Actions / build (other)

single-argument constructors must be marked explicit to avoid unintentional implicit conversions [google-explicit-constructor,-warnings-as-errors]

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

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

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

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

private:
int stamina_threshold = -1;
int initial_stamina = -1;
};

#endif // CATA_SRC_ACTIVITY_ACTOR_DEFINITIONS_H
35 changes: 0 additions & 35 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ static const activity_id ACT_VIBE( "ACT_VIBE" );
static const activity_id ACT_VIEW_RECIPE( "ACT_VIEW_RECIPE" );
static const activity_id ACT_WAIT( "ACT_WAIT" );
static const activity_id ACT_WAIT_NPC( "ACT_WAIT_NPC" );
static const activity_id ACT_WAIT_STAMINA( "ACT_WAIT_STAMINA" );
static const activity_id ACT_WAIT_WEATHER( "ACT_WAIT_WEATHER" );

static const ammotype ammo_battery( "battery" );
Expand Down Expand Up @@ -297,7 +296,6 @@ activity_handlers::do_turn_functions = {
{ ACT_ROBOT_CONTROL, robot_control_do_turn },
{ ACT_TREE_COMMUNION, tree_communion_do_turn },
{ ACT_STUDY_SPELL, study_spell_do_turn },
{ ACT_WAIT_STAMINA, wait_stamina_do_turn },
{ ACT_MULTIPLE_CRAFT, multiple_craft_do_turn },
{ ACT_MULTIPLE_DIS, multiple_dis_do_turn },
{ ACT_MULTIPLE_READ, multiple_read_do_turn },
Expand Down Expand Up @@ -337,7 +335,6 @@ activity_handlers::finish_functions = {
{ ACT_WAIT, wait_finish },
{ ACT_WAIT_WEATHER, wait_weather_finish },
{ ACT_WAIT_NPC, wait_npc_finish },
{ ACT_WAIT_STAMINA, wait_stamina_finish },
{ ACT_SOCIALIZE, socialize_finish },
{ ACT_OPERATION, operation_finish },
{ ACT_VIBE, vibe_finish },
Expand Down Expand Up @@ -3117,38 +3114,6 @@ void activity_handlers::wait_npc_finish( player_activity *act, Character *you )
act->set_to_null();
}

void activity_handlers::wait_stamina_do_turn( player_activity *act, Character *you )
{
int stamina_threshold = you->get_stamina_max();
if( !act->values.empty() ) {
stamina_threshold = act->values[0];
// remember initial stamina, only for waiting-with-threshold
if( act->values.size() == 1 ) {
act->values.push_back( you->get_stamina() );
}
}
if( you->get_stamina() >= stamina_threshold ) {
wait_stamina_finish( act, you );
}
}

void activity_handlers::wait_stamina_finish( player_activity *act, Character *you )
{
if( !act->values.empty() ) {
const int stamina_threshold = act->values[0];
const int stamina_initial = ( act->values.size() > 1 ) ? act->values[1] : you->get_stamina();
if( you->get_stamina() < stamina_threshold && you->get_stamina() <= stamina_initial ) {
debugmsg( "Failed to wait until stamina threshold %d reached, only at %d. You may not be regaining stamina.",
act->values.front(), you->get_stamina() );
}
} else if( you->get_stamina() < you->get_stamina_max() ) {
you->add_msg_if_player( _( "You are bored of waiting, so you stop." ) );
} else {
you->add_msg_if_player( _( "You finish waiting and feel refreshed." ) );
}
act->set_to_null();
}

void activity_handlers::socialize_finish( player_activity *act, Character *you )
{
you->add_msg_if_player( _( "%s finishes chatting with you." ), act->str_values[0] );
Expand Down
2 changes: 0 additions & 2 deletions src/activity_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ void vehicle_deconstruction_do_turn( player_activity *act, Character *you );
void vehicle_repair_do_turn( player_activity *act, Character *you );
void vibe_do_turn( player_activity *act, Character *you );
void view_recipe_do_turn( player_activity *act, Character *you );
void wait_stamina_do_turn( player_activity *act, Character *you );

// defined in activity_handlers.cpp
extern const std::map< activity_id, std::function<void( player_activity *, Character * )> >
Expand Down Expand Up @@ -277,7 +276,6 @@ void vibe_finish( player_activity *act, Character *you );
void view_recipe_finish( player_activity *act, Character *you );
void wait_finish( player_activity *act, Character *you );
void wait_npc_finish( player_activity *act, Character *you );
void wait_stamina_finish( player_activity *act, Character *you );
void wait_weather_finish( player_activity *act, Character *you );

int move_cost( const item &it, const tripoint_bub_ms &src, const tripoint_bub_ms &dest );
Expand Down
9 changes: 6 additions & 3 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1234,9 +1234,12 @@ static void wait()
actType = ACT_WAIT;
}

player_activity new_act( actType, 100 * to_turns<int>( time_to_wait ), 0 );

player_character.assign_activity( new_act );
if( actType == ACT_WAIT_STAMINA ) {
player_character.assign_activity( wait_stamina_activity_actor() );
} else {
player_activity new_act( actType, 100 * to_turns<int>( time_to_wait ), 0 );
player_character.assign_activity( new_act );
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/player_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <memory>
#include <new>

#include "activity_actor_definitions.h"
#include "activity_handlers.h"
#include "activity_type.h"
#include "avatar.h"
Expand Down Expand Up @@ -340,8 +341,6 @@ void player_activity::do_turn( Character &you )
}

auto_resume = true;
player_activity new_act( ACT_WAIT_STAMINA, to_moves<int>( 5_minutes ) );
new_act.values.push_back( you.get_stamina_max() );
if( you.is_avatar() && !ignoreQuery ) {
uilist tired_query;
tired_query.text = _( "You struggle to continue. Keep trying?" );
Expand All @@ -364,7 +363,7 @@ void player_activity::do_turn( Character &you )
}
}
if( !ignoreQuery && auto_resume ) {
you.assign_activity( new_act );
you.assign_activity( wait_stamina_activity_actor( you.get_stamina_max() ) );
}
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/player_activity.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class player_activity
void deserialize_legacy_type( int legacy_type, activity_id &dest );

/**
* Preform necessary initialization to start or resume the activity. Must be
* Perform necessary initialization to start or resume the activity. Must be
* called whenever a Character starts a new activity.
* When resuming an activity, do not call activity_actor::start
*/
Expand Down

0 comments on commit f60fcb5

Please sign in to comment.