summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source
diff options
context:
space:
mode:
authorRobert Vollmert <rvollmert@gmx.net>2009-11-17 17:28:25 +0100
committerRobert Vollmert <rvollmert@gmx.net>2009-11-17 17:33:50 +0100
commit840bf56930dfa95308cf3aa9e77e2051868ac568 (patch)
tree287e761027ca34a53ec49c26b9fe57e8b37813fb /crawl-ref/source
parenta387822ef4e74569e5a4df906efdefd739511f4a (diff)
downloadcrawl-ref-840bf56930dfa95308cf3aa9e77e2051868ac568.tar.gz
crawl-ref-840bf56930dfa95308cf3aa9e77e2051868ac568.zip
Add one-key-fighting.
Adds dat/lua/autofight.lua, which provides the macro-able hit_closest. It's quite primitive but could easily be improved.
Diffstat (limited to 'crawl-ref/source')
-rw-r--r--crawl-ref/source/dat/lua/autofight.lua81
1 files changed, 81 insertions, 0 deletions
diff --git a/crawl-ref/source/dat/lua/autofight.lua b/crawl-ref/source/dat/lua/autofight.lua
new file mode 100644
index 0000000000..70e5e044d3
--- /dev/null
+++ b/crawl-ref/source/dat/lua/autofight.lua
@@ -0,0 +1,81 @@
+---------------------------------------------------------------------------
+-- autofight.lua:
+-- One-key fighting.
+--
+-- To use this, add this line to your init.txt:
+-- lua_file = lua/autofight.lua
+-- Then macro any key to "===hit_closest".
+--
+-- This uses the very incomplete client monster and view bindings, and
+-- is currently very primitive. Improvements welcome!
+---------------------------------------------------------------------------
+
+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)
+ if view.feature_at(dx, dy) == "floor" then
+ return delta_to_vi(dx, dy)
+ else
+ return ""
+ end
+end
+
+local function move_towards(dx, dy)
+ local move = ""
+ if abs(dx) > abs(dy) then
+ move = try_move(sign(dx), 0)
+ if move == "" then move = try_move(sign(dx), sign(dy)) end
+ if move == "" then move = try_move(0, sign(dy)) end
+ elseif abs(dx) == abs(dy) then
+ move = try_move(sign(dx), sign(dy))
+ if move == "" then move = try_move(sign(dx), 0) end
+ if move == "" then move = try_move(0, sign(dy)) end
+ else
+ move = try_move(0, sign(dy))
+ if move == "" then move = try_move(sign(dx), sign(dy)) end
+ if move == "" then move = try_move(sign(dx), 0) end
+ end
+ if move == "" then
+ crawl.mpr("failed to move towards target")
+ else
+ crawl.process_keys(move)
+ end
+end
+
+local function find_next_monster()
+ local r, x, y
+ for r = 1,8 do
+ for x = -r,r do
+ for y = -r,r do
+ if monster.get_monster_at(x, y) then
+ return x, y
+ end
+ end
+ end
+ end
+ return 0, 0
+end
+
+function hit_closest()
+ local x, y = find_next_monster()
+ if x == 0 and y == 0 then
+ crawl.mpr("couldn't find monster")
+ else
+ move_towards(x, y)
+ end
+end