How to load a map from an array of variables?

The place to post your WML questions and answers.

Moderators: Forum Moderators, Developers

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
User avatar
PapaSmurfReloaded
Posts: 608
Joined: November 17th, 2007, 1:10 pm
Location: Argentina

How to load a map from an array of variables?

Post by PapaSmurfReloaded » June 18th, 2018, 4:10 pm

Here is the thing, I want to save the terrains of the entire map, to be able to load it eventually.

How does it work? First I have to save the map using [store_locations], but then how do I load the entire map using [terrain]?
18th Century Warfare Era, try it out!
A Random RPG Adventure, play it!
Infantry Wars Era, give it a shot!
Zombie Apocalypse RPG, run for your life!

User avatar
octalot
Code Contributor
Posts: 213
Joined: July 17th, 2010, 7:40 pm

Re: How to load a map from an array of variables?

Post by octalot » June 18th, 2018, 4:32 pm

Do you mean "load" as in "the scenario is already running, I've covered half the map with lava, now I want to change those hexes back to what they originally were"?

Code: Select all

{FOREACH original i}
    {MODIFY_TERRAIN $original[$i].terrain $original[$i].x $original[$i].y}
{NEXT i}
{CLEAR_VARIABLE i}
[redraw][/redraw]
(Note: cut, modified and pasted without checking that I didn't break anything)

Shiki
Developer
Posts: 232
Joined: July 13th, 2015, 9:53 pm
Location: Germany

Re: How to load a map from an array of variables?

Post by Shiki » June 18th, 2018, 5:06 pm

Although, better use [foreach] or [for], as {FOREACH} is deprecated by them.
Maintainer of Era of Myths.
Introduction to WML filtering and WML variables.

User avatar
PapaSmurfReloaded
Posts: 608
Joined: November 17th, 2007, 1:10 pm
Location: Argentina

Re: How to load a map from an array of variables?

Post by PapaSmurfReloaded » June 18th, 2018, 5:28 pm

octalot wrote:
June 18th, 2018, 4:32 pm
Do you mean "load" as in "the scenario is already running, I've covered half the map with lava, now I want to change those hexes back to what they originally were"?

Code: Select all

{FOREACH original i}
{MODIFY_TERRAIN $original[$i].terrain $original[$i].x $original[$i].y}
{NEXT i}
{CLEAR_VARIABLE i}
[redraw][/redraw]
(Note: cut, modified and pasted without checking that I didn't break anything)
Yep, to be exact what I am doing working on the ARRPGA, allowing the DM to change the seasons of the map. As well adding an option to return the map to what it originally looked like(since it might have terrains which correspond to different "seasons".

Edit: Works perfectly, thank you a million.
18th Century Warfare Era, try it out!
A Random RPG Adventure, play it!
Infantry Wars Era, give it a shot!
Zombie Apocalypse RPG, run for your life!

User avatar
zookeeper
WML Wizard
Posts: 9739
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Re: How to load a map from an array of variables?

Post by zookeeper » June 18th, 2018, 5:42 pm

If the map is at all large, then saving the map using nothing but [store_locations] would naturally create a gigantic array. You could avoid that by iterating through the columns and rows of the map and constructing a proper map data string that you could later apply using [terrain_mask] [replace_map], but obviously that's more complicated to write.

enclave
Posts: 909
Joined: December 15th, 2007, 8:52 am

Re: How to load a map from an array of variables?

Post by enclave » June 19th, 2018, 1:42 am

PapaSmurfReloaded wrote:
June 18th, 2018, 5:28 pm
Yep, to be exact what I am doing working on the ARRPGA, allowing the DM to change the seasons of the map. As well adding an option to return the map to what it originally looked like(since it might have terrains which correspond to different "seasons".
If you just wanted to use seasons, in some cases it could be useful and more efficient or fast to just do something like this:

Code: Select all

[terrain]
terrain=Kha
[and]
terrain=Kh
[/and]
[/terrain]
All castles with terrain code Kh change into snow castles with terrain code Kha
And then reverse them... when needed.
Same way:

Code: Select all

[terrain]
terrain=Ai
[and]
terrain=W*,W*^*
[/and]
[/terrain]
All water changes to ice...
But of course to make it look nicer it's better change every tile manually... but some areas you probably could automate.. limiting the x, y area..
zookeeper wrote:
June 18th, 2018, 5:42 pm
If the map is at all large, then saving the map using nothing but [store_locations] would naturally create a gigantic array.
in which case it may be a bit of a slow lag to replace the terrains, but not necessarily.. However if it's slow you could convert the WML code into lua and make it run much faster, because lua for cycle is much faster than wml cycle. So you could store locations in WML as normal (as in octalot example I used "original" as an array variable name), but then instead of WML [foreach] do something like this:

Code: Select all

[lua]
code = <<
for i=0,wesnoth.get_variable("original.length")-1,1 do
local current_hex_x = wesnoth.get_variable("original["..tostring(i).."].x")
local current_hex_y = wesnoth.get_variable("original["..tostring(i).."].y")
local current_hex_terrain = wesnoth.get_variable("original["..tostring(i).."].terrain")
wesnoth.set_terrain(tonumber(current_hex_x), tonumber(current_hex_y), current_hex_terrain)
end			
>>
[/lua]
and it should make it replace terrains faster.
if it doesn't work let me know...

User avatar
PapaSmurfReloaded
Posts: 608
Joined: November 17th, 2007, 1:10 pm
Location: Argentina

Re: How to load a map from an array of variables?

Post by PapaSmurfReloaded » June 19th, 2018, 2:28 pm

The feature is currently added to ARRPGA.
It doesn't take that long to change the hexes of the entire map (three second max I believe) and ARRPGA uses pretty big maps in general, along the lines of 100 x 100. It isn't a feature that is supposed to be used often that I find it alright. The idea is to allow the DM to be able to add some more flavor to the setting of the story.

As for the replacement, it works as desired. Of course not every maps looks well with changing seasons (like those with "pre-set seasons" like Northern Mountains which is a winter environment. the Desert of Doom which is a dry sand desert (duh). or the tropical Lost Island.

But for mostly greenish maps like Wesnoth, the Far North, the Green Isle, it works fine.

The only issues is with reefs being replaced by ice in Winter(technically the issue is that I don't have anything better to change it into, ice covered by debris looks bad xD) but I find it quite irrelevant.
18th Century Warfare Era, try it out!
A Random RPG Adventure, play it!
Infantry Wars Era, give it a shot!
Zombie Apocalypse RPG, run for your life!

enclave
Posts: 909
Joined: December 15th, 2007, 8:52 am

Re: How to load a map from an array of variables?

Post by enclave » June 19th, 2018, 3:15 pm

PapaSmurfReloaded wrote:
June 19th, 2018, 2:28 pm
It doesn't take that long to change the hexes of the entire map (three second max I believe)
For me I think 3 seconds is quite long, and keep in mind that maybe on your computer it is 3 seconds, but on some old computer it could be 10 :) And it would look like a big lag/freeze, not something with a nice image saying "loading please wait"
I would try to add my lua code to check on difference (I already wrote it so that it will be 100% compatible with octalot WML example), but of course it's up to you.
PapaSmurfReloaded wrote:
June 19th, 2018, 2:28 pm
The only issues is with reefs being replaced by ice in Winter(technically the issue is that I don't have anything better to change it into, ice covered by debris looks bad xD) but I find it quite irrelevant.
Well yeah I dont think its so extremely important to change reefs into proper ice reefs, but while Ai^Es doesn't look too bad at all you could always add [item] with image like "terrain/water/reef2.png" like so:

Code: Select all

[item]
 x,y=25,28
    image="terrain/water/reef.png"
[/item]
and then remove it with:

Code: Select all

[remove_item]
 x,y=25,28
    image="terrain/water/reef.png"
[/remove_item]
And i think item supports standard location filter, it says something like that in wiki... so you could filter it by terrains so possibly something like this would work well:

Code: Select all

[item]
terrain=Wwr
    image="terrain/water/reef.png"
[/item]
[terrain]
terrain=Ai
[and]
terrain=Wwr
[/and]
[/terrain]
Maybe you would have to add some unnoticable terrain overlay too to make it reversible when winter is over...

Never tried to use image on top of ice, not sure how it will look like:) maybe not nice at all :D no idea.

User avatar
PapaSmurfReloaded
Posts: 608
Joined: November 17th, 2007, 1:10 pm
Location: Argentina

Re: How to load a map from an array of variables?

Post by PapaSmurfReloaded » June 19th, 2018, 5:50 pm

enclave wrote:
June 19th, 2018, 3:15 pm
PapaSmurfReloaded wrote:
June 19th, 2018, 2:28 pm
It doesn't take that long to change the hexes of the entire map (three second max I believe)
For me I think 3 seconds is quite long, and keep in mind that maybe on your computer it is 3 seconds, but on some old computer it could be 10 :) And it would look like a big lag/freeze, not something with a nice image saying "loading please wait"
I would try to add my lua code to check on difference (I already wrote it so that it will be 100% compatible with octalot WML example), but of course it's up to you.
My computer is an old computer, and it lags big maps.
ARRPGA is about roleplaying and the game already takes a lot of time anyway.
Three seconds is absolutely nothing compared to narrating the story and unit actions. :lol:

I thought about adding ^Es, but then I would have issue with frozen shallow river tiles that have ^Es overlays, which in ARRPGA maps are far, far way more common than reefs.

I honestly have never delved into LUA, I should sometime. Too lazy. :whistle:
18th Century Warfare Era, try it out!
A Random RPG Adventure, play it!
Infantry Wars Era, give it a shot!
Zombie Apocalypse RPG, run for your life!

enclave
Posts: 909
Joined: December 15th, 2007, 8:52 am

Re: How to load a map from an array of variables?

Post by enclave » June 19th, 2018, 9:13 pm

PapaSmurfReloaded wrote:
June 19th, 2018, 5:50 pm
I honestly have never delved into LUA, I should sometime. Too lazy. :whistle:
well it's quite simple if you ever used any other programming language...
I guess you just never really needed it, I only started using lua because i had no choice :)

Post Reply