private void EnsurePopulation() { RecalculateEnemyCount(); var state = new int[12]; var diff = new int[12]; var c = 0; for (var i = 0; i < state.Length; i++) { if (enemyCounts[i] > enemyMaxCounts[i] * 1.5) //Kill some { state[i] = 1; diff[i] = enemyCounts[i] - enemyMaxCounts[i]; c++; } else if (enemyCounts[i] < enemyMaxCounts[i] * realmevent) //Add some (realm events percent by dev) { state[i] = 2; diff[i] = enemyMaxCounts[i] - enemyCounts[i]; } else { state[i] = 0; } } foreach (var i in world.Enemies) //Kill { var idx = (int)i.Value.Terrain - 1; float dist = 10; if (idx == -1 || state[idx] == 0 || BehaviorBase.GetNearestEntity(i.Value, ref dist, true) != null || diff[idx] == 0) { continue; } if (state[idx] == 1) { world.LeaveWorld(i.Value); diff[idx]--; if (diff[idx] == 0) { c--; } } if (c == 0) { break; } } int w = world.Map.Width, h = world.Map.Height; for (var i = 0; i < state.Length; i++) //Add { if (state[i] != 2) { continue; } var x = diff[i]; var t = (WmapTerrain)(i + 1); for (var j = 0; j < x;) { var objType = GetRandomObjType(spawn[t].Item2); if (objType == 0) { continue; } j += Spawn(XmlDatas.ObjectDescs[objType], t, w, h); } } RecalculateEnemyCount(); GC.Collect(); }