ecco7777

Labyrinth Generator2

Mar 5th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. size=15
  2. wayLenght=50
  3. minLenght=20
  4. trys=0
  5. avtrys=0
  6.  
  7. function newMap(size)
  8. map={}
  9. for x=1,size do
  10. map[x]={}
  11. for y=1,size do
  12. map[x][y]=0
  13. end
  14. end
  15. return map
  16. end
  17.  
  18. function getOptions(map,x,y)
  19. local i = 1
  20. local option={}
  21. if x~=nil and y~=nil then
  22. if x+2<=size and y+2<=size and x+2>0 and y+2>0 and x-2<=size and y-2<=size and x-2>0 and y-2>0 then
  23. if map[x][y+2]==nil or map[x][y+2]==1 then  else map[x][y+2]=1 map[x][y+1]=1 option[i]=1 i=i+1 print("debug1") end
  24. if map[x][y-2]==nil or map[x][y-2]==1 then  else map[x][y-2]=1 map[x][y-1]=1 option[i]=2 i=i+1 print("debug2") end
  25. if map[x+2][y]==nil or map[x+2][y]==1 then  else map[x+2][y]=1 map[x+1][y]=1 option[i]=3 i=i+1 print("debug3") end
  26. if map[x-2][y]==nil or map[x-2][y]==1 then  else map[x-2][y]=1 map[x-1][y]=1 option[i]=4 i=i+1 print("debug4") end
  27. end
  28. end
  29. return option
  30. end
  31.  
  32. function goForward(map,dir)
  33. if x+2<=size and y+2<=size and x+2>0 and y+2>0 and x-2<=size and y-2<=size and x-2>0 and y-2>0 then
  34. if dir==1 then
  35. if map[x][y+2]==nil or map[x][y+2]==1 then return false else map[x][y+2]=1 map[x][y+1]=1 y=y+2 end
  36. end
  37. if dir==3 then
  38. if map[x][y-2]==nil or map[x][y-2]==1 then return false else map[x][y-2]=1 map[x][y-1]=1 y=y-2 end
  39. end
  40. if dir==2 then
  41. if map[x+2][y]==nil or map[x+2][y]==1 then return false else map[x+2][y]=1 map[x+1][y]=1 x=x+2 end
  42. end
  43. if dir==4 then
  44. if map[x-2][y]==nil or map[x-2][y]==1 then return false else map[x-2][y]=1 map[x-1][y]=1 x=x-2  end
  45. end
  46. addStack(x,y)
  47. else
  48. return false
  49. end
  50. end
  51.  
  52. function getPathLenght(map)
  53. wert=0
  54. for x=1,size do
  55. for y=1,size do
  56. wert=wert+map[x][y]
  57. end
  58. end
  59. return wert
  60. end
  61.  
  62. function makeStack()
  63. stack={}
  64. stack["current"]=0
  65. stack["x"]={}
  66. stack["y"]={}
  67.  
  68. end
  69.  
  70. function addStack(x,y)
  71. stack["current"]=stack["current"]+1
  72. stack["x"][stack["current"]]=x
  73. stack["y"][stack["current"]]=y
  74. end
  75.  
  76. function rmStack()
  77. stack["current"]=stack["current"]-1
  78. end
  79.  
  80. function getStack()
  81. return stack["x"][stack["current"]], stack["y"][stack["current"]]
  82. end
  83.  
  84. function writeMap(map,file)
  85. shell.run("rm "..file)
  86. fp=fs.open(file,"w")
  87. size=#map[1]
  88. for x=1,size do
  89. for y=1,size do
  90. fp.write(tostring(map[x][y]))
  91. end
  92. fp.write("\n")
  93. end
  94. fp.close()
  95. end
  96.  
  97. makeStack()
  98.  
  99. while true do
  100. way=newMap(size)
  101. x=math.random(1,size)
  102. y=math.random(1,size)
  103. way[x][y]=1
  104. for i=1,wayLenght do
  105. --while stack["current"]>1 do
  106. local option=getOptions(map,x,y)
  107. print("Option "..tostring(#option))
  108. term.clear()
  109. if #option~=0 then
  110. goForward(way,option[math.random(1,#option)])
  111. else
  112. rmStack()
  113. x,y=getStack()
  114. end
  115. end
  116.  
  117. if getPathLenght(way)>=minLenght then
  118. paintutils.drawImage(way,1,1)
  119. term.setBackgroundColor(colors.black)
  120. term.setCursorPos(1,1)
  121. term.write(trys)
  122. avtrys=(avtrys+trys)/2
  123. term.setCursorPos(1,2)
  124. term.write(avtrys)
  125. sleep(0.5)
  126. term.clear()
  127. trys=0
  128. else trys=trys+1
  129. end
  130. end
Add Comment
Please, Sign In to add comment