forked from allista/ThrottleControlledAvionics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCAComponent.cs
193 lines (152 loc) · 6.14 KB
/
TCAComponent.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
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2015 Allis Tauri
//
// This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/
// or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
using System;
using System.Linq;
using System.Collections.Generic;
using AT_Utils;
namespace ThrottleControlledAvionics
{
public interface ITCAComponent
{
VesselConfig CFG { get; }
TCAState State { get; }
void SetState(TCAState state);
bool IsStateSet(TCAState state);
}
public abstract class TCAComponent : ConfigNodeObject, ITCAComponent
{
public readonly ModuleTCA TCA;
public VesselWrapper VSL { get { return TCA.VSL; } }
internal static Globals GLB { get { return Globals.Instance; } }
public VesselConfig CFG { get { return TCA.VSL.CFG; } }
public TCAState State { get { return VSL.State; } }
public void SetState(TCAState state) { VSL.State |= state; }
public bool IsStateSet(TCAState state) { return VSL.IsStateSet(state); }
protected SquadControl SQD;
protected TCAComponent(ModuleTCA tca) { TCA = tca; }
public void InitModuleFields() { TCA.InitModuleFields(this); }
protected void Message(float duration, string msg, params object[] args)
{ if(VSL.IsActiveVessel) Utils.Message(duration, msg, args); }
protected void Message(string msg, params object[] args) { Message(5, msg, args); }
protected void ClearStatus() { if(VSL.IsActiveVessel) TCAGui.ClearStatus(); }
protected void Status(double seconds, string msg, params object[] args)
{ if(VSL.IsActiveVessel) TCAGui.Status(seconds, msg, args); }
protected void Status(string msg, params object[] args)
{ Status(-1, msg, args); }
protected void Status(double seconds, string color, string msg, params object[] args)
{ if(VSL.IsActiveVessel) TCAGui.Status(seconds, color, msg, args); }
protected void Status(string color, string msg, params object[] args)
{ Status(-1, color, msg, args); }
protected string LogTemplate(string msg)
{ return string.Format("{0}.{1}: {2}", VSL.vessel.vesselName, GetType().Name, msg); }
protected void Log(string msg, params object[] args) { Utils.Log(LogTemplate(msg), args); }
#if DEBUG
protected void LogFST(string msg, params object[] args) { DebugUtils.Log(LogTemplate(msg), args); }
protected void CSV(params object[] args)
{
var tag = string.Format("{0}.{1}.csv", VSL.vessel.vesselName, GetType().Name).Replace(' ', '_');
DebugUtils.CSV(tag, args);
}
#endif
}
public abstract class DrawableComponent : TCAComponent
{
protected DrawableComponent(ModuleTCA tca) : base(tca) {}
public abstract void Draw();
}
public class TCAModule : DrawableComponent
{
public class ModuleConfig : ConfigNodeObject
{
public virtual void Init() {}
}
public bool ControlsActive { get; protected set; } = true;
public bool IsActive { get; protected set; }
public bool Working { get; protected set; }
protected TCAModule(ModuleTCA tca) : base(tca) {}
public virtual void Init() { InitModuleFields(); LoadFromConfig(); }
public void OnFixedUpdate() { UpdateState(); Update(); }
public virtual void Reset() {}
public virtual void ClearFrameState() {}
public virtual void OnEnable(bool enabled) {}
public virtual void ProcessKeys() {}
public override void Draw() {}
protected virtual void UpdateState() { IsActive = VSL != null && CFG.Enabled; ControlsActive = true; }
protected virtual void Update() {}
protected virtual void reset() {}
protected void SetTarget(WayPoint wp = null) { VSL.SetTarget(this, wp); }
protected void SetTarget(Vessel vsl) { SetTarget(new WayPoint(vsl)); }
protected void UseTarget() { VSL.TargetUsers.Add(this); }
protected void StopUsingTarget() { VSL.TargetUsers.Remove(this); }
public bool RegisterTo<S>(Func<VesselWrapper,bool> predicate = null)
where S : TCAService
{
var srv = TCA.GetModule<S>();
return srv != null && srv.Register(this, predicate);
}
public bool NeedRadarWhenMooving()
{ return RegisterTo<Radar>(vsl => vsl.HorizontalSpeed.MoovingFast); }
public bool UnregisterFrom<S>()
where S : TCAService
{
var srv = TCA.GetModule<S>();
return srv != null && srv.Unregister(this);
}
public void SaveToConfig()
{
var node = new ConfigNode(GetType().Name);
Save(node);
CFG.ModuleConfigs[node.name] = node;
}
public void LoadFromConfig()
{
ConfigNode node;
var name = GetType().Name;
if(CFG.ModuleConfigs.TryGetValue(name, out node)) Load(node);
}
public void SaveGame(string description)
{
if(Globals.Instance.AutosaveBeforeLanding)
Utils.SaveGame(VSL.vessel.vesselName.Replace(" ", "_")+"-"+description);
}
}
public abstract class AutopilotModule : TCAModule
{
protected AutopilotModule(ModuleTCA tca) : base(tca) {}
public override void Init()
{
base.Init();
VSL.vessel.OnAutopilotUpdate -= UpdateCtrlState;
VSL.vessel.OnAutopilotUpdate += UpdateCtrlState;
}
public override void Reset() { VSL.vessel.OnAutopilotUpdate -= UpdateCtrlState; }
public void UpdateCtrlState(FlightCtrlState s) { UpdateState(); OnAutopilotUpdate(s); }
protected abstract void OnAutopilotUpdate(FlightCtrlState s);
}
public abstract class TCAService : TCAModule
{
public struct Client
{
public readonly TCAModule Module;
public Func<VesselWrapper,bool> Predicate;
public Client(TCAModule module, Func<VesselWrapper,bool> predicate = null)
{ Module = module; Predicate = predicate; }
public static implicit operator bool(Client c)
{ return c.Module != null && (c.Predicate == null || c.Predicate(c.Module.VSL)); }
public override int GetHashCode() { return Module.GetHashCode(); }
}
readonly HashSet<Client> Clients = new HashSet<Client>();
protected bool HasActiveClients { get { return Clients.Any(c => c); } }
public bool Register(TCAModule module, Func<VesselWrapper,bool> predicate = null)
{ return Clients.Add(new Client(module, predicate)); }
public bool Unregister(TCAModule module)
{ return Clients.Remove(new Client(module)); }
protected TCAService(ModuleTCA tca) : base(tca) {}
}
}