forked from ericpzh/MiniRealisticAirways
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindSock.cs
203 lines (176 loc) · 6.61 KB
/
WindSock.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
using HarmonyLib;
using System;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.Rendering;
namespace MiniRealisticAirways
{
class WindSock : MonoBehaviour
{
public override string ToString()
{
return "Wind: " + (int)Math.Round(windDirection_) + "°";
}
public void InitializeText()
{
if (windsock_ == null)
{
return;
}
// Init wind text.
textGameObject_ = Instantiate(new GameObject("Text"));
text_ = textGameObject_.AddComponent<TextMeshPro>();
text_.fontSize = 4f;
text_.horizontalAlignment = HorizontalAlignmentOptions.Left;
text_.verticalAlignment = VerticalAlignmentOptions.Middle;
text_.rectTransform.sizeDelta = new Vector2(2, 1);
// make sorting layer of obj "Text"
SortingGroup sg = textGameObject_.AddComponent<SortingGroup>();
sg.sortingLayerName = "Text";
sg.sortingOrder = 1;
}
public bool CanLand(float heading, Weight weight)
{
// Convert wind heading into aircraft heading.
float convertedWindDirection_ = windDirection_ - 180;
if (convertedWindDirection_ < 0)
{
convertedWindDirection_ += 360;
}
float angle = Math.Min((heading - convertedWindDirection_) < 0 ? heading - convertedWindDirection_ + 360 : heading - convertedWindDirection_,
(convertedWindDirection_ - heading) < 0 ? convertedWindDirection_ - heading + 360 : convertedWindDirection_ - heading);
if (angle <= 90)
{
return true;
}
if (GoAroundProbability(angle, weight) < UnityEngine.Random.value)
{
return true;
}
Plugin.Log.LogInfo(
"Go-around induced by wind. Current wind: " + windDirection_ + ". Converted wind: " + convertedWindDirection_ +
" Current Heading: " + heading + " Angle: " + angle + " Probabaility " + GoAroundProbability(angle, weight));
return false;
}
private void CorrectWindDirection()
{
if (windDirection_ < 0)
{
windDirection_ += 360;
}
else if (windDirection_ >= 360)
{
windDirection_ -= 360;
}
}
private float GoAroundProbability(float x, Weight weight)
{
float f = (float)(1 / (1 + Math.Pow(1.05, 135 - x)));
switch (weight)
{
case Weight.Light:
f += 0.1f;
break;
case Weight.Medium:
f += 0.05f;
break;
case Weight.Heavy:
break;
}
return (float)Math.Clamp(f, 0, 1);
}
private float RandomUniform(float limit)
{
return UnityEngine.Random.value * 2f * limit - limit;
}
private float RandomDirection()
{
float randomOffset = RandomUniform(WIND_RANDOM_OFFSET_LIMIT);
float windShiftDirection = UnityEngine.Random.value > 0.5 ? 1 : -1;
return WIND_RANDOM_BASE * windShiftDirection + randomOffset;
}
private IEnumerator UpdateWindCoroutine()
{
float updateTime = WIND_BASE_TIME + RandomUniform(WIND_RANDOM_TIME_OFFSET_LIMIT);
float timeGradient = updateTime / UPDATE_COUNT;
float windGradient = RandomDirection() / UPDATE_COUNT;
Plugin.Log.LogInfo("Wind updated, moving from " + windDirection_ + " to " + windGradient + " in time " + updateTime);
for (int i = 0; i < UPDATE_COUNT; i++)
{
windDirection_ += windGradient;
CorrectWindDirection();
if (!windsock_.activeSelf)
{
// Force windsock to become active.
Plugin.Log.LogWarning("windsock_ isn't active, forcing it to be. Current wind: " + windDirection_);
windsock_.SetActive(value: true);
}
windsock_.transform.rotation = Quaternion.AngleAxis(windDirection_, Vector3.back);
yield return new WaitForSeconds(timeGradient);
}
yield return UpdateWindCoroutine();
}
private void Start()
{
windDirection_ = UnityEngine.Random.value * 360f;
StartCoroutine(UpdateWindCoroutine());
}
private void Update()
{
if (Time.timeScale == 0f)
{
// Skip update during time pause.
return;
}
if (text_ == null || textGameObject_ == null)
{
return;
}
if (!Plugin.showText_)
{
text_.text = "";
return;
}
// TLPBR from GUIAutoHider.
Vector3 escButtonBottomRight = Camera.main.ViewportToWorldPoint(new Vector3(0.07f, 0.88f, 0f));
textGameObject_.transform.position = new Vector3(escButtonBottomRight.x + 1f, escButtonBottomRight.y + 0.5f, 0f);
text_.text = ToString();
}
public GameObject windsock_;
public float windDirection_ = 0;
private GameObject textGameObject_;
private TMP_Text text_;
private const float WIND_RANDOM_BASE = 180f;
private const float WIND_RANDOM_OFFSET_LIMIT = 30f;
private const float WIND_BASE_TIME = 6f * 300f /* Time per day */;
private const float WIND_RANDOM_TIME_OFFSET_LIMIT = 0.5f * 300f /* Time per day */;
private const float UPDATE_COUNT = 360f;
}
[HarmonyPatch(typeof(GUIAutoHider), "CheckTL", new Type[] { })]
class PatchCheckTL
{
static bool Prefix(ref GUIAutoHider __instance)
{
// Disable auto-hiding of windsock.
return false;
}
}
[HarmonyPatch(typeof(GUIAutoHider), "Update", new Type[] { })]
class PatchGUIAutoHiderUpdate
{
static void Postfix(ref GUIAutoHider __instance)
{
if (Time.timeScale == 0f)
{
// Skip update during time pause.
return;
}
if (!GameOverManager.Instance.GameOverFlag && __instance.TL.alpha < 1f)
{
Plugin.Log.LogWarning("Windsock auto hidden in-game.");
__instance.TL.alpha = 1f;
}
}
}
}