Miniwindow similar to inventory capture

Posted by Dacrian on Mon 01 Feb 2010 10:03 PM — 12 posts, 44,352 views.

Australia #0
Edit: See below :)
Amended on Thu 11 Mar 2010 09:33 PM by Dacrian
Australia #1
ok, so I watched the YouTube tutorial thing (http://gammon.com.au/forum/bbshowpost.php?id=9965). Wonderful! I probably should have done that first! I posted this in that thread, but it hasn't been noticed, I think, so I will delete it and re-post it here.

I used the video (typed out the script as I watched) to create practically the same thing, except I want to capture lines that appear after I send an alias to count a whole bunch of components in a container. I can configure the string match that is stopping the loop to match a certain phrase (eg. You close His Mouth) however I can't get the script to write anything in the miniwindow.

Here's the code I'm using (practically identical to the one in the vid, written above):


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) -- font selection for winiwindow
end -- if

-- request Components

Send "comps"

-- wait for component listing

local x = wait.match ("You open His Mouth.", 10)

if not x then
  ColourNote ("white", "blue", "No components list within 10 seconds")
  return
end -- if

local inv = {}
local max_width = WindowTextWidth (win, font, "Components")


-- loop until end of components

while true do
  local line, wildcards, style = wait.match ("*")
  
  -- see if ended yet
  if not string.match (line, "^You count") then
    ColourNote ("white", "blue", "Missed 'You count'") -- To notify if something breaks it, eg. no component there
    break
  end -- if

  -- save line in table
  table.insert (inv, styles)
  -- work out max width needed
  max_width = math.max (max_width, WindowTextWidth (win, font, line))

end -- while loop

local font_height = WindowFontInfo (win, font, 1)

local window_width = max_width + 10
local window_height = font_height * (#inv + 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, "Components", 5, 5, 0, 0, ColourNameToRGB "yellow")

--draw each inventory line
local y = font_height * 2 + 5


for i, styles in pairs (inv) do
  local x = 5
  for _, styles in ipairs (styles) do
    x = x + WindowText (win, font, style.text, x, y, 0, 0, style.textcolour)
  end -- for
  y = y + font_height
end -- for

WindowShow (win, true)



end) --end of coroutine


"comps" is the (MUD side) alias that lists the components (I couldn't work out how to call the client side alias called "listcomps" so I just did it this way), however the problem I have is that Discworld MUD echoes back "Queued command: [command]" when you send a whole bunch of commands. So obviously, this was triggering the "if not" end loop. Fixed that by matching the "You close His Mouth." phrase instead of not matching the "You count".
The mini-windows will draw, Components will be written as the heading, except nothing will be written in it.

So I have two problems: Can I get the script to ignore any line that says "Queued command: blah" and how come nothing is getting written in the mini-window?

thanks
D

addition: here's what appears in the output window after "comps" is fired:

comps
You open His Mouth.
You count one cured human left index finger with a total of one item.
You count thirty-five beeswax candles with a total of thirty-five items.
Queued command: count eyes in his mouth
Queued command: count ash in his mouth
Queued command: count powder in box in his mouth
Queued command: count white powder in his mouth
Queued command: count hearts in his mouth
Queued command: count torches in his mouth
Queued command: close his mouth
Queued command: sb
You count four cured human right eyes and four cured human left eyes with a total of eight items.
You count about eight handfuls of some fine grey ash inside a pocket in His Mouth with a total of eight items.
You count about seven handfuls of some purple mineral powder inside the small cardboard box inside a pocket in His Mouth with a total of seven items.
You count about four handfuls of some white mineral powder inside a pocket in His Mouth with a total of four items.
You count four cured human hearts with a total of four items.
Cannot find "torches in his mouth", no match.
You close His Mouth.
Hp: 1593(1593)  Gp: 391(391)  Xp: 315404  Burden: 44%


Australia Forum Administrator #2

local line, wildcards, style = wait.match ("*")
  
...

  -- save line in table
  table.insert (inv, styles)


You are getting "style" but saving "styles".
Australia Forum Administrator #3
Dacrian said:

Can I get the script to ignore any line that says "Queued command: blah"


Just check for that, eg.



-- save line in table
  if not string.match (line, "^Queued command") then
     table.insert (inv, styles)
  end -- if
Australia #4
Nick Gammon said:


local line, wildcards, style = wait.match ("*")
  
...

  -- save line in table
  table.insert (inv, styles)


You are getting "style" but saving "styles".



just goes to show that a new set of eyes spots it. I re-read that code 10 times trying to spot it!!

cheers!
Australia #5
That works beautifully.

Next issue! I can either attempt to wrap the output that I'm collecting (looks a bit tricksy), or create some triggers to capture to variables the amount of each component, eg. just capture the (second) "six" in the line "You count six beeswax candles with a total of six items."

How do I display in the miniwindow defined lines intead of the captured text? eg.
Candles: (variable1)
Torches: (variable2)
Ash: etc


thanks for the help!
Australia Forum Administrator #6
Just WindowText ... whatever.
Australia #7
*facepalm*

what, just like it's written in the script to make the heading?

i'm an eeeeediot!
Australia #8
ok, it's been a while. Ilost this code, so now I'm trying to get it to work again.

running this alias:

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) -- font selection for winiwindow
end -- if

-- request Components

Send "comps"

-- wait for component listing

local x = wait.match ("You open His Mouth.", 10)

if not x then
  ColourNote ("white", "blue", "No components list within 10 seconds")
  return
end -- if

local inv = {}
local max_width = WindowTextWidth (win, font, "Components")


-- loop until end of components

while true do
  local line, wildcards, style = wait.match ("*")
  
  -- see if ended yet
  if not string.match (line, "^You count") then
    ColourNote ("white", "blue", "Missed 'You count'") -- To notify if something breaks it, eg. no component there
    break
  end -- if

  -- save line in table
  table.insert (inv, style)
  -- work out max width needed
  max_width = math.max (max_width, WindowTextWidth (win, font, line))

end -- while loop

local font_height = WindowFontInfo (win, font, 1)

local window_width = max_width + 10
local window_height = font_height * (#inv + 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, "Components", 5, 5, 0, 0, ColourNameToRGB "yellow")

--draw each inventory line
local y = font_height * 2 + 5


for i, styles in pairs (inv) do
  local x = 5
  for _, styles in ipairs (styles) do
    x = x + WindowText (win, font, style.text, x, y, 0, 0, style.textcolour)
  end -- for
  y = y + font_height
end -- for

WindowShow (win, true)



end) --end of coroutine


returns this error:

Error number: 0
Event:        Run-time error
Description:  I:\Program Files\MUSHclient\lua\wait.lua:67: [string "Alias: "]:67: attempt to index global 'style' (a nil value)

stack traceback:

	[C]: in function 'error'

	I:\Program Files\MUSHclient\lua\wait.lua:67: in function <I:\Program Files\MUSHclient\lua\wait.lua:59>
Called by:    Function/Sub: wait.trigger_resume called by trigger

Reason: processing trigger "wait_trigger_178"


here's the output from the MUD:
comps
You open His Mouth.
You count thirty-five beeswax candles with a total of thirty-five items.
You count one cured human right eye with a total of one item.
You count about one bucketful of some fine grey ash inside a pocket in His Mouth with a total of one item.
You count about thirteen handfuls of some purple mineral powder inside the small cardboard box inside a pocket in His Mouth with a total of thirteen items.
You count about one pinch of some white mineral powder inside a pocket in His Mouth with a total of one item.
You count one cured dog heart and five cured human hearts with a total of six items.
You count five lightable torches with a total of five items.
You close His Mouth.
Missed 'You count'
Error raised in trigger function (in wait module)
stack traceback:
        [string "Alias: "]:67: in function <[string "Alias: "]:3>
.
Trigger function "wait.trigger_resume" not found or had a previous error.
Hp: 1593(1593)  Gp: 397(397)  Xp: 261961  Burden: 49%


I have no idea what broke :(
Australia Forum Administrator #9

for i, styles in pairs (inv) do
  local x = 5
  for _, styles in ipairs (styles) do
    x = x + WindowText (win, font, style.text, x, y, 0, 0, style.textcolour)
  end -- for
  y = y + font_height
end -- for


Similar to before. You get "styles" but index into "style".
Australia #10
so everywhere that styles appears, i should change it to style? apologies for sounding very dense, I took a 12 month break from MUDding and that was the only time I ever looked at code. Thanks for the fast answer!
Australia #11
Did it, worked :)

now just to switch in the WindowText thing and I'm back to where I was

thanks again!

actually, how would I call an alias instead of sending to the MUD side alias? thanks!