Miniwindow plugin - status window for Aardwolf

Posted by Nick Gammon on Sun 27 Jul 2008 12:44 AM — 14 posts, 53,346 views.

Australia Forum Administrator #0
The plugin below illustrates using the new miniwindows concept in a plugin.

Miniwindows were released in version 4.34, see here for a copy:

http://www.gammon.com.au/forum/?id=8811


The plugin below shows a nice health bar in the bottom RH corner, so you can see at a glance your health, compared to the mob you are fighting.


To use it, download the code below and save as Aardwolf_Health_Bar.xml in the Aardwolf subdirectory below your Plugins directory. If you haven't used any Aardwolf plugins before you may need to make the Aardwolf directory.


This plugin also uses the "stats detector" plugin, so you also need this file:

http://www.gammon.com.au/mushclient/plugins/Aardwolf/Stats_Detector.xml

The "Stats detector" plugin uses telnet negotation, so you also need this file:

http://www.gammon.com.au/mushclient/plugins/Aardwolf/telnet_options.lua

In a standard MUSHclient installation these three files should to into this directory:


C:\Program Files\MUSHclient\worlds\plugins\Aardwolf\


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>

<!-- Plugin "Health_Bar" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Aardwolf_Health_Bar_v2"
   author="Nick Gammon"
   id="1459b816b5acd49c249f72e7"
   language="Lua"
   purpose="Shows Aardwolf stats in a mini window"
   date_written="2008-07-18"
   requires="4.34"
   version="1.0"
   save_state="y"
   >
<description trim="y">
<![CDATA[
Install this plugin to show an info bar with HP, Mana, 
and Movement points shown as a bar (between 0 and 10 blocks).
If you are fighting the enemy's name and health are also displayed.
]]>
</description>

</plugin>

<!--  Script  -->


<script>
<![CDATA[

gauge_left = 45
gauge_height = 11
gauge_right = 190

background_colour = 0x808080
box_colour = 0xD3D3D3

require "checkplugin"

function capitalize (s)
  return string.sub (s, 1, 1):upper () .. string.sub (s, 2):lower ()
end -- capitalize 

function DoGauge (sPrompt, Percent, sGoodColour, sBadColour)

  Percent = tonumber (Percent)
   
  local width = WindowTextWidth (win, font_id, sPrompt)
  
  WindowText (win, font_id, sPrompt,
                             gauge_left - width - 2, vertical - 2, 0, 0, 0x000000)

  WindowRectOp (win, 1,   -- frame rectangle
          gauge_left, vertical, 
          gauge_right, vertical + gauge_height, 
          box_colour)


  local colour
  
--
--  Below 30% warn by using different colour
--

  if Percent < 30 then
    colour = sBadColour
  else
    colour = sGoodColour
  end -- if

  local pixels = (gauge_right - gauge_left - 1) * Percent / 100

  WindowRectOp (win, 2, -- fill rectangle
          gauge_left + 1, vertical + 1, 
          gauge_left + pixels, vertical + gauge_height - 1, 
          ColourNameToRGB (colour))

  vertical = vertical + font_height
end -- function

function draw_bar ()

  check (WindowRectOp (win, 2, 0, 0, 0, 0, background_colour))  -- fill entire box
  
  -- DrawEdge rectangle
  check (WindowRectOp (win, 5, 0, 0, 0, 0, 10, 15))
  vertical = 8  -- pixel to start at
  
  DoGauge ("HP: ",   stats.hp_percent, "darkgreen", "maroon")

  if stats.enemy_percent ~= "9999" then
    -- enemy name:  stats.enemy
    DoGauge ("Enemy: ", stats.enemy_percent, "darkred", "red")
  end -- if fighting 

  DoGauge ("Mana: ", stats.mana_percent, "mediumblue", "mediumblue")
  DoGauge ("Move: ", stats.moves_percent, "gold", "gold")

  WindowShow (win, true)
  
end -- draw_bar

function OnPluginBroadcast (msg, id, name, text)
  if msg == 1 and id == "8a710e0783b431c06d61a54c" then
  
   -- get all variables
   stats = GetPluginVariableList("8a710e0783b431c06d61a54c")
     
   draw_bar ()
   
  end -- stats changed
end

function OnPluginInstall ()
  
  win = GetPluginID ()
  font_id = "fn"
  
  font_name = "Sylfaen"    -- the actual font
  
  -- make miniwindow so I can grab the font info
  check (WindowCreate (win, 
                 0, 0, 200, 65,  
                 8,   -- bottom right
                 0,   -- flags
                 background_colour) )

  check (WindowFont (win, font_id, font_name, 8, false, false, false, false, 0, 0))  -- normal
  
  font_height = WindowFontInfo (win, font_id, 1)  -- height
  
  if GetVariable ("enabled") == "false" then
    ColourNote ("yellow", "", "Warning: Plugin " .. GetPluginName ().. " is currently disabled.")
    check (EnablePlugin(GetPluginID (), false))
    return
  end -- they didn't enable us last time
  
  OnPluginEnable ()  -- do initialization stuff
  
end -- OnPluginInstall

function OnPluginEnable ()
  checkplugin ("8a710e0783b431c06d61a54c", "Stats_Detector.xml")
end -- OnPluginEnable

function OnPluginDisable ()
  WindowShow (win, false)
end -- OnPluginDisable

function OnPluginSaveState ()
  SetVariable ("enabled", tostring (GetPluginInfo (GetPluginID (), 17)))
end -- OnPluginSaveState


]]>
</script>

</muclient>
Amended on Sun 27 Jul 2008 08:47 PM by Nick Gammon
Australia Forum Administrator #1

Example of it in operation:

#2
is there any way to put this mini-window in a different corner of the main window? or perhaps a different window?
Australia Forum Administrator #3
Check out this page:

http://www.gammon.com.au/mushclient/mw_creation.htm#WindowCreate

Now in the plugin above is this stuff:


check (WindowCreate (win, 
                 0, 0, 200, 65,  
                 8,   -- bottom right
                 0,   -- flags
                 background_colour) )


The 8 means bottom right, as the comment says. Looking at the link above you can change the 8 to some other number to get a different position.
#4
Thanks Nick, that worked. I actually just found your Map miniwindow plugin, so i'm using that and the Health bar miniwindow in the main window now. Problem solved. :)
Amended on Thu 07 Aug 2008 03:58 PM by Kenneth
#5
Sorry with all the bone quests, but im new at all this.

Could someone post the BASICS of one of these bars, once I get a grip on something im ok at coding but I havent a clue where to start.

My prompt is,

HP:*/130 EP:*/130 REG:* >

If someone could explain to me the basics to just get my HP in a bar (in the info bar) i'd be very gracious. Thanks,
Australia Forum Administrator #6
Look at the Health Bar plugin on this page:

http://www.gammon.com.au/mushclient/plugins/

Basically it uses a trigger to match your prompt, extracts the numbers, and displays them.
#7
Trying to convert this to...


HP:130/130 EP:130/130 REG: >

...is this easily possible?

Great plugin by the way.


Australia Forum Administrator #8
Did you look at the plugin I mentioned? It is certainly possible, you basically want a trigger to match on:

HP:*/* EP:*/* REG: >

and then draw the gauges appropriately.
#9
Okay, I got the bar working fine with my prompt set too (with my full HP at 100).

HP:56/100 EP:65/100 REG: >

However, I wish to have it working from my normal prompt...

HP:56 EP:56 REG: >

I edited the script to replace the "wildcards(2)" and the "wildcards(4)" with the value "100", however when the trigger fires the following error occurs, is there any way round this?

Error number: -2146828275
Event: Execution of line 403 column 3
Description: Type mismatch: 'CInt'
Called by: Function/Sub: DoPrompt called by trigger

Reason: processing trigger "InfoBar"


Again, this is probably something simple - but with my limited knowledge I can't solve it.
USA #10
Could you show what your code looks like?
#11
TRIGGER:

<triggers>
<trigger
enabled="y"
expand_variables="y"
match="^HP\:(\d+)\/(\d+) EP\:(\d+)\/(\d+) REG\: \&gt;$"
name="InfoBar"
regexp="y"
script="DoPrompt"
send_to="12"
sequence="100"
>
<send>setvariable "Health", "%1"
setvariable "Energy", "%3"
setvariable "Region", "%5"

status = WindowCreate ("6", 0, 0, 290,673, 6, 0, ColourNameToRGB ("black"))

status = WindowGradient (6, 27, 10, 50, 33, ColourNameToRGB ("white"),ColourNameToRGB ("white"),1)
status = WindowGradient (6, 34, 10, 42, 33, ColourNameToRGB ("darkred"),ColourNameToRGB ("red"),1)
status = WindowGradient (6, 27, 18, 50, 25, ColourNameToRGB ("darkred"),ColourNameToRGB ("red"),1)

status = WindowGradient (6, 27, 60, 50, 83, ColourNameToRGB ("white"),ColourNameToRGB ("white"),1)
status = WindowGradient (6, 34, 60, 42, 83, ColourNameToRGB ("green"),ColourNameToRGB ("lime"),1)
status = WindowGradient (6, 27, 67, 50, 75, ColourNameToRGB ("green"),ColourNameToRGB ("lime"),1)

status = WindowGradient (6, 20, 110, 50, 133, ColourNameToRGB ("black"),ColourNameToRGB ("saddlebrown"),1)
status = WindowGradient (6, 20, 118, 50, 123, ColourNameToRGB ("sienna"),ColourNameToRGB ("yellow"),1)
status = WindowGradient (6,32, 116, 37, 125, ColourNameToRGB ("orange"),ColourNameToRGB ("yellow"),1)

status = WindowGradient (6, 20, 156, 26, 183, ColourNameToRGB ("cyan"),ColourNameToRGB ("blue"),1)
status = WindowGradient (6, 29, 165, 36, 183, ColourNameToRGB ("fuscia"),ColourNameToRGB ("purple"),1)
status = WindowGradient (6, 38, 173, 46, 183, ColourNameToRGB ("green"),ColourNameToRGB ("lime"),1)
status = WindowGradient (6, 20, 183, 50, 187, ColourNameToRGB ("white"),ColourNameToRGB ("red"),1)

status = WindowFont (6, "heading", "Trebuchet MS", 16, TRUE, FALSE, FALSE, FALSE, 0, 0)
status = WindowText (6, "heading", " Health: @Health",65, 10, 0, 0,ColourNameToRGB ("white"),false)
status = WindowText (6, "heading", " Energy: @Energy",65, 58, 0, 0,ColourNameToRGB ("white"),false)
status = WindowText (6, "heading", " Gold: @Gold",65,108, 0, 0,ColourNameToRGB ("white"),false)
status = WindowText (6, "heading", " Exp: @Exp",65,156, 0, 0,ColourNameToRGB ("white"),false)

status = WindowShow (6, true)

</send>
</trigger>
</triggers>


SCRIPT:

sub DoGauge (sPrompt, iCurrent, iMax, sGoodColour, sBadColour)
dim pc, count

'
' Do prompt in black Arial
'
InfoColour "black"
InfoFont "Arial", 10, 0
Info sPrompt

'
' Use Webdings for gauge (black square)
'

InfoFont "Webdings", 12 , 0

pc = CInt ((CInt (iCurrent) / CInt (iMax)) * 20)

'
' Below 20% warn by using different colour
'

if pc < 2 then
InfoColour sBadColour
else
InfoColour sGoodColour
end if

'
' Draw active part of gauge
'
for count = 0 to pc
Info "g"
next

'
' Draw rest of gauge in grey (ie. unfilled bit)
'

InfoColour "dimgray"
while count <= 20
count = count + 1
Info "g"
wend

end sub

sub DoPrompt (sName, sLine, wildcards)

InfoClear

'
' World name
'

InfoFont "Arial", 12, 1 ' 12 point Arial *bold*
InfoColour "black"
Info GetInfo (2) ' world name
InfoColour "white"
info "Health: "
DoGauge "", wildcards (1), 130, "red", "aqua"
InfoColour "white"
InfoFont "Arial", 12, 1 ' 12 point Arial *bold*
info " Energy: "
DoGauge "", wildcards (3), 130, "mediumblue", "mediumblue"

end sub

'
' Do this once
'
ShowInfoBar vbTrue











END
Australia Forum Administrator #12
I don't understand why the DoGauge script is still there - that is drawing a gauge in the Info bar, whereas the trigger is drawing another one in the miniwindow.
#13
The trigger is just putting some variables in the miniwindow, targets/health/mana points etc etc, not a minibar.