SHARE
TWEET

Untitled

a guest Jun 25th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @description Used upon item units like ArachnidMesh.castChargedSkill([skillId]) or directly on the "me" unit me.castChargedSkill(278);
  3.  * @param {int} skillId = undefined
  4.  * @param {int} x = undefined
  5.  * @param {int} y = undefined
  6.  * @return boolean
  7.  * @throws Error
  8.  **/
  9. Unit.prototype.castChargedSkill = function (...args) {
  10.     let skillId, x, y, unit, chargedItem, charge,
  11.         chargedItems = [],
  12.         validCharge = function (itemCharge) {
  13.             return itemCharge.skill === skillId && itemCharge.charges;
  14.         };
  15.  
  16.     switch (args.length) {
  17.     case 0: // item.castChargedSkill()
  18.         break;
  19.     case 1:
  20.         if (args[0] instanceof Unit) { // hellfire.castChargedSkill(monster);
  21.             unit = args[0];
  22.         } else {
  23.             skillId = args[0];
  24.         }
  25.  
  26.         break;
  27.     case 2:
  28.         if (typeof args[0] === 'number') {
  29.             if (args[1] instanceof Unit) { // me.castChargedSkill(skillId,unit)
  30.                 [skillId, unit] = [...args];
  31.             } else if (typeof args[1] === 'number') { // item.castChargedSkill(x,y)
  32.                 [x, y] = [...args];
  33.             }
  34.         } else {
  35.             throw new Error(' invalid arguments, expected (skillId, unit) or (x, y)');
  36.         }
  37.  
  38.         break;
  39.     case 3:
  40.         // If all arguments are numbers
  41.         if (typeof args[0] === 'number' && typeof args[1] === 'number' && typeof args[2] === 'number') {
  42.             [skillId, x, y] = [...args];
  43.         }
  44.  
  45.         break;
  46.     default:
  47.         throw new Error("invalid arguments, expected 'me' object or 'item' unit");
  48.     }
  49.  
  50.     // Charged skills can only be casted on x, y coordinates
  51.     unit && ([x, y] = [unit.x, unit.y]);
  52.  
  53.     if (this !== me && this.type === 4) {
  54.         throw Error("invalid arguments, expected 'me' object or 'item' unit");
  55.     }
  56.  
  57.     if (this === me) { // Called the function the unit, me.
  58.         if (!skillId) {
  59.             throw Error('Must supply skillId on me.castChargedSkill');
  60.         }
  61.  
  62.         chargedItems = [];
  63.  
  64.         this.getItems(-1) // Item must be in inventory, or a charm in inventory
  65.             .filter(item => item && (item.location === 1 || (item.location === 3 && item.itemType === 82)))
  66.             .forEach(function (item) {
  67.                 let stats = item.getStat(-2);
  68.  
  69.                 if (!stats.hasOwnProperty(204)) {
  70.                     stats = stats[204].filter(validCharge); // This is where error occurs, 1204
  71.                     stats.length && chargedItems.push({
  72.                         charge: stats.first(),
  73.                         item: item
  74.                     });
  75.                 }
  76.             });
  77.  
  78.         if (chargedItems.length === 0) {
  79.             throw Error("Don't have the charged skill (" + skillId + "), or not enough charges");
  80.         }
  81.  
  82.         chargedItem = chargedItems.sort((a, b) => a.charge.level - b.charge.level).first().item;
  83.  
  84.         return chargedItem.castChargedSkill.apply(chargedItem, args);
  85.     } else if (this.type === 4) {
  86.         charge = this.getStat(-2)[204]; // WARNING. Somehow this gives duplicates
  87.  
  88.         if (!charge) {
  89.             throw Error('No charged skill on this item');
  90.         }
  91.  
  92.         if (skillId) {
  93.             charge = charge.filter(item => (skillId && item.skill === skillId) && !!item.charges); // Filter out all other charged skills
  94.         } else if (charge.length > 1) {
  95.             throw new Error('multiple charges on this item without a given skillId');
  96.         }
  97.  
  98.         charge = charge.first();
  99.  
  100.         if (charge) {
  101.             // Setting skill on hand
  102.             if (!Config.PacketCasting || Config.PacketCasting === 1 && skillId !== 54) {
  103.                 return Skill.cast(skillId, 0, x || me.x, y || me.y, this); // Non packet casting
  104.             }
  105.  
  106.             // Packet casting
  107.             sendPacket(1, 0x3c, 2, charge.skill, 1, 0x0, 1, 0x00, 4, this.gid);
  108.             // No need for a delay, since its TCP, the server recv's the next statement always after the send cast skill packet
  109.  
  110.             // The result of "successfully" casted is different, so we cant wait for it here. We have to assume it worked
  111.             sendPacket(1, 0x0C, 2, x || me.x, 2, y || me.y); // Cast the skill
  112.  
  113.             return true;
  114.         }
  115.     }
  116.  
  117.     return false;
  118. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top