Advertisement
Guest User

Love2d Algorithm Attempt

a guest
Jan 2nd, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. function love.load()
  2.     player =  -- Sets up a table to store all the player variables such as location, speed and angle
  3.     {
  4.     x = 50,
  5.     y = 50,
  6.     speed = 100,
  7.     angle = 0
  8.     }
  9.     --Below here is the map generation algorithm
  10.     -- Proceed with caution (Highly spaghettified)
  11.  
  12.     -- Create Data Structure
  13.     map = {
  14.     width = 800,
  15.     height = 600,
  16.     }
  17.  
  18.     for i = 1, #map do
  19.         map[i][j] = 0
  20.     end
  21.  
  22.     -- rooms and room sizes
  23.     minRooms = (map.width * map.height) / 300
  24.     maxRooms = (map.width * map.height) / 150
  25.     roomCount = math.random(minRooms, maxRooms)
  26.     widthRoot = math.sqrt(map.width * 2)
  27.     heightRoot = math.sqrt(map.height * 2)
  28.     minWidth = (map.width * 0.5) / widthRoot
  29.     maxWidth = (map.width * 2.0) / widthRoot
  30.     minHeight = (map.height * 0.5) / heightRoot
  31.     maxHeight = (map.height * 2.0) / heightRoot
  32.  
  33.     -- Create the rooms!
  34.     roomList= {}
  35.     room = {}
  36.     for i = 0, roomCount do
  37.         ok = false
  38.         while ok ~= true do
  39.             room.x = math.random(1 , map.width)
  40.             room.y = math.random(1 , map.height)
  41.             room.w = math.random(minWidth, maxWidth)
  42.             room.h = math.random(minHeight, maxHeight)
  43.             if room.x > map.width then
  44.              room.x = map.width - room.x
  45.                 roomList.add(room)
  46.                 ok = true
  47.             end
  48.         end
  49.     end
  50.  
  51.     --Connection Area
  52.     connectionCount = roomCount
  53.     connectedCells = {}
  54.     for i = 0, connectionCount do
  55.         roomA = roomList[i]
  56.         roomB = roomList[j]
  57.         -- Increasing this number will make hallways straighter
  58.         --Lowering it will make hallways skewer
  59.         sideStepChance = 10
  60.     pointA = {
  61.         x =(Math.random()*roomA.w) + roomA.x,
  62.         y = (Math.random()*roomA.h) + roomA.y
  63.         }
  64.     pointB = {
  65.         x = (Math.random()*roomB.w) + roomB.x,
  66.         y = (Math.random()*roomB.h) + roomB.y
  67.         }
  68.  
  69.         -- Drunken/Lazy walk algorithm
  70.         while pointB.x ~= pointA.x or pointB.y ~= pointA.y do
  71.         num = math.random(1, 100)
  72.  
  73.         if num < sidestepChance then
  74.             if pointB.x ~= pointA.x then
  75.                 if pointB.x > pointA.x then
  76.                     pointB.x = pointb.X - 1
  77.                 else
  78.                     pointB.x = pointb.X + 1
  79.                 end
  80.             end
  81.         elseif pointB.y ~= pointA.y then
  82.  
  83.             if pointB.y > pointA.y then
  84.                 pointB.y = pointB.y - 1
  85.             else
  86.                 pointB.y = pointB.y + 1;
  87.             end
  88.         end
  89.     end
  90.  
  91.  
  92.     -- Create map data
  93.  
  94.     for i = 0, roomList.length do
  95.         for y = roomList[i].y, roomList[i].y + roomList[i].h do
  96.             for x = roomList[i].x, x < roomList[i].x + roomList[i].w do
  97.                 -- console.log(y + " : " + x);
  98.                 map[y][x] = 1;
  99.             end
  100.         end
  101.     end
  102.  
  103.  
  104.     for i = 0, connectedCells.length do
  105.       map[connectedCells[i].y][connectedCells[i].x] = 2;
  106.     end
  107.  
  108.     for y = 0, y < height do
  109.         for x= 0, x < width do
  110.             if map[y][x] == 0 then
  111.                 wall = false;
  112.                 for  yy = y-2, yy < y+2 do
  113.                     for xx = x-2, xx < x+2 do
  114.                         if xx > 0 and yy > 0 and xx < width and yy < height then
  115.                             if map[yy][xx] == 1 or map[yy][xx] == 2 then
  116.                                 map[y][x] = 3;
  117.                                 wall = true;
  118.                             end
  119.                         end
  120.                     end
  121.                 if(wall) then
  122.                     break
  123.                 end
  124.                 end
  125.             end
  126.         end
  127.     end
  128.     end
  129. end
  130.  
  131. function love.update(dt)
  132.     if love.keyboard.isDown("d") then -- Right movement
  133.         player.x = player.x + (player.speed * dt)
  134.     elseif love.keyboard.isDown("a") then -- Left Movement
  135.         player.x = player.x - (player.speed * dt)
  136.     end
  137.  
  138.     if love.keyboard.isDown("s") then -- Down movement
  139.         player.y = player.y + (player.speed * dt)
  140.     elseif love.keyboard.isDown("w") then -- Up movement
  141.             player.y = player.y - (player.speed * dt)
  142.     end
  143. end
  144.  
  145. function love.draw()
  146.     love.graphics.setBackgroundColor(0,0,0)
  147.     love.graphics.rectangle("fill", player.x, player.y, 10, 10)
  148.     for  y = 0, #map[x] do
  149.         for x = 0, #map[y] do
  150.             if map[y][x] == 0 then
  151.             elseif map[y][x] == 1 then
  152.                     love.graphics.rectangle("fill", map[x], map[y])
  153.             elseif map[y][x] == 2 then
  154.                     love.graphics.rectangle("fill", map[x], map[y])
  155.             elseif map[y][x] ==  3 then
  156.                     love.graphics.rectangle("fill", map[x], map[y])
  157.             end
  158.         end
  159.     end
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement