Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------------------------------------------------------------------------
- -- autopray.lua:
- -- Sacrifice any corpse in LOS.
- --
- -- To use this, add this line to your init.txt:
- -- lua_file = lua/autopray.lua
- -- then macro a key to ===offer_all
- --
- ---------------------------------------------------------------------------
- local function delta_to_vi(dx, dy)
- local d2v = {
- [-1] = { [-1] = 'y', [0] = 'h', [1] = 'b'},
- [0] = { [-1] = 'k', [1] = 'j'},
- [1] = { [-1] = 'u', [0] = 'l', [1] = 'n'},
- }
- return d2v[dx][dy]
- end
- local function sign(a)
- return a > 0 and 1 or a < 0 and -1 or 0
- end
- local function abs(a)
- return a * sign(a)
- end
- local function try_move(dx, dy)
- m = monster.get_monster_at(dx, dy)
- -- attitude > ATT_NEUTRAL should mean you can push past the monster
- if view.is_safe_square(dx, dy) and (not m or m:attitude() > ATT_NEUTRAL) then
- delta = {}
- delta.dx = dx
- delta.dy = dy
- return delta
- else
- return nil
- end
- end
- local function move_towards(dx, dy)
- local move = nil
- if abs(dx) > abs(dy) then
- if abs(dy) == 1 then
- move = try_move(sign(dx), 0)
- end
- if move == nil then move = try_move(sign(dx), sign(dy)) end
- if move == nil then move = try_move(sign(dx), 0) end
- if move == nil and abs(dx) > abs(dy)+1 then
- move = try_move(sign(dx), 1)
- end
- if move == nil and abs(dx) > abs(dy)+1 then
- move = try_move(sign(dx), -1)
- end
- if move == nil then move = try_move(0, sign(dy)) end
- elseif abs(dx) == abs(dy) then
- move = try_move(sign(dx), sign(dy))
- if move == nil then move = try_move(sign(dx), 0) end
- if move == nil then move = try_move(0, sign(dy)) end
- else
- if abs(dx) == 1 then
- move = try_move(0, sign(dy))
- end
- if move == nil then move = try_move(sign(dx), sign(dy)) end
- if move == nil then move = try_move(0, sign(dy)) end
- if move == nil and abs(dy) > abs(dx)+1 then
- move = try_move(1, sign(dy))
- end
- if move == nil and abs(dy) > abs(dx)+1 then
- move = try_move(-1, sign(dy))
- end
- if move == nil then move = try_move(sign(dx), 0) end
- end
- if move == nil then
- crawl.mpr("Failed to move towards target.")
- else
- local vi_move = delta_to_vi(move.dx, move.dy)
- if vi_move ~= nil then
- crawl.process_keys(vi_move)
- else
- return nil
- end
- end
- return move
- end
- local function is_rotting(item)
- return string.find(item.name(), 'rotting')
- end
- local function is_skeleton(item)
- return string.find(item.name(), 'skeleton')
- end
- local function is_good_corpse(item)
- if not item then
- return false
- end
- if item.class() == 'Carrion' and not is_rotting(item) and not is_skeleton(item) then
- if you.god() ~= 'Beogh' or string.find(item.name(), 'orc ') ~= nil then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- local function is_candidate_for_offering()
- floor_items = you.floor_items()
- if not floor_items then
- return false
- end
- for i, el in ipairs(floor_items) do
- if is_good_corpse(el) then
- return true
- end
- end
- return false
- end
- local function move_to(dx, dy)
- while dx ~= 0 or dy ~= 0 do
- local move = move_towards(dx, dy)
- if move then
- coroutine.yield(true)
- dx = dx - move.dx
- dy = dy - move.dy
- else
- return false
- end
- end
- return true
- end
- local function offer()
- crawl.process_keys('p')
- coroutine.yield(true)
- end
- local function get_all_targets()
- local targets = {}
- local index = 1
- for x = -8,8 do
- for y = -8,8 do
- if items.get_item_at(x, y) then
- targets[index] = {}
- targets[index].x = x
- targets[index].y = y
- targets[index].d = (abs(x) > abs(y)) and abs(x) or abs(y)
- index = index + 1
- end
- end
- end
- return targets
- end
- function offer_all()
- if you.confused() then
- crawl.mpr("You are too confused!")
- return
- elseif caught then
- crawl.mpr("You are " .. caught .. "!")
- return
- end
- local god = you.god()
- if god ~= 'Makhleb' and god ~= 'Lugonu' and god ~= 'Okawaru'
- and god ~= 'Trog' and god ~= 'Beogh' then
- -- if god ~= 'Nemelex Xobeh' and god ~= 'Elyvilon' then
- -- crawl.mpr("You can't offer anything.")
- -- end
- return
- end
- local targets = get_all_targets()
- if table.getn(targets) < 1 then
- return
- end
- table.sort(targets, function (a, b) return (a.d < b.d) end)
- local last_move = {x=0, y=0}
- crawl.more_autoclear(true)
- for _,t in ipairs(targets) do
- if move_to(t.x - last_move.x, t.y - last_move.y) then
- if is_candidate_for_offering() then
- offer()
- end
- if is_candidate_for_offering() then
- -- avoid infinite loop
- return
- end
- last_move.x = t.x
- last_move.y = t.y
- else
- -- pathing problems? return to initial position, and try again
- if move_to(-last_move.x, -last_move.y) then
- if move_to(t.x, t.y) then
- if is_candidate_for_offering() then
- offer()
- end
- if is_candidate_for_offering() then
- -- avoid infinite loop
- return
- end
- last_move.x = t.x
- last_move.y = t.y
- else
- -- can't go to the initial pos
- return
- end
- else
- -- can't go to t
- return
- end
- end
- end
- crawl.more_autoclear(false)
- end
- function autopray_interrupt_macro(interrupt_name)
- return interrupt_name == "hp_loss" or interrupt_name == "stat" or
- interrupt_name == "monster" or interrupt_name == "force"
- end
- -- Add a macro interrupt hook so that we don't get stopped by any old interrupt
- chk_interrupt_macro.offer_all = autopray_interrupt_macro
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement