Exemplo n.º 1
0
    private void EnforceLimits(SpawnPopulation population, SpawnDistribution distribution)
    {
        int targetCount = this.GetTargetCount(population, distribution);

        Spawnable[] all = this.FindAll(population);
        if (all.Length <= targetCount)
        {
            return;
        }
        Debug.Log((object)(population.ToString() + " has " + (object)all.Length + " objects, but max allowed is " + (object)targetCount));
        int count = all.Length - targetCount;

        Debug.Log((object)(" - deleting " + (object)count + " objects"));
        foreach (Spawnable spawnable in ((IEnumerable <Spawnable>)all).Take <Spawnable>(count))
        {
            BaseEntity baseEntity = ((Component)spawnable).get_gameObject().ToBaseEntity();
            if (baseEntity.IsValid())
            {
                baseEntity.Kill(BaseNetworkable.DestroyMode.None);
            }
            else
            {
                GameManager.Destroy(((Component)spawnable).get_gameObject(), 0.0f);
            }
        }
    }
    public void SpawnInitial(SpawnPopulation population, SpawnDistribution distribution)
    {
        int targetCount  = this.GetTargetCount(population, distribution);
        int currentCount = targetCount - this.GetCurrentCount(population, distribution);

        this.Spawn(population, distribution, targetCount, currentCount, currentCount * population.SpawnAttemptsInitial);
    }
    private void EnforceLimits(SpawnPopulation population, SpawnDistribution distribution)
    {
        int targetCount = this.GetTargetCount(population, distribution);

        Spawnable[] spawnableArray = this.FindAll(population);
        if ((int)spawnableArray.Length <= targetCount)
        {
            return;
        }
        UnityEngine.Debug.Log(string.Concat(new object[] { population, " has ", (int)spawnableArray.Length, " objects, but max allowed is ", targetCount }));
        int length = (int)spawnableArray.Length - targetCount;

        UnityEngine.Debug.Log(string.Concat(" - deleting ", length, " objects"));
        foreach (Spawnable spawnable in spawnableArray.Take <Spawnable>(length))
        {
            BaseEntity baseEntity = spawnable.gameObject.ToBaseEntity();
            if (!baseEntity.IsValid())
            {
                GameManager.Destroy(spawnable.gameObject, 0f);
            }
            else
            {
                baseEntity.Kill(BaseNetworkable.DestroyMode.None);
            }
        }
    }
Exemplo n.º 4
0
    private IEnumerator SpawnTick()
    {
label_1:
        do
        {
            yield return((object)CoroutineEx.waitForEndOfFrame);
        }while (!this.spawnTick || !Spawn.respawn_populations);
        yield return((object)CoroutineEx.waitForSeconds(Spawn.tick_populations));

        for (int i = 0; i < this.AllSpawnPopulations.Length; ++i)
        {
            SpawnPopulation allSpawnPopulation = this.AllSpawnPopulations[i];
            if (!((BaseScriptableObject)allSpawnPopulation == (BaseScriptableObject)null))
            {
                SpawnDistribution spawnDistribution = this.SpawnDistributions[i];
                if (spawnDistribution != null)
                {
                    try
                    {
                        if (this.SpawnDistributions != null)
                        {
                            this.SpawnRepeating(allSpawnPopulation, spawnDistribution);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.LogError((object)ex);
                    }
                    yield return((object)CoroutineEx.waitForEndOfFrame);
                }
            }
        }
        goto label_1;
    }
Exemplo n.º 5
0
    public void SpawnRepeating(SpawnPopulation population, SpawnDistribution distribution)
    {
        int targetCount  = this.GetTargetCount(population, distribution);
        int currentCount = this.GetCurrentCount(population, distribution);
        int num          = Mathf.RoundToInt((float)(targetCount - currentCount) * population.GetCurrentSpawnRate());
        int numToFill    = Random.Range(Mathf.Min(num, this.MinSpawnsPerTick), Mathf.Min(num, this.MaxSpawnsPerTick));

        this.Spawn(population, distribution, targetCount, numToFill, numToFill * population.SpawnAttemptsRepeating);
    }
    public void SpawnRepeating(SpawnPopulation population, SpawnDistribution distribution)
    {
        int targetCount  = this.GetTargetCount(population, distribution);
        int currentCount = targetCount - this.GetCurrentCount(population, distribution);

        currentCount = Mathf.RoundToInt((float)currentCount * population.GetCurrentSpawnRate());
        currentCount = UnityEngine.Random.Range(Mathf.Min(currentCount, this.MinSpawnsPerTick), Mathf.Min(currentCount, this.MaxSpawnsPerTick));
        this.Spawn(population, distribution, targetCount, currentCount, currentCount * population.SpawnAttemptsRepeating);
    }
    public int GetTargetCount(SpawnPopulation population, SpawnDistribution distribution)
    {
        float size = TerrainMeta.Size.x * TerrainMeta.Size.z;
        float currentSpawnDensity = population.GetCurrentSpawnDensity();

        if (population.ScaleWithSpawnFilter)
        {
            currentSpawnDensity *= distribution.Density;
        }
        return(Mathf.RoundToInt(size * currentSpawnDensity));
    }
    public void UpdateDistributions()
    {
        if (World.Size == 0)
        {
            return;
        }
        this.SpawnDistributions      = new SpawnDistribution[(int)this.AllSpawnPopulations.Length];
        this.population2distribution = new Dictionary <SpawnPopulation, SpawnDistribution>();
        Vector3 size     = TerrainMeta.Size;
        Vector3 position = TerrainMeta.Position;
        int     num      = Mathf.NextPowerOfTwo((int)((float)((float)World.Size) * 0.25f));

        for (int i1 = 0; i1 < (int)this.AllSpawnPopulations.Length; i1++)
        {
            SpawnPopulation allSpawnPopulations = this.AllSpawnPopulations[i1];
            if (allSpawnPopulations != null)
            {
                byte[]      factor = new byte[num * num];
                SpawnFilter filter = allSpawnPopulations.Filter;
                Parallel.For(0, num, (int z) => {
                    for (int i = 0; i < num; i++)
                    {
                        float popRes        = ((float)i + 0.5f) / (float)num;
                        float single        = ((float)z + 0.5f) / (float)num;
                        factor[z * num + i] = (byte)(255f * filter.GetFactor(popRes, single));
                    }
                });
                SpawnDistribution[] spawnDistributions = this.SpawnDistributions;
                SpawnDistribution   spawnDistribution  = new SpawnDistribution(this, factor, position, size);
                SpawnDistribution   spawnDistribution1 = spawnDistribution;
                spawnDistributions[i1] = spawnDistribution;
                this.population2distribution.Add(allSpawnPopulations, spawnDistribution1);
            }
            else
            {
                UnityEngine.Debug.LogError("Spawn handler contains null spawn population.");
            }
        }
        int num1 = Mathf.NextPowerOfTwo((int)((float)((float)World.Size) * 0.5f));

        byte[]      numArray       = new byte[num1 * num1];
        SpawnFilter characterSpawn = this.CharacterSpawn;

        Parallel.For(0, num1, (int z) => {
            for (int i = 0; i < num1; i++)
            {
                float charRes          = ((float)i + 0.5f) / (float)num1;
                float single           = ((float)z + 0.5f) / (float)num1;
                numArray[z * num1 + i] = (byte)(255f * characterSpawn.GetFactor(charRes, single));
            }
        });
        this.CharDistribution = new SpawnDistribution(this, numArray, position, size);
    }
Exemplo n.º 9
0
    public void UpdateDistributions()
    {
        if (World.Size == 0U)
        {
            return;
        }
        this.SpawnDistributions      = new SpawnDistribution[this.AllSpawnPopulations.Length];
        this.population2distribution = new Dictionary <SpawnPopulation, SpawnDistribution>();
        Vector3 size     = TerrainMeta.Size;
        Vector3 position = TerrainMeta.Position;
        int     pop_res  = Mathf.NextPowerOfTwo((int)((double)World.Size * 0.25));

        for (int index1 = 0; index1 < this.AllSpawnPopulations.Length; ++index1)
        {
            SpawnPopulation allSpawnPopulation = this.AllSpawnPopulations[index1];
            if ((BaseScriptableObject)allSpawnPopulation == (BaseScriptableObject)null)
            {
                Debug.LogError((object)"Spawn handler contains null spawn population.");
            }
            else
            {
                byte[]      map    = new byte[pop_res * pop_res];
                SpawnFilter filter = allSpawnPopulation.Filter;
                Parallel.For(0, pop_res, (Action <int>)(z =>
                {
                    for (int index = 0; index < pop_res; ++index)
                    {
                        float normX = ((float)index + 0.5f) / (float)pop_res;
                        float normZ = ((float)z + 0.5f) / (float)pop_res;
                        map[z * pop_res + index] = (byte)((double)byte.MaxValue * (double)filter.GetFactor(normX, normZ));
                    }
                }));
                SpawnDistribution spawnDistribution = this.SpawnDistributions[index1] = new SpawnDistribution(this, map, position, size);
                this.population2distribution.Add(allSpawnPopulation, spawnDistribution);
            }
        }
        int char_res = Mathf.NextPowerOfTwo((int)((double)World.Size * 0.5));

        byte[]      map1    = new byte[char_res * char_res];
        SpawnFilter filter1 = this.CharacterSpawn;

        Parallel.For(0, char_res, (Action <int>)(z =>
        {
            for (int index = 0; index < char_res; ++index)
            {
                float normX = ((float)index + 0.5f) / (float)char_res;
                float normZ = ((float)z + 0.5f) / (float)char_res;
                map1[z * char_res + index] = (byte)((double)byte.MaxValue * (double)filter1.GetFactor(normX, normZ));
            }
        }));
        this.CharDistribution = new SpawnDistribution(this, map1, position, size);
    }
Exemplo n.º 10
0
    public int GetTargetCount(SpawnPopulation population, SpawnDistribution distribution)
    {
        // ISSUE: variable of the null type
        __Null local = TerrainMeta.Size.x * TerrainMeta.Size.z;
        float  currentSpawnDensity = population.GetCurrentSpawnDensity();

        if (population.ScaleWithSpawnFilter)
        {
            currentSpawnDensity *= distribution.Density;
        }
        double num = (double)currentSpawnDensity;

        return(Mathf.RoundToInt((float)(local * num)));
    }
Exemplo n.º 11
0
 public void EnforceLimits(bool forceAll = false)
 {
     if (this.SpawnDistributions == null)
     {
         return;
     }
     for (int i = 0; i < (int)this.AllSpawnPopulations.Length; i++)
     {
         if (this.AllSpawnPopulations[i] != null)
         {
             SpawnPopulation   allSpawnPopulations = this.AllSpawnPopulations[i];
             SpawnDistribution spawnDistributions  = this.SpawnDistributions[i];
             if (forceAll || allSpawnPopulations.EnforcePopulationLimits)
             {
                 this.EnforceLimits(allSpawnPopulations, spawnDistributions);
             }
         }
     }
 }
Exemplo n.º 12
0
 public void EnforceLimits(bool forceAll = false)
 {
     if (this.SpawnDistributions == null)
     {
         return;
     }
     for (int index = 0; index < this.AllSpawnPopulations.Length; ++index)
     {
         if (!((BaseScriptableObject)this.AllSpawnPopulations[index] == (BaseScriptableObject)null))
         {
             SpawnPopulation   allSpawnPopulation = this.AllSpawnPopulations[index];
             SpawnDistribution spawnDistribution  = this.SpawnDistributions[index];
             if (forceAll || allSpawnPopulation.EnforcePopulationLimits)
             {
                 this.EnforceLimits(allSpawnPopulation, spawnDistribution);
             }
         }
     }
 }
Exemplo n.º 13
0
 private void Spawn(
     SpawnPopulation population,
     SpawnDistribution distribution,
     int targetCount,
     int numToFill,
     int numToTry)
 {
     if (targetCount == 0)
     {
         return;
     }
     if (!population.Initialize())
     {
         Debug.LogError((object)("[Spawn] No prefabs to spawn in " + population.ResourceFolder), (Object)population);
     }
     else
     {
         if (Global.developer > 1)
         {
             Debug.Log((object)("[Spawn] Population " + population.ResourceFolder + " needs to spawn " + (object)numToFill));
         }
         float num1 = Mathf.Max((float)population.ClusterSizeMax, distribution.GetGridCellArea() * population.GetMaximumSpawnDensity());
         population.UpdateWeights(distribution, targetCount);
         while (numToFill >= population.ClusterSizeMin && numToTry > 0)
         {
             ByteQuadtree.Element node = distribution.SampleNode();
             int num2 = Random.Range(population.ClusterSizeMin, population.ClusterSizeMax + 1);
             int num3 = Mathx.Min(numToTry, numToFill, num2);
             for (int index = 0; index < num3; ++index)
             {
                 Vector3    spawnPos;
                 Quaternion spawnRot;
                 if (distribution.Sample(out spawnPos, out spawnRot, node, population.AlignToNormal, (float)population.ClusterDithering) && (double)population.Filter.GetFactor(spawnPos) > 0.0 && (double)distribution.GetCount(spawnPos) < (double)num1)
                 {
                     this.Spawn(population, spawnPos, spawnRot);
                     --numToFill;
                 }
                 --numToTry;
             }
         }
     }
 }
Exemplo n.º 14
0
    private IEnumerator SpawnTick()
    {
        SpawnHandler spawnHandler = null;

        while (true)
        {
            yield return(CoroutineEx.waitForEndOfFrame);

            if (spawnHandler.spawnTick && Spawn.respawn_populations)
            {
                yield return(CoroutineEx.waitForSeconds(Spawn.tick_populations));

                for (int i = 0; i < (int)spawnHandler.AllSpawnPopulations.Length; i++)
                {
                    SpawnPopulation allSpawnPopulations = spawnHandler.AllSpawnPopulations[i];
                    if (allSpawnPopulations != null)
                    {
                        SpawnDistribution spawnDistributions = spawnHandler.SpawnDistributions[i];
                        if (spawnDistributions != null)
                        {
                            try
                            {
                                if (spawnHandler.SpawnDistributions != null)
                                {
                                    spawnHandler.SpawnRepeating(allSpawnPopulations, spawnDistributions);
                                }
                            }
                            catch (Exception exception)
                            {
                                UnityEngine.Debug.LogError(exception);
                            }
                            yield return(CoroutineEx.waitForEndOfFrame);
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 15
0
    public void UpdateWeights(SpawnDistribution distribution, int targetCount)
    {
        int num;
        int num1 = 0;

        for (int i = 0; i < (int)this.Prefabs.Length; i++)
        {
            Prefab <Spawnable> prefabs = this.Prefabs[i];
            num1 += (prefabs.Parameters ? prefabs.Parameters.Count : 1);
        }
        int num2 = Mathf.CeilToInt((float)targetCount / (float)num1);

        this.sumToSpawn = 0;
        for (int j = 0; j < (int)this.Prefabs.Length; j++)
        {
            Prefab <Spawnable> prefab = this.Prefabs[j];
            num = (prefab.Parameters ? prefab.Parameters.Count : 1);
            int count = distribution.GetCount(prefab.ID);
            int num3  = Mathf.Max(num * num2 - count, 0);
            this.numToSpawn[j] = num3;
            this.sumToSpawn   += num3;
        }
    }
Exemplo n.º 16
0
    private void Spawn(SpawnPopulation population, SpawnDistribution distribution, int targetCount, int numToFill, int numToTry)
    {
        Vector3    vector3;
        Quaternion quaternion;

        if (targetCount == 0)
        {
            return;
        }
        if (!population.Initialize())
        {
            UnityEngine.Debug.LogError(string.Concat("[Spawn] No prefabs to spawn in ", population.ResourceFolder), population);
            return;
        }
        if (Global.developer > 1)
        {
            UnityEngine.Debug.Log(string.Concat(new object[] { "[Spawn] Population ", population.ResourceFolder, " needs to spawn ", numToFill }));
        }
        float single = Mathf.Max((float)population.ClusterSizeMax, distribution.GetGridCellArea() * population.GetMaximumSpawnDensity());

        population.UpdateWeights(distribution, targetCount);
        while (numToFill >= population.ClusterSizeMin && numToTry > 0)
        {
            ByteQuadtree.Element element = distribution.SampleNode();
            int num = UnityEngine.Random.Range(population.ClusterSizeMin, population.ClusterSizeMax + 1);
            num = Mathx.Min(numToTry, numToFill, num);
            for (int i = 0; i < num; i++)
            {
                if (distribution.Sample(out vector3, out quaternion, element, population.AlignToNormal, (float)population.ClusterDithering) && population.Filter.GetFactor(vector3) > 0f && (float)distribution.GetCount(vector3) < single)
                {
                    this.Spawn(population, vector3, quaternion);
                    numToFill--;
                }
                numToTry--;
            }
        }
    }
Exemplo n.º 17
0
    public void UpdateWeights(SpawnDistribution distribution, int targetCount)
    {
        int num1 = 0;

        for (int index = 0; index < this.Prefabs.Length; ++index)
        {
            Prefab <Spawnable> prefab = this.Prefabs[index];
            int num2 = Object.op_Implicit((Object)prefab.Parameters) ? prefab.Parameters.Count : 1;
            num1 += num2;
        }
        int num3 = Mathf.CeilToInt((float)targetCount / (float)num1);

        this.sumToSpawn = 0;
        for (int index = 0; index < this.Prefabs.Length; ++index)
        {
            Prefab <Spawnable> prefab = this.Prefabs[index];
            int num2  = Object.op_Implicit((Object)prefab.Parameters) ? prefab.Parameters.Count : 1;
            int count = distribution.GetCount(prefab.ID);
            int num4  = num3;
            int num5  = Mathf.Max(num2 * num4 - count, 0);
            this.numToSpawn[index] = num5;
            this.sumToSpawn       += num5;
        }
    }
Exemplo n.º 18
0
    public string GetReport(bool detailed = true)
    {
        StringBuilder stringBuilder = new StringBuilder();

        if (this.AllSpawnPopulations == null)
        {
            stringBuilder.AppendLine("Spawn population array is null.");
        }
        if (this.SpawnDistributions == null)
        {
            stringBuilder.AppendLine("Spawn distribution array is null.");
        }
        if (this.AllSpawnPopulations != null && this.SpawnDistributions != null)
        {
            for (int index = 0; index < this.AllSpawnPopulations.Length; ++index)
            {
                if (!((BaseScriptableObject)this.AllSpawnPopulations[index] == (BaseScriptableObject)null))
                {
                    SpawnPopulation   allSpawnPopulation = this.AllSpawnPopulations[index];
                    SpawnDistribution spawnDistribution  = this.SpawnDistributions[index];
                    if ((BaseScriptableObject)allSpawnPopulation != (BaseScriptableObject)null)
                    {
                        if (!string.IsNullOrEmpty(allSpawnPopulation.ResourceFolder))
                        {
                            stringBuilder.AppendLine(((Object)allSpawnPopulation).get_name() + " (autospawn/" + allSpawnPopulation.ResourceFolder + ")");
                        }
                        else
                        {
                            stringBuilder.AppendLine(((Object)allSpawnPopulation).get_name());
                        }
                        if (detailed)
                        {
                            stringBuilder.AppendLine("\tPrefabs:");
                            if (allSpawnPopulation.Prefabs != null)
                            {
                                foreach (Prefab <Spawnable> prefab in allSpawnPopulation.Prefabs)
                                {
                                    stringBuilder.AppendLine("\t\t" + prefab.Name + " - " + (object)prefab.Object);
                                }
                            }
                            else
                            {
                                stringBuilder.AppendLine("\t\tN/A");
                            }
                        }
                        if (spawnDistribution != null)
                        {
                            int currentCount = this.GetCurrentCount(allSpawnPopulation, spawnDistribution);
                            int targetCount  = this.GetTargetCount(allSpawnPopulation, spawnDistribution);
                            stringBuilder.AppendLine("\tPopulation: " + (object)currentCount + "/" + (object)targetCount);
                        }
                        else
                        {
                            stringBuilder.AppendLine("\tDistribution #" + (object)index + " is not set.");
                        }
                    }
                    else
                    {
                        stringBuilder.AppendLine("Population #" + (object)index + " is not set.");
                    }
                    stringBuilder.AppendLine();
                }
            }
        }
        return(stringBuilder.ToString());
    }
Exemplo n.º 19
0
 public int GetCurrentCount(SpawnPopulation population, SpawnDistribution distribution)
 {
     return(distribution.Count);
 }
Exemplo n.º 20
0
    public string GetReport(bool detailed = true)
    {
        StringBuilder stringBuilder = new StringBuilder();

        if (this.AllSpawnPopulations == null)
        {
            stringBuilder.AppendLine("Spawn population array is null.");
        }
        if (this.SpawnDistributions == null)
        {
            stringBuilder.AppendLine("Spawn distribution array is null.");
        }
        if (this.AllSpawnPopulations != null && this.SpawnDistributions != null)
        {
            for (int i = 0; i < (int)this.AllSpawnPopulations.Length; i++)
            {
                if (this.AllSpawnPopulations[i] != null)
                {
                    SpawnPopulation   allSpawnPopulations = this.AllSpawnPopulations[i];
                    SpawnDistribution spawnDistributions  = this.SpawnDistributions[i];
                    if (allSpawnPopulations == null)
                    {
                        stringBuilder.AppendLine(string.Concat("Population #", i, " is not set."));
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(allSpawnPopulations.ResourceFolder))
                        {
                            stringBuilder.AppendLine(allSpawnPopulations.name);
                        }
                        else
                        {
                            stringBuilder.AppendLine(string.Concat(allSpawnPopulations.name, " (autospawn/", allSpawnPopulations.ResourceFolder, ")"));
                        }
                        if (detailed)
                        {
                            stringBuilder.AppendLine("\tPrefabs:");
                            if (allSpawnPopulations.Prefabs == null)
                            {
                                stringBuilder.AppendLine("\t\tN/A");
                            }
                            else
                            {
                                Prefab <Spawnable>[] prefabs = allSpawnPopulations.Prefabs;
                                for (int j = 0; j < (int)prefabs.Length; j++)
                                {
                                    Prefab <Spawnable> prefab = prefabs[j];
                                    stringBuilder.AppendLine(string.Concat(new object[] { "\t\t", prefab.Name, " - ", prefab.Object }));
                                }
                            }
                        }
                        if (spawnDistributions == null)
                        {
                            stringBuilder.AppendLine(string.Concat("\tDistribution #", i, " is not set."));
                        }
                        else
                        {
                            int currentCount = this.GetCurrentCount(allSpawnPopulations, spawnDistributions);
                            int targetCount  = this.GetTargetCount(allSpawnPopulations, spawnDistributions);
                            stringBuilder.AppendLine(string.Concat(new object[] { "\tPopulation: ", currentCount, "/", targetCount }));
                        }
                    }
                    stringBuilder.AppendLine();
                }
            }
        }
        return(stringBuilder.ToString());
    }