Advertisement
Redxone

[CC - E3] BuffPixel [WIP]

Jan 28th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.39 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. function checkUpdate(apiloc)
  42.     if(http)then
  43.         local upd = http.get("http://pastebin.com/raw/0smCR408")
  44.         if(upd ~= nil)then
  45.             local cont = upd.readAll()
  46.             local f = fs.open(apiloc,"r")
  47.             local pcont = f.readAll()
  48.             f.close()
  49.             if(cont ~= pcont)then
  50.                 local f = fs.open(apiloc,"w")
  51.                 f.write(cont)
  52.                 f.close()
  53.                 return "update"
  54.             end
  55.         else
  56.             return "noconnection"
  57.         end
  58.     else
  59.         return "nohttp"
  60.     end
  61.     return "noupdate"
  62. end
  63.  
  64. if(not term.current().setVisible)then
  65.     term.current().setVisible = function(noval)
  66.         return false
  67.     end
  68. end
  69.  
  70. function new(mw,mh,nx,ny,nw,nh)
  71.     local nmap = {}
  72.     for y = 1, math.ceil(mh) do
  73.         nmap[y] = {}
  74.         for x = 1, math.ceil(mw) do
  75.             nmap[y][x] = {}
  76.         end
  77.     end
  78.     local ntmap = {}
  79.     for y = 1, math.ceil(mh) do
  80.         ntmap[y] = {}
  81.         for x = 1, math.ceil(mw) do
  82.             ntmap[y][x] = ""
  83.         end
  84.     end
  85.     local w, h = term.getSize()
  86.     local nbuffer = {}
  87.     for y = 1, h do
  88.         nbuffer[y] = {}
  89.         for x = 1, w do
  90.             nbuffer[y][x] = {}
  91.         end
  92.     end
  93.  
  94. local mutils = {
  95.     map = nmap,
  96.     tmap = ntmap,
  97.     player = {},
  98.     buffer = nbuffer,
  99.     huds = {},
  100.     viewx = 0,
  101.     viewy = 0,
  102.     vieww = 0,
  103.     viewh = 0,
  104.     roffx = 0,
  105.     roffy = 0,
  106.     btext = {"", colors.white},
  107.     clock = 0,
  108.     time = 0,
  109.     dialogln = 0,
  110.     entities = {},
  111.     -- Misc map utils
  112.     importMap = function(self,file)
  113.         if(fs.exists(file))then
  114.             local f = fs.open(file,"r")
  115.             local gmap = textutils.unserialize(f.readAll())
  116.             if(type(gmap) == "table")then self.map = gmap else error("importMap: Map invalid or corrupt. ") end
  117.             f.close()
  118.             self:resetTMap()
  119.             self:initBuffer()
  120.         else
  121.             error("importMap: File doesnt exist: " .. file)    
  122.         end
  123.     end,
  124.  
  125.     setRenderOffset = function(self,x,y)
  126.         -- Make sure x and y is a number and that the number is an Integer
  127.         self.roffx = math.ceil(tonumber(x))
  128.         self.roffy = math.ceil(tonumber(y))
  129.     end,
  130.  
  131.     getWidth = function(self)
  132.         return self.vieww
  133.     end,
  134.  
  135.     getHeight = function(self)
  136.         return self.viewh
  137.     end,
  138.  
  139.     getViews = function(self)
  140.         return self.viewx, self.viewy
  141.     end,
  142.  
  143.     getRenderOffsets = function(self)
  144.         return self.roffx, self.roffy
  145.     end,
  146.  
  147.     -- Misc camera buffer utils
  148.     initBuffer = function(self)
  149.         self.buffer = {}
  150.         for y = 0, self.viewh do
  151.                 self.buffer[y] = {}
  152.             for x = 0, self.vieww do
  153.                 self.buffer[y][x] = {}
  154.             end
  155.         end
  156.     end,
  157.     fillBuffer = function(self,pix)
  158.         for y = 1, #self.map do
  159.             for x = 1, #self.map[y] do
  160.                 self:setBuffer(x,y,pix)
  161.             end
  162.         end
  163.     end,
  164.     setBufferPixel = function(self,x,y,pix)
  165.         self.buffer[y][x] = pix
  166.     end,
  167.     isBuffsize = function(self,x,y)
  168.         if(self.buffer[y] ~= nil)then
  169.             if(self.buffer[y][x] ~= nil)then
  170.                 return true
  171.             end
  172.         end
  173.         return false
  174.     end,
  175.     checkViews = function(self)
  176.         local nocap = true
  177.         if( self.viewy-self.viewh > #self.map   )then self.viewy = #self.map-self.viewh nocap = false  end
  178.         if( self.viewx-self.vieww > #self.map[1] )then self.viewx = #self.map[1]-self.vieww nocap = false  end
  179.         if( self.viewx < 0 )then self.viewx = 0 nocap = false end
  180.         if( self.viewy < 0 )then self.viewy = 0 nocap = false end
  181.         return nocap
  182.     end,
  183.     setBuffer = function(self,buff)
  184.         self.buffer = buff
  185.     end,
  186.     clearBuffer = function(self)
  187.         self.buffer = {}
  188.     end,
  189.     getOutsideViews = function(self,x,y,xd,yd)
  190.         local vox, voy = 0, 0
  191.         if( x-xd <= self.viewx               ) then vox = -1 end
  192.         if( x+xd >  self.vieww +  self.viewx ) then vox = 1  end
  193.         if( y-yd <= self.viewy               ) then voy = -1 end
  194.         if( y+yd >  self.viewh +  self.viewy ) then voy = 1  end
  195.         if(x-xd < 1 or x+xd > #self.map[1])then
  196.             vox = 0
  197.         end
  198.         if(y-yd < 1 or y+yd > #self.map)then
  199.             voy = 0
  200.         end
  201.         return tonumber(vox), tonumber(voy)
  202.     end,
  203.     isOutsideView = function(self,x,y)
  204.         return (x <= self.roffx) or ( x > self.vieww+self.roffx ) or (y <= self.roffy) or (y > self.viewh+self.roffy)
  205.     end,
  206.     isOutsideViewRelative = function(self,x,y)
  207.         return (x < self.viewx+1) or ( x > self.vieww+self.viewx ) or (y < self.viewy+1) or (y > self.viewh + self.viewy)
  208.     end,
  209.  
  210.     -- Misc pixel utils
  211.     newPixel = function(col,g,s)
  212.         return {col .. g, s}
  213.     end,
  214.     -- Color utils
  215.     getColoredByte = function(byte)
  216.         return 2^tonumber(byte:sub(1,1),16), 2^tonumber(byte:sub(2,2),16)
  217.     end,
  218.     setColors = function(tcol, bcol)
  219.         term.setTextColor(tcol)
  220.         term.setBackgroundColor(bcol)
  221.     end,
  222.  
  223.     -- Map utils
  224.     fillMap = function(self,pix)
  225.         for y = 1, #self.map do
  226.             for x = 1, #self.map[y] do
  227.                 self:setPixel(x,y,pix,s)
  228.             end
  229.         end
  230.     end,
  231.     clearMap = function(self)
  232.         for y = 1, #self.map do
  233.             for x = 1, #self.map[y] do
  234.                 self.map[y][x] = {}
  235.             end
  236.         end
  237.     end,
  238.     setMapRegion = function(self,sx,sy,regiontable)
  239.         for y = sy, #regiontable+sy do
  240.             for x = sx, #regiontable[y-sy]+sx do
  241.                 self.map[y][x] = regiontable[y-sy][x-sx]
  242.             end
  243.         end
  244.     end,
  245.     setMap = function(self, map)
  246.         self.map = map
  247.         if(#map > #self.tmap or #map[1] > #self.tmap)then
  248.            for y = 1, #self.map do
  249.                     self.tmap[y] = {}
  250.                 for x = 1, #self.map[y] do
  251.                     self.tmap[y][x] = {}
  252.                 end
  253.            end
  254.         end
  255.     end,
  256.     getMap = function(self)
  257.         return self.map
  258.     end,
  259.     getRegion = function(self,x,y,xx,yy)
  260.         local retmap = {}
  261.         for yr = y, yy do
  262.             for xr = x, xx do
  263.                 retmap[yr-y][xr-x] = self.map[yr][xr]
  264.             end
  265.         end
  266.         return retmap
  267.     end,
  268.  
  269.     -- Pixel utils
  270.     setSolid = function(self,x,y,s)
  271.         self.map[y][x].solid = s
  272.     end,
  273.     isPixelSolid = function(self,x,y)
  274.         return self.map[y][x].solid
  275.     end,
  276.     getPixel = function(self,x,y)
  277.         if(self.map[y] == nil)then return {"END", solid=true} end
  278.         if(self.map[y][x] == nil)then return {"END", solid=true} end
  279.         return self.map[y][x][1]
  280.     end,
  281.     drawPixel = function(self,x,y,cpix)
  282.         if(cpix:sub(1,1) == "G")then
  283.             term.setTextColor(2^tonumber( self.map[y+self.viewy][x+self.viewx][1]:sub(1,1), 16 ) )
  284.             term.setBackgroundColor(2^tonumber( cpix:sub(2,2), 16) )
  285.         elseif(cpix:sub(2,2) == "G")then
  286.             term.setTextColor(2^tonumber( cpix:sub(1,1), 16) )
  287.             term.setBackgroundColor( 2^tonumber( self.map[y+self.viewy][x+self.viewx][1]:sub(2,2),16 ) )
  288.         else
  289.             self.setColors(self.getColoredByte(cpix))
  290.         end
  291.         if(self:isBuffsize(x, y))then
  292.             self:setBufferPixel(x, y, cpix)
  293.         end
  294.         term.setCursorPos(x + self.roffx, y + self.roffy)
  295.         write(cpix:sub(3,3))
  296.         self:checkHudRedraw(x + self.roffx,y + self.roffy)
  297.     end,
  298.     getPixelRaw = function(self,x,y)
  299.         if(self.map[y] == nil)then return {"END", solid=true} end
  300.         if(self.map[y][x] == nil)then return {"END", solid=true} end
  301.         return self.map[y][x]
  302.     end,
  303.     getPixelAttribute = function(self,x,y,attrib)
  304.         return self.map[y][x][attrib]
  305.     end,
  306.     setPixelAttribute = function(self,x,y,attrib,eq)
  307.         self.map[y][x][attrib] = eq
  308.     end,
  309.     drawMapPixel = function(self,x,y,t)
  310.         if(not self:isOutsideViewRelative(x,y))then
  311.             self.setColors(self.getColoredByte(self.map[y][x][1]))
  312.             term.setCursorPos( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  313.             write(self.map[y][x][1]:sub(3,3))
  314.             if(not t)then
  315.                 self:drawTPixel(x,y)
  316.             end
  317.             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  318.         end
  319.     end,
  320.     drawMapPixelAt = function(self,x,y,dx,dy)
  321.         self.setColors(self.getColoredByte(self.map[y][x][1]))
  322.         term.setCursorPos(dx, dy)
  323.         write(self.map[y][x][1]:sub(3,3))
  324.     end,
  325.     setPixel = function(self,x,y,pix)
  326.         self.map[y][x] = {pix[1],solid=pix[2]}
  327.         if(self:isBuffsize(x-self.viewx, y-self.viewy))then
  328.             self:setBufferPixel(x-self.viewx, y-self.viewy, pix[1])
  329.         end
  330.         if(self.player ~= {})then
  331.             if(x == self.player.x and y == self.player.y)then
  332.                 self:drawPlayer()
  333.             elseif(not self:isOutsideViewRelative(x,y))then
  334.                 self:drawMapPixel(x,y)
  335.             end
  336.         elseif(not self:isOutsideViewRelative(x,y))then
  337.             self:drawMapPixel(x,y)
  338.         end
  339.         self:checkHudRedraw((x - self.viewx) + self.roffx,(y - self.viewy) + self.roffy)
  340.     end,
  341.  
  342.     checkPixelDistance = function(self, fx, fy, tx, ty)
  343.         -- Again, thanks math teacher xD
  344.         local dist = math.sqrt( (tx - fx)^2 + (ty - fy)^2 )
  345.         return math.floor(dist)
  346.     end,
  347.  
  348.     setPixelRelative = function(self,x,y,pix)
  349.         self:setPixel(x + self.viewx - self.roffx, y + self.viewy - self.roffy, pix)
  350.     end,
  351.  
  352.     -- Transparent Pixel utils
  353.     resetTMap = function(self)
  354.         for y = 1, #map do
  355.             ntmap[y] = {}
  356.             for x = 1, #map[y] do
  357.                 ntmap[y][x] = ""
  358.             end
  359.         end
  360.     end,
  361.     getTPixel = function(self,x,y)
  362.         return self.tmap[y][x]
  363.     end,
  364.     isTPixelSolid = function(self,x,y)
  365.         if(self.tmap[y][x] == "")then
  366.             return false
  367.         end
  368.         return self.tmap[y][x]:sub(4,4) == "1"
  369.     end,
  370.     setTPixelSolid = function(self,x,y,s)
  371.         if(s == true )then s = 1 elseif(s == false)then s = 0 end
  372.         if(s == nil)then s = 0 end
  373.         self.tmap[y][x] = self.tmap[y][x]:sub(1,3) .. tostring(s)
  374.     end,
  375.     setTPixel = function(self,x,y,pix,s)
  376.         if(s == true )then s = 1 elseif(s == false)then s = 0 end
  377.         if(s == nil)then s = 0 end
  378.         if(#pix == 3)then
  379.             self.tmap[y][x] = pix .. tostring(s)
  380.         elseif(#pix == 4)then
  381.             self.tmap[y][x] = pix
  382.         end
  383.         if(self:isBuffsize(x-self.viewx, y-self.viewy))then
  384.             self:setBufferPixel(x-self.viewx, y-self.viewy, pix)
  385.         end
  386.         if(not self:isOutsideViewRelative(x,y))then
  387.             self:drawTPixel(x,y)
  388.         end
  389.         self:checkHudRedraw((x - self.viewx) + self.roffx,(y - self.viewy) + self.roffy)
  390.     end,
  391.  
  392.     setTPixelRelative = function(self,x,y,pix,s)
  393.         self:setTPixel(x + self.viewx - self.roffx, y + self.viewy - self.roffy, pix,s)
  394.     end,
  395.     drawTPixel = function(self,x,y)
  396.         local cpix = self:getTPixel(x,y)
  397.         if(cpix ~= "")then
  398.             if(cpix:sub(1,1) == "G")then
  399.                 term.setTextColor(2^tonumber( self.map[y+self.viewy][x+self.viewx][1]:sub(1,1), 16 ) )
  400.                 term.setBackgroundColor(2^tonumber( cpix:sub(2,2), 16) )
  401.             elseif(cpix:sub(2,2) == "G")then
  402.                 term.setTextColor(2^tonumber( cpix:sub(1,1), 16) )
  403.                 term.setBackgroundColor( 2^tonumber( self.map[y+self.viewy][x+self.viewx][1]:sub(2,2),16 ) )
  404.             else
  405.                 self.setColors(self.getColoredByte(cpix))
  406.             end
  407.             if(not self:isOutsideViewRelative(x,y))then
  408.                 term.setCursorPos( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  409.                 write(cpix:sub(3,3))
  410.                 self:checkHudRedraw(x + self.roffx,y + self.roffy)
  411.             end
  412.         end
  413.     end,
  414.     clearTPixel = function(self,x,y)
  415.         self.tmap[y][x] = ""
  416.         self:drawMapPixel(x,y,1)
  417.     end,
  418.     moveTPixel = function(self,fx,fy,ax,ay)
  419.         local cpix = self:getTPixel(fx,fy)
  420.         self:clearTPixel(fx,fy)
  421.         self:setTPixel(fx+ax,fy+ay,cpix)
  422.     end,
  423.     setTPixelPos = function(self,fx,fy,tx,ty)
  424.         local cpix = self:getTPixel(fx,fy)
  425.         self:clearTPixel(fx,fy)
  426.         self:setTPixel(tx,ty,cpix)
  427.     end,
  428.     checkCollision = function(self,cx,cy)
  429.         if(self.player ~= {})then
  430.             return (cx == self.player.x and cy == self.player.y ) or (self:isTPixelSolid(cx,cy)) or (self:isPixelSolid(cx,cy)) or (cx > #self.map[cy]) or (cy > #self.map)  or (cy < 1) or (cx < 1)
  431.         end
  432.     end,
  433.     ---- Player utils
  434.     drawPlayer = function(self)
  435.         if(self.player == {})then return false end
  436.         local xd = self.player.x-self.viewx
  437.         local yd = self.player.y-self.viewy
  438.         if(xd <= 0)then xd = 1 end
  439.         if(yd <= 0)then yd = 1 end
  440.         if(self.player.x > 0 and self.player.y > 0)then
  441.             self:drawPixel( xd, yd ,self.player.pix)
  442.         end
  443.     end,
  444.    
  445.     setPlayerGraphic = function(self,col,g)
  446.         self.player.pix = col .. g
  447.         self:drawPlayer()
  448.     end,
  449.  
  450.     undrawPlayer = function(self)
  451.         if(self.player == {})then return false end
  452.         self:drawMapPixel(self.player.x,self.player.y)
  453.     end,
  454.     drawPlayerAt = function(self,x,y)
  455.         if(self.player == {})then return false end
  456.         self:drawPixel(x,y,self.player.pix)
  457.     end,
  458.     movePlayer = function(self,ox,oy,r)
  459.         if(self.player == {})then return false end
  460.         if(r)then
  461.             self:undrawPlayer()
  462.         end
  463.         if( ( ( self:getPixelRaw( self.player.x + ox, self.player.y + oy ).solid == false and not self:isTPixelSolid( self.player.x + ox, self.player.y + oy ) ) or self.player.scollide == false ) and self.player.mcollide and
  464.             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
  465.             self.player.x = self.player.x + ox
  466.             self.player.y = self.player.y + oy
  467.         else
  468.             if(r)then
  469.                 self:drawPlayer()
  470.             end        
  471.             return false
  472.         end
  473.         if(r)then
  474.             self:drawPlayer()
  475.         end
  476.     end,
  477.     setPlayerPos = function(self,x,y)
  478.         self.player.x = x
  479.         self.player.y = y
  480.     end,
  481.     getPlayerPos = function(self)
  482.         return self.player.x, self.player.y
  483.     end,
  484.     newPlayer = function(self,pcol,pg,px,py,aup,adown,aleft,aright,aact,aspd)
  485.         if(aup == nil or adown == nil or aleft == nil or aright == nil or aact == nil)then
  486.             error("BuffPixel: newPlayer -> missing control!")
  487.         end
  488.         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,interact=aact,spd=aspd,canmove=true}
  489.         self:drawPlayer()
  490.     end,
  491.     setPlayerControls = function(self,aup,adown,aleft,aright,aact)
  492.         self.player.up = aup
  493.         self.player.down = adown
  494.         self.player.left = aleft
  495.         self.player.right = aright
  496.         self.player.interact = aact
  497.     end,
  498.     setPlayerCollisionType = function(self,colw)
  499.         self.player.mcollide = colw
  500.     end,
  501.     playerSolidCollision = function(self,tf)
  502.         self.player.scollide = tf
  503.     end,
  504.     clearPlayerEvents = function(self)
  505.         self.player.events = {}
  506.     end,
  507.     addPlayerEvent = function(self,name,evtbl,call)
  508.         self.player.events[name] = {evs = evtbl,func = call}
  509.     end,
  510.     removePlayerEvent = function(self,name)
  511.         if(self.player.events[name] ~= nil)then
  512.            self.player.events[name] = nil
  513.         end
  514.     end,
  515.     updatePlayer = function(self,ev,spd)
  516.         -- Compare user specific player events
  517.         for k, v in pairs(self.player.events) do
  518.             if(ev[1] == v.evs[1] and ev[2] == v.evs[2])then
  519.              v:func()
  520.             end
  521.         end
  522.         -- Player Input and movement
  523.         if(ev[1] == "key")then
  524.             local key = ev[2]
  525.             if(self.player.canmove)then
  526.                     if(key == self.player.up   )then
  527.                         self:movePlayer(0,-self.player.spd,true)
  528.                 elseif(key == self.player.down )then
  529.                         self:movePlayer(0,self.player.spd,true)
  530.                 elseif(key == self.player.left )then
  531.                         self:movePlayer(-self.player.spd,0,true)
  532.                 elseif(key == self.player.right)then
  533.                         self:movePlayer(self.player.spd,0,true)
  534.                 end
  535.             end
  536.             if(spd ~= nil)then
  537.                 sleep(spd)
  538.             end
  539.         end
  540.  
  541.         for k, v in pairs(self.entities) do
  542.             if(v.alive ~= nil)then
  543.                 if(v.alive == false)then
  544.                     table.remove(self.entities,k)
  545.                     return false
  546.                 end
  547.             end
  548.             if(ev[1] == "key" and self.player ~= {})then
  549.                 if(ev[2] == self.player.interact and v.interact ~= nil)then
  550.                     if(v.getPos == nil)then error("updateEntities -> Must have function getPos return X and Y for an interactable entity. ") end
  551.                     if(self:checkPlayerDistance(v:getPos()) <= 1)then
  552.                         v:interact(ev)
  553.                     end
  554.                 end
  555.             end
  556.         end
  557.     end,
  558.     checkPlayerDistance = function(self,x,y)
  559.         -- checks players distance to block
  560.         -- Thanks math teacher :)
  561.         local dist = math.sqrt( (x - self.player.x)^2 + (y - self.player.y)^2 )
  562.         return math.floor(dist)
  563.     end,
  564.     -- Entity utils
  565.     addEntity = function(self,entity)
  566.         self.entities[#self.entities] = entity
  567.     end,
  568.     addEntityByName = function(self, name, entity)
  569.         if(self.entities[name] == nil)then
  570.             self.entities[name] = entity
  571.             return true
  572.         end
  573.         return false
  574.     end,
  575.     removeEntity = function(self, name)
  576.         if(self.entities[name] ~= nil)then
  577.             table.remove(self.entities,name)
  578.             return true
  579.         end
  580.         return false
  581.     end,
  582.     updateEntities = function(self, e)
  583.         for k, v in pairs(self.entities) do
  584.             if(v.alive ~= nil)then
  585.                 if(v.alive == false)then
  586.                     table.remove(self.entities,k)
  587.                     return false
  588.                 end
  589.             end
  590.             if(v.update ~= nil)then
  591.                 v:update(e)
  592.             end
  593.         end
  594.     end,
  595.  
  596.     -- Hud utils
  597.     renderHuds = function(self)
  598.         for k, v in pairs(self.huds) do
  599.             term.setCursorPos(v.offx, v.offy)
  600.             v.draw()
  601.         end
  602.     end,
  603.  
  604.     updateHuds = function(self,e)
  605.         for k, v in pairs(self.huds) do
  606.             term.setCursorPos(v.offx, v.offy)
  607.             v.update(e)
  608.         end
  609.     end,
  610.    
  611.     drawHud = function(self,name)
  612.         if(self.huds[name] ~= nil)then
  613.             self.huds[name].draw()
  614.         end
  615.     end,
  616.  
  617.     checkHudRedraw = function(self,x,y)
  618.         for k, v in pairs(self.huds) do
  619.             --term.setCursorPos(1,2)
  620.             --write(x .. ">= " .. v.offx .. " " .. x .. "<= " .. v.width + v.offx-1)
  621.             --term.setCursorPos(1,3)
  622.             --write(y .. ">= " .. v.offy .. " " .. y .. "<= " .. v.height + v.offy-1)
  623.             if( x >= v.offx and x <= v.width + v.offx-1 and y >= v.offy and y <= v.height + v.offy -1 )then
  624.                 self:drawHud(k)
  625.             end
  626.         end
  627.     end,
  628.  
  629.     newHud = function(self,name,xo,yo,w,h,drawf,updatef)
  630.         self.huds[name] = {offx=xo,offy=yo,width=w,height=h,draw=drawf,update=updatef}
  631.         self:drawHud(name)
  632.     end,
  633.  
  634.     removeHud = function(self, name)
  635.         if(self.huds[name] ~= nil)then
  636.             self.huds[name] = nil
  637.         end
  638.     end,
  639.  
  640.     setHudCursorPos = function(self,name,x,y)
  641.         if(self.huds[name] ~= nil)then
  642.             term.setCursorPos(self.huds[name].offx + x, self.huds[name].offy + y)
  643.         end
  644.     end,
  645.  
  646.     -- Special event handlers
  647.     getMousePlaceBlock = function(self,e,blockone,blocktwo)
  648.         if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
  649.           if(not self:isOutsideView(e[3],e[4]))then
  650.              if(e[2] == 1 and blockone ~= nil)then
  651.                  self:setPixelRelative(e[3],e[4],blockone)
  652.              elseif(blocktwo ~= nil)then
  653.                  self:setPixelRelative(e[3],e[4],blocktwo)
  654.              end
  655.           end
  656.        end
  657.     end,
  658.  
  659.     -- Clock Utils
  660.     setClock = function(self,t)
  661.         self.clock = os.startTimer(t)
  662.         self.time = t
  663.     end,
  664.     updateWithClock = function(self,e)
  665.         if(e[1] == "timer" and e[2] == self.clock)then
  666.             self:updateEntities(e)
  667.             self:updateHuds(e)
  668.             self.clock = os.startTimer(self.time)
  669.         end
  670.     end,
  671.  
  672.     -- Render utils
  673.     renderSection = function(self,fx,fy,tx,ty)
  674.         term.current().setVisible(false)
  675.         if(#self.buffer ~= self.viewh or #self.buffer[1] ~= self.vieww)then
  676.             self:initBuffer()
  677.         end
  678.         self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] = {self.player.pix, solid=false}
  679.         for y=fy, ty do
  680.                 for x=fx, tx do
  681.                    
  682.                         -- Draw the pixel
  683.                         self:drawMapPixel(x,y)
  684.                         if(opt == true)then
  685.                             self.setColors(self.getColoredByte(self.map[y][x][1]))
  686.                             term.setCursorPos( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  687.                             write("R")
  688.                         end
  689.                         -- Add the pixel to the buffer
  690.                         self:setBufferPixel(x-self.viewx, y-self.viewy, self.map[y][x][1])
  691.                         self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  692.                     if( self.tmap[y][x] ~= "")then
  693.                             -- Draw the player
  694.                             self:drawTPixel(x,y)
  695.                             if(opt == true)then
  696.                                 self.setColors(self.getColoredByte(self.tmap[y][x]))
  697.                                 term.setCursorPos( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  698.                                 write("R")
  699.                             end
  700.                             -- Add the pixel to the buffer
  701.                             self:setBufferPixel(x-self.viewx, y-self.viewy, self.tmap[y][x])
  702.                             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy) 
  703.                     end
  704.                     if(self.player ~= {})then
  705.                             -- Draw the player
  706.                             self:drawPlayer()
  707.                             -- Add the pixel to the buffer
  708.                             self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] = {self.player.pix}
  709.                             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy) 
  710.                     end
  711.                 end
  712.             end
  713.         local success, err = pcall(self.renderHuds,self)
  714.         if( not success )then
  715.             term.current().setVisible(true)
  716.             print("Hud renderer errored with: " .. err)
  717.         end
  718.         term.current().setVisible(true)
  719.     end,
  720.     refreshRender = function(self, vis)
  721.         if( not vis )then term.current().setVisible(false) end
  722.         if(#self.buffer ~= self.viewh or #self.buffer[1] ~= self.vieww)then
  723.             self:initBuffer()
  724.         end
  725.         for y=self.viewy+1, self.viewy + self.viewh do
  726.                 for x=self.viewx+1, self.viewx + self.vieww do
  727.                     -- If we have a new pixel in view
  728.                         -- Draw the pixel
  729.                         self:drawMapPixel(x,y)
  730.                         -- Add the pixel to the buffer
  731.                         self:setBufferPixel(x-self.viewx, y-self.viewy, self.map[y][x][1])
  732.                         self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  733.  
  734.                     if( (self.buffer[(y-self.viewy)][(x-self.viewx)] ~= self.tmap[y][x]) and self.tmap[y][x] ~= "")then
  735.                             -- Draw the player
  736.                             self:drawTPixel(x,y)
  737.                             -- Add the pixel to the buffer
  738.                             self:setBufferPixel(x-self.viewx, y-self.viewy, self.tmap[y][x])
  739.                             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy) 
  740.                     end
  741.                     if(self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] ~= {self.player.pix})then
  742.                             -- Draw the player
  743.                             self:drawPlayer()
  744.                             -- Add the pixel to the buffer
  745.                             self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] = {self.player.pix}
  746.                             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy) 
  747.                     end
  748.             end
  749.         end
  750.         local success, err = pcall(self.renderHuds,self)
  751.         if( not success )then
  752.             term.current().setVisible(true)
  753.             print("Hud renderer errored with: " .. err)
  754.         end
  755.         if( not vis )then term.current().setVisible(true) end
  756.         if(self.player ~= {})then self:drawPlayer() end
  757.     end,
  758.  
  759.     render = function(self,opt)
  760.         term.current().setVisible(false)
  761.         if(#self.buffer ~= self.viewh or #self.buffer[1] ~= self.vieww)then
  762.             self:initBuffer()
  763.         end
  764.         for y=self.viewy+1, self.viewy + self.viewh do
  765.                 for x=self.viewx+1, self.viewx + self.vieww do
  766.                     -- If we have a new pixel in view
  767.                     if(self.buffer[(y-self.viewy)][(x-self.viewx)] ~= self.map[y][x][1])then
  768.                         -- Draw the pixel
  769.                         self:drawMapPixel(x,y)
  770.                         if(opt == true)then
  771.                             self.setColors(self.getColoredByte(self.map[y][x][1]))
  772.                             term.setCursorPos( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  773.                             write("R")
  774.                         end
  775.                         -- Add the pixel to the buffer
  776.                         self:setBufferPixel(x-self.viewx, y-self.viewy, self.map[y][x][1])
  777.                         self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  778.                     end
  779.                     if( (self.buffer[(y-self.viewy)][(x-self.viewx)] ~= self.tmap[y][x]) and self.tmap[y][x] ~= "")then
  780.                             -- Draw the player
  781.                             self:drawTPixel(x,y)
  782.                             if(opt == true)then
  783.                                 self.setColors(self.getColoredByte(self.tmap[y][x]))
  784.                                 term.setCursorPos( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy)
  785.                                 write("R")
  786.                             end
  787.                             -- Add the pixel to the buffer
  788.                             self:setBufferPixel(x-self.viewx, y-self.viewy, self.tmap[y][x])
  789.                             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy) 
  790.                     end
  791.                     if(self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] ~= {self.player.pix})then
  792.                             -- Draw the player
  793.                             self:drawPlayer()
  794.                             -- Add the pixel to the buffer
  795.                             self.buffer[(self.player.y-self.viewy)][(self.player.x-self.viewx)] = {self.player.pix}
  796.                             self:checkHudRedraw( (x-self.viewx) + self.roffx, (y-self.viewy) + self.roffy) 
  797.                     end
  798.                 end
  799.         end
  800.         local success, err = pcall(self.renderHuds,self)
  801.         if( not success )then
  802.             term.current().setVisible(true)
  803.             print("Hud renderer errored with: " .. err)
  804.         end
  805.         term.current().setVisible(true)
  806.     end,
  807.     setView = function(self,nx,ny,nw,nh)
  808.         local oh, ow = self.viewh, self.vieww
  809.         self.viewx = nx
  810.         self.viewy = ny
  811.         self.vieww = nw
  812.         self.viewh = nh
  813.         if(nw ~= ow or nh ~= oh)then self:initBuffer() end
  814.         if( self:checkViews() )then return self:render() end
  815.     end,
  816.  
  817.     updateView = function(self,ox,oy,ow,oh)
  818.         local ovx = self.viewx
  819.         local ovy = self.viewy
  820.         local ovh = self.viewh
  821.         local ovw = self.vieww
  822.         self.viewx = self.viewx + ox
  823.         self.viewy = self.viewy + oy
  824.         self.vieww = self.vieww + ow
  825.         self.viewh = self.viewh + oh
  826.         if(ow ~= 0 or oh ~= 0)then self:initBuffer() end
  827.         if( self:checkViews() )then return self:render() end
  828.     end,
  829.  
  830.     lerpView = function(self,ox,oy,ax,ay,time)
  831.         -- ox, and oy are directional variables
  832.         -- ax, and ay are the amount to move the view
  833.         -- time is how long to wait before moving the view again
  834.         self.player.canmove = false
  835.         if(ox ~= 0 )then
  836.              term.current().setVisible(false)
  837.              self:undrawPlayer()
  838.              for x = 1, ax do
  839.                 self:updateView(ox,0,0,0,true)
  840.                 if(time ~= nil)then
  841.                     sleep(time)
  842.                 end
  843.              end
  844.             self.clock = os.startTimer(self.time)
  845.         end
  846.        if(oy ~= 0 )then
  847.           term.current().setVisible(false)
  848.           self:undrawPlayer()
  849.           for y = 1, ay do
  850.              self:updateView(0,oy,0,0,true)
  851.              if(time ~= nil)then
  852.                 sleep(time)
  853.              end
  854.           end
  855.           if(self.time ~= 0)then self.clock = os.startTimer(self.time) end
  856.        end
  857.        self.player.canmove = true
  858.     end,
  859.  
  860.  
  861.     -- Screen utils
  862.     checkScreenCollision = function(self, x, y, w, h)
  863.         return (x >= self.roffx-1 and x <= self.vieww + self.roffx + 1 and y >= self.roffy-1 and y <= self.viewh + self.roffy + 1)
  864.     end,
  865.     setBorderText = function(self, txt, color)
  866.         self.btext = {txt, color}
  867.     end,
  868.     drawBorder = function(self, color)
  869.         paintutils.drawBox(self.roffx, self.roffy, self.vieww+self.roffx+1, self.viewh+self.roffy+1, color)
  870.         term.setCursorPos(self.roffx , self.roffy)
  871.         --+ (self.vieww /2) - (#self.btext[1] /2) centered text
  872.         term.setTextColor(self.btext[2])
  873.         write(self.btext[1]:sub(1,self.vieww))
  874.     end,
  875.     clearScreen = function(self,color)
  876.         paintutils.drawFilledBox(self.roffx, self.roffy, self.vieww+self.roffx+1, self.viewh+self.roffy+1, color)
  877.     end,
  878.     setScreenPos = function(self,tox,toy, color)
  879.         term.current().setVisible(false)
  880.         self:clearScreen(color)
  881.         self.roffx = tox
  882.         self.roffy = toy
  883.         self:refreshRender()
  884.         term.current().setVisible(true)
  885.     end,
  886.     moveScreen = function(self,tox,toy, color)
  887.         term.current().setVisible(false)
  888.         self:clearScreen(color)
  889.         self.roffx = self.roffx + tox
  890.         self.roffy = self.roffy + toy
  891.         self:refreshRender()
  892.         term.current().setVisible(true)
  893.     end,
  894.  
  895.     -- Dialog utils
  896.     openDialog = function(self,bcol,tcol)
  897.             self.dialogln = 0
  898.             paintutils.drawBox(self.roffx+1,self.viewh + self.roffy-5,self.vieww+self.roffx,self.viewh + self.roffy,bcol)
  899.             paintutils.drawFilledBox(self.roffx+2,self.viewh + self.roffy-4,self.vieww+self.roffx-1,self.viewh + self.roffy-1,tcol)
  900.     end,
  901.      
  902.     clearDialog = function(self,tc,bc)
  903.         self.dialogln = 0
  904.         self:openDialog(tc,bc)
  905.     end,
  906.      
  907.     drawDialog = function(self,text,speed,txcol)
  908.             term.setCursorPos(self.roffx+2,self.roffy+self.viewh- ( 4 - ( self.dialogln ) ) )
  909.             term.setTextColor(txcol)
  910.             local x,y = term.getCursorPos()
  911.             local st = 0
  912.             for i = 1, #text do
  913.                 if(i-st > self.vieww-4)then
  914.                     text = text:sub(1,i) .. "\n" .. text:sub(i+1,-1)
  915.                     st = i
  916.                 end
  917.             end
  918.             local ln = 0
  919.             for line in text:gmatch("[^\n]+") do
  920.                 term.setCursorPos(x,y+ln)
  921.                 textutils.slowPrint(line,speed)
  922.                 ln = ln + 1
  923.             end
  924.             if(self.time ~= 0)then self.clock = os.startTimer(self.time) end
  925.     end,
  926.      
  927.     getDialogYesNo = function(self,YText,NText,ln)
  928.             local sel = 1
  929.             local waiting = true
  930.             local opt = {
  931.                     {name=YText};
  932.                     {name=NText};
  933.             }
  934.      
  935.             function waitResult()
  936.                     while waiting do
  937.                             for i = 1, #opt do
  938.                                     term.setCursorPos(self.roffx+(self.vieww/2) - #opt[i].name/2,self.roffy + self.viewh- ( 4 - ( (ln+i)-1 ) ) )
  939.                                     if(sel == i )then
  940.                                             write("["..opt[i].name.."]")
  941.                                     else
  942.                                             write(" "..opt[i].name.." ")
  943.                                     end
  944.                             end
  945.      
  946.                             a = {os.pullEvent("key")}
  947.      
  948.                             if(a[2] == keys.w and sel > 1)then
  949.                                     sel = sel - 1
  950.                             end
  951.                             if(a[2] == keys.s and sel < #opt)then
  952.                                     sel = sel + 1
  953.                             end
  954.                             if(a[2] == keys.space)then
  955.                                     if(sel == 1)then  waiting = false return "yes" end
  956.                                     if(sel == 2)then  waiting = false return  "no" end
  957.                             end
  958.                     end
  959.             end
  960.             if(self.time ~= 0)then self.clock = os.startTimer(self.time) end
  961.             return waitResult()
  962.     end,
  963.      
  964.     closeDialog = function(self)
  965.         os.pullEvent("key")
  966.         self.dialogln = 0
  967.         self:renderSection(1,self.viewh-5,self.vieww+self.viewx,self.viewh+self.viewy)
  968.     end,
  969.      
  970.     closeDialogRaw = function(self)
  971.         self.dialogln = 0
  972.         self:renderSection(1,self.viewh-5,self.vieww+self.viewx,self.viewh+self.viewy)
  973.     end
  974.  
  975. }
  976.     mutils.viewx = nx
  977.     mutils.viewy = ny
  978.     mutils.vieww = nw
  979.     mutils.viewh = nh
  980.     return mutils
  981. end
  982.  
  983. function exportMap(map, file)
  984.     if(fs.exists(file))then
  985.         error("exportMap: File already exists: " .. file)
  986.     else
  987.         local f = fs.open(file,"w")
  988.         map = textutils.serialize(map)
  989.         map = map:gsub("n", "")
  990.         map = map:gsub("%s+", "")
  991.         f.write(map)
  992.         f.close()
  993.     end
  994. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement