summaryrefslogtreecommitdiff
path: root/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/TerrainModifier.cs
blob: 980483091c9675f112b6c66bb6d8e8de6b9d4cc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class TerrainModifier : MonoBehaviour
{
	public enum PaintType
	{
		Dirt,
		Cultivate,
		Paved,
		Reset
	}

	private static bool m_triggerOnPlaced = false;

	public int m_sortOrder;

	public bool m_playerModifiction;

	public float m_levelOffset;

	[Header("Level")]
	public bool m_level;

	public float m_levelRadius = 2f;

	public bool m_square = true;

	[Header("Smooth")]
	public bool m_smooth;

	public float m_smoothRadius = 2f;

	public float m_smoothPower = 3f;

	[Header("Paint")]
	public bool m_paintCleared = true;

	public bool m_paintHeightCheck;

	public PaintType m_paintType;

	public float m_paintRadius = 2f;

	[Header("Effects")]
	public EffectList m_onPlacedEffect = new EffectList();

	[Header("Spawn items")]
	public GameObject m_spawnOnPlaced;

	public float m_chanceToSpawn = 1f;

	public int m_maxSpawned = 1;

	public bool m_spawnAtMaxLevelDepth = true;

	private bool m_wasEnabled;

	private ZNetView m_nview;

	private static List<TerrainModifier> m_instances = new List<TerrainModifier>();

	private static bool m_needsSorting = false;

	private void Awake()
	{
		m_instances.Add(this);
		m_needsSorting = true;
		m_nview = GetComponent<ZNetView>();
		m_wasEnabled = base.enabled;
		if (base.enabled)
		{
			if (m_triggerOnPlaced)
			{
				OnPlaced();
			}
			PokeHeightmaps();
		}
	}

	private void OnDestroy()
	{
		m_instances.Remove(this);
		m_needsSorting = true;
		if (m_wasEnabled)
		{
			PokeHeightmaps();
		}
	}

	private void PokeHeightmaps()
	{
		bool delayed = !m_triggerOnPlaced;
		foreach (Heightmap allHeightmap in Heightmap.GetAllHeightmaps())
		{
			if (allHeightmap.TerrainVSModifier(this))
			{
				allHeightmap.Poke(delayed);
			}
		}
		if ((bool)ClutterSystem.instance)
		{
			ClutterSystem.instance.ResetGrass(base.transform.position, GetRadius());
		}
	}

	public float GetRadius()
	{
		float num = 0f;
		if (m_level && m_levelRadius > num)
		{
			num = m_levelRadius;
		}
		if (m_smooth && m_smoothRadius > num)
		{
			num = m_smoothRadius;
		}
		if (m_paintCleared && m_paintRadius > num)
		{
			num = m_paintRadius;
		}
		return num;
	}

	public static void SetTriggerOnPlaced(bool trigger)
	{
		m_triggerOnPlaced = trigger;
	}

	private void OnPlaced()
	{
		RemoveOthers(base.transform.position, GetRadius() / 4f);
		m_onPlacedEffect.Create(base.transform.position, Quaternion.identity);
		if ((bool)m_spawnOnPlaced && (m_spawnAtMaxLevelDepth || !Heightmap.AtMaxLevelDepth(base.transform.position + Vector3.up * m_levelOffset)) && Random.value <= m_chanceToSpawn)
		{
			Vector3 vector = Random.insideUnitCircle * 0.2f;
			GameObject obj = Object.Instantiate(m_spawnOnPlaced, base.transform.position + Vector3.up * 0.5f + vector, Quaternion.identity);
			obj.GetComponent<ItemDrop>().m_itemData.m_stack = Random.Range(1, m_maxSpawned + 1);
			obj.GetComponent<Rigidbody>().velocity = Vector3.up * 4f;
		}
	}

	private static void GetModifiers(Vector3 point, float range, List<TerrainModifier> modifiers, TerrainModifier ignore = null)
	{
		foreach (TerrainModifier instance in m_instances)
		{
			if (!(instance == ignore) && Utils.DistanceXZ(point, instance.transform.position) < range)
			{
				modifiers.Add(instance);
			}
		}
	}

	public static Piece FindClosestModifierPieceInRange(Vector3 point, float range)
	{
		float num = 999999f;
		TerrainModifier terrainModifier = null;
		foreach (TerrainModifier instance in m_instances)
		{
			if (!(instance.m_nview == null))
			{
				float num2 = Utils.DistanceXZ(point, instance.transform.position);
				if (!(num2 > range) && !(num2 > num))
				{
					num = num2;
					terrainModifier = instance;
				}
			}
		}
		if ((bool)terrainModifier)
		{
			return terrainModifier.GetComponent<Piece>();
		}
		return null;
	}

	private void RemoveOthers(Vector3 point, float range)
	{
		List<TerrainModifier> list = new List<TerrainModifier>();
		GetModifiers(point, range, list, this);
		int num = 0;
		foreach (TerrainModifier item in list)
		{
			if ((m_level || !item.m_level) && (!m_paintCleared || m_paintType != PaintType.Reset || (item.m_paintCleared && item.m_paintType == PaintType.Reset)) && (bool)item.m_nview)
			{
				num++;
				item.m_nview.ClaimOwnership();
				item.m_nview.Destroy();
			}
		}
	}

	private static int SortByModifiers(TerrainModifier a, TerrainModifier b)
	{
		if (a.m_playerModifiction == b.m_playerModifiction)
		{
			if (a.m_sortOrder == b.m_sortOrder)
			{
				return a.GetCreationTime().CompareTo(b.GetCreationTime());
			}
			return a.m_sortOrder.CompareTo(b.m_sortOrder);
		}
		return a.m_playerModifiction.CompareTo(b.m_playerModifiction);
	}

	public static List<TerrainModifier> GetAllInstances()
	{
		if (m_needsSorting)
		{
			m_instances.Sort(SortByModifiers);
			m_needsSorting = false;
		}
		return m_instances;
	}

	private void OnDrawGizmosSelected()
	{
		Gizmos.matrix = Matrix4x4.TRS(base.transform.position + Vector3.up * m_levelOffset, Quaternion.identity, new Vector3(1f, 0f, 1f));
		if (m_level)
		{
			Gizmos.color = Color.green;
			Gizmos.DrawWireSphere(Vector3.zero, m_levelRadius);
		}
		if (m_smooth)
		{
			Gizmos.color = Color.blue;
			Gizmos.DrawWireSphere(Vector3.zero, m_smoothRadius);
		}
		if (m_paintCleared)
		{
			Gizmos.color = Color.yellow;
			Gizmos.DrawWireSphere(Vector3.zero, m_paintRadius);
		}
		Gizmos.matrix = Matrix4x4.identity;
	}

	public long GetCreationTime()
	{
		if ((bool)m_nview && m_nview.GetZDO() != null)
		{
			return m_nview.GetZDO().m_timeCreated;
		}
		return 0L;
	}
}