unitState.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, ABN.CCB];
  20. class unitState extends EventEmitter {
  21. /**
  22. *Creates an instance of unitState.
  23. * @param {deps} deps dependencies
  24. * @memberof unitState
  25. */
  26. constructor(deps) {
  27. super();
  28. this.__mod = deps.mod;
  29. this.__proto = deps.proto;
  30. this.__log = deps.log;
  31. Object.assign(this, {
  32. //main properties for state management
  33. "presets": {}, // all loaded per class presets
  34. "preset": {}, //current chosen preset
  35. "activeMode": undefined,
  36. "prevMode": undefined,
  37. "lockedMode": false,
  38. "availableClasses": new Set(),
  39. "userTemplateToClassMap": {},
  40. "lockedPacketsData": [],
  41. "throttlePacketsData": [],
  42. //lookup structures
  43. "shapeChangersBuffs": new Set(),
  44. "onScreenEffectsBuffs": new Set(),
  45. "smtData": new Set(),
  46. "specialAs": new Set()
  47. });
  48. deps.mod.game.data.abnormalities.forEach(abnormality => {
  49. let types = abnormality.effects.map(x => x.type);
  50. if (utils.arraysHasIntersect(types, shapeChangers))
  51. this.shapeChangersBuffs.add(abnormality.id);
  52. if (utils.arraysHasIntersect(types, onScreenEffects))
  53. this.onScreenEffectsBuffs.add(abnormality.id);
  54. });
  55. this.__mod.queryData("/S1ActionScripts@type=?/Script/", ["Teleport"], true, false, ["id"]).then(result => {
  56. let res = result.map(rs => rs.attributes.id);
  57. res.forEach(x => { this.specialAs.add(x); });
  58. });
  59. this.availableClasses.add("common");
  60. deps.mod.game.data.users.forEach(template => {
  61. this.availableClasses.add(template.class);
  62. this.userTemplateToClassMap[template.id] = template.class;
  63. });
  64. this.Init();
  65. }
  66. Init() {
  67. console.log("DEBUG: unitState -> Init");
  68. ([...this.availableClasses]).forEach(x => {
  69. this.presets[x] = utils.loadJson(utils.getFullPath(__dirname, `../settings/presets/${x}.json`));
  70. });
  71. let staticData = utils.loadJson(utils.getFullPath(__dirname, "../data/staticData.json"));
  72. this.lockedPacketsData = staticData.blockedProto;
  73. this.throttlePacketsData = staticData.throttledProto;
  74. this.smtData = new Set(...staticData.blockedSmt);
  75. this.__mod.hook("S_LOGIN", RAW_HOOK_MODE.EVENT, HOOK_SETTINGS.FIRST, () => {
  76. this.lockedMode = false;
  77. this.preset = this.__mod.settings.classBasedPresets
  78. ? (this.presets[this.__mod.game.me.class] || this.presets["common"])
  79. : this.presets["common"];
  80. if (!this.preset) { throw new Error("Preset can't be loaded! Critical error. "); }
  81. this.activeMode = this.preset.modes[this.preset.directives["default"]];
  82. if (this.__mod.settings.interactive) this.__log.send(`Default mode: "${this.preset.directives["default"]}".`);
  83. this._eventTriggerPartial();
  84. });
  85. this.__mod.hook(...this.__proto.getData("S_LOAD_TOPO"), (event) => {
  86. if (!this.__mod.settings.enableEvents || this.lockedMode) return;
  87. this.prevMode = rfdc(this.activeMode);
  88. if (this.__mod.game.me.inBattleground && this.preset.directives.triggerBG) {
  89. this.activeMode = this.preset.modes[this.preset.directives.triggerBG];
  90. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerBG}".`);
  91. }
  92. else if (this.__mod.game.me.inCivilUnrest && this.preset.directives.triggerCU) {
  93. this.activeMode = this.preset.modes[this.preset.directives.triggerCU];
  94. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerCU}".`);
  95. }
  96. else if (this.__mod.game.me.inDungeon && this.preset.directives.triggerDungeons) {
  97. let mode = undefined;
  98. let name = undefined;
  99. if (this.preset.modes[this.preset.directives.triggerDungeons[event.zone]]) {
  100. name = this.preset.directives.triggerDungeons[event.zone];
  101. mode = this.preset.modes[this.preset.directives.triggerDungeons[event.zone]];
  102. }
  103. else if (this.preset.modes[this.preset.directives.triggerDungeons["all"]]) {
  104. name = this.preset.directives.triggerDungeons["all"];
  105. mode = this.preset.modes[this.preset.directives.triggerDungeons["all"]];
  106. }
  107. if (mode) {
  108. this.activeMode = mode;
  109. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to ${name}`);
  110. }
  111. }
  112. else if (this.__mod.game.me.inOpenWorld && this.preset.directives.triggerOW) {
  113. this.activeMode = this.preset.modes[this.preset.directives.triggerOW];
  114. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerOW}".`);
  115. }
  116. this._eventTriggerPartial();
  117. });
  118. this.__mod.hook("S_FIELD_EVENT_ON_ENTER", RAW_HOOK_MODE.EVENT, () => {
  119. if (!this.__mod.settings.enableEvents) return;
  120. this.prevMode = rfdc(this.activeMode);
  121. this.lockedMode = true;
  122. this.activeMode = this.preset.modes[this.preset.directives.triggerGuardians];
  123. if (this.__mod.settings.interactive) this.__log.send(`Mode was changed to "${this.preset.directives.triggerGuardians}".`);
  124. this._eventTrigger();
  125. });
  126. this.__mod.hook("S_FIELD_EVENT_ON_LEAVE", RAW_HOOK_MODE.EVENT, () => {
  127. if (!this.__mod.settings.enableEvents) return;
  128. this.activeMode = rfdc(this.prevMode);
  129. this.lockedMode = false;
  130. if (this.__mod.settings.interactive) this.__log.send("Mode was changed back");
  131. this._eventTrigger();
  132. });
  133. }
  134. _eventTrigger() {
  135. console.log("DEBUG: unitState -> _eventTrigger");
  136. this.emit(EVENTS.RECONFIGURE);
  137. }
  138. _eventTriggerPartial() {
  139. console.log("DEBUG: unitState -> _eventTriggerPartial");
  140. this.emit(EVENTS.RECONFIGUREPARTIAL);
  141. }
  142. }
  143. module.exports = unitState;