Page 1 of 1

Expose ammo details via API

Posted: Sat May 28, 2016 11:45 pm
by sparr
I would like to be able to access ammo_type (and all of its contents) via the API.

My specific use case is to make the AutoFill mod smarter, so it can figure out which ammo items go in which weapons (shotgun shells don't go in gun turrets) and which ones do the most damage.

Here's some code I wrote that would use this feature, if the data were exposed as it currently exists in the prototype lua files:

Code: Select all

  updateAmmoArrays = function(tbl)
    tbl["ammo-bullets"] = {}
    local bul = tbl["ammo-bullets"]
    
    if bul then
      for name, item in pairs(game.item_prototypes) do
        if item.type == "ammo" then
          if item.ammo_type ~= nil then
            if item.ammo_type.category == "bullet" then
              bul[#bul + 1] = name
            end
          end
        end
      end
    end

    local ammoTotalDamage = function(name)
      local item = game.item_prototypes[name]
      local d_tot = 0
      if item.ammo_type.action then
        if item.ammo_type.action.action_delivery then
          if item.ammo_type.action.action_delivery.target_effects then
            for e in item.ammo_type.action.action_delivery.target_effects do
              if e.type == "damage" then
                d_tot = d_tot + e.damage.amount
              end
            end
          end
        end
      end
      return d_tot
    end

    local ammoHighToLow = function(a,b)
      return ammoTotalDamage(a) > ammoTotalDamage(b)
    end

    table.sort(bul, ammoHighToLow)
  end  

Re: Expose ammo details via API

Posted: Sat May 28, 2016 11:50 pm
by daniel34
Moved to Modding interface requests.