debug_battleDat.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8. namespace FF8
  9. {
  10. public class Debug_battleDat
  11. {
  12. int id;
  13. readonly EntityType entityType;
  14. byte[] buffer;
  15. /// <summary>
  16. /// V is the scale dividor. For now in Battle and World modules I'm dividing native f16 to 2048f
  17. /// </summary>
  18. public const float V = 2048.0f;
  19. public struct DatFile
  20. {
  21. public uint cSections;
  22. public uint[] pSections;
  23. public uint eof;
  24. }
  25. public DatFile datFile;
  26. #region section 1 Skeleton
  27. [StructLayout(LayoutKind.Sequential,Pack = 1,Size =16)]
  28. public struct Skeleton
  29. {
  30. public ushort cBones;
  31. public ushort scale;
  32. public ushort unk2;
  33. public ushort unk3;
  34. public ushort unk4;
  35. public ushort unk5;
  36. public ushort unk6;
  37. public ushort unk7;
  38. public Bone[] bones;
  39. public Vector3 GetScale => new Vector3(scale/V*12, scale/V*12, scale/V*12);
  40. }
  41. [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 48)]
  42. public struct Bone
  43. {
  44. public ushort parentId;
  45. public short boneSize;
  46. private short unk1; //360
  47. private short unk2; //360
  48. private short unk3; //360
  49. private short unk4;
  50. private short unk5;
  51. private short unk6;
  52. [MarshalAs(UnmanagedType.ByValArray,SizeConst = 28 )]
  53. public byte[] Unk;
  54. public float Size { get => boneSize / V; }
  55. public float Unk1 { get => unk1 / 4096.0f * 360.0f; }
  56. public float Unk2 { get => unk2 / 4096.0f * 360.0f; }
  57. public float Unk3 { get => unk3 / 4096.0f * 360.0f; }
  58. public float Unk4 { get => unk4 / 4096.0f; }
  59. public float Unk5 { get => unk5 / 4096.0f; }
  60. public float Unk6 { get => unk6 / 4096.0f; }
  61. }
  62. /// <summary>
  63. /// Skeleton data section
  64. /// </summary>
  65. /// <param name="v"></param>
  66. /// <param name="ms"></param>
  67. /// <param name="br"></param>
  68. private void ReadSection1(uint v, MemoryStream ms, BinaryReader br)
  69. {
  70. ms.Seek(v, SeekOrigin.Begin);
  71. #if _WINDOWS //looks like Linux Mono doesn't like marshalling structure with LPArray to Bone[]
  72. skeleton = MakiExtended.ByteArrayToStructure<Skeleton>(br.ReadBytes(16));
  73. #else
  74. skeleton = new Skeleton()
  75. {
  76. cBones = br.ReadUInt16(),
  77. unk = br.ReadUInt16(),
  78. unk2 = br.ReadUInt16(),
  79. unk3 = br.ReadUInt16(),
  80. ScaleX = br.ReadUInt16(),
  81. ScaleY = br.ReadUInt16(),
  82. ScaleZ = br.ReadUInt16(),
  83. unk4 = br.ReadUInt16()
  84. };
  85. #endif
  86. skeleton.bones = new Bone[skeleton.cBones];
  87. for (int i = 0; i < skeleton.cBones; i++)
  88. skeleton.bones[i] = MakiExtended.ByteArrayToStructure<Bone>(br.ReadBytes(48));
  89. return;
  90. }
  91. public Skeleton skeleton;
  92. #endregion
  93. #region section 2 Geometry
  94. public struct Geometry
  95. {
  96. public uint cObjects;
  97. public uint[] pObjects;
  98. public Object[] objects;
  99. public uint cTotalVert;
  100. }
  101. public struct Object
  102. {
  103. public ushort cVertices;
  104. public VerticeData[] verticeData;
  105. public ushort cTriangles;
  106. public ushort cQuads;
  107. public ulong padding;
  108. public Triangle[] triangles;
  109. public Quad[] quads;
  110. }
  111. public struct VerticeData
  112. {
  113. public ushort boneId;
  114. public ushort cVertices;
  115. public Vertex[] vertices;
  116. }
  117. [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 8)]
  118. public struct Vertex
  119. {
  120. public short x;
  121. public short y;
  122. public short z;
  123. public Vector3 GetVector => new Vector3(-x/ V, -z/V, -y/V);
  124. }
  125. [StructLayout(LayoutKind.Sequential, Pack =1, Size =16)]
  126. public struct Triangle
  127. {
  128. private ushort A;
  129. private ushort B;
  130. private ushort C;
  131. public UV vta;
  132. public UV vtb;
  133. public ushort texUnk;
  134. public UV vtc;
  135. public ushort u;
  136. public ushort A1 { get => (ushort)(A & 0xFFF); set => A = value; }
  137. public ushort B1 { get => (ushort)(B & 0xFFF); set => B = value; }
  138. public ushort C1 { get => (ushort)(C & 0xFFF); set => C = value; }
  139. public byte textureIndex { get => (byte)((texUnk >> 6) & 0b111); }
  140. }
  141. [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 20)]
  142. public struct Quad
  143. {
  144. private ushort A;
  145. private ushort B;
  146. private ushort C;
  147. private ushort D;
  148. public UV vta;
  149. public ushort texUnk;
  150. public UV vtb;
  151. public ushort u;
  152. public UV vtc;
  153. public UV vtd;
  154. public ushort A1 { get => (ushort)(A & 0xFFF); set => A = value; }
  155. public ushort B1 { get => (ushort)(B & 0xFFF); set => B = value; }
  156. public ushort C1 { get => (ushort)(C & 0xFFF); set => C = value; }
  157. public ushort D1 { get => (ushort)(D & 0xFFF); set => D = value; }
  158. public byte textureIndex { get => (byte)((texUnk >> 6) & 0b111); }
  159. }
  160. [StructLayout(LayoutKind.Sequential, Pack =1,Size =2)]
  161. public struct UV
  162. {
  163. public byte U;
  164. public byte V;
  165. public float U1(float h=128f) { return U / h; }
  166. public float V1(float w=128f) { return V > 128 ? //if bigger than 128, then multitexture index to odd
  167. (V - 128f)/w
  168. : w==32 ? //if equals 32, then it's weapon texture and should be in range of 96-128
  169. (V-96)/w
  170. : V/w; } //if none of these cases, then divide by resolution;
  171. public override string ToString()
  172. {
  173. return $"{U};{U1()};{V};{V1()}";
  174. }
  175. }
  176. public Geometry geometry;
  177. /// <summary>
  178. /// Geometry section
  179. /// </summary>
  180. /// <param name="v"></param>
  181. /// <param name="ms"></param>
  182. /// <param name="br"></param>
  183. private void ReadSection2(uint v, MemoryStream ms, BinaryReader br)
  184. {
  185. ms.Seek(v, SeekOrigin.Begin);
  186. geometry = new Geometry { cObjects = br.ReadUInt32() };
  187. geometry.pObjects = new uint[geometry.cObjects];
  188. for (int i = 0; i < geometry.cObjects; i++)
  189. geometry.pObjects[i] = br.ReadUInt32();
  190. geometry.objects = new Object[geometry.cObjects];
  191. for (int i = 0; i < geometry.cObjects; i++)
  192. geometry.objects[i] = ReadGeometryObject(v+geometry.pObjects[i], ms, br);
  193. geometry.cTotalVert = br.ReadUInt32();
  194. }
  195. private Object ReadGeometryObject(uint v, MemoryStream ms, BinaryReader br)
  196. {
  197. ms.Seek(v, SeekOrigin.Begin);
  198. Object @object = new Object { cVertices = br.ReadUInt16() };
  199. @object.verticeData = new VerticeData[@object.cVertices];
  200. if (ms.Position + @object.cVertices * 6 >= ms.Length)
  201. return @object;
  202. for (int n = 0; n < @object.cVertices; n++){
  203. @object.verticeData[n].boneId = br.ReadUInt16();
  204. @object.verticeData[n].cVertices = br.ReadUInt16();
  205. @object.verticeData[n].vertices = new Vertex[@object.verticeData[n].cVertices];
  206. for (int i = 0; i < @object.verticeData[n].cVertices; i++)
  207. @object.verticeData[n].vertices[i] = MakiExtended.ByteArrayToStructure<Vertex>(br.ReadBytes(6));}
  208. ms.Seek(4-(ms.Position%4 == 0 ? 4 : ms.Position%4), SeekOrigin.Current);
  209. @object.cTriangles = br.ReadUInt16();
  210. @object.cQuads = br.ReadUInt16();
  211. @object.padding = br.ReadUInt64();
  212. @object.triangles = new Triangle[@object.cTriangles];
  213. if (@object.cTriangles == 0 && @object.cQuads == 0)
  214. return @object;
  215. @object.quads = new Quad[@object.cQuads];
  216. for (int i = 0; i < @object.cTriangles; i++)
  217. @object.triangles[i] = MakiExtended.ByteArrayToStructure<Triangle>(br.ReadBytes(16));
  218. for (int i = 0; i < @object.cQuads; i++)
  219. @object.quads[i] = MakiExtended.ByteArrayToStructure<Quad>(br.ReadBytes(20));
  220. return @object;
  221. }
  222. /// <summary>
  223. /// This method returns geometry data AFTER animation matrix translations, local position/rotation translations. This is the final step of calculation.
  224. /// This data should be used only by Renderer. Any translations/vertices manipulation should happen inside this method or earlier
  225. /// </summary>
  226. /// <param name="objectId">Monsters can have more than one object. Treat it like multi-model geometry. They are all needed to build whole model</param>
  227. /// <param name="position">a Vector3 to set global position</param>
  228. /// <param name="rotation">a Quaternion to set the correct rotation. 1=90, 2=180 ... </param>
  229. /// <param name="animationId">an animation pointer. Animation 0 is always idle</param>
  230. /// <param name="animationFrame">an animation frame from animation id. You should pass incrementing frame and reset to 0 when frameCount max is hit</param>
  231. /// <param name="step">FEATURE: This float (0.0 - 1.0) is used in Linear interpolation in animation frames blending. 0.0 means frameN, 1.0 means FrameN+1. Usually this should be a result of deltaTime to see if computer is capable of rendering smooth animations rather than constant 15 FPS</param>
  232. /// <returns></returns>
  233. public Tuple<VertexPositionTexture[],byte[]> GetVertexPositions(int objectId, Vector3 position,Quaternion rotation, int animationId, int animationFrame, float step)
  234. {
  235. Object obj = geometry.objects[objectId];
  236. if (animationFrame >= animHeader.animations[animationId].animationFrames.Length || animationFrame<0)
  237. animationFrame = 0;
  238. AnimationFrame frame = animHeader.animations[animationId].animationFrames[animationFrame];
  239. AnimationFrame nextFrame;
  240. if (animationFrame+1 >= animHeader.animations[animationId].animationFrames.Length)
  241. nextFrame = animHeader.animations[animationId].animationFrames[0];
  242. else nextFrame = animHeader.animations[animationId].animationFrames[animationFrame+1];
  243. List<VertexPositionTexture> vpt = new List<VertexPositionTexture>();
  244. List<Tuple<Vector3, int>> verts = new List<Tuple<Vector3, int>>();
  245. int i = 0;
  246. foreach (var a in obj.verticeData)
  247. foreach (var b in a.vertices)
  248. verts.Add(CalculateFrame(new Tuple<Vector3, int>(b.GetVector, a.boneId),frame,nextFrame, step));
  249. byte[] texturePointers = new byte[obj.cTriangles + obj.cQuads*2];
  250. Vector3 snapToGround = Vector3.Zero;
  251. FixScaleYOffset(out snapToGround);
  252. Vector3 translationPosition = position + Vector3.SmoothStep(frame.Position, nextFrame.Position, step) + snapToGround;
  253. for (;i<obj.cTriangles; i++ )
  254. {
  255. ///=/=/=/=/==/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=
  256. ///
  257. ////////////////////=============VERTEX C========\\\\\\\\\\\\\\\\\\\\\
  258. Tuple<Vector3, int> VerticeC = verts[ obj.triangles[i].C1];
  259. Vector3 VerticeDataC = VerticeC.Item1;
  260. VerticeDataC = Vector3.Transform(VerticeDataC, Matrix.CreateFromQuaternion(rotation));
  261. VerticeDataC = Vector3.Transform(VerticeDataC, Matrix.CreateTranslation(translationPosition));
  262. ////////////////////=============VERTEX A========\\\\\\\\\\\\\\\\\\\\\
  263. Tuple<Vector3, int> VerticeA = verts[obj.triangles[i].A1];
  264. Vector3 VerticeDataA = VerticeA.Item1;
  265. VerticeDataA = Vector3.Transform(VerticeDataA, Matrix.CreateFromQuaternion(rotation));
  266. VerticeDataA = Vector3.Transform(VerticeDataA, Matrix.CreateTranslation(translationPosition));
  267. ////////////////////=============VERTEX B========\\\\\\\\\\\\\\\\\\\\\
  268. Tuple<Vector3, int> VerticeB = verts[obj.triangles[i].B1];
  269. Vector3 VerticeDataB = VerticeB.Item1;
  270. VerticeDataB = Vector3.Transform(VerticeDataB, Matrix.CreateFromQuaternion(rotation));
  271. VerticeDataB = Vector3.Transform(VerticeDataB, Matrix.CreateTranslation(translationPosition));
  272. ///
  273. ///=/=/=/=/==/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=/=
  274. var prevarTexT = textures.textures[obj.triangles[i].textureIndex];
  275. vpt.Add(new VertexPositionTexture(VerticeDataC, new Vector2(obj.triangles[i].vta.U1(prevarTexT.Width), obj.triangles[i].vta.V1(prevarTexT.Height))));
  276. vpt.Add(new VertexPositionTexture(VerticeDataA, new Vector2(obj.triangles[i].vtb.U1(prevarTexT.Width), obj.triangles[i].vtb.V1(prevarTexT.Height))));
  277. vpt.Add(new VertexPositionTexture(VerticeDataB, new Vector2(obj.triangles[i].vtc.U1(prevarTexT.Width), obj.triangles[i].vtc.V1(prevarTexT.Height))));
  278. texturePointers[i] = obj.triangles[i].textureIndex;
  279. }
  280. for (i = 0; i < obj.cQuads; i++)
  281. {
  282. ////////////////////=============VERTEX A========\\\\\\\\\\\\\\\\\\\\\
  283. Tuple<Vector3, int> VerticeA = verts[obj.quads[i].A1];
  284. Vector3 VerticeDataA = VerticeA.Item1;
  285. VerticeDataA = Vector3.Transform(VerticeDataA, Matrix.CreateFromQuaternion(rotation));
  286. VerticeDataA = Vector3.Transform(VerticeDataA, Matrix.CreateTranslation(translationPosition));
  287. ////////////////////=============VERTEX B========\\\\\\\\\\\\\\\\\\\\\
  288. Tuple<Vector3, int> VerticeB = verts[obj.quads[i].B1];
  289. Vector3 VerticeDataB = VerticeB.Item1;
  290. VerticeDataB = Vector3.Transform(VerticeDataB, Matrix.CreateFromQuaternion(rotation));
  291. VerticeDataB = Vector3.Transform(VerticeDataB, Matrix.CreateTranslation(translationPosition));
  292. ////////////////////=============VERTEX C========\\\\\\\\\\\\\\\\\\\\\
  293. Tuple<Vector3, int> VerticeC = verts[obj.quads[i].C1];
  294. Vector3 VerticeDataC = VerticeC.Item1;
  295. VerticeDataC = Vector3.Transform(VerticeDataC, Matrix.CreateFromQuaternion(rotation));
  296. VerticeDataC = Vector3.Transform(VerticeDataC, Matrix.CreateTranslation(translationPosition));
  297. ////////////////////=============VERTEX D========\\\\\\\\\\\\\\\\\\\\\
  298. Tuple<Vector3, int> VerticeD = verts[obj.quads[i].D1];
  299. Vector3 VerticeDataD = VerticeD.Item1;
  300. VerticeDataD = Vector3.Transform(VerticeDataD, Matrix.CreateFromQuaternion(rotation));
  301. VerticeDataD = Vector3.Transform(VerticeDataD, Matrix.CreateTranslation(translationPosition));
  302. ///
  303. var preVarTex = textures.textures[obj.quads[i].textureIndex];
  304. vpt.Add(new VertexPositionTexture(VerticeDataA, new Vector2(obj.quads[i].vta.U1(preVarTex.Width), obj.quads[i].vta.V1(preVarTex.Height))));
  305. vpt.Add(new VertexPositionTexture(VerticeDataB, new Vector2(obj.quads[i].vtb.U1(preVarTex.Width), obj.quads[i].vtb.V1(preVarTex.Height))));
  306. vpt.Add(new VertexPositionTexture(VerticeDataD, new Vector2(obj.quads[i].vtd.U1(preVarTex.Width), obj.quads[i].vtd.V1(preVarTex.Height))));
  307. vpt.Add(new VertexPositionTexture(VerticeDataA, new Vector2(obj.quads[i].vta.U1(preVarTex.Width), obj.quads[i].vta.V1(preVarTex.Height))));
  308. vpt.Add(new VertexPositionTexture(VerticeDataC, new Vector2(obj.quads[i].vtc.U1(preVarTex.Width), obj.quads[i].vtc.V1(preVarTex.Height))));
  309. vpt.Add(new VertexPositionTexture(VerticeDataD, new Vector2(obj.quads[i].vtd.U1(preVarTex.Width), obj.quads[i].vtd.V1(preVarTex.Height))));
  310. texturePointers[obj.cTriangles+i*2] = obj.quads[i].textureIndex;
  311. texturePointers[obj.cTriangles + i * 2+1] = obj.quads[i].textureIndex;
  312. }
  313. return new Tuple<VertexPositionTexture[], byte[]>(vpt.ToArray(), texturePointers);
  314. }
  315. /// <summary>
  316. /// Lazy class to fix Y axis of entities because due to scalling some appeared above or below the stage.
  317. /// Those entities that have so far unknown Y axis are vurnable to DEBUGframe variable
  318. /// </summary>
  319. /// <param name="snapToGround"></param>
  320. private void FixScaleYOffset(out Vector3 snapToGround)
  321. {
  322. //snapToGround = Vector3.Zero;
  323. //return;
  324. float fScaleResolver = skeleton.GetScale.Y;
  325. float y = 0f;
  326. switch(entityType)
  327. {
  328. case EntityType.Monster:
  329. switch (GetId)
  330. {
  331. case 68:
  332. y = -300f;
  333. break;
  334. case 21:
  335. case 18:
  336. y = -125;
  337. break;
  338. case 125:
  339. y = -120;
  340. break;
  341. case 25:
  342. y = -100;
  343. break;
  344. case 113:
  345. y = -90;
  346. break;
  347. case 54:
  348. y = -57;
  349. break;
  350. case 119:
  351. case 136:
  352. y = -50;
  353. break;
  354. case 88:
  355. case 100:
  356. y = -30;
  357. break;
  358. case 55:
  359. y = -28;
  360. break;
  361. case 82:
  362. case 142:
  363. y = -27;
  364. break;
  365. case 85:
  366. case 86:
  367. case 87:
  368. case 128:
  369. case 71:
  370. case 73:
  371. case 76:
  372. case 108:
  373. case 15:
  374. case 134:
  375. case 143:
  376. case 17:
  377. y = -25;
  378. break;
  379. case 75:
  380. y = -23;
  381. break;
  382. case 28:
  383. case 29:
  384. case 78:
  385. case 79:
  386. case 120:
  387. case 137:
  388. case 14:
  389. y = -20;
  390. break;
  391. case 60:
  392. y = -19;
  393. break;
  394. case 31:
  395. y = -18;
  396. break;
  397. case 64:
  398. case 72:
  399. case 74:
  400. case 135:
  401. y = -17;
  402. break;
  403. case 4:
  404. case 46:
  405. case 56:
  406. case 59:
  407. case 13:
  408. y = -14;
  409. break;
  410. case 39:
  411. case 40:
  412. y = -12;
  413. break;
  414. case 42:
  415. y = -11;
  416. break;
  417. case 69:
  418. y = -10;
  419. break;
  420. case 57:
  421. y = -6;
  422. break;
  423. case 129:
  424. y = -4;
  425. break;
  426. case 62:
  427. case 23:
  428. case 80:
  429. case 81:
  430. y = -3;
  431. break;
  432. case 96:
  433. y = -2;
  434. break;
  435. case 33:
  436. case 95:
  437. case 37:
  438. case 99:
  439. case 48:
  440. case 124:
  441. break; //f default 0, so no need to explicitely write it again
  442. case 53:
  443. y = 2;
  444. break;
  445. case 19:
  446. y = 3;
  447. break;
  448. case 32:
  449. case 93:
  450. y = 4;
  451. break;
  452. case 52:
  453. y = 5;
  454. break;
  455. case 121:
  456. case 22:
  457. y = 6;
  458. break;
  459. case 9:
  460. case 27:
  461. case 65:
  462. case 35:
  463. case 36:
  464. case 50:
  465. case 12:
  466. case 11:
  467. y = 10;
  468. break;
  469. case 7:
  470. case 16:
  471. y = 15;
  472. break;
  473. case 45:
  474. y = 18;
  475. break;
  476. case 6:
  477. case 43:
  478. case 3:
  479. case 8:
  480. y = 20;
  481. break;
  482. case 1:
  483. y = 24;
  484. break;
  485. case 63:
  486. y = 25;
  487. break;
  488. case 41:
  489. case 24:
  490. y = 30;
  491. break;
  492. case 115:
  493. y = 32;
  494. break;
  495. case 20:
  496. case 10:
  497. case 133:
  498. y = 40;
  499. break;
  500. case 38:
  501. case 49:
  502. y = 50;
  503. break;
  504. case 61:
  505. case 114:
  506. y = 60;
  507. break;
  508. case 89:
  509. case 97:
  510. case 98:
  511. case 111:
  512. y = 62;
  513. break;
  514. case 90:
  515. y = 90;
  516. break;
  517. case 51:
  518. y = 72;
  519. break;
  520. case 66:
  521. y = 80;
  522. break;
  523. case 112:
  524. y = 85;
  525. break;
  526. case 130:
  527. y = 90;
  528. break;
  529. case 5:
  530. case 26:
  531. case 77:
  532. case 30:
  533. case 92:
  534. y = 100;
  535. break;
  536. case 67:
  537. case 132:
  538. y = 150;
  539. break;
  540. case 131:
  541. y = 160;
  542. break;
  543. case 122:
  544. case 84:
  545. y = 180;
  546. break;
  547. case 109:
  548. y = 240;
  549. break;
  550. case 118:
  551. case 140:
  552. case 138:
  553. case 141:
  554. y = 250;
  555. break;
  556. default:
  557. y = Module_battle_debug.DEBUGframe;
  558. break;
  559. }
  560. break;
  561. case EntityType.Character:
  562. case EntityType.Weapon:
  563. switch(GetId)
  564. {
  565. case 0:
  566. y = -30;
  567. break;
  568. case 1:
  569. case 2:
  570. case 8:
  571. y = -32;
  572. break;
  573. case 3:
  574. y = -64;
  575. break;
  576. case 4:
  577. y = -66;
  578. break;
  579. case 5:
  580. y = -60;
  581. break;
  582. case 6:
  583. y = -26;
  584. break;
  585. case 7:
  586. y = -24;
  587. break;
  588. case 9:
  589. y = -36;
  590. break;
  591. case 10:
  592. y = 0;
  593. break;
  594. default:
  595. y = Module_battle_debug.DEBUGframe;
  596. break;
  597. }
  598. break;
  599. }
  600. y = fScaleResolver+ y/10f;
  601. snapToGround = new Vector3(0, y, 0);
  602. }
  603. /// <summary>
  604. /// Complex function that provides linear interpolation between two matrices of actual to-render animation frame and next frame data for blending
  605. /// </summary>
  606. /// <param name="tuple">the tuple that contains bone identificator and vertex</param>
  607. /// <param name="frame">current animation frame to render</param>
  608. /// <param name="nextFrame">animation frame to render that is AFTER the actual one. If last frame, then usually 0 is the 'next' frame</param>
  609. /// <param name="step">step is variable used to determinate the progress for linear interpolation. I.e. 0 for current frame and 1 for next frame; 0.5 for blend of two frames</param>
  610. /// <returns></returns>
  611. private Tuple<Vector3, int> CalculateFrame(Tuple<Vector3, int> tuple, AnimationFrame frame,AnimationFrame nextFrame, float step)
  612. {
  613. Matrix matrix = frame.boneRot.Item3[tuple.Item2]; //get's bone matrix
  614. Vector3 rootFramePos = new Vector3(
  615. matrix.M11 * tuple.Item1.X + matrix.M41 + matrix.M12 * tuple.Item1.Z + matrix.M13 * -tuple.Item1.Y,
  616. matrix.M21 * tuple.Item1.X + matrix.M42 + matrix.M22 * tuple.Item1.Z + matrix.M23 * -tuple.Item1.Y,
  617. matrix.M31 * tuple.Item1.X + matrix.M43 + matrix.M32 * tuple.Item1.Z + matrix.M33 * -tuple.Item1.Y);
  618. matrix = nextFrame.boneRot.Item3[tuple.Item2];
  619. Vector3 nextFramePos = new Vector3(
  620. matrix.M11 * tuple.Item1.X + matrix.M41 + matrix.M12 * tuple.Item1.Z + matrix.M13 * -tuple.Item1.Y,
  621. matrix.M21 * tuple.Item1.X + matrix.M42 + matrix.M22 * tuple.Item1.Z + matrix.M23 * -tuple.Item1.Y,
  622. matrix.M31 * tuple.Item1.X + matrix.M43 + matrix.M32 * tuple.Item1.Z + matrix.M33 * -tuple.Item1.Y);
  623. rootFramePos = Vector3.Transform(rootFramePos, Matrix.CreateScale(skeleton.GetScale));
  624. nextFramePos = Vector3.Transform(nextFramePos, Matrix.CreateScale(skeleton.GetScale));
  625. rootFramePos = Vector3.SmoothStep(rootFramePos, nextFramePos, step);
  626. return new Tuple<Vector3, int>(rootFramePos, tuple.Item2);
  627. }
  628. #endregion
  629. #region section 3 Animation
  630. public struct AnimationData
  631. {
  632. public uint cAnimations;
  633. public uint[] pAnimations;
  634. public Animation[] animations;
  635. }
  636. public struct Animation
  637. {
  638. public byte cFrames;
  639. public AnimationFrame[] animationFrames;
  640. }
  641. public struct AnimationFrame
  642. {
  643. private Vector3 position;
  644. public Tuple<Vector3[], ShortVector[],Matrix[]> boneRot;
  645. public Vector3 Position { get => position; set => position = value; }
  646. }
  647. public struct ShortVector
  648. {
  649. public short x;
  650. public short y;
  651. public short z;
  652. }
  653. /// <summary>
  654. /// Animation section
  655. /// </summary>
  656. /// <param name="v"></param>
  657. /// <param name="ms"></param>
  658. /// <param name="br"></param>
  659. private void ReadSection3(uint v, MemoryStream ms, BinaryReader br)
  660. {
  661. ms.Seek(v, SeekOrigin.Begin);
  662. animHeader = new AnimationData() {cAnimations = br.ReadUInt32() };
  663. animHeader.pAnimations = new uint[animHeader.cAnimations];
  664. for (int i = 0; i < animHeader.cAnimations; i++)
  665. animHeader.pAnimations[i] = br.ReadUInt32();
  666. animHeader.animations = new Animation[animHeader.cAnimations];
  667. for (int i = 0; i < animHeader.cAnimations; i++) //animation
  668. {
  669. ms.Seek(v + animHeader.pAnimations[i], SeekOrigin.Begin);
  670. animHeader.animations[i] = new Animation() {cFrames = br.ReadByte() };
  671. animHeader.animations[i].animationFrames = new AnimationFrame[animHeader.animations[i].cFrames];
  672. ExtapathyExtended.BitReader bitReader = new ExtapathyExtended.BitReader(ms);
  673. for(int n = 0; n<animHeader.animations[i].cFrames; n++) //frames
  674. {
  675. float x = bitReader.ReadPositionType()*.01f;
  676. float y = bitReader.ReadPositionType() * .01f * -1f;
  677. float z = bitReader.ReadPositionType() * .01f;
  678. if (n == 0)
  679. animHeader.animations[i].animationFrames[n] = new AnimationFrame()
  680. {Position = new Vector3(x,y,z)};
  681. else
  682. animHeader.animations[i].animationFrames[n] = new AnimationFrame()
  683. {Position = new Vector3(
  684. animHeader.animations[i].animationFrames[n - 1].Position.X + x,
  685. animHeader.animations[i].animationFrames[n - 1].Position.Y + y,
  686. animHeader.animations[i].animationFrames[n - 1].Position.Z + z)};
  687. bitReader.ReadBits(1); //padding byte;
  688. animHeader.animations[i].animationFrames[n].boneRot = new Tuple<Vector3[], ShortVector[], Matrix[]>(new Vector3[skeleton.cBones], new ShortVector[skeleton.cBones], new Matrix[skeleton.cBones]);
  689. for (int k = 0; k < skeleton.cBones; k++) //bones iterator
  690. {
  691. if (n != 0)
  692. {
  693. animHeader.animations[i].animationFrames[n].boneRot.Item2[k] = new ShortVector() { x = bitReader.ReadRotationType(), y = bitReader.ReadRotationType(), z = bitReader.ReadRotationType() };
  694. var grabber = animHeader.animations[i].animationFrames[n - 1].boneRot.Item2[k];
  695. var adder = animHeader.animations[i].animationFrames[n].boneRot.Item2[k];
  696. animHeader.animations[i].animationFrames[n].boneRot.Item2[k] = new ShortVector() { x = (short)(grabber.x + adder.x), y = (short)(grabber.y + adder.y), z = (short)(grabber.z + adder.z) };
  697. animHeader.animations[i].animationFrames[n].boneRot.Item1[k] = new Vector3(
  698. (animHeader.animations[i].animationFrames[n].boneRot.Item2[k].x) * 360f / 4096f,
  699. (animHeader.animations[i].animationFrames[n].boneRot.Item2[k].y) * 360f / 4096f,
  700. (animHeader.animations[i].animationFrames[n].boneRot.Item2[k].z) * 360f / 4096f);
  701. }
  702. else
  703. {
  704. animHeader.animations[i].animationFrames[n].boneRot.Item2[k] = new ShortVector() { x = bitReader.ReadRotationType(), y = bitReader.ReadRotationType(), z = bitReader.ReadRotationType() };
  705. animHeader.animations[i].animationFrames[n].boneRot.Item1[k] = new Vector3(
  706. (animHeader.animations[i].animationFrames[n].boneRot.Item2[k].x * 360f / 4096f),
  707. (animHeader.animations[i].animationFrames[n].boneRot.Item2[k].y * 360f / 4096f),
  708. (animHeader.animations[i].animationFrames[n].boneRot.Item2[k].z * 360f / 4096f));
  709. }
  710. }
  711. for(int k = 0; k<skeleton.cBones; k++)
  712. {
  713. var rad = animHeader.animations[i].animationFrames[n].boneRot.Item1[k];
  714. Matrix xRot = MakiExtended.GetRotationMatrixX(-rad.X);
  715. Matrix yRot = MakiExtended.GetRotationMatrixY(-rad.Y);
  716. Matrix zRot = MakiExtended.GetRotationMatrixZ(-rad.Z);
  717. var MatrixZ = MakiExtended.MatrixMultiply_transpose(yRot, xRot);
  718. MatrixZ = MakiExtended.MatrixMultiply_transpose(zRot, MatrixZ);
  719. if (skeleton.bones[k].parentId == 0xFFFF)
  720. {
  721. //I'm leaving the code below, because there might be some issue with M4 dimension for position translation for bone0 only
  722. //Matrix rt = Matrix.CreateTranslation(animHeader.animations[i].animationFrames[n].Position);
  723. //MatrixZ.M41 = rt.M42;
  724. //MatrixZ.M42 = rt.M41; //up/down
  725. //MatrixZ.M43 = rt.M43;
  726. //MatrixZ.M44 = 1;
  727. MatrixZ.M43 = animHeader.animations[i].animationFrames[n].Position.Z + 2;
  728. }
  729. else
  730. {
  731. Matrix prevBone = animHeader.animations[i].animationFrames[n].boneRot.Item3[skeleton.bones[k].parentId];
  732. MatrixZ.M43 = skeleton.bones[skeleton.bones[k].parentId].Size;
  733. Matrix rMatrix = Matrix.Multiply(prevBone, MatrixZ);
  734. rMatrix.M41 = prevBone.M11 * MatrixZ.M41 + prevBone.M12 * MatrixZ.M42 + prevBone.M13 * MatrixZ.M43 + prevBone.M41;
  735. rMatrix.M42 = prevBone.M21 * MatrixZ.M41 + prevBone.M22 * MatrixZ.M42 + prevBone.M23 * MatrixZ.M43 + prevBone.M42;
  736. rMatrix.M43 = prevBone.M31 * MatrixZ.M41 + prevBone.M32 * MatrixZ.M42 + prevBone.M33 * MatrixZ.M43 + prevBone.M43;
  737. MatrixZ = rMatrix;
  738. }
  739. animHeader.animations[i].animationFrames[n].boneRot.Item3[k] = MatrixZ;
  740. }
  741. }
  742. }
  743. }
  744. public AnimationData animHeader;
  745. public int frame;
  746. public float frameperFPS = 0.0f;
  747. #endregion
  748. #region section 7 Information
  749. [StructLayout(LayoutKind.Sequential, Pack =1, Size =380)]
  750. public struct Information
  751. {
  752. [MarshalAs(UnmanagedType.ByValArray,SizeConst =24)]
  753. private char[] monsterName;
  754. public uint hp;
  755. public uint str;
  756. public uint vit;
  757. public uint mag;
  758. public uint spr;
  759. public uint spd;
  760. public uint eva;
  761. public Abilities abilitiesLow;
  762. public Abilities abilitiesMed;
  763. public Abilities abilitiesHigh;
  764. public byte medLevelStart;
  765. public byte highLevelStart;
  766. public byte unk;
  767. public byte bitSwitch;
  768. public byte cardLow;
  769. public byte cardMed;
  770. public byte cardHigh;
  771. public byte devourLow;
  772. public byte devourMed;
  773. public byte devourHigh;
  774. public byte bitSwitch2;
  775. public byte unk2;
  776. public ushort extraExp;
  777. public ushort exp;
  778. public ulong drawLow;
  779. public ulong drawMed;
  780. public ulong drawHigh;
  781. public ulong mugLow;
  782. public ulong mugMed;
  783. public ulong mugHigh;
  784. public ulong dropLow;
  785. public ulong dropMed;
  786. public ulong dropHigh;
  787. public byte mugRate;
  788. public byte dropRate;
  789. public byte padding;
  790. public byte ap;
  791. [MarshalAs(UnmanagedType.ByValArray, SizeConst =16)]
  792. public byte[] unk3;
  793. public byte fireResistance;
  794. public byte iceResistance;
  795. public byte thunderResistance;
  796. public byte earthResistance;
  797. public byte poisonResistance;
  798. public byte windResistance;
  799. public byte waterResistance;
  800. public byte holyResistance;
  801. public byte deathResistanceMental;
  802. public byte poisonResistanceMental;
  803. public byte petrifyResistanceMental;
  804. public byte darknessResistanceMental;
  805. public byte silenceResistanceMental;
  806. public byte berserkResistanceMental;
  807. public byte zombieResistanceMental;
  808. public byte sleepResistanceMental;
  809. public byte hasteResistanceMental;
  810. public byte slowResistanceMental;
  811. public byte stopResistanceMental;
  812. public byte regenResistanceMental;
  813. public byte reflectResistanceMental;
  814. public byte doomResistanceMental;
  815. public byte slowPetrifyResistanceMental;
  816. public byte floatResistanceMental;
  817. public byte confuseResistanceMental;
  818. public byte drainResistanceMental;
  819. public byte explusionResistanceMental;
  820. public byte unkResistanceMental;
  821. public string GetNameNormal => new string(monsterName);
  822. }
  823. public struct Abilities
  824. {
  825. public byte kernelId; //0x2 magic, 0x4 item, 0x8 mosterAbility;
  826. public byte unk;
  827. public ushort abilityId;
  828. }
  829. private void ReadSection7(uint v, MemoryStream ms, BinaryReader br)
  830. {
  831. ms.Seek(v, SeekOrigin.Begin);
  832. }
  833. public Information information;
  834. #endregion
  835. #region section 11 Textures
  836. public struct Textures
  837. {
  838. public uint cTims;
  839. public uint[] pTims;
  840. public uint Eof;
  841. public Texture2D[] textures;
  842. }
  843. private void ReadSection11(uint v, MemoryStream ms, BinaryReader br)
  844. {
  845. ms.Seek(v, SeekOrigin.Begin);
  846. textures = new Textures() { cTims = br.ReadUInt32() };
  847. textures.pTims = new uint[textures.cTims];
  848. for (int i = 0; i < textures.cTims; i++)
  849. textures.pTims[i] = br.ReadUInt32();
  850. textures.Eof = br.ReadUInt32();
  851. textures.textures = new Texture2D[textures.cTims];
  852. for(int i = 0; i<textures.cTims; i++)
  853. {
  854. ms.Seek(v + textures.pTims[i], SeekOrigin.Begin);
  855. TIM2 tm = new TIM2(buffer, (uint)ms.Position); //broken
  856. textures.textures[i] = new Texture2D(Memory.graphics.GraphicsDevice, tm.GetWidth, tm.GetHeight, false,
  857. SurfaceFormat.Color);
  858. textures.textures[i].SetData(tm.CreateImageBuffer(tm.GetClutColors(0), true)); //??
  859. tm.KillStreams();
  860. }
  861. }
  862. public Textures textures;
  863. #endregion
  864. public enum EntityType
  865. {
  866. Monster,
  867. Character,
  868. Weapon
  869. };
  870. /// <summary>
  871. /// Creates new instance of DAT class that provides every sections parsed into structs and helper functions for renderer
  872. /// </summary>
  873. /// <param name="fileId">This number is used in c0m(fileId) or d(fileId)cXYZ</param>
  874. /// <param name="entityType">Supply Monster, character or weapon (0,1,2)</param>
  875. /// <param name="additionalFileId">Used only in character or weapon to supply for d(fileId)[c/w](additionalFileId)</param>
  876. public Debug_battleDat(int fileId, EntityType entityType, int additionalFileId = -1, Debug_battleDat skeletonReference = null)
  877. {
  878. id = fileId;
  879. Console.WriteLine($"DEBUG: Creating new BattleDat with {fileId},{entityType},{additionalFileId}");
  880. ArchiveWorker aw = new ArchiveWorker(Memory.Archives.A_BATTLE);
  881. string fileName = entityType == EntityType.Monster ? $"c0m{id.ToString("D03")}" :
  882. entityType == EntityType.Character ? $"d{fileId.ToString("x")}c{additionalFileId.ToString("D03")}" :
  883. entityType == EntityType.Weapon ? $"d{fileId.ToString("x")}w{additionalFileId.ToString("D03")}" : string.Empty;
  884. this.entityType = entityType;
  885. if (string.IsNullOrEmpty(fileName))
  886. return;
  887. string path = aw.GetListOfFiles().First(x => x.ToLower().Contains(fileName));
  888. buffer = ArchiveWorker.GetBinaryFile(Memory.Archives.A_BATTLE, path);
  889. #if _WINDOWS
  890. try
  891. {
  892. string targetdir = @"d:\";
  893. if (Directory.Exists(targetdir))
  894. {
  895. var drivei = DriveInfo.GetDrives().Where(x => x.Name.IndexOf(Path.GetPathRoot(targetdir),StringComparison.OrdinalIgnoreCase)>=0).ToArray();
  896. var di = new DirectoryInfo(targetdir);
  897. if(!di.Attributes.HasFlag(FileAttributes.ReadOnly) && drivei.Count()==1 && drivei[0].DriveType == DriveType.Fixed)
  898. MakiExtended.DumpBuffer(buffer, Path.Combine(targetdir,"out.dat"));
  899. }
  900. }
  901. catch(IOException)
  902. {
  903. }
  904. #endif
  905. using (MemoryStream ms = new MemoryStream(buffer))
  906. using (BinaryReader br = new BinaryReader(ms))
  907. {
  908. datFile = new DatFile { cSections = br.ReadUInt32()};
  909. datFile.pSections = new uint[datFile.cSections];
  910. for (int i = 0; i < datFile.cSections; i++)
  911. datFile.pSections[i] = br.ReadUInt32();
  912. datFile.eof = br.ReadUInt32();
  913. switch (this.entityType)
  914. {
  915. case EntityType.Monster:
  916. if (id == 127) return;
  917. ReadSection1(datFile.pSections[0], ms, br);
  918. ReadSection3(datFile.pSections[2], ms, br);
  919. ReadSection2(datFile.pSections[1], ms, br);
  920. //ReadSection4(datFile.pSections[3]);
  921. //ReadSection5(datFile.pSections[4]);
  922. //ReadSection6(datFile.pSections[5]);
  923. ReadSection7(datFile.pSections[6], ms, br);
  924. //ReadSection8(datFile.pSections[7]);
  925. //ReadSection9(datFile.pSections[8]);
  926. //ReadSection10(datFile.pSections[9]);
  927. ReadSection11(datFile.pSections[10], ms, br);
  928. break;
  929. case EntityType.Character:
  930. ReadSection1(datFile.pSections[0], ms, br);
  931. ReadSection3(datFile.pSections[2], ms, br);
  932. ReadSection2(datFile.pSections[1], ms, br);
  933. if(fileId==7 && entityType==EntityType.Character)
  934. ReadSection11(datFile.pSections[8], ms, br);
  935. else
  936. ReadSection11(datFile.pSections[5], ms, br);
  937. break;
  938. case EntityType.Weapon:
  939. if (skeletonReference == null)
  940. {
  941. ReadSection1(datFile.pSections[0], ms, br);
  942. ReadSection3(datFile.pSections[2], ms, br);
  943. ReadSection2(datFile.pSections[1], ms, br);
  944. ReadSection11(datFile.pSections[6], ms, br);
  945. }
  946. else
  947. {
  948. skeleton = skeletonReference.skeleton;
  949. animHeader = skeletonReference.animHeader;
  950. ReadSection2(datFile.pSections[0], ms, br);
  951. ReadSection11(datFile.pSections[4], ms, br);
  952. }
  953. break;
  954. }
  955. }
  956. }
  957. public int GetId => id;
  958. }
  959. }