From 34a745436de0daac072e4e1c5cb2096998b7743f Mon Sep 17 00:00:00 2001 From: Kiplacon Date: Tue, 7 Mar 2023 20:41:07 -0500 Subject: [PATCH] Changed a valid check for thrown items to occur only when a surface is deleted instead of during the item's flight --- .vscode/launch.json | 26 ++ .vscode/settings.json | 33 ++ changelog.txt | 5 + control.lua | 21 +- info.json | 2 +- script/event/FlyingItems.lua | 716 +++++++++++++++++------------------ 6 files changed, 436 insertions(+), 367 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..318bc85 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "factoriomod", + "request": "launch", + "name": "Factorio Mod Debug" + }, + { + "type": "factoriomod", + "request": "launch", + "name": "Factorio Mod Debug (Settings & Data)", + "hookSettings": true, + "hookData": true + }, + { + "type": "factoriomod", + "request": "launch", + "name": "Factorio Mod Debug (Profile)", + "hookMode": "profile" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..28dd266 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,33 @@ +{ + "factorio.versions": [ + { + "name": "Steam Install", + "factorioPath": "d:\\Steam\\steamapps\\common\\Factorio\\bin\\x64\\factorio.exe", + "active": true + } + ], + "Lua.workspace.library": [ + "d:\\Steam\\steamapps\\common\\Factorio\\data", + "c:/Users/xiaox/AppData/Roaming/Code/User/workspaceStorage/f34a32745aa52066e3ffde1bd64d08ef/justarandomgeek.factoriomod-debug/sumneko-3rd/factorio/library" + ], + "Lua.workspace.userThirdParty": [ + "c:\\Users\\xiaox\\AppData\\Roaming\\Code\\User\\workspaceStorage\\f34a32745aa52066e3ffde1bd64d08ef\\justarandomgeek.factoriomod-debug\\sumneko-3rd" + ], + "Lua.diagnostics.globals": [ + "__DebugAdapter", + "__Profiler" + ], + "Lua.runtime.builtin": { + "io": "disable", + "os": "disable", + "math": "disable", + "debug": "disable", + "coroutine": "disable", + "package": "disable" + }, + "Lua.runtime.version": "Lua 5.2", + "Lua.runtime.special": { + "__object_name": "type" + }, + "Lua.runtime.plugin": "c:/Users/xiaox/AppData/Roaming/Code/User/workspaceStorage/f34a32745aa52066e3ffde1bd64d08ef/justarandomgeek.factoriomod-debug/sumneko-3rd/factorio/plugin.lua" +} \ No newline at end of file diff --git a/changelog.txt b/changelog.txt index 9bfb96b..100aefc 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,4 +1,9 @@ --------------------------------------------------------------------------------------------------- +Version: 1.1.11 +Date: 3/7/2023 + Changes: + - Changed a valid check for thrown items to occur only when a surface is deleted instead of during the item's flight. Should help with lag. The check was originally added to fix a crash when a surface with thrown items on it was deleted (like warptorio). Having the check only happen on surface deletion is less comprehensive but I've seen more comments about lag recently so I think it's worth a try +--------------------------------------------------------------------------------------------------- Version: 1.1.10 Date: 1/14/2023 Changes: diff --git a/control.lua b/control.lua index 7ac4ef2..a11f237 100644 --- a/control.lua +++ b/control.lua @@ -75,9 +75,7 @@ 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 @@ -456,3 +454,22 @@ function(event) end end) +script.on_event(defines.events.on_pre_surface_deleted, +--surface_index :: uint +--name :: defines.events Identifier of the event +--tick :: uint Tick the event was generated. +function(event) + for each, FlyingItem in pairs(global.FlyingItems) do + if (rendering.get_surface(FlyingItem.sprite).index == event.surface_index) then + rendering.destroy(FlyingItem.sprite) + rendering.destroy(FlyingItem.shadow) + if (FlyingItem.destination ~= nil and global.OnTheWay[FlyingItem.destination]) then + global.OnTheWay[FlyingItem.destination][FlyingItem.item] = global.OnTheWay[FlyingItem.destination][FlyingItem.item] - FlyingItem.amount + end + if (FlyingItem.player) then + SwapBackFromGhost(FlyingItem.player, FlyingItem) + end + global.FlyingItems[each] = nil + end + end +end) diff --git a/info.json b/info.json index a6b718f..115a508 100644 --- a/info.json +++ b/info.json @@ -1,6 +1,6 @@ { "name": "RenaiTransportation", - "version": "1.1.10", + "version": "1.1.11", "title": "Renai Transportation", "author": "Kiplacon", "factorio_version": "1.1", diff --git a/script/event/FlyingItems.lua b/script/event/FlyingItems.lua index 8b4f1e6..8eba6e8 100644 --- a/script/event/FlyingItems.lua +++ b/script/event/FlyingItems.lua @@ -2,96 +2,64 @@ local function on_tick(event) for each, FlyingItem in pairs(global.FlyingItems) do - if (rendering.is_valid(FlyingItem.sprite) == false) then - rendering.destroy(FlyingItem.sprite) - rendering.destroy(FlyingItem.shadow) - if (FlyingItem.destination ~= nil and global.OnTheWay[FlyingItem.destination]) then - global.OnTheWay[FlyingItem.destination][FlyingItem.item] = global.OnTheWay[FlyingItem.destination][FlyingItem.item] - FlyingItem.amount - end - if (FlyingItem.player) then - SwapBackFromGhost(FlyingItem.player, FlyingItem) - end - global.FlyingItems[each] = nil + local clear = true + if (game.tick < FlyingItem.LandTick and FlyingItem.player == nil) then + local duration = game.tick-FlyingItem.StartTick + local progress = duration/FlyingItem.AirTime + local height = (duration/(FlyingItem.arc*FlyingItem.AirTime))-(duration^2/(FlyingItem.arc*FlyingItem.AirTime^2)) + rendering.set_target(FlyingItem.sprite, {FlyingItem.start.x+(progress*FlyingItem.vector.x), FlyingItem.start.y+(progress*FlyingItem.vector.y)+height}) + rendering.set_target(FlyingItem.shadow, {FlyingItem.start.x+(progress*FlyingItem.vector.x)-height, FlyingItem.start.y+(progress*FlyingItem.vector.y)}) + rendering.set_orientation(FlyingItem.sprite, rendering.get_orientation(FlyingItem.sprite)+FlyingItem.spin) + rendering.set_orientation(FlyingItem.shadow, rendering.get_orientation(FlyingItem.shadow)+FlyingItem.spin) - else - local clear = true - if (game.tick < FlyingItem.LandTick and FlyingItem.player == nil) then - local duration = game.tick-FlyingItem.StartTick - local progress = duration/FlyingItem.AirTime - local height = (duration/(FlyingItem.arc*FlyingItem.AirTime))-(duration^2/(FlyingItem.arc*FlyingItem.AirTime^2)) - rendering.set_target(FlyingItem.sprite, {FlyingItem.start.x+(progress*FlyingItem.vector.x), FlyingItem.start.y+(progress*FlyingItem.vector.y)+height}) - rendering.set_target(FlyingItem.shadow, {FlyingItem.start.x+(progress*FlyingItem.vector.x)-height, FlyingItem.start.y+(progress*FlyingItem.vector.y)}) - rendering.set_orientation(FlyingItem.sprite, rendering.get_orientation(FlyingItem.sprite)+FlyingItem.spin) - rendering.set_orientation(FlyingItem.shadow, rendering.get_orientation(FlyingItem.shadow)+FlyingItem.spin) - - elseif (game.tick == FlyingItem.LandTick and FlyingItem.space == nil) then - local ThingLandedOn = rendering.get_surface(FlyingItem.sprite).find_entities_filtered + elseif (game.tick == FlyingItem.LandTick and FlyingItem.space == nil) then + local ThingLandedOn = rendering.get_surface(FlyingItem.sprite).find_entities_filtered + { + position = {math.floor(FlyingItem.target.x)+0.5, math.floor(FlyingItem.target.y)+0.5}, + collision_mask = "object-layer" + }[1] + local LandedOnCargoWagon = rendering.get_surface(FlyingItem.sprite).find_entities_filtered { - position = {math.floor(FlyingItem.target.x)+0.5, math.floor(FlyingItem.target.y)+0.5}, - collision_mask = "object-layer" + area = {{FlyingItem.target.x-0.5, FlyingItem.target.y-0.5}, {FlyingItem.target.x+0.5, FlyingItem.target.y+0.5}}, + type = "cargo-wagon" }[1] - local LandedOnCargoWagon = rendering.get_surface(FlyingItem.sprite).find_entities_filtered - { - area = {{FlyingItem.target.x-0.5, FlyingItem.target.y-0.5}, {FlyingItem.target.x+0.5, FlyingItem.target.y+0.5}}, - type = "cargo-wagon" - }[1] - -- landed on something - if (ThingLandedOn) then - if (string.find(ThingLandedOn.name, "BouncePlate")) then -- if that thing was a bounce plate - clear = false - local unitx = 1 - local unity = 1 - effect = "BouncePlateParticle" - if (string.find(ThingLandedOn.name, "DirectedBouncePlate")) then - unitx = global.OrientationUnitComponents[ThingLandedOn.orientation].x - unity = global.OrientationUnitComponents[ThingLandedOn.orientation].y - if (FlyingItem.player) then - global.AllPlayers[FlyingItem.player.index].PlayerLauncher.direction = global.OrientationUnitComponents[ThingLandedOn.orientation].name - end - elseif (string.find(ThingLandedOn.name, "DirectorBouncePlate")) then - for each, parameter in pairs(ThingLandedOn.get_or_create_control_behavior().parameters) do - if (parameter.signal.type == "item" and parameter.signal.name == FlyingItem.item) then - if (parameter.index >= 1 and parameter.index <= 10) then - unitx = 0 - unity = -1 - effect = "BouncePlateParticlered" - elseif (parameter.index >= 11 and parameter.index <= 20) then - unitx = 1 - unity = 0 - effect = "BouncePlateParticlegreen" - elseif (parameter.index >= 21 and parameter.index <= 30) then - unitx = 0 - unity = 1 - effect = "BouncePlateParticleblue" - elseif (parameter.index >= 31 and parameter.index <= 40) then - unitx = -1 - unity = 0 - effect = "BouncePlateParticleyellow" - end - break - end - end - if (unitx == 1 and unity == 1) then -- if there is no matching signal - if (FlyingItem.start.y > FlyingItem.target.y - and math.abs(FlyingItem.start.y-FlyingItem.target.y) > math.abs(FlyingItem.start.x-FlyingItem.target.x)) then + -- landed on something + if (ThingLandedOn) then + if (string.find(ThingLandedOn.name, "BouncePlate")) then -- if that thing was a bounce plate + clear = false + local unitx = 1 + local unity = 1 + effect = "BouncePlateParticle" + if (string.find(ThingLandedOn.name, "DirectedBouncePlate")) then + unitx = global.OrientationUnitComponents[ThingLandedOn.orientation].x + unity = global.OrientationUnitComponents[ThingLandedOn.orientation].y + if (FlyingItem.player) then + global.AllPlayers[FlyingItem.player.index].PlayerLauncher.direction = global.OrientationUnitComponents[ThingLandedOn.orientation].name + end + elseif (string.find(ThingLandedOn.name, "DirectorBouncePlate")) then + for each, parameter in pairs(ThingLandedOn.get_or_create_control_behavior().parameters) do + if (parameter.signal.type == "item" and parameter.signal.name == FlyingItem.item) then + if (parameter.index >= 1 and parameter.index <= 10) then unitx = 0 unity = -1 - elseif (FlyingItem.start.y < FlyingItem.target.y - and math.abs(FlyingItem.start.y-FlyingItem.target.y) > math.abs(FlyingItem.start.x-FlyingItem.target.x)) then + effect = "BouncePlateParticlered" + elseif (parameter.index >= 11 and parameter.index <= 20) then + unitx = 1 + unity = 0 + effect = "BouncePlateParticlegreen" + elseif (parameter.index >= 21 and parameter.index <= 30) then unitx = 0 unity = 1 - elseif (FlyingItem.start.x > FlyingItem.target.x - and math.abs(FlyingItem.start.y-FlyingItem.target.y) < math.abs(FlyingItem.start.x-FlyingItem.target.x)) then + effect = "BouncePlateParticleblue" + elseif (parameter.index >= 31 and parameter.index <= 40) then unitx = -1 unity = 0 - elseif (FlyingItem.start.x < FlyingItem.target.x - and math.abs(FlyingItem.start.y-FlyingItem.target.y) < math.abs(FlyingItem.start.x-FlyingItem.target.x)) then - unitx = 1 - unity = 0 + effect = "BouncePlateParticleyellow" end + break end - elseif (string.find(ThingLandedOn.name, "BouncePlate")) then - ---- determine "From" direction ---- + end + if (unitx == 1 and unity == 1) then -- if there is no matching signal if (FlyingItem.start.y > FlyingItem.target.y and math.abs(FlyingItem.start.y-FlyingItem.target.y) > math.abs(FlyingItem.start.x-FlyingItem.target.x)) then unitx = 0 @@ -110,328 +78,334 @@ local function on_tick(event) unity = 0 end end - - ---- Bounce modifiers ---- - -- Defaults -- - primable = "" - range = 9.9 - RangeBonus = 0 - SidewaysShift = 0 - tunez = "bounce" - if (string.find(ThingLandedOn.name, "Train")) then - range = 39.9 - elseif (ThingLandedOn.name == "BouncePlate5" or ThingLandedOn.name == "DirectedBouncePlate5") then - range = 4.9 - elseif (ThingLandedOn.name == "BouncePlate15" or ThingLandedOn.name == "DirectedBouncePlate15") then - range = 14.9 + elseif (string.find(ThingLandedOn.name, "BouncePlate")) then + ---- determine "From" direction ---- + if (FlyingItem.start.y > FlyingItem.target.y + and math.abs(FlyingItem.start.y-FlyingItem.target.y) > math.abs(FlyingItem.start.x-FlyingItem.target.x)) then + unitx = 0 + unity = -1 + elseif (FlyingItem.start.y < FlyingItem.target.y + and math.abs(FlyingItem.start.y-FlyingItem.target.y) > math.abs(FlyingItem.start.x-FlyingItem.target.x)) then + unitx = 0 + unity = 1 + elseif (FlyingItem.start.x > FlyingItem.target.x + and math.abs(FlyingItem.start.y-FlyingItem.target.y) < math.abs(FlyingItem.start.x-FlyingItem.target.x)) then + unitx = -1 + unity = 0 + elseif (FlyingItem.start.x < FlyingItem.target.x + and math.abs(FlyingItem.start.y-FlyingItem.target.y) < math.abs(FlyingItem.start.x-FlyingItem.target.x)) then + unitx = 1 + unity = 0 end + end - -- Modifiers -- - if (ThingLandedOn.name == "PrimerBouncePlate" and FlyingItem.player == nil and game.entity_prototypes[FlyingItem.item.."-projectileFromRenaiTransportationPrimed"]) then - primable = "Primed" - RangeBonus = 30 - tunez = "PrimeClick" - effect = "PrimerBouncePlateParticle" - elseif (ThingLandedOn.name == "PrimerSpreadBouncePlate" and FlyingItem.player == nil and game.entity_prototypes[FlyingItem.item.."-projectileFromRenaiTransportationPrimed"]) then - primable = "Primed" - tunez = "PrimeClick" - effect = "PrimerBouncePlateParticle" - elseif (ThingLandedOn.name == "SignalBouncePlate") then - ThingLandedOn.get_control_behavior().enabled = not ThingLandedOn.get_control_behavior().enabled - effect = "SignalBouncePlateParticle" - end + ---- Bounce modifiers ---- + -- Defaults -- + primable = "" + range = 9.9 + RangeBonus = 0 + SidewaysShift = 0 + tunez = "bounce" + if (string.find(ThingLandedOn.name, "Train")) then + range = 39.9 + elseif (ThingLandedOn.name == "BouncePlate5" or ThingLandedOn.name == "DirectedBouncePlate5") then + range = 4.9 + elseif (ThingLandedOn.name == "BouncePlate15" or ThingLandedOn.name == "DirectedBouncePlate15") then + range = 14.9 + end - if (not FlyingItem.tracing) then - ---- Creating the bounced thing ---- - if (primable == "Primed") then - for kidamogus = 1, FlyingItem.amount do - if (ThingLandedOn.name == "PrimerSpreadBouncePlate") then - RangeBonus = math.random(270,300)*0.1 - SidewaysShift = math.random(-200,200)*0.1 - end - ThingLandedOn.surface.create_entity - ({ - name = FlyingItem.item.."-projectileFromRenaiTransportation"..primable, - position = ThingLandedOn.position, --required setting for rendering, doesn't affect spawn - source = event.source_entity, --defaults to nil if there was no source_entity and uses source_position instead - source_position = ThingLandedOn.position, - target_position = {ThingLandedOn.position.x +unitx*(range+RangeBonus) +unity*(SidewaysShift), ThingLandedOn.position.y +unity*(range+RangeBonus) +unitx*(SidewaysShift)}, - force = ThingLandedOn.force - }) + -- Modifiers -- + if (ThingLandedOn.name == "PrimerBouncePlate" and FlyingItem.player == nil and game.entity_prototypes[FlyingItem.item.."-projectileFromRenaiTransportationPrimed"]) then + primable = "Primed" + RangeBonus = 30 + tunez = "PrimeClick" + effect = "PrimerBouncePlateParticle" + elseif (ThingLandedOn.name == "PrimerSpreadBouncePlate" and FlyingItem.player == nil and game.entity_prototypes[FlyingItem.item.."-projectileFromRenaiTransportationPrimed"]) then + primable = "Primed" + tunez = "PrimeClick" + effect = "PrimerBouncePlateParticle" + elseif (ThingLandedOn.name == "SignalBouncePlate") then + ThingLandedOn.get_control_behavior().enabled = not ThingLandedOn.get_control_behavior().enabled + effect = "SignalBouncePlateParticle" + end + + if (not FlyingItem.tracing) then + ---- Creating the bounced thing ---- + if (primable == "Primed") then + for kidamogus = 1, FlyingItem.amount do + if (ThingLandedOn.name == "PrimerSpreadBouncePlate") then + RangeBonus = math.random(270,300)*0.1 + SidewaysShift = math.random(-200,200)*0.1 end + ThingLandedOn.surface.create_entity + ({ + name = FlyingItem.item.."-projectileFromRenaiTransportation"..primable, + position = ThingLandedOn.position, --required setting for rendering, doesn't affect spawn + source = event.source_entity, --defaults to nil if there was no source_entity and uses source_position instead + source_position = ThingLandedOn.position, + target_position = {ThingLandedOn.position.x +unitx*(range+RangeBonus) +unity*(SidewaysShift), ThingLandedOn.position.y +unity*(range+RangeBonus) +unitx*(SidewaysShift)}, + force = ThingLandedOn.force + }) + end + else + local x = ThingLandedOn.position.x +unitx*(range+RangeBonus) +unity*(SidewaysShift) + local y = ThingLandedOn.position.y +unity*(range+RangeBonus) +unitx*(SidewaysShift) + local distance = math.sqrt((x-ThingLandedOn.position.x)^2 + (y-ThingLandedOn.position.y)^2) + if (string.find(ThingLandedOn.name, "Train")) then + FlyingItem.speed = 0.6 else - local x = ThingLandedOn.position.x +unitx*(range+RangeBonus) +unity*(SidewaysShift) - local y = ThingLandedOn.position.y +unity*(range+RangeBonus) +unitx*(SidewaysShift) - local distance = math.sqrt((x-ThingLandedOn.position.x)^2 + (y-ThingLandedOn.position.y)^2) - if (string.find(ThingLandedOn.name, "Train")) then - FlyingItem.speed = 0.6 - else - FlyingItem.speed = 0.18 - end - local speed = FlyingItem.speed - local AirTime = math.floor(distance/speed) - local vector = {x=x-ThingLandedOn.position.x, y=y-ThingLandedOn.position.y} - FlyingItem.target={x=x, y=y} - FlyingItem.start=ThingLandedOn.position - FlyingItem.StartTick=game.tick - if (FlyingItem.tracing == nil) then - FlyingItem.AirTime=AirTime - FlyingItem.arc = -0.1 - FlyingItem.LandTick=game.tick+AirTime - else - FlyingItem.AirTime=1 - FlyingItem.LandTick=game.tick+1 - end - FlyingItem.vector=vector + FlyingItem.speed = 0.18 + end + local speed = FlyingItem.speed + local AirTime = math.floor(distance/speed) + local vector = {x=x-ThingLandedOn.position.x, y=y-ThingLandedOn.position.y} + FlyingItem.target={x=x, y=y} + FlyingItem.start=ThingLandedOn.position + FlyingItem.StartTick=game.tick + if (FlyingItem.tracing == nil) then + FlyingItem.AirTime=AirTime + FlyingItem.arc = -0.1 + FlyingItem.LandTick=game.tick+AirTime + else + FlyingItem.AirTime=1 + FlyingItem.LandTick=game.tick+1 end - ThingLandedOn.surface.create_particle - ({ - name = effect, + FlyingItem.vector=vector + end + ThingLandedOn.surface.create_particle + ({ + name = effect, + position = ThingLandedOn.position, + movement = {0,0}, + height = 0, + vertical_speed = 0.1, + frame_speed = 1 + }) + ThingLandedOn.surface.play_sound + { + path = tunez, position = ThingLandedOn.position, - movement = {0,0}, - height = 0, - vertical_speed = 0.1, - frame_speed = 1 - }) + volume = 0.7 + } + else --it is a tracer + -- add the bounce pad to the bounce path list if its a tracer + if (primable ~= "Primed") then + if (global.ThrowerPaths[ThingLandedOn.unit_number] == nil) then + global.ThrowerPaths[ThingLandedOn.unit_number] = {} + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] = {} + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true + elseif (global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] == nil) then + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] = {} + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true + else + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true + end + script.register_on_entity_destroyed(ThingLandedOn) + local x = ThingLandedOn.position.x +unitx*(range+RangeBonus) +unity*(SidewaysShift) + local y = ThingLandedOn.position.y +unity*(range+RangeBonus) +unitx*(SidewaysShift) + local distance = math.sqrt((x-ThingLandedOn.position.x)^2 + (y-ThingLandedOn.position.y)^2) + local speed = FlyingItem.speed + local AirTime = math.floor(distance/speed) + local vector = {x=x-ThingLandedOn.position.x, y=y-ThingLandedOn.position.y} + FlyingItem.target={x=x, y=y} + FlyingItem.start=ThingLandedOn.position + FlyingItem.StartTick=game.tick + if (FlyingItem.tracing == nil) then + FlyingItem.AirTime=AirTime + FlyingItem.arc = -0.1 + FlyingItem.LandTick=game.tick+AirTime + else + FlyingItem.AirTime=1 + FlyingItem.LandTick=game.tick+1 + end + FlyingItem.vector=vector + else + global.CatapultList[FlyingItem.tracing].ImAlreadyTracer = "traced" + global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = "nothing" + clear = true + end + end + + -- non-tracers falling on something + elseif (FlyingItem.tracing == nil) then + -- players falling on something + if (FlyingItem.player) then + ---- Doesn't make sense for player landing on cliff to destroy it ---- + if (ThingLandedOn.name == "cliff") then + FlyingItem.player.teleport(ThingLandedOn.surface.find_non_colliding_position("iron-chest", FlyingItem.target, 0, 0.5)) + elseif (ThingLandedOn.name ~= "PlayerLauncher") then + ---- Damage the player based on thing's size and destroy what they landed on to prevent getting stuck ---- + FlyingItem.player.character.damage(10*(ThingLandedOn.bounding_box.right_bottom.x-ThingLandedOn.bounding_box.left_top.x)*(ThingLandedOn.bounding_box.right_bottom.y-ThingLandedOn.bounding_box.left_top.y), "neutral", "impact", ThingLandedOn) + ThingLandedOn.die() + end + -- items falling on something + else + if (ThingLandedOn.name == "OpenContainer" and ThingLandedOn.can_insert({name=FlyingItem.item})) then + if (FlyingItem.CloudStorage) then + ThingLandedOn.insert(FlyingItem.CloudStorage[1]) + FlyingItem.CloudStorage.destroy() + else + ThingLandedOn.insert({name=FlyingItem.item, count=FlyingItem.amount}) + end ThingLandedOn.surface.play_sound { - path = tunez, + path = "RTClunk", position = ThingLandedOn.position, - volume = 0.7 + volume_modifier = 0.9 } - else --it is a tracer - -- add the bounce pad to the bounce path list if its a tracer - if (primable ~= "Primed") then - if (global.ThrowerPaths[ThingLandedOn.unit_number] == nil) then - global.ThrowerPaths[ThingLandedOn.unit_number] = {} - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] = {} - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true - elseif (global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] == nil) then - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] = {} - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true - else - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true - end - script.register_on_entity_destroyed(ThingLandedOn) - local x = ThingLandedOn.position.x +unitx*(range+RangeBonus) +unity*(SidewaysShift) - local y = ThingLandedOn.position.y +unity*(range+RangeBonus) +unitx*(SidewaysShift) - local distance = math.sqrt((x-ThingLandedOn.position.x)^2 + (y-ThingLandedOn.position.y)^2) - local speed = FlyingItem.speed - local AirTime = math.floor(distance/speed) - local vector = {x=x-ThingLandedOn.position.x, y=y-ThingLandedOn.position.y} - FlyingItem.target={x=x, y=y} - FlyingItem.start=ThingLandedOn.position - FlyingItem.StartTick=game.tick - if (FlyingItem.tracing == nil) then - FlyingItem.AirTime=AirTime - FlyingItem.arc = -0.1 - FlyingItem.LandTick=game.tick+AirTime - else - FlyingItem.AirTime=1 - FlyingItem.LandTick=game.tick+1 - end - FlyingItem.vector=vector + + ---- If the thing it landed on has an inventory and a hatch, insert the item ---- + elseif (ThingLandedOn.surface.find_entity('HatchRT', {math.floor(FlyingItem.target.x)+0.5, math.floor(FlyingItem.target.y)+0.5}) and ThingLandedOn.can_insert({name=FlyingItem.item})) then + if (FlyingItem.CloudStorage) then + ThingLandedOn.insert(FlyingItem.CloudStorage[1]) + FlyingItem.CloudStorage.destroy() else - global.CatapultList[FlyingItem.tracing].ImAlreadyTracer = "traced" - global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = "nothing" - clear = true + ThingLandedOn.insert({name=FlyingItem.item, count=FlyingItem.amount}) end - end + ThingLandedOn.surface.play_sound + { + path = "RTClunk", + position = ThingLandedOn.position, + volume_modifier = 0.7 + } - -- non-tracers falling on something - elseif (FlyingItem.tracing == nil) then - -- players falling on something - if (FlyingItem.player) then - ---- Doesn't make sense for player landing on cliff to destroy it ---- - if (ThingLandedOn.name == "cliff") then - FlyingItem.player.teleport(ThingLandedOn.surface.find_non_colliding_position("iron-chest", FlyingItem.target, 0, 0.5)) - elseif (ThingLandedOn.name ~= "PlayerLauncher") then - ---- Damage the player based on thing's size and destroy what they landed on to prevent getting stuck ---- - FlyingItem.player.character.damage(10*(ThingLandedOn.bounding_box.right_bottom.x-ThingLandedOn.bounding_box.left_top.x)*(ThingLandedOn.bounding_box.right_bottom.y-ThingLandedOn.bounding_box.left_top.y), "neutral", "impact", ThingLandedOn) - ThingLandedOn.die() + ---- If it landed on something but there's also a cargo wagon there + elseif (LandedOnCargoWagon ~= nil and LandedOnCargoWagon.can_insert({name=FlyingItem.item})) then + if (FlyingItem.CloudStorage) then + LandedOnCargoWagon.insert(FlyingItem.CloudStorage[1]) + FlyingItem.CloudStorage.destroy() + else + LandedOnCargoWagon.insert({name=FlyingItem.item, count=FlyingItem.amount}) end - -- items falling on something - else - if (ThingLandedOn.name == "OpenContainer" and ThingLandedOn.can_insert({name=FlyingItem.item})) then - if (FlyingItem.CloudStorage) then - ThingLandedOn.insert(FlyingItem.CloudStorage[1]) - FlyingItem.CloudStorage.destroy() - else - ThingLandedOn.insert({name=FlyingItem.item, count=FlyingItem.amount}) - end - ThingLandedOn.surface.play_sound - { - path = "RTClunk", - position = ThingLandedOn.position, - volume_modifier = 0.9 - } - ---- If the thing it landed on has an inventory and a hatch, insert the item ---- - elseif (ThingLandedOn.surface.find_entity('HatchRT', {math.floor(FlyingItem.target.x)+0.5, math.floor(FlyingItem.target.y)+0.5}) and ThingLandedOn.can_insert({name=FlyingItem.item})) then - if (FlyingItem.CloudStorage) then - ThingLandedOn.insert(FlyingItem.CloudStorage[1]) - FlyingItem.CloudStorage.destroy() - else - ThingLandedOn.insert({name=FlyingItem.item, count=FlyingItem.amount}) - end - ThingLandedOn.surface.play_sound - { - path = "RTClunk", - position = ThingLandedOn.position, - volume_modifier = 0.7 - } - - ---- If it landed on something but there's also a cargo wagon there - elseif (LandedOnCargoWagon ~= nil and LandedOnCargoWagon.can_insert({name=FlyingItem.item})) then - if (FlyingItem.CloudStorage) then - LandedOnCargoWagon.insert(FlyingItem.CloudStorage[1]) - FlyingItem.CloudStorage.destroy() - else - LandedOnCargoWagon.insert({name=FlyingItem.item, count=FlyingItem.amount}) - end - - ---- otherwise it bounces off whatever it landed on and lands as an item on the nearest empty space within 10 tiles. destroyed if no space ---- - else - if (FlyingItem.CloudStorage) then -- for things with data/tags or whatever, should only ever be 1 in stack - if (ThingLandedOn.type == "transport-belt") then - for l = 1, 2 do - for i = 0, 0.9, 0.1 do - if (FlyingItem.CloudStorage[1].count > 0 and ThingLandedOn.get_transport_line(l).can_insert_at(i) == true) then - ThingLandedOn.get_transport_line(l).insert_at(i, FlyingItem.CloudStorage[1]) - FlyingItem.CloudStorage[1].count = FlyingItem.CloudStorage[1].count - 1 - end + ---- otherwise it bounces off whatever it landed on and lands as an item on the nearest empty space within 10 tiles. destroyed if no space ---- + else + if (FlyingItem.CloudStorage) then -- for things with data/tags or whatever, should only ever be 1 in stack + if (ThingLandedOn.type == "transport-belt") then + for l = 1, 2 do + for i = 0, 0.9, 0.1 do + if (FlyingItem.CloudStorage[1].count > 0 and ThingLandedOn.get_transport_line(l).can_insert_at(i) == true) then + ThingLandedOn.get_transport_line(l).insert_at(i, FlyingItem.CloudStorage[1]) + FlyingItem.CloudStorage[1].count = FlyingItem.CloudStorage[1].count - 1 end end end - FlyingItem.CloudStorage.destroy() - else -- depreciated drop method from old item tracking system - local total = FlyingItem.amount - if (ThingLandedOn.type == "transport-belt") then - for l = 1, 2 do - for i = 0, 0.9, 0.1 do - if (total > 0 and ThingLandedOn.get_transport_line(l).can_insert_at(i) == true) then - ThingLandedOn.get_transport_line(l).insert_at(i, {name=FlyingItem.item, count=1}) - total = total - 1 - end + end + FlyingItem.CloudStorage.destroy() + else -- depreciated drop method from old item tracking system + local total = FlyingItem.amount + if (ThingLandedOn.type == "transport-belt") then + for l = 1, 2 do + for i = 0, 0.9, 0.1 do + if (total > 0 and ThingLandedOn.get_transport_line(l).can_insert_at(i) == true) then + ThingLandedOn.get_transport_line(l).insert_at(i, {name=FlyingItem.item, count=1}) + total = total - 1 end end end - if (total > 0) then - rendering.get_surface(FlyingItem.sprite).spill_item_stack - ( - rendering.get_surface(FlyingItem.sprite).find_non_colliding_position("item-on-ground",FlyingItem.target, 0, 0.1), - {name=FlyingItem.item, count=total} - ) - end - - end - - end - end - -- tracers falling on something - else - if (global.CatapultList[FlyingItem.tracing]) then - if (LandedOnCargoWagon) then - global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = LandedOnCargoWagon - if (global.ThrowerPaths[LandedOnCargoWagon.unit_number] == nil) then - global.ThrowerPaths[LandedOnCargoWagon.unit_number] = {} - global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing] = {} - global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing][FlyingItem.item] = true - elseif (global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing] == nil) then - global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing] = {} - global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing][FlyingItem.item] = true - else - global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing][FlyingItem.item] = true end - script.register_on_entity_destroyed(LandedOnCargoWagon) - elseif (ThingLandedOn.unit_number == nil) then -- cliffs/trees/other things without unit_numbers - global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = "nothing" - else - global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = ThingLandedOn - if (global.ThrowerPaths[ThingLandedOn.unit_number] == nil) then - global.ThrowerPaths[ThingLandedOn.unit_number] = {} - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] = {} - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true - elseif (global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] == nil) then - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] = {} - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true - else - global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true + if (total > 0) then + rendering.get_surface(FlyingItem.sprite).spill_item_stack + ( + rendering.get_surface(FlyingItem.sprite).find_non_colliding_position("item-on-ground",FlyingItem.target, 0, 0.1), + {name=FlyingItem.item, count=total} + ) end - script.register_on_entity_destroyed(ThingLandedOn) + end - global.CatapultList[FlyingItem.tracing].ImAlreadyTracer = "traced" + end end - -- didn't land on anything - -- non-tracers - elseif (FlyingItem.tracing == nil) then - if (rendering.get_surface(FlyingItem.sprite).find_tiles_filtered{position = FlyingItem.target, radius = 1, limit = 1, collision_mask = "player-layer"}[1] ~= nil) then -- in theory, tiles the player cant walk on are some sort of fluid or other non-survivable ground - rendering.get_surface(FlyingItem.sprite).create_entity - ({ - name = "water-splash", - position = FlyingItem.target - }) - if (FlyingItem.player) then - FlyingItem.player.character.die() - else - if (FlyingItem.item == "raw-fish") then - for i = 1, math.floor(FlyingItem.amount/5) do - rendering.get_surface(FlyingItem.sprite).create_entity - { - name = "fish", - position = FlyingItem.target, - } - end + -- tracers falling on something + else + if (global.CatapultList[FlyingItem.tracing]) then + if (LandedOnCargoWagon) then + global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = LandedOnCargoWagon + if (global.ThrowerPaths[LandedOnCargoWagon.unit_number] == nil) then + global.ThrowerPaths[LandedOnCargoWagon.unit_number] = {} + global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing] = {} + global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing][FlyingItem.item] = true + elseif (global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing] == nil) then + global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing] = {} + global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing][FlyingItem.item] = true else - rendering.get_surface(FlyingItem.sprite).pollute(FlyingItem.target, FlyingItem.amount*0.5) - end - - if (FlyingItem.CloudStorage) then - FlyingItem.CloudStorage.destroy() + global.ThrowerPaths[LandedOnCargoWagon.unit_number][FlyingItem.tracing][FlyingItem.item] = true end - end - else - if (FlyingItem.player == nil) then - if (FlyingItem.CloudStorage) then - rendering.get_surface(FlyingItem.sprite).spill_item_stack - ( - rendering.get_surface(FlyingItem.sprite).find_non_colliding_position("item-on-ground", FlyingItem.target, 0, 0.1), - FlyingItem.CloudStorage[1] - ) - FlyingItem.CloudStorage.destroy() + script.register_on_entity_destroyed(LandedOnCargoWagon) + elseif (ThingLandedOn.unit_number == nil) then -- cliffs/trees/other things without unit_numbers + global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = "nothing" + else + global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = ThingLandedOn + if (global.ThrowerPaths[ThingLandedOn.unit_number] == nil) then + global.ThrowerPaths[ThingLandedOn.unit_number] = {} + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] = {} + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true + elseif (global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] == nil) then + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing] = {} + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true else - rendering.get_surface(FlyingItem.sprite).spill_item_stack - ( - rendering.get_surface(FlyingItem.sprite).find_non_colliding_position("item-on-ground", FlyingItem.target, 0, 0.1), - {name=FlyingItem.item, count=FlyingItem.amount} - ) + global.ThrowerPaths[ThingLandedOn.unit_number][FlyingItem.tracing][FlyingItem.item] = true end + script.register_on_entity_destroyed(ThingLandedOn) end + global.CatapultList[FlyingItem.tracing].ImAlreadyTracer = "traced" end - -- tracer - elseif (FlyingItem.tracing ~= nil and global.CatapultList[FlyingItem.tracing]) then - --game.print(FlyingItem.tracing) - global.CatapultList[FlyingItem.tracing].ImAlreadyTracer = "traced" - global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = "nothing" end - if (clear == true) then - rendering.destroy(FlyingItem.sprite) - rendering.destroy(FlyingItem.shadow) - if (FlyingItem.destination ~= nil and global.OnTheWay[FlyingItem.destination]) then - global.OnTheWay[FlyingItem.destination][FlyingItem.item] = global.OnTheWay[FlyingItem.destination][FlyingItem.item] - FlyingItem.amount - end + -- didn't land on anything + -- non-tracers + elseif (FlyingItem.tracing == nil) then + if (rendering.get_surface(FlyingItem.sprite).find_tiles_filtered{position = FlyingItem.target, radius = 1, limit = 1, collision_mask = "player-layer"}[1] ~= nil) then -- in theory, tiles the player cant walk on are some sort of fluid or other non-survivable ground + rendering.get_surface(FlyingItem.sprite).create_entity + ({ + name = "water-splash", + position = FlyingItem.target + }) if (FlyingItem.player) then - SwapBackFromGhost(FlyingItem.player, FlyingItem) - end - global.FlyingItems[each] = nil - end + FlyingItem.player.character.die() + else + if (FlyingItem.item == "raw-fish") then + for i = 1, math.floor(FlyingItem.amount/5) do + rendering.get_surface(FlyingItem.sprite).create_entity + { + name = "fish", + position = FlyingItem.target, + } + end + else + rendering.get_surface(FlyingItem.sprite).pollute(FlyingItem.target, FlyingItem.amount*0.5) + end - elseif (game.tick > FlyingItem.LandTick) then - if (FlyingItem.sprite) then - rendering.destroy(FlyingItem.sprite) - rendering.destroy(FlyingItem.shadow) + if (FlyingItem.CloudStorage) then + FlyingItem.CloudStorage.destroy() + end + end + else + if (FlyingItem.player == nil) then + if (FlyingItem.CloudStorage) then + rendering.get_surface(FlyingItem.sprite).spill_item_stack + ( + rendering.get_surface(FlyingItem.sprite).find_non_colliding_position("item-on-ground", FlyingItem.target, 0, 0.1), + FlyingItem.CloudStorage[1] + ) + FlyingItem.CloudStorage.destroy() + else + rendering.get_surface(FlyingItem.sprite).spill_item_stack + ( + rendering.get_surface(FlyingItem.sprite).find_non_colliding_position("item-on-ground", FlyingItem.target, 0, 0.1), + {name=FlyingItem.item, count=FlyingItem.amount} + ) + end + end end + -- tracer + elseif (FlyingItem.tracing ~= nil and global.CatapultList[FlyingItem.tracing]) then + --game.print(FlyingItem.tracing) + global.CatapultList[FlyingItem.tracing].ImAlreadyTracer = "traced" + global.CatapultList[FlyingItem.tracing].targets[FlyingItem.item] = "nothing" + end + if (clear == true) then + rendering.destroy(FlyingItem.sprite) + rendering.destroy(FlyingItem.shadow) if (FlyingItem.destination ~= nil and global.OnTheWay[FlyingItem.destination]) then global.OnTheWay[FlyingItem.destination][FlyingItem.item] = global.OnTheWay[FlyingItem.destination][FlyingItem.item] - FlyingItem.amount end @@ -440,7 +414,21 @@ local function on_tick(event) end global.FlyingItems[each] = nil end + + elseif (game.tick > FlyingItem.LandTick) then + if (FlyingItem.sprite) then + rendering.destroy(FlyingItem.sprite) + rendering.destroy(FlyingItem.shadow) + end + if (FlyingItem.destination ~= nil and global.OnTheWay[FlyingItem.destination]) then + global.OnTheWay[FlyingItem.destination][FlyingItem.item] = global.OnTheWay[FlyingItem.destination][FlyingItem.item] - FlyingItem.amount + end + if (FlyingItem.player) then + SwapBackFromGhost(FlyingItem.player, FlyingItem) + end + global.FlyingItems[each] = nil end + end end