-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.lua
189 lines (167 loc) · 5.58 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
require("lib")
local anzLoc = 0
local anzControl = 0
function CallRemoteInterface()
if remote.interfaces["FuelTrainStop"] then
remote.call("FuelTrainStop", "exclude_from_fuel_schedule", "et-electric-locomotive-1")
remote.call("FuelTrainStop", "exclude_from_fuel_schedule", "et-electric-locomotive-2")
remote.call("FuelTrainStop", "exclude_from_fuel_schedule", "et-electric-locomotive-3")
remote.call("FuelTrainStop", "exclude_from_fuel_schedule", "et-electric-locomotive-4")
end
end
function Init()
global = {}
global.LocList = {}
global.ControlList = {}
end
function Load()
CallRemoteInterface()
anzLoc = table.count(global.LocList)
anzControl = table.count(global.ControlList)
end
function Reinitialize()
global = global or {}
global.LocList = global.LocList or {}
global.ControlList = global.ControlList or {}
Load()
end
function OnInit()
Init()
Load()
end
script.on_init(OnInit)
function OnLoad()
Load()
end
script.on_load(OnLoad)
function OnConfigurationChanged(data)
local modName = "ElectricTrain-Microhawk"
if not IsModChanged(data,modName) then
Load()
else
oldVersion = GetOldVersion(data,modName)
newVersion = GetNewVersion(data,modName)
if oldVersion == newVersion then
Reinitialize()
else
Init()
for _,surface in pairs(game.surfaces) do
local trains = surface.find_entities_filtered{type="locomotive"}
for _,train in pairs(trains) do
if train.name:match("^et%-electric%-locomotive%-%d$") or train.name:match("^et%-electric%-locomotive%-%d%-mu$") then
table.insert(global.LocList,{entity=train,provider=nil})
train.burner.currently_burning = game.item_prototypes['et-electric-locomotive-fuel']
end
end
end
anzLoc = table.count(global.LocList)
for _,surface in pairs(game.surfaces) do
local controls = surface.find_entities_filtered{type="electric-energy-interface"}
for _,control in pairs(controls) do
if control.name:match("^et%-control%-station%-%d$") then
table.insert(global.ControlList,control)
end
if control.name:match("^et%-electric%-locomotive%-%d%-power$") or control.name:match("^et%-electric%-locomotive%-%d%-mu-power$") then
control.destroy()
end
end
end
anzControl = table.count(global.ControlList)
end
end
end
script.on_configuration_changed(OnConfigurationChanged)
function OnBuiltEntity(event)
local entity = event.created_entity or event.entity
if entity and entity.valid then
if entity.name:match("^et%-control%-station%-%d$") then
table.insert(global.ControlList,entity)
anzControl = anzControl + 1
elseif entity.type == "locomotive" then
if entity.name:match("^et%-electric%-locomotive%-%d$") or entity.name:match("^et%-electric%-locomotive%-%d%-mu$") then
table.insert(global.LocList,{entity=entity,provider=nil})
entity.burner.currently_burning = game.item_prototypes['et-electric-locomotive-fuel']
anzLoc = anzLoc + 1
end
end
end
end
script.on_event({defines.events.on_built_entity,defines.events.on_robot_built_entity,defines.events.script_raised_built},OnBuiltEntity)
function OnRemoveEntity(event)
local entity = event.entity
if entity and entity.valid then
if entity.name:match("^et%-control%-station%-%d$") then
for i,control in pairs(global.ControlList) do
if control == entity then
for _,loc in pairs(global.LocList) do
if loc.provider and loc.provider.valid then
loc.provider.destroy()
end
loc.provider = nil
end
table.remove(global.ControlList,i)
anzControl = anzControl - 1
break
end
end
elseif entity.type == "locomotive" then
if entity.name:match("^et%-electric%-locomotive%-%d$") or entity.name:match("^et%-electric%-locomotive%-%d%-mu$") then
for i,loc in pairs(global.LocList) do
if loc.entity == entity then
if loc.provider and loc.provider.valid then
loc.provider.destroy()
end
table.remove(global.LocList,i)
anzLoc = anzLoc - 1
break
end
end
end
end
end
end
script.on_event({defines.events.on_pre_player_mined_item,defines.events.on_robot_pre_mined,defines.events.on_entity_died,defines.events.script_raised_destroy},OnRemoveEntity)
function CreateProvider(loc)
local control = global.ControlList[1]
local pos = control.position
local surface = control.surface
local force = control.force
local entity = surface.create_entity{name=loc.entity.name.."-power",position=pos,force=force}
loc.provider = entity
end
function RemoveLoc(i)
if global.LocList[i] then
if global.LocList[i].entity and global.LocList[i].entity.valid then
global.LocList[i].entity.destroy()
end
if global.LocList[i].provider and global.LocList[i].provider.valid then
global.LocList[i].provider.destroy()
end
end
table.remove(global.LocList,i)
end
function OnTick()
if anzLoc > 0 and anzControl > 0 then
for i,loc in pairs(global.LocList) do
if loc and loc.entity and loc.entity.valid then
if not (loc.provider and loc.provider.valid) then
CreateProvider(loc)
else
needPower = loc.entity.burner.currently_burning.fuel_value - loc.entity.burner.remaining_burning_fuel
restPower = loc.provider.energy - needPower
if restPower > 0 then
loc.entity.burner.remaining_burning_fuel = loc.entity.burner.currently_burning.fuel_value
loc.provider.energy = loc.provider.energy - needPower
else
loc.entity.burner.remaining_burning_fuel = loc.entity.burner.remaining_burning_fuel + loc.provider.energy
loc.provider.energy = 0
end
end
else
RemoveLoc(i)
end
end
end
end
--script.on_event(defines.events.on_tick,OnTick)
script.on_nth_tick(2,OnTick)