Last active
December 14, 2015 01:29
-
-
Save shadmar/5007053 to your computer and use it in GitHub Desktop.
Spawn 200 ships to battle close to you.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Copyright © 2008-2013 Pioneer Developers. See AUTHORS.txt for details | |
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt | |
local scannedShips | |
local ships = {} | |
local maxships = 200 | |
local killSomething = function(ship) | |
-- scan for ships and exclude itself and player, so we can just watch. | |
scannedShips = Space.GetBodies(function (body) return body:isa("Ship") and body ~= ship and body~=Game.player end) | |
if #scannedShips > 0 then | |
-- concentrate fire on a few.. | |
local ks = Engine.rand:Integer(1,math.ceil(#scannedShips/30)) | |
if scannedShips[ks]:isa("Ship") and ship:isa("Ship") then | |
-- and kill them | |
ship:AIKill(scannedShips[ks]) | |
ship:SetLabel(' [kill..]') | |
scannedShips[ks]:SetLabel(' [help..]') | |
-- have the victims circle around you.. | |
if scannedShips[ks]:DistanceTo(Game.player)>100 then | |
scannedShips[ks]:AIFlyTo(Game.player) | |
end | |
else | |
if scannedShips[ks]:isa("Ship") then | |
scannedShips[ks]:SetLabel(' [idle..]') | |
end | |
if ship:isa("Ship") then | |
ship:SetLabel(' [idle..]') | |
end | |
end | |
end | |
end | |
local spawnShip = function() | |
-- find some ships to use. | |
local shipdefs = build_array(filter(function (k,def) return def.tag == 'SHIP' and def.hullMass >= 10 and def.hullMass <= 250 end, pairs(ShipDef))) | |
if #shipdefs == 0 then return end | |
local shipdef = shipdefs[Engine.rand:Integer(1,#shipdefs)] | |
local default_drive = shipdef.defaultHyperdrive | |
-- and find equipment.. | |
local max_laser_size = shipdef.capacity - EquipDef[default_drive].mass | |
local laserdefs = build_array(filter(function (k, def) return def.slot == 'LASER' and def.mass <= max_laser_size and string.sub(def.id,0,11) == 'PULSECANNON' end, pairs(EquipDef))) | |
local laserdef = laserdefs[Engine.rand:Integer(1,#laserdefs)] | |
-- create and spawn near you. | |
local ship = Space.SpawnShipNear(shipdef.id, Game.player, 10, 20) | |
-- somwhat stupid method to identify just the ones spawned. | |
ships[ship] = { | |
spawned = 'true' | |
} | |
-- and equip them | |
ship:AddEquip(default_drive) | |
ship:AddEquip(laserdef.id) | |
-- make sure everyone has work to do every 30 sec.. | |
Timer:CallEvery(30, function() | |
if ship:exists() then killSomething(ship) end | |
end) | |
return ship | |
end | |
local onEnterSystem = function (player) | |
if not player:IsPlayer() then return end | |
-- create all ships | |
local max_pirates = maxships | |
while max_pirates > 0 do | |
max_pirates = max_pirates-1 | |
local ship=spawnShip() | |
scannedShips=nil | |
end | |
end | |
Event.Register("onEnterSystem", onEnterSystem) | |
local onAICompleted = function (ship, ai_error) | |
if ship:isa("Ship") and ships[ship]~=nil then | |
ship:SetLabel(' [idle]') | |
killSomething(ship) | |
-- if #scannedShips < maxships then spawnShip() end --replenish, neverending battle | |
print(#scannedShips) -- how many are left | |
end | |
end | |
Event.Register("onAICompleted", onAICompleted) | |
local onShipDestroyed = function (ship, attacker) | |
if attacker:isa("Ship") and ships[attacker]~=nil then | |
attacker:SetLabel(' [idle]') | |
killSomething(attacker) | |
--if #scannedShips < maxships then spawnShip() end --replenish, neverending battle | |
print(#scannedShips) -- how many are left | |
end | |
end | |
Event.Register("onShipDestroyed", onShipDestroyed) | |
local onGameStart = function () | |
onEnterSystem(Game.player) | |
end | |
Event.Register("onGameStart", onGameStart) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment