/// <summary> /// Generate the forest /// </summary> /// <param name="argForestCenter">Center of the forest</param> /// <param name="argForestSize">Forest size. Defines the square to border the forest</param> /// <param name="argDensity">Forest density (0 - 1). Defines trees density in the forest square</param> public void GenerateForest(Point argForestCenter, Size argForestSize, double argDensity) { logger.Debug("Generating new forest at {0} of size {1} with {2} density", argForestCenter, argForestSize, argDensity); int chanceForNewTree = 50; //Chance for algorythm to create a new tree near the current Random currentChance = new Random(); //Current counted chance to compare with chanceForNewTree List <Point> newTrees = new List <Point>(); //Trees, that created in the current phase List <Point> currentTreesArray = new List <Point>(); //Trees, that are using for creating new ones int borderLeft = argForestCenter.X - argForestSize.Width / 2; //Left border of the forest int borderRight = argForestCenter.X + argForestSize.Width / 2; //Right border of the forest int borderTop = argForestCenter.Y - argForestSize.Height / 2; //Top border of the forest int borderBottom = argForestCenter.Y + argForestSize.Height / 2; //Bottom border of the forest int maxTrees = (int)(argForestSize.Height * argForestSize.Width * argDensity); //Max number of trees in the forest int totalTreesAdded = 0; //Total new trees created currentTreesArray.Add(argForestCenter); while (currentTreesArray.Count != 0 && totalTreesAdded < maxTrees) { foreach (Point p in currentTreesArray) { for (int i = p.X - 1; i <= p.X + 1; i++) { if (i > borderRight || i < borderLeft) { continue; } for (int j = p.Y - 1; j <= p.Y + 1; j++) { if (totalTreesAdded > maxTrees / 10) //edges of the forest are more discontinuous, so we are not checking diagonal cells { if (j != p.Y && (i == p.X - 1 || i == p.X + 1)) { continue; } if (i != p.X && (j == p.Y - 1 || j == p.Y + 1)) { continue; } } if (j > borderBottom || j < borderTop) { continue; } if (i == p.X && j == p.Y) { continue; } if (currentChance.Next(100) < chanceForNewTree) { if (GetAgentsAt(new Point(i, j)).Count != 0) { continue; } AgentTree currentAddingAgent = new AgentTree(); currentAddingAgent.Name = "Tree_at_" + i.ToString() + "_" + j.ToString(); currentAddingAgent.CurrentLocation = new Location(i, j); AddAgent(currentAddingAgent); newTrees.Add(new Point(i, j)); totalTreesAdded++; } } } } currentTreesArray.Clear(); currentTreesArray.AddRange(newTrees); newTrees.Clear(); } logger.Info("Generated new forest. {0} trees created", totalTreesAdded); }
/// <summary> /// Here we basicly create world. /// </summary> public void Initialise() { Agent currentAddingAgent; //Agent, that is adding to the world now. This variable is necessary to configure the agent Agents = new List <Agent>(); Actions = new List <ActionAbstract>(); worldCommands = new List <worldCommand>(); GenerateForest(new Point(-5, -5), new Size(10, 10), 0.50); GenerateForest(new Point(-15, 5), new Size(20, 20), 0.20); /*//Terrific test init (for hard and cruel test) * for (int i = 0; i < 3; ++i) * { * currentAddingAgent = new AgentLivingIndigo(); * currentAddingAgent.Name = string.Format("Indigo{0}",i); * currentAddingAgent.Location = new Point((i+1)*10, (i+1)*10); * currentAddingAgent.CurrentState.Health.MaxValue = 100; * currentAddingAgent.CurrentState.Health.CurrentUnitValue = 100; * (currentAddingAgent as AgentLivingIndigo).AgentsRangeOfView = 100; * currentAddingAgent.Inventory.StorageSize = 3; * AddAgent(currentAddingAgent); * }*/ //Basic test init currentAddingAgent = new AgentLivingIndigo(); currentAddingAgent.Name = "Indigo1"; currentAddingAgent.CurrentLocation = new Location(10, 10); currentAddingAgent.CurrentState.Health.MaxValue = 100; currentAddingAgent.CurrentState.Health.CurrentUnitValue = 100; (currentAddingAgent as AgentLivingIndigo).AgentsRangeOfView = 20; currentAddingAgent.Inventory.StorageSize = 3; AddAgent(currentAddingAgent); currentAddingAgent = new AgentLivingIndigo(); currentAddingAgent.Name = "Indigo2"; currentAddingAgent.CurrentLocation = new Location(10, 15); currentAddingAgent.CurrentState.Health.MaxValue = 100; currentAddingAgent.CurrentState.Health.CurrentUnitValue = 100; (currentAddingAgent as AgentLivingIndigo).AgentsRangeOfView = 20; currentAddingAgent.Inventory.StorageSize = 3; AddAgent(currentAddingAgent); currentAddingAgent = new AgentLivingIndigo(); currentAddingAgent.Name = "Indigo3"; currentAddingAgent.CurrentLocation = new Location(15, 15); currentAddingAgent.CurrentState.Health.MaxValue = 100; currentAddingAgent.CurrentState.Health.CurrentUnitValue = 100; (currentAddingAgent as AgentLivingIndigo).AgentsRangeOfView = 20; currentAddingAgent.Inventory.StorageSize = 3; AddAgent(currentAddingAgent); currentAddingAgent = new AgentLivingIndigo(); currentAddingAgent.Name = "Indigo4"; currentAddingAgent.CurrentLocation = new Location(-20, 0); currentAddingAgent.CurrentState.Health.MaxValue = 100; currentAddingAgent.CurrentState.Health.CurrentUnitValue = 100; (currentAddingAgent as AgentLivingIndigo).AgentsRangeOfView = 10; currentAddingAgent.Inventory.StorageSize = 3; AddAgent(currentAddingAgent); currentAddingAgent = new AgentItemFoodFruit(); currentAddingAgent.Name = "Fruit1"; currentAddingAgent.CurrentLocation = new Location(0, 2); currentAddingAgent.CurrentState.Health.MaxValue = 100; currentAddingAgent.CurrentState.Health.CurrentUnitValue = 100; AddAgent(currentAddingAgent); currentAddingAgent = new AgentItemFoodFruit(); currentAddingAgent.Name = "Fruit2"; currentAddingAgent.CurrentLocation = new Location(2, 0); currentAddingAgent.CurrentState.Health.MaxValue = 100; currentAddingAgent.CurrentState.Health.CurrentUnitValue = 100; AddAgent(currentAddingAgent); currentAddingAgent = new AgentPuddle(); currentAddingAgent.Name = "Puddle1"; currentAddingAgent.CurrentLocation = new Location(-2, -2); currentAddingAgent.CurrentState.Health.MaxValue = 200; currentAddingAgent.CurrentState.Health.CurrentUnitValue = 200; AddAgent(currentAddingAgent); currentAddingAgent = new AgentPuddle(); currentAddingAgent.Name = "Puddle2"; currentAddingAgent.CurrentLocation = new Location(-15, 0); currentAddingAgent.CurrentState.Health.MaxValue = 200; currentAddingAgent.CurrentState.Health.CurrentUnitValue = 200; AddAgent(currentAddingAgent); currentAddingAgent = new AgentPuddle(); currentAddingAgent.Name = "Puddle3"; currentAddingAgent.CurrentLocation = new Location(-10, 16); currentAddingAgent.CurrentState.Health.MaxValue = 200; currentAddingAgent.CurrentState.Health.CurrentUnitValue = 200; AddAgent(currentAddingAgent); currentAddingAgent = new AgentItemResLog(); currentAddingAgent.Name = "Log1"; AddAgent(currentAddingAgent); currentAddingAgent = new AgentItemResLog(); currentAddingAgent.Name = "Log2"; currentAddingAgent.CurrentLocation = new Location(3, 4); AddAgent(currentAddingAgent); currentAddingAgent = new AgentItemResLog(); currentAddingAgent.Name = "Log3"; currentAddingAgent.CurrentLocation = new Location(-3, 4); AddAgent(currentAddingAgent); currentAddingAgent = new AgentItemResLog(); currentAddingAgent.Name = "Log4"; currentAddingAgent.CurrentLocation = new Location(-3, -4); AddAgent(currentAddingAgent); currentAddingAgent = new AgentTree(); currentAddingAgent.Name = "Tree_at_" + 1.ToString() + "_" + 1.ToString(); currentAddingAgent.CurrentLocation = new Location(1, 1); AddAgent(currentAddingAgent); }