Skip to content

Commit

Permalink
Restore playing the scan pulse
Browse files Browse the repository at this point in the history
  • Loading branch information
ahicks92 committed Oct 5, 2024
1 parent 4a63df7 commit 8882a96
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,8 @@ end

--Called every tick. Used to call scheduled and repeated functions.
function on_tick(event)
ScannerEntrypoint.on_tick()

if global.scheduled_events[event.tick] then
for _, to_call in pairs(global.scheduled_events[event.tick]) do
_G[to_call[1]](to_call[2], to_call[3], to_call[4])
Expand Down
32 changes: 28 additions & 4 deletions scripts/scanner/entrypoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ local mod = {}
---@field scanner_cursor fa.scanner.CursorPos
---@field surface LuaSurface
---@field entries table<fa.scanner.Category, fa.scanner.SubcategoryData[]>
---@field pending_refresh_counter number? used to delay refreshes by a tick.
---@field pending_direction_filter defines.direction?

---@returns fa.scanner.GlobalPlayerState
local function new_player_state(pindex)
Expand All @@ -53,6 +55,8 @@ local function new_player_state(pindex)
},
surface = player.surface,
entries = {},
pending_refresh_counter = nil,
pending_direction_filter = nil,
}

return ret
Expand Down Expand Up @@ -81,10 +85,7 @@ local function apply_sort(player, pstate)
end
end

-- Given a pindex, refresh the player's local view over a scan.
---@param pindex number
---@param direction_filter defines.direction?
function mod.do_refresh(pindex, direction_filter)
local function do_refresh_after_sfx(pindex, direction_filter)
-- Step 1: drop the player's data entirely, but keep the category cursor
-- settings. We can't patch subcategories back in: they're a result of the
-- scan refresh.
Expand Down Expand Up @@ -202,6 +203,16 @@ is_chunk_charted(surface, { x = ex / 32, y = ey / 32 })
end
end

-- Given a pindex, refresh the player's local view over a scan.
---@param pindex number
---@param direction_filter defines.direction?
function mod.do_refresh(pindex, direction_filter)
game.get_player(pindex).play_sound({ path = "scanner-pulse" })

player_state[pindex].pending_refresh_counter = 2
player_state[pindex].pending_direction_filter = direction_filter
end

-- Sentinel tokens to represent not having moved, and to say which end.

---@alias fa.scanner.AT_BEGINNING "at_beginning"
Expand Down Expand Up @@ -523,4 +534,17 @@ function mod.on_surface_delete(index)
SurfaceScanner.on_surface_delete(index)
end

function mod.on_tick()
for pindex, state in pairs(player_state) do
if state.pending_refresh_counter then
state.pending_refresh_counter = state.pending_refresh_counter - 1
if state.pending_refresh_counter == 0 then
do_refresh_after_sfx(pindex, state.pending_direction_filter)
state.pending_refresh_counter = nil
state.pending_direction_filter = nil
end
end
end
end

return mod

0 comments on commit 8882a96

Please sign in to comment.