public override void Initialize(Simulation game) { base.Initialize(game); growGroup = new ThreadTaskGroup(); int divider = 4; int numTilesPerTaskX = Math.Max(1, Width / divider); int numTilesPerTaskY = Math.Max(1, Height / divider); for (int taskX = 0; taskX < (Width / numTilesPerTaskX); taskX++) { for (int taskY = 0; taskY < (Height / numTilesPerTaskY); taskY++) { int closureTaskX = taskX; int closureTaskY = taskY; Action <Simulation, float> action = (Simulation sim, float time) => { float deltaTime = (float)time; for (int tileOffsetX = 0; tileOffsetX < numTilesPerTaskX; tileOffsetX++) { for (int tileOffsetY = 0; tileOffsetY < numTilesPerTaskY; tileOffsetY++) { int tileIndexX = closureTaskX * numTilesPerTaskX + tileOffsetX; int tileIndexY = closureTaskY * numTilesPerTaskY + tileOffsetY; if (tileIndexX < Width && tileIndexY < Height) { if (IsFertile(tileIndexX, tileIndexY)) { Grow(tileIndexX, tileIndexY, deltaTime); } } } } }; SimpleSimulationTask task = new SimpleSimulationTask(simulation, action); growGroup.AddTask(task); } } growGroup.AddTask(new SimpleSimulationTask(simulation, (sim, time) => { FoodRecord.Add(CalculateFoodAvailable()); })); simulation.TaskManager.AddGroup(growGroup); }
private void CreateTasks() { ThreadTaskGroup spawnCreaturesGroup = new ThreadTaskGroup(); spawnCreaturesGroup.AddTask(new SimpleSimulationTask(simulation, (Simulation sim, float time) => { while (creatures.Count < sim.SimulationConfiguration.MinCreatures) { Creature justSpawned = new Creature( sim.RandomWorldPosition(), Simulation.RandomFloat() * Mathf.PI * 2, this); lock (this) { creatures.Add(justSpawned); if (SelectedCreature == null || SelectedCreature.Energy <= 100) { SelectedCreature = justSpawned; } } } })); //spawnCreaturesGroup.AddDependency(simulation.TileMap.growGroup); simulation.TaskManager.AddGroup(spawnCreaturesGroup); ThreadTaskGroup readSensorGroup = new ThreadTaskGroup(); for (int taskIndex = 0; taskIndex < simulation.SimulationConfiguration.NumCreatureTasks; taskIndex++) { CreatureReadSensorTask task = new CreatureReadSensorTask(simulation, taskIndex, simulation.SimulationConfiguration.NumCreatureTasks); readSensorGroup.AddTask(task); } readSensorGroup.AddDependency(spawnCreaturesGroup); simulation.TaskManager.AddGroup(readSensorGroup); ThreadTaskGroup actGroup = new ThreadTaskGroup(); for (int taskIndex = 0; taskIndex < simulation.SimulationConfiguration.NumCreatureTasks; taskIndex++) { CreatureActTask task = new CreatureActTask(simulation, taskIndex, simulation.SimulationConfiguration.NumCreatureTasks); actGroup.AddTask(task); } actGroup.AddDependency(readSensorGroup); simulation.TaskManager.AddGroup(actGroup); ThreadTaskGroup mergeAndSpawnGroup = new ThreadTaskGroup(); MergeCreatureArraysTask mergeTask = new MergeCreatureArraysTask(simulation, actGroup); mergeAndSpawnGroup.AddTask(mergeTask); mergeAndSpawnGroup.AddDependency(actGroup); simulation.TaskManager.AddGroup(mergeAndSpawnGroup); ThreadTaskGroup collisionGroup = new ThreadTaskGroup(); for (int taskIndex = 0; taskIndex < simulation.SimulationConfiguration.NumCreatureTasks; taskIndex++) { CreatureHandleCollisionTask task = new CreatureHandleCollisionTask(simulation, taskIndex, simulation.SimulationConfiguration.NumCreatureTasks); collisionGroup.AddTask(task); } collisionGroup.AddDependency(mergeAndSpawnGroup); simulation.TaskManager.AddGroup(collisionGroup); ThreadTaskGroup cleanupAndStatisticsGroup = new ThreadTaskGroup(); cleanupAndStatisticsGroup.AddDependency(collisionGroup); simulation.TaskManager.AddGroup(cleanupAndStatisticsGroup); cleanupAndStatisticsGroup.AddTask(new SimpleSimulationTask(simulation, (Simulation sim, float time) => { ResetCollisionGrid(); })); cleanupAndStatisticsGroup.AddTask(new SimpleSimulationTask(simulation, (Simulation sim, float time) => { // Collect some statistics Tick++; AliveCreaturesRecord.Add(creatures.Count); })); cleanupAndStatisticsGroup.AddTask(new SimpleSimulationTask(simulation, (Simulation sim, float time) => { if (creatures.Count > 0) { OldestCreatureAlive = creatures[0]; foreach (Creature c in creatures) { if (c.Age > OldestCreatureAlive.Age) { OldestCreatureAlive = c; } } } })); }