Advertisement
Guest User

IdleScript

a guest
Sep 21st, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. usedWeaponSlots = {}
  2. usedWeaponSlots[0] = false
  3. usedWeaponSlots[1] = true
  4. usedWeaponSlots[2] = true
  5. usedWeaponSlots[3] = true
  6. usedWeaponSlots[4] = true
  7. usedWeaponSlots[5] = true
  8.  
  9.  
  10. cos3 = math.cos(1/60*math.pi)
  11. sin3 = math.sin(1/60*math.pi)
  12. timeIdling = 0
  13. TURRET = 4
  14. CANNON = 0
  15. THRESHOLD = .1
  16.  
  17. function Update(I)
  18.   if I:GetNumberOfMainframes() > 0 and I:GetNumberOfTargets(0) > 0 then
  19.     timeIdling = 0
  20.     return
  21.   end
  22.   if timeIdling < 20 then
  23.     timeIdling = timeIdling + 1
  24.     return
  25.   end
  26.   local right = I:GetConstructRightVector()
  27.   local up = I:GetConstructUpVector()
  28.   local forward = I:GetConstructForwardVector()
  29.   local totalWeaponCount = I:GetWeaponCount()
  30.   local turretCount = 0
  31.   local turretTable = {}
  32.   local info
  33.   for weaponIndex = 0, totalWeaponCount - 1 do
  34.     info = I:GetWeaponInfo(weaponIndex)
  35.     if info.Valid and info.WeaponType == TURRET then
  36.       turretCount = turretCount + 1
  37.       turretTable[turretCount] = info.GlobalPosition
  38.     end
  39.   end
  40.  
  41.   local vUF = up*sin3 + forward*cos3
  42.   local vUB = up*sin3 + -forward*cos3
  43.   local vDF = -up*sin3 + forward*cos3
  44.   local vDB = -up*sin3 + -forward*cos3
  45.   local turretSpinnerCount = I:GetTurretSpinnerCount()
  46.   local weaponCount, weaponInfo, rightness, forwardness, orientedForward, orientedUp, turretPosition
  47.   for turretSpinnerIndex = 0, turretSpinnerCount - 1 do
  48.     weaponCount = I:GetWeaponCountOnTurretOrSpinner(turretSpinnerIndex)
  49.     for weaponIndex = 0, weaponCount - 1 do
  50.       repeat
  51.       weaponInfo = I:GetWeaponInfoOnTurretOrSpinner(turretSpinnerIndex, weaponIndex)
  52.       if not weaponInfo.Valid or weaponInfo.WeaponType ~= CANNON or not usedWeaponSlots[weaponInfo.WeaponSlot] then
  53.         break
  54.       end
  55.       rightness = Vector3.Dot(weaponInfo.CurrentDirection, right)
  56.       if math.abs(rightness) > THRESHOLD then break end
  57.       forwardness = Vector3.Dot(weaponInfo.CurrentDirection, forward)
  58.       if forwardness > 0 then
  59.         orientedForward = true
  60.       else
  61.         orientedForward= false
  62.       end
  63.       turretPosition = getTurretPosition(weaponInfo.LocalPosition, weaponInfo.GlobalPosition, turretTable, turretCount)
  64.       if turretPosition == nil then break end
  65.       if Vector3.Dot(weaponInfo.GlobalPosition - turretPosition, up) * weaponInfo.LocalPosition.y > 0 then
  66.         orientedUp = true
  67.       else
  68.         orientedUp = false
  69.       end
  70.       if orientedForward and orientedUp then
  71.         I:AimWeaponInDirectionOnTurretOrSpinner(turretSpinnerIndex, weaponIndex, vUF.x, vUF.y, vUF.z, 0)
  72.         break
  73.       end
  74.       if orientedForward and not orientedUp then
  75.         I:AimWeaponInDirectionOnTurretOrSpinner(turretSpinnerIndex, weaponIndex, vDF.x, vDF.y, vDF.z, 0)
  76.         break
  77.       end
  78.       if not orientedForward and orientedUp then
  79.         I:AimWeaponInDirectionOnTurretOrSpinner(turretSpinnerIndex, weaponIndex, vUB.x, vUB.y, vUB.z, 0)
  80.         break
  81.       end
  82.       if not orientedForward and not orientedUp then
  83.         I:AimWeaponInDirectionOnTurretOrSpinner(turretSpinnerIndex, weaponIndex, vDB.x, vDB.y, vDB.z, 0)
  84.         break
  85.       end
  86.       break
  87.       until true
  88.     end
  89.   end
  90. end
  91.  
  92. function getTurretPosition(localWeaponPosition, globalWeaponPosition, turretTable, turretCount)
  93.   local localSquareDistance = localWeaponPosition.sqrMagnitude
  94.   local bestError = math.huge
  95.   local squareDistance, bestPosition
  96.   for i = 1, turretCount do
  97.     squareDistance = (globalWeaponPosition - turretTable[i]).sqrMagnitude
  98.     error = math.abs(squareDistance - localSquareDistance)
  99.     if error < bestError then
  100.       bestError = error
  101.       bestPosition = turretTable[i]
  102.     end
  103.   end
  104.   return bestPosition
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement