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
|
--[[
-- @brief Wrapper for pilot.add() that can operate on tables of fleets.
--
-- @usage pilots = addShips( "Pirate Hyena", "pirate", nil, 2 ) -- Creates two Pirate Hyenas with pirate AIs.
-- @usage pilots = addShips( { "Trader Rhino", "Trader Koala" }, nil, nil, 2 ) -- Creates a convoy of four trader ships with default AIs.
--
-- @luaparam ship Fleet to add.
-- @luaparam ai AI to override the default with.
-- @luaparam location Location to jump in from, take off from, or appear at.
-- @luaparam count Number of times to repeat the pattern.
-- @luareturn Table of created pilots.
-- @luafunc addShips( fleet, ai, location, count )
--]]
function addShips( ship, ai, location, count )
local ais = {}
local locations = {}
local out = {}
if type(ship) ~= "table" and type(ship) ~= "string" then
print(_("addShips: Error, ship list is not a fleet or table of fleets!"))
return
elseif type(ship) == "string" then -- Put lone fleet into table.
ship = { ship }
end
ais = _buildDupeTable( ai, #ship )
locations = _buildDupeTable( location, #ship )
if count == nil then
count = 1
end
for i=1,count do -- Repeat the pattern as necessary.
for k,v in ipairs(ship) do
out = _mergeTables( out, pilot.add( ship[k], ais[k], locations[k] ) )
end
end
if #out > 1 then
_randomizePositions( out )
end
return out
end
--[[
-- @brief Wrapper for pilot.addRaw() that can operate on tables of ships.
--
-- @usage pilots = addRawShips( "Hyena", "pirate", nil, "Pirate" ) -- Creates a facsimile of a Pirate Hyena.
-- @usage pilots = addRawShips( { "Rhino", "Koala" }, nil, nil, "Trader", 2 ) -- Creates four Trader ships.
--
-- @luaparam ship Ship to add.
-- @luaparam ai AI to give the pilot.
-- @luaparam location Location to jump in from, take off from, or appear at.
-- @luaparam faction Faction to give the pilot.
-- @luaparam count Number of times to repeat the pattern.
-- @luareturn Table of created pilots.
-- @luafunc addRawShips( ship, ai, location, faction, count )
--]]
function addRawShips( ship, ai, location, faction, count )
local ais = {}
local locations = {}
local factions = {}
local out = {}
if type(ship) ~= "table" and type(ship) ~= "string" then
print(_("addRawShips: Error, ship list is not a ship or table of ships!"))
return
elseif type(ship) == "string" then -- Put lone ship into table.
ship = { ship }
end
ais = _buildDupeTable( ai, #ship )
locations = _buildDupeTable( location, #ship )
factions = _buildDupeTable( faction, #ship )
if factions[1] == nil then
print(_("addRawShips: Error, raw ships must have factions!"))
return
end
if count == nil then
count = 1
end
for i=1,count do -- Repeat the pattern as necessary.
for k,v in ipairs(ship) do
out[k+(i-1)*#ship] = pilot.addRaw( ship[k], ais[k], locations[k], factions[k] )
end
end
if #out > 1 then
_randomizePositions( out )
end
return out
end
function _buildDupeTable( input, count )
local tmp = {}
if type(input) == "table" then
if #input ~= count then
print(_("Warning: Tables are different lengths."))
end
return input
else
for i=1,count do
tmp[i] = input
end
return tmp
end
end
function _mergeTables( old, new )
if type(old) ~= "table" or type(new) ~= "table" then
print(_("_mergeTables: Error, this function only accepts tables."))
end
for k,v in ipairs(new) do
table.insert(old, v )
end
return old
end
-- Randomize the locations of ships in the same manner than pilot.add() does.
function _randomizePositions( ship, override )
if type(ship) ~= "table" and type(ship) ~= "userdata" then
print(_("_randomizePositions: Error, ship list is not a pilot or table of pilots!"))
return
elseif type(ship) == "userdata" then -- Put lone pilot into table.
ship = { ship }
end
local x = 0
local y = 0
for k,v in ipairs(ship) do
if k ~= 1 and not override then
if vec2.dist( ship[1]:pos(), v:pos() ) == 0 then
x = x + rnd.rnd(75,150) * (rnd.rnd(0,1) - 0.5) * 2
y = y + rnd.rnd(75,150) * (rnd.rnd(0,1) - 0.5) * 2
v:setPos( v:pos() + vec2.new( x, y ) )
end
end
end
end
|