/
TraitsInterfaces.cs
530 lines (442 loc) · 17.2 KB
/
TraitsInterfaces.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#region Copyright & License Information
/*
* Copyright 2007-2019 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Network;
using OpenRA.Primitives;
namespace OpenRA.Traits
{
public sealed class RequireExplicitImplementationAttribute : Attribute { }
[Flags]
public enum DamageState
{
Undamaged = 1,
Light = 2,
Medium = 4,
Heavy = 8,
Critical = 16,
Dead = 32
}
/// <summary>
/// Type tag for DamageTypes <see cref="Primitives.BitSet{T}"/>.
/// </summary>
public sealed class DamageType { DamageType() { } }
public interface IHealthInfo : ITraitInfo
{
int MaxHP { get; }
}
public interface IHealth
{
DamageState DamageState { get; }
int HP { get; }
int MaxHP { get; }
int DisplayHP { get; }
bool IsDead { get; }
void InflictDamage(Actor self, Actor attacker, Damage damage, bool ignoreModifiers);
void Kill(Actor self, Actor attacker, BitSet<DamageType> damageTypes);
}
// depends on the order of pips in WorldRenderer.cs!
public enum PipType { Transparent, Green, Yellow, Red, Gray, Blue, Ammo, AmmoEmpty }
[Flags]
public enum Stance
{
None = 0,
Enemy = 1,
Neutral = 2,
Ally = 4,
}
public static class StanceExts
{
public static bool HasStance(this Stance s, Stance stance)
{
// PERF: Enum.HasFlag is slower and requires allocations.
return (s & stance) == stance;
}
}
public class AttackInfo
{
public Damage Damage;
public Actor Attacker;
public DamageState DamageState;
public DamageState PreviousDamageState;
}
public class Damage
{
public readonly int Value;
public readonly BitSet<DamageType> DamageTypes;
public Damage(int damage, BitSet<DamageType> damageTypes)
{
Value = damage;
DamageTypes = damageTypes;
}
public Damage(int damage)
{
Value = damage;
DamageTypes = default(BitSet<DamageType>);
}
}
[RequireExplicitImplementation]
public interface ITick { void Tick(Actor self); }
[RequireExplicitImplementation]
public interface ITickRender { void TickRender(WorldRenderer wr, Actor self); }
public interface IRender
{
IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr);
IEnumerable<Rectangle> ScreenBounds(Actor self, WorldRenderer wr);
}
// TODO: Replace Rectangle with an int2[] polygon
public interface IMouseBounds { Rectangle MouseoverBounds(Actor self, WorldRenderer wr); }
public interface IMouseBoundsInfo : ITraitInfoInterface { }
public interface IAutoMouseBounds { Rectangle AutoMouseoverBounds(Actor self, WorldRenderer wr); }
// HACK: This provides a shim for legacy code until it can be rewritten
public interface IDecorationBounds { Rectangle DecorationBounds(Actor self, WorldRenderer wr); }
public interface IDecorationBoundsInfo : ITraitInfoInterface { }
public static class DecorationBoundsExtensions
{
public static Rectangle FirstNonEmptyBounds(this IEnumerable<IDecorationBounds> decorationBounds, Actor self, WorldRenderer wr)
{
// PERF: Avoid LINQ.
foreach (var decoration in decorationBounds)
{
var bounds = decoration.DecorationBounds(self, wr);
if (!bounds.IsEmpty)
return bounds;
}
return Rectangle.Empty;
}
public static Rectangle FirstNonEmptyBounds(this IDecorationBounds[] decorationBounds, Actor self, WorldRenderer wr)
{
// PERF: Avoid LINQ.
foreach (var decoration in decorationBounds)
{
var bounds = decoration.DecorationBounds(self, wr);
if (!bounds.IsEmpty)
return bounds;
}
return Rectangle.Empty;
}
}
public interface IIssueOrder
{
IEnumerable<IOrderTargeter> Orders { get; }
Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued);
}
[Flags] public enum TargetModifiers { None = 0, ForceAttack = 1, ForceQueue = 2, ForceMove = 4 }
public static class TargetModifiersExts
{
public static bool HasModifier(this TargetModifiers self, TargetModifiers m)
{
// PERF: Enum.HasFlag is slower and requires allocations.
return (self & m) == m;
}
}
public interface IOrderTargeter
{
string OrderID { get; }
int OrderPriority { get; }
bool CanTarget(Actor self, Target target, List<Actor> othersAtTarget, ref TargetModifiers modifiers, ref string cursor);
bool IsQueued { get; }
bool TargetOverridesSelection(TargetModifiers modifiers);
}
public interface IResolveOrder { void ResolveOrder(Actor self, Order order); }
public interface IValidateOrder { bool OrderValidation(OrderManager orderManager, World world, int clientId, Order order); }
public interface IOrderVoice { string VoicePhraseForOrder(Actor self, Order order); }
[RequireExplicitImplementation]
public interface INotifyCreated { void Created(Actor self); }
[RequireExplicitImplementation]
public interface INotifyAddedToWorld { void AddedToWorld(Actor self); }
[RequireExplicitImplementation]
public interface INotifyRemovedFromWorld { void RemovedFromWorld(Actor self); }
[RequireExplicitImplementation]
public interface INotifyActorDisposing { void Disposing(Actor self); }
public interface INotifyOwnerChanged { void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner); }
public interface INotifyEffectiveOwnerChanged { void OnEffectiveOwnerChanged(Actor self, Player oldEffectiveOwner, Player newEffectiveOwner); }
public interface INotifyOwnerLost { void OnOwnerLost(Actor self); }
[RequireExplicitImplementation]
public interface IVoiced
{
string VoiceSet { get; }
bool PlayVoice(Actor self, string phrase, string variant);
bool PlayVoiceLocal(Actor self, string phrase, string variant, float volume);
bool HasVoice(Actor self, string voice);
}
[RequireExplicitImplementation]
public interface IStoreResources { int Capacity { get; } }
public interface IEffectiveOwner
{
bool Disguised { get; }
Player Owner { get; }
}
public interface ITooltip
{
ITooltipInfo TooltipInfo { get; }
Player Owner { get; }
}
public interface ITooltipInfo : ITraitInfoInterface
{
string TooltipForPlayerStance(Stance stance);
bool IsOwnerRowVisible { get; }
}
public interface IProvideTooltipInfo
{
bool IsTooltipVisible(Player forPlayer);
string TooltipText { get; }
}
public interface IDisabledTrait { bool IsTraitDisabled { get; } }
public interface IDefaultVisibilityInfo : ITraitInfoInterface { }
public interface IDefaultVisibility { bool IsVisible(Actor self, Player byPlayer); }
public interface IVisibilityModifier { bool IsVisible(Actor self, Player byPlayer); }
public interface IActorMap
{
IEnumerable<Actor> GetActorsAt(CPos a);
IEnumerable<Actor> GetActorsAt(CPos a, SubCell sub);
bool HasFreeSubCell(CPos cell, bool checkTransient = true);
SubCell FreeSubCell(CPos cell, SubCell preferredSubCell = SubCell.Any, bool checkTransient = true);
SubCell FreeSubCell(CPos cell, SubCell preferredSubCell, Func<Actor, bool> checkIfBlocker);
bool AnyActorsAt(CPos a);
bool AnyActorsAt(CPos a, SubCell sub, bool checkTransient = true);
bool AnyActorsAt(CPos a, SubCell sub, Func<Actor, bool> withCondition);
void AddInfluence(Actor self, IOccupySpace ios);
void RemoveInfluence(Actor self, IOccupySpace ios);
int AddCellTrigger(CPos[] cells, Action<Actor> onEntry, Action<Actor> onExit);
void RemoveCellTrigger(int id);
int AddProximityTrigger(WPos pos, WDist range, WDist vRange, Action<Actor> onEntry, Action<Actor> onExit);
void RemoveProximityTrigger(int id);
void UpdateProximityTrigger(int id, WPos newPos, WDist newRange, WDist newVRange);
void AddPosition(Actor a, IOccupySpace ios);
void RemovePosition(Actor a, IOccupySpace ios);
void UpdatePosition(Actor a, IOccupySpace ios);
IEnumerable<Actor> ActorsInBox(WPos a, WPos b);
WDist LargestActorRadius { get; }
WDist LargestBlockingActorRadius { get; }
}
[RequireExplicitImplementation]
public interface IRenderModifier
{
IEnumerable<IRenderable> ModifyRender(Actor self, WorldRenderer wr, IEnumerable<IRenderable> r);
// HACK: This is here to support the WithShadow trait.
// That trait should be rewritten using standard techniques, and then this interface method removed
IEnumerable<Rectangle> ModifyScreenBounds(Actor self, WorldRenderer wr, IEnumerable<Rectangle> r);
}
[RequireExplicitImplementation]
public interface IProvidesCursorPaletteInfo : ITraitInfoInterface
{
string Palette { get; }
ImmutablePalette ReadPalette(IReadOnlyFileSystem fileSystem);
}
public interface ILoadsPalettes { void LoadPalettes(WorldRenderer wr); }
public interface ILoadsPlayerPalettes { void LoadPlayerPalettes(WorldRenderer wr, string playerName, Color playerColor, bool replaceExisting); }
public interface IPaletteModifier { void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> b); }
public interface IPips { IEnumerable<PipType> GetPips(Actor self); }
[RequireExplicitImplementation]
public interface ISelectionBar { float GetValue(); Color GetColor(); bool DisplayWhenEmpty { get; } }
public interface ISelectionDecorations { void DrawRollover(Actor self, WorldRenderer worldRenderer); }
public interface IMapPreviewSignatureInfo : ITraitInfoInterface
{
void PopulateMapPreviewSignatureCells(Map map, ActorInfo ai, ActorReference s, List<Pair<MPos, Color>> destinationBuffer);
}
public interface IOccupySpaceInfo : ITraitInfoInterface
{
IReadOnlyDictionary<CPos, SubCell> OccupiedCells(ActorInfo info, CPos location, SubCell subCell = SubCell.Any);
bool SharesCell { get; }
}
public interface IOccupySpace
{
WPos CenterPosition { get; }
CPos TopLeft { get; }
Pair<CPos, SubCell>[] OccupiedCells();
}
public enum SubCell : byte { Invalid = byte.MaxValue, Any = byte.MaxValue - 1, FullCell = 0, First = 1 }
public interface IPositionableInfo : IOccupySpaceInfo
{
bool CanEnterCell(World world, Actor self, CPos cell, Actor ignoreActor = null, bool checkTransientActors = true);
}
public interface IPositionable : IOccupySpace
{
bool CanExistInCell(CPos location);
bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any);
bool CanEnterCell(CPos location, Actor ignoreActor = null, bool checkTransientActors = true);
SubCell GetValidSubCell(SubCell preferred = SubCell.Any);
SubCell GetAvailableSubCell(CPos location, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true);
void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.Any);
void SetPosition(Actor self, WPos pos);
void SetVisualPosition(Actor self, WPos pos);
}
public interface ITemporaryBlockerInfo : ITraitInfoInterface { }
[RequireExplicitImplementation]
public interface ITemporaryBlocker
{
bool CanRemoveBlockage(Actor self, Actor blocking);
bool IsBlocking(Actor self, CPos cell);
}
public interface IFacing
{
int TurnSpeed { get; }
int Facing { get; set; }
}
public interface IFacingInfo : ITraitInfoInterface { int GetInitialFacing(); }
public interface ITraitInfoInterface { }
public interface ITraitInfo : ITraitInfoInterface { object Create(ActorInitializer init); }
public class TraitInfo<T> : ITraitInfo where T : new() { public virtual object Create(ActorInitializer init) { return new T(); } }
public interface ILobbyCustomRulesIgnore { }
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1302:InterfaceNamesMustBeginWithI", Justification = "Not a real interface, but more like a tag.")]
public interface Requires<T> where T : class, ITraitInfoInterface { }
[RequireExplicitImplementation]
public interface INotifySelected { void Selected(Actor self); }
[RequireExplicitImplementation]
public interface INotifySelection { void SelectionChanged(); }
public interface IWorldLoaded { void WorldLoaded(World w, WorldRenderer wr); }
public interface INotifyGameLoading { void GameLoading(World w); }
public interface INotifyGameLoaded { void GameLoaded(World w); }
public interface INotifyGameSaved { void GameSaved(World w); }
public interface IGameSaveTraitData
{
List<MiniYamlNode> IssueTraitData(Actor self);
void ResolveTraitData(Actor self, List<MiniYamlNode> data);
}
[RequireExplicitImplementation]
public interface ICreatePlayers { void CreatePlayers(World w); }
public interface IBotInfo : ITraitInfoInterface
{
string Type { get; }
string Name { get; }
}
public interface IBot
{
void Activate(Player p);
void QueueOrder(Order order);
IBotInfo Info { get; }
Player Player { get; }
}
[RequireExplicitImplementation]
public interface IRenderOverlay { void Render(WorldRenderer wr); }
[RequireExplicitImplementation]
public interface INotifyBecomingIdle { void OnBecomingIdle(Actor self); }
[RequireExplicitImplementation]
public interface INotifyIdle { void TickIdle(Actor self); }
public interface IRenderAboveWorld { void RenderAboveWorld(Actor self, WorldRenderer wr); }
public interface IRenderShroud { void RenderShroud(Shroud shroud, WorldRenderer wr); }
[RequireExplicitImplementation]
public interface IRenderTerrain { void RenderTerrain(WorldRenderer wr, Viewport viewport); }
public interface IRenderAboveShroud
{
IEnumerable<IRenderable> RenderAboveShroud(Actor self, WorldRenderer wr);
bool SpatiallyPartitionable { get; }
}
public interface IRenderAboveShroudWhenSelected
{
IEnumerable<IRenderable> RenderAboveShroud(Actor self, WorldRenderer wr);
bool SpatiallyPartitionable { get; }
}
public interface ISelection
{
int Hash { get; }
IEnumerable<Actor> Actors { get; }
void SetNotifySelection(IEnumerable<INotifySelection> worldNotifySelection);
void Add(Actor a);
void Remove(Actor a);
void OnOwnerChanged(Actor a, Player oldOwner, Player newOwner);
bool Contains(Actor a);
void Combine(World world, IEnumerable<Actor> newSelection, bool isCombine, bool isClick);
void Clear();
void Tick(World world);
void DoControlGroup(World world, WorldRenderer worldRenderer, int group, Modifiers mods, int multiTapCount);
void AddToControlGroup(Actor a, int group);
void RemoveFromControlGroup(Actor a);
int? GetControlGroupForActor(Actor a);
List<MiniYamlNode> Serialize();
void Deserialize(World world, List<MiniYamlNode> data);
}
/// <summary>
/// Indicates target types as defined on <see cref="Traits.ITargetable"/> are present in a <see cref="Primitives.BitSet{T}"/>.
/// </summary>
public sealed class TargetableType { TargetableType() { } }
public interface ITargetableInfo : ITraitInfoInterface
{
BitSet<TargetableType> GetTargetTypes();
}
public interface ITargetable
{
// Check IsTraitEnabled or !IsTraitDisabled first
BitSet<TargetableType> TargetTypes { get; }
bool TargetableBy(Actor self, Actor byActor);
bool RequiresForceFire { get; }
}
[RequireExplicitImplementation]
public interface ITargetablePositions
{
IEnumerable<WPos> TargetablePositions(Actor self);
bool AlwaysEnabled { get; }
}
public interface IMoveInfo : ITraitInfoInterface { }
[RequireExplicitImplementation]
public interface IGameOver { void GameOver(World world); }
public interface IWarhead
{
int Delay { get; }
bool IsValidAgainst(Actor victim, Actor firedBy);
bool IsValidAgainst(FrozenActor victim, Actor firedBy);
void DoImpact(Target target, Actor firedBy, IEnumerable<int> damageModifiers);
}
public interface IRulesetLoaded<TInfo> { void RulesetLoaded(Ruleset rules, TInfo info); }
public interface IRulesetLoaded : IRulesetLoaded<ActorInfo>, ITraitInfoInterface { }
[RequireExplicitImplementation]
public interface ILobbyOptions : ITraitInfoInterface
{
IEnumerable<LobbyOption> LobbyOptions(Ruleset rules);
}
public class LobbyOption
{
public readonly string Id;
public readonly string Name;
public readonly string Description;
public readonly IReadOnlyDictionary<string, string> Values;
public readonly string DefaultValue;
public readonly bool IsLocked;
public readonly bool IsVisible;
public readonly int DisplayOrder;
public LobbyOption(string id, string name, string description, bool visible, int displayorder,
IReadOnlyDictionary<string, string> values, string defaultValue, bool locked)
{
Id = id;
Name = name;
Description = description;
IsVisible = visible;
DisplayOrder = displayorder;
Values = values;
DefaultValue = defaultValue;
IsLocked = locked;
}
public virtual string ValueChangedMessage(string playerName, string newValue)
{
return playerName + " changed " + Name + " to " + Values[newValue] + ".";
}
}
public class LobbyBooleanOption : LobbyOption
{
static readonly Dictionary<string, string> BoolValues = new Dictionary<string, string>()
{
{ true.ToString(), "enabled" },
{ false.ToString(), "disabled" }
};
public LobbyBooleanOption(string id, string name, string description, bool visible, int displayorder, bool defaultValue, bool locked)
: base(id, name, description, visible, displayorder, new ReadOnlyDictionary<string, string>(BoolValues), defaultValue.ToString(), locked) { }
public override string ValueChangedMessage(string playerName, string newValue)
{
return playerName + " " + BoolValues[newValue] + " " + Name + ".";
}
}
[RequireExplicitImplementation]
public interface IUnlocksRenderPlayer { bool RenderPlayerUnlocked { get; } }
}