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
|
require 'numstring.lua'
--[[
-- @brief Tests to see if it is possible to swap ships for the player.
--]]
function swapshiptest( template )
local pp = player.pilot()
local total_cargo = 0
for k,v in ipairs( pp ) do
total_cargo = total_cargo + v.q
end
-- See if we can swap over
return total_cargo <= template:cargoFree()
end
--[[
-- @brief Swaps the player's ship.
--
-- @param template Should be a pilot template.
-- @return true on success, false otherwise
--]]
function swapship( template )
local pp = player.pilot()
local total_cargo = 0
local mission_cargo = 0
for k,v in ipairs( pp:cargoList() ) do
total_cargo = total_cargo + v.q
-- Add mission cargo separately
if v.m then
mission_cargo = mission_cargo + v.q
end
end
-- Impossible to move over, too much mission cargo
if mission_cargo > template:cargoFree() then
return false
end
-- Case not enough room to move stuff over
if total_cargo > template:cargoFree() then
-- Simulate cargo removal
local cl = pp:cargoList()
local space_needed = total_cargo-template:cargoFree()
local removals = {}
for k,v in ipairs( cl ) do
if not v.m then
v.p = commodity.get(v.nameRaw):price()
end
end
while space_needed > 0 do
-- Find cheapest
local cn, cq, ck
local cp = 1e10
for k,v in pairs( cl ) do
if not v.m then
if v.p < cp then
ck = k
cn = v.nameRaw
cp = v.p
cq = v.q
end
end
end
-- Simulate removal
cq = math.min( space_needed, cq )
removals[cn] = cq
cl[ck].q = cl[ck].q - cq
if cl[ck].q <= 0 then
cl[ck] = nil
end
space_needed = space_needed - cq
end
-- Format into a nice string all the removals
local remove_str = ""
for n,q in pairs(removals) do
remove_str = remove_str .. string.format("\n %s %s", tonnestring_short(q), _(n))
end
-- Ask if the player is fine with removing the cargo
if tk.yesno(
_("Too much cargo for new ship"),
_("You do not have enough space to move your cargo to the new ship. Get rid of the following to make room?")..remove_str) then
-- Get rid of the cargo
for n,q in pairs(removals) do
pp:cargoRm( n, q )
end
else
-- Player abandoned attempting to free cargo
return false
end
end
-- Create new ship
player.swapShip( template:ship() )
pp = player.pilot() -- Update struct to new pilot
-- Start with an empty ship
pp:rmOutfit( "all" )
pp:rmOutfit( "cores" )
-- Copy equipment
for k,v in ipairs( template:outfits() ) do
pp = player.pilot()
pp:addOutfit( v, 1, false )
end
-- Delete pilot
template:rm()
return true
end
|