forked from Crimso777/Factorio-Access
-
Notifications
You must be signed in to change notification settings - Fork 9
/
control.lua
8780 lines (8199 loc) · 360 KB
/
control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--Main file for mod runtime
local util = require("util")
local fa_utils = require("scripts.fa-utils")
local fa_info = require("scripts.fa-info")
local fa_localising = require("scripts.localising")
local fa_crafting = require("scripts.crafting")
local fa_electrical = require("scripts.electrical")
local fa_equipment = require("scripts.equipment")
local fa_combat = require("scripts.combat")
local fa_graphics = require("scripts.graphics")
local fa_mouse = require("scripts.mouse")
local fa_tutorial = require("scripts.tutorial-system")
local fa_sectors = require("scripts.building-vehicle-sectors")
local fa_menu_search = require("scripts.menu-search")
local fa_building_tools = require("scripts.building-tools")
local fa_mining_tools = require("scripts.mining-tools")
local fa_rails = require("scripts.rails")
local fa_rail_builder = require("scripts.rail-builder")
local fa_trains = require("scripts.trains")
local fa_train_stops = require("scripts.train-stops")
local fa_driving = require("scripts.driving")
local fa_spidertrons = require("scripts.spidertron")
local fa_belts = require("scripts.transport-belts")
local fa_zoom = require("scripts.zoom")
local fa_bot_logistics = require("scripts.worker-robots")
local fa_blueprints = require("scripts.blueprints")
local fa_travel = require("scripts.travel-tools")
local fa_teleport = require("scripts.teleport")
local fa_warnings = require("scripts.warnings")
local fa_circuits = require("scripts.circuit-networks")
local fa_kk = require("scripts.kruise-kontrol-wrapper")
local fa_quickbar = require("scripts.quickbar")
local Consts = require("scripts.consts")
local Rulers = require("scripts.rulers")
local ScannerEntrypoint = require("scripts.scanner.entrypoint")
local WorkQueue = require("scripts.work-queue")
---@meta scripts.shared-types
groups = {}
entity_types = {}
production_types = {}
building_types = {}
local dirs = defines.direction
ENT_NAMES_CLEARED_AS_OBSTACLES = {
"tree-01-stump",
"tree-02-stump",
"tree-03-stump",
"tree-04-stump",
"tree-05-stump",
"tree-06-stump",
"tree-07-stump",
"tree-08-stump",
"tree-09-stump",
"small-scorchmark",
"small-scorchmark-tintable",
"medium-scorchmark",
"medium-scorchmark-tintable",
"big-scorchmark",
"big-scorchmark-tintable",
"huge-scorchmark",
"huge-scorchmark-tintable",
"rock-big",
"rock-huge",
"sand-rock-big",
}
ENT_TYPES_YOU_CAN_WALK_OVER = {
"resource",
"transport-belt",
"underground-belt",
"splitter",
"item-entity",
"entity-ghost",
"heat-pipe",
"pipe",
"pipe-to-ground",
"character",
"rail-signal",
"flying-text",
"highlight-box",
"combat-robot",
"logistic-robot",
"construction-robot",
"rocket-silo-rocket-shadow",
}
ENT_TYPES_YOU_CAN_BUILD_OVER = {
"resource",
"entity-ghost",
"flying-text",
"highlight-box",
"combat-robot",
"logistic-robot",
"construction-robot",
"rocket-silo-rocket-shadow",
}
EXCLUDED_ENT_NAMES = { "highlight-box", "flying-text" }
WALKING = {
TELESTEP = 0,
STEP_BY_WALK = 1,
SMOOTH = 2,
}
--This function gets scheduled.
function call_to_fix_zoom(pindex)
fa_zoom.fix_zoom(pindex)
end
--This function gets scheduled.
function call_to_sync_graphics(pindex)
fa_graphics.sync_build_cursor_graphics(pindex)
end
--This function gets scheduled.
function call_to_restore_equipped_atomic_bombs(pindex)
fa_equipment.restore_equipped_atomic_bombs(pindex)
end
--This function gets scheduled.
function call_to_check_ghost_rails(pindex)
fa_rails.check_ghost_rail_planning_results(pindex)
end
--Define primary ents, which are ents that show up first when reading tiles.
--Notably, the definition is done by listing which types count as secondary.
function ent_is_primary(ent, pindex)
return ent.type ~= "logistic-robot"
and ent.type ~= "construction-robot"
and ent.type ~= "combat-robot"
and ent.type ~= "corpse"
and ent.type ~= "rocket-silo-rocket-shadow"
and ent.type ~= "resource"
and (ent.type ~= "character" or ent.player ~= pindex)
end
-- Sorts a list of entities by bringing primary entities to the start
function sort_ents_by_primary_first(ents)
table.sort(ents, function(a, b)
-- Return false if either are invalid
if a == nil or a.valid == false then return false end
if b == nil or b.valid == false then return false end
-- Check if primary
local a_is_primary = ent_is_primary(a, pindex)
local b_is_primary = ent_is_primary(b, pindex)
--For rails, check if end rail
local a_is_end_rail = false
local b_is_end_rail = false
if a.name == "straight-rail" or a.name == "curved-rail" then
local is_end_rail, dir, comment = fa_rails.check_end_rail(a, pindex)
a_is_end_rail = is_end_rail
end
if b.name == "straight-rail" or b.name == "curved-rail" then
local is_end_rail, dir, comment = fa_rails.check_end_rail(b, pindex)
b_is_end_rail = is_end_rail
end
if a_is_end_rail and not b_is_end_rail then return true end
-- Both or none are primary
if a_is_primary == b_is_primary then return false end
-- a is primary while b is not
if a_is_primary then return true end
-- b is primary while a is not
return false
end)
end
--Get the first entity at a tile
--The entity list is sorted to have primary entities first, so a primary entity is expected.
function get_first_ent_at_tile(pindex)
local ents = players[pindex].tile.ents
--Return nil for an empty ents list
if ents == nil or #ents == 0 then return nil end
--Attempt to find the next ent (init to end)
for i = 1, #ents, 1 do
current = ents[i]
if current and current.valid then
players[pindex].tile.ent_index = i
players[pindex].tile.last_returned_index = i
return current
end
end
--By this point there are no valid ents
return nil
end
--Get the next entity at this tile and note its index.
--The tile entity list is already sorted such that primary ents are listed first.
function get_next_ent_at_tile(pindex)
local ents = players[pindex].tile.ents
local init_index = players[pindex].tile.ent_index
local last_returned_index = players[pindex].tile.last_returned_index
local current = ents[init_index]
--Return nil for an empty ents list
if ents == nil or #ents == 0 then return nil end
--Attempt to find the next ent (init to end)
for i = init_index, #ents, 1 do
current = ents[i]
if current and current.valid then
--If this is not a repeat then return it
if last_returned_index == 0 or last_returned_index ~= i then
players[pindex].tile.ent_index = i
players[pindex].tile.last_returned_index = i
return current
end
end
end
--Return nil to get the tile info instead
if last_returned_index ~= 0 then
players[pindex].tile.ent_index = 0
players[pindex].tile.last_returned_index = 0
return nil
end
--Attempt to find the next ent (start to init)
for i = 1, init_index - 1, 1 do
current = ents[i]
if current and current.valid then
--If this is not a repeat then return it
if last_returned_index == 0 or last_returned_index ~= i then
players[pindex].tile.ent_index = i
players[pindex].tile.last_returned_index = i
return current
end
end
end
--By this point there are no valid ents
players[pindex].tile.ent_index = 0
players[pindex].tile.last_returned_index = 0
return nil
end
--- Produce an iterator over all valid entities for a player's selected tile,
-- while filtering out the player themselves.
local function iterate_selected_ents(pindex)
local tile = players[pindex].tile
local ents = tile.ents
local i = 1
local next_fn
next_fn = function()
-- Ignore all entities that are a character belonging to this player. It
-- should only be one, but we don't mutate so we don't know.
while i <= #ents do
local ent = ents[i]
i = i + 1
if ent and ent.valid then
if ent.type ~= "character" or ent.player ~= pindex then return ent end
end
end
return nil
end
return next_fn, nil, nil
end
--???
function prune_item_groups(array)
if #groups == 0 then
local dict = game.item_prototypes
local a = fa_utils.get_iterable_array(dict)
for i, v in ipairs(a) do
local check1 = true
local check2 = true
for i1, v1 in ipairs(groups) do
if v1.name == v.group.name then check1 = false end
if v1.name == v.subgroup.name then check2 = false end
end
if check1 then table.insert(groups, v.group) end
if check2 then table.insert(groups, v.subgroup) end
end
end
local i = 1
while i < #array and array ~= nil and array[i] ~= nil do
local check = true
for i1, v in ipairs(groups) do
if v ~= nil and array[i].name == v.name then
i = i + 1
check = false
break
end
end
if check then table.remove(array, i) end
end
end
function read_item_selector_slot(pindex, start_phrase)
start_phrase = start_phrase or ""
printout(start_phrase .. players[pindex].item_cache[players[pindex].item_selector.index].name, pindex)
end
--Reads the selected player inventory's selected menu slot. Default is to read the main inventory.
function read_inventory_slot(pindex, start_phrase_in, inv_in)
local p = game.get_player(pindex)
local result = start_phrase_in or ""
local index = players[pindex].inventory.index
local inv = inv_in or players[pindex].inventory.lua_inventory
if index < 1 then
index = 1
elseif index > #inv then
index = #inv
end
players[pindex].inventory.index = index
local stack = inv[index]
if stack == nil or not stack.valid_for_read then
--Label it as an empty slot
result = result .. "Empty Slot"
--Check if the empty slot has a filter set
local filter_name = p.get_main_inventory().get_filter(index)
if filter_name ~= nil then
result = result .. " filtered for " .. filter_name --laterdo localise this name
end
printout(result, pindex)
return
end
if stack.is_blueprint then
printout(fa_blueprints.get_blueprint_info(stack, false, pindex), pindex)
elseif stack.is_blueprint_book then
printout(fa_blueprints.get_blueprint_book_info(stack, false), pindex)
elseif stack.valid_for_read then
--Check if the slot is filtered
local filter_name = p.get_main_inventory().get_filter(index)
if filter_name ~= nil then result = result .. " filtered " end
--Check if the stack has damage
if stack.health < 1 then result = result .. " damaged " end
result = result
.. fa_localising.get(stack, pindex)
.. " x "
.. stack.count
.. " "
.. stack.prototype.subgroup.name
printout(result, pindex)
end
end
--Reads the item in hand, its facing direction if applicable, its count, and its total count including units in the main inventory.
function read_hand(pindex)
if players[pindex].skip_read_hand == true then
players[pindex].skip_read_hand = false
return
end
local cursor_stack = game.get_player(pindex).cursor_stack
local cursor_ghost = game.get_player(pindex).cursor_ghost
if cursor_stack and cursor_stack.valid_for_read then
if cursor_stack.is_blueprint then
--Blueprint extra info
printout(fa_blueprints.get_blueprint_info(cursor_stack, true, pindex), pindex)
elseif cursor_stack.is_blueprint_book then
printout(fa_blueprints.get_blueprint_book_info(cursor_stack, true), pindex)
elseif cursor_stack.name == "spidertron-remote" then
local remote_info = ""
if cursor_stack.connected_entity == nil then
remote_info = " not linked "
else
if cursor_stack.connected_entity.entity_label == nil then
remote_info = " for unlabelled spidertron "
else
remote_info = " for spidertron " .. cursor_stack.connected_entity.entity_label
end
end
printout(fa_localising.get(cursor_stack, pindex) .. remote_info, pindex)
else
--Any other valid item
local out = { "fa.cursor-description" }
table.insert(out, cursor_stack.prototype.localised_name)
local build_entity = cursor_stack.prototype.place_result
if build_entity and build_entity.supports_direction then
table.insert(out, 1)
table.insert(out, { "fa.facing-direction", players[pindex].building_direction })
else
table.insert(out, 0)
table.insert(out, "")
end
table.insert(out, cursor_stack.count)
local extra = game.get_player(pindex).get_main_inventory().get_item_count(cursor_stack.name)
if extra > 0 then
table.insert(out, cursor_stack.count + extra)
else
table.insert(out, 0)
end
printout(out, pindex)
end
elseif cursor_ghost ~= nil then
--Any ghost
local out = { "fa.cursor-description" }
table.insert(out, cursor_ghost.localised_name)
local build_entity = cursor_ghost.place_result
if build_entity and build_entity.supports_direction then
table.insert(out, 1)
table.insert(out, { "fa.facing-direction", players[pindex].building_direction })
else
table.insert(out, 0)
table.insert(out, "")
end
table.insert(out, 0)
local extra = 0
if extra > 0 then
table.insert(out, cursor_stack.count + extra)
else
table.insert(out, 0)
end
printout(out, pindex)
else
printout({ "fa.empty_cursor" }, pindex)
end
end
--Clears the item in hand and then locates it from the first found player inventory slot. laterdo can use API:player.hand_location in the future if it has advantages
function locate_hand_in_player_inventory(pindex)
local p = game.get_player(pindex)
local inv = p.get_main_inventory()
local stack = p.cursor_stack
if p.cursor_stack_temporary then
printout("This item is temporary", pindex)
return
end
--Check if stack empty and menu supported
if stack == nil or not stack.valid_for_read or not stack.valid then
--Hand is empty
return
end
if players[pindex].in_menu and players[pindex].menu ~= "inventory" then
--Unsupported menu type, laterdo add support for building menu and closing the menu with a call
printout("Another menu is open.", pindex)
return
end
if not players[pindex].in_menu then
--Open the inventory if nothing is open
players[pindex].in_menu = true
players[pindex].menu = "inventory"
p.opened = p
end
--Save the hand stack item name
local item_name = stack.name
--Empty hand stack (clear cursor stack)
players[pindex].skip_read_hand = true
local successful = p.clear_cursor()
if not successful then
local message = "Unable to empty hand"
if inv.count_empty_stacks() == 0 then message = message .. ", inventory full" end
printout(message, pindex)
return
end
--Iterate the inventory until you find the matching item name's index
local found = false
local i = 0
while not found and i < #inv do
i = i + 1
if inv[i] and inv[i].valid_for_read and inv[i].name == item_name then found = true end
end
--If found, read it from the inventory
if not found then
printout("Error: " .. fa_localising.get(stack, pindex) .. " not found in player inventory", pindex)
return
else
players[pindex].inventory.index = i
read_inventory_slot(pindex, "inventory ")
end
end
--Clears the item in hand and then locates it from the first found building output slot
function locate_hand_in_building_output_inventory(pindex)
local p = game.get_player(pindex)
local inv = nil
local stack = p.cursor_stack
local pb = players[pindex].building
if p.cursor_stack_temporary then
printout("This item is temporary", pindex)
return
end
if stack.is_blueprint or stack.is_blueprint_book or stack.is_deconstruction_item or stack.is_upgrade_item then
return
end
--Check if stack empty and menu supported
if stack == nil or not stack.valid_for_read or not stack.valid then
--Hand is empty
return
end
if
players[pindex].in_menu
and (players[pindex].menu == "building" or players[pindex].menu == "vehicle")
and pb.sectors
and pb.sectors[pb.sector]
and pb.sectors[pb.sector].name == "Output"
then
inv = p.opened.get_output_inventory()
else
--Unsupported menu type
return
end
--Save the hand stack item name
local item_name = stack.name
--Empty hand stack (clear cursor stack)
players[pindex].skip_read_hand = true
local successful = p.clear_cursor()
if not successful then
local message = "Unable to empty hand"
if inv.count_empty_stacks() == 0 then message = message .. ", inventory full" end
printout(message, pindex)
return
end
--Iterate the inventory until you find the matching item name's index
local found = false
local i = 0
while not found and i < #inv do
i = i + 1
if inv[i] and inv[i].valid_for_read and inv[i].name == item_name then found = true end
end
--If found, read it from the inventory
if not found then
printout(fa_localising.get(stack, pindex) .. " not found in building output", pindex)
return
else
players[pindex].building.index = i
fa_sectors.read_sector_slot(pindex, false)
end
end
--Clears the item in hand and then locates its recipe from the crafting menu. Closes some other menus, does not run in some other menus, uses the menu search function.
function locate_hand_in_crafting_menu(pindex)
local p = game.get_player(pindex)
local inv = p.get_main_inventory()
local stack = p.cursor_stack
if
p.cursor_stack_temporary
or stack.is_blueprint
or stack.is_blueprint_book
or stack.is_deconstruction_item
or stack.is_upgrade_item
then
printout("This item cannot be crafted", pindex)
return
end
--Check if stack empty and menu supported
if stack == nil or not stack.valid_for_read or not stack.valid then
--Hand is empty
return
end
if
players[pindex].in_menu
and players[pindex].menu ~= "inventory"
and players[pindex].menu ~= "building"
and players[pindex].menu ~= "crafting"
then
--Unsupported menu types...
printout("Another menu is open.", pindex)
return
end
--Open the crafting Menu
close_menu_resets(pindex)
players[pindex].in_menu = true
players[pindex].menu = "crafting"
p.opened = p
--Get the name
local item_name = string.lower(
fa_utils.get_substring_before_space(
fa_utils.get_substring_before_dash(fa_localising.get(stack.prototype, pindex))
)
)
players[pindex].menu_search_term = item_name
--Empty hand stack (clear cursor stack) after getting the name
players[pindex].skip_read_hand = true
local successful = p.clear_cursor()
if not successful then
local message = "Unable to empty hand"
if inv.count_empty_stacks() == 0 then message = message .. ", inventory full" end
printout(message, pindex)
return
end
--Run the search
fa_menu_search.fetch_next(pindex, item_name, nil)
end
--If there is an entity at the cursor, moves the mouse pointer to it, else moves to the cursor tile.
--TODO: remove this, by calling the appropriate mouse module functions instead.
function target_mouse_pointer_deprecated(pindex)
if players[pindex].vanilla_mode then return end
local surf = game.get_player(pindex).surface
local ents = surf.find_entities_filtered({ position = players[pindex].cursor_pos })
if ents and ents[1] and ents[1].valid then
fa_mouse.move_mouse_pointer(ents[1].position, pindex)
else
fa_mouse.move_mouse_pointer(players[pindex].cursor_pos, pindex)
end
end
--Used when a tile has multiple overlapping entities. Reads out the next entity.
function tile_cycle(pindex)
local ent = get_next_ent_at_tile(pindex)
if ent and ent.valid then
printout(fa_info.ent_info(pindex, ent, ""), pindex)
game.get_player(pindex).selected = ent
else
printout(players[pindex].tile.tile, pindex)
end
end
--Checks if the global players table has been created, and if the table entry for this player exists. Otherwise it is initialized.
function check_for_player(index)
if not players then
global.players = global.players or {}
players = global.players
end
if players[index] == nil then
initialize(game.get_player(index))
return false
else
return true
end
end
--Prints a string to the Factorio Access Launcher app for the vocalizer to read out.
function printout(str, pindex)
if pindex ~= nil and pindex > 0 then
players[pindex].last = str
else
return
end
if players[pindex].vanilla_mode == nil then players[pindex].vanilla_mode = false end
if not players[pindex].vanilla_mode then localised_print({ "", "out " .. pindex .. " ", str }) end
end
--Reprints the last sent string to the Factorio Access Launcher app for the vocalizer to read out.
function repeat_last_spoken(pindex)
printout(players[pindex].last, pindex)
end
-- Force the mod to disable/reset nall cursor modes. Useful for KK.
function force_cursor_off(pindex)
local p = game.get_player(pindex)
--Disable
players[pindex].cursor = false
players[pindex].cursor_pos = fa_utils.offset_position(players[pindex].position, players[pindex].player_direction, 1)
players[pindex].cursor_pos = fa_utils.center_of_tile(players[pindex].cursor_pos)
fa_mouse.move_mouse_pointer(players[pindex].cursor_pos, pindex)
fa_graphics.sync_build_cursor_graphics(pindex)
players[pindex].player_direction = p.character.direction
players[pindex].build_lock = false
--Close Remote view
toggle_remote_view(pindex, false, true, true)
p.close_map()
end
--Toggles cursor mode on or off. Appropriately affects other modes such as build lock or remote view.
function toggle_cursor_mode(pindex, muted)
local p = game.get_player(pindex)
if p.character == nil then
players[pindex].cursor = true
players[pindex].build_lock = false
return
end
if (not players[pindex].cursor) and not players[pindex].hide_cursor then
--Enable
players[pindex].cursor = true
players[pindex].build_lock = false
--Teleport to the center of the nearest tile to align
center_player_character(pindex)
--Finally, read the new tile
if muted ~= true then read_tile(pindex, "Cursor mode enabled, ") end
else
force_cursor_off(pindex)
--Finally, read the new tile
if muted ~= true then read_tile(pindex, "Cursor mode disabled, ") end
end
if players[pindex].cursor_size < 2 then
--Update cursor highlight
local ent = get_first_ent_at_tile(pindex)
if ent and ent.valid then
fa_graphics.draw_cursor_highlight(pindex, ent, nil)
else
fa_graphics.draw_cursor_highlight(pindex, nil, nil)
end
else
local left_top = {
math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size,
math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size,
}
local right_bottom = {
math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1,
math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1,
}
fa_graphics.draw_large_cursor(left_top, right_bottom, pindex)
end
end
--Toggles remote view on or off. Appropriately affects build lock or remote view.
function toggle_remote_view(pindex, force_true, force_false, muted)
if (players[pindex].remote_view ~= true or force_true == true) and force_false ~= true then
players[pindex].remote_view = true
players[pindex].cursor = true
players[pindex].build_lock = false
center_player_character(pindex)
if muted ~= true then read_tile(pindex, "Remote view opened, ") end
else
players[pindex].remote_view = false
players[pindex].build_lock = false
if muted ~= true then read_tile(pindex, "Remote view closed, ") end
game.get_player(pindex).close_map()
end
--Fix zoom
fa_zoom.fix_zoom(pindex)
end
--Teleports the player character to the nearest tile center position to allow grid aligned cursor movement.
function center_player_character(pindex)
local p = game.get_player(pindex)
local can_port = p.surface.can_place_entity({ name = "character", position = fa_utils.center_of_tile(p.position) })
local ents = p.surface.find_entities_filtered({
position = fa_utils.center_of_tile(p.position),
radius = 0.1,
type = { "character" },
invert = true,
})
if #ents > 0 and ents[1].valid then
local ent = ents[1]
--Ignore ents you can walk through, laterdo better collision checks**
can_port = can_port and all_ents_are_walkable(p.position)
end
if can_port then p.teleport(fa_utils.center_of_tile(p.position)) end
players[pindex].position = p.position
players[pindex].cursor_pos = fa_utils.center_of_tile(players[pindex].cursor_pos)
fa_mouse.move_mouse_pointer(players[pindex].cursor_pos, pindex)
end
--Teleports the cursor to the player character
function jump_to_player(pindex)
local first_player = game.get_player(pindex)
players[pindex].cursor_pos.x = math.floor(first_player.position.x) + 0.5
players[pindex].cursor_pos.y = math.floor(first_player.position.y) + 0.5
read_coords(pindex, "Cursor returned ")
if players[pindex].cursor_size < 2 then
fa_graphics.draw_cursor_highlight(pindex, nil, nil)
else
local scan_left_top = {
math.floor(players[pindex].cursor_pos.x) - players[pindex].cursor_size,
math.floor(players[pindex].cursor_pos.y) - players[pindex].cursor_size,
}
local scan_right_bottom = {
math.floor(players[pindex].cursor_pos.x) + players[pindex].cursor_size + 1,
math.floor(players[pindex].cursor_pos.y) + players[pindex].cursor_size + 1,
}
fa_graphics.draw_large_cursor(scan_left_top, scan_right_bottom, pindex)
end
end
function return_cursor_to_character(pindex)
if not check_for_player(pindex) then return end
if not players[pindex].in_menu then
if players[pindex].cursor then jump_to_player(pindex) end
end
end
--Re-checks the cursor tile and indexes the entities on it, returns a boolean on whether it is successful.
function refresh_player_tile(pindex)
local surf = game.get_player(pindex).surface
local c_pos = players[pindex].cursor_pos
if math.floor(c_pos.x) == math.ceil(c_pos.x) then c_pos.x = c_pos.x - 0.01 end
if math.floor(c_pos.y) == math.ceil(c_pos.y) then c_pos.y = c_pos.y - 0.01 end
local search_area = {
{ x = math.floor(c_pos.x) + 0.01, y = math.floor(c_pos.y) + 0.01 },
{ x = math.ceil(c_pos.x) - 0.01, y = math.ceil(c_pos.y) - 0.01 },
}
players[pindex].tile.ents =
surf.find_entities_filtered({ area = search_area, name = EXCLUDED_ENT_NAMES, invert = true })
sort_ents_by_primary_first(players[pindex].tile.ents)
--Draw the tile
--rendering.draw_rectangle{left_top = search_area[1], right_bottom = search_area[2], color = {1,0,1}, surface = surf, time_to_live = 100}--
local wide_area = {
{ x = math.floor(c_pos.x) - 0.01, y = math.floor(c_pos.y) - 0.01 },
{ x = math.ceil(c_pos.x) + 0.01, y = math.ceil(c_pos.y) + 0.01 },
}
local remnants = surf.find_entities_filtered({ area = wide_area, type = "corpse" })
for i, remnant in ipairs(remnants) do
table.insert(players[pindex].tile.ents, remnant)
end
players[pindex].tile.ent_index = 1
if #players[pindex].tile.ents == 0 then players[pindex].tile.ent_index = 0 end
players[pindex].tile.last_returned_index = 0
if
not (
pcall(function()
players[pindex].tile.tile = surf.get_tile(players[pindex].cursor_pos.x, players[pindex].cursor_pos.y).name
players[pindex].tile.tile_object = surf.get_tile(players[pindex].cursor_pos.x, players[pindex].cursor_pos.y)
end)
)
then
return false
end
return true
end
--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.
function read_tile(pindex, start_text)
local result = start_text or ""
if not refresh_player_tile(pindex) then
printout(result .. "Tile uncharted and out of range", pindex)
return
end
local ent = get_first_ent_at_tile(pindex)
if not (ent and ent.valid) then
--If there is no ent, read the tile instead
players[pindex].tile.previous = nil
local tile = players[pindex].tile.tile
result = result .. fa_localising.get(players[pindex].tile.tile_object, pindex)
if
tile == "water"
or tile == "deepwater"
or tile == "water-green"
or tile == "deepwater-green"
or tile == "water-shallow"
or tile == "water-mud"
or tile == "water-wube"
then
--Identify shores and crevices and so on for water tiles
result = result .. fa_utils.identify_water_shores(pindex)
end
fa_graphics.draw_cursor_highlight(pindex, nil, nil)
game.get_player(pindex).selected = nil
else --laterdo tackle the issue here where entities such as tree stumps block preview info
result = result .. fa_info.ent_info(pindex, ent)
fa_graphics.draw_cursor_highlight(pindex, ent, nil)
game.get_player(pindex).selected = ent
--game.get_player(pindex).print(result)--
players[pindex].tile.previous = ent
end
if not ent or ent.type == "resource" then --possible bug here with the h box being a new tile ent
local stack = game.get_player(pindex).cursor_stack
--Run build preview checks
if stack and stack.valid_for_read and stack.valid and stack.prototype.place_result ~= nil then
result = result .. fa_building_tools.build_preview_checks_info(stack, pindex)
--game.get_player(pindex).print(result)--
end
end
--If the player is holding a cut-paste tool, every entity being read gets mined as soon as you read a new tile.
local stack = game.get_player(pindex).cursor_stack
if stack and stack.valid_for_read and stack.name == "cut-paste-tool" and not players[pindex].vanilla_mode then
if ent and ent.valid then --not while loop, because it causes crashes
local name = ent.name
game.get_player(pindex).play_sound({ path = "player-mine" })
if fa_mining_tools.try_to_mine_with_soun(ent, pindex) then result = result .. name .. " mined, " end
--Second round, in case two entities are there. While loops do not work!
ent = get_first_ent_at_tile(pindex)
if ent and ent.valid and players[pindex].walk ~= WALKING.SMOOTH then --not while
local name = ent.name
game.get_player(pindex).play_sound({ path = "player-mine" })
if fa_mining_tools.try_to_mine_with_soun(ent, pindex) then result = result .. name .. " mined, " end
end
end
end
--Add info on whether the tile is uncharted or blurred or distant
result = result .. fa_mouse.cursor_visibility_info(pindex)
printout(result, pindex)
--game.get_player(pindex).print(result)--**
end
--Read the current co-ordinates of the cursor on the map or in a menu. For crafting recipe and technology menus, it reads the ingredients / requirements instead.
--Todo: split this function by menu.
function read_coords(pindex, start_phrase)
start_phrase = start_phrase or ""
local result = start_phrase
local ent = players[pindex].building.ent
local offset = 0
if
(players[pindex].menu == "building" or players[pindex].menu == "vehicle")
and players[pindex].building.recipe_list ~= nil
then
offset = 1
end
if not players[pindex].in_menu or players[pindex].menu == "structure-travel" or players[pindex].menu == "travel" then
if players[pindex].vanilla_mode then players[pindex].cursor_pos = game.get_player(pindex).position end
if game.get_player(pindex).driving then
--Give vehicle coords and orientation and speed --laterdo find exact speed coefficient
local vehicle = game.get_player(pindex).vehicle
local speed = vehicle.speed * 215
if vehicle.type ~= "spider-vehicle" then
if speed > 0 then
result = result
.. " heading "
.. fa_utils.get_heading_info(vehicle)
.. " at "
.. math.floor(speed)
.. " kilometers per hour "
elseif speed < 0 then
result = result
.. " facing "
.. fa_utils.get_heading_info(vehicle)
.. " while reversing at "
.. math.floor(-speed)
.. " kilometers per hour "
else
result = result .. " parked facing " .. fa_utils.get_heading_info(vehicle)
end
else
result = result .. " moving at " .. math.floor(speed) .. " kilometers per hour "
end
result = result .. " in " .. fa_localising.get(vehicle, pindex) .. " at point "
printout(result .. math.floor(vehicle.position.x) .. ", " .. math.floor(vehicle.position.y), pindex)
else
--Simply give coords (floored for the readout, extra precision for the console)
local location = fa_utils.get_entity_part_at_cursor(pindex)
if location == nil then location = " " end
local marked_pos = { x = players[pindex].cursor_pos.x, y = players[pindex].cursor_pos.y }
result = result .. " " .. location .. " at " .. math.floor(marked_pos.x) .. ", " .. math.floor(marked_pos.y)
game.get_player(pindex).print(
result .. "\n (" .. math.floor(marked_pos.x * 10) / 10 .. ", " .. math.floor(marked_pos.y * 10) / 10 .. ")",
{ volume_modifier = 0 }
)
--Draw the point
rendering.draw_circle({
color = { 1.0, 0.2, 0.0 },
radius = 0.1,
width = 5,
target = players[pindex].cursor_pos,
surface = game.get_player(pindex).surface,
time_to_live = 180,
})
--If there is a build preview, give its dimensions and which way they extend
local stack = game.get_player(pindex).cursor_stack
if
stack
and stack.valid_for_read
and stack.valid
and stack.prototype.place_result ~= nil
and (stack.prototype.place_result.tile_height > 1 or stack.prototype.place_result.tile_width > 1)
then
local dir = players[pindex].building_direction
turn_to_cursor_direction_cardinal(pindex)
local p_dir = players[pindex].player_direction
local preview_str = ", preview is "
if dir == dirs.north or dir == dirs.south then
preview_str = preview_str .. stack.prototype.place_result.tile_width .. " tiles wide "
elseif dir == dirs.east or dir == dirs.west then
preview_str = preview_str .. stack.prototype.place_result.tile_height .. " tiles wide "
end
if players[pindex].cursor or p_dir == dirs.east or p_dir == dirs.south or p_dir == dirs.north then
preview_str = preview_str .. " to the East "
elseif not players[pindex].cursor and p_dir == dirs.west then
preview_str = preview_str .. " to the West "
end
if dir == dirs.north or dir == dirs.south then
preview_str = preview_str .. " and " .. stack.prototype.place_result.tile_height .. " tiles high "
elseif dir == dirs.east or dir == dirs.west then
preview_str = preview_str .. " and " .. stack.prototype.place_result.tile_width .. " tiles high "
end
if players[pindex].cursor or p_dir == dirs.east or p_dir == dirs.south or p_dir == dirs.west then
preview_str = preview_str .. " to the South "
elseif not players[pindex].cursor and p_dir == dirs.north then
preview_str = preview_str .. " to the North "
end
result = result .. preview_str
elseif
stack
and stack.valid_for_read
and stack.valid
and stack.is_blueprint
and stack.is_blueprint_setup()
then
--Blueprints have their own data
local left_top, right_bottom, build_pos = fa_blueprints.get_blueprint_corners(pindex, false)
local bp_dim_1 = right_bottom.x - left_top.x
local bp_dim_2 = right_bottom.y - left_top.y
local preview_str = ", blueprint preview is "
.. bp_dim_1
.. " tiles wide to the East and "
.. bp_dim_2
.. " tiles high to the South"