forked from LoafOrc/FacilityMeltdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerminalHandler.cs
112 lines (91 loc) · 4.88 KB
/
TerminalHandler.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FacilityMeltdown.Util;
using HarmonyLib;
using JetBrains.Annotations;
using TerminalApi;
using TerminalApi.Classes;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
using static TerminalApi.Events.Events;
using static TerminalApi.TerminalApi;
namespace FacilityMeltdown {
internal class TerminalHandler {
internal class ReactorHealthReport {
public float reactorInstability, timeRemaining;
public float generatedAt = Time.time;
public string GetFlavourText() {
if (timeRemaining/MeltdownConfig.Instance.MELTDOWN_TIME.Value > .75f) {
return "reactorscan.result.flavour.start";
} else if (timeRemaining / MeltdownConfig.Instance.MELTDOWN_TIME.Value > .5f) {
return "reactorscan.result.flavour.low";
} else if (timeRemaining / MeltdownConfig.Instance.MELTDOWN_TIME.Value > .33f) {
return "reactorscan.result.flavour.medium";
} else if (timeRemaining / MeltdownConfig.Instance.MELTDOWN_TIME.Value > .15f) {
return "reactorscan.result.flavour.high";
}
return "";
}
public string GetTeminalOutput() {
String output = "reactorscan.result.unstable".Translate();
output = SubstituteVariables(output);
output += "\n\n" + SubstituteVariables(GetFlavourText().Translate()) + "\n\n";
return output;
}
}
internal static float lastHealthCheck = 0;
internal static ReactorHealthReport lastReport = null;
internal static AudioSource source;
internal static string SubstituteVariables(string text) {
StringBuilder builder = new StringBuilder(text);
builder.Replace("<cooldown>", MeltdownConfig.Instance.SCAN_COOLDOWN.Value.ToString());
builder.Replace("<instability>", lastReport.reactorInstability.ToString());
builder.Replace("<time_left>", lastReport.timeRemaining.ToString());
return builder.ToString();
}
internal static bool ReactorHealthCheckReady() {
return Time.time >= lastHealthCheck + MeltdownConfig.Instance.SCAN_COOLDOWN.Value;
}
internal static ReactorHealthReport GetNewReactorHealthReport() {
float reactorInstability = ((MeltdownConfig.Instance.MELTDOWN_TIME.Value - MeltdownHandler.Instance.meltdownTimer) / MeltdownConfig.Instance.MELTDOWN_TIME.Value) * 100; // this is at perfect accuracy
reactorInstability = Mathf.Round(reactorInstability / MeltdownConfig.Instance.SCAN_ACCURACY.Value) * MeltdownConfig.Instance.SCAN_ACCURACY.Value; // now the ship is not 100% perfect but still consistent (unlike a random value)
float timeRemaining = (1 - (reactorInstability / 100)) * MeltdownConfig.Instance.MELTDOWN_TIME.Value; // not perfectly accurate either
ReactorHealthReport report = new ReactorHealthReport {
reactorInstability = reactorInstability,
timeRemaining = timeRemaining
};
return report;
}
internal static void Init() {
TerminalNode triggerNode = CreateTerminalNode($"use >reactor health to check the current health of the reactor\n", true);
TerminalKeyword verbKeyword = CreateTerminalKeyword("health", true);
TerminalKeyword nounKeyword = CreateTerminalKeyword("reactor");
verbKeyword = verbKeyword.AddCompatibleNoun(nounKeyword, triggerNode);
nounKeyword.defaultVerb = verbKeyword;
AddTerminalKeyword(verbKeyword);
// The second parameter passed in is a CommandInfo, if you want to have a callback.
AddTerminalKeyword(nounKeyword, new CommandInfo() {
TriggerNode = triggerNode,
DisplayTextSupplier = () => {
if(MeltdownHandler.Instance) {
string prefix = "reactorscan.error.overheat";
if(ReactorHealthCheckReady()) {
lastHealthCheck = Time.time;
lastReport = GetNewReactorHealthReport();
prefix = "reactorscan.success";
}
return SubstituteVariables(prefix.Translate()) + lastReport.GetTeminalOutput();
} else {
return "reactorscan.result.stable".Translate();
}
},
Category = "Other",
Description = "do actions with the reactor"
// The above would look like '>FRANK\nThis is just a test command.' in Other
});
}
}
}