How to determine inventory weapon slot and ammo type a weapon has loaded?

sLoPpYdOtBiGhOlE
Scavenger
Posts: 36
Joined: 12 Jun 2017, 23:44

How to determine inventory weapon slot and ammo type a weapon has loaded?

Postby sLoPpYdOtBiGhOlE » 28 Jul 2017, 21:53

Hi again,
Q1. How do I check if a weapon in my inventory is in a weapon slot?

Q2. How do I determine the type of ammo a weapon in my inventory currently has loaded?

I'm writing a simple script function to Unload All Weapons in player inventory, apart from slotted weapons.
Crude function lacking the above 2 questions of coarse:

Code: Select all

local function unload_all_weapons()
   local npc = db.actor
   local t = {}
   local function itr(npc,item)
      if (string.find(item:name(), "wpn_")) then
         
         --if not (item in weapon slot ?) then
            
            local i = item:get_ammo_in_magazine()
            if (i > 0) then
               printf("%s ammo count: %s", item:name(), i)

               --local type_ammo = item: get ammo type?
               
               local type_ammo = "ammo_5.56x45_ap" -- test ammo place holder

               if (t[type_ammo] == nil) then
                  t[type_ammo] = i
               else
                  t[type_ammo] = t[type_ammo] + i
               end

               item:unload_magazine()
            end
         
         --end
      end
   end
   npc:iterate_inventory(itr,npc)
   for k,v in pairs(t) do
      printf("k: %s = v: %s", k, v)
      create_ammo(k,npc:position(),npc:level_vertex_id(),npc:game_vertex_id(),npc:id(), v)
   end
end


Sorry for below novice scripter questions.
I'm still finding my way around on how to find info on functions, parameters, use and returns.
eg: most functions I'm finding in lua_help.script.
Then I usually look in the various other scripts that ship with CoC to see the way a function is used.
I still haven't found a wiki or page that I can look up what the actual functions return or their uses or expected paramters.
Apart from standard LUA pages, but they don't really help with functions that are written specifically for stalker.

Learning curve is steap since I have no prior LUA script experience..lol
The only scripting languages I've used are AutoIt and Papyrus.
In most cases I can usually piece things together a bit of reading and experimenting.

User avatar
Alundaio
S.T.A.L.K.E.R.
Posts: 1371
Joined: 26 May 2012, 22:26

Re: How to determine inventory weapon slot and ammo type a weapon has loaded?

Postby Alundaio » 28 Jul 2017, 22:52

Well, you can make a table of all the items in the actor's slots, like:

Code: Select all

   local slot_list = {}
   for i=1,12 do
      local itm = db.actor:item_in_slot(i)
      if (itm) then
         slot_list[itm:id()] = true
      end
   end



Then in the iterator function itr, you can add the conditional: if (slot_list[item:id()] == true) then


For question two, you need to use packet editing. In 1.5 CoC, there will be a way to get it without doing this.

Code: Select all

   local data = stpk_utils.get_weapon_data(se_obj)
   if not (data) then
      return
   end
   
   data.ammo_type


ammo_type will usually return a number between 0 and 1. It corresponds to the ammo_class field in the weapon section, in configs.

so if ammo_class = ammo_5.45x39_fmj, ammo_5.45x39_ap

That means ammo_type value of 0 will mean the weapon has fmj. 1 will mean it has ap.

---------
in 1.5 it will be as easy as doing:

local se_obj = alife_object(item:id())
se_obj.ammo_type
"I have a dream that one day this community will rise up and live out the true meaning of its creed: "We hold these truths to be self-evident; that all mods are created equal."

sLoPpYdOtBiGhOlE
Scavenger
Posts: 36
Joined: 12 Jun 2017, 23:44

Re: How to determine inventory weapon slot and ammo type a weapon has loaded?

Postby sLoPpYdOtBiGhOlE » 28 Jul 2017, 23:17

Thank You, greatly appreciated :)
That's the info I was looking for but I just couldn't find an example to base off.
Now I can commit to what I was doing and post another small addon (after some testing) in the near future.

sLoPpYdOtBiGhOlE
Scavenger
Posts: 36
Joined: 12 Jun 2017, 23:44

Re: How to determine inventory weapon slot and ammo type a weapon has loaded?

Postby sLoPpYdOtBiGhOlE » 29 Jul 2017, 00:57

Just to let you know got it working as expected.
My code looks untidy, but it's fully functional... lol

Code: Select all

local function unload_all_weapons()
   local npc = db.actor
   local ts = {}
   for i=1,12 do
      local itm = npc:item_in_slot(i)
      if (itm) then
         ts[itm:id()] = true
      end
   end
   local ta = {}
   local ini = system_ini()
   local function itr(npc,item)
      if (string.find(item:name(), "wpn_")) then
         if not (ts[item:id()]) then
            local i = item:get_ammo_in_magazine()
            if (i > 0) then
               printf("%s ammo count: %s", item:name(), i)
               local se_obj = item and alife():object(item:id())
               local data = se_obj and stpk_utils.get_weapon_data(se_obj)
               if (data) then
                  local s = ini:r_string_ex(item:section(),"ammo_class","")
                  if (s and s ~= "") then
                     local p = alun_utils.str_explode(s,",")
                     if (p ~= nil) then
                        s = p[data.ammo_type + 1]
                        if (s and s ~= "") then
                           if (ta[s] == nil) then
                              ta[s] = i
                           else
                              ta[s] = ta[s] + i
                           end
                           item:unload_magazine()
                        end
                     end
                  end
               end
            end
         end
      end
   end
   npc:iterate_inventory(itr,npc)
   for k,v in pairs(ta) do
      printf("Ammo k: %s = v: %s", k, v)
      create_ammo(k,npc:position(),npc:level_vertex_id(),npc:game_vertex_id(),npc:id(), v)
   end
end

Surprised how easy it comes together when you have a script whiz like you feeding the info :)

Thank You


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest