astral17

mazeMaster

Nov 5th, 2016
318
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- TODO
  2. -- ADD TextBox check function (do not need anymore)
  3. -- ADD When Disabled TextBox Painted have another color
  4. -- ADD config saver and checker for correct data
  5. -- ADD PathFinder for help -- IN PROGRESS (~70%)
  6. -- ADD Color Points (mb 1-3 colors)
  7. -- ADD new algorithms for maze generator
  8. -- ADD Mode where Camera move with character
  9. -- ADD Braille draw mode (only draw, not playable)
  10. -- FIX HalfSymbol mode if you stand above exit it is erased
  11. -- ADD Kruskal algorithm
  12. local AGUI_VERSION = "0.5"
  13. local component = require("component")
  14. local success, gui
  15. repeat
  16.   success, gui = pcall(require, "AGUI")
  17.   if not success then
  18.     print(gui)
  19.     io.write("For the application need a library AGUI (https://pastebin.com/s4UFSFwn).\n")
  20.     if not component.isAvailable("internet") then
  21.       os.exit()
  22.     end
  23.     io.write("Do you want to install?[Y/n] ")
  24.     if not ((io.read() or "n").."y"):match("^%s*[Yy]") then
  25.       os.exit()
  26.     end
  27.     loadfile("/bin/wget.lua")("https://pastebin.com/raw/s4UFSFwn", "/lib/AGUI.lua", "-f")
  28.   end
  29. until success
  30.  
  31. if gui.Version ~= AGUI_VERSION then
  32.   io.write("Recommended version of AGUI library ("..AGUI_VERSION..") does not match installed ("..(gui.Version or "unknown")..")\n")
  33.   io.write("Do you want to continue?[Y/n] ")
  34.     if not ((io.read() or "n").."y"):match("^%s*[Yy]") then
  35.       os.exit()
  36.     end
  37. end
  38.  
  39. local gpu = component.gpu
  40. local event = require("event")
  41. local unicode = require("unicode")
  42. --------------SETTINGS----------------
  43. local cfg = {}
  44. cfg.MapWidth = 159 --79
  45. cfg.MapHeight = 97 --49
  46. cfg.Algorithm = "recursive"--"hunt&kill" -- ("recursive"/"hunt&kill")
  47. local WallColor = 0x666666
  48. local FloorColor = 0x000000
  49. local PlayerColor = 0xffff00
  50. local ExitColor = 0x00ff00
  51. local PathColor = 0x0000ff
  52. local MarkerColor = 0xff0000 -- mb more colors
  53. local Qualities = {DoubleSpace = 0, HalfSymbol = 1, Braille = 2} -- not optimal for speed but more beautiful
  54. local Cells = {Empty = 0, Wall = 1, Exit = 2, Path = 3, Marker = 4}
  55. local offsetY = 1
  56. cfg.Quality = Qualities.HalfSymbol
  57. -- cfg.Quality = Qualities.DoubleSpace
  58. cfg.AllowPathFinder = true
  59. cfg.AllowMarkers = true
  60. cfg.StartX = 2
  61. cfg.StartY = 2
  62. cfg.FinishX = -1
  63. cfg.FinishY = -1
  64.  
  65. function InRange(value, left, right)
  66.   return left <= value and value <= right
  67. end
  68.  
  69. function UpdateScreenSettings()
  70.   if cfg.Quality == Qualities.HalfSymbol then
  71.     cfg.ScreenWidth = cfg.MapWidth
  72.     cfg.ScreenHeight = math.floor((cfg.MapHeight + 1) / 2) + offsetY
  73.   else
  74.     cfg.ScreenWidth = cfg.MapWidth * 2
  75.     cfg.ScreenHeight = cfg.MapHeight + offsetY
  76.   end
  77. end
  78.  
  79. function ValidateConfig()
  80.   local MaxScreenWidth, MaxScreenHeight
  81.   MaxScreenWidth, MaxScreenHeight = gpu.maxResolution()
  82.   UpdateScreenSettings()
  83.  
  84.   if not InRange(cfg.ScreenWidth, 1, MaxScreenWidth) then
  85.     return "MapWidthError", "out of bounds"
  86.   end
  87.   if cfg.MapWidth < 5 then
  88.     return "MapWidthError", "out of bounds"
  89.   end
  90.   if cfg.MapWidth % 2 == 0 then
  91.     return "MapWidthError", "only odd value"
  92.   end
  93.  
  94.   if not InRange(cfg.ScreenHeight, 1, MaxScreenHeight) then
  95.     return "MapHeightError", "out of bounds"
  96.   end
  97.   if cfg.MapHeight < 5 then
  98.     return "MapHeightError", "out of bounds"
  99.   end
  100.   if cfg.MapHeight % 2 == 0 then
  101.     return "MapHeightError", "only odd value"
  102.   end
  103.  
  104.   local x, y
  105.   x, y = (cfg.StartX + cfg.MapWidth - 1) % cfg.MapWidth + 1, (cfg.StartY + cfg.MapHeight - 1) % cfg.MapHeight + 1
  106.   if x % 2 == 1 then
  107.     return "StartXError", "only even point"
  108.   end
  109.   if y % 2 == 1 then
  110.     return "StartYError", "only even point"
  111.   end
  112.  
  113.   x, y = (cfg.FinishX + cfg.MapWidth - 1) % cfg.MapWidth + 1, (cfg.FinishY + cfg.MapHeight - 1) % cfg.MapHeight + 1
  114.   if not InRange(x, 1, cfg.MapWidth) then
  115.     return "FinishXError", "out of bounds"
  116.   end
  117.   if x % 2 == 1 then
  118.     return "FinishXError", "only even point"
  119.   end
  120.   if not InRange(y, 1, cfg.MapHeight) then
  121.     return "FinishYError", "out of bounds"
  122.   end
  123.   if y % 2 == 1 then
  124.     return "FinishYError", "only even point"
  125.   end
  126. end
  127.  
  128. --UpdateScreenSettings()
  129. local err, msg = ValidateConfig()
  130. if err ~= nil then
  131.   print(err, msg)
  132.   os.exit()
  133. end
  134. --------------------------------------
  135. local map = {}
  136. local distMap = {}
  137. local markerMap = {}
  138. local size = 200 -- width map mas NEED CHANGE TO cfg.MapWidth
  139.  
  140. local isRunning = false
  141. local quit = false
  142. local plx, ply, finX, finY
  143.  
  144. local algorithms = {}
  145. local Draw = {}
  146. DIR={{x=0,y=-1},{x=-1,y=0},{x=1,y=0},{x=0,y=1}}
  147. -----------------------
  148. -- DEPRECATED
  149. -- function centerText(x, y, w, text)
  150.   -- gpu.set(x+math.floor(w/2-string.len(text)/2),y,text)
  151. -- end
  152.  
  153. -- function messageBox(title,text,color)
  154.   -- local x,y=(cfg.MapWidth-6)/((cfg.Quality and 2)or 1),(math.floor(cfg.MapHeight/2)-3)/((cfg.Quality and 2)or 1)
  155.   -- local len1,len2=string.len(title),string.len(text)
  156.   -- local len3=math.max(len1,len2)+2
  157.   -- gpu.setBackground(0xffffff)
  158.   -- gpu.fill(x,y,len3,2+3," ")
  159.   -- gpu.setForeground(color or 0xFF0000)
  160.   -- centerText(x,y+1,len3,title)
  161.   -- gpu.setForeground(0x000000)
  162.   -- centerText(x,y+3,len3,text)
  163.   -- gpu.setBackground(color or 0xFF0000)
  164.   -- gpu.setForeground(0xffffff)
  165.   -- gpu.fill(x,y+5,len3,3," ")
  166.   -- centerText(x,y+6,len3,"OK")
  167. -- end
  168. -----------------------
  169.  
  170. function toXY(num)
  171.   local y=math.floor((num-1)/size)+1
  172.   return num-(y-1)*size,y
  173. end
  174. function fromXY(x,y)
  175.   return (y-1)*size+x
  176. end
  177.  
  178. function clearMap()
  179.   for x=1,cfg.MapWidth+10 do
  180.     for y=1,cfg.MapHeight+10 do
  181.       if (x%2==0)and(y%2==0) then
  182.         map[fromXY(x, y)] = nil
  183.       else
  184.         map[fromXY(x, y)] = Cells.Wall
  185.       end
  186.     end
  187.   end
  188.   markerMap = {}
  189.   -- markerMap[fromXY(2,2)] = true
  190.   -- markerMap[fromXY(2,3)] = true
  191. end
  192.  
  193. --DoubleSpace
  194. Draw[Qualities.DoubleSpace] = {}
  195. Draw[Qualities.DoubleSpace].Map = function()
  196.   gpu.setBackground(WallColor)
  197.   gpu.fill(1, 1 + offsetY, cfg.MapWidth * 2, cfg.MapHeight, " ")
  198.   gpu.setBackground(FloorColor)
  199.   for x=1,cfg.MapWidth do
  200.     for y=1,cfg.MapHeight do
  201.       if map[fromXY(x,y)] ~= Cells.Wall then
  202.         gpu.set(x * 2 - 1, y + offsetY, "  ")
  203.         -- gpu.set(x * 2 - 1, y + offsetY, (distMap[fromXY(x, y)] < 10 and "0" or "")..tostring(distMap[fromXY(x, y)]))
  204.       end
  205.     end
  206.   end
  207. end
  208.  
  209. Draw[Qualities.DoubleSpace].Point = function(x, y, col)
  210.   gpu.setBackground(col or (markerMap[fromXY(x, y)] and MarkerColor or FloorColor))
  211.   gpu.set(x * 2 - 1, y + offsetY, "  ")
  212.   gpu.setBackground(0x000000)
  213.   -- gpu.setBackground(0xffff00)
  214.   -- gpu.set(3,2 + offsetY,"  ")
  215.   -- gpu.setBackground(0x00ff00)
  216.   -- gpu.set((cfg.MapWidth-1)*2-1,cfg.MapHeight-1 + offsetY,"  ")
  217.   -- gpu.setBackground(0x000000)
  218. end
  219.  
  220. Draw[Qualities.DoubleSpace].Path = function(x, y)
  221.   local point = Draw[Qualities.DoubleSpace].Point
  222.   local b
  223.   while distMap[fromXY(x, y)] ~= 0 do
  224.     point(x, y, PathColor)
  225.     b = true
  226.     for dr = 1, 4 do
  227.       if distMap[fromXY(x + DIR[dr].x, y + DIR[dr].y)] ~= nil and distMap[fromXY(x + DIR[dr].x, y + DIR[dr].y)] < distMap[fromXY(x, y)] then
  228.         x, y = x + DIR[dr].x, y + DIR[dr].y
  229.         b = false
  230.         break
  231.       end
  232.     end
  233.     if b then
  234.       break
  235.     end
  236.   end
  237. end
  238.  
  239. --HalfSymbol
  240. Draw[Qualities.HalfSymbol] = {}
  241. local PIX={[0]=" ",[1]="▀",[2]="▄",[3]="█"}
  242.  
  243. Draw[Qualities.HalfSymbol].Map = function()
  244.   gpu.setBackground(FloorColor)
  245.   gpu.setForeground(WallColor)
  246.   local s,tmp="none",0
  247.   for y=2,cfg.MapHeight+1,2 do
  248. --      s=""
  249.     s = {}
  250.     for x=1,cfg.MapWidth do
  251.       tmp=0
  252.       if map[fromXY(x,y-1)] == Cells.Wall then
  253.         tmp=tmp+1
  254.       end
  255.       if (map[fromXY(x,y)] == Cells.Wall)and(y~=cfg.MapHeight+1) then
  256.         tmp=tmp+2
  257.       end
  258. --        s=s..PIX[tmp]
  259.       table.insert(s, PIX[tmp])
  260.     end
  261.     -- if y==52 then
  262.      -- print("1"..s.."1")
  263.     -- end
  264.     s = table.concat(s)
  265.     gpu.set(1, math.floor(y / 2) + offsetY, s)
  266.   end
  267.   gpu.setForeground(0xffffff)
  268.   gpu.setBackground(0x000000)
  269. end
  270.  
  271. Draw[Qualities.HalfSymbol].Point = function(x, y, color, full)
  272.   local TopColor, BottomColor
  273.   if full then
  274.     TopColor    = color
  275.     BottomColor = color
  276.   else
  277.     if y%2 == 0 then
  278.       TopColor    = map[fromXY(x, y - 1)] == Cells.Wall and WallColor or (markerMap[fromXY(x, y - 1)] and MarkerColor or FloorColor)
  279.       BottomColor = color or (markerMap[fromXY(x, y)] and MarkerColor or FloorColor)
  280.     else
  281.       TopColor    = color or (markerMap[fromXY(x, y)] and MarkerColor or FloorColor)
  282.       BottomColor = map[fromXY(x, y + 1)] == Cells.Wall and WallColor or (markerMap[fromXY(x, y + 1)] and MarkerColor or FloorColor)
  283.     end
  284.   end
  285.  
  286.   gpu.setForeground(TopColor)
  287.   gpu.setBackground(BottomColor)
  288.   gpu.set(x, math.floor((y + 1) / 2) + offsetY, PIX[1])
  289.   gpu.setBackground(0x000000)
  290.   gpu.setForeground(0xffffff)
  291. end
  292.  
  293. Draw[Qualities.HalfSymbol].Path = function(x, y)
  294.   local point = Draw[Qualities.HalfSymbol].Point
  295.   local nY
  296.   local fullDraw = false
  297.   for dr = 1, 4 do
  298.     if distMap[fromXY(x + DIR[dr].x, y + DIR[dr].y)] ~= nil and distMap[fromXY(x + DIR[dr].x, y + DIR[dr].y)] < distMap[fromXY(x, y)] then
  299.       x, y = x + DIR[dr].x, y + DIR[dr].y
  300.       break
  301.     end
  302.   end
  303.   while distMap[fromXY(x, y)] ~= 0 do
  304.     point(x, y, PathColor, fullDraw)
  305.     fullDraw = false
  306.     nY = nil
  307.     for dr = 1, 4 do
  308.       if distMap[fromXY(x + DIR[dr].x, y + DIR[dr].y)] ~= nil and distMap[fromXY(x + DIR[dr].x, y + DIR[dr].y)] < distMap[fromXY(x, y)] then
  309.         x, nY = x + DIR[dr].x, y + DIR[dr].y
  310.         break
  311.       end
  312.     end
  313.     if nY == nil then
  314.       break
  315.     end
  316.     if math.floor((nY + 1) / 2) == math.floor((y + 1) / 2) and nY ~= y then
  317.       fullDraw = true
  318.     end
  319.     y = nY
  320.   end
  321. end
  322.  
  323. -- Draw[Qualities.HalfSymbol].Point = function(x, y, col)
  324.   -- local tmp=0
  325.   -- if y%2==0 then
  326.     -- if map[fromXY(x,y-1)]==false then
  327.       -- gpu.setBackground(0x000000)
  328.     -- else
  329.       -- gpu.setBackground(0x666666)
  330.     -- end
  331.     -- gpu.setForeground(col or 0xffff00)
  332.     -- tmp=2
  333.   -- elseif (x==cfg.MapWidth-1)and(y==cfg.MapHeight-2) then
  334.     -- gpu.setBackground(0x00ff00)
  335.     -- gpu.setForeground(col or 0xffff00)
  336.     -- tmp=1
  337.   -- else
  338.     -- if map[fromXY(x,y+1)]==false then
  339.       -- gpu.setBackground(0x000000)
  340.     -- else
  341.       -- gpu.setBackground(0x666666)
  342.     -- end
  343.     -- gpu.setForeground(col or 0xffff00)
  344.     -- tmp=1
  345.   -- end
  346.   -- gpu.set(x, math.floor((y-1)/2) + 1 + offsetY, PIX[tmp])
  347. -- end
  348.  
  349. --[[
  350. function drawMap()
  351.   if cfg.Quality then
  352.     gpu.setBackground(0x000000)
  353.     gpu.setForeground(0x666666)
  354.     local s,tmp="none",0
  355.     for y=2,cfg.MapHeight+1,2 do
  356.      -- s=""
  357.       s = {}
  358.       for x=1,cfg.MapWidth do
  359.         tmp=0
  360.         if map[fromXY(x,y-1)] then
  361.           tmp=tmp+1
  362.         end
  363.         if (map[fromXY(x,y)])and(y~=cfg.MapHeight+1) then
  364.           tmp=tmp+2
  365.         end
  366.        -- s=s..PIX[tmp]
  367.         table.insert(s, PIX[tmp])
  368.       end
  369.       -- if y==52 then
  370.        -- print("1"..s.."1")
  371.       -- end
  372.       s = table.concat(s)
  373.       gpu.set(1, math.floor(y / 2) + offsetY, s)
  374.     end
  375.     gpu.setForeground(0xffffff)
  376.   else
  377.     gpu.setBackground(0x666666)
  378.     gpu.fill(1, 1 + offsetY, cfg.MapWidth * 2, cfg.MapHeight, " ")
  379.     gpu.setBackground(0x000000)
  380.     for x=1,cfg.MapWidth do
  381.       for y=1,cfg.MapHeight do
  382.         if not map[fromXY(x,y)] then
  383.           gpu.set(x * 2 - 1, y + offsetY, "  ")
  384.         end
  385.       end
  386.     end
  387.   end
  388.   gpu.setBackground(0x000000)
  389. end
  390. --]]
  391.  
  392. algorithms["recursive"] = function()
  393.   local stack={}
  394.   local stklen,step=1,0
  395.   local posx, posy = math.random(1, math.floor(cfg.MapWidth / 2)) * 2,math.random(1, math.floor(cfg.MapHeight / 2)) * 2
  396.   stack[1]={x=posx,y=posy}
  397.   while stklen>0 do
  398. --    step=(step+1)%1000
  399. --    os.sleep(0)
  400.     map[fromXY(stack[stklen].x,stack[stklen].y)] = Cells.Empty
  401. --    drawMap()
  402.     local bool,tmp=true,true
  403.     while bool do
  404.       tmp=false
  405.       local rnd=math.random(0,3)
  406.       if (map[fromXY(stack[stklen].x-2,stack[stklen].y)]==nil)and(stack[stklen].x-1~=1) then
  407.         tmp=true
  408.         if rnd==0 then
  409.           map[fromXY(stack[stklen].x-1,stack[stklen].y)] = Cells.Empty
  410.           stklen=stklen+1
  411.           stack[stklen]={x=stack[stklen-1].x-2,y=stack[stklen-1].y}
  412.           break
  413.         end
  414.       end
  415.       if (map[fromXY(stack[stklen].x+2,stack[stklen].y)]==nil)and(stack[stklen].x+1~=cfg.MapWidth) then
  416.         tmp=true
  417.         if rnd==1 then
  418.           map[fromXY(stack[stklen].x+1,stack[stklen].y)] = Cells.Empty
  419.           stklen=stklen+1
  420.           stack[stklen]={x=stack[stklen-1].x+2,y=stack[stklen-1].y}
  421.           break
  422.         end
  423.       end
  424.       if (map[fromXY(stack[stklen].x,stack[stklen].y-2)]==nil)and(stack[stklen].y-1~=1) then
  425.         tmp=true
  426.         if rnd==2 then
  427.           map[fromXY(stack[stklen].x,stack[stklen].y-1)] = Cells.Empty
  428.           stklen=stklen+1
  429.           stack[stklen]={x=stack[stklen-1].x,y=stack[stklen-1].y-2}
  430.           break
  431.         end
  432.       end
  433.       if (map[fromXY(stack[stklen].x,stack[stklen].y+2)]==nil)and(stack[stklen].y+1~=cfg.MapHeight) then
  434.         tmp=true
  435.         if rnd==3 then
  436.           map[fromXY(stack[stklen].x,stack[stklen].y+1)] = Cells.Empty
  437.           stklen=stklen+1
  438.           stack[stklen]={x=stack[stklen-1].x,y=stack[stklen-1].y+2}
  439.           break
  440.         end
  441.       end
  442.       bool=tmp
  443.     end
  444.     if not tmp then
  445.       stklen=stklen-1
  446.     end
  447.   end
  448. end
  449.  
  450. algorithms["hunt&kill"] = function()
  451.   local function testField()
  452.     for j=2,cfg.MapHeight,2 do
  453.       for i=2,cfg.MapWidth,2 do
  454.         if map[fromXY(i,j)]==nil then
  455.           for dr=1,4 do
  456.             if map[fromXY(i+DIR[dr].x*2,j+DIR[dr].y*2)] == Cells.Empty then
  457.               map[fromXY(i,j)] = Cells.Empty
  458.               map[fromXY(i+DIR[dr].x,j+DIR[dr].y)] = Cells.Empty
  459.               return i,j
  460.             end
  461.           end
  462.         end
  463.       end
  464.     end
  465.     return false
  466.   end
  467.   local isRunning,tmp=true,true
  468.   local posx,posy,rnd=math.random(1,math.floor(cfg.MapWidth/2))*2,math.random(1,math.floor(cfg.MapHeight/2))*2,0
  469.   while isRunning do
  470.     tmp=true
  471.     map[fromXY(posx, posy)] = Cells.Empty
  472.     while tmp do
  473.       tmp=false
  474.       rnd=math.random(1,4)
  475.       for dr=1,4 do
  476.         if (map[fromXY(posx+DIR[dr].x*2,posy+DIR[dr].y*2)]==nil)and(posx+DIR[dr].x~=1)and(posx+DIR[dr].x~=cfg.MapWidth)and(posy+DIR[dr].y~=1)and(posy+DIR[dr].y~=cfg.MapHeight) then
  477.           tmp=true
  478.           if rnd==dr then
  479.             map[fromXY(posx+DIR[dr].x,posy+DIR[dr].y)] = Cells.Empty
  480.             map[fromXY(posx+DIR[dr].x*2,posy+DIR[dr].y*2)] = Cells.Empty
  481.             posx,posy=posx+DIR[dr].x*2,posy+DIR[dr].y*2
  482.             break
  483.           end
  484.         end
  485.       end
  486.     end
  487.     -- Draw[cfg.Quality].Map();os.sleep(0)
  488.     posx,posy=testField()
  489.     if posx==false then
  490.       isRunning=false
  491.     end
  492.   end
  493. end
  494.  
  495. -- function drawPoint(x,y,col)
  496.   -- local tmp=0
  497.   -- if y%2==0 then
  498.     -- if map[fromXY(x,y-1)]==false then
  499.       -- gpu.setBackground(0x000000)
  500.     -- else
  501.       -- gpu.setBackground(0x666666)
  502.     -- end
  503.     -- gpu.setForeground(col or 0xffff00)
  504.     -- tmp=2
  505.   -- elseif (x==cfg.MapWidth-1)and(y==cfg.MapHeight-2) then
  506.     -- gpu.setBackground(0x00ff00)
  507.     -- gpu.setForeground(col or 0xffff00)
  508.     -- tmp=1
  509.   -- else
  510.     -- if map[fromXY(x,y+1)]==false then
  511.       -- gpu.setBackground(0x000000)
  512.     -- else
  513.       -- gpu.setBackground(0x666666)
  514.     -- end
  515.     -- gpu.setForeground(col or 0xffff00)
  516.     -- tmp=1
  517.   -- end
  518.   -- gpu.set(x, math.floor((y-1)/2) + 1 + offsetY, PIX[tmp])
  519. -- end
  520.  
  521. function onKeyDown(_, _, ch1, ch2)
  522.   if ch1 == 112 and cfg.AllowPathFinder then
  523.     -- isRunning=false
  524.     Draw[cfg.Quality].Path(plx, ply)
  525.   end
  526.   if ch1 == 32 and cfg.AllowMarkers then
  527.     markerMap[fromXY(plx, ply)] = not markerMap[fromXY(plx, ply)]
  528.   end
  529.   if ch1==0 then
  530. --    print(ch2)
  531.     if ch2==203 then
  532.       lP=true
  533.     elseif ch2==200 then
  534.       uP=true
  535.     elseif ch2==205 then
  536.       rP=true
  537.     elseif ch2==208 then
  538.       dP=true
  539.     end
  540.   end
  541. end
  542.  
  543. function onKeyUp(_,_,ch1,ch2)
  544.   if ch1==0 then
  545.     if ch2==203 then
  546.       lP=false
  547.     elseif ch2==200 then
  548.       uP=false
  549.     elseif ch2==205 then
  550.       rP=false
  551.     elseif ch2==208 then
  552.       dP=false
  553.     end
  554.   end
  555. end
  556.  
  557. function FindPath()
  558.   local queue = {fromXY(finX, finY)}
  559.   local queueFront = 1
  560.   local x, y, cell, nextCell, dist
  561.   distMap = {}
  562.   distMap[ queue[1] ] = 0
  563.   while queue[queueFront] ~= nil do
  564.     cell = queue[queueFront]
  565.     queueFront = queueFront + 1
  566.     x, y = toXY(cell)
  567.     dist = distMap[cell] + 1
  568.     for dr = 1, 4 do
  569.       if InRange(x + DIR[dr].x, 1, cfg.MapWidth) and InRange(y + DIR[dr].y, 1, cfg.MapHeight) then
  570.         nextCell = fromXY(x + DIR[dr].x, y + DIR[dr].y)
  571.         if distMap[nextCell] == nil and map[nextCell] ~= Cells.Wall then
  572.           distMap[nextCell] = dist
  573.           table.insert(queue, nextCell)
  574.         end
  575.       end
  576.     end
  577.   end
  578. end
  579.  
  580. function GameInit()
  581.   clearMap()
  582.   plx, ply   = (cfg.StartX  + cfg.MapWidth - 1) % cfg.MapWidth + 1, (cfg.StartY  + cfg.MapHeight - 1) % cfg.MapHeight + 1
  583.   finX, finY = (cfg.FinishX + cfg.MapWidth - 1) % cfg.MapWidth + 1, (cfg.FinishY + cfg.MapHeight - 1) % cfg.MapHeight + 1
  584.   algorithms[cfg.Algorithm]()
  585.   -- if cfg.Algorithm == "recursive" then
  586.     -- generateMaze(cfg.StartX, cfg.StartY)
  587.   -- elseif cfg.Algorithm == "hunt&kill" then
  588.     -- generateMaze2()
  589.   -- end
  590.   FindPath()
  591.   --os.sleep(2)
  592.   -- drawMap()
  593.   Draw[cfg.Quality].Map()
  594.  
  595.   -- Draw[cfg.Quality].Path(2, 2)
  596.   Draw[cfg.Quality].Point(plx, ply, PlayerColor)
  597.   Draw[cfg.Quality].Point(finX, finY, ExitColor)
  598.   -- if cfg.Quality == Qualities.HalfSymbol then
  599.     -- drawPoint(2,2)
  600.     -- drawPoint(cfg.MapWidth-1,cfg.MapHeight-1,0x00ff00)
  601.   -- else
  602.     -- gpu.setBackground(0xffff00)
  603.     -- gpu.set(3,2 + offsetY,"  ")
  604.     -- gpu.setBackground(0x00ff00)
  605.     -- gpu.set((cfg.MapWidth-1)*2-1,cfg.MapHeight-1 + offsetY,"  ")
  606.     -- gpu.setBackground(0x000000)
  607.   -- end
  608.   lP, rP, uP, dP = false, false, false, false
  609.   --
  610.   event.listen("key_down",onKeyDown)
  611.   event.listen("key_up",onKeyUp)
  612.   isRunning = true
  613. end
  614.  
  615. function GameHandle()
  616. -- while isRunning do
  617.   local plxb,plyb=plx,ply
  618.   if (lP)and(map[fromXY(plx-1,ply)] ~= Cells.Wall) then--left
  619.     plx=plx-1
  620.   elseif (uP)and(map[fromXY(plx,ply-1)] ~= Cells.Wall) then--up
  621.     ply=ply-1
  622.   elseif (rP)and(map[fromXY(plx+1,ply)] ~= Cells.Wall) then--right
  623.     plx=plx+1
  624.   elseif (dP)and(map[fromXY(plx,ply+1)] ~= Cells.Wall) then--down
  625.     ply=ply+1
  626.   end
  627.   if (plx~=plxb)or(ply~=plyb) then
  628.     Draw[cfg.Quality].Point(plxb, plyb)
  629.     Draw[cfg.Quality].Point(plx, ply, PlayerColor)
  630.    
  631.     -- if cfg.Quality then
  632.       -- drawPoint(plxb,plyb,0x000000)
  633.       -- drawPoint(plx,ply)
  634.     -- else
  635.       -- gpu.setBackground(0x000000)
  636.       -- gpu.set(plxb*2-1,plyb + offsetY,"  ")
  637.       -- gpu.setBackground(0xffff00)
  638.       -- gpu.set(plx*2-1,ply + offsetY,"  ")
  639.     -- end
  640.     if (plx == finX) and (ply == finY) then
  641.       isRunning = false
  642.       --messageBox("congratulation","You Win!")
  643.       gui.backend.MessageBox:Create("congratulation", "You Win!"):Modify{ButtonHandle = function() GameForm.Elements["Exit"]:OnElementClick() end}:Init():Paint()
  644.     end
  645.   end
  646.   os.sleep(0.1)
  647. -- end
  648. end
  649.  
  650. -- event.ignore("key_down",onKeyDown)
  651. -- event.ignore("key_up",onKeyUp)
  652. -- map=nil
  653. -- gpu.setBackground(0x000000)
  654. -- gpu.setForeground(0xffffff)
  655. --event.pull("key_down")
  656.  
  657. -------------------- GUI --------------------
  658. gui.Init()
  659.  
  660. GameForm = gui.backend.Form:Create(cfg.ScreenWidth, cfg.ScreenHeight,
  661.   {
  662.     Exit = gui.backend.Button:Create(1, 1, 8, 1, "[Return]", function()
  663.       event.ignore("key_down",onKeyDown)
  664.       event.ignore("key_up",onKeyUp)
  665.       isRunning = false
  666.       GameForm:Disable(true)
  667.       MainForm:Enable():Paint()
  668.     end),
  669.    
  670.     algo = gui.backend.Text:Create(10, 1, nil, cfg.Algorithm),
  671.   }
  672. ):Init()
  673.  
  674. SettingsForm = gui.backend.Form:Create(32, 15,
  675.   {
  676.     gui.backend.Text:Create(1, 1, 32, "Settings"),
  677.    
  678.     gui.backend.Text:Create(1, 3, 7, "Width"),
  679.     MapWidth = gui.backend.TextBox:Create(1, 4, 7, cfg.MapWidth .. "", "0123456789"),
  680.     MapWidthError = gui.backend.Text:Create(1, 5, nil, ""):Modify{TextColor = 0xff0000},
  681.    
  682.     gui.backend.Text:Create(9, 3, 7, "Height"),
  683.     MapHeight = gui.backend.TextBox:Create(9, 4, 7, cfg.MapHeight .. "", "0123456789"),
  684.     MapHeightError = gui.backend.Text:Create(1, 5, nil, ""):Modify{TextColor = 0xff0000},
  685.    
  686.     -- Quality = gui.backend.CheckBox:Create(1, 6, 20, "Super Quality", cfg.Quality == Qualities.HalfSymbol),
  687.     gui.backend.Text:Create(1, 6, nil, "Graphic Mode"),
  688.     Quality = gui.backend.RadioGroup:Create(
  689.     {
  690.       [Qualities.DoubleSpace] = gui.backend.RadioButton:Create(1, 7, 15, "DoubleSpace"),
  691.       [Qualities.HalfSymbol]  = gui.backend.RadioButton:Create(1, 8, 15, "Half Symbol"),
  692.     }, cfg.Quality),
  693.    
  694.     gui.backend.Text:Create(1, 10, nil, "Algorithm"),
  695.     Algorithm = gui.backend.RadioGroup:Create(
  696.     {
  697.       ["recursive"] = gui.backend.RadioButton:Create(1, 11, 15, "Recursive"),
  698.       ["hunt&kill"] = gui.backend.RadioButton:Create(1, 12, 15, "Hunt&Kill"),
  699.     }, cfg.Algorithm),
  700.    
  701.     gui.backend.Text:Create(18, 3, 15, "Start Position"),
  702.     StartX = gui.backend.TextBox:Create(18, 4, 7, cfg.StartX .. "", "0123456789"),
  703.     StartXError = gui.backend.Text:Create(18, 5, nil, ""):Modify{TextColor = 0xff0000},
  704.     StartY = gui.backend.TextBox:Create(26, 4, 7, cfg.StartY .. "", "0123456789"),
  705.     StartYError = gui.backend.Text:Create(18, 5, nil, ""):Modify{TextColor = 0xff0000},
  706.    
  707.     gui.backend.Text:Create(18, 6, 15, "Finish Position"),
  708.     FinishX = gui.backend.TextBox:Create(18, 7, 7, cfg.FinishX .. "", "0123456789"),
  709.     FinishXError = gui.backend.Text:Create(18, 8, nil, ""):Modify{TextColor = 0xff0000},
  710.     FinishY = gui.backend.TextBox:Create(26, 7, 7, cfg.FinishY .. "", "0123456789"),
  711.     FinishYError = gui.backend.Text:Create(18, 8, nil, ""):Modify{TextColor = 0xff0000},
  712.    
  713.     gui.backend.Text:Create(18, 9, nil, "Another"),
  714.     AllowMarkers = gui.backend.CheckBox:Create(18, 10, 15, "Markers", cfg.AllowMarkers),
  715.     AllowPathFinder = gui.backend.CheckBox:Create(18, 11, 15, "PathFinder", cfg.AllowPathFinder),
  716.    
  717.     gui.backend.Button:Create(1, 13, 32, 3, "Back", function()
  718.       cfg.MapWidth = SettingsForm.Elements["MapWidth"].Text + 0
  719.       cfg.MapHeight = SettingsForm.Elements["MapHeight"].Text + 0
  720.       cfg.StartX = SettingsForm.Elements["StartX"].Text + 0
  721.       cfg.StartY = SettingsForm.Elements["StartY"].Text + 0
  722.       cfg.FinishX = SettingsForm.Elements["FinishX"].Text + 0
  723.       cfg.FinishY = SettingsForm.Elements["FinishY"].Text + 0
  724.       cfg.Quality = SettingsForm.Elements["Quality"].Checked
  725.       cfg.Algorithm = SettingsForm.Elements["Algorithm"].Checked
  726.       cfg.AllowPathFinder = SettingsForm.Elements["AllowPathFinder"].Checked
  727.       cfg.AllowMarkers = SettingsForm.Elements["AllowMarkers"].Checked
  728.       -- UpdateScreenSettings()
  729.       SettingsForm.Elements["MapWidthError"]:Modify{Text = ""}:Paint()
  730.       SettingsForm.Elements["MapWidth"]:Modify{TextColor = 0xffffff}:Paint()
  731.       SettingsForm.Elements["MapHeightError"]:Modify{Text = ""}:Paint()
  732.       SettingsForm.Elements["MapHeight"]:Modify{TextColor = 0xffffff}:Paint()
  733.       SettingsForm.Elements["StartXError"]:Modify{Text = ""}:Paint()
  734.       SettingsForm.Elements["StartX"]:Modify{TextColor = 0xffffff}:Paint()
  735.       SettingsForm.Elements["StartYError"]:Modify{Text = ""}:Paint()
  736.       SettingsForm.Elements["StartY"]:Modify{TextColor = 0xffffff}:Paint()
  737.       SettingsForm.Elements["FinishXError"]:Modify{Text = ""}:Paint()
  738.       SettingsForm.Elements["FinishX"]:Modify{TextColor = 0xffffff}:Paint()
  739.       SettingsForm.Elements["FinishYError"]:Modify{Text = ""}:Paint()
  740.       SettingsForm.Elements["FinishY"]:Modify{TextColor = 0xffffff}:Paint()
  741.       local err, msg = ValidateConfig()
  742.       if err ~= nil then
  743.         SettingsForm.Elements[err]:Modify{Text = msg}:Paint()
  744.         SettingsForm.Elements[unicode.sub(err, 1, -6)]:Modify{TextColor = 0xff0000}:Paint()
  745.         return false
  746.       end
  747.       GameForm.Width = cfg.ScreenWidth
  748.       GameForm.Height = cfg.ScreenHeight
  749.       GameForm.Elements["algo"].Text = cfg.Algorithm
  750.       SettingsForm:Disable(true)
  751.       MainForm:Enable():Paint()
  752.     end),
  753.   }
  754. ):Init()
  755.  
  756. MainForm = gui.backend.Form:Create(20, 11,
  757.   {
  758.     gui.backend.Button:Create(1, 1, 20, 3, "Start", function()
  759.       MainForm:Disable(true)
  760.       GameForm:Enable():Paint()
  761.       GameInit()
  762.     end),
  763.     gui.backend.Button:Create(1, 5, 20, 3, "Settings", function() MainForm:Disable(true) SettingsForm:Enable():Paint() end),
  764.     gui.backend.Button:Create(1, 9, 20, 3, "Exit", function() quit = true end),
  765.   }
  766. )
  767.  
  768. MainForm:Init():Enable():Paint()
  769.  
  770. quit = false
  771. pcall(function()
  772.   while not quit do
  773.     if isRunning then
  774.       GameHandle()
  775.     else
  776.       event.pull()
  777.     end
  778.   end
  779. end)
  780.  
  781. gui.Destroy()
  782. event.ignore("key_down",onKeyDown)
  783. event.ignore("key_up",onKeyUp)
  784.  
  785. gpu.setBackground(0x000000)
  786. gpu.setForeground(0xffffff)
RAW Paste Data