local map = ...
local game = map:get_game()

function map:on_opening_transition_finished()
  local mov = sol.movement.create("straight")
  mov:set_speed(50)
  mov:start(ent)

  -- Apply a sequence of speed changes including very low speeds.
  -- Before the fix, repeatedly calling set_speed while the entity was
  -- mid-period could push next_move_date further into the future each
  -- time, causing the entity to freeze for several seconds.
  local speeds = { 50, 0.5, 0.3, 0.1, 50 }
  local step = 0

  sol.timer.start(map, 100, function()
    step = step + 1
    mov:set_speed(speeds[step])

    if step < #speeds then
      return true
    end

    -- After restoring to speed 50, the entity must move within one period
    -- (~20 ms). Allow a generous 500 ms window to account for timing.
    local x0, y0 = ent:get_position()
    sol.timer.start(map, 500, function()
      local x1, y1 = ent:get_position()
      assert(x1 ~= x0 or y1 ~= y0,
        "Entity is frozen after repeated set_speed calls (x=" .. x0 .. ")")
      sol.main.exit()
    end)
    return false
  end)
end
