Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Request: DFHack Plugin/Script forbid fertilized eggs  (Read 2227 times)

Falco

  • Escaped Lunatic
    • View Profile
Request: DFHack Plugin/Script forbid fertilized eggs
« on: November 20, 2017, 01:10:52 pm »

A request for a script or plugin that forbids fertilized eggs that are in nestboxes.
I'm not all that good in lua. And the API confuses me. I though I'd hack it together really quickly but I'm kinda stuck, so I'm wondering if someone may want to do this for me instead.
I think it'd be a nice inclusion overall.

Specs:
- Runs over the entire map regularly (maybe with repeat)
either
- Finds fertilized eggs and checks if they're in a nestbox
or
- finds all nestboxes and checks if there are fertilized eggs inside

- it forbids those eggs
- other eggs should keep their status as they are
- a nice help function, explaining all parameters
- a nice description
optional:
either
- have a list of egg types and choose which to forbid and which to take
- have a nice gui to toggle
- save the list with the game (without breaking anything, even with new version of plugin or when plugin deactivated)
or
- have command line argument for specific egg type to check for (ex. hen egg)

Here is what I have so far. it's mostly semi-pseudo code, very buggy and not working at all:
Code: [Select]
local help = [====[

forbid-fertile-eggs [ "help" | egg-type ]
========
Goes through all nestboxes and forbids eggs that are fertile.
Eggs that have already been taken out of the nest boxes are not affected.

help: print this help message
egg-type: If egg-type is given, then only eggs of that type are forbidden.
]====]

local args = {...}
if args[1] == 'help' or args[1] == '?' then
    print(help)
else
-- is world loaded
if not dfhack.isMapLoaded() then
qerror("World and map aren't loaded.")
-- is fortress mode
elseif not dfhack.world.isFortressMode() then
qerror('This script can only be used in fortress mode')
else
-- get buildings
local buildings = df.global.world.buildings.all
for i = 0, #buildings - 1 do
local building = buildings[i]
-- Zones and stockpiles don't have the contained_items field.
if df.building_actual:is_instance(building) then
-- if building:getType() == df.building_type.NESTBOX then -- NESTBOX not valid building_type
local items = building.contained_items
for j = 0, #items - 1 do
local contained = items[j]
-- is it an egg?
if contained:getType() == df.item_type.EGG
and not contained.item.flags.dump
then
print ("found egg in building type " ..building:getType())
if contained.item.flags.fertilized == true
then
contained.item.flags.forbiden = true
else
contained.item.flags.forbiden = false
end
end
end
end
end

-- find all items
-- for k,item in ipairs(df.global.world.items.all) do
-- -- get container (may be null if item on ground)
-- container = dfhack.items.getBuilding(item)
-- if (container ~= nil and item:getType() == df.item_type.EGG) then
-- print ("is in container. type is: " .. container:getType())
-- end
-- -- is item what we're looking for? (egg in nestbox)
-- if (
-- container ~= nil and
-- -- container:getType() == df.building_type.TODO_NESTBOX and
-- item ~= nil and
-- item:getType() == df.item_type.EGG
-- )
-- then
-- item_type = item:getType()
-- print ("item - key: " ..k.. " type: " ..item_type)
-- print ("in container: " ..container .. " of type " ..container:getType())
-- end
-- end
end -- world, map and fortress mode loaded
end -- args[1] == 'help' | '?'

Thanks for the help.
Mostly I struggle with 'how do I get a list of X' or 'what building_type' is a nestbox, etc.
the API docs really don't explain a whole lot of that kind of stuff. what i have there is mostly just cobbled together from existing scripts
Logged

Fleeting Frames

  • Bay Watcher
  • Spooky cart at distance
    • View Profile
Re: Request: DFHack Plugin/Script forbid fertilized eggs
« Reply #1 on: November 20, 2017, 05:23:30 pm »

What building type is nestbox...Here's not the most beautiful snippet from my constructmultiz:
Spoiler: getBuildingTypeIndex (click to show/hide)

It is not necessary for you. For nestbox, <building_nest_boxst: 0xasbadsre435a> would translate to df.building_type.NestBox - which is 48, but you already have the above just by using tostring(building).

For, say, checking an egg is hen egg, you could do df.global.world.raws.creatures.all[<eggitem>.race].caste[<eggitem>.caste].caste_name[0] to get the name of the female/male/third+ gender of the species.

Other not necessarily good options for this idea:

- I recall someone, I think Warmist (but am not sure), already has something like this? Couldn't quite manage to stop already in-process jobs from retrieving, but otherwise worked iirc.

- There's already autonestbox plugin; it'd make sense to attach this to it.

- Instead of iterating nestboxes, could disable eggs in a food stockpile whenever it is placed (less overhead, maybe) as well as disabling eggs from being cooked. Inferior from user perspective, ofc.

- You could also go over df.global.world.items.other.EGG, which contains all the eggs in a fortress. No idea how it compares to checking nestboxes.

- You could also follow around pregnant female birds and watch for them to lay eggs. Probably not optimal, though.
« Last Edit: November 20, 2017, 05:31:24 pm by Fleeting Frames »
Logged

milo christiansen

  • Bay Watcher
  • Something generic here
    • View Profile
Re: Request: DFHack Plugin/Script forbid fertilized eggs
« Reply #2 on: November 21, 2017, 02:43:35 pm »

- I recall someone, I think Warmist (but am not sure), already has something like this? Couldn't quite manage to stop already in-process jobs from retrieving, but otherwise worked iirc.

Stopping in-progress jobs is easy. All you need to do is hunt down any jobs with that item in the job list and delete them. I did this with my conveyor belts in Rubble, and it works great.
Logged
Rubble 8 - The most powerful modding suite in existence!
After all, coke is for furnaces, not for snorting.
You're not true dwarven royalty unless you own the complete 'Signature Collection' baby-bone bedroom set from NOKEAS

Falco

  • Escaped Lunatic
    • View Profile
Re: Request: DFHack Plugin/Script forbid fertilized eggs
« Reply #3 on: November 21, 2017, 04:05:10 pm »

I'd be more happy about some complete and functional code. this is just a bit too much for me.
the autonestbox is a bit odd, and I don't quite like it either. it'd be much more practical if it took care of all the nestboxes in the zone, instead of only one at the corner.
Cause to use autonestbox then I have to make one zone for each nestbox, and i don't have that many roosters :P
Also disabling eggs in the food stockpiles is exactly what I don't want. I have multiple food stockpiles, and i still want to cook those eggs that are unfertile. and changing it back and forth is just the kind of annoyance i want to skip with this script in the first place. and because the only way to feasibly do that is to forbid the eggs, the other option would be to manually check all the nestboxes every few minutes. which is also why i want this automated.
« Last Edit: November 21, 2017, 05:13:34 pm by Falco »
Logged

Falco

  • Escaped Lunatic
    • View Profile
Re: Request: DFHack Plugin/Script forbid fertilized eggs
« Reply #4 on: December 08, 2017, 04:00:49 pm »

*push*

anyone has a solution for this?
Logged