Skip to content

Commit

Permalink
TradeShips hyperjump code change
Browse files Browse the repository at this point in the history
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.

Co-Authored-By: Gliese852 <[email protected]>
  • Loading branch information
Max5377 and Gliese852 committed Oct 20, 2023
1 parent 04c6111 commit 5ffb844
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 16 deletions.
33 changes: 19 additions & 14 deletions data/modules/TradeShips/Events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
Expand Down Expand Up @@ -159,26 +159,31 @@ 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)

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"
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions data/modules/TradeShips/Flow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,25 @@ Flow.spawnInitialShips = function()
return ships_in_space
end

Flow.setPlayerAsTraderDocked = function()
local ship = Game.player
local dockedStation = ship:GetDockedWith()
if dockedStation then
Core.ships[ship] = { ts_error = "OK", status = 'docked', starport = dockedStation, ship_name = Game.player.shipId }
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
local nearestStation = ship:FindNearestTo("SPACESTATION")
if nearestStation then
Core.ships[ship] = { ts_error = "OK", status = 'inbound', starport = nearestStation, ship_name = Game.player.shipId }
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])
Expand Down
54 changes: 52 additions & 2 deletions data/modules/TradeShips/Trader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5ffb844

Please sign in to comment.