unitState.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. deps.mod.game.data.abnormalities.forEach(abnormality => {
  55. let types = abnormality.effects.map(x => x.type);
  56. if (utils.arraysHasIntersect(types, shapeChangers))
  57. this.shapeChangersBuffs.add(abnormality.id);
  58. if (utils.arraysHasIntersect(types, onScreenEffects))
  59. this.onScreenEffectsBuffs.add(abnormality.id);
  60. if (utils.arraysHasIntersect(types, ccbEffects))
  61. this.ccbBuffs.add(abnormality.id);
  62. });
  63. this.__mod.queryData("/S1ActionScripts@type=?/Script/", ["Teleport"], true, false, ["id"]).then(result => {
  64. let res = result.map(rs => rs.attributes.id);
  65. res.forEach(x => { this.specialAs.add(x); });
  66. });
  67. this.availableClasses.add("common");
  68. deps.mod.game.data.users.forEach(template => {
  69. this.availableClasses.add(template.class);
  70. this.userTemplateToClassMap[template.id] = template.class;
  71. });
  72. this.Init();
  73. }
  74. Init() {
  75. ([...this.availableClasses]).forEach(x => {
  76. this.presets[x] = utils.loadJson(utils.getFullPath(__dirname, `../settings/presets/${x}.json`));
  77. });
  78. let staticData = utils.loadJson(utils.getFullPath(__dirname, "../data/staticData.json"));
  79. this.lockedPacketsData = staticData.blockedProto;
  80. this.throttlePacketsData = staticData.throttledProto;
  81. this.smtData = new Set(staticData.blockedSmt);
  82. this.whitelistAbnormals = new Set(staticData.excludedAbnormals);
  83. this.whitelistProjectiles = new Set(staticData.excludedProjectiles);
  84. this.__mod.hook("S_LOGIN", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
  85. this.lockedMode = false;
  86. if(this.__mod.manager.isLoaded("shaker")) {
  87. this.__mod.warning("Camera shake effects will not be applied as the Shaker mod is installed. Please use \"/toolbox shaker\" to configure camera shaking behavior");
  88. this.externalShakeControl = true;
  89. }
  90. this.preset = this.__mod.settings.classBasedPresets
  91. ? (this.presets[this.__mod.game.me.class] || this.presets["common"])
  92. : this.presets["common"];
  93. if (!this.preset) { throw new Error("Preset can't be loaded! Critical error. "); }
  94. this.activeMode = this.preset.modes[this.preset.directives["default"]];
  95. if (this.__mod.settings.interactive) this.__log.send(`Default mode: "${this.preset.directives["default"]}".`);
  96. this.inGame = true;
  97. this._eventTriggerPartial();
  98. });
  99. this.__mod.hook(...this.__proto.getData("S_LOAD_TOPO"), (event) => {
  100. if (!this.__mod.settings.enableEvents || this.lockedMode) return;
  101. this.prevMode = rfdc(this.activeMode);
  102. if (this.__mod.game.me.inBattleground && this.preset.directives.triggerBG) {
  103. this.activeMode = this.preset.modes[this.preset.directives.triggerBG];
  104. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerBG}".`);
  105. }
  106. else if (this.__mod.game.me.inCivilUnrest && this.preset.directives.triggerCU) {
  107. this.activeMode = this.preset.modes[this.preset.directives.triggerCU];
  108. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerCU}".`);
  109. }
  110. else if (this.__mod.game.me.inDungeon && this.preset.directives.triggerDungeons) {
  111. let mode = undefined;
  112. let name = undefined;
  113. if (this.preset.modes[this.preset.directives.triggerDungeons[event.zone]]) {
  114. name = this.preset.directives.triggerDungeons[event.zone];
  115. mode = this.preset.modes[this.preset.directives.triggerDungeons[event.zone]];
  116. }
  117. else if (this.preset.modes[this.preset.directives.triggerDungeons["all"]]) {
  118. name = this.preset.directives.triggerDungeons["all"];
  119. mode = this.preset.modes[this.preset.directives.triggerDungeons["all"]];
  120. }
  121. if (mode) {
  122. this.activeMode = mode;
  123. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to ${name}`);
  124. }
  125. }
  126. else if (this.__mod.game.me.inOpenWorld && this.preset.directives.triggerOW) {
  127. this.activeMode = this.preset.modes[this.preset.directives.triggerOW];
  128. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerOW}".`);
  129. }
  130. this._eventTriggerPartial();
  131. });
  132. this.__mod.hook("S_FIELD_EVENT_ON_ENTER", RAW_HOOK_MODE.EVENT, () => {
  133. if (!this.__mod.settings.enableEvents) return;
  134. this.prevMode = rfdc(this.activeMode);
  135. this.lockedMode = true;
  136. this.activeMode = this.preset.modes[this.preset.directives.triggerGuardians];
  137. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerGuardians}".`);
  138. this._eventTrigger();
  139. });
  140. this.__mod.hook("S_FIELD_EVENT_ON_LEAVE", RAW_HOOK_MODE.EVENT, () => {
  141. if (!this.__mod.settings.enableEvents) return;
  142. this.activeMode = rfdc(this.prevMode);
  143. this.lockedMode = false;
  144. if (this.__mod.settings.interactive) this.__log.send("Mode was changed back");
  145. this._eventTrigger();
  146. });
  147. this.__mod.hook("S_RETURN_TO_LOBBY", RAW_HOOK_MODE.EVENT, () => {
  148. this.inGame = false;
  149. });
  150. }
  151. _eventTrigger() {
  152. this.emit(EVENTS.RECONFIGURE);
  153. }
  154. _eventTriggerPartial() {
  155. this.emit(EVENTS.RECONFIGUREPARTIAL);
  156. }
  157. }
  158. module.exports = unitState;