Example #1
0
        private Image RenderTurn(ExperimentTurn turn)
        {
            var sqlConnection      = this.databaseConnector.OpenSqlConnection();
            var turnOrganismStates = sqlConnection.GetTurnOrganismStates(turn.Id).ToDictionary(state => state.Position);

            var bitmap   = new Bitmap(AlienSpaceVector.WorldWidth * CellSize, AlienSpaceVector.WorldHeight * CellSize);
            var graphics = Graphics.FromImage(bitmap);
            var sunBrush = new SolidBrush(Color.Yellow);

            for (var x = 0; x < AlienSpaceVector.WorldWidth; x++)
            {
                for (var y = 0; y < AlienSpaceVector.WorldHeight; y++)
                {
                    var position = new AlienSpaceVector(x, y);

                    var distanceFromSun = AlienSpaceVector.GetCoordinateDelta(position, turn.SunPosition).Abs();
                    if (distanceFromSun < Experiment.SunRadius)
                    {
                        graphics.FillRectangle(sunBrush, x * CellSize, y * CellSize, CellSize, CellSize);
                    }

                    if (turnOrganismStates.ContainsKey(position))
                    {
                        var state         = turnOrganismStates[position];
                        var organismBrush = new SolidBrush(Color.FromArgb(ScaleColorComponent(state.Red), ScaleColorComponent(state.Green), ScaleColorComponent(state.Blue)));
                        graphics.FillEllipse(organismBrush, x * CellSize, y * CellSize, CellSize, CellSize);
                    }
                }
            }
            return(bitmap);
        }
Example #2
0
        public static void SaveExperimentTurn(this SqlConnection sqlConnection, ExperimentTurn experimentTurn)
        {
            var sqlCommand            = new SqlCommand(createExperimentTurnCommand, sqlConnection);
            var experimentIdParameter = sqlCommand.Parameters.Add("@ExperimentId", SqlDbType.BigInt);

            experimentIdParameter.SqlValue = experimentTurn.ExperimentId;
            var simulationTimeParameter = sqlCommand.Parameters.Add("@SimulationTime", SqlDbType.Int);

            simulationTimeParameter.SqlValue = experimentTurn.SimulationTime.TotalTurns;
            var dayParameter = sqlCommand.Parameters.Add("@Day", SqlDbType.Int);

            dayParameter.SqlValue = experimentTurn.SimulationTime.Day;
            var phaseOfDayParameter = sqlCommand.Parameters.Add("@PhaseOfDay", SqlDbType.Int);

            phaseOfDayParameter.SqlValue = experimentTurn.SimulationTime.PhaseOfDay;
            var phaseParameter = sqlCommand.Parameters.Add("@Phase", SqlDbType.Int);

            phaseParameter.SqlValue = experimentTurn.SimulationTime.Phase;
            var randomSeedParameter = sqlCommand.Parameters.Add("@RandomSeed", SqlDbType.Int);

            randomSeedParameter.SqlValue = experimentTurn.RandomSeed;
            var sunXParameter = sqlCommand.Parameters.Add("@SunX", SqlDbType.Int);

            sunXParameter.SqlValue = experimentTurn.SunPosition.X;
            var sunYParameter = sqlCommand.Parameters.Add("@SunY", SqlDbType.Int);

            sunYParameter.SqlValue = experimentTurn.SunPosition.Y;
            var extraEnergyParameter = sqlCommand.Parameters.Add("@ExtraEnergy", SqlDbType.Decimal);

            extraEnergyParameter.SqlValue = experimentTurn.ExtraEnergy;

            experimentTurn.Id = sqlCommand.ReadResults(reader => reader.GetInt64(0)).Single();
        }
        private static void AbsorbEnergy(ExperimentTurn experimentTurn, OrganismState organismState, decimal delta)
        {
            Claws.AtLeast(() => delta, 0);

            delta = Math.Min(delta, Math.Min(experimentTurn.ExtraEnergy, 1 - organismState.Health));

            experimentTurn.ExtraEnergy -= delta;
            organismState.Health       += delta;
        }
        private static void ReleaseEnergy(ExperimentTurn experimentTurn, OrganismState organismState, decimal delta)
        {
            Claws.AtLeast(() => delta, 0);

            delta = Math.Min(delta, organismState.Health);

            organismState.Health       -= delta;
            experimentTurn.ExtraEnergy += delta;
        }
        private static ExperimentTurn BuildNextExperimentTurn(ExperimentStatus experimentStatus)
        {
            var newSimulationTime = experimentStatus.LatestSimulationTime + 1.Turn() ?? new AlienDateTime(0);

            var random = experimentStatus.LatestRandomSeed == null ? new Random() : new Random(experimentStatus.LatestRandomSeed.Value);
            var moveUp = random.Next(4) == 0;

            var newSunPosition = (experimentStatus.LatestSunPosition == null ?
                                  new AlienSpaceVector(32, 18) :
                                  experimentStatus.LatestSunPosition.Value + new AlienSpaceVector(1, moveUp ? -1 : 0)).ToCoordinates();

            var newExtraEnergy = experimentStatus.LatestExtraEnergy ?? AlienSpaceVector.WorldWidth * AlienSpaceVector.WorldHeight * Experiment.Richness;

            var newExperimentTurn = new ExperimentTurn {
                ExperimentId   = experimentStatus.ExperimentId,
                SimulationTime = newSimulationTime,
                RandomSeed     = random.Next(),
                SunPosition    = newSunPosition,
                ExtraEnergy    = newExtraEnergy,
            };

            return(newExperimentTurn);
        }