Expose ammo details via API

Post Reply
sparr
Smart Inserter
Smart Inserter
Posts: 1331
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Expose ammo details via API

Post 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  

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Expose ammo details via API

Post by daniel34 »

Moved to Modding interface requests.
quick links: log file | graphical issues | wiki

Post Reply

Return to “Implemented mod requests”