Latest Posts

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

kaputtnik
Avatar
Topic Opener
Joined: 2013-02-18, 20:48
Posts: 2227
OS: Archlinux
Version: current master
Ranking
One Elder of Players
Location: Germany
Posted at: 2023-01-08, 11: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:
https://wide-fighter.netlify.app/

Top Quote
Nordfriese
Avatar
Joined: 2017-01-17, 18:07
Posts: 1747
OS: Ubuntu MATE 22.10
Version: Latest master
Ranking
One Elder of Players
Location: 0x55555d3a34c0
Posted at: 2023-01-08, 12:11

Easy enough to achieve… https://github.com/widelands/widelands/pull/5739

Functions like region and ln delegate to the C++ map backend which does all this low-level arithmetic/magic for us, see https://github.com/widelands/widelands/blob/3571958f8f45132bfcf691e0087117ccf1cb603d/src/logic/map.h#L698-L1356


Top Quote