I've made 5 functions. "genStrip" and "genWorld" generate a random 100x100 world, "makeTile" creates a tile kind with the desired properties and "renderTile" and "renderWorld" draws a tile acording to the player position (so I made a temporary value for this).
The problem is that when I use renderWorld it says that the world table is nil.
Code:
Spoiler
world={}
world.config={} --[1]=difficulty [2]=time [3]=season
world.tiles={}
world.tiles.blocks={} -- works like [(layer)][(y)][(x)]=[tile id]
world.tiles.blocks[0]={}
world.tiles.blocks[1]={}
world.tiles.blocks[2]={}
world.tiles.blocks[3]={}
world.tiles.blocks[4]={}
world.tiles.objects={} -- works like [(layer)][(y)][(x)]=[object id]
world.player={}
world.player.inv={} --works like [(slot number)][1]=item id ,[(slot number)][2]=item durability ,[(slot number)][3]=item ammount
world.player.stats={} --[1]=health [2]=hunger [3]=thirst [4][1]=xpos [4][2]=ypos [5]=score [6]=layer
tiles={}
items={}
objects={}
monsters={}
--Tile config:
function makeTile(id,name,char,bgcolor,txtcolor,durability,isafloor,specialtype,toolneeded,droppedid,dropsitem,haschance,chancedroppeditem,canbedigged,idwhendigged,chanceditemwhendigged,whendiggeditemid)
tiles[id]={}
tiles[id][1]=name
if char==nil then
tiles[id][2]=" "
else
tiles[id][2]=char
end
tiles[id][3]=bgcolor
tiles[id][4]=durability --make this a random value or even nil if the "toolneeded" is 0 (indistructible)
tiles[id][5]=isafloor
tiles[id][6]=specialtype
tiles[id][7]=toolneeded --0:cant be destroyed 1:hands and all others 2:pickaxe 3:shovel
if dropsitem==true then
tiles[id][8]=items[droppedid]
else
tiles[id][8]=tiles[droppedid]
end
if haschance==true then
tiles[id][9]=items[chancedroppeditem]
else
tiles[id][9]=nil
end
tiles[id][10]=txtcolor
if chanceditemwhendigged==true then
tiles[id][11]=items[whendiggeditemid]
else
tiles[id][11]=nil
end
end
makeTile(0,"Air",nil,colors.lightBlue,colors.lightBlue,0,false,0,0,nil,false,false,nil,false,nil,false,nil)
makeTile(1,"Grass","'",colors.lime,colors.green,0,true,1,3,2,false,false,nil,true,2,true,7)
makeTile(2,"Dirt","'",colors.brown,colors.lightGray,0,true,1,3,2,false,false,nil,true,21,false,nil)
makeTile(3,"Stone","L",colors.gray,colors.lightGray,0,false,0,2,1,true,true,2,false,nil,false,nil)
makeTile(4,"Flowers","*",colors.lime,colors.yellow,0,true,2,1,4,false,false,nil,true,1,true,7)
makeTile(5,"Tree","O",colors.lime,colors.green,0,false,0,1,3,true,true,8,false,nil,false,nil)
makeTile(6,"Water","~",colors.blue,colors.lightBlue,0,true,3,0,nil,false,false,nil,false,nil,false,nil)
makeTile(7,"Iron","*",colors.gray,colors.brown,1,false,0,2,4,true,false,nil,false,nil,false,nil)
makeTile(8,"Gold","*",colors.gray,colors.yellow,2,false,0,2,5,true,false,nil,false,nil,false,nil)
makeTile(9,"Gem","*",colors.gray,colors.pink,3,false,0,2,6,true,false,nil,false,nil,false,nil)
makeTile(10,"Lava","~",colors.orange,colors.red,0,true,4,0,nil,false,false,nil,false,nil,false,nil)
makeTile(11,"Obsidian","L",colors.purple,colors.black,4,false,0,2,1,true,true,2,false,nil,false,nil)
makeTile(12,"Cloud",nil,colors.white,colors.white,0,true,0,0,nil,false,false,nil,false,nil,false,nil)
makeTile(13,"Spikes","*",colors.white,colors.lightGray,4,false,0,2,nil,false,false,nil,false,nil,false,nil)
makeTile(14,"Upstair","^",colors.lightGray,colors.gray,0,true,5,0,nil,false,false,nil,false,nil,false,nil)
makeTile(15,"Downstair","V",colors.lightGray,colors.gray,0,true,6,0,nil,false,false,nil,false,nil,false,nil)
makeTile(16,"Farmland","=",colors.brown,colors.lightBlue,0,true,7,0,nil,false,false,nil,true,2,false,nil)
makeTile(17,"FarmSeeds","=",colors.brown,colors.lime,0,true,8,0,7,true,false,nil,true,16,false,nil)
makeTile(18,"FarmWheat","W",colors.brown,colors.yellow,0,true,0,0,9,true,false,nil,true,16,true,9)
makeTile(19,"Hole","O",colors.brown,colors.black,0,false,9,0,nil,false,false,nil,false,nil,false,nil)
makeTile(20,"Sand","~",colors.yellow,colors.yellow,0,true,10,3,20,false,false,nil,true,1,false,nil)
makeTile(21,"Cactus","O",colors.yellow,colors.green,0,false,0,1,21,false,false,nil,false,nil,false,nil)
--Internal
function genStrip(layer,y)
world.tiles.blocks[layer][y]={}
for x = 1, 100 do
if layer==0 then
local terrain=math.random(1,3)
elseif layer==1 then
local terrain=math.random(1,7)
elseif layer==2 then
local terrain=math.random(1,3)
elseif layer==3 then
local terrain=math.random(1,4)
elseif layer==4 then
local terrain=math.random(1,4)
end
if layer==0 then
if terrain==1 then
world.tiles.blocks[layer][y][x]=0
elseif terrain==2 then
world.tiles.blocks[layer][y][x]=12
elseif terrain==3 then
world.tiles.blocks[layer][y][x]=13
end
elseif layer==1 then
if terrain==1 then
world.tiles.blocks[layer][y][x]=1
elseif terrain==2 then
world.tiles.blocks[layer][y][x]=3
elseif terrain==3 then
world.tiles.blocks[layer][y][x]=4
elseif terrain==4 then
world.tiles.blocks[layer][y][x]=5
elseif terrain==5 then
world.tiles.blocks[layer][y][x]=6
elseif terrain==6 then
world.tiles.blocks[layer][y][x]=20
elseif terrain==7 then
world.tiles.blocks[layer][y][x]=21
end
elseif layer==2 then
if terrain==1 then
world.tiles.blocks[layer][y][x]=1
elseif terrain==2 then
world.tiles.blocks[layer][y][x]=3
elseif terrain==3 then
world.tiles.blocks[layer][y][x]=7
end
elseif layer==3 then
if terrain==1 then
world.tiles.blocks[layer][y][x]=1
elseif terrain==2 then
world.tiles.blocks[layer][y][x]=3
elseif terrain==3 then
world.tiles.blocks[layer][y][x]=8
elseif terrain==4 then
world.tiles.blocks[layer][y][x]=6
end
elseif layer==4 then
if terrain==1 then
world.tiles.blocks[layer][y][x]=1
elseif terrain==2 then
world.tiles.blocks[layer][y][x]=3
elseif terrain==3 then
world.tiles.blocks[layer][y][x]=9
elseif terrain==4 then
world.tiles.blocks[layer][y][x]=10
end
end
end
end
function genWorld()
for currenty = 1,100 do
genStrip(0,currenty)
genStrip(1,currenty)
genStrip(2,currenty)
genStrip(3,currenty)
genStrip(4,currenty)
end
end
function renderTile(layer,y,x)
local rid=world.tiles.blocks[layer][y][x]
term.setTextColor(tiles[rid][10]) <<<<<<<<<<<<<<<<<<<<<<<<--The program crashes here
term.setBackgroundColor(tiles[rid][3])
if tiles[rid][2]==nil then
term.write(" ")
else
term.write(tiles[rid][2])
end
end
function renderWorld(playerlayer,playery,playerx)
for y = 0, 17 do
local rendery=(playery-8)+y
for x = 0, 49 do
local renderx=(playerx-24)+x
renderTile(playerlayer,rendery,renderx)
end
end
end
genWorld()
renderWorld(1,10,10)
Error:
minicraft:156: attempt to index ? (a nil value)
Help is apreciated,
Jakos
Edit:RickiHN nailed