-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathKerbalHealthList.cs
118 lines (107 loc) · 3.8 KB
/
KerbalHealthList.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
using System.Collections.Generic;
using System.Linq;
namespace KerbalHealth
{
/// <summary>
/// List of all tracked kerbals
/// </summary>
public class KerbalHealthList : Dictionary<string, KerbalHealthStatus>
{
public new KerbalHealthStatus this[string name]
{
get => TryGetValue(name, out KerbalHealthStatus res) ? res : null;
set => base[name] = value;
}
public KerbalHealthStatus this[ProtoCrewMember pcm]
{
get => this[pcm.name];
set => base[pcm.name] = value;
}
public KerbalHealthList()
: base(HighLogic.fetch.currentGame.CrewRoster.Count)
{ }
public List<KerbalHealthStatus> List => Values.ToList();
/// <summary>
/// Adds a kerbal to the list, unless already exists
/// </summary>
public void Add(ProtoCrewMember pcm)
{
if (pcm == null)
{
Core.Log($"Trying to register a KerbalHealthStatus for a null ProtoCrewMember!", LogLevel.Error);
return;
}
if (ContainsKey(pcm.name))
return;
Core.Log($"Registering {pcm.name}.", LogLevel.Important);
Add(pcm.name, new KerbalHealthStatus(pcm));
}
/// <summary>
/// Adds a kerbal to the list, unless already exists
/// </summary>
public void Add(KerbalHealthStatus khs)
{
if (!ContainsKey(khs.Name))
Add(khs.Name, khs);
}
/// <summary>
/// Changes name of a registered kerbal and renames the entry
/// </summary>
public void Rename(string name1, string name2)
{
Core.Log($"KerbalHealthList.Rename('{name1}', '{name2}')");
if (name1 == name2)
return;
if (ContainsKey(name1))
{
this[name1].Name = name2;
Add(name2, this[name1]);
Remove(name1);
}
else Core.Log($"Could not find '{name1}'.", LogLevel.Error);
}
/// <summary>
/// Scans all trackable kerbals and adds them to the list
/// </summary>
public void RegisterKerbals()
{
Core.Log("Registering kerbals...");
RemoveUntrackable();
KerbalRoster kerbalRoster = HighLogic.fetch.currentGame.CrewRoster;
List<ProtoCrewMember> list = new List<ProtoCrewMember>(kerbalRoster.Crew);
list.AddRange(kerbalRoster.Tourist);
Core.Log($"{list.Count} total trackable kerbals.", LogLevel.Important);
for (int i = 0; i < list.Count; i++)
if (list[i].IsTrackable())
Add(list[i]);
Core.Log($"KerbalHealthList updated: {Count} kerbals found.", LogLevel.Important);
}
public void Update(float interval)
{
RemoveUntrackable();
List<KerbalHealthStatus> list = List;
for (int i = 0; i < Count; i++)
list[i].Update(interval);
}
/// <summary>
/// Returns the list of names
/// </summary>
public override string ToString()
{
string s = "";
foreach (string n in Keys)
s += $"{n}\r\n";
return s.Trim();
}
void RemoveUntrackable()
{
List<KerbalHealthStatus> list = List;
for (int i = Count - 1; i >= 0; i--)
{
KerbalHealthStatus khs = list[i];
if (!khs.ProtoCrewMember.IsTrackable() && !khs.IsFrozen)
Remove(khs.Name);
}
}
}
}