forked from DaveMcW/blueprint-string
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
306 lines (256 loc) · 8.97 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
require "defines"
local BlueprintString = require "blueprintstring.blueprintstring"
BlueprintString.COMPRESS_STRINGS = true
BlueprintString.LINE_LENGTH = 120
function init_gui(player, ignoretech)
if (not ignoretech and not player.force.technologies["automated-construction"].researched) then
return
end
if (not player.gui.top["blueprint-string-button"]) then
player.gui.top.add{type="button", name="blueprint-string-button", style="blueprintstring_button_main"}
end
end
script.on_init(function()
for _, player in pairs(game.players) do
init_gui(player, false)
end
end)
script.on_event(defines.events.on_player_created, function(event)
init_gui(game.players[event.player_index], false)
end)
script.on_event(defines.events.on_research_finished, function(event)
if (event.research.name == "automated-construction") then
for _, player in pairs(game.players) do
if (event.research.force.name == player.force.name) then
init_gui(player, true)
end
end
end
end)
function expand_gui(player)
local frame = player.gui.left["blueprint-string"]
if (frame) then
frame.destroy()
else
frame = player.gui.left.add{type="frame", name="blueprint-string"}
frame.add{type="label", caption={"textbox-caption"}}
frame.add{type="textfield", name="blueprint-string-text"}
frame.add{type="button", name="blueprint-string-load", style="blueprintstring_button_load"}
frame.add{type="button", name="blueprint-string-save-as", style="blueprintstring_button_saveas"}
frame.add{type="button", name="blueprint-string-save-all", style="blueprintstring_button_saveall"}
frame.add{type="button", name="blueprint-upgrade", style="blueprintstring_button_upgrade"}
end
end
function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function filter_blueprints(inventory)
local blueprints = {}
if (inventory) then
local total = inventory.get_item_count("blueprint")
local count = 0
local i = 1
while (count < total) do
if (inventory[i].valid_for_read and inventory[i].type == "blueprint") then
blueprints[i] = inventory[i]
count = count + inventory[i].count
end
i = i + 1
assert(i < 999999, "Infinite loop in filter_blueprints")
end
end
return blueprints
end
function find_empty_blueprint(player)
for _, stack in pairs(filter_blueprints(player.get_inventory(defines.inventory.player_quickbar))) do
if (not stack.is_blueprint_setup()) then
return stack
end
end
for _, stack in pairs(filter_blueprints(player.get_inventory(defines.inventory.player_main))) do
if (not stack.is_blueprint_setup()) then
return stack
end
end
return nil
end
function load_blueprint(player)
local blueprint = find_empty_blueprint(player)
if (not blueprint) then
player.print({"no-empty-blueprint"})
return
end
local data = trim(player.gui.left["blueprint-string"]["blueprint-string-text"].text)
player.gui.left["blueprint-string"]["blueprint-string-text"].text = ""
if (data == "") then
player.print({"no-string"})
return
end
local blueprint_format = BlueprintString.fromString(data)
if (not blueprint_format or type(blueprint_format) ~= "table") then
player.print({"unknown-format"})
return
end
if (not blueprint_format.icons or type(blueprint_format.icons) ~= "table" or #blueprint_format.icons < 1) then
player.print({"unknown-format"})
return
end
status, result = pcall(blueprint.set_blueprint_entities, blueprint_format.entities)
if (not status) then
player.print({"blueprint-api-error", result})
blueprint.set_blueprint_entities({})
return
end
status, result = pcall(blueprint.set_blueprint_tiles, blueprint_format.tiles)
if (not status) then
player.print({"blueprint-api-error", result})
blueprint.set_blueprint_entities({})
return
end
if (blueprint.is_blueprint_setup()) then
status, result = pcall(function() blueprint.blueprint_icons = blueprint_format.icons end)
if (not status) then
player.print({"blueprint-icon-error", result})
blueprint.set_blueprint_entities({})
return
end
end
end
local blueprints_saved = 0
function save_blueprint(player, stack, filename)
local icons = stack.blueprint_icons
local entities = stack.get_blueprint_entities()
local tiles = stack.get_blueprint_tiles()
local blueprint_format = {entities=entities, icons=icons, tiles=tiles}
local data = BlueprintString.toString(blueprint_format)
if (#game.players > 1 and player.name and player.name ~= "") then
local name = player.name
filename = name .. "-" .. filename
end
filename = filename:gsub("[/\\:*?\"<>|]", "_")
game.write_file("blueprint-string/" .. filename .. ".txt", data)
blueprints_saved = blueprints_saved + 1
end
function holding_blueprint(player)
return (player.cursor_stack.valid_for_read and player.cursor_stack.type == "blueprint" and player.cursor_stack.is_blueprint_setup())
end
function save_blueprint_as(player, filename)
blueprints_saved = 0
if (not holding_blueprint(player)) then
player.print({"no-blueprint-in-hand"})
return
end
if (not filename or filename == "") then
player.print({"no-filename"})
return
end
save_blueprint(player, player.cursor_stack, filename)
player.gui.center["blueprint-string-filename-prompt"].destroy()
if (blueprints_saved > 0) then
player.print({"blueprint-saved-as", filename})
else
player.print({"blueprints-not-saved"})
end
end
function save_blueprints(player)
blueprints_saved = 0
for position, stack in pairs(filter_blueprints(player.get_inventory(defines.inventory.player_quickbar))) do
if (stack.is_blueprint_setup()) then
save_blueprint(player, stack, "toolbar-"..position)
end
end
for position, stack in pairs(filter_blueprints(player.get_inventory(defines.inventory.player_main))) do
if (stack.is_blueprint_setup()) then
save_blueprint(player, stack, "inventory-"..position)
end
end
if (blueprints_saved > 0) then
player.print({"blueprints-saved", blueprints_saved})
else
player.print({"blueprints-not-saved"})
end
end
function prompt_for_filename(player)
if (not holding_blueprint(player)) then
player.print({"no-blueprint-in-hand"})
return
end
local frame = player.gui.center["blueprint-string-filename-prompt"]
if (frame) then
frame.destroy()
end
frame = player.gui.center.add{type="frame", direction="vertical", name="blueprint-string-filename-prompt"}
local line1 = frame.add{type="flow", direction="horizontal"}
line1.add{type="label", caption={"save-as-2"}}
frame.add{type="textfield", name="blueprint-string-filename"}
local line2 = frame.add{type="flow", direction="horizontal"}
line2.add{type="button", name="blueprint-string-filename-save", caption={"save"}, font_color=white, style="blueprintstring_button_style"}
line2.add{type="button", name="blueprint-string-filename-cancel", caption={"cancel"}, font_color=white, style="blueprintstring_button_style"}
end
function contains_entities(blueprint, entities)
if not blueprint.entities then
return false
end
for _,e in pairs(blueprint.entities) do
if entities[e.name] then
return true
end
end
return false
end
function upgrade_blueprint(player)
if (not holding_blueprint(player)) then
player.print({"no-blueprint-in-hand"})
return
end
local entities = player.cursor_stack.get_blueprint_entities()
local tiles = player.cursor_stack.get_blueprint_tiles()
local offset = { x=-0.5, y=-0.5 }
local rail_entities = {}
rail_entities["straight-rail"] = true
rail_entities["curved-rail"]=true
rail_entities["rail-signal"]=true
rail_entities["rail-chain-signal"]=true
rail_entities["train-stop"]=true
rail_entities["smart-train-stop"]=true
if contains_entities(entities, rail_entities) then
offset = { x = -1, y = -1 }
end
if (entities) then
for _, entity in pairs(entities) do
entity.position = {x = entity.position.x + offset.x, y = entity.position.y + offset.y}
end
player.cursor_stack.set_blueprint_entities(entities)
end
if (tiles) then
for _, entity in pairs(tiles) do
tile.position = {x = tile.position.x + offset.x, y = tile.position.y + offset.y}
end
player.cursor_stack.set_blueprint_tiles(tiles)
end
end
script.on_event(defines.events.on_gui_click, function(event)
local player = game.players[event.element.player_index]
local name = event.element.name
if (name == "blueprint-string-load") then
load_blueprint(player)
elseif (name == "blueprint-string-save-all") then
save_blueprints(player)
elseif (name == "blueprint-string-save-as") then
prompt_for_filename(player)
elseif (name == "blueprint-string-filename-save") then
save_blueprint_as(player, player.gui.center["blueprint-string-filename-prompt"]["blueprint-string-filename"].text)
elseif (name == "blueprint-string-filename-cancel") then
player.gui.center["blueprint-string-filename-prompt"].destroy()
elseif (name == "blueprint-string-button") then
expand_gui(player)
elseif (name == "blueprint-upgrade") then
upgrade_blueprint(player)
end
end)
script.on_event(defines.events.on_robot_built_entity, function(event)
local entity = event.created_entity
if (entity and entity.type == "assembling-machine" and entity.recipe and not entity.recipe.enabled) then
entity.recipe = nil
end
end)