<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE muclient >

<muclient>
   <plugin name="Harvester" author="Keldar" language="lua" purpose="Harvesting plants"
     save_state="y" date_written="2006-05-15" requires="3.80" version="1.2" 
     id="50f66e302539e7c789647d52">
 
    <description trim="y">

<![CDATA[
Commands summary:

  plants              -  display a list of plants
  
  autoharvest on|off  -  turn autoharvesting mode on
                         or off
                         
The output of the plants command will include hyperlinks
for all plants above minimum (harvestable). Clicking on
a plant's hyperlink harvests it.

When in autoharvest mode, clicking on a hyperlink will
make the plugin harvest the plant until its quantity
reaches the legal minimum. To stop autoharvesting when
it is in progress Ctrl+click in the output window and
click on "Stop autoharvest".
]]>
    </description>

</plugin>


<aliases>
<alias
  match="hrv *"
  enabled="y"
  send_to="12"
 ><send>Harvest("%1")</send>
 </alias>
</aliases>
<aliases>
  <alias
   script="GetPlants"
   match="^\s*plants\s*$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>
<aliases>
  <alias
   name="restrict_move"
   match="^\s*(?:n(?:orth)?w(?:est)?|s(?:outh)?w(?:est)?|n(?:orth)?e(?:ast)?|s(?:outh)?e(?:ast)?|e(?:ast)?|w(?:est)?|n(?:orth)?|s(?:outh)?)\s*$"
   regexp="y"
   sequence="100"
  >
  <send>%0</send>
  </alias>
</aliases>
<aliases>
  <alias
   name="Stop_autoharvest"
   match="^stop harvest$"
   regexp="y"
   send_to="12"
   menu="y"
   sequence="100"
  >
  <send>do_autoharvest = false</send>
  </alias>
</aliases>
<aliases>
  <alias
   script="SetHarvestingMode"
   match="^\s*autoharvest\s+(on|off)\s*$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>


<variables>
<variable name="HarvestManual">1</variable>
</variables>

<script> 
<![CDATA[
require "flc.kwait"
require "flc.kutils"
require "tprint"

MINIMUM = 10

do_autoharvest = false

norm_names = {['myrrh bush'] = 'myrrh',
              ['wild ginger'] = 'ginger',
              ['red elm'] = 'elm',
              ['bayberry tree'] = 'bayberry',
              ['irid moss'] = 'moss',
              ["lady's slipper"] = "slipper",
              ["prickly ash tree"] = "ash",
              ["black cohosh"] = "cohosh",
              ["kola tree"] = "kola",
              ["kuzu vine"] = "kuzu",
              ["cactus"] = "pear",
              ["cactus weed"] = "weed"}
      
plants_here = {}



function plantsGetter(tocall)
  kwait.make(function()
    Send("plants")
    
    local line,wildcs,styles = kwait.regexp([[^The following plants are growing in this room\:$]], 5, true)
    local plants = {}
    
   
    while line ~= nil do
      line,wildcs,styles = kwait.regexp([[^(?:(.*?\()(.*?)(\)\s+)(\d{1,2}) left\.|]] .. 
                                        [[\d+h, \d+m(?:, \d+e)?(?:, \d+w)? c?e?x?k?d?b?\@?-)$]], nil, true)
                                        
      if string.find(line, "^%d+h, %d+m") then
        break
      end
      local name = norm_names[ wildcs[2] ]  or wildcs[2]
      plants[name] = {quant = tonumber(wildcs[4]), bits=wildcs}
    end
  
    tocall( plants, styles )
  end)
end


function GetPlants()
  local tocall
  tocall = function()
    plantsGetter(tocall)
    local plants, prompt = coroutine.yield()
    show_plants(plants, prompt)
  end
  
  tocall = coroutine.wrap(tocall)
  tocall()
end

function show_plants(plants, prompt)
  ColourNote("green", "", "The following plants are growing in this room:")
  for plant,info in pairs(plants) do
    if info.quant > MINIMUM then
    	Hyperlink("hrv " .. plant, info.bits[1] .. info.bits[2], "harvest plant", "silver", "", false)
    	ColourTell("silver", "", info.bits[3] .. info.bits[4] .. " left.\n")
    else
      ColourNote("silver", "", info.bits[0])
    end      
  end
  kutils.display_styled(prompt)
end


function Harvest(plant)
  if IsModeManual() then
    Send("harvest " .. plant .. "\ninr all plant")
    GetPlants()
  else
    AutoHarvest(plant)
  end
end

function AutoHarvest(plant)
  local tocall
  tocall = function()
    plantsGetter(tocall)
    local plants = coroutine.yield()
    
    if plants[plant] == nil then
      kutils.DoNote("No " .. plant .. " was found in this location.")
      return
    end
    
    if plants[plant].quant > MINIMUM then
      EnableAlias("Stop_autoharvest", true)
      do_autoharvest = true
    end
    
    local line,wildcs, styles
    local harvested = 0   
    
    while (plants[plant].quant > MINIMUM) and do_autoharvest do
      EnableAlias("restrict_move", true)
      Send("harvest " .. plant)
      line = kwait.regexp([[^You reach out and carefully harvest]], 6)
      if not line then
        break
      end
      
      harvested = harvested + 1
      kutils.DoNote("Harvested " .. tostring(harvested) .. " pieces of " .. plant .. ". " 
                    .. tostring(plants[plant].quant - MINIMUM - 1) .. " to go." )
      
      kwait.regexp ([[^\d+h, \d+m(?:, \d+e)?(?:, \d+w)? c?exk?d?b?\@?-$]])
      plantsGetter(tocall)
      plants = coroutine.yield()
      
      if plants[plant] == nil then
        kutils.DoNote("No " .. plant .. " was found in this location.")
        break
      elseif plants[plant].quant < MINIMUM then
        kutils.DoNote("You have just overharvested, dolt! Now I'll have to clean up your mess.")
        Send("plant " .. plant)
        break
      else
        Send("inr all " .. plant)
      end
   
    end
    
    GetPlants()
    EnableAlias("restrict_move", false)
    EnableAlias("Stop_autoharvest", false)
    do_autoharvest = false
  end
  tocall = coroutine.wrap(tocall)
  tocall()
end

function IsModeManual()
  if (tonumber(GetVariable("HarvestManual")) == 1) then
    return true
  else
    return false
  end
end

function SetModeManual(mode)
  if mode then
    SetVariable("HarvestManual", "1")
  else
    SetVariable("HarvestManual", "0")
  end  
end

function SetHarvestingMode(n,o,wildcs)
  if wildcs[1] == "on" then
    SetModeManual(false)
    kutils.DoNote("You will now continue harvesting a plant down to the minimum. (" .. tostring(MINIMUM) .. ")")
  elseif wildcs[1] == "off" then
    SetModeManual(true)
    kutils.DoNote("You chose to harvest plants one at a time.")
  end
end

]]>        
</script>

</muclient>
