D:\ValheimDev\Dumps\Old\assembly_valheim\SpawnArea.cs D:\ValheimDev\Dumps\Latest\assembly_valheim\SpawnArea.cs
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
   
public class SpawnArea : MonoBehaviour public class SpawnArea : MonoBehaviour
{ {
    private void Awake()     private void Awake()
    {     {
        this.m_nview = base.GetComponent<ZNetView>();         this.m_nview = base.GetComponent<ZNetView>();
        base.InvokeRepeating("UpdateSpawn", 2f, 2f);         base.InvokeRepeating("UpdateSpawn", 2f, 2f);
    }     }
   
    private void UpdateSpawn()     private void UpdateSpawn()
    {     {
        if (!this.m_nview.IsOwner())         if (!this.m_nview.IsOwner())
        {         {
            return;             return;
        }         }
        if (ZNetScene.instance.OutsideActiveArea(base.transform.position))         if (ZNetScene.instance.OutsideActiveArea(base.transform.position))
        {         {
            return;             return;
        }         }
        if (!Player.IsPlayerInRange(base.transform.position, this.m_triggerDistance))         if (!Player.IsPlayerInRange(base.transform.position, this.m_triggerDistance))
        {         {
            return;             return;
        }         }
        this.m_spawnTimer += 2f;         this.m_spawnTimer += 2f;
        if (this.m_spawnTimer > this.m_spawnIntervalSec)         if (this.m_spawnTimer > this.m_spawnIntervalSec)
        {         {
            this.m_spawnTimer = 0f;             this.m_spawnTimer = 0f;
            this.SpawnOne();             this.SpawnOne();
        }         }
    }     }
   
    private bool SpawnOne()     private bool SpawnOne()
    {     {
        int num;         int num;
        int num2;         int num2;
        this.GetInstances(out num, out num2);         this.GetInstances(out num, out num2);
        if (num >= this.m_maxNear || num2 >= this.m_maxTotal)         if (num >= this.m_maxNear || num2 >= this.m_maxTotal)
        {         {
            return false;             return false;
        }         }
        SpawnArea.SpawnData spawnData = this.SelectWeightedPrefab();         SpawnArea.SpawnData spawnData = this.SelectWeightedPrefab();
        if (spawnData == null)         if (spawnData == null)
        {         {
            return false;             return false;
        }         }
        Vector3 vector;         Vector3 vector;
        if (!this.FindSpawnPoint(spawnData.m_prefab, out vector))         if (!this.FindSpawnPoint(spawnData.m_prefab, out vector))
        {         {
            return false;             return false;
        }         }
        GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(spawnData.m_prefab, vector, Quaternion.Euler(0f, (float)UnityEngine.Random.Range(0, 360), 0f));         GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(spawnData.m_prefab, vector, Quaternion.Euler(0f, (float)UnityEngine.Random.Range(0, 360), 0f));
        if (this.m_setPatrolSpawnPoint)         if (this.m_setPatrolSpawnPoint)
        {         {
            BaseAI component = gameObject.GetComponent<BaseAI>();             BaseAI component = gameObject.GetComponent<BaseAI>();
            if (component != null)             if (component != null)
            {             {
                component.SetPatrolPoint();                 component.SetPatrolPoint();
            }             }
        }         }
        Character component2 = gameObject.GetComponent<Character>();         Character component2 = gameObject.GetComponent<Character>();
        if (spawnData.m_maxLevel > 1)         if (spawnData.m_maxLevel > 1)
        {         {
            int num3 = spawnData.m_minLevel;             int num3 = spawnData.m_minLevel;
            while (num3 < spawnData.m_maxLevel && UnityEngine.Random.Range(0f, 100f) <= this.GetLevelUpChance())             while (num3 < spawnData.m_maxLevel && UnityEngine.Random.Range(0f, 100f) <= this.GetLevelUpChance())
            {             {
                num3++;                 num3++;
            }             }
            if (num3 > 1)             if (num3 > 1)
            {             {
                component2.SetLevel(num3);                 component2.SetLevel(num3);
            }             }
        }         }
        Vector3 centerPoint = component2.GetCenterPoint();         Vector3 centerPoint = component2.GetCenterPoint();
        this.m_spawnEffects.Create(centerPoint, Quaternion.identity, null, 1f, -1);         this.m_spawnEffects.Create(centerPoint, Quaternion.identity, null, 1f, -1);
        return true;         return true;
    }     }
   
    private bool FindSpawnPoint(GameObject prefab, out Vector3 point)     private bool FindSpawnPoint(GameObject prefab, out Vector3 point)
    {     {
        prefab.GetComponent<BaseAI>();         prefab.GetComponent<BaseAI>();
        for (int i = 0; i < 10; i++)         for (int i = 0; i < 10; i++)
        {         {
            Vector3 vector = base.transform.position + Quaternion.Euler(0f, (float)UnityEngine.Random.Range(0, 360), 0f) * Vector3.forward * UnityEngine.Random.Range(0f, this.m_spawnRadius);             Vector3 vector = base.transform.position + Quaternion.Euler(0f, (float)UnityEngine.Random.Range(0, 360), 0f) * Vector3.forward * UnityEngine.Random.Range(0f, this.m_spawnRadius);
            float num;             float num;
            if (ZoneSystem.instance.FindFloor(vector, out num) && (!this.m_onGroundOnly || !ZoneSystem.instance.IsBlocked(vector)))             if (ZoneSystem.instance.FindFloor(vector, out num) && (!this.m_onGroundOnly || !ZoneSystem.instance.IsBlocked(vector)))
            {             {
                vector.y = num + 0.1f;                 vector.y = num + 0.1f;
                point = vector;                 point = vector;
                return true;                 return true;
            }             }
        }         }
        point = Vector3.zero;         point = Vector3.zero;
        return false;         return false;
    }     }
   
    private SpawnArea.SpawnData SelectWeightedPrefab()     private SpawnArea.SpawnData SelectWeightedPrefab()
    {     {
        if (this.m_prefabs.Count == 0)         if (this.m_prefabs.Count == 0)
        {         {
            return null;             return null;
        }         }
        float num = 0f;         float num = 0f;
        foreach (SpawnArea.SpawnData spawnData in this.m_prefabs)         foreach (SpawnArea.SpawnData spawnData in this.m_prefabs)
        {         {
            num += spawnData.m_weight;             num += spawnData.m_weight;
        }         }
        float num2 = UnityEngine.Random.Range(0f, num);         float num2 = UnityEngine.Random.Range(0f, num);
        float num3 = 0f;         float num3 = 0f;
        foreach (SpawnArea.SpawnData spawnData2 in this.m_prefabs)         foreach (SpawnArea.SpawnData spawnData2 in this.m_prefabs)
        {         {
            num3 += spawnData2.m_weight;             num3 += spawnData2.m_weight;
            if (num2 <= num3)             if (num2 <= num3)
            {             {
                return spawnData2;                 return spawnData2;
            }             }
        }         }
        return this.m_prefabs[this.m_prefabs.Count - 1];         return this.m_prefabs[this.m_prefabs.Count - 1];
    }     }
   
    private void GetInstances(out int near, out int total)     private void GetInstances(out int near, out int total)
    {     {
        near = 0;         near = 0;
        total = 0;         total = 0;
        Vector3 position = base.transform.position;         Vector3 position = base.transform.position;
.        foreach (BaseAI baseAI in BaseAI.Instances)         foreach (BaseAI baseAI in BaseAI.BaseAIInstances)
        {         {
            if (this.IsSpawnPrefab(baseAI.gameObject))             if (this.IsSpawnPrefab(baseAI.gameObject))
            {             {
                float num = Utils.DistanceXZ(baseAI.transform.position, position);                 float num = Utils.DistanceXZ(baseAI.transform.position, position);
                if (num < this.m_nearRadius)                 if (num < this.m_nearRadius)
                {                 {
                    near++;                     near++;
                }                 }
                if (num < this.m_farRadius)                 if (num < this.m_farRadius)
                {                 {
                    total++;                     total++;
                }                 }
            }             }
        }         }
    }     }
   
    private bool IsSpawnPrefab(GameObject go)     private bool IsSpawnPrefab(GameObject go)
    {     {
        string name = go.name;         string name = go.name;
        Character component = go.GetComponent<Character>();         Character component = go.GetComponent<Character>();
        foreach (SpawnArea.SpawnData spawnData in this.m_prefabs)         foreach (SpawnArea.SpawnData spawnData in this.m_prefabs)
        {         {
            if (name.CustomStartsWith(spawnData.m_prefab.name) && (!component || !component.IsTamed()))             if (name.CustomStartsWith(spawnData.m_prefab.name) && (!component || !component.IsTamed()))
            {             {
                return true;                 return true;
            }             }
        }         }
        return false;         return false;
    }     }
   
    public float GetLevelUpChance()     public float GetLevelUpChance()
    {     {
        if (Game.m_worldLevel > 0 && Game.instance.m_worldLevelEnemyLevelUpExponent > 0f)         if (Game.m_worldLevel > 0 && Game.instance.m_worldLevelEnemyLevelUpExponent > 0f)
        {         {
            return Mathf.Min(70f, Mathf.Pow(this.m_levelupChance, (float)Game.m_worldLevel * Game.instance.m_worldLevelEnemyLevelUpExponent));             return Mathf.Min(70f, Mathf.Pow(this.m_levelupChance, (float)Game.m_worldLevel * Game.instance.m_worldLevelEnemyLevelUpExponent));
        }         }
        return this.m_levelupChance * Game.m_enemyLevelUpRate;         return this.m_levelupChance * Game.m_enemyLevelUpRate;
    }     }
   
    private void OnDrawGizmosSelected()     private void OnDrawGizmosSelected()
    {     {
        Gizmos.color = Color.red;         Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(base.transform.position, this.m_spawnRadius);         Gizmos.DrawWireSphere(base.transform.position, this.m_spawnRadius);
        Gizmos.color = Color.yellow;         Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(base.transform.position, this.m_nearRadius);         Gizmos.DrawWireSphere(base.transform.position, this.m_nearRadius);
    }     }
   
    private const float dt = 2f;     private const float dt = 2f;
   
    public List<SpawnArea.SpawnData> m_prefabs = new List<SpawnArea.SpawnData>();     public List<SpawnArea.SpawnData> m_prefabs = new List<SpawnArea.SpawnData>();
   
    public float m_levelupChance = 15f;     public float m_levelupChance = 15f;
   
    public float m_spawnIntervalSec = 30f;     public float m_spawnIntervalSec = 30f;
   
    public float m_triggerDistance = 256f;     public float m_triggerDistance = 256f;
   
    public bool m_setPatrolSpawnPoint = true;     public bool m_setPatrolSpawnPoint = true;
   
    public float m_spawnRadius = 2f;     public float m_spawnRadius = 2f;
   
    public float m_nearRadius = 10f;     public float m_nearRadius = 10f;
   
    public float m_farRadius = 1000f;     public float m_farRadius = 1000f;
   
    public int m_maxNear = 3;     public int m_maxNear = 3;
   
    public int m_maxTotal = 20;     public int m_maxTotal = 20;
   
    public bool m_onGroundOnly;     public bool m_onGroundOnly;
   
    public EffectList m_spawnEffects = new EffectList();     public EffectList m_spawnEffects = new EffectList();
   
    private ZNetView m_nview;     private ZNetView m_nview;
   
    private float m_spawnTimer;     private float m_spawnTimer;
   
    [Serializable]     [Serializable]
    public class SpawnData     public class SpawnData
    {     {
        public GameObject m_prefab;         public GameObject m_prefab;
   
        public float m_weight;         public float m_weight;
   
        [Header("Level")]         [Header("Level")]
        public int m_maxLevel = 1;         public int m_maxLevel = 1;
   
        public int m_minLevel = 1;         public int m_minLevel = 1;
    }     }
} }