<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Thursday, May 17, 2007, 9:30 AM -->
<!-- MuClient version 4.05 -->

<!-- Plugin "moonbot" generated by Plugin Wizard -->
<!-- TODO: Add ability to show window even when moons are not known - show "Unknown" in the plot -->
<muclient>
<plugin
   name="moonbot"
   author="Balaam/Bast"
   id="2349913955ee86427355ad5e"
   language="Lua"
   purpose="moon phase tracker"
   save_state="y"
   date_written="2007-05-17 09:28:14"
   requires="4.37"
   version="1.5"
   >
</plugin>

<!--  Get our standard constants -->

<include name="constants.lua"/>

<!--  Triggers  -->

<triggers>
  <trigger
   enabled="y"
   group="moons"
   match="--&gt; TICK &lt;--*"
   name="moontick"
   send_to="12"
   sequence="100"
  >
  <send>
for i,v in pairs(moons) do
  if moons[i].now ~= nil then
    moons[i].now = (v.now+1) % v.cycle
  end
end
if countdown == nil then
  countdown, countdowndur = nextunion()
  if countdown == 0 and countdowndur > 0 then
    ColourNote("lime", "black", "Three moons are up for "..countdowndur.." ticks!" )
  end
else
  if countdown == 0 then
    countdowndur = countdowndur - 1
  else
    countdown = countdown - 1
  end
  if countdown == 0 and countdowndur > 0 then
    ColourNote("lime", "black", "Three moons are up for "..countdowndur.." ticks!" )
  elseif countdowndur == 0 then
    countdown, countdowndur = nextunion()
  end
end
styleplotdata()
</send>
  </trigger>
  <trigger
   enabled="y"
   group="moons"
   match="^You notice the (black|grey|white) moon falling to the (ea|we)st.$"
   name="moonsetting"
   regexp="y"
   script="moonsetting"
   sequence="100"
  >
  </trigger>
  <trigger
   enabled="y"
   group="moons"
   match="^You see the (black|grey|white) moon rising in the (ea|we)st.$"
   name="moonrising"
   regexp="y"
   script="moonrising"
   sequence="100"
  >
  </trigger>
</triggers>

<!--  Script  -->
<script>
<![CDATA[
require "utils"
require "commas"

-- var is required for pluginhelper below
require "var"
require "pluginhelper"

-- required for miniwindow functions
require "miniwin"

require "verify"

moonwin = Miniwindow{name="Moons", windowpos=4, font_size=8, font="DejaVu Sans Mono", text_colour="gold"}
moonwin:disable()
moonwin:add_setting("black_colour", {help="the colour for the black moon", type="colour", default=verify_colour("midnightblue"), sortlev=1})
moonwin:add_setting("white_colour", {help="the colour for the white moon", type="colour", default=verify_colour("white"), sortlev=1})
moonwin:add_setting("grey_colour", {help="the colour for the grey moon", type="colour", default=verify_colour("dimgray"), sortlev=1})
moonwin:add_setting("three_colour", {help="the colour for the when three moons are up", type="colour", default=verify_colour("gold"), sortlev=2})
moonwin:add_setting("three_bg_colour", {help="the colour for the background when the three moons are up", type="colour", default=verify_colour("lime"), sortlev=2})


countdown = nil
countdowndur = nil

moons = {
  black={now=nil,cycle=50,up=13},
  grey={now=nil,cycle=30,up=8},
  white={now=nil,cycle=65,up=17}
}

function togglewindow()
  moonwin:togglewindow(moonwin)
end

-- mousedown
function mousedown (flags, hotspotid)
  moonwin:mousedown(flags, hotspotid)
end -- mousedown

function styleplotdata()
  local texttable = {}
  local max_width = 0

  local style = {}

  if (moons.black.now == nil ) or
     (moons.grey.now == nil ) or
     (moons.white.now == nil ) then
     moonwin:disable()  
     return
  end
  
  -- heading
  
  style = {}
  style.text = "Moon Plot:  "
  max_width = math.max (max_width, #style.text)
  style.len = #style.text
  --style.start = 20
  style.style = 0
  table.insert (texttable, {style})
  
  local tplotdata=buildplotdata(var.plotlength)
  for i,t in pairs(tplotdata) do
    local tstyle = {}
    style = {}
    style.text = "["
    style.len = 1
    style.style = 0
    table.insert (tstyle, style)    
    for u,v in ipairs(t) do
      style = {}
      style.backcolour = moonwin.header_colour
      style.style = 0
      style.len = 1
      if v then
        style.text = "O"
        style.textcolour = i.."_colour"
        union = true
        for moon,_ in pairs(moons) do
          union = union and tplotdata[moon][u]
        end
        if union then
          style.textcolour = 'three_colour'
          style.backcolour = 'three_bg_colour'
        end
      else
        style.text = "."
      end
      table.insert (tstyle, style)      
    end
    style = {}
    style.text = "]"
    style.len = #style.text
    style.style = 0
    table.insert (tstyle, style)
    table.insert (texttable, tstyle)
  end

  local offset
  local duration 
  offset, duration = nextunion()
  style = {}
  if offset == 0 then
    style.text = "3 moons are up for " .. tostring(duration) .. " ticks!!"  
    style.textcolour = 'three_colour'
    local mstyle = {}
    mstyle.text = tostring(0) .. ":" .. tostring(duration)
    mstyle.len = #mstyle.text
    mstyle.start = "mid"
    mstyle.textcolour = 'three_colour'
    moonwin.winhide:createwin({"", {mstyle}})
  else
    style.text = "Next Union in " .. tostring(offset) .. " ticks for " .. tostring(duration) .. " ticks."
    local mstyle = {}
    mstyle.text = tostring(offset) .. ":" .. tostring(duration)
    mstyle.len = #mstyle.text
    mstyle.start = "mid"
    moonwin.winhide:createwin({"", {mstyle}})
  end
  max_width = math.max (max_width, #style.text)
  style.len = #style.text
  style.style = 0
  table.insert (texttable, {style})  
  
  moonwin:enable()
  moonwin:createwin(texttable)  
  
end -- plotdatastyle


function plotdata()
  if (moons.black.now == nil ) or
     (moons.grey.now == nil ) or
     (moons.white.now == nil ) then
    Note( "The cycle of one or more moons is unknown.")
  else
    local tplotdata=buildplotdata(var.plotlength)
    for i,t in pairs(tplotdata) do
      local temp = "["
      for _,v in ipairs(t) do
        if v then
          temp = temp.."O"
        else
          temp = temp.."."
        end
      end
      print(temp.."] "..i)
    end
  end
end


function buildplotdata(length)
  local tplotdata={white={},grey={},black={}}
  for i = 0,length do
    for m,t in pairs(tplotdata) do
      table.insert(t, ((moons[m].now + i ) % moons[m].cycle) < moons[m].up)
    end
  end
  return tplotdata
end


function moonsetting( tname, tstr, wildcards )
  local moon = wildcards[1]
  moons[moon].now = moons[moon].up - 2
end-- moonsetting


function moonrising( tname, tstr, wildcards)
  local moon = wildcards[1]
  moons[moon].now = moons[moon].cycle - 1
end


function printmoons()
  Note( "Black="..(moons.black.now or "nil")..
        " Grey="..(moons.grey.now or "nil")..
        " White="..(moons.white.now or "nil") )
end

function nextunion()
  if (moons.black.now == nil ) or
     (moons.grey.now == nil ) or
     (moons.white.now == nil ) then
    return nil,nil
  else
    local offset = 0
    while ((moons.white.now + offset) % moons.white.cycle >= moons.white.up) or
          ((moons.black.now + offset) % moons.black.cycle >= moons.black.up) or
          ((moons.grey.now + offset) % moons.grey.cycle >= moons.grey.up) do
      offset = offset + 1
    end
    local duration = 1
    while ((moons.white.now + offset + duration) % moons.white.cycle < moons.white.up) and
          ((moons.black.now + offset + duration) % moons.black.cycle < moons.black.up) and
          ((moons.grey.now + offset + duration) % moons.grey.cycle < moons.grey.up) do
      duration = duration + 1
    end
    return offset,duration
  end
end -- nextunion()

function check_tickgag()
  if var.tickgag then
     temp = "y"
  else
     temp = "n"
  end
  SetTriggerOption( "moontick", "omit_from_output", temp )
  return true
end -- tickgag

function OnPluginInstall()
  if GetVariable ("enabled") == "false" then
    ColourNote ("yellow", "", "Warning: Plugin " .. GetPluginName ().. " is currently disabled.")
    check (EnablePlugin(GetPluginID (), false))
    moonwin:disable()
    return
  end -- they didn't enable us last time
  OnPluginEnable ()  -- do initialization stuff              
  plugin_help_helper("", "", "", plugin_options)
end -- OnPluginInstall

function OnPluginClose()
  ColourNote( "white", "black", "Plugin ",
              RGBColourToName(var.plugin_colour),  "black",  GetPluginName(),
          "white", "black", " closed." )
  moonwin:disable()          
end -- OnPluginClose

function OnPluginDisable ()
  moonwin:disable()
end -- OnPluginDisable

function OnPluginSaveState()
  plugin_save_vars(set_options)
  moonwin:savestate()
end

function OnPluginEnable ()
  moonbot_reset() 
end -- OnPluginEnable

function OnPluginConnect ()
  moonbot_reset()
end -- OnPluginConnect
]]>
</script>

<!--  Aliases  -->

<aliases>
  <alias
   name="plugin_parse"
   script="plugin_parse"
   match="nothing"
   enabled="y"
   regexp="y"
   ignore_case="y"
   sequence="110"
   expand_variables="y"
  >
  </alias>   
</aliases>

<script>
<![CDATA[

function moonbot_union(name, line, wildcards)
  print(nextunion())
  return true
end -- moonbot_union

function moonbot_reset()
  for i,v in pairs(moons) do
    moons[i].now = nil
  end
  countdown = nil
  countdowndur = nil
  moonwin:disable()
  ColourNote("", "", "")  
  ColourNote( "orange",  "black", "Moonbot reset" )
  ColourNote("", "", "")
end -- moonbot_reset

set_options  = {
  plotlength = {help="set the length of the moon plot", type="number", high=80, low=0, after=styleplotdata, default=66},
  plugin_colour = {help="set the plugin colour", type="colour", default="lime"},
  tickgag = {help="toggle gagging the tick", type="bool", after=check_tickgag,default=false},
  shortcmd = {help="the short command for this plugin", type="string", after=set_plugin_alias, default="mb"},
  longcmd = {help="the long command for this plugin", type="string", after=set_plugin_alias, default="moonbot"},
}

plugin_options = {
  plot      = {func=plotdata, help="plot moons"},
  union     = {func=moonbot_union, help="show next moons union"}, 
  print     = {func=printmoons, help="print the current moon phase #s"},
  toggle    = {func=togglewindow, help="toggle the moons miniwindow"}, 
  reset     = {func=moonbot_reset, help="reset the moons"},
}
  
function plugin_parse (name, line, wildcards)
  plugin_parse_helper(name, line, wildcards, plugin_options, set_options, moonwin)
end -- plugin_parse

init_plugin_vars(set_options)
set_plugin_alias()
]]>
</script>

</muclient>
