Skip to content

Commit 3e15c5f

Browse files
Merge branch 'sci-gym-script' into SquidHack
2 parents a8dfa64 + 8cee911 commit 3e15c5f

File tree

6 files changed

+73
-85
lines changed

6 files changed

+73
-85
lines changed

autotraining.lua

Lines changed: 64 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ validArgs = utils.invert({
1111

1212
local args = utils.processArgs({...}, validArgs)
1313
local GLOBAL_KEY = "autotraining"
14-
local need_id = df.need_type['MartialTraining']
14+
local MartialTraining = df.need_type['MartialTraining']
1515
local ignore_count = 0
1616

1717
local 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()
9896
end
@@ -103,32 +101,35 @@ end
103101
--######
104102
function 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
134135
end
@@ -147,10 +148,11 @@ function getTrainingSquads()
147148
return squads
148149
end
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
176168
function 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
192184
end
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
212204
end
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-
225206
function 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 )
260248
end
261249

262250
function 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)
269255
end
270256

271257
function stop()
272-
removeAll()
273258
repeatUtil.cancel(GLOBAL_KEY)
274-
dfhack.println(GLOBAL_KEY .. " | STOP")
275259
end
276260

277261
if dfhack_flags.enable then
@@ -287,11 +271,10 @@ if dfhack_flags.module then
287271
return
288272
end
289273

290-
if ( state.enabled ) then
274+
if state.enabled then
291275
start()
292-
dfhack.println(GLOBAL_KEY .." | Enabled")
293276
else
294277
stop()
295-
dfhack.println(GLOBAL_KEY .." | Disabled")
278+
removeAll()
296279
end
297280
persist_state()

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Template for new versions:
1717
## New Features
1818

1919
## Fixes
20+
- `gui/gm-unit`: remove reference to ``think_counter``, removed in v51.12
2021

2122
## Misc Improvements
2223

@@ -31,6 +32,9 @@ Template for new versions:
3132
## New Features
3233

3334
## Fixes
35+
- `gui/journal`: fix typo which caused the table of contents to always be regenerated even when not needed
36+
- `gui/mod-manager`: gracefully handle mods with missing or broken ``info.txt`` files
37+
- `uniform-unstick`: resolve overlap with new buttons in 51.13
3438

3539
## Misc Improvements
3640

gui/journal.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function JournalWindow:init()
7272
self.subviews.table_of_contents_panel.visible = not collapsed
7373
self.subviews.table_of_contents_divider.visible = not collapsed
7474

75-
if not colllapsed then
75+
if not collapsed then
7676
self:reloadTableOfContents()
7777
end
7878

gui/mod-manager.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,8 @@ function ModlistWindow:refresh_list()
524524
local choices = {}
525525
for idx,mod in ipairs(scriptmanager.get_active_mods()) do
526526
if not include_vanilla and mod.vanilla then goto continue end
527-
local steam_id = scriptmanager.get_mod_info_metadata(mod.path, 'STEAM_FILE_ID').STEAM_FILE_ID
527+
local metadata = scriptmanager.get_mod_info_metadata(mod.path, 'STEAM_FILE_ID')
528+
local steam_id = metadata and metadata.STEAM_FILE_ID or nil
528529
local url = steam_id and (': https://steamcommunity.com/sharedfiles/filedetails/?id=%s'):format(steam_id) or ''
529530
table.insert(choices, {
530531
text={

internal/gm-unit/editor_counters.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Editor_Counters=defclass(Editor_Counters, base_editor.Editor)
99
Editor_Counters.ATTRS{
1010
frame_title = "Counters editor",
1111
counters1={
12-
"think_counter",
1312
"job_counter",
1413
"swap_counter",
1514
"winded",

uniform-unstick.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,11 @@ local MIN_WIDTH = 26
343343
EquipOverlay = defclass(EquipOverlay, overlay.OverlayWidget)
344344
EquipOverlay.ATTRS{
345345
desc='Adds a link to the equip screen to fix equipment conflicts.',
346-
default_pos={x=7,y=21},
346+
default_pos={x=7,y=23},
347347
default_enabled=true,
348348
viewscreens='dwarfmode/Squads/Equipment/Default',
349349
frame={w=MIN_WIDTH, h=1},
350+
version=1
350351
}
351352

352353
function EquipOverlay:init()

0 commit comments

Comments
 (0)