Template:TerrainBuilder3DRbxTerrainGen

-- Variables Seed = 1 MaxY = 64 / 16 _G.TerrainBuilderFunctions = { }

local TBFunctions = _G.TerrainBuilderFunctions TBFunctions.Print = { } local TBPrint = TBFunctions.Print

TBFunctions.TerrainGeneration = { } local TBTerrain = TBFunctions.TerrainGeneration

-- Prints table contents. function PrintTable(t) for key,value in pairs(t) do     print(key,value) end end local PrintTable = PrintTable TBPrint.PrintTable = PrintTable

-- Returns the name of the block Id. function BIDN(ID) return TableBlockName[ID] end TBPrint.BIDN = BIDN

-- Prints table contents using a table refrence. function PrintTableVRef(Table,Ref) print("Table "..tostring(Table)) for key,value in pairs(Table) do     print(key, BIDN(value)) end end local PrintTableVRef = PrintTableVRef TBPrint.PrintTableVRef = PrintTableVRef

-- Print Block function PrintBlock(TableChunk, TableBlock) PrintTableVRef(value, TableBlockName) print("Block "..key) end local PrintBlock = PrintBlock TBPrint.PrintBlock = PrintBlock

--Print the chunk function PrintTChunk(Table) print("Chunk "..tostring(Table)) for YKey, YValue in pairs(Table) do     print("Block Y: ".. YKey, YValue) for XKey, XValue in pairs(Table) do        print("Block X: "..XKey,XValue) for ZKey, ZValue in pairs(Table) do           print("Block Z: "..ZKey,ZValue) print("ID: " .. (Table[YKey][XKey][ZKey]["ID"]) .. " Name: " .. BIDN((Table[YKey][XKey][ZKey]["ID"]))) PrintTable((Table[YKey][XKey][ZKey])) end end end end local PrintTChunk = PrintTChunk TBPrint.PrintTChunk = PrintTChunk

-- Actual Function used to print a chunk function PrintChunk(x, y, z)  PrintTChunk(ChunkTable[y][x][z]) end TBPrint.PrintChunk = PrintChunk

function PrintFlatChunk(Table,YKey) print("Chunk "..tostring(Table)) for XKey, XValue in pairs(Table) do     -- print("Block X: "..XKey,XValue) local PrintString = ">" for ZKey, ZValue in pairs(Table) do        local ConCat = Table[YKey][XKey][ZKey]["ID"] if ConCat == 0 then PrintString = PrintString.."    " elseif ConCat == 1 then PrintString = PrintString.."SS" elseif ConCat == 2 then PrintString = PrintString.."GG" elseif ConCat == 3 then PrintString = PrintString.."ss" end -- print("Block Z: "..ZKey,ZValue) -- print("ID: " .. (Table[YKey][XKey][ZKey]["ID"]) .. " Name: " .. BIDN((Table[YKey][XKey][ZKey]["ID"]))) end print(PrintString) end end TBPrint.PrintFlatChunk = PrintFlatChunk

-- Returns TableBlock in Chunk using TableChunk, X, Y, and Z. function GetBlockChunk(TableChunk,BX,BY,BZ) return TableChunk[BY][BX][BZ] end

-- Functions to generate Blocks. function BlAir local Air = { ["ID"] = 0, ["DataValue"] = 0 }  return Air end

function BlStone local Stone = { ["ID"] = 1, ["DataValue"] = 0 }  return Stone end

function BlDirt local Dirt = { ["ID"] = 2, ["DataValue"] = 0 }  return Dirt end

function BlGrass local Grass = { ["ID"] = 2, ["DataValue"] = 15 }  return Grass end

function BlSand local Sand = { ["ID"] = 3, ["DataValue"] = 0 }  return Sand end

-- Table for the Block Names. TableBlockName= { [00] = "Air", [01] = "Stone", [02] = "Dirt", [03] = "Sand", [04] = "ComputerBlock", [05] = "Iron Ore", [06] = "Iron Block", [07] = "Water", [08] = "Tree Log", [09] = "Wooden Planks", [10] = "Tree", [11] = "Leaves", [12] = "Chest", [13] = "BlockLogic", [14] = "Block 14", [15] = "Block 15", [16] = "Block 16", [17] = "Block 17", [18] = "Block 18", [19] = "Block 19", [20] = "Block 20", [21] = "Block 21", [22] = "Block 22", [23] = "Block 23", [24] = "Block 24", [25] = "Block 25", [26] = "Block 26", [27] = "Block 27", [28] = "Block 28", [29] = "Block 29", [30] = "Block 30", [31] = "Block 31" } local TableBlockName = TableBlockName

--- -- Chunk Test -- ---

-- X=Width, Y=Height, Z=Depth

TableMap = { } local TableMap=TableMap

TableHeightMap = { } local TableHeightMap=TableHeightMap

-- ChunkTable TableMap.ChunkTable = { } ChunkTable = TableMap.ChunkTable

-- Gets Value of X,Y,Z function GetBlockFromValue(BlockX,BlockY,BlockZ) local Block local BlockToGen = math.random(1,2) if BlockToGen == 1 then Block = BlDirt elseif BlockToGen == 2 then Block = BlGrass end return Block end

-- Generates a Chunk Height Map. function GenHeightMap(x,y,z) local A, HeiMap = pcall(function     return TableHeightMap[y][x][z]   end) if HeiMap == nil or type(HeiMap) == "string" then HeiMap = NewChunkMap(x,y,z) else return HeiMap end local GenSeed = Seed math.randomseed(GenSeed) local MaxHeightY = MaxY * 16 local MaxNoise = (2 * (MaxY / 4)) local OldHeight = ((48 / 64) * MaxHeightY) for XKey=1, 16 do     HeiMap[XKey] = { }     local XTable = HeiMap[XKey] PrintTable(XTable) for ZKey=1, 16 do        --print("Y: "..YKey.." )         XTable[ZKey] = math.random(OldHeight - MaxNoise, OldHeight + MaxNoise)         -- print("X: "..XKey.." Z: "..ZKey.." Height: "..XTable[ZKey])      end   end   return HeiMap end

-- Generates a Chunk. function GenChunk(x,y,z,Table) local HMT = GenHeightMap(x,y,z) for YKey=1, 16 do     Table[YKey] = { }     local YTable = Table[YKey] for XKey=1, 16 do

YTable[XKey] = { }        local XTable = YTable[XKey] for ZKey=1, 16 do           -- print("Y: "..YKey.." X: "..XKey.." Z: "..ZKey) -- print("HMT Value: ".. HMT[XKey][ZKey]) local HMTValue = HMT[XKey][ZKey] XTable[ZKey] = { }           -- PrintTable(XTable[ZKey]) local YValue = (YKey + (16*y) - 16) if (YValue > (HMTValue - 4)) and (YValue <= (HMTValue)) then -- print("YValue: "..YValue.."<".. HMTValue) -- print("Surface Block") XTable[ZKey] = BlGrass elseif (YValue > (HMTValue)) then XTable[ZKey] = BlAir else XTable[ZKey] = BlStone end end end end return Table end TBTerrain.GenChunk = GenChunk

-- Creates a Chunk. function NewChunk(x, y, z)  -- Chunk Y Pos ChunkTable[y] = { }  -- Chunk X Pos ChunkTable[y][x] = { }  -- Chunk Z Pos ChunkTable[y][x][z] = { }  local TableChunk = ChunkTable[y][x][z] return TableChunk end TBTerrain.NewChunk = NewChunk

function NewChunkMap(x, y, z)  -- Chunk Y Pos TableHeightMap[y] = { }  -- Chunk X Pos TableHeightMap[y][x] = { }  -- Chunk Z Pos TableHeightMap[y][x][z] = { }  local TableChunk = TableHeightMap[y][x][z] print(TableChunk) return TableChunk end TBTerrain.NewChunkMap = NewChunkMap

--