Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- usedWeaponSlots = {}
- usedWeaponSlots[0] = false
- usedWeaponSlots[1] = true
- usedWeaponSlots[2] = true
- usedWeaponSlots[3] = true
- usedWeaponSlots[4] = true
- usedWeaponSlots[5] = true
- cos3 = math.cos(1/60*math.pi)
- sin3 = math.sin(1/60*math.pi)
- timeIdling = 0
- TURRET = 4
- CANNON = 0
- THRESHOLD = .1
- function Update(I)
- if I:GetNumberOfMainframes() > 0 and I:GetNumberOfTargets(0) > 0 then
- timeIdling = 0
- return
- end
- if timeIdling < 20 then
- timeIdling = timeIdling + 1
- return
- end
- local right = I:GetConstructRightVector()
- local up = I:GetConstructUpVector()
- local forward = I:GetConstructForwardVector()
- local totalWeaponCount = I:GetWeaponCount()
- local turretCount = 0
- local turretTable = {}
- local info
- for weaponIndex = 0, totalWeaponCount - 1 do
- info = I:GetWeaponInfo(weaponIndex)
- if info.Valid and info.WeaponType == TURRET then
- turretCount = turretCount + 1
- turretTable[turretCount] = info.GlobalPosition
- end
- end
- local vUF = up*sin3 + forward*cos3
- local vUB = up*sin3 + -forward*cos3
- local vDF = -up*sin3 + forward*cos3
- local vDB = -up*sin3 + -forward*cos3
- local turretSpinnerCount = I:GetTurretSpinnerCount()
- local weaponCount, weaponInfo, rightness, forwardness, orientedForward, orientedUp, turretPosition
- for turretSpinnerIndex = 0, turretSpinnerCount - 1 do
- weaponCount = I:GetWeaponCountOnTurretOrSpinner(turretSpinnerIndex)
- for weaponIndex = 0, weaponCount - 1 do
- repeat
- weaponInfo = I:GetWeaponInfoOnTurretOrSpinner(turretSpinnerIndex, weaponIndex)
- if not weaponInfo.Valid or weaponInfo.WeaponType ~= CANNON or not usedWeaponSlots[weaponInfo.WeaponSlot] then
- break
- end
- rightness = Vector3.Dot(weaponInfo.CurrentDirection, right)
- if math.abs(rightness) > THRESHOLD then break end
- forwardness = Vector3.Dot(weaponInfo.CurrentDirection, forward)
- if forwardness > 0 then
- orientedForward = true
- else
- orientedForward= false
- end
- turretPosition = getTurretPosition(weaponInfo.LocalPosition, weaponInfo.GlobalPosition, turretTable, turretCount)
- if turretPosition == nil then break end
- if Vector3.Dot(weaponInfo.GlobalPosition - turretPosition, up) * weaponInfo.LocalPosition.y > 0 then
- orientedUp = true
- else
- orientedUp = false
- end
- if orientedForward and orientedUp then
- I:AimWeaponInDirectionOnTurretOrSpinner(turretSpinnerIndex, weaponIndex, vUF.x, vUF.y, vUF.z, 0)
- break
- end
- if orientedForward and not orientedUp then
- I:AimWeaponInDirectionOnTurretOrSpinner(turretSpinnerIndex, weaponIndex, vDF.x, vDF.y, vDF.z, 0)
- break
- end
- if not orientedForward and orientedUp then
- I:AimWeaponInDirectionOnTurretOrSpinner(turretSpinnerIndex, weaponIndex, vUB.x, vUB.y, vUB.z, 0)
- break
- end
- if not orientedForward and not orientedUp then
- I:AimWeaponInDirectionOnTurretOrSpinner(turretSpinnerIndex, weaponIndex, vDB.x, vDB.y, vDB.z, 0)
- break
- end
- break
- until true
- end
- end
- end
- function getTurretPosition(localWeaponPosition, globalWeaponPosition, turretTable, turretCount)
- local localSquareDistance = localWeaponPosition.sqrMagnitude
- local bestError = math.huge
- local squareDistance, bestPosition
- for i = 1, turretCount do
- squareDistance = (globalWeaponPosition - turretTable[i]).sqrMagnitude
- error = math.abs(squareDistance - localSquareDistance)
- if error < bestError then
- bestError = error
- bestPosition = turretTable[i]
- end
- end
- return bestPosition
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement