From d3a2db977db741c1812dbfd4859fa0a091ab3208 Mon Sep 17 00:00:00 2001 From: Max5377 <69468517+Max5377@users.noreply.github.com> Date: Fri, 13 Oct 2023 22:34:20 +0300 Subject: [PATCH] TradeShips hyperjump code change TradeShips now will check for distance(altitude) between orbital station(station's parent body) before making the jump. This should address accumulating hyperclouds around stations and rare illegal jumps. The algorithm will be: For orbital stations: 1. Undock; 2. Fly to limits of current orbital station (AIFlyTo); 3. OnAICompleted event fires, tell ship to fly to current system's star; 4. Assign task to check distance between current orbital station and ship, if distance is large enough, make the jump. For ground stations; 1. Undock; 2. Tell ship to fly to current system's star; 3. Assign task to check altitude between current orbital station's parent and ship, if altitude is high enough, make the jump. --- data/modules/TradeShips/Events.lua | 33 ++++++++++-------- data/modules/TradeShips/Flow.lua | 19 +++++++++++ data/modules/TradeShips/Trader.lua | 54 ++++++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/data/modules/TradeShips/Events.lua b/data/modules/TradeShips/Events.lua index ff7295dad2b..b31cdd6a31d 100644 --- a/data/modules/TradeShips/Events.lua +++ b/data/modules/TradeShips/Events.lua @@ -100,7 +100,7 @@ local onLeaveSystem = function (ship) end Event.Register("onLeaveSystem", onLeaveSystem) -local onFrameChanged = function (ship) +--[[local onFrameChanged = function (ship) if not ship:isa("Ship") or Core.ships[ship] == nil then return end local trader = Core.ships[ship] Core.log:add(ship, "Entered frame " .. (ship.frameBody and ship.frameBody:GetLabel() or "unknown")) @@ -115,7 +115,7 @@ local onFrameChanged = function (ship) end end end -Event.Register("onFrameChanged", onFrameChanged) +Event.Register("onFrameChanged", onFrameChanged)--]] local onShipDocked = function (ship, starport) if Core.ships[ship] == nil then return end @@ -159,11 +159,18 @@ Event.Register("onShipDocked", onShipDocked) local onShipUndocked = function (ship, starport) if Core.ships[ship] == nil then return end + local trader = Core.ships[ship] + + if trader.starport.type == "STARPORT_ORBITAL" then + -- fly to the limit of the starport frame + ship:AIFlyTo(starport) + else + -- enter low orbit of first system's star + ship:AIEnterLowOrbit(trader.starport.path:GetStarSystem():GetStars()[1].body) + Trader.assignTask(ship, Game.time + 10, 'hyperjumpAtDistance') + end - -- fly to the limit of the starport frame - ship:AIFlyTo(starport) - - Core.ships[ship]['status'] = 'outbound' + trader['status'] = 'outbound' end Event.Register("onShipUndocked", onShipUndocked) @@ -171,14 +178,12 @@ local onAICompleted = function (ship, ai_error) if Core.ships[ship] == nil then return end local trader = Core.ships[ship] if ai_error ~= 'NONE' then - Core.log:add(ship, 'AICompleted: Error: '..ai_error..' Status: '..trader.status) end - + Core.log:add(ship, 'AICompleted: Error: '..ai_error..' Status: '..trader.status) + end if trader.status == 'outbound' then - if Trader.getSystemAndJump(ship) ~= 'OK' then - ship:AIDockWith(trader.starport) - trader['status'] = 'inbound' - trader.ts_error = 'cnt_jump_aicomp' - end + -- enter low orbit of first system's star + ship:AIEnterLowOrbit(trader.starport.path:GetStarSystem():GetStars()[1].body) + Trader.assignTask(ship, Game.time + 10, 'hyperjumpAtDistance') elseif trader.status == 'orbit' then if ai_error == 'NONE' then trader.ts_error = "wait_6h" @@ -258,7 +263,7 @@ local onShipHit = function (ship, attacker) elseif trader.starport and Engine.rand:Number(1) < trader.chance then local distance = ship:DistanceTo(trader.starport) if distance > Core.AU * (2 - trader.chance) then - if Trader.getSystemAndJump(ship) then + if Trader.getSystemAndJump(ship) == 'OK' then return else trader['no_jump'] = true diff --git a/data/modules/TradeShips/Flow.lua b/data/modules/TradeShips/Flow.lua index 0f4f7bd6938..c53eedb6222 100644 --- a/data/modules/TradeShips/Flow.lua +++ b/data/modules/TradeShips/Flow.lua @@ -494,6 +494,25 @@ Flow.spawnInitialShips = function() return ships_in_space end +Flow.setPlayerAsTraderDocked = function() + local ship = Game.player + if ship then + Core.ships[ship] = { ts_error = "OK", status = 'docked', starport = ship:GetDockedWith(), ship_name = Game.player.shipId } + Trader.addEquip(ship) + Trader.assignTask(ship, Game.time + utils.deviation(Core.params.port_params[Core.ships[ship].starport].time * 3600, 0.8), 'doUndock') + end +end + +Flow.setPlayerAsTraderInbound = function() + local ship = Game.player + if ship then + Core.ships[ship] = { ts_error = "OK", status = 'inbound', starport = ship:FindNearestTo("SPACESTATION"), ship_name = Game.player.shipId } + Trader.addEquip(ship) + Space.PutShipOnRoute(ship, Core.ships[ship].starport, Engine.rand:Number(0.0, 0.999))-- don't generate at the destination + ship:AIDockWith(Core.ships[ship].starport) + end +end + Flow.run = function() local ship_name = utils.chooseEqual(Core.params.ship_names) local cloud = utils.chooseEqual(Core.params.hyper_routes[ship_name]) diff --git a/data/modules/TradeShips/Trader.lua b/data/modules/TradeShips/Trader.lua index f1efea44372..54931f0ffa5 100644 --- a/data/modules/TradeShips/Trader.lua +++ b/data/modules/TradeShips/Trader.lua @@ -200,10 +200,12 @@ Trader.getSystemAndJump = function (ship) if Core.ships[ship].starport then local body = Space.GetBody(Core.ships[ship].starport.path:GetSystemBody().parent.index) local port = Core.ships[ship].starport - -- boost away from the starport before jumping if it is too close + + --[[-- boost away from the starport before jumping if it is too close if (ship:DistanceTo(port) < 20000) then ship:AIEnterLowOrbit(body) - end + end--]] + return jumpToSystem(ship, getSystem(ship)) end end @@ -275,6 +277,21 @@ Trader.removeFuel = function (ship, count) return removed end +Trader.checkDistBetweenStarport = function (ship) + local trader = Core.ships[ship] + if trader then + local distance + if trader.starport.type == "STARPORT_ORBITAL" then + distance = ship:DistanceTo(trader.starport) + else + local stationsParent = Space.GetBody(Core.ships[ship].starport.path:GetSystemBody().parent.index) + distance = ship:GetAltitudeRelTo(stationsParent) + end + return distance >= trader.hyperjumpDist + end + return nil +end + -- TRADER DEFERRED TASKS -- -- call the passed function in a specified time, checking whether we are still @@ -293,6 +310,39 @@ local trader_task = {} -- the task function prototype should be: -- function(ship) +trader_task.hyperjumpAtDistance = function(ship) + -- the player may have left the system + if ship:exists() then + local trader = Core.ships[ship] + if trader then + if trader.status == "outbound" and trader.ts_error ~= "HIT" then + -- if trader is not under attack and started to leave station + if trader.starport ~= nil then + -- if trader has not started to hyperjump + if trader.hyperjumpDist == nil then + trader.hyperjumpDist = Engine.rand:Integer(20000, 240000) + end + if Trader.checkDistBetweenStarport(ship) then + -- if distance is large enough attempt to hyperjump + local status = Trader.getSystemAndJump(ship) + if status ~= 'OK' then + ship:CancelAI() + ship:AIDockWith(trader.starport) + trader['status'] = 'inbound' + trader.ts_error = 'cnt_jump_aicomp' + end + trader.hyperjumpDist = nil + else + Trader.assignTask(ship, Game.time + 10, 'hyperjumpAtDistance') + end + end + else + trader.hyperjumpDist = nil + end + end + end +end + trader_task.doUndock = function(ship) -- the player may have left the system or the ship may have already undocked if ship:exists() and ship:GetDockedWith() then