<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Autosipper"
   author="Keldar"
   id="3c4b3e476bbd5891190cd22a"
   language="Lua"
   purpose="Automatic healing of health and mana"
   date_written="2006-06-04"
   requires="3.80"
   version="1.3"
   >

<description trim="y">
<![CDATA[
Command summary:

  dh             - drink health
  dm             - drink mana
  em             - eat moss
  
  ah on|off      - turn autohealing on and off
  moss on|off    - turn moss healing on and off
  am             - give priority to mana when healing
  ah             - give priority to health
  
]]>
</description>
   

</plugin>


<!--  Triggers  -->

<triggers>
  <trigger
   enabled="y"
   match="^(?P&lt;health&gt;\d+)h, (?P&lt;mana&gt;\d+)m(?:, \d+e|)(?:, \d+w|) (?P&lt;stat&gt;c?e?x?k?d?b?\@?)-$"
   regexp="y"
   script="Prompt"
   sequence="1"
   keep_evaluating="y"
  >
  </trigger>

</triggers>

<!--  Aliases  -->

<aliases>
  <alias
   name="qsc_open"
   match="^\s*qsc\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>doQSC(false)</send>
  </alias>
  
  <alias
   match="^ah\s+(on|off)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>local t = {on = true, off = false}
CurrentState.on = t.%1
t = {on="lime", off="red"}
ColourNote("olive", "", "[Autosipper]: ", "silver", "", "Healing is now ", t.%1, "", string.upper("%1") .. ".")
</send>
  </alias>
  <alias
   match="^moss\s+(on|off)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>local t = {on = true, off = false}
CurrentState.mossOn = t.%1
t = {on="lime", off="red"}
ColourNote("olive", "", "[Autosipper]: ", "silver", "", "Moss curing is now ", t.%1, "", string.upper("%1") .. ".")</send>
  </alias>
  <alias
   name="drink_health"
   script="Manual"
   match="^\s*dh\s*$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
  <alias
   name="drink_mana"
   script="Manual"
   match="^\s*dm\s*$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
  <alias
   name="eat_moss"
   script="Manual"
   match="^\s*em\s*$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>

  <alias
   match="^\s*a(h|m)\s*$"
   script="SwitchPriority"
   enabled="y"
   regexp="y"
  ></alias>
</aliases>

<aliases>
  <alias
   match="^\s*(qsc|score?)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>doQSC("%1", false)</send>
  </alias>
</aliases>

<!--  Timers  -->


<!--  Script  -->



<script>
<![CDATA[
require "flc.klib"
require "flc.kwait"
require "flc.events"
require "tprint"
require "flc.katcp"
require "flc.kutils"

local wait = kwait


-- Set the ATCP option
--
use_atcp = false

function set_atcp(val)
  if val then
    use_atcp = true
    kutils.DoNote("Plugin will use ATCP to gather vitals data.")
    SetVariable("UseATCP", "1")
  else
    use_atcp = false
    kutils.DoNote("Plugin will use the prompt to gather vitals data.")
    SetVariable("UseATCP", "0")
  end
end

events.SetEvent(events.FLC_ATCP_ON, function() set_atcp(true) end)
events.SetEvent(events.FLC_ATCP_OFF, function() set_atcp(false) end)


-- A stub for the FLC_STATS event
--
events.SetEvent(events.FLC_STATS, function() end)

function QSCGetter(tocall, command, omit)
  local res = true
  wait.make( function()
    local t = {}
    
    Send(command)
    
    local line, wildcs = wait.regexp([[^.+?\((?:fe)?male .+?\)$]], 5, omit)
    
    if not line then
      tocall(nil)
      return
    end
    while true do
      
      line,wildcs = wait.regexp([[^(?:(?P<stat1>Health|Endurance)\:\s+(?P<currValue1>\d+)\s*\/\s*(?P<maxValue1>\d+)\s+(?P<stat2>Mana|Willpower)\:\s+(?P<currValue2>\d+)\s*\/\s*(?P<maxValue2>\d+)|]] ..
                                      [[You are level .+? and .+? of the way to the next level\.|]] ..
                                      [[\d+h, \d+m(?:, \d+e|)(?:, \d+w|) c?e?x?k?d?b?\@?-|]] ..
                                      [[Strength\:\s+\d+  Dexterity\:\s+\d+  Constitution\:\s+\d+  Intelligence\:\s+\d+)$]], 2, omit)
      
      if not line then
        res = false
        break
      elseif string.find(line, "^%d+h, %d+m") then
        break
      end
      
      if wildcs.stat1 and (wildcs.stat1 ~= "") then
        t[ "curr_" .. string.lower(wildcs.stat1) ]  = tonumber(wildcs.currValue1)
        t[ "max_" .. string.lower(wildcs.stat1) ] = tonumber(wildcs.maxValue1)
        t[ "curr_" .. string.lower(wildcs.stat2) ] = tonumber(wildcs.currValue2)
        t[ "max_" .. string.lower(wildcs.stat2) ] = tonumber(wildcs.maxValue2)
      end
      
    end
    
    if not res then
      tocall(nil)
    else
      tocall(t)
    end
  
  end)
end


CurrentState = {
	maxHealth = nil,
	maxMana = nil,
	currHealth = nil,
	currMana = nil,
	manaRatio = nil,
	healthRatio = nil,
	minHealth = 80,
	minMana = 60,
	mossOn = false,
	on = true,
	inQSC = false
}

function Prompt(name, output, wildcs, s)
  if use_atcp then
    local vitals = katcp.GetVitals()
    if vitals then
      CurrentState.currHealth = tonumber(vitals.curr_health)
      CurrentState.currMana = tonumber(vitals.curr_mana)
      CurrentState.maxHealth = tonumber(vitals.max_health)
      CurrentState.maxMana = tonumber(vitals.max_mana)
    else
	    CurrentState.currHealth = tonumber(wildcs.health)
	    CurrentState.currMana = tonumber(wildcs.mana)
      klib.SetCurrHealth(wildcs.health)
      klib.SetCurrMana(wildcs.mana)
    end
  else
	  CurrentState.currHealth = tonumber(wildcs.health)
	  CurrentState.currMana = tonumber(wildcs.mana)
    klib.SetCurrHealth(wildcs.health)
    klib.SetCurrMana(wildcs.mana)
  end
  klib.SetPromptStatLine(wildcs.stat)
	if CurrentState.maxHealth == nil or CurrentState.maxMana == nil then
		if CurrentState.inQSC then
			return
		else
			doQSC("qsc", true)
      CurrentState.inQSC = true
		end
	else
		CurrentState.inQSC = false
		if not use_atcp then
      klib.SetMaxHealth(CurrentState.maxHealth)
      klib.SetMaxMana(CurrentState.maxMana)
    end
    CurrentState.manaRatio = math.floor( (CurrentState.currMana/CurrentState.maxMana)*100 )
    CurrentState.healthRatio = math.floor( (CurrentState.currHealth/CurrentState.maxHealth)*100 )
		doCuring()
	end
end

function doCuring()
	if CurrentState.on then 
		cure() 
		if CurrentState.mossOn then
			cureMoss()
		end
	end
end


function healthFirst()
  local balances = klib.GetBalances()
	if CurrentState.healthRatio <= CurrentState.minHealth then
		if balances.health then
			Execute("flc_dhealth")
		end
	elseif CurrentState.manaRatio <= CurrentState.minMana then
		if balances.health then
			Execute("flc_dmana")
		end
	end
end


function manaFirst()
  local balances = klib.GetBalances()
	if CurrentState.manaRatio <= CurrentState.minMana then
		if balances.health then
		  Execute("flc_dmana")
		end
	elseif CurrentState.healthRatio <= CurrentState.minHealth then
		if balances.health then
			Execute("flc_dhealth")
		end
	end
end


function cureMoss()
	if CurrentState.mossOn then
		if (CurrentState.healthRatio <= CurrentState.minHealth - 10) or
		   (CurrentState.manaRatio <= CurrentState.minMana - 10) then
      local balances = klib.GetBalances()
			if balances.moss then
				Execute("flc_emoss")
			end
		end
	end
end

cure = healthFirst

function OnPluginConnect()
  CurrentState.inQSC = true
	doQSC("qsc", true)
end

function Manual(name, output, wildcs)
	local temp = utils.split(name, "_")
	local act, what = temp[1], temp[2]
	if act == "drink" then
	  Execute("flc_d" .. what)
	elseif act == "eat" then
	  Execute("flc_emoss")
	end 
end

function doQSC(command,omit)
	CurrentState.inQSC = true
	local tocall
	tocall = function()
	  QSCGetter(tocall, command, omit)
	  local stats = coroutine.yield()
	  if stats == nil then
	    CurrentState.inQSC = false
	    return	    
	  end
	  CurrentState.maxHealth = stats.max_health
	  CurrentState.maxMana = stats.max_mana
	  klib.SetMaxHealth(stats.max_health)
	  klib.SetMaxMana(stats.max_mana)
	  CurrentState.inQSC = false
	  Execute(events.FLC_STATS)
	end
	tocall = coroutine.wrap(tocall)
	tocall()
end


function SwitchPriority(n,o,wildcs)
	if wildcs[1] == "h" then
		cure = healthFirst
		ColourNote("olive", "", "[Autosipper]:", "silver", "", " Health will receive priority when healing.")
	elseif wildcs[1] == "m" then
		cure = manaFirst
		ColourNote("olive", "", "[Autosipper]:", "silver", "", " Mana will receive priority when healing.")
	end
end

]]>
</script>


</muclient>
