<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
  name="Harvester"
  author="Soludra"
  id="9214348f48803b5c28e31646"
  language="Lua"
  save_state="y"
  purpose="Automatically harvests available plants in a room."
  date_written="2008-08-25 03:11:04"
  requires="4.35"
  version="1.5"
  />

<triggers>
<!-- BEGIN PERCEIVE TRIGGERS -->

  <!-- PERCEIVE for pre-harvest imprint check -->
  <trigger enabled="n" group="harvest_perceive1" keep_evaluating="y" omit_from_output="y"
           regexp="y" send_to="14" sequence="100"
           match="^(?:You discern that this location has not yet been imprinted by any being\.|You are not standing in the forest\.)$" >
    <send>
      EnableGroup("harvest_perceive1", false)
      EnableGroup("harvest_perceive2", true)
    </send>
  </trigger>

  <trigger enabled="n" group="harvest_perceive1" keep_evaluating="y"
           regexp="y" send_to="12" sequence="100"
           match="^(?:You discern that this location is the Grove of |You discern that this location will not permit imprinting.$)" >
    <send>
      EnableGroup("harvest_perceive1", false)
      running = false
    </send>
  </trigger>

  <!-- Gags the perceive line -->
  <trigger enabled="n" group="harvest_perceive2" keep_evaluating="y" omit_from_output="y"
           regexp="y" send_to="14" sequence="101"
           match="^.*$" />

  <!-- Acts based on imprintation of the room -->
  <trigger enabled="n" group="harvest_perceive2" keep_evaluating="y" omit_from_output="y"
           regexp="y" send_to="14" sequence="100"
           match="^(?:\d+h, )?(?:\d+m,? )?(?:\d+e,? )?(?:\d+w,? )?c?e?x?k?d?b?@? ?(?:Vote)?- ?$" >
    <send>
      EnableGroup("harvest_perceive2", false)
      EnableGroup("harvest_scan", true)
      SendNoEcho("plants")
    </send>
  </trigger>

<!-- END PERCEIVE TRIGGERS -->
<!-- BEGIN PLANTS TRIGGERS -->

  <!-- Activates the PLANTS-scanning triggers -->
  <trigger enabled="n" group="harvest_scan" keep_evaluating="y" omit_from_output="y"
           regexp="y" send_to="14" sequence="100"
           match="^The following plants are growing in this room\:$" >
    <send>
      toharvest = {}
      EnableGroup("harvest_scan", false)
      EnableGroup("harvest_scan2", true)
    </send>
  </trigger>

  <!-- Handles PLANTS in trees -->
  <trigger enabled="n" group="harvest_scan" keep_evaluating="y" omit_from_output="n"
           regexp="y" send_to="14" sequence="100"
           match="^There are no plants growing way up here\!$" >
    <send>
      EnableGroup("harvest_scan", false)
      running = false
    </send>
  </trigger>

  <!-- Parses PLANTS for the list of herbs present -->
  <trigger enabled="n" group="harvest_scan2" keep_evaluating="y" omit_from_output="y"
           regexp="y" send_to="14" sequence="100"
           match="^[A-Za-z' ]+ \(([a-z' ]+)\)\s+\w+$" >
    <send>
      table.insert(toharvest, (herbs["%1"] or "%1"))
    </send>
  </trigger>

  <!-- Ends the parsing of PLANTS and acts on the list of plants -->
  <trigger enabled="n" group="harvest_scan2" keep_evaluating="y" omit_from_output="y"
           regexp="y" send_to="14" sequence="100"
           match="^(?:\d+h, )?(?:\d+m,? )?(?:\d+e,? )?(?:\d+w,? )?c?e?x?k?d?b?@? ?(?:Vote)?- ?$" >
    <send>
      EnableGroup("harvest_scan2", false)
      if (#toharvest > 0) then
        EnableGroup("harvest_act", true)
        DoHarvest()
      else
        Note("There aren't any plants to harvest!")
        running = false
        Send()
      end
    </send>
  </trigger>

<!-- END PLANTS TRIGGERS -->
<!-- BEGIN HARVEST TRIGGERS -->

  <!-- Continues harvesating when able -->
  <trigger enabled="n" group="harvest_act" keep_evaluating="y" omit_from_output="n"
           regexp="y" send_to="12" sequence="100"
           match="^(?:You have recovered balance on all limbs\.$|You have recovered equilibrium\.$|That plant has been fully harvested\.$|You have already harvested from this plant recently\.$|You find that you don't know how to harvest that\. How odd\.$|You don't have the skill to harvest from that plant yet\.$|What do you wish to harvest\?$|You scrutinise the plant for something to harvest\, but alas, you are)"
           script="DoHarvest" />

  <!-- harvesting without balance or equilibrium -->
  <trigger enabled="n" group="harvest_act" keep_evaluating="y" omit_from_output="n"
           regexp="y" send_to="12" sequence="100"
           match="^(?:You must regain balance first\.|You must regain equilibrium first\.)$" />

  <!-- Counts and autorifts harvested herbs -->
  <trigger enabled="n" group="harvest_act" keep_evaluating="y" omit_from_output="n"
           regexp="y" send_to="12" sequence="100"
           match="^You reach out and carefully harvest ((?:\d{0,2})?)\s?(?:[a-z' ]+)\.$"
           script="DoAutorift" />

<!-- END HARVEST TRIGGERS -->
</triggers>

<aliases>
  <!-- Begins harvesting -->
  <alias enabled="y" ignore_case="y" omit_from_output="y"
         regexp="y" send_to="14" sequence="100"
         match="^\s*HARVEST(?:\s+(GROVE))?\s*$"
         script="StartHarvester" />

  <!-- Ends harvesting prematurely -->
  <alias enabled="n" group="harvest_act" ignore_case="y" keep_evaluating="y" omit_from_output="y"
         regexp="y" send_to="12" sequence="100"
         match="^\s*stop\s*$" >
    <send>
      EnableGroup("harvest_act", false)
      toharvest = {}
      Note("Harvesting halted.")
      running = false
      Send("stop")
    </send>
  </alias>

  <!-- Configuration commands -->
  <alias enabled="y" echo_alias="y" ignore_case="y"
         regexp="y" send_to="12" sequence="100"
         match="^\s*HARVESTER(?:\s+(\S+)(?:\s+(\S+))?)?\s*$"
         script="HarvesterConfig" />
</aliases>

<script>
<![CDATA[

running = false

toharvest = {} -- Plants in the room to harvest
harvcount = {} -- Count of harvested plants
harvcount.total = 0

settings = { -- Variables which modify the plugin state names to single words
  perceive = ((GetVariable("perceive") or "0") == "1"),
  autorift = ((GetVariable("autorift") or "0") == "1")
}

herbs = { -- Translation table from PLANTS names to plant names
  ["myrrh bush"]       = "myrrh",
  ["wild ginger"]      = "ginger",
  ["red elm"]          = "elm",
  ["lady's slipper"]   = "slipper",
  ["irid moss"]        = "moss",
  ["bayberry tree"]    = "bayberry",
  ["cactus weed"]      = "weed",
  ["kola tree"]        = "kola",
  ["kuzu vine"]        = "kuzu",
  ["prickly ash tree"] = "ash",
  ["black cohosh"]     = "cohosh" 
}

confunc = { -- Functions used with HARVESTER <setting>

  perceive = function (wildcards)
    if not wildcards[1] then
      Note("Pre-harvest Perceive is set to: " .. (settings.perceive and "ON" or "OFF"))
      Note("Usage: HARVESTER PERCEIVE [ON|OFF]")
    elseif wildcards[1]:lower() == "on" then
      settings.perceive = true
      Note("Pre-harvest Perceive is now set to: ON")
    elseif wildcards[1]:lower() == "off" then
      settings.perceive = false
      Note("Pre-harvest Perceive is now set to: OFF")
    else
      Note("Invalid option for HARVESTER PERCEIVE.")
      Note("Usage: HARVESTER PERCEIVE [ON|OFF]")
  end
    Send()
  end,

  autorift = function (wildcards)
    if not wildcards[1] then
      Note("Autorift is set to: " .. (settings.perceive and "ON" or "OFF"))
      Note("Usage: HARVESTER AUTORIFT [ON|OFF]")
    elseif wildcards[1]:lower() == "on" then
      settings.autorift = true
      Note("Autorift is now set to: ON")
    elseif wildcards[1]:lower() == "off" then
      settings.autorift = false
      Note("Autorift is now set to: OFF")
    else
      Note("Invalid option for HARVESTER AUTORIFT.")
      Note("Usage: HARVESTER AUTORIFT [ON|OFF]")
    end
    Send()
  end,

  list = function (wildcards)
    if not wildcards[1] or wildcards[1]:lower() == "show" then
      for key,value in pairs(harvcount) do
        if (key ~= "total") then
          Tell(key)
          local i = 2 - key:len()/8
          while i > 0 do
            Tell("\t")
            i = i - 1
          end
          Note(value)
        end
      end
      Note("~!~\nTotal:\t\t" .. harvcount.total)
    elseif wildcards[1]:lower() == "clear" then
      harvcount = {total = 0}
      Note("Herb counts cleared.")
    else
      Note("Invalid option for HARVESTER LIST.")
      Note("Usage: HARVESTER LIST [CLEAR]")
    end
    Send()
  end,

  [""] = function (wildcards)
    Note("PERCEIVE: " .. (settings.perceive and "ON" or "OFF"))
    Note("AUTORIFT: " .. (settings.autorift and "ON" or "OFF"))
    Send()
  end,

  error = function (wildcards)
    Note("Error: " .. wildcards[0])
    Send()
  end
}


-- Functions called by triggers/aliases

HarvesterConfig = function (name, line, wildcards)
  wildcards[0] = nil
  local temp = wildcards[1]
  table.remove(wildcards, 1)
  wildcards[0] = (temp or "")

  ; (confunc[wildcards[0]:lower()] or confunc.error)(wildcards)
end

DoAutorift = function (name, line, wildcards)
  local num = (#wildcards[1] == 0) and 1 or wildcards[1]

  if settings.autorift then
    SendNoEcho("inr " .. num .. " " .. lastharvested)
  end
  harvcount[lastharvested] = (harvcount[lastharvested] or 0) + num
  harvcount.total = (harvcount.total or 0) + num 
end
 
DoHarvest = function ()
  if #toharvest == 0 then
    EndHarvester()
    return
  end

  SendNoEcho("harvest " .. toharvest[#toharvest])
  lastharvested = toharvest[#toharvest]
  table.remove(toharvest)
end

StartHarvester = function (name, line, wildcards)
  if not running then
    running = true
    if settings.perceive and (wildcards[1] or ""):lower() ~= "grove" then
      EnableGroup("harvest_perceive1", true)
      SendNoEcho("perceive")
    else
      EnableGroup("harvest_scan", true)
      SendNoEcho("plants")
    end
  end
end

EndHarvester = function ()
  EnableGroup("harvest_act", false)
  Note("Harvesting finished.")
  running = false
end

-- Save plugin state here
OnPluginSaveState = function ()
  SetVariable("perceive", (settings.perceive and "1" or "0"))
  SetVariable("autorift", (settings.autorift and "1" or "0"))
end
]]>
</script>
</muclient>