/
EntityList.cs
192 lines (160 loc) · 5.25 KB
/
EntityList.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
// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
using System;
using OpenTK;
using ClassicalSharp.GraphicsAPI;
using ClassicalSharp.Physics;
namespace ClassicalSharp.Entities {
public enum NameMode { None, Hovered, All, AllHovered, AllUnscaled }
public enum EntityShadow { None, SnapToBlock, Circle, CircleAll, }
public sealed class EntityList : IDisposable {
public const int MaxCount = 256;
public const byte SelfID = 255;
public Entity[] List = new Entity[MaxCount];
public Game game;
public EntityShadow ShadowMode = EntityShadow.None;
byte closestId;
/// <summary> Mode of how names of hovered entities are rendered (with or without depth testing),
/// and how other entity names are rendered. </summary>
public NameMode NamesMode = NameMode.Hovered;
public EntityList(Game game) {
this.game = game;
Events.ContextLost += ContextLost;
Events.ContextRecreated += ContextRecreated;
Events.ChatFontChanged += ChatFontChanged;
NamesMode = Options.GetEnum(OptionsKey.NamesMode, NameMode.Hovered);
if (game.ClassicMode) NamesMode = NameMode.Hovered;
ShadowMode = Options.GetEnum(OptionsKey.EntityShadow, EntityShadow.None);
if (game.ClassicMode) ShadowMode = EntityShadow.None;
}
public void Tick(ScheduledTask task) {
for (int i = 0; i < List.Length; i++) {
if (List[i] == null) continue;
List[i].Tick(task.Interval);
}
}
public void RenderModels(IGraphicsApi gfx, double delta, float t) {
gfx.Texturing = true;
gfx.AlphaTest = true;
for (int i = 0; i < List.Length; i++) {
if (List[i] == null) continue;
List[i].RenderModel(delta, t);
}
gfx.Texturing = false;
gfx.AlphaTest = false;
}
bool hadFog;
public void RenderNames(IGraphicsApi gfx, double delta) {
if (NamesMode == NameMode.None) return;
closestId = GetClosetPlayer(game.LocalPlayer);
if (!game.LocalPlayer.Hacks.CanSeeAllNames || NamesMode != NameMode.All) return;
gfx.Texturing = true;
gfx.AlphaTest = true;
hadFog = gfx.Fog;
if (hadFog) gfx.Fog = false;
for (int i = 0; i < List.Length; i++) {
if (List[i] == null) continue;
if (i != closestId || i == SelfID) {
List[i].RenderName();
}
}
gfx.Texturing = false;
gfx.AlphaTest = false;
if (hadFog) gfx.Fog = true;
}
public void RenderHoveredNames(IGraphicsApi gfx, double delta) {
if (NamesMode == NameMode.None) return;
gfx.Texturing = true;
gfx.AlphaTest = true;
gfx.DepthTest = false;
hadFog = gfx.Fog;
if (hadFog) gfx.Fog = false;
bool allNames = !(NamesMode == NameMode.Hovered || NamesMode == NameMode.All)
&& game.LocalPlayer.Hacks.CanSeeAllNames;
for (int i = 0; i < List.Length; i++) {
bool hover = (i == closestId || allNames) && i != SelfID;
if (List[i] != null && hover) {
List[i].RenderName();
}
}
gfx.Texturing = false;
gfx.AlphaTest = false;
gfx.DepthTest = true;
if (hadFog) gfx.Fog = true;
}
void ContextLost() {
for (int i = 0; i < List.Length; i++) {
if (List[i] == null) continue;
List[i].ContextLost();
}
game.Graphics.DeleteTexture(ref ShadowComponent.shadowTex);
}
void ContextRecreated() {
for (int i = 0; i < List.Length; i++) {
if (List[i] == null) continue;
List[i].ContextRecreated();
}
}
void ChatFontChanged() {
for (int i = 0; i < List.Length; i++) {
if (List[i] == null) continue;
Player p = List[i] as Player;
if (p != null) p.UpdateName();
}
}
public void RemoveEntity(byte id) {
Events.RaiseEntityRemoved(id);
List[id].Despawn();
List[id] = null;
}
public void Dispose() {
for (int i = 0; i < List.Length; i++) {
if (List[i] == null) continue;
RemoveEntity((byte)i);
}
Events.ContextLost -= ContextLost;
Events.ContextRecreated -= ContextRecreated;
Events.ChatFontChanged -= ChatFontChanged;
if (ShadowComponent.shadowTex != 0) {
game.Graphics.DeleteTexture(ref ShadowComponent.shadowTex);
}
}
public byte GetClosetPlayer(Player src) {
Vector3 eyePos = src.EyePosition;
Vector3 dir = Utils.GetDirVector(src.HeadYRadians, src.HeadXRadians);
float closestDist = float.PositiveInfinity;
byte targetId = SelfID;
for (int i = 0; i < SelfID; i++) { // because we don't want to pick against local player
Entity p = List[i];
if (p == null) continue;
float t0, t1;
if (Intersection.RayIntersectsRotatedBox(eyePos, dir, p, out t0, out t1) && t0 < closestDist) {
closestDist = t0;
targetId = (byte)i;
}
}
return targetId;
}
public void DrawShadows() {
if (ShadowMode == EntityShadow.None) return;
ShadowComponent.boundShadowTex = false;
IGraphicsApi gfx = game.Graphics;
gfx.AlphaArgBlend = true;
gfx.DepthWrite = false;
gfx.AlphaBlending = true;
gfx.Texturing = true;
gfx.SetBatchFormat(VertexFormat.P3fT2fC4b);
ShadowComponent.Draw(game, List[SelfID]);
if (ShadowMode == EntityShadow.CircleAll) {
for (int i = 0; i < SelfID; i++) {
if (List[i] == null) continue;
Player p = List[i] as Player;
if (p != null) ShadowComponent.Draw(game, p);
}
}
gfx.AlphaArgBlend = false;
gfx.DepthWrite = true;
gfx.AlphaBlending = false;
gfx.Texturing = false;
}
}
}