private Dictionary <Vec2i, ChunkStructure> GenerateElementalDungeonEntranceStructures() { Dictionary <Vec2i, ChunkStructure> elDunShells = new Dictionary <Vec2i, ChunkStructure>(); //iterate all counts for (int i = 0; i < 4; i++) { //We make5 attempts to find a valid place for each bandit camp for (int a = 0; a < 5; a++) { //Generate random position and size Vec2i position = GenerationRandom.RandomFromList(GameGenerator.TerrainGenerator.LandChunks); Vec2i size = GenerationRandom.RandomVec2i(1, 3); //Check if position is valid, if (IsPositionValid(position)) { //if valid, we add the structure to ChunkBases and to the dictionary of shells ChunkStructure banditCampShell = new BanditCamp(position, size); for (int x = 0; x < size.x; x++) { for (int z = 0; z < size.z; z++) { GameGenerator.TerrainGenerator.ChunkBases[position.x + x, position.z + z].AddChunkStructure(banditCampShell); } } elDunShells.Add(position, banditCampShell); } } } return(elDunShells); }
// Calculates the position of the two mountains required (good & bad dragon locations) // Ensures the two mountains are seperated by a minimum distance /// <summary> /// Calculates the position of the two required mountains (good and bad dragon locations) /// Ensures the two mountains are seperated by a minimum distance '<paramref name="minSep"/>' /// </summary> /// <param name="minSep"></param> /// <returns>An array containing position of two mountains such that /// Vec2i[0] = bad dragon /// Vec2i[1] = good dragon </returns> private Vec2i[] DecideMountainPlacement(int minSep = 356) { Vec2i mid = new Vec2i(World.WorldSize / 2, World.WorldSize / 2); Vec2i badDragonMount = GenRan.RandomVec2i(-World.WorldSize / 3, World.WorldSize / 3) + mid; Vec2i goodDragonMount = GenRan.RandomVec2i(-World.WorldSize / 3, World.WorldSize / 3) + mid; //Ensure they are seperated by at least 356 while (badDragonMount.QuickDistance(goodDragonMount) < minSep * minSep) { goodDragonMount = GenRan.RandomVec2i(-World.WorldSize / 3, World.WorldSize / 3) + mid; } return(new Vec2i[] { badDragonMount, goodDragonMount }); }
public Vec2i GetFreePoint() { Vec2i toOut = GenerationRandom.RandomVec2i(1, TileSize - 2); //Vec2i toOut = new Vec2i(MiscMaths.RandomRange(1, TileSize - 2), MiscMaths.RandomRange(1, TileSize - 2)); while (true) { if (Tiles[toOut.x, toOut.z] == null && SettlementObjects[toOut.x, toOut.z] == null) { return(toOut); } toOut = GenerationRandom.RandomVec2i(1, TileSize - 2); } }
/// <summary> /// Generates all the rivers in the world. /// Generates <para name="count"></para> rivers in total /// </summary> /// <param name="count"></param> public void GenerateAllRivers(int count = 10) { //Create a RNG to use for generating rivers GenerationRandom genRan = new GenerationRandom(GameGenerator.Seed); Vec2i middle = new Vec2i(World.WorldSize / 2, World.WorldSize / 2); River[] rivers = new River[count]; for (int i = 0; i < count; i++) { //Define the start point as one within a set distance of the middle of the map Vec2i startOff = genRan.RandomVec2i(-World.WorldSize / 5, World.WorldSize / 5); //Create null direction, define main direction heading away from land mass centre Vec2i dir = null; //Check directions travelling in negative x direction. if (startOff.x <= 0) { //If the z value is of greater magnitude, then the river travels in the z direction if (Mathf.Abs(startOff.z) > Mathf.Abs(startOff.x)) { dir = new Vec2i(0, (int)Mathf.Sign(startOff.z)); } else { dir = new Vec2i(-1, 0); } } else { //If the z value is of greater magnitude, then the river travels in the z direction if (Mathf.Abs(startOff.z) > Mathf.Abs(startOff.x)) { dir = new Vec2i(0, (int)Mathf.Sign(startOff.z)); } else { dir = new Vec2i(1, 0); } } //Generate and store river rivers[i] = GenerateRiver(middle + startOff, dir, genRan); } foreach (River riv in rivers) { foreach (KeyValuePair <Vec2i, RiverNode> kvp in riv.GenerateRiverNodes()) { if (GameGenerator.TerrainGenerator.ChunkBases[kvp.Key.x, kvp.Key.z].RiverNode == null) { GameGenerator.TerrainGenerator.ChunkBases[kvp.Key.x, kvp.Key.z].AddRiver(kvp.Value); } else { GameGenerator.LakeGenerator.AddLake(new Lake(kvp.Key, 10)); } } } }
// Start is called before the first frame update void Start() { return; int maxSize = 32 * World.ChunkSize; GenerationRandom genRan = new GenerationRandom(0); for (int i = 0; i < 20; i++) { Vec2i pos = genRan.RandomVec2i(maxSize / 2, maxSize); GameObject ent = Instantiate(EntityPrefab); ent.transform.parent = transform; ent.transform.position = new Vector3(pos.x, 0, pos.z); } }
private Dungeon GenerateFireDungeon(DungeonEntrance entrance) { ChunkData[,] dungeonChunks = new ChunkData[5, 5]; Dictionary <int, WorldObjectData>[,] chunkObjects = new Dictionary <int, WorldObjectData> [5, 5]; for (int x = 0; x < 5; x++) { for (int z = 0; z < 5; z++) { chunkObjects[x, z] = new Dictionary <int, WorldObjectData>(); dungeonChunks[x, z] = new ChunkData(x, z, (int[, ])BASE_DIRT.Clone(), true, chunkObjects[x, z]); } } Vec2i chunkPos = World.GetChunkPosition(entrance.WorldPosition); Vec2i dungEntr = new Vec2i(5, 5); EntityFaction dungFact = new EntityFaction("Dungeon_faction"); List <Entity> dungeonEntities = new List <Entity>(); DungeonBoss testBoss = new DungeonBossTest(new BasicHumanoidCombatAI(), new CreatureTaskAI()); testBoss.SetEntityFaction(dungFact); for (int i = 0; i < 25; i++) { Vec2i randomPos = GenRan.RandomVec2i(World.ChunkSize, 5 * World.ChunkSize - 5); Entity newEnt = new Bandit(); newEnt.SetPosition(randomPos); newEnt.SetEntityFaction(dungFact); dungeonEntities.Add(newEnt); } Dungeon dungeon = new Dungeon(dungeonChunks, dungEntr, entrance.WorldPosition, dungeonEntities, testBoss); entrance.SetDungeon(dungeon); return(dungeon); }
public void GenerateChunkBases() { ChunkBases = new ChunkBase[World.WorldSize, World.WorldSize]; LandChunks = new List <Vec2i>(); Vec2i mid = new Vec2i(World.WorldSize / 2, World.WorldSize / 2); float r_sqr = (World.WorldSize / 3) * (World.WorldSize / 3); WorldRad = (World.WorldSize / 2.1f) * (World.WorldSize / 2.1f); Texture2D t = new Texture2D(World.WorldSize, World.WorldSize); Texture2D hum = new Texture2D(World.WorldSize, World.WorldSize); Texture2D temp = new Texture2D(World.WorldSize, World.WorldSize); float[,] humdity = new float[World.WorldSize, World.WorldSize]; Vec2i offset = GenRan.RandomVec2i(World.WorldSize / 8, World.WorldSize / 4); Vec2i humMid = mid + offset; float humRadSqr = GenRan.Random(World.WorldSize / 4, World.WorldSize / 2); humRadSqr *= humRadSqr; Vec2i tempMid = mid - offset; float tempRadSqr = GenRan.Random(World.WorldSize / 4, World.WorldSize / 2); tempRadSqr *= tempRadSqr; float[,] temperature = new float[World.WorldSize, World.WorldSize]; for (int x = 0; x < World.WorldSize; x++) { for (int z = 0; z < World.WorldSize; z++) { float c = WorldHeightChunk(x, z); humdity[x, z] = 0.4f + 0.6f * Mathf.PerlinNoise(4000 + x * 0.02f, 4000 + z * 0.02f); humdity[x, z] /= (((x - humMid.x) * (x - humMid.x) + (z - humMid.z) * (z - humMid.z)) / humRadSqr); humdity[x, z] = Mathf.Clamp(humdity[x, z], 0, 1); //temperature[x, z] = Mathf.PerlinNoise(700 + x * 0.02f, 700 + z * 0.02f); temperature[x, z] = 0.4f + 0.6f * Mathf.PerlinNoise(700 + x * 0.02f, 700 + z * 0.02f); temperature[x, z] /= (((x - tempMid.x) * (x - tempMid.x) + (z - tempMid.z) * (z - tempMid.z)) / tempRadSqr); temperature[x, z] = Mathf.Clamp(temperature[x, z], 0, 1); hum.SetPixel(x, z, new Color(humdity[x, z], humdity[x, z], humdity[x, z])); temp.SetPixel(x, z, new Color(temperature[x, z], temperature[x, z], temperature[x, z])); //c /= (((x - mid.x) * (x - mid.x) + (z - mid.z) * (z - mid.z)) / r_sqr); t.SetPixel(x, z, new Color(c / World.ChunkHeight, c / World.ChunkHeight, c / World.ChunkHeight)); Vec2i v = new Vec2i(x, z); //if ((x - mid.x) * (x - mid.x) + (z - mid.z) * (z - mid.z) < r_sqr) if (c > 40 && !(x == 0 || z == 0 || x == World.WorldSize - 1 || z == World.WorldSize - 1)) { //If point within this radius of middle ChunkBases[x, z] = new ChunkBase(v, Mathf.FloorToInt(c), true); LandChunks.Add(v); if (c > 100) { ChunkBases[x, z].SetBiome(ChunkBiome.mountain); } else if (temperature[x, z] > 0.7f && humdity[x, z] < 0.4f) { ChunkBases[x, z].SetBiome(ChunkBiome.dessert); } else if (humdity[x, z] > 0.4f && temperature[x, z] > 0.5f) { ChunkBases[x, z].SetBiome(ChunkBiome.forrest); } else { ChunkBases[x, z].SetBiome(ChunkBiome.grassland); } /* * if(temperature[x, z] > 0.7f && humdity[x,z] < 0.4f) * { * ChunkBases[x, z].SetBiome(ChunkBiome.dessert); * }else if(temperature[x, z] < 0.7f && humdity[x, z] > 0.6f) * if (temperature[x, z] < 0.25f) * { * ChunkBases[x, z].SetBiome(ChunkBiome.forrest); * } * else * { * ChunkBases[x, z].SetBiome(ChunkBiome.grassland); * }*/ } else { ChunkBases[x, z] = new ChunkBase(v, 1, false); } } } t.Apply(); hum.Apply(); temp.Apply(); /* * GameManager.Game.toDrawTexts[0] = t; * GameManager.Game.toDrawTexts[1] = hum; * GameManager.Game.toDrawTexts[2] = temp;*/ }
public Vec2i[] ChooseStartChunks(int count, int minSep) { //The angle between map middle that each capital should approximately //be seperated by float thetaSep = 360f / count; float thetaOffset = GenRan.Random(0, 90); Vector2 middle = new Vector2(World.WorldSize / 2, World.WorldSize / 2); Vec2i[] caps = new Vec2i[count]; //We iterate each of the kingdom capital positions we wish to calculate for (int i = 0; i < count; i++) { //Theta compared to (1,0) anticlockwise float theta = thetaOffset + thetaSep * i; float dx = Mathf.Cos(theta * Mathf.Deg2Rad); float dz = Mathf.Sin(theta * Mathf.Deg2Rad); Vector2 delta = new Vector2(dx, dz); //We start at middle Vector2 current = middle; int minLength = 32; int maxLength = -1; //iterate out for (int l = minLength; l < World.WorldSize / 2; l++) { current = middle + delta * l; Vec2i curChunk = Vec2i.FromVector2(current); ChunkBase2 cb = GameGen.TerGen.ChunkBases[curChunk.x, curChunk.z]; if (cb.Biome == ChunkBiome.ocean) { //When we reach the ocean, we define this as the max distance away maxLength = l - 1; break; } } //Capital is random point between min length, and max length Vec2i capPoint = Vec2i.FromVector2(middle + GenRan.RandomInt(minLength, maxLength) * delta); caps[i] = capPoint; } return(caps); Vec2i[] dirs = new Vec2i[] { new Vec2i(1, 1), new Vec2i(-1, 1), new Vec2i(-1, -1), new Vec2i(1, -1) }; Vec2i mid = new Vec2i(World.WorldSize / 2, World.WorldSize / 2); for (int i = 100; i < 400; i++) { for (int j = 0; j < dirs.Length; j++) { if (caps[j] == null) { Vec2i p = mid + dirs[j] * i; ChunkBase2 b = GameGen.TerGen.ChunkBases[p.x, p.z]; if (b.Biome == ChunkBiome.ocean) { Vec2i p_ = mid + dirs[j] * (int)(0.75f * i); caps[j] = p_; GridPoint gp = GameGen.GridPlacement.GetNearestPoint(p_); } } } } return(caps); for (int i = 0; i < count; i++) { bool validPoint = false; while (!validPoint) { Vec2i pos = GenRan.RandomVec2i(0, World.WorldSize - 1); GridPoint p = GameGen.GridPlacement.GetNearestPoint(pos); if (p != null && p.IsValid) { if (i == 0) { caps[0] = p.ChunkPos; validPoint = true; } else { int minDist = -1; for (int j = 0; j < i; j++) { int sqrDist = p.ChunkPos.QuickDistance(caps[j]); if (minDist == -1 || sqrDist < minDist) { minDist = sqrDist; } } if (minDist < minSep * minSep) { caps[i] = p.ChunkPos; validPoint = true; } } } } Debug.Log(caps[i]); } return(caps); }
public void GenerateChunkBases() { ChunkBases = new ChunkBase[World.WorldSize, World.WorldSize]; LandChunks = new List <Vec2i>(); GenerationRandom genRan = new GenerationRandom(0); Vec2i mid = new Vec2i(World.WorldSize / 2, World.WorldSize / 2); float r_sqr = (World.WorldSize / 3) * (World.WorldSize / 3); Texture2D t = new Texture2D(World.WorldSize, World.WorldSize); Texture2D hum = new Texture2D(World.WorldSize, World.WorldSize); Texture2D temp = new Texture2D(World.WorldSize, World.WorldSize); float[,] humdity = new float[World.WorldSize, World.WorldSize]; Vec2i offset = genRan.RandomVec2i(World.WorldSize / 8, World.WorldSize / 4); Vec2i humMid = mid + offset; float humRadSqr = genRan.Random(World.WorldSize / 4, World.WorldSize / 2); humRadSqr *= humRadSqr; Vec2i tempMid = mid - offset; float tempRadSqr = genRan.Random(World.WorldSize / 4, World.WorldSize / 2); tempRadSqr *= tempRadSqr; float[,] temperature = new float[World.WorldSize, World.WorldSize]; for (int x = 0; x < World.WorldSize; x++) { for (int z = 0; z < World.WorldSize; z++) { float c = 1 - Mathf.Pow(Mathf.PerlinNoise(x * 0.01f, z * 0.01f), 2); humdity[x, z] = 0.4f + 0.6f * Mathf.PerlinNoise(4000 + x * 0.02f, 4000 + z * 0.02f); humdity[x, z] /= (((x - humMid.x) * (x - humMid.x) + (z - humMid.z) * (z - humMid.z)) / humRadSqr); humdity[x, z] = Mathf.Clamp(humdity[x, z], 0, 1); //temperature[x, z] = Mathf.PerlinNoise(700 + x * 0.02f, 700 + z * 0.02f); temperature[x, z] = 0.4f + 0.6f * Mathf.PerlinNoise(700 + x * 0.02f, 700 + z * 0.02f); temperature[x, z] /= (((x - tempMid.x) * (x - tempMid.x) + (z - tempMid.z) * (z - tempMid.z)) / tempRadSqr); temperature[x, z] = Mathf.Clamp(temperature[x, z], 0, 1); hum.SetPixel(x, z, new Color(humdity[x, z], humdity[x, z], humdity[x, z])); temp.SetPixel(x, z, new Color(temperature[x, z], temperature[x, z], temperature[x, z])); c /= (((x - mid.x) * (x - mid.x) + (z - mid.z) * (z - mid.z)) / r_sqr); t.SetPixel(x, z, new Color(c, c, c)); Vec2i v = new Vec2i(x, z); //if ((x - mid.x) * (x - mid.x) + (z - mid.z) * (z - mid.z) < r_sqr) if (c > 0.5) { //If point within this radius of middle ChunkBases[x, z] = new ChunkBase(v, true); LandChunks.Add(v); //Deserts if its hot and dry if (temperature[x, z] > 0.7f && humdity[x, z] < 0.4f) { ChunkBases[x, z].SetBiome(ChunkBiome.dessert); } else if (humdity[x, z] > 0.4f && temperature[x, z] > 0.5f) { ChunkBases[x, z].SetBiome(ChunkBiome.forrest); } else { ChunkBases[x, z].SetBiome(ChunkBiome.grassland); } /* * if(temperature[x, z] > 0.7f && humdity[x,z] < 0.4f) * { * ChunkBases[x, z].SetBiome(ChunkBiome.dessert); * }else if(temperature[x, z] < 0.7f && humdity[x, z] > 0.6f) * if (temperature[x, z] < 0.25f) * { * ChunkBases[x, z].SetBiome(ChunkBiome.forrest); * } * else * { * ChunkBases[x, z].SetBiome(ChunkBiome.grassland); * }*/ } else { ChunkBases[x, z] = new ChunkBase(v, false); } } } t.Apply(); hum.Apply(); temp.Apply(); GameManager.Game.toDrawTexts[0] = t; GameManager.Game.toDrawTexts[1] = hum; GameManager.Game.toDrawTexts[2] = temp; }