Advertisement
CommanderC

autopray.lua v2

Apr 22nd, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.65 KB | None | 0 0
  1. ---------------------------------------------------------------------------
  2. -- autopray.lua:
  3. -- Sacrifice any corpse in LOS.
  4. --
  5. -- To use this, add this line to your init.txt:
  6. --   lua_file = lua/autopray.lua
  7. -- then macro a key to ===offer_all
  8. --
  9. ---------------------------------------------------------------------------
  10.  
  11. AUTOPRAY_EXPLORE = false
  12.  
  13. local function delta_to_vi(dx, dy)
  14.   local d2v = {
  15.     [-1] = { [-1] = 'y', [0] = 'h', [1] = 'b'},
  16.     [0]  = { [-1] = 'k',            [1] = 'j'},
  17.     [1]  = { [-1] = 'u', [0] = 'l', [1] = 'n'},
  18.   }
  19.   return d2v[dx][dy]
  20. end
  21.  
  22. local function sign(a)
  23.   return a > 0 and 1 or a < 0 and -1 or 0
  24. end
  25.  
  26. local function abs(a)
  27.   return a * sign(a)
  28. end
  29.  
  30. local function try_move(dx, dy)
  31.   m = monster.get_monster_at(dx, dy)
  32.   -- attitude > ATT_NEUTRAL should mean you can push past the monster
  33.   if view.is_safe_square(dx, dy) and (not m or m:attitude() > ATT_NEUTRAL) then
  34.     delta = {}
  35.     delta.dx = dx
  36.     delta.dy = dy
  37.     return delta
  38.   else
  39.     return nil
  40.   end
  41. end
  42.  
  43. local function move_towards(dx, dy)
  44.   local move = nil
  45.   if abs(dx) > abs(dy) then
  46.     if abs(dy) == 1 then
  47.       move = try_move(sign(dx), 0)
  48.     end
  49.     if move == nil then move = try_move(sign(dx), sign(dy)) end
  50.     if move == nil then move = try_move(sign(dx), 0) end
  51.     if move == nil and abs(dx) > abs(dy)+1 then
  52.       move = try_move(sign(dx), 1)
  53.     end
  54.     if move == nil and abs(dx) > abs(dy)+1 then
  55.       move = try_move(sign(dx), -1)
  56.     end
  57.     if move == nil then move = try_move(0, sign(dy)) end
  58.   elseif abs(dx) == abs(dy) then
  59.     move = try_move(sign(dx), sign(dy))
  60.     if move == nil then move = try_move(sign(dx), 0) end
  61.     if move == nil then move = try_move(0, sign(dy)) end
  62.   else
  63.     if abs(dx) == 1 then
  64.       move = try_move(0, sign(dy))
  65.     end
  66.     if move == nil then move = try_move(sign(dx), sign(dy)) end
  67.     if move == nil then move = try_move(0, sign(dy)) end
  68.     if move == nil and abs(dy) > abs(dx)+1 then
  69.       move = try_move(1, sign(dy))
  70.     end
  71.     if move == nil and abs(dy) > abs(dx)+1 then
  72.       move = try_move(-1, sign(dy))
  73.     end
  74.     if move == nil then move = try_move(sign(dx), 0) end
  75.   end
  76.   if move == nil then
  77.     crawl.mpr("Failed to move towards target.")
  78.   else
  79.     local vi_move = delta_to_vi(move.dx, move.dy)
  80.     if vi_move ~= nil then
  81.       crawl.process_keys(vi_move)
  82.     else
  83.       return nil    
  84.     end
  85.   end
  86.   return move
  87. end
  88.  
  89. local function is_rotting(item)
  90.   return string.find(item.name(), 'rotting')
  91. end
  92.  
  93. local function is_skeleton(item)
  94.    return string.find(item.name(), 'skeleton')
  95. end
  96.  
  97. local function is_good_corpse(item)
  98.   if not item then
  99.     return false
  100.   end
  101.   if item.class() == 'Carrion' and not is_rotting(item) and not is_skeleton(item) then
  102.     if you.god() ~= 'Beogh' or string.find(item.name(), 'orc ') ~= nil then
  103.        return true
  104.     else
  105.        return false
  106.     end
  107.   else
  108.     return false
  109.   end
  110. end
  111.  
  112. local function is_candidate_for_offering(x, y)
  113.   floor_items = items.get_items_at(x, y)
  114.   if not floor_items then
  115.     return false
  116.   end
  117.   for i, el in ipairs(floor_items) do
  118.     if is_good_corpse(el) then
  119.       return true
  120.     end
  121.   end
  122.   return false
  123. end
  124.  
  125. local function move_to(dx, dy)
  126.   while dx ~= 0 or dy ~= 0 do
  127.      local move = move_towards(dx, dy)
  128.      if move then
  129.        coroutine.yield(true)
  130.        dx = dx - move.dx
  131.        dy = dy - move.dy
  132.      else
  133.        return false
  134.      end
  135.   end
  136.   return true
  137. end
  138.  
  139. local function offer()
  140.    crawl.process_keys('p')
  141.    coroutine.yield(true)
  142. end
  143.  
  144. local function get_all_targets()
  145.   local targets = {}
  146.   local index = 1
  147.   for x = -8,8 do
  148.     for y = -8,8 do
  149.       if is_candidate_for_offering(x, y) then
  150.         targets[index] = {x=x, y=y}
  151.         targets[index].d = (abs(x) > abs(y)) and abs(x) or abs(y)        
  152.         index = index + 1
  153.       end
  154.     end
  155.   end
  156.   return targets
  157. end
  158.  
  159. local function set_ap_explore(key, value)
  160.    AUTOPRAY_EXPLORE = string.lower(value) ~= false
  161. end
  162.  
  163. local function do_offer()
  164.   table.sort(targets, function (a, b) return (a.d < b.d) end)
  165.   local last_move = {x=0, y=0}
  166.  
  167.   crawl.more_autoclear(true)
  168.   for _,t in ipairs(targets) do
  169.     if move_to(t.x - last_move.x, t.y - last_move.y) then
  170.       offer()
  171.  
  172.       last_move.x = t.x
  173.       last_move.y = t.y
  174.     else
  175.       -- pathing problems? return to initial position, and try again
  176.       if move_to(-last_move.x, -last_move.y) then
  177.         if move_to(t.x, t.y) then
  178.           offer()
  179.           last_move.x = t.x
  180.           last_move.y = t.y
  181.         else
  182.           -- can't go to the initial pos
  183.           return
  184.         end
  185.        else
  186.         -- can't go to t
  187.         return
  188.       end
  189.     end
  190.   end
  191.   crawl.more_autoclear(false)
  192. end
  193.  
  194. function offer_all()
  195.   if you.confused() then
  196.     crawl.mpr("You are too confused!")
  197.     return
  198.   elseif caught then
  199.     crawl.mpr("You are " .. caught .. "!")
  200.     return
  201.  end
  202.  
  203.   local god = you.god()
  204.   if god == 'Makhleb' or god == 'Lugonu' or god == 'Okawaru'
  205.      and god == 'Trog' and god == 'Beogh' then
  206.     local targets = get_all_targets()
  207.     if table.getn(targets) > 0 then
  208.       do_offer()
  209.     end
  210.   end  
  211.   if AUTOPRAY_EXPLORE then
  212.     crawl.process_keys('o')
  213.     --coroutine.yield(true)
  214.   end
  215. end
  216.  
  217. function autopray_interrupt_macro(interrupt_name)
  218.     return interrupt_name == "hp_loss" or interrupt_name == "stat" or
  219.            interrupt_name == "monster" or interrupt_name == "force"
  220. end
  221.  
  222. -- Add a macro interrupt hook so that we don't get stopped by any old interrupt
  223. chk_interrupt_macro.offer_all = autopray_interrupt_macro
  224.  
  225. chk_lua_option.autopray_explore = set_ap_explore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement