billysback

ninja

Jul 10th, 2013
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.91 KB | None | 0 0
  1. local curCols = {}
  2. buffer_details = {1, 1, colors.black, colors.white, nil}
  3. local sw,sh = term.getSize()
  4.  
  5. function getColorTab(char, background, text)
  6.     if curCols[char] ~= nil then
  7.         if curCols[char][background] ~= nil then
  8.             if curCols[char][background][text] ~= nil then
  9.                 return curCols[char][background][text]
  10.             end
  11.         end
  12.     end
  13.     return nil
  14. end
  15.  
  16. function addColorTab(col)
  17.     local char = col.char
  18.     local background = col.background
  19.     local text = col.text
  20.     if getColorTab(char, background, text) == nil then
  21.         if curCols[char] == nil then curCols[char] = {} end
  22.         if curCols[char][background] == nil then curCols[char][background] = {} end
  23.         curCols[char][background][text] = col
  24.         return true
  25.     end
  26.     return false
  27. end
  28.  
  29. function createColor(char, background, text)
  30.     --sleep(0)
  31.     local col = getColorTab(char, background, text)
  32.     if col == nil then
  33.         col = {}
  34.         col.char = char
  35.         col.background = background
  36.         col.text = text
  37.         addColorTab(col)
  38.     end
  39.     return col
  40. end
  41.  
  42. function createPixel(x, y, col)
  43.     local pixel = {}
  44.     pixel.x = x
  45.     pixel.y = y
  46.     pixel.col = col
  47.     pixel.compare = function(self, col)
  48.         return self.col == col
  49.     end
  50.     return pixel
  51. end
  52.  
  53.  
  54. function createBuffer()
  55.     local buffer = {}
  56.     buffer.width = sw
  57.     buffer.height = height
  58.     buffer.changes = {}
  59.     buffer.changed = {}
  60.     buffer.term = term
  61.    
  62.     buffer_details[5] = buffer
  63.     for x=1,sw do
  64.         buffer[x] = {}
  65.         buffer.changes[x] = {}
  66.         for y=1,sh do
  67.             --print("x="..x..", y="..y)
  68.             buffer[x][y] = createPixel(x, y, createColor(" ", colors.black, colors.white))
  69.             buffer.changes[x][y] = false
  70.         end
  71.     end
  72.    
  73.     buffer.get = function(self, x, y)
  74.         if self[x] ~= nil then
  75.             if self[x][y] ~= nil then
  76.                 return self[x][y]
  77.             end
  78.         end
  79.         return nil
  80.     end
  81.    
  82.     buffer.check = function(self, x, y)
  83.         if self.changes[x] ~= nil then
  84.             if self.changes[x][y] ~= nil then
  85.                 return self.changes[x][y]
  86.             end
  87.         end
  88.         return false
  89.     end
  90.    
  91.     buffer.set = function(self, x, y, col)
  92.         local pixel = self:get(x,y)
  93.         if pixel ~= nil then
  94.             if not pixel:compare(col) then
  95.                 pixel.col = col
  96.                 if not self:check(x,y) then
  97.                     self.changed[#self.changed + 1] = {x,y}
  98.                     if self.changes[x] == nil then self.changes[x] = {} end
  99.                     self.changes[x][y] = true
  100.                 end
  101.             end
  102.         end
  103.     end
  104.    
  105.     buffer.render = function(self)
  106.         for i=1,#self.changed do
  107.             local pos = self.changed[i]
  108.             self.changes[pos[1]][pos[2]] = false
  109.             local pixel = self[pos[1]][pos[2]]
  110.             self.term.setCursorPos(pixel.x, pixel.y)
  111.             self.term.setBackgroundColor(pixel.col.background)
  112.             self.term.setTextColor(pixel.col.text)
  113.             self.term.write(pixel.col.char)
  114.         end
  115.         term.setCursorPos(1, 13)
  116.         term.setBackgroundColor(colors.black)
  117.         term.setTextColor(colors.white)
  118.         print(#self.changed.." pixels changed")
  119.         self.changed = {}
  120.     end
  121.    
  122.     buffer.setBuffer = function(self)
  123.         buffer_detials[5] = self
  124.     end
  125.    
  126.     buffer.setCursorPos = function(x, y)
  127.         buffer_details[1] = x
  128.         buffer_details[2] = y
  129.     end
  130.    
  131.     buffer.setBackgroundColor = function(col)
  132.         buffer_details[3] = col
  133.     end
  134.    
  135.     buffer.setTextColor = function(col)
  136.         buffer_details[4] = col
  137.     end
  138.    
  139.     buffer.getSize = function()
  140.         local buffer = buffer_details[5]
  141.         if buffer == nil then print("NIL_BUFFER") end
  142.         return buffer_details[5].width, buffer_details[5].height
  143.     end
  144.    
  145.     buffer.write = function(char)
  146.         char = char:sub(1,1)
  147.         local col = createColor(char, buffer_details[3], buffer_details[4])
  148.         local buffer = buffer_details[5]
  149.         if buffer ~= nil then
  150.             buffer:set(buffer_details[1], buffer_details[2], col)
  151.         end
  152.     end
  153.    
  154.     buffer.clear = function()
  155.         buffer_details[5]:reset()
  156.     end
  157.    
  158.     buffer.reset = function(self)
  159.         local buffer = self
  160.         for x=1,self.width do
  161.             buffer[x] = {}
  162.             buffer.changes[x] = {}
  163.             for y=1,sh do
  164.                 --print("x="..x..", y="..y)
  165.                 local pixel = createPixel(x, y, createColor(" ", buffer_details[3], colors.white))
  166.                 term.setCursorPos(x, y)
  167.                 term.setBackgroundColor(pixel.col.background)
  168.                 term.setTextColor(pixel.col.text)
  169.                 term.write(pixel.col.char)
  170.                 buffer[x][y] = pixel
  171.                
  172.                 buffer.changes[x][y] = false
  173.             end
  174.         end
  175.         buffer.changes = {}
  176.         buffer.changed = {}
  177.     end
  178.    
  179.     return buffer
  180. end
  181.  
  182. print("making buffer...")
  183. local buffer = createBuffer()
  184. print("overriding buffer...")
  185. --buffer = buffer
  186. print("setting buffer details...")
  187. buffer_details[5] = buffer
  188. if buffer_details[5] == nil then print("NO BUFFER :<") end
  189.  
  190. --[[
  191.  
  192.     GAME CODE;
  193.  
  194. ]]
  195.  
  196. print("testing size...")
  197. --local sw,sh = buffer.getSize()
  198.  
  199. local data = {
  200.     ["+"] = {
  201.         name = "wall",
  202.         solid = true,
  203.         id = 2,
  204.         grip = false,
  205.         col = {colors.brown, colors.black, " "},
  206.         finish = false,
  207.         boundry = true,
  208.         checkpoint = false,
  209.     },
  210.     ["|"] = {
  211.         name = "rail",
  212.         solid = true,
  213.         id = 3,
  214.         grip = true,
  215.         col = {colors.brown, colors.white, "|"},
  216.         finish = false,
  217.         boundry = true,
  218.         checkpoint = false,
  219.     },
  220.     [" "] = {
  221.         name = "air",
  222.         solid = false,
  223.         id = 1,
  224.         grip = false,
  225.         col = {colors.white, colors.black, " "},
  226.         finish = false,
  227.         boundry = false,
  228.         checkpoint = false,
  229.     },
  230.     ["~"] = {
  231.         name = "finish",
  232.         solid = false,
  233.         id = 4,
  234.         grip = false,
  235.         col = {colors.white, colors.orange, "@"},
  236.         finish = true,
  237.         boundry = false,
  238.         checkpoint = false,
  239.     },
  240.     ["="] = {
  241.         name = "boundry",
  242.         solid = false,
  243.         id = 5,
  244.         grip = false,
  245.         col = {colors.white, colors.black, "."},
  246.         finish = false,
  247.         boundry = true,
  248.         checkpoint = false,
  249.     },
  250.     ["!"] = {
  251.         name = "checkpoint",
  252.         solid = false,
  253.         id = 6,
  254.         grip = false,
  255.         col = {colors.white, colors.orange, "!"},
  256.         finish = false,
  257.         boundry = true,
  258.         checkpoint = true,
  259.     },
  260.     [":"] = {
  261.         name = "rope",
  262.         solid = false,
  263.         id = 7,
  264.         grip = true,
  265.         col = {colors.white, colors.brown, ":"},
  266.         finish = false,
  267.         boundry = false,
  268.         checkpoint = false,
  269.     },
  270.     ["#"] = {
  271.         name = "gate",
  272.         solid = true,
  273.         id = 8,
  274.         grip = false,
  275.         col = {colors.white, colors.black, "#"},
  276.         finish = false,
  277.         boundry = false,
  278.         checkpoint = false,
  279.     },
  280. }
  281. local function createTile(type, x, y)
  282.     if data[type] ~= nil then
  283.         local dat = data[type]
  284.         local tile = {}
  285.         tile.name = dat.name
  286.         tile.solid = dat.solid
  287.         tile.id = dat.id
  288.         tile.grip = dat.grip
  289.         tile.finish = dat.finish
  290.         tile.x = x
  291.         tile.y = y
  292.         tile.col = dat.col
  293.         tile.boundry = dat.boundry
  294.         tile.checkpoint = dat.checkpoint
  295.         tile.block = false
  296.         tile.draw = function(self, ox, oy)
  297.             buffer.setCursorPos(self.x + ox, self.y + oy)
  298.             buffer.setBackgroundColor(self.col[1])
  299.             buffer.setTextColor(self.col[2])
  300.             buffer.write(self.col[3])
  301.         end
  302.         return tile
  303.     elseif type == "@" then
  304.         return 1
  305.     elseif type == ">" then
  306.         return 2
  307.     elseif type == "v" then
  308.         return 3
  309.     elseif type == "^" then
  310.         return 4
  311.     elseif type == "O" then
  312.         return 5
  313.     elseif type == "U" then
  314.         return 6
  315.     elseif type == "-" then
  316.         return 7
  317.     elseif type == "x" then
  318.         return 8
  319.     end
  320. end
  321.  
  322.  
  323. local function createActor(x, y, states, map)
  324.     local actor = {}
  325.     actor.pos = {x, y}
  326.     actor.state = 1
  327.     actor.states = states
  328.     actor.grip = 0
  329.     actor.map = map
  330.     actor.jump = 0
  331.     actor.dir = 0
  332.     actor.active = true
  333.     actor.drop = false
  334.     actor.lastPos = {x, y}
  335.     actor.god = false
  336.    
  337.     actor.draw = function(self, ox, oy)
  338.         buffer.setCursorPos(self.pos[1] + ox, self.pos[2] + oy)
  339.         local state = self.states[self.state]
  340.         buffer.setBackgroundColor(state[1])
  341.         buffer.setTextColor(state[2])
  342.         buffer.write(state[3])
  343.     end
  344.     actor.check = function(self, dx, dy, solid, bound)
  345.         local opos = {self.pos[1], self.pos[2]}
  346.         local result = self:move(dx, dy, solid, bound)
  347.         self.pos = {opos[1], opos[2]}
  348.         return result
  349.     end
  350.     actor.move = function(self, dx, dy, solid, bound)
  351.         local x = self.pos[1] + dx
  352.         local y = self.pos[2] + dy
  353.         if y <= self.map.height and y > 0 and x <= self.map.width and x > 0 then
  354.             local tile = self.map[x][y]
  355.             if ((not tile.solid and solid) or (not solid)) and ((not tile.boundry and bound) or (not bound)) then
  356.                 self.lastPos = {self.pos[1], self.pos[2]}
  357.                 self.pos = {x, y}
  358.                 return true
  359.             else
  360.                 if not self.god then
  361.                     self.jump = 0
  362.                 end
  363.             end
  364.         else
  365.             if not self.god then
  366.                 self.jump = 0
  367.             end
  368.         end
  369.         return false
  370.     end
  371.     actor.doJump = function(self, amt)
  372.         if not self:isFlying() then self.jump = amt end
  373.     end
  374.     actor.isFlying = function(self)
  375.         if not self.god then
  376.             local pos = self.pos
  377.             local flying = false
  378.             if pos[2] + 1 <= self.map.height then
  379.                 local down = self.map[pos[1]][pos[2]+1]
  380.                 if (not down.solid and not down.block) and self.grip == 0 then flying = true end
  381.             end
  382.             return flying
  383.         else
  384.             return false
  385.         end
  386.     end
  387.     actor.update = function(self)
  388.    
  389.     end
  390.     actor.gravity = function(self, canGrip, solid, bound)
  391.         local opos = {self.pos[1], self.pos[2]}
  392.         if not canGrip then self.drop = true end
  393.         local pos = self.pos
  394.         if actor.jump == 0 and not actor.god then
  395.             if pos[2] + 1 <= self.map.height then
  396.                 local down = self.map[pos[1]][pos[2]+1]
  397.                 if ((not down.solid and not down.block and solid) or (not solid)) and ((not down.boundry and bound) or (not bound)) then
  398.                     local grip = 0
  399.                     if pos[1] - 1 > 0 then
  400.                         local left = self.map[pos[1]-1][pos[2]]
  401.                         if left.grip then grip = 1 end
  402.                     end
  403.                     if grip == 0 then
  404.                         if pos[1] + 1 <= self.map.width then
  405.                             local right = self.map[pos[1]+1][pos[2]]
  406.                             if right.grip then grip = 2 end
  407.                         end
  408.                     end
  409.                     self.grip = grip
  410.                     if grip == 0 or self.drop then
  411.                         self.pos[2] = self.pos[2] + 1
  412.                     end
  413.                 end
  414.             end
  415.         elseif self.jump > 0 and not actor.god then
  416.             if pos[2] - 1 > 0 then
  417.                 local up = self.map[pos[1]][pos[2]-1]
  418.                 if ((not up.solid and not block and solid) or (not solid)) and ((not up.boundry and bound) or (not bound)) then
  419.                     self.pos[2] = self.pos[2] - 1
  420.                     self.jump = self.jump - 1
  421.                 else
  422.                     self.jump = 0
  423.                 end
  424.             end
  425.             self.grip = 0
  426.         elseif actor.god then  
  427.             if self.drop then
  428.                 self:move(0, 1, solid, bound)
  429.             elseif self.jump > 0 then
  430.                 self:move(0, -1, solid, bound)
  431.                 self.jump = 0
  432.             end
  433.         end
  434.         if self.dir ~= 0 then
  435.             if not self:move(self.dir, 0, solid, bound) then
  436.                
  437.             end
  438.             self.dir = 0
  439.             self.grip = 0
  440.         end
  441.         self.drop = false
  442.        
  443.         if opos[1] ~= pos[1] or opos[2] ~= pos[2] then
  444.             self.lastPos = opos
  445.         end
  446.     end
  447.     return actor
  448. end
  449.  
  450. local function createActorType(x, y, type, map)
  451.     local tile = nil
  452.     local actor = nil
  453.     if type == 1 then
  454.         tile = createTile(" ", x, y)
  455.         map.player = createActor(x, y, {{colors.white, colors.black, "|"}, {colors.white, colors.black, "\\"}, {colors.white, colors.black, "/"}}, map)
  456.         map.player.update = function(self)
  457.             self:gravity(true, true, false)
  458.             self.state = self.grip + 1
  459.             term.setCursorPos(1, 11)
  460.             term.setBackgroundColor(colors.black)
  461.             term.setTextColor(colors.white)
  462.             print("last pos: "..self.lastPos[1]..","..self.lastPos[2])
  463.             print("cur pos : "..self.pos[1]..","..self.pos[2])
  464.         end
  465.         map.start = {x, y}
  466.         actor = map.player
  467.     elseif type == 2 or type == 3 then
  468.         local id = type
  469.         tile = createTile(" ", x, y)
  470.         actor = createActor(x, y, {{colors.white, colors.black, "x"}, {colors.white, colors.black, "+"}}, map)
  471.         actor.update = function(self)
  472.             if self.id == 2 then
  473.                 if not self:move(self.dir, 0, false, true) then self.dir = self.dir * -1 end
  474.             elseif self.id == 3 then
  475.                 if not self:move(0, self.dir, false, true) then self.dir = self.dir * -1 end
  476.             end
  477.             if self.state == 1 then self.state = 2 else self.state = 1 end
  478.            
  479.             if self.pos[1] == self.map.player.pos[1] and self.pos[2] == self.map.player.pos[2] then
  480.                 --self.active = false
  481.                 self.map.player.active = false
  482.             end
  483.         end
  484.         actor.dir = 1
  485.         actor.id = id
  486.        
  487.     elseif type == 4 then
  488.         tile = createTile(" ", x, y)
  489.         tile.boundry = true
  490.         actor = createActor(x, y, {{colors.white, colors.red, "^"}}, map)
  491.         actor.update = function(self)
  492.             if self.pos[1] == self.map.player.pos[1] and self.pos[2] == self.map.player.pos[2] then
  493.                 self.map.player.active = false
  494.             end
  495.         end
  496.     elseif type == 5 then
  497.         tile =  createTile(" ", x, y)
  498.         actor = createActor(x, y, {{colors.white, colors.brown, "O"}}, map)
  499.         actor.update = function(self)
  500.             local opos = {self.pos[1], self.pos[2]}
  501.             self:gravity(false, true, true)
  502.             if opos[1] == self.pos[1] and opos[2] == self.pos[2] then else
  503.                 self.map[opos[1]][opos[2]].boundry = false
  504.                 self.map[opos[1]][opos[2]].block = false
  505.             end
  506.             self.map[self.pos[1]][self.pos[2]].boundry = true
  507.             self.map[self.pos[1]][self.pos[2]].block = true
  508.            
  509.             local pos = self.pos
  510.             local ppos = self.map.player.pos
  511.             if pos[1] == ppos[1] and pos[2] == ppos[2] then
  512.                 local lpos = self.map.player.lastPos
  513.                 if #lpos > 0 then
  514.                     if pos[1] - 1 == lpos[1] and pos[2] == lpos[2] then
  515.                         local opos = {self.pos[1], self.pos[2]}
  516.                         if not self:move(1, 0, true, true) then
  517.                             self.map.player.pos[1] = lpos[1]
  518.                             self.map.player.pos[2] = lpos[2]
  519.                         else
  520.                             self.map[self.pos[1]][self.pos[2]].boundry = true
  521.                             self.map[self.pos[1]][self.pos[2]].block = true
  522.                             self.map[opos[1]][opos[2]].boundry = false
  523.                             self.map[opos[1]][opos[2]].block = false
  524.                         end
  525.                     elseif pos[1] + 1 == lpos[1] and pos[2] == lpos[2] then
  526.                         local opos = {self.pos[1], self.pos[2]}
  527.                         if not self:move(-1, 0, true, true) then
  528.                             self.map.player.pos[1] = lpos[1]
  529.                             self.map.player.pos[2] = lpos[2]
  530.                         else
  531.                             self.map[self.pos[1]][self.pos[2]].boundry = true
  532.                             self.map[self.pos[1]][self.pos[2]].block = true
  533.                             self.map[opos[1]][opos[2]].boundry = false
  534.                             self.map[opos[1]][opos[2]].block = false
  535.                         end
  536.                     else
  537.                         self.map.player.pos[1] = lpos[1]
  538.                         self.map.player.pos[2] = lpos[2]
  539.                     end
  540.                 end
  541.             end
  542.         end
  543.     elseif type == 6 then
  544.         tile = createTile(" ", x, y)
  545.         actor = createActor(x, y, {{colors.white, colors.gray, "U"}, {colors.white, colors.lightGray, "$"}}, map)
  546.         actor.update = function(self)
  547.             local pos = {self.pos[1], self.pos[2] + 1}
  548.             if pos[2] <= self.map.height then
  549.                 local tile = self.map[pos[1]][pos[2]]
  550.                 while tile ~= nil do
  551.                     if tile.block == false and tile.solid == false then
  552.                         if self.map.player.pos[1] == tile.x and self.map.player.pos[2] == tile.y then
  553.                             self.map.player.active = false
  554.                         end
  555.                         pos[2] = pos[2] + 1
  556.                         if pos[2] <= self.map.height then
  557.                             tile = self.map[pos[1]][pos[2]]
  558.                         else
  559.                             tile = nil
  560.                         end
  561.                     else
  562.                         tile = nil
  563.                     end
  564.                 end
  565.             end
  566.         end
  567.         actor.draw = function(self, ox, oy)
  568.             buffer.setCursorPos(self.pos[1] + ox, self.pos[2] + oy)
  569.             local state = self.states[self.state]
  570.             buffer.setBackgroundColor(state[1])
  571.             buffer.setTextColor(state[2])
  572.             buffer.write(state[3])
  573.            
  574.             local pos = {self.pos[1], self.pos[2] + 1}
  575.             if pos[2] <= self.map.height then
  576.                 local tile = self.map[pos[1]][pos[2]]
  577.                 while tile ~= nil do
  578.                     if tile.block == false and tile.solid == false then
  579.                         buffer.setCursorPos(pos[1] + ox, pos[2] + oy)
  580.                         local state = self.states[2]
  581.                         buffer.setBackgroundColor(state[1])
  582.                         buffer.setTextColor(state[2])
  583.                         buffer.write(state[3])
  584.                         pos[2] = pos[2] + 1
  585.                         if pos[2] <= self.map.height then
  586.                             tile = self.map[pos[1]][pos[2]]
  587.                         else
  588.                             tile = nil
  589.                         end
  590.                     else
  591.                         tile = nil
  592.                     end
  593.                 end
  594.             end
  595.         end
  596.     elseif type == 7 then
  597.         tile = createTile(" ", x, y)
  598.         actor = createActor(x, y, {{colors.white, colors.black, "-"}}, map)
  599.         actor.update = function(self)
  600.             local pos = {self.pos[1], self.pos[2]-1}
  601.             if pos[1] == self.map.player.pos[1] and pos[2] == self.map.player.pos[2] then
  602.                 self.map.player:move(self.dir, 0, false, true)
  603.             end
  604.             local opos = {self.pos[1], self.pos[2]}
  605.             if not self:move(self.dir, 0, false, true) then self.dir = self.dir * -1
  606.             else
  607.                 if self.pos[1] == self.map.player.pos[1] and self.pos[2] == self.map.player.pos[2] then
  608.                     self.pos = {opos[1], opos[2]}
  609.                     self.dir = self.dir * -1
  610.                 else
  611.                     self.map[opos[1]][opos[2]].solid = false
  612.                     self.map[opos[1]][opos[2]].boundry = false
  613.                 end
  614.             end
  615.            
  616.             self.map[self.pos[1]][self.pos[2]].solid = true
  617.             self.map[self.pos[1]][self.pos[2]].boundry = true
  618.         end
  619.         actor.dir = 1
  620.     elseif type == 8 then
  621.         tile = createTile(" ", x, y)
  622.         actor = createActor(x, y, {{colors.white, colors.black, "x"}, {colors.white, colors.black, "+"}}, map)
  623.         actor.update = function(self)
  624.             local dir = self.dirs[self.curdir]
  625.             local dirx = dir[1]
  626.             local diry = dir[2]
  627.             if not self:move(dirx, diry, false, true) then
  628.                 local ndir = self.curdir + 1
  629.                 if ndir > #self.dirs then ndir = 1 end
  630.                 if not self:check(self.dirs[ndir][1], self.dirs[ndir][2], false, true) then
  631.                     ndir = self.curdir - 1
  632.                     if ndir < 1 then ndir = #self.dirs end
  633.                 end
  634.                 self.curdir = ndir
  635.             end
  636.             if self.state == 1 then self.state = 2 else self.state = 1 end
  637.            
  638.             if self.pos[1] == self.map.player.pos[1] and self.pos[2] == self.map.player.pos[2] then
  639.                 --self.active = false
  640.                 self.map.player.active = false
  641.             end
  642.         end
  643.         actor.dirs = {{1,0},{0,1},{-1,0},{0,-1}}
  644.         actor.curdir = 1
  645.     end
  646.     return actor, tile
  647. end
  648.  
  649. local function loadMap(maptab)
  650.     local map = {}
  651.     map.width = 0
  652.     map.height = #maptab
  653.     map.player = nil
  654.     map.actors = {}
  655.     map.start = {1,1}
  656.     map.chars = " +|~>v^U=#O-x"
  657.     map.curChar = 1
  658.     for y=1,#maptab do
  659.         local line = maptab[y]
  660.         if map.width == 0 then map.width = string.len(line) end
  661.         for x=1,string.len(line) do
  662.             if map[x] == nil then map[x] = {} end
  663.             local tile = createTile(line:sub(x, x), x, y)
  664.             if type(tile) == "number" then
  665.                 local actor, tle = createActorType(x, y, tile, map)
  666.                 if actor ~= nil then
  667.                     map.actors[#map.actors + 1] = actor
  668.                 end
  669.                 if tle ~= nil then
  670.                     tile = tle
  671.                 else
  672.                     tile = createTile(" ", x, y)
  673.                 end
  674.             end
  675.             map[x][y] = tile
  676.         end
  677.     end
  678.    
  679.     map.offset = {0,0}
  680.     map.updateOffset = function(self)
  681.         if self.width > sw or self.height > sh then
  682.  
  683.             local offset = {self.player.pos[1] - math.floor(sw/2), self.player.pos[2] - math.floor(sh/2)}
  684.             if offset[1] < 0 then offset[1] = 0 end
  685.             if offset[2] < 0 then offset[2] = 0 end
  686.             if self.width > sw and offset[1] > self.width-sw then
  687.                 offset[1] = self.width-sw
  688.             end
  689.             if self.height > sh and offset[1] > self.height-sh then
  690.                 offset[2] = self.height-sh
  691.             end
  692.             self.offset = offset
  693.         else
  694.            
  695.         end
  696.     end
  697.     map.update = function(self)
  698.         self.player:update()
  699.         local nactors = {}
  700.         for i=1,#self.actors do
  701.             local actor = self.actors[i]
  702.             if actor.active then
  703.                 if actor ~= self.player then actor:update() end
  704.                 nactors[#nactors + 1] = actor
  705.             end
  706.         end
  707.         self.actors = nactors
  708.         local tile = self[self.player.pos[1]][self.player.pos[2]]
  709.         if tile.checkpoint then self.start = {self.player.pos[1],self.player.pos[2]} end
  710.         self:updateOffset()
  711.         return self.player.active
  712.     end
  713.     map.drawTiles = function(self)
  714.         --print("DRAWING TILES... "..self.width..","..self.height)
  715.         local width = self.width
  716.         local height = self.height
  717.         if width > sw then width = sw end
  718.         if height > sh then height = sh end
  719.        
  720.         for x=1+map.offset[1],width+map.offset[1] do
  721.             --print("LOOPED X")
  722.             for y=1+map.offset[2],height+map.offset[2] do
  723.                 local tile = self[x][y]
  724.                 tile:draw(-map.offset[1], -map.offset[2])
  725.                 --print("DREW TILE @ "..x..","..y)
  726.             end
  727.         end
  728.     end
  729.     map.drawActors = function(self)
  730.         for i=1,#self.actors do
  731.             local actor = self.actors[i]
  732.             if actor.pos[1] > map.offset[1] and actor.pos[2] > map.offset[2] and actor.pos[1] < map.offset[1] + sw and actor.pos[2] < map.offset[2] + sh then
  733.                 actor:draw(-map.offset[1], -map.offset[2])
  734.             end
  735.         end
  736.         self.player:draw(-map.offset[1], -map.offset[2])
  737.     end
  738.     map.draw = function(self)
  739.         self:drawTiles()
  740.         self:drawActors()
  741.         if self.player.god then
  742.             buffer.setCursorPos(1,1)
  743.             buffer.setBackgroundColor(colors.black)
  744.             buffer.setTextColor(colors.white)
  745.             buffer.write(self.chars:sub(self.curChar, self.curChar))
  746.         end
  747.     end
  748.     map.checkFinish = function(self)
  749.         local tile = self[self.player.pos[1]][self.player.pos[2]]
  750.         return tile.finish
  751.     end
  752.     map.place = function(self, x, y)
  753.         local char = self.chars:sub(self.curChar, self.curChar)
  754.         if map[x] ~= nil then
  755.             if map[x][y] ~= nil then
  756.                 local tile = createTile(char, x, y)
  757.                 if type(tile) == "number" then
  758.                     local actor, tle = createActorType(x, y, tile, map)
  759.                     if actor ~= nil then
  760.                         map.actors[#map.actors + 1] = actor
  761.                     end
  762.                     if tle ~= nil then
  763.                         tile = tle
  764.                     else
  765.                         tile = createTile(" ", x, y)
  766.                     end
  767.                 end
  768.                 map[x][y] = tile
  769.             end
  770.         end
  771.     end
  772.     return map
  773. end
  774.  
  775. local maps =
  776. {
  777.  
  778.     {"+++++++++++++++",
  779.      "+      ~      +",
  780.      "+^    |+|    ^+",
  781.      "+++|       |+++",
  782.      "+     x       +",
  783.      "+   ++ : ++   +",
  784.      "+   O  :  !   +",
  785.      "| =+++ : +++  |",
  786.      "|      @  =   |",
  787.      "+++++++++++++++"},
  788.      
  789.     {"+++++++++++++++",
  790.      "+     =       +",
  791.      "+>     =      +",
  792.      "+| | + | + + ++",
  793.      "+      |  >   +",
  794.      "+| | + ++ + + +",
  795.      "+      +      +",
  796.      "++  |+v+++++  +",
  797.      "+@     +~     +",
  798.      "+++++++++++++++"},
  799.  
  800.  }
  801.  
  802.  local function getLines(dir)
  803.     local lines = {}
  804.     if fs.exists(dir) then
  805.         local file = fs.open(dir, "r")
  806.         local line = file.readLine()
  807.         while line ~= nil do
  808.             lines[#lines + 1] = line
  809.             line = file.readLine()
  810.         end
  811.         file.close()
  812.     end
  813.     return lines
  814.  end
  815.  
  816.  local function loadMaps(dir)
  817.     local lines = getLines(dir)
  818.     if #lines > 0 then
  819.         local maps = {}
  820.         local curmap = {}
  821.         for i=1,#lines do
  822.             local line = lines[i]
  823.             if line == "###" then
  824.                 if #curmap > 0 then
  825.                     maps[#maps + 1] = curmap
  826.                     curmap = {}
  827.                 end
  828.             elseif line ~= "" then
  829.                 curmap[#curmap + 1] = line
  830.             end
  831.         end
  832.         return maps
  833.     else
  834.         return nil
  835.     end
  836.  end
  837.  
  838.  local stuff = {...}
  839.  if #stuff > 0 then
  840.     local dir = table.concat(stuff, " ")
  841.     local mps = loadMaps(dir)
  842.     if mps ~= nil then maps = mps end
  843.  end
  844.  
  845.  local title =
  846.  {  "    )  (        )                (      ____ ",
  847.     " ( /(  )\\ )  ( /(         (      )\\ )  |   / ",
  848.     " )\\())(()/(  )\\())   (    )\\    (()/(  |  /  ",
  849.     "((_)\\  /(_))((_)\\    )\\((((_)(   /(_)) | /   ",
  850.     " _((_)(_))   _((_)  ((_))\\ _ )\\ (_))   |/    ",
  851.     "| \\| ||_ _| | \\| | _ | |(_)_\\(_)/ __| (      ",
  852.     "| .` | | |  | .` || || | / _ \\  \\__ \\ )\\     ",
  853.     "|_|\\_||___| |_|\\_| \\__/ /_/ \\_\\ |___/((_)   "}
  854.    
  855. local start =
  856.             {"    _             _     ",
  857.             " ___| |_ __ _ _ __| |_    ",
  858.             "/ __| __/ _` | '__| __|   ",
  859.             "\\__ \\ || (_| | |  | |_    ",
  860.             "|___/\\__\\__,_|_|   \\__|   "}
  861.  
  862. local load =
  863.             {"_                 _   ",
  864.             "| | ___   __ _  __| |   ",
  865.             "| |/ _ \\ / _` |/ _` |   ",
  866.             "| | (_) | (_| | (_| |   ",
  867.             "|_|\\___/ \\__,_|\\__,_|   "}
  868.  
  869. local function centrePrint(str, y)
  870.     local midx = math.floor(sw/2)
  871.     local x = midx - math.floor(string.len(str)/2)
  872.     term.setCursorPos(x, y)
  873.     term.write(str)
  874. end
  875.  
  876. local function getMaps()
  877.     local mps = {}
  878.     local list = fs.list("")
  879.     for i=1,#list do
  880.         local dir = list[i]
  881.         if dir:sub(-4, -1) == ".nmp" then mps[#mps + 1] = dir end
  882.     end
  883.     return mps
  884. end
  885.  
  886.  local function titleScreen()
  887.     term.setBackgroundColor(colors.black)
  888.     term.setTextColor(colors.red)
  889.     term.clear()
  890.     term.setCursorPos(1,1)
  891.     for i=1,#title do
  892.         centrePrint(title[i], i)
  893.     end
  894.     term.setTextColor(colors.orange)
  895.     for i=1,#start do
  896.         centrePrint(start[i], #title + i )
  897.     end
  898.     for i=1,#load do
  899.         centrePrint(load[i], #title + #start + i )
  900.     end
  901.    
  902.    
  903.     local maps = {}
  904.     local pages = 1
  905.     local cur = 1
  906.     local state = 1
  907.     local tsOn = true
  908.     while tsOn do
  909.         local event, p1, p2, p3 = os.pullEvent()
  910.         if event == "key" then
  911.             local key = p1
  912.             if key == 29 then
  913.                 tsOn = false
  914.                 return nil
  915.             elseif state == 2 then
  916.                 if key == 203 then --left
  917.                     if cur > 1 then cur = cur - 1 end
  918.                 elseif key == 205 then --right
  919.                     if cur < pages then cur = cur + 1 end
  920.                 elseif key >= 2 and key <= 6 then
  921.                     local dir = maps[(5*(cur-1))+(key-1)]
  922.                     if dir ~= nil then
  923.                         tsOn = false
  924.                         return dir
  925.                     end
  926.                 end
  927.                
  928.                 for i=((cur-1)*5) + 1, ((cur-1)*5) + 6 do
  929.                     local j = i - ((cur-1)*5)
  930.                     if maps[i] ~= nil then
  931.                         centrePrint("                                            ", #title + #load + j + 1)
  932.                         centrePrint(j..": "..maps[i], #title + #load + j + 1)
  933.                     end
  934.                 end
  935.             end
  936.         elseif event == "mouse_click" then
  937.             if state == 1 then
  938.                 local y = p3
  939.                 if y > #title and y <= #title + #start then
  940.                     tsOn = false
  941.                     return true
  942.                 elseif y > #title + #start and y <= #title + #start + #load then
  943.                     term.clear()
  944.                     term.setTextColor(colors.red)
  945.                     term.setCursorPos(1,1)
  946.                     for i=1,#title do
  947.                         centrePrint(title[i], i)
  948.                     end
  949.                     term.setTextColor(colors.orange)
  950.                     for i=1,#load do
  951.                         centrePrint(load[i], #title + i)
  952.                     end
  953.                    
  954.                     maps = getMaps()
  955.                     if #maps > 5 then pages = math.floor(#maps/5) end
  956.                     for i=((cur-1)*5) + 1, ((cur-1)*5) + 6 do
  957.                         local j = i - ((cur-1)*5)
  958.                         if maps[i] ~= nil then
  959.                             centrePrint("                                            ", #title + #load + j + 1)
  960.                             centrePrint(j..": "..maps[i], #title + #load + j + 1)
  961.                         end
  962.                     end
  963.                     state = 2
  964.                 end
  965.             end
  966.         end
  967.     end
  968.     return nil
  969.  end
  970.  
  971.  local win =
  972.  {"     )      )                           )       )  ",
  973.   "  ( /(   ( /(              (  (      ( /(    ( /(  ",
  974.   "  )\\())  )\\())      (      )\\))(   ' )\\())   )\\()) ",
  975.   " ((_)\\  ((_)\\       )\\    ((_)()\\ ) ((_)\\   ((_)\\  ",
  976.   "__ ((_)   ((_)   _ ((_)   _(())\\_)()  ((_)   _((_) ",
  977.   "\\ \\ / /  / _ \\  | | | |   \\ \\((_)/ / / _ \\  | \\| | ",
  978.   " \\ V /  | (_) | | |_| |    \\ \\/\\/ / | (_) | | .' | ",
  979.   "  |_|    \\___/   \\___/      \\_/\\_/   \\___/  |_|\\_| "}
  980.  
  981.  
  982.  local result = titleScreen()
  983.  if type(result) == "string" then
  984.     maps = loadMaps(result)
  985.     result = true
  986. end
  987.  
  988.  if result then
  989.      local curmap = 1
  990.      local map = loadMap(maps[curmap])
  991.      buffer.setBackgroundColor(colors.black)
  992.      buffer.clear()
  993.      buffer.setCursorPos(1,1)
  994.      map:draw()
  995.      local on = true
  996.      local pause = 0.1
  997.      local timer = os.startTimer(pause)
  998.      while on do
  999.         local event, p1, p2, p3 = os.pullEvent()
  1000.         if event == "timer" and p1 == timer then
  1001.             if not map:update() then
  1002.                 if not map.player.god then map.player.pos = {map.start[1], map.start[2]} end
  1003.                 map.player.active = true
  1004.             end
  1005.             --print("DRAWING..")
  1006.             map:draw()
  1007.             if map:checkFinish() then
  1008.                 curmap = curmap + 1
  1009.                 if curmap > #maps then
  1010.                     buffer.setBackgroundColor(colors.black)
  1011.                     buffer.setTextColor(colors.red)
  1012.                     buffer.clear()
  1013.                     buffer.setCursorPos(1,1)
  1014.                     for i=1,#win do
  1015.                         local line = win[i]
  1016.                         print(line)
  1017.                         sleep(0.2)
  1018.                     end
  1019.                     for i=1,3 do
  1020.                         buffer.clear()
  1021.                         buffer.setCursorPos(1,1)
  1022.                         sleep(0.25)
  1023.                         for i=1,#win do
  1024.                             local line = win[i]
  1025.                             print(line)
  1026.                         end
  1027.                         sleep(0.35)
  1028.                     end
  1029.                     on = false
  1030.                 else
  1031.                     map = loadMap(maps[curmap])
  1032.                 end
  1033.             end
  1034.             buffer:render()
  1035.             timer = os.startTimer(pause)
  1036.         elseif event == "mouse_scroll" then
  1037.             local dir = p1
  1038.             if map.player.god then
  1039.                 map.curChar = map.curChar + dir
  1040.                 if map.curChar > string.len(map.chars) then map.curChar = 1 end
  1041.                 if map.curChar < 1 then map.curChar = string.len(map.chars) end
  1042.             end
  1043.         elseif event == "key" then
  1044.             local key = p1
  1045.             if key == 17 then --w
  1046.                 map.player:doJump(2)
  1047.             elseif key == 30 then --a
  1048.                 map.player.dir = -1
  1049.             elseif key == 31 then --s
  1050.                 map.player.jump = 0
  1051.                 map.player.drop = true
  1052.             elseif key == 32 then --d
  1053.                 map.player.dir = 1
  1054.             elseif key == 57 then --space
  1055.                 map.player:doJump(2)
  1056.             elseif key == 19 then --r
  1057.                 map = loadMap(maps[curmap])
  1058.             elseif key == 29 then --ctrl
  1059.                 on = false
  1060.             elseif key == 41 then
  1061.                 map.player.god = (map.player.god == false)
  1062.             elseif key == 200 then --up
  1063.                 if not map.player.god then map.player:doJump(2) else
  1064.                     map:place(map.player.pos[1], map.player.pos[2] - 1)
  1065.                 end
  1066.             elseif key == 203 then --left
  1067.                 if not map.player.god then map.player.dir = -1 else
  1068.                     map:place(map.player.pos[1] - 1, map.player.pos[2])
  1069.                 end
  1070.             elseif key == 208 then --down
  1071.                 if not map.player.god then
  1072.                     map.player.jump = 0
  1073.                     map.player.drop = true
  1074.                 else
  1075.                     map:place(map.player.pos[1], map.player.pos[2] + 1)
  1076.                 end
  1077.             elseif key == 205 then --right
  1078.                 if not map.player.god then map.player.dir = 1 else
  1079.                     map:place(map.player.pos[1] + 1, map.player.pos[2])
  1080.                 end
  1081.             elseif key == 79 then
  1082.                 local dir = 1
  1083.                 if map.player.god then
  1084.                     map.curChar = map.curChar + dir
  1085.                     if map.curChar > string.len(map.chars) then map.curChar = 1 end
  1086.                     if map.curChar < 1 then map.curChar = string.len(map.chars) end
  1087.                 end
  1088.             elseif key == 82 then
  1089.                 local dir = -1
  1090.                 if map.player.god then
  1091.                     map.curChar = map.curChar + dir
  1092.                     if map.curChar > string.len(map.chars) then map.curChar = 1 end
  1093.                     if map.curChar < 1 then map.curChar = string.len(map.chars) end
  1094.                 end
  1095.             end
  1096.         end
  1097.      end
  1098.  end
  1099.  
  1100.  --buffer = buffer.buffer
Advertisement
Add Comment
Please, Sign In to add comment