forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiren.lua
122 lines (103 loc) · 2.95 KB
/
siren.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
121
122
-- Wakes up the sleeping, breaks up parties and stops breaks.
local utils = require 'utils'
local args = {...}
local burrows = {}
local bnames = {}
if not dfhack.isMapLoaded() then
qerror('Map is not loaded.')
end
for _,v in ipairs(args) do
local b = dfhack.burrows.findByName(v)
if not b then
qerror('Unknown burrow: '..v)
end
table.insert(bnames, v)
table.insert(burrows, b)
end
local in_siege = false
function is_in_burrows(pos)
if #burrows == 0 then
return true
end
for _,v in ipairs(burrows) do
if dfhack.burrows.isAssignedTile(v, pos) then
return true
end
end
end
function add_thought(unit, code)
for _,v in ipairs(unit.status.recent_events) do
if v.type == code then
v.age = 0
return
end
end
unit.status.recent_events:insert('#', { new = true, type = code })
end
function wake_unit(unit)
local job = unit.job.current_job
if not job or job.job_type ~= df.job_type.Sleep then
return
end
if job.completion_timer > 0 then
unit.counters.unconscious = 0
add_thought(unit, df.unit_thought_type.SleepNoiseWake)
elseif job.completion_timer < 0 then
add_thought(unit, df.unit_thought_type.Tired)
end
job.pos:assign(unit.pos)
job.completion_timer = 0
unit.path.dest:assign(unit.pos)
unit.path.path.x:resize(0)
unit.path.path.y:resize(0)
unit.path.path.z:resize(0)
unit.counters.job_counter = 0
end
function stop_break(unit)
local counter = dfhack.units.getMiscTrait(unit, df.misc_trait_type.OnBreak)
if counter then
counter.id = df.misc_trait_type.TimeSinceBreak
counter.value = 100800 - 30*1200
add_thought(unit, df.unit_thought_type.Tired)
end
end
-- Check siege for thought purpose
for _,v in ipairs(df.global.ui.invasions.list) do
if v.flags.siege and v.flags.active then
in_siege = true
break
end
end
-- Stop rest
for _,v in ipairs(df.global.world.units.active) do
local x,y,z = dfhack.units.getPosition(v)
if x and dfhack.units.isCitizen(v) and is_in_burrows(xyz2pos(x,y,z)) then
if not in_siege and v.military.squad_id < 0 then
add_thought(v, df.unit_thought_type.LackProtection)
end
wake_unit(v)
stop_break(v)
end
end
-- Stop parties
for _,v in ipairs(df.global.ui.parties) do
local pos = utils.getBuildingCenter(v.location)
if is_in_burrows(pos) then
v.timer = 0
for _, u in ipairs(v.units) do
add_thought(unit, df.unit_thought_type.Tired)
end
end
end
local place = 'the halls and tunnels'
if #bnames > 0 then
if #bnames == 1 then
place = bnames[1]
else
place = table.concat(bnames,', ',1,#bnames-1)..' and '..bnames[#bnames]
end
end
dfhack.gui.showAnnouncement(
'A loud siren sounds throughout '..place..', waking the sleeping and startling the awake.',
COLOR_BROWN, true
)