forked from Kiplacon/RenaiTransportation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
428 lines (392 loc) · 16.9 KB
/
control.lua
File metadata and controls
428 lines (392 loc) · 16.9 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
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
if script.active_mods["gvv"] then require("__gvv__.gvv")() end
-- Setup tables and stuff for new/existing saves ----
script.on_init(
require("script.event.init")
)
-- game version changes, prototypes change, startup mod settings change, and any time mod versions change including adding or removing mods
script.on_configuration_changed(
require("script.event.config_changed")
)
---- Add new players to the AllPlayers table ----
script.on_event(
defines.events.on_player_created,
require("script.event.player_created")
)
-- On Built/Copy/Stuff
---- adds new thrower inserters to the list of throwers to check.
---- Make player launchers (reskinned inserters) to be inoperable
---- and inactive ----
script.on_event(
{
defines.events.on_built_entity, --| built by hand ----
defines.events.on_robot_built_entity, --| built by robot ----
defines.events.script_raised_built, --| built by script ----
defines.events.on_entity_cloned, -- | cloned by script ----
defines.events.script_raised_revive, -- | ghost revived by script
},
require("script.event.entity_built")
)
-- On Rotate
script.on_event(
defines.events.on_player_rotated_entity,
require("script.event.rotate")
)
-- Thrower Range blueprint auto build cancel
script.on_event(defines.events.on_player_cursor_stack_changed, -- only has .player_index
function(event)
if (global.AllPlayers[event.player_index].RangeAdjusting == true) then
global.AllPlayers[event.player_index].RangeAdjusting = false
end
end)
-- Clear invalid things
script.on_nth_tick(300,
function(event)
for unitID, ItsStuff in pairs(global.BouncePadList) do
if (ItsStuff.TheEntity and ItsStuff.TheEntity.valid) then
-- it's good
else
global.BouncePadList[unitID] = nil
end
end
for unitID, ItsStuff in pairs(global.MagnetRamps) do
if (ItsStuff.entity and ItsStuff.entity.valid) then
-- it's good
else
global.MagnetRamps[unitID] = nil
end
end
-- for each, world in pairs(game.surfaces) do
-- for every, ZiplinePart in pairs(world.find_entities_filtered{name = {"RTZipline", "RTZiplinePowerDrain"}}) do
-- local owned = false
-- for all, player in pairs(global.AllPlayers) do
-- if ((player.ChuggaChugga and ZiplinePart.unit_number == player.ChuggaChugga.unit_number)
-- or (player.succ and ZiplinePart.unit_number == player.succ.unit_number)
-- ) then
-- owned = true
-- end
-- end
-- if (owned == false) then
-- ZiplinePart.destroy()
-- end
-- end
-- end
end)
-- Thrower Check
---- checks if thrower inserters have something in their hands and it's in the throwing position, then creates the approppriate projectile ----
script.on_nth_tick(3,
function(event)
if (global.CatapultList ~= {}) then
for catapultID, properties in pairs(global.CatapultList) do
local catapult = properties.entity
BurnerSelfRefuelCompensation = 0.2
if (catapult.valid and catapult.burner == nil and catapult.fluidbox == nil and catapult.energy/catapult.electric_buffer_size >= 0.9) then
catapult.active = true
BurnerSelfRefuelCompensation = 0
elseif (catapult.valid and catapult.name == "RTThrower-PrimerThrower") then
BurnerSelfRefuelCompensation = -0.1
elseif (catapult.valid and catapult.burner == nil and catapult.fluidbox == nil) then
catapult.active = false
rendering.draw_sprite
{
sprite = "utility.electricity_icon_unplugged",
x_scale = 0.5,
y_scale = 0.5,
target = catapult,
surface = catapult.surface,
time_to_live = 4
}
end
if (catapult.valid and catapult.held_stack.valid_for_read) then
if (catapult.name ~= "RTThrower-PrimerThrower" and settings.global["RTOverflowComp"].value == true) then
-- pointing at some entity
if (properties.targets[catapult.held_stack.name] ~= nil
and properties.targets[catapult.held_stack.name].valid
and global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number]
and global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name]) then
if (properties.targets[catapult.held_stack.name].type ~= "transport-belt") then
if (global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] < 0) then
global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] = 0
end
local total = global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] + catapult.held_stack.count
local inserted = properties.targets[catapult.held_stack.name].insert({name=catapult.held_stack.name, count=total})
if (inserted < total) then
catapult.active = false
else
catapult.active = true
end
if (inserted > 0) then -- when the destination is full. Have to check otherwise there's an error
properties.targets[catapult.held_stack.name].remove_item({name=catapult.held_stack.name, count=inserted})
end
elseif (properties.targets[catapult.held_stack.name].type == "transport-belt") then
local incomming = 0
for name, count in pairs(global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number]) do
incomming = incomming + count
end
local total = incomming + properties.targets[catapult.held_stack.name].get_transport_line(1).get_item_count() + properties.targets[catapult.held_stack.name].get_transport_line(2).get_item_count()
if (total <= 6) then
catapult.active = true
else
catapult.active = false
end
end
-- pointing at nothing/the ground
elseif (properties.targets[catapult.held_stack.name] == "nothing") then
catapult.active = true
-- item needs path validation/is currently tracking path
elseif (properties.targets[catapult.held_stack.name] == nil) then
-- start path tracking
if (properties.ImAlreadyTracer == nil or properties.ImAlreadyTracer == "traced") then
properties.ImAlreadyTracer = "tracing"
local sprite = rendering.draw_sprite
{
sprite = "RTBlank",
target = properties.entity.position,
surface = properties.entity.surface
}
local shadow = rendering.draw_sprite
{
sprite = "RTBlank",
target = properties.entity.position,
surface = properties.entity.surface
}
local x = properties.entity.drop_position.x
local y = properties.entity.drop_position.y
local speed = 999
local arc = -5 -- lower number is higher arc
local AirTime = 1
local vector = {x=x-properties.entity.position.x, y=y-properties.entity.position.y}
local spin = 0
global.FlyingItems[global.FlightNumber] =
{sprite=sprite,
shadow=shadow,
speed=speed,
arc=arc,
spin=spin,
item=catapult.held_stack.name,
amount=0,
target={x=x, y=y},
start=properties.entity.position,
AirTime=AirTime,
StartTick=game.tick,
LandTick=game.tick+AirTime,
vector=vector,
tracing = properties.entity.unit_number}
global.FlightNumber = global.FlightNumber + 1
end
catapult.active = false
-- first time throws for an item
elseif (properties.targets[catapult.held_stack.name]
and properties.targets[catapult.held_stack.name].valid
and global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number] == nil) then
global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number] = {}
global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] = 0
elseif (properties.targets[catapult.held_stack.name]
and properties.targets[catapult.held_stack.name].valid
and global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number]
and global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] == nil) then
global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] = 0
end
-- overflow prevention off
else
catapult.active = true
end
-- if the thrower is still active after the checks then:
if (catapult.active == true) then
if (catapult.orientation == 0 and catapult.held_stack_position.y >= catapult.position.y+BurnerSelfRefuelCompensation)
or (catapult.orientation == 0.25 and catapult.held_stack_position.x <= catapult.position.x-BurnerSelfRefuelCompensation)
or (catapult.orientation == 0.50 and catapult.held_stack_position.y <= catapult.position.y-BurnerSelfRefuelCompensation)
or (catapult.orientation == 0.75 and catapult.held_stack_position.x >= catapult.position.x+BurnerSelfRefuelCompensation)
then
if (catapult.name == "RTThrower-PrimerThrower" and game.entity_prototypes["RTPrimerThrowerShooter-"..catapult.held_stack.name]) then
catapult.inserter_stack_size_override = 1
catapult.active = false
global.PrimerThrowerLinks[properties.entangled.detector.unit_number].ready = true
else
local sprite = rendering.draw_sprite
{
sprite = "item/"..catapult.held_stack.name,
x_scale = 0.5,
y_scale = 0.5,
target = catapult.held_stack_position,
surface = catapult.surface
}
local shadow = rendering.draw_sprite
{
sprite = "item/"..catapult.held_stack.name,
tint = {0,0,0,0.5},
x_scale = 0.5,
y_scale = 0.5,
target = catapult.held_stack_position,
surface = catapult.surface
}
local x = catapult.drop_position.x
local y = catapult.drop_position.y
local distance = math.sqrt((x-catapult.held_stack_position.x)^2 + (y-catapult.held_stack_position.y)^2)
local arc = -(0.3236*distance^-0.404)-- closer to 0 = higher arc
local space = nil
if (string.find(catapult.surface.name, " Orbit") or string.find(catapult.surface.name, " Field") or string.find(catapult.surface.name, " Belt")) then
arc = -99999999999999
x = x + (-global.OrientationUnitComponents[catapult.orientation].x * 1000)
y = y + (-global.OrientationUnitComponents[catapult.orientation].y * 1000)
distance = math.sqrt((x-catapult.held_stack_position.x)^2 + (y-catapult.held_stack_position.y)^2)
space = true
end
local start=catapult.held_stack_position
local vector = {x=x-catapult.held_stack_position.x, y=y-catapult.held_stack_position.y}
local speed = 0.18
if (catapult.name == "RTThrower-EjectorHatchRT") then
distance = math.sqrt((x-catapult.position.x)^2 + (y-catapult.position.y)^2)
vector = {x=x-catapult.position.x, y=y-catapult.position.y}
start=catapult.position
speed = 0.25
rendering.set_target(sprite, catapult.position)
rendering.set_target(shadow, catapult.position)
-- catapult.surface.play_sound
-- {
-- path = "RTEjector",
-- position = catapult.position,
-- volume = 0.7
-- }
else
-- catapult.surface.play_sound
-- {
-- path = "RTThrow",
-- position = catapult.position
-- }
end
local AirTime = math.floor(distance/speed)
if (AirTime == 0) then -- for super fast throwers that move right on top of their target
AirTime = 1
end
local spin = math.random(-10,10)*0.01
local destination = nil
if (settings.global["RTOverflowComp"].value == true) then
if (properties.targets[catapult.held_stack.name] ~= nil and properties.targets[catapult.held_stack.name].valid) then
destination = properties.targets[catapult.held_stack.name].unit_number
if (global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number] == nil) then
global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number] = {}
global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] = catapult.held_stack.count
elseif (global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] == nil) then
global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] = catapult.held_stack.count
else
global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] = global.OnTheWay[properties.targets[catapult.held_stack.name].unit_number][catapult.held_stack.name] + catapult.held_stack.count
end
elseif (properties.targets[catapult.held_stack.name] == "nothing") then -- recheck pointing at nothing/things without unit_numbers
properties.targets[catapult.held_stack.name] = nil
end
end
global.FlyingItems[global.FlightNumber] =
{sprite=sprite,
shadow=shadow,
speed=speed,
arc=arc,
spin=spin,
item=catapult.held_stack.name,
amount=catapult.held_stack.count,
target={x=x, y=y},
start=start,
AirTime=AirTime,
StartTick=game.tick,
LandTick=game.tick+AirTime,
vector=vector,
destination=destination,
space=space}
if (catapult.held_stack.item_number ~= nil) then
local CloudStorage = game.create_inventory(1)
CloudStorage.insert(catapult.held_stack)
global.FlyingItems[global.FlightNumber].CloudStorage = CloudStorage
end
global.FlightNumber = global.FlightNumber + 1
catapult.held_stack.clear()
end
end
end
elseif (catapult.valid and catapult.held_stack.valid_for_read == false) then
catapult.active = true
elseif (catapult.valid == false) then
global.CatapultList[catapultID] = nil
end
end
end
end)
-- Projectile Lands
-- When a projectile lands and its effect_id is triggered, what to do ----
script.on_event(
defines.events.on_script_trigger_effect,
require("script.event.effect_triggered")
)
-- Animating/On Tick
script.on_nth_tick(
1,
require("script.event.on_tick")
)
-- On Damaged
script.on_event(
defines.events.on_entity_damaged,
require("script.event.entity_damaged")
)
-- On Interact
script.on_event(
"RTInteract",
require("script.event.interact")
)
-- On Click
script.on_event(
"RTClick",
require("script.event.click")
)
script.on_event(
defines.events.on_entity_destroyed,
require("script.event.entity_destroyed")
)
script.on_event(defines.events.on_player_changed_surface,
-- .player_index :: uint: The player who changed surfaces
-- .surface_index :: uint: The surface index the player was on
function(event)
local player = game.players[event.player_index]
local PlayerProperties = global.AllPlayers[event.player_index]
if (PlayerProperties and PlayerProperties.state == "zipline" and player.surface.name ~= PlayerProperties.zipline.StartingSurface.name) then
player.teleport(player.position, game.get_surface(event.surface_index))
end
end)
script.on_event(defines.events.on_runtime_mod_setting_changed,
-- player_index :: uint (optional): The player who changed the setting or nil if changed by script.
-- setting :: string: The setting name that changed.
-- setting_type :: string: The setting type: "runtime-per-user", or "runtime-global".
function(event)
if (event.setting == "RTOverflowComp" and settings.global["RTOverflowComp"].value == false) then
global.OnTheWay = {}
end
end)
-- script.on_event(defines.events.on_player_driving_changed_state,
-- -- player_index :: uint
-- -- entity :: LuaEntity (optional): The vehicle if any.
-- function(event)
-- local player = game.players[event.player_index]
-- if (player.character and player.driving == false) then
-- for each, properties in pairs(global.FlyingTrains) do
-- if (properties.passenger and properties.passenger.unit_number == player.character.unit_number) then
-- properties.GuideCar.set_passenger(player)
-- end
-- end
-- end
-- end)
script.on_event(defines.events.on_gui_closed,
function(event)
if (event.entity and event.entity.name == "DirectorBouncePlate" and global.ThrowerPaths[event.entity.unit_number] ~= nil) then
for ThrowerUN, TrackedItems in pairs(global.ThrowerPaths[event.entity.unit_number]) do
if (global.CatapultList[ThrowerUN]) then
for item, asthma in pairs(TrackedItems) do
global.CatapultList[ThrowerUN].targets[item] = nil
end
end
end
global.ThrowerPaths[event.entity.unit_number] = {}
end
end)
-- a bunch of functions used in various other places
require("script.MiscFunctions")
require("script.GUIs")
script.on_event(defines.events.on_gui_click,
require("script.event.ClickGUI")
)