rail.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. namespace OpenVIII.World
  7. {
  8. /// <summary>
  9. /// Rail.obj is a file that contains waypoints and stops for trains including sequences
  10. /// </summary>
  11. internal class rail
  12. {
  13. private const int RAIL_BLOCK_SIZE = 2048;
  14. private const int BLOCK_HEADER_SIZE = 12;
  15. [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 8)]
  16. private struct RailEntry
  17. {
  18. public byte cKeypoints;
  19. public byte unk;
  20. public ushort unk2;
  21. public uint trainStop1;
  22. public uint trainStop2;
  23. public Keypoint[] keypoints;
  24. public Keypoint TrainStop1 => keypoints[trainStop1];
  25. public Keypoint TrainStop2 => keypoints[trainStop2];
  26. }
  27. /// <summary>
  28. /// to reverse, but probably should be finally changed to vec4- it's normal vector, but
  29. /// relative to world coordinates of vanilla world map
  30. /// </summary>
  31. [StructLayout(LayoutKind.Sequential, Pack = 1, Size = 16)]
  32. private struct Keypoint
  33. {
  34. public int x;
  35. public int y;
  36. private int z;
  37. public int padd;
  38. public int Z => -z;
  39. }
  40. private RailEntry[] railEntries;
  41. public rail(byte[] buffer)
  42. {
  43. var railEntries = buffer.Length / RAIL_BLOCK_SIZE;
  44. this.railEntries = new RailEntry[railEntries];
  45. MemoryStream ms = null;
  46. using (var br = new BinaryReader(ms = new MemoryStream(buffer)))
  47. {
  48. for (var i = 0; i < railEntries; i++)
  49. this.railEntries[i] = ParseBlock(br.ReadBytes(2048));
  50. ms = null;
  51. }
  52. }
  53. private RailEntry ParseBlock(byte[] block)
  54. {
  55. var entry = new RailEntry()
  56. {
  57. cKeypoints = block[0],
  58. unk = block[1],
  59. unk2 = BitConverter.ToUInt16(block, 2),
  60. trainStop1 = BitConverter.ToUInt32(block, 4),
  61. trainStop2 = BitConverter.ToUInt32(block, 8)
  62. };
  63. entry.keypoints = new Keypoint[entry.cKeypoints];
  64. for (var i = 0; i < entry.cKeypoints; i++)
  65. entry.keypoints[i] = Extended.ByteArrayToStructure<Keypoint>(block.Skip(BLOCK_HEADER_SIZE + (i * 16)).Take(16).ToArray());
  66. return entry;
  67. }
  68. /// <summary>
  69. /// Gets Count of all keypoints associated with provided trackId
  70. /// </summary>
  71. /// <param name="trackId"></param>
  72. /// <returns></returns>
  73. public int GetTrainTrackFrameCount(int trackId) => railEntries[trackId].keypoints.Length;
  74. /// <summary>
  75. /// Gets Count of all available tracks
  76. /// </summary>
  77. /// <returns></returns>
  78. public int GetTrainTrackCount() => railEntries.Length;
  79. /// <summary>
  80. /// Gets Vector3 translated to openviii coordinates from given track animation's frame ID
  81. /// </summary>
  82. /// <param name="trackId"></param>
  83. /// <param name="frameId"></param>
  84. /// <returns></returns>
  85. public Vector3 GetTrackFrameVector(int trackId, int frameId)
  86. {
  87. var kp = railEntries[trackId].keypoints[frameId];
  88. return new Vector3(Extended.ConvertVanillaWorldXAxisToOpenVIII(kp.x),
  89. Extended.ConvertVanillaWorldYAxisToOpenVIII(kp.y),
  90. Extended.ConvertVanillaWorldZAxisToOpenVIII(kp.Z)
  91. );
  92. }
  93. }
  94. }