private MapField Closest(IEnumerable <MapField> listFields) { MapField closestField = null; float minDist = float.PositiveInfinity; foreach (MapField field in listFields) { float cur = MapField.Distance(Location, field); if (cur < minDist) { minDist = cur; closestField = field; } } return(closestField); }
public void UpdateTime(object sender, UpdateEventArgs e) { //Console.WriteLine($"{State} \t {counter_:g} \t \t {preparationCounter_:g}"); for (int index = 0; index < besigers_.Count;) { if (besigers_[index].HP.Value > 0f) { index++; continue; } --menStillNotFleeing_; besigers_[index].Die(); besigers_.Remove(besigers_[index]); foreach (Men other in besigers_) { other.Morale.Value -= 5f; } } if (besigersToRemove_.Count != 0) { foreach (Men besiger in besigersToRemove_) { besiger.Die(); besigers_.Remove(besiger); } besigersToRemove_.Clear(); State = Status.None; } if (State == Status.Spawning) { menStillNotFleeing_ = menAttacking_ = rand_.Next(3, 11); MapField location = null; while (location == null || !location.IsAvaliable || colony_.Any(colonist => MapField.Distance(location, colonist.Location) <= 0.4f * map_.Size)) { location = map_[rand_.Next(map_.Size), rand_.Next(map_.Size)]; } for (int i = 0; i < menAttacking_; i++) { MapField currentField = null; while (currentField == null || !currentField.IsAvaliable) { try { currentField = location.Neighbour[rand_.Next(-7, 7), rand_.Next(-7, 7)]; } catch (NoSouchNeighbourException) { } } besigers_.Add(new Men() { Name = "Adam", MoveSpeed = 5, Location = currentField, TextureSelected = new Sprite(BesigerTexture), TextureNotSelected = new Sprite(BesigerTexture), IsSelected = false, HP = new FuzzyHP(50f, 50f), Laziness = new FuzzyLaziness((float)rand_.NextDouble() * 10f), RestF = new FuzzyRest((float)rand_.NextDouble() * 10f), Strength = (float)rand_.NextDouble() * 10f, Morale = new FuzzyMorale(7.5f), Mining = (float)rand_.NextDouble() * 10f, Constructing = (float)rand_.NextDouble() * 10f, }); } foreach (Men besiger in besigers_) { map_.UpdateTimeEvent += besiger.UpdateTime; } State = Status.Preparation; } else if (State == Status.Preparation) { if (preparationCounter_ == null) { preparationCounter_ = new TimeSpan(0, rand_.Next(1, 5), rand_.Next(60)); } preparationCounter_ -= e.Elapsed; if (preparationCounter_ <= TimeSpan.Zero) { preparationCounter_ = null; State = Status.Attack; } } else if (State == Status.Attack) { if (menStillNotFleeing_ <= 2) { State = Status.Flee; return; } foreach (Men besiger in besigers_) { string stateFleeing = MakeDecision(besiger.HP, besiger.Morale); switch (stateFleeing) { case "Flee": if (besiger.Job is Men.FleeJob) { continue; } besiger.Job = new Men.FleeJob(besiger, map_, colony_, this); --menStillNotFleeing_; foreach (Men other in besigers_.Where(b => b != besiger)) { other.Morale.Value -= 2f; } break; case "Atack": if (besiger.Job is Men.AttackJob) { continue; } besiger.Job = colony_.Colonist[rand_.Next(colony_.Colonist.Count)].AttackThis; break; } } } else if (State == Status.Flee) { foreach (Men besiger in besigers_.Where(besiger => besiger.Job == null || besiger.Job.GetType() != typeof(Men.FleeJob))) { besiger.Job = new Men.FleeJob(besiger, map_, colony_, this); } } else if (counter_ <= TimeSpan.Zero) { State = Status.Spawning; counter_ = new TimeSpan(0, rand_.Next(1, 15), rand_.Next(60)); } else if (State == Status.None) { counter_ -= e.Elapsed; } }