Skip to content

Instantly share code, notes, and snippets.

@tuefekci
Last active October 15, 2022 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuefekci/baadb5acd4dbb372b1575962712591e9 to your computer and use it in GitHub Desktop.
Save tuefekci/baadb5acd4dbb372b1575962712591e9 to your computer and use it in GitHub Desktop.
Factorio MoonLogic AAI Attack Squad
out = {}
--var = {}
delay = 1
-- =====================================================
-- Functions
-- =====================================================
function math.average(t)
local sum = 0
for _,v in pairs(t) do -- Get the sum of all numbers in t
sum = sum + v
end
return sum / #t
end
function min(t)
local min = math.huge
for i = 1, #t do
min = min < t[i] and min or t[i]
end
return min
end
function max(a)
local values = {}
for k,v in pairs(a) do
values[#values+1] = v
end
table.sort(values) -- automatically sorts lowest to highest
return values[#values]
end
function count(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
-- =====================================================
-- Const
-- =====================================================
unitId = red['vehicle-chaingunner-vehicle-chaingunner-gun-_-signal']
-- // Only if valid unit
if unitId > 0 then
var['units'] = red['signal-count']
-- =====================================================
-- Checks
-- =====================================================
-- =====================================================
-- Distance
if var['distance'] == nil or count(var['distance']) > var['units'] then
var['distance'] = {}
end
distance = 0
distance = distance + red['signal-range']
var['distance'][unitId] = distance
var['max-distance'] = max(var['distance'])
-- =====================================================
-- Health Check
if var['health'] == nil or count(var['health']) > var['units'] then
var['health'] = {}
end
health = 0
health = health + red['signal-health']
var['health'][unitId] = health
-- =====================================================
-- Ammo Check
if var['ammo'] == nil or count(var['ammo']) > var['units'] then
var['ammo'] = {}
end
ammo = 0
ammo = ammo + red['firearm-magazine']
ammo = ammo + red['piercing-rounds-magazine']
var['ammo'][unitId] = ammo
-- =====================================================
-- Fuel Check
if var['fuel'] == nil or count(var['fuel']) > var['units'] then
var['fuel'] = {}
end
fuel = 0
fuel = fuel + red['processed-fuel']
var['fuel'][unitId] = fuel
--game.print(min(var['health']))
-- =====================================================
-- Logic
-- =====================================================
if red['zone-x-red'] == nil then
red['zone-x-red'] = 0
end
out['vehicle-chaingunner-vehicle-chaingunner-gun-_-signal'] = unitId
if red['vehicle-chaingunner-vehicle-chaingunner-gun-_-signal'] == 1 then
var['commander-id'] = red['signal-id']
end
if red['zone-x-red'] == 1 and var['units'] > 4 and min(var['health']) > 200 and min(var['ammo']) > 25 and min(var['fuel']) > 20 then
var['action'] = "attack"
else
var['action'] = "depot"
end
if var['action'] == "attack" then
if red['signal-id'] == var['commander-id'] then
if max(var['distance']) > 25 then
var['commander-pause'] = 1
out['signal-speed'] = 1
else
var['commander-pause'] = 0
out['signal-speed'] = 500
out['signal-x-tile'] = red['signal-x-tile']
out['signal-y-tile'] = red['signal-y-tile']
end
else
if red['signal-time-move'] > 10000 and distance > 10 then
out['signal-x-tile'] = red['signal-x-tile'] + 10
out['signal-y-tile'] = red['signal-y-tile'] + 10
else
out['signal-follow-id'] = var['commander-id']
end
end
out['signal-red'] = 1
attack = 1
var['action'] = "attack"
end
if var['action'] == "depot" then
if red['signal-time-move'] > 10000 and distance > 10 then
out['signal-x-tile'] = green['signal-x-tile'] + 10
out['signal-y-tile'] = green['signal-y-tile'] + 10
else
out['signal-x-tile'] = green['signal-x-tile']
out['signal-y-tile'] = green['signal-y-tile']
end
out['signal-black'] = 1
attack = 0
if var['action'] == "attack" or var['action'] == "depot" then
var['min-health'] = nil
var['min-ammo'] = nil
var['min-fuel'] = nil
end
var['action'] = "depot"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment