forked from GarwelGarwel/KerbalHealth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logic.cs
224 lines (211 loc) · 10.7 KB
/
Logic.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace KerbalHealth
{
/// <summary>
/// Contains logical conditions to be checked for true/false (e.g. object is in a specific SOI etc.)
/// </summary>
public class Logic
{
public enum OperatorType { And, Or };
public OperatorType Operator { get; set; } = OperatorType.And;
public bool Inverse { get; set; } = false;
public static string Padding = "-";
public string Situation { get; set; } = null;
public string InSOI { get; set; } = null;
public string KerbalStatus { get; set; } = null;
public double MissionTime { get; set; } = Double.NaN;
public string Gender { get; set; } = null;
public string GenderPresent { get; set; } = null;
public string TraitPresent { get; set; } = null;
public string ConditionPresent { get; set; } = null;
public List<Logic> Operands { get; set; } = new List<Logic>();
bool Op(ref bool operand1, bool operand2)
{
switch (Operator)
{
case OperatorType.And: operand1 &= operand2; break;
case OperatorType.Or: operand1 |= operand2; break;
}
return operand1;
}
public bool Test(ProtoCrewMember pcm)
{
Core.Log("Logic.Test('" + pcm.name + "')");
bool res = true;
if (pcm == null)
{
Core.Log("ProtoCrewMember argument in Logic.Test is null!", Core.LogLevel.Error);
return res;
}
Vessel v = Core.KerbalVessel(pcm);
if (Situation != null)
if (v != null)
{
Core.Log("Checking 'situation = " + Situation + "'. " + v.vesselName + " is " + v.situation);
switch (Situation.ToLower())
{
case "prelaunch": Op(ref res, v.situation == Vessel.Situations.PRELAUNCH); break;
case "landed": Op(ref res, v.situation == Vessel.Situations.LANDED); break;
case "splashed": Op(ref res, v.situation == Vessel.Situations.SPLASHED); break;
case "ground": Op(ref res, (v.situation == Vessel.Situations.LANDED) || (v.situation == Vessel.Situations.SPLASHED)); break;
case "flying": Op(ref res, v.situation == Vessel.Situations.FLYING); break;
case "suborbital": Op(ref res, v.situation == Vessel.Situations.SUB_ORBITAL); break;
case "orbiting": Op(ref res, v.situation == Vessel.Situations.ORBITING); break;
case "escaping": Op(ref res, v.situation == Vessel.Situations.ESCAPING); break;
case "in space": Op(ref res, (v.situation == Vessel.Situations.SUB_ORBITAL) || (v.situation == Vessel.Situations.ORBITING) || (v.situation == Vessel.Situations.ESCAPING)); break;
}
}
else Op(ref res, false);
if (InSOI != null)
if (v != null)
{
Core.Log("Checking 'inSOI = " + InSOI + "' rule. " + pcm.name + " is in " + v.mainBody?.name + "'s SOI.");
Op(ref res, InSOI.Equals(v.mainBody.name, StringComparison.CurrentCultureIgnoreCase));
}
else
{
Core.Log("Checking 'inSOI = " + InSOI + "' rule. " + pcm.name + " is not in a vessel => this logic is false.");
Op(ref res, false);
}
if (KerbalStatus != null)
{
Core.Log("Checking 'kerbalStatus = " + KerbalStatus + "'. " + pcm.name + " is " + pcm.rosterStatus);
Op(ref res, KerbalStatus.Equals(pcm.rosterStatus.ToString(), StringComparison.CurrentCultureIgnoreCase));
}
if (!Double.IsNaN(MissionTime))
{
Core.Log("Checking 'missionTime = " + MissionTime + "'. MET is " + v?.missionTime + ".");
if (v != null) Op(ref res, v.missionTime >= MissionTime);
else Op(ref res, false);
}
if (Gender != null)
{
ProtoCrewMember.Gender g = pcm.gender;
Core.Log("Checking condition 'gender = " + Gender + "'. " + pcm.name + " is " + g);
switch (Gender.ToLower())
{
case "female": Op(ref res, g == ProtoCrewMember.Gender.Female); break;
case "male": Op(ref res, g == ProtoCrewMember.Gender.Male); break;
}
}
if (GenderPresent != null)
{
ProtoCrewMember.Gender g;
switch (GenderPresent.ToLower())
{
case "female": g = ProtoCrewMember.Gender.Female; break;
case "male": g = ProtoCrewMember.Gender.Male; break;
case "same": g = pcm.gender; break;
case "other": g = pcm.gender == ProtoCrewMember.Gender.Female ? ProtoCrewMember.Gender.Male : ProtoCrewMember.Gender.Female; break;
default:
Core.Log("Unrecognized value for gender in 'genderPresent = " + GenderPresent + "'. Assuming 'other'.");
goto case "other";
}
Core.Log("Checking condition 'genderPresent = " + GenderPresent + "'. Looking for " + g + " crewmates.");
bool found = false;
if (v != null)
foreach (ProtoCrewMember crewmate in v.GetVesselCrew())
if ((crewmate.gender == g) && (crewmate != pcm))
{
found = true;
break;
}
Core.Log(g + " crewmates " + (found ? "" : "not ") + "found.");
Op(ref res, found);
}
if (TraitPresent != null)
{
Core.Log("Checking condition 'traitPresent = " + TraitPresent + "'.");
bool found = false;
if (v != null)
foreach (ProtoCrewMember crewmate in v.GetVesselCrew())
if ((crewmate.trait.ToLower() == TraitPresent.ToLower()) && (crewmate != pcm))
{
found = true;
break;
}
Core.Log(TraitPresent + " crewmates " + (found ? "" : "not ") + "found.");
Op(ref res, found);
}
if (ConditionPresent != null)
{
Core.Log("Checking condition 'conditionPresent = " + ConditionPresent + "'.");
bool found = false;
if (v != null)
foreach (ProtoCrewMember crewmate in v.GetVesselCrew())
if ((Core.KerbalHealthList.Find(crewmate.name).HasCondition(ConditionPresent)) && (crewmate != pcm))
{
found = true;
break;
}
Core.Log("Crewmates with " + ConditionPresent + (found ? "" : " not") + " found.");
Op(ref res, found);
}
foreach (Logic l in Operands)
Op(ref res, l.Test(pcm));
return res ^ Inverse;
}
public string Description(int level)
{
string res = "";
string indent1 = "";
for (int i = 0; i < level; i++) indent1 += Padding;
string indent2 = indent1 + Padding + " ";
if (level > 0) indent1 += " ";
if (Situation != null) res += "\n" + indent2 + "Is " + Situation;
if (InSOI != null) res += "\n" + indent2 + "Kerbal is in the SOI of " + InSOI;
if (KerbalStatus != null) res += "\n" + indent2 + "Kerbal is" + KerbalStatus;
if (!Double.IsNaN(MissionTime)) res += "\n" + indent2 + "Mission lasts at least " + Core.ParseUT(MissionTime, false, 100);
if (Gender != null) res += "\n" + indent2 + "Kerbal is " + Gender;
if (GenderPresent != null) res += "\n" + indent2 + GenderPresent + " gender kerbal(s) present in the vessel";
if (TraitPresent != null) res += "\n" + indent2 + TraitPresent + " kerbal(s) present in the vessel";
if (ConditionPresent != null) res += "\n" + indent2 + "Kerbal(s) with " + ConditionPresent + " present in the vessel";
foreach (Logic l in Operands) res += "\n" + l.Description(level + 1);
if (Core.CountChars(res, '\n') >= 2)
res = indent1 + (Operator == OperatorType.And ? (Inverse ? "One" : "All") : (Inverse ? "None" : "One")) + " of the following conditions is " + ((Operator == OperatorType.And) && Inverse ? "false" : "true") + ":" + res;
else if ((res != "") && Inverse) res = indent1 + "This is FALSE:" + res;
else res = res.Trim('\n');
return res;
}
public override string ToString() => Description(0);
public ConfigNode ConfigNode
{
set
{
string s = Core.GetString(value, "operator") ?? Core.GetString(value, "logic");
switch (s?.ToLower())
{
case "and":
case "all":
case null:
Operator = OperatorType.And;
break;
case "or":
case "any":
Operator = OperatorType.Or;
break;
default:
Core.Log("Unrecognized Logic operator '" + s + "' in config node " + value.name + ".", Core.LogLevel.Error);
break;
}
Inverse = Core.GetBool(value, "inverse");
Situation = Core.GetString(value, "situation");
InSOI = Core.GetString(value, "inSOI");
if (InSOI?.ToLower() == "home") InSOI = FlightGlobals.GetHomeBodyName();
KerbalStatus = Core.GetString(value, "kerbalStatus");
MissionTime = Core.GetDouble(value, "missionTime", Double.NaN);
Gender = Core.GetString(value, "gender");
GenderPresent = Core.GetString(value, "genderPresent");
TraitPresent = Core.GetString(value, "traitPresent");
ConditionPresent = Core.GetString(value, "conditionPresent");
foreach (ConfigNode node in value.GetNodes("LOGIC"))
Operands.Add(new Logic(node));
}
}
public Logic() { }
public Logic(ConfigNode node) => ConfigNode = node;
}
}