Guest User

Adventurer bodyswap for DFHack

a guest
Aug 19th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. -- Swap adventurer
  2.  
  3. local help = [====[
  4.  
  5. adv-bodyswap
  6. ==============
  7. Swap adventurer with selected (`l`ook) unit
  8. ]====]
  9.  
  10. function getCreatureAtPos(x,y,z) -- gets the creature index @ x,y,z coord
  11.     --local x,y,z=getxyz() --get 'X' coords
  12.     local vector=df.global.world.units.all -- load all creatures
  13.     for i = 0, #vector-1 do -- look into all creatures offsets
  14.         local curpos=vector[i].pos --get its coordinates
  15.         local cx=curpos.x
  16.         local cy=curpos.y
  17.         local cz=curpos.z
  18.         if cx==x and cy==y and cz==z then --compare them
  19.             return vector[i] --return index
  20.         end
  21.     end
  22.     print("Creature not found!")
  23.     return nil
  24.    
  25. end
  26.  
  27. function change_adv2(unit,nemesis)
  28.     if nemesis==nil then
  29.         nemesis=true --default value is nemesis switch too.
  30.     end
  31.     if unit==nil then
  32.         unit=getCreatureAtPos(getxyz())    --getCreatureAtPointer()--getSelectedUnit()
  33.     end
  34.     if unit==nil then
  35.         error("Invalid unit!")
  36.     end
  37.     local other=df.global.world.units.active
  38.     local unit_indx
  39.     for k,v in pairs(other) do
  40.         if v==unit then
  41.             unit_indx=k
  42.             break
  43.         end
  44.     end
  45.     if unit_indx==nil then
  46.         error("Unit not found in array?!") --should not happen
  47.     end
  48.     other[unit_indx]=other[0]
  49.     other[0]=unit
  50.     if nemesis then --basicly copied from advtools plugin...
  51.         local nem=dfhack.units.getNemesis(unit)
  52.         local other_nem=dfhack.units.getNemesis(other[unit_indx])
  53.         if other_nem then
  54.             other_nem.flags[0]=false
  55.             other_nem.flags[1]=true
  56.         end
  57.         if nem then
  58.             nem.flags[0]=true
  59.             nem.flags[2]=true
  60.             for k,v in pairs(df.global.world.nemesis.all) do
  61.                 if v.id==nem.id then
  62.                     df.global.ui_advmode.player_id=k
  63.                 end
  64.             end
  65.         else
  66.             qerror("Current unit does not have nemesis record, further working not guaranteed")
  67.         end
  68.     end
  69. end
  70.  
  71.  
  72. function change_adv3(unit,nemesis)
  73.     if nemesis==nil then
  74.         nemesis=true --default value is nemesis switch too.
  75.     end
  76.     if unit==nil then
  77.         error("No unit!")   --getCreatureAtPointer()--getSelectedUnit()
  78.     end
  79.     if unit==nil then
  80.         error("Invalid unit!")
  81.     end
  82.     local units=df.global.world.units.active
  83.     local unit_indx
  84.     for k,v in pairs(units) do
  85.         if v==unit then
  86.             unit_indx=k
  87.             break
  88.         end
  89.     end
  90.     if unit_indx==nil then
  91.         error("Unit not found in array?!") --should not happen
  92.     end
  93.     units[unit_indx]=units[0]
  94.     units[0]=unit
  95.     if nemesis then --basicly copied from advtools plugin...
  96.         local old_nem = dfhack.units.getNemesis(units[unit_indx])
  97.         local new_nem=dfhack.units.getNemesis(unit)
  98.         if old_nem==nil or new_nem==nil then
  99.             error("Not have both nemesis")
  100.         end
  101.         if old_nem then
  102.             old_nem.flags[0]=false -- No more adventurer
  103.             old_nem.flags[1]=true -- However, retired adventurer
  104.         end
  105.         if new_nem then
  106.             new_nem.flags[0]=true -- Now current adventurer
  107.             new_nem.flags[1]=false -- Now more considered as retired
  108.             new_nem.flags[2]=true -- Now is adventurer in legends
  109.  
  110.             -- Now change player
  111.             for k,v in pairs(df.global.world.nemesis.all) do
  112.                 if v.id==new_nem.id then
  113.                     df.global.ui_advmode.player_id=k
  114.                 end
  115.             end
  116.         else
  117.             qerror("Current unit does not have nemesis record, further working not guaranteed")
  118.         end
  119.     end
  120. end
  121.  
  122.  
  123.  
  124. local other_unit = dfhack.gui.getSelectedUnit()
  125.  
  126. change_adv3(other_unit, true)
Add Comment
Please, Sign In to add comment