FtdのLuaでweaponIndexからタレットのIDを得る


  • この記事は「From The Depths」というゲーム(以下FtD)で使用できるLuaに関する記事です。
  • Luaに関心のあるFtDプレイヤーを対象としています。

I:AimWeaponInDirection(weaponIndex, x,y,z, weaponSlot)I:GetWeaponBlockInfo(weaponIndex)などでタレットを操作する際、weaponIndexが必要ですが、被弾などでタレットを喪失するとweaponIndexはズレるので、決め打ちでは使いにくい。
せっかくタレットにIDが振られているのでID指定でアクセスしたい。
そんな時がたまにあります。

というわけで、IDをキーにweaponIndexを値にもつテーブルを用意すれば後が楽になります。
コードは以下。

-- subconstIdをキーに、weaponIndexを要素にもつテーブルを返す
function get_weapon_index_map(I)
  local map = {}
  local subs = I:GetAllSubConstructs();
  local wc = I:GetWeaponCount()
  for weaponIndex = 0, wc - 1 do
    local wp_info = I:GetWeaponBlockInfo(weaponIndex)
    for i = 1, #subs do
      local id = subs[i]
      local sub_info = I:GetSubConstructInfo(id)

      if sub_info.Position.x == wp_info.Position.x 
      and sub_info.Position.y == wp_info.Position.y 
      and sub_info.Position.z == wp_info.Position.z then
        map[id] = weaponIndex
        break
      end
    end
  end
  return map
end

使い方はこのように。

  local id = 1
  local map = get_weapon_index_map(I)
  local blockInfo = I:GetWeaponBlockInfo(map[id])
  local last_pos = blockInfo.Position

タレット直接操作することはあまりないとは思いますが。

以上。