<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Balances"
   author="Keldar"
   id="a7a632af546c98f0661803fc"
   language="Lua"
   purpose="Tracking various balances"
   date_written="2006-09-19"
   requires="3.80"
   version="1.3"
   >
<description trim="y">
<![CDATA[
Command summary:

  flc_dhealth         - drink a health elixir
  flc_dmana           - drink a mana elixir
  flc_emoss           - eat a moss plant
  flc_eat herb        - eat a hearb
  flc_focus           - use focus
  
]]>
</description>

</plugin>

<triggers>
  <trigger
   enabled="y"
   match="^\d+h, \d+m(?:, \d+e|)(?:, \d+w|) c?(e)?(x)?k?d?b?\@?-$"
   regexp="y"
   keep_evaluating="y"
   script="checkTiming"
   sequence="100"
  >
  </trigger>
</triggers>

<aliases>
  <alias
   name="flc_drink_health"
   script="hb.execute"
   match="^flc\_dhealth$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
  <alias
   name="flc_drink_mana"
   script="hb.execute"
   match="^flc\_dmana$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
  <alias
   name="flc_eat_herb"
   script="herbb.execute"
   match="^flc\_eat (?P&lt;herb&gt;goldenseal|lobelia|cohosh|bloodroot)$"
   enabled="y"
   sequence="100"
  >
  </alias>
  <alias
   name="flc_focus"
   script="fb.execute"
   match="^flc\_focus$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>
<aliases>
  <alias
   name="flc_eat_moss"
   script="mb.execute"
   match="^flc\_emoss$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>


<script>
<![CDATA[
require "flc.klib"
require "flc.balances"
require "flc.kwait"
require "flc.events"

local wait = kwait
local Balance = balances.Balance


--================================================
-- Event stubs
--================================================
events.SetEvent(events.FLC_HRBEAT, function() end)
events.SetEvent(events.FLC_SIP, function() end)
events.SetEvent(events.FLC_EMOSS, function() end)




--================================================
-- All focusable afflictions
--================================================
focusable = {"stupidity",
             "anorexia",
             "confusion",
             "masochism",
             "recklessness",
             "weakness",
             "epilepsy",
             "claustrophobia",
             "agoraphobia",
             "shyness",
             "dizziness",
             "loneliness",
             "pacifism"}



--================================================
-- Focus balance
--================================================

fb = Balance("focus", 2.0)
fb.tocall = function(comm)
  local line = wait.regexp([[^(?:You focus your mind intently on curing your mental maladies\.|]]..
                           [[You concentrate, but your mind is too tired to focus\.)$]], 1)

  if not line then
    if comm then fb.repeater(comm) end
    return
  elseif string.find(line, "^You focus your mind") then
    line = wait.regexp([[^.+$]])
    
    if string.find(line, "^%d+h, %d+m") then
      klib.RemoveAfflictions(focusable)
    end
  end
  
  wait.regexp([[^Your mind is able to focus once again\.$]], 2)
  fb.set(true)
end

fb.execute = function(name, outp, wildcs)
  Send("focus")
  fb.set(false, wildcs[0])
end
fb.set(true)


--================================================
-- Health balance
--================================================

hb = Balance("health", 5.0)
hb.tocall = function(comm)
  local line,wildcs = wait.regexp([[^(?:The elixir flows down your throat without effect\.|]] .. 
                                  [[What is it that you wish to drink\?|]] ..
                                  [[The elixir heals and soothes you\.|]] ..
                                  [[Your mind feels stronger and more alert\.|]] .. 
                                  [[The idea of putting something in your stomach sickens you\.)$]], 1.5)

  if line == nil then
    if comm then hb.repeater(comm) end
    return  
  elseif (line == "What is it that you wish to drink?") or 
         (line == "The idea of putting something in your stomach sickens you.") then
    hb.set(true)
    return
  end
  line,wildcs = wait.regexp([[^You may drink another health or mana elixir\.$]], 5)
  hb.set(true)
end

hb.execute = function(name, output, wildcs)
  if name == "flc_drink_health" then
    Send("drink health")
  elseif name == "flc_drink_mana" then
    Send("drink mana")
  end
  hb.set(false, wildcs[0])
end
hb.set(true)
--================================================
-- Moss balance
--================================================

mb = Balance("moss", 5.0)
mb.tocall = function (comm)
  local line,wildcs = wait.regexp([[^(?:You feel your health and mana replenished\.|]] .. 
                                  [[What do you want to eat\?|]] .. 
                                  [[The moss slides down without effect\.|]] ..
                                  [[The idea of putting something in your stomach sickens you\.)$]], 1.5)
  
  if line == nil then
    if comm then mb.repeater(comm) end
    return
  elseif (line == "What do you want to eat?") or
         (line == "The idea of putting something in your stomach sickens you.") then
    mb.set(true)
    return
  end
  
  line, wildcs = wait.regexp([[^You may eat another bit of irid moss\.$]],5)
  
  mb.set(true)
end

mb.execute = function(name, outp, wildcs)
  Send("outr moss\r\neat moss")
  mb.set(false, wildcs[0])
end
mb.set(true)

--================================================
--Herb balance
--================================================
herbb = Balance("herb", 2.0)
herbb.tocall = function (comm)

end

herbb.execute = function(name, outp, wildcs)
  Send("outr " .. wildcs.herb .. "\neat " .. wildcs.herb)
  herbb.set(false, wildcs[0])
end

local bals = {hb, mb, fb, herbb}

-- physical balance
phys_bal = true
klib.AddBalance("physical")
-- equilibrium
eq = true
klib.AddBalance("equilibrium")


function checkTiming(name, outp, wildcs)
  for _,b in ipairs(bals) do
    b.active()
  end
  if (wildcs[1] == "e") and (not eq) then 
    klib.AddBalance("equilibrium")
    eq = true
  elseif (wildcs[1] ~= "e") and eq then
    klib.RemoveBalance("equilibrium")
    eq = false
  end
  if (wildcs[2] == "x") and (not phys_bal) then
    klib.AddBalance("physical")
    phys_bal = true
  elseif (wildcs[2] ~= "x") and phys_bal then
    klib.RemoveBalance("physical")
    phys_bal = false
  end
end



]]>
</script> 


<!--  Plugin help  -->

<aliases>
  <alias
   script="OnHelp"
   match="Balances:help"
   enabled="y"
  >
  </alias>
</aliases>

<script>
<![CDATA[
function OnHelp ()
  world.Note (world.GetPluginInfo (world.GetPluginID (), 3))
end
]]>
</script> 

</muclient>
