-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathadvfort.lua
1846 lines (1762 loc) · 64.2 KB
/
advfort.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
-- allows to do jobs in adv. mode.
--[====[
gui/advfort
===========
This script allows to perform jobs in adventure mode. For more complete help
press :kbd:`?` while script is running. It's most comfortable to use this as a
keybinding. (e.g. ``keybinding set Ctrl-T gui/advfort``). Possible arguments:
:-a, --nodfassign: uses different method to assign items.
:-i, --inventory: checks inventory for possible items to use in the job.
:-c, --cheat: relaxes item requirements for buildings (e.g. walls from bones). Implies -a
:job: selects that job (e.g. Dig or FellTree)
An example of player digging in adventure mode:
.. image:: /docs/images/advfort.png
**WARNING:** changes only persist in non procedural sites, namely: player forts, caves, and camps.
]====]
--[==[
version: 0.044
changelog:
*0.044
- added output to clear_jobs of number of cleared jobs
- another failed attempt at gather plants fix
- added track stop configuration window
*0.043
- fixed track carving: up/down was reversed and removed (temp) requirements because they were not working correctly
- added checks for unsafe conditions (currently quite stupid). Should save few adventurers that are trying to work in dangerous conditions (e.g. fishing)
- unsafe checks disabled by "-u" ir "--unsafe"
*0.042
- fixed (probably for sure now) the crash bug.
- added --clear_jobs debug option. Will delete ALL JOBS!
*0.041
- fixed cooking allowing already cooked meals
*0.04
- add (-q)uick mode. Autoselects materials.
- fixed few(?) crash bugs
- fixed job errors not being shown in df
*0.031
- make forbiding optional (-s)afe mode
*0.03
- forbid doing anything in non-sites unless you are (-c)heating
- a bit more documentation and tidying
- add a deadlock fix
*0.021
- advfort_items now autofills items
- tried out few things to fix gather plants
*0.02
- fixed axles not being able to be placed in other direction (thanks SyrusLD)
- added lever linking
- restructured advfort_items, don't forget to update that too!
- Added clutter view if shop is cluttered.
*0.013
- fixed siege weapons and traps (somewhat). Now you can load them with new menu :)
*0.012
- fix for some jobs not finding correct building.
*0.011
- fixed crash with building jobs (other jobs might have been crashing too!)
- fixed bug with building asking twice to input items
*0.01
- instant job startation
- item selection screen (!)
- BUG:custom jobs need stuff on ground to work
*0.003
- fixed farms (i think...)
- added faster time pasing (yay for random deaths from local wildlife)
- still hasn't fixed gather plants. but you can visit local market, buy a few local fruits/vegetables eat them and use seeds
- other stuff
*0.002
- kind-of fixed the item problem... now they get teleported (if teleport_items=true which should be default for adventurer)
- gather plants still not working... Other jobs seem to work.
- added new-and-improved waiting. Interestingly it could be improved to be interuptable.
todo list:
- document everything! Maybe somebody would understand what is happening then and help me :<
- when building trap add to known traps (or known adventurers?) so that it does not trigger on adventurer
bugs list:
- items blocking construction stuck the game
- burning charcoal crashed game
- gem thingies probably broken
- custom reactions semibroken
- gathering plants still broken
--]==]
--keybinding, change to your hearts content. Only the key part.
keybinds={
nextJob={key="CUSTOM_SHIFT_T",desc="Next job in the list"},
prevJob={key="CUSTOM_SHIFT_R",desc="Previous job in the list"},
continue={key="A_WAIT",desc="Continue job if available"},
down_alt1={key="CUSTOM_CTRL_D",desc="Use job down"},
down_alt2={key="CURSOR_DOWN_Z_AUX",desc="Use job down"},
up_alt1={key="CUSTOM_CTRL_E",desc="Use job up"},
up_alt2={key="CURSOR_UP_Z_AUX",desc="Use job up"},
use_same={key="A_MOVE_SAME_SQUARE",desc="Use job at the tile you are standing"},
workshop={key="CHANGETAB",desc="Show building menu"},
quick={key="CUSTOM_Q",desc="Toggle quick item select"},
}
-- building filters
build_filter={
forbid_all=false, --this forbits all except the "allow"
allow={"MetalSmithsForge"}, --ignored if forbit_all=false
forbid={} --ignored if forbit_all==true
}
build_filter.HUMANish={
forbid_all=true,
allow={"Masons"},
forbid={}
}
--economic stone fix: just disable all of them
--[[ FIXME: maybe let player select which to disable?]]
for k,v in ipairs(df.global.ui.economic_stone) do df.global.ui.economic_stone[k]=0 end
local gui = require 'gui'
local wid=require 'gui.widgets'
local dialog=require 'gui.dialogs'
local buildings=require 'dfhack.buildings'
local bdialog=require 'gui.buildings'
local workshopJobs=require 'dfhack.workshops'
local utils=require 'utils'
local gscript=require 'gui.script'
local tile_attrs = df.tiletype.attrs
settings={build_by_items=false,use_worn=false,check_inv=true,teleport_items=true,df_assign=false,gui_item_select=true,only_in_sites=false}
function hasValue(tbl,val)
for k,v in pairs(tbl) do
if v==val then
return true
end
end
return false
end
function reverseRaceLookup(id)
return df.global.world.raws.creatures.all[id].creature_id
end
function deon_filter(name,type_id,subtype_id,custom_id, parent)
--print(name)
local adv=df.global.world.units.active[0]
local race_filter=build_filter[reverseRaceLookup(adv.race)]
if race_filter then
if race_filter.forbid_all then
return hasValue(race_filter.allow,name)
else
return not hasValue(race_filter.forbid,name)
end
else
if build_filter.forbid_all then
return hasValue(build_filter.allow,name)
else
return not hasValue(build_filter.forbid,name)
end
end
end
local mode_name
for k,v in ipairs({...}) do --setting parsing
if v=="-c" or v=="--cheat" then
settings.build_by_items=true
settings.df_assign=false
elseif v=="-q" or v=="--quick" then
settings.quick=true
elseif v=="-u" or v=="--unsafe" then --ignore pain and etc
settings.unsafe=true
elseif v=="-s" or v=="--safe" then
settings.safe=true
elseif v=="-i" or v=="--inventory" then
settings.check_inv=true
settings.df_assign=false
elseif v=="-a" or v=="--nodfassign" then
settings.df_assign=false
elseif v=="-h" or v=="--help" then
settings.help=true
elseif v=="--clear_jobs" then
settings.clear_jobs=true
else
mode_name=v
end
end
mode=mode or 0
last_building=last_building or {}
function Disclaimer(tlb)
local dsc={"The Gathering Against ",{text="Goblin ",pen=dfhack.pen.parse{fg=COLOR_GREEN,bg=0}}, "Oppresion ",
"(TGAGO) is not responsible for all ",NEWLINE,"the damage that this tool can (and will) cause to you and your loved worlds",NEWLINE,"and/or sanity.Please use with caution.",NEWLINE,{text="Magma not included.",pen=dfhack.pen.parse{fg=COLOR_LIGHTRED,bg=0}}}
if tlb then
for _,v in ipairs(dsc) do
table.insert(tlb,v)
end
end
return dsc
end
function showHelp()
local helptext={
"This tool allow you to perform jobs as a dwarf would in dwarf mode. When ",NEWLINE,
"cursor is available you can press ",{key="SELECT", text="select",key_sep="()"},
" to enqueue a job from",NEWLINE,"pointer location. If job is 'Build' and there is no planed construction",NEWLINE,
"at cursor this tool show possible building choices.",NEWLINE,NEWLINE,{text="Keybindings:",pen=dfhack.pen.parse{fg=COLOR_CYAN,bg=0}},NEWLINE
}
for k,v in pairs(keybinds) do
table.insert(helptext,{key=v.key,text=v.desc,key_sep=":"})
table.insert(helptext,NEWLINE)
end
table.insert(helptext,{text="CAREFULL MOVE",pen=dfhack.pen.parse{fg=COLOR_LIGHTGREEN,bg=0}})
table.insert(helptext,": use job in that direction")
table.insert(helptext,NEWLINE)
table.insert(helptext,NEWLINE)
Disclaimer(helptext)
dialog.showMessage("Help!?!",helptext)
end
if settings.help then
showHelp()
return
end
--[[ Util functions ]]--
function advGlobalPos()
local map=df.global.world.map
local wd=df.global.world.world_data
local adv=df.global.world.units.active[0]
--wd.adv_region_x*16+wd.adv_emb_x,wd.adv_region_y*16+wd.adv_emb_y
--return wd.adv_region_x*16+wd.adv_emb_x,wd.adv_region_y*16+wd.adv_emb_y
--return wd.adv_region_x*16+wd.adv_emb_x+adv.pos.x/16,wd.adv_region_y*16+wd.adv_emb_y+adv.pos.y/16
--print(map.region_x,map.region_y,adv.pos.x,adv.pos.y)
--print(map.region_x+adv.pos.x/48, map.region_y+adv.pos.y/48,wd.adv_region_x*16+wd.adv_emb_x,wd.adv_region_y*16+wd.adv_emb_y)
return math.floor(map.region_x+adv.pos.x/48), math.floor(map.region_y+adv.pos.y/48)
end
function inSite()
local tx,ty=advGlobalPos()
for k,v in pairs(df.global.world.world_data.sites) do
local tp={v.pos.x,v.pos.y}
if tx>=tp[1]*16+v.rgn_min_x and tx<=tp[1]*16+v.rgn_max_x and
ty>=tp[2]*16+v.rgn_min_y and ty<=tp[2]*16+v.rgn_max_y then
return v
end
end
end
--[[ low level job management ]]--
function findAction(unit,ltype)
ltype=ltype or df.unit_action_type.None
for i,v in ipairs(unit.actions) do
if v.type==ltype then
return v
end
end
end
function add_action(unit,action_data)
local action=findAction(unit) --find empty action
if action then
action:assign(action_data)
action.id=unit.next_action_id
unit.next_action_id=unit.next_action_id+1
else
local tbl=copyall(action_data)
tbl.new=true
tbl.id=unit.next_action_id
unit.actions:insert("#",tbl)
unit.next_action_id=unit.next_action_id+1
end
end
function addJobAction(job,unit) --what about job2?
if job==nil then
error("invalid job")
end
if findAction(unit,df.unit_action_type.Job) or findAction(unit,df.unit_action_type.Job2) then
print("Already has job action")
return
end
local action=findAction(unit)
local pos=copyall(unit.pos)
--local pos=copyall(job.pos)
unit.path.dest:assign(pos)
--job
local data={type=df.unit_action_type.Job,data={job={x=pos.x,y=pos.y,z=pos.z,timer=10}}}
--job2:
--local data={type=df.unit_action_type.Job2,data={job2={timer=10}}}
add_action(unit,data)
--add_action(unit,{type=df.unit_action_type.Unsteady,data={unsteady={timer=5}}})
end
function make_native_job(args)
if args.job == nil then
local newJob=df.job:new()
newJob.id=df.global.job_next_id
df.global.job_next_id=df.global.job_next_id+1
newJob.flags.special=true
newJob.job_type=args.job_type
newJob.completion_timer=-1
newJob.pos:assign(args.pos)
--newJob.pos:assign(args.unit.pos)
args.job=newJob
args.unlinked=true
end
end
function smart_job_delete( job )
local gref_types=df.general_ref_type
--TODO: unmark items as in job
for i,v in ipairs(job.general_refs) do
if v:getType()==gref_types.BUILDING_HOLDER then
local b=v:getBuilding()
if b then
--remove from building
for i,v in ipairs(b.jobs) do
if v==job then
b.jobs:erase(i)
break
end
end
else
print("Warning: building holder ref was invalid while deleting job")
end
elseif v:getType()==gref_types.UNIT_WORKER then
local u=v:getUnit()
if u then
u.job.current_job =nil
else
print("Warning: unit worker ref was invalid while deleting job")
end
else
print("Warning: failed to remove link from job with type:",gref_types[v:getType()])
end
end
--unlink job
local link=job.list_link
if link.prev then
link.prev.next=link.next
end
if link.next then
link.next.prev=link.prev
end
link:delete()
--finally delete the job
job:delete()
end
--TODO: this logic might be better with other --starting logic--
if settings.clear_jobs then
print("Clearing job list!")
local counter=0
local job_link=df.global.world.job_list.next
while job_link and job_link.item do
local job=job_link.item
job_link=job_link.next
smart_job_delete(job)
counter=counter+1
end
print("Deleted: "..counter.." jobs")
return
end
function makeJob(args)
gscript.start(function ()
make_native_job(args)
local failed
for k,v in ipairs(args.pre_actions or {}) do
local ok,msg=v(args)
if not ok then
failed=msg
break
end
end
if failed==nil then
AssignUnitToJob(args.job,args.unit,args.from_pos)
for k,v in ipairs(args.post_actions or {}) do
local ok,msg=v(args)
if not ok then
failed=msg
break
end
end
if failed then
UnassignJob(args.job,args.unit)
end
end
if failed==nil then
if args.unlinked then
dfhack.job.linkIntoWorld(args.job,true)
args.unlinked=false
end
addJobAction(args.job,args.unit)
args.screen:wait_tick()
else
if not args.no_job_delete then
smart_job_delete(args.job)
end
dfhack.gui.showAnnouncement("Job failed:"..failed,5,1)
end
end)
end
function UnassignJob(job,unit,unit_pos)
unit.job.current_job=nil
end
function AssignUnitToJob(job,unit,unit_pos)
job.general_refs:insert("#",{new=df.general_ref_unit_workerst,unit_id=unit.id})
unit.job.current_job=job
unit_pos=unit_pos or {x=job.pos.x,y=job.pos.y,z=job.pos.z}
unit.path.dest:assign(unit_pos)
return true
end
function SetCreatureRef(args)
local job=args.job
local pos=args.pos
for k,v in pairs(df.global.world.units.active) do
if v.pos.x==pos.x and v.pos.y==pos.y and v.pos.z==pos.z then
job.general_refs:insert("#",{new=df.general_ref_unit_cageest,unit_id=v.id})
return
end
end
end
function SetWebRef(args)
local pos=args.pos
for k,v in pairs(df.global.world.items.other.ANY_WEBS) do
if v.pos.x==pos.x and v.pos.y==pos.y and v.pos.z==pos.z then
args.job.general_refs:insert("#",{new=df.general_ref_item,item_id=v.id})
return
end
end
end
function SetPatientRef(args)
local job=args.job
local pos=args.pos
for k,v in pairs(df.global.world.units.active) do
if v.pos.x==pos.x and v.pos.y==pos.y and v.pos.z==pos.z then
job.general_refs:insert("#",{new=df.general_ref_unit_patientst,unit_id=v.id})
return
end
end
end
function SetCarveDir(args)
local job=args.job
local pos=args.pos
local from_pos=args.from_pos
local dirs={up=18,down=19,right=20,left=21}
if pos.x>from_pos.x then
job.item_category[dirs.right]=true
elseif pos.x<from_pos.x then
job.item_category[dirs.left]=true
elseif pos.y>from_pos.y then
job.item_category[dirs.down]=true
elseif pos.y<from_pos.y then
job.item_category[dirs.up]=true
end
end
function MakePredicateWieldsItem(item_skill)
local pred=function(args)
local inv=args.unit.inventory
for k,v in pairs(inv) do
if v.mode==1 and v.item:getMeleeSkill()==item_skill and args.unit.body.weapon_bp==v.body_part_id then
return true
end
end
return false,"Correct tool not equiped"
end
return pred
end
function makeset(args)
local tbl={}
for k,v in pairs(args) do
tbl[v]=true
end
return tbl
end
function NotConstruct(args)
local tt=dfhack.maps.getTileType(args.pos)
if tile_attrs[tt].material~=df.tiletype_material.CONSTRUCTION and dfhack.buildings.findAtTile(args.pos)==nil then
return true
else
return false, "Can only do it on non constructions"
end
end
function NoConstructedBuilding(args)
local bld=dfhack.buildings.findAtTile(args.pos)
if bld and bld.construction_stage==3 then
return false, "Can only do it on clear area or non-finished buildings"
end
return true
end
function IsBuilding(args)
if dfhack.buildings.findAtTile(args.pos) then
return true
end
return false, "Can only do it on buildings"
end
function IsConstruct(args)
local tt=dfhack.maps.getTileType(args.pos)
if tile_attrs[tt].material==df.tiletype_material.CONSTRUCTION then
return true
else
return false, "Can only do it on constructions"
end
end
function SameSquare(args)
local pos1=args.pos
local pos2=args.from_pos
if pos1.x==pos2.x and pos1.y==pos2.y and pos1.z==pos2.z then
return true
else
return false, "Can only do it on same square"
end
end
function IsHardMaterial(args)
local tt=dfhack.maps.getTileType(args.pos)
local mat=tile_attrs[tt].material
local hard_materials=makeset{df.tiletype_material.STONE,df.tiletype_material.FEATURE,
df.tiletype_material.LAVA_STONE,df.tiletype_material.MINERAL,df.tiletype_material.FROZEN_LIQUID,}
if hard_materials[mat] then
return true
else
return false, "Can only do it on hard materials"
end
end
function IsStairs(args)
local tt=dfhack.maps.getTileType(args.pos)
local shape=tile_attrs[tt].shape
if shape==df.tiletype_shape.STAIR_UP or shape==df.tiletype_shape.STAIR_DOWN or shape==df.tiletype_shape.STAIR_UPDOWN or shape==df.tiletype_shape.RAMP then
return true
else
return false,"Can only do it on stairs/ramps"
end
end
function IsFloor(args)
local tt=dfhack.maps.getTileType(args.pos)
local shape=tile_attrs[tt].shape
if shape==df.tiletype_shape.FLOOR or shape==df.tiletype_shape.BOULDER or shape==df.tiletype_shape.PEBBLES then
return true
else
return false,"Can only do it on floors"
end
end
function IsWall(args)
local tt=dfhack.maps.getTileType(args.pos)
if tile_attrs[tt].shape==df.tiletype_shape.WALL then
return true
else
return false, "Can only do it on walls"
end
end
function IsTree(args)
local tt=dfhack.maps.getTileType(args.pos)
if tile_attrs[tt].material==df.tiletype_material.TREE then
return true
else
return false, "Can only do it on trees"
end
end
function IsPlant(args)
local tt=dfhack.maps.getTileType(args.pos)
if tile_attrs[tt].shape==df.tiletype_shape.SHRUB then
return true
else
return false, "Can only do it on plants"
end
end
function IsWater(args)
return true
end
function IsUnit(args)
local pos=args.pos
for k,v in pairs(df.global.world.units.active) do
if v.pos.x==pos.x and v.pos.y==pos.y and v.pos.z==pos.z then
return true
end
end
return false,"Unit must be present"
end
function itemsAtPos(pos,tbl)
local ret=tbl or {}
for k,v in pairs(df.global.world.items.all) do
if v.pos.x==pos.x and v.pos.y==pos.y and v.pos.z==pos.z and v.flags.on_ground then
table.insert(ret,v)
end
end
return ret
end
function AssignBuildingRef(args)
local bld=args.building or dfhack.buildings.findAtTile(args.pos)
args.job.general_refs:insert("#",{new=df.general_ref_building_holderst,building_id=bld.id})
bld.jobs:insert("#",args.job)
args.building=args.building or bld
return true
end
function chooseBuildingWidthHeightDir(args) --TODO nicer selection dialog
local btype=df.building_type
local area=makeset{"w","h"}
local all=makeset{"w","h","d"}
local needs={[btype.FarmPlot]=area,[btype.Bridge]=all,
[btype.RoadDirt]=area,[btype.RoadPaved]=area,[btype.ScrewPump]=makeset{"d"},
[btype.AxleHorizontal]=all,[btype.WaterWheel]=makeset{"d"},[btype.Rollers]=makeset{"d"}}
local myneeds=needs[args.type]
if myneeds==nil then return end
if args.width==nil and myneeds.w then
--args.width=3
dialog.showInputPrompt("Building size:", "Input building width:", nil, "1",
function(txt) args.width=tonumber(txt);BuildingChosen(args) end)
return true
end
if args.height==nil and myneeds.h then
--args.height=4
dialog.showInputPrompt("Building size:", "Input building height:", nil, "1",
function(txt) args.height=tonumber(txt);BuildingChosen(args) end)
return true
end
if args.direction==nil and myneeds.d then
--args.direction=0--?
dialog.showInputPrompt("Building size:", "Input building direction:", nil, "0",
function(txt) args.direction=tonumber(txt);BuildingChosen(args) end)
return true
end
return false
--width = ..., height = ..., direction = ...
end
CheckAndFinishBuilding=nil
function BuildingChosen(inp_args,type_id,subtype_id,custom_id)
local args=inp_args or {}
args.type=type_id or args.type
args.subtype=subtype_id or args.subtype
args.custom=custom_id or args.custom_id
if inp_args then
args.pos=inp_args.pos or args.pos
end
last_building.type=args.type
last_building.subtype=args.subtype
last_building.custom=args.custom
if chooseBuildingWidthHeightDir(args) then
return
end
--if settings.build_by_items then
-- args.items=itemsAtPos(inp_args.from_pos)
--end
args.building=buildings.constructBuilding(args)
CheckAndFinishBuilding(args,args.building)
end
function RemoveBuilding(args)
local bld=dfhack.buildings.findAtTile(args.pos)
if bld~=nil then
bld:queueDestroy()
for k,v in ipairs(bld.jobs) do
if v.job_type==df.job_type.DestroyBuilding then
AssignUnitToJob(v,args.unit,args.from_pos)
return true
end
end
return false,"Building removal job failed to be created"
else
return false,"No building to remove"
end
end
function isSuitableItem(job_item,item)
--todo butcher test
if job_item.item_type~=-1 then
if item:getType()~= job_item.item_type then
return false, "type"
elseif job_item.item_subtype~=-1 then
if item:getSubtype()~=job_item.item_subtype then
return false,"subtype"
end
end
end
if job_item.mat_type~=-1 then
if item:getActualMaterial()~= job_item.mat_type then --unless we would want to make hist-fig specific reactions
return false, "material"
elseif job_item.mat_index~=-1 then
if item:getActualMaterialIndex()~=job_item.mat_index then
return false,"material index"
end
end
end
if job_item.flags1.sand_bearing and not item:isSandBearing() then
return false,"not sand bearing"
end
if job_item.flags1.butcherable and not (item:getType()== df.item_type.CORPSE or item:getType()==df.item_type.CORPSEPIECE) then
return false,"not butcherable"
end
local matinfo=dfhack.matinfo.decode(item)
--print(matinfo:getCraftClass())
--print("Matching ",item," vs ",job_item)
if job_item.flags1.cookable and item:getType()==df.item_type.FOOD then
return false,"already cooked"
end
if type(job_item) ~= "table" and not matinfo:matches(job_item) then
--[[
local true_flags={}
for k,v in pairs(job_item.flags1) do
if v then
table.insert(true_flags,k)
end
end
for k,v in pairs(job_item.flags2) do
if v then
table.insert(true_flags,k)
end
end
for k,v in pairs(job_item.flags3) do
if v then
table.insert(true_flags,k)
end
end
for k,v in pairs(true_flags) do
print(v)
end
--]]
return false,"matinfo"
end
-- some bonus checks:
if job_item.flags2.building_material and not item:isBuildMat() then
return false,"non-build mat"
end
-- *****************
--print("--Matched")
--reagen_index?? reaction_id??
if job_item.metal_ore~=-1 and not item:isMetalOre(job_item.metal_ore) then
return false,"metal ore"
end
if job_item.min_dimension~=-1 then
end
-- if #job_item.contains~=0 then
-- end
if job_item.has_tool_use~=-1 then
if not item:hasToolUse(job_item.has_tool_use) then
return false,"tool use"
end
end
if job_item.has_material_reaction_product~="" then
local ok=false
for k,v in pairs(matinfo.material.reaction_product.id) do
if v.value==job_item.has_material_reaction_product then
ok=true
break
end
end
if not ok then
return false, "no material reaction product"
end
end
if job_item.reaction_class~="" then
local ok=false
for k,v in pairs(matinfo.material.reaction_class) do
if v.value==job_item.reaction_class then
ok=true
break
end
end
if not ok then
return false, "no material reaction class"
end
end
return true
end
function getItemsUncollected(job)
local ret={}
for id,jitem in pairs(job.items) do
local x,y,z=dfhack.items.getPosition(jitem.item)
if x~=job.pos.x or y~=job.pos.y or z~=job.pos.z then
table.insert(ret,jitem)
end
end
return ret
end
function AddItem(tbl,item,recurse,skip_add)
if not skip_add then
table.insert(tbl,item)
end
if recurse then
local subitems=dfhack.items.getContainedItems(item)
if subitems~=nil then
for k,v in pairs(subitems) do
AddItem(tbl,v,recurse)
end
end
end
end
function EnumItems(args)
local ret=args.table or {}
if args.all then
for k,v in pairs(df.global.world.items.all) do
if v.flags.on_ground then
AddItem(ret,v,args.deep)
end
end
elseif args.pos~=nil then
for k,v in pairs(df.global.world.items.all) do
if v.pos.x==args.pos.x and v.pos.y==args.pos.y and v.pos.z==args.pos.z and v.flags.on_ground then
AddItem(ret,v,args.deep)
end
end
end
if args.unit~=nil then
for k,v in pairs(args.unit.inventory) do
if args.inv[v.mode] then
AddItem(ret,v.item,args.deep)
elseif args.deep then
AddItem(ret,v.item,args.deep,true)
end
end
end
return ret
end
function putItemsInBuilding(building,job_item_refs)
for k,v in ipairs(job_item_refs) do
--local pos=dfhack.items.getPosition(v)
if not dfhack.items.moveToBuilding(v.item,building,0) then
print("Could not put item:",k,v.item)
end
v.is_fetching=0
end
end
function putItemsInHauling(unit,job_item_refs)
for k,v in ipairs(job_item_refs) do
--local pos=dfhack.items.getPosition(v)
print("moving:",tostring(v),tostring(v.item))
printall(v)
if not dfhack.items.moveToInventory(v.item,unit,0,0) then
print("Could not put item:",k,v.item)
end
v.is_fetching=0
end
end
function finish_item_assign(args)
local job=args.job
local item_modes={
[df.job_type.PlantSeeds]="haul",
[df.job_type.Eat]="haul",
}
local item_mode=item_modes[job.job_type] or "teleport"
if settings.teleport_items and item_mode=="teleport" then
putItemsInBuilding(args.building,job.items)
end
local uncollected = getItemsUncollected(job)
if #uncollected == 0 then
job.flags.working=true
if item_mode=="haul" then
putItemsInHauling(args.unit,job.items)
end
else
job.flags.fetching=true
uncollected[1].is_fetching=1
end
end
function EnumItems_with_settings( args )
if settings.check_inv then
return EnumItems{pos=args.from_pos,unit=args.unit,
inv={[df.unit_inventory_item.T_mode.Hauled]=settings.use_worn,[df.unit_inventory_item.T_mode.Worn]=settings.use_worn,
[df.unit_inventory_item.T_mode.Weapon]=settings.use_worn,},deep=true}
else
return EnumItems{pos=args.from_pos}
end
end
function find_suitable_items(job,items,job_items)
job_items=job_items or job.job_items
local item_counts={}
for job_id, trg_job_item in ipairs(job_items) do
item_counts[job_id]=trg_job_item.quantity
end
local item_suitability={}
local used_item_id={}
for job_id, trg_job_item in ipairs(job_items) do
item_suitability[job_id]={}
for _,cur_item in pairs(items) do
if not used_item_id[cur_item.id] then
local item_suitable,msg=isSuitableItem(trg_job_item,cur_item)
if item_suitable or settings.build_by_items then
table.insert(item_suitability[job_id],cur_item)
end
--[[
if msg then
print(cur_item,msg)
else
print(cur_item,"ok")
end
--]]
if not settings.gui_item_select then
if (item_counts[job_id]>0 and item_suitable) or settings.build_by_items then
--cur_item.flags.in_job=true
job.items:insert("#",{new=true,item=cur_item,role=df.job_item_ref.T_role.Reagent,job_item_idx=job_id})
item_counts[job_id]=item_counts[job_id]-cur_item:getTotalDimension()
--print(string.format("item added, job_item_id=%d, item %s, quantity left=%d",job_id,tostring(cur_item),item_counts[job_id]))
used_item_id[cur_item.id]=true
end
end
end
end
end
return item_suitability,item_counts
end
function AssignJobItems(args)
if settings.df_assign then --use df default logic and hope that it would work
return true
end
-- first find items that you want to use for the job
local job=args.job
local its=EnumItems_with_settings(args)
local item_suitability,item_counts=find_suitable_items(job,its)
--[[while(#job.items>0) do --clear old job items
job.items[#job.items-1]:delete()
job.items:erase(#job.items-1)
end]]
if settings.gui_item_select and #job.job_items>0 then
local item_dialog=require('hack.scripts.gui.advfort_items')
if settings.quick then --TODO not so nice hack. instead of rewriting logic for job item filling i'm using one in gui dialog...
local item_editor=item_dialog.jobitemEditor{
job = job,
items = item_suitability,
}
if item_editor:jobValid() then
item_editor:commit()
finish_item_assign(args)
return true
else
return false, "Quick select items"
end
else
local ret=item_dialog.showItemEditor(job,item_suitability)
if ret then
finish_item_assign(args)
return true
else
print("Failed job, i'm confused...")
end
--end)
return false,"Selecting items"
end
else
if not settings.build_by_items then
for job_id, trg_job_item in ipairs(job.job_items) do
if item_counts[job_id]>0 then
print("Not enough items for this job")
return false, "Not enough items for this job"
end
end
end
finish_item_assign(args)
return true
end
end
CheckAndFinishBuilding=function (args,bld)
args.building=args.building or bld
for idx,job in pairs(bld.jobs) do
if job.job_type==df.job_type.ConstructBuilding then
args.job=job
args.no_job_delete=true
break
end
end
if args.job~=nil then
args.pre_actions={AssignJobItems}
else
local t={items=buildings.getFiltersByType({},bld:getType(),bld:getSubtype(),bld:getCustomType())}
args.pre_actions={dfhack.curry(setFiltersUp,t),AssignBuildingRef}--,AssignJobItems
end
args.no_job_delete=true
makeJob(args)
end
function AssignJobToBuild(args)
local bld=args.building or dfhack.buildings.findAtTile(args.pos)
args.building=bld
args.job_type=df.job_type.ConstructBuilding
if bld~=nil then
CheckAndFinishBuilding(args,bld)
else
bdialog.BuildingDialog{on_select=dfhack.curry(BuildingChosen,args),hide_none=true,building_filter=deon_filter}:show()
end
return true
end
function BuildLast(args)
local bld=dfhack.buildings.findAtTile(args.pos)
args.job_type=df.job_type.ConstructBuilding
if bld~=nil then
CheckAndFinishBuilding(args,bld)
else