forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings-manager.lua
More file actions
1542 lines (1416 loc) · 53.7 KB
/
settings-manager.lua
File metadata and controls
1542 lines (1416 loc) · 53.7 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
--@ module = true
local argparse = require('argparse')
local control_panel = reqscript('control-panel')
local dialogs = require('gui.dialogs')
local gui = require('gui')
local json = require('json')
local overlay = require('plugins.overlay')
local utils = require('utils')
local widgets = require('gui.widgets')
local GLOBAL_KEY = 'settings-manager'
config = config or json.open("dfhack-config/settings-manager.json")
--------------------------
-- DifficultyOverlayBase
--
DifficultyOverlayBase = defclass(DifficultyOverlayBase, overlay.OverlayWidget)
DifficultyOverlayBase.ATTRS {
frame={w=46, h=5},
frame_style=gui.MEDIUM_FRAME,
frame_background=gui.CLEAR_PEN,
}
local function save_difficulty(df_difficulty)
local difficulty = utils.clone(df_difficulty, true)
for _, v in pairs(difficulty) do
if type(v) == 'table' and not v[1] then
for name in pairs(v) do
if tonumber(name) then
-- remove numeric "filler" vals from bitflag records
v[name] = nil
end
end
end
end
-- replace top-level button states to say "Custom"
-- one of the vanilla presets might actually apply, but we don't know that
-- unless we do some diffing
difficulty.difficulty_enemies = df.setting_difficulty_enemies_type.Custom
difficulty.difficulty_economy = df.setting_difficulty_economy_type.Custom
config.data.difficulty = difficulty
config:write()
end
local function load_difficulty(df_difficulty)
local difficulty = utils.clone(config.data.difficulty or {}, true)
for _, v in pairs(difficulty) do
if type(v) == 'table' and v[1] then
-- restore 0-based index for static arrays and prevent resizing
for i, elem in ipairs(v) do
v[i-1] = elem
v[i] = nil
end
v.resize = false
end
end
df_difficulty:assign(difficulty)
end
local function save_auto(val)
config.data.auto = val
config:write()
end
function DifficultyOverlayBase:init()
self:addviews{
widgets.HotkeyLabel{
view_id='save',
frame={l=0, t=0, w=16},
key='CUSTOM_SHIFT_S',
label='Save settings',
on_activate=self:callback('do_save'),
},
widgets.Label{
view_id='save_flash',
frame={l=6, t=0},
text='Saved',
text_pen=COLOR_GREEN,
visible=false,
},
widgets.HotkeyLabel{
view_id='load',
frame={l=22, t=0, w=22},
key='CUSTOM_SHIFT_L',
label='Load saved settings',
on_activate=self:callback('do_load'),
enabled=function() return next(config.data.difficulty or {}) end,
},
widgets.Label{
view_id='load_flash',
frame={l=28, t=0},
text='Loaded',
text_pen=COLOR_GREEN,
visible=false,
},
widgets.ToggleHotkeyLabel{
frame={l=0, t=2},
key='CUSTOM_SHIFT_A',
label='Apply saved settings for new embarks:',
on_change=save_auto,
initial_option=not not config.data.auto,
enabled=function() return next(config.data.difficulty or {}) end,
},
}
end
local function flash(self, which)
self.subviews[which].visible = false
self.subviews[which..'_flash'].visible = true
local end_ms = dfhack.getTickCount() + 5000
local function label_reset()
if dfhack.getTickCount() < end_ms then
dfhack.timeout(10, 'frames', label_reset)
else
self.subviews[which..'_flash'].visible = false
self.subviews[which].visible = true
end
end
label_reset()
end
-- overridden by subclasses
function DifficultyOverlayBase:get_df_struct()
end
function DifficultyOverlayBase:do_save()
flash(self, 'save')
save_difficulty(self:get_df_struct().difficulty)
end
function DifficultyOverlayBase:do_load()
flash(self, 'load')
load_difficulty(self:get_df_struct().difficulty)
end
function DifficultyOverlayBase:onInput(keys)
if self:get_df_struct().entering_value_str then return false end
return DifficultyOverlayBase.super.onInput(self, keys)
end
----------------------------
-- DifficultyEmbarkOverlay
--
DifficultyEmbarkOverlay = defclass(DifficultyEmbarkOverlay, DifficultyOverlayBase)
DifficultyEmbarkOverlay.ATTRS {
desc='Adds buttons to the embark difficulty screen for saving and restoring settings.',
default_pos={x=-20, y=5},
viewscreens='setupdwarfgame/CustomSettings',
default_enabled=true,
}
show_notification = show_notification or false
function DifficultyEmbarkOverlay:get_df_struct()
return dfhack.gui.getDFViewscreen(true)
end
function DifficultyEmbarkOverlay:onInput(keys)
show_notification = false
return DifficultyEmbarkOverlay.super.onInput(self, keys)
end
----------------------------------------
-- DifficultyEmbarkNotificationOverlay
--
DifficultyEmbarkNotificationOverlay = defclass(DifficultyEmbarkNotificationOverlay, overlay.OverlayWidget)
DifficultyEmbarkNotificationOverlay.ATTRS {
desc='Displays a message when saved difficulty settings have been automatically applied.',
default_pos={x=75, y=18},
viewscreens='setupdwarfgame/Default',
default_enabled=true,
frame={w=25, h=3},
}
function DifficultyEmbarkNotificationOverlay:init()
self:addviews{
widgets.Panel{
frame={h=3, b=0, w=25},
frame_style=gui.FRAME_MEDIUM,
frame_background=gui.CLEAR_PEN,
subviews={
widgets.Label{
text='Saved settings restored',
text_pen=COLOR_LIGHTGREEN,
},
},
visible=function() return show_notification end,
},
}
end
function DifficultyEmbarkNotificationOverlay:preUpdateLayout(parent_rect)
self.frame.w = parent_rect.width - (self.frame.l or (self.default_pos.x - 1))
end
function DifficultyEmbarkNotificationOverlay:render(dc)
local scr = dfhack.gui.getDFViewscreen(true)
self.frame.h = #scr.embark_profile == 0 and 11 or 3
DifficultyEmbarkNotificationOverlay.super.render(self, dc)
end
local last_scr_type
dfhack.onStateChange[GLOBAL_KEY] = function(sc)
if sc ~= SC_VIEWSCREEN_CHANGED then return end
local scr = dfhack.gui.getDFViewscreen(true)
if last_scr_type == scr._type then return end
last_scr_type = scr._type
show_notification = false
if not df.viewscreen_setupdwarfgamest:is_instance(scr) then return end
if not config.data.auto then return end
load_difficulty(scr.difficulty)
show_notification = true
end
------------------------------
-- DifficultySettingsOverlay
--
DifficultySettingsOverlay = defclass(DifficultySettingsOverlay, DifficultyOverlayBase)
DifficultySettingsOverlay.ATTRS {
desc='Adds buttons to the fort difficulty screen for saving and restoring settings.',
default_pos={x=-42, y=8},
viewscreens='dwarfmode/Settings/DIFFICULTY/CustomSettings',
default_enabled=true,
}
function DifficultySettingsOverlay:get_df_struct()
return df.global.game.main_interface.settings
end
------------------------------
-- ImportExportAutoOverlay
--
ImportExportAutoOverlay = defclass(ImportExportAutoOverlay, overlay.OverlayWidget)
ImportExportAutoOverlay.ATTRS {
default_enabled=true,
frame_style=gui.MEDIUM_FRAME,
frame_background=gui.CLEAR_PEN,
save_label=DEFAULT_NIL,
load_label=DEFAULT_NIL,
auto_label=DEFAULT_NIL,
save_fn=DEFAULT_NIL,
load_fn=DEFAULT_NIL,
has_data_fn=DEFAULT_NIL,
autostart_command=DEFAULT_NIL,
}
function ImportExportAutoOverlay:init()
self:addviews{
widgets.HotkeyLabel{
view_id='save',
frame={l=0, t=0, w=39},
key='CUSTOM_CTRL_E',
label=self.save_label,
on_activate=self:callback('do_save'),
},
widgets.Label{
view_id='save_flash',
frame={l=18, t=0},
text='Saved',
text_pen=COLOR_GREEN,
visible=false,
},
widgets.HotkeyLabel{
view_id='load',
frame={l=42, t=0, w=34},
key='CUSTOM_CTRL_I',
label=self.load_label,
on_activate=self:callback('do_load'),
enabled=self.has_data_fn,
},
widgets.Label{
view_id='load_flash',
frame={l=51, t=0},
text='Loaded',
text_pen=COLOR_GREEN,
visible=false,
},
widgets.ToggleHotkeyLabel{
view_id='auto',
frame={l=0, t=2},
key='CUSTOM_CTRL_A',
label=self.auto_label,
on_change=self:callback('do_auto'),
enabled=self.has_data_fn,
},
}
end
function ImportExportAutoOverlay:do_save()
flash(self, 'save')
self.save_fn()
end
function ImportExportAutoOverlay:do_load()
flash(self, 'load')
self.load_fn()
end
AutoMessage = defclass(AutoMessage, widgets.Window)
AutoMessage.ATTRS {
frame={w=61, h=9},
autostart_command=DEFAULT_NIL,
enabled=DEFAULT_NIL,
}
function AutoMessage:init()
self:addviews{
widgets.Label{
view_id='label',
frame={t=0, l=0},
text={
'The "', self.autostart_command, '" command', NEWLINE,
'has been ',
{text=self.enabled and 'enabled' or 'disabled', pen=self.enabled and COLOR_GREEN or COLOR_LIGHTRED},
' in the ',
{text='Automation', pen=COLOR_YELLOW}, ' -> ',
{text='Autostart', pen=COLOR_YELLOW}, ' tab of ', NEWLINE,
{text='.', gap=25},
},
},
widgets.HotkeyLabel{
frame={t=2, l=0},
label='gui/control-panel',
key='CUSTOM_CTRL_G',
auto_width=true,
on_activate=function()
self.parent_view:dismiss()
dfhack.run_script('gui/control-panel')
end,
},
widgets.HotkeyLabel{
frame={b=0, l=0, r=0},
label='Ok',
key='SELECT',
auto_width=true,
on_activate=function() self.parent_view:dismiss() end,
},
}
end
AutoMessageScreen = defclass(AutoMessageScreen, gui.ZScreenModal)
AutoMessageScreen.ATTRS {
focus_path='settings-manager/prompt',
autostart_command=DEFAULT_NIL,
enabled=DEFAULT_NIL,
}
function AutoMessageScreen:init()
self:addviews{
AutoMessage{
frame_title=(self.enabled and 'Enabled' or 'Disabled')..' auto-restore',
autostart_command=self.autostart_command,
enabled=self.enabled,
},
}
end
function ImportExportAutoOverlay:do_auto(val)
dfhack.run_script('control-panel', (val and '' or 'no') .. 'autostart', self.autostart_command)
AutoMessageScreen{autostart_command=self.autostart_command, enabled=val}:show()
end
function ImportExportAutoOverlay:onRenderFrame(dc, rect)
ImportExportAutoOverlay.super.onRenderFrame(self, dc, rect)
local enabled = control_panel.get_autostart(self.autostart_command)
self.subviews.auto:setOption(enabled)
end
------------------------------
-- StandingOrdersOverlay
--
local plotinfo = df.global.plotinfo
local li = plotinfo.labor_info
local ps = plotinfo.stockpile
local function save_standing_orders()
local standing_orders = {}
for name, val in pairs(df.global) do
if name:startswith('standing_orders_') then
standing_orders[name] = val
end
end
config.data.standing_orders = standing_orders
local chores = {}
chores.enabled = li.flags.children_do_chores
chores.labors = utils.clone(li.chores)
config.data.chores = chores
config.data.stockpile = {reserved_barrels=ps.reserved_barrels}
config:write()
end
local function load_standing_orders()
for name, val in pairs(config.data.standing_orders or {}) do
df.global[name] = val
end
li.flags.children_do_chores = not not safe_index(config.data.chores, 'enabled')
for i, val in ipairs(safe_index(config.data.chores, 'labors') or {}) do
li.chores[i-1] = val
end
local reserved_barrels = safe_index(config.data.stockpile, 'reserved_barrels')
if reserved_barrels then
ps.reserved_barrels = reserved_barrels
end
end
local function has_saved_standing_orders()
return next(config.data.standing_orders or {})
end
StandingOrdersOverlay = defclass(StandingOrdersOverlay, ImportExportAutoOverlay)
StandingOrdersOverlay.ATTRS {
desc='Adds buttons to the standing orders screen for saving and restoring settings.',
default_pos={x=6, y=-5},
viewscreens='dwarfmode/Info/LABOR/STANDING_ORDERS/AUTOMATED_WORKSHOPS',
frame={w=78, h=5},
save_label='Save standing orders (all tabs)',
load_label='Load saved standing orders',
auto_label='Apply saved settings for new embarks:',
save_fn=save_standing_orders,
load_fn=load_standing_orders,
has_data_fn=has_saved_standing_orders,
autostart_command='gui/settings-manager load-standing-orders',
}
------------------------------
-- ReservedBarrelsOverlay
--
ReservedBarrelsOverlay = defclass(ReservedBarrelsOverlay, overlay.OverlayWidget)
ReservedBarrelsOverlay.ATTRS {
desc='Exposes the setting for reserved barrels on the standing orders screen.',
default_pos={x=59, y=18},
default_enabled=true,
viewscreens='dwarfmode/Info/LABOR/STANDING_ORDERS/AUTOMATED_WORKSHOPS',
frame={w=26, h=6},
frame_style=gui.MEDIUM_FRAME,
frame_background=gui.CLEAR_PEN,
}
function ReservedBarrelsOverlay:init()
self:addviews{
widgets.Label{
frame={t=0, l=0},
text={
'Barrels reserved for use', NEWLINE,
'by workshop jobs: ',
{
text=function() return ps.reserved_barrels end,
pen=function() return ps.reserved_barrels == 0 and COLOR_YELLOW or COLOR_GREEN end,
},
},
},
widgets.HotkeyLabel{
frame={t=3, l=0},
key='CUSTOM_CTRL_B',
label='Set num reserved',
on_activate=function()
dialogs.InputBox{
frame_title='Set reserved barrels',
text={
'You can ensure a number of barrels are reserved, preventing', NEWLINE,
'them from being claimed by stockpiles as container storage.', NEWLINE,
'For example, if there are 5 reserved barrels, no stockpile', NEWLINE,
'will claim an empty barrel for storing items until you have', NEWLINE,
'at least 6 barrels lying around.', NEWLINE, NEWLINE,
'This feature is most often used to ensure that a fortress has', NEWLINE,
'ample empty barrels for the production of alcohol, although', NEWLINE,
'empty barrels are also necessary for other jobs.',
},
text_pen=COLOR_YELLOW,
label_text='Number of barrels to reserve: ',
input=tostring(ps.reserved_barrels),
on_input=function(input)
input = tonumber(input)
if not input or input < 0 then return end
ps.reserved_barrels = math.floor(input)
end,
}:show()
end,
},
}
end
------------------------------
-- WorkDetailsOverlay
--
local function clone_wd_flags(flags)
return {
cannot_be_everybody=flags.cannot_be_everybody,
no_modify=flags.no_modify,
mode=flags.mode,
}
end
local function save_work_details()
local details = {}
for idx, wd in ipairs(li.work_details) do
local detail = {
name=wd.name,
icon=wd.icon,
flags=clone_wd_flags(wd.flags),
allowed_labors=utils.clone(wd.allowed_labors),
}
details[idx+1] = detail
end
config.data.work_details = details
config:write()
end
local function load_work_details()
if not config.data.work_details or #config.data.work_details < 10 then
-- not enough data to cover built-in work details
return
end
li.work_details:resize(#config.data.work_details)
-- keep unit assignments for overwritten indices
for idx, wd in ipairs(config.data.work_details) do
local detail = {
new=df.work_detail,
name=wd.name,
icon=wd.icon,
flags=wd.flags or wd.work_detail_flags, -- compat for old name
}
li.work_details[idx-1] = detail
local al = li.work_details[idx-1].allowed_labors
for i,v in ipairs(wd.allowed_labors) do
al[i-1] = v
end
end
local scr = dfhack.gui.getDFViewscreen(true)
if dfhack.gui.matchFocusString('dwarfmode/Info/LABOR/WORK_DETAILS', scr) then
gui.simulateInput(scr, 'LEAVESCREEN')
gui.simulateInput(scr, 'D_LABOR')
end
end
local function has_saved_work_details()
return next(config.data.work_details or {})
end
WorkDetailsOverlay = defclass(WorkDetailsOverlay, ImportExportAutoOverlay)
WorkDetailsOverlay.ATTRS {
desc='Adds buttons to the work details screen for saving and restoring settings.',
default_pos={x=80, y=-5},
viewscreens='dwarfmode/Info/LABOR/WORK_DETAILS/Default',
frame={w=35, h=5},
save_label='Save work details',
load_label='Load work details',
auto_label='Load for new embarks:',
save_fn=save_work_details,
load_fn=load_work_details,
has_data_fn=has_saved_work_details,
autostart_command='gui/settings-manager load-work-details',
}
function WorkDetailsOverlay:init()
self.subviews.save.frame.w = 25
self.subviews.save_flash.frame.l = 10
self.subviews.load.frame.t = 1
self.subviews.load.frame.l = 0
self.subviews.load.frame.w = 25
self.subviews.load_flash.frame.t = 1
self.subviews.load_flash.frame.l = 10
end
OVERLAY_WIDGETS = {
embark_difficulty=DifficultyEmbarkOverlay,
embark_notification=DifficultyEmbarkNotificationOverlay,
settings_difficulty=DifficultySettingsOverlay,
standing_orders=StandingOrdersOverlay,
reserved_barrels=ReservedBarrelsOverlay,
work_details=WorkDetailsOverlay,
}
if dfhack_flags.module then
return
end
------------------------------
-- CLI processing
--
local help = false
local positionals = argparse.processArgsGetopt({...}, {
{'h', 'help', handler=function() help = true end},
})
local command = (positionals or {})[1]
if help then
print(dfhack.script_help())
return
end
local scr = dfhack.gui.getDFViewscreen(true)
local is_embark = df.viewscreen_setupdwarfgamest:is_instance(scr)
local is_fort = df.viewscreen_dwarfmodest:is_instance(scr)
if command == 'save-difficulty' then
if is_embark then save_difficulty(scr.difficulty)
elseif is_fort then
save_difficulty(df.global.game.main_interface.settings.difficulty)
else
qerror('must be on the embark preparation screen or in a loaded fort')
end
elseif command == 'load-difficulty' then
if is_embark then
load_difficulty(scr.difficulty)
show_notification = true
elseif is_fort then
load_difficulty(df.global.game.main_interface.settings.difficulty)
else
qerror('must be on the embark preparation screen or in a loaded fort')
end
elseif command == 'save-standing-orders' then
if is_fort then save_standing_orders()
else
qerror('must be in a loaded fort')
end
elseif command == 'load-standing-orders' then
if is_fort then load_standing_orders()
else
qerror('must be in a loaded fort')
end
elseif command == 'save-work-details' then
if is_fort then save_work_details()
else
qerror('must be in a loaded fort')
end
elseif command == 'load-work-details' then
if is_fort then load_work_details()
else
qerror('must be in a loaded fort')
end
else
print(dfhack.script_help())
end
return
--[[
TODO: reinstate color editor
-- An in-game init file editor
VERSION = '0.6.0'
local gui = require "gui"
local dialog = require 'gui.dialogs'
local widgets = require "gui.widgets"
local enabler = df.global.enabler
local gps = df.global.gps
-- settings-manager display settings
ui_settings = {
color = COLOR_GREEN,
highlightcolor = COLOR_LIGHTGREEN,
}
function dup_table(tbl)
-- Given {a, b, c}, returns {{a, a}, {b, b}, {c, c}}
local t = {}
for i = 1, #tbl do
table.insert(t, {tbl[i], tbl[i]})
end
return t
end
function set_variable(name, value)
local parts = name:split('.', true)
local last_field = table.remove(parts, #parts)
parent = _G
for _, field in pairs(parts) do
parent = parent[field]
end
parent[last_field] = value
end
-- Validation, used in FONT, FULLFONT, GRAPHICS_FONT, and GRAPHICS_FULLFONT
function font_exists(font)
if font ~= '' and file_exists('data/art/' .. font) then
return true
else
return false, '"' .. font .. '" does not exist'
end
end
-- Used in NICKNAME_DWARF, NICKNAME_ADVENTURE, and NICKNAME_LEGENDS
local nickname_choices = {
{'REPLACE_FIRST', 'Replace first name'},
{'CENTRALIZE', 'Display between first and last name'},
{'REPLACE_ALL', 'Replace entire name'}
}
-- Used in PRINT_MODE
local print_modes = {
{'2D', '2D (default)'}, {'2DSW', '2DSW'}, {'2DASYNC', '2DASYNC'},
{'STANDARD', 'STANDARD (OpenGL)'}, {'PROMPT', 'Prompt (STANDARD/2D)'},
{'ACCUM_BUFFER', 'ACCUM_BUFFER'}, {'FRAME_BUFFER', 'FRAME_BUFFER'}, {'VBO', 'VBO'}
}
if dfhack.getOSType() == 'linux' or dfhack.getOSType() == 'darwin' then
table.insert(print_modes, {'TEXT', 'TEXT (ncurses)'})
end
Setting descriptions
Settings listed MUST exist, but settings not listed will be ignored
Fields:
- id: "Tag name" in file (e.g. [id:params])
- type: Data type (used for entry). Valid choices:
- 'bool' - boolean - "Yes" and "No", saved as "YES" and "NO"
- 'int' - integer
- 'string'
- 'select' - string input restricted to the values given in the 'choices' field
- desc: Human-readable description
'>>' is converted to string.char(192) .. ' '
- min (optional): Minimum
Requires type 'int'
- max (optional): Maximum
Requires type 'int'
- choices: A list of valid options
Requires type 'select'
Each choice should be a table of the following format:
{ "RAW_VALUE", "Human-readable value" [, "Enum value"] }
- validate (optional): Function that recieves string as input, should return true or false
Requires type 'string'
- in_game (optional): Value to modify to change setting in-game (as a string)
For type 'select', requires 'enum' to be specified
- enum: Enum to convert string setting values to in-game (numeric) values
Uses "Enum value" specified in 'choices', or "RAW_VALUE" if not specified
Reserved field names:
- value (set to current setting value when settings are loaded)
SETTINGS = {
init = {
{id = 'SOUND', type = 'bool', desc = 'Enable sound'},
{id = 'VOLUME', type = 'int', desc = '>>Volume', min = 0, max = 255},
{id = 'INTRO', type = 'bool', desc = 'Display intro movies'},
{id = 'WINDOWED', type = 'select', desc = 'Start in windowed mode',
choices = {{'YES', 'Yes'}, {'PROMPT', 'Prompt'}, {'NO', 'No'}}
},
{id = 'WINDOWEDX', type = 'int', desc = 'Windowed X dimension (columns)', min = 80},
{id = 'WINDOWEDY', type = 'int', desc = 'Windowed Y dimension (rows)', min = 25},
{id = 'RESIZABLE', type = 'bool', desc = 'Allow resizing window'},
{id = 'FONT', type = 'string', desc = 'Font (windowed)', validate = font_exists},
{id = 'FULLSCREENX', type = 'int', desc = 'Fullscreen X dimension (columns)', min = 0},
{id = 'FULLSCREENY', type = 'int', desc = 'Fullscreen Y dimension (rows)', min = 0},
{id = 'FULLFONT', type = 'string', desc = 'Font (fullscreen)', validate = font_exists},
{id = 'BLACK_SPACE', type = 'select', desc = 'Mismatched resolution behavior',
choices = {{'YES', 'Pad with black space'}, {'NO', 'Stretch tiles'}}
},
{id = 'GRAPHICS', type = 'bool', desc = 'Enable graphics'},
{id = 'GRAPHICS_WINDOWEDX', type = 'int', desc = '>>Windowed X dimension (columns)', min = 80},
{id = 'GRAPHICS_WINDOWEDY', type = 'int', desc = '>>Windowed Y dimension (rows)', min = 25},
{id = 'GRAPHICS_FONT', type = 'string', desc = '>>Font (windowed)', validate = font_exists},
{id = 'GRAPHICS_FULLSCREENX', type = 'int', desc = '>>Fullscreen X dimension (columns)', min = 0},
{id = 'GRAPHICS_FULLSCREENY', type = 'int', desc = '>>Fullscreen Y dimension (rows)', min = 0},
{id = 'GRAPHICS_FULLFONT', type = 'string', desc = '>>Font (fullscreen)', validate = font_exists},
{id = 'PRINT_MODE', type = 'select', desc = 'Print mode', choices = print_modes},
{id = 'SINGLE_BUFFER', type = 'bool', desc = '>>Single-buffer'},
{id = 'ARB_SYNC', type = 'bool', desc = '>>Enable ARB_sync (unstable)'},
{id = 'VSYNC', type = 'bool', desc = '>>Enable vertical synchronization'},
{id = 'TEXTURE_PARAM', type = 'select', desc = '>>Texture value behavior', choices = {
{'NEAREST', 'Use nearest pixel'}, {'LINEAR', 'Average over adjacent pixels'}
}},
{id = 'TOPMOST', type = 'bool', desc = 'Make DF topmost window'},
{id = 'FPS', type = 'bool', desc = 'Show FPS indicator',
in_game = 'df.global.gps.display_frames',
in_game_type = 'int',
},
{id = 'FPS_CAP', type = 'int', desc = 'Computational FPS cap', min = 1,
in_game = 'df.global.enabler.fps', -- can't be set to 0
},
{id = 'G_FPS_CAP', type = 'int', desc = 'Graphical FPS cap', min = 1,
in_game = 'df.global.enabler.gfps',
},
{id = 'PRIORITY', type = 'select', desc = 'Process priority',
choices = dup_table({'REALTIME', 'HIGH', 'ABOVE_NORMAL', 'NORMAL', 'BELOW_NORMAL', 'IDLE'})
},
{id = 'ZOOM_SPEED', type = 'int', desc = 'Zoom speed', min = 1},
{id = 'MOUSE', type = 'bool', desc = 'Enable mouse'},
{id = 'MOUSE_PICTURE', type = 'bool', desc = '>>Use custom cursor'},
{id = 'KEY_HOLD_MS', type = 'int', desc = 'Key repeat delay (ms)'},
{id = 'KEY_REPEAT_ACCEL_LIMIT', type = 'int', desc = '>>Maximum key acceleration (multiple)', min = 1},
{id = 'KEY_REPEAT_ACCEL_START', type = 'int', desc = '>>Key acceleration delay', min = 1},
{id = 'MACRO_MS', type = 'int', desc = 'Macro instruction delay (ms)', min = 0,
in_game = 'df.global.init.input.macro_time'},
{id = 'RECENTER_INTERFACE_SHUTDOWN_MS', type = 'int', desc = 'Delay after recentering (ms)', min = 0,
in_game = 'df.global.init.input.pause_zoom_no_interface_ms'},
{id = 'COMPRESSED_SAVES', type = 'bool', desc = 'Enable compressed saves'},
},
d_init = {
{id = 'AUTOSAVE', type = 'select', desc = 'Autosave', choices = {
{'NONE', 'Disabled'}, {'SEASONAL', 'Seasonal'}, {'YEARLY', 'Yearly'}
}},
{id = 'AUTOBACKUP', type = 'bool', desc = 'Make backup copies of automatic saves'},
{id = 'AUTOSAVE_PAUSE', type = 'bool', desc = 'Pause after autosaving'},
{id = 'INITIAL_SAVE', type = 'bool', desc = 'Save after embarking'},
{id = 'EMBARK_WARNING_ALWAYS', type = 'bool', desc = 'Always prompt before embark'},
{id = 'SHOW_EMBARK_TUNNEL', type = 'select', desc = 'Local feature visibility', choices = {
{'ALWAYS', 'Always'}, {'FINDER', 'Only in site finder'}, {'NO', 'Never'}
}},
{id = 'TEMPERATURE', type = 'bool', desc = 'Enable temperature'},
{id = 'WEATHER', type = 'bool', desc = 'Enable weather'},
{id = 'INVADERS', type = 'bool', desc = 'Enable invaders'},
{id = 'CAVEINS', type = 'bool', desc = 'Enable cave-ins'},
{id = 'ARTIFACTS', type = 'bool', desc = 'Enable artifacts'},
{id = 'TESTING_ARENA', type = 'bool', desc = 'Enable object testing arena'},
{id = 'WALKING_SPREADS_SPATTER_DWF', type = 'bool', desc = 'Walking spreads spatter (fort mode)'},
{id = 'WALKING_SPREADS_SPATTER_ADV', type = 'bool', desc = 'Walking spreads spatter (adv mode)'},
{id = 'LOG_MAP_REJECTS', type = 'bool', desc = 'Log map rejects'},
{id = 'EMBARK_RECTANGLE', type = 'string', desc = 'Default embark size (x:y)', validate = function(s)
local parts = s:split(':')
if #parts == 2 then
a, b = tonumber(parts[1]), tonumber(parts[2])
if a~= nil and b ~= nil and a >= 2 and a <= 16 and b >= 2 and b <= 16 then
return true
end
else
return false, 'Must be in format "x:y"'
end
return false, 'Dimensions must be integers\nbetween 2 and 16'
end},
{id = 'IDLERS', type = 'select', desc = 'Idlers indicator (fortress mode)', choices = {
{'TOP', 'Top'}, {'BOTTOM', 'Bottom'}, {'OFF', 'Disabled'}
}},
{id = 'SET_LABOR_LISTS', type = 'select', desc = 'Automatically set labors', choices = {
{'SKILLS', 'By skill'}, {'BY_UNIT_TYPE', 'By unit type'}, {'NO', 'Disabled'}
}},
{id = 'POPULATION_CAP', type = 'int', desc = 'Population cap', min = 0,
in_game = 'df.global.d_init.population_cap'},
{id = 'STRICT_POPULATION_CAP', type = 'int', desc = 'Strict population cap',
min = 0, in_game = 'df.global.d_init.strict_population_cap'},
{id = 'VARIED_GROUND_TILES', type = 'bool', desc = 'Varied ground tiles'},
{id = 'ENGRAVINGS_START_OBSCURED', type = 'bool', desc = 'Obscure engravings by default'},
{id = 'SHOW_IMP_QUALITY', type = 'bool', desc = 'Show item quality indicators'},
{id = 'SHOW_FLOW_AMOUNTS', type = 'select', desc = 'Liquid display', choices = {
{'NO', 'Symbols (' .. string.char(247) .. ')'}, {'YES', 'Numbers (1-7)'}
}},
{id = 'SHOW_ALL_HISTORY_IN_DWARF_MODE', type = 'bool', desc = 'Show all history (fortress mode)'},
{id = 'DISPLAY_LENGTH', type = 'int', desc = 'Announcement display length (adv mode)', min = 1,
in_game = 'df.global.d_init.display_length'},
{id = 'MORE', type = 'bool', desc = '>>"More" indicator',
in_game = 'df.global.d_init.flags2.MORE'},
{id = 'ADVENTURER_TRAPS', type = 'bool', desc = 'Enable traps in adventure mode'},
{id = 'ADVENTURER_ALWAYS_CENTER', type = 'bool', desc = 'Center screen on adventurer'},
{id = 'NICKNAME_DWARF', type = 'select', desc = 'Nickname behavior (fortress mode)', choices = nickname_choices},
{id = 'NICKNAME_ADVENTURE', type = 'select', desc = 'Nickname behavior (adventure mode)', choices = nickname_choices},
{id = 'NICKNAME_LEGENDS', type = 'select', desc = 'Nickname behavior (legends mode)', choices = nickname_choices},
},
colors = {
-- populated below
}
}
COLORS = {
{id = 'BLACK', name = 'Black'},
{id = 'BLUE', name = 'Dark Blue'},
{id = 'GREEN', name = 'Dark Green'},
{id = 'CYAN', name = 'Dark Cyan'},
{id = 'RED', name = 'Dark Red'},
{id = 'MAGENTA', name = 'Dark Magenta'},
{id = 'BROWN', name = 'Brown'},
{id = 'LGRAY', name = 'Light Gray'},
{id = 'DGRAY', name = 'Dark Gray'},
{id = 'LBLUE', name = 'Light Blue'},
{id = 'LGREEN', name = 'Light Green'},
{id = 'LCYAN', name = 'Light Cyan'},
{id = 'LRED', name = 'Light Red'},
{id = 'LMAGENTA', name = 'Light Magenta'},
{id = 'YELLOW', name = 'Yellow'},
{id = 'WHITE', name = 'White'},
}
for k, v in pairs(COLORS) do
v.num_id = k - 1
for _, component in pairs({'R', 'G', 'B'}) do
table.insert(SETTINGS.colors, {
id = v.id .. '_' .. component,
type = 'int',
min = 0,
max = 255,
desc = v.name .. ' - ' .. component
})
end
end
function file_exists(path)
local f = io.open(path, "r")
if f ~= nil then io.close(f) return true
else return false
end
end
function settings_load()
for file, settings in pairs(SETTINGS) do
local f = io.open('data/init/' .. file .. '.txt')
local contents = f:read('*all')
for i, s in pairs(settings) do
local a, b = contents:find('[' .. s.id .. ':', 1, true)
if a ~= nil then
s.value = contents:sub(b + 1, contents:find(']', b, true) - 1)
else
return false, 'Could not find "' .. s.id .. '" in ' .. file .. '.txt'
end
end
f:close()
end
return true
end
function settings_save()
for file, settings in pairs(SETTINGS) do
local path = 'data/init/' .. file .. '.txt'
local f = io.open(path, 'r')
local contents = f:read('*all')
for i, s in pairs(settings) do
local a, b = contents:find('[' .. s.id .. ':', 1, true)
if a ~= nil then
local e = contents:find(']', b, true)
contents = contents:sub(1, b) .. s.value .. contents:sub(e)
else
return false, 'Could not find ' .. s.id .. ' in ' .. file .. '.txt'
end
end
f:close()
f = io.open(path, 'w')
f:write(contents)
f:close()
end
print('Saved settings')
end
function dialog.showValidationError(str)
dialog.showMessage('Error', str, COLOR_LIGHTRED)
end
settings_manager = defclass(settings_manager, gui.FramedScreen)
settings_manager.focus_path = 'settings_manager'
function settings_manager:reset()
self.frame_title = "Settings"
self.file = nil
end
function settings_manager:init()
self:reset()
local file_list = widgets.List{
choices = {"init.txt", "d_init.txt", "colors.txt"},
text_pen = {fg = ui_settings.color},
cursor_pen = {fg = ui_settings.highlightcolor},
on_submit = self:callback("select_file"),
frame = {l = 1, t = 3},
view_id = "file_list",
}
local file_page = widgets.Panel{
subviews = {
widgets.Label{
text = 'File:',
frame = {l = 1, t = 1},
},
file_list,
widgets.Label{
text = {
{key = 'LEAVESCREEN', text = ': Back'}
},
frame = {l = 1, t = 4 + #file_list.choices},
},
widgets.Label{
text = 'settings-manager v' .. VERSION,
frame = {l = 1, b = 0},
text_pen = {fg = COLOR_GREY},
},
},
}
local settings_list = widgets.List{
choices = {},
text_pen = {fg = ui_settings.color},