Skip to content

Commit

Permalink
eoc: propagate const_dialogue changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei8l committed Nov 8, 2024
1 parent 54f6c1f commit c0ed85d
Show file tree
Hide file tree
Showing 43 changed files with 897 additions and 869 deletions.
8 changes: 4 additions & 4 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -1131,12 +1131,12 @@ class Character : public Creature, public visitable
bool handle_melee_wear( item_location shield, float wear_multiplier = 1.0f );
/** Returns a random technique/vector/contact area set from the possible techs */
std::tuple<matec_id, attack_vector_id, sub_bodypart_str_id> pick_technique(
Creature &t, const item_location &weap,
Creature const &t, const item_location &weap,
bool crit, bool dodge_counter, bool block_counter, const std::vector<matec_id> &blacklist = {} )
const;
// Filter techniques per tech, return a tech/vector/sublimb set
std::optional<std::tuple<matec_id, attack_vector_id, sub_bodypart_str_id>>
evaluate_technique( const matec_id &tec_id, Creature &t, const item_location &weap,
evaluate_technique( const matec_id &tec_id, Creature const &t, const item_location &weap,
bool crit = false, bool dodge_counter = false, bool block_counter = false ) const;
void perform_technique( const ma_technique &technique, Creature &t, damage_instance &di,
int &move_cost, item_location &cur_weapon );
Expand Down Expand Up @@ -1236,8 +1236,8 @@ class Character : public Creature, public visitable
bool can_autolearn( const matype_id &ma_id ) const;
private:
/** Check if an area-of-effect technique has valid targets */
bool valid_aoe_technique( Creature &t, const ma_technique &technique ) const;
bool valid_aoe_technique( Creature &t, const ma_technique &technique,
bool valid_aoe_technique( Creature const &t, const ma_technique &technique ) const;
bool valid_aoe_technique( Creature const &t, const ma_technique &technique,
std::vector<Creature *> &targets ) const;
public:

Expand Down
569 changes: 287 additions & 282 deletions src/condition.cpp

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions src/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ duration_or_var get_duration_or_var( const JsonObject &jo, const std::string_vie
duration_or_var_part get_duration_or_var_part( const JsonValue &jv, const std::string_view &member,
bool required = true,
time_duration default_val = 0_seconds );
tripoint_abs_ms get_tripoint_from_var( std::optional<var_info> var, dialogue const &d,
tripoint_abs_ms get_tripoint_from_var( std::optional<var_info> var, const_dialogue const &d,
bool is_npc );
var_info read_var_info( const JsonObject &jo );
translation_var_info read_translation_var_info( const JsonObject &jo );
Expand All @@ -73,7 +73,7 @@ std::string get_talk_var_basename( const JsonObject &jo, std::string_view member
bool check_value );
// the truly awful declaration for the conditional_t loading helper_function
void read_condition( const JsonObject &jo, const std::string &member_name,
std::function<bool( dialogue & )> &condition, bool default_val );
std::function<bool( const_dialogue const & )> &condition, bool default_val );

void finalize_conditions();

Expand All @@ -86,19 +86,20 @@ void finalize_conditions();
*/
struct conditional_t {
public:
using func = std::function<bool( dialogue & )>;
using func = std::function<bool( const_dialogue const & )>;

conditional_t() = default;
explicit conditional_t( std::string_view type );
explicit conditional_t( const JsonObject &jo );

static std::function<std::string( const dialogue & )> get_get_string( const JsonObject &jo );
static std::function<translation( const dialogue & )> get_get_translation( const JsonObject &jo );
static std::function<double( dialogue & )> get_get_dbl( std::string_view checked_value,
static std::function<std::string( const_dialogue const & )> get_get_string( const JsonObject &jo );
static std::function<translation( const_dialogue const & )> get_get_translation(
const JsonObject &jo );
static std::function<double( const_dialogue const & )> get_get_dbl( std::string_view checked_value,
char scope );
std::function<void( dialogue &, double )>
static get_set_dbl( std::string_view checked_value, char scope );
bool operator()( dialogue &d ) const {
bool operator()( const_dialogue const &d ) const {
if( !condition ) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3441,7 +3441,7 @@ std::unique_ptr<talker> get_talker_for( Creature &me )
}
}

std::unique_ptr<talker> get_talker_for( const Creature &me )
std::unique_ptr<const_talker> get_const_talker_for( const Creature &me )
{
if( !me.is_monster() ) {
return std::make_unique<talker_character_const>( me.as_character() );
Expand Down
3 changes: 2 additions & 1 deletion src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class monster;
class nc_color;
class npc;
class talker;
class const_talker;
class translation;
namespace catacurses
{
Expand Down Expand Up @@ -1346,6 +1347,6 @@ class Creature : public viewer
void print_proj_avoid_msg( Creature *source, viewer &player_view ) const;
};
std::unique_ptr<talker> get_talker_for( Creature &me );
std::unique_ptr<talker> get_talker_for( const Creature &me );
std::unique_ptr<const_talker> get_const_talker_for( const Creature &me );
std::unique_ptr<talker> get_talker_for( Creature *me );
#endif // CATA_SRC_CREATURE_H
12 changes: 6 additions & 6 deletions src/dialogue.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ using trial_mod = std::pair<std::string, int>;
struct talk_trial {
talk_trial_type type = TALK_TRIAL_NONE;
int difficulty = 0;
std::function<bool( dialogue & )> condition;
std::function<bool( const_dialogue const & )> condition;

// If this talk_trial is skill check, this is the string ID of the skill that we check the level of.
std::string skill_required;

int calc_chance( dialogue &d ) const;
int calc_chance( const_dialogue const &d ) const;
/**
* Returns a user-friendly representation of @ref type
*/
Expand Down Expand Up @@ -176,22 +176,22 @@ struct talk_response {
*/
translation truetext;
translation falsetext;
std::function<bool( dialogue & )> truefalse_condition;
std::function<bool( const_dialogue const & )> truefalse_condition;

talk_trial trial;
/**
* The following values are forwarded to the chatbin of the NPC (see @ref npc_chatbin).
*/

//copy of json_talk_response::condition, optional
std::function<bool( dialogue & )> condition;
std::function<bool( const_dialogue const & )> condition;

//whether to display this response in normal gameplay even if condition is false
bool show_always = false;
//appended to response if condition fails or show_always/show_condition
std::string show_reason;
//show_always, but on show_condition being true
std::function<bool( dialogue & )> show_condition;
std::function<bool( const_dialogue const & )> show_condition;

//flag to hold result of show_anyways (not read from JSON)
bool ignore_conditionals = false;
Expand Down Expand Up @@ -427,7 +427,7 @@ class json_talk_response
{
private:
talk_response actual_response;
std::function<bool( dialogue & )> condition;
std::function<bool( const_dialogue const & )> condition;
bool has_condition_ = false;
bool is_switch = false;
bool is_default = false;
Expand Down
37 changes: 19 additions & 18 deletions src/dialogue_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

template<class T>
std::optional<std::string> maybe_read_var_value(
const abstract_var_info<T> &info, const dialogue &d, int call_depth )
const abstract_var_info<T> &info, const_dialogue const &d, int call_depth )
{
global_variables &globvars = get_globals();
switch( info.type ) {
Expand All @@ -17,9 +17,9 @@ std::optional<std::string> maybe_read_var_value(
case var_type::context:
return d.maybe_get_value( info.name );
case var_type::u:
return d.actor( false )->maybe_get_value( info.name );
return d.const_actor( false )->maybe_get_value( info.name );
case var_type::npc:
return d.actor( true )->maybe_get_value( info.name );
return d.const_actor( true )->maybe_get_value( info.name );
case var_type::var: {
std::optional<std::string> const var_val = d.maybe_get_value( info.name );
if( call_depth > 1000 && var_val ) {
Expand All @@ -40,20 +40,19 @@ std::optional<std::string> maybe_read_var_value(
}

template
std::optional<std::string> maybe_read_var_value( const var_info &, const dialogue &,
int call_depth );
template
std::optional<std::string> maybe_read_var_value( const translation_var_info &, const dialogue &,
std::optional<std::string> maybe_read_var_value( const var_info &, const_dialogue const &,
int call_depth );
template std::optional<std::string> maybe_read_var_value( const translation_var_info &,
const_dialogue const &, int call_depth );

template<>
std::string read_var_value( const var_info &info, const dialogue &d )
std::string read_var_value( const var_info &info, const_dialogue const &d )
{
return maybe_read_var_value( info, d ).value_or( info.default_val );
}

template<>
std::string read_var_value( const translation_var_info &info, const dialogue &d )
std::string read_var_value( const translation_var_info &info, const_dialogue const &d )
{
return maybe_read_var_value( info, d ).value_or( info.default_val.translated() );
}
Expand Down Expand Up @@ -93,7 +92,7 @@ var_info process_variable( const std::string &type )
}

template<>
std::string str_or_var::evaluate( dialogue const &d ) const
std::string str_or_var::evaluate( const_dialogue const &d ) const
{
if( function.has_value() ) {
return function.value()( d );
Expand Down Expand Up @@ -123,7 +122,7 @@ std::string str_or_var::evaluate( dialogue const &d ) const
}

template<>
std::string translation_or_var::evaluate( dialogue const &d ) const
std::string translation_or_var::evaluate( const_dialogue const &d ) const
{
if( function.has_value() ) {
return function.value()( d ).translated();
Expand Down Expand Up @@ -152,14 +151,14 @@ std::string translation_or_var::evaluate( dialogue const &d ) const
return "";
}

std::string str_translation_or_var::evaluate( dialogue const &d ) const
std::string str_translation_or_var::evaluate( const_dialogue const &d ) const
{
return std::visit( [&d]( auto &&val ) {
return val.evaluate( d );
}, val );
}

double dbl_or_var_part::evaluate( dialogue &d ) const
double dbl_or_var_part::evaluate( const_dialogue const &d ) const
{
if( dbl_val.has_value() ) {
return dbl_val.value();
Expand All @@ -182,21 +181,22 @@ double dbl_or_var_part::evaluate( dialogue &d ) const
return 0;
}
if( math_val ) {
return math_val->act( d );
dialogue loosey_goosey( d );
return math_val->act( loosey_goosey );
}
debugmsg( "No valid value for dbl_or_var_part. %s", d.get_callstack() );
return 0;
}

double dbl_or_var::evaluate( dialogue &d ) const
double dbl_or_var::evaluate( const_dialogue const &d ) const
{
if( pair ) {
return rng( min.evaluate( d ), max.evaluate( d ) );
}
return min.evaluate( d );
}

time_duration duration_or_var_part::evaluate( dialogue &d ) const
time_duration duration_or_var_part::evaluate( const_dialogue const &d ) const
{
if( dur_val.has_value() ) {
return dur_val.value();
Expand All @@ -221,13 +221,14 @@ time_duration duration_or_var_part::evaluate( dialogue &d ) const
return 0_seconds;
}
if( math_val ) {
return time_duration::from_turns( math_val->act( d ) );
dialogue loosey_goosey( d );
return time_duration::from_turns( math_val->act( loosey_goosey ) );
}
debugmsg( "No valid value for duration_or_var_part. %s", d.get_callstack() );
return 0_seconds;
}

time_duration duration_or_var::evaluate( dialogue &d ) const
time_duration duration_or_var::evaluate( const_dialogue const &d ) const
{
if( pair ) {
return rng( min.evaluate( d ), max.evaluate( d ) );
Expand Down
19 changes: 10 additions & 9 deletions src/dialogue_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class JsonObject;
class math_exp;
class npc;
struct dialogue;
struct const_dialogue;

using talkfunction_ptr = std::add_pointer_t<void ( npc & )>;
using dialogue_fun_ptr = std::add_pointer_t<void( npc & )>;
Expand Down Expand Up @@ -52,16 +53,16 @@ struct abstract_str_or_var {
std::optional<T> str_val;
std::optional<abstract_var_info<T>> var_val;
std::optional<T> default_val;
std::optional<std::function<T( const dialogue & )>> function;
std::string evaluate( dialogue const & ) const;
std::optional<std::function<T( const_dialogue const & )>> function;
std::string evaluate( const_dialogue const & ) const;
};

using str_or_var = abstract_str_or_var<std::string>;
using translation_or_var = abstract_str_or_var<translation>;

struct str_translation_or_var {
std::variant<str_or_var, translation_or_var> val;
std::string evaluate( dialogue const & ) const;
std::string evaluate( const_dialogue const & ) const;
};

struct talk_effect_fun_t {
Expand Down Expand Up @@ -91,10 +92,10 @@ struct talk_effect_fun_t {
};

template<class T>
std::string read_var_value( const abstract_var_info<T> &info, const dialogue &d );
std::string read_var_value( const abstract_var_info<T> &info, const_dialogue const &d );
template<class T>
std::optional<std::string> maybe_read_var_value(
const abstract_var_info<T> &info, const dialogue &d, int call_depth = 0 );
const abstract_var_info<T> &info, const_dialogue const &d, int call_depth = 0 );

var_info process_variable( const std::string &type );

Expand Down Expand Up @@ -141,7 +142,7 @@ struct dbl_or_var_part {
std::optional<var_info> var_val;
std::optional<double> default_val;
std::optional<eoc_math> math_val;
double evaluate( dialogue &d ) const;
double evaluate( const_dialogue const &d ) const;

bool is_constant() const {
return dbl_val.has_value();
Expand Down Expand Up @@ -169,7 +170,7 @@ struct dbl_or_var {
bool pair = false;
dbl_or_var_part min;
dbl_or_var_part max;
double evaluate( dialogue &d ) const;
double evaluate( const_dialogue const &d ) const;

bool is_constant() const {
return !max && min.is_constant();
Expand All @@ -194,14 +195,14 @@ struct duration_or_var_part {
std::optional<var_info> var_val;
std::optional<time_duration> default_val;
std::optional<eoc_math> math_val;
time_duration evaluate( dialogue &d ) const;
time_duration evaluate( const_dialogue const &d ) const;
};

struct duration_or_var {
bool pair = false;
duration_or_var_part min;
duration_or_var_part max;
time_duration evaluate( dialogue &d ) const;
time_duration evaluate( const_dialogue const &d ) const;
};

#endif // CATA_SRC_DIALOGUE_HELPERS_H
4 changes: 2 additions & 2 deletions src/effect_on_condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,15 @@ bool effect_on_condition::activate( dialogue &d, bool require_callstack_check )
return retval;
}

bool effect_on_condition::check_deactivate( dialogue &d ) const
bool effect_on_condition::check_deactivate( const_dialogue const &d ) const
{
if( !has_deactivate_condition || has_false_effect ) {
return false;
}
return deactivate_condition( d );
}

bool effect_on_condition::test_condition( dialogue &d ) const
bool effect_on_condition::test_condition( const_dialogue const &d ) const
{
return !has_condition || condition( d );
}
Expand Down
8 changes: 4 additions & 4 deletions src/effect_on_condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ struct effect_on_condition {
effect_on_condition_id id;
std::vector<std::pair<effect_on_condition_id, mod_id>> src;
eoc_type type;
std::function<bool( dialogue & )> condition;
std::function<bool( dialogue & )> deactivate_condition;
std::function<bool( const_dialogue const & )> condition;
std::function<bool( const_dialogue const & )> deactivate_condition;
talk_effect_t true_effect;
talk_effect_t false_effect;
bool has_deactivate_condition = false;
Expand All @@ -71,8 +71,8 @@ struct effect_on_condition {
event_type required_event;
duration_or_var recurrence;
bool activate( dialogue &d, bool require_callstack_check = true ) const;
bool check_deactivate( dialogue &d ) const;
bool test_condition( dialogue &d ) const;
bool check_deactivate( const_dialogue const &d ) const;
bool test_condition( const_dialogue const &d ) const;
void apply_true_effects( dialogue &d ) const;
void load( const JsonObject &jo, std::string_view src );
void finalize();
Expand Down
Loading

0 comments on commit c0ed85d

Please sign in to comment.