Advertisement
Guest User

pacman

a guest
Mar 29th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.06 KB | None | 0 0
  1. local function compare(table1, table2, icap)
  2.     local ok = true
  3.     for j=1,#table1 do
  4.         local yes = true
  5.         local table = table1[j]
  6.         for i=1,icap do
  7.             if table[i] ~= table2[i] then yes = false end
  8.         end
  9.         if yes then ok = false end
  10.     end
  11.     return ok
  12. end
  13.  
  14. local function findPath(s, e, map)
  15.     local done = {}
  16.     local todo = {{s[1], s[2], nil}}
  17.     local result = nil
  18.     local ok = true
  19.     local n = 0
  20.     while ok do
  21.         local temp = {}
  22.         if #todo > 0 then
  23.             local p = 1
  24.             for i=1,#todo do
  25.                 local cur = todo[i]
  26.                 local new = { {cur[1]+1, cur[2]}, {cur[1]-1, cur[2]}, {cur[1], cur[2]+1}, {cur[1], cur[2]-1} }
  27.                 for i=1,#new do
  28.                     local pos = new[i]
  29.                     pos[3] = cur[3]
  30.                     if compare(done, pos, 2) then
  31.                         local typ = map:getBlock(pos[1], pos[2])
  32.                         if typ == 0 then
  33.                             temp[#temp + 1] = {pos[1], pos[2], {cur[1], cur[2], cur[3]}}
  34.                         end
  35.                     end
  36.                 end
  37.                 local newDone = {cur[1], cur[2], cur[3]}
  38.                 done[#done + 1] = newDone
  39.                 if cur[1] == e[1] and cur[2] == e[2] then
  40.                     result = newDone
  41.                     ok = false
  42.                 end
  43.                 p = p + 1
  44.                 if p > 50 then
  45.                     sleep(0)
  46.                 end
  47.             end
  48.             todo = {}
  49.             for i=1,#temp do todo[#todo + 1] = temp[i] end
  50.         else
  51.             ok = false
  52.         end
  53.         if n > 20 then
  54.             sleep(0)
  55.         end
  56.         term.setCursorPos(1,1)
  57.     end
  58.    
  59.     local path = {}
  60.     if result ~= nil then
  61.         local cur = {result[1], result[2], result[3]}
  62.         while cur ~= nil do
  63.             path[#path + 1] = {cur[1], cur[2]}
  64.             cur = cur[3]
  65.         end
  66.     end
  67.     return path
  68. end
  69.  
  70. local function flipTable(table)
  71.     local ntable = {}
  72.     for i=1,#table do
  73.         local index = (#table - i) + 1
  74.         ntable[i] = table[index]
  75.     end
  76.     return ntable
  77. end
  78.  
  79. local function createMap(lines, mapping)
  80.     local info = {}
  81.     local map = {
  82.         getBlock = function(self, x, y)
  83.             local xtab = self[y]
  84.             if xtab ~= nil then
  85.                 local n = xtab[x]
  86.                 if n == nil then n = 1 end
  87.                 return n
  88.             end
  89.             return 1
  90.         end,
  91.         draw = function(self, ox, oy, mapping, scale)
  92.             for y=1,#self do
  93.                 local xtab = self[y]
  94.                 if xtab ~= nil then
  95.                     for x=1,#xtab do
  96.                         local n = xtab[x]
  97.                         local data = mapping[tostring(n)]
  98.                         local o = 1
  99.                         if scale == 1 then o = 0 end
  100.                         local nx = (x * scale) - o
  101.                         local ny = (y * scale) - o
  102.                         for wx = 0,(scale-1) do
  103.                             for wy = 0,(scale-1) do
  104.                                 term.setCursorPos(nx+wx+ox,ny+wy+oy)
  105.                                 if data ~= nil then
  106.                                     term.setBackgroundColor(data[1])
  107.                                     term.setTextColor(data[2])
  108.                                     term.write(data[3])
  109.                                 end
  110.                             end
  111.                         end
  112.                     end
  113.                 end
  114.             end
  115.         end,
  116.     }
  117.     for y=1,#lines do
  118.         local xtab = {}
  119.         local line = lines[y]
  120.         for x=1,string.len(line) do
  121.             local ch = string.sub(line, x, x)
  122.             local n = tonumber(ch)
  123.             for i=1,#mapping do
  124.                 local mapp = mapping[i]
  125.                 if mapp[2] == n then
  126.                     n = mapp[3]
  127.                     info[mapp[1]] = {x, y}
  128.                 end
  129.             end
  130.             xtab[x] = n
  131.         end
  132.         map[y] = xtab
  133.     end
  134.    
  135.     return map, info
  136. end
  137.  
  138. local function createPlayer(sx, sy)
  139.     local player = {
  140.         x = sx,
  141.         y = sy,
  142.         dir = {0,0},
  143.         draw = function(self, scale)
  144.             local o = 1
  145.             if scale == 1 then o = 0 end
  146.             local x = (self.x * scale) - o
  147.             local y = (self.y * scale) - o
  148.             for w = 0,scale-1 do
  149.                 for h = 0,scale-1 do
  150.                     term.setCursorPos(x+w, y+h)
  151.                     term.setBackgroundColor(colors.white)
  152.                     term.setTextColor(colors.red)
  153.                     term.write("@")
  154.                 end
  155.             end
  156.         end,
  157.         checkForGhost = function(self, gs)
  158.             for i=1,#gs do
  159.                 local ghost = gs[i]
  160.                 if ghost.x == self.x and ghost.y == self.y then
  161.                     return true
  162.                 end
  163.             end
  164.             return false
  165.         end,
  166.         update = function(self, map, gs)
  167.             local nx = self.x + self.dir[1]
  168.             local ny = self.y + self.dir[2]
  169.             local b = map:getBlock(nx, ny)
  170.             if b == 0 then
  171.                 self.x = nx
  172.                 self.y = ny
  173.             end
  174.             return self:checkForGhost(gs)
  175.         end,
  176.     }
  177.     return player
  178. end
  179. local map, info = createMap({
  180. "111111111",
  181. "100000001",
  182. "101101101",
  183. "101000101",
  184. "100010001",
  185. "111030111",
  186. "100010001",
  187. "101000101",
  188. "101101101",
  189. "100020001",
  190. "111111111"
  191. },
  192. {{"player", 2, 0}, {"ghost", 3, 0}}
  193. )
  194. local cmaps = {
  195.     ["0"] = {colors.white, colors.black, " "},
  196.     ["1"] = {colors.black, colors.white, "+"}
  197. }
  198.  
  199.  
  200. local player = createPlayer(info.player[1], info.player[2])
  201.  
  202. local function createGhost(sx, sy, smap, sid)
  203.     local gpath = flipTable(findPath({sx, sy}, {player.x, player.y}, smap))
  204.     local ghost = {
  205.         x = sx,
  206.         y = sy,
  207.         id = sid,
  208.         map = smap,
  209.         path = gpath,
  210.         cur = 1,
  211.         getPath = function(self, play)
  212.             local gpath = flipTable(findPath({self.x, self.y}, {play.x, play.y}, self.map))
  213.             if #gpath == 0 or math.random(100) <= 25 then
  214.                 local rloc = nil
  215.                 local sov = 0
  216.                 while rloc == nil and sov < 100 do
  217.                     local cloc = {math.random(#self.map[1]), math.random(#self.map)}
  218.                     if self.map:getBlock(cloc[1], cloc[2], self.map) == 0 then rloc = cloc end
  219.                     sov = sov + 1
  220.                 end
  221.                 if rloc == nil then rloc = {2,2} end
  222.                 gpath = flipTable(findPath({self.x, self.y}, {play.x, play.y}, self.map))
  223.             end
  224.             return gpath
  225.         end,
  226.         checkForGhosts = function(self, gs)
  227.             for i=1,#gs do
  228.                 local ghost = gs[i]
  229.                 if ghost.x == self.x and ghost.y == self.y and self.id ~= ghost.id then
  230.                     return true
  231.                 end
  232.             end
  233.             return false
  234.         end,
  235.         checkForIntersect = function(self)
  236.             local n = 0
  237.             local new = { {self.x+1, self.y}, {self.x-1, self.y}, {self.x, self.y+1}, {self.x, self.y-1} }
  238.             for i=1,#new do
  239.                 local n = self.map:getBlock(new[i][1], new[i][2])
  240.                 if n == 0 then n = n + 1 end
  241.             end
  242.             return (n > 2)
  243.         end,
  244.         update = function(self, gs, play)
  245.            
  246.             if self.cur > #self.path then
  247.                 self.path = self:getPath(play)
  248.                 self.cur = 1
  249.             end
  250.             if self.cur <= #self.path then
  251.                 local nloc = self.path[self.cur]
  252.                 local oloc = {self.x, self.y}
  253.                 self.x = nloc[1]
  254.                 self.y = nloc[2]
  255.                 if self:checkForGhosts(self, gs) then
  256.                     self.x = oloc[1]
  257.                     self.y = oloc[2]
  258.                 elseif self.x == play.x and self.y == play.y then return true
  259.                 elseif self:checkForIntersect() and math.random(10) < 5 then
  260.                     self.path = self:getPath(play)
  261.                 else
  262.                     self.cur = self.cur + 1
  263.                 end
  264.             else
  265.                 self.cur = self.cur + 1
  266.             end
  267.             return false
  268.         end,
  269.         draw = function(self, scale)
  270.             local o = 1
  271.             if scale == 1 then o = 0 end
  272.             local x = (self.x * scale) - o
  273.             local y = (self.y * scale) - o
  274.             for w = 0,scale-1 do
  275.                 for h = 0,scale-1 do
  276.                     term.setCursorPos(x+w, y+h)
  277.                     term.setBackgroundColor(colors.white)
  278.                     term.setTextColor(colors.red)
  279.                     term.write("O")
  280.                 end
  281.             end
  282.         end,   
  283.     }
  284.     return ghost
  285. end
  286.  
  287. local ghosts = {
  288.     createGhost(info.ghost[1], info.ghost[2], map, 1)
  289. }
  290.  
  291. local on = true
  292. local interval = 0.2
  293.  
  294. local scale = 2
  295.  
  296. local stuff = {...}
  297. if #stuff == 1 then
  298.     scale = tonumber(stuff[1])
  299. elseif #stuff == 2 then
  300.     scale = tonumber(stuff[1])
  301.     interval = tonumber(stuff[2])
  302. end
  303.  
  304. local tick = 0
  305.  
  306. local timer = os.startTimer(0)
  307. local score = 0
  308. while on do
  309.     local event,p1 = os.pullEvent()
  310.     if event == "timer" and p1 == timer then
  311.         term.setBackgroundColor(colors.black)
  312.         term.setTextColor(colors.white)
  313.         term.clear()
  314.         term.setCursorPos(1,1)
  315.         map:draw(0, 0, cmaps, scale)
  316.         if player:update(map, ghosts) then
  317.             on = false
  318.         end
  319.         for i=1,#ghosts do
  320.             if ghosts[i]:update(ghosts, player) then on = false end
  321.             ghosts[i]:draw(scale)
  322.         end
  323.         player:draw(scale)
  324.         if #ghosts < 4 then
  325.             tick = tick + 1
  326.             if tick > 50 then
  327.                 ghosts[#ghosts + 1] = createGhost(info.ghost[1], info.ghost[2], map, #ghosts+1)
  328.                 tick = 0
  329.             end
  330.         end
  331.         score = score + 1
  332.         timer = os.startTimer(interval)
  333.     elseif event == "key" then
  334.         local key = p1
  335.         if key == 200 then -- up
  336.             player.dir = {0, -1}
  337.         elseif key == 203 then -- left
  338.             player.dir = {-1, 0}
  339.         elseif key == 208 then -- down
  340.             player.dir = {0, 1}
  341.         elseif key == 205 then -- right
  342.             player.dir = {1, 0}
  343.         elseif key == 29 then
  344.             on = false
  345.         end
  346.     end
  347. end
  348. term.setBackgroundColor(colors.black)
  349. term.setTextColor(colors.white)
  350. term.setCursorPos(1, (#map*scale) + 2)
  351. print("!!! !!! GAME OVER !!! !!!")
  352. print("!!! SCORE: "..score)
  353. print("!!! !!! !!!! !!!! !!! !!!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement