Skip to content

Commit 941f60a

Browse files
committed
First pass attempt at getting read_tile to a file
1 parent 9981a6d commit 941f60a

4 files changed

Lines changed: 120 additions & 92 deletions

File tree

control.lua

Lines changed: 30 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ local SpidertronRemote = require("scripts.spidertron-remote")
5858
local TH = require("scripts.table-helpers")
5959
local Teleport = require("scripts.teleport")
6060
local TestFramework = require("scripts.test-framework")
61+
local TileReader = require("scripts.tile-reader")
6162
local TransportBelts = require("scripts.transport-belts")
6263
local TravelTools = require("scripts.travel-tools")
6364
local VirtualTrainDriving = require("scripts.rails.virtual-train-driving")
@@ -208,87 +209,24 @@ function check_for_player(index)
208209
end
209210
end
210211

211-
--refresh_player_tile has been moved to EntitySelection module
212-
213-
-- Lua's version of a forward declaration.
214-
local read_tile_inner
215-
216-
--Reads the cursor tile and reads out the result. If an entity is found, its ent info is read. Otherwise info about the tile itself is read.
217-
function read_tile(pindex, start_text)
218-
local res = read_tile_inner(pindex)
219-
if start_text then table.insert(res, 1, start_text) end
220-
Speech.speak(pindex, FaUtils.localise_cat_table(res))
221-
end
222-
223-
read_tile_inner = function(pindex, start_text)
224-
local result = {}
225-
226-
local tile_name, tile_object = EntitySelection.get_player_tile(pindex)
227-
if not tile_name then return { "Tile uncharted and out of range" } end
212+
---Local helper: Reads tile info and adds build preview info if player is holding a building
213+
---@param pindex integer Player index
214+
---@param start_text string? Optional text to prepend to the result
215+
local function read_tile_with_preview_info(pindex, start_text)
216+
local result = TileReader.read_tile_inner(pindex)
228217

218+
-- Add build preview info if holding a building and tile is empty/has resources
229219
local ent = EntitySelection.get_first_ent_at_tile(pindex)
230-
if not (ent and ent.valid) then
231-
--If there is no ent, read the tile instead
232-
table.insert(result, Localising.get_localised_name_with_fallback(tile_object))
233-
if
234-
tile_name == "water"
235-
or tile_name == "deepwater"
236-
or tile_name == "water-green"
237-
or tile_name == "deepwater-green"
238-
or tile_name == "water-shallow"
239-
or tile_name == "water-mud"
240-
or tile_name == "water-wube"
241-
then
242-
--Identify shores and crevices and so on for water tiles
243-
table.insert(result, FaUtils.identify_water_shores(pindex))
244-
end
245-
Graphics.draw_cursor_highlight(pindex, nil, nil)
246-
game.get_player(pindex).selected = nil
247-
else --laterdo tackle the issue here where entities such as tree stumps block preview info
248-
-- Special handling for rails: announce all rails at this position
249-
if ent.type == "straight-rail" then
250-
local player = game.get_player(pindex)
251-
if player then
252-
local floor_x = math.floor(ent.position.x)
253-
local floor_y = math.floor(ent.position.y)
254-
local search_area = {
255-
{ x = floor_x + 0.001, y = floor_y + 0.001 },
256-
{ x = floor_x + 0.999, y = floor_y + 0.999 },
257-
}
258-
259-
-- Find all rail entities at this position
260-
local rail_entities = player.surface.find_entities_filtered({
261-
area = search_area,
262-
type = { "straight-rail", "curved-rail-a", "curved-rail-b", "half-diagonal-rail" },
263-
})
264-
265-
-- Announce each rail
266-
if #rail_entities > 0 then
267-
for _, rail_entity in ipairs(rail_entities) do
268-
if rail_entity and rail_entity.valid then
269-
table.insert(result, FaInfo.ent_info(pindex, rail_entity))
270-
end
271-
end
272-
end
273-
end
274-
else
275-
table.insert(result, FaInfo.ent_info(pindex, ent))
276-
end
277-
Graphics.draw_cursor_highlight(pindex, ent, nil)
278-
game.get_player(pindex).selected = ent
279-
end
280-
if not ent or ent.type == "resource" then --possible bug here with the h box being a new tile ent
220+
if not ent or ent.type == "resource" then
281221
local stack = game.get_player(pindex).cursor_stack
282-
--Run build preview checks
283222
if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil then
284223
table.insert(result, BuildingTools.build_preview_checks_info(stack, pindex))
285-
--game.get_player(pindex).print(result)--
286224
end
287225
end
288226

289-
--Add info on whether the tile is uncharted or blurred or distant
290-
table.insert(result, Mouse.cursor_visibility_info(pindex))
291-
return result
227+
-- Add optional prefix and speak
228+
if start_text then table.insert(result, 1, start_text) end
229+
Speech.speak(pindex, FaUtils.localise_cat_table(result))
292230
end
293231

294232
--Update the position info and cursor info during smooth walking.
@@ -1263,7 +1201,7 @@ local function move(direction, pindex, nudged)
12631201
storage.players[pindex].position = new_pos
12641202
if nudged ~= true then
12651203
vp:set_cursor_pos(FaUtils.offset_position_legacy(storage.players[pindex].position, direction, 1))
1266-
read_tile(pindex)
1204+
read_tile_with_preview_info(pindex)
12671205
end
12681206

12691207
local stack = first_player.cursor_stack
@@ -1298,7 +1236,7 @@ local function move(direction, pindex, nudged)
12981236
)
12991237
then
13001238
target_mouse_pointer_deprecated(pindex)
1301-
read_tile(pindex)
1239+
read_tile_with_preview_info(pindex)
13021240
end
13031241

13041242
--Rotate belts in hand for build lock Mode
@@ -1369,7 +1307,7 @@ local function cursor_mode_move(direction, pindex, single_only)
13691307
vp:set_cursor_pos_continuous(cursor_pos, direction)
13701308

13711309
EntitySelection.reset_entity_index(pindex)
1372-
read_tile(pindex)
1310+
read_tile_with_preview_info(pindex)
13731311

13741312
--Update drawn cursor
13751313
local stack = p.cursor_stack
@@ -1743,7 +1681,7 @@ local function cursor_skip(pindex, direction, iteration_limit, use_preview_size)
17431681
end
17441682

17451683
--Read the tile reached
1746-
read_tile(pindex, result)
1684+
read_tile_with_preview_info(pindex, result)
17471685
Graphics.sync_build_cursor_graphics(pindex)
17481686
end
17491687

@@ -2033,7 +1971,7 @@ EventManager.on_event(
20331971
"fa-k",
20341972
---@param event EventData.CustomInputEvent
20351973
function(event, pindex)
2036-
-- Check for virtual train driving (status command doesn't need read_tile)
1974+
-- Check for virtual train driving (status command doesn't need TileReader.read_tile)
20371975
if VirtualTrainDriving.on_kb_descriptive_action_name(event) then return end
20381976

20391977
read_coords(pindex)
@@ -2253,7 +2191,7 @@ EventManager.on_event(
22532191
function(event, pindex)
22542192
-- Check for virtual train driving
22552193
if VirtualTrainDriving.on_kb_descriptive_action_name(event) then
2256-
read_tile(pindex)
2194+
TileReader.read_tile(pindex)
22572195
return
22582196
end
22592197

@@ -2371,7 +2309,7 @@ local function toggle_cursor_mode(pindex, muted)
23712309
--Finally, read the new tile
23722310
vp:set_cursor_anchored(false)
23732311
-- For the unanchored case it's worth reading the tile the cursor ended up on.
2374-
if muted ~= true then read_tile(pindex, { "fa.unanchored-cursor" }) end
2312+
if muted ~= true then read_tile_with_preview_info(pindex, { "fa.unanchored-cursor" }) end
23752313
end
23762314
end
23772315

@@ -3024,12 +2962,13 @@ local function kb_click_hand(event)
30242962
elseif proto.place_result or proto.place_as_tile_result then
30252963
-- Item can be placed/built
30262964
local vp = Viewpoint.get_viewpoint(pindex)
3027-
BuildingTools.build_item_in_hand_with_params({
2965+
local success = BuildingTools.build_item_in_hand_with_params({
30282966
pindex = pindex,
30292967
building_direction = vp:get_hand_direction(),
30302968
flip_horizontal = vp:get_flipped_horizontal(),
30312969
flip_vertical = vp:get_flipped_vertical(),
30322970
})
2971+
if success then TileReader.read_tile(pindex) end
30332972
else
30342973
-- Item cannot be built (e.g., intermediate products, tools, etc.)
30352974
-- Could add a message or different action here if needed
@@ -3039,12 +2978,13 @@ local function kb_click_hand(event)
30392978
elseif player.cursor_ghost then
30402979
-- Ghost building
30412980
local vp = Viewpoint.get_viewpoint(pindex)
3042-
BuildingTools.build_item_in_hand_with_params({
2981+
local success = BuildingTools.build_item_in_hand_with_params({
30432982
pindex = pindex,
30442983
building_direction = vp:get_hand_direction(),
30452984
flip_horizontal = vp:get_flipped_horizontal(),
30462985
flip_vertical = vp:get_flipped_vertical(),
30472986
})
2987+
if success then TileReader.read_tile(pindex) end
30482988
end
30492989
end
30502990

@@ -3439,12 +3379,13 @@ EventManager.on_event(
34393379
if proto.place_result or proto.place_as_tile_result then
34403380
-- Item can be placed as a ghost
34413381
local vp = Viewpoint.get_viewpoint(pindex)
3442-
BuildingTools.place_ghost_with_params({
3382+
local success = BuildingTools.place_ghost_with_params({
34433383
pindex = pindex,
34443384
building_direction = vp:get_hand_direction(),
34453385
flip_horizontal = vp:get_flipped_horizontal(),
34463386
flip_vertical = vp:get_flipped_vertical(),
34473387
})
3388+
if success then TileReader.read_tile(pindex) end
34483389
else
34493390
-- Item cannot be built
34503391
Speech.speak(pindex, { "fa.cannot-build-item" })
@@ -4127,7 +4068,7 @@ end
41274068
EventManager.on_event("fa-comma", function(event)
41284069
-- Check for virtual train driving
41294070
if VirtualTrainDriving.on_kb_descriptive_action_name(event) then
4130-
read_tile(event.player_index)
4071+
TileReader.read_tile(event.player_index)
41314072
return
41324073
end
41334074

@@ -4161,7 +4102,7 @@ end, EventManager.EVENT_KIND.WORLD)
41614102
EventManager.on_event("fa-m", function(event)
41624103
-- Check for virtual train driving
41634104
if VirtualTrainDriving.on_kb_descriptive_action_name(event) then
4164-
read_tile(event.player_index)
4105+
TileReader.read_tile(event.player_index)
41654106
return
41664107
end
41674108

@@ -4178,7 +4119,7 @@ EventManager.on_event("fa-m", function(event)
41784119
return
41794120
elseif stack.prototype.type == "spidertron-remote" then
41804121
SpidertronRemote.cycle_spidertrons(player, -1)
4181-
read_tile(pindex)
4122+
TileReader.read_tile(pindex)
41824123
return
41834124
end
41844125
end
@@ -4189,7 +4130,7 @@ end, EventManager.EVENT_KIND.WORLD)
41894130
EventManager.on_event("fa-dot", function(event)
41904131
-- Check for virtual train driving
41914132
if VirtualTrainDriving.on_kb_descriptive_action_name(event) then
4192-
read_tile(event.player_index)
4133+
TileReader.read_tile(event.player_index)
41934134
return
41944135
end
41954136

@@ -4206,7 +4147,7 @@ EventManager.on_event("fa-dot", function(event)
42064147
return
42074148
elseif stack.prototype.type == "spidertron-remote" then
42084149
SpidertronRemote.cycle_spidertrons(player, 1)
4209-
read_tile(pindex)
4150+
TileReader.read_tile(pindex)
42104151
return
42114152
end
42124153
end
@@ -4270,7 +4211,7 @@ end, EventManager.EVENT_KIND.WORLD)
42704211
EventManager.on_event("fa-backspace", function(event)
42714212
-- Check for virtual train driving
42724213
if VirtualTrainDriving.on_kb_descriptive_action_name(event) then
4273-
read_tile(event.player_index)
4214+
TileReader.read_tile(event.player_index)
42744215
return
42754216
end
42764217

scripts/building-tools.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ function mod.build_item_in_hand_with_params(params)
217217

218218
if p.can_build_from_cursor(building) then
219219
p.build_from_cursor(building)
220-
read_tile(pindex)
221220
return true
222221
else
223222
-- Report errors (if enabled)
@@ -356,7 +355,6 @@ function mod.place_ghost_with_params(params)
356355

357356
if ghost then
358357
Speech.speak(pindex, { "fa.building-placed-ghost", { "entity-name." .. decision.entity_name } })
359-
read_tile(pindex)
360358
return true
361359
else
362360
-- Ghost placement failed

scripts/tile-reader.lua

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
--Here: Reading tiles and entities at the cursor position
2+
3+
local EntitySelection = require("scripts.entity-selection")
4+
local FaInfo = require("scripts.fa-info")
5+
local FaUtils = require("scripts.fa-utils")
6+
local Graphics = require("scripts.graphics")
7+
local Localising = require("scripts.localising")
8+
local Mouse = require("scripts.mouse")
9+
local Speech = require("scripts.speech")
10+
11+
local mod = {}
12+
13+
---Reads the tile at the cursor position and speaks the result
14+
---@param pindex integer Player index
15+
---@param start_text string? Optional text to prepend to the result
16+
function mod.read_tile(pindex, start_text)
17+
local res = mod.read_tile_inner(pindex)
18+
if start_text then table.insert(res, 1, start_text) end
19+
Speech.speak(pindex, FaUtils.localise_cat_table(res))
20+
end
21+
22+
---Internal function that gathers information about the tile at the cursor position
23+
---@param pindex integer Player index
24+
---@return table Array of localized strings describing the tile
25+
function mod.read_tile_inner(pindex)
26+
local result = {}
27+
28+
local tile_name, tile_object = EntitySelection.get_player_tile(pindex)
29+
if not tile_name then return { "Tile uncharted and out of range" } end
30+
31+
local ent = EntitySelection.get_first_ent_at_tile(pindex)
32+
if not (ent and ent.valid) then
33+
--If there is no ent, read the tile instead
34+
table.insert(result, Localising.get_localised_name_with_fallback(tile_object))
35+
if
36+
tile_name == "water"
37+
or tile_name == "deepwater"
38+
or tile_name == "water-green"
39+
or tile_name == "deepwater-green"
40+
or tile_name == "water-shallow"
41+
or tile_name == "water-mud"
42+
or tile_name == "water-wube"
43+
then
44+
--Identify shores and crevices and so on for water tiles
45+
table.insert(result, FaUtils.identify_water_shores(pindex))
46+
end
47+
Graphics.draw_cursor_highlight(pindex, nil, nil)
48+
game.get_player(pindex).selected = nil
49+
else --laterdo tackle the issue here where entities such as tree stumps block preview info
50+
-- Special handling for rails: announce all rails at this position
51+
if ent.type == "straight-rail" then
52+
local player = game.get_player(pindex)
53+
if player then
54+
local floor_x = math.floor(ent.position.x)
55+
local floor_y = math.floor(ent.position.y)
56+
local search_area = {
57+
{ x = floor_x + 0.001, y = floor_y + 0.001 },
58+
{ x = floor_x + 0.999, y = floor_y + 0.999 },
59+
}
60+
61+
-- Find all rail entities at this position
62+
local rail_entities = player.surface.find_entities_filtered({
63+
area = search_area,
64+
type = { "straight-rail", "curved-rail-a", "curved-rail-b", "half-diagonal-rail" },
65+
})
66+
67+
-- Announce each rail
68+
if #rail_entities > 0 then
69+
for _, rail_entity in ipairs(rail_entities) do
70+
if rail_entity and rail_entity.valid then
71+
table.insert(result, FaInfo.ent_info(pindex, rail_entity))
72+
end
73+
end
74+
end
75+
end
76+
else
77+
table.insert(result, FaInfo.ent_info(pindex, ent))
78+
end
79+
Graphics.draw_cursor_highlight(pindex, ent, nil)
80+
game.get_player(pindex).selected = ent
81+
end
82+
83+
--Add info on whether the tile is uncharted or blurred or distant
84+
table.insert(result, Mouse.cursor_visibility_info(pindex))
85+
return result
86+
end
87+
88+
return mod

scripts/walking.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ local FaUtils = require("scripts.fa-utils")
2121
local EntitySelection = require("scripts.entity-selection")
2222
local Graphics = require("scripts.graphics")
2323
local Sounds = require("scripts.ui.sounds")
24+
local TileReader = require("scripts.tile-reader")
2425

2526
local mod = {}
2627

@@ -99,7 +100,7 @@ function mod.process_walking_announcements(pindex)
99100
player.selected = nil
100101
end
101102

102-
read_tile(pindex)
103+
TileReader.read_tile(pindex)
103104
else
104105
Graphics.draw_cursor_highlight(pindex, nil, nil)
105106
player.selected = nil

0 commit comments

Comments
 (0)