Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ] Created by: Redxone(Lewisk3) [ --
- -- ] (c)Lewisk3 Corpz 2016 [ --
- -- DOCUMENTATION (Sorta...) --
- --] grid.create(width,height)
- --] grid:set(x,y,thing)
- --] grid:find(obj) - returns the found objects x and y location
- --] grid:findByAttrib(attrib,compare)
- --] grid:get(x,y) - returns the object in the x any y of the grid
- --] grid:getByAttrib(attrib,compare)
- --] grid:getWidth() - returns width of grid
- --] grid:getHeight() - returns grid height
- --] grid:unset(x,y) - set grid spot to {}
- --] grid:getRaw() - returns entire grid unserialized
- --] grid:getSerial() - returns entire grid serialized
- --] grid:replace(newgrid) - replaces grid with another grid
- --] grid:draw()
- --] grid:drawAttrib(attrib)
- --] grid:fill(with) - fills grid with something
- --] grid:setRegion(startx,starty,endx,endy,with) - fills grid from start x and start y to ends with something
- --] grid:drawRegion(startx,starty,endx,endy)
- function create(w, h)
- local gridtable = {
- maxw = w,
- maxh = h,
- grid = {},
- set = function(self,x,y,obj)
- if(x <= self.maxw and y <= self.maxh)then
- self.grid[y][x] = obj
- else
- error("grid:set -> Invaild X and/or Y param(s).")
- end
- end,
- find = function(self,obj)
- for y = 0, self.maxh do
- for x = 0, self.maxw do
- if(textutils.serialize(self.grid[y][x]) == textutils.serialize(obj))then
- return x, y
- end
- end
- end
- end,
- get = function(self,x,y)
- local tbl = {}
- if(x <= self.maxw and y <= self.maxh )then
- return self.grid[y][x]
- else
- return tbl
- end
- end,
- getByAttribEqual = function(self,a,c)
- local gx, gy = 0, 0
- for y = 0, self.maxh do
- for x = 0, self.maxw do
- if(self.grid[y][x][a] == c)then
- gx = x
- gy = y
- end
- end
- end
- return self:get(gx,gy)
- end,
- checkAttribEqual = function(self,x,y,a,e)
- if(self.grid[y][x][a] == c)then
- return true
- else
- return false
- end
- end,
- getByAttrib = function(self,a)
- local xx, yy = 0, 0
- for y = 0, self.maxh do
- for x = 0, self.maxw do
- if(self.grid[y][x][a] ~= nil)then
- xx = x
- yy = y
- end
- end
- end
- if(xx ~= 0 and yy ~= 0)then
- return self:get(xx,yy)[a]
- else
- return {}
- end
- end,
- setByAttrib = function(self,a,n)
- for y = 0, self.maxh do
- for x = 0, self.maxw do
- if(self.grid[y][x][a] ~= nil)then
- local bset = self.grid[y][x]
- bset[a] = n
- self:set(x,y,bset)
- end
- end
- end
- end,
- getWidth = function(self)
- return self.maxw
- end,
- getHeight = function(self)
- return self.maxh
- end,
- unset = function(self,x,y)
- if(x <= self.maxw and y <= self.maxh)then
- self.grid[y][x] = {}
- else
- error("grid:unset -> Invaild X and/or Y param(s).")
- end
- end,
- getRaw = function(self)
- return self.grid
- end,
- getSerial = function(self)
- return textutils.serialize(self.grid):gsub("\n%s*","")
- end,
- replace = function(self,grid)
- if(type(grid) ~= "table")then error("grid:replace -> Invalid grid type. ") end
- -- calculating grid size... --
- self.maxh = 0
- self.maxw = 0
- for iy = 1, #grid do
- self.maxh = self.maxh + 1
- end
- for ix = 1, #grid[0] do
- self.maxw = self.maxw + 1
- end
- self.grid = grid
- if(self.maxh <= 0 or self.maxw <= 0)then error("grid:replace -> Grid is malformed.") end
- end,
- draw = function(self)
- for y = 0, self.maxh do
- for x = 0, self.maxw do
- term.setCursorPos(x,y)
- if(self.grid[y][x] ~= {})then
- term.write(self.grid[y][x])
- end
- end
- end
- end,
- swap = function(self,fromx,fromy,tox,toy)
- local prevo = self.grid[fromy][fromx]
- local too = self.grid[toy][tox]
- self.grid[fromy][fromx] = too
- self.grid[toy][tox] = prevo
- end,
- moveInto = function(self,obj,x,y,uap,flags)
- local prloc = self:get(x,y)
- local ox, oy = self:find(obj)
- -- only take place if flags on other block are met --
- if(type(flags) == "table")then
- for k, v in pairs(flags) do
- if(not self:checkAttribEqual(x,y,k,v))then
- return false
- end
- end
- end
- -- if passes flags --
- if(type(uap) == "table")then
- for k, v in pairs(uap) do
- obj[k] = v
- end
- end
- self:set(ox,oy,prloc)
- self:set(x,y,obj)
- end,
- renderSwap = function(self, charattrib, tcolattrib, bcolattrib, fromx,fromy,tox,toy)
- self:swap(fromx,fromy,tox,toy)
- self:renderRegion(charattrib, tcolattrib, bcolattrib,tox,toy,tox,toy)
- self:renderRegion(charattrib, tcolattrib, bcolattrib,fromx,fromy,fromx,fromy)
- end,
- render = function(self,charattrib,tcolattrib,bcolattrib)
- local ta = colors.white
- local ba = colors.black
- for y = 0, self.maxh do
- for x = 0, self.maxw do
- term.setCursorPos(x,y)
- term.setTextColor(self.grid[y][x][tcolattrib])
- term.setBackgroundColor(self.grid[y][x][bcolattrib])
- ta = colors.white
- ba = colors.black
- if(type(tcolattrib) == "number")then ta = tcolattrib end
- if(type(bcolattrib) == "number")then ba = bcolattrib end
- if(self.grid[y][x][charattrib] ~= nil)then
- if(self.grid[y][x][tcolattrib] ~= nil)then
- ta = self.grid[y][x][tcolattrib]
- end
- if(self.grid[y][x][bcolattrib] ~= nil)then
- ba = self.grid[y][x][bcolattrib]
- end
- term.setTextColor(ta)
- term.setBackgroundColor(ba)
- term.write(self.grid[y][x][charattrib])
- end
- end
- end
- end,
- drawAttrib = function(self,a)
- for y = 0, self.maxh do
- for x = 0, self.maxw do
- term.setCursorPos(x,y)
- if(self.grid[y][x][a] ~= nil)then
- term.write(self.grid[y][x][a])
- end
- end
- end
- end,
- fill = function(self,obj)
- for y = 0, self.maxh do
- for x = 0, self.maxw do
- self.grid[y][x] = obj
- end
- end
- end,
- setCursorPos = function(self,x,y)
- term.setCursorPos(y,x)
- end,
- loop = function(self,f,startx,starty,endx,endy)
- if(startx == nil)then startx = 0 end
- if(starty == nil)then starty = 0 end
- if(endx == nil and startx ~= nil)then endx = startx end
- if(endy == nil and starty ~= nil)then endy = starty end
- if(endx == nil)then endx = self.maxw end
- if(endy == nil)then endy = self.maxh end
- for y = tonumber(starty), tonumber(endy) do
- for x = tonumber(startx), tonumber(endx) do
- f(x,y)
- end
- end
- end,
- setRegion = function(self, sx, sy, ex, ey, obj)
- if(sx <= self.maxw and sy <= self.maxh and ex <= self.maxw and ey <= self.maxh)then
- for y = tonumber(sy), tonumber(ey) do
- for x = tonumber(sx), tonumber(ex) do
- self.grid[y][x] = obj
- end
- end
- else
- error("grid:setRegion -> Invaild X and/or Y param(s).")
- end
- end,
- drawRegion = function(self, sx, sy, ex, ey)
- if(sx <= self.maxw and sy <= self.maxh and ex <= self.maxw and ey <= self.maxh)then
- for y = tonumber(sy), tonumber(ey) do
- for x = tonumber(sx), tonumber(ex) do
- term.setCursorPos(x,y)
- if(self.grid[y][x] ~= {})then
- term.write(self.grid[y][x])
- end
- end
- end
- else
- error("grid:drawRegion -> Invaild X and/or Y param(s).")
- end
- end,
- renderRegion = function(self, charattrib, tcolattrib, bcolattrib, sx, sy, ex, ey)
- if(sx <= self.maxw and sy <= self.maxh and ex <= self.maxw and ey <= self.maxh)then
- local ta = colors.white
- local ba = colors.black
- for y = tonumber(sy), tonumber(ey) do
- for x = tonumber(sx), tonumber(ex) do
- term.setCursorPos(x,y)
- ta = colors.white
- ba = colors.black
- if(self.grid[y][x][charattrib] ~= nil)then
- if(self.grid[y][x][tcolattrib] ~= nil)then
- ta = self.grid[y][x][tcolattrib]
- end
- if(self.grid[y][x][bcolattrib] ~= nil)then
- ba = self.grid[y][x][bcolattrib]
- end
- term.setTextColor(ta)
- term.setBackgroundColor(ba)
- term.write(self.grid[y][x][charattrib])
- end
- end
- end
- else
- error("grid:renderRegion -> Invaild X and/or Y param(s).")
- end
- end,
- }
- -- generate grid locally --
- for y = 0, h do
- gridtable.grid[y] = {}
- for x = 0, w do
- gridtable.grid[y][x] = {}
- end
- end
- -- return grid to user --
- return gridtable
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement