unitTracker.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. "use strict";
  2. /*
  3. * Unit for Fps Manager. Tracking original data from server
  4. * Author: SaltyMonkey
  5. * Contributors:
  6. */
  7. /**
  8. * @typedef {import("../fpsManager.js").deps} deps
  9. */
  10. const { ENTITY_HOOK_TYPE, HOOK_SETTINGS, RAW_HOOK_MODE, SPECIAL_ZONES } = require("./helpers/enums");
  11. class unitTracker {
  12. /**
  13. *Creates an instance of unitState.
  14. * @param {deps} deps dependencies
  15. * @memberof unitTracker
  16. */
  17. constructor(deps) {
  18. const mod = deps.mod;
  19. const proto = deps.proto;
  20. // portals "shapeId": 652992,
  21. // ninja log "shapeId": 303030
  22. // sentinel "shapeId": 730500
  23. Object.assign(this, {
  24. "spawnedCollections": new Map(),
  25. "spawnedDrop": new Map(),
  26. "spawnedServants": new Map(),
  27. "spawnedPlayers": new Map(),
  28. "spawnedNPCS": new Map(),
  29. "spawnedSummons": new Map(),
  30. "spawnedBuildObjects": new Map()
  31. });
  32. const dropHandler = (type, event) => {
  33. switch (type) {
  34. case (ENTITY_HOOK_TYPE.DESPAWN): this.spawnedDrop.delete(event.gameId); break;
  35. case (ENTITY_HOOK_TYPE.SPAWN): this.spawnedDrop.set(event.gameId, event); break;
  36. }
  37. };
  38. const collectionHandler = (type, event) => {
  39. switch (type) {
  40. case (ENTITY_HOOK_TYPE.DESPAWN): this.spawnedCollections.delete(event.gameId); break;
  41. case (ENTITY_HOOK_TYPE.SPAWN): this.spawnedCollections.set(event.gameId, event); break;
  42. }
  43. };
  44. const buildObjectHandler = (type, event) => {
  45. switch (type) {
  46. case (ENTITY_HOOK_TYPE.DESPAWN): this.spawnedBuildObjects.delete(event.gameId); break;
  47. case (ENTITY_HOOK_TYPE.SPAWN): this.spawnedBuildObjects.set(event.gameId, event); break;
  48. }
  49. };
  50. const servantsHandler = (type, event) => {
  51. switch (type) {
  52. case (ENTITY_HOOK_TYPE.DESPAWN): this.spawnedServants.delete(event.gameId); break;
  53. case (ENTITY_HOOK_TYPE.SPAWN): this.spawnedServants.set(event.gameId, event); break;
  54. }
  55. };
  56. const userHandler = (type, event) => {
  57. switch (type) {
  58. case (ENTITY_HOOK_TYPE.DESPAWN): this.spawnedPlayers.delete(event.gameId); break;
  59. case (ENTITY_HOOK_TYPE.SPAWN): this.spawnedPlayers.set(event.gameId, event); break;
  60. }
  61. };
  62. const npcHandler = (type, event) => {
  63. switch (type) {
  64. case (ENTITY_HOOK_TYPE.DESPAWN):
  65. this.spawnedNPCS.delete(event.gameId);
  66. this.spawnedSummons.delete(event.gameId);
  67. break;
  68. case (ENTITY_HOOK_TYPE.SPAWN):
  69. if (!event.bySpawnEvent && event.huntingZoneId === SPECIAL_ZONES.HELPERS && this.spawnedPlayers.has(event.owner)) {
  70. this.spawnedSummons.set(event.gameId, event);
  71. }
  72. else {
  73. this.spawnedNPCS.set(event.gameId, event);
  74. }
  75. // eslint-disable-next-line no-magic-numbers
  76. if (event.replaceId !== BigInt(0)) {
  77. this.spawnedNPCS.delete(event.replaceId);
  78. this.spawnedSummons.delete(event.replaceId);
  79. }
  80. break;
  81. }
  82. };
  83. const userMounted = (type, event) => {
  84. if (this.spawnedPlayers.has(event.gameId)) {
  85. let data = undefined;
  86. switch (type) {
  87. case (ENTITY_HOOK_TYPE.DESPAWN):
  88. data = this.spawnedPlayers.get(event.gameId);
  89. data.mount = 0;
  90. this.spawnedPlayers.set(event.gameId, data);
  91. break;
  92. case (ENTITY_HOOK_TYPE.SPAWN):
  93. data = this.spawnedPlayers.get(event.gameId);
  94. data.mount = event.id;
  95. this.spawnedPlayers.set(event.gameId, data);
  96. break;
  97. }
  98. }
  99. };
  100. const updateAngle = (data, w) => {
  101. data.loc.w = w;
  102. return data;
  103. };
  104. const updateLoc = (data, loc) => {
  105. data.loc = loc;
  106. return data;
  107. };
  108. const updateAngleHandler = (event) => {
  109. let gameId = event.gameId;
  110. if (this.spawnedPlayers.has(gameId)) this.spawnedPlayers.set(gameId, updateAngle(this.spawnedPlayers.get(gameId), event.w));
  111. else if (this.spawnedNPCS.has(gameId)) this.spawnedNPCS.set(gameId, updateAngle(this.spawnedNPCS.get(gameId), event.w));
  112. else if (this.spawnedSummons.has(gameId)) this.spawnedSummons.set(gameId, updateAngle(this.spawnedSummons.get(gameId), event.w));
  113. };
  114. const playerLocationHandler = (event) => {
  115. if (this.spawnedPlayers.has(event.gameId)) {
  116. this.spawnedPlayers.set(event.gameId, updateLoc(this.spawnedPlayers.get(event.gameId), event.loc));
  117. }
  118. };
  119. const npcLocationHandler = (event) => {
  120. if (this.spawnedNPCS.has(event.gameId)) {
  121. this.spawnedNPCS.set(event.gameId, updateLoc(this.spawnedNPCS.get(event.gameId), event.loc));
  122. }
  123. else if (this.spawnedSummons.has(event.gameId)) {
  124. this.spawnedSummons.set(event.gameId, updateLoc(this.spawnedSummons.get(event.gameId), event.loc));
  125. }
  126. else if (this.spawnedServants.has(event.gameId)) {
  127. this.spawnedServants.set(event.gameId, updateLoc(this.spawnedServants.get(event.gameId), event.loc));
  128. }
  129. };
  130. const creatureLifeHandler = (event) => {
  131. if (this.spawnedPlayers.has(event.gameId)) {
  132. let data = this.spawnedPlayers.get(event.gameId);
  133. data.alive = event.alive;
  134. this.spawnedPlayers.set(event.gameId, data);
  135. }
  136. };
  137. const cleanup = () => {
  138. this.spawnedNPCS.clear();
  139. this.spawnedSummons.clear();
  140. this.spawnedPlayers.clear();
  141. this.spawnedServants.clear();
  142. this.spawnedCollections.clear();
  143. this.spawnedDrop.clear();
  144. this.spawnedBuildObjects.clear();
  145. };
  146. const bullrushHandler = (event) => {
  147. playerLocationHandler(event);
  148. npcLocationHandler(event);
  149. };
  150. mod.hook(...proto.getData("S_CREATURE_ROTATE"), HOOK_SETTINGS.FIRST, updateAngleHandler);
  151. mod.hook(...proto.getData("S_CREATURE_LIFE"), HOOK_SETTINGS.FIRST, creatureLifeHandler);
  152. mod.hook(...proto.getData("S_NPC_LOCATION"), HOOK_SETTINGS.FIRST, npcLocationHandler);
  153. mod.hook(...proto.getData("S_USER_LOCATION"), HOOK_SETTINGS.FIRST, playerLocationHandler);
  154. mod.hook(...proto.getData("S_STICK_TO_USER_END"), HOOK_SETTINGS.LAST, bullrushHandler);
  155. mod.hook(...proto.getData("S_USER_LOCATION_IN_ACTION"), HOOK_SETTINGS.FIRST, playerLocationHandler);
  156. mod.hook(...proto.getData("S_MOUNT_VEHICLE"), HOOK_SETTINGS.FIRST, userMounted.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  157. mod.hook(...proto.getData("S_UNMOUNT_VEHICLE"), HOOK_SETTINGS.FIRST, userMounted.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  158. mod.hook(...proto.getData("S_SPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  159. mod.hook(...proto.getData("S_DESPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  160. mod.hook(...proto.getData("S_SPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  161. mod.hook(...proto.getData("S_DESPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  162. mod.hook(...proto.getData("S_REQUEST_SPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  163. mod.hook(...proto.getData("S_REQUEST_DESPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  164. mod.hook(...proto.getData("S_SPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  165. mod.hook(...proto.getData("S_DESPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  166. mod.hook(...proto.getData("S_SPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  167. mod.hook(...proto.getData("S_DESPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  168. mod.hook(...proto.getData("S_SPAWN_BUILD_OBJECT"), HOOK_SETTINGS.FIRST, buildObjectHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  169. mod.hook(...proto.getData("S_DESPAWN_BUILD_OBJECT"), HOOK_SETTINGS.FIRST, buildObjectHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  170. mod.hook("S_LOAD_TOPO", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, cleanup);
  171. }
  172. }
  173. module.exports = unitTracker;