<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Auto_Recall"
   author="Abelinc"
   id="c5cb69d0dae842c3f62b71f6"
   language="Lua"
   purpose="Tries to recall if in battle and health falls below preset limit"
   date_written="2010-06-23"
   requires="4.00"
   version="1.0"
   save_state="y"
   >
<description trim="y">
<![CDATA[
Install this plugin to automatically cast word of recall when your health falls 
below a preset limit. If mana is below 30, it will attempt to flee.

autorecall                 --> Displays the current values of variables

autorecall percent #       --> Sets what percent you must fall below to cast word of recall

autorecall on/off          --> Turns the actions of the plugin on or off

autorecall help            --> This text
]]>
</description>

</plugin>

<!--  configure recalling limits -->


<aliases>

  <alias
   name="autorecall"
   script="autorecall_settings"
   match="^autorecall ?(percent|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_autorecall ()
   
  end -- stats changed
end


function do_autorecall ()
	if (os.time () - autorecall_lastfired) > 3 then
		if autorecall_enable == "yes" then
			if stats.position == "4" then
				if tonumber (stats.hp_percent) < tonumber (autorecall_percent) then
					if tonumber (stats.mana) > 30 then
						Send ("c word")
						ColourNote ("yellow", "", "Ack! We're dying! Recall! Recall!")
							autorecall_lastfired = os.time ()
					else Send ("fl")
						Send ("fl")
						Send ("fl")
						Send ("fl")
						ColourNote ("yellow", "", "We don't have the mana to recall, try fleeing!")
							autorecall_lastfired = os.time ()
					end
				end
			end
		end
	end
end -- do_autorecall


function autorecall_settings (name, line, wildcards)
	if wildcards [1] == false then
		autorecall_tellme ()
	elseif wildcards [1] == "on" then
		autorecall_enable = "yes"
		ColourNote ("yellow", "", "Autorecall plugin enabled.")
	elseif wildcards [1] == "off" then
		autorecall_enable = "no"
		ColourNote ("yellow", "", "Autorecall plugin disabled.")
	elseif wildcards [1] == "help" then
		autorecall_help ()
	elseif wildcards [1] == "percent" then
		if tonumber (wildcards [2]) == nil then
			ColourNote ("yellow", "", "You need to provide a value.")
		elseif tonumber (wildcards [2]) < 0 then
			ColourNote ("yellow", "", "Try a larger value.")
		elseif tonumber (wildcards [2]) > 99 then
			ColourNote ("yellow", "", "Try a smaller value.")
		else
			autorecall_percent = tonumber (wildcards [2])
			SetVariable ("autorecall_percent", autorecall_percent)
			ColourNote ("yellow", "", "We just set autorecall_percent to " .. autorecall_percent)
		end
	else
		autorecall_help ()
	end
end -- autorecall_settings

function autorecall_tellme (name, line, wildcards)
	ColourNote ("yellow", "", "autorecall_percent is currently " .. GetVariable ("autorecall_percent") )
	ColourNote ("yellow", "", "autorecall_enable is currently " .. autorecall_enable)
end

function autorecall_help (name, line, wildcards)
  ColourNote ("teal", "", world.GetPluginInfo (world.GetPluginID (), 3))
  return true -- done  
end -- autorecall_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 ("autorecall_percent") == nil then 
		SetVariable ("autorecall_percent", tonumber(30))
	end
	autorecall_percent = GetVariable ("autorecall_percent")
	if GetVariable ("autorecall_enable") == nil then 
		SetVariable ("autorecall_enable", "yes")
	end
	autorecall_enable = GetVariable ("autorecall_enable")
	autorecall_lastfired = os.time ()

end -- OnPluginEnable

function OnPluginSaveState ()
  SetVariable ("enabled", tostring (GetPluginInfo (GetPluginID (), 17)))
  SetVariable ("autorecall_percent", autorecall_percent)
  SetVariable ("autorecall_enable", autorecall_enable)
end -- OnPluginSaveState


]]>
</script>

</muclient>
