Advertisement
Guest User

Untitled

a guest
Sep 7th, 2022
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. --[====[
  2.  
  3. bugtest
  4. =====
  5.  
  6. utilities for testing the Hauling items bug(s)
  7.  
  8. Usage:
  9.  
  10. Usage:
  11.  
  12. IF UNIT SELECTED in Dwarf Fortress:
  13. :bugtest:
  14. dump whatever relevant inventory/hauling/job info I can collect for that unit
  15.  
  16. OTHERWISE:
  17. :bugtest:
  18. among all active units, find those with Hauled items; add these units to burrow
  19. 'test'
  20. print counts for valid and bugged-looking items
  21.  
  22. :bugtest -many:
  23. similar to previous, but only add units with *multiple* Hauled items in inventory
  24. to the 'test' burrow
  25.  
  26. :bugtest -followup:
  27. consider only the units already in the 'test' burrow. print which ones still look
  28. bugged and which ones do not
  29. remove the un-bugged ones from 'test' burrow
  30.  
  31. :bugtest -labors:
  32. for all units in 'test' burrow enable HAUL_STONE labor (this is to facilitate testing
  33. how readily they switch to some other available job activities)
  34.  
  35. :bugtest -tscript:
  36. generate a simple filter script for Dwarf Therapist, that will show units in
  37. 'test' burrow
  38.  
  39. :bugtest -dumpall
  40. data dump for all units in 'test' burrow
  41.  
  42. ]====]
  43.  
  44. local utils = require('utils')
  45.  
  46. local validArgs = utils.invert({
  47. "help",
  48. "followup",
  49. "many",
  50. "labors",
  51. "tscript",
  52. "dumpall"
  53. })
  54.  
  55. local utils = require 'utils'
  56.  
  57. local args = utils.processArgs({...}, validArgs)
  58.  
  59. if args.help then
  60. print(dfhack.script_help())
  61. return
  62. end
  63.  
  64.  
  65. local function enum2str(value, enumt)
  66. for index, val in ipairs(enumt) do
  67. if index == value then
  68. return val
  69. end
  70. end
  71.  
  72. return "enum not found"
  73. end
  74.  
  75. local function bitfield2str(bf)
  76. local retval = "{"
  77. local is1st = true
  78.  
  79. for a, b in pairs(bf) do
  80. if b then
  81. if not is1st then
  82. retval = retval .. ","
  83. end
  84. is1st = false
  85. retval = retval .. a
  86. end
  87. end
  88.  
  89. return retval .. "}"
  90. end
  91.  
  92. local function unit_name(unit)
  93. return dfhack.df2console(dfhack.TranslateName(dfhack.units.getVisibleName(unit)))
  94. end
  95.  
  96. local function dump_unit_data(unit)
  97. print(string.format("unit name: %s current_job: %s, unit.actions: %d", unit_name(unit), unit.job.current_job, #unit.actions))
  98. if unit.job.current_job then
  99. dfhack.job.printJobDetails(unit.job.current_job)
  100. end
  101.  
  102. for i,a in ipairs(unit.actions) do
  103. local a_str = string.format("unit action [%d] type: %d (%s) id: %d raw_data: ", i, a.type, enum2str(a.type, df.unit_action_type), a.id)
  104.  
  105. for _,v in ipairs(a.data.raw_data) do
  106. a_str = a_str .. " " .. v
  107. end
  108.  
  109. print(a_str)
  110. end
  111.  
  112. for k, inv_item in pairs(unit.inventory) do
  113. local item = inv_item.item
  114.  
  115. print("inventory item [" .. k .. "]", "mode: ", inv_item.mode .. "(" .. enum2str(inv_item.mode, df.unit_inventory_item.T_mode) ..
  116. ")", "description:", dfhack.df2console( dfhack.items.getDescription(item, 0, true)) )
  117.  
  118. print("item flags: ", bitfield2str(item.flags), "flags2: ", bitfield2str(item.flags2))
  119. end
  120. end
  121.  
  122. print(string.format("year %d tick %d (day %d; day's ticks %d)", df.global.cur_year, df.global.cur_year_tick, df.global.cur_year_tick // 1200,
  123. (df.global.cur_year_tick) % 1200))
  124.  
  125. unit = dfhack.gui.getSelectedUnit()
  126. if unit then
  127. dump_unit_data(unit)
  128. return
  129. end
  130.  
  131. local b_test = dfhack.burrows.findByName("test")
  132. if not b_test then
  133. qerror('test burrow not found')
  134. end
  135.  
  136. if args.followup then
  137. local bugged_found = 0
  138. local nonbugged_found = 0
  139. local test_units_n = #b_test.units
  140. local unbugged_units = ""
  141.  
  142. for _,unit in ipairs(df.global.world.units.active) do
  143. if dfhack.burrows.isAssignedUnit(b_test,unit) then
  144. if dfhack.units.isCitizen(unit) and dfhack.units.isAdult(unit) then
  145. local hauled_count = 0
  146. local hauled_no_injob = 0
  147.  
  148. for k, inv_item in pairs(unit.inventory) do
  149. local item = inv_item.item
  150.  
  151. if inv_item.mode == df.unit_inventory_item.T_mode.Hauled then
  152. hauled_count = hauled_count + 1
  153. if not item.flags.in_job then
  154. hauled_no_injob = hauled_no_injob + 1
  155. end
  156. end
  157. end
  158.  
  159. if hauled_no_injob > 0 then
  160. print(string.format("%s got bugged; hauled : %02d ; bugged(?): %02d", unit_name(unit),
  161. hauled_count, hauled_no_injob))
  162. if unit.job.current_job then
  163. dfhack.job.printJobDetails(unit.job.current_job)
  164. else
  165. print(string.format("job = nil, %d actions, %d individual_drills, %d social_activities, %d conversations, %d activities", #unit.actions,
  166. #unit.military.individual_drills, #unit.social_activities, #unit.conversations, #unit.activities))
  167. end
  168.  
  169. bugged_found = bugged_found + 1;
  170. else
  171. nonbugged_found = nonbugged_found + 1;
  172.  
  173. dfhack.burrows.setAssignedUnit(b_test,unit,false)
  174.  
  175. unbugged_units = unbugged_units .. "unbugged: " .. unit_name(unit) .. "\n"
  176. end
  177. end
  178. end
  179. end
  180.  
  181. print(string.format("test burrow had %d; found %d bugged, %d nonbugged. test burrow now has %d", test_units_n, bugged_found, nonbugged_found,
  182. #b_test.units))
  183. if (unbugged_units ~= "") then
  184. print(unbugged_units)
  185. end
  186. else
  187. dfhack.burrows.clearUnits(b_test)
  188.  
  189. for _,unit in ipairs(df.global.world.units.active) do
  190. if dfhack.units.isCitizen(unit) and dfhack.units.isAdult(unit) then
  191. local hauled_count = 0
  192. local hauled_no_injob = 0
  193.  
  194. for k, inv_item in pairs(unit.inventory) do
  195. local item = inv_item.item
  196.  
  197. if inv_item.mode == df.unit_inventory_item.T_mode.Hauled then
  198. hauled_count = hauled_count + 1
  199. if not item.flags.in_job then
  200. hauled_no_injob = hauled_no_injob + 1
  201. end
  202. end
  203. end
  204.  
  205. if hauled_count > 0 and not (args.many and hauled_count == 1) then
  206. print(string.format("%s hauled : %02d ; bugged(?): %02d", unit_name(unit),
  207. hauled_count, hauled_no_injob))
  208.  
  209. dfhack.burrows.setAssignedUnit(b_test,unit,true)
  210. end
  211. end
  212. end
  213. end
  214.  
  215. if (args.labors) then
  216. print(string.format("setting HAUL_STONE for units in 'test' burrow (%d units)", #b_test.units))
  217.  
  218. local labors_changed = 0
  219. local labors_nonchanged = 0
  220.  
  221. for _,unit in ipairs(df.global.world.units.active) do
  222. if dfhack.burrows.isAssignedUnit(b_test,unit) then
  223. local labors = unit.status.labors
  224.  
  225. if labors.HAUL_STONE then
  226. labors_nonchanged = labors_nonchanged + 1
  227. else
  228. labors.HAUL_STONE = true
  229. labors_changed = labors_changed + 1
  230. end
  231. end
  232. end
  233.  
  234. print(string.format("labor changed for %d, non changed for %d", labors_changed, labors_nonchanged))
  235.  
  236. print("disabling HAUL_STONE for other units")
  237.  
  238. for _,unit in ipairs(df.global.world.units.active) do
  239. if dfhack.units.isCitizen(unit) and dfhack.units.isAdult(unit) and not dfhack.burrows.isAssignedUnit(b_test,unit) then
  240. local labors = unit.status.labors
  241. labors.HAUL_STONE = false
  242. end
  243. end
  244. end
  245.  
  246. if (args.tscript) then
  247. local is_first = true
  248.  
  249. print("Dwarf Therapist script for units in 'test' burrow:")
  250. for _,unit in ipairs(df.global.world.units.active) do
  251. if dfhack.burrows.isAssignedUnit(b_test,unit) then
  252. local s = 'd.name_matches("' .. dfhack.TranslateName(dfhack.units.getVisibleName(unit)) .. '")'
  253. if not is_first then
  254. s = ' || ' .. s
  255. end
  256. is_first = false
  257.  
  258. print(s)
  259. end
  260. end
  261. end
  262.  
  263. if (args.dumpall) then
  264. print("data dump for all units in 'test' burrow:")
  265. for _,unit in ipairs(df.global.world.units.active) do
  266. if dfhack.burrows.isAssignedUnit(b_test,unit) then
  267. dump_unit_data(unit)
  268. end
  269. end
  270. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement