jeneeus

PreemptiveEvent

Nov 1st, 2015
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //==============================================================================
  2. // ** Preemptive-Surprise Event Battle
  3. // by: Jeneeus Guruman
  4. //==============================================================================
  5.  
  6. var Jene = Jene || {};
  7.  
  8. /*:
  9.  * @plugindesc Preemptive-Surprise Event Battle v1.1.0
  10.  * @author Jeneeus Guruman
  11.  *
  12.  * @param preemptiveSideChance
  13.  * @desc Chances by percentage on having preemptive when attacking sides. Negative numbers will use the default formula.
  14.  * @default -1
  15.  *
  16.  * @param surprisedSideChance
  17.  * @desc Chances by percentage on having surprised when attacking sides. Negative numbers will use the default formula.
  18.  * @default -1
  19.  *
  20.  * @param preemptiveSurpriseSwitch
  21.  * @desc Enable to use this plugin. 0 value makes it always enabled. Negative values disables instead.
  22.  * @default 0
  23.  *
  24.  * @param preemptiveSurpriseTag
  25.  * @desc The note tag of the event that will enable to use the preemptive-surprise function.
  26.  * @default <enemy>
  27.  *
  28.  * @param stopEvent
  29.  * @desc Enable to stop movements while the current event runs.
  30.  * 0 = disable; -1 = tagged events; -2 = all; 1+ = switch #
  31.  * @default -1
  32.  *
  33.  * @help
  34.  *
  35.  *     This plugin allows to make evented encounters have preemptive and
  36.  *   surprise encounters.
  37.  *
  38.  *   How to use:
  39.  *
  40.  *     * Plug-n-play
  41.  *     * Not compatible and has problems with the following:
  42.  *       - Any 8-directional movement plugins
  43.  *       - Any pixel movement plugins
  44.  *     * Not recommended if followers are enabled.
  45.  *     * "Encounter Half" and "Encounter None" party abilities do not work
  46.  *     for evented encounters but "Raise Preemptive" and "Cancel Surprise"
  47.  *     does work.
  48.  *
  49.  *   Changelog:
  50.  *
  51.  *     * v1.1.1: Renamed parameters to be readable easily.
  52.  *     * v1.1.0: Added "stopEvent" parameter to help stop events from
  53.  *     moving while the current event is running.
  54.  */
  55.  
  56. parameters = PluginManager.parameters('PreemptiveEvent');
  57.  
  58. Jene.preemptiveSideChance = Number(parameters['preemptiveSideChance']);
  59. Jene.surprisedSideChance = Number(parameters['surprisedSideChance']);
  60. Jene.preemptiveSurpriseSwitch = Number(parameters['preemptiveSurpriseSwitch']);
  61. Jene.preemptiveSurpriseTag = String(parameters['preemptiveSurpriseTag']);
  62. Jene.stopEvent = Number(parameters['stopEvent']);
  63.  
  64. Jene.battleManagerOnEncounter = BattleManager.onEncounter;
  65.  
  66. BattleManager.onEncounter = function() {
  67.     var psbeEnable = true;
  68.     if (Jene.preemptiveSurpriseSwitch > 0) {
  69.       psbeEnable = $gameSwitches.value(Jene.preemptiveSurpriseSwitch);
  70.     }
  71.     else if (Jene.preemptiveSurpriseSwitch < 0) {
  72.       psbeEnable = !$gameSwitches.value(Jene.preemptiveSurpriseSwitch * -1);
  73.     }
  74.     if (psbeEnable) {
  75.       this._preemptive = $gameTemp.psNum.call($gameTemp) > 0;
  76.       this._surprise = $gameTemp.psNum.call($gameTemp) < 0;
  77.     }
  78.     else {
  79.       Jene.battleManagerOnEncounter.call(this);
  80.     }
  81. };
  82.  
  83. Jene.battleManagerUpdateBattleEnd = BattleManager.updateBattleEnd;
  84.  
  85. BattleManager.updateBattleEnd = function() {
  86.     $gameTemp.setPsNum.call(0);
  87.     Jene.battleManagerUpdateBattleEnd.call(this);
  88. };
  89.  
  90. Jene.gameTempPreemptiveInitalize = Game_Temp.prototype.initialize;
  91.  
  92. Game_Temp.prototype.initialize = function() {
  93.     Jene.gameTempPreemptiveInitalize.call(this);
  94.     this._psNum = 0;
  95. };
  96.  
  97. Game_Temp.prototype.psNum = function() {
  98.     return this._psNum;
  99. };
  100.  
  101. Game_Temp.prototype.setPsNum = function(psNum) {
  102.     this._psNum = psNum;
  103. };
  104.  
  105. Jene.gameCharacterBaseInitMembers = Game_CharacterBase.prototype.initMembers;
  106.  
  107. Game_CharacterBase.prototype.initMembers = function() {
  108.     Jene.gameCharacterBaseInitMembers.call(this);
  109.     this._prevDirection = 2;
  110. };
  111.  
  112. Game_CharacterBase.prototype.x = function() {
  113.     return this._x;
  114. };
  115.  
  116. Game_CharacterBase.prototype.y = function() {
  117.     return this._y;
  118. };
  119.  
  120. Game_CharacterBase.prototype.prevDirection = function() {
  121.     return this._prevDirection;
  122. };
  123.  
  124. Jene.gameCharacterBaseSetDirection = Game_CharacterBase.prototype.setDirection;
  125.  
  126. Game_CharacterBase.prototype.setDirection = function(d) {
  127.   if (!this.isDirectionFixed() && this._direction !== 0) {
  128.     this._prevDirection = this._direction;
  129.   }
  130.     Jene.gameCharacterBaseSetDirection.call(this, d);
  131. };
  132.  
  133. Jene.gameEventUpdateSelfMovement = Game_Event.prototype.updateSelfMovement;
  134.  
  135. Game_Event.prototype.updateSelfMovement = function() {
  136.   var disableMove = false;
  137.   switch (Jene.stopEvent) {
  138.   case -2:
  139.     disableMove = $gameMap.isEventRunning();
  140.     break;
  141.   case -1:
  142.     disableMove = $gameMap.isEventRunning() && this.event().note.indexOf(Jene.preemptiveSurpriseTag) >= 0;
  143.     break;
  144.   case 0:
  145.     disableMove = false;
  146.     break;
  147.   default:
  148.     disableMove = $gameSwitches.value(Jene.stopEvent);
  149.     break;
  150.   }
  151.   if (!disableMove) {
  152.     Jene.gameEventUpdateSelfMovement.call(this);
  153.   }
  154. };
  155.  
  156. Jene.gameInterpreterCommand301 = Game_Interpreter.prototype.command301;
  157.  
  158. Game_Interpreter.prototype.command301 = function() {
  159.     var psbeEnable = true;
  160.     if (Jene.preemptiveSurpriseSwitch > 0) {
  161.         psbeEnable = $gameSwitches.value(Jene.preemptiveSurpriseSwitch);
  162.     }
  163.     else if (Jene.preemptiveSurpriseSwitch < 0) {
  164.         psbeEnable = $gameSwitches.value(Jene.preemptiveSurpriseSwitch * -1);
  165.     }
  166.     if (psbeEnable) {
  167.         if (!$gameParty.inBattle()) {
  168.             var troopId;
  169.             if (this._params[0] === 0) {  // Direct designation
  170.                 troopId = this._params[1];
  171.             } else if (this._params[0] === 1) {  // Designation with a variable
  172.                 troopId = $gameVariables.value(this._params[1]);
  173.             } else {  // Same as Random Encounter
  174.                 troopId = $gamePlayer.makeEncounterTroopId();
  175.             }
  176.             if ($dataTroops[troopId]) {
  177.                   this.preemptiveOrSurprise();
  178.                 BattleManager.setup(troopId, this._params[2], this._params[3]);
  179.                 BattleManager.setEventCallback(function(n) {
  180.                     this._branch[this._indent] = n;
  181.                 }.bind(this));
  182.               BattleManager.onEncounter();
  183.                 SceneManager.push(Scene_Battle);
  184.             }
  185.         }
  186.     }
  187.     else {
  188.         Jene.gameInterpreterCommand301.call(this);
  189.     }
  190.     return true;
  191. };
  192.  
  193. Game_Interpreter.prototype.preemptiveOrSurprise = function() {
  194.     var enemy = this.character(0);
  195.     var player = this.character(-1);
  196.     var enemyEvent = enemy.event.call(this);
  197.     var playerDirection = player.direction.call(player);
  198.     var playerPrevDirection = player.prevDirection.call(player);
  199.     var enemyDirection = enemy.direction.call(enemy);
  200.     var enemyPrevDirection = enemy.prevDirection.call(enemy);
  201.     if (enemyEvent.note.indexOf(Jene.preemptiveSurpriseTag) >= 0) {
  202.       if (playerDirection === enemyPrevDirection) {
  203.         switch (playerDirection) {
  204.         case 2:
  205.           if (player.y < enemy.y) {
  206.             $gameTemp.setPsNum(1);
  207.           }
  208.           break;
  209.         case 4:
  210.           if (player.x > enemy.x) {
  211.             $gameTemp.setPsNum(1);
  212.           }
  213.           break;
  214.         case 6:
  215.           if (player.x < enemy.x) {
  216.             $gameTemp.setPsNum(1);
  217.           }
  218.           break;
  219.         case 8:
  220.           if (player.y > enemy.y) {
  221.             $gameTemp.setPsNum(1);
  222.           }
  223.           break;
  224.         }
  225.       }
  226.       if (playerDirection === enemyDirection && !$gameParty.hasCancelSurprise.call($gameParty)) {
  227.         switch (enemyDirection) {
  228.         case 2:
  229.           if (enemy.y < player.y) {
  230.             $gameTemp.setPsNum(-1);
  231.           }
  232.           break;
  233.         case 4:
  234.           if (enemy.x > player.x) {
  235.             $gameTemp.setPsNum(-1);
  236.           }
  237.           break;
  238.         case 6:
  239.           if (enemy.x < player.x) {
  240.             $gameTemp.setPsNum(-1);
  241.           }
  242.           break;
  243.         case 8:
  244.           if (enemy.y > player.y) {
  245.             $gameTemp.setPsNum(-1);
  246.           }
  247.           break;
  248.         }
  249.       }
  250.       if (10 - playerDirection !== enemyPrevDirection) {
  251.         var chance = false;
  252.         if (Jene.preemptiveSideChance < 0) {
  253.           chance = Math.random() < $gameParty.ratePreemptive();
  254.         }
  255.         else {
  256.           chance = Math.random() < (Jene.preemptiveSideChance * ($gameParty.hasRaisePreemptive() ? 4 : 1)) / 100.0;
  257.         }
  258.         switch (playerDirection) {
  259.         case 2:
  260.           if (player.y < enemy.y && chance) {
  261.             $gameTemp.setPsNum(1);
  262.             break;
  263.           }
  264.         case 4:
  265.           if (player.x > enemy.x && chance) {
  266.             $gameTemp.setPsNum(1);
  267.             break;
  268.           }
  269.         case 6:
  270.           if (player.x < enemy.x && chance) {
  271.             $gameTemp.setPsNum(1);
  272.             break;
  273.           }
  274.         case 8:
  275.           if (player.y > enemy.y && chance) {
  276.             $gameTemp.setPsNum(1);
  277.             break;
  278.           }
  279.         }
  280.       }
  281.       if (10 - enemyDirection !== playerDirection && !$gameParty.hasCancelSurprise.call($gameParty)) {
  282.         var chance = false;
  283.         if (Jene.surprisedSideChance < 0) {
  284.           chance = Math.random() < $gameParty.rateSurprise();
  285.         }
  286.         else {
  287.           chance = Math.random() < Jene.surprisedSideChance / 100.0;
  288.         }
  289.         switch (enemyDirection) {
  290.         case 2:
  291.           if (player.y > enemy.y && chance) {
  292.             $gameTemp.setPsNum(-1);
  293.             break;
  294.           }
  295.         case 4:
  296.           if (player.x < enemy.x && chance) {
  297.             $gameTemp.setPsNum(-1);
  298.             break;
  299.           }
  300.         case 6:
  301.           if (player.x > enemy.x && chance) {
  302.             $gameTemp.setPsNum(-1);
  303.             break;
  304.           }
  305.         case 8:
  306.           if (player.y < enemy.y && chance) {
  307.             $gameTemp.setPsNum(-1);
  308.             break;
  309.           }
  310.         }
  311.       }
  312.     }
  313.   };
Advertisement
Add Comment
Please, Sign In to add comment