Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

694 lignes
16 KiB

  1. let spellTemplate = require('../config/spells/spellTemplate');
  2. let animations = require('../config/animations');
  3. let playerSpells = require('../config/spells');
  4. let playerSpellsConfig = require('../config/spellsConfig');
  5. //Helpers
  6. const rotationManager = require('./spellbook/rotationManager');
  7. //Component
  8. module.exports = {
  9. type: 'spellbook',
  10. spells: [],
  11. physics: null,
  12. objects: null,
  13. closestRange: -1,
  14. furthestRange: -1,
  15. callbacks: [],
  16. rotation: null,
  17. init: function (blueprint) {
  18. this.objects = this.obj.instance.objects;
  19. this.physics = this.obj.instance.physics;
  20. this.dmgMult = blueprint.dmgMult;
  21. (blueprint.spells || []).forEach(s => this.addSpell(s, -1));
  22. if (blueprint.rotation) {
  23. const { duration, spells } = blueprint.rotation;
  24. this.rotation = {
  25. currentTick: 0,
  26. duration,
  27. spells
  28. };
  29. }
  30. delete blueprint.spells;
  31. //External helpers that should form part of the component
  32. this.getSpellToCast = rotationManager.getSpellToCast.bind(null, this);
  33. this.getFurthestRange = rotationManager.getFurthestRange.bind(null, this);
  34. this.resetRotation = rotationManager.resetRotation.bind(null, this);
  35. },
  36. transfer: function () {
  37. let spells = this.spells;
  38. this.spells = [];
  39. spells.forEach(s => this.addSpell(s, -1));
  40. },
  41. die: function () {
  42. this.stopCasting();
  43. this.spells.forEach(s => {
  44. let reserve = s.manaReserve;
  45. if (reserve && reserve.percentage && s.active) {
  46. let reserveEvent = {
  47. spell: s.name,
  48. reservePercent: reserve.percentage
  49. };
  50. this.obj.fireEvent('onBeforeReserveMana', reserveEvent);
  51. this.obj.stats.addStat('manaReservePercent', -reserveEvent.reservePercent);
  52. }
  53. s.die();
  54. }, this);
  55. },
  56. simplify: function (self) {
  57. if (!self)
  58. return null;
  59. let s = {
  60. type: this.type,
  61. closestRange: this.closestRange,
  62. furthestRange: this.furthestRange
  63. };
  64. let spells = this.spells;
  65. if (spells.length && spells[0].obj)
  66. spells = spells.map(f => f.simplify());
  67. s.spells = spells;
  68. return s;
  69. },
  70. addSpell: function (options, spellId) {
  71. if (!options.type) {
  72. options = {
  73. type: options
  74. };
  75. }
  76. let type = options.type[0].toUpperCase() + options.type.substr(1);
  77. let typeTemplate = {
  78. type: type,
  79. template: null
  80. };
  81. this.obj.instance.eventEmitter.emit('onBeforeGetSpellTemplate', typeTemplate);
  82. if (!typeTemplate.template)
  83. typeTemplate.template = require('../config/spells/spell' + type);
  84. let builtSpell = extend({}, spellTemplate, typeTemplate.template, options);
  85. builtSpell.obj = this.obj;
  86. builtSpell.baseDamage = builtSpell.damage || 0;
  87. builtSpell.damage += (options.damageAdd || 0);
  88. if (options.damage)
  89. builtSpell.damage = options.damage;
  90. if (builtSpell.animation) {
  91. let animation = null;
  92. let sheetName = this.obj.sheetName || '../../../images/characters.png';
  93. let animationName = builtSpell.animation;
  94. if (sheetName === 'mobs')
  95. animation = animations.mobs;
  96. else if (sheetName === 'bosses')
  97. animation = animations.bosses;
  98. else if (sheetName.indexOf('/') > -1)
  99. animation = animations.mobs[sheetName];
  100. else
  101. animation = animations.classes;
  102. if ((animation) && (animation[this.obj.cell]) && (animation[this.obj.cell][animationName])) {
  103. builtSpell.animation = extend({}, animation[this.obj.cell][animationName]);
  104. builtSpell.animation.name = animationName;
  105. } else
  106. builtSpell.animation = null;
  107. }
  108. if (!builtSpell.castOnDeath && builtSpell.range) {
  109. if (this.closestRange === -1 || builtSpell.range < this.closestRange)
  110. this.closestRange = builtSpell.range;
  111. if (this.furthestRange === -1 || builtSpell.range > this.furthestRange)
  112. this.furthestRange = builtSpell.range;
  113. }
  114. if ((!options.has('id')) && (spellId === -1)) {
  115. spellId = 0;
  116. this.spells.forEach(function (s) {
  117. if (s.id >= spellId)
  118. spellId = s.id + 1;
  119. });
  120. }
  121. builtSpell.id = !options.has('id') ? spellId : options.id;
  122. //Mobs don't get abilities put on CD when they learn them
  123. if (!this.obj.mob && builtSpell.cdMax)
  124. builtSpell.cd = builtSpell.cdMax;
  125. this.spells.push(builtSpell);
  126. this.spells.sort(function (a, b) {
  127. return (a.id - b.id);
  128. });
  129. builtSpell.calcDps(null, true);
  130. if (builtSpell.init)
  131. builtSpell.init();
  132. if (this.obj.player)
  133. this.obj.syncer.setArray(true, 'spellbook', 'getSpells', builtSpell.simplify());
  134. return builtSpell.id;
  135. },
  136. addSpellFromRune: function (runeSpell, spellId) {
  137. let type = runeSpell.type;
  138. let playerSpell = playerSpells.spells.find(s => (s.name.toLowerCase() === runeSpell.name.toLowerCase())) || playerSpells.spells.find(s => (s.type === type));
  139. let playerSpellConfig = playerSpellsConfig.spells[runeSpell.name.toLowerCase()] || playerSpellsConfig.spells[runeSpell.type];
  140. if (!playerSpellConfig)
  141. return -1;
  142. if (!runeSpell.rolls)
  143. runeSpell.rolls = {};
  144. runeSpell.values = {};
  145. let builtSpell = extend({
  146. type: runeSpell.type,
  147. values: {}
  148. }, playerSpell, playerSpellConfig, runeSpell);
  149. for (let r in builtSpell.random) {
  150. let range = builtSpell.random[r];
  151. let roll = runeSpell.rolls[r] || 0;
  152. runeSpell.rolls[r] = roll;
  153. let int = r.indexOf('i_') === 0;
  154. let val = range[0] + ((range[1] - range[0]) * roll);
  155. if (int) {
  156. val = Math.round(val);
  157. r = r.replace('i_', '');
  158. } else
  159. val = ~~(val * 100) / 100;
  160. builtSpell[r] = val;
  161. builtSpell.values[r] = val;
  162. runeSpell.values[r] = val;
  163. }
  164. if (runeSpell.properties) {
  165. for (let p in runeSpell.properties)
  166. builtSpell[p] = runeSpell.properties[p];
  167. }
  168. if (runeSpell.cdMult)
  169. builtSpell.cdMax *= runeSpell.cdMult;
  170. delete builtSpell.rolls;
  171. delete builtSpell.random;
  172. return this.addSpell(builtSpell, spellId);
  173. },
  174. calcDps: function () {
  175. this.spells.forEach(s => s.calcDps());
  176. },
  177. removeSpellById: function (id) {
  178. let exists = this.spells.spliceFirstWhere(s => (s.id === id));
  179. if (exists) {
  180. if (exists.manaReserve && exists.active) {
  181. let reserve = exists.manaReserve;
  182. if (reserve.percentage) {
  183. let reserveEvent = {
  184. spell: exists.name,
  185. reservePercent: reserve.percentage
  186. };
  187. this.obj.fireEvent('onBeforeReserveMana', reserveEvent);
  188. this.obj.stats.addStat('manaReservePercent', -reserveEvent.reservePercent);
  189. }
  190. }
  191. if (exists.unlearn)
  192. exists.unlearn();
  193. this.obj.syncer.setArray(true, 'spellbook', 'removeSpells', id);
  194. }
  195. },
  196. queueAuto: function (action, spell) {
  197. if (!action.auto || spell.autoActive)
  198. return true;
  199. this.spells.forEach(s => s.setAuto(null));
  200. spell.setAuto({
  201. target: action.target,
  202. spell: spell.id
  203. });
  204. },
  205. getTarget: function (spell, action) {
  206. let target = action.target;
  207. //Cast on self?
  208. if (action.self) {
  209. if (spell.targetGround) {
  210. target = {
  211. x: this.obj.x,
  212. y: this.obj.y
  213. };
  214. } else if (spell.spellType === 'buff')
  215. target = this.obj;
  216. }
  217. if (!spell.aura && !spell.targetGround) {
  218. //Did we pass in the target id?
  219. if (target && !target.id) {
  220. target = this.objects.objects.find(o => o.id === target);
  221. if (!target)
  222. return null;
  223. }
  224. if (target === this.obj && spell.noTargetSelf)
  225. target = null;
  226. if (!target || !target.player) {
  227. if (spell.autoTargetFollower) {
  228. target = this.spells.find(s => s.minions && s.minions.length > 0);
  229. if (target)
  230. target = target.minions[0];
  231. else
  232. return null;
  233. }
  234. }
  235. if (spell.spellType === 'buff' || spell.spellType === 'heal') {
  236. if (this.obj.aggro.faction !== target.aggro.faction)
  237. return;
  238. } else if (target.aggro && !this.obj.aggro.canAttack(target)) {
  239. if (this.obj.player)
  240. this.sendAnnouncement("You don't feel like attacking that target");
  241. return;
  242. }
  243. }
  244. if (!spell.targetGround && target && !target.aggro && !spell.aura) {
  245. this.sendAnnouncement("You don't feel like attacking that target");
  246. return;
  247. }
  248. if (spell.aura)
  249. target = this.obj;
  250. return target;
  251. },
  252. canCast: function (action) {
  253. if (!action.has('spell'))
  254. return false;
  255. let spell = this.spells.find(s => (s.id === action.spell));
  256. if (!spell)
  257. return false;
  258. let target = this.getTarget(spell, action);
  259. return spell.canCast(target);
  260. },
  261. cast: function (action, isAuto) {
  262. if (!action.has('spell')) {
  263. const isCasting = this.isCasting();
  264. this.stopCasting();
  265. const consumeTick = isCasting;
  266. return consumeTick;
  267. }
  268. let spell = this.spells.find(s => (s.id === action.spell));
  269. if (!spell)
  270. return false;
  271. action.target = this.getTarget(spell, action);
  272. //If a target has become nonSelectable, we need to stop attacks that are queued/auto
  273. if (!action.target || action.target.nonSelectable)
  274. return false;
  275. action.auto = spell.auto;
  276. let success = true;
  277. if (spell.cd > 0) {
  278. if (!isAuto) {
  279. let type = (spell.auto) ? 'Weapon' : 'Spell';
  280. this.sendAnnouncement(`${type} is on cooldown`);
  281. }
  282. success = false;
  283. } else if (spell.manaCost > this.obj.stats.values.mana) {
  284. if (!isAuto)
  285. this.sendAnnouncement('Insufficient mana to cast spell');
  286. success = false;
  287. } else if (spell.has('range')) {
  288. let distance = Math.max(Math.abs(action.target.x - this.obj.x), Math.abs(action.target.y - this.obj.y));
  289. let range = spell.range;
  290. if ((spell.useWeaponRange) && (this.obj.player)) {
  291. let weapon = this.obj.inventory.findItem(this.obj.equipment.eq.oneHanded) || this.obj.inventory.findItem(this.obj.equipment.eq.twoHanded);
  292. if (weapon)
  293. range = weapon.range || 1;
  294. }
  295. if (distance > range) {
  296. if (!isAuto)
  297. this.sendAnnouncement('Target out of range');
  298. success = false;
  299. }
  300. }
  301. //LoS check
  302. //Null means we don't have LoS and as such, we should move
  303. if (spell.needLos && success) {
  304. if (!this.physics.hasLos(~~this.obj.x, ~~this.obj.y, ~~action.target.x, ~~action.target.y)) {
  305. if (!isAuto)
  306. this.sendAnnouncement('Target not in line of sight');
  307. action.auto = false;
  308. success = null;
  309. }
  310. }
  311. if (!success) {
  312. this.queueAuto(action, spell);
  313. return success;
  314. } else if (!this.queueAuto(action, spell))
  315. return false;
  316. let castSuccess = {
  317. success: true
  318. };
  319. this.obj.fireEvent('beforeCastSpell', castSuccess);
  320. if (!castSuccess.success)
  321. return false;
  322. if (spell.manaReserve) {
  323. let reserve = spell.manaReserve;
  324. if (reserve.percentage) {
  325. let reserveEvent = {
  326. spell: spell.name,
  327. reservePercent: reserve.percentage
  328. };
  329. this.obj.fireEvent('onBeforeReserveMana', reserveEvent);
  330. if (!spell.active) {
  331. if (1 - this.obj.stats.values.manaReservePercent < reserve.percentage) {
  332. this.sendAnnouncement('Insufficient mana to cast spell');
  333. return;
  334. } this.obj.stats.addStat('manaReservePercent', reserveEvent.reservePercent);
  335. } else
  336. this.obj.stats.addStat('manaReservePercent', -reserveEvent.reservePercent);
  337. }
  338. }
  339. if (spell.targetFurthest)
  340. spell.target = this.obj.aggro.getFurthest();
  341. else if (spell.targetRandom)
  342. spell.target = this.obj.aggro.getRandom();
  343. success = spell.castBase(action);
  344. this.stopCasting(spell, true);
  345. if (success) {
  346. spell.consumeMana();
  347. spell.setCd();
  348. }
  349. this.obj.fireEvent('afterCastSpell', {
  350. castSuccess: success,
  351. spell,
  352. action
  353. });
  354. //Null means we didn't fail but are initiating casting
  355. return (success === null || success === true);
  356. },
  357. getClosestRange: function (spellNum) {
  358. if (spellNum)
  359. return this.spells[spellNum].range;
  360. return this.closestRange;
  361. },
  362. getCooldowns: function () {
  363. let cds = [];
  364. this.spells.forEach(
  365. s => cds.push({
  366. cd: s.cd,
  367. cdMax: s.cdMax,
  368. canCast: ((s.manaCost <= this.obj.stats.values.mana) && (s.cd === 0))
  369. }), this);
  370. return cds;
  371. },
  372. update: function () {
  373. let didCast = false;
  374. const isCasting = this.isCasting();
  375. if (this.rotation)
  376. rotationManager.tick(this);
  377. this.spells.forEach(s => {
  378. let auto = s.autoActive;
  379. if (auto) {
  380. if (!auto.target || auto.target.destroyed)
  381. s.setAuto(null);
  382. else if (!isCasting && this.cast(auto, true))
  383. didCast = true;
  384. }
  385. s.updateBase();
  386. if (s.update)
  387. s.update();
  388. });
  389. let callbacks = this.callbacks;
  390. let cLen = callbacks.length;
  391. for (let i = 0; i < cLen; i++) {
  392. let c = callbacks[i];
  393. //If a spellCallback kills a mob he'll unregister his callbacks
  394. if (!c) {
  395. i--;
  396. cLen--;
  397. continue;
  398. }
  399. c.time -= consts.tickTime;
  400. if (c.time <= 0) {
  401. if (c.callback)
  402. c.callback();
  403. if (c.destroyCallback)
  404. c.destroyCallback();
  405. callbacks.splice(i, 1);
  406. i--;
  407. cLen--;
  408. }
  409. }
  410. return didCast || isCasting;
  411. },
  412. registerCallback: function (sourceId, callback, time, destroyCallback, targetId, destroyOnRezone) {
  413. let obj = {
  414. sourceId: sourceId,
  415. targetId: targetId,
  416. callback: callback,
  417. destroyCallback: destroyCallback,
  418. destroyOnRezone: destroyOnRezone,
  419. time: time
  420. };
  421. this.callbacks.push(obj);
  422. return obj;
  423. },
  424. unregisterCallback: function (objId, isTarget) {
  425. let callbacks = this.callbacks;
  426. let cLen = callbacks.length;
  427. for (let i = 0; i < cLen; i++) {
  428. let c = callbacks[i];
  429. if (
  430. (
  431. isTarget &&
  432. c.targetId === objId
  433. ) ||
  434. (
  435. !isTarget &&
  436. c.sourceId === objId
  437. )
  438. ) {
  439. if (c.destroyCallback)
  440. c.destroyCallback();
  441. callbacks.splice(i, 1);
  442. i--;
  443. cLen--;
  444. }
  445. }
  446. },
  447. sendAnnouncement: function (msg) {
  448. process.send({
  449. method: 'events',
  450. data: {
  451. onGetAnnouncement: [{
  452. obj: {
  453. msg: msg
  454. },
  455. to: [this.obj.serverId]
  456. }]
  457. }
  458. });
  459. },
  460. fireEvent: function (event, args) {
  461. let spells = this.spells;
  462. let sLen = spells.length;
  463. for (let i = 0; i < sLen; i++) {
  464. let s = spells[i];
  465. let spellEvents = s.events;
  466. if (spellEvents) {
  467. let callback = spellEvents[event];
  468. if (!callback)
  469. continue;
  470. callback.apply(s, args);
  471. }
  472. if (s.castEvent === event)
  473. s.cast();
  474. }
  475. },
  476. isCasting: function () {
  477. return this.spells.some(s => s.currentAction);
  478. },
  479. stopCasting: function (ignore, skipAuto) {
  480. this.spells.forEach(s => {
  481. if (s === ignore)
  482. return;
  483. if (!skipAuto)
  484. s.setAuto(null);
  485. if (!s.currentAction)
  486. return;
  487. s.castTime = 0;
  488. s.currentAction = null;
  489. if (!ignore || !ignore.castTimeMax)
  490. this.obj.syncer.set(false, null, 'casting', 0);
  491. });
  492. },
  493. destroy: function () {
  494. this.spells.forEach(s => {
  495. if (s.destroy)
  496. s.destroy();
  497. });
  498. },
  499. events: {
  500. beforeMove: function () {
  501. this.stopCasting(null, true);
  502. },
  503. onBeforeUseItem: function () {
  504. this.stopCasting(null, true);
  505. },
  506. clearQueue: function () {
  507. this.stopCasting(null, true);
  508. },
  509. beforeDeath: function () {
  510. this.stopCasting(null, true);
  511. this.spells.forEach(function (s) {
  512. if (!s.castOnDeath)
  513. return;
  514. s.cast();
  515. });
  516. },
  517. beforeRezone: function () {
  518. this.spells.forEach(function (s) {
  519. if (s.active) {
  520. s.active = false;
  521. let reserve = s.manaReserve;
  522. if (reserve && reserve.percentage) {
  523. let reserveEvent = {
  524. spell: s.name,
  525. reservePercent: reserve.percentage
  526. };
  527. this.obj.fireEvent('onBeforeReserveMana', reserveEvent);
  528. this.obj.stats.addStat('manaReservePercent', -reserveEvent.reservePercent);
  529. }
  530. //Make sure to remove the buff from party members
  531. s.updateInactive();
  532. }
  533. }, this);
  534. let callbacks = this.callbacks;
  535. let cLen = callbacks.length;
  536. for (let i = 0; i < cLen; i++) {
  537. let c = callbacks[i];
  538. //If a spellCallback kills a mob he'll unregister his callbacks
  539. //Probably not needed since we aren't supposed to damage mobs in destroyCallback
  540. if (!c) {
  541. i--;
  542. cLen--;
  543. continue;
  544. }
  545. if (c.destroyOnRezone) {
  546. if (c.destroyCallback)
  547. c.destroyCallback();
  548. callbacks.splice(i, 1);
  549. i--;
  550. cLen--;
  551. }
  552. }
  553. }
  554. }
  555. };