private void SetChunks(ChunkData[,] chunks) { int regX = Mathf.CeilToInt(((float)chunks.GetLength(0)) / World.RegionSize); int regZ = Mathf.CeilToInt(((float)chunks.GetLength(1)) / World.RegionSize); for (int rx = 0; rx < regX; rx++) { for (int rz = 0; rz < regZ; rz++) { ChunkData[,] cd = new ChunkData[World.RegionSize, World.RegionSize]; for (int x = 0; x < World.RegionSize; x++) { for (int z = 0; z < World.RegionSize; z++) { int x_ = rx * World.RegionSize + x; int z_ = rz * World.RegionSize + z; if (x < chunks.GetLength(0) && z < chunks.GetLength(1)) { cd[x, z] = chunks[x_, z_]; } } } ChunkRegion cr = new ChunkRegion(rx, rz, cd); WorldManager.Instance.CRManager.LoadedRegions[rx, rz] = cr; } } }
public ArrayBlockVboMapper(ChunkRegion chunkRegion, VertexBufferObject vbo, VertexArrayObject vao, int elementSize, int maximumAmount, BufferTarget bufferTarget = BufferTarget.ArrayBuffer) { _chunkRegion = chunkRegion; _regionWorldPosition = chunkRegion.Position << World2dRegion.WorldPositionShift; _vbo = vbo; _vao = vao; _array = new float[elementSize * maximumAmount]; _elementSize = elementSize; _bufferTarget = bufferTarget; _amount = 0; _maximumAmount = maximumAmount; _updates = 0; _onBackground = false; _requiresResize = false; _tasks = new ConcurrentQueue <VboMapperTask <Block> >(); _offsets = new int[ChunkRegion.RegionLength, ChunkRegion.RegionLength, ChunkRegion.RegionLength]; _keys = new Dictionary <int, Vector3i>(); for (var x = 0; x < ChunkRegion.RegionLength; x++) { for (var y = 0; y < ChunkRegion.RegionLength; y++) { for (var z = 0; z < ChunkRegion.RegionLength; z++) { _offsets[x, y, z] = -1; } } } }
private void ThreadGenerateRemainingRegionsInternal() { bool isComplete = false; while (!isComplete) { Vec2i toGen = null; //Use lock for thread safty (though this should only be accessed by this thread, so should be safe lock (LOCK_OBJ) { if (RegionsToGen.Count == 0) { isComplete = true; } else { toGen = RegionsToGen[0]; RegionsToGen.RemoveAt(0); Debug.Log("Generating region " + toGen + ". " + RegionsToGen.Count + " remaining", Debug.WORLD_GENERATION); } } if (toGen != null) { ChunkRegion cr = GenerateRegion(toGen.x, toGen.z); GameManager.LoadSave.SaveChunkRegion(cr); } } }
public TorchBlockRender(ChunkRegion chunkRegion) { _chunkRegion = chunkRegion; _vao = null; _dataBuffer = null; _mapper = new ArrayBlockVboMapper(chunkRegion, null, null, InstanceDataLength, MaxBlocks); _generated = false; }
private void CreateWorld() { World world = new World(); //DungeonGenerator dugeonGenerator = new DungeonGenerator(); //dungeon = dugeonGenerator.GenerateDungeon(); ChunkRegion r = new ChunkRegion(0, 0, dungeon.SubworldChunks); WorldManager.SetWorld(world); WorldManager.LoadedRegions[0, 0] = r; }
void Awake() { LoadedChunks = new Dictionary <Vec2i, LoadedChunk>(); LoadedRegions = new ChunkRegion[World.RegionCount, World.RegionCount]; SubworldChunks = new Dictionary <Vec2i, LoadedChunk>(); ToLoadIn = new List <ChunkData>(); ToLoad = new List <Vec2i>(); InSubworld = false; ThreadSafe = new Object(); }
public SlabBlockRender(ChunkRegion chunkRegion) { _chunkRegion = chunkRegion; _vaos = new VertexArrayObject[6]; _dataBuffers = new VertexBufferObject[6]; _mappers = new VboMapper <Block> [6]; _generated = false; foreach (var face in BlockFaceMethods.All) { _mappers[(int)face] = new ArrayBlockVboMapper(chunkRegion, null, null, InstanceDataLength, MaxFaces); } }
void Awake() { Instance = this; ToGetLoadedChunks = new Dictionary <ChunkData, int>(); LoadedChunks = new Dictionary <Vec2i, LoadedChunk2>(); LoadedRegions = new ChunkRegion[World.RegionCount, World.RegionCount]; ToUnloadChunks = new List <Vec2i>(); SubworldChunks = new Dictionary <Vec2i, LoadedChunk2>(); ThreadSafe = new Object(); ChunkLoader = GetComponent <ChunkLoader>(); }
public ChunkRegion LoadChunkRegion(int x, int z) { string filePath = PersistentDataPath + "/" + SaveFile + "/ChunkRegions/" + (x + "_" + z) + ".save"; if (File.Exists(filePath)) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(filePath, FileMode.Open); Debug.Log("Loading region " + x + "_" + z, Debug.CHUNK_LOADING); ChunkRegion r = (ChunkRegion)bf.Deserialize(file); file.Close(); return(r); } return(null); }
public void SaveChunkRegion(ChunkRegion cr) { string file = PersistentDataPath + "/" + SaveFile + "/ChunkRegions/" + (cr.X + "_" + cr.Z) + ".save"; string direction = PersistentDataPath + "/" + SaveFile + "/ChunkRegions"; Directory.CreateDirectory(direction); //File.Cre //UnityEngine.Profiling.Profiler.BeginSample("serialise"); Debug.Log("Saving region " + cr.X + "_" + cr.Z, Debug.WORLD_GENERATION); BinaryFormatter bf = new BinaryFormatter(); FileStream fs = File.Create(file); bf.Serialize(new BufferedStream(fs), cr); fs.Close(); //UnityEngine.Profiling.Profiler.EndSample(); }
/// <summary> /// Generates a map of moveable tiles based on the given region /// </summary> /// <param name="chunkRegion"></param> public void LoadRegion(ChunkRegion chunkRegion) { Debug.Log("[PathFinding] Chunk Region " + chunkRegion.X + "," + chunkRegion.Z + " loading"); //Get region position & generate empty array Vec2i rPos = new Vec2i(chunkRegion.X, chunkRegion.Z); float[,] tileData = new float[RegionTileSize, RegionTileSize]; //Iterate whole region and fill array as required //TODO - see if there is a quicker way to do this... for (int cx = 0; cx < World.RegionSize; cx++) { for (int cz = 0; cz < World.RegionSize; cz++) { for (int x = 0; x < World.ChunkSize; x++) { for (int z = 0; z < World.ChunkSize; z++) { ChunkData c = chunkRegion.Chunks[cx, cz]; if (c == null) { Debug.Error("Chunk Region " + chunkRegion.ToString() + " has null chunk with local: " + cx + "," + cz); continue; } float val = tileData[cx * World.ChunkSize + x, cz *World.ChunkSize + z] = c.GetTile(x, z).SpeedMultiplier; //TODO - update this bad boy. /* * if (c.Objects != null && c.GetObject(x, z) != null) * { * WorldObjectData wod = c.GetObject(x, z); * if (wod.IsCollision) * val = Mathf.Infinity; * }*/ tileData[cx * World.ChunkSize + x, cz *World.ChunkSize + z] = val; } } } } RegionTileValues.Add(rPos, tileData); }
public override BlockRender CreateBlockRender(ChunkRegion chunkRegion) { return(new SlabBlockRender(chunkRegion)); }
private void GenerateWorld3() { World world = new World(); //Create chunk bases ChunkBase[,] chunk_b = new ChunkBase[World.RegionSize, World.RegionSize]; ChunkData[,] chunks = new ChunkData[World.RegionSize, World.RegionSize]; River r = new River(); r.SetFirstChunk(new Vec2i(2, 2), 6); r.AddChunk(new Vec2i(3, 2), 6); r.AddChunk(new Vec2i(4, 2), 6); r.AddChunk(new Vec2i(4, 3), 6); r.AddChunk(new Vec2i(5, 3), 6); /* * r.AddChunk(new Vec2i(3,2), 6); * r.AddChunk(new Vec2i(3, 3), 6); * r.AddChunk(new Vec2i(4, 3), 6); * r.AddChunk(new Vec2i(4, 2), 6); * r.AddChunk(new Vec2i(4, 1), 6); * r.AddChunk(new Vec2i(3, 1), 6); * r.AddChunk(new Vec2i(2, 1), 6); * r.AddChunk(new Vec2i(1, 1), 6); */ /* * Vec2i last = new Vec2i(2, 2); * for(int i=0; i<10; i++) * { * int t = (i % 8); * int dx = 0; * int dz = 0; * if (t == 0 || t==1) * dx = 1; * else if (t == 2 || t == 3) * dz = 1; * else if (t == 3 || t == 5) * dx = -1; * else * dz = -1; * //int dx = (i % 2); * //int dz = (i + 1) % 2; * last = last + new Vec2i(dx, dz); * r.AddChunk(last, 6); * }*/ for (int rx = 0; rx < World.RegionSize; rx++) { for (int rz = 0; rz < World.RegionSize; rz++) { chunk_b[rx, rz] = new ChunkBase(new Vec2i(rx, rz), true); } } foreach (KeyValuePair <Vec2i, RiverNode> kvp in r.GenerateRiverNodes()) { chunk_b[kvp.Key.x, kvp.Key.z].AddRiver(kvp.Value); } int[,] emptyLandChunk = new int[World.ChunkSize, World.ChunkSize]; for (int x = 0; x < World.ChunkSize; x++) { for (int z = 0; z < World.ChunkSize; z++) { emptyLandChunk[x, z] = Tile.GRASS.ID; } } for (int rx = 0; rx < World.RegionSize; rx++) { for (int rz = 0; rz < World.RegionSize; rz++) { chunks[rx, rz] = GenerateChunk((int[, ])emptyLandChunk.Clone(), chunk_b[rx, rz]); } } ChunkRegion region = new ChunkRegion(0, 0, chunks); WorldManager.SetWorld(world); WorldManager.LoadedRegions[0, 0] = region; world.SetChunkBases(chunk_b); for (int rx = 0; rx < World.RegionSize - 1; rx++) { for (int rz = 0; rz < World.RegionSize - 1; rz++) { WorldManager.CRManager.LoadChunk(new Vec2i(rx, rz)); } } }
private void CreateWorld2() { World world = new World(); SettlementBase b = new SettlementBase(new Vec2i(8, 8), 8, SettlementType.CAPITAL); SettlementBuilder build = new SettlementBuilder(null, b); build.GenerateSettlement(); /*foreach(SettlementPathNode p in build.nodes) * { * TestNodes.Add(p); * }*/ Tile[,] tiles = build.Tiles; WorldObjectData[,] objs = build.SettlementObjects; int tileSize = build.TileSize; int cSize = tileSize / World.ChunkSize; ChunkData[,] cData = new ChunkData[cSize, cSize]; ChunkBase[,] cBase = new ChunkBase[cSize, cSize]; for (int x = 0; x < cSize; x++) { for (int z = 0; z < cSize; z++) { cBase[x, z] = new ChunkBase(new Vec2i(x, z), true); int[,] cTiles = new int[World.ChunkSize, World.ChunkSize]; WorldObjectData[,] cObj = new WorldObjectData[World.ChunkSize, World.ChunkSize]; Dictionary <int, WorldObjectData> wObjData = new Dictionary <int, WorldObjectData>(); for (int x_ = 0; x_ < World.ChunkSize; x_++) { for (int z_ = 0; z_ < World.ChunkSize; z_++) { if (tiles[x * World.ChunkSize + x_, z *World.ChunkSize + z_] != null) { cTiles[x_, z_] = tiles[x * World.ChunkSize + x_, z *World.ChunkSize + z_].ID; } else { cTiles[x_, z_] = Tile.GRASS.ID; } if (objs[x * World.ChunkSize + x_, z *World.ChunkSize + z_] != null) { wObjData.Add(WorldObject.ObjectPositionHash(x_, z_), objs[x * World.ChunkSize + x_, z * World.ChunkSize + z_]); } // cObj[x_, z_] = objs[x * World.ChunkSize + x_, z * World.ChunkSize + z_]; } } cData[x, z] = new ChunkData(x, z, cTiles, true, wObjData); } } ChunkRegion r = new ChunkRegion(0, 0, cData); WorldManager.LoadedRegions[0, 0] = r; Kingdom k = new Kingdom("test", new Vec2i(0, 0)); k.SetKingdomID(world.AddKingdom(k)); Settlement set = new Settlement(k, "test_set", build); set.SetSettlementID(world.AddSettlement(set)); WorldManager.SetWorld(world); KingdomNPCGenerator npcGen = new KingdomNPCGenerator(new GameGenerator(0), k, EntityManager); Debug.Log("BASE:" + set.BaseCoord); Debug.Log(cSize); npcGen.GenerateSettlementNPC(set); world.SetChunkBases(cBase); for (int x = 0; x < cSize - 1; x++) { for (int z = 0; z < cSize - 1; z++) { //LoadChunk(cData[x, z]); WorldManager.CRManager.LoadChunk(new Vec2i(x, z)); EntityManager.LoadChunk(null, new Vec2i(x, z)); } } /* * DungeonGenerator dugeonGenerator = new DungeonGenerator(); * dungeon = dugeonGenerator.GenerateDungeon(); * ChunkRegion r = new ChunkRegion(0, 0, dungeon.SubworldChunks); * world.LoadedRegions.Add(new Vec2i(0, 0), r); * WorldManager.SetWorld(world);*/ //world.Set }
public abstract BlockRender CreateBlockRender(ChunkRegion chunkRegion);
public override BlockRender CreateBlockRender(ChunkRegion chunkRegion) { return(new TallGrassBlockRender(chunkRegion)); }