Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eoc/math_parser: port time handling to math #70996

Merged
merged 13 commits into from
Jan 24, 2024
Prev Previous commit
Next Next commit
eoc: port time_since_cataclysm to math
  • Loading branch information
andrei8l committed Jan 24, 2024
commit a481fd5a1d193561ce60be8406ca5e6be9195b14
83 changes: 3 additions & 80 deletions data/json/npcs/TALK_TEST.json
Original file line number Diff line number Diff line change
Expand Up @@ -948,12 +948,12 @@
{
"text": "time_since_cataclysm > 3_days.",
"topic": "TALK_DONE",
"condition": { "compare_num": [ { "time_since_cataclysm": "turns" }, ">", { "time": "3 days" } ] }
"condition": { "math": [ "time_since('cataclysm') > time('3 d')" ] }
},
{
"text": "time_since_cataclysm in days > 3",
"topic": "TALK_DONE",
"condition": { "compare_num": [ { "time_since_cataclysm": "days" }, ">", { "const": 3 } ] }
"condition": { "math": [ "time_since('cataclysm', 'unit':'days') > 3" ] }
},
{
"text": "This is a time since u_var test response for > 3_days.",
Expand Down Expand Up @@ -1116,83 +1116,6 @@
}
]
},
{
"type": "talk_topic",
"id": "TALK_TEST_ARITHMETIC_OP",
"dynamic_line": "This is a test conversation that shouldn't appear in the game.",
"responses": [
{
"text": "Sets time since cataclysm to 2 * 5 turns. (10)",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "=", { "const": 2 }, "*", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm to 15 / 5 turns. (3)",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "=", { "const": 15 }, "/", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm to 2 + 5 turns. (7)",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "=", { "const": 2 }, "+", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm to 5 - 2 turns. (3)",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "=", { "const": 5 }, "-", { "const": 2 } ] }
},
{
"text": "Sets time since cataclysm to 15 % 10 turns. (5)",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "=", { "const": 15 }, "%", { "const": 10 } ] }
},
{
"text": "Sets time since cataclysm to 2 ^ 5 turns. (32)",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "=", { "const": 2 }, "^", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm to 5 turns. (5)",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "=", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm to *= 5 turns.",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "*=", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm to /= 5 turns.",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "/=", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm to += 5 turns.",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "+=", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm to -= 5 turns.",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "-=", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm to %= 5 turns.",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "%=", { "const": 5 } ] }
},
{
"text": "Sets time since cataclysm++.",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "++" ] }
},
{
"text": "Sets time since cataclysm--.",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "--" ] }
}
]
},
{
"type": "talk_topic",
"id": "TALK_TEST_ARITHMETIC",
Expand All @@ -1201,7 +1124,7 @@
{
"text": "Sets time since cataclysm to 1.",
"topic": "TALK_DONE",
"effect": { "arithmetic": [ { "time_since_cataclysm": "turns" }, "=", { "const": 1 } ] }
"effect": { "math": [ "time('now')", "=", "time('cataclysm') + 1" ] }
},
{
"text": "Sets temperature to 2.",
Expand Down
4 changes: 1 addition & 3 deletions data/json/npcs/random_encounters/john_bailey.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@
"effect": [
{ "u_add_var": "u_met_John_Bailey", "value": "yes" },
{ "npc_first_topic": "TALK_RC_John_Bailey_1a" },
{
"arithmetic": [ { "npc_val": "var", "var_name": "zkc_john" }, "=", { "const": 2 }, "*", { "time_since_cataclysm": "days" } ]
},
{ "math": [ "n_zkc_john", "=", "2 * time_since('cataclysm', 'unit': 'days')" ] },
{ "math": [ "n_zkc_john", "+=", "rand(10)" ] }
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
"speaker_effect": {
"effect": [
{ "finish_mission": "MISSION_TACOMA_Nunez", "success": true },
{ "math": [ "nunez_travel_timer", "=", "u_val('time_since_cataclysm: days') + 2" ] },
{ "math": [ "nunez_travel_timer", "=", "time_since('cataclysm', 'unit': 'days') + 2" ] },
{ "math": [ "nunez_travel_stage", "=", "1" ] }
]
}
Expand Down
2 changes: 1 addition & 1 deletion data/json/npcs/tacoma_ranch/Nunez/Arrival_code.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"condition": {
"and": [
{ "math": [ "nunez_travel_stage", "==", "1" ] },
{ "math": [ "nunez_travel_timer", "<", "u_val('time_since_cataclysm: days')" ] }
{ "math": [ "nunez_travel_timer", "<", "time_since('cataclysm', 'unit': 'days')" ] }
]
},
"deactivate_condition": { "math": [ "nunez_travel_stage", "==", "2" ] },
Expand Down
2 changes: 1 addition & 1 deletion data/mods/Xedra_Evolved/eocs/spell_learning_eoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"eoc_type": "EVENT",
"required_event": "player_levels_spell",
"//": "time_since_cataclysm check is used because spells, given by scenario, call player_levels_spell event also",
"condition": { "and": [ { "one_in_chance": 16 }, { "math": [ "u_val('time_since_cataclysm: turns')", ">", "2" ] } ] },
"condition": { "and": [ { "one_in_chance": 16 }, { "math": [ "time_since('cataclysm') > 2" ] } ] },
"effect": [
{
"if": { "and": [ { "u_has_trait": "EATER" }, { "compare_string": [ "EATER", { "context_val": "school" } ] } ] },
Expand Down
2 changes: 1 addition & 1 deletion doc/MAPGEN.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Examples:
"//3": "evaluated dynamically from global variable"
"weight": { "global_val": "my_weight" },
"//4": "evaluated dynamically from math expression"
"weight": { "math": [ "my_weight * u_val('time_since_cataclysm: days')" ] }
"weight": { "math": [ "my_weight * time_since('cataclysm', 'unit': 'days')" ] }
```


Expand Down
1 change: 0 additions & 1 deletion doc/NPCs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,6 @@ Example | Description
--- | ---
`"const": 5` | A constant value, in this case 5. Can be read but not written to.
`"time": "5 days"` | A constant time value. Will be converted to turns. Can be read but not written to.
`"time_since_cataclysm": "turns"` | Time since the start of the cataclysm in turns. Can instead take other time units such as minutes, hours, days, weeks, seasons, and years.
`"time_until_eoc": "eoc_id"` | Time from now until the next scheduled run of `eoc_id`. Optional member `unit` can specify a member to use instead of turns such as minutes, hours, days, weeks, seasons, and years.
`"rand": 20` | A random value between 0 and a given value, in this case 20. Can be read but not written to.
`"faction_trust": "free_merchants"` | The trust the faction has for the player (see [FACTIONS.md](FACTIONS.md)) for details.
Expand Down
45 changes: 1 addition & 44 deletions src/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2062,27 +2062,6 @@ std::function<double( dialogue & )> conditional_t::get_get_dbl( J const &jo )
return [value]( dialogue const & ) {
return value;
};
} else if( jo.has_member( "time_since_cataclysm" ) ) {
time_duration given_unit = 1_turns;
if( jo.has_string( "time_since_cataclysm" ) ) {
std::string given_unit_str = jo.get_string( "time_since_cataclysm" );
bool found = false;
for( const auto &pair : time_duration::units ) {
const std::string &unit = pair.first;
if( unit == given_unit_str ) {
given_unit = pair.second;
found = true;
break;
}
}
if( !found ) {
jo.throw_error( "unrecognized time unit in " + jo.str() );
}
}
return [given_unit]( dialogue const & ) {
return ( to_turn<int>( calendar::turn ) - to_turn<int>( calendar::start_of_cataclysm ) ) /
to_turns<int>( given_unit );
};
} else if( jo.has_member( "time_until_eoc" ) ) {
time_duration given_unit = 1_turns;
effect_on_condition_id eoc_id = effect_on_condition_id( jo.get_string( "time_until_eoc" ) );
Expand Down Expand Up @@ -2730,29 +2709,7 @@ conditional_t::get_set_dbl( const J &jo, const std::optional<dbl_or_var_part> &m
} else if( jo.has_member( "const" ) ) {
jo.throw_error( "attempted to alter a constant value in " + jo.str() );
} else if( jo.has_member( "time" ) ) {
jo.throw_error( "can not alter a time constant. Did you mean time_since_cataclysm? In "
+ jo.str() );
} else if( jo.has_member( "time_since_cataclysm" ) ) {
time_duration given_unit = 1_turns;
if( jo.has_string( "time_since_cataclysm" ) ) {
std::string given_unit_str = jo.get_string( "time_since_cataclysm" );
bool found = false;
for( const auto &pair : time_duration::units ) {
const std::string &unit = pair.first;
if( unit == given_unit_str ) {
given_unit = pair.second;
found = true;
break;
}
}
if( !found ) {
jo.throw_error( "unrecognized time unit in " + jo.str() );
}
}
return [given_unit, min, max]( dialogue & d, double input ) {
calendar::turn = time_point( handle_min_max( d, input, min,
max ) * to_turns<int>( given_unit ) );
};
jo.throw_error( "can not alter a time constant. In " + jo.str() );
} else if( jo.has_member( "rand" ) ) {
jo.throw_error( "can not alter the random number generator, silly! In " + jo.str() );
} else if( jo.has_member( "faction_trust" ) ) {
Expand Down
97 changes: 0 additions & 97 deletions tests/npc_talk_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1265,103 +1265,6 @@ TEST_CASE( "npc_compare_int", "[npc_talk]" )
player_character.remove_value( "npctalk_var_test_var_time_test_test" );
}

TEST_CASE( "npc_arithmetic_op", "[npc_talk]" )
{
dialogue d;
prep_test( d );

d.add_topic( "TALK_TEST_ARITHMETIC_OP" );
gen_response_lines( d, 14 );

calendar::turn = calendar::turn_zero;
calendar::start_of_cataclysm = calendar::turn_zero;
calendar::start_of_game = calendar::turn_zero;

REQUIRE( calendar::turn == time_point( 0 ) );
// "Sets time since cataclysm to 2 * 5 turns. (10)"
talk_effect_t &effects = d.responses[ 0 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 10 ) );

calendar::turn = calendar::turn_zero;
// "Sets time since cataclysm to 15 / 5 turns. (3)"
effects = d.responses[ 1 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 3 ) );

calendar::turn = calendar::turn_zero;
// "Sets time since cataclysm to 2 + 5 turns. (7)"
effects = d.responses[ 2 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 7 ) );

calendar::turn = calendar::turn_zero;
// "Sets time since cataclysm to 5 - 2 turns. (3)"
effects = d.responses[ 3 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 3 ) );

calendar::turn = calendar::turn_zero;
// "Sets time since cataclysm to 15 % 10 turns. (5)"
effects = d.responses[ 4 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 5 ) );

calendar::turn = calendar::turn_zero;
// "Sets time since cataclysm to 2 ^ 5 turns. (32)"
effects = d.responses[ 5 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 32 ) );

calendar::turn = calendar::turn_zero;
// "Sets time since cataclysm to 5 turns. (5)"
effects = d.responses[ 6 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 5 ) );

calendar::turn = time_point( 5 );
// "Sets time since cataclysm to *= 5 turns."
effects = d.responses[ 7 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 25 ) );

calendar::turn = time_point( 5 );
// "Sets time since cataclysm to /= 5 turns."
effects = d.responses[ 8 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 1 ) );

calendar::turn = time_point( 5 );
// "Sets time since cataclysm to += 5 turns."
effects = d.responses[ 9 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 10 ) );

calendar::turn = time_point( 11 );
// "Sets time since cataclysm to -= 5 turns."
effects = d.responses[ 10 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 6 ) );

calendar::turn = time_point( 17 );
// "Sets time since cataclysm to %= 5 turns."
effects = d.responses[ 11 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 2 ) );

calendar::turn = time_point( 5 );
// "Sets time since cataclysm++."
effects = d.responses[ 12 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 6 ) );

calendar::turn = time_point( 5 );
// "Sets time since cataclysm--."
effects = d.responses[ 13 ].success;
effects.apply( d );
CHECK( calendar::turn == time_point( 4 ) );
}

TEST_CASE( "npc_arithmetic", "[npc_talk]" )
{
dialogue d;
Expand Down