forked from sruon/ffxivlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cs
305 lines (241 loc) · 11.2 KB
/
Entity.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace ffxivlib
{
public class Entity : BaseObject<Entity.ENTITYINFO>
{
#region Constructor
public Entity(ENTITYINFO structure, IntPtr address)
: base(structure, address)
{
Initialize();
}
#endregion
#region Properties
public int GatheringType { get; set; }
public string Name { get; set; }
public int PCId { get; set; }
public int NPCId { get; set; }
public TYPE MobType { get; set; }
public CURRENTTARGET CurrentTarget { get; set; }
public byte Distance { get; set; }
public byte GatheringStatus { get; set; }
public float X { get; set; }
public float Z { get; set; }
public float Y { get; set; }
public float Heading { get; set; }
public short FateId { get; set; }
public byte GatheringInvisible { get; set; }
public int ModelID { get; set; }
public ENTITYSTATUS PlayerStatus { get; set; }
public bool IsGM { get; set; }
public byte Icon { get; set; }
public STATUS IsEngaged { get; set; }
public int TargetId { get; set; }
public byte GrandCompany { get; set; }
public byte GrandCompanyRank { get; set; }
public byte Title { get; set; }
public JOB Job { get; set; }
public byte Level { get; set; }
public int CurrentHP { get; set; }
public int MaxHP { get; set; }
public int CurrentMP { get; set; }
public int MaxMP { get; set; }
public short CurrentTP { get; set; }
public short CurrentGP { get; set; }
public short MaxGP { get; set; }
public short CurrentCP { get; set; }
public short MaxCP { get; set; }
public byte Race { get; set; }
public SEX Sex { get; set; }
public COMBATSTATUS CombatStatus { get; set; }
public Buff.BUFFINFO[] _Buffs { get; set; }
public bool IsCasting { get; set; }
public short CastingSpellId { get; set; }
public float CastingProgress { get; set; }
public float CastingTime { get; set; }
#endregion
#region Extra properties
public List<Buff> Buffs
{
get
{
var BuffList = new List<Buff>();
foreach (Buff.BUFFINFO buff in _Buffs)
{
BuffList.Add(new Buff(buff, IntPtr.Zero));
}
return BuffList;
}
}
public int CastingPercentage
{
get
{
if (IsCasting && CastingTime > 0)
return (int) ((CastingProgress/CastingTime)*100);
return 0;
}
}
public bool IsFate
{
get { return FateId != 0; }
}
#endregion
#region Unmanaged structure
[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct ENTITYINFO
{
[MarshalAs(UnmanagedType.I4)] [FieldOffset(0x0)] public int GatheringType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] [FieldOffset(0x30)] public string Name;
// Not exactly PC ID...
[MarshalAs(UnmanagedType.I4)] [FieldOffset(0x74)] public int PCId;
[MarshalAs(UnmanagedType.I4)] [FieldOffset(0x78)] public int NPCId;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x8A)] public TYPE MobType;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x8C)] public CURRENTTARGET CurrentTarget;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x8D)] public byte Distance;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x8E)] public byte GatheringStatus;
[MarshalAs(UnmanagedType.R4)] [FieldOffset(0xA0)] public float X;
[MarshalAs(UnmanagedType.R4)] [FieldOffset(0xA4)] public float Z;
[MarshalAs(UnmanagedType.R4)] [FieldOffset(0xA8)] public float Y;
[MarshalAs(UnmanagedType.R4)] [FieldOffset(0xB0)] public float Heading;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(0xE8)] public short FateId;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x11C)] public byte GatheringInvisible;
[MarshalAs(UnmanagedType.I4)] [FieldOffset(0x174)] public int ModelId;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x188)] public ENTITYSTATUS PlayerStatus;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x189)] public bool IsGM;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x194)] public ICON Icon;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x195)] public STATUS IsEngaged;
[MarshalAs(UnmanagedType.I4)] [FieldOffset(0xD78)] public int TargetId;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x169A)] public byte GrandCompany;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x169B)] public byte GrandCompanyRank;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x169E)] public byte Title;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x1698)] public JOB Job;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x1699)] public byte Level;
[MarshalAs(UnmanagedType.I4)] [FieldOffset(0x16A0)] public int CurrentHP;
[MarshalAs(UnmanagedType.I4)] [FieldOffset(0x16A4)] public int MaxHP;
[MarshalAs(UnmanagedType.I4)] [FieldOffset(0x16A8)] public int CurrentMP;
[MarshalAs(UnmanagedType.I4)] [FieldOffset(0x16AC)] public int MaxMP;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(0x16B0)] public short CurrentTP;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(0x16B2)] public short CurrentGP;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(0x16B4)] public short MaxGP;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(0x16B6)] public short CurrentCP;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(0x16B8)] public short MaxCP;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x2E58)] public byte Race;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x2E59)] public SEX Sex;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x2E72)] public COMBATSTATUS CombatStatus;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)] [FieldOffset(0x2FF8)] public Buff.BUFFINFO[] _Buffs;
[MarshalAs(UnmanagedType.I1)] [FieldOffset(0x3170)] public bool IsCasting;
[MarshalAs(UnmanagedType.I2)] [FieldOffset(0x3174)] public short CastingSpellId;
[MarshalAs(UnmanagedType.R4)] [FieldOffset(0x31A4)] public float CastingProgress;
[MarshalAs(UnmanagedType.R4)] [FieldOffset(0x31A8)] public float CastingTime;
};
#endregion
#region Public methods
/// <summary>
/// Returns the distance between current Entity and a given Entity
/// </summary>
/// <param name="other">Entity to compare to</param>
/// <returns>Distance</returns>
public float GetDistanceTo(Entity other)
{
float fDistX = Math.Abs(Structure.X - other.Structure.X);
float fDistY = Math.Abs(Structure.Y - other.Structure.Y);
float fDistZ = Math.Abs(Structure.Z - other.Structure.Z);
return (float) Math.Sqrt((fDistX*fDistX) + (fDistY*fDistY) + (fDistZ*fDistZ));
}
#endregion
}
public partial class FFXIVLIB
{
#region Public methods
/// <summary>
/// This function build an Entity object according to the position in the Entity array
/// You may effectively loop by yourself on this function.
/// </summary>
/// <param name="id">Position in the Entity Array, use Constants.ENTITY_ARRAY_SIZE as your max (exclusive)</param>
/// <returns>Entity object or null</returns>
/// <exception cref="System.IndexOutOfRangeException">Out of range</exception>
public Entity GetEntityById(int id)
{
if (id >= Constants.ENTITY_ARRAY_SIZE)
throw new IndexOutOfRangeException();
IntPtr pointer = IntPtr.Add(_mr.GetArrayStart(Constants.PCPTR), id*0x4);
IntPtr address = _mr.ResolvePointer(pointer);
if (address == IntPtr.Zero) return null;
var en = _mr.CreateStructFromAddress<Entity.ENTITYINFO>(address);
if (!Equals(en, default(Entity.ENTITYINFO)))
{
var e = new Entity(en, address);
return e;
}
return null;
}
/// <summary>
/// Deprecated, use getEntityById
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Entity GetEntityInfo(int id)
{
return GetEntityById(id);
}
/// <summary>
/// This function attempts to retrieve a list of Entity by its name in the Entity array
/// This is potentially a costly call as we build a complete list to look for the Entity.
/// This doesn't include Gathering nodes at the moment. To be fixed.
/// </summary>
/// <param name="name">Name of the Entity to be retrieved</param>
/// <returns>Enumerable list of Entity object or</returns>
public IEnumerable<Entity> GetEntityByName(string name)
{
IntPtr pointer = _mr.GetArrayStart(Constants.PCPTR);
var entityList = new List<Entity>();
for (int i = 0; i < Constants.ENTITY_ARRAY_SIZE; i++)
{
IntPtr address = pointer + (i*0x4);
var en = _mr.CreateStructFromPointer<Entity.ENTITYINFO>(address);
if (!Equals(en, default(Entity.ENTITYINFO)))
{
var e = new Entity(en, address);
entityList.Add(e);
}
}
IEnumerable<Entity> results = entityList.Where(obj => obj.Structure.Name == name);
return results;
}
/// <summary>
/// Retrieves a list of Entity corresponding to the given TYPE
/// Needs to be refactored.
/// </summary>
/// <param name="type">Type of entity</param>
/// <returns>Enumerable list of Entity objects</returns>
public IEnumerable<Entity> GetEntityByType(TYPE type)
{
List<int> pointerPath = Constants.PCPTR;
uint arraySize = Constants.ENTITY_ARRAY_SIZE;
if (type == TYPE.Gathering)
{
pointerPath = Constants.GATHERINGPTR;
arraySize = Constants.GATHERING_ARRAY_SIZE;
}
IntPtr pointer = _mr.GetArrayStart(pointerPath);
var entityList = new List<Entity>();
for (int i = 0; i < arraySize; i++)
{
IntPtr address = pointer + (i*0x4);
var en = _mr.CreateStructFromPointer<Entity.ENTITYINFO>(address);
if (!Equals(en, default(Entity.ENTITYINFO)))
{
var e = new Entity(en, address);
entityList.Add(e);
}
}
IEnumerable<Entity> results = entityList.Where(e => e.Structure.MobType == type);
return results;
}
#endregion
}
}