[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Getting Started
. . -> [Subject]  Video showing how to make an inventory alias
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Video showing how to make an inventory alias

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Pages: 1  2  3  4  5  6  7  8  9  10 

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #135 on Mon 22 Sep 2014 10:31 PM (UTC)
Message
Ah yes, I didn't allow for the x22 being at the end of the longest line. I amended the code below to handle that better. Now it allows for the maximum quantity to be at the end of the longest line. It isn't perfect, because the longest line might have only one of them, but the worst that happens if you have a bit of a gap on the right.

Quote:

It's also printing the quantity twice which is odd because it doesn't look like it should be.


Oops, I had printing the quantity in the inner loop, so you must have had two style runs whereas I only had one in my test. I moved the quantity print outside the loop. That should work better.


<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 = { }
max_quantity = 1

-- 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
      max_quantity = math.max (max_quantity, inv [item_number].quantity)
    else
      styles.quantity = 1  -- start with one of them
      existing_items [line] = #inv + 1  -- remember its new position
      -- save inventory line
      inv [#inv + 1] = 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

max_quantity_digits = math.floor (math.log10 (max_quantity)) + 1

if max_quantity &gt; 1 then
  max_width = max_width + WindowTextWidth (win, font, string.format (" x%s", string.rep ("9", max_quantity_digits)))
end -- if

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 &lt; b.category then return true end
  -- category greater than?
  if a.category &gt; b.category then return false end
  -- name less than?
  if a.line &lt; b.line then return true end
  -- name greater than?
  if a.line &gt; b.line then return false end
  -- names the same, compare weights ...
  -- weight less than
  if a.weight and b.weight then
    if a.weight &lt; 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)
  end -- for
  if styles.quantity &gt; 1 then
    WindowText (win, font, string.format (" x%i", styles.quantity), x, y, 0, 0, ColourNameToRGB  "lightgreen")
  end -- if
  y = y + font_height

end -- for each inventory item

WindowShow (win, true)


end)   -- end of coroutine
</send>
  </alias>
</aliases>



Template:pasting For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Shady Stranger   USA  (45 posts)  [Biography] bio
Date Reply #136 on Tue 23 Sep 2014 10:46 PM (UTC)
Message
Thank you so much for this. It works great.
[Go to top] top

Posted by Shady Stranger   USA  (45 posts)  [Biography] bio
Date Reply #137 on Fri 26 Sep 2014 11:24 PM (UTC)
Message
I swear this is the last thing on this one...

I have been playing around with the alias and have been trying to send the same info to the output window with ColourNote but it is only listing the items with quantities and putting an extra "0" too, like this:


Inventory:

Ammo
a small quiver of oak arrows (0.0 lbs) 0 x8
a small quiver of poplar arrows (0.0 lbs) 0 x13
Containers
Other
Resources


continued in next post...
[Go to top] top

Posted by Shady Stranger   USA  (45 posts)  [Biography] bio
Date Reply #138 on Fri 26 Sep 2014 11:24 PM (UTC)
Message
...continued

I put some lines in bold to note the changes I made to print in the output window.


<aliases>
  <alias
   match="inv"
   enabled="y"
   group="inventory"
   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", 6)  
end -- if

-- request inventory

Send "inventory"


-- wait for inventory to start

local x = wait.match ("Inventory:", 10, trigger_flag.OmitFromOutput)

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", "backpack", "box", "chest", },
  Weapons    = { "scepter", "sword", "dirk", "dagger", },
  Ammo       = { "arrows", "bullets", "darts", },
  Jewelry    = { "necklace", "bangle", "bracelet", "ring", },
  Resources  = { "bundle", "salt", },
  Armor      = { "cuirass", "chestplate", "armor", },

  [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 = { }
max_quantity = 1

-- loop until end of inventory

while true do
  local line, wildcards, styles = wait.match ("*", 0, trigger_flag.OmitFromOutput)


 -- 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
      max_quantity = math.max (max_quantity, inv [item_number].quantity)
    else
      styles.quantity = 1  -- start with one of them
      existing_items [line] = #inv + 1  -- remember its new position
      -- save inventory line
      inv [#inv + 1] = 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

max_quantity_digits = math.floor (math.log10 (max_quantity)) + 1

if max_quantity &gt; 1 then
  max_width = max_width + WindowTextWidth (win, font, string.format (" x%s", string.rep ("9", max_quantity_digits)))
end -- if

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")
  ColourNote ("yellow", "black", "Inventory:")
-- draw each inventory line

local y = font_height * 2 + 5

-- sort them

function my_sort_function (a, b)

  -- category less than?
  if a.category &lt; b.category then return true end
  -- category greater than?
  if a.category &gt; b.category then return false end
  -- name less than?
  if a.line &lt; b.line then return true end
  -- name greater than?
  if a.line &gt; b.line then return false end
  -- names the same, compare weights ...
  -- weight less than
  if a.weight and b.weight then
    if a.weight &lt; 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  "lightgray")
    ColourNote ("lightgray", "", (styles.category))
    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)
  end -- for
  if styles.quantity &gt; 1 then
    WindowText (win, font, string.format (" x%i", styles.quantity), x, y, 0, 0, ColourNameToRGB  "lightgreen")
    ColourNote ("mediumorchid", "", (styles.line .. " " .. styles.weight), "lightgreen", "", (string.format (" x%i", styles.quantity)))

  end -- if
  y = y + font_height

end -- for each inventory item

WindowShow (win, true)

end)   -- end of coroutine


</send>
  </alias>
</aliases>


I am curious why it is not displaying the items with a quantity of 1 as well as why that "0" is showing up at the end of the item.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #139 on Sat 27 Sep 2014 12:52 AM (UTC)

Amended on Sat 27 Sep 2014 12:53 AM (UTC) by Nick Gammon

Message
Try this variation:


<aliases>
  <alias
   match="inv"
   enabled="y"
   group="inventory"
   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", 6)  
end -- if

-- request inventory

Send "inventory"


-- wait for inventory to start

local x = wait.match ("Inventory:", 10, trigger_flag.OmitFromOutput)

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", "backpack", "box", "chest", },
  Weapons    = { "scepter", "sword", "dirk", "dagger", },
  Ammo       = { "arrows", "bullets", "darts", },
  Jewelry    = { "necklace", "bangle", "bracelet", "ring", },
  Resources  = { "bundle", "salt", },
  Armor      = { "cuirass", "chestplate", "armor", },

  [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 = { }
max_quantity = 1

-- loop until end of inventory

while true do
  local line, wildcards, styles = wait.match ("*", 0, trigger_flag.OmitFromOutput)


 -- 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
      max_quantity = math.max (max_quantity, inv [item_number].quantity)
    else
      styles.quantity = 1  -- start with one of them
      existing_items [line] = #inv + 1  -- remember its new position
      -- save inventory line
      inv [#inv + 1] = 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

max_quantity_digits = math.floor (math.log10 (max_quantity)) + 1

if max_quantity &gt; 1 then
  max_width = max_width + WindowTextWidth (win, font, string.format (" x%s", string.rep ("9", max_quantity_digits)))
end -- if

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")
ColourNote ("yellow", "black", "Inventory:")
-- draw each inventory line

local y = font_height * 2 + 5

-- sort them

function my_sort_function (a, b)

  -- category less than?
  if a.category &lt; b.category then return true end
  -- category greater than?
  if a.category &gt; b.category then return false end
  -- name less than?
  if a.line &lt; b.line then return true end
  -- name greater than?
  if a.line &gt; b.line then return false end
  -- names the same, compare weights ...
  -- weight less than
  if a.weight and b.weight then
    if a.weight &lt; 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  "lightgray")
    ColourNote ("lightgray", "", (styles.category))
    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)
    ColourTell (style.textcolour, style.backcolour, style.text)
  end -- for
  if styles.quantity &gt; 1 then
    WindowText (win, font, string.format (" x%i", styles.quantity), x, y, 0, 0, ColourNameToRGB  "lightgreen")
    ColourTell ("lightgreen", "", string.format (" x%i", styles.quantity))
  end -- if
  print ""  -- finish off line
  y = y + font_height

end -- for each inventory item

WindowShow (win, true)

end)   -- end of coroutine


</send>
  </alias>
</aliases>



You were only doing a ColourNote if the quantity was > 1 so naturally the other lines did not appear. I've put a ColourTell into the styles loop, which will preserve the style of the original (ie. the colour). I don't know about that zero, but it probably came from this:


styles.line .. " " .. styles.weight


The weight would have been zero (ie. not bold, underlined, etc.)

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


113,031 views.

This is page 10, subject is 10 pages long:  [Previous page]  1  2  3  4  5  6  7  8  9  10 

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]