<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Auto_Quaff"
   author="Abelinc"
   id="91db3f0b6bb87464ca75f5c7"
   language="Lua"
   purpose="Quaffs potions if in battle and health falls below preset limit"
   date_written="2010-02-26"
   requires="4.00"
   version="1.0"
   save_state="y"
   >
<description trim="y">
<![CDATA[
Install this plugin to automatically quaff a potion when your health falls 
below a preset limit.

autoquaff                  --> Displays the current values of variables

autoquaff bag <bagname>    --> Sets what container to get potions from

autoquaff potion <potname> --> Sets what potion to quaff

autoquaff percent #        --> Sets what percent you must fall below to quaff

autoquaff count #          --> Sets how many consecutive rounds the plugin
                               will quaff before skipping a round

autoquaff on/off           --> Turns the actions of the plugin on or off
]]>
</description>

</plugin>

<!--  configure quaffing limits -->


<aliases>

  <alias
   name="autoquaff"
   script="autoquaff_settings"
   match="^autoquaff ?(percent|count|potion|bag|on|off|help)?( [a-z0-9]+)?"
   enabled="y"
   regexp="y"
   ignore_case="y"
   sequence="100"
  >
  </alias>
   
</aliases>

<script>
<![CDATA[

require "checkplugin"

function OnPluginBroadcast (msg, id, name, text)
  if msg == 1 and id == "8a710e0783b431c06d61a54c" then
  
   -- get all variables
   stats = GetPluginVariableList("8a710e0783b431c06d61a54c")
     
   do_autoquaff ()
   
  end -- stats changed
end


function do_autoquaff ()
	if (os.time () - autoquaff_lastfired) > 3 then
		if autoquaff_enable == "yes" then
			if stats.position ~= "4" then
				autoquaff_currcount = 0
			end
			if stats.position == "4" then
				if tonumber (stats.hp_percent) < tonumber (autoquaff_percent) then
					if tonumber (autoquaff_currcount) < tonumber (autoquaff_count) then
						Send ("g" .. autoquaff_potion .. autoquaff_bag)
						Send ("quaff" .. autoquaff_potion)
						autoquaff_currcount = autoquaff_currcount + 1
						ColourNote ("yellow", "", "Ack! We're dying! Quaff! Quaff! currcount is now " .. autoquaff_currcount)
							autoquaff_lastfired = os.time ()
					else autoquaff_currcount = 0
						ColourNote ("yellow", "", "We already quaffed to much, so didn't fire. We set currcount to" .. autoquaff_currcount)
							autoquaff_lastfired = os.time ()
					end
				else ColourNote ("yellow", "", "We've got plenty of health. No need to fire")
					autoquaff_currcount = 0
				end
			end
		end
	end
end -- do_autoquaff


function autoquaff_settings (name, line, wildcards)
	if wildcards [1] == false then
		autoquaff_tellme ()
	elseif wildcards [1] == "on" then
		autoquaff_enable = "yes"
			ColourNote ("yellow", "", "Autoquaff plugin enabled.")
	elseif wildcards [1] == "off" then
		autoquaff_enable = "no"
			ColourNote ("yellow", "", "Autoquaff plugin disabled.")
	elseif wildcards [1] == "help" then
			autoquaff_help ()
	else
		if wildcards [2] == false then
			ColourNote ("yellow", "", "You need to provide a value.")
		end
		if wildcards [1] == "potion" then
			autoquaff_potion = wildcards [2]
		elseif wildcards [1] == "bag" then
			autoquaff_bag = wildcards [2]
		elseif tonumber (wildcards [2]) > 99 then
			ColourNote ("yellow", "", "Try a smaller value.")
		else
			if wildcards [1] == "percent" then
				autoquaff_percent = tonumber (wildcards [2])
				ColourNote ("yellow", "", "We just set autoquaff_percent to " .. autoquaff_percent)
			elseif wildcards [1] == "count" then
				autoquaff_count = tonumber (wildcards [2])
				ColourNote ("yellow", "", "We just set autoquaff_count to " .. autoquaff_count)
			end
		end
	end
end -- autoquaff_settings

function autoquaff_tellme (name, line, wildcards)
	ColourNote ("yellow", "", "autoquaff_percent is currently " .. autoquaff_percent)
	ColourNote ("yellow", "", "autoquaff_count is currently " .. autoquaff_count)
	ColourNote ("yellow", "", "autoquaff_potion is currently " .. autoquaff_potion)
	ColourNote ("yellow", "", "autoquaff_bag is currently " .. autoquaff_bag)
	ColourNote ("yellow", "", "autoquaff_enable is currently " .. autoquaff_enable)
end

function autoquaff_help (name, line, wildcards)
  ColourNote ("teal", "", world.GetPluginInfo (world.GetPluginID (), 3))
  return true -- done  
end -- autoquaff_help

function OnPluginInstall ()

  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")
	if GetVariable ("autoquaff_count") == nil then 
		SetVariable ("autoquaff_count", tonumber(1))
	end
	autoquaff_count = GetVariable ("autoquaff_count")
	if GetVariable ("autoquaff_percent") == nil then 
		SetVariable ("autoquaff_percent", tonumber(30))
	end
	autoquaff_percent = GetVariable ("autoquaff_percent")
	if GetVariable ("autoquaff_potion") == nil then 
		SetVariable ("autoquaff_potion", "jade")
	end
	autoquaff_potion = GetVariable ("autoquaff_potion")
	if GetVariable ("autoquaff_bag") == nil then 
		SetVariable ("autoquaff_bag", "bag")
	end
	autoquaff_bag = GetVariable ("autoquaff_bag")
	if GetVariable ("autoquaff_enable") == nil then 
		SetVariable ("autoquaff_enable", "yes")
	end
	autoquaff_enable = GetVariable ("autoquaff_enable")
	autoquaff_lastfired = os.time ()

end -- OnPluginEnable

function OnPluginSaveState ()
  SetVariable ("enabled", tostring (GetPluginInfo (GetPluginID (), 17)))
  SetVariable ("autoquaff_percent", autoquaff_percent)
  SetVariable ("autoquaff_count", autoquaff_count)
  SetVariable ("autoquaff_potion", autoquaff_potion)
  SetVariable ("autoquaff_bag", autoquaff_bag)
  SetVariable ("autoquaff_enable", autoquaff_enable)
end -- OnPluginSaveState


]]>
</script>

</muclient>
