Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function compare(table1, table2, icap)
- local ok = true
- for j=1,#table1 do
- local yes = true
- local table = table1[j]
- for i=1,icap do
- if table[i] ~= table2[i] then yes = false end
- end
- if yes then ok = false end
- end
- return ok
- end
- local function findPath(s, e, map)
- local done = {}
- local todo = {{s[1], s[2], nil}}
- local result = nil
- local ok = true
- local n = 0
- while ok do
- local temp = {}
- if #todo > 0 then
- local p = 1
- for i=1,#todo do
- local cur = todo[i]
- local new = { {cur[1]+1, cur[2]}, {cur[1]-1, cur[2]}, {cur[1], cur[2]+1}, {cur[1], cur[2]-1} }
- for i=1,#new do
- local pos = new[i]
- pos[3] = cur[3]
- if compare(done, pos, 2) then
- local typ = map:getBlock(pos[1], pos[2])
- if typ == 0 then
- temp[#temp + 1] = {pos[1], pos[2], {cur[1], cur[2], cur[3]}}
- end
- end
- end
- local newDone = {cur[1], cur[2], cur[3]}
- done[#done + 1] = newDone
- if cur[1] == e[1] and cur[2] == e[2] then
- result = newDone
- ok = false
- end
- p = p + 1
- if p > 50 then
- sleep(0)
- end
- end
- todo = {}
- for i=1,#temp do todo[#todo + 1] = temp[i] end
- else
- ok = false
- end
- if n > 20 then
- sleep(0)
- end
- term.setCursorPos(1,1)
- end
- local path = {}
- if result ~= nil then
- local cur = {result[1], result[2], result[3]}
- while cur ~= nil do
- path[#path + 1] = {cur[1], cur[2]}
- cur = cur[3]
- end
- end
- return path
- end
- local function flipTable(table)
- local ntable = {}
- for i=1,#table do
- local index = (#table - i) + 1
- ntable[i] = table[index]
- end
- return ntable
- end
- local function createMap(lines, mapping)
- local info = {}
- local map = {
- getBlock = function(self, x, y)
- local xtab = self[y]
- if xtab ~= nil then
- local n = xtab[x]
- if n == nil then n = 1 end
- return n
- end
- return 1
- end,
- draw = function(self, ox, oy, mapping, scale)
- for y=1,#self do
- local xtab = self[y]
- if xtab ~= nil then
- for x=1,#xtab do
- local n = xtab[x]
- local data = mapping[tostring(n)]
- local o = 1
- if scale == 1 then o = 0 end
- local nx = (x * scale) - o
- local ny = (y * scale) - o
- for wx = 0,(scale-1) do
- for wy = 0,(scale-1) do
- term.setCursorPos(nx+wx+ox,ny+wy+oy)
- if data ~= nil then
- term.setBackgroundColor(data[1])
- term.setTextColor(data[2])
- term.write(data[3])
- end
- end
- end
- end
- end
- end
- end,
- }
- for y=1,#lines do
- local xtab = {}
- local line = lines[y]
- for x=1,string.len(line) do
- local ch = string.sub(line, x, x)
- local n = tonumber(ch)
- for i=1,#mapping do
- local mapp = mapping[i]
- if mapp[2] == n then
- n = mapp[3]
- info[mapp[1]] = {x, y}
- end
- end
- xtab[x] = n
- end
- map[y] = xtab
- end
- return map, info
- end
- local function createPlayer(sx, sy)
- local player = {
- x = sx,
- y = sy,
- dir = {0,0},
- draw = function(self, scale)
- local o = 1
- if scale == 1 then o = 0 end
- local x = (self.x * scale) - o
- local y = (self.y * scale) - o
- for w = 0,scale-1 do
- for h = 0,scale-1 do
- term.setCursorPos(x+w, y+h)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.red)
- term.write("@")
- end
- end
- end,
- checkForGhost = function(self, gs)
- for i=1,#gs do
- local ghost = gs[i]
- if ghost.x == self.x and ghost.y == self.y then
- return true
- end
- end
- return false
- end,
- update = function(self, map, gs)
- local nx = self.x + self.dir[1]
- local ny = self.y + self.dir[2]
- local b = map:getBlock(nx, ny)
- if b == 0 then
- self.x = nx
- self.y = ny
- end
- return self:checkForGhost(gs)
- end,
- }
- return player
- end
- local map, info = createMap({
- "111111111",
- "100000001",
- "101101101",
- "101000101",
- "100010001",
- "111030111",
- "100010001",
- "101000101",
- "101101101",
- "100020001",
- "111111111"
- },
- {{"player", 2, 0}, {"ghost", 3, 0}}
- )
- local cmaps = {
- ["0"] = {colors.white, colors.black, " "},
- ["1"] = {colors.black, colors.white, "+"}
- }
- local player = createPlayer(info.player[1], info.player[2])
- local function createGhost(sx, sy, smap, sid)
- local gpath = flipTable(findPath({sx, sy}, {player.x, player.y}, smap))
- local ghost = {
- x = sx,
- y = sy,
- id = sid,
- map = smap,
- path = gpath,
- cur = 1,
- getPath = function(self, play)
- local gpath = flipTable(findPath({self.x, self.y}, {play.x, play.y}, self.map))
- if #gpath == 0 or math.random(100) <= 25 then
- local rloc = nil
- local sov = 0
- while rloc == nil and sov < 100 do
- local cloc = {math.random(#self.map[1]), math.random(#self.map)}
- if self.map:getBlock(cloc[1], cloc[2], self.map) == 0 then rloc = cloc end
- sov = sov + 1
- end
- if rloc == nil then rloc = {2,2} end
- gpath = flipTable(findPath({self.x, self.y}, {play.x, play.y}, self.map))
- end
- return gpath
- end,
- checkForGhosts = function(self, gs)
- for i=1,#gs do
- local ghost = gs[i]
- if ghost.x == self.x and ghost.y == self.y and self.id ~= ghost.id then
- return true
- end
- end
- return false
- end,
- checkForIntersect = function(self)
- local n = 0
- local new = { {self.x+1, self.y}, {self.x-1, self.y}, {self.x, self.y+1}, {self.x, self.y-1} }
- for i=1,#new do
- local n = self.map:getBlock(new[i][1], new[i][2])
- if n == 0 then n = n + 1 end
- end
- return (n > 2)
- end,
- update = function(self, gs, play)
- if self.cur > #self.path then
- self.path = self:getPath(play)
- self.cur = 1
- end
- if self.cur <= #self.path then
- local nloc = self.path[self.cur]
- local oloc = {self.x, self.y}
- self.x = nloc[1]
- self.y = nloc[2]
- if self:checkForGhosts(self, gs) then
- self.x = oloc[1]
- self.y = oloc[2]
- elseif self.x == play.x and self.y == play.y then return true
- elseif self:checkForIntersect() and math.random(10) < 5 then
- self.path = self:getPath(play)
- else
- self.cur = self.cur + 1
- end
- else
- self.cur = self.cur + 1
- end
- return false
- end,
- draw = function(self, scale)
- local o = 1
- if scale == 1 then o = 0 end
- local x = (self.x * scale) - o
- local y = (self.y * scale) - o
- for w = 0,scale-1 do
- for h = 0,scale-1 do
- term.setCursorPos(x+w, y+h)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.red)
- term.write("O")
- end
- end
- end,
- }
- return ghost
- end
- local ghosts = {
- createGhost(info.ghost[1], info.ghost[2], map, 1)
- }
- local on = true
- local interval = 0.2
- local scale = 2
- local stuff = {...}
- if #stuff == 1 then
- scale = tonumber(stuff[1])
- elseif #stuff == 2 then
- scale = tonumber(stuff[1])
- interval = tonumber(stuff[2])
- end
- local tick = 0
- local timer = os.startTimer(0)
- local score = 0
- while on do
- local event,p1 = os.pullEvent()
- if event == "timer" and p1 == timer then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- map:draw(0, 0, cmaps, scale)
- if player:update(map, ghosts) then
- on = false
- end
- for i=1,#ghosts do
- if ghosts[i]:update(ghosts, player) then on = false end
- ghosts[i]:draw(scale)
- end
- player:draw(scale)
- if #ghosts < 4 then
- tick = tick + 1
- if tick > 50 then
- ghosts[#ghosts + 1] = createGhost(info.ghost[1], info.ghost[2], map, #ghosts+1)
- tick = 0
- end
- end
- score = score + 1
- timer = os.startTimer(interval)
- elseif event == "key" then
- local key = p1
- if key == 200 then -- up
- player.dir = {0, -1}
- elseif key == 203 then -- left
- player.dir = {-1, 0}
- elseif key == 208 then -- down
- player.dir = {0, 1}
- elseif key == 205 then -- right
- player.dir = {1, 0}
- elseif key == 29 then
- on = false
- end
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1, (#map*scale) + 2)
- print("!!! !!! GAME OVER !!! !!!")
- print("!!! SCORE: "..score)
- print("!!! !!! !!!! !!!! !!! !!!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement