SHARE
TWEET

lava runner

electronic_steve Apr 13th, 2016 (edited) 377 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- lava runner by electronic_steve.
  2. local event = require "event"
  3. local com   = require "component"
  4. local term  = require "term"
  5. local gpu   = com.gpu
  6. local config={graphics_mode=1,timer=5,render_radius=5}
  7. local screen={config.render_radius*2+1,config.render_radius+1}
  8. local quad={{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1}}
  9. local mquad={{0,-1},{1,0},{0,1},{-1,0}}
  10. local amquad={{-1,-1},{1,1},{1,-1},{-1,1}}
  11. local old={bg=0,fg=0}
  12. local images={
  13.   [-5]  ={"()","()","()"},
  14.   [-4]  ={"..","..",".."},
  15.   [-3]  ={"░░","░░","░░"},
  16.   [-2]  ={"▓▓","▓▓","▓▓"},
  17.   [-1]  ={"  ","  ","  "},
  18.   [0]   ={"ER","ER","ER"},
  19.   [1]   ={"◀▶","██","██"},  --графониум!
  20.   [2]   ={"◥◤","██","▀▀"},
  21.   [3]   ={"◀█","██"," █"},
  22.   [4]   ={"◥█","██","▀█"},
  23.   [5]   ={"◢◣","██","▄▄"},
  24.   [6]   ={"██","██","██"},
  25.   [7]   ={"◢█","██","▄█"},
  26.   [8]   ={"██","██","██"},
  27.   [9]   ={"█▶","██","█ "},
  28.   [10]  ={"█◤","██","█▀"},
  29.   [11]  ={"██","██","██"},
  30.   [12]  ={"██","██","██"},
  31.   [13]  ={"█◣","██","█▄"},
  32.   [14]  ={"██","██","██"},
  33.   [15]  ={"██","██","██"},
  34.   [16]  ={"██","██","██"},
  35. }
  36.  
  37. local colors={
  38.   [0]  =0xffffff,
  39.   [-4] =0x222222,
  40.   [-2] =0xff0000,
  41.   [-3] =0xff0000,
  42.   [-5] =0xffffff,
  43.   [-6] =0xff0000,
  44. }
  45.  
  46. for i=1,16 do
  47.   colors[i]=0x222222
  48. end
  49.  
  50. local keys={
  51.   --[[A]]   [30]=function() move(4) end,
  52.   --[[W]]   [17]=function() move(1) end,
  53.   --[[D]]   [32]=function() move(2) end,
  54.   --[[S]]   [31]=function() move(3) end,
  55.   --[[TAB]] [15]=function() config.graphics_mode=looper(1,config.graphics_mode+1,3) end,
  56. }
  57.  
  58. event.shouldInterrupt = function()  return false end
  59. gpu.setResolution(config.render_radius*4+2,config.render_radius*2+1)
  60. term.clear()
  61.  
  62. function move(id)
  63.   local lx,ly=map.player.x+mquad[id][1],map.player.y+mquad[id][2] local tile=map:get(lx,ly) if tile.type==-1 then map.player.x=lx map.player.y=ly end map:lava_update()
  64. end
  65.  
  66. function looper(min,n,max)
  67.   if n>max then n=n%max end
  68.   if n<min then n=(n-min)%max+min end
  69.   local J=math.abs(math.modf((n-min)/max))
  70.   return n,J
  71. end
  72.  
  73.  
  74. -- карта.
  75. local empty_tile={type="none"}
  76. local map_meta={
  77.   generate=function(self,size,N)
  78.     self.player.level=self.player.level+1
  79.     self.timer=config.timer
  80.     self.tiles={}
  81.     size=size or 10
  82.     N=N or 3
  83.     for X=1,N do
  84.       local x,y=0,0
  85.       for Y=1,size do
  86.         p={}
  87.         for i=1,4 do
  88.           local t=self:get(x+mquad[i][1],y+mquad[i][2]).type
  89.           if t=="none" or t==-1  then table.insert(p,i) end
  90.         end
  91.         if #p>0 then
  92.         local id = math.random(1,#p)
  93.           x=x+mquad[id][1]
  94.           y=y+mquad[id][2]
  95.           self.player.x=x
  96.           self.player.y=y
  97.           self:set(x,y,{type=-1})
  98.         end
  99.       end
  100.     end
  101.     for _,i in pairs(self:get_all(-1)) do
  102.       local x,y,tile=i[1],i[2],i[3]
  103.       for i=1,8 do
  104.         local X,Y=x+quad[i][1],y+quad[i][2]
  105.         if self:get(X,Y).type=="none"  then self:set(X,Y,{type=0}) end
  106.       end
  107.     end
  108.     for _,i in pairs(self:get_all(0)) do
  109.       local x,y,tile=i[1],i[2],i[3]
  110.       local imgid=1
  111.       for i=1,4 do
  112.         local X,Y=x+mquad[i][1],y+mquad[i][2]
  113.         if self:get(X,Y).type==0 then
  114.           imgid=imgid+2^(i-1)
  115.         end
  116.       end
  117.  
  118.       self:get(x,y).alt_type=imgid
  119.     end
  120.     local winer={0,0,0}
  121.     for _,i in pairs(self:get_all(-1)) do
  122.       local x,y,tile=i[1],i[2],i[3]
  123.       local dist=math.sqrt((self.player.x-x)^2+(self.player.y-y)^2)^0.5
  124.       if winer[1]<dist then winer={dist,x,y} end
  125.     end
  126.     self:set(winer[2],winer[3],{type=-1,alt_type=-5})
  127.   end,
  128.   set=function(self,x,y,tile)
  129.     if self.tiles[x] then self.tiles[x][y]=tile else self.tiles[x]={[y]=tile} end
  130.   end,
  131.   get=function(self,x,y)
  132.     if self.tiles[x] then return self.tiles[x][y] or empty_tile else return empty_tile end
  133.   end,
  134.   get_all=function(self,filt)
  135.     local out={}
  136.     for x,tbl in pairs(self.tiles) do
  137.       for y,tile in pairs(tbl) do
  138.         if filt then
  139.           if tile.type==filt then
  140.             table.insert(out,{x,y,tile})
  141.           end
  142.         else
  143.           table.insert(out,{x,y,tile})
  144.         end
  145.       end
  146.     end
  147.     return out
  148.   end,
  149.   lava_update=function(self)
  150.     if self.timer==0 then self:set(self.tx,self.ty,{type=-3}) elseif self.timer==config.timer then self.tx,self.ty=self.player.x,self.player.y end
  151.     if self.timer~=-1 then self.timer=self.timer-1 end
  152.     for _,body in pairs(self:get_all(-3)) do
  153.       for i=1,4 do
  154.         local x,y=body[1]+mquad[i][1],body[2]+mquad[i][2]
  155.         local tile=self:get(x,y)
  156.         if tile.type==-1 then self:set(x,y,{type=-3}) else if tile.type~=-3 then tile.bg=colors[-6] self:set(x,y,tile) end end
  157.       end
  158.       self:set(body[1],body[2],{type=-2})
  159.     end
  160.     if self:get(self.player.x,self.player.y).type==-3 then gpu.setResolution(gpu.maxResolution()) term.clear() print("ты сгорел в лаве на "..self.player.level.." уровне пещер.")  os.exit() end
  161.   end,
  162.   update=function(self)
  163.     -- регенерация уровня
  164.     if  self:get(self.player.x,self.player.y).alt_type==-5 then
  165.       self.player.x,self.player.y=nil,nil
  166.       local p=self.player
  167.       self:generate(5*(math.min(self.player.level,5)),math.min(self.player.level,3)*2)
  168.       self.player=p
  169.       self:draw()
  170.     end
  171.     -- управление
  172.     local tile=self:get(self.player.x,self.player.y)
  173.     if tile.type==-1 then tile.alt_type=-4 self:set(self.player.x,self.player.y,tile) end
  174.     _,_,_,code=event.pull("key_down")
  175.     if code==16 then gpu.setResolution(gpu.maxResolution()) term.clear() print("ты вышел на "..self.player.level.." уровне  пещер.")  os.exit() else if keys[code] then keys[code]() end end
  176.   end,
  177.   draw=function(self,ox,oy)
  178.     term.clear()
  179.     for rx=-config.render_radius,config.render_radius do
  180.       for ry=-config.render_radius,config.render_radius do
  181.         local X,Y=map.player.x+rx,map.player.y+ry
  182.         local dx,dy=rx*2+screen[1],ry+screen[2]
  183.         local tile=map:get(X,Y)
  184.         if tile.type~="none" then
  185.           if tile.bg then setbg(tile.bg) end
  186.           if tile.alt_type then
  187.             if colors[tile.alt_type] then setfg(colors[tile.alt_type]) end
  188.             gpu.set(dx,dy,images[tile.alt_type][config.graphics_mode])
  189.           else
  190.             if colors[tile.type] then setfg(colors[tile.type]) end
  191.             gpu.set(dx,dy,images[tile.type][config.graphics_mode])
  192.           end
  193.           setbg(0)
  194.         end
  195.       end
  196.     end
  197.     gpu.setForeground(0xff0000)
  198.     gpu.set(screen[1],screen[2],"@@")
  199.     gpu.setForeground(0xFFFFFF)
  200.     old.fg=0xFFFFFF
  201.   end,
  202.   delete=function(self,x,y)
  203.     self.tiles[x][y]=nil
  204.   end,
  205.   delete_all=function(self,filt)
  206.     for x,tbl in pairs(self) do
  207.       for y,tile in pairs(tbl) do
  208.         if filt then
  209.           if tile.type==filt then
  210.             self.tiles[x][y]=nil
  211.           end
  212.         else
  213.           self.tiles[x][y]=nil
  214.         end
  215.       end
  216.     end
  217.   end
  218. } map_meta.__index=map_meta
  219. function setfg(x)
  220.   if x~=old.fg then gpu.setForeground(x) old.fg=x end
  221. end
  222. function setbg(x)
  223.   if x~=old.bg then gpu.setBackground(x) old.bg=x end
  224. end
  225. function create_map(size,N)
  226.   local map=setmetatable({tiles={},player={level=0,r=5,x=0,y=0},timer=config.timer,tx=0,ty=0},map_meta)
  227.   map:generate(size,N)
  228.   return map
  229. end
  230.  
  231. map=create_map(5,4)
  232. while true do
  233.   os.sleep(0)
  234.   map:draw() map:update()
  235. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top