Currently Online

Latest Posts

Topic: map boundaries and map.get_field(x,y)

Topic Opener
Joined: 2013-02-18, 19:48
Posts: 2480
OS: Archlinux
Version: current master
One Elder of Players
Location: Germany
Posted at: 2023-01-08, 10:12

When calculating fields manually one has to check always if the calculated value does not exceed map.width() or is < 0 to get a valid field with map:get_field(x,y). Example for mirroring an existing field at a y-axis:

local map = wl.Game().map
local f = map.get_field(0,0)

local mirrored_field = map.get_field(f.x, f.y - 3)    -- doesn't work because f.y = 0-3 = -3

To get around this one has to create a new Lua function which takes care if the value isn't correct and call this function first, e.g.

local new_y = check_map_wrap(f.y - 3)                -- on a map with height 64 it will return 61
local mirrored_field = map.get_field(f.x, new_y)     -- all fine

Wouldn't it be possible to have such a recalculating automatically in map:get_field(x,y)? Or create another function map:get_map_field(x,y) wich takes care of map boundaries?

Somewhere in the code must be such a check, e.g. when calling field:region() or using field.ln (left neighbor field) the result takes care of map boundaries. But i was not able to find such recalculating in the code face-sad.png

Fight simulator for Widelands:

Top Quote
Joined: 2017-01-17, 17:07
Posts: 1985
OS: Debian Testing
Version: Latest master
One Elder of Players
Location: 0x55555d3a34c0
Posted at: 2023-01-08, 11:11

Easy enough to achieve…

Functions like region and ln delegate to the C++ map backend which does all this low-level arithmetic/magic for us, see

Top Quote