/// <summary> /// Обновляет информацию о последних действиях получения энергии формы жизни /// </summary> /// <param name="world">Мир, для которого происходит обновление</param> public void Update(Simple2DWorld world) { int height = world.Map.LifeForms.GetLength(1); int width = world.Map.LifeForms.GetLength(0); Parallel.For(0, height, y => { for (int x = 0; x < width; x++) { BotLifeForm bot = world.Map.LifeForms[x, y]; if (world.Map.LifeForms[x, y] != null) { if (!bot.Dead) { if (bot.LastEnergyActions.Count >= 15) { bot.LastEnergyActions.Dequeue(); } SetRGB(bot); } else { bot.Color = Color.FromArgb(132, 132, 132); } } } }); }
/// <summary> /// получает энергетическое состояние бота на основании текущей энергии бота /// </summary> /// <returns>Энергетическое состояние</returns> private EnergyStates GetState(BotLifeForm bot) { EnergyStates flag; if (bot.Energy < 0) { flag = EnergyStates.Dead; bot.Dead = true; return(flag); } if (bot.Energy >= MaxEnergy) { flag = EnergyStates.EnergyCollapse; bot.Dead = true; return(flag); } if (bot.Energy >= MaxEnergy * 0.9) { flag = EnergyStates.ForsedReproduction; return(flag); } if (bot.Energy >= MaxEnergy * 0.7) { flag = EnergyStates.CanReproduce; return(flag); } flag = EnergyStates.Alive; return(flag); }
/// <summary> /// Устанавливает действие бота в соответствии с геномом /// </summary> /// <param name="lifeForm"></param> private void SetAction(BotLifeForm lifeForm) { switch (lifeForm.Genom[lifeForm.YTK]) { case 1: lifeForm.Action = Actions.Photosynthesis; NextStep(lifeForm); break; case 2: lifeForm.Action = Actions.Extraction; NextStep(lifeForm); break; case 3: lifeForm.Action = Actions.DoDescendant; NextStep(lifeForm); lifeForm.Direction = (Directions)((lifeForm.Genom[lifeForm.YTK] / 8) + 1); NextStep(lifeForm); break; case 4: lifeForm.Action = Actions.Eat; NextStep(lifeForm); lifeForm.Direction = (Directions)((lifeForm.Genom[lifeForm.YTK] / 8) + 1); NextStep(lifeForm); break; case 5: lifeForm.Action = Actions.Move; NextStep(lifeForm); lifeForm.Direction = (Directions)((lifeForm.Genom[lifeForm.YTK] / 8) + 1); NextStep(lifeForm); break; case 6: lifeForm.Action = Actions.ShareEnergy; NextStep(lifeForm); break; default: try { lifeForm.YTK = Convert.ToByte(((lifeForm.YTK + lifeForm.Genom[lifeForm.YTK]) % 63)); } catch (Exception e) { throw new ArgumentException("Недопустимое значение YTK", (((lifeForm.YTK + lifeForm.Genom[lifeForm.YTK]) % 63)).ToString(), e); } lifeForm.Action = Actions.None; lifeForm.Direction = Directions.None; NextStep(lifeForm); break; } }
/// <summary> /// Определяет, оказывает ли феномен воздействие на форму жизни и оказывает это воздействие /// </summary> /// <param name="bot">Бот, получающий эффект</param> public void GetEffects(BotLifeForm bot) { Point botPoint = new Point(bot.Point.X, bot.Point.Y); if (isIn(botPoint)) { double depthFactor = 1 / (1 + (double)(botPoint.Y - _place[0].Y) / _place[1].Y); bot.Energy += (int)(BaseIntensity * _powerFactor * depthFactor); bot.LastEnergyActions.Enqueue(Actions.Photosynthesis); } }
/// <summary> /// Устанавливает RGB в соответствии с последними действиями формы жизни /// </summary> public void SetRGB(BotLifeForm bot) { const int MaxBotEnergy = 1500; byte R = 0; byte G = 0; byte B = 0; byte part = 0; switch (_colorType) { case ColorType.Default: foreach (Actions Action in bot.LastEnergyActions) { switch (Action) { case Actions.Extraction: B++; break; case Actions.Photosynthesis: G++; break; case Actions.Eat: R++; break; } } if (R < 0 || G < 0 || B < 0) { throw new ArgumentException("Один из параметров RGB был отрицательным"); } if (R + G + B != 0) { part = Convert.ToByte(255 / (R + G + B)); } bot.Color = Color.FromArgb((part * R), (part * G), (part * B)); break; case ColorType.EnergyDisplay: R = 255; G = (byte)(255 - (bot.Energy / (double)MaxBotEnergy) * 255); bot.Color = Color.FromArgb(R, G, B); break; case ColorType.GenomDisplay: R = (byte)bot.FriendId; G = (byte)(bot.FriendId * bot.FriendId); B = (byte)(bot.FriendId / Math.E); bot.Color = Color.FromArgb(R, G, B); break; } }
public static void DoDescendant(BotList lifeForms, BotLifeForm bot, int X, int Y) { BotLifeForm lifeForm = new BotLifeForm(new Point(X, Y), ((BotLifeForm)bot)); lifeForms.Add(lifeForm); }
public void NextStep(BotLifeForm bot) { bot.YTK = (byte)((bot.YTK + 1) % 64); }