forked from jrossignol/ContractConfigurator
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathKSCConnectivityParameter.cs
95 lines (82 loc) · 2.9 KB
/
KSCConnectivityParameter.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
using ContractConfigurator.Parameters;
using Contracts;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using UnityEngine;
using KSP.Localization;
using RemoteTech;
using RemoteTech.API;
using ContractConfigurator;
namespace ContractConfigurator.RemoteTech
{
public class KSCConnectivityParameter : RemoteTechParameter
{
public bool hasConnectivity { get; set; }
const int CHECK_COUNT = 10;
bool[] checks = new bool[CHECK_COUNT];
int currentCheck = 0;
bool initialized = false;
public KSCConnectivityParameter()
: this(true, "")
{
}
public KSCConnectivityParameter(bool hasConnectivity, string title)
: base(title)
{
this.title = string.IsNullOrEmpty(title) ? Localizer.GetStringByTag(hasConnectivity ? "#cc.remotetech.param.KSCConnectivity" : "#cc.remotetech.param.KSCConnectivity.x") : title;
this.hasConnectivity = hasConnectivity;
}
protected override void OnParameterSave(ConfigNode node)
{
base.OnParameterSave(node);
node.AddValue("hasConnectivity", hasConnectivity);
}
protected override void OnParameterLoad(ConfigNode node)
{
base.OnParameterLoad(node);
hasConnectivity = ConfigNodeUtil.ParseValue<bool>(node, "hasConnectivity");
}
/// <summary>
/// Whether this vessel meets the parameter condition.
/// </summary>
/// <param name="vessel">The vessel to check</param>
/// <returns>Whether the vessel meets the condition</returns>
protected override bool VesselMeetsCondition(Vessel vessel)
{
LoggingUtil.LogVerbose(this, "Checking VesselMeetsCondition: {0}", vessel.id);
var satellite = RTCore.Instance.Satellites[vessel.id];
foreach (var v in RTCore.Instance.Network[satellite])
{
LoggingUtil.LogVerbose(this, " Goal = {0}", v.Goal.Name);
LoggingUtil.LogVerbose(this, " Links.Count = {0}", v.Links.Count);
}
// Do a single check
bool result = API.HasConnectionToKSC(vessel.id) ^ !hasConnectivity;
// Store the result
if (!initialized)
{
for (int i = 0; i < CHECK_COUNT; i++)
{
checks[i] = result;
initialized = true;
}
}
else
{
currentCheck = (currentCheck + 1) % CHECK_COUNT;
checks[currentCheck] = result;
}
// Only need one check to have passed
for (int i = 0; i < CHECK_COUNT; i++)
{
if (checks[i])
{
return true;
}
}
return false;
}
}
}