Hiranus

DFHack religion

Feb 24th, 2024
958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.97 KB | None | 0 0
  1. local argparse = require('argparse')
  2.  
  3. local MyCivId = df.global.world.world_data.active_site[0].entity_links[0].entity_id
  4.  
  5. local options = {
  6.   mode = "",
  7.   allunits = false,
  8. }
  9. local args = { ... }
  10.  
  11. local function ValidateMode(arg)
  12.   local argLower = string.lower(arg);
  13.   local validModes = {
  14.     ["atheism"] = true,
  15.     ["monotheism"] = true
  16.   }
  17.   if not validModes[argLower] then
  18.     qerror('Invalid mode')
  19.   end
  20.   options.mode = argLower
  21. end
  22.  
  23. argparse.processArgsGetopt(args, {
  24.   { 'm', 'mode', handler = ValidateMode,                             hasArg = true },
  25.   { 'a', 'all',  handler = function(arg) options.allunits = true end },
  26. })
  27.  
  28. local function GetGodToLeave(needs, histFigLinks)
  29.   if options.mode == "monotheism" then
  30.     --check if needs correpond to hist flag links
  31.     for _, link in pairs(histFigLinks) do
  32.       for _, need in pairs(needs) do
  33.         if link.target_hf == need.deity_id then
  34.           return need.deity_id
  35.         end
  36.       end
  37.     end
  38.     --no links or unit is not hist fig, pick first pray need
  39.     if needs[1] then
  40.       return needs[1].deity_id
  41.     end
  42.   end
  43. end
  44.  
  45. local function CleanupReligion(histfigId, religion)
  46.   --remove all mentions of this citizen in religion, making it ass if he never belonged here
  47.   for i = #religion.histfig_ids - 1, 0, -1 do
  48.     if religion.histfig_ids[i] == histfigId then
  49.       religion.histfig_ids:erase(i)
  50.     end
  51.   end
  52.   for i = #religion.hist_figures - 1, 0, -1 do
  53.     if religion.hist_figures[i].id == histfigId then
  54.       religion.hist_figures:erase(i)
  55.     end
  56.   end
  57. end
  58.  
  59. local function TableCleanup(tab, valsToRemove, checkFunc, useDelete)
  60.   for i = #tab - 1, 0, -1 do
  61.     for _, val in pairs(valsToRemove) do
  62.       if checkFunc(tab[i], val) then
  63.         local todel = tab[i]
  64.         tab:erase(i)
  65.         if useDelete then
  66.           todel:delete()
  67.         end
  68.         break
  69.       end
  70.     end
  71.   end
  72. end
  73.  
  74. local function CleanupReligions(histfig, needGod)
  75.   local religions = {}
  76.   local religionToLeave
  77.   --find all dwarf religions
  78.   for _, link in pairs(histfig.entity_links) do
  79.     local ent = df.historical_entity.find(link.entity_id)
  80.     if ent.type == 5 then -- religion
  81.       table.insert(religions, ent)
  82.       --if we will leave one religion we need to make sure that the religion matches a god
  83.       if not religionToLeave and needGod then
  84.         for _, god in pairs(ent.relations.deities) do
  85.           if needGod == god then
  86.             religionToLeave = ent.id
  87.           end
  88.         end
  89.       end
  90.     end
  91.   end
  92.   for _, rel in pairs(religions) do
  93.     if religionToLeave ~= rel.id then
  94.       CleanupReligion(histfig.id, rel)
  95.       local id
  96.       for key, value in pairs(histfig.entity_links) do
  97.         if value.entity_id == rel.id then
  98.           id = key
  99.         end
  100.       end
  101.       local del = histfig.entity_links[id]
  102.       histfig.entity_links:erase(id)
  103.       del:delete()
  104.     end
  105.   end
  106. end
  107.  
  108. local function ProceessUnit(dwarf)
  109.   --Needs to be checked for tamed sentients and other similar cases
  110.   if not (dwarf and dwarf.civ_id == MyCivId and dfhack.units.isCitizen(dwarf)) then
  111.     return
  112.   end
  113.   local needTargets = {}
  114.   local histfigLinkTargets = {}
  115.  
  116.   for _, need in pairs(dwarf.status.current_soul.personality.needs) do
  117.     if need.id == 2 then
  118.       table.insert(needTargets,
  119.         {
  120.           deity_id = need.deity_id,
  121.           focus_level = need.focus_level,
  122.           need_level = need.need_level,
  123.         })
  124.     end
  125.   end
  126.   local dwarf_hist_fig
  127.  
  128.   if dwarf.hist_figure_id ~= -1 then
  129.     dwarf_hist_fig = df.historical_figure.find(dwarf.hist_figure_id)
  130.   end
  131.  
  132.   if dwarf_hist_fig then
  133.     for _, histfig_link in pairs(dwarf_hist_fig.histfig_links) do
  134.       if histfig_link._type == df.histfig_hf_link_deityst then
  135.         table.insert(histfigLinkTargets,
  136.           { target_hf = histfig_link.target_hf, link_strength = histfig_link.link_strength })
  137.       end
  138.     end
  139.   end
  140.  
  141.   local needGod = GetGodToLeave(needTargets, histfigLinkTargets)
  142.  
  143.   TableCleanup(dwarf.status.current_soul.personality.needs, needTargets,
  144.     function(elem, target)
  145.       return (needGod == nil or needGod ~= target.deity_id) and
  146.           elem.id == 2 and
  147.           elem.deity_id == target.deity_id
  148.     end, false)
  149.  
  150.   if dwarf_hist_fig then
  151.     TableCleanup(dwarf_hist_fig.histfig_links, histfigLinkTargets,
  152.       function(elem, target)
  153.         return (needGod == nil or needGod ~= target.target_hf) and
  154.             elem._type == df.histfig_hf_link_deityst and
  155.             target.target_hf == elem.target_hf
  156.       end, true)
  157.     CleanupReligions(dwarf_hist_fig, needGod)
  158.   end
  159. end
  160.  
  161. if not dfhack.isMapLoaded() then
  162.   qerror('This script requires a fortress map to be loaded')
  163. end
  164.  
  165. if options.allunits then
  166.   for i, unit in pairs(df.global.world.units.all) do
  167.     ProceessUnit(unit)
  168.   end
  169. else
  170.   local unit = dfhack.gui.getSelectedUnit()
  171.   if not unit then
  172.     qerror("Select a unit and run the script again.")
  173.   end
  174.   ProceessUnit(unit)
  175. end
  176.  
Advertisement
Add Comment
Please, Sign In to add comment