stoneharry

Untitled

Jan 26th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.68 KB | None | 0 0
  1. --[[
  2. Functions:
  3.     SpawnMaze(maze (table), x (number), y (number), z (number) [, save (boolean)])
  4.         Spawns a maze using 'maze' as a blueprint. 'x', 'y', 'z' determines the upper-left
  5.         corner of the maze. 'save' determines if the maze is saved to the database (defaults
  6.         to false).
  7.         returns: nothing.
  8.     SpawnMazeLoad(file (string), x (number), y (number), z (number) [, save (boolean)])
  9.         Loads a maze from 'file'.txt and passes it to SpawnMaze().
  10.         returns: nothing.
  11.     GenerateMaze(width, height)
  12.         Generates a maze with dimensions 'width'*2, 'height'*2. It also includes a
  13.         start point (randomly selected at start of generation) and an end point
  14.         (the point farthest from the start point at end of generation).
  15.         Does not generate despawning walls.
  16.         returns: a table representing the generated maze.
  17.        
  18. Please use SpawnMazeLoad() to actually spawn the maze! It gives much more power over
  19. the final output, and allows placement of despawning walls!
  20. Also, the 'save' argument should only be used when the maze is finalized, as it saves
  21. the whole thing to the database.
  22.  
  23. Please follow these guidelines when editing a maze file:
  24.     # = wall
  25.     S = start point
  26.     E = end point (defaults as the farthest point from start)
  27.     H = despawning wall
  28.     + = corridor
  29. ]]
  30.  
  31. local sizeW = 6 --Width of maze box
  32. local sizeH = 5.5125 --Height of maze box
  33. local Box1 = 50000 --Normal maze box (gameobject)
  34. local Box2 = 50001 --Despawning wall (gameobject)
  35. local StartDummy = 60010 --Start dummy (unit)
  36. local EndDummy = 60016 --End dummy (unit)
  37. local firstMaze = true
  38. local Duration = 60 * 60 * 1000 --1 hour
  39.  
  40. mazeInfo = {
  41.     generating = false,
  42.     players = {},
  43.     completed = {},
  44.     currentMaze = {},
  45.     Start,
  46.     End,
  47.     mazeStart = {0, 0, 0}
  48. }
  49.  
  50. function SpawnMaze(maze, x, y, z)
  51.     assert (type(maze) == "table", "Bad argument #1 to SpawnMaze (table expected, got "..type(maze)..")")
  52.     for k, v in pairs (maze) do
  53.         assert (type(v) == "string")
  54.     end
  55.     assert (type(x) == "number", "Bad argument #2 to SpawnMaze (number expected, got "..type(x)..")")
  56.     assert (type(y) == "number", "Bad argument #3 to SpawnMaze (number expected, got "..type(y)..")")
  57.     assert (type(z) == "number", "Bad argument #4 to SpawnMaze (number expected, got "..type(z)..")")
  58.     for k, v in pairs (maze) do
  59.         for i = 1, string.len(v) do
  60.             local s = string.sub(v, i, i)
  61.             local nX = x + (i * sizeW)
  62.             local nY = y + (k * sizeW)
  63.             if (s == "#") then --Wall
  64.                 local nZ = z
  65.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  66.             elseif (s == "+") then --Corridor
  67.                 local nZ = z + sizeH
  68.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  69.                 nZ = z - sizeH
  70.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  71.             elseif (s == "S") then --Start point dummy
  72.                 local nZ = z + sizeH
  73.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  74.                 nZ = z - sizeH
  75.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  76.                 nZ = z + 0.1
  77.                 PerformIngameSpawn(1, StartDummy, 13, nX, nY, nZ, 0, 35, Duration)
  78.                 mazeInfo.mazeStart[1] = nX
  79.                 mazeInfo.mazeStart[2] = nY
  80.                 mazeInfo.mazeStart[3] = nZ + 0.9
  81.             elseif (s == "E") then --End point dummy
  82.                 local nZ = z + sizeH
  83.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  84.                 nZ = z - sizeH
  85.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  86.                 nZ = z + 0.1
  87.                 PerformIngameSpawn(1, EndDummy, 13, nX, nY, nZ, 0, 35, Duration)
  88.             elseif (s == "H") then --Despawning wall
  89.                 local nZ = z
  90.                 PerformIngameSpawn(2, Box2, 13, nX, nY, nZ, 0, 400, Duration)
  91.                 nZ = z + sizeH
  92.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  93.                 nZ = z - sizeH
  94.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  95.             else
  96.                 print ("Unhandled character \'"..s.."\' in maze spawning code. Replacing with corridor.")
  97.                 local nZ = z + sizeH
  98.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  99.                 nZ = z - sizeH
  100.                 PerformIngameSpawn(2, Box1, 13, nX, nY, nZ, 0, 400, Duration)
  101.             end
  102.         end
  103.     end
  104. end
  105. function SpawnMazeLoad(file, x, y, z)
  106.     local f = assert(io.open("scripts/dropbox/kronos/"..file..".txt"), "Bad argument #1 to SpawnMazeLoad (no .txt file with name "..file..")")
  107.     assert (type(x) == "number", "Bad argument #2 to SpawnMazeLoad (number expected, got "..type(x)..")")
  108.     assert (type(y) == "number", "Bad argument #3 to SpawnMazeLoad (number expected, got "..type(y)..")")
  109.     assert (type(z) == "number", "Bad argument #4 to SpawnMazeLoad (number expected, got "..type(z)..")")
  110.     local maze = {}
  111.     for l in f:lines() do
  112.         table.insert(maze, l)
  113.     end
  114.     f:close()
  115.     SpawnMaze(maze, x, y, z)
  116. end
  117.  
  118. function GenerateMaze(arg1, arg2)
  119.     --Remember, width and height is doubled to make the recursive backtracking work with
  120.     --uneven numbers, so a 10x10 maze actually becomes 20x20.
  121.     --arg1 = width
  122.     --arg2 = height
  123.     assert (type(arg1) == "number", "Bad argument #1 to GenerateMaze (number expected, got "..type(arg1)..")")
  124.     assert (type(arg2) == "number", "Bad argument #2 to GenerateMaze (number expected, got "..type(arg2)..")")
  125.     local maze = {}
  126.     for x = 1, arg1 * 2 do
  127.         maze[x] = {}
  128.         for y = 1, arg2 * 2 do
  129.             maze[x][y] = {"#", 0, 0}
  130.         end
  131.     end
  132.     local hash = (os.time() / os.clock()) + math.random(1, 2^16) - math.random(2^10, 2^16)
  133.     math.randomseed(hash)
  134.     local x = math.random(1, arg1) * 2
  135.     local y = math.random(1, arg2) * 2
  136.     maze[x][y][1] = "S"
  137.     local startX = x
  138.     local startY = y
  139.     local fromStart = 0
  140.     local longestFromStart = 0
  141.     local fromStartX = 0
  142.     local fromStartY = 0
  143.     while (true) do
  144.         local found = false
  145.         local oldX = x
  146.         local oldY = y
  147.         local m = {}
  148.         if (x < ((arg1 * 2) - 2) and maze[x+2][y][1] == "#") then
  149.             table.insert(m, {x+2, y, 1})
  150.         end
  151.         if (x > 2 and maze[x-2][y][1] == "#") then
  152.             table.insert(m, {x-2, y, 2})
  153.         end
  154.         if (y < ((arg2 * 2) - 2) and maze[x][y+2][1] == "#") then
  155.             table.insert(m, {x, y+2, 3})
  156.         end
  157.         if (y > 2 and maze[x][y-2][1] == "#") then
  158.             table.insert(m, {x, y-2, 4})
  159.         end
  160.         if ((#m) > 0) then
  161.             fromStart = fromStart + 1
  162.             local r = math.random(1, (#m))
  163.             x = m[r][1]
  164.             y = m[r][2]
  165.             maze[x][y][1] = "+"
  166.             maze[x][y][2] = oldX
  167.             maze[x][y][3] = oldY
  168.             if (m[r][3] == 1) then
  169.                 maze[x-1][y][1] = "+"
  170.             elseif (m[r][3] == 2) then
  171.                 maze[x+1][y][1] = "+"
  172.             elseif (m[r][3] == 3) then
  173.                 maze[x][y-1][1] = "+"
  174.             else
  175.                 maze[x][y+1][1] = "+"
  176.             end
  177.         else
  178.             if (fromStart > longestFromStart) then
  179.                 longestFromStart = fromStart
  180.                 fromStartX = x
  181.                 fromStartY = y
  182.             end
  183.             fromStart = fromStart - 1
  184.             local tmp1 = x
  185.             x = maze[x][y][2]
  186.             y = maze[tmp1][y][3]
  187.         end
  188.         if (x == startX and y == startY) then
  189.             maze[fromStartX][fromStartY][1] = "E"
  190.             break
  191.         end
  192.     end
  193.     local maze2 = {}
  194.     for k1 = 1, ((arg1 * 2) - 1) do
  195.         if (not maze2[k1]) then
  196.             maze2[k1] = {}
  197.         end
  198.         for k2 = 1, ((arg2 * 2) - 1) do
  199.             maze2[k1][k2] = maze[k1][k2][1]
  200.         end
  201.     end
  202.     local Max = 0
  203.     for k1, v1 in pairs (maze2) do
  204.         for k2, v2 in pairs (v1) do
  205.             local r = math.random(1, ((arg1 * arg2) / 5))
  206.             if (Max < 10 and maze2[k1][k2] == "#" and r == 1) then
  207.                 local m = {}
  208.                 if (maze2[k1+2] and maze2[k1+2][k2] and maze2[k1+2][k2] == "+") then
  209.                     table.insert(m, {k1+1, k2})
  210.                 end
  211.                 if (maze2[k1-2] and maze2[k1-2][k2] and maze2[k1-2][k2] == "+") then
  212.                     table.insert(m, {k1-1, k2})
  213.                 end
  214.                 if (maze2[k1] and maze2[k1][k2+2] and maze2[k1][k2+2] == "+") then
  215.                     table.insert(m, {k1, k2+1})
  216.                 end
  217.                 if (maze2[k1] and maze2[k1][k2-2] and maze2[k1][k2-2] == "+") then
  218.                     table.insert(m, {k1, k2-1})
  219.                 end
  220.                 if ((#m) > 0) then
  221.                     Max = Max + 1
  222.                     maze2[k1][k2] = "+"
  223.                 end
  224.             end
  225.         end
  226.     end
  227.     local maze3 = {}
  228.     for k1, v1 in pairs (maze2) do
  229.         maze3[k1] = table.concat(v1)
  230.     end
  231.     return maze3
  232. end
  233.  
  234. function GenerateNewMaze()
  235.     mazeInfo.generating = true
  236.     if (firstMaze) then
  237.         firstMaze = false
  238.     else
  239.         for k, v in pairs (mazeInfo.players) do
  240.             if (v) then
  241.                 v:Teleport(0, -7477.58, -1254.11, 477.4)
  242.             end
  243.             table.remove(k)
  244.         end
  245.     end
  246.     CreateLuaEvent(DelayGeneration, 2000, 1)
  247. end
  248. function DelayGeneration()
  249.     local maze = GenerateMaze(20, 20) --40x40
  250.     SpawnMaze(maze, -100, -100, 250)
  251.     mazeInfo.generating = false
  252. end
  253.  
  254. function MazeCheckPlayers()
  255.     if (not mazeInfo.generating) then
  256.         for k, v in pairs (mazeInfo.players) do
  257.             if (not v or (v:IsInWorld() and v:GetMapId() ~= 13)) then
  258.                 table.remove(mazeInfo.players, k)
  259.             end
  260.         end
  261.     end
  262. end
  263.  
  264. CreateLuaEvent(MazeCheckPlayers, 2000, 0)
  265.  
  266. function MazeDummyGossip(pUnit, event, player, id, intid)
  267.     if (event == 1) then
  268.         local tmp = true
  269.         for k, v in pairs (mazeInfo.completed) do
  270.             if (v:GetName() == player:GetName()) then
  271.                 tmp = false
  272.             end
  273.         end
  274.         if (tmp) then
  275.             table.insert(mazeInfo.completed, player)
  276.             pUnit:SendChatMessageToPlayer(42, 0, "You have received 5 \124cffa335ee\124Hitem:29434:0:0:0:0:0:0:0:0\124h[Badge of Justice]\124h\124r for completing this course!", player)
  277.             player:AddItem(29434, 5)
  278.             player:CastSpell(47292)
  279.         end
  280.         pUnit:GossipCreateMenu(1, player, 0)
  281.         pUnit:GossipMenuAddItem(2, "Return me to Blackrock Mountain.", 1, 0)
  282.         pUnit:GossipMenuAddItem(0, "Nevermind.", 2, 0)
  283.         pUnit:GossipSendMenu(player)
  284.     elseif (event == 2) then
  285.         if (intid == 1) then
  286.             for k, v in pairs (mazeInfo.players) do
  287.                 if (v:GetName() == player:GetName()) then
  288.                     table.remove(mazeInfo.players, k)
  289.                 end
  290.             end
  291.             player:GossipComplete()
  292.             player:Teleport(0, -7555, -1200, 477)
  293.         elseif (intid == 2) then
  294.             player:GossipComplete()
  295.         end
  296.     end
  297. end
  298.  
  299. RegisterUnitGossipEvent(EndDummy, 1, MazeDummyGossip)
  300. RegisterUnitGossipEvent(EndDummy, 2, MazeDummyGossip)
  301.  
  302. CreateLuaEvent(GenerateNewMaze, 10000, 1)
  303. CreateLuaEvent(GenerateNewMaze, Duration + 1000, 0)
Advertisement
Add Comment
Please, Sign In to add comment