fix(ludic.minimap): minimap_pins_near refills a list the state keeps - the compass asked every frame and got a new list each time
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6e132d25c8
commit
968030535e
4 changed files with 19 additions and 3 deletions
|
|
@ -60,7 +60,7 @@ export port MinimapWorld { # every member has a default
|
|||
| `minimap_pin_count()`, `minimap_pin_max()`, `minimap_pin_full()`, `minimap_pin(i) -> MinimapPin` | the marks |
|
||||
| `minimap_pin_set_label/icon/colour(i, v)`, `minimap_pin_last_icon()` | editing one |
|
||||
| `minimap_pin_open()`, `minimap_pin_select(i)`, `minimap_pin_followed()`, `minimap_pin_follow(i)`, `minimap_pin_follow_toggle()` | the open one and the followed one |
|
||||
| `minimap_pins_near(x, z, n, within) -> []int` | the nearest marks, not the followed one (for a compass) |
|
||||
| `minimap_pins_near(x, z, n, within) -> []int` | the nearest marks, not the followed one (for a compass), in a list the state keeps and refills: read it, do not keep it |
|
||||
| `minimap_config_view(span, zoom_min, zoom_max, focus)`, `minimap_window(x0, y0, size)` | the view's range, and where the window is this frame |
|
||||
| `minimap_px(x)`, `minimap_py(z)`, `minimap_wx(rx)`, `minimap_wz(ry)`, `minimap_span()`, `minimap_scale()`, `minimap_scale_metres()` | projection, and a scale bar's round distance |
|
||||
| `minimap_zoom()`, `minimap_zoom_set(z)`, `minimap_zoom_step(dir)`, `minimap_zoom_about(rx, ry, factor)` | zoom |
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
# grid.ludic - the explored grid: square cells round the origin, and the part of it "explored" is
|
||||
# measured over (a core, so a bigger grid does not change what a share of it means)
|
||||
export state MinimapState {
|
||||
mm_near: []int = new []int # minimap_pins_near's answer, cleared and refilled: a compass asks every frame
|
||||
mm_land_n: int = 0 # land cells in the core, counted once per origin
|
||||
mm_land_ox: float = 0.0
|
||||
mm_land_oz: float = 0.0
|
||||
|
|
|
|||
|
|
@ -71,8 +71,9 @@ export function minimap_pin(minimap_st: MinimapState, i: int) -> MinimapPin {
|
|||
}
|
||||
|
||||
# the nearest n marks within `within` of (x, z), nearest first, leaving out the followed one
|
||||
export function minimap_pins_near(minimap_st: MinimapState, x: float, z: float, n: int, within: float) -> []int {
|
||||
let out = new []int
|
||||
export function minimap_pins_near(minimap_st: mut MinimapState, x: float, z: float, n: int, within: float) -> []int {
|
||||
let out = minimap_st.mm_near
|
||||
List.clear(out)
|
||||
for k in 0 .. n {
|
||||
var best = -1
|
||||
var bd = within * within
|
||||
|
|
|
|||
|
|
@ -111,6 +111,20 @@ program MinimapTest {
|
|||
expect_eq(near[1], 1)
|
||||
}
|
||||
|
||||
test "asking for the nearest marks every frame allocates nothing" (minimap_st: mut MinimapState, minimap_test_st: mut MinimapTestState) {
|
||||
fresh(minimap_st, minimap_test_st)
|
||||
minimap_config_pins(minimap_st, 8, 8, 5)
|
||||
minimap_pin_add(minimap_st, 110.0, 100.0, 0, "near")
|
||||
minimap_pin_add(minimap_st, 130.0, 100.0, 0, "next")
|
||||
minimap_pins_near(minimap_st, 100.0, 100.0, 2, 400.0)
|
||||
let before = Os.heap_bytes()
|
||||
for k in 0 .. 1000 { minimap_pins_near(minimap_st, 100.0 + float(k % 7), 100.0, 2, 400.0) }
|
||||
let grew = Os.heap_bytes() - before
|
||||
print(`minimap: 1000 asks, heap {grew} bytes`)
|
||||
expect(grew == long(0))
|
||||
expect_eq(len(minimap_pins_near(minimap_st, 100.0, 100.0, 2, 400.0)), 2)
|
||||
}
|
||||
|
||||
test "the view: the world on the window and back, zoom about a point, a pan, focus and the clamp" (minimap_st: mut MinimapState, minimap_test_st: mut MinimapTestState) {
|
||||
fresh(minimap_st, minimap_test_st)
|
||||
minimap_centre(minimap_st, 100.0, 100.0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue