Skip to content

Commit bb34304

Browse files
authored
Merge pull request CleverRaven#49857 from Ramza13/morale_json
Add morale and focus functions
2 parents d70fed3 + 87a6eb4 commit bb34304

File tree

8 files changed

+125
-3
lines changed

8 files changed

+125
-3
lines changed

doc/NPCs.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ Effect | Description
492492
`u_mod_fatigue: fatigue_int`<br/>`npc_mod_fatigue: fatigue_int` | Your character or the NPC will have `fatigue_int` added or subtracted from its fatigue.
493493
`u_make_sound, npc_make_sound: message_string`, `volume: volume_int`, `type: type_string`, | A sound of description `message_string` will be made at your character or the NPC's location of volume `volume_int` and type `type_string`. Possible types are: background, weather, music, movement, speech, electronic_speech, activity, destructive_activity, alarm, combat, alert, or order
494494
`u_mod_healthy, npc_mod_healthy : amount_int, cap: cap_int` | Your character or the NPC will have `amount_int` added or subtracted from its health value, but not beyond `cap_int`.
495+
`u_add_morale: morale_string`, (*optional* `bonus: bonus_int` ), (*optional* `max_bonus: max_bonus_int` ), (*optional* `duration: duration_int`), (*optional* `decay_start` : `decay_int`), (*optional* `capped`: `capped_bool`)<br/> `npc_add_morale: morale_string`, (*optional* `bonus: bonus_int` ), (*optional* `max_bonus: max_bonus_int` ), (*optional* `duration: duration_int`), (*optional*`decay_start` : `decay_int`), (*optional* `capped`: `capped_bool`)| Your character or the NPC will gain a morale bonus of type `morale_string`. Morale is changed by `bonus_int` (default 1), with a maximum of up to `max_bonus_int` (default 1). It will last for `duration: duration_int` seconds (default 1 hour). It will begin to decay after `decay_int` seconds (default 0.5 hours). `capped_bool` Whether this morale is capped or not, defaults to false.
496+
`u_lose_morale: morale_string`<br/>`npc_lose_morale: morale_string` | Your character or the NPC will lose any morale of type `morale_string`.
497+
`u_mod_focus: focus_int`<br/>`npc_mod_focus: focus_int` | Your character or the NPC will have `focus_int` added or subtracted from its focus.
495498

496499
#### Trade / Items
497500

@@ -644,6 +647,8 @@ Condition | Type | Description
644647
`"u_has_power"`<br/>`"npc_has_power"` | int | `true` if the player character's or NPC's bionic power is at least the value of `u_has_power` or `npc_has_power`.
645648
`"u_can_see"`<br/>`"npc_can_see"` | simple string | `true` if the player character or NPC is not blind and is either not sleeping or has the see_sleep trait.
646649
`"u_is_deaf"`<br/>`"npc_is_deaf"` | int | `true` if the player character or NPC can't hear.
650+
`"u_has_focus"`<br/>`"npc_has_focus"` | int | `true` if the player character's or NPC's focus is at least the value of `u_has_focus` or `npc_has_focus`.
651+
`"u_has_morale"`<br/>`"npc_has_morale"` | int | `true` if the player character's or NPC's morale is at least the value of `u_has_morale` or `npc_has_morale`.
647652

648653
#### Player Only conditions
649654

src/condition.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,26 @@ void conditional_t<T>::set_has_effect( const JsonObject &jo, const std::string &
291291
};
292292
}
293293

294+
template<class T>
295+
void conditional_t<T>::set_has_morale( const JsonObject &jo, const std::string &member,
296+
bool is_npc )
297+
{
298+
const int min_morale = jo.get_int( member );
299+
condition = [min_morale, is_npc]( const T & d ) {
300+
return d.actor( is_npc )->morale_cur() >= min_morale;
301+
};
302+
}
303+
304+
template<class T>
305+
void conditional_t<T>::set_has_focus( const JsonObject &jo, const std::string &member,
306+
bool is_npc )
307+
{
308+
const int min_focus = jo.get_int( member );
309+
condition = [min_focus, is_npc]( const T & d ) {
310+
return d.actor( is_npc )->focus_cur() >= min_focus;
311+
};
312+
}
313+
294314
template<class T>
295315
void conditional_t<T>::set_need( const JsonObject &jo, const std::string &member, bool is_npc )
296316
{
@@ -1149,10 +1169,18 @@ conditional_t<T>::conditional_t( const JsonObject &jo )
11491169
set_has_pain( jo, "u_has_pain" );
11501170
} else if( jo.has_member( "npc_has_pain" ) ) {
11511171
set_has_pain( jo, "npc_has_pain", is_npc );
1172+
} else if( jo.has_int( "u_has_morale" ) ) {
1173+
set_has_morale( jo, "u_has_morale" );
1174+
} else if( jo.has_int( "npc_has_morale" ) ) {
1175+
set_has_morale( jo, "npc_has_morale", is_npc );
11521176
} else if( jo.has_member( "u_has_power" ) ) {
11531177
set_has_power( jo, "u_has_power" );
11541178
} else if( jo.has_member( "npc_has_power" ) ) {
11551179
set_has_power( jo, "npc_has_power", is_npc );
1180+
} else if( jo.has_int( "u_has_focus" ) ) {
1181+
set_has_focus( jo, "u_has_focus" );
1182+
} else if( jo.has_int( "npc_has_focus" ) ) {
1183+
set_has_focus( jo, "npc_has_focus", is_npc );
11561184
} else if( jo.has_string( "is_weather" ) ) {
11571185
set_is_weather( jo );
11581186
} else {

src/condition.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const std::unordered_set<std::string> complex_conds = { {
4646
"u_compare_time_since_var", "npc_compare_time_since_var", "is_weather", "one_in_chance",
4747
"is_temperature", "is_windpower", "is_humidity", "is_pressure", "u_is_height", "npc_is_height",
4848
"u_has_worn_with_flag", "npc_has_worn_with_flag", "u_has_wielded_with_flag", "npc_has_wielded_with_flag",
49-
"u_has_pain", "npc_has_pain", "u_has_power", "npc_has_power"
49+
"u_has_pain", "npc_has_pain", "u_has_power", "npc_has_power", "u_has_focus", "npc_has_focus", "u_has_morale", "npc_has_morale"
5050
}
5151
};
5252
} // namespace dialogue_data
@@ -156,7 +156,8 @@ struct conditional_t {
156156
void set_u_know_recipe( const JsonObject &jo, const std::string &member );
157157
void set_mission_has_generic_rewards();
158158
void set_can_see( bool is_npc = false );
159-
159+
void set_has_morale( const JsonObject &jo, const std::string &member, bool is_npc = false );
160+
void set_has_focus( const JsonObject &jo, const std::string &member, bool is_npc = false );
160161
bool operator()( const T &d ) const {
161162
if( !condition ) {
162163
return false;

src/dialogue.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ struct talk_effect_fun_t {
139139
const translation &name );
140140
void set_u_learn_recipe( const std::string &learned_recipe_id );
141141
void set_npc_first_topic( const std::string &chat_topic );
142-
142+
void set_mod_focus( const JsonObject &jo, const std::string &member, bool is_npc );
143+
void set_add_morale( const JsonObject &jo, const std::string &member, bool is_npc );
144+
void set_lose_morale( const JsonObject &jo, const std::string &member, bool is_npc );
143145
void operator()( const dialogue &d ) const {
144146
if( !function ) {
145147
return;

src/npctalk.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,39 @@ void talk_effect_fun_t::set_queue_effect_on_condition( const JsonObject &jo,
23162316
}
23172317
};
23182318
}
2319+
void talk_effect_fun_t::set_add_morale( const JsonObject &jo, const std::string &member,
2320+
bool is_npc )
2321+
{
2322+
std::string new_type = jo.get_string( member );
2323+
int bonus = jo.get_int( "bonus" );
2324+
int max_bonus = jo.get_int( "max_bonus", 0 );
2325+
time_duration duration = time_duration::from_seconds( jo.get_int( "duration", 3600 ) );
2326+
time_duration decay_start = time_duration::from_seconds( jo.get_int( "decay_start", 1800 ) );
2327+
const bool capped = jo.get_bool( "capped", false );
2328+
function = [is_npc, new_type, bonus, max_bonus, duration, decay_start,
2329+
capped]( const dialogue & d ) {
2330+
d.actor( is_npc )->add_morale( morale_type( new_type ), bonus, max_bonus, duration, decay_start,
2331+
capped );
2332+
};
2333+
}
2334+
2335+
void talk_effect_fun_t::set_lose_morale( const JsonObject &jo, const std::string &member,
2336+
bool is_npc )
2337+
{
2338+
std::string old_morale = jo.get_string( member );
2339+
function = [is_npc, old_morale]( const dialogue & d ) {
2340+
d.actor( is_npc )->remove_morale( morale_type( old_morale ) );
2341+
};
2342+
}
2343+
2344+
void talk_effect_fun_t::set_mod_focus( const JsonObject &jo, const std::string &member,
2345+
bool is_npc )
2346+
{
2347+
int amount = jo.get_int( member );
2348+
function = [is_npc, amount]( const dialogue & d ) {
2349+
d.actor( is_npc )->mod_focus( amount );
2350+
};
2351+
}
23192352

23202353
void talk_effect_t::set_effect_consequence( const talk_effect_fun_t &fun,
23212354
dialogue_consequence con )
@@ -2586,6 +2619,18 @@ void talk_effect_t::parse_sub_effect( const JsonObject &jo )
25862619
subeffect_fun.set_mod_healthy( jo, "u_mod_healthy", false );
25872620
} else if( jo.has_member( "npc_mod_healthy" ) ) {
25882621
subeffect_fun.set_mod_healthy( jo, "npc_mod_healthy", true );
2622+
} else if( jo.has_int( "u_mod_focus" ) ) {
2623+
subeffect_fun.set_mod_focus( jo, "u_mod_focus", false );
2624+
} else if( jo.has_int( "npc_mod_focus" ) ) {
2625+
subeffect_fun.set_mod_focus( jo, "npc_mod_focus", true );
2626+
} else if( jo.has_string( "u_add_morale" ) ) {
2627+
subeffect_fun.set_add_morale( jo, "u_add_morale", false );
2628+
} else if( jo.has_string( "npc_add_morale" ) ) {
2629+
subeffect_fun.set_add_morale( jo, "npc_add_morale", true );
2630+
} else if( jo.has_string( "u_lose_morale" ) ) {
2631+
subeffect_fun.set_lose_morale( jo, "u_lose_morale", false );
2632+
} else if( jo.has_string( "npc_lose_morale" ) ) {
2633+
subeffect_fun.set_lose_morale( jo, "npc_lose_morale", true );
25892634
} else {
25902635
jo.throw_error( "invalid sub effect syntax: " + jo.str() );
25912636
}

src/talker.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,14 @@ class talker
333333
return 0_kJ;
334334
}
335335
virtual void mod_healthy_mod( int, int ) {};
336+
virtual int morale_cur() const {
337+
return 0;
338+
}
339+
virtual int focus_cur() const {
340+
return 0;
341+
}
342+
virtual void mod_focus( int ) {}
343+
virtual void add_morale( const morale_type &, int, int, time_duration, time_duration, bool ) {}
344+
virtual void remove_morale( const morale_type & ) {}
336345
};
337346
#endif // CATA_SRC_TALKER_H

src/talker_character.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,29 @@ void talker_character::mod_healthy_mod( int amount, int cap )
340340
{
341341
me_chr->mod_healthy_mod( amount, cap );
342342
}
343+
344+
int talker_character::morale_cur() const
345+
{
346+
return me_chr->get_morale_level();
347+
}
348+
349+
void talker_character::add_morale( const morale_type &new_morale, int bonus, int max_bonus,
350+
time_duration duration, time_duration decay_start, bool capped )
351+
{
352+
me_chr->add_morale( new_morale, bonus, max_bonus, duration, decay_start, capped );
353+
}
354+
355+
void talker_character::remove_morale( const morale_type &old_morale )
356+
{
357+
me_chr->rem_morale( old_morale );
358+
}
359+
360+
int talker_character::focus_cur() const
361+
{
362+
return me_chr->get_focus();
363+
}
364+
365+
void talker_character::mod_focus( int amount )
366+
{
367+
me_chr->mod_focus( amount );
368+
}

src/talker_character.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ class talker_character: public talker
117117
void mod_pain( int amount ) override;
118118
bool can_see() const override;
119119
void mod_healthy_mod( int, int ) override;
120+
int morale_cur() const override;
121+
void add_morale( const morale_type &new_morale, int bonus, int max_bonus, time_duration duration,
122+
time_duration decay_started, bool capped ) override;
123+
void remove_morale( const morale_type &old_morale ) override;
124+
int focus_cur() const override;
125+
void mod_focus( int ) override;
120126
protected:
121127
talker_character() = default;
122128
player *me_chr;

0 commit comments

Comments
 (0)