Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 60 additions & 11 deletions lua/neohack/actions.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local chat = require("neohack.chat")
local inventory = require("neohack.inventory")
local M = {
leader_key = nil,
tick = nil,
Expand All @@ -13,7 +14,7 @@ local utils = require("neohack.utils")
M.actions = {
---comment
inventory = function()
message.notify("Level " .. state.current_floor .. " " .. player.show_inventory())
message.notify("You have " .. player.get_inventory())
end,

---comment
Expand Down Expand Up @@ -61,7 +62,7 @@ M.actions = {
wait = function(request)
local count = utils.string_to_int(request.object)
if count then
message.notify("Waiting " .. count .. " turns")
message.notify(utils.capitalize_first_letter(request.action) .. "ing " .. count .. " turns")
for _ = 1, count do
M.tick()
end
Expand All @@ -87,6 +88,42 @@ M.actions = {
end,
}

M.synonyms = {
h = "help",
["?"] = "help",
["-h"] = "help",
["-help"] = "help",
["--help"] = "help",

i = "inventory",
items = "inventory",

k = "kick",

w = "wear",
equip = "wear",
wield = "wear",

f = "fuse",

l = "look",
read = "look",
examine = "look",

e = "eat",

d = "drop",

s = "say",
speak = "say",
yell = "say",
intone = "say",

sleep = "wait",
sit = "wait",
rest = "wait",
}

---Insert the action command
---@param action_string string
M.insert_action = function(action_string)
Expand All @@ -104,7 +141,7 @@ end

---comment
M.prompt_wear = function()
message.notify("You have:\n0:nothing\n" .. player.get_inventory_item_with_index())
message.notify("You have:\n0:nothing\n" .. inventory.get_inventory_item_with_index())
vim.defer_fn(function()
local object = M.prompt_one_word("Wear what?")
if object then
Expand All @@ -119,7 +156,7 @@ end

---comment
M.prompt_fuse = function()
message.notify("You have:\n" .. player.get_inventory_item_with_index())
message.notify("You have:\n" .. inventory.get_inventory_item_with_index())
vim.defer_fn(function()
local index = M.prompt("Fuse what?")
if index then
Expand All @@ -134,7 +171,7 @@ end

---comment
M.prompt_look = function()
message.notify("You have:\n0:self\n" .. player.get_inventory_item_with_index())
message.notify("You have:\n0:self\n" .. inventory.get_inventory_item_with_index())
vim.defer_fn(function()
local index = M.prompt_one_word("Look at what?")
if index then
Expand All @@ -146,7 +183,7 @@ end

---comment
M.prompt_eat = function()
message.notify("You have:\n" .. player.get_inventory_item_with_index())
message.notify("You have:\n" .. inventory.get_inventory_item_with_index())
vim.defer_fn(function()
local index = M.prompt_one_word("Eat what?")
if index then
Expand All @@ -158,7 +195,7 @@ end

---comment
M.prompt_drop = function()
message.notify("You have:\n" .. player.get_inventory_item_with_index())
message.notify("You have:\n" .. inventory.get_inventory_item_with_index())
vim.defer_fn(function()
local index = M.prompt_one_word("Drop what?")
if index then
Expand Down Expand Up @@ -193,17 +230,29 @@ M.prompt_wait = function()
end, 50)
end

---parse a string into performing an action
---parse a string into an action and the request
---@param inserted_chars string
---@return function?
---@return Request?
M.parse_action = function(inserted_chars)
local request = chat.parse_request(inserted_chars)
if request == nil then
return
return nil, nil
end
local action = M.actions[request.action]
local action_word = M.synonyms[request.action] or request.action
local action = M.actions[action_word]
if action == nil then
chat.no_action(request)
return chat.no_action, request
else
return action, request
end
end

---perform an action from free text
---@param inserted_chars string
M.execute_action = function(inserted_chars)
local action, request = M.parse_action(inserted_chars)
if action then
action(request)
end
end
Expand Down
Loading
Loading