forked from Hazardu/ChampionsOfForest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModReferences.cs
334 lines (306 loc) · 8.5 KB
/
ModReferences.cs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ModAPI;
using TheForest.Utils;
using UnityEngine;
namespace ChampionsOfForest
{
public class ModReferences : MonoBehaviour
{
private float LevelRequestCooldown = 10;
private float MFindRequestCooldown = 300;
public static Material bloodInfusedMaterial;
private static ModReferences instance;
public static ClientItemPicker ItemPicker => ClientItemPicker.Instance;
public static List<IPlayerState> PlayerStates = new List<IPlayerState>();
public static List<GameObject> Players
{
get;
private set;
}
public static string ThisPlayerID
{
get;
private set;
}
public static List<BoltEntity> AllPlayerEntities = new List<BoltEntity>();
public static Dictionary<string, int> PlayerLevels = new Dictionary<string, int>();
public static Dictionary<string, Transform> PlayerHands = new Dictionary<string, Transform>();
public static Transform rightHandTransform = null;
private void Start()
{
instance = this;
if (BoltNetwork.isRunning)
{
Players = new List<GameObject>();
StartCoroutine(InitPlayerID());
StartCoroutine(UpdateSetups());
if (GameSetup.IsMpServer && BoltNetwork.isRunning)
{
InvokeRepeating("UpdateLevelData", 1, 1);
}
PlayerLevels.Clear();
}
else
{
Players = new List<GameObject>() { LocalPlayer.GameObject };
}
}
public static void SendRandomItemDrops(int count, EnemyProgression.Enemy type,long bounty, ModSettings.Difficulty difficulty, Vector3 position)
{
instance.StartCoroutine(Player.RCoroutines.i.AsyncSendRandomItemDrops(count, type, bounty,difficulty, position));
}
private IEnumerator InitPlayerID()
{
if (GameSetup.IsSinglePlayer)
{
ThisPlayerID = "LocalPlayer";
}
if (ModSettings.IsDedicated)
{
yield break;
}
while (LocalPlayer.Entity == null)
{
yield return null;
}
ThisPlayerID = LocalPlayer.Entity.GetState<IPlayerState>().name;
}
float lastLevelCheckTimestamp;
private int lastPlayerLevelCount;
public static void UpdatePlayerLevel(string nameOfPlayer, int level)
{
for (int i = 0; i < Scene.SceneTracker.allPlayers.Count; i++)
{
var state = Scene.SceneTracker.allPlayers[i].GetComponent<BoltEntity>().GetState<IPlayerState>();
if (state!= null)
{
if (state.name == nameOfPlayer)
{
Scene.SceneTracker.allPlayers[i].GetComponentInChildren<TheForest.UI.Multiplayer.PlayerName>().SendMessage("SetNameWithLevel",level);
break;
}
}
}
}
public static void RemoveUnusedPlayerLevels()
{
//todo fix this
List<string> stringsToRemove = new List<string>();
foreach (var item in PlayerLevels)
{
if (!PlayerStates.Any(x => x.name == item.Key))
{
stringsToRemove.Add(item.Key);
}
}
ModAPI.Console.Write("Removing old player level data: count=" + stringsToRemove.Count);
//foreach (var item in stringsToRemove)
//{
// PlayerLevels.Remove(item);
//}
}
public static void RequestAllPlayerLevels()
{
if (Time.time - instance.lastLevelCheckTimestamp > 120|| instance.lastPlayerLevelCount != Players.Count)
{
RemoveUnusedPlayerLevels();
instance.LevelRequestCooldown = 120;
instance.lastLevelCheckTimestamp = Time.time;
instance.lastPlayerLevelCount = Players.Count;
using (System.IO.MemoryStream answerStream = new System.IO.MemoryStream())
{
using (System.IO.BinaryWriter w = new System.IO.BinaryWriter(answerStream))
{
w.Write(18);
w.Write("x");
w.Close();
}
Network.NetworkManager.SendLine(answerStream.ToArray(), Network.NetworkManager.Target.Clients);
answerStream.Close();
}
}
}
private void UpdateLevelData()
{
if (Players.Count > 1)
{
LevelRequestCooldown -= 1;
if (LevelRequestCooldown < 0 || lastPlayerLevelCount != Players.Count)
{
LevelRequestCooldown = 120;
lastLevelCheckTimestamp = Time.time;
lastPlayerLevelCount = Players.Count;
RemoveUnusedPlayerLevels();
using (System.IO.MemoryStream answerStream = new System.IO.MemoryStream())
{
using (System.IO.BinaryWriter w = new System.IO.BinaryWriter(answerStream))
{
w.Write(18);
w.Close();
}
Network.NetworkManager.SendLine(answerStream.ToArray(), Network.NetworkManager.Target.Clients);
answerStream.Close();
}
}
MFindRequestCooldown--;
if (MFindRequestCooldown <= 0)
{
using (System.IO.MemoryStream answerStream = new System.IO.MemoryStream())
{
using (System.IO.BinaryWriter w = new System.IO.BinaryWriter(answerStream))
{
w.Write(23);
w.Close();
}
Network.NetworkManager.SendLine(answerStream.ToArray(), Network.NetworkManager.Target.Everyone);
answerStream.Close();
}
MFindRequestCooldown = 300;
}
if (PlayerHands.ContainsValue(null))
{
PlayerHands.Clear();
}
}
else
{
//PlayerLevels.Clear();
}
}
public static void Reset()
{
instance.MFindRequestCooldown = 0;
instance.LevelRequestCooldown= 0;
}
public static void Host_RequestLevels()
{
if (GameSetup.IsMpServer)
{
using (System.IO.MemoryStream answerStream = new System.IO.MemoryStream())
{
using (System.IO.BinaryWriter w = new System.IO.BinaryWriter(answerStream))
{
w.Write(18);
w.Close();
}
Network.NetworkManager.SendLine(answerStream.ToArray(), Network.NetworkManager.Target.Clients);
answerStream.Close();
}
}
}
public static float DamageReduction(int armor)
{
armor = Mathf.Max(armor, 0);
float a = armor;
float b = (armor + 500f);
return Mathf.Pow(Mathf.Clamp01(a/b),2f);
}
//invalid il code error happens here. i have no clue why, so im randomly changing it so maybe it fixes itself
//im trying splitting to more classes
public static void FindHands()
{
PlayerHands.Clear();
for (int i = 0; i < Scene.SceneTracker.allPlayers.Count; i++)
{
if (Scene.SceneTracker.allPlayers[i].transform.root != LocalPlayer.Transform)
{
//BoltEntity entity = Scene.SceneTracker.allPlayers[i].GetComponent<BoltEntity>();
//if (entity == null) continue;
//IPlayerState state = entity.GetState<IPlayerState>();
//if (state == null) continue;
string playerName = getName(Scene.SceneTracker.allPlayers[i]);
if (playerName != "")
{
Transform hand = FindDeepChild(Scene.SceneTracker.allPlayers[i].transform.root, "rightHandHeld");
if (hand != null)
{
PlayerHands.Add(playerName, hand.parent);
}
}
}
}
}
private static string getName(GameObject gameObject)
{
BoltEntity entity = gameObject.GetComponent<BoltEntity>();
if (entity == null)
return "";
else
{
IPlayerState state = entity.GetState<IPlayerState>();
if (state == null)
return "";
else
return state.name;
}
}
private IEnumerator UpdateSetups()
{
while (true)
{
yield return null;
if (Players.Any(x => x == null))
{
Players.Clear();
AllPlayerEntities.Clear();
PlayerStates.Clear();
Players.AddRange(Scene.SceneTracker.allPlayers);
for (int i = 0; i < Scene.SceneTracker.allPlayers.Count; i++)
{
BoltEntity b = Scene.SceneTracker.allPlayers[i].GetComponent<BoltEntity>();
if (b != null)
{
AllPlayerEntities.Add(b);
PlayerStates.Add(b.GetState<IPlayerState>());
}
}
}
yield return new WaitForSeconds(10);
}
}
public static Transform FindDeepChild(Transform aParent, string aName)
{
Transform result = aParent.Find(aName);
if (result != null)
{
return result;
}
else
{
foreach (Transform child in aParent)
{
Transform result2 = FindDeepChild(child, aName);
if (result2 != null)
{
return result2;
}
}
return null;
}
}
public static string RecursiveTransformList(Transform tr, string s = "", int indents = 0)
{
s += new System.String('\t', indents) + tr.name + "\n";
foreach (Transform item in tr)
{
s = RecursiveTransformList(item, s, indents + 1);
}
return s;
}
public static void RecursiveComponentList(GameObject go)
{
RecursiveComponentList(go.transform, "");
}
private static void RecursiveComponentList(Transform tr, string start)
{
ModAPI.Log.Write(start + tr.name + '-' + tr.GetComponents<Component>().Select(x => x.GetType().Name).Join(", "));
start += '\t';
foreach (Transform item in tr)
{
RecursiveComponentList(item, start);
}
}
}
}