forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-offsets.lua
1963 lines (1683 loc) · 55.1 KB
/
find-offsets.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
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
-- Find some global addresses
--luacheck:skip-entirely
--[====[
devel/find-offsets
==================
.. warning::
THIS SCRIPT IS STRICTLY FOR DFHACK DEVELOPERS.
Running this script on a new DF version will NOT
MAKE IT RUN CORRECTLY if any data structures
changed, thus possibly leading to CRASHES AND/OR
PERMANENT SAVE CORRUPTION.
Finding the first few globals requires this script to be
started immediately after loading the game, WITHOUT
first loading a world. The rest expect a loaded save,
not a fresh embark. Finding current_weather requires
a special save previously processed with `devel/prepare-save`
on a DF version with working dfhack.
The script expects vanilla game configuration, without
any custom tilesets or init file changes. Never unpause
the game unless instructed. When done, quit the game
without saving using 'die'.
Arguments:
* global names to force finding them
* ``all`` to force all globals
* ``nofeed`` to block automated fake input searches
* ``nozoom`` to disable neighboring object heuristics
]====]
--luacheck-flags: strictsubtype
local utils = require 'utils'
local ms = require 'memscan'
local gui = require 'gui'
local is_known = dfhack.internal.getAddress
local os_type = dfhack.getOSType()
local force_scan = {} --as:bool[]
for _,v in ipairs({...}) do
force_scan[v] = true
end
PTR_SIZE = (function()
local tmp = df.new('uintptr_t')
local size = tmp:sizeof()
tmp:delete()
return size
end)()
collectgarbage()
function prompt_proceed(indent)
if not indent then indent = 0 end
return utils.prompt_yes_no(string.rep(' ', indent) .. 'Proceed?', true)
end
print[[
WARNING: THIS SCRIPT IS STRICTLY FOR DFHACK DEVELOPERS.
Running this script on a new DF version will NOT
MAKE IT RUN CORRECTLY if any data structures
changed, thus possibly leading to CRASHES AND/OR
PERMANENT SAVE CORRUPTION.
Finding the first few globals requires this script to be
started immediately after loading the game, WITHOUT
first loading a world. The rest expect a loaded save,
not a fresh embark. Finding current_weather requires
a special save previously processed with devel/prepare-save
on a DF version with working dfhack.
The script expects vanilla game configuration, without
any custom tilesets or init file changes. Never unpause
the game unless instructed. When done, quit the game
without saving using 'die'.
]]
if not utils.prompt_yes_no('Proceed?') then
return
end
-- Data segment location
local data = ms.get_data_segment()
if not data then
qerror('Could not find data segment')
end
print('\nData section: '..tostring(data))
if data.size < 5000000 then
qerror('Data segment too short.')
end
local searcher = ms.DiffSearcher.new(data)
local function get_screen(class, prompt)
if not is_known('gview') then
print('Please navigate to '..prompt)
if not prompt_proceed() then
return nil, false
end
return nil, true
end
while true do
local cs = dfhack.gui.getCurViewscreen(true)
if not df.is_instance(class, cs) then
print('Please navigate to '..prompt)
if not prompt_proceed() then
return nil, false
end
else
return cs, true
end
end
end
local function screen_title()
return get_screen(df.viewscreen_titlest, 'the title screen')
end
local function screen_dwarfmode()
return get_screen(df.viewscreen_dwarfmodest, 'the main dwarf mode screen')
end
local function validate_offset(name,validator,addr,tname,...)
local obj = data:object_by_field(addr,tname,...)
if obj and not validator(obj) then
obj = nil
end
ms.found_offset(name,obj)
end
local function zoomed_searcher(startn, end_or_sz, bidirectional)
if force_scan.nozoom then
return nil
end
local sv = is_known(startn)
if not sv then
return nil
end
local ev
if type(end_or_sz) == 'number' then
ev = sv + end_or_sz
if end_or_sz < 0 then
sv, ev = ev, sv
end
else
ev = is_known(end_or_sz)
if not ev then
return nil
end
end
if bidirectional then
sv = sv - (ev - sv)
end
sv = sv - (sv % 4)
ev = ev + 3
ev = ev - (ev % 4)
if data:contains_range(sv, ev-sv) then
return ms.DiffSearcher.new(ms.MemoryArea.new(sv,ev))
end
end
local finder_searches = {} --as:string[]
local function exec_finder(finder, names, validators)
local validators = validators --as:{_type:function,_node:bool}[]
if type(names) ~= 'table' then
names = { names } --luacheck: retype
end
if type(validators) ~= 'table' then --luacheck: skip
validators = { validators }
end
local search = force_scan['all']
for k,v in ipairs(names) do
if force_scan[v] or not is_known(v) then
table.insert(finder_searches, v)
search = true
elseif validators[k] then
if not validators[k](df.global[v]) then --luacheck: skip
dfhack.printerr('Validation failed for '..v..', will try to find again')
table.insert(finder_searches, v)
search = true
end
end
end
if search then
local ok, err = dfhack.safecall(finder)
if not ok then
if tostring(err):find('abort') or not utils.prompt_yes_no('Proceed with the rest of the script?') then
searcher:reset()
qerror('Quit')
end
end
else
print('Already known: '..table.concat(names,', '))
end
end
local ordinal_names = {
[0] = '1st entry',
[1] = '2nd entry',
[2] = '3rd entry'
}
setmetatable(ordinal_names, {
__index = function(self,idx) return (idx+1)..'th entry' end
})
local function list_index_choices(length_func)
return function(id)
if id > 0 then
local ok, len = pcall(length_func)
if not ok then
len = 5
elseif len > 10 then
len = 10
end
return id % len
else
return 0
end
end
end
local function can_feed()
return not force_scan['nofeed'] and is_known 'gview'
end
local function dwarfmode_feed_input(...)
local screen = screen_dwarfmode()
if not df.isvalid(screen) then
qerror('could not retrieve dwarfmode screen')
end
try_save_cursor()
for _,v in ipairs({...}) do
gui.simulateInput(screen, v)
end
end
local function dwarfmode_step_frames(count)
local screen = screen_dwarfmode()
if not df.isvalid(screen) then
qerror('could not retrieve dwarfmode screen')
end
for i = 1,(count or 1) do
gui.simulateInput(screen, 'D_ONESTEP')
if screen.keyRepeat ~= 1 then
qerror('Could not step one frame: . did not work')
end
screen:logic()
end
end
local function dwarfmode_to_top()
if not can_feed() then
return false
end
local screen = screen_dwarfmode()
if not df.isvalid(screen) then
return false
end
for i=0,10 do
if is_known 'ui' and df.global.ui.main.mode == df.ui_sidebar_mode.Default then
break
end
gui.simulateInput(screen, 'LEAVESCREEN')
end
-- force pause just in case
screen.keyRepeat = 1
return true
end
local prev_cursor = df.global.T_cursor:new()
prev_cursor.x = -30000
function try_save_cursor()
if not dfhack.internal.getAddress('cursor') then return end
for _, v in pairs(df.global.cursor) do
if v < 0 then
return
end
end
prev_cursor:assign(df.global.cursor)
end
function try_restore_cursor()
if not dfhack.internal.getAddress('cursor') then return end
if prev_cursor.x >= 0 then
df.global.cursor:assign(prev_cursor)
dfhack.gui.refreshSidebar()
end
end
local function feed_menu_choice(catnames,catkeys,enum,enter_seq,exit_seq,prompt)
return function (idx)
if idx == 0 and prompt and not prompt_proceed(2) then
return false
end
if idx > 0 then
dwarfmode_feed_input(table.unpack(exit_seq or {}))
end
idx = idx % #catnames + 1
dwarfmode_feed_input(table.unpack(enter_seq or {}))
dwarfmode_feed_input(catkeys[idx])
if enum then
return true, enum[catnames[idx]]
else
return true, catnames[idx]
end
end
end
local function feed_list_choice(count,upkey,downkey)
return function(idx)
if idx > 0 then
local ok, len
if type(count) == 'number' then
ok, len = true, count
else
ok, len = pcall(count)
end
if not ok then
len = 5
elseif len > 10 then
len = 10
end
local hcnt = len-1
local rix = 1 + (idx-1) % (hcnt*2)
if rix >= hcnt then
dwarfmode_feed_input(upkey or 'SECONDSCROLL_UP')
return true, hcnt*2 - rix
else
dwarfmode_feed_input(donwkey or 'SECONDSCROLL_DOWN')
return true, rix
end
else
print(' Please select the first list item.')
if not prompt_proceed(2) then
return false
end
return true, 0
end
end
end
local function feed_menu_bool(enter_seq, exit_seq)
return function(idx)
if idx == 0 then
if not prompt_proceed(2) then
return false
end
return true, 0
end
if idx == 5 then
print(' Please resize the game window.')
if not prompt_proceed(2) then
return false
end
end
if idx%2 == 1 then
dwarfmode_feed_input(table.unpack(enter_seq))
return true, 1
else
dwarfmode_feed_input(table.unpack(exit_seq))
return true, 0
end
end
end
--
-- Cursor group
--
local function find_cursor()
local _, ok = screen_title()
if not ok then
return false
end
-- Unpadded version
local idx, addr = data.int32_t:find_one{
-30000, -30000, -30000,
-30000, -30000, -30000, -30000, -30000, -30000,
df.game_mode.NONE, df.game_type.NONE
}
if idx then
ms.found_offset('cursor', addr)
ms.found_offset('selection_rect', addr + 12)
ms.found_offset('gamemode', addr + 12 + 24)
ms.found_offset('gametype', addr + 12 + 24 + 4)
return true
end
-- Padded version
idx, addr = data.int32_t:find_one{
-30000, -30000, -30000, 0,
-30000, -30000, -30000, -30000, -30000, -30000, 0, 0,
df.game_mode.NONE, 0, 0, 0, df.game_type.NONE
}
if idx then
ms.found_offset('cursor', addr)
ms.found_offset('selection_rect', addr + 0x10)
ms.found_offset('gamemode', addr + 0x30)
ms.found_offset('gametype', addr + 0x40)
return true
end
-- New in 0.43.05 x64
idx, addr = data.int32_t:find_one{
-30000, -30000, -30000, 0, 0,
-30000, -30000, -30000, -30000, -30000, -30000,
df.game_mode.NONE, df.game_type.NONE
}
if idx then
ms.found_offset('cursor', addr)
ms.found_offset('selection_rect', addr + 0x14)
ms.found_offset('gamemode', addr + 0x2C)
ms.found_offset('gametype', addr + 0x30)
return true
end
-- New in 0.43.05 x64 Linux
if os_type == 'linux' then
idx, addr = data.int32_t:find_one{
df.game_type.NONE, 0, 0, 0,
df.game_mode.NONE, 0, 0, 0,
-30000, -30000, -30000, -30000,
-30000, -30000, 0, 0,
-30000, -30000, -30000
}
if idx then
ms.found_offset('cursor', addr + 0x40)
ms.found_offset('selection_rect', addr + 0x20)
ms.found_offset('gamemode', addr + 0x10)
ms.found_offset('gametype', addr)
return true
end
end
dfhack.printerr('Could not find cursor.')
return false
end
--
-- d_init
--
local function is_valid_d_init(di)
if di.sky_tile ~= 178 then
print('Sky tile expected 178, found: '..di.sky_tile)
if not utils.prompt_yes_no('Ignore?') then
return false
end
end
return true
end
local function find_d_init()
local idx, addr = data.int16_t:find_one{
1,0, 2,0, 5,0, 25,0, -- path_cost
4,4, -- embark_rect
20,1000,1000,1000,1000 -- store_dist
}
if idx then
validate_offset('d_init', is_valid_d_init, addr, df.d_init, 'path_cost')
return
end
dfhack.printerr('Could not find d_init')
end
--
-- gview
--
local function find_gview()
local vs_vtable = dfhack.internal.getVTable('viewscreenst')
if not vs_vtable then
dfhack.printerr('Cannot search for gview - no viewscreenst vtable.')
return
end
local idx, addr = data.uintptr_t:find_one{0, vs_vtable}
if idx then
ms.found_offset('gview', addr)
return
end
idx, addr = data.uintptr_t:find_one{100, vs_vtable}
if idx then
ms.found_offset('gview', addr)
return
end
dfhack.printerr('Could not find gview')
end
--
-- enabler
--
local function lookup_colors()
local f = io.open('data/init/colors.txt', 'r') or error('failed to open file')
local text = f:read('*all')
f:close()
local colors = {}
for _, color in pairs({'BLACK', 'BLUE', 'GREEN', 'CYAN', 'RED', 'MAGENTA',
'BROWN', 'LGRAY', 'DGRAY', 'LBLUE', 'LGREEN', 'LCYAN', 'LRED',
'LMAGENTA', 'YELLOW', 'WHITE'}) do
for _, part in pairs({'R', 'G', 'B'}) do
local opt = color .. '_' .. part
table.insert(colors, tonumber(text:match(opt .. ':(%d+)') or error('missing from colors.txt: ' .. opt)))
end
end
return colors
end
local function is_valid_enabler(e)
if not ms.is_valid_vector(e.textures.raws, PTR_SIZE)
or not ms.is_valid_vector(e.text_system, PTR_SIZE)
then
dfhack.printerr('Vector layout check failed.')
return false
end
return true
end
local function find_enabler()
-- Data from data/init/colors.txt
local default_colors = {
0, 0, 0, 0, 0, 128, 0, 128, 0,
0, 128, 128, 128, 0, 0, 128, 0, 128,
128, 128, 0, 192, 192, 192, 128, 128, 128,
0, 0, 255, 0, 255, 0, 0, 255, 255,
255, 0, 0, 255, 0, 255, 255, 255, 0,
255, 255, 255
}
local colors
local ok, ret = pcall(lookup_colors)
if not ok then
dfhack.printerr('Failed to look up colors, using defaults: \n' .. ret)
colors = default_colors
else
colors = ret
end
for i = 1,#colors do colors[i] = colors[i]/255 end
local idx, addr = data.float:find_one(colors)
if not idx then
idx, addr = data.float:find_one(default_colors)
end
if idx then
validate_offset('enabler', is_valid_enabler, addr, df.enabler, 'ccolor')
return
end
dfhack.printerr('Could not find enabler')
end
--
-- gps
--
local function is_valid_gps(g)
if g.clipx[0] < 0 or g.clipx[0] > g.clipx[1] or g.clipx[1] >= g.dimx then
dfhack.printerr('Invalid clipx: ', g.clipx[0], g.clipx[1], g.dimx)
end
if g.clipy[0] < 0 or g.clipy[0] > g.clipy[1] or g.clipy[1] >= g.dimy then
dfhack.printerr('Invalid clipy: ', g.clipy[0], g.clipy[1], g.dimy)
end
return true
end
local function find_gps()
print('\nPlease ensure the mouse cursor is not over the game window.')
if not prompt_proceed() then
return
end
local zone
if os_type == 'windows' or os_type == 'linux' then
zone = zoomed_searcher('cursor', 0x1000)
elseif os_type == 'darwin' then
zone = zoomed_searcher('enabler', 0x1000)
end
zone = zone or searcher
local w,h = ms.get_screen_size()
local idx, addr = zone.area.int32_t:find_one{w, h, -1, -1}
if not idx then
idx, addr = data.int32_t:find_one{w, h, -1, -1}
end
if idx then
validate_offset('gps', is_valid_gps, addr, df.graphic, 'dimx')
return
end
dfhack.printerr('Could not find gps')
end
--
-- World
--
local function is_valid_world(world)
if not ms.is_valid_vector(world.units.all, PTR_SIZE)
or not ms.is_valid_vector(world.units.active, PTR_SIZE)
or not ms.is_valid_vector(world.units.bad, PTR_SIZE)
or not ms.is_valid_vector(world.history.figures, PTR_SIZE)
or not ms.is_valid_vector(world.features.map_features, PTR_SIZE)
then
dfhack.printerr('Vector layout check failed.')
return false
end
if #world.units.all == 0 or #world.units.all ~= #world.units.bad then
print('Different or zero size of units.all and units.bad:'..#world.units.all..' vs '..#world.units.bad)
if not utils.prompt_yes_no('Ignore?') then
return false
end
end
return true
end
local function find_world()
local catnames = {
'Corpses', 'Refuse', 'Stone', 'Wood', 'Gems', 'Bars', 'Cloth', 'Leather', 'Ammo', 'Coins'
}
local catkeys = {
'STOCKPILE_GRAVEYARD', 'STOCKPILE_REFUSE', 'STOCKPILE_STONE', 'STOCKPILE_WOOD',
'STOCKPILE_GEM', 'STOCKPILE_BARBLOCK', 'STOCKPILE_CLOTH', 'STOCKPILE_LEATHER',
'STOCKPILE_AMMO', 'STOCKPILE_COINS'
}
local addr
if dwarfmode_to_top() then
dwarfmode_feed_input('D_STOCKPILES')
addr = searcher:find_interactive(
'Auto-searching for world.',
'int32_t',
feed_menu_choice(catnames, catkeys, df.stockpile_category),
20
)
end
if not addr then
addr = searcher:find_menu_cursor([[
Searching for world. Please open the stockpile creation
menu, and select different types as instructed below:]],
'int32_t', catnames, df.stockpile_category
)
end
validate_offset('world', is_valid_world, addr, df.world, 'selected_stockpile_type')
end
--
-- UI
--
local function is_valid_ui(ui)
if not ms.is_valid_vector(ui.economic_stone, 1)
or not ms.is_valid_vector(ui.dipscripts, PTR_SIZE)
then
dfhack.printerr('Vector layout check failed.')
return false
end
if ui.follow_item ~= -1 or ui.follow_unit ~= -1 then
print('Invalid follow state: '..ui.follow_item..', '..ui.follow_unit)
return false
end
return true
end
local function find_ui()
local catnames = {
'DesignateMine', 'DesignateChannel', 'DesignateRemoveRamps',
'DesignateUpStair', 'DesignateDownStair', 'DesignateUpDownStair',
'DesignateUpRamp', 'DesignateChopTrees'
}
local catkeys = {
'DESIGNATE_DIG', 'DESIGNATE_CHANNEL', 'DESIGNATE_DIG_REMOVE_STAIRS_RAMPS',
'DESIGNATE_STAIR_UP', 'DESIGNATE_STAIR_DOWN', 'DESIGNATE_STAIR_UPDOWN',
'DESIGNATE_RAMP', 'DESIGNATE_CHOP'
}
local addr
if dwarfmode_to_top() then
dwarfmode_feed_input('D_DESIGNATE')
addr = searcher:find_interactive(
'Auto-searching for ui.',
'int16_t',
feed_menu_choice(catnames, catkeys, df.ui_sidebar_mode),
20
)
end
if not addr then
addr = searcher:find_menu_cursor([[
Searching for ui. Please open the designation
menu, and switch modes as instructed below:]],
'int16_t', catnames, df.ui_sidebar_mode
)
end
validate_offset('ui', is_valid_ui, addr, df.ui, 'main', 'mode')
end
--
-- ui_sidebar_menus
--
local function is_valid_ui_sidebar_menus(usm)
if not ms.is_valid_vector(usm.workshop_job.choices_all, 4)
or not ms.is_valid_vector(usm.workshop_job.choices_visible, 4)
then
dfhack.printerr('Vector layout check failed.')
return false
end
if #usm.workshop_job.choices_all == 0
or #usm.workshop_job.choices_all ~= #usm.workshop_job.choices_visible then
print('Different or zero size of visible and all choices:'..
#usm.workshop_job.choices_all..' vs '..#usm.workshop_job.choices_visible)
if not utils.prompt_yes_no('Ignore?') then
return false
end
end
return true
end
local function find_ui_sidebar_menus()
local addr
if dwarfmode_to_top() then
dwarfmode_feed_input('D_BUILDJOB')
addr = searcher:find_interactive([[
Auto-searching for ui_sidebar_menus. Please select a Mason's,
Craftsdwarf's, or Carpenter's workshop:]],
'int32_t',
function(idx)
if idx == 0 then
prompt_proceed(2)
-- ensure that the job list isn't full
dwarfmode_feed_input('BUILDJOB_CANCEL', 'BUILDJOB_ADD')
return true, 0
end
return feed_list_choice(7)(idx)
end,
20
)
end
if not addr then
addr = searcher:find_menu_cursor([[
Searching for ui_sidebar_menus. Please switch to 'q' mode,
select a Mason, Craftsdwarfs, or Carpenters workshop, open
the Add Job menu, and move the cursor within:]],
'int32_t',
{ 0, 1, 2, 3, 4, 5, 6 },
ordinal_names
)
end
validate_offset('ui_sidebar_menus', is_valid_ui_sidebar_menus,
addr, df.ui_sidebar_menus, 'workshop_job', 'cursor')
end
--
-- ui_build_selector
--
local function is_valid_ui_build_selector(ubs)
if not ms.is_valid_vector(ubs.requirements, 4)
or not ms.is_valid_vector(ubs.choices, 4)
then
dfhack.printerr('Vector layout check failed.')
return false
end
if ubs.building_type ~= df.building_type.Trap
or ubs.building_subtype ~= df.trap_type.PressurePlate then
print('Invalid building type and subtype:'..ubs.building_type..','..ubs.building_subtype)
return false
end
return true
end
local function find_ui_build_selector()
local addr
if dwarfmode_to_top() then
addr = searcher:find_interactive([[
Auto-searching for ui_build_selector. This requires mechanisms.]],
'int32_t',
function(idx)
if idx == 0 then
dwarfmode_to_top()
dwarfmode_feed_input(
'D_BUILDING',
'HOTKEY_BUILDING_TRAP',
'HOTKEY_BUILDING_TRAP_TRIGGER',
'BUILDING_TRIGGER_ENABLE_CREATURE'
)
else
dwarfmode_feed_input('BUILDING_TRIGGER_MIN_SIZE_UP')
end
return true, 5000 + 1000*idx
end,
20
)
end
if not addr then
addr = searcher:find_menu_cursor([[
Searching for ui_build_selector. Please start constructing
a pressure plate, and enable creatures. Then change the min
weight as requested, knowing that the ui shows 5000 as 5K:]],
'int32_t',
{ 5000, 6000, 7000, 8000, 9000, 10000, 11000 }
)
end
validate_offset('ui_build_selector', is_valid_ui_build_selector,
addr, df.ui_build_selector, 'plate_info', 'unit_min')
end
--
-- init
--
local function is_valid_init(i)
-- derived from curses_*.png image sizes presumably
if i.font.small_font_dispx ~= 8 or i.font.small_font_dispy ~= 12 or
i.font.large_font_dispx ~= 10 or i.font.large_font_dispy ~= 12 then
print('Unexpected font sizes: ',
i.font.small_font_dispx, i.font.small_font_dispy,
i.font.large_font_dispx, i.font.large_font_dispy)
if not utils.prompt_yes_no('Ignore?') then
return false
end
end
return true
end
local function find_init()
local zone
--[[if os_type == 'windows' then
zone = zoomed_searcher('ui_build_selector', 0x3000)
elseif os_type == 'linux' or os_type == 'darwin' then
zone = zoomed_searcher('d_init', -0x2000)
end]]
zone = zone or searcher
local idx, addr = zone.area.long:find_one{250, 150, 15, 0}
if idx then
validate_offset('init', is_valid_init, addr, df.init, 'input', 'hold_time')
return
end
local w,h = ms.get_screen_size()
local idx, addr = zone.area.int32_t:find_one{w, h}
if idx then
validate_offset('init', is_valid_init, addr, df.init, 'display', 'grid_x')
return
end
dfhack.printerr('Could not find init')
end
--
-- current_weather
--
local function find_current_weather()
local zone
if os_type == 'windows' then
zone = zoomed_searcher('crime_next_id', 512)
elseif os_type == 'darwin' then
zone = zoomed_searcher('cursor', 128, true)
elseif os_type == 'linux' then
zone = zoomed_searcher('ui_selected_unit', 512)
end
zone = zone or searcher
local wbytes = {
2, 1, 0, 2, 0,
1, 2, 1, 0, 0,
2, 0, 2, 1, 2,
1, 2, 0, 1, 1,
2, 0, 1, 0, 2
}
local idx, addr = zone.area.int8_t:find_one(wbytes)
if not idx then
idx, addr = data.int8_t:find_one(wbytes)
end
if idx then
ms.found_offset('current_weather', addr)
return
end
dfhack.printerr('Could not find current_weather - must be a wrong save.')
end
--
-- ui_menu_width
--
local function find_ui_menu_width()
local addr
if dwarfmode_to_top() then
addr = searcher:find_interactive('Auto-searching for ui_menu_width', 'int8_t', function(idx)
local val = (idx % 3) + 1
if idx == 0 then
print('Switch to the default [map][menu][map] layout (with Tab)')
if not prompt_proceed(2) then return false end
else
dwarfmode_feed_input('CHANGETAB', val ~= 3 and 'CHANGETAB')
end
return true, val
end)
end
if not addr then
addr = searcher:find_menu_cursor([[
Searching for ui_menu_width. Please exit to the main
dwarfmode menu, then use Tab to do as instructed below:]],
'int8_t',
{ 2, 3, 1 },
{ [2] = 'switch to the most usual [mapmap][menu] layout',
[3] = 'hide the menu completely',
[1] = 'switch to the default [map][menu][map] layout' }
)
end
ms.found_offset('ui_menu_width', addr)
-- reset to make sure view is small enough for window_x/y scan on small maps
df.global.ui_menu_width[0] = 2
df.global.ui_menu_width[1] = 3
end
--
-- ui_selected_unit
--
local function find_ui_selected_unit()
if not is_known 'world' then
dfhack.printerr('Cannot search for ui_selected_unit: no world')
return
end
for i,unit in ipairs(df.global.world.units.active) do
-- This function does a lot of things and accesses histfigs, souls and so on:
--dfhack.units.setNickname(unit, i)
-- Instead use just a simple bit of code that only requires the start of the
-- unit to be valid. It may not work properly with vampires or reset later
-- if unpaused, but is sufficient for this script and won't crash:
unit.name.nickname = tostring(i)
unit.name.has_name = true
end
local addr = searcher:find_menu_cursor([[
Searching for ui_selected_unit. Please activate the 'v'
mode, point it at units, and enter their numeric nickname
into the prompts below:]],
'int32_t',
function()