Advertisement
Redxone

Grid - CC Api

Feb 18th, 2016
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.35 KB | None | 0 0
  1.  
  2. -- ] Created by: Redxone(Lewisk3) [ --
  3. -- ]    (c)Lewisk3 Corpz 2016     [ --
  4.  
  5. -- DOCUMENTATION (Sorta...) --
  6. --] grid.create(width,height)
  7. --] grid:set(x,y,thing)
  8. --] grid:find(obj) - returns the found objects x and y location
  9. --] grid:findByAttrib(attrib,compare)
  10. --] grid:get(x,y) - returns the object in the x any y of the grid
  11. --] grid:getByAttrib(attrib,compare)
  12. --] grid:getWidth() - returns width of grid
  13. --] grid:getHeight() - returns grid height
  14. --] grid:unset(x,y) - set grid spot to {}
  15. --] grid:getRaw() - returns entire grid unserialized
  16. --] grid:getSerial() - returns entire grid serialized
  17. --] grid:replace(newgrid) - replaces grid with another grid
  18. --] grid:draw()
  19. --] grid:drawAttrib(attrib)
  20. --] grid:fill(with) - fills grid with something
  21. --] grid:setRegion(startx,starty,endx,endy,with) - fills grid from start x and start y to ends with something
  22. --] grid:drawRegion(startx,starty,endx,endy)
  23.  
  24. function create(w, h)
  25.     local gridtable = {
  26.         maxw = w,
  27.         maxh = h,
  28.         grid = {},
  29.  
  30.         set = function(self,x,y,obj)
  31.             if(x <= self.maxw and y <= self.maxh)then
  32.                 self.grid[y][x] = obj
  33.             else
  34.                 error("grid:set -> Invaild X and/or Y param(s).")
  35.             end
  36.         end,
  37.  
  38.         find = function(self,obj)
  39.             for y = 0, self.maxh do
  40.                 for x = 0, self.maxw do
  41.                     if(textutils.serialize(self.grid[y][x]) == textutils.serialize(obj))then
  42.                         return x, y
  43.                     end
  44.                 end
  45.              end
  46.         end,
  47.  
  48.         get = function(self,x,y)
  49.             local tbl = {}
  50.             if(x <= self.maxw and y <= self.maxh )then
  51.                 return self.grid[y][x]
  52.             else
  53.                 return tbl
  54.             end
  55.         end,
  56.  
  57.         getByAttribEqual = function(self,a,c)
  58.             local gx, gy = 0, 0
  59.             for y = 0, self.maxh do
  60.                 for x = 0, self.maxw do
  61.                     if(self.grid[y][x][a] == c)then
  62.                         gx = x
  63.                         gy = y
  64.                     end
  65.                 end
  66.              end
  67.             return self:get(gx,gy)
  68.         end,
  69.  
  70.         checkAttribEqual = function(self,x,y,a,e)
  71.              if(self.grid[y][x][a] == c)then
  72.                 return true
  73.              else
  74.                 return false
  75.              end
  76.         end,
  77.  
  78.         getByAttrib = function(self,a)
  79.             local xx, yy = 0, 0
  80.             for y = 0, self.maxh do
  81.                 for x = 0, self.maxw do
  82.                     if(self.grid[y][x][a] ~= nil)then
  83.                          xx = x
  84.                          yy = y
  85.                     end
  86.                 end
  87.              end
  88.  
  89.              if(xx ~= 0 and yy ~= 0)then
  90.                 return self:get(xx,yy)[a]
  91.              else
  92.                 return {}
  93.              end
  94.         end,
  95.  
  96.         setByAttrib = function(self,a,n)
  97.             for y = 0, self.maxh do
  98.                 for x = 0, self.maxw do
  99.                     if(self.grid[y][x][a] ~= nil)then
  100.                          local bset = self.grid[y][x]
  101.                          bset[a] = n
  102.                          self:set(x,y,bset)
  103.                     end
  104.                 end
  105.              end
  106.         end,
  107.  
  108.         getWidth = function(self)
  109.             return self.maxw
  110.         end,
  111.  
  112.         getHeight = function(self)
  113.             return self.maxh
  114.         end,
  115.  
  116.         unset = function(self,x,y)
  117.             if(x <= self.maxw and y <= self.maxh)then
  118.                 self.grid[y][x] = {}
  119.             else
  120.                 error("grid:unset -> Invaild X and/or Y param(s).")
  121.             end
  122.         end,
  123.  
  124.         getRaw = function(self)
  125.             return self.grid
  126.         end,
  127.  
  128.         getSerial = function(self)
  129.             return textutils.serialize(self.grid):gsub("\n%s*","")
  130.         end,
  131.  
  132.         replace = function(self,grid)
  133.             if(type(grid) ~= "table")then error("grid:replace -> Invalid grid type. ") end
  134.              -- calculating grid size... --
  135.              self.maxh = 0
  136.              self.maxw = 0
  137.  
  138.              for iy = 1, #grid do
  139.                 self.maxh = self.maxh + 1
  140.              end
  141.  
  142.              for ix = 1, #grid[0] do
  143.                 self.maxw = self.maxw + 1
  144.              end
  145.  
  146.              self.grid = grid
  147.  
  148.              if(self.maxh <= 0 or self.maxw <= 0)then error("grid:replace -> Grid is malformed.") end
  149.  
  150.         end,
  151.  
  152.         draw = function(self)
  153.             for y = 0, self.maxh do
  154.                 for x = 0, self.maxw do
  155.                     term.setCursorPos(x,y)
  156.                     if(self.grid[y][x] ~= {})then
  157.                         term.write(self.grid[y][x])
  158.                     end
  159.                 end
  160.              end
  161.         end,
  162.  
  163.         swap = function(self,fromx,fromy,tox,toy)
  164.             local prevo = self.grid[fromy][fromx]
  165.             local too = self.grid[toy][tox]
  166.             self.grid[fromy][fromx] = too
  167.             self.grid[toy][tox] = prevo
  168.         end,
  169.  
  170.         moveInto = function(self,obj,x,y,uap,flags)
  171.             local prloc = self:get(x,y)
  172.             local ox, oy = self:find(obj)
  173.             -- only take place if flags on other block are met --
  174.             if(type(flags) == "table")then
  175.                 for k, v in pairs(flags) do
  176.                     if(not self:checkAttribEqual(x,y,k,v))then
  177.                         return false
  178.                     end
  179.                 end
  180.             end
  181.             -- if passes flags --
  182.             if(type(uap) == "table")then
  183.                 for k, v in pairs(uap) do
  184.                     obj[k] = v
  185.                 end            
  186.             end
  187.             self:set(ox,oy,prloc)
  188.             self:set(x,y,obj)
  189.         end,
  190.  
  191.         renderSwap = function(self, charattrib, tcolattrib, bcolattrib, fromx,fromy,tox,toy)
  192.             self:swap(fromx,fromy,tox,toy)     
  193.             self:renderRegion(charattrib, tcolattrib, bcolattrib,tox,toy,tox,toy)
  194.             self:renderRegion(charattrib, tcolattrib, bcolattrib,fromx,fromy,fromx,fromy)
  195.         end,
  196.  
  197.         render = function(self,charattrib,tcolattrib,bcolattrib)
  198.             local ta = colors.white
  199.             local ba = colors.black
  200.             for y = 0, self.maxh do
  201.                 for x = 0, self.maxw do
  202.                     term.setCursorPos(x,y)
  203.                     term.setTextColor(self.grid[y][x][tcolattrib])
  204.                     term.setBackgroundColor(self.grid[y][x][bcolattrib])
  205.                         ta = colors.white
  206.                         ba = colors.black
  207.                         if(type(tcolattrib) == "number")then ta = tcolattrib end
  208.                         if(type(bcolattrib) == "number")then ba = bcolattrib end
  209.                         if(self.grid[y][x][charattrib] ~= nil)then
  210.                             if(self.grid[y][x][tcolattrib] ~= nil)then
  211.                                 ta = self.grid[y][x][tcolattrib]
  212.                             end
  213.                             if(self.grid[y][x][bcolattrib] ~= nil)then
  214.                                 ba = self.grid[y][x][bcolattrib]
  215.                             end
  216.                             term.setTextColor(ta)
  217.                             term.setBackgroundColor(ba)
  218.                             term.write(self.grid[y][x][charattrib])
  219.                         end
  220.                 end
  221.              end
  222.         end,
  223.  
  224.         drawAttrib = function(self,a)
  225.             for y = 0, self.maxh do
  226.                 for x = 0, self.maxw do
  227.                     term.setCursorPos(x,y)
  228.                     if(self.grid[y][x][a] ~= nil)then
  229.                         term.write(self.grid[y][x][a])
  230.                     end
  231.                 end
  232.              end
  233.         end,
  234.  
  235.         fill = function(self,obj)
  236.             for y = 0, self.maxh do
  237.                 for x = 0, self.maxw do
  238.                     self.grid[y][x] = obj
  239.                 end
  240.              end
  241.         end,
  242.  
  243.         setCursorPos = function(self,x,y)
  244.             term.setCursorPos(y,x)
  245.         end,
  246.  
  247.         loop = function(self,f,startx,starty,endx,endy)
  248.             if(startx == nil)then startx = 0 end
  249.             if(starty == nil)then starty = 0 end
  250.             if(endx == nil and startx ~= nil)then endx = startx end
  251.             if(endy == nil and starty ~= nil)then endy = starty end
  252.             if(endx == nil)then endx = self.maxw end
  253.             if(endy == nil)then endy = self.maxh end
  254.             for y = tonumber(starty), tonumber(endy) do
  255.                 for x = tonumber(startx), tonumber(endx) do
  256.                     f(x,y)
  257.                 end
  258.             end
  259.         end,
  260.  
  261.         setRegion = function(self, sx, sy, ex, ey, obj)
  262.             if(sx <= self.maxw and sy <= self.maxh and ex <= self.maxw and ey <= self.maxh)then
  263.                 for y = tonumber(sy), tonumber(ey) do
  264.                     for x = tonumber(sx), tonumber(ex) do
  265.                         self.grid[y][x] = obj
  266.                     end
  267.                  end
  268.             else
  269.                 error("grid:setRegion -> Invaild X and/or Y param(s).")
  270.             end
  271.         end,
  272.  
  273.         drawRegion = function(self, sx, sy, ex, ey)
  274.             if(sx <= self.maxw and sy <= self.maxh and ex <= self.maxw and ey <= self.maxh)then
  275.                 for y = tonumber(sy), tonumber(ey) do
  276.                     for x = tonumber(sx), tonumber(ex) do
  277.                         term.setCursorPos(x,y)
  278.                         if(self.grid[y][x] ~= {})then
  279.                             term.write(self.grid[y][x])
  280.                         end
  281.                     end
  282.                  end
  283.             else
  284.                 error("grid:drawRegion -> Invaild X and/or Y param(s).")
  285.             end
  286.         end,
  287.  
  288.         renderRegion = function(self, charattrib, tcolattrib, bcolattrib, sx, sy, ex, ey)
  289.             if(sx <= self.maxw and sy <= self.maxh and ex <= self.maxw and ey <= self.maxh)then
  290.                 local ta = colors.white
  291.                 local ba = colors.black
  292.                 for y = tonumber(sy), tonumber(ey) do
  293.                     for x = tonumber(sx), tonumber(ex) do
  294.                         term.setCursorPos(x,y)
  295.                         ta = colors.white
  296.                         ba = colors.black
  297.                         if(self.grid[y][x][charattrib] ~= nil)then
  298.                             if(self.grid[y][x][tcolattrib] ~= nil)then
  299.                                 ta = self.grid[y][x][tcolattrib]
  300.                             end
  301.                             if(self.grid[y][x][bcolattrib] ~= nil)then
  302.                                 ba = self.grid[y][x][bcolattrib]
  303.                             end
  304.                             term.setTextColor(ta)
  305.                             term.setBackgroundColor(ba)
  306.                             term.write(self.grid[y][x][charattrib])
  307.                         end
  308.                     end
  309.                  end
  310.             else
  311.                 error("grid:renderRegion -> Invaild X and/or Y param(s).")
  312.             end
  313.         end,
  314.  
  315.     }
  316.  
  317.  
  318.     -- generate grid locally --
  319.     for y = 0, h do
  320.         gridtable.grid[y] = {}
  321.         for x = 0, w do
  322.             gridtable.grid[y][x] = {}
  323.         end
  324.     end
  325.  
  326.     -- return grid to user --
  327.     return gridtable
  328. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement