You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

321 lines
7.2 KiB

  1. let generatorStats = require('../items/generators/stats');
  2. module.exports = {
  3. type: 'equipment',
  4. eq: {},
  5. doAutoEq: true,
  6. init: function (blueprint) {
  7. },
  8. transfer: function () {
  9. if (this.eqTransfer) {
  10. this.eq = this.eqTransfer;
  11. delete this.eqTransfer;
  12. }
  13. },
  14. simplify: function (self) {
  15. return {
  16. type: 'equipment',
  17. eq: {},
  18. eqTransfer: this.eq
  19. };
  20. },
  21. autoEquip: function (itemId) {
  22. if (!this.doAutoEq)
  23. return;
  24. let stats = this.obj.stats.values;
  25. let item = this.obj.inventory.findItem(itemId);
  26. if (!item)
  27. return;
  28. else if ((!item.slot) || (item.material) || (item.quest) || (item.ability) || (!this.obj.inventory.canEquipItem(item))) {
  29. item.eq = false;
  30. return;
  31. }
  32. let currentEqId = this.eq[item.slot];
  33. if (currentEqId === null) {
  34. this.equip(itemId);
  35. return true;
  36. }
  37. },
  38. equip: function (itemId) {
  39. let slot = null;
  40. if (typeof (itemId) === 'object') {
  41. slot = itemId.slot;
  42. itemId = itemId.itemId;
  43. }
  44. let item = this.obj.inventory.findItem(itemId);
  45. if (!item)
  46. return;
  47. else if ((!item.slot) || (item.material) || (item.quest) || (item.ability) || (!this.obj.inventory.canEquipItem(item))) {
  48. item.eq = false;
  49. return;
  50. }
  51. if (!slot)
  52. slot = item.equipSlot || item.slot;
  53. if (slot === 'twoHanded') {
  54. let currentEqId = this.eq.offHand;
  55. if (currentEqId !== null)
  56. this.unequip(currentEqId);
  57. slot = 'oneHanded';
  58. } else if (slot === 'offHand') {
  59. let currentEqId = this.eq.oneHanded;
  60. if (currentEqId !== null) {
  61. let currentEq = this.obj.inventory.findItem(currentEqId);
  62. if ((currentEq !== null) && (currentEq.slot === 'twoHanded'))
  63. this.unequip(currentEqId);
  64. }
  65. }
  66. let equipMsg = {
  67. success: true,
  68. item: item
  69. };
  70. this.obj.fireEvent('beforeEquipItem', equipMsg);
  71. if (!equipMsg.success) {
  72. this.obj.instance.syncer.queue('onGetMessages', {
  73. id: this.obj.id,
  74. messages: [{
  75. class: 'color-redA',
  76. message: equipMsg.msg || 'you cannot equip that item',
  77. type: 'info'
  78. }]
  79. }, [this.obj.serverId]);
  80. return;
  81. }
  82. delete item.pos;
  83. this.obj.syncer.setArray(true, 'inventory', 'getItems', item);
  84. if (slot === 'finger') {
  85. let f1 = (this.eq['finger-1'] !== null);
  86. let f2 = (this.eq['finger-2'] !== null);
  87. if ((f1) && (f2))
  88. slot = 'finger-1';
  89. else if (!f1)
  90. slot = 'finger-1';
  91. else if (!f2)
  92. slot = 'finger-2';
  93. }
  94. let spellId = null;
  95. let currentEqId = this.eq[slot];
  96. let currentEq = this.obj.inventory.findItem(currentEqId);
  97. if (currentEq === item)
  98. return;
  99. if (currentEqId !== null) {
  100. spellId = currentEq.spellId;
  101. this.unequip(currentEqId);
  102. }
  103. let stats = item.stats;
  104. for (let s in stats) {
  105. let val = stats[s];
  106. this.obj.stats.addStat(s, val);
  107. }
  108. (item.implicitStats || []).forEach(function (s) {
  109. this.obj.stats.addStat(s.stat, s.value);
  110. }, this);
  111. item.eq = true;
  112. this.eq[slot] = itemId;
  113. item.equipSlot = slot;
  114. this.obj.spellbook.calcDps();
  115. if ((!this.obj.mob) || (item.ability)) {
  116. if (item.spell)
  117. this.obj.inventory.learnAbility(itemId, item.runeSlot);
  118. else {
  119. let result = item;
  120. if (item.effects) {
  121. result = extend(true, {}, item);
  122. result.effects = result.effects.map(e => ({
  123. factionId: e.factionId,
  124. text: e.text,
  125. properties: e.properties
  126. }));
  127. let reputation = this.obj.reputation;
  128. if (result.factions) {
  129. result.factions = result.factions.map(function (f) {
  130. let faction = reputation.getBlueprint(f.id);
  131. let factionTier = reputation.getTier(f.id);
  132. let noEquip = null;
  133. if (factionTier < f.tier)
  134. noEquip = true;
  135. return {
  136. name: faction.name,
  137. tier: f.tier,
  138. tierName: ['Hated', 'Hostile', 'Unfriendly', 'Neutral', 'Friendly', 'Honored', 'Revered', 'Exalted'][f.tier],
  139. noEquip: noEquip
  140. };
  141. }, this);
  142. }
  143. }
  144. this.obj.syncer.setArray(true, 'inventory', 'getItems', result);
  145. }
  146. }
  147. this.obj.fireEvent('afterEquipItem', item);
  148. },
  149. unequip: function (itemId) {
  150. let item = itemId;
  151. let slot = null;
  152. if (typeof (itemId) === 'object') {
  153. slot = itemId.slot;
  154. itemId = itemId.itemId;
  155. }
  156. if (item.id === null)
  157. item = this.obj.inventory.findItem(itemId);
  158. if (!item)
  159. return;
  160. let stats = item.stats;
  161. delete item.eq;
  162. delete this.eq[item.equipSlot];
  163. delete item.equipSlot;
  164. for (let s in stats) {
  165. let val = stats[s];
  166. this.obj.stats.addStat(s, -val);
  167. }
  168. (item.implicitStats || []).forEach(function (s) {
  169. this.obj.stats.addStat(s.stat, -s.value);
  170. }, this);
  171. this.obj.inventory.setItemPosition(itemId);
  172. if (item.spell) {
  173. item.eq = true;
  174. this.obj.inventory.unlearnAbility(itemId, item.runeSlot);
  175. } else if (!item.effects)
  176. this.obj.syncer.setArray(true, 'inventory', 'getItems', item);
  177. else {
  178. let result = extend(true, {}, item);
  179. result.effects = result.effects.map(e => ({
  180. factionId: e.factionId,
  181. text: e.text,
  182. properties: e.properties
  183. }));
  184. let reputation = this.obj.reputation;
  185. if (result.factions) {
  186. result.factions = result.factions.map(function (f) {
  187. let faction = reputation.getBlueprint(f.id);
  188. let factionTier = reputation.getTier(f.id);
  189. let noEquip = null;
  190. if (factionTier < f.tier)
  191. noEquip = true;
  192. return {
  193. name: faction.name,
  194. tier: f.tier,
  195. tierName: ['Hated', 'Hostile', 'Unfriendly', 'Neutral', 'Friendly', 'Honored', 'Revered', 'Exalted'][f.tier],
  196. noEquip: noEquip
  197. };
  198. }, this);
  199. }
  200. this.obj.syncer.setArray(true, 'inventory', 'getItems', result);
  201. }
  202. this.obj.spellbook.calcDps();
  203. this.obj.fireEvent('afterUnequipItem', item);
  204. },
  205. unequipAll: function () {
  206. let eq = this.eq;
  207. Object.keys(this.eq).forEach(function (slot) {
  208. this.unequip(eq[slot]);
  209. }, this);
  210. },
  211. unequipAttrRqrGear: function () {
  212. let inventory = this.obj.inventory;
  213. let eq = this.eq;
  214. Object.keys(this.eq).forEach(function (slot) {
  215. let itemId = eq[slot];
  216. let item = inventory.findItem(itemId);
  217. if (!item)
  218. return;
  219. let errors = inventory.equipItemErrors(item);
  220. if (errors.length > 0) {
  221. this.unequip(itemId);
  222. let message = ({
  223. int: `You suddenly feel too stupid to wear your ${item.name}`,
  224. str: `Your weak body can no longer equip your ${item.name}`,
  225. dex: `Your sluggish physique cannot possibly equip your ${item.name}`
  226. })[errors[0]];
  227. this.obj.instance.syncer.queue('onGetMessages', {
  228. id: this.obj.id,
  229. messages: [{
  230. class: 'color-redA',
  231. message: message,
  232. type: 'rep'
  233. }]
  234. }, [this.obj.serverId]);
  235. }
  236. }, this);
  237. },
  238. unequipFactionGear: function (factionId, tier) {
  239. let inventory = this.obj.inventory;
  240. let eq = this.eq;
  241. Object.keys(this.eq).forEach(function (slot) {
  242. let itemId = eq[slot];
  243. let item = inventory.findItem(itemId);
  244. let factions = item.factions;
  245. if (!factions)
  246. return;
  247. let findFaction = factions.find(f => f.id === factionId);
  248. if (!findFaction)
  249. return;
  250. if (findFaction.tier > tier) {
  251. this.unequip(itemId);
  252. this.obj.instance.syncer.queue('onGetMessages', {
  253. id: this.obj.id,
  254. messages: [{
  255. class: 'color-redA',
  256. message: 'You unequip your ' + item.name + ' as it zaps you.',
  257. type: 'rep'
  258. }]
  259. }, [this.obj.serverId]);
  260. }
  261. }, this);
  262. }
  263. };