forked from GabeHasWon/VerdantMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActiveEntities.cs
142 lines (116 loc) · 3.63 KB
/
ActiveEntities.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Terraria;
namespace Verdant;
/// <summary>
/// System taken from implementation of the same name in Overhaul, used with permission.<br/>
/// https://github.com/Mirsario/TerrariaOverhaul/blob/1.4/Utilities/ActiveEntities.cs?ts=4
/// </summary>
public static class ActiveEntities
{
public struct EntityEnumerator<T> : IEnumerable<T>, IEnumerator<T>
{
private readonly T[] array;
private readonly int arrayLength;
private int index;
public T Current { get; private set; }
object IEnumerator.Current => Current!;
[MethodImpl(OptimizationFlags)]
public EntityEnumerator(T[] entities, int length)
{
array = entities;
arrayLength = length;
index = -1;
Current = default!;
}
[MethodImpl(OptimizationFlags)]
public bool MoveNext()
{
while (true)
{
index++;
if (index >= arrayLength)
{
Current = default!;
return false;
}
var entity = array[index];
if (IsActive(entity))
{
Current = entity;
return true;
}
}
}
public void Reset()
{
index = -1;
Current = default!;
}
[MethodImpl(OptimizationFlags)]
public EntityEnumerator<T> GetEnumerator() => this;
IEnumerator IEnumerable.GetEnumerator() => this;
IEnumerator<T> IEnumerable<T>.GetEnumerator() => this;
void IDisposable.Dispose() { }
}
private struct EntityArrayWrapper<T>
{
private readonly T[] array;
private readonly T[] sourceArray;
private int length;
private int maxLength;
private uint lastArrayUpdateTick;
private EntityEnumerator<T> enumerator;
public EntityArrayWrapper(T[] sourceArray, int maxLength)
{
length = 0;
this.maxLength = maxLength;
array = new T[maxLength];
this.sourceArray = sourceArray;
enumerator = default;
lastArrayUpdateTick = uint.MaxValue;
}
public EntityEnumerator<T> GetEnumerator()
{
if (Main.GameUpdateCount != lastArrayUpdateTick)
{
length = 0;
lastArrayUpdateTick = Main.GameUpdateCount;
for (int i = 0; i < maxLength; i++)
{
var entry = sourceArray[i];
if (entry != null && IsActive(entry))
{
array[length++] = entry;
}
}
enumerator = new EntityEnumerator<T>(array, length);
}
return enumerator;
}
}
private const MethodImplOptions OptimizationFlags = MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization;
private static EntityArrayWrapper<NPC> npcs = new(Main.npc, Main.maxNPCs);
private static EntityArrayWrapper<Gore> gores = new(Main.gore, Main.maxGore);
private static EntityArrayWrapper<Dust> dusts = new(Main.dust, Main.maxDust);
private static EntityArrayWrapper<Item> items = new(Main.item, Main.maxItems);
private static EntityArrayWrapper<Player> players = new(Main.player, Main.maxPlayers);
private static EntityArrayWrapper<Projectile> projectiles = new(Main.projectile, Main.maxProjectiles);
public static EntityEnumerator<NPC> NPCs => npcs.GetEnumerator();
public static EntityEnumerator<Gore> Gores => gores.GetEnumerator();
public static EntityEnumerator<Dust> Dusts => dusts.GetEnumerator();
public static EntityEnumerator<Item> Items => items.GetEnumerator();
public static EntityEnumerator<Player> Players => players.GetEnumerator();
public static EntityEnumerator<Projectile> Projectiles => projectiles.GetEnumerator();
// This should be inlined as just the return value.
[MethodImpl(OptimizationFlags)]
private static bool IsActive<T>(T t) => t switch
{
Entity entity => entity.active,
Gore gore => gore.active,
Dust dust => dust.active,
_ => throw new NotImplementedException()
};
}