public static Animal averageAnimals(List<Animal> animals) { Animal average = new Animal(); float b = 0; float d = 0; float resp = 1.0f / (float)animals.Count; foreach (Animal a in animals) { average.accumulated_energy += a.accumulated_energy * resp; average.age += a.age * resp; average.amount_eaten += a.amount_eaten * resp; average.attack_cooldown += a.attack_cooldown * resp; average.attack_rate += a.attack_rate * resp; average.attacked_by_cooldown += a.attacked_by_cooldown * resp; average.breeding_age += a.breeding_age * resp; average.breeding_count += a.breeding_count * resp; average.breeding_rate += a.breeding_rate * resp; average.damage_base += a.damage_base * resp; average.aggression += a.aggression * resp; average.deathtime += a.deathtime * resp; average.dexterity += a.dexterity * resp; average.diet += a.diet * resp; average.energy += a.energy * resp; average.entity_mass += a.entity_mass * resp; average.entity_size += a.entity_size * resp; average.facing += a.facing * resp; average.fertility += a.fertility * resp; average.health += a.health * resp; average.hunger_damage_cooldown += a.hunger_damage_cooldown * resp; average.hunger_heal_cooldown += a.hunger_heal_cooldown * resp; average.hunger_limits_breed += a.hunger_limits_breed * resp; average.hunger_limits_lower += a.hunger_limits_lower * resp; average.hunger_limits_upper += a.hunger_limits_upper * resp; average.intelligence += a.intelligence * resp; average.life_expectancy += a.life_expectancy * resp; average.mate_find_cooldown += a.mate_find_cooldown * resp; average.maxHealth += a.maxHealth * resp; average.mutation_amount += a.mutation_amount * resp; average.mutation_rate += a.mutation_rate * resp; average.rest_energy_per_day += a.rest_energy_per_day * resp; average.running_speed += a.running_speed * resp; average.sight_radius += a.sight_radius * resp; average.strength += a.strength * resp; average.turning_speed += a.turning_speed * resp; average.walking_speed += a.walking_speed * resp; average.local_population_max += a.local_population_max * resp; average.desired_num_children += a.desired_num_children * resp; average.numAliveChildren += a.numAliveChildren * resp; average.position.x += a.position.x * resp; average.position.y += a.position.y * resp; average.destination.location.x = a.destination.location.x * resp; average.destination.location.y = a.destination.location.y * resp; foreach (KeyValuePair<string, int> s in a.deathbys) { if (average.deathbys.ContainsKey(s.Key)) { average.deathbys[s.Key] += s.Value; } else { average.deathbys[s.Key] = s.Value; } } b += convertDate(a.birthdate); d += convertDate(a.deathdate); } average.birthdate = convertDate(b); average.deathdate = convertDate(d); return average; }
void worker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (cc != null && cc.Length > 0) { bool findState = false; string endState = null; string state = "none"; char tile = '\0'; int mapWidth = -1; Dictionary<string, float> values = new Dictionary<string, float>(); int numAnimals = 0; int oldPercent = -1; for (int i = 0; i < cc.Length; i++) { string line = cc[i]; if (line == "--") { findState = true; continue; } int percent = (int)(((float)i / (float)cc.Length) * 100); if (percent != oldPercent) { oldPercent = percent; worker.ReportProgress(percent); } if (findState) { findState = false; endState = line; } if (state == null || state.Equals("none", StringComparison.CurrentCultureIgnoreCase) || state.Equals("TileData", StringComparison.CurrentCultureIgnoreCase)) { } else if (state.Equals("tiles", StringComparison.CurrentCultureIgnoreCase)) { if (mCharCount < mColors.Length) { if (tile == '\0') { tile = line[0]; } else { int t = int.Parse(line); if (!mColourMap.ContainsKey(tile)) { Console.WriteLine("Adding char {0} with colour {1}", tile, mColors[t].ToKnownColor().ToString()); mColourMap[tile] = mColors[t]; //addColour(mColors[t], tile, true); mCharCount++; tile = '\0'; } } } } else if (state.Equals("options", StringComparison.CurrentCultureIgnoreCase)) { switch (line) { case "current_time": mCurrentTime = float.Parse(cc[++i]); break; case "current_day": mCurrentDay = int.Parse(cc[++i]); break; case "day_length": mDayLength = float.Parse(cc[++i]); break; default: break; } } else if (state.Equals("map", StringComparison.CurrentCultureIgnoreCase)) { if (endState != null) { renderImage(ref mMapTotal); } else { if (mMapList.Count == 0) { mapWidth = line.Length; } mMapList.Add(line); } } else if (state.Equals("entities", StringComparison.CurrentCultureIgnoreCase) || state.Equals("RemovedEntities", StringComparison.CurrentCultureIgnoreCase)) { if (line == "Animal") { i++; Animal a = new Animal(); a.load(ref cc, ref i); i--; string identifier = a.graphic.ToString() + '_' + a.species; if (!mPopulations.ContainsKey(identifier)) { mPopulations[identifier] = new PopulationStat(); } if (a.isDead) { mPopulations[identifier].dead++; } else { mPopulations[identifier].alive++; } } } else if (state.Equals("history", StringComparison.CurrentCultureIgnoreCase)) { if (line.Equals("DayEvent", StringComparison.CurrentCultureIgnoreCase)) { DayEvents day = new DayEvents(); day.load(ref cc, ref i); mHistory.Add(day); } } else { Console.WriteLine("Unknown state {0}", state); } if (endState != null) { state = endState; endState = null; } } mValues = new List<KeyPair>(); foreach (KeyValuePair<string, float> pair in values) { float value = pair.Value / numAnimals; KeyPair p = new KeyPair(pair.Key, value); if (pair.Key.Equals("diet", StringComparison.CurrentCultureIgnoreCase)) { p.max = 1.0f; } mValues.Add(p); } } }