@@ -11,7 +11,7 @@ validArgs = utils.invert({
1111
1212local args = utils .processArgs ({... }, validArgs )
1313local GLOBAL_KEY = " autotraining"
14- local need_id = df .need_type [' MartialTraining' ]
14+ local MartialTraining = df .need_type [' MartialTraining' ]
1515local ignore_count = 0
1616
1717local function get_default_state ()
@@ -89,10 +89,8 @@ dfhack.onStateChange[GLOBAL_KEY] = function(sc)
8989 -- retrieve state saved in game. merge with default state so config
9090 -- saved from previous versions can pick up newer defaults.
9191 load_state ()
92- if ( state .enabled ) then
92+ if state .enabled then
9393 start ()
94- else
95- stop ()
9694 end
9795 persist_state ()
9896end
@@ -103,32 +101,35 @@ end
103101-- ######
104102function getTrainingCandidates ()
105103 local ret = {}
106- local citizen = dfhack .units .getCitizens (true )
107104 ignore_count = 0
108- for _ , unit in ipairs (citizen ) do
109- if dfhack .units .isAdult (unit ) then
110- local noblePos = dfhack .units .getNoblePositions (unit )
111- local isIgnNoble = false
112- if ( not state .ignored [unit .id ] ) then
113- if noblePos ~= nil then
114- for _ , position in ipairs (noblePos ) do
115- if state .ignored_nobles [position .position .code ] then
116- isIgnNoble = true
117- break
118- end
119- end
120- end
121- if not isIgnNoble then
122- table.insert (ret , unit )
123- else
124- removeTraining (unit )
125- ignore_count = ignore_count + 1
105+ for _ , unit in ipairs (dfhack .units .getCitizens (true )) do
106+ if state .ignored [unit .id ] then
107+ ignore_count = ignore_count + 1
108+ goto next_unit
109+ end
110+ if not dfhack .units .isAdult (unit ) then
111+ goto next_unit
112+ end
113+ local need = getTrainingNeed (unit )
114+ if not need or need .focus_level >= state .threshold then
115+ goto next_unit
116+ end
117+ local noblePos = dfhack .units .getNoblePositions (unit )
118+ local isIgnNoble = false
119+ if noblePos ~= nil then
120+ for _ , position in ipairs (noblePos ) do
121+ if state .ignored_nobles [position .position .code ] then
122+ isIgnNoble = true
123+ break
126124 end
127- else
128- removeTraining (unit )
129- ignore_count = ignore_count + 1
130125 end
131126 end
127+ if isIgnNoble then
128+ ignore_count = ignore_count + 1
129+ goto next_unit
130+ end
131+ table.insert (ret , unit )
132+ :: next_unit::
132133 end
133134 return ret
134135end
@@ -147,10 +148,11 @@ function getTrainingSquads()
147148 return squads
148149end
149150
150- function findNeed (unit )
151+ function getTrainingNeed (unit )
152+ if unit == nil then return nil end
151153 local needs = unit .status .current_soul .personality .needs
152154 for _ , need in ipairs (needs ) do
153- if need .id == need_id then
155+ if need .id == MartialTraining then
154156 return need
155157 end
156158 end
@@ -161,48 +163,38 @@ end
161163-- Main
162164-- ######
163165
164- function getByID (id )
165- for _ , unit in ipairs (getTrainingCandidates ()) do
166- if (unit .hist_figure_id == id ) then
167- return unit
168- end
169- end
170-
171- return nil
172- end
173-
174166-- Find all training squads
175167-- Abort if no squads found
176168function checkSquads ()
177169 local squads = {}
178170 for _ , squad in ipairs (getTrainingSquads ()) do
179171 if squad .entity_id == df .global .plotinfo .group_id then
180172 local leader = squad .positions [0 ].occupant
181- if ( leader ~= - 1 ) then
173+ if leader ~= - 1 then
182174 table.insert (squads ,squad )
183175 end
184176 end
185177 end
186178
187- if ( # squads == 0 ) then
179+ if # squads == 0 then
188180 return nil
189181 end
190182
191183 return squads
192184end
193185
194- function addTraining (unit )
195- if ( unit .military .squad_id ~= - 1 ) then
196- for _ , squad in ipairs (getTrainingSquads () ) do
186+ function addTraining (unit , good_squads )
187+ if unit .military .squad_id ~= - 1 then
188+ for _ , squad in ipairs (good_squads ) do
197189 if unit .military .squad_id == squad .id then
198190 return true
199191 end
200192 end
201193 return false
202194 end
203- for _ , squad in ipairs (getTrainingSquads () ) do
195+ for _ , squad in ipairs (good_squads ) do
204196 for i = 1 ,9 ,1 do
205- if ( squad .positions [i ].occupant == - 1 ) then
197+ if squad .positions [i ].occupant == - 1 then
206198 return dfhack .military .addToSquad (unit .id ,squad .id ,i )
207199 end
208200 end
@@ -211,24 +203,13 @@ function addTraining(unit)
211203 return false
212204end
213205
214- function removeTraining (unit )
215- for _ , squad in ipairs (getTrainingSquads ()) do
216- for i = 1 ,9 ,1 do
217- if ( unit .hist_figure_id == squad .positions [i ].occupant ) then
218- return dfhack .military .removeFromSquad (unit .id )
219- end
220- end
221- end
222- return false
223- end
224-
225206function removeAll ()
226- if ( state .training_squads == nil ) then return end
207+ if state .training_squads == nil then return end
227208 for _ , squad in ipairs (getTrainingSquads ()) do
228209 for i = 1 ,9 ,1 do
229- local dwarf = getByID (squad .positions [i ].occupant )
230- if ( dwarf ~= nil ) then
231- removeTraining ( dwarf )
210+ local hf = df . historical_figure . find (squad .positions [i ].occupant )
211+ if hf ~= nil then
212+ dfhack . military . removeFromSquad ( hf . unit_id )
232213 end
233214 end
234215 end
@@ -239,39 +220,42 @@ function check()
239220 local squads = checkSquads ()
240221 local intraining_count = 0
241222 local inque_count = 0
242- if ( squads == nil ) then return end
243- for _ , unit in ipairs (getTrainingCandidates ()) do
244- local need = findNeed (unit )
245- if ( need ~= nil ) then
246- if ( need .focus_level < state .threshold ) then
247- local bol = addTraining (unit )
248- if ( bol ) then
249- intraining_count = intraining_count + 1
250- else
251- inque_count = inque_count + 1
223+ if squads == nil then return end
224+ for _ ,squad in ipairs (squads ) do
225+ for i = 1 ,9 ,1 do
226+ if squad .positions [i ].occupant ~= - 1 then
227+ local hf = df .historical_figure .find (squad .positions [i ].occupant )
228+ if hf ~= nil then
229+ local unit = df .unit .find (hf .unit_id )
230+ local training_need = getTrainingNeed (unit )
231+ if not training_need or training_need .focus_level >= state .threshold then
232+ dfhack .military .removeFromSquad (unit )
233+ end
252234 end
253- else
254- removeTraining (unit )
255235 end
256236 end
257237 end
238+ for _ , unit in ipairs (getTrainingCandidates ()) do
239+ local added = addTraining (unit , squads )
240+ if added then
241+ intraining_count = intraining_count + 1
242+ else
243+ inque_count = inque_count + 1
244+ end
245+ end
258246
259- dfhack .println (GLOBAL_KEY .. " | IGN : " .. ignore_count .. " TRAIN : " .. intraining_count .. " QUE : " .. inque_count )
247+ dfhack .println (GLOBAL_KEY .. " | IGNORED : " .. ignore_count .. " TRAINING : " .. intraining_count .. " QUEUE : " .. inque_count )
260248end
261249
262250function start ()
263- dfhack .println (GLOBAL_KEY .. " | START" )
264-
265- if (args .t ) then
251+ if args .t then
266252 state .threshold = 0 - tonumber (args .t )
267253 end
268254 repeatUtil .scheduleEvery (GLOBAL_KEY , 1 , ' days' , check )
269255end
270256
271257function stop ()
272- removeAll ()
273258 repeatUtil .cancel (GLOBAL_KEY )
274- dfhack .println (GLOBAL_KEY .. " | STOP" )
275259end
276260
277261if dfhack_flags .enable then
@@ -287,11 +271,10 @@ if dfhack_flags.module then
287271 return
288272end
289273
290- if ( state .enabled ) then
274+ if state .enabled then
291275 start ()
292- dfhack .println (GLOBAL_KEY .. " | Enabled" )
293276else
294277 stop ()
295- dfhack . println ( GLOBAL_KEY .. " | Disabled " )
278+ removeAll ( )
296279end
297280persist_state ()
0 commit comments