forked from MikeG621/Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTrigger.cs
More file actions
155 lines (145 loc) · 6.29 KB
/
Copy pathBaseTrigger.cs
File metadata and controls
155 lines (145 loc) · 6.29 KB
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
/*
* Idmr.Platform.dll, X-wing series mission library file, XW95-XWA
* Copyright (C) 2009-2024 Michael Gaisser (mjgaisser@gmail.com)
* Licensed under the MPL v2.0 or later
*
* Full notice in ../help/Idmr.Platform.chm
* Version: 7.0
*
* CHANGELOG
* v7.0, 241006
* [NEW] GetBytes, this[TriggerIndex], TriggerIndex.XwaParameter
* v3.0, 180309
* [NEW] helper functions for FG move/delete [JB]
* v2.3, 150405
* [NEW] Added TriggerIndex enum
* v2.2, 150205
* [FIX #5] marked Serializable
* v2.1, 141214
* [UPD] change to MPL
* v2.0, 120525
* [NEW] Indexer<T> implemented
*/
using System;
namespace Idmr.Platform
{
/// <summary>Base class for mission Triggers.</summary>
[Serializable]
public abstract class BaseTrigger : Common.Indexer<byte>
{
/// <summary>Indexes within the Trigger array.</summary>
public enum TriggerIndex : byte
{
/// <summary>The Trigger event.</summary>
Condition,
/// <summary>The category <see cref="Variable"/> belongs to.</summary>
VariableType,
/// <summary>The Trigger subject.</summary>
Variable,
/// <summary>The amount required to fire the event.</summary>
Amount,
/// <summary>The Region or FG paramater.</summary>
/// <remarks>XWA only, should only be called from <see cref="Xwa.Mission.Trigger.Parameter"/> since it's really a short.</remarks>
XwaParameter
};
/// <summary>Default constructor for derived classes.</summary>
protected BaseTrigger() { /* do nothing */ }
/// <summary>Default constructor for derived classes.</summary>
/// <param name="raw">Raw data.</param>
protected BaseTrigger(byte[] raw) : base(raw) { /* do nothing */ }
/// <summary>Gets or sets the appropriate value.</summary>
/// <param name="index">The selected index.</param>
/// <returns>The selected value, or <b>255</b> if invalid selection.</returns>
public byte this[TriggerIndex index]
{
get => (_items.Length == 4 && index == TriggerIndex.XwaParameter) ? (byte)255 : _items[(byte)index];
set { if (_items.Length != 4 || index != TriggerIndex.XwaParameter) _items[(byte)index] = value; }
}
/// <summary>Gets or sets the Trigger itself.</summary>
public byte Condition
{
get => _items[0];
set => _items[0] = value;
}
/// <summary>Gets or sets the category <see cref="Variable"/> belongs to.</summary>
public byte VariableType
{
get => _items[1];
set => _items[1] = value;
}
/// <summary>Gets or sets the Trigger subject.</summary>
public byte Variable
{
get => _items[2];
set => _items[2] = value;
}
/// <summary>Gets or sets the amount required to fire the Trigger.</summary>
public byte Amount
{
get => _items[3];
set => _items[3] = value;
}
/// <summary>Helper function that changes Flight Group indexes during a FG Move or Delete operation.</summary>
/// <remarks>Should not be called directly unless part of a larger FG Move or Delete operation. FG references may exist in other mission properties, ensure those properties are adjusted when applicable.</remarks>
/// <param name="srcIndex">The FG index to match and replace (Move), or match and Delete.</param>
/// <param name="dstIndex">The FG index to replace with. Specify <b>-1</b> to Delete, or <b>zero</b> or above to Move.</param>
/// <param name="delCond">Ignored unless FG is deleted. If <b>true</b>, condition is set to "Always (true)" otherwise "Never (false)".</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="dstIndex"/> is greater than <b>255</b>.</exception>
/// <returns>Returns <b>true</b> if anything was changed.</returns>
public bool TransformFGReferences(int srcIndex, int dstIndex, bool delCond)
{
bool change = false;
bool delete = false;
if (dstIndex < 0)
{
dstIndex = 0;
delete = true;
}
else if (dstIndex > 255) throw new ArgumentOutOfRangeException("TransformFGRef: dstIndex out of range.");
change |= TransformFGReferencesExtended(srcIndex, dstIndex, delete, delCond);
//In TIE/XvT/XWA, VariableType==1 is targeting a specific Flight Group. Condition==0 is TRUE, Condition==10 is FALSE.
if (VariableType == 1 && Variable == srcIndex)
{
change = true;
Variable = (byte)dstIndex;
if (delete)
{
Amount = 0;
VariableType = 0;
Variable = 0;
Condition = (byte)(delCond ? 0 : 10); //this will expand the bool true/false into the condition true/false
}
}
else if (VariableType == 1 && Variable > srcIndex && delete)
{
change = true;
Variable--; //If deleting, decrement FG index to maintain
}
return change;
}
/// <summary>This allows overrides to check additional properties without needing to override the base function.</summary>
/// <param name="srcIndex">The FG index to match and replace (Move), or match and Delete.</param>
/// <param name="dstIndex">The FG index to replace with. Specify <b>-1</b> to Delete, or <b>zero</b> or above to Move.</param>
/// <param name="delete">Whether or not to delete the FG.</param>
/// <param name="delCond">Ignored unless FG is deleted. If <b>true</b>, condition is set to "Always (true)" otherwise "Never (false)".</param>
/// <returns>Always returns <b>false</b>.</returns>
protected virtual bool TransformFGReferencesExtended(int srcIndex, int dstIndex, bool delete, bool delCond) => false; /* do nothing */
/// <summary>Helper function that changes Flight Group indexes during a Move (index swap) operation.</summary>
/// <remarks>Should not be called directly unless part of a larger FG Move operation. FG references may exist in other mission properties, ensure those properties are adjusted when applicable.</remarks>
/// <param name="srcIndex">The FG index to match and replace.</param>
/// <param name="dstIndex">The FG index to replace with.</param>
/// <returns>Returns true if anything was changed.</returns>
public bool SwapFGReferences(int srcIndex, int dstIndex)
{
bool change = false;
change |= TransformFGReferences(dstIndex, 255, false);
change |= TransformFGReferences(srcIndex, dstIndex, false);
change |= TransformFGReferences(255, srcIndex, false);
return change;
}
/// <summary>Gets a copy of the Trigger as a byte array.</summary>
/// <remarks>Length is <b>4</b> for TIE and XvT/BoP, <b>6</b> for XWA.</remarks>
/// <returns>The byte array equivalent.</returns>
public byte[] GetBytes() => (byte[])_items.Clone();
}
}