Advertisement
Redxone

[CC - E3] BuffPixel (NO TMAP)

Feb 5th, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.25 KB | None | 0 0
  1. ---------------------------------------------------------------------------------|
  2. --]] Copyright (C) Nova Corporations                                         [[--|
  3. --]] Do not edit, Do not re-distrubute, Do not Copy                          [[--|
  4. --]] All things under the Authority of the Owner(s) Lewisk3 (Gluuon, Redxone)[[--|
  5. ---------------------------------------------------------------------------------|
  6.  
  7. --]] Lua buffer map API ideas
  8.  
  9. --getMap(w, h) -- Returns map in table format
  10. -------------]]
  11.     --]] Map Functions -- "FF*", where FF is text color, then back color, and * is the graphic
  12. --      changePixelGraphic(x,y,to) -- Changes a pixels graphic at an X and Y to a specific string "Text-Back-Str"
  13. --      setSolid(x,y,tf)       -- Sets a pixel at X, Y to be solid or not (map[y][x].solid = tf)
  14. --      getRegionMap(fx,fy,tx,ty)  -- Returns a map of pixels from a specific region
  15. --      setPixelAttribute(x,y,att) -- Concatenates att table onto the Pixels table at X, Y
  16. --      setPixelColor(x,y,cb)      -- Sets pixel at x and y to color, cb (color byte)
  17. --      getPixelAttribute(x,y,name)-- Returns Attribute by name
  18. --      setPixel(x,y,g,s)      -- sets pixel at x, y to graphic - g and solid - s
  19. --      getPixel(x,y)          -- returns entire pixel at x and y
  20. --      drawPixel(x,y)             -- draws pixel from map x, y
  21. --      drawPixelAt(x,y,sx,sy)     -- draws pixel from map x, y at screen sx, sy
  22. ---------------]]
  23. --createCamera(fx,fy) -- Returns camera table with focal pointed offsets of - fx and fy    
  24.     --]] Camera Functions -- store local buffer    
  25. --      render(map,mx,my,ox,oy) -- render map starting ox, oy and ending at mx+ox and my+oy, viewx = ox, viewy = oy, vieww = mx, viewh = my : checks buffer for smart rendering
  26. --      updateView(map,cx,cy)   -- Updates cam view by adding cx and cy to it, renders
  27. --      clearBuffer()       -- Clears the buffer
  28. --      isOutsideView(x,y)  -- Check view variables to see of coord is outside view
  29. --      setBuffer(buff)     -- Sets buffer table to, buff    
  30. ---------------]]
  31. --Transparent pixel utils -- render used to draw pixels unrelated to the table map
  32. --]] Transparent pixel functions -- Returns renderer
  33. --  setProjection(x,y) -- sets starting point for adding offsets too
  34. --  addProjection(xo,yo) -- adds xo, yo to projection
  35. --  drawTPixel(x,y,g) -- draws pixel at x, y with g as graphic
  36. --  moveTPixel(map,xo,yo,g,a) -- takes map and draws map[y][x] at projection then draws g, at projection +xo, +yo boolean a is add offset to projection.
  37. --  clearTPixel(map,x,y)      -- redraws x, y to map[y][x]
  38. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  39.  
  40. --]] API: Start
  41. if(not term.current().setVisible)then
  42.     term.current().setVisible = function(noval)
  43.         return false
  44.     end
  45. end
  46.  
  47. function new(w,h,nx,ny,nw,nh)
  48.     local nmap = {}
  49.     for y = 1, math.ceil(h) do
  50.         nmap[y] = {}
  51.         for x = 1, math.ceil(w) do
  52.             nmap[y][x] = {}
  53.         end
  54.     end
  55.     local w, h = term.getSize()
  56.     local nbuffer = {}
  57.     for y = 1, h do
  58.         nbuffer[y] = {}
  59.         for x = 1, w do
  60.             nbuffer[y][x] = {}
  61.         end
  62.     end
  63.  
  64. local mutils = {
  65.     map = nmap,
  66.     player = {},
  67.     buffer = nbuffer,
  68.     huds = {},
  69.     viewx = 0,
  70.     viewy = 0,
  71.     vieww = 0,
  72.     viewh = 0,
  73.     roffx = 0,
  74.     roffy = 0,
  75.     btext = {"", colors.white},
  76.     -- Misc map utils
  77.     importMap = function(self,file)
  78.         if(fs.exists(file))then
  79.             local f = fs.open(file,"r")
  80.             local gmap = textutils.unserialize(f.readAll())
  81.             if(type(gmap) == "table")then self.map = gmap else error("importMap: Map invalid or corrupt. ") end
  82.             f.close()
  83.         else
  84.             error("importMap: File doesnt exist: " .. file)    
  85.         end
  86.     end,
  87.  
  88.     setRenderOffset = function(self,x,y)
  89.         -- Make sure x and y is a number and that the number is an Integer
  90.         self.roffx = math.ceil(tonumber(x))
  91.         self.roffy = math.ceil(tonumber(y))
  92.     end,
  93.  
  94.     getWidth = function(self)
  95.         return self.vieww
  96.     end,
  97.  
  98.     getHeight = function(self)
  99.         return self.viewh
  100.     end,
  101.  
  102.     getViews = function(self)
  103.         return self.viewx, self.viewy
  104.     end,
  105.  
  106.     getRenderOffsets = function(self)
  107.         return self.roffx, self.roffy
  108.     end,
  109.  
  110.     -- Misc camera buffer utils
  111.     initBuffer = function(self)
  112.         self.buffer = {}
  113.         for y = 0, self.viewh do
  114.                 self.buffer[y] = {}
  115.             for x = 0, self.vieww do
  116.                 self.buffer[y][x] = {}
  117.             end
  118.         end
  119.     end,
  120.     fillBuffer = function(self,pix)
  121.         for y = 1, #self.map do
  122.             for x = 1, #self.map[y] do
  123.                 self:setBuffer(x,y,pix)
  124.             end
  125.         end
  126.     end,
  127.     setBufferPixel = function(self,x,y,pix)
  128.         self.buffer[y][x] = pix
  129.     end,
  130.     isBuffsize = function(self,x,y)
  131.         if(self.buffer[y] ~= nil)then
  132.             if(self.buffer[y][x] ~= nil)then
  133.                 return true
  134.             end
  135.         end
  136.         return false
  137.     end,
  138.     checkViews = function(self)
  139.         local nocap = true
  140.         if( self.viewy > #self.map-self.viewh    )then self.viewy = #self.map-self.viewh nocap = false  end
  141.         if( self.viewx > #self.map[1]-self.vieww )then self.viewx = #self.map[1]-self.vieww nocap = false  end
  142.         if( self.viewx < 0 )then self.viewx = 0 nocap = false end
  143.         if( self.viewy < 0 )then self.viewy = 0 nocap = false end
  144.         return nocap
  145.     end,
  146.     setBuffer = function(self,buff)
  147.         self.buffer = buff
  148.     end,
  149.     clearBuffer = function(self)
  150.         self.buffer = {}
  151.     end,
  152.     getOutsideViews = function(self,x,y,xd,yd)
  153.         local vox, voy = 0, 0
  154.         if( x-xd <= self.viewx               ) then vox = -1 end
  155.         if( x+xd >  self.vieww +  self.viewx ) then vox = 1  end
  156.         if( y-yd <= self.viewy               ) then voy = -1 end
  157.         if( y+yd >  self.viewh +  self.viewy ) then voy = 1  end
  158.         if(x-xd < 1 or x+xd > #self.map[1])then
  159.             vox = 0
  160.         end
  161.         if(y-yd < 1 or y+yd > #self.map)then
  162.             voy = 0
  163.         end
  164.         return tonumber(vox), tonumber(voy)
  165.     end,
  166.     isOutsideView = function(self,x,y)
  167.         return (x <= self.roffx) or ( x > self.vieww+self.roffx ) or (y <= self.roffy) or (y > self.viewh+self.roffy)
  168.     end,
  169.     isOutsideViewRelative = function(self,x,y)
  170.         return (x < self.viewx+1) or ( x > self.vieww+self.viewx ) or (y < self.viewy+1) or (y > self.viewh + self.viewy)
  171.     end,
  172.  
  173.     -- Misc pixel utils
  174.     newPixel = function(col,g,s)
  175.         return {col .. g, s}
  176.     end,
  177.     -- Color utils
  178.     getColoredByte = function(byte)
  179.         return 2^tonumber(byte:sub(1,1),16), 2^tonumber(byte:sub(2,2),16)
  180.     end,
  181.     setColors = function(tcol, bcol)
  182.         term.setTextColor(tcol)
  183.         term.setBackgroundColor(bcol)
  184.     end,
  185.  
  186.     -- Map utils
  187.     fillMap = function(self,pix,s)
  188.         for y = 1, #self.map do
  189.             for x = 1, #self.map[y] do
  190.                 self:setPixel(x,y,pix,s)
  191.             end
  192.         end
  193.     end,
  194.     clearMap = function(self)
  195.         for y = 1, #self.map do
  196.             for x = 1, #self.map[y] do
  197.                 self.map[y][x] = {}
  198.             end
  199.         end
  200.     end,
  201.     setMapRegion = function(self,sx,sy,regiontable)
  202.         for y = sy, #regiontable+sy do
  203.             for x = sx, #regiontable[y-sy]+sx do
  204.                 self.map[y][x] = regiontable[y-sy][x-sx]
  205.             end
  206.         end
  207.     end,
  208.     setMap = function(self, map)
  209.         self.map = map
  210.     end,
  211.     getMap = function(self)
  212.         return self.map
  213.     end,
  214.     getRegion = function(self,x,y,xx,yy)
  215.         local retmap = {}
  216.         for yr = y, yy do
  217.             for xr = x, xx do
  218.                 retmap[yr-y][xr-x] = self.map[yr][xr]
  219.             end
  220.         end
  221.         return retmap
  222.     end,
  223.  
  224.     -- Pixel utils
  225.     setSolid = function(self,x,y)
  226.         return self.map[y][x].solid
  227.     end,
  228.     getPixel = function(self,x,y)
  229.         if(self.map[y] == nil)then return {"END", solid=true} end
  230.         if(self.map[y][x] == nil)then return {"END", solid=true} end
  231.         return self.map[y][x][1]
  232.     end,
  233.     getPixelRaw = function(self,x,y)
  234.         if(self.map[y] == nil)then return {"END", solid=true} end
  235.         if(self.map[y][x] == nil)then return {"END", solid=true} end
  236.         return self.map[y][x]
  237.     end,
  238.     getPixelAttribute = function(self,x,y,attrib)
  239.         return self.map[y][x][attrib]
  240.     end,
  241.     setPixelAttribute = function(self,attrib,eq)
  242.         self.map[y][x][attrib] = eq
  243.     end,
  244.     drawMapPixel = function(self,x,y)
  245.         self.setColors(self.getColoredByte(self.map[y][x][1]))
  246.         term.setCursorPos( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  247.         write(self.map[y][x][1]:sub(3,3))
  248.         self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  249.     end,
  250.     drawMapPixelAt = function(self,x,y,dx,dy)
  251.         self.setColors(self.getColoredByte(self.map[y][x][1]))
  252.         term.setCursorPos(dx, dy)
  253.         write(self.map[y][x][1]:sub(3,3))
  254.     end,
  255.     setPixel = function(self,x,y,pix)
  256.         self.map[y][x] = {pix[1],solid=pix[2]}
  257.         if(self:isBuffsize(x-self.viewx, y-self.viewy))then
  258.             self:setBufferPixel(x-self.viewx, y-self.viewy, pix[1])
  259.         end
  260.         if(self.player ~= {})then
  261.             if(x == self.player.x and y == self.player.y)then
  262.                 self:drawPlayer()
  263.             elseif(not self:isOutsideViewRelative(x,y))then
  264.                 self:drawMapPixel(x,y)
  265.             end
  266.         elseif(not self:isOutsideViewRelative(x,y))then
  267.             self:drawMapPixel(x,y)
  268.         end
  269.         self:checkHudRedraw((x - self.viewx) + self.roffx,(y - self.viewy) + self.roffy)
  270.     end,
  271.  
  272.     setPixelRelative = function(self,x,y,pix)
  273.         self:setPixel(x + self.viewx - self.roffx, y + self.viewy - self.roffy, pix)
  274.     end,
  275.  
  276.     -- Transparent Pixel utils
  277.     drawTPixel = function(self,x,y,cpix)
  278.         if(cpix:sub(1,1) == "G")then
  279.             term.setTextColor(2^tonumber( self.map[y+self.viewy][x+self.viewx][1]:sub(1,1), 16 ) )
  280.             term.setBackgroundColor(2^tonumber( cpix:sub(2,2), 16) )
  281.         elseif(cpix:sub(2,2) == "G")then
  282.             term.setTextColor(2^tonumber( cpix:sub(1,1), 16) )
  283.             term.setBackgroundColor( 2^tonumber( self.map[y+self.viewy][x+self.viewx][1]:sub(2,2),16 ) )
  284.         else
  285.             self.setColors(self.getColoredByte(cpix))
  286.         end
  287.         if(self:isBuffsize(x, y))then
  288.             self:setBufferPixel(x, y, cpix)
  289.         end
  290.         term.setCursorPos(x + self.roffx, y + self.roffy)
  291.         write(cpix:sub(3,3))
  292.         self:checkHudRedraw(x + self.roffx,y + self.roffy)
  293.     end,
  294.     clearTPixel = function(self,x,y)
  295.         if(self:isBuffsize(x-self.viewx, y-self.viewy))then
  296.             self:setBufferPixel(x-self.viewx, y-self.viewy, self.map[y][x][1])
  297.         end
  298.         self:drawMapPixel(x,y)
  299.     end,
  300.  
  301.     ---- Player utils
  302.     drawPlayer = function(self)
  303.         if(self.player == {})then return false end
  304.         local xd = self.player.x-self.viewx
  305.         local yd = self.player.y-self.viewy
  306.         if(xd <= 0)then xd = 1 end
  307.         if(yd <= 0)then yd = 1 end
  308.         if(self.player.x > 0 and self.player.y > 0)then
  309.             self:drawTPixel( xd, yd ,self.player.pix)
  310.         end
  311.     end,
  312.    
  313.     setPlayerGraphic = function(self,col,g)
  314.         self.player.pix = col .. g
  315.         self:drawPlayer()
  316.     end,
  317.  
  318.     undrawPlayer = function(self)
  319.         if(self.player == {})then return false end
  320.         self:drawMapPixel(self.player.x,self.player.y)
  321.     end,
  322.     drawPlayerAt = function(self,x,y)
  323.         if(self.player == {})then return false end
  324.         self:drawTPixel(x,y,self.player.pix)
  325.     end,
  326.     movePlayer = function(self,ox,oy,r)
  327.         if(self.player == {})then return false end
  328.         if(r)then
  329.             self:clearTPixel(self.player.x,self.player.y)
  330.         end
  331.         if( ( ( self:getPixelRaw( self.player.x + ox, self.player.y + oy ).solid == false ) or self.player.scollide == false ) and self.player.mcollide and
  332.             self.player.x+ox >= 1 and self.player.y+oy >= 1 and self.player.y+oy <= #self.map and self.player.x+ox <= #self.map[1] )then
  333.             self.player.x = self.player.x + ox
  334.             self.player.y = self.player.y + oy
  335.         else
  336.             if(r)then
  337.                 self:drawPlayer()
  338.             end        
  339.             return false
  340.         end
  341.         if(r)then
  342.             self:drawPlayer()
  343.         end
  344.     end,
  345.     setPlayerPos = function(self,x,y)
  346.         self.player.x = x
  347.         self.player.y = y
  348.     end,
  349.     getPlayerPos = function(self)
  350.         return self.player.x, self.player.y
  351.     end,
  352.     newPlayer = function(self,pcol,pg,px,py,aup,adown,aleft,aright,aspd)
  353.         self.player = {scollide=true,mcollide=true,pix=pcol..pg,x=math.ceil(px),y=math.ceil(py),events={},up=aup,down=adown,left=aleft,right=aright,spd=aspd,canmove=true}
  354.         self:drawPlayer()
  355.     end,
  356.     setPlayerCollisionType = function(self,colw)
  357.         self.player.mcollide = colw
  358.     end,
  359.     playerSolidCollision = function(self,tf)
  360.         self.player.scollide = tf
  361.     end,
  362.     clearPlayerEvents = function(self)
  363.         self.player.events = {}
  364.     end,
  365.     addPlayerEvent = function(self,name,evtbl,call)
  366.         self.player.events[name] = {evs = evtbl,func = call}
  367.     end,
  368.     removePlayerEvent = function(self,name)
  369.         if(self.player.events[name] ~= nil)then
  370.            self.player.events[name] = nil
  371.         end
  372.     end,
  373.     updatePlayer = function(self,ev,spd)
  374.         -- Compare user specific player events
  375.         for k, v in pairs(self.player.events) do
  376.             if(ev[1] == v.evs[1] and ev[2] == v.evs[2])then
  377.              v:func()
  378.             end
  379.         end
  380.         -- Player Input and movement
  381.         if(ev[1] == "key")then
  382.             local key = ev[2]
  383.             if(self.player.canmove)then
  384.                     if(key == self.player.up   )then
  385.                         self:movePlayer(0,-self.player.spd,true)
  386.                 elseif(key == self.player.down )then
  387.                         self:movePlayer(0,self.player.spd,true)
  388.                 elseif(key == self.player.left )then
  389.                         self:movePlayer(-self.player.spd,0,true)
  390.                 elseif(key == self.player.right)then
  391.                         self:movePlayer(self.player.spd,0,true)
  392.                 end
  393.             end
  394.             if(spd ~= nil)then
  395.                 sleep(spd)
  396.             end
  397.         end
  398.     end,
  399.  
  400.     -- Hud utils
  401.     renderHuds = function(self)
  402.         for k, v in pairs(self.huds) do
  403.             term.setCursorPos(v.offx, v.offy)
  404.             v.draw()
  405.         end
  406.     end,
  407.  
  408.     updateHuds = function(self,e)
  409.         for k, v in pairs(self.huds) do
  410.             term.setCursorPos(v.offx, v.offy)
  411.             v.update(e)
  412.         end
  413.     end,
  414.    
  415.     drawHud = function(self,name)
  416.         if(self.huds[name] ~= nil)then
  417.             self.huds[name].draw()
  418.         end
  419.     end,
  420.  
  421.     checkHudRedraw = function(self,x,y)
  422.         for k, v in pairs(self.huds) do
  423.             --term.setCursorPos(1,2)
  424.             --write(x .. ">= " .. v.offx .. " " .. x .. "<= " .. v.width + v.offx-1)
  425.             --term.setCursorPos(1,3)
  426.             --write(y .. ">= " .. v.offy .. " " .. y .. "<= " .. v.height + v.offy-1)
  427.             if( x >= v.offx and x <= v.width + v.offx-1 and y >= v.offy and y <= v.height + v.offy -1 )then
  428.                 self:drawHud(k)
  429.             end
  430.         end
  431.     end,
  432.  
  433.     newHud = function(self,name,xo,yo,w,h,drawf,updatef)
  434.         self.huds[name] = {offx=xo,offy=yo,width=w,height=h,draw=drawf,update=updatef}
  435.         self:drawHud(name)
  436.     end,
  437.  
  438.     removeHud = function(self, name)
  439.         if(self.huds[name] ~= nil)then
  440.             self.huds[name] = nil
  441.         end
  442.     end,
  443.  
  444.     setHudCursorPos = function(self,name,x,y)
  445.         if(self.huds[name] ~= nil)then
  446.             term.setCursorPos(self.huds[name].offx + x, self.huds[name].offy + y)
  447.         end
  448.     end,
  449.  
  450.     -- Special event handlers
  451.     getMousePlaceBlock = function(self,e,blockone,blocktwo)
  452.         if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
  453.           if(not self:isOutsideView(e[3],e[4]))then
  454.              if(e[2] == 1 and blockone ~= nil)then
  455.                  self:setPixelRelative(e[3],e[4],blockone)
  456.              elseif(blocktwo ~= nil)then
  457.                  self:setPixelRelative(e[3],e[4],blocktwo)
  458.              end
  459.           end
  460.        end
  461.     end,
  462.  
  463.     -- Render utils
  464.  
  465.     renderSection = function(self,fx,fy,tx,ty)
  466.         term.current().setVisible(false)
  467.         if(#self.buffer ~= self.viewh or #self.buffer[1] ~= self.vieww)then
  468.             self:initBuffer()
  469.         end
  470.         self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] = {self.player.pix, solid=false}
  471.         for y=fy, ty do
  472.                 for x=fx, tx do
  473.                     if(self.buffer[(y-self.viewy)][(x-self.viewx)] ~= self.map[y][x][1])then
  474.                         -- Draw the pixel
  475.                         self:drawMapPixel(x,y)
  476.                         -- Add the pixel to the buffer
  477.                         self.buffer[(y-self.viewy)][(x-self.viewx)] = self.map[y][x][1]
  478.                         self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  479.                     elseif(self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] ~= {self.player.pix})then
  480.                             -- Draw the player
  481.                             self:drawPlayer()
  482.                             -- Add the pixel to the buffer
  483.                             self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] = {self.player.pix}
  484.                             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy) 
  485.                     end
  486.                 end
  487.         end
  488.         local success, err = pcall(self.renderHuds,self)
  489.         if( not success )then
  490.             term.current().setVisible(true)
  491.             print("Hud renderer errored with: " .. err)
  492.         end
  493.         term.current().setVisible(true)
  494.     end,
  495.     refreshRender = function(self, vis)
  496.         if( not vis )then term.current().setVisible(false) end
  497.         if(#self.buffer ~= self.viewh or #self.buffer[1] ~= self.vieww)then
  498.             self:initBuffer()
  499.         end
  500.         for y=self.viewy+1, self.viewy + self.viewh do
  501.                 for x=self.viewx+1, self.viewx + self.vieww do
  502.                     -- If we have a new pixel in view
  503.                         -- Draw the pixel
  504.                         self:drawMapPixel(x,y)
  505.                         -- Add the pixel to the buffer
  506.                         self:setBufferPixel(x-self.viewx, y-self.viewy, self.map[y][x][1])
  507.                         self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  508.                     if(self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] ~= {self.player.pix})then
  509.                             -- Draw the player
  510.                             self:drawPlayer()
  511.                             -- Add the pixel to the buffer
  512.                             self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] = {self.player.pix}
  513.                             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy) 
  514.                     end
  515.                 end
  516.         end
  517.         local success, err = pcall(self.renderHuds,self)
  518.         if( not success )then
  519.             term.current().setVisible(true)
  520.             print("Hud renderer errored with: " .. err)
  521.         end
  522.         if( not vis )then term.current().setVisible(true) end
  523.         if(self.player ~= {})then self:drawPlayer() end
  524.     end,
  525.  
  526.     render = function(self,opt)
  527.         term.current().setVisible(false)
  528.         if(#self.buffer ~= self.viewh or #self.buffer[1] ~= self.vieww)then
  529.             self:initBuffer()
  530.         end
  531.         for y=self.viewy+1, self.viewy + self.viewh do
  532.                 for x=self.viewx+1, self.viewx + self.vieww do
  533.                     -- If we have a new pixel in view
  534.                     if(self.buffer[(y-self.viewy)][(x-self.viewx)] ~= self.map[y][x][1])then
  535.                         -- Draw the pixel
  536.                         self:drawMapPixel(x,y)
  537.                         if(opt == true)then
  538.                             self.setColors(self.getColoredByte(self.map[y][x][1]))
  539.                             term.setCursorPos( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  540.                             write("R")
  541.                         end
  542.                         -- Add the pixel to the buffer
  543.                         self:setBufferPixel(x-self.viewx, y-self.viewy, self.map[y][x][1])
  544.                         self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  545.                     end
  546.                     if(self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] ~= {self.player.pix})then
  547.                             -- Draw the player
  548.                             self:drawPlayer()
  549.                             -- Add the pixel to the buffer
  550.                             self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] = {self.player.pix}
  551.                             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy) 
  552.                     end
  553.                 end
  554.         end
  555.         local success, err = pcall(self.renderHuds,self)
  556.         if( not success )then
  557.             term.current().setVisible(true)
  558.             print("Hud renderer errored with: " .. err)
  559.         end
  560.         term.current().setVisible(true)
  561.     end,
  562.     setView = function(self,nx,ny,nw,nh,r)
  563.         local oh, ow = self.viewh, self.vieww
  564.         self.viewx = nx
  565.         self.viewy = ny
  566.         self.vieww = nw
  567.         self.viewh = nh
  568.         if(nw ~= ow or nh ~= oh)then self:initBuffer() end
  569.         if(r and self:checkViews() )then return self:render() end
  570.     end,
  571.  
  572.     updateView = function(self,ox,oy,ow,oh,r)
  573.         local ovx = self.viewx
  574.         local ovy = self.viewy
  575.         local ovh = self.viewh
  576.         local ovw = self.vieww
  577.         self.viewx = self.viewx + ox
  578.         self.viewy = self.viewy + oy
  579.         self.vieww = self.vieww + ow
  580.         self.viewh = self.viewh + oh
  581.         self:checkViews()
  582.         if(ow ~= 0 or oh ~= 0)then self:initBuffer() end
  583.         if(r and self:checkViews() )then return self:render() end
  584.     end,
  585.  
  586.     lerpView = function(self,ox,oy,ax,ay,time)
  587.         -- ox, and oy are directional variables
  588.         -- ax, and ay are the amount to move the view
  589.         -- time is how long to wait before moving the view again
  590.         self.player.canmove = false
  591.         if(ox ~= 0 )then
  592.              term.current().setVisible(false)
  593.              self:undrawPlayer()
  594.              for x = 1, ax do
  595.                 self:updateView(ox,0,0,0,true)
  596.                 if(time ~= nil)then
  597.                     sleep(time)
  598.                 end
  599.              end
  600.         end
  601.        if(oy ~= 0 )then
  602.           term.current().setVisible(false)
  603.           self:undrawPlayer()
  604.           for y = 1, ay do
  605.              self:updateView(0,oy,0,0,true)
  606.              if(time ~= nil)then
  607.                 sleep(time)
  608.              end
  609.           end
  610.        end
  611.        self.player.canmove = true
  612.     end,
  613.  
  614.  
  615.     -- Screen utils
  616.     checkScreenCollision = function(self, x, y, w, h)
  617.         return (x >= self.roffx-1 and x <= self.vieww + self.roffx + 1 and y >= self.roffy-1 and y <= self.viewh + self.roffy + 1)
  618.     end,
  619.     setBorderText = function(self, txt, color)
  620.         self.btext = {txt, color}
  621.     end,
  622.     drawBorder = function(self, color)
  623.         paintutils.drawBox(self.roffx, self.roffy, self.vieww+self.roffx+1, self.viewh+self.roffy+1, color)
  624.         term.setCursorPos(self.roffx , self.roffy)
  625.         --+ (self.vieww /2) - (#self.btext[1] /2) centered text
  626.         term.setTextColor(self.btext[2])
  627.         write(self.btext[1]:sub(1,self.vieww))
  628.     end,
  629.     clearScreen = function(self,color)
  630.         paintutils.drawFilledBox(self.roffx, self.roffy, self.vieww+self.roffx+1, self.viewh+self.roffy+1, color)
  631.     end,
  632.     setScreenPos = function(self,tox,toy, color)
  633.         term.current().setVisible(false)
  634.         self:clearScreen(color)
  635.         self.roffx = tox
  636.         self.roffy = toy
  637.         self:refreshRender()
  638.         term.current().setVisible(true)
  639.     end,
  640.     moveScreen = function(self,tox,toy, color)
  641.         term.current().setVisible(false)
  642.         self:clearScreen(color)
  643.         self.roffx = self.roffx + tox
  644.         self.roffy = self.roffy + toy
  645.         self:refreshRender()
  646.         term.current().setVisible(true)
  647.     end,
  648.  
  649. }
  650.     mutils.viewx = nx
  651.     mutils.viewy = ny
  652.     mutils.vieww = nw
  653.     mutils.viewh = nh
  654.     return mutils
  655. end
  656.  
  657. function exportMap(map, file)
  658.     if(fs.exists(file))then
  659.         error("exportMap: File already exists: " .. file)
  660.     else
  661.         local f = fs.open(file,"w")
  662.         map = textutils.serialize(map)
  663.         map = map:gsub("n", "")
  664.         map = map:gsub("%s+", "")
  665.         f.write(map)
  666.         f.close()
  667.     end
  668. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement