unitTracker.js 7.2 KB

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