This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DannySMc's profile picture

Game World Render/Generator Problems

Started by DannySMc, 04 February 2015 - 02:16 PM
DannySMc #1
Posted 04 February 2015 - 03:16 PM
Hi guys,

I have a question:
I am making a game and I am a little stuck. I have a map renderer program, as well as a random map generator, Everytime you move the map will redraw the map and your position. The thing is if a map doesn't exist for the sector you are in then it will generate one (to make the world endless); which means everytime I move on a generated world, it will re-generate the map and then draw it, meaning that the map will always change everytime you press a key. Please help :(/>

Here is the main game function:


function game.start()
  if not player.pos.current then
    player.pos.current = "space1x1"
  end
  game.drawstats()
  game.drawmap(player.pos.current)
  game.drawplayer()

  while true do
    local args = { os.pullEvent() }
    game.input(args[1], args[2], args[3], args[4], args[5])
    game.drawstats()
    game.drawmap(player.pos.current)
    game.drawplayer()
  end
end

Here is the game's map renderer:


function game.drawmap(name)
  col.set("white", "black")
  name = name
  if fs.exists("/data/maps/"..name..".lua") == false then
    map = game.worldgen(name)
  else
    local map1 = fs.open("/data/maps/"..name..".lua", "r")
    map = textutils.unserialize(map1.readAll())
  end
  intx = 2
  inty = 2
  for k1, v1 in ipairs(map) do
    for k2, v2 in ipairs(map[k1]) do
      term.setCursorPos(k2, k1)
      write(v2)
    end
  end
end

and here is the game's map generator (I have deleted the content in the itemset1 as this is just to add the content in to the map and it is huge like 70 lines, so I have taken it out due to space):


function game.worldgen(name)
  itemset1 = {}

  key = 1
  world = {{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},}
  for i = 1, 19 do
    for j = 1, 35 do
      local ran = math.floor(math.random(1000))
      table.insert(world[key], itemset1[ran])
    end
    key = key + 1
  end
  return world
end

Thanks in advance
Bomb Bloke #2
Posted 04 February 2015 - 10:56 PM
I don't get it. Why don't you simply save each new map to disk after generating them? Better yet, why not keep them in RAM after loading, so that you're not hammering the disk drive?

Eg:

local map = {}

function game.drawmap(name)
  col.set("white", "black")
  name = name  --# I'm really curious as to what you think this is supposed to do.
  if not map[name] then
    if not fs.exists("/data/maps/"..name..".lua") then
      map[name] = game.worldgen(name)
      local map1 = fs.open("/data/maps/"..name..".lua", "w")
      map1.writeLine(textutils.serialize(map[name]))
      map1.close()
    else
      local map1 = fs.open("/data/maps/"..name..".lua", "r")
      map[name] = textutils.unserialize(map1.readAll())
      map1.close()  --# Don't forget to close your file handles!
    end
  end
  .
  .
  .
DannySMc #3
Posted 05 February 2015 - 09:28 AM
I don't get it. Why don't you simply save each new map to disk after generating them? Better yet, why not keep them in RAM after loading, so that you're not hammering the disk drive?

Eg:

local map = {}

function game.drawmap(name)
  col.set("white", "black")
  name = name  --# I'm really curious as to what you think this is supposed to do.
  if not map[name] then
	if not fs.exists("/data/maps/"..name..".lua") then
	  map[name] = game.worldgen(name)
	  local map1 = fs.open("/data/maps/"..name..".lua", "w")
	  map1.writeLine(textutils.serialize(map[name]))
	  map1.close()
	else
	  local map1 = fs.open("/data/maps/"..name..".lua", "r")
	  map[name] = textutils.unserialize(map1.readAll())
	  map1.close()  --# Don't forget to close your file handles!
	end
  end
  .
  .
  .

Yeah okay, and it's just code I have left in there, will delete when I get a chance, and yeah I guess