unitTracker.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. });
  31. const dropHandler = (type, event) => {
  32. switch (type) {
  33. case (ENTITY_HOOK_TYPE.DESPAWN): this.spawnedDrop.delete(event.gameId); break;
  34. case (ENTITY_HOOK_TYPE.SPAWN): this.spawnedDrop.set(event.gameId, event); break;
  35. }
  36. };
  37. const collectionHandler = (type, event) => {
  38. switch (type) {
  39. case (ENTITY_HOOK_TYPE.DESPAWN): this.spawnedCollections.delete(event.gameId); break;
  40. case (ENTITY_HOOK_TYPE.SPAWN): this.spawnedCollections.set(event.gameId, event); break;
  41. }
  42. };
  43. const servantsHandler = (type, event) => {
  44. switch (type) {
  45. case (ENTITY_HOOK_TYPE.DESPAWN): this.spawnedServants.delete(event.gameId); break;
  46. case (ENTITY_HOOK_TYPE.SPAWN): this.spawnedServants.set(event.gameId, event); break;
  47. }
  48. };
  49. const userHandler = (type, event) => {
  50. switch (type) {
  51. case (ENTITY_HOOK_TYPE.DESPAWN): this.spawnedPlayers.delete(event.gameId); break;
  52. case (ENTITY_HOOK_TYPE.SPAWN): this.spawnedPlayers.set(event.gameId, event); break;
  53. }
  54. };
  55. const npcHandler = (type, event) => {
  56. switch (type) {
  57. case (ENTITY_HOOK_TYPE.DESPAWN):
  58. this.spawnedNPCS.delete(event.gameId);
  59. this.spawnedSummons.delete(event.gameId);
  60. break;
  61. case (ENTITY_HOOK_TYPE.SPAWN):
  62. // eslint-disable-next-line no-magic-numbers
  63. if (event.huntingZoneId === SPECIAL_ZONES.HELPERS && this.spawnedPlayers.has(event.owner)) {
  64. this.spawnedSummons.set(event.gameId, event);
  65. }
  66. else {
  67. this.spawnedNPCS.set(event.gameId, event);
  68. }
  69. // eslint-disable-next-line no-magic-numbers
  70. if (event.replaceId !== BigInt(0)) {
  71. this.spawnedNPCS.delete(event.replaceId);
  72. this.spawnedSummons.delete(event.replaceId);
  73. }
  74. break;
  75. }
  76. };
  77. const userMounted = (type, event) => {
  78. if (this.spawnedPlayers.has(event.gameId)) {
  79. let data = undefined;
  80. switch (type) {
  81. case (ENTITY_HOOK_TYPE.DESPAWN):
  82. data = this.spawnedPlayers.get(event.gameId);
  83. data.mount = 0;
  84. this.spawnedPlayers.set(event.gameId, data);
  85. break;
  86. case (ENTITY_HOOK_TYPE.SPAWN):
  87. data = this.spawnedPlayers.get(event.gameId);
  88. data.mount = event.id;
  89. this.spawnedPlayers.set(event.gameId, data);
  90. break;
  91. }
  92. }
  93. };
  94. const updateAngle = (data, w) => {
  95. data.loc.w = w;
  96. return data;
  97. };
  98. const updateLoc = (data, loc) => {
  99. data.loc = loc;
  100. return data;
  101. };
  102. const updateAngleHandler = (event) => {
  103. let gameId = event.gameId;
  104. if (this.spawnedPlayers.has(gameId)) this.spawnedPlayers.set(gameId, updateAngle(this.spawnedPlayers.get(gameId), event.w));
  105. else if (this.spawnedNPCS.has(gameId)) this.spawnedNPCS.set(gameId, updateAngle(this.spawnedNPCS.get(gameId), event.w));
  106. else if (this.spawnedSummons.has(gameId)) this.spawnedSummons.set(gameId, updateAngle(this.spawnedSummons.get(gameId), event.w));
  107. };
  108. const playerLocationHandler = (event) => {
  109. if (this.spawnedPlayers.has(event.gameId)) {
  110. this.spawnedPlayers.set(event.gameId, updateLoc(this.spawnedPlayers.get(event.gameId), event.loc));
  111. }
  112. };
  113. const npcLocationHandler = (event) => {
  114. if (this.spawnedNPCS.has(event.gameId)) {
  115. this.spawnedNPCS.set(event.gameId, updateLoc(this.spawnedNPCS.get(event.gameId), event.loc));
  116. }
  117. else if (this.spawnedSummons.has(event.gameId)) {
  118. this.spawnedSummons.set(event.gameId, updateLoc(this.spawnedSummons.get(event.gameId), event.loc));
  119. }
  120. else if (this.spawnedServants.has(event.gameId)) {
  121. this.spawnedServants.set(event.gameId, updateLoc(this.spawnedServants.get(event.gameId), event.loc));
  122. }
  123. };
  124. const creatureLifeHandler = (event) => {
  125. if (this.spawnedPlayers.has(event.gameId)) {
  126. let data = this.spawnedPlayers.get(event.gameId);
  127. data.alive = event.alive;
  128. this.spawnedPlayers.set(event.gameId, data);
  129. }
  130. };
  131. const cleanup = () => {
  132. console.log("DEBUG: unitTracker -> cleanup");
  133. this.spawnedNPCS.clear();
  134. this.spawnedSummons.clear();
  135. this.spawnedPlayers.clear();
  136. this.spawnedServants.clear();
  137. this.spawnedCollections.clear();
  138. this.spawnedDrop.clear();
  139. };
  140. const bullrushHandler = (event) => {
  141. playerLocationHandler(event);
  142. npcLocationHandler(event);
  143. };
  144. mod.hook(...proto.getData("S_CREATURE_ROTATE"), HOOK_SETTINGS.FIRST, updateAngleHandler);
  145. mod.hook(...proto.getData("S_CREATURE_LIFE"), HOOK_SETTINGS.FIRST, creatureLifeHandler);
  146. mod.hook(...proto.getData("S_NPC_LOCATION"), HOOK_SETTINGS.FIRST, npcLocationHandler);
  147. mod.hook(...proto.getData("S_USER_LOCATION"), HOOK_SETTINGS.FIRST, playerLocationHandler);
  148. mod.hook(...proto.getData("S_STICK_TO_USER_END"), HOOK_SETTINGS.LAST, bullrushHandler);
  149. mod.hook(...proto.getData("S_USER_LOCATION_IN_ACTION"), HOOK_SETTINGS.FIRST, playerLocationHandler);
  150. mod.hook(...proto.getData("S_MOUNT_VEHICLE"), HOOK_SETTINGS.FIRST, userMounted.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  151. mod.hook(...proto.getData("S_UNMOUNT_VEHICLE"), HOOK_SETTINGS.FIRST, userMounted.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  152. mod.hook(...proto.getData("S_SPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  153. mod.hook(...proto.getData("S_DESPAWN_NPC"), HOOK_SETTINGS.FIRST, npcHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  154. mod.hook(...proto.getData("S_SPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  155. mod.hook(...proto.getData("S_DESPAWN_USER"), HOOK_SETTINGS.FIRST, userHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  156. mod.hook(...proto.getData("S_REQUEST_SPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  157. mod.hook(...proto.getData("S_REQUEST_DESPAWN_SERVANT"), HOOK_SETTINGS.FIRST, servantsHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  158. mod.hook(...proto.getData("S_SPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  159. mod.hook(...proto.getData("S_DESPAWN_COLLECTION"), HOOK_SETTINGS.FIRST, collectionHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  160. mod.hook(...proto.getData("S_SPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.SPAWN));
  161. mod.hook(...proto.getData("S_DESPAWN_DROPITEM"), HOOK_SETTINGS.FIRST, dropHandler.bind(null, ENTITY_HOOK_TYPE.DESPAWN));
  162. mod.hook("S_LOAD_TOPO", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, cleanup);
  163. }
  164. }
  165. module.exports = unitTracker;