unitState.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. "use strict";
  2. /*
  3. * Unit for Fps Manager. Managing module state and events
  4. * Author: SaltyMonkey
  5. * Contributors:
  6. */
  7. /**
  8. * @typedef {import("../fpsManager.js").deps} deps
  9. */
  10. //using es5 compatible mode because json not supports es6 types and es6 version is slow ->(require("fast-deep-equal/es6");)
  11. const EventEmitter = require("events").EventEmitter;
  12. const rfdc = require("rfdc")({ "proto": true });
  13. const utils = require("./helpers/utils");
  14. //const watchdog = require("./helpers/watchdog");
  15. //import enums
  16. const { RAW_HOOK_MODE, HOOK_SETTINGS, ABN, EVENTS } = require("./helpers/enums");
  17. //types for abnormals to find in client data
  18. const shapeChangers = [ABN.BigHead, ABN.ShapeChanger];
  19. const onScreenEffects = [ABN.FX, ABN.PostProcessingEffect];
  20. const ccbEffects = [ABN.CCB];
  21. class unitState extends EventEmitter {
  22. /**
  23. *Creates an instance of unitState.
  24. * @param {deps} deps dependencies
  25. * @memberof unitState
  26. */
  27. constructor(deps) {
  28. super();
  29. this.__mod = deps.mod;
  30. this.__proto = deps.proto;
  31. this.__log = deps.log;
  32. Object.assign(this, {
  33. //main properties for state management
  34. "presets": {}, // all loaded per class presets
  35. "preset": {}, //current chosen preset
  36. "activeMode": undefined,
  37. "prevMode": undefined,
  38. "lockedMode": false,
  39. "inGame": false,
  40. "externalShakeControl": false,
  41. "availableClasses": new Set(),
  42. "userTemplateToClassMap": {},
  43. "lockedPacketsData": [],
  44. "throttlePacketsData": [],
  45. //lookup structures
  46. "shapeChangersBuffs": new Set(),
  47. "onScreenEffectsBuffs": new Set(),
  48. "ccbBuffs": new Set(),
  49. "smtData": new Set(),
  50. "specialAs": new Set(),
  51. "whitelistAbnormals": new Set(),
  52. "whitelistProjectiles": new Set()
  53. });
  54. ([3107001, 3107002, 3107003]).forEach(x => {
  55. this.specialAs.add(x);
  56. });
  57. deps.mod.game.data.abnormalities.forEach(abnormality => {
  58. let types = abnormality.effects.map(x => x.type);
  59. if (utils.arraysHasIntersect(types, shapeChangers))
  60. this.shapeChangersBuffs.add(abnormality.id);
  61. if (utils.arraysHasIntersect(types, onScreenEffects))
  62. this.onScreenEffectsBuffs.add(abnormality.id);
  63. if (utils.arraysHasIntersect(types, ccbEffects))
  64. this.ccbBuffs.add(abnormality.id);
  65. });
  66. this.__mod.queryData("/S1ActionScripts@type=?/Script/", ["Teleport"], true, false, ["id"]).then(result => {
  67. let res = result.map(rs => rs.attributes.id);
  68. res.forEach(x => { this.specialAs.add(x); });
  69. });
  70. this.availableClasses.add("common");
  71. deps.mod.game.data.users.forEach(template => {
  72. this.availableClasses.add(template.class);
  73. this.userTemplateToClassMap[template.id] = template.class;
  74. });
  75. this.Init();
  76. }
  77. Init() {
  78. ([...this.availableClasses]).forEach(x => {
  79. this.presets[x] = utils.loadJson(utils.getFullPath(__dirname, `../settings/presets/${x}.json`));
  80. });
  81. let staticData = utils.loadJson(utils.getFullPath(__dirname, "../data/staticData.json"));
  82. this.lockedPacketsData = staticData.blockedProto;
  83. this.throttlePacketsData = staticData.throttledProto;
  84. this.smtData = new Set(staticData.blockedSmt);
  85. this.whitelistAbnormals = new Set(staticData.excludedAbnormals);
  86. this.whitelistProjectiles = new Set(staticData.excludedProjectiles);
  87. this.__mod.hook("S_LOGIN", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
  88. this.lockedMode = false;
  89. if(this.__mod.manager.isLoaded("camera-control")) {
  90. this.__mod.log("Camera shake effects will not be applied as the Shaker mod is installed. Please use \"/toolbox shaker\" to configure camera shaking behavior");
  91. this.externalShakeControl = true;
  92. }
  93. this.preset = this.__mod.settings.classBasedPresets
  94. ? (this.presets[this.__mod.game.me.class] || this.presets["common"])
  95. : this.presets["common"];
  96. if (!this.preset) { throw new Error("Preset can't be loaded! Critical error. "); }
  97. this.activeMode = this.preset.modes[this.preset.directives["default"]];
  98. if (this.__mod.settings.interactive) this.__log.send(`Default mode: "${this.preset.directives["default"]}".`);
  99. this.inGame = true;
  100. this._eventTriggerPartial();
  101. });
  102. this.__mod.hook(...this.__proto.getData("S_LOAD_TOPO"), (event) => {
  103. if (!this.__mod.settings.enableEvents || this.lockedMode) return;
  104. this.prevMode = rfdc(this.activeMode);
  105. if (this.__mod.game.me.inBattleground && this.preset.directives.triggerBG) {
  106. this.activeMode = this.preset.modes[this.preset.directives.triggerBG];
  107. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerBG}".`);
  108. }
  109. else if (this.__mod.game.me.inCivilUnrest && this.preset.directives.triggerCU) {
  110. this.activeMode = this.preset.modes[this.preset.directives.triggerCU];
  111. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerCU}".`);
  112. }
  113. else if (this.__mod.game.me.inDungeon && this.preset.directives.triggerDungeons) {
  114. let mode = undefined;
  115. let name = undefined;
  116. if (this.preset.modes[this.preset.directives.triggerDungeons[event.zone]]) {
  117. name = this.preset.directives.triggerDungeons[event.zone];
  118. mode = this.preset.modes[this.preset.directives.triggerDungeons[event.zone]];
  119. }
  120. else if (this.preset.modes[this.preset.directives.triggerDungeons["all"]]) {
  121. name = this.preset.directives.triggerDungeons["all"];
  122. mode = this.preset.modes[this.preset.directives.triggerDungeons["all"]];
  123. }
  124. if (mode) {
  125. this.activeMode = mode;
  126. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to ${name}`);
  127. }
  128. }
  129. else if (this.__mod.game.me.inOpenWorld && this.preset.directives.triggerOW) {
  130. this.activeMode = this.preset.modes[this.preset.directives.triggerOW];
  131. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerOW}".`);
  132. }
  133. this._eventTriggerPartial();
  134. });
  135. this.__mod.hook("S_FIELD_EVENT_ON_ENTER", RAW_HOOK_MODE.EVENT, () => {
  136. if (!this.__mod.settings.enableEvents) return;
  137. this.prevMode = rfdc(this.activeMode);
  138. this.lockedMode = true;
  139. this.activeMode = this.preset.modes[this.preset.directives.triggerGuardians];
  140. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerGuardians}".`);
  141. this._eventTrigger();
  142. });
  143. this.__mod.hook("S_FIELD_EVENT_ON_LEAVE", RAW_HOOK_MODE.EVENT, () => {
  144. if (!this.__mod.settings.enableEvents) return;
  145. this.activeMode = rfdc(this.prevMode);
  146. this.lockedMode = false;
  147. if (this.__mod.settings.interactive) this.__log.send("Mode was changed back");
  148. this._eventTrigger();
  149. });
  150. this.__mod.hook("S_RETURN_TO_LOBBY", RAW_HOOK_MODE.EVENT, () => {
  151. this.inGame = false;
  152. });
  153. }
  154. _eventTrigger() {
  155. this.emit(EVENTS.RECONFIGURE);
  156. }
  157. _eventTriggerPartial() {
  158. this.emit(EVENTS.RECONFIGUREPARTIAL);
  159. }
  160. }
  161. module.exports = unitState;