@@ -58,6 +58,7 @@ local SpidertronRemote = require("scripts.spidertron-remote")
5858local TH = require (" scripts.table-helpers" )
5959local Teleport = require (" scripts.teleport" )
6060local TestFramework = require (" scripts.test-framework" )
61+ local TileReader = require (" scripts.tile-reader" )
6162local TransportBelts = require (" scripts.transport-belts" )
6263local TravelTools = require (" scripts.travel-tools" )
6364local VirtualTrainDriving = require (" scripts.rails.virtual-train-driving" )
@@ -208,87 +209,24 @@ function check_for_player(index)
208209 end
209210end
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 ))
292230end
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 )
17481686end
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
23762314end
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
30492989end
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
41274068EventManager .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)
41614102EventManager .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)
41894130EventManager .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)
42704211EventManager .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
0 commit comments