From 3d1b6412320cbb69e0ce54d0164b1c030687b9e0 Mon Sep 17 00:00:00 2001 From: Ryan Williams Date: Tue, 16 Jul 2024 16:42:13 -0700 Subject: [PATCH] Make use of dfhack.units.getCasteRaw * Update unretire-anyone.lua * Update workorder.lua * Update set-personality.lua * Update uniform-unstick.lua * Update editor_colors.lua * Update notifications.lua * Update unit-syndromes.lua * Update set-orientation.lua * Update unit-info-viewer.lua * Update companion-order.lua --- gui/companion-order.lua | 9 ++------- gui/unit-info-viewer.lua | 16 ++++++---------- gui/unit-syndromes.lua | 2 +- internal/gm-unit/editor_colors.lua | 4 ++-- internal/notify/notifications.lua | 6 ++---- modtools/set-personality.lua | 2 +- set-orientation.lua | 2 +- uniform-unstick.lua | 2 +- unretire-anyone.lua | 2 +- workorder.lua | 5 +---- 10 files changed, 18 insertions(+), 32 deletions(-) diff --git a/gui/companion-order.lua b/gui/companion-order.lua index 9f5449d3c1..9659c79785 100644 --- a/gui/companion-order.lua +++ b/gui/companion-order.lua @@ -62,13 +62,8 @@ function getxyz() -- this will return pointers x,y and z coordinates. return x,y,z -- return the coords end -function GetCaste(race_id,caste_id) - local race=df.creature_raw.find(race_id) - return race.caste[caste_id] -end - function EnumBodyEquipable(race_id,caste_id) - local caste=GetCaste(race_id,caste_id) + local caste=dfhack.units.getCasteRaw(race_id,caste_id) local bps=caste.body_info.body_parts local ret={} for k,v in pairs(bps) do @@ -149,7 +144,7 @@ function AddIfFits(body_equip,unit,item) return false end function EnumGrasps(race_id,caste_id) - local caste=GetCaste(race_id,caste_id) + local caste=dfhack.units.getCasteRaw(race_id,caste_id) local bps=caste.body_info.body_parts local ret={} for k,v in pairs(bps) do diff --git a/gui/unit-info-viewer.lua b/gui/unit-info-viewer.lua index 56751e9fd1..ed677453a9 100644 --- a/gui/unit-info-viewer.lua +++ b/gui/unit-info-viewer.lua @@ -168,10 +168,6 @@ local function get_death_type(death_cause) return DEATH_TYPES[death_cause] or ' died of unknown causes' end -local function get_caste_data(unit) - return df.global.world.raws.creatures.all[unit.race].caste[unit.caste] -end - local function get_creature_data(unit) return df.global.world.raws.creatures.all[unit.race] end @@ -190,7 +186,7 @@ local function get_translated_name_chunk(unit) end local function get_description_chunk(unit) - local desc = get_caste_data(unit).description + local desc = dfhack.units.getCasteRaw(unit).description if #desc == 0 then return end return {text=desc, pen=COLOR_WHITE} end @@ -271,7 +267,7 @@ end local function get_max_age_chunk(unit) if not dfhack.units.isAlive(unit) then return end - local caste = get_caste_data(unit) + local caste = dfhack.units.getCasteRaw(unit) local blurb if caste.misc.maxage_min == -1 then blurb = ' only die of unnatural causes.' @@ -395,7 +391,7 @@ end local function get_grazer_chunk(unit) if not dfhack.units.isGrazer(unit) then return end - local caste = get_caste_data(unit) + local caste = dfhack.units.getCasteRaw(unit) local blurb = 'Grazing satisfies ' .. tostring(caste.misc.grazer) .. ' units of hunger.' return {text=blurb, pen=COLOR_LIGHTGREEN} end @@ -403,7 +399,7 @@ end local function get_milkable_chunk(unit) if not dfhack.units.isAlive(unit) or not dfhack.units.isMilkable(unit) then return end if not dfhack.units.isAnimal(unit) then return end - local caste = get_caste_data(unit) + local caste = dfhack.units.getCasteRaw(unit) local milk = dfhack.matinfo.decode(caste.extracts.milkable_mat, caste.extracts.milkable_matidx) if not milk then return end local days, seconds = math.modf(caste.misc.milkable / TU_PER_DAY) @@ -419,7 +415,7 @@ end local function get_shearable_chunk(unit) if not dfhack.units.isAlive(unit) then return end if not dfhack.units.isAnimal(unit) then return end - local caste = get_caste_data(unit) + local caste = dfhack.units.getCasteRaw(unit) local mat_types = caste.body_info.materials.mat_type local mat_idxs = caste.body_info.materials.mat_index for idx, mat_type in ipairs(mat_types) do @@ -438,7 +434,7 @@ end local function get_egg_layer_chunk(unit) if not dfhack.units.isAlive(unit) or not dfhack.units.isEggLayer(unit) then return end - local caste = get_caste_data(unit) + local caste = dfhack.units.getCasteRaw(unit) local clutch = (caste.misc.clutch_size_max + caste.misc.clutch_size_min) // 2 local blurb = ('She lays clutches of about %d egg%s.'):format(clutch, clutch == 1 and '' or 's') return {text=blurb, pen=COLOR_GREEN} diff --git a/gui/unit-syndromes.lua b/gui/unit-syndromes.lua index b8ea3a37ee..b789bfd97d 100644 --- a/gui/unit-syndromes.lua +++ b/gui/unit-syndromes.lua @@ -306,7 +306,7 @@ local function getLivestock() local units = {} for _, unit in pairs(df.global.world.units.active) do - local caste_flags = unit.caste and df.global.world.raws.creatures.all[unit.race].caste[unit.caste].flags + local caste_flags = dfhack.units.getCasteRaw(unit).flags if dfhack.units.isFortControlled(unit) and caste_flags and (caste_flags.PET or caste_flags.PET_EXOTIC) then table.insert(units, unit) diff --git a/internal/gm-unit/editor_colors.lua b/internal/gm-unit/editor_colors.lua index 57a7c9fde5..decebda005 100644 --- a/internal/gm-unit/editor_colors.lua +++ b/internal/gm-unit/editor_colors.lua @@ -64,7 +64,7 @@ end function Editor_Colors:random() local featureChoiceIndex, featureChoice = self.subviews.features:getSelected() -- This is the part / feature that's selected - local caste = df.creature_raw.find(self.target_unit.race).caste[self.target_unit.caste] + local caste = dfhack.units.getCasteRaw(self.target_unit) -- Nil check in case there are no features if featureChoiceIndex == nil then @@ -136,7 +136,7 @@ function Editor_Colors:featureSelected(index, choice) end function Editor_Colors:updateChoices() - local caste = df.creature_raw.find(self.target_unit.race).caste[self.target_unit.caste] + local caste = dfhack.units.getCasteRaw(self.target_unit) local choices = {} for index, colorMod in ipairs(caste.color_modifiers) do table.insert(choices, {text = colorMod.part:gsub("^%l", string.upper), mod = colorMod, index = index}) diff --git a/internal/notify/notifications.lua b/internal/notify/notifications.lua index 5273d83b8e..b06f139d90 100644 --- a/internal/notify/notifications.lua +++ b/internal/notify/notifications.lua @@ -105,10 +105,8 @@ local function for_moody(fn, reverse) end, fn, reverse) end -local races = df.global.world.raws.creatures.all - local function is_stealer(unit) - local casteFlags = races[unit.race].caste[unit.caste].flags + local casteFlags = dfhack.units.getCasteRaw(unit).flags if casteFlags.CURIOUS_BEAST_EATER or casteFlags.CURIOUS_BEAST_GUZZLER or casteFlags.CURIOUS_BEAST_ITEM @@ -224,7 +222,7 @@ end local function summarize_units(for_fn) local counts = {} for_fn(function(unit) - local names = races[unit.race].caste[unit.caste].caste_name + local names = dfhack.units.getCasteRaw(unit).caste_name local record = ensure_key(counts, names[0], {count=0, plural=names[1]}) record.count = record.count + 1 end) diff --git a/modtools/set-personality.lua b/modtools/set-personality.lua index c0136c56a5..29c966e25c 100644 --- a/modtools/set-personality.lua +++ b/modtools/set-personality.lua @@ -165,7 +165,7 @@ end -- Gets the range of the unit caste's min, average, and max value for a trait, as defined in the PERSONALITY creature tokens. function getUnitCasteTraitRange(unit, trait) - local caste = df.creature_raw.find(unit.race).caste[unit.caste] + local caste = dfhack.units.getCasteRaw(unit) local range = {} range.min = caste.personality.a[df.personality_facet_type[trait]] diff --git a/set-orientation.lua b/set-orientation.lua index 19583f7913..6b821b8fcf 100644 --- a/set-orientation.lua +++ b/set-orientation.lua @@ -155,7 +155,7 @@ function randomiseOrientation(unit, sex) return end - local caste = df.creature_raw.find(unit.race).caste[unit.caste] + local caste = dfhack.units.getCasteRaw(unit) -- Build a weighted table for use in the weighted roll function local sexname = getSexString(sex) diff --git a/uniform-unstick.lua b/uniform-unstick.lua index 9c578bc18c..af86aa8006 100644 --- a/uniform-unstick.lua +++ b/uniform-unstick.lua @@ -50,7 +50,7 @@ end local function bodyparts_that_can_wear(unit, item) local bodyparts = {} - local unitparts = df.creature_raw.find(unit.race).caste[unit.caste].body_info.body_parts + local unitparts = dfhack.units.getCasteRaw(unit).body_info.body_parts if item._type == df.item_helmst then for index, part in ipairs(unitparts) do diff --git a/unretire-anyone.lua b/unretire-anyone.lua index c977ebdfd5..0e79fccaba 100644 --- a/unretire-anyone.lua +++ b/unretire-anyone.lua @@ -50,7 +50,7 @@ function showNemesisPrompt(advSetUpScreen) not histFlags.deity and not histFlags.force then - local creature = df.creature_raw.find(histFig.race).caste[histFig.caste] + local creature = dfhack.units.getCasteRaw(histFig.race, histFig.caste) local name = creature.caste_name[0] if histFig.info and histFig.info.curse then local curse = histFig.info.curse diff --git a/workorder.lua b/workorder.lua index 7c58e785b5..c72ec99981 100644 --- a/workorder.lua +++ b/workorder.lua @@ -594,10 +594,7 @@ end -- true/false or nil if no shearable_tissue_layer with length > 0. local function canShearCreature(u) - local stls = world.raws.creatures - .all[u.race] - .caste[u.caste] - .shearable_tissue_layer + local stls = dfhack.units.getCasteRaw(u).shearable_tissue_layer local any for _, stl in ipairs(stls) do