<commit_before>--[[
Weapon guidance AI, version 0.2.1.0
https://github.com/Blothorn/FTD for further documentation and license.
--]]

-- Globals
TargetBufferSize = 120
TTTIterationThreshold = 5
TTTMaxIterations = 4

TargetLists = {
  AA = {
    MainframeIndex = 0,
    MinimumSpeed = 0,
    MaximumSpeed = 350,
    MinimumAltitude = -5,
    MaximumAltitude = 99999,
    MaximumRange = 1200,
    TTT = 5,
    Depth = 2,
  }
}

WeaponSystems = {}

-- Sample configuration (Dart AA thumpers)
WeaponSystems[1] = {
  Type = 2,
  TargetList = 'AA',
  MaximumAltitude = 99999,
  MinimumAltitude = -3,
  MaximumRange = 900,
  MinimumRange = 100,
  FiringAngle = 60,
  Speed = 175,
  LaunchDelay = 0.3,
  MinimumConvergenceSpeed = 150,
  Endurance = 5,
  MinimumCruiseAltitude = 3,
  AttackPatterns = {Vector3(-15,0,0), Vector3(15,0,0)},
  PatternConvergeTime = 0.3,
  PatternTimeCap = 3,
}

Flag = 0
Normalized = false

DefaultSecantInterval = function(ttt) return math.min(math.ceil(40*ttt/2), 100) end

-- Target buffers
Targets = {}
Missiles = {}

for i = 0, 5 do
  if WeaponSystems[i] and WeaponSystems[i].Type == 2 then
    DefaultMissileGroup = i
    break
  end
end

-- Normalize weapon configuration
function Normalize(I)
  for i = 0, 5 do
    if WeaponSystems[i] then
      local ws = WeaponSystems[i]
      ws.NextFire = -9999
      if ws.AimPointProportion and ws.AimPointProportion > 0 then
        ws.AimPointCounter = 1
      end
      if ws.AttackPatterns then ws.AttackPatternIndex = 1 end
      if not ws.Stagger then ws.Stagger = 0 end
      ws.Fired = 0
      if not ws.VolleySize then ws.VolleySize = 1 end
    end
  end
  for k, tl in pairs(TargetLists) do
    if not tl.Depth then tl.Depth = 1 end
  end
end

function NewTarget(I)
  return {
    AimPoints = {},
    Index = 0,
    Wrapped = 0,
    AimPointIndex = 0,
    NumMissiles = 0,
    NumFired = 0,
  }
end

function UpdateTargets(I, gameTime)
  -- Find all target locations
  local nmf = I:GetNumberOfMainframes()
  local TargetLocations = {}

  -- Aimpoint locations
  for ti = 0, I:GetNumberOfTargets(0) - 1 do
    local t = I:GetTargetInfo(0,ti)
    TargetLocations[t.Id] = {t.AimPointPosition}
  end

  -- Non-aimpoint locations
  for mfi = 1, nmf - 1 do
    for ti = 0, I:GetNumberOfTargets(mfi) do
      local t = I:GetTargetInfo(mfi,ti)
      if TargetLocations[t.Id] and t.AimPointPosition ~= TargetLocations[t.Id][1] then
        table.insert(TargetLocations[t.Id], t.AimPointPosition)
      end
    end
  end

  -- Find priority targets
  for tli, tl in pairs(TargetLists) do
    local m = (tl.MainframeIndex < nmf and tl.MainframeIndex) or 0

    -- Find qualifying target
    tl.PresentTarget = {}
    local num = 1
    for tInd = 0, I:GetNumberOfTargets(m) - 1 do
       local t = I:GetTargetInfo(m,tInd)
       local speed = Vector3.Magnitude(t.Velocity)
       local interceptPoint = t.Position + t.Velocity * tl.TTT
       if t.Protected and TargetLocations[t.Id]
         and (speed >= tl.MinimumSpeed) and (speed < tl.MaximumSpeed)
         and (Vector3.Distance(I:GetConstructPosition(), interceptPoint) < tl.MaximumRange) then
        local found = false
        for k, p in ipairs(TargetLocations[t.Id]) do
          if (p.y > tl.MinimumAltitude) and (p.y < tl.MaximumAltitude) then
            found = true
            break
          end
        end<commit_msg>Fixed indexing error.<commit_after>--[[
Weapon guidance AI, version 0.2.1.1
https://github.com/Blothorn/FTD for further documentation and license.
--]]

-- Globals
TargetBufferSize = 120
TTTIterationThreshold = 5
TTTMaxIterations = 4

TargetLists = {
  AA = {
    MainframeIndex = 0,
    MinimumSpeed = 0,
    MaximumSpeed = 350,
    MinimumAltitude = -5,
    MaximumAltitude = 99999,
    MaximumRange = 1200,
    TTT = 5,
    Depth = 2,
  }
}

WeaponSystems = {}

-- Sample configuration (Dart AA thumpers)
WeaponSystems[1] = {
  Type = 2,
  TargetList = 'AA',
  MaximumAltitude = 99999,
  MinimumAltitude = -3,
  MaximumRange = 900,
  MinimumRange = 100,
  FiringAngle = 60,
  Speed = 175,
  LaunchDelay = 0.3,
  MinimumConvergenceSpeed = 150,
  Endurance = 5,
  MinimumCruiseAltitude = 3,
  AttackPatterns = {Vector3(-15,0,0), Vector3(15,0,0)},
  PatternConvergeTime = 0.3,
  PatternTimeCap = 3,
}

Flag = 0
Normalized = false

DefaultSecantInterval = function(ttt) return math.min(math.ceil(40*ttt/2), 100) end

-- Target buffers
Targets = {}
Missiles = {}

for i = 0, 5 do
  if WeaponSystems[i] and WeaponSystems[i].Type == 2 then
    DefaultMissileGroup = i
    break
  end
end

-- Normalize weapon configuration
function Normalize(I)
  for i = 0, 5 do
    if WeaponSystems[i] then
      local ws = WeaponSystems[i]
      ws.NextFire = -9999
      if ws.AimPointProportion and ws.AimPointProportion > 0 then
        ws.AimPointCounter = 1
      end
      if ws.AttackPatterns then ws.AttackPatternIndex = 1 end
      if not ws.Stagger then ws.Stagger = 0 end
      ws.Fired = 0
      if not ws.VolleySize then ws.VolleySize = 1 end
    end
  end
  for k, tl in pairs(TargetLists) do
    if not tl.Depth then tl.Depth = 1 end
  end
end

function NewTarget(I)
  return {
    AimPoints = {},
    Index = 0,
    Wrapped = 0,
    AimPointIndex = 0,
    NumMissiles = 0,
    NumFired = 0,
  }
end

function UpdateTargets(I, gameTime)
  -- Find all target locations
  local nmf = I:GetNumberOfMainframes()
  local TargetLocations = {}

  -- Aimpoint locations
  for ti = 0, I:GetNumberOfTargets(0) - 1 do
    local t = I:GetTargetInfo(0,ti)
    TargetLocations[t.Id] = {t.AimPointPosition}
  end

  -- Non-aimpoint locations
  for mfi = 1, nmf - 1 do
    for ti = 0, I:GetNumberOfTargets(mfi) - 1 do
      local t = I:GetTargetInfo(mfi,ti)
      if TargetLocations[t.Id] and t.AimPointPosition ~= TargetLocations[t.Id][1] then
        table.insert(TargetLocations[t.Id], t.AimPointPosition)
      end
    end
  end

  -- Find priority targets
  for tli, tl in pairs(TargetLists) do
    local m = (tl.MainframeIndex < nmf and tl.MainframeIndex) or 0

    -- Find qualifying target
    tl.PresentTarget = {}
    local num = 1
    for tInd = 0, I:GetNumberOfTargets(m) - 1 do
       local t = I:GetTargetInfo(m,tInd)
       local speed = Vector3.Magnitude(t.Velocity)
       local interceptPoint = t.Position + t.Velocity * tl.TTT
       if t.Protected and TargetLocations[t.Id]
         and (speed >= tl.MinimumSpeed) and (speed < tl.MaximumSpeed)
         and (Vector3.Distance(I:GetConstructPosition(), interceptPoint) < tl.MaximumRange) then
        local found = false
        for k, p in ipairs(TargetLocations[t.Id]) do
          if (p.y > tl.MinimumAltitude) and (p.y < tl.MaximumAltitude) then
            found = true
            break
          end
        end
{'fraction_non_alphanumeric': 0.10264802142219577, 'fraction_numerical': 0.03198452841416245, 'mean_word_length': 2.7123136388735505, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '624937', 'n_tokens_mistral': 2603, 'n_tokens_neox': 2348, 'n_words': 634}