-
Notifications
You must be signed in to change notification settings - Fork 5
/
buildings.lua
2272 lines (1824 loc) · 77 KB
/
buildings.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
-- Most of the functions below operate on the currently selected building only,
-- even if they accept a [unused] building id argument.
-- Building interactions usually start with building_query_selected, which
-- will try to transition to [q]uery mode and select the passed building.
--todo: They may also say "Leave me. I need... things... certain things", in which case they want special items such as skulls or vermin remains.
local mood_items = { --as:string[][]
[df.item_type.WOOD] = { 'logs', 'a forest', 'tree... life' }, --'wood logs'?
[df.item_type.CLOTH] = { 'cloth', 'stacked cloth', 'cloth... thread' },
[df.item_type.SMALLGEM] = { 'cut gems', 'cut gems', 'gems... shining' },
[df.item_type.BAR] = { 'metal bars', 'shining bars of metal', 'bars... metal' },
[df.item_type.SKIN_TANNED] = { 'tanned hides', 'stacked leather', 'leather... skin' },
[df.item_type.BLOCKS] = { 'rock blocks', 'square blocks', 'blocks... bricks' },
[df.item_type.BOULDER] = { 'rock', 'a quarry', 'stone... rock' },
[df.item_type.ROUGH] = { 'rough gems', 'rough gems', 'rough... color' },
['bone'] = { 'bones', 'skeletons', 'bones... yes' },
['shell'] = { 'shells', 'shells', 'a shell...' },
['skull'] = { 'body parts', 'death', 'a corpse', 'things... certain things' },
}
function building_workshop_get_mood(bld)
local job = (#bld.jobs > 0) and bld.jobs[0]
if not job or df.job_type[job.job_type]:sub(1,#'StrangeMood') ~= 'StrangeMood' then
return nil
end
local unitid = dfhack.job.getGeneralRef(job, df.general_ref_type.UNIT_WORKER).unit_id --hint:df.general_ref_unit_workerst
local unit = df.unit.find(unitid)
if not unit then
return nil
end
local uname = unitname(unit)
local prof = unitprof(unit)
local demands = {}
if unit.mood ~= df.mood_type.Fell then
for i,ji in ipairs(job.job_items) do
local mi = nil
if ji.item_type == -1 then
if ji.flags2.bone then
mi = mood_items['bone']
elseif ji.flags2.shell then
mi = mood_items['shell']
elseif ji.flags2.body_part then
mi = mood_items['skull']
end
else
mi = mood_items[ji.item_type]
end
if not mi then
table.insert(demands, '???') --todo: !!
goto next
end
local demand = mi[unit.mood+1] or mi[1]
if #demands > 0 and demands[#demands] == demand then --todo: really want this?
goto next
end
table.insert(demands, demand)
::next::
end
end
local artname = dfhack.TranslateName(unit.status.artifact_name) or ''
return { uname, prof, unit.mood, demands, dfhack.df2utf(artname), job.flags.working }
end
local bait_types = { df.item_type.MEAT, df.item_type.FISH, df.item_type.GEM }
local function bait_idx(t)
for i,v in ipairs(bait_types) do
if v == t then
return i
end
end
return 0
end
local function get_squads_use(bld)
local squads = {}
local eid = df.global.ui.main.fortress_entity.id
for i,squad in ipairs(df.global.world.squads.all) do
if squad.entity_id == eid then
local name = squadname(squad)
-- find if squad uses this room
local roomuse = utils.binsearch(squad.rooms, bld.id, 'building_id')
local mode = roomuse and roomuse.mode.whole or 0
table.insert(squads, { name, squad.id, mode })
end
end
return squads
end
local function building_is_machine(btype)
return btype == df.building_type.Windmill or btype == df.building_type.WaterWheel or btype == df.building_type.AxleVertical
or btype == df.building_type.AxleHorizontal or btype == df.building_type.GearAssembly or btype == df.building_type.Rollers
or btype == df.building_type.ScrewPump
end
local function building_can_be_room(btype)
return btype == df.building_type.Chair or btype == df.building_type.Table or btype == df.building_type.Statue
or btype == df.building_type.Bed or btype == df.building_type.Box or btype == df.building_type.Cabinet
or btype == df.building_type.Armorstand or btype == df.building_type.Weaponrack or btype == df.building_type.ArcheryTarget
or btype == df.building_type.Coffin or btype == df.building_type.Slab or btype == df.building_type.Cage
or btype == df.building_type.Chain or btype == df.building_type.Well or btype == df.building_type.DisplayFurniture
end
local function building_can_assign_location(btype)
return btype == df.building_type.Table or btype == df.building_type.Bed or btype == df.building_type.DisplayFurniture
or btype == df.building_type.Statue
end
local function building_is_workshop(btype, bsub)
return (btype == df.building_type.Workshop or btype == df.building_type.Furnace
or (btype == df.building_type.Trap and (bsub == df.trap_type.Lever or bsub == df.trap_type.PressurePlate)))
end
local function bed_is_in_tavern(bld)
local loc = bld.location_id ~= -1 and location_find_by_id(bld.location_id)
return loc and loc._type == df.abstract_building_inn_tavernst
end
function query__specific_info(bld)
local btype = bld:getType()
if btype == df.building_type.TradeDepot then
local bld = bld --as:df.building_tradedepotst
--todo: return broker name, current job, accessibility
local caravan_state = #df.global.ui.caravans > 0 and df.global.ui.caravans[0].trade_state or 0
local can_trade = depot_can_trade(bld)
local can_movegoods = depot_can_movegoods(bld)
local avail_flags = packbits(can_movegoods, can_trade)
return { bld.trade_flags.whole, avail_flags }
end
if btype == df.building_type.Door or btype == df.building_type.Hatch then
local bld = bld --as:df.building_doorst
return { bld.door_flags.whole }
end
-- machine
if building_is_machine(btype) then
local machine = df.machine.find(bld.machine.machine_id)
local ttype = dfhack.maps.getTileType(bld.centerx, bld.centery, bld.z)
local stable_foundation = (ttype ~= df.tiletype.OpenSpace)
local ret = {
machine and machine.cur_power or -1, machine and machine.min_power or -1, machine and machine.flags.whole or 0,
stable_foundation
}
if btype == df.building_type.Rollers or btype == df.building_type.ScrewPump then
local bld = bld --as:df.building_rollersst
table.insert(ret, bld.direction)
end
if btype == df.building_type.ScrewPump then
local bld = bld --as:df.building_screw_pumpst
table.insert(ret, bld.pump_manually)
end
return ret
end
-- workshops, furnaces, and also levers and pressure plates
if building_is_workshop(btype, bld:getSubtype()) then
local jobs = {}
for i,job in ipairs(bld.jobs) do
local title = jobname(job)
--todo: first check that job type supports setting details
local can_set_details = (#job.items == 0)
--todo: game actually shows 'A' not based on flags but rather on presence of general_ref_unit_workerst ref
local moreflags = packbits(can_set_details)
table.insert(jobs, { title, job.flags.whole, moreflags })
end
local moodinfo = building_workshop_get_mood(bld)
local workshop_type = -1
--todo: --fixme: should pass type for furnaces as well, but app currently doesn't check building type
-- when comparing subtype, thus treating magma smelters and jeweler's workshops
if btype == df.building_type.Trap then --as:bld=df.building_trapst
workshop_type = bld.trap_type
elseif btype == df.building_type.Workshop then --as:bld=df.building_workshopst
workshop_type = bld.type
end
local profile_info
if have_noble('MANAGER') then
local min = bld.profile.min_level
local max = bld.profile.max_level
if min == 3000 then
min = 15
end
if max == 3000 then
max = 15
end
profile_info = { #bld.profile.permitted_workers, min, max }
end
local clt = (btype == df.building_type.Workshop or btype == df.building_type.Furnace) and bld:getClutterLevel() or 0
local num_items = (btype == df.building_type.Workshop or btype == df.building_type.Furnace) and #bld.contained_items or 0 --hint:df.building_actual
--TODO: how to determine this properly?
local millstone_needs_power = (workshop_type == df.workshop_type.Millstone) and #building_workshop_get_jobchoices(bld.id) == 0 or false
return { workshop_type, jobs, moodinfo or mp.NIL, profile_info or mp.NIL, clt, num_items, millstone_needs_power }
end
if btype == df.building_type.Table then
local bld = bld --as:df.building_tablest
return { bld.table_flags.meeting_hall } --todo: pass all table_flags.whole
end
if btype == df.building_type.Bed then
local bld = bld --as:df.building_bedst
return { bld.bed_flags.whole, bed_is_in_tavern(bld) }
end
if btype == df.building_type.ArcheryTarget then
local bld = bld --as:df.building_archerytargetst
return { bld.archery_direction }
end
if btype == df.building_type.Coffin then
local bld = bld --as:df.building_coffinst
local owner = bld.owner
local buried = owner and owner.flags1.inactive or false
local mode = bld.burial_mode
local flags = packbits(mode.allow_burial, not mode.no_citizens, not mode.no_pets, buried)
return { flags }
end
if btype == df.building_type.Slab then
local bld = bld --as:df.building_slabst
local slabitem = bld.contained_items[0].item --as:df.item_slabst
local inmemory = slabitem.engraving_type == df.slab_engraving_type.Memorial and slabitem.topic and hfname(df.historical_figure.find(slabitem.topic)) or mp.NIL
--todo: show full description and not just the name
return { inmemory }
end
if btype == df.building_type.Cage then
local bld = bld --as:df.building_cagest
local occupants = {}
for i,v in ipairs(bld.contained_items[0].item.general_refs) do
if v._type == df.general_ref_contains_unitst then
local unit = df.unit.find(v.unit_id) --hint:df.general_ref_contains_unitst
if unit then
local title = unit_fulltitle(unit)
table.insert(occupants, { title, unit.id, 0 })
end
elseif v._type == df.general_ref_contains_itemst then
local item = df.item.find(v.item_id) --hint:df.general_ref_contains_itemst
if item then
local title = itemname(item, 0, true)
table.insert(occupants, { title, item.id, 1 })
end
end
end
return { occupants }
end
if btype == df.building_type.Chain then
local bld = bld --as:df.building_chainst
local assigned = bld.assigned and unit_fulltitle(bld.assigned) or mp.NIL
local chained = bld.chained and unit_fulltitle(bld.chained) or mp.NIL
return {
bld.flags.justice,
assigned, bld.assigned and bld.assigned.id or -1,
chained, bld.chained and bld.chained.id or -1,
}
end
if btype == df.building_type.Well then
local bld = bld --as:df.building_wellst
local is_active = building_well_is_active()
-- find bucket and check if liquid amount is 10
local liquid = 0
for j,w in ipairs(bld.contained_items) do
if w.item._type == df.item_bucketst then
for i,v in ipairs(w.item.general_refs) do
if v._type == df.general_ref_contains_itemst then
local item = df.item.find(v.item_id) --hint:df.general_ref_item
if item and item._type == df.item_liquid_miscst then
liquid = liquid + item.stack_size --hint:df.item_liquid_miscst
end
end
end
break
end
end
local is_full = liquid == 10
local flags = packbits(is_active, is_full)
return { flags }
end
if btype == df.building_type.DisplayFurniture then --dfver:4412-
local dispitems = {}
for i,v in ipairs(bld.displayed_items) do --hint:df.building_display_furniturest
local item = df.item.find(v)
local iname = item and itemname(item, 0, false) or '#unknown item#'
table.insert(dispitems, { iname, v })
end
return { dispitems }
end
if btype == df.building_type.FarmPlot then
local bld = bld --as:df.building_farmplotst
local keys = { K'BUILDJOB_FARM_SPRING', K'BUILDJOB_FARM_SUMMER', K'BUILDJOB_FARM_AUTUMN', K'BUILDJOB_FARM_WINTER' }
local ws = dfhack.gui.getCurViewscreen(true)
local crops = {}
for i,sk in ipairs(keys) do
gui.simulateInput(ws, sk)
local seascrops = { { 'Fallow', 9999, true } }
for i,plantid in ipairs(df.global.ui.selected_farm_crops) do
local name = df.global.world.raws.plants.all[plantid].name_plural:gsub("^%l", string.upper)
local has_seeds = df.global.ui.available_seeds[i]
table.insert(seascrops, { name, plantid, has_seeds })
end
table.insert(crops, seascrops)
end
local seasons = {}
for i,plantid in ipairs(bld.plant_id) do
local name = (plantid ~= -1) and df.global.world.raws.plants.all[plantid].name_plural:gsub("^%l", string.upper) or 'Fallow'
table.insert(seasons, name)
end
local fert = (#bld.jobs > 0 and bld.jobs[0].job_type == df.job_type.FertilizeField)
local fertinfo = { fert, bld.current_fertilization, bld.max_fertilization, bld.material_amount }
return { seasons, crops, fertinfo, bld.farm_flags.seasonal_fertilize }
end
if btype == df.building_type.Stockpile then
local bld = bld --as:df.building_stockpilest
local area = (bld.x2-bld.x1+1)*(bld.y2-bld.y1+1)
local give = {}
local take = {}
local links = { give, take }
for i,v in ipairs(bld.links.give_to_pile) do
table.insert(give, { bldname(v), v.id })
end
for i,v in ipairs(bld.links.take_from_pile) do
table.insert(take, { bldname(v), v.id })
end
for i,v in ipairs(bld.links.give_to_workshop) do
table.insert(give, { bldname(v), v.id })
end
for i,v in ipairs(bld.links.take_from_workshop) do
table.insert(take, { bldname(v), v.id })
end
return { area, bld.max_barrels, bld.max_bins, bld.max_wheelbarrows, links, bld.use_links_only }
end
if btype == df.building_type.SiegeEngine then
local bld = bld --as:df.building_siegeenginest
return { bld.type, bld.action, bld.facing }
end
if btype == df.building_type.NestBox then
local bld = bld --as:df.building_nest_boxst
local unit = bld.claimed_by ~= -1 and df.unit.find(bld.claimed_by)
local unitname = unit and unit_fulltitle(unit) or mp.NIL
return { unitname, unit and unit.id or -1 }
end
if btype == df.building_type.Hive then
local bld = bld --as:df.building_hivest
local hiveflags = bld.hive_flags.whole
local has_access = building_hive_has_access()
return { hiveflags, has_access }
end
if btype == df.building_type.AnimalTrap then
local bld = bld --as:df.building_animaltrapst
local baitidx = bait_idx(bld.bait_type)
local bait = #bld.contained_items > 1 and bld.contained_items[1].item --todo: is this correct?
local itemname = bait and itemname(bait, 0, true) or mp.NIL
local caught = bait and bait._type == df.item_verminst
return { baitidx, itemname, caught }
end
return nil
end
function query__room_info(bld)
local btype = bld:getType()
if bld.is_room then
local owner = bld.owner
local ownername = owner and unit_fulltitle(owner) or ''
if btype ~= df.building_type.Coffin and owner and owner.relationship_ids[df.unit_relationship_type.Spouse] ~= -1 then
local owner2 = df.unit.find(owner.relationship_ids[df.unit_relationship_type.Spouse])
if owner2 then
ownername = ownername .. ' & ' .. unit_fulltitle(owner2)
end
end
-- normally rooms can be assigned and the owner is displayed
local can_assign = true
local show_owner = true
local show_squads = false
if btype == df.building_type.Bed then
local bld = bld --as:df.building_bedst
-- owner is shown when it's not barracks or dormitory
show_owner = not bld.bed_flags.barracks and not bld.bed_flags.dormitory
show_squads = bld.bed_flags.barracks
-- can assign if not in a tavern
can_assign = not bed_is_in_tavern(bld)
elseif btype == df.building_type.ArcheryTarget then
-- archery targets have no owners
can_assign = false
show_owner = false
show_squads = true
elseif btype == df.building_type.Box or btype == df.building_type.Cabinet or btype == df.building_type.Armorstand or btype == df.building_type.Weaponrack then
-- owner is shown when it's set, otherwise squads are shown
show_owner = #ownername > 0
show_squads = not show_owner
elseif btype == df.building_type.Cage then
-- owner is shown when it's set
show_owner = #ownername > 0
elseif btype == df.building_type.Chain then
-- chain can have owner when it's not used by justice
show_owner = not bld.flags.justice and #ownername > 0
can_assign = not bld.flags.justice
elseif btype == df.building_type.Well then
-- wells have no owners
can_assign = false
show_owner = false
end
local flags = packbits(can_assign, show_owner, show_squads, building_can_assign_location(btype))
local loc = bld.location_id ~= -1 and location_find_by_id(bld.location_id)
local lname = loc and (locname(loc) or '#unknown location#') or mp.NIL
--todo: pass owners as an array and include unit ids
return {
flags, ownername,
show_squads and get_squads_use(bld) or mp.NIL,
lname, bld.location_id,
}
end
return nil
end
--luacheck: in=number
function building_query_selected(bldid)
local ws = dfhack.gui.getCurViewscreen(true)
if ws._type ~= df.viewscreen_dwarfmodest then
error(errmsg_wrongscreen(ws))
end
local bld
if bldid and bldid ~= -1 then
bld = df.building.find(bldid)
else
bld = df.global.world.selected_building
end
if not bld then
error('no building with id '..tostring(bldid))
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.QueryBuilding or df.global.world.selected_building ~= bld then
df.global.ui.main.mode = df.ui_sidebar_mode.QueryBuilding
building_focus(bld)
end
--todo: this is temporary for backward compatibility with old apps that don't call building_stockpile_edit_settings
stockpile_editing_settings = nil
display_items_cache = nil
local btype = bld:getType()
local bsub = bld:getSubtype()
local removing = (#bld.jobs > 0 and bld.jobs[0].job_type == df.job_type.DestroyBuilding)
local actual = df.building_actual:is_instance(bld)
local forbidden = actual and #bld.contained_items > 0 and bld.contained_items[0].item.flags.forbid --hint:df.building_actual
local curstage = bld:getBuildStage()
local maxstage = bld:getMaxBuildStage()
local constructed = (curstage == maxstage)
local workshop_like = building_is_workshop(btype, bsub)
local can_be_room = building_can_be_room(btype)
local is_machine = building_is_machine(btype)
local genflags = packbits(removing, forbidden, actual, constructed, workshop_like, can_be_room, is_machine)
if not constructed then
local needsarchitect = (bld:needsDesign() and bld.design and not bld.design.flags.designed) --hint:df.building_actual
--todo: how can there be no construction job (found in logs)?
local cjob = #bld.jobs > 0 and bld.jobs[0]
local active = cjob and (cjob.flags.fetching or cjob.flags.bringing or cjob.flags.working)
local suspended = cjob and cjob.flags.suspend
--todo: 'Construction initiated.' - when?
local stagename
if needsarchitect then
stagename = 'Waiting for architect...'
elseif curstage == 0 then
stagename = 'Waiting for construction...'
elseif curstage == 1 then
stagename = 'Partially constructed.'
elseif curstage == 2 then
stagename = 'Construction nearly done.'
else
stagename = 'Constructing...' --xxx: just not to leave it empty in case there are other values
end
return {
bldname(bld), bld.id, btype, genflags, bldname(bld, true),
stagename, active, suspended
}
end
return {
bldname(bld), bld.id, btype, genflags, bldname(bld, true),
query__room_info(bld) or mp.NIL, query__specific_info(bld) or mp.NIL,
}
end
--luacheck: in=number,number,bool
function building_workshop_set_repeat(bldid, idx, value)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
return
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.QueryBuilding or df.global.world.selected_building == nil then
return
end
local bld = df.global.world.selected_building
bld.jobs[idx].flags['repeat'] = istrue(value)
return true
end
--luacheck: in=number,number,bool
function building_workshop_set_suspend(bldid, idx, value)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
return
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.QueryBuilding or df.global.world.selected_building == nil then
return
end
local bld = df.global.world.selected_building
if bld.jobs[idx].flags['suspend'] ~= istrue(value) then
df.global.ui_workshop_job_cursor = idx
gui.simulateInput(ws, K'BUILDJOB_SUSPEND')
end
return true
end
--luacheck: in=number,number,bool
function building_workshop_set_do_now(bldid, idx, value)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
return
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.QueryBuilding or df.global.world.selected_building == nil then
return
end
local bld = df.global.world.selected_building
if bld.jobs[idx].flags['do_now'] ~= istrue(value) then
df.global.ui_workshop_job_cursor = idx
gui.simulateInput(ws, K'BUILDJOB_NOW')
end
return true
end
--luacheck: in=number,number
function building_workshop_cancel(bldid, idx)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
return
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.QueryBuilding or df.global.world.selected_building == nil then
return
end
local bld = df.global.world.selected_building
df.global.ui_workshop_job_cursor = idx
gui.simulateInput(ws, K'BUILDJOB_CANCEL')
return true
end
--luacheck: in=number,number,number
function building_workshop_reorder(bldid, fromidx, toidx)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
return
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.QueryBuilding or df.global.world.selected_building == nil then
return
end
local bld = df.global.world.selected_building
local j = bld.jobs[fromidx]
bld.jobs:erase(fromidx)
bld.jobs:insert(toidx, j)
return true
end
function clone_job_button(orig)
local btn = df.interface_button_building_new_jobst:new()
--btn.building=bld
btn.job_type = orig.job_type
btn.reaction_name=orig.reaction_name
btn.item_type = orig.item_type
btn.item_subtype=orig.item_subtype
btn.mat_type=orig.mat_type
btn.mat_index=orig.mat_index
btn.item_category.whole=orig.item_category.whole
btn.material_category.whole=orig.material_category.whole
btn.hist_figure_id=orig.hist_figure_id
return btn
end
local jobchoices = {}
--todo: refactor this to have two separate paths for interface_button_building_new_jobst and subcategories
function get_job_choices(ws, level)
local ret = {}
for i,btn in ipairs(df.global.ui_sidebar_menus.workshop_job.choices_visible) do
local btn = btn --as:df.interface_button_building_new_jobst
local unavailable = false
local memorialized = false
local slab = false
if btn._type == df.interface_button_building_new_jobst then
unavailable = btn.is_custom
table.insert(jobchoices, clone_job_button(btn))
end
local title = utils.call_with_string(btn, 'getLabel')
if btn._type == df.interface_button_building_new_jobst and btn.job_type == df.job_type.EngraveSlab then
slab = true
title = title:gsub(' %(Engrave Memorial%)', '')
local hfid = btn.hist_figure_id
for j,w in ipairs(df.global.world.buildings.other.SLAB) do --as:df.building_slabst
if #w.contained_items > 0 and w.contained_items[0].item.topic == hfid then --hint:df.item_slabst
memorialized = true
break
end
end
end
local key = dfhack.screen.getKeyDisplay(btn.hotkey_id)
if key == '?' then
key = ''
end
local descr = mp.NIL
local subchoices = mp.NIL
if btn._type == df.interface_button_buildingst or
btn._type == df.interface_button_building_material_selectorst or
btn._type == df.interface_button_building_category_selectorst or
btn._type == df.interface_button_building_custom_category_selectorst then
if level == 0 then
df.global.ui_workshop_in_add = true
ws:logic() --to initialize / switch to add job menu
end
btn:click()
--gui.simulateInput(ws, btn.hotkey_id)
ws:logic()
subchoices = get_job_choices(ws, level+1)
if level > 0 then
gui.simulateInput(ws, K'LEAVESCREEN')
else
gui.simulateInput(ws, K'CURSOR_DOWN_Z')
gui.simulateInput(ws, K'CURSOR_UP_Z')
end
else
if btn.job_type == df.job_type.CustomReaction and btn.reaction_name:sub(1,5) == 'MAKE_' then
local rid = btn.reaction_name:sub(6)
local found = false
for i,v in ipairs(df.global.world.raws.itemdefs.instruments) do
if v.id == rid then
descr = dfhack.df2utf(v.description:gsub('%s+', ' '))
found = true
break
end
end
if not found then
for i,v in ipairs(df.global.world.raws.itemdefs.tools) do
if v.id == rid then
descr = dfhack.df2utf(v.description:gsub('%s+', ' '))
found = true
break
end
end
end
end
end
local flags = packbits(unavailable, slab, memorialized)
table.insert(ret, { dfhack.df2utf(title), key, subchoices, #jobchoices, flags, descr })
::continue::
end
return ret
end
--todo: don't hardcode this; should find all glasses in df.global.world.raws.mat_table.builtin
local glasses = { 'green glass', 'clear glass', 'crystal glass' }
--todo: bldid is not used at the moment
--luacheck: in=number
function building_workshop_get_jobchoices(bldid)
local ws = dfhack.gui.getCurViewscreen() --as:df.viewscreen_dwarfmodest
if ws._type ~= df.viewscreen_dwarfmodest then
error(errmsg_wrongscreen(ws))
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.QueryBuilding or df.global.world.selected_building == nil then
error('no selected building')
end
local ret = {}
local bld = df.global.world.selected_building
if bld._type == df.building_trapst and (bld.trap_type == df.trap_type.Lever or bld.trap_type == df.trap_type.PressurePlate) then --hint:df.building_trapst
local bld = bld --as:df.building_trapst
if bld.trap_type == df.trap_type.Lever then
table.insert(ret, { 'Pull the Lever', 'P', mp.NIL, 0, 0 })
end
table.insert(ret, { 'Link up a Bridge', 'b', mp.NIL, 1, 0 })
table.insert(ret, { 'Link up a Cage', 'j', mp.NIL, 2, 0 })
table.insert(ret, { 'Link up a Chain', 'c', mp.NIL, 3, 0 })
table.insert(ret, { 'Link up a Door', 'd', mp.NIL, 4, 0 })
table.insert(ret, { 'Link up a Floodgate', 'f', mp.NIL, 5, 0 })
table.insert(ret, { 'Link up a Hatch', 'h', mp.NIL, 6, 0 })
table.insert(ret, { 'Link up a Wall Grate', 'w', mp.NIL, 7, 0 })
table.insert(ret, { 'Link up a Floor Grate', 'g', mp.NIL, 8, 0 })
table.insert(ret, { 'Link up Vertical Bars', 'B', mp.NIL, 9, 0 })
table.insert(ret, { 'Link up a Floor Bars', 'Alt+b', mp.NIL, 10, 0 })
table.insert(ret, { 'Link up a Support', 's', mp.NIL, 11, 0 })
table.insert(ret, { 'Link up Spears / Spikes', 'S', mp.NIL, 12, 0 })
table.insert(ret, { 'Link up a Gear Assembly', 'a', mp.NIL, 13, 0 })
table.insert(ret, { 'Link up a Track Stop', 'T', mp.NIL, 14, 0 })
elseif bld._type == df.building_workshopst then
local bld = bld --as:df.building_workshopst
local wtype = bld.type
if wtype == df.workshop_type.Mechanics then
table.insert(ret, { 'Make Rock Mechanisms', 't', mp.NIL, 0, 0 })
table.insert(ret, { 'Make Traction Bench', 'R', mp.NIL, 1, 0 })
elseif wtype == df.workshop_type.Butchers then
table.insert(ret, { 'Butcher a dead animal', 'b', mp.NIL, 0, 0 })
table.insert(ret, { 'Extract from a dead animal', 'e', mp.NIL, 1, 0 })
table.insert(ret, { 'Capture a live land animal', 'a', mp.NIL, 2, 0 })
elseif wtype == df.workshop_type.Fishery then
table.insert(ret, { 'Process a Raw Fish', 'p', mp.NIL, 0, 0 })
table.insert(ret, { 'Extract from a Raw Fish', 'e', mp.NIL, 1, 0 })
table.insert(ret, { 'Capture a Live Fish', 'f', mp.NIL, 2, 0 })
elseif wtype == df.workshop_type.Loom then
table.insert(ret, { 'Collect Webs', 'c', mp.NIL, 0, 0 })
table.insert(ret, { 'Weave Cloth (Plant Thread)', 'w', mp.NIL, 1, 0 })
table.insert(ret, { 'Weave Silk Cloth', 's', mp.NIL, 2, 0 })
table.insert(ret, { 'Weave Cloth (Wool/Hair Yarn)', 'y', mp.NIL, 3, 0 })
table.insert(ret, { 'Weave Metal Cloth', 'a', mp.NIL, 4, 0 })
elseif wtype == df.workshop_type.Kennels then
table.insert(ret, { 'Capture a Live Land Animal', 'a', mp.NIL, 0, 0 })
table.insert(ret, { 'Tame a Small Animal', 't', mp.NIL, 1, 0 })
elseif wtype == df.workshop_type.Dyers then
table.insert(ret, { 'Dye Thread', 't', mp.NIL, 0, 0 })
table.insert(ret, { 'Dye Cloth', 'c', mp.NIL, 1, 0 })
elseif wtype == df.workshop_type.Jewelers then
for i=0,#ws.jeweler_cutgem-1 do
local matname
local cut = ws.jeweler_cutgem[i] > 0
local enc = ws.jeweler_encrust[i] > 0
if cut or enc then
if i < #df.global.world.raws.inorganics then
local matinfo = dfhack.matinfo.decode(0,i)
matname = matinfo and matinfo.material.state_name[0] or '#unknown material#'
elseif i - #df.global.world.raws.inorganics < 3 then
matname = glasses[i - #df.global.world.raws.inorganics + 1]
else
matname = '#unknown material#'
end
end
local opts = {}
if cut then
table.insert(opts, { 'Cut '..matname, '', mp.NIL, #ret*4+0, 0 })
end
if enc then
table.insert(opts, { 'Encrust Finished Goods with '..matname, '', mp.NIL, #ret*4+1, 0 })
table.insert(opts, { 'Encrust Furniture with '..matname, '', mp.NIL, #ret*4+2, 0 })
table.insert(opts, { 'Encrust Ammo with '..matname, '', mp.NIL, #ret*4+3, 0 })
end
if #opts > 0 then
table.insert(ret, { matname, '', opts, (cut and 1 or 0) + (enc and 2 or 0), 0 })
end
end
else
jobchoices = {}
ret = get_job_choices(ws, 0)
end
else
jobchoices = {}
ret = get_job_choices(ws, 0)
end
return ret
end
local jobs_mechanics = { K'HOTKEY_MECHANIC_PARTS', K'HOTKEY_MECHANIC_TRACTION_BENCH' }
local jobs_butchers = { K'HOTKEY_BUTCHER_BUTCHER', K'HOTKEY_BUTCHER_EXTRACT', K'HOTKEY_BUTCHER_CATCH' }
local jobs_fishery = { K'HOTKEY_FISHERY_PROCESS', K'HOTKEY_FISHERY_EXTRACT', K'HOTKEY_FISHERY_CATCH' }
local jobs_loom = { K'HOTKEY_LOOM_COLLECT_SILK', K'HOTKEY_LOOM_WEAVE_CLOTH', K'HOTKEY_LOOM_WEAVE_SILK', K'HOTKEY_LOOM_WEAVE_YARN', K'HOTKEY_LOOM_WEAVE_METAL' }
local jobs_kennels = { K'HOTKEY_KENNEL_CATCH_VERMIN', K'HOTKEY_KENNEL_TAME_VERMIN' }
local jobs_dyers = { K'HOTKEY_DYER_THREAD', K'HOTKEY_DYER_CLOTH' }
local jobs_trap = { K'HOTKEY_TRAP_PULL_LEVER', K'HOTKEY_TRAP_BRIDGE', K'HOTKEY_TRAP_CAGE', K'HOTKEY_TRAP_CHAIN', K'HOTKEY_TRAP_DOOR', K'HOTKEY_TRAP_FLOODGATE', K'HOTKEY_TRAP_HATCH', K'HOTKEY_TRAP_GRATE_WALL', K'HOTKEY_TRAP_GRATE_FLOOR', K'HOTKEY_TRAP_BARS_VERTICAL', K'HOTKEY_TRAP_BARS_FLOOR', K'HOTKEY_TRAP_SUPPORT', K'HOTKEY_TRAP_SPIKE', K'HOTKEY_TRAP_GEAR_ASSEMBLY', K'HOTKEY_TRAP_TRACK_STOP' }
--luacheck: in=number,number,bool
function building_workshop_addjob(bldid, idx, rep)
local ws = dfhack.gui.getCurViewscreen()
if ws._type ~= df.viewscreen_dwarfmodest then
error(errmsg_wrongscreen(ws))
end
if df.global.ui.main.mode ~= df.ui_sidebar_mode.QueryBuilding or df.global.world.selected_building == nil then
error('no selected building')
end
local bld = df.global.world.selected_building
if bld._type ~= df.building_workshopst and bld._type ~= df.building_furnacest and bld._type ~= df.building_trapst then
error('not workshop or furnace or trap '..tostring(bld._type))
end
if #bld.jobs >= 10 then
return --error('too many jobs')
end
df.global.ui_workshop_in_add = false
if bld._type == df.building_trapst and (bld.trap_type == df.trap_type.Lever or bld.trap_type == df.trap_type.PressurePlate) then --hint:df.building_trapst
local bld = bld --as:df.building_trapst
if idx < #jobs_trap then
gui.simulateInput(ws, K'BUILDJOB_ADD')
ws:logic() --to initialize / switch to add job menu
gui.simulateInput(ws, jobs_trap[idx+1])
if df.global.ui_workshop_in_add and df.global.ui_lever_target_type ~= -1 then
recenter_view(df.global.cursor.x, df.global.cursor.y, df.global.cursor.z)
end
end
elseif bld._type == df.building_workshopst then
local bld = bld --as:df.building_workshopst
local wtype = bld.type
if wtype == df.workshop_type.Mechanics then
if idx < #jobs_mechanics then
gui.simulateInput(ws, K'BUILDJOB_ADD')
ws:logic() --to initialize / switch to add job menu
gui.simulateInput(ws, jobs_mechanics[idx+1])
end
elseif wtype == df.workshop_type.Butchers then
if idx < #jobs_butchers then
gui.simulateInput(ws, K'BUILDJOB_ADD')
ws:logic() --to initialize / switch to add job menu
gui.simulateInput(ws, jobs_butchers[idx+1])
end
elseif wtype == df.workshop_type.Fishery then
if idx < #jobs_fishery then
gui.simulateInput(ws, K'BUILDJOB_ADD')
ws:logic() --to initialize / switch to add job menu
gui.simulateInput(ws, jobs_fishery[idx+1])
end
elseif wtype == df.workshop_type.Loom then
if idx < #jobs_loom then
gui.simulateInput(ws, 'BUILDJOB_ADD')
ws:logic() --to initialize / switch to add job menu
gui.simulateInput(ws, jobs_loom[idx+1])
end
elseif wtype == df.workshop_type.Kennels then
if idx < #jobs_kennels then
gui.simulateInput(ws, K'BUILDJOB_ADD')
ws:logic() --to initialize / switch to add job menu
gui.simulateInput(ws, jobs_kennels[idx+1])
end
elseif wtype == df.workshop_type.Dyers then
if idx < #jobs_dyers then
gui.simulateInput(ws, K'BUILDJOB_ADD')
ws:logic() --to initialize / switch to add job menu
gui.simulateInput(ws, jobs_dyers[idx+1])
end
elseif wtype == df.workshop_type.Jewelers then
gui.simulateInput(ws, K'BUILDJOB_ADD')
ws:logic() --to initialize / switch to add job menu
local m = math.floor(idx / 4)
local t = idx % 4
df.global.ui_building_item_cursor = m
if t == 0 then
gui.simulateInput(ws, K'HOTKEY_JEWELER_CUT')
else