-
Notifications
You must be signed in to change notification settings - Fork 0
/
autowheelbarrow.lua
120 lines (109 loc) · 6.42 KB
/
autowheelbarrow.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
local repeatutil = require("repeat-util")
local GLOBAL_KEY = "autowheelbarrow"
enabled = enabled or false
-- Main function for managing wheelbarrows
local function unassign_wheelbarrows()
local wheelbarrow_count = 0
for _, item in ipairs(df.global.world.items.other.TOOL) do
if df.item_toolst:is_instance(item) then
local tool_def = dfhack.items.getSubtypeDef(item:getType(), item:getSubtype())
if tool_def and tool_def.id == "ITEM_TOOL_WHEELBARROW" then
wheelbarrow_count = wheelbarrow_count + 1 -- Count each wheelbarrow
if item.flags.in_job then
-- Skip if in use
elseif item.stockpile then
item.stockpile.id = -1 -- Unassign wheelbarrow from stockpile
end
end
end
end
return wheelbarrow_count -- Return the total count of wheelbarrows processed
end
local function set_wheelbarrows_for_all_stockpiles()
for _, building in ipairs(df.global.world.buildings.all) do
if building:getType() == df.building_type.Stockpile then
local stockpile_settings = building.settings
local skip_wheelbarrows = false
-- Check food settings to skip wheelbarrows
if stockpile_settings and stockpile_settings.food then
local food_settings = stockpile_settings.food
skip_wheelbarrows = #food_settings.meat > 0 or #food_settings.fish > 0 or #food_settings.unprepared_fish > 0 or
#food_settings.egg > 0 or #food_settings.plants > 0 or #food_settings.drink_plant > 0 or
#food_settings.drink_animal > 0 or #food_settings.cheese_animal > 0 or #food_settings.cheese_plant > 0 or
#food_settings.seeds > 0 or #food_settings.leaves > 0 or #food_settings.powder_plant > 0 or
#food_settings.powder_creature > 0 or #food_settings.glob > 0 or #food_settings.glob_paste > 0 or
#food_settings.glob_pressed > 0 or #food_settings.liquid_plant > 0 or #food_settings.liquid_animal > 0 or
#food_settings.liquid_misc > 0
--dfhack.println("Skipping stockpile due to food items" .. tostring(building))
end
-- Check all the flags and skip if any of them are true (for individual stockpiles)
if stockpile_settings and stockpile_settings.flags then
local flags = stockpile_settings.flags
if flags.animals == true or flags.food == true or flags.furniture == true or
flags.corpses == true or flags.refuse == true or flags.ammo == true or flags.coins == true or
flags.bars_blocks == true or flags.gems == true or flags.finished_goods == true or
flags.leather == true or flags.cloth == true or flags.wood == true or
flags.weapons == true or flags.armor == true or flags.sheet == true or
flags[17] == true or flags[18] == true or flags[19] == true or flags[20] == true or
flags[21] == true or flags[22] == true or flags[23] == true or flags[24] == true or
flags[25] == true or flags[26] == true or flags[27] == true or flags[28] == true or
flags[29] == true or flags[30] == true or flags[31] == true then
skip_wheelbarrows = true
--dfhack.println("individual stockpiles Skipping stockpile due to one or more flags being true at " .. tostring(building))
end
end
-- Check if all flags are set to false (for the none stockpile)
if stockpile_settings and stockpile_settings.flags then
local flags = stockpile_settings.flags
if flags.animals == false and flags.food == false and flags.furniture == false and
flags.corpses == false and flags.refuse == false and flags.stone == false and flags.ammo == false and
flags.coins == false and flags.bars_blocks == false and flags.gems == false and flags.finished_goods == false and
flags.leather == false and flags.cloth == false and flags.wood == false and flags.weapons == false and
flags.armor == false and flags.sheet == false and
flags[17] == false and flags[18] == false and flags[19] == false and flags[20] == false and
flags[21] == false and flags[22] == false and flags[23] == false and flags[24] == false and
flags[25] == false and flags[26] == false and flags[27] == false and flags[28] == false and
flags[29] == false and flags[30] == false and flags[31] == false then
skip_wheelbarrows = true
--dfhack.println("Skipping NONE stockpile" .. tostring(building))
end
end
if skip_wheelbarrows then
building.max_wheelbarrows = 0
else
local count = unassign_wheelbarrows()
building.max_wheelbarrows = count
--dfhack.println("Set wheelbarrow limit for stockpile at " .. tostring(building) .. " to " .. count)
end
end
end
end
local function auto_wheelbarrows()
for _, unit in ipairs(df.global.world.units.active) do
if dfhack.units.isCitizen(unit) and unit.job.current_job and unit.job.current_job.job_type == 38 then
unassign_wheelbarrows()
set_wheelbarrows_for_all_stockpiles()
return
end
end
end
-- Event loop function to call periodically
local function event_loop()
if enabled then
dfhack.println("Running autowheelbarrow.lua ")
auto_wheelbarrows()
-- Check again in 1 days
repeatutil.scheduleUnlessAlreadyScheduled(GLOBAL_KEY, 1, "days", event_loop)
end
end
-- Manage on state change
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc == SC_MAP_LOADED and df.global.gamemode == df.game_mode.DWARF then
event_loop() -- Start the event loop when the game map is loaded
elseif sc == SC_MAP_UNLOADED then
repeatutil.cancel(GLOBAL_KEY) -- Stop the event loop when the map is unloaded
end
end
-- Enable the script and start the event loop
enabled = true
event_loop()