Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Script to find armor data on dwarves and in stockpile  (Read 795 times)

senger

  • Escaped Lunatic
    • View Profile
Script to find armor data on dwarves and in stockpile
« on: August 23, 2018, 06:36:07 pm »

Hi! Im looking for a helping hand in writing a lua script, but Im don't know how to get this done.

The problem: some of my military dwarves ale missing armor pieces, and some pieces could be upgraded from copper (abundance) to iron/bronze/steel (few bars from caravan/melting), now I would want to know:
1. how many missing copper gauntlets I need to forge
2. how many copper mail shirts are equipped so I can forge few iron/bronze ones
3. also how many copper mail shirts can be replaced with iron/bronze breastplates/mail shirts from the ones in stockpiles
so basically statistics of equipped/missing/stockpiled armor pieces.

How to access these things in a lua script ran by dfhack?
How to get (and print with names) a list of missing/equipped armor on my military dwarves?
How to iterate through stockpiles and find metal armor pieces there?

I am a java programmer, but have no knowledge of lua, so any help will be appreciated :) I did read some dfhack scripts (gui/choose_weapons) but didn't what to do with the data...
Also not sure if this is right board for this question, please move if needed.
 
Logged

PatrikLundell

  • Bay Watcher
    • View Profile
Re: Script to find armor data on dwarves and in stockpile
« Reply #1 on: August 24, 2018, 02:07:46 am »

Firstly, I think you approach this from the wrong direction:
- How many militia members do you have? That's the number of high quality items you need.
- If you have an abundance of copper, just churn out copper gear as training. I don't touch the high quality stuff until my smith has been trained.

To do just about anything in script or High Level Assembly (a.k.a. C(++), of which Java is more or less a script version with some safety nets typically present in high level languages) you need to be able to find what you're looking for in DF's data structure. Almost everything is somewhere in df.global.world. If I remember correctly, you ought to continue to "items" and then an appropriate sub category, rather than look at what's in stockpile and unit inventories. use "gui/gm-editor df.global.world" to browse the structure.

The Lua "for" iterator takes some getting used to. Also look at this document https://dfhack.readthedocs.io/en/latest/docs/Lua%20API.html#id12.
Logged

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: Script to find armor data on dwarves and in stockpile
« Reply #2 on: August 24, 2018, 08:57:25 am »

On 2., note the delay dwarves take with changing gear - you may have situations where they still have copper mail on despite iron ones in stockpile if they haven't checked for replacement yet. Producing based on that will give you overstock.

Tbh, I'd instead rec using manager (requires stockpile logic for quality check) or workflow to maintain a stock of 1-10 sets of military armour as gear claimed by dwarves isn't counted towards available armour and dwarftherapist to check they are actually putting them on.

But if you want, note that abstract uniforms are under df.global.ui.main.fortress_entity.uniforms, while their actual equipment (both assigned as individual's uniform and their picked match) is under df.global.world.squads (also lists non-fortress squads). What they actually have on is under <unit instance>.inventory. I recommend looking around with gui/gm-editor to better understand the structures therein.

senger

  • Escaped Lunatic
    • View Profile
Re: Script to find armor data on dwarves and in stockpile
« Reply #3 on: August 27, 2018, 08:00:59 pm »

I know how many armor sets I should have, but I don't know how many sets I have (both in stockpiles and dwarves inventory), albo how many should be forged. I there any way to list items in use (in inventory)? I was lucky enough to get two legendary armoring dwarves, so training is not necessary :)

Oh god, thank you for the gm-editor! I did explore a bit, but got confused, Urist McSpearman had a Steel Spear with id 32895, but world.items.all had only ~28k entries, so I did not find the spear id, I guess these are separate item lists? Also the spear had only mat_intex = 8 (material = steel?), but I couldn't find material name. Will explore further.

Logged

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: Script to find armor data on dwarves and in stockpile
« Reply #4 on: August 27, 2018, 09:17:35 pm »

How many sets total? Well, you can just count the parts in df.global.world.items, which includes held armor (normal manager doesn't count those).

Sometimes, items get deleted (atomsmashing/magma sea I think?), so you can have shorter list in df.global.world.items.all - the actual id of the item is listed inside the entry. You can find the weapon with df.item.find(32895).

And yeah, 8 is steel (specifically, df.global.world.raws.inorganics[8]).

PatrikLundell

  • Bay Watcher
    • View Profile
Re: Script to find armor data on dwarves and in stockpile
« Reply #5 on: August 28, 2018, 02:24:54 am »

As Fleeting Frames indicated, the item list isn't "dense" with respect to Id. Each item has an Id, which includes that roast Urist McSated just ate, so things come and go, and I don't think Ids are reused. It's the same with many other lists, in particular Units, as units enter and leave.
DFHack uses a counter intuitive way of finding items in many of those lists, namely a "find" function on the "struct" type the elements of the list belongs to (rather than the lists that are to be searched). That "find" function then "knows" which list to search in (using binary search).

If you're iterating over the item list to find what you've got in total, you need to check whether the items are held by your units or visitors. Things in invader and visiting merc inventories are all items it the big item list...
Logged

Paralyzoid

  • Bay Watcher
    • View Profile
Re: Script to find armor data on dwarves and in stockpile
« Reply #6 on: August 30, 2018, 08:13:53 pm »

Each item has an Id, which includes that roast Urist McSated just ate, so things come and go, and I don't think Ids are reused.

So does this mean that as you keep playing, you'll eventually hit the max ID?

One day Urist McShearer is herding a pet llama along to the workshop, and as soon as he makes the first snip, a black hole opens...
Logged
He felt aroused after seeing the goblin Ramkal Drunkensiege's dead body.

Saiko Kila

  • Bay Watcher
  • Dwarven alchemist
    • View Profile
Re: Script to find armor data on dwarves and in stockpile
« Reply #7 on: August 31, 2018, 02:00:45 am »

Each item has an Id, which includes that roast Urist McSated just ate, so things come and go, and I don't think Ids are reused.

So does this mean that as you keep playing, you'll eventually hit the max ID?

One day Urist McShearer is herding a pet llama along to the workshop, and as soon as he makes the first snip, a black hole opens...

Possible,  because it's only 32-bit (I know another game where this limit hit me two times, and in the sequel the designers increased it to 64-bit). However, once the last number is achieved, they are rolled over and used again. I don't know what happens if there is a collision... Maybe nothing.
Logged

Dozebôm Lolumzalěs

  • Bay Watcher
  • what even is truth
    • View Profile
    • test
Re: Script to find armor data on dwarves and in stockpile
« Reply #8 on: August 31, 2018, 03:12:14 am »

I think this might fit better in the DF Modding board. People ask for help there and receive it, I think, and it's generally all-around preferable for threads to be categorized well. (This board is geared more for helping people navigate the raid system than the internal code of the game.)
Logged
Quote from: King James Programming
...Simplification leaves us with the black extra-cosmic gulfs it throws open before our frenzied eyes...
Quote from: Salvané Descocrates
The only difference between me and a fool is that I know that I know only that I think, therefore I am.
Sigtext!