Advertisement
Guest User

SmartPath mod 1

a guest
Oct 26th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Smart Pathfinding
  3. // by Shaz
  4. // Last Updated: 2015.10.26
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Allows events or players to do smart Pathfinding
  9.  * @author Shaz
  10.  *
  11.  * @help
  12.  *
  13.  * Plugin Command:
  14.  *  SmartPath eventId1 eventId2      # Makes event 1 find path to event 2
  15.  *  SmartPath eventId x y            # Makes event find a path to location x, y
  16.  *  SmartPath eventId cancel         # Cancel Pathfinding for event
  17.  *
  18.  *  event = number for specific event
  19.  *  event = 0 for "this" event
  20.  *  event = -1 for player
  21.  *  event = $gameVariables.value(x) to get the event id from variable x
  22.  *
  23.  *  x, y = coordinates or $gameVariables.value(#) to use a variable coordinate
  24.  *
  25.  * Update by DragoonKain:
  26.  *   Use Game_Interpreter.findEvent to allow specifying events by name.
  27.  *   Added Wait, Auto_Cancel and Auto_Cancel_Near.
  28.  */
  29.  
  30. (function() {
  31.   var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  32.   Game_Interpreter.prototype.pluginCommand = function(command, args) {
  33.     _Game_Interpreter_pluginCommand.call(this, command, args);
  34.  
  35.     if (command.toUpperCase() === 'SMARTPATH') {
  36.       subject = this.findEvent
  37.         ? this.findEvent(args[0])
  38.         : this.character(eval(args[0]));
  39.  
  40.       if (args[1].toUpperCase() === 'CANCEL') {
  41.         subject.clearTarget();
  42.       } else if (args[1].toUpperCase() === 'WAIT') {
  43.         if (subject._target && !subject.isOnTarget(subject._targetX, subject._targetY)) {
  44.           this.wait(20);
  45.           this.jumpTo(this._index-1);
  46.         }
  47.       } else if (args[1].toUpperCase() == 'AUTO_CANCEL') {
  48.         subject.setTargetAutoCancel('on');
  49.       } else if (args[1].toUpperCase() == 'AUTO_CANCEL_NEAR') {
  50.         subject.setTargetAutoCancel('near');
  51.       } else if (args.length > 2) {
  52.         subject.setTarget(null, eval(args[1]), eval(args[2]));
  53.       } else {
  54.         var target = this.findEvent
  55.           ? this.findEvent(args[1])
  56.           : this.character(eval(args[1]));
  57.         subject.setTarget(target);
  58.       }
  59.     }
  60.   };
  61.  
  62.   var _Game_CharacterBase_initMembers = Game_CharacterBase.prototype.initMembers;
  63.   Game_CharacterBase.prototype.initMembers = function() {
  64.     _Game_CharacterBase_initMembers.call(this);
  65.     this._target = null;
  66.     this._targetX = null;
  67.     this._targetY = null;
  68.     this._targetAutoCancel = false; // 'on', 'near', false
  69.     this._break = false;
  70.   };
  71.  
  72.   Game_CharacterBase.prototype.setTarget = function(target, targetX, targetY) {
  73.     this._target = target;
  74.     if (this._target) {
  75.       this._targetX = this._target.x;
  76.       this._targetY = this._target.y;
  77.     } else {
  78.       this._targetX = targetX;
  79.       this._targetY = targetY;
  80.     }
  81.     this._targetAutoCancel = false;
  82.   };
  83.  
  84.   Game_CharacterBase.prototype.setTargetAutoCancel = function(targetAutoCancel) {
  85.     this._targetAutoCancel = targetAutoCancel;
  86.   }
  87.  
  88.   Game_CharacterBase.prototype.clearTarget = function() {
  89.     this._target = null;
  90.     this._targetX = null;
  91.     this._targetY = null;
  92.   };
  93.  
  94.   Game_CharacterBase.prototype.isNearTarget = function(x, y) {
  95.     return Math.abs(this.x - x) <= 1 &&
  96.            Math.abs(this.y - t) <= 1;
  97.   }
  98.  
  99.   Game_CharacterBase.prototype.isOnTarget = function(x, y) {
  100.     return this.x == x &&
  101.            this.y == y;
  102.   }
  103.  
  104.   var _Game_CharacterBase_updateStop = Game_CharacterBase.prototype.updateStop;
  105.   Game_CharacterBase.prototype.updateStop = function() {
  106.     _Game_CharacterBase_updateStop.call(this);
  107.  
  108.     if (this._target) {
  109.       this._targetX = this._target.x;
  110.       this._targetY = this._target.y;
  111.     }
  112.  
  113.     if (this._targetX != null) {
  114.       direction = this.findDirectionTo(this._targetX, this._targetY);
  115.       if (direction > 0)
  116.       {
  117.         this.moveStraight(direction);
  118.       }
  119.     }
  120.  
  121.     if (this._targetAutoCancel) {
  122.       if (this._targetAutoCancel.toUpperCase() === 'ON') {
  123.         if (this.isOnTarget(this._targetX, this._targetY)) {
  124.           console.log('cancelling target');
  125.           this.clearTarget();
  126.         }
  127.       } else if (this._targetAutoCancel.toUpperCase() === 'NEAR') {
  128.         if (this.isNearTarget(this._targetX, this._targetY)) {
  129.           console.log('cancelling target');
  130.           this.clearTarget();
  131.         }
  132.       }
  133.     }
  134.   };
  135. })();
  136.  
  137.  
  138. //=============================================================================
  139. // KCL - FindEventByName
  140. // KCL_FindEventByName.js
  141. // Version: 1.00
  142. //=============================================================================
  143.  
  144. var KCL = KCL || {};
  145.  
  146. //=============================================================================
  147.  /*:
  148.  * @plugindesc This plugin adds a new call to the game interpeter to find
  149.  * events by their name instead of just by their ID.
  150.  *
  151.  * @help
  152.  * This plug-in allows scripters to call out events by either name or ID.
  153.  * To do this, instead of calling GameIntepreter.character(eval(id)) use
  154.  * GameInterpeter.findEvent(target). The findEvent call will be smart enough
  155.  * to check if you're handing in a string or a number and look up the event
  156.  * in the most ideal way.
  157.  */
  158. //=============================================================================
  159.  
  160.  
  161. Game_Map.prototype.event = function(eventId) {
  162.     return this._events[eventId]||this._getEventByName(eventId);
  163. }
  164.  
  165. Game_Map.prototype._getEventByName = function(name) {
  166.     return this.events().filter(function(e) {
  167.         return e.event().name == name;
  168.     })[0];
  169. }
  170.  
  171. Game_Interpreter.prototype.findEvent = function(eventId) {
  172.     if (!isNaN(eventId))
  173.         return this.character(eval(eventId));
  174.     else
  175.         return $gameMap.event(eventId);
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement