/// <summary> /// Generates a map with a particular biome /// </summary> /// <param name="herdAmount">The total amount of herds to generate</param> /// <param name="BanditAmount">The total amount of bandits to generate.</param> /// <param name="actors"></param> /// <returns></returns> public static MapBlock[,] GenerateMap(GlobalBiome biome, int herdAmount, int banditAmount, out Actor[] actors, out MapCoordinate startPoint) { MapBlock[,] map = new MapBlock[MAP_EDGE, MAP_EDGE]; Random random = new Random(); ItemFactory.ItemFactory factory = new ItemFactory.ItemFactory(); int tileID = 0; factory.CreateItem(Archetype.TILES, details[biome].BaseTileTag, out tileID); //Create a new map which is edge X edge in dimensions and made of the base tile for (int x = 0; x < MAP_EDGE; x++) { for (int y = 0; y < MAP_EDGE; y++) { MapBlock block = new MapBlock(); map[x, y] = block; block.Tile = factory.CreateItem("tile", tileID); block.Tile.Coordinate = new MapCoordinate(x, y, 0, MapType.LOCAL); } } #region Leave Town Item //Now select all the border tiles and put in a "Exit here" border for (int x = 0; x < map.GetLength(0); x++) { MapCoordinate coo = new MapCoordinate(x, 0, 0, MapType.LOCAL); LeaveTownItem lti = new LeaveTownItem(); lti.Coordinate = coo; lti.Description = "continue on your journey"; lti.Name = "Leave Area"; lti.Coordinate = coo; map[x, 0].ForcePutItemOnBlock(lti); coo = new MapCoordinate(x, map.GetLength(1) - 1, 0, MapType.LOCAL); lti = new LeaveTownItem(); lti.Coordinate = coo; lti.Description = "continue on your journey"; lti.Name = "Leave Area"; lti.Coordinate = coo; map[x, map.GetLength(1) - 1].ForcePutItemOnBlock(lti); } for (int y = 0; y < map.GetLength(1); y++) { MapCoordinate coo = new MapCoordinate(0, y, 0, MapType.LOCAL); LeaveTownItem lti = new LeaveTownItem(); lti.Coordinate = coo; lti.Description = "continue on your journey"; lti.Name = "Leave Area"; lti.Coordinate = coo; map[0, y].ForcePutItemOnBlock(lti); coo = new MapCoordinate(map.GetLength(0) - 1, y, 0, MapType.LOCAL); lti = new LeaveTownItem(); lti.Coordinate = coo; lti.Description = "continue on your journey"; lti.Name = "Leave Area"; lti.Coordinate = coo; map[map.GetLength(0) - 1, y].ForcePutItemOnBlock(lti); } #endregion #region Desert Oasis if (biome == GlobalBiome.ARID_DESERT) { //Let's create a pool of water towards one of the corners. int randomNumber = random.Next(2); int rXCoord = 0; if (randomNumber == 1) { //Lower rXCoord = random.Next(0, MAP_EDGE / 3); } else { rXCoord = random.Next(2 * MAP_EDGE / 3, MAP_EDGE); } randomNumber = random.Next(2); int rYCoord = 0; if (randomNumber == 1) { //Lower rYCoord = random.Next(0, MAP_EDGE / 3); } else { rYCoord = random.Next(2 * MAP_EDGE / 3, MAP_EDGE); } //The pool will have a radius of 3 MapCoordinate coo = new MapCoordinate(rXCoord, rYCoord, 0, MapType.LOCAL); //Go through the blocks with a radius of 3 var oasisBlocks = map.Cast<MapBlock>().ToArray().Where(b => Math.Abs(b.Tile.Coordinate - coo) <= 3).ToArray(); int waterTile = -1; factory.CreateItem(Archetype.TILES, "water", out waterTile); foreach (var block in oasisBlocks) { var coord = block.Tile.Coordinate; block.Tile = factory.CreateItem("tile", waterTile); block.Tile.Coordinate = coord; } var aroundOasis = map.Cast<MapBlock>().ToArray().Where(b => Math.Abs(b.Tile.Coordinate - coo) <= 4 && Math.Abs(b.Tile.Coordinate - coo) > 3).ToArray(); int dummy; int grassTile = 0; factory.CreateItem(Archetype.TILES, "grass", out grassTile); foreach (var block in aroundOasis) { var coord = block.Tile.Coordinate; block.Tile = factory.CreateItem("tile", grassTile); block.Tile.Coordinate = coord; } //Put in some trees around the pool for (int i = 0; i < 3; i++) { MapItem tree = factory.CreateItem(Archetype.MUNDANEITEMS, "jungle tree", out dummy); var block = aroundOasis[random.Next(aroundOasis.Length)]; if (block.MayContainItems) { //Drop it block.ForcePutItemOnBlock(tree); } } } #endregion #region Wetland Splotches if (biome == GlobalBiome.WETLAND) { int waterTile = -1; factory.CreateItem(Archetype.TILES, "water", out waterTile); for (int i=0; i < 7; i++) { MapBlock rBlock = map[random.Next(map.GetLength(0)), random.Next(map.GetLength(1))]; Rectangle safeRect = new Rectangle( MAP_EDGE/2 - 5,MAP_EDGE/2 -5,10,10); if (safeRect.Contains(rBlock.Tile.Coordinate.X,rBlock.Tile.Coordinate.Y)) { continue; //Not here! } int size = random.Next(1, 3); //Get all the tiles around the block for a particular size var pool = map.Cast<MapBlock>().ToArray().Where(b => Math.Abs(b.Tile.Coordinate - rBlock.Tile.Coordinate) <= size).ToArray(); foreach(var block in pool) { MapCoordinate coo = block.Tile.Coordinate; block.Tile = factory.CreateItem("tiles", waterTile); block.Tile.Coordinate = coo; } } } #endregion for (int i = 0; i < details[biome].TreeCount; i++) { int treeID = 0; MapItem item = null; item = factory.CreateItem(Archetype.MUNDANEITEMS, details[biome].TreeTag, out treeID); //try 50 times to put it somewhere int tries = 0; while (tries < 50) { MapBlock randomBlock = map[random.Next(map.GetLength(0)), random.Next(map.GetLength(1))]; if (randomBlock.MayContainItems) { randomBlock.ForcePutItemOnBlock(item); break; } tries++; } } List<Actor> actorList = new List<Actor>(); //There, now that's done, lets generate some animals if (herdAmount + banditAmount > 0) { var herds = ActorGeneration.CreateAnimalHerds(biome, false, null, herdAmount); var bandits = CampGenerator.CreateBandits(banditAmount); var actorGroups = herds.Union(bandits); //Each actor group will be placed in a random 3 radius circle foreach (var actorGroup in actorGroups) { MapBlock randomBlock = map[random.Next(map.GetLength(0)), random.Next(map.GetLength(1))]; Rectangle wanderRect = new Rectangle(randomBlock.Tile.Coordinate.X - 2, randomBlock.Tile.Coordinate.Y - 2, 4, 4); //Put the actor groups somewhere around that block var blocks = map.Cast<MapBlock>().ToArray().Where(b => Math.Abs(b.Tile.Coordinate - randomBlock.Tile.Coordinate) < 4).ToArray(); //Pick a number of random blocks foreach (var newActor in actorGroup) { int tries = 0; while (tries < 50) { var actorBlock = blocks[random.Next(blocks.Length)]; if (actorBlock.MayContainItems) { //Put it there newActor.MapCharacter.Coordinate = actorBlock.Tile.Coordinate; actorBlock.ForcePutItemOnBlock(newActor.MapCharacter); //Make them wander newActor.MissionStack.Push(new WanderMission() { LoiterPercentage = 50, WanderPoint = actorBlock.Tile.Coordinate, WanderRectangle = wanderRect }); actorList.Add(newActor); break; } tries++; } } } } //Drop the player in the thick of it MapBlock center = map[map.GetLength(0) / 2, map.GetLength(1) / 2]; center.RemoveTopItem(); //Remove it if someone else wanted it startPoint = center.Tile.Coordinate; actors = actorList.ToArray(); return map; }
/// <summary> /// Generates a camp /// </summary> /// <returns></returns> public static MapBlock[,] GenerateCamp(int enemies, out MapCoordinate startPoint, out DRObjects.Actor[] enemyArray) { MapBlock[,] map = new MapBlock[MAP_EDGE, MAP_EDGE]; Random random = new Random(); ItemFactory.ItemFactory factory = new ItemFactory.ItemFactory(); int grassTileID = 0; factory.CreateItem(Archetype.TILES, "grass", out grassTileID); //Create a new map which is edge X edge in dimensions and made of grass for (int x = 0; x < MAP_EDGE; x++) { for (int y = 0; y < MAP_EDGE; y++) { MapBlock block = new MapBlock(); map[x, y] = block; block.Tile = factory.CreateItem("tile", grassTileID); block.Tile.Coordinate = new MapCoordinate(x, y, 0, MapType.LOCAL); } } //Now created a wall int pallisadeID = 0; factory.CreateItem(Archetype.MUNDANEITEMS, "pallisade wall lr", out pallisadeID); //Create a square of pallisade wall int startCoord = (MAP_EDGE - FORTIFICATION_EDGE) / 2; int endCoord = MAP_EDGE - ((MAP_EDGE - FORTIFICATION_EDGE) / 2); for (int x = startCoord + 1; x < endCoord; x++) { MapBlock block = map[x, startCoord]; MapItem item = factory.CreateItem("mundaneitems", pallisadeID); block.ForcePutItemOnBlock(item); block = map[x, endCoord]; item = factory.CreateItem("mundaneitems", pallisadeID); block.ForcePutItemOnBlock(item); } pallisadeID = 0; for (int y = startCoord + 1; y < endCoord; y++) { factory.CreateItem(Archetype.MUNDANEITEMS, "pallisade wall tb", out pallisadeID); MapBlock block = map[startCoord, y]; MapItem item = factory.CreateItem("mundaneitems", pallisadeID); block.ForcePutItemOnBlock(item); factory.CreateItem(Archetype.MUNDANEITEMS, "pallisade wall bt", out pallisadeID); block = map[endCoord, y]; item = factory.CreateItem("mundaneitems", pallisadeID); block.ForcePutItemOnBlock(item); } //We need to poke a hole in wall as an entrance //Let's poke one at the top and one at the bottom int center = MAP_EDGE / 2; int rValue = GameState.Random.Next(2); for (int x = -1; x < 2; x++) { if (rValue == 1) { MapBlock block = map[center + x, startCoord]; block.RemoveTopItem(); } else { MapBlock block = map[center + x, endCoord]; block.RemoveTopItem(); } } rValue = GameState.Random.Next(2); for (int y = -1; y < 2; y++) { if (rValue == 1) { MapBlock block = map[startCoord, y + center]; block.RemoveTopItem(); } else { MapBlock block = map[endCoord, y + center]; block.RemoveTopItem(); } } //Now, let's create some maplets in there //There's a single maplet containing the other maplets - let's get it LocalMapXMLParser lm = new LocalMapXMLParser(); Maplet maplet = lm.ParseMapletFromTag("camp"); LocalMapGenerator gen = new LocalMapGenerator(); //TODO: LATER THE OWNER MIGHT NOT BE A BANDIT MapletActorWanderArea[] wanderAreas = null; MapletPatrolPoint[] patrolPoints = null; MapletFootpathNode[] footPath = null; var gennedMap = gen.GenerateMap(grassTileID, null, maplet, false, "", OwningFactions.BANDITS, out enemyArray, out wanderAreas, out patrolPoints,out footPath); gen.JoinMaps(map, gennedMap, startCoord + 1, startCoord + 1); //Let's add some trees and stuff int decorCount = (int)(map.GetLength(1) * 0.50); //Just find as many random points and if it happens to be grass, drop them int itemID = 0; for (int i = 0; i < decorCount; i++) { //Just trees MapItem decorItem = factory.CreateItem(Archetype.MUNDANEITEMS, "tree", out itemID); //Pick a random point MapBlock randomBlock = map[random.Next(map.GetLength(0)), random.Next(map.GetLength(1))]; //Make sure its not inside the camp if (randomBlock.Tile.Coordinate.X >= startCoord && randomBlock.Tile.Coordinate.X <= endCoord && randomBlock.Tile.Coordinate.Y >= startCoord && randomBlock.Tile.Coordinate.Y <= endCoord) { //Not within the camp i--; continue; //try again } if (randomBlock.MayContainItems && randomBlock.Tile.Name == "Grass") { //Yes, can support it randomBlock.ForcePutItemOnBlock(decorItem); } //Otherwise forget all about it } //Now select all the border tiles and put in a "Exit here" border for (int x = 0; x < map.GetLength(0); x++) { MapCoordinate coo = new MapCoordinate(x, 0, 0, MapType.LOCAL); LeaveTownItem lti = new LeaveTownItem(); lti.Coordinate = coo; lti.Description = "path outside the town"; lti.Name = "Leave Town"; lti.Coordinate = coo; map[x, 0].ForcePutItemOnBlock(lti); coo = new MapCoordinate(x, map.GetLength(1) - 1, 0, MapType.LOCAL); lti = new LeaveTownItem(); lti.Coordinate = coo; lti.Description = "path outside the town"; lti.Name = "Leave Town"; lti.Coordinate = coo; map[x, map.GetLength(1) - 1].ForcePutItemOnBlock(lti); } for (int y = 0; y < map.GetLength(1); y++) { MapCoordinate coo = new MapCoordinate(0, y, 0, MapType.LOCAL); LeaveTownItem lti = new LeaveTownItem(); lti.Coordinate = coo; lti.Description = "path outside the town"; lti.Name = "Leave Town"; lti.Coordinate = coo; map[0, y].ForcePutItemOnBlock(lti); coo = new MapCoordinate(map.GetLength(0) - 1, y, 0, MapType.LOCAL); lti = new LeaveTownItem(); lti.Coordinate = coo; lti.Description = "path outside the town"; lti.Name = "Town Borders"; lti.Coordinate = coo; map[map.GetLength(0) - 1, y].ForcePutItemOnBlock(lti); } #region Treasure Room //This is a bit naughty. We need to locate where the tiles become soil MapCoordinate soilStart = null; bool breakOut = false; for (int x = 0; x < map.GetLength(0); x++) { for (int y = 0; y < map.GetLength(1); y++) { if (map[x, y].Tile.Name.ToLower().Equals("soil")) { soilStart = new MapCoordinate(x, y, 0, MapType.LOCAL); breakOut = true; break; } } if (breakOut) { break; } } //Also naughty, we know it's 5x5 #region Inner Wall for (int x = 0; x < 5; x++) { factory.CreateItem(Archetype.MUNDANEITEMS, "pallisade wall lr", out pallisadeID); MapItem item = factory.CreateItem("mundaneitems", pallisadeID); if (x != 2) //hole in the top { map[soilStart.X + x, soilStart.Y].ForcePutItemOnBlock(item); } factory.CreateItem(Archetype.MUNDANEITEMS, "pallisade wall lr", out pallisadeID); item = factory.CreateItem("mundaneitems", pallisadeID); map[soilStart.X + x, soilStart.Y + 5].ForcePutItemOnBlock(item); } for (int y = 0; y < 5; y++) { factory.CreateItem(Archetype.MUNDANEITEMS, "pallisade wall tb", out pallisadeID); MapItem item = factory.CreateItem("mundaneitems", pallisadeID); map[soilStart.X - 1, soilStart.Y + y].ForcePutItemOnBlock(item); factory.CreateItem(Archetype.MUNDANEITEMS, "pallisade wall bt", out pallisadeID); item = factory.CreateItem("mundaneitems", pallisadeID); map[soilStart.X + 5, soilStart.Y + y].ForcePutItemOnBlock(item); } #endregion #endregion #region Patrol Points //Now let's collect the patrol points. We're going to have two possible patrols - one around each of the entrances - and another on the outside corners of the map List<MapCoordinate> outsidePatrol = new List<MapCoordinate>(); outsidePatrol.Add(new MapCoordinate(startCoord - 2, startCoord, 0, MapType.LOCAL)); outsidePatrol.Add(new MapCoordinate(endCoord + 2, startCoord, 0, MapType.LOCAL)); outsidePatrol.Add(new MapCoordinate(endCoord + 2, endCoord, 0, MapType.LOCAL)); outsidePatrol.Add(new MapCoordinate(startCoord - 2, endCoord, 0, MapType.LOCAL)); List<MapCoordinate> insidePatrol = new List<MapCoordinate>(); insidePatrol.Add(new MapCoordinate(center, startCoord + 1, 0, MapType.LOCAL)); insidePatrol.Add(new MapCoordinate(center, endCoord - 1, 0, MapType.LOCAL)); insidePatrol.Add(new MapCoordinate(startCoord + 1, center, 0, MapType.LOCAL)); insidePatrol.Add(new MapCoordinate(endCoord - 1, center, 0, MapType.LOCAL)); //Go through all of those and make sure they're clear of anything that wouldn't let them walk upon them foreach (var coordinate in outsidePatrol) { map[coordinate.X, coordinate.Y].RemoveTopItem(); } foreach (var coordinate in insidePatrol) { map[coordinate.X, coordinate.Y].RemoveTopItem(); } #endregion #region Actors enemyArray = CreateBandits(enemies, outsidePatrol.Select(op => new PatrolPoint() { AcceptableRadius = 2, Coordinate = op }).ToList(), insidePatrol.Select(op => new PatrolPoint() { AcceptableRadius = 2, Coordinate = op }).ToList()); int tries = 0; //Put them on the mappity map for (int i = 0; i < enemyArray.Length; i++) { Actor actor = enemyArray[i]; int randomX = random.Next(map.GetLength(0)); int randomY = random.Next(map.GetLength(1)); if (map[randomX, randomY].MayContainItems) { //Plop it on there actor.MapCharacter.Coordinate = new MapCoordinate(randomX, randomY, 0, MapType.LOCAL); map[randomX, randomY].ForcePutItemOnBlock(actor.MapCharacter); tries = 0; ////If they are wandering, make them wander in the right place //var mission = actor.MissionStack.Peek(); //if (mission.MissionType == ActorMissionType.WANDER) //{ // var wander = mission as WanderMission; // wander.WanderPoint = new MapCoordinate(actor.MapCharacter.Coordinate); // wander.WanderRectangle = new Rectangle(startCoord, startCoord, FORTIFICATION_EDGE, FORTIFICATION_EDGE); //} } else { tries++; i--; } if (tries >= 50) { //give up continue; } } #endregion startPoint = new MapCoordinate(map.GetLength(0) / 2, 0, 0, MapType.LOCAL); return map; }