Ok, so I tried making a map generator, I also put the t.console in conf.lua to true because I laid down alot of print() functions around the map generator code so I can really make sure that it's working properly, but my problem is some time after the map generation begins then my game will suddenly freeze and not respond so I have to close it
is it because there's too many loops? code:
Code: Select all
-- Variables
local generating = false
local gen_default = false
local TILES = wazmb.ALLTILES
local done = false
local lmt = love.math
wazmb.MAP = {}
wazmb.MAP.X = 0
wazmb.MAP.Y = 0
-----
local function newBuilding(x, y)
TILES[x][y] = {tiletype = 3, tilesetn = 1}
TILES[x+1][y] = {tiletype = 3, tilesetn = 7}
TILES[x+2][y] = {tiletype = 3, tilesetn = 2}
TILES[x][y+1] = {tiletype = 3, tilesetn = 3}
TILES[x+1][y+1] = {tiletype = 3, tilesetn = 4}
TILES[x+2][y+1] = {tiletype = 3, tilesetn = 9}
TILES[x][y+2] = {tiletype = 3, tilesetn = 5}
TILES[x+1][y+2] = {tiletype = 3, tilesetn = 6}
TILES[x+1][y+2] = {tiletype = 3, tilesetn = 8}
end
local function tile_occupied(x, y)
if TILES[x][y].tilesetn ~= 0 then
return true
end
return false
end
local function GENERATE_DEFAULT_TILES(maxx, maxy)
local numx, numy = 0, 0
while gen_default do
local randx, randy = lmt.random(1, maxx), lmt.random(1, maxy)
if numx >= maxx and maxy >= maxy then gen_default = false end
if not tile_occupied(randx, randy) then
TILES[randx][randy] = {tiletype = 1, tilesetn = 1}
numx = numx + 1
numx = numy + 1
print("Placed default tile in "..randx..", "..randy)
end
end
wazmb.ALLTILES = TILES
done = true
print("Map generation done")
end
local function GENERATE_BUILDINGS(maxx, maxy, maxb)
print("Generating buildings...")
local cb = 0
print("1")
while generating do
local rand1 = math.random(1, 100)
local rand2 = math.random(1, 100)
local randx, randy = lmt.random(1, maxx), lmt.random(1, maxy)
if cb >= maxb then generating = false; gen_default = true end
if rand1 - rand2 >= 35 then
newBuilding(randx, randy)
print("Building generated at - "..randx..", "..randy)
cb = cb + 1
end
end
if gen_default then GENERATE_DEFAULT_TILES(maxx, maxy) end
end
function wazmb.GENERATOR_INIT(maxx, maxy, maxb)
print("Generating random map...")
if maxx ~= nil then
if maxx < 200 then maxx = 200 end
end
if maxy ~= nil then
if maxy < 200 then maxy = 200 end
end
local MAP_MAX_X = maxx or lmt.random(200, 500)
local MAP_MAX_Y = maxy or lmt.random(200, 500)
local MAXBUILD = maxb or lmt.random(20, 50)
wazmb.MAINCAM = wazmb.gamera.new(0, 0, MAP_MAX_X, MAP_MAX_Y)
for X = 1, MAP_MAX_X do
TILES[X] = {}
for Y = 1, MAP_MAX_Y do
print("Tile "..X..", "..Y)
TILES[X][Y] = {tiletype = 0, tilesetn = 0}
end
end
generating = true
local ok, err = pcall(GENERATE_BUILDINGS, MAP_MAX_X, MAP_MAX_Y, MAXBUILD)
if err then print(err) end
end
function wazmb.DRAW_MAP()
print("Drawing map...")
for X, map in ipairs(wazmb.ALLTILES) do
for Y, t in ipairs(map) do
local tiletype = wazmb._TILE._TILESET[t.tilesetn].q
local x, y = (X - 1) * wazmb._TILE._TILESIZE, (Y - 1) * wazmb._TILE._TILESIZE
lg.draw(wazmb._TILE._TILE_SET_, tiletype, x, y)
end
end
end
function wazmb.Map_Control()
if done then
local isDown = wazmb.lk.isDown
local cam = wazmb.MAINCAM
local t = {
["up"] = {x = 0, y = 10},
["down"] = {x = 0, y = -10},
["right"] = {x = 10, y = 0},
["left"] = {x = -10, y = 0}
}
if t[isDown] ~= nil then
wazmb.MAP.X = wazmb.MAP.X + t[isDown].x
wazmb.MAP.Y = wazmb.MAP.Y + t[isDown].y
end
cam:setPosition(wazmb.MAP.X, wazmb.MAP.Y)
end
end