Advertisement
Anaristos

Moonbot [beta] (No front end)

Jan 20th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.26 KB | None | 0 0
  1. Moon    = {}
  2. Orbit   = {}
  3. Moonbot = {}
  4. --
  5. Moon.__index    = Moon
  6. Orbit.__index   = Orbit
  7. Moonbot.__index = Moonbot
  8. --
  9. function Moon.ctor(above, below) -- Constructor
  10. -- 
  11.     local self = {} -- PUBLIC FUNCTIONS CONTAINER
  12.    
  13.     -- #PRIVATE FIELDS
  14.     local cd = '' -- grey
  15.     local cg = '' -- green
  16.     local cm = '' -- magenta
  17.     local cn = '' -- black
  18.     local cr = '' -- red
  19.     local cw = '' -- white
  20.     local cy = '' -- yellow
  21.     local rc = ''    -- reset
  22.    
  23.     local above   = above or -1 -- Number of ticks this moon is visible.
  24.     local below   = below or -1 -- Number of ticks this moon is not visible.
  25.     local current = -1          -- Number of ticks left before leaving this part of the sky.
  26.     local gmcp    = 1           -- Adjustment based on whether gmcp tick processing is on or off.      
  27.     local rising  = false       -- Indicates whether the moon is above or below the horizon.
  28.     local color   = "black"     -- color of the display digits returned by the ToString method.
  29.     local ansi    = false       -- Use ANSI color sequences.
  30.    
  31.     -- #PRIVATE METHODS
  32.     local function starts(str,strt)
  33.     --
  34.               return str:sub(1,strt:len()) == strt
  35.     --
  36.           end
  37.    
  38.     local function ends(str,endstr)
  39.     --
  40.               return endstr == '' or str:sub(-endstr:len()) == endstr
  41.     --
  42.           end
  43.    
  44.     -- #PUBLIC METHODS
  45.     function self.isDefined()
  46.     --
  47.         return current ~= -1
  48.     --
  49.     end
  50.    
  51.     function self.getColor()
  52.     --
  53.         return color
  54.     --
  55.     end
  56.    
  57.     function self.getOrbitalInterval()
  58.     --
  59.         return above + below
  60.     --
  61.     end
  62.    
  63.     function self.getState()
  64.     --
  65.         return rising
  66.     --
  67.     end
  68.    
  69.     function self.getLocation()
  70.     --
  71.         if self.isDefined() then
  72.             return current * (self.getState() and 1 or -1)
  73.         else
  74.             return 0
  75.         end
  76.     --
  77.     end
  78.    
  79.     function self.getPosition()
  80.     --
  81.         if rising or not self.isDefined() then return current end
  82.        
  83.         return current + above
  84.     --
  85.     end
  86.    
  87.     function self.getTimeAloft()
  88.     --
  89.         return above;
  90.     --
  91.     end
  92.    
  93.     function self.setPosition(p) -- Useful only during the test phase. Remove afterwards.
  94.     --
  95.         if typeof(p) ~= "number" or p == 0 then return
  96.        
  97.         current = math.abs(p)
  98.        
  99.         rising  = p > 0
  100.     --
  101.     end
  102.    
  103.     function self.isANSI()
  104.     --
  105.         return ansi
  106.     --
  107.     end
  108.    
  109.     function self.isGMCP()
  110.     --
  111.         return gmcp == 0
  112.     --
  113.     end
  114.    
  115.     function self.moonFalling()
  116.     --
  117.         current = 1 + gmcp
  118.        
  119.         rising = true
  120.     --
  121.     end
  122.    
  123.     function self.moonRising()
  124.     --
  125.         current = above + gmcp
  126.        
  127.         rising = true
  128.     --
  129.     end
  130.    
  131.     function self.setANSI(status)
  132.     --
  133.         ansi = false
  134.        
  135.         if type(status) == "boolean" then ansi = status end
  136.     --
  137.     end
  138.    
  139.     function self.setGMCP(status)
  140.     --
  141.         local b = false
  142.        
  143.         if type(status) == "boolean" then b = status end
  144.        
  145.         gmcp = b and 0 or 1
  146.     --
  147.     end
  148.    
  149.     function self.setColor(color)
  150.     --
  151.         if type(color) == "string" then    
  152.             if not ansi or (starts(color,'[') and ends(color,'m')) then color = color end
  153.         end
  154.     --
  155.     end
  156.    
  157.     function self.stop()
  158.     --
  159.         current = -1
  160.        
  161.         rising = false
  162.     --
  163.     end
  164.    
  165.     function self.tick()
  166.     --
  167.         if self.isDefined() then
  168.             current = current - 1
  169.             if current == 0 then
  170.                 if rising then
  171.                     rising  = false
  172.                     current = below
  173.                 else
  174.                     rising  = true
  175.                     current = above
  176.                 end
  177.             end
  178.         end
  179.     --
  180.     end
  181.    
  182.     function self.show()
  183.     --
  184.         local disp = cd.."Undefined"..rc
  185.         local head = ""
  186.         local tick = "tick"
  187.        
  188.         if not ansi then disp = "<color gray>Undefined</color>" end
  189.        
  190.         if  current ~= 1 then tick = tick.."s" end
  191.        
  192.         if self.isDefined() then
  193.             if ansi then
  194.                 head = self.getState() and cg.."ON" or cr.."OFF"
  195.                 disp = head..cw.." for "..color..current..cw.." "..tick..rc
  196.             else
  197.                 head = self.getState() and '"0x00FF00">ON' or "red>OFF"
  198.                 disp = "<color "..head.."</color><color white> for </color><color "..color..">"
  199.                 disp = disp..current.."</color><color white> "..tick.."</color>"
  200.             end
  201.         end
  202.        
  203.         return disp
  204.     --
  205.     end
  206.    
  207.     return self
  208. --
  209. end
  210. --
  211. setmetatable(Moon, { __call = function(_, ...) return Moon.ctor(...) end })
  212. --
  213. function Orbit.ctor(moon)
  214. --
  215. --
  216.     local self = {} -- PUBLIC FUNCTIONS CONTAINER
  217.    
  218.     -- #PRIVATE FIELDS
  219.     local moon  = moon or nil -- Reference to the moon object.
  220.     local above = 0           -- Maximum number of ticks this moon is visible.
  221.     local len   = 0           -- Length of an orbital cycle.
  222.     local pos   = -1          -- Actual moon position at computation time.
  223.     local temp  = 0           -- Utility variable.
  224.    
  225.     -- #PUBLIC METHODS
  226.     function self.setMoon(m)
  227.     --
  228.         moon  = m
  229.         above = moon.getTimeAloft()
  230.         len   = moon.getOrbitalInterval()
  231.     --
  232.     end
  233.  
  234.     function self.move(tref)
  235.     --
  236.         temp = (((pos + tref.ticks) % len) + len) % len
  237.         if temp > above or temp == 0 then
  238.             tref.ticks = tref.ticks - 1
  239.             return true
  240.         end
  241.        
  242.         return false
  243.     --
  244.     end
  245.    
  246.     function self.reset()
  247.     --
  248.         if moon then
  249.             pos = moon.getPosition()
  250.         end
  251.     --
  252.     end
  253.    
  254.     function self.track(ticks)
  255.     --
  256.         temp = (((pos + ticks) % len) + len) % len
  257.         if (temp > above or temp == 0) then
  258.             return false
  259.         end
  260.        
  261.         return true
  262.     --
  263.     end
  264.    
  265.     if moon then self.setMoon(moon) end
  266.    
  267.     return self
  268. --
  269. end
  270. --
  271. setmetatable(Orbit, { __call = function(_, ...) return Orbit.ctor(...) end })
  272. --
  273. function Moonbot.ctor()
  274. --
  275.     local self = {} -- PUBLIC FUNCTIONS CONTAINER
  276.    
  277.     -- #PRIVATE FIELDS 
  278.     local moons = {} -- Moon dictionary.
  279.    
  280.     local _open = false
  281.    
  282.     local tickinfo = -1 -- projected number of ticks until tri-moon event.
  283.     local ticklen  = -1 -- length, in ticks, of tri-moon event.
  284.    
  285.     local tref  = { ticks = 0 } -- this is the tick reference object.
  286.     local ticks = 0
  287.    
  288.     local ob -- Orbital object for black moon.
  289.     local og -- Orbital object for grey  moon.
  290.     local ow -- Orbital object for white moon.
  291.    
  292.     local interval = 0
  293.    
  294.     -- #PRIVATE METHODS
  295.     local function starts(str,strt)
  296.     --
  297.                     return str:sub(1,strt:len()) == strt
  298.     --
  299.           end
  300.    
  301.     local function ends(str,endstr)
  302.     --
  303.               return endstr == '' or str:sub(-endstr:len()) == endstr
  304.     --
  305.           end
  306.          
  307.     local function init()
  308.     --
  309.                         moons = {  white = Moon(17,48)
  310.                                   ,grey  = Moon(8 ,22)
  311.                                   ,black = Moon(13,37)
  312.                                 }
  313.                
  314.                         og = Orbit(moons.grey)
  315.                         ob = Orbit(moons.black)
  316.                         ow = Orbit(moons.white)
  317.        
  318.                         _open = true
  319.     --
  320.                    end
  321.    
  322.     -- #PUBLIC METHODS 
  323.     function self.displayMoons()
  324.     --
  325.         local mtab = {}
  326.        
  327.         for k, v in pairs(moons)
  328.             do
  329.                 mtab[k] = v.show()
  330.             end
  331.            
  332.         return mtab
  333.     --
  334.     end
  335.    
  336.     function self.moonsAreDefined()
  337.     --
  338.         if not _open then return false end
  339.        
  340.         for _, v in pairs(moons)
  341.             do
  342.                 if not v.isDefined() then
  343.                     return false
  344.                 end
  345.             end
  346.            
  347.         return true
  348.     --
  349.     end
  350.    
  351.     function self.getColor(name)
  352.     --
  353.         if type(name) == "string" then
  354.             if moons[name] then
  355.                 return moons[name].getColor()
  356.             end
  357.         end
  358.     --
  359.     end
  360.    
  361.     function self.getMoons()
  362.     --
  363.         local mtab = {}
  364.        
  365.         for k, v in pairs(moons)
  366.             do
  367.                 mtab[k] = v.getLocation()
  368.             end
  369.            
  370.         return mtab
  371.     --
  372.     end
  373.    
  374.     function self.isANSI()
  375.     --
  376.         for _, v in pairs(moons)
  377.             do
  378.                 if not v.isANSI() then return false end
  379.             end
  380.            
  381.         return true
  382.     --
  383.     end
  384.    
  385.     function self.isGMCP()
  386.     --
  387.         for _, v in pairs(moons)
  388.             do
  389.                 if not v.isGMCP() then return false end
  390.             end
  391.            
  392.         return true
  393.     --
  394.     end
  395.    
  396.     function self.isOpen()
  397.     --
  398.         return _open
  399.     --
  400.     end
  401.    
  402.     function self.setPosition(moon, pos)
  403.     --
  404.         if type(moon) == "string" and type(pos) == "number" and moons[moon] then
  405.             moons[moon].setPosition(pos)
  406.         end
  407.     --
  408.     end
  409.    
  410.     function self.predict() -- compute time interval and length of the next tri-moon event.
  411.     --
  412.         tickno  = -1
  413.         ticklen = -1
  414.        
  415.         if self.moonsAreDefined() then
  416.             ob.reset()
  417.             og.reset()
  418.             ow.reset()
  419.            
  420.             tref.ticks = 0 -- tick count is passed to the move routine by reference.
  421.            
  422.             while true -- here we loop until all moons are visible in the sky.
  423.                 do
  424.                     if not og.move(tref) then         -- loop until grey  moon appears in the sky.
  425.                         if not ob.move(tref) then     -- loop until black moon appears in the sky.
  426.                             if not ow.move(tref) then -- loop until white moon appears in the sky.
  427.                                 break                 -- at this point all moons are visible.
  428.                             end
  429.                         end
  430.                     end
  431.                 end
  432.            
  433.             interval = 0
  434.            
  435.             ticks  = tref.ticks
  436.            
  437.             while true -- here we loop until one of the moons disappears from the sky.
  438.                 do             
  439.                     if not og.track(ticks) then break end -- test grey  moon position.
  440.                
  441.                     if not ob.track(ticks) then break end -- test black moon position.
  442.                
  443.                     if not ow.track(ticks) then break end -- test white moon position.
  444.                    
  445.                     interval = interval + 1 -- increment tri-moon event interval length.
  446.                    
  447.                     ticks = ticks - 1 -- decrement orbital tracking pointer.
  448.                 end        
  449.            
  450.             tickno  = math.abs(tref.ticks)
  451.             ticklen = interval
  452.         end
  453.        
  454.         return { tickno = tickno, ticklen = ticklen }
  455.     --
  456.     end
  457.    
  458.     function self.moonFalling(name)
  459.     --
  460.         if _open then
  461.             if type(name) == "string" then
  462.                 if moons[name] then
  463.                     moons[name].moonFalling()
  464.                 end
  465.             end
  466.         end
  467.     --
  468.     end
  469.    
  470.     function self.moonRising(name)
  471.     --
  472.         if _open then
  473.             if type(name) == "string" then
  474.                 if moons[name] then
  475.                     moons[name].moonRising()
  476.                 end
  477.             end
  478.         end
  479.     --
  480.     end
  481.    
  482.     function self.mudTick()
  483.     --
  484.         if _open then
  485.             for _, v in pairs(moons)
  486.                 do
  487.                     v.tick()
  488.                 end
  489.         end
  490.     --
  491.     end
  492.    
  493.     function self.setANSI(state)
  494.     --
  495.         b = state
  496.         if type(state) ~= "boolean" then b = false end
  497.        
  498.         for _,v in pairs(moons)
  499.             do
  500.                 v.setANSI(b)
  501.             end
  502.     --
  503.     end
  504.    
  505.     function self.setGMCP(state)
  506.     --
  507.         b = state
  508.         if type(state) ~= "boolean" then b = false end
  509.        
  510.         for _,v in pairs(moons)
  511.             do
  512.                 v.setGMCP(b)
  513.             end
  514.     --
  515.     end
  516.  
  517.     function self.setStatus(status)
  518.     --
  519.         if type(status) == "boolean" then
  520.             _open = status
  521.             for _, v in pairs(moons)
  522.                 do
  523.                     v.stop()
  524.                 end
  525.         end
  526.     --
  527.     end
  528.    
  529.     function self.BlackMoon()
  530.     --
  531.         return moons.black.show()
  532.     --
  533.     end
  534.    
  535.     function self.GreyMoon()
  536.     --
  537.         return moons.grey.show()
  538.     --
  539.     end
  540.    
  541.     function self.WhiteMoon()
  542.     --
  543.         return moons.white.show()
  544.     --
  545.     end
  546.    
  547.     init()
  548.    
  549.     return self
  550. --
  551. end
  552. --
  553. setmetatable(Moonbot, { __call = function(_, ...) return Moonbot.ctor(...) end })
  554. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement