-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZoneSystemVariantController.cs
1655 lines (1318 loc) · 62.6 KB
/
ZoneSystemVariantController.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using HarmonyLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using static Seasons.Seasons;
using static Seasons.ZoneSystemVariantController;
namespace Seasons
{
public class ZoneSystemVariantController : MonoBehaviour
{
public class WaterState
{
public GameObject m_iceSurface;
public float m_surfaceOffset;
public float m_foamDepth;
public Color m_colorTop;
public Color m_colorBottom;
public Color m_colorBottomShallow;
public Color m_colorTopFrozen;
public Color m_colorBottomFrozen;
public Color m_colorBottomShallowFrozen;
public WaterState(WaterVolume waterVolume)
{
m_surfaceOffset = waterVolume.m_surfaceOffset;
m_foamDepth = waterVolume.m_waterSurface.sharedMaterial.GetFloat("_FoamDepth");
m_colorTop = waterVolume.m_waterSurface.sharedMaterial.GetColor("_ColorTop");
m_colorBottom = waterVolume.m_waterSurface.sharedMaterial.GetColor("_ColorBottom");
m_colorBottomShallow = waterVolume.m_waterSurface.sharedMaterial.GetColor("_ColorBottomShallow");
InitFrozenColors();
}
public WaterState(MeshRenderer waterSurface)
{
m_foamDepth = waterSurface.sharedMaterial.GetFloat("_FoamDepth");
m_colorTop = waterSurface.sharedMaterial.GetColor("_ColorTop");
m_colorBottom = waterSurface.sharedMaterial.GetColor("_ColorBottom");
m_colorBottomShallow = waterSurface.sharedMaterial.GetColor("_ColorBottomShallow");
InitFrozenColors();
}
private void InitFrozenColors()
{
m_colorTopFrozen = new Color(0.98f, 0.98f, 1f);
m_colorBottomFrozen = Color.Lerp(m_colorBottom, Color.white, 0.5f);
m_colorBottomShallowFrozen = Color.Lerp(m_colorBottomShallow, Color.white, 0.5f);
}
}
private static MeshRenderer s_waterPlane;
private static WaterState s_waterPlaneState;
public static readonly Dictionary<WaterVolume, WaterState> waterStates = new Dictionary<WaterVolume, WaterState>();
private static readonly MaterialPropertyBlock s_matBlock = new MaterialPropertyBlock();
private static readonly List<ZDO> m_tempZDOList = new List<ZDO>();
private static readonly List<Vector3> m_tempHits = new List<Vector3>();
private static float s_freezeStatus = 0f;
public static float s_colliderHeight = 0f;
public const float _winterWaterSurfaceOffset = 2f;
public const float _colliderOffset = 0.01f;
public const string _iceSurfaceName = "IceSurface";
public const string _iceFloeName = "ice1";
public static int _iceFloePrefab = _iceFloeName.GetStableHashCode();
public static GameObject s_iceSurface;
public static ZoneSystem.ZoneVegetation s_iceFloe;
private const float _FoamDepthFrozen = 10f;
private const float _WaveVel = 0f;
private const float _WaveFoam = 0f;
private const float _Glossiness = 0.95f;
private const float _Metallic = 0.1f;
private const float _DepthFade = 20f;
private const float _ShoreFade = 0f;
public float m_createDestroyTimer;
public RaycastHit[] rayHits = new RaycastHit[200];
private ParticleSystem m_snowStorm;
private Heightmap.Biome m_currentBiome;
private int m_snowStormMaxParticles;
private float m_snowStormEmissionRate;
private static ZoneSystemVariantController m_instance;
public static ZoneSystemVariantController Instance => m_instance;
public readonly List<WaterVolume> waterVolumesCheckFloes = new List<WaterVolume>();
private readonly List<WaterVolume> tempWaterVolumesList = new List<WaterVolume>();
private readonly List<ZoneSystem.ClearArea> m_tempClearAreas = new List<ZoneSystem.ClearArea>();
private readonly List<GameObject> m_tempSpawnedObjects = new List<GameObject>();
private static readonly List<Color32> s_tempColors = new List<Color32>();
private static readonly List<Color32> s_smoothColors = new List<Color32>();
private static readonly List<Heightmap> s_protectedHeightmaps = new List<Heightmap>();
private static readonly List<Heightmap> s_tempHeightmaps = new List<Heightmap>();
public static bool IsWaterSurfaceFrozen() => s_freezeStatus == 1f;
public static bool IsTimeForIceFloes() => enableIceFloes.Value && !IsWaterSurfaceFrozen() && seasonState.GetCurrentSeason() == Season.Winter && (int)iceFloesInWinterDays.Value.x <= seasonState.GetCurrentDay() && seasonState.GetCurrentDay() <= (int)iceFloesInWinterDays.Value.y;
public static float WaterLevel => s_colliderHeight == 0f || !IsWaterSurfaceFrozen() ? ZoneSystem.instance.m_waterLevel : s_colliderHeight;
private void Awake()
{
m_instance = this;
}
public void Update()
{
float deltaTime = Time.deltaTime;
m_createDestroyTimer += deltaTime;
if (m_createDestroyTimer >= (1f / 15f) && waterVolumesCheckFloes.Count > 0)
{
m_createDestroyTimer = 0f;
CreateDestroyFloes();
}
}
private void CreateDestroyFloes()
{
tempWaterVolumesList.Clear();
foreach (WaterVolume waterVolume in waterVolumesCheckFloes)
{
if (!CheckWaterVolumeForIceFloes(waterVolume))
tempWaterVolumesList.Add(waterVolume);
}
waterVolumesCheckFloes.Clear();
waterVolumesCheckFloes.AddRange(tempWaterVolumesList);
}
private void OnDestroy()
{
s_waterPlane = null;
s_waterPlaneState = null;
s_iceSurface = null;
waterStates.Clear();
m_instance = null;
}
public void Initialize(ZoneSystem instance)
{
Transform waterPlane = EnvMan.instance.transform.Find("WaterPlane");
if (waterPlane != null)
s_waterPlane = waterPlane.GetComponentInChildren<MeshRenderer>();
s_waterPlaneState = new WaterState(s_waterPlane);
Transform water = instance.m_zonePrefab.transform.Find("Water");
if (water != null)
AddIceCollider(water);
s_iceFloe ??= ZoneSystem.instance.m_vegetation.Find(veg => veg.m_prefab?.name == _iceFloeName).Clone();
s_iceFloe.m_biome = Heightmap.Biome.Ocean;
if (!s_iceFloe.m_prefab.TryGetComponent<IceFloeClimb>(out _))
s_iceFloe.m_prefab.AddComponent<IceFloeClimb>();
s_iceFloe.m_prefab.GetComponent<ZNetView>().m_syncInitialScale = true;
}
public bool BiomeChanged(Heightmap.Biome biome)
{
if (m_currentBiome == biome)
return false;
m_currentBiome = biome;
return true;
}
public void CheckBiomeChanged(Heightmap.Biome biome)
{
if (!UseTextureControllers())
return;
if (!SeasonState.IsActive)
return;
if (reduceSnowStormInWinter.Value == Vector2.zero || Player.m_localPlayer == null)
{
if (m_snowStorm != null && m_snowStormMaxParticles != 0 && m_snowStormEmissionRate != 0f)
{
ParticleSystem.MainModule main = m_snowStorm.main;
ParticleSystem.EmissionModule emission = m_snowStorm.emission;
emission.rateOverTimeMultiplier = m_snowStormEmissionRate;
main.maxParticles = m_snowStormMaxParticles;
}
return;
}
if (!BiomeChanged(biome))
return;
if (m_snowStorm == null)
{
Transform snowStormTransform = EnvMan.instance.transform.Find("FollowPlayer/SnowStorm") ?? Utils.FindChild(EnvMan.instance.transform, "SnowStorm");
if (snowStormTransform == null)
return;
Transform snowParticles = snowStormTransform.Find("snow (1)");
if (snowParticles == null)
return;
m_snowStorm = snowParticles.GetComponent<ParticleSystem>();
}
ParticleSystem.MainModule snowStormMain = m_snowStorm.main;
ParticleSystem.EmissionModule snowStormEmission = m_snowStorm.emission;
if (m_snowStormMaxParticles == 0)
m_snowStormMaxParticles = snowStormMain.maxParticles;
if (m_snowStormEmissionRate == 0f)
m_snowStormEmissionRate = snowStormEmission.rateOverTimeMultiplier;
bool reduceParticles = seasonState.GetCurrentSeason() == Season.Winter && biome != Heightmap.Biome.Mountain && biome != Heightmap.Biome.AshLands && biome != Heightmap.Biome.DeepNorth;
snowStormEmission.rateOverTimeMultiplier = reduceParticles ? reduceSnowStormInWinter.Value.x : m_snowStormEmissionRate;
snowStormMain.maxParticles = reduceParticles ? (int)reduceSnowStormInWinter.Value.y : m_snowStormMaxParticles;
}
public static void SnowStormReduceParticlesChanged()
{
if (!Instance)
return;
Instance.m_currentBiome = Heightmap.Biome.None;
}
public static void UpdateTerrainColor(Heightmap heightmap)
{
if (heightmap?.m_renderMesh == null)
return;
Heightmap_GetBiomeColor_TerrainColor.overrideColor = true;
int num = heightmap.m_width + 1;
Vector3 vector = heightmap.transform.position + new Vector3((float)((double)heightmap.m_width * (double)heightmap.m_scale * -0.5), 0f, (float)((double)heightmap.m_width * (double)heightmap.m_scale * -0.5));
s_tempColors.Clear();
bool hasShieldedPosition = false;
for (int i = 0; i < num; i++)
for (int j = 0; j < num; j++)
if (heightmap.m_isDistantLod)
{
float wx = vector.x + j * heightmap.m_scale;
float wy = vector.z + i * heightmap.m_scale;
Heightmap.Biome biome = WorldGenerator.instance.GetBiome(wx, wy);
s_tempColors.Add(Heightmap.GetBiomeColor(biome));
}
else
{
float ix = DUtils.SmoothStep(0f, 1f, (float)j / heightmap.m_width);
float iy = DUtils.SmoothStep(0f, 1f, (float)i / heightmap.m_width);
if (IsProtectedHeightmap(heightmap) && IsShieldedPosition(heightmap.transform.position + heightmap.CalcVertex(j, i)))
{
hasShieldedPosition = true;
Heightmap_GetBiomeColor_TerrainColor.overrideColor = false;
s_tempColors.Add(heightmap.GetBiomeColor(ix, iy));
Heightmap_GetBiomeColor_TerrainColor.overrideColor = true;
}
else
{
s_tempColors.Add(heightmap.GetBiomeColor(ix, iy));
}
}
Heightmap_GetBiomeColor_TerrainColor.overrideColor = false;
if (hasShieldedPosition)
SmoothenProtectedBorders(s_tempColors, heightmap.m_width + 1);
heightmap.m_renderMesh.SetColors(s_tempColors);
s_tempColors.Clear();
}
public static void SmoothenProtectedBorders(List<Color32> colors, int size)
{
s_smoothColors.Clear();
s_smoothColors.AddRange(colors);
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
int r = 0, g = 0, b = 0, a = 0;
int count = 0;
for (int dx = -1; dx <= 1; dx++)
for (int dy = -1; dy <= 1; dy++)
{
int nx = x + dx;
int ny = y + dy;
if (nx >= 0 && nx < size && ny >= 0 && ny < size)
{
Color32 neighbor = colors[ny * size + nx];
r += neighbor.r;
g += neighbor.g;
b += neighbor.b;
a += neighbor.a;
count++;
}
}
s_smoothColors[y * size + x] = new Color32((byte)(r / count), (byte)(g / count), (byte)(b / count), (byte)(a / count));
}
}
colors.Clear();
colors.AddRange(s_smoothColors);
s_smoothColors.Clear();
}
public static void UpdateTerrainColors()
{
UpdateTerrainColorsFromList(Heightmap.Instances.Cast<Heightmap>());
}
public static void UpdateTerrainColorsFromList(IEnumerable<Heightmap> list)
{
UpdateProtectedHeightmaps();
foreach (Heightmap instance in list)
UpdateTerrainColor(instance);
}
private static void UpdateProtectedHeightmaps()
{
s_protectedHeightmaps.Clear();
if (IsShieldProtectionActive())
foreach (ShieldGenerator instance in ShieldGenerator.m_instances)
Heightmap.FindHeightmap(instance.m_shieldDome?.transform.position ?? instance.transform.position, instance.m_radius, s_protectedHeightmaps);
}
public static bool IsProtectedHeightmap(Heightmap heightmap)
{
return heightmap != null && s_protectedHeightmaps.Contains(heightmap);
}
public static void UpdateTerrainColorsAroundPosition(Vector3 position, float radius, float delay = 0f)
{
if (instance == null)
return;
if (delay == 0f)
UpdateTerrainAroundPosition(position, radius);
else
instance.StartCoroutine(UpdateTerrainColorsAroundPositionDelayed(position, radius, delay));
}
public static IEnumerator UpdateTerrainColorsAroundPositionDelayed(Vector3 position, float radius, float delay = 0f)
{
yield return new WaitForSeconds(delay);
UpdateTerrainAroundPosition(position, radius);
}
private static void UpdateTerrainAroundPosition(Vector3 position, float radius)
{
s_tempHeightmaps.Clear();
Heightmap.FindHeightmap(position, radius, s_tempHeightmaps);
UpdateTerrainColorsFromList(s_tempHeightmaps);
ClutterSystem.instance.ResetGrass(position, radius);
}
public static void AddIceCollider(Transform water)
{
if (s_iceSurface != null)
return;
Transform iceSurfaceTransform = water.Find(_iceSurfaceName);
if (iceSurfaceTransform != null)
{
s_iceSurface = iceSurfaceTransform.gameObject;
return;
}
Transform waterSurface = water.Find("WaterSurface");
if (s_colliderHeight == 0f)
s_colliderHeight = waterSurface.transform.position.y + _colliderOffset;
s_iceSurface = new GameObject(_iceSurfaceName);
s_iceSurface.transform.SetParent(water);
s_iceSurface.layer = 0;
s_iceSurface.transform.localScale = new Vector3(waterSurface.transform.localScale.x, Math.Abs(_colliderOffset), waterSurface.transform.localScale.z);
s_iceSurface.transform.localPosition = new Vector3(0, _colliderOffset, 0);
s_iceSurface.SetActive(false);
MeshCollider iceCollider = s_iceSurface.gameObject.AddComponent<MeshCollider>();
iceCollider.sharedMesh = waterSurface.GetComponent<MeshFilter>().sharedMesh;
iceCollider.material.staticFriction = 0.1f;
iceCollider.material.dynamicFriction = 0.1f;
iceCollider.material.frictionCombine = PhysicMaterialCombine.Minimum;
iceCollider.cookingOptions = MeshColliderCookingOptions.UseFastMidphase;
}
public static void UpdateWaterState()
{
if (!SeasonState.IsActive)
return;
s_freezeStatus = seasonState.GetWaterSurfaceFreezeStatus();
CheckToRemoveIceFloes();
foreach (KeyValuePair<WaterVolume, WaterState> waterState in waterStates)
UpdateWater(waterState.Key, waterState.Value);
UpdateWaterSurface(s_waterPlane, s_waterPlaneState);
Seasons.instance.StartCoroutine(UpdateWaterObjects());
}
public static void UpdateWater(WaterVolume waterVolume, WaterState waterState, bool revertState = false)
{
SetupIceCollider(waterVolume, waterState, revertState);
if (s_freezeStatus == 0f || revertState)
{
if (waterVolume.m_waterSurface != null && waterVolume.m_waterSurface.HasPropertyBlock())
waterVolume.m_waterSurface.SetPropertyBlock(null);
waterVolume.m_surfaceOffset = waterState.m_surfaceOffset;
waterVolume.m_useGlobalWind = true;
waterVolume.SetupMaterial();
return;
}
UpdateWaterSurface(waterVolume.m_waterSurface, waterState);
waterVolume.m_surfaceOffset = waterState.m_surfaceOffset - (IsWaterSurfaceFrozen() ? _winterWaterSurfaceOffset : 0);
waterVolume.m_useGlobalWind = !IsWaterSurfaceFrozen();
waterVolume.SetupMaterial();
}
private static void UpdateWaterSurface(MeshRenderer waterSurface, WaterState waterState)
{
if (waterSurface == null || waterState == null)
return;
s_matBlock.Clear();
s_matBlock.SetColor("_FoamColor", new Color(0.95f, 0.96f, 0.98f));
s_matBlock.SetFloat("_FoamDepth", Mathf.Lerp(waterState.m_foamDepth, _FoamDepthFrozen, s_freezeStatus));
s_matBlock.SetColor("_ColorTop", Color.Lerp(waterState.m_colorTop, waterState.m_colorTopFrozen, s_freezeStatus));
s_matBlock.SetColor("_ColorBottom", Color.Lerp(waterState.m_colorBottom, waterState.m_colorBottomFrozen, s_freezeStatus));
s_matBlock.SetColor("_ColorBottomShallow", Color.Lerp(waterState.m_colorBottomShallow, waterState.m_colorBottomShallowFrozen, s_freezeStatus));
if (IsWaterSurfaceFrozen())
{
s_matBlock.SetFloat(WaterVolume.s_shaderWaterTime, 0f);
s_matBlock.SetFloat(WaterVolume.s_shaderUseGlobalWind, 0f);
s_matBlock.SetFloat("_DepthFade", _DepthFade);
s_matBlock.SetFloat("_Glossiness", _Glossiness);
s_matBlock.SetFloat("_Metallic", _Metallic);
s_matBlock.SetFloat("_ShoreFade", _ShoreFade);
s_matBlock.SetFloat("_WaveVel", _WaveVel);
s_matBlock.SetFloat("_WaveFoam", _WaveFoam);
}
waterSurface.SetPropertyBlock(s_matBlock);
}
private static void SetupIceCollider(WaterVolume waterVolume, WaterState waterState, bool revertState)
{
if (waterState.m_iceSurface == null)
waterState.m_iceSurface = waterVolume.transform.parent.Find(_iceSurfaceName)?.gameObject;
if (revertState)
waterState.m_iceSurface?.SetActive(false);
else
waterState.m_iceSurface?.SetActive(IsWaterSurfaceFrozen());
}
public static bool LocalPlayerIsOnFrozenOcean() => IsWaterSurfaceFrozen()
&& Player.m_localPlayer != null
&& Player.m_localPlayer.GetCurrentBiome() == Heightmap.Biome.Ocean;
public static IEnumerator UpdateWaterObjects()
{
yield return waitForFixedUpdate;
foreach (WaterVolume waterVolume in waterStates.Keys)
{
foreach (IWaterInteractable waterInteractable in waterVolume.m_inWater)
if (waterInteractable is Fish)
CheckIfFishAboveSurface(waterInteractable as Fish);
else if (waterInteractable is Character)
CheckIfCharacterAboveSurface(waterInteractable as Character);
Instance.waterVolumesCheckFloes.Add(waterVolume);
}
yield return waitForFixedUpdate;
foreach (Ship ship in Ship.Instances.ToArray().Cast<Ship>())
yield return CheckIfShipBelowSurface(ship);
}
public static bool IsUnderwaterAI(Character character, out BaseAI ai)
{
return character.TryGetComponent(out ai) && (ai.m_pathAgentType == Pathfinding.AgentType.Fish || ai.m_pathAgentType == Pathfinding.AgentType.BigFish);
}
public static void UpdateShipsPositions()
{
foreach (Ship ship in Ship.Instances.ToArray().Cast<Ship>())
if (ship.m_nview.IsOwner())
PlaceShip(ship);
}
public static IEnumerator CheckIfShipBelowSurface(Ship ship)
{
if (ship == null || ship.gameObject == null)
yield break;
while (ship.m_nview.IsValid() && !ship.m_nview.HasOwner())
yield return waitForFixedUpdate;
if (!ship.m_nview.IsValid() || !ship.m_nview.IsOwner())
yield break;
PlaceShip(ship);
}
public static void PlaceShip(Ship ship)
{
ship.m_body.WakeUp();
ship.m_body.isKinematic = false;
List<MeshRenderer> watermask = ship.GetComponentsInChildren<MeshRenderer>(includeInactive: true)
.Where(renderer => renderer.sharedMaterial != null && renderer.sharedMaterial.shader != null && renderer.sharedMaterial.shader.name == "Custom/WaterMask").ToList();
watermask.Do(renderer => renderer.gameObject.SetActive(true));
float positionDelta = ship.m_body.position.y - (WaterLevel + ship.m_waterLevelOffset);
if (positionDelta > 0 || !IsWaterSurfaceFrozen())
return;
ship.m_body.isKinematic = !placeShipAboveFrozenOcean.Value;
if (ship.TryGetComponent(out ZSyncTransform zSyncTransform))
zSyncTransform.m_isKinematicBody = ship.m_body.isKinematic;
if (placeShipAboveFrozenOcean.Value)
{
ship.m_body.rotation = Quaternion.identity;
ship.m_body.position = new Vector3(ship.m_body.position.x, WaterLevel + ship.m_waterLevelOffset + 0.1f, ship.m_body.position.z);
ship.m_body.velocity = Vector3.zero;
}
else if (frozenKarvePositionFix.Value && Utils.GetPrefabName(ship.name) == "Karve" && positionDelta <= -1.43f)
{
ship.m_body.rotation = Quaternion.identity;
ship.m_body.position = new Vector3(ship.m_body.position.x, WaterLevel + ship.m_waterLevelOffset - 1.42f, ship.m_body.position.z);
ship.m_body.velocity = Vector3.zero;
}
else if (positionDelta < -ship.m_waterLevelOffset * 1.5f && ship.m_body.isKinematic)
{
watermask.Do(renderer => renderer.gameObject.SetActive(false));
}
}
public static void CheckIfFishAboveSurface(Fish fish)
{
if (fish == null || fish.m_nview == null || !fish.m_nview.IsValid())
return;
if (fish.m_nview.HasOwner() && !fish.m_nview.IsOwner())
return;
float maximumLevel = WaterLevel - _winterWaterSurfaceOffset - fish.m_height - 0.2f;
if (fish.transform.position.y > maximumLevel)
fish.transform.position = new Vector3(fish.transform.position.x, maximumLevel, fish.transform.position.z);
fish.m_body.velocity = Vector3.zero;
fish.m_haveWaypoint = false;
fish.m_isJumping = false;
}
public static void CheckIfCharacterAboveSurface(Character character)
{
if (character == null || character.m_nview == null || !character.m_nview.IsValid())
return;
if (!character.m_nview.IsOwner())
return;
if (IsUnderwaterAI(character, out BaseAI ai))
{
if (character.transform.position.y >= WaterLevel)
{
m_tempHits.Clear();
Pathfinding.instance.FindGround(character.transform.position, testWater: true, m_tempHits, Pathfinding.instance.GetSettings(ai.m_pathAgentType));
Vector3 hit = m_tempHits.Find(h => h.y < WaterLevel);
if (hit.y != 0)
{
character.m_body.velocity = Vector3.zero;
character.transform.position = new Vector3(character.transform.position.x, Mathf.Max(WaterLevel - _winterWaterSurfaceOffset, hit.y + 0.1f), character.transform.position.z);
}
}
}
else if (character.transform.position.y <= WaterLevel && !character.IsAttachedToShip())
{
character.m_body.velocity = Vector3.zero;
character.transform.position = new Vector3(character.transform.position.x, WaterLevel + 0.5f, character.transform.position.z);
character.InvalidateCachedLiquidDepth();
character.m_maxAirAltitude = character.transform.position.y;
character.m_swimTimer = 0.6f;
}
}
public static void CheckToRemoveIceFloes()
{
if (IsTimeForIceFloes() || !ZNet.instance.IsServer())
return;
int floes = 0;
foreach (ZDO zdo in ZDOMan.instance.m_objectsByID.Values)
{
if (zdo.GetPrefab() != _iceFloePrefab)
continue;
Vector3 position = zdo.GetPosition();
float num = WorldGenerator.WorldAngle(position.x, position.z) * 100.0f;
if (new Vector2(position.x, position.z + 4000.0f).magnitude > 12000.0f + num)
continue;
RemoveObject(zdo, true);
floes++;
}
LogFloeState($"Removed overworld floes:{floes}");
}
public static bool CheckWaterVolumeForIceFloes(WaterVolume waterVolume)
{
if (waterVolume == null || waterVolume.m_heightmap == null)
return true;
Vector3 position = waterVolume.transform.position;
if (WorldGenerator.instance.GetBiome(position) != Heightmap.Biome.Ocean)
return true;
Vector2i zoneID = ZoneSystem.GetZone(position);
if (!ZoneSystem.instance.IsZoneLoaded(zoneID))
return false;
m_tempZDOList.Clear();
ZDOMan.instance.FindObjects(zoneID, m_tempZDOList);
m_tempZDOList.RemoveAll(zdo => zdo.GetPrefab() != _iceFloePrefab);
if (IsTimeForIceFloes() && m_tempZDOList.Count > 0)
return true;
else if (!IsTimeForIceFloes() && m_tempZDOList.Count == 0)
return true;
else if (!IsTimeForIceFloes() && m_tempZDOList.Count > 0)
{
LogFloeState($"Removing occasional floes: {m_tempZDOList.Count}");
foreach (ZDO zdo in m_tempZDOList)
RemoveObject(zdo, force: true);
}
else if (IsTimeForIceFloes() && m_tempZDOList.Count == 0)
{
Instance.m_tempClearAreas.Clear();
Instance.m_tempSpawnedObjects.Clear();
Vector3 zonePos = ZoneSystem.GetZonePos(zoneID);
ZoneSystem.SpawnMode mode = ZNetScene.instance.IsAreaReady(position) ? ZoneSystem.SpawnMode.Full : ZoneSystem.SpawnMode.Ghost;
PlaceIceFloes(zoneID, zonePos, Instance.m_tempClearAreas, mode, Instance.m_tempSpawnedObjects);
LogFloeState($"{zoneID} {zonePos} Spawned {mode} floes:{Instance.m_tempSpawnedObjects.Count}");
if (mode == ZoneSystem.SpawnMode.Ghost)
{
foreach (GameObject tempSpawnedObject in Instance.m_tempSpawnedObjects)
Destroy(tempSpawnedObject);
Instance.m_tempSpawnedObjects.Clear();
}
}
return true;
}
public static void PlaceIceFloes(Vector2i zoneID, Vector3 zoneCenterPos, List<ZoneSystem.ClearArea> clearAreas, ZoneSystem.SpawnMode mode, List<GameObject> spawnedObjects)
{
UnityEngine.Random.State state = UnityEngine.Random.state;
int seed = WorldGenerator.instance.GetSeed();
float num = ZoneSystem.instance.m_zoneSize / 2f;
UnityEngine.Random.InitState(seed + zoneID.x * 4271 + zoneID.y * 9187 + _iceFloePrefab);
int spawnCount = UnityEngine.Random.Range((int)amountOfIceFloesInWinterDays.Value.x, (int)amountOfIceFloesInWinterDays.Value.y + 1);
for (int i = 0; i < spawnCount; i++)
{
Vector3 p = new Vector3(UnityEngine.Random.Range(zoneCenterPos.x - num, zoneCenterPos.x + num), 0f, UnityEngine.Random.Range(zoneCenterPos.z - num, zoneCenterPos.z + num));
if (ZoneSystem.instance.InsideClearArea(clearAreas, p))
continue;
if (s_iceFloe.m_blockCheck && ZoneSystem.instance.IsBlocked(p))
continue;
float num11 = p.y - ZoneSystem.instance.m_waterLevel;
if (num11 < s_iceFloe.m_minAltitude || num11 > s_iceFloe.m_maxAltitude)
continue;
ZoneSystem.instance.GetGroundData(ref p, out _, out var biome, out var biomeArea, out var hmap2);
if ((s_iceFloe.m_biome & biome) == 0 || (s_iceFloe.m_biomeArea & biomeArea) == 0)
continue;
if (s_iceFloe.m_minOceanDepth != s_iceFloe.m_maxOceanDepth)
{
float oceanDepth = hmap2.GetOceanDepth(p);
if (oceanDepth < s_iceFloe.m_minOceanDepth || oceanDepth > s_iceFloe.m_maxOceanDepth)
continue;
}
if (s_iceFloe.m_snapToWater)
p.y = ZoneSystem.instance.m_waterLevel - _winterWaterSurfaceOffset;
if (mode == ZoneSystem.SpawnMode.Ghost)
ZNetView.StartGhostInit();
GameObject gameObject = Instantiate(s_iceFloe.m_prefab, p, Quaternion.Euler(0, UnityEngine.Random.Range(0, 360), 0));
if (mode == ZoneSystem.SpawnMode.Ghost)
ZNetView.FinishGhostInit();
ZNetView component = gameObject.GetComponent<ZNetView>();
float scaleX = UnityEngine.Random.Range(iceFloesScale.Value.x, iceFloesScale.Value.y);
float scaleY = UnityEngine.Random.Range(iceFloesScale.Value.x, iceFloesScale.Value.y);
float scaleZ = UnityEngine.Random.Range(iceFloesScale.Value.x, iceFloesScale.Value.y);
float radius = 5f;
component.SetLocalScale(new Vector3(scaleX, scaleY, scaleZ));
Collider[] componentsInChildren = gameObject.GetComponentsInChildren<Collider>();
foreach (Collider obj in componentsInChildren)
{
obj.enabled = false;
obj.enabled = true;
radius = Math.Max(radius, obj.bounds.size.magnitude / 2);
}
if (mode == ZoneSystem.SpawnMode.Ghost)
spawnedObjects.Add(gameObject);
clearAreas.Add(new ZoneSystem.ClearArea(p, radius));
}
UnityEngine.Random.state = state;
}
private static void RemoveObject(ZDO zdo, bool force = false)
{
if (zdo == null || !zdo.IsValid())
return;
if (!zdo.IsOwner())
{
if (!force && !ZNet.instance.IsServer())
return;
zdo.SetOwner(ZDOMan.GetSessionID());
}
if (ZNetScene.instance.m_instances.TryGetValue(zdo, out ZNetView netView))
ZNetScene.instance.Destroy(netView.gameObject);
else
ZDOMan.instance.DestroyZDO(zdo);
}
private static void LogFloeState(string log)
{
if (!logFloes.Value)
return;
LogInfo(log);
}
}
public static class CharacterExtentions_FrozenOceanSliding
{
private struct SlideStatus
{
public Vector3 m_iceSlipVelocity;
public float m_slip;
}
private static readonly Dictionary<Character, SlideStatus> charactersSlides = new Dictionary<Character, SlideStatus>();
public static bool IsOnIce(this Character character)
{
if (!IsWaterSurfaceFrozen())
return false;
if (!character.IsOnGround())
return false;
Collider lastGroundCollider = character.GetLastGroundCollider();
if (lastGroundCollider == null)
return false;
return lastGroundCollider.name == _iceSurfaceName;
}
public static void StartIceSliding(this Character character, Vector3 currentVel, bool checkMagnitude = false, bool checkRunning = true)
{
if (frozenOceanSlipperiness.Value <= 0)
return;
if (checkRunning && character.IsRunning())
return;
SlideStatus slideStatus = charactersSlides.GetValueSafe(character);
slideStatus.m_slip = 1f;
if (checkMagnitude && slideStatus.m_iceSlipVelocity.magnitude > currentVel.magnitude)
return;
slideStatus.m_iceSlipVelocity = Vector3.ClampMagnitude(currentVel, 10f);
charactersSlides[character] = slideStatus;
}
public static void UpdateIceSliding(this Character character, ref Vector3 currentVel)
{
SlideStatus slideStatus = charactersSlides[character];
if (slideStatus.m_slip > 0f && (character.IsOnIce() || !character.IsOnGround()))
{
currentVel = Vector3.Lerp(currentVel, slideStatus.m_iceSlipVelocity, slideStatus.m_slip);
float delta = character.IsOnGround() ? Time.fixedDeltaTime / 2 / Mathf.Abs(frozenOceanSlipperiness.Value) : Time.fixedDeltaTime;
slideStatus.m_slip = Mathf.MoveTowards(slideStatus.m_slip, 0f, delta);
charactersSlides[character] = slideStatus;
}
else
{
character.StopIceSliding();
}
}
public static void StopIceSliding(this Character character)
{
charactersSlides.Remove(character);
}
[HarmonyPatch(typeof(Character), nameof(Character.OnDestroy))]
public static class Character_OnDestroy_WaterVariantControllerInit
{
private static void Prefix(Character __instance)
{
__instance.StopIceSliding();
}
}
[HarmonyPatch(typeof(Player), nameof(Player.SetControls))]
public static class Player_SetControls_FrozenOceanSlippery
{
private static void Prefix(Player __instance, bool run)
{
if (frozenOceanSlipperiness.Value == 0 || __instance != Player.m_localPlayer || !__instance.IsOnIce())
return;
if (!run && __instance.m_run)
__instance.StartIceSliding(__instance.m_currentVel, checkRunning: false);
}
}
[HarmonyPatch(typeof(Character), nameof(Character.SetRun))]
public static class Character_SetRun_FrozenOceanSlippery
{
private static void Prefix(Character __instance, bool run, ZNetView ___m_nview)
{
if (frozenOceanSlipperiness.Value == 0 || !__instance.IsOnIce() || !___m_nview.IsValid() || !___m_nview.IsOwner())
return;
if (!run && __instance.m_run)
__instance.StartIceSliding(__instance.m_currentVel, checkRunning: false);
}
}
[HarmonyPatch(typeof(Character), nameof(Character.UpdateGroundContact))]
public static class Character_UpdateGroundContact_FrozenOceanSlippery
{
private static readonly Dictionary<Character, Vector3> m_characterSlideVelocity = new Dictionary<Character, Vector3>();
public static void CheckForSlide(Character characterSyncVelocity)
{
if (m_characterSlideVelocity.TryGetValue(characterSyncVelocity, out Vector3 bodyVelocity))
{
characterSyncVelocity.StartIceSliding(bodyVelocity, checkMagnitude: true);
m_characterSlideVelocity.Remove(characterSyncVelocity);
}
}
private static void Prefix(Character __instance, ZNetView ___m_nview, float ___m_maxAirAltitude, ref Tuple<float, Vector3> __state)
{
if (frozenOceanSlipperiness.Value == 0 || !___m_nview.IsValid() || !___m_nview.IsOwner())
return;
__state = new Tuple<float, Vector3>(Mathf.Max(0f, ___m_maxAirAltitude - __instance.transform.position.y), __instance.m_body.velocity);
}
private static void Postfix(Character __instance, float ___m_maxAirAltitude, Tuple<float, Vector3> __state)
{
if (__state == null)
return;
if (__state.Item1 > 1f && frozenOceanSlipperiness.Value > 0 && (___m_maxAirAltitude == __instance.transform.position.y) && __instance.IsOnIce())
m_characterSlideVelocity[__instance] = __state.Item2;
}
[HarmonyPatch(typeof(Character), nameof(Character.SyncVelocity))]
public static class Character_SyncVelocity_FrozenOceanSlippery
{
private static void Prefix(Character __instance)
{
CheckForSlide(__instance);
}
}
}
[HarmonyPatch(typeof(Player), nameof(Player.UpdateDodge))]
public static class Player_UpdateDodge_FrozenOceanSlippery
{
private static bool m_initiateSlide;
private static Vector3 m_bodyVelocity = Vector3.zero;
[HarmonyPriority(Priority.First)]
private static void Prefix(Player __instance, bool ___m_inDodge, ref bool __state)
{
if (m_initiateSlide && !___m_inDodge && __instance.IsOnIce())
__instance.StartIceSliding(m_bodyVelocity);
__state = frozenOceanSlipperiness.Value > 0 && ___m_inDodge && __instance == Player.m_localPlayer;
}
private static void Postfix(Player __instance, bool ___m_inDodge, ref bool __state)
{
m_initiateSlide = frozenOceanSlipperiness.Value > 0 && ___m_inDodge && __instance == Player.m_localPlayer;
m_bodyVelocity = __instance.m_queuedDodgeDir * __instance.m_body.velocity.magnitude;
if (__state && !___m_inDodge && __instance.IsOnIce())
__instance.StartIceSliding(m_bodyVelocity);
}
}
[HarmonyPatch(typeof(Character), nameof(Character.ApplyGroundForce))]
public static class Character_ApplyGroundForce_FrozenOceanSlippery
{
private static void Postfix(Character __instance, ZNetView ___m_nview, ref Vector3 vel)
{
if (!charactersSlides.ContainsKey(__instance))
return;
if (frozenOceanSlipperiness.Value == 0 || !___m_nview.IsValid() || !___m_nview.IsOwner())
{
__instance.StopIceSliding();
return;
}
__instance.UpdateIceSliding(ref vel);
}
}
[HarmonyPatch(typeof(Character), nameof(Character.UpdateBodyFriction))]
public static class Character_UpdateBodyFriction_FrozenOceanSurface
{
private static bool Prefix(Character __instance, CapsuleCollider ___m_collider)
{
if (!__instance.IsOnIce())
return true;
___m_collider.material.staticFriction = 0.1f;
___m_collider.material.dynamicFriction = 0.1f;
___m_collider.material.frictionCombine = PhysicMaterialCombine.Minimum;