Modding, Mission Design, and Coding > The Scripting Workshop

Something that i wrote as i got bored...

(1/1)

Wanderer:
Cookie for the first one who guesses what it does (not for Nuke or Axem  ;) )...


--- Code: ---#Global Hooks

$GameInit:

[

getLead = function(weaponvel)
   if playerTarget ~= nil then
      -- gather required data
      local targetpos = playerTarget.Position
      local targetvel = playerTarget.Physics.Velocity
      local lenghttargetvel = targetvel:getMagnitude()
      local plrtotrg = playerPosition - targetpos
      local lenghtplrtotrg = plrtotrg: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 = plrtotrg:getDotProduct(targetvel)
      local a = (( weaponvel * weaponvel ) - ( lenghttargetvel * lenghttargetvel ))
      local b = ( 2 * trgangle )
      local c = -( lenghtplrtotrg * lenghtplrtotrg )
      local discrim = ((b * b) - 4 * a * c)
      --idiot checks, i dont think lua can handle imaginary values...
      if discrim >= 0 and a ~= 0 then
         multipl1 = (( -b + math.sqrt(discrim)) / ( 2 * a))
         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 and hv.Player ~= nil then
   frontVector = ba.createVector(0,0,100)
   playerObject = hv.Player
   playerPosition = playerObject.Position
   playerOrientation = playerObject.Orientation
   playerFrontVector = playerOrientation:unrotateVector(frontVector)
   playerFrontProj = playerPosition + playerFrontVector
   if playerObject.Target ~= nil and playerObject.Target ~= false and playerObject.Target.HitpointsLeft ~= nil then
      playerTarget = playerObject.Target
      getLead(650)
      playerOrientation = playerObject.Orientation
      playerFrontVector = playerOrientation:unrotateVector(frontVector)
      playerFrontProj = playerPosition + playerFrontVector
      plrtotrgVector = leadpos - playerPosition
      plrProjtotrgVector = leadpos - playerFrontProj
      if plrtotrgVector:getMagnitude() > plrProjtotrgVector:getMagnitude() then
         plrtotrgDot = playerFrontVector:getDotProduct(plrtotrgVector)
         dotmult1 = plrtotrgVector:getMagnitude()
         dotmult2 = playerFrontVector:getMagnitude()
         plrtotrgAngle = plrtotrgDot / (dotmult1 * dotmult2)
         if plrtotrgAngle > 0.92 then
            plrTolead = leadpos - playerPosition
            plrToleadNVec = plrTolead / (plrTolead:getMagnitude())
         else
            plrToleadNVec = playerFrontVector / (playerFrontVector:getMagnitude())
         end
      else
         plrToleadNVec = playerFrontVector / (playerFrontVector:getMagnitude())
      end
   else
      plrToleadNVec = playerFrontVector / (playerFrontVector:getMagnitude())
   end
   for u=1,#mn.Weapons do
      weapon = mn.Weapons[u]
      weapontype = weapon.Class.Name
      if weapontype == "Corona" then
         weaponLife = weapon.LifeLeft
         if weaponLife < 4.0 and weaponLife ~= 0 then
            weaponPosition = weapon.Position
            weaponOrientation = plrToleadNVec:getOrientation()
            weapon.LifeLeft = 0
            mn.createWeapon(tb.WeaponClasses["Halo"],weaponOrientation,weaponPosition,playerObject,-1)
         end
      end
   end
end

]

#End
--- End code ---

Its very hackish and works only for single weapon type pair but its a start atleast




EDIT: If the script itself isnt clear enough.. then perhaps watching this clip might help

Nuke:
well i recognize your lead reticle function. :D
what build you running this on?

Wanderer:
Phreaks build (fs2_open_r.exe dated 20070203)

Any case that script forces player ships guns to function as guns seen in I-War 2, or whatever like this blindfire thing is.. It currently uses weapon vel 650 for lead calculation. Ship should be armed with dummy weapon named 'Corona' and then the real weapon should be named 'Halo'.

Works only with player ship.

Nuke:
hey it works. and well i might add. i like how you delt with weapon timing. gives me a few ideas :D

Wanderer:
First upgrade... This time it ought to have been change global instead of player specific...


--- Code: ---#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
--- End code ---

Navigation

[0] Message Index

Go to full version