Advertisement
GauHelldragon

mcrawl-map.lua

Dec 14th, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.81 KB | None | 0 0
  1. local map = {
  2.     tiles = {},
  3.     revealmap = {},
  4.    
  5.     max_x = 50,
  6.     max_y = 50,
  7.     max_rooms = 10,
  8.     rooms = {},
  9.     floorItems = {}
  10.    
  11. }
  12.  
  13.  
  14. -- MAP GENERATION
  15.  
  16. local function drawSquare(x,y,x2,y2)
  17.    xadder = 1
  18.    yadder = 1
  19.    if ( x > x2 ) then xadder = -1 end
  20.    if ( y > y2 ) then yadder = -1 end
  21.  
  22.    for mx = x,x2,xadder do
  23.       for my = y,y2,yadder do
  24.          if ( my >= 1 and my <= map.max_y and mx >= 1 and mx <= map.max_x ) then
  25.             if ( math.random() > 0.1 ) then map.tiles[mx][my] = "."
  26.             else map.tiles[mx][my] = "," end
  27.          end
  28.       end
  29.    end
  30. end
  31.  
  32. local function doRoomsIntersect(room1,room2)
  33.    if ( room1.x-2 < room2.x2 and room1.x2+2 > room2.x and room1.y-2 < room2.y2 and room1.y2+2 > room2.y ) then return true end
  34.    return false
  35. end
  36.  
  37. local function isRoomOK(room)
  38.    for i,croom in pairs(map.rooms)    do
  39.      
  40.       if ( doRoomsIntersect(room,croom)) then return false end
  41.    end
  42.    return true
  43. end
  44.  
  45.  
  46. local function drawRoom(room)
  47.    x = room.x
  48.    y = room.y
  49.    x2 = room.x2
  50.    y2 = room.y2
  51.    drawSquare(x,y,x2,y2)
  52. end
  53.  
  54.  
  55. local function newRoom()
  56.    local width = math.random(4,8)
  57.    local height = math.random(4,8)
  58.    local mx = math.random(2,map.max_x-width-1)
  59.    local my = math.random(2,map.max_y-height-1)
  60.    room = {
  61.       x = mx,
  62.       y = my,
  63.       w = width,
  64.       h = height,
  65.       x2 = mx + width - 1,
  66.       y2 = my + height - 1
  67.    }
  68.    return room
  69. end
  70.  
  71. function map.getRandomSpot(room)
  72.    local x = room.x + math.random(0,room.w-1)
  73.    local y = room.y + math.random(0,room.h-1)
  74.    return x,y
  75. end
  76.  
  77. local function drawPath(room1,room2)
  78.    startX, startY = map.getRandomSpot(room1)
  79.    endX, endY = map.getRandomSpot(room2)
  80.    
  81.    if ( math.random() > 0.5 ) then
  82.       drawSquare(startX,startY,endX,startY)
  83.       drawSquare(endX,startY,endX,endY)
  84.    else
  85.       drawSquare(startX,startY,startX,endY)
  86.       drawSquare(startX,endY,endX,endY)
  87.    end
  88.  
  89. end
  90.  
  91. local function revealRoom(room)
  92.    room.revealed = true
  93.    for x=room.x-1,room.x2+1 do
  94.       for y=room.y-1,room.y2+1 do
  95.          map.revealMap[x][y] = 1
  96.       end
  97.    end
  98. end
  99.  
  100.  
  101. local function revealTunnel(player)
  102.    for x=player.x-1,player.x+1 do
  103.       for y=player.y-1,player.y+1 do
  104.          map.revealMap[x][y] = 1
  105.       end
  106.    end
  107. end  
  108.  
  109. function map.getPlayerRoom(player)
  110.    for i,room in pairs(map.rooms) do
  111.       if ( player.x >= room.x and player.x <= room.x2 and player.y >= room.y and player.y <= room.y2 ) then return room end
  112.    end
  113.    return 0
  114. end
  115.  
  116.  
  117. function map.changeReveal(player)
  118.    local playerRoom = map.getPlayerRoom(player)
  119.    if ( playerRoom ~= 0 ) then
  120.       if ( playerRoom.revealed ~= true ) then
  121.          revealRoom(playerRoom)
  122.       end
  123.    else
  124.       revealTunnel(player)
  125.    end
  126. end
  127.  
  128.  
  129. function map.generateMap()
  130.    map.tiles = {}
  131.    map.revealMap = {}
  132.    for x=1,map.max_x do  -- Clear Map
  133.       map.tiles[x] = {}
  134.      map.revealMap[x] = {}
  135.       for y=1,map.max_y do
  136.          map.tiles[x][y] = "#"
  137.        map.revealMap[x][y] = 0
  138.       end
  139.    end
  140.    
  141.    local totalRooms = 0
  142.    for i=1,map.max_rooms do
  143.       local newRoom = newRoom()
  144.       local tries = 1
  145.       while ( not isRoomOK(newRoom) and tries < 10 ) do
  146.          tries = tries + 1
  147.          newRoom = newRoom()
  148.       end
  149.       if ( isRoomOK(newRoom) ) then
  150.       table.insert(map.rooms,newRoom)
  151.       totalRooms = totalRooms + 1
  152.      local ix, iy = map.getRandomSpot(newRoom)
  153.      addNewFloorItem("Apple",ix,iy)
  154.    end
  155.    end
  156.    
  157.    map.max_rooms = totalRooms
  158.    
  159.    startRoom = map.rooms[1]
  160.    player.x, player.y = map.getRandomSpot(startRoom)
  161.    revealRoom(startRoom)
  162.    
  163.    for i=1,map.max_rooms do
  164.      drawRoom(map.rooms[i])
  165.    end
  166.    for i=1,map.max_rooms-1 do
  167.      drawPath(map.rooms[i],map.rooms[i+1])
  168.    end
  169. end
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. return map
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement