-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathupdater.lua
More file actions
154 lines (133 loc) · 4.47 KB
/
updater.lua
File metadata and controls
154 lines (133 loc) · 4.47 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
-- Capabilities related to updating blueprints.
local Util = require("util")
local actions = require("actions")
local CLONED_BLUEPRINT = "BlueprintExtensions_cloned-blueprint"
local VERSION_PATTERN = "(v[.]?)(%d)$" -- Matches version number at end of blueprints.
local DEFAULT_VERSION = " v.2"
local AWAITING_GUI = 1
local AWAITING_BP = 2
local Updater = {
events = {}
}
function Updater.clone(player, event, action)
--local player_index = event.player_index
--local player = game.players[player_index]
if not player.valid then
return nil
end
local bp = Util.get_blueprint(player.cursor_stack)
if not bp then
return nil
end
local updater = {
name = bp.name,
label = bp.label,
icons = bp.blueprint_icons,
status = nil,
}
-- Create replacer tool
if not player.clean_cursor() then
return
end
Util.get_pdata(player.index).updater = updater
player.cursor_stack.set_stack(CLONED_BLUEPRINT)
if updater.label then
player.cursor_stack.label = updater.label
end
end
function Updater.on_selected_area(event)
if event.item ~= CLONED_BLUEPRINT then
return
end
local alt = (event.name == defines.events.on_player_alt_selected_area)
local player_index = event.player_index
local area = event.area
-- Handle blueprint replacer.
local player = game.players[player_index]
if not player.valid then
return nil
end
local cursor = player.cursor_stack
if not (cursor.valid and cursor.valid_for_read and cursor.name == CLONED_BLUEPRINT) then
return nil
end
local pdata = Util.get_pdata(player_index)
local updater = pdata.updater
if not updater then
return nil
end
cursor.set_stack(updater.name)
cursor.create_blueprint{
surface=player.surface,
force=player.force,
area=area,
always_include_tiles=true,
include_entities=true,
include_modules=true,
include_station_names=true,
include_trains=true,
include_fuel=true
}
if not cursor.is_blueprint_setup() then -- Empty blueprint area?
cursor.set_stack(CLONED_BLUEPRINT)
return
end
cursor.blueprint_icons = updater.icons
if updater.label then
local label = updater.label
local found
local versioning = player.mod_settings[
alt and "BlueprintExtensions_alt-version-increment" or "BlueprintExtensions_version-increment"
].value
if versioning ~= 'off' then
label, found = string.gsub(label, VERSION_PATTERN, function(v, n) return v .. (n+1) end)
if found == 0 and versioning == 'on' then
label = label .. DEFAULT_VERSION
end
end
cursor.label = label
end
-- Move this blueprint to a temporary item
local stack = Util.store_item(player_index, 'updater-blueprint', player.cursor_stack)
player.cursor_stack.clear()
updater.status = AWAITING_GUI
player.opened = stack
end
function Updater.on_gui_opened(event)
-- If opening an item, this means our target blueprint was closed at some point and that any
-- on_player_configured_blueprint events we see are nonsense.
if event.gui_type ~= defines.gui_type.item then
return
end
local pdata = Util.get_pdata(event.player_index)
if not pdata.updater then
return
end
if pdata.updater.status == AWAITING_GUI then
pdata.updater.status = AWAITING_BP
else
pdata.updater = nil
end
end
function Updater.on_player_configured_blueprint(event)
local pdata = Util.get_pdata(event.player_index)
-- game.print(serpent.block(pdata.updater))
if not pdata.updater or pdata.updater.status ~= AWAITING_BP then
return
end
pdata.updater = nil -- Nuke this.
local player = game.players[event.player_index]
if not player.clean_cursor() then
player.print({"bpex.error_cannot_set_stack"})
else
player.cursor_stack.set_stack(Util.fetch_item(event.player_index, 'updater-blueprint'))
end
end
actions['BlueprintExtensions_clone-blueprint'].handler = Updater.clone
Updater.events = {
[defines.events.on_player_selected_area] = Updater.on_selected_area,
[defines.events.on_player_alt_selected_area] = Updater.on_selected_area,
[defines.events.on_player_configured_blueprint] = Updater.on_player_configured_blueprint,
[defines.events.on_gui_opened] = Updater.on_gui_opened,
}
return Updater