Repository navigation
Expand file tree
/
Copy pathdead-noble.lua
More file actions
1258 lines (1194 loc) · 51.3 KB
/
Copy pathdead-noble.lua
File metadata and controls
1258 lines (1194 loc) · 51.3 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Repair or replace a fortress's dead/missing land holder (baron/count/duke).
--[====[
dead-noble
==========
Diagnose and fix a fortress that has lost its baron, count, or duke and
can no longer receive a replacement.
See dead-noble.rst for full documentation.
]====]
local argparse = require('argparse')
local world = df.global.world
local plotinfo = df.global.plotinfo
local utils = require('utils')
local function qerr(fmt, ...)
qerror(fmt:format(...))
end
local function civ()
return df.historical_entity.find(plotinfo.civ_id)
or qerr('cannot resolve parent civ %d', plotinfo.civ_id)
end
local function position_def(entity, position_id)
for _, def in ipairs(entity.positions.own) do
if def.id == position_id then return def end
end
end
local function def_for(entity, code)
for _, def in ipairs(entity.positions.own) do
if def.code == code then return def end
end
end
local function find_assignment(entity, id)
for _, a in ipairs(entity.positions.assignments) do
if a.id == id then return a end
end
end
local function site_link_to(entity, site_id)
for _, link in ipairs(entity.site_links) do
if link.target == site_id then return link end
end
end
local function hf_of(hfid)
return hfid ~= -1 and df.historical_figure.find(hfid) or nil
end
local function site_landmass(site)
if not site then return nil end
local col = world.world_data.region_map[site.pos.x]
return col and col:_displace(site.pos.y).landmass_id or nil
end
local function hf_landmass(wb)
if wb.abs_smm_x == -1 or wb.abs_smm_y == -1 then return nil end
local col = world.world_data.region_map[wb.abs_smm_x // 0x30]
return col and col:_displace(wb.abs_smm_y // 0x30).landmass_id or nil
end
local function hf_desc(hf)
if not hf then return '<missing hf>' end
local name = dfhack.df2utf(dfhack.translation.translateName(hf.name, true))
local state = hf.died_year ~= -1 and ('DEAD ' .. hf.died_year) or 'alive'
local site = 'site?'
local wb = hf.info and hf.info.whereabouts or nil
if wb then
site = 'site ' .. tostring(wb.site_id)
local lm = hf_landmass(wb)
if lm then
site = site .. ', landmass ' .. tostring(lm)
end
end
return ('%s (hf %d, %s, %s)'):format(name, hf.id, state, site)
end
-- land-holder assignments the civ associates with our site.
-- strict=false lists every land-holder assignment (diagnostics);
-- strict=true keeps only those actually tied to this site: the
-- assignment our site link points at, plus any with st_id == our site.
local function site_land_holder_assignments(entity, strict)
local out = {}
local link = site_link_to(entity, plotinfo.site_id)
local linked_id = link and link.position_profile_id or -1
-- site links pointing at each assignment id
local link_sites = {}
for _, l in ipairs(entity.site_links) do
if l.position_profile_id ~= -1 then
link_sites[l.position_profile_id] =
link_sites[l.position_profile_id] or {}
table.insert(link_sites[l.position_profile_id], l.target)
end
end
for _, a in ipairs(entity.positions.assignments) do
local def = position_def(entity, a.position_id)
if def and def.land_holder > 0 then
local ours = a.st_id == plotinfo.site_id or a.id == linked_id
if not ours and link_sites[a.id] then
for _, sid in ipairs(link_sites[a.id]) do
if sid == plotinfo.site_id then ours = true break end
end
end
if not strict or ours then
table.insert(out, {
assignment = a, def = def,
ours = ours and true or false,
sites = link_sites[a.id],
})
end
end
end
table.sort(out, function(x, y)
return x.def.land_holder < y.def.land_holder
end)
return out, linked_id
end
local function in_vector(vec, item)
for _, v in ipairs(vec) do
if v == item then return true end
end
return false
end
local function vec_desc(vec, item)
return in_vector(vec, item) and 'yes' or 'no'
end
local function holder_is_dead(hf)
return hf == nil or hf.died_year ~= -1
end
local function holder_off_site(hf)
return hf and hf.info and hf.info.whereabouts
and hf.info.whereabouts.site_id ~= plotinfo.site_id
end
local function rd_i32(obj, off)
local _, a = df.sizeof(obj)
return df.reinterpret_cast('int32_t', a + off).value
end
local function nemesis_of_hfid(hfid)
for _, n in ipairs(world.nemesis.all) do
if n.figure and n.figure.id == hfid then return n end
end
end
-- emulate worldst;:evaluate_entity_strength's gate chain for one nemesis, printing each verdict
local function trace_sweep(nem, hf)
local function gate(ok, name)
print((' [%s] %s'):format(ok and 'PASS' or 'FAIL', name))
return ok
end
if not gate(hf.died_year == -1,
('died_year == -1 [=%d]'):format(hf.died_year))
then return end
if not gate(nem.group_leader_id == -1, 'group_leader_id == -1') then return end
local uok = nem.unit == nil or (nem.unit.flags1.important_historical_figure) == 0
if not gate(uok, 'unit == NULL or unit.flags1 bit31 clear') then return end
-- entity-link scan: POSITION link -> entity -> assignments[assignment_id]
-- native quirk: RSP+0x70 (dest's st_id source) is the LAST resolved link's
-- assignment, while RSP+0x38/0x48 (fallback + army bookkeeping) is the
-- MIN-precedence winner.
local assignment, ent, def -- min-precedence winner
local last_asg, last_ent, last_def -- last resolved link
local best_prec = 30001
for _, l in ipairs(hf.entity_links) do
if l._type == df.histfig_entity_link_positionst and l.entity_id ~= -1 then
local e = df.historical_entity.find(l.entity_id)
local asg
if e then
for _, a2 in ipairs(e.positions.assignments) do
if a2.id == l.assignment_id then asg = a2 break end
end
end
local d
if asg then
for _, p in ipairs(e.positions.own) do
if p.id == asg.position_id then d = p break end
end
end
print((' link: asg_id=%d ent=%s -> asg %s (%s) st_id=%s prec=%s%s'):
format(l.assignment_id, tostring(l.entity_id),
asg and asg.id or 'UNRESOLVED',
d and d.code or '?',
asg and asg.st_id or '?',
d and d.precedence or '?',
asg and ' [last-wins candidate]' or ''))
if asg then
last_asg, last_ent, last_def = asg, e, d
if d and d.precedence < best_prec then
best_prec = d.precedence
assignment, ent, def = asg, e, d
end
end
end
end
if last_asg and last_asg ~= assignment then
print((' !! dest st_id read from LAST link (asg %s st_id=%s), ' ..
'winner is asg %s'):
format(last_asg.id, tostring(last_asg.st_id), assignment.id))
end
if not gate(assignment ~= nil,
('POSITION link resolved (asg=%s ent=%s prec=%d)'):format(
assignment and assignment.id or 'nil',
ent and ent.id or 'nil', best_prec)) then return end
local wb = hf.info and hf.info.whereabouts
if not gate(wb ~= nil, 'whereabouts present') then return end
if not gate(wb.state ~= whereabouts_type.army_died and wb.state ~= whereabouts_type.army_survived,
('whereabouts.state %d not in {army_died,army_survived}'):format(wb.state))
then return end
if not gate(wb.abs_smm_x ~= -1, 'abs_smm_x set') then return end
-- debugger-verified: land_holder>0 on the LAST resolved def skips
-- unconditionally BEFORE destination resolution (def+0x2c8, jg -> tail)
if not gate(last_def and last_def.land_holder <= 0,
('last def.land_holder %s <= 0 (unconditional skip)'):
format(last_def and last_def.land_holder))
then return end
-- destination: st_id is a site ID resolved by the sweep's get_site
-- (verified live: st_id=1629 produced an army to site id 1629).
local dest
if last_asg.st_id ~= -1 then
dest = df.world_site.find(last_asg.st_id)
print((' [dest] st_id path (asg %d st_id=%d) -> site %s'):
format(last_asg.id, last_asg.st_id, dest and dest.id or 'nil'))
elseif ent.type == df.historical_entity_type.Civilization then
for _, l in ipairs(ent.site_links) do
if l.flags.capital then dest = df.world_site.find(l.target) end
end
print((' [dest] capital path -> site %s'):format(dest and dest.id or 'nil'))
elseif ent.type == df.historical_entity_type.SiteGovernment
or ent.type == df.historical_entity_type.Religion
or ent.type == df.historical_entity_type.MilitaryUnit then
for _, l in ipairs(ent.site_links) do
if l.flags.residence then dest = df.world_site.find(l.target) end
end
print((' [dest] residence path -> site %s'):format(dest and dest.id or 'nil'))
else
gate(false, ('winner entity type %s not in {0,1,5,7} -> skip'):
format(tostring(ent.type)))
return
end
if not gate(dest ~= nil, 'destination site resolved') then return end
local lm_hf = hf_landmass(wb)
local lm_dest = site_landmass(dest)
gate(lm_hf ~= nil and lm_hf == lm_dest,
('landmass match %s == %s'):format(tostring(lm_hf), tostring(lm_dest)))
local g = dest.min_depth
gate(g == 0, ('min_depth == 0 [=%d]'):format(g))
local home = nil
for _, l in ipairs(hf.site_links) do
local t = l:getType()
if (t == df.histfig_site_link_type.HOME_SITE_ABSTRACT_BUILDING or
t == df.histfig_site_link_type.HOME_SITE_REALIZATION_BUILDING or
t == df.histfig_site_link_type.LAIR or
t == df.histfig_site_link_type.HOME_SITE_REALIZATION_SUL or
t == df.histfig_site_link_type.HOME_SITE_SAVED_CIVZONE)
and l.site ~= -1 then home = l.site break end
end
if not home then home = wb.site_id end
if not gate(home ~= dest.id,
('not already home: home %d != dest %d'):format(home, dest.id))
then return end
for _, l in ipairs(hf.entity_links) do
local t = l:getType()
if (t == df.histfig_entity_link_type.SLAVE or t == df.histfig_entity_link_type.PRISONER) and l.link_strength ~= 0 then
gate(false, ('PRISONER/SLAVE link strength %d'):format(l.link_strength))
return
end
end
for _, l in ipairs(hf.histfig_links) do
if l:getType() == df.histfig_hf_link_type.IMPRISONER and l.link_strength ~= 0 then
gate(false, 'IMPRISONER link')
return
end
end
print(' [OK] all gates pass -> native sweep SHOULD create MOVE_TO_SITE')
end
local function cmd_trace()
local entity = civ()
-- the native call needs cur_season_tick%120==50 AND the region_logic
-- frame counter (every 144 calls) to coincide -> fires ~every 120-720 ticks
print(('cur_season_tick %d (%%120 = %d, needs 50)'):
format(df.global.cur_season_tick, df.global.cur_season_tick % 120))
local rows = site_land_holder_assignments(entity, true)
for _, row in ipairs(rows) do
local hf = hf_of(row.assignment.histfig)
if not hf then
print(('assignment %d: no living holder'):format(row.assignment.id))
else
local nem = nemesis_of_hfid(hf.id)
print(('assignment %d holder %s nemesis %s'):
format(row.assignment.id, hf.id, nem and nem.id or 'nil'))
if nem then trace_sweep(nem, hf) end
end
end
end
local function cmd_status()
local entity = civ()
local rows, linked_id = site_land_holder_assignments(entity, false)
print(('civ %d (%s), site %d, site-link position_profile_id %d'):
format(entity.id,
dfhack.df2utf(dfhack.translation.translateName(entity.name, true)),
plotinfo.site_id, linked_id))
if #rows == 0 then
print('no land-holder assignments found for this site')
-- still show defs in case none were ever created
for _, def in ipairs(entity.positions.own) do
if def.land_holder > 0 then
print((' def %d %s land_holder=%d (no assignment)'):
format(def.id, def.code, def.land_holder))
end
end
return
end
local fort_site = df.world_site.find(plotinfo.site_id)
local fort_lm = site_landmass(fort_site)
for _, row in ipairs(rows) do
local a, def = row.assignment, row.def
local cur = hf_of(a.histfig)
local last = hf_of(a.histfig2)
print(('assignment %d%s: %s (def %d, land_holder %d, st_id %d, flags {%s})'):
format(a.id, row.ours and ' [THIS SITE]' or '', def.code,
def.id, def.land_holder, a.st_id,
a.flags.active and 'active' or 'INACTIVE'))
if row.sites then
print((' holds land at sites: %s'):
format(table.concat(row.sites, ' ')))
end
print((' holder %d: %s'):format(a.histfig, hf_desc(cur)))
if cur and cur.died_year == -1 and holder_off_site(cur) then
local lm = hf_landmass(cur.info.whereabouts)
if lm and fort_lm and lm ~= fort_lm then
print(' !! holder is on a different landmass; the native ' ..
'succession-traveler army cannot bring them home')
end
end
print((' last_holder %d: %s'):format(a.histfig2, hf_desc(last)))
if a.id == linked_id and cur then
-- does the holder actually carry the position link?
local has_link = false
for _, l in ipairs(cur.entity_links) do
if df.histfig_entity_link_positionst:is_instance(l)
and l.assignment_id == a.id
then
has_link = true
end
end
print((' holder has POSITION link to this assignment: %s'):
format(tostring(has_link)))
for _, l in ipairs(cur.site_links) do
if l.site ~= -1 then
print((' holder site link: %s -> site %d'):
format(tostring(l._type), l.site))
end
end
end
print((' caches: evaluate=%s succession=%s appointable=%s elected=%s claimable=%s'):
format(vec_desc(entity.positions.possible_evaluate, a),
vec_desc(entity.positions.possible_succession, a),
vec_desc(entity.positions.possible_appointable, a),
vec_desc(entity.positions.possible_elected, a),
vec_desc(entity.positions.possible_claimable, a)))
end
local f = entity.flags
print(('entity flags: eval_activity=%s possible_evaluate=%s succession=%s appointable=%s elected=%s nems_check=%s'):
format(tostring(f.evaluate_position_profile_activity),
tostring(f.possible_evaluate_position_profile),
tostring(f.possible_succession_position_profile),
tostring(f.possible_appointable_position_profile),
tostring(f.possible_elected_position_profile),
tostring(f.make_nems_check_positions)))
-- which sites' civ links point at which assignments; flags on ours
print('site links with land-holder pointers:')
local linkflag_names = {
'land_for_holding', 'central_holding_land', 'land_holder_residence',
'residence', 'local_market',
}
for _, link in ipairs(entity.site_links) do
if link.position_profile_id ~= -1 or link.flags.land_for_holding then
local flagstr = {}
for _, fn in ipairs(linkflag_names) do
if link.flags[fn] then table.insert(flagstr, fn) end
end
local site = df.world_site.find(link.target)
local sname = site and dfhack.df2utf(dfhack.translation.translateName(site.name, true)) or '?'
print((' site %d (%s) -> assignment %d [%s] type %s%s'):
format(link.target, sname, link.position_profile_id,
table.concat(flagstr, ' '), tostring(link.type),
link.target == plotinfo.site_id and ' <== THIS SITE' or ''))
end
end
-- the fortress entity's own residence link
local gov = df.historical_entity.find(plotinfo.group_id)
if gov then
for _, link in ipairs(gov.site_links) do
if link.flags.residence then
print(('fortress gov RESIDENCE link: site %d, position_profile_id %d'):
format(link.target, link.position_profile_id))
end
end
end
end
-- erase `item` from a pointer vector, returning success
local function vec_erase(vec, item)
for i, v in ipairs(vec) do
if v == item then vec:erase(i) return true end
end
return false
end
-- remove `assignment` from each responsibility slot its def claims
local function remove_from_responsibility_caches(entity, def, assignment)
for resp, held in pairs(def.responsibilities) do
if held then
vec_erase(entity.assignments_by_type[resp], assignment)
end
end
end
local function add_to_responsibility_caches(entity, def, assignment)
for resp, held in pairs(def.responsibilities) do
if held then
local vec = entity.assignments_by_type[resp]
if not in_vector(vec, assignment) then vec:insert('#', assignment) end
end
end
end
-- queue a vacant assignment for re-evaluation (mirrors
-- history_figurest::remove_entity_position_link_by_index)
local function queue_vacancy(entity, assignment)
if assignment.flags.active
and not in_vector(entity.positions.possible_evaluate, assignment)
then
entity.positions.possible_evaluate:insert('#', assignment)
entity.flags.possible_evaluate_position_profile = true
world.flags.an_entity_has_possible_evaluate_position_profile = true
end
entity.flags.evaluate_position_profile_activity = true
world.flags.an_entity_has_evaluate_position_profile_activity = true
end
local function new_remove_link_event(entity, hf, position_id)
world.history.events:insert('#', {
new = df.history_event_remove_hf_entity_linkst,
id = df.global.hist_event_next_id,
year = df.global.cur_year,
seconds = df.global.cur_year_tick,
civ = entity.id,
histfig = hf.id,
link_type = df.histfig_entity_link_type.POSITION,
position_id = position_id,
})
df.global.hist_event_next_id = df.global.hist_event_next_id + 1
end
local function new_add_link_event(entity, hf, assignment)
world.history.events:insert('#', {
new = df.history_event_add_hf_entity_linkst,
id = df.global.hist_event_next_id,
year = df.global.cur_year,
seconds = df.global.cur_year_tick,
civ = entity.id,
histfig = hf.id,
link_type = df.histfig_entity_link_type.POSITION,
position_id = assignment.id, -- native stores the assignment id here
})
df.global.hist_event_next_id = df.global.hist_event_next_id + 1
end
local function new_add_member_event(entity, hf)
world.history.events:insert('#', {
new = df.history_event_add_hf_entity_linkst,
id = df.global.hist_event_next_id,
year = df.global.cur_year,
seconds = df.global.cur_year_tick,
civ = entity.id,
histfig = hf.id,
link_type = df.histfig_entity_link_type.MEMBER,
position_id = -1,
})
df.global.hist_event_next_id = df.global.hist_event_next_id + 1
end
local function new_change_state_event(hf, state, site_id, reason)
local site = site_id ~= -1 and df.world_site.find(site_id) or nil
local ev = df.history_event_change_hf_statest:new()
ev.id = df.global.hist_event_next_id - 1
ev.year = df.global.cur_year
ev.seconds = df.global.cur_year_tick
ev.hfid = hf.id
ev.state = state
ev.reason = reason or df.history_event_reason.none
ev.site = site_id
ev.region = -1
ev.layer = -1
if site then ev.region_pos:assign(site.pos) end
world.history.events:insert('#', ev)
df.global.hist_event_next_id = df.global.hist_event_next_id + 1
end
-- strip `hf`'s POSITION link for `assignment`, mirroring
-- history_figurest::remove_entity_position_link_by_index
local function strip_position_link(entity, hf, def, assignment)
local start_year, link_strength
for i = #hf.entity_links - 1, 0, -1 do
local link = hf.entity_links[i]
if df.histfig_entity_link_positionst:is_instance(link)
and link.entity_id == entity.id
and link.assignment_id == assignment.id
then
start_year = link.start_year
link_strength = link.link_strength
hf.entity_links:erase(i)
link:delete()
end
end
if start_year then
hf.entity_links:insert('#', {
new = df.histfig_entity_link_former_positionst,
entity_id = entity.id,
assignment_id = assignment.id,
link_strength = link_strength or 100,
start_year = start_year,
end_year = df.global.cur_year,
})
new_remove_link_event(entity, hf, def.id)
end
remove_from_responsibility_caches(entity, def, assignment)
assignment.histfig = -1 -- histfig2 (last holder) is kept for heirs
queue_vacancy(entity, assignment)
end
-- vacancy cleanup when the dead hf record itself is gone
local function assignment_cleanup(entity, def, assignment)
remove_from_responsibility_caches(entity, def, assignment)
assignment.histfig = -1
queue_vacancy(entity, assignment)
end
-- find the nemesis record for an hf, and report army/controller state
local function army_desc(a)
local c = a.controller
local goal = c and tostring(c.goal) or '?'
return ('army %d pos(%d,%d) flags{%s} controller %s goal=%s site_id=%s')
:format(a.id, a.pos.x, a.pos.y,
a.flags.player and 'player' or '-',
c and tostring(c.id) or 'none', goal,
c and tostring(c.site_id) or '?')
end
-- native gate at world_site.min_depth (evaluate_entity_strength @0x14147e08c):
-- must be 0 for the sweep to send a noble there
local function probe_site_gate(site_id)
local site = df.world_site.find(site_id)
if not site then return end
local gate = site.min_depth
print((' site %d (%s, type %s): min_depth = %d, raw min_depth=%d%s'):
format(site_id,
dfhack.df2utf(dfhack.translation.translateName(site.name)),
tostring(site.type), gate, site.min_depth,
gate ~= 0 and ' -- nonzero BLOCKS native travel' or ''))
end
local function cmd_army()
local entity = civ()
local rows = site_land_holder_assignments(entity, true)
if #rows == 0 then
print('no land-holder assignment tied to this site')
return
end
for _, row in ipairs(rows) do
local a = row.assignment
local hf = hf_of(a.histfig)
if not hf then
print(('assignment %d (%s): no living holder to track'):
format(a.id, row.def.code))
goto continue
end
print(('assignment %d (%s): st_id=%d ab_id=%d squad_id=%d army_controller=%d'):
format(a.id, row.def.code, a.st_id, a.ab_id, a.squad_id,
a.assigned_army_controller_id))
print((' holder %s'):format(hf_desc(hf)))
local nem = nemesis_of_hfid(hf.id)
if not nem then
print(' no nemesis record for this hf')
goto continue
end
print((' nemesis %d (unit_id %d, group_leader %d, travel_link %d)'):
format(nem.id, nem.unit_id, nem.group_leader_id,
nem.travel_link_nemid))
-- unit gate: unit==NULL passes; a unit with flags1 bit31 skips
if nem.unit then
print((' ! nemesis.unit -> unit %d flags1=0x%x bit31=%d%s'):
format(nem.unit.id, nem.unit.flags1.whole,
nem.unit.flags1.whole >> 31 & 1,
(nem.unit.flags1.whole >> 31 & 1) == 1
and ' -- native sweep SKIPS this nemesis' or ''))
end
-- duplicate nemesis records would confuse the sweep
local nems = 0
for _, n in ipairs(world.nemesis.all) do
if n.figure and n.figure.id == hf.id then nems = nems + 1 end
end
if nems > 1 then
print((' ! %d nemesis records point at this hf'):format(nems))
end
-- is the nemesis in the civilized_actor sweep list?
-- (debugger-verified: the sweep's inner loop iterates this
-- vector, at world+0x13cb8 in the 0.53.17-steam build)
local in_sweep = false
for _, n in ipairs(world.nemesis.civilized_actor) do
if n == nem then in_sweep = true break end
end
print((' in civilized_actor sweep: %s'):format(tostring(in_sweep)))
-- native sweep gates (FUN_14147d730)
local wb = hf.info and hf.info.whereabouts or nil
if wb then
print((' whereabouts: state=%s site_id=%d abs_smm=(%d,%d)'):
format(tostring(wb.state), wb.site_id,
wb.abs_smm_x, wb.abs_smm_y))
else
print(' whereabouts: <none> -- native sweep skips this hf')
end
for _, l in ipairs(hf.entity_links) do
local t = l._type
if t == df.histfig_entity_link_prisonerst
or t == df.histfig_entity_link_slavest
then
print((' ! %s link, link_strength=%d (blocks travel)'):
format(tostring(t), l.link_strength))
end
end
for _, l in ipairs(hf.histfig_links) do
if tostring(l._type):find('IMPRISONER') then
print((' ! IMPRISONER link, link_strength=%d (blocks travel)'):
format(l.link_strength))
end
end
-- POSITION links: sweep resolves entity -> assignments[assignment_id]
for _, l in ipairs(hf.entity_links) do
if l._type == df.histfig_entity_link_positionst then
local e = df.historical_entity.find(l.entity_id)
print((' POSITION link: entity=%d (%s) assignment_id=%d strength=%d'):
format(l.entity_id, e and tostring(e.type) or '?',
l.assignment_id, l.link_strength))
end
end
-- HOME_SITE* site links (types 3-7): native treats the first as "home"
for _, l in ipairs(hf.site_links) do
local t = l:getType()
if (t == df.histfig_site_link_type.HOME_SITE_ABSTRACT_BUILDING or
t == df.histfig_site_link_type.HOME_SITE_REALIZATION_BUILDING or
t == df.histfig_site_link_type.LAIR or
t == df.histfig_site_link_type.HOME_SITE_REALIZATION_SUL or
t == df.histfig_site_link_type.HOME_SITE_SAVED_CIVZONE)
then
print((' home-site link: %s -> site %d'):
format(tostring(l._type), l.site))
end
end
-- armies containing the nemesis
local found = false
for _, army in ipairs(world.armies.all) do
for _, m in ipairs(army.members) do
if m.nemesis_id == nem.id then
print(' member of ' .. army_desc(army))
found = true
end
end
end
if not found then print(' not in any army') end
::continue::
end
-- destination the native sweep would pick for a civ nemesis
for _, l in ipairs(entity.site_links) do
if l.flags.capital then
print(('civ CAPITAL site link -> %d (army destination for civ nobles)')
:format(l.target))
probe_site_gate(l.target)
end
end
probe_site_gate(plotinfo.site_id)
-- any controller already headed for our site?
for _, c in ipairs(world.army_controllers.all) do
if c.site_id == plotinfo.site_id then
print(('controller %d targets our site: goal=%s'):
format(c.id, tostring(c.goal)))
end
end
-- is the succession-traveler mechanism alive anywhere in the world?
local goal_counts = {}
for _, c in ipairs(world.army_controllers.all) do
local g = tostring(c.goal)
goal_counts[g] = (goal_counts[g] or 0) + 1
end
print('army_controller goals worldwide:')
for g, n in pairs(goal_counts) do
print((' %s x%d'):format(g, n))
end
print(('civilized_actor nemeses: %d'):format(#world.nemesis.civilized_actor))
end
local function cmd_vacate(opts)
local entity = civ()
local rows = site_land_holder_assignments(entity, true)
local touched = 0
for _, row in ipairs(rows) do
local a, def = row.assignment, row.def
local hf = hf_of(a.histfig)
local clear = false
if a.histfig == -1 then
-- already vacant; make sure it's queued
queue_vacancy(entity, a)
print(('assignment %d (%s): already vacant, re-queued'):
format(a.id, def.code))
touched = touched + 1
elseif holder_is_dead(hf) then
clear = true
elseif opts.living and holder_off_site(hf) then
clear = true
end
if clear then
if hf then
strip_position_link(entity, hf, def, a)
else
assignment_cleanup(entity, def, a)
end
print(('assignment %d (%s): cleared holder %d, queued for succession'):
format(a.id, def.code, a.histfig2))
touched = touched + 1
end
end
if touched == 0 then
print('nothing to vacate: no dead or absent land-holder assignments')
else
print(('%d assignment(s) queued; native succession will run on the '
.. 'next entity tick'):format(touched))
end
end
local function cmd_claim(opts)
local entity = civ()
local rows = site_land_holder_assignments(entity, true)
local pushed = 0
for _, row in ipairs(rows) do
local a, def = row.assignment, row.def
local hf = hf_of(a.histfig)
if a.histfig ~= -1 and (holder_is_dead(hf)
or (opts.living and holder_off_site(hf))) then
strip_position_link(entity, hf, def, a)
end
if a.histfig == -1 then
if not in_vector(entity.positions.possible_claimable, a) then
entity.positions.possible_claimable:insert('#', a)
end
entity.flags.make_nems_check_positions = true
world.flags.an_entity_wants_to_mark_news_for_position_claim = true
print(('assignment %d (%s): queued for claimant'):
format(a.id, def.code))
pushed = pushed + 1
end
end
if pushed == 0 then
print('no vacant land-holder assignments to claim')
else
print(('queued %d claim(s); a claimant should be generated and '
.. 'travel to the fort'):format(pushed))
end
end
-- Point the assignment's bound-site field (st_id, +0x2c) at our fort.
-- st_id is a SITE ID: the sweep resolves it through get_site and builds a
-- MOVE_TO_SITE army for the holder. NOTE: the sweep's land_holder>0 gate
-- (def+0x2c8, jg -> tail) fires BEFORE this read -- the native path only
-- works if that check is neutralized (debugger NOP test) or the position
-- is not a land-holder. If a created army heads for the wrong site, the
-- live controller can be retargeted: set site_id/pos_x/pos_y directly
-- (verified live: a retargeted army delivered the holder correctly).
local function cmd_recall(opts)
local entity = civ()
local rows = site_land_holder_assignments(entity, true)
if #rows == 0 then
print('no land-holder assignment tied to this site')
return
end
for _, row in ipairs(rows) do
local a, def = row.assignment, row.def
local hf = hf_of(a.histfig)
if not hf or holder_is_dead(hf) then
print(('assignment %d (%s): no living holder to recall'):
format(a.id, def.code))
elseif holder_off_site(hf) == false then
print(('assignment %d (%s): holder already at this site'):
format(a.id, def.code))
elseif plotinfo.site_id == a.st_id then
print(('assignment %d (%s): st_id already points here; '
.. 'sweep should act next tick'):format(a.id, def.code))
else
local old = a.st_id
a.st_id = plotinfo.site_id
print(('assignment %d (%s): st_id %d -> %d; '
.. 'native sweep should create a MOVE_TO_SITE army'):
format(a.id, def.code, old, a.st_id))
end
end
end
-- a recalled holder's MOVE_TO_SITE army delivers them as a visitor:
-- materialized at the fort but still flagged guest and uninstalled.
-- unitst::make_own (via dfhack.units.makeown) is the native routine the
-- petition-for-citizenship flow uses -- it grants the MEMBER link to the
-- fort's site government, updates the entity's member lists, switches
-- whereabouts to settler, and clears guest/visitor flags. Verified live:
-- after makeown the holder appears on the nobles screen and citizens
-- list. st_id is then reset to -1 (the settled-state convention).
local function cmd_settle(opts)
local entity = civ()
local rows = site_land_holder_assignments(entity, true)
if #rows == 0 then
print('no land-holder assignment tied to this site')
return
end
for _, row in ipairs(rows) do
local a, def = row.assignment, row.def
local hf = hf_of(a.histfig)
local wb = hf and hf.info and hf.info.whereabouts
local nem = hf and nemesis_of_hfid(hf.id)
local unit = nem and nem.unit
if not hf then
print(('assignment %d (%s): no holder to settle'):
format(a.id, def.code))
elseif not wb or wb.site_id ~= plotinfo.site_id then
print(('assignment %d (%s): holder not at this site (site %d)'):
format(a.id, def.code, wb and wb.site_id or -1))
elseif not unit then
print(('assignment %d (%s): holder unit not materialized yet'):
format(a.id, def.code))
elseif not (unit.flags3.guest or unit.flags2.visitor
or unit.flags2.visitor_uninvited
or wb.state ~= df.whereabouts_type.settler)
then
print(('assignment %d (%s): holder already settled'):
format(a.id, def.code))
else
-- unitst::make_own is history-silent: emit the link/state
-- events a natural settle produces so legends stay complete
local gov = df.historical_entity.find(plotinfo.group_id)
local had_member = false
for _, l in ipairs(hf.entity_links) do
if l:getType() == df.histfig_entity_link_type.MEMBER
and l.entity_id == plotinfo.group_id then
had_member = true break
end
end
dfhack.units.makeown(unit)
local has_member = false
for _, l in ipairs(hf.entity_links) do
if l:getType() == df.histfig_entity_link_type.MEMBER
and l.entity_id == plotinfo.group_id then
has_member = true break
end
end
if gov and not had_member and has_member then
new_add_member_event(gov, hf)
end
new_change_state_event(hf, df.whereabouts_type.settler,
plotinfo.site_id, df.history_event_reason.become_citizen)
if a.st_id == plotinfo.site_id then a.st_id = -1 end
local who = dfhack.df2utf(
dfhack.translation.translateName(hf.name, true))
local what = def.name[0] ~= '' and def.name[0]
or def.code:lower()
dfhack.gui.showAnnouncement(
('%s has arrived to take the position of %s.'):
format(who, what), COLOR_CYAN, true)
print(('assignment %d (%s): settled holder hf %d at site %d '
.. '(st_id now %d)'):
format(a.id, def.code, hf.id, plotinfo.site_id, a.st_id))
end
end
end
-- Emulated MOVE_TO_SITE army creation, replicating the tail of the
-- civilized-actor sweep (FUN_14147d730) for a single nemesis -- the
-- patch-free alternative to bypassing the land_holder gate. Per the
-- decompile the sweep: allocates an army_controller, fills the
-- goal_move_to_site data, allocates an army, add_nemesis, places it at
-- the nemesis's smm position. Uncertain fields (tracking/sneak/odor
-- ratings, path vectors, travel_rate) are left at benign defaults; the
-- native army-advance machinery drives the march itself.
local function cmd_fetch(opts)
local entity = civ()
local rows = site_land_holder_assignments(entity, true)
if #rows == 0 then
print('no land-holder assignment tied to this site')
return
end
local site = df.world_site.find(plotinfo.site_id)
if not site then qerr('cannot find our site %d', plotinfo.site_id) end
-- smm coords = site rect (embark tiles) * 3, verified live
local dst_x = math.floor((site.global_min_x + site.global_max_x) / 2) * 3
local dst_y = math.floor((site.global_min_y + site.global_max_y) / 2) * 3
for _, row in ipairs(rows) do
local a, def = row.assignment, row.def
local hf = hf_of(a.histfig)
local wb = hf and hf.info and hf.info.whereabouts
local nem = hf and nemesis_of_hfid(hf.id)
if not hf or holder_is_dead(hf) then
print(('assignment %d (%s): no living holder to fetch'):
format(a.id, def.code))
elseif not wb then
print(('assignment %d (%s): holder has no whereabouts'):
format(a.id, def.code))
elseif wb.site_id == plotinfo.site_id then
print(('assignment %d (%s): holder already at this site'):
format(a.id, def.code))
elseif wb.state == df.whereabouts_type.army_died
or wb.state == df.whereabouts_type.army_survived then
print(('assignment %d (%s): holder already traveling '
.. '(army %d)'):format(a.id, def.code, wb.army_id))
elseif not nem then
print(('assignment %d (%s): no nemesis record'):
format(a.id, def.code))
else
-- mirror the native landmass gate: armies cannot pathfind
-- across water, so a cross-landmass fetch would leave the
-- holder stranded in-army forever (native blocks this too)
local lm_hf, lm_dst = hf_landmass(wb), site_landmass(site)
if lm_hf ~= nil and lm_dst ~= nil and lm_hf ~= lm_dst
and not opts.force
then
print(('assignment %d (%s): holder on landmass %s, '
.. 'fort on %s -- armies cannot cross water; '
.. 'use --force to try anyway or install locally'):
format(a.id, def.code,
tostring(lm_hf), tostring(lm_dst)))
goto continue
end
-- start at his smm pos; if abstract (-1), start at his site
local src_x, src_y = wb.abs_smm_x, wb.abs_smm_y
if (src_x == -1 or src_y == -1) and wb.site_id ~= -1 then
local s = df.world_site.find(wb.site_id)
if s then
src_x = math.floor(
(s.global_min_x + s.global_max_x) / 2) * 3
src_y = math.floor(
(s.global_min_y + s.global_max_y) / 2) * 3
end
end
if src_x == -1 or src_y == -1 then
print(('assignment %d (%s): cannot place army '
.. '(no smm position)'):format(a.id, def.code))
goto continue
end
-- mirrors evaluate_entity_strength's creation tail (army_controller_pool_get
-- [object_pool<army_controller>::get]) /
-- armyst::_create (armyst constructor) are the allocators that also insert into the
-- world vectors; set_goal/add_nemesis/place are the native
-- calls our struct writes stand in for). Successor-traveler
-- controllers are entityless (entity_id=-1) and are NOT
-- pushed into entity->armies/army_controllers -- those
-- vectors are for entity-owned military ops only.
local c = df.army_controller:new()
c.id = df.global.army_controller_next_id
df.global.army_controller_next_id = c.id + 1
c.entity_id = -1
c.site_id = plotinfo.site_id
c.subregion_id = -1
c.pos_x = -1 -- native inits -1; march coords fill in later
c.pos_y = -1
c.year = df.global.cur_year
c.year_tick = df.global.cur_season_tick
c.parent_id = -1
c.master_id = c.id
c.master_hf = -1
c.commander_hf = -1
c.origin_task_holder_nemesis_id = -1
c.origin_task_id = -1
c.origin_plot_holder_nemesis_id = -1
c.origin_plot_id = -1
local g = df.army_controller_goal_move_to_sitest:new()
g.goal_hfid = hf.id
g.goal_srbid = -1
g.flag.RETURNING_TO_CURRENT_HOME = true