Posted by
| Nick Gammon
Australia (23,122 posts) Bio
Forum Administrator |
Message
| Yes, reading the Lua tutorials will help you make future changes. :)
Your idea of quantities is a good one. This seems to work:
<aliases>
<alias
match="inv"
enabled="y"
send_to="12"
sequence="100"
>
<send>
require "wait"
wait.make (function () -- coroutine starts here
local win = GetPluginID () .. ":inventory"
local font = "f"
if not WindowInfo (win, 1) then
WindowCreate (win, 0, 0, 0, 0, 6, 0, 0)
WindowFont (win, font, "Lucida Console", 9)
end -- if
-- request inventory
Send "inventory"
-- wait for inventory to start
local x = wait.match ("Inventory:", 10)
if not x then
ColourNote ("white", "blue", "No inventory received within 10 seconds")
return
end -- if
local inv = {}
local max_width = WindowTextWidth (win, font, "Inventory")
default_category = "Other"
inventory_type = {
Containers = { "pack", "bag", "sack", },
Weapons = { "scepter", "sword", },
Ammo = { "arrows", "bullets", },
Jewels = { "necklace", "bangle", "bracelet", },
[default_category] = { }, -- default
} -- end of inventory_type
category_count = 0
for name in pairs (inventory_type) do
category_count = category_count + 1
end -- for
-- for duplicates
existing_items = { }
-- loop until end of inventory
while true do
local line, wildcards, styles = wait.match ("*")
-- see if end of inventory
if not string.match (line, "^ ?an?") then
if not string.match (line, "^ ?some") then
if not string.match (line, "^$") then
break
end -- if
end -- if
end -- if
if styles [1] then
line = Trim (line)
styles.line = line
styles.weight = tonumber (string.match (line, "%%(([0-9.]+) lbs?%%)"))
styles [1].text = string.gsub (styles [1].text, "^ ", "")
for category, keywords in pairs (inventory_type) do
if styles.category then
break
end -- if
for _, item in ipairs (keywords) do
if string.find (line, item, 1, true) then
styles.category = category
break
end -- if found
end -- for each item
end -- for each category
if not styles.category then
styles.category = default_category
end -- if
-- look for a duplicate
item_number = existing_items [line]
-- if found, increase its quantity
if item_number then
inv [item_number].quantity = inv [item_number].quantity + 1
else
styles.quantity = 1 -- start with one of them
existing_items [line] = #inv + 1 -- remember its new position
-- save inventory line
table.insert (inv, styles)
-- work out max width
max_width = math.max (max_width, WindowTextWidth (win, font, line))
end -- if
end -- if at least one style
end -- while loop
local font_height = WindowFontInfo (win, font, 1)
local window_width = max_width + 10
local window_height = font_height * (#inv + (category_count * 2) + 2) + 10
-- make window correct size
WindowCreate (win, 0, 0, window_width, window_height, 6, 0, ColourNameToRGB "#373737")
WindowRectOp (win, 5, 0, 0, 0, 0, 5, 15 + 0x1000)
-- heading line
WindowText (win, font, "Inventory", 5, 5, 0, 0, ColourNameToRGB "yellow")
-- draw each inventory line
local y = font_height * 2 + 5
-- sort them
function my_sort_function (a, b)
-- category less than?
if a.category < b.category then return true end
-- category greater than?
if a.category > b.category then return false end
-- name less than?
if a.line < b.line then return true end
-- name greater than?
if a.line > b.line then return false end
-- names the same, compare weights ...
-- weight less than
if a.weight and b.weight then
if a.weight < b.weight then return true end
end -- if
-- not less than
return false
end -- function
table.sort (inv, my_sort_function)
old_category = nil
for i, styles in ipairs (inv) do
if styles.category ~= old_category then
y = y + font_height
WindowText (win, font, styles.category, 5, y, 0, 0, ColourNameToRGB "mediumorchid")
old_category = styles.category
y = y + font_height
end -- if new category
local x = 5
for _, style in ipairs (styles) do
x = x + WindowText (win, font, style.text, x, y, 0, 0, style.textcolour)
if styles.quantity > 1 then
WindowText (win, font, string.format (" x%i", styles.quantity), x, y, 0, 0, ColourNameToRGB "lightgreen")
end -- if
end -- for
y = y + font_height
end -- for each inventory item
WindowShow (win, true)
end) -- end of coroutine
</send>
</alias>
</aliases>
|
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.
|
Modified code in bold. I used a separate table (existing_items) to detect duplicates. Since the weight is part of the line we don't have to test for that separately. If a line is a duplicate of an existing one, we just add one to the quantity for the existing item. Otherwise we create a new one and assign a quantity of one.
Then in the printing section we print any quantities greater than one.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|