Skip to content

Commit

Permalink
Allow '1-infinity' in ranges, and tighten the schema's validation of …
Browse files Browse the repository at this point in the history
…ranges

Fixes #4253.

The schema's validation of ranges now rejects negative numbers, because the C++
code doesn't support them (both before and after this commit):
* utils::parse_range("-7") will return {0,0}, because std::stoi("") throws invalid_argument.
* src/map/location.cpp will parse it as -7, but that will never match an on-map coordinate
  • Loading branch information
stevecotton committed Aug 29, 2019
1 parent ba4f3a7 commit 1c123fb
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 115 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Expand Up @@ -8,6 +8,8 @@
* Set up for translating the Wings of Victory campaign (PR#4265)
* Changed the :help command's output to split over multiple lines
* Added translatable explanations of :droid, :help and :idle's arguments
### WML engine
* Ranges can now have upper limit "infinity" instead using a big number like "99" or "99999"

## Version 1.15.1
### Editor
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/scenarios/recruiting.cfg
Expand Up @@ -115,7 +115,7 @@ A Micro AI can be added and adapted to the need of a scenario easily using only
[if]
[not]
[have_unit]
count=1-9999
count=1-infinity
side=$unit.side
[/have_unit]
[/not]
Expand Down
2 changes: 1 addition & 1 deletion data/ai/micro_ais/scenarios/wolves.cfg
Expand Up @@ -262,7 +262,7 @@ Also note that the wolves AI is coded as a Micro AI. A Micro AI can be added and
[if]
[not]
[have_unit]
count=1-9999
count=1-infinity
side=$unit.side
[/have_unit]
[/not]
Expand Down
Expand Up @@ -195,7 +195,7 @@
[have_unit]
# if they have less than 5 surviving units, then it's the wrong time to hint about healers
side=1
count=5-99999
count=5-infinity
[/have_unit]
[/filter_condition]

Expand Down
Expand Up @@ -230,7 +230,7 @@
[/variable]
[have_location]
owner_side=1
count=4-99
count=4-infinity
[/have_location]

# Make sure the guards actually exist, in case there was no space for them
Expand Down
10 changes: 5 additions & 5 deletions data/campaigns/Eastern_Invasion/scenarios/11_Captured.cfg
Expand Up @@ -238,7 +238,7 @@
[filter]
side=1
[not]
x,y=1-999,1-999
{EVERYWHERE}
[/not]
level=4
[/filter]
Expand All @@ -251,7 +251,7 @@
[filter]
side=1
[not]
x,y=1-999,1-999
{EVERYWHERE}
[/not]
level=3
[/filter]
Expand All @@ -265,7 +265,7 @@
[filter]
side=1
[not]
x,y=1-999,1-999
{EVERYWHERE}
[/not]
level=2
[/filter]
Expand All @@ -279,7 +279,7 @@
[filter]
side=1
[not]
x,y=1-999,1-999
{EVERYWHERE}
[/not]
level=1
[/filter]
Expand Down Expand Up @@ -677,7 +677,7 @@
[if]
[have_unit]
side=1
count=2-99
count=2-infinity
[/have_unit]

[then]
Expand Down
Expand Up @@ -230,7 +230,7 @@
[capture_village]
side=4
x=1-26
y=1-999
y=1-infinity
[/capture_village]
[/event]
#endif
Expand Down
Expand Up @@ -364,7 +364,7 @@
[/have_unit]
[have_unit]
side=1,2,3,4
count=25-999
count=25-infinity
[/have_unit]
[then]
[message]
Expand Down
3 changes: 1 addition & 2 deletions data/campaigns/Legend_of_Wesmere/utils/map-utils.cfg
Expand Up @@ -7,8 +7,7 @@
#define LOAD_SUBMAP X_SPAN Y_SPAN OFFSET_X OFFSET_Y MAP
[store_unit]
[filter]
x=1-999
y=1-999
{EVERYWHERE}
[/filter]
variable=MOVE_UNIT_store
kill=yes
Expand Down
4 changes: 2 additions & 2 deletions data/campaigns/Liberty/scenarios/08_Glory.cfg
Expand Up @@ -123,7 +123,7 @@
[/avoid]
[/ai]
[ai]
turns=7-99999
turns=7-infinity

# Now just avoid Sir Gwydion
[avoid]
Expand Down Expand Up @@ -195,7 +195,7 @@
[/avoid]
[/ai]
[ai]
turns=7-99999
turns=7-infinity

# Now just avoid the orc leader
[avoid]
Expand Down
10 changes: 5 additions & 5 deletions data/campaigns/Secrets_of_the_Ancients/scenarios/04_Becalmed.cfg
Expand Up @@ -81,8 +81,8 @@ The journey started well enough, but on the dawn following our departure, the wi
green=-30
blue=-25
[/time]
x=14, 0-99
y=12, 13-99
x=14, 0-infinity
y=12, 13-infinity
[/time_area]

{DEFAULT_MUSIC_PLAYLIST}
Expand Down Expand Up @@ -297,7 +297,7 @@ The journey started well enough, but on the dawn following our departure, the wi
# will stick to the script. She's fair game on the
# third barrel though.
x=1-10
y=11-99
y=11-infinity
[/or]
[or]
# She also doesn't get *immediately* attacked if she
Expand Down Expand Up @@ -345,11 +345,11 @@ The journey started well enough, but on the dawn following our departure, the wi
y=6
[/filter]
[filter_location]
x=0-99
x=0-infinity
y=0-12
[/filter_location]
[filter_location_enemy]
x=0-99
x=0-infinity
y=13-99
[/filter_location_enemy]
[/micro_ai]
Expand Down
Expand Up @@ -371,7 +371,7 @@ I was so sure I had the answer! I was wrong. I wanted to believe that I was imag
[not]
variation=bat # undead bat
[/not]
count=6-99 # six or more
count=6-infinity # six or more
[/have_unit]
[then]
[fire_event]
Expand Down
Expand Up @@ -385,8 +385,7 @@ After several days of travel, he approached the frontier town of Carcyn."
[then]
[capture_village]
side=1
x=1-99
y=1-99
{EVERYWHERE}
[/capture_village]
[delay]
time=800
Expand Down
Expand Up @@ -144,7 +144,7 @@
[store_unit]
[filter]
race=human
x=1-99
{EVERYWHERE}
[not]
id=Ras-Tabahn
[/not]
Expand Down
Expand Up @@ -1404,7 +1404,7 @@ _ "These undead trolls fight again in death as they did in life, except this tim
# Event 24: Troll Undead Uprising

# count number of elves that have entered the trolls cave. Once 3 elves
# are in the cave, then active the troll undead uprising
# are in the cave, then activate the troll undead uprising

[event]
name=new turn
Expand All @@ -1414,7 +1414,7 @@ _ "These undead trolls fight again in death as they did in life, except this tim
x=35-50
y=23-29
side=1
count=3-99999
count=3-infinity
[/have_unit]
[/filter_condition]

Expand Down
2 changes: 1 addition & 1 deletion data/core/macros/scenario-utils.cfg
Expand Up @@ -15,7 +15,7 @@

# Everywhere on the map. For filters that would otherwise touch recall lists.
#define EVERYWHERE
x,y=1-999,1-999 #enddef
x,y=1-infinity,1-infinity #enddef

#define NO_MAP_DATA
# The display code in the game becomes confused if you give it a
Expand Down
4 changes: 2 additions & 2 deletions data/core/macros/side-utils.cfg
Expand Up @@ -38,7 +38,7 @@
[have_unit]
side=$side_number
type={TYPE}
count="{LIMIT_NUMBER}-99999"
count="{LIMIT_NUMBER}-infinity"
[/have_unit]

[then]
Expand Down Expand Up @@ -68,7 +68,7 @@
[have_unit]
side=$side_number
type={TYPE}
count="{LIMIT_NUMBER}-99999"
count="{LIMIT_NUMBER}-infinity"
[/have_unit]
[/filter_condition]

Expand Down
16 changes: 8 additions & 8 deletions data/schema/game_config.cfg
Expand Up @@ -72,16 +72,16 @@
[/type]
[/union]
[/type]
# definition of range_list and s_range_list, before macros are expanded
{LIST_TYPE_COMPLEX range (
[element]
[list]
split="-"
min=1
max=2
[element]
link=unsigned
[/element]
[/list]
value="\d+"
[/element]
[element]
value="\d+-\d+"
[/element]
[element]
value="\d+-infinity"
[/element]
)}
[type]
Expand Down
25 changes: 25 additions & 0 deletions data/test/scenarios/store_locations.cfg
Expand Up @@ -8,3 +8,28 @@
{RETURN ({VARIABLE_CONDITIONAL tmp.terrain equals "Gg^Kov"})}
[/event]
)}

# This doubles as a test of the "1-infinity" range
{GENERIC_UNIT_TEST "store_locations_range" (
[event]
name = start

# Including the borders, the map is 18 x 10
# Doubles as a test that whitespace is allowed within ranges.
[store_locations]
x,y=10 - infinity,5-infinity
variable=tmp
[/store_locations]
{ASSERT ({VARIABLE_CONDITIONAL tmp.length equals 40})}

# The definition of EVERYWHERE excludes x=0 and y=0, and the include_borders=no also excludes those columns
[store_locations]
{EVERYWHERE}
include_borders=no
variable=tmp
[/store_locations]
{ASSERT ({VARIABLE_CONDITIONAL tmp.length equals 128})}

{SUCCEED}
[/event]
)}
2 changes: 1 addition & 1 deletion src/game_events/conditional_wml.cpp
Expand Up @@ -42,7 +42,7 @@ static lg::log_domain log_engine("engine");
namespace game_events {

namespace builtin_conditions {
std::vector<std::pair<int,int>> default_counts = utils::parse_ranges("1-99999");
std::vector<std::pair<int,int>> default_counts = utils::parse_ranges("1-infinity");

bool have_unit(const vconfig& cfg)
{
Expand Down

0 comments on commit 1c123fb

Please sign in to comment.