Guest User

mods/automagic/scripts.js

a guest
Jan 31st, 2017
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. exports.BattleScripts = {
  4.     moveHit: function (target, pokemon, move, moveData, isSecondary, isSelf) {
  5.             let damage;
  6.             move = this.getMoveCopy(move);
  7.  
  8.             if (!moveData) moveData = move;
  9.             if (!moveData.flags) moveData.flags = {};
  10.             let hitResult = true;
  11.  
  12.             // TryHit events:
  13.             //   STEP 1: we see if the move will succeed at all:
  14.             //   - TryHit, TryHitSide, or TryHitField are run on the move,
  15.             //     depending on move target (these events happen in useMove
  16.             //     or tryMoveHit, not below)
  17.             //   == primary hit line ==
  18.             //   Everything after this only happens on the primary hit (not on
  19.             //   secondary or self-hits)
  20.             //   STEP 2: we see if anything blocks the move from hitting:
  21.             //   - TryFieldHit is run on the target
  22.             //   STEP 3: we see if anything blocks the move from hitting the target:
  23.             //   - If the move's target is a pokemon, TryHit is run on that pokemon
  24.  
  25.             // Note:
  26.             //   If the move target is `foeSide`:
  27.             //     event target = pokemon 0 on the target side
  28.             //   If the move target is `allySide` or `all`:
  29.             //     event target = the move user
  30.             //
  31.             //   This is because events can't accept actual sides or fields as
  32.             //   targets. Choosing these event targets ensures that the correct
  33.             //   side or field is hit.
  34.             //
  35.             //   It is the `TryHitField` event handler's responsibility to never
  36.             //   use `target`.
  37.             //   It is the `TryFieldHit` event handler's responsibility to read
  38.             //   move.target and react accordingly.
  39.             //   An exception is `TryHitSide` as a single event (but not as a normal
  40.             //   event), which is passed the target side.
  41.  
  42.             if (move.target === 'all' && !isSelf) {
  43.                 hitResult = this.singleEvent('TryHitField', moveData, {}, target, pokemon, move);
  44.             } else if ((move.target === 'foeSide' || move.target === 'allySide') && !isSelf) {
  45.                 hitResult = this.singleEvent('TryHitSide', moveData, {}, target.side, pokemon, move);
  46.             } else if (target) {
  47.                 hitResult = this.singleEvent('TryHit', moveData, {}, target, pokemon, move);
  48.             }
  49.             if (!hitResult) {
  50.                 if (hitResult === false) this.add('-fail', target);
  51.                 return false;
  52.             }
  53.  
  54.             if (target && !isSecondary && !isSelf) {
  55.                 if (move.target !== 'all' && move.target !== 'allySide' && move.target !== 'foeSide') {
  56.                     hitResult = this.runEvent('TryPrimaryHit', target, pokemon, moveData);
  57.                     if (hitResult === 0) {
  58.                         // special Substitute flag
  59.                         hitResult = true;
  60.                         target = null;
  61.                     }
  62.                 }
  63.             }
  64.             if (target && isSecondary && !moveData.self) {
  65.                 hitResult = true;
  66.             }
  67.             if (!hitResult) {
  68.                 return false;
  69.             }
  70.  
  71.             if (target) {
  72.                 let didSomething = false;
  73.  
  74.                 damage = this.getDamage(pokemon, target, moveData);
  75.  
  76.                 // getDamage has several possible return values:
  77.                 //
  78.                 //   a number:
  79.                 //     means that much damage is dealt (0 damage still counts as dealing
  80.                 //     damage for the purposes of things like Static)
  81.                 //   false:
  82.                 //     gives error message: "But it failed!" and move ends
  83.                 //   null:
  84.                 //     the move ends, with no message (usually, a custom fail message
  85.                 //     was already output by an event handler)
  86.                 //   undefined:
  87.                 //     means no damage is dealt and the move continues
  88.                 //
  89.                 // basically, these values have the same meanings as they do for event
  90.                 // handlers.
  91.  
  92.                 if ((damage || damage === 0) && !target.fainted) {
  93.                     if (move.noFaint && damage >= target.hp) {
  94.                         damage = target.hp - 1;
  95.                     }
  96.                     damage = this.damage(damage, target, pokemon, move);
  97.                     if (!(damage || damage === 0)) {
  98.                         this.debug('damage interrupted');
  99.                         return false;
  100.                     }
  101.                     didSomething = true;
  102.                 }
  103.                 if (damage === false || damage === null) {
  104.                     if (damage === false && !isSecondary && !isSelf) {
  105.                         this.add('-fail', target);
  106.                     }
  107.                     this.debug('damage calculation interrupted');
  108.                     return false;
  109.                 }
  110.  
  111.                 if (moveData.boosts && !target.fainted) {
  112.                     hitResult = this.boost(moveData.boosts, target, pokemon, move, isSecondary, isSelf);
  113.                     didSomething = didSomething || hitResult;
  114.                 }
  115.                 if (moveData.heal && !target.fainted) {
  116.                     let d = target.heal((this.gen < 5 ? Math.floor : Math.round)(target.maxhp * moveData.heal[0] / moveData.heal[1]));
  117.                     if (!d && d !== 0) {
  118.                         this.add('-fail', target);
  119.                         this.debug('heal interrupted');
  120.                         return false;
  121.                     }
  122.                     this.add('-heal', target, target.getHealth);
  123.                     didSomething = true;
  124.                 }
  125.                 if (moveData.status) {
  126.                     hitResult = target.trySetStatus(moveData.status, pokemon, moveData.ability ? moveData.ability : move);
  127.                     if (!hitResult && move.status) return hitResult;
  128.                     didSomething = didSomething || hitResult;
  129.                 }
  130.                 if (moveData.forceStatus) {
  131.                     hitResult = target.setStatus(moveData.forceStatus, pokemon, move);
  132.                     didSomething = didSomething || hitResult;
  133.                 }
  134.                 if (moveData.volatileStatus) {
  135.                     hitResult = target.addVolatile(moveData.volatileStatus, pokemon, move);
  136.                     didSomething = didSomething || hitResult;
  137.                 }
  138.                 if (moveData.sideCondition) {
  139.                     hitResult = target.side.addSideCondition(moveData.sideCondition, pokemon, move);
  140.                     didSomething = didSomething || hitResult;
  141.                 }
  142.                 if (moveData.weather) {
  143.                     hitResult = this.setWeather(moveData.weather, pokemon, move);
  144.                     didSomething = didSomething || hitResult;
  145.                 }
  146.                 if (moveData.terrain) {
  147.                     hitResult = this.setTerrain(moveData.terrain, pokemon, move);
  148.                     didSomething = didSomething || hitResult;
  149.                 }
  150.                 if (moveData.pseudoWeather) {
  151.                     hitResult = this.addPseudoWeather(moveData.pseudoWeather, pokemon, move);
  152.                     didSomething = didSomething || hitResult;
  153.                 }
  154.                 if (moveData.forceSwitch) {
  155.                     if (this.canSwitch(target.side)) didSomething = true; // at least defer the fail message to later
  156.                 }
  157.                 if (moveData.selfSwitch) {
  158.                     if (this.canSwitch(pokemon.side)) didSomething = true; // at least defer the fail message to later
  159.                 }
  160.                 // Hit events
  161.                 //   These are like the TryHit events, except we don't need a FieldHit event.
  162.                 //   Scroll up for the TryHit event documentation, and just ignore the "Try" part. ;)
  163.                 hitResult = null;
  164.                 if (move.target === 'all' && !isSelf) {
  165.                     if (moveData.onHitField) hitResult = this.singleEvent('HitField', moveData, {}, target, pokemon, move);
  166.                 } else if ((move.target === 'foeSide' || move.target === 'allySide') && !isSelf) {
  167.                     if (moveData.onHitSide) hitResult = this.singleEvent('HitSide', moveData, {}, target.side, pokemon, move);
  168.                 } else {
  169.                     if (moveData.onHit) hitResult = this.singleEvent('Hit', moveData, {}, target, pokemon, move);
  170.                     if (!isSelf && !isSecondary) {
  171.                         this.runEvent('Hit', target, pokemon, move);
  172.                     }
  173.                     if (moveData.onAfterHit) hitResult = this.singleEvent('AfterHit', moveData, {}, target, pokemon, move);
  174.                 }
  175.  
  176.                 if (!hitResult && !didSomething && !moveData.self && !moveData.selfdestruct) {
  177.                     if (!isSelf && !isSecondary) {
  178.                         if (hitResult === false || didSomething === false) this.add('-fail', target);
  179.                     }
  180.                     this.debug('move failed because it did nothing');
  181.                     return false;
  182.                 }
  183.             }
  184.             if (moveData.self) {
  185.                 let selfRoll;
  186.                 if (!isSecondary && moveData.self.boosts) selfRoll = this.random(100);
  187.                 // This is done solely to mimic in-game RNG behaviour. All self drops have a 100% chance of happening but still grab a random number.
  188.                 if (typeof moveData.self.chance === 'undefined' || selfRoll < moveData.self.chance) {
  189.                     this.moveHit(pokemon, pokemon, move, moveData.self, isSecondary, true);
  190.                 }
  191.             }
  192.             if (moveData.secondaries) {
  193.                 let secondaryRoll;
  194.                 let secondaries = this.runEvent('ModifySecondaries', target, pokemon, moveData, moveData.secondaries.slice());
  195.                 for (let i = 0; i < secondaries.length; i++) {
  196.                     secondaryRoll = this.random(100);
  197.                     if (typeof secondaries[i].chance === 'undefined' || secondaryRoll < secondaries[i].chance) {
  198.                         this.moveHit(target, pokemon, move, secondaries[i], true, isSelf);
  199.                         //mod for setup++ start
  200.                         this.runEvent('AfterSecondaryEffect', target, pokemon, moveData);
  201.                         // mod for setup++ end
  202.                     }
  203.                 }
  204.             }
  205.             if (target && target.hp > 0 && pokemon.hp > 0 && moveData.forceSwitch && this.canSwitch(target.side)) {
  206.                 hitResult = this.runEvent('DragOut', target, pokemon, move);
  207.                 if (hitResult) {
  208.                     target.forceSwitchFlag = true;
  209.                 } else if (hitResult === false && move.category === 'Status') {
  210.                     this.add('-fail', target);
  211.                 }
  212.             }
  213.             if (move.selfSwitch && pokemon.hp) {
  214.                 pokemon.switchFlag = move.selfSwitch;
  215.             }
  216.             return damage;
  217.         },
  218. };
Add Comment
Please, Sign In to add comment