First upgrade... This time it ought to have been change global instead of player specific...
#Global Hooks
$GameInit:
[
getLead = function(weaponvel)
if currentTarget ~= nil then
-- gather required data
local targetPos = currentTarget.Position
local targetVel = currentTarget.Physics.Velocity
local targetVelLength = targetVel:getMagnitude()
local shiptotrg = currentPosition - targetPos
local plrtotrglength = shiptotrg:getMagnitude()
--trigonometry 101...
--use cosine to get the interception time basicaly derived from c^2 = a^2 + b^2 - 2ab cos(C)
--use dotproduct to get ab cos(C)
local trgangle = shiptotrg:getDotProduct(targetVel)
local a = (( weaponvel * weaponvel ) - ( targetVelLength * targetVelLength ))
local b = ( 2 * trgangle )
local c = -( plrtotrglength * plrtotrglength )
local discrim = ((b * b) - 4 * a * c)
--idiot checks, i dont think lua can handle imaginary values...
if discrim >= 0 and a ~= 0 then
local multipl1 = (( -b + math.sqrt(discrim)) / ( 2 * a))
local multipl2 = (( -b - math.sqrt(discrim)) / ( 2 * a))
--we dont want negative lead do we?
if multipl1 >=0 and multipl1 <= multipl2 and multipl2 >= 0 then
targetmult = multipl1
elseif multipl1 >=0 and multipl2 < 0 then
targetmult = multipl1
elseif multipl2 >=0 then
targetmult = multipl2
else targetmult = nil
end
--with interception time we get the interception coordinates
if targetmult ~= nil then
if targetmult ~= 0 then
leadvel = targetVel/(1/targetmult)
else
leadvel = 0
end
leadpos = targetPos + leadvel
end
end
end
end
]
$Simulation:
[
missiontime = mn.getMissionTime()
if missiontime > 0.5 then
frontVector = ba.createVector(0,0,100)
nullVec = ba.createVector(0,0,0)
shipleadVecs = nil
shipleadVecs = {}
for y=1,#mn.Ships do
shipleadVecs[y] = "false"
local currentObject = mn.Ships[y]
local currentOrientation = currentObject.Orientation
blindfirearmed = 0
local weaponPrimaryBanks = currentObject.PrimaryBanks
for j=1,#weaponPrimaryBanks do
local weaponBank = currentObject.PrimaryBanks[j]
local weaponBankWeapon = weaponBank.WeaponClass.Name
if weaponBankWeapon == "Corona" then
blindfirearmed = 1
end
end
if blindfirearmed == 1 and currentObject.Target ~= nil and currentObject.Target ~= false and currentObject.Target:getBreedName() ~= (null) then
currentPosition = currentObject.Position
local currentFrontVector = currentOrientation:unrotateVector(frontVector)
local currentFrontProj = currentPosition + currentFrontVector
currentTarget = currentObject.Target
getLead(650)
local shiptotargetVector = leadpos - currentPosition
local shiptotargetProVector = leadpos - currentFrontProj
if shiptotargetVector:getMagnitude() > shiptotargetProVector:getMagnitude() then
local shiptotargetDot = currentFrontVector:getDotProduct(shiptotargetVector)
local dotmult1 = shiptotargetVector:getMagnitude()
local dotmult2 = currentFrontVector:getMagnitude()
local shiptotargetAngle = shiptotargetDot / (dotmult1 * dotmult2)
if shiptotargetAngle > 0.92 then
local shipTolead = leadpos - currentPosition
shipToleadNorVec = shipTolead / (shipTolead:getMagnitude())
shipleadVecs[y] = shipToleadNorVec
end
end
end
end
for u=1,#mn.Weapons do
local weapon = mn.Weapons[u]
local weapontype = weapon.Class.Name
local weaponLife = weapon.LifeLeft
if weapontype == "Corona" and weaponLife > 0.2 then
local weaponPosition = weapon.Position
shiplist = nil
shiplist = {}
shooterlist = nil
shooterlist = {}
for h = 1,#mn.Ships do
local shooter = mn.Ships[h]
local shooterPos = shooter.Position
local distanceVec = weaponPosition - shooterPos
local distance = math.floor(distanceVec:getMagnitude())
table.insert(shiplist,distance)
table.insert(shooterlist,distance,h)
table.sort(shiplist)
end
newdistance = shiplist[1]
newindex = shooterlist[newdistance]
newshooter = mn.Ships[newindex]
weaponOrientation = weapon.Orientation
newvector = shipleadVecs[newindex]
if newvector ~= "false" then
weaponOrientation = newvector:getOrientation()
end
weapon.LifeLeft = 0
mn.createWeapon(tb.WeaponClasses["Halo"],weaponOrientation,weaponPosition,newshooter,-1)
end
end
end
]
#End