forked from DemonicLaxatives/RailBow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
131 lines (113 loc) · 3.67 KB
/
Copy pathcontrol.lua
File metadata and controls
131 lines (113 loc) · 3.67 KB
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
local handler = require("__core__.lualib.event_handler")
local mod_gui = require("mod-gui")
--- @class IterationState
--- @field n_steps integer
--- @field last_step integer
--- @field calculation_complete boolean
--- @class MaskCalculation
--- @field tiles table<integer, string>
--- @field tiles_min integer
--- @field tiles_max integer
--- @field rails LuaEntity[]
--- @field drive_directions table<integer, integer>
--- @field p0 [integer, integer]
--- @field tile_map table<integer, table<integer, table<integer, number>>>
--- @field tile_array [integer, integer][]
--- @field iteration_state IterationState
--- @class TileCalculation
--- @field instant_build boolean
--- @field entity_remove_filter table<string>
--- @field mode string
--- @field iteration_state IterationState
--- @class RailBowCalculation
--- @field player_index integer
--- @field mask_calculation MaskCalculation
--- @field tile_calculation TileCalculation
--- @field rb_debug boolean
--- @class RailBowConfig
--- @field name string
--- @field tiles table<integer, string>
--- @field remove_trees boolean
--- @field remove_cliffs boolean
--- @class RailBowSelectionTool
--- @field presets RailBowConfig[]
--- @field selected_preset integer
--- @field opened_preset integer|nil
--- @field copied_tile string|nil
local function initialize_global(player)
if not player then
return
end
if not storage.railbow_tools[player.index] then
local init_tiles = {}
for i = -8, 8 do
if i ~= 0 then
init_tiles[i] = nil
end
end
--- @type RailBowConfig
local init_config = {
name = "default",
tiles = init_tiles,
mode = "vote",
remove_trees = true,
remove_cliffs = false
}
storage.railbow_tools[player.index] = {
presets = { init_config },
selected_preset = 1,
opened_preset = nil,
copied_tile = nil
}
end
end
local function create_button(player)
if not player then return end
local button_flow = mod_gui.get_button_flow(player)
if not button_flow.railbow_button then
button_flow.add {
type = "sprite-button",
name = "railbow_button",
sprite = "item/railbow-selection-tool",
tooltip = { "tooltips.railbow-open-gui" },
style = mod_gui.button_style
}
end
end
---@param event EventData.on_player_created
local function on_player_created(event)
local player = game.get_player(event.player_index)
initialize_global(player)
create_button(player)
end
---@param event EventData.on_player_removed
local function on_player_removed(event)
storage.railbow_tools[event.player_index] = nil
for i, data in pairs(storage.railbow_calculation_queue) do
if data.player_index == event.player_index then
table.remove(storage.railbow_calculation_queue, i)
end
end
end
local function on_init()
--- @type table<integer, RailBowSelectionTool>
storage.railbow_tools = {}
--- @type RailBowCalculation[]
storage.railbow_calculation_queue = {}
for _, player in pairs(game.players) do
initialize_global(player)
create_button(player)
end
end
local control = {}
control.on_init = on_init
control.events = {
[defines.events.on_player_created] = on_player_created,
[defines.events.on_player_removed] = on_player_removed
}
handler.add_libraries({
control,
require("scripts.selection"),
require("scripts.gui"),
require("scripts.calculator"),
})