Player Position History Plugin v1.04

bluebooth

Veteran
Veteran
Joined
Nov 12, 2015
Messages
95
Reaction score
115
First Language
English
Primarily Uses
N/A
Transition History v1.04

Author: Michael Morris @Blue Booth Studios


 

Introduction
A number of my scripts require knowledge of the previous (or "calling") player position.  For example, a player walks onto a save point, and the game loads a new map with a campsite.  At some point, the player opts to leave the campsite, and needs to appear at the position he/she occupied before entering the campsite.  On the World Map, this information is tracked so that if a player decides not to use the World Map (cancels out of it), the player will appear at their position prior to entering said map.  This same information can easily be used for mini-games, etc.  By itself, it isn't hard to do (assign 3 or 4 variables).  But I ran into a more complicated use case when considering daisy-chaining world maps.


For example, let's say the player enters your world map.  You might have one world map for the Dark Forest zone.  The player chooses the first node in that world map, which leads out of the Dark Forest zone, and into the Open Plains zone (if this still is not clear, consider how world maps are handled in games like Super Mario World or Mario RPG).  If the player selects cancel while in the Open Plains zone (the second world map called), the player will transition back to the Dark Forest zone world map, at the first node.  But the original player position (that is, the player position before the Dark Forest zone) has been lost!  In order to get around this, this plugin allows you to track a variable size array of previous locations.  And while this has been created with a specific use in mind, the coding allows for any use of it you can think of.  I know you guys will come up with some other incredible ways to use it, and I look forward to seeing them!


Features
- Can 'save' and 'load' a position from $gameVariables specified in parameter settings (nothing that can't be done by eventing alone, these two functions are for convenience).


- Factors out the 'save' and 'load' from the World Map script to keep responsibilities separate.


- Can set the total size of the transition history array in parameters.


- Array uses push and pop so the most recent transition is always at the end of the array (use getHead to access).  If no elements in array, uses the saved position variables.


- If the array grows beyond it's specified maximum size, it uses shift to replace the oldest element.


- Added storeCurrent to make storing current player position less ***bersome.


How to Use
- Copy script into your game js/plugins directory.


- Call script plugin commands within your own events or plugins.


Requirements


This script has no external requirements.


Demo


No demo provided - this script doesn't produce visible effects by itself.  It needs to be used in conjunction with another script like the World Map.


Script

//=============================================================================
// Bluebooth Plugins - Transition History
// BBS_TransitionHistory.js
//=============================================================================

//=============================================================================
/*:
* @plugindesc v1.04 Stores a list of previous player transition positions.
* @author Michael Morris
*
* @param History Length
* @desc How many transitions should be remembered? If the number of transitions is
* larger than the history length size than the oldest transition will be removed from
* the list.
* @default 8
*
* @param Last Map Id
* @desc Choose the variable ID that stores the player's last map ID.
* @default 1
*
* @param Last Player Map X
* @desc Choose the variable ID that stores the player's last map X.
* -1 do not remember last Player X.
* @default 2
*
* @param Last Player Map Y
* @desc Choose the variable ID that stores the player's last map Y.
* -1 do not remember last Player Y.
* @default 3
*
* @param Last Player Facing Direction
* @desc Choose the variable ID that stores the player's last facing direction.
* -1 do not remember last facing direction.
* @default 4
*
* @param Next Map Id
* @desc Choose the variable ID that stores the player's next map ID.
* @default 5
*
* @param Next Player Map X
* @desc Choose the variable ID that stores the player's next map X.
* -1 do not remember last Player X.
* @default 6
*
* @param Next Player Map Y
* @desc Choose the variable ID that stores the player's next map Y.
* -1 do not remember last Player Y.
* @default 7
*
* @param Next Player Facing Direction
* @desc Choose the variable ID that stores the player's next facing direction.
* -1 do not remember last facing direction.
* @default 8
*
* @param Debug Mode
* @desc Enable to activate console variable logging. Use for debugging odd behaviour.
* true to enable console variable logging.
* @default false
*
* @help
* ============================================================================
* Introduction
* ============================================================================
*
* To be used in more complicated scripts, this tracks a list of last player map positions
* before transition. It is intended to be used for things like daisy chaining world maps.
* If the player transfers back to the previous map, then use pop. Should not be used for
* data you want to be saved to disk. This is for the current session only.
*
* ============================================================================
* Plugin Commands
* ============================================================================
*
* Use the following plugin commands to manipulate world map nodes.
*
* Plugin Commands:
*
* TransitionHistory init # Initialize Transition History.
* TransitionHistory saveMapPos mapId, x, y, dir # Stores in $gameVariables map Id, player coordinates, and facing direction.
* TransitionHistory setNext mapId, x, y, dir # Stores in next $gameVariables map Id, player coordinates, and facing direction.
* TransitionHistory storeCurrent # Stores current map Id, player coordinates, and facing direction.
* TransitionHistory store mapId, x, y, dir # Stores map Id, player coordinates, and facing direction.
* TransitionHistory getHead # Gets the head element from the history array. Sadly not sexual.
* TransitionHistory pop # Removes most recent map Id, player coordinate, and facing direction.
* TransitionHistory clear # Frees all memory allocated by Transition History.
*
* ============================================================================
* Versions
* ============================================================================
* 1.04 - Fixed bad logic error where changing maps would break transition history. Ouch.
* 1.03 - Added store current function as store ... was ***bersome.
* 1.02 - If history array is empty, getHead will return saved map pos (see: saveMapPos). Can't pop if history array empty.
* 1.01 - Plugin finished.
*
*/
//=============================================================================

//=============================================================================
var Imported = Imported || {} ;
var BBS = BBS || {};
Imported.TransitionHistory = 1;
BBS.TransitionHistory = BBS.TransitionHistory || {};

(function() {

//=============================================================================
// Parameter Variables
//=============================================================================
var parameters = PluginManager.parameters('BBS_TransitionHistory');
var historyMaxSize = Number(parameters['History Length'] || '8');
var lastMapVar = Number(parameters['Last Map Id'] || '1');
var lastXVar = Number(parameters['Last Player Map X'] || '2');
var lastYVar = Number(parameters['Last Player Map Y'] || '3');
var lastFDirVar = Number(parameters['Last Player Facing Direction'] || '4');
var nextMapVar = Number(parameters['Next Map Id'] || '5');
var nextXVar = Number(parameters['Next Player Map X'] || '6');
var nextYVar = Number(parameters['Next Player Map Y'] || '7');
var nextFDirVar = Number(parameters['Next Player Facing Direction'] || '8');
var debugging = eval(String(parameters['Debug Mode'] || 'false'));

//=============================================================================
// Game_Interpreter
//=============================================================================
var BBS_TH_Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
if (command === 'TransitionHistory') {
switch (args[0]) {
case 'init':
$gameTemp.THhistoryInit();
break;
case 'saveMapPos':
$gameTemp.THsaveMapPos();
break;
case 'setNext':
$gameTemp.THsetNext(Number(args[1]), Number(args[2]), Number(args[3]), Number(args[4]));
break;
case 'storeCurrent':
$gameTemp.THstoreCurrent();
break;
case 'store':
$gameTemp.THstoreMapPos(Number(args[1]), Number(args[2]), Number(args[3]), Number(args[4]));
break;
case 'getHead':
$gameTemp.THgetHead();
break;
case 'pop':
$gameTemp.THpopMapPos();
break;
case 'clear':
$gameTemp.THclearHistory();
break;
};
}
else {
BBS_TH_Game_Interpreter_pluginCommand.call(this, command, args);
}
};

//=============================================================================
// LastPosition Class
//=============================================================================
function LastPosition(mapId, x, y, faceDir) {
this.mapId = mapId;
this.x = x;
this.y = y;
this.faceDir = faceDir;
};

//=============================================================================
// Game Temp
//=============================================================================
Game_Temp.prototype.THhistoryInit = function() {
this._history = new Array();
};

Game_Temp.prototype.THsaveMapPos = function() {
$gameVariables.setValue(lastMapVar, $gameMap.mapId());
$gameVariables.setValue(lastXVar, 0);
$gameVariables.setValue(lastYVar, 0);
$gameVariables.setValue(lastFDirVar, 2);

// Override defaults with values, if they were provided.
if (lastXVar > 0) {
$gameVariables.setValue(lastXVar, $gamePlayer.x);
}

if (lastYVar > 0) {
$gameVariables.setValue(lastYVar, $gamePlayer.y);
}

if (lastFDirVar > 0) {
$gameVariables.setValue(lastFDirVar, $gamePlayer.direction());
}

if (debugging) {
console.log("Saved: ");
console.log($gameMap.mapId());
console.log($gamePlayer.x);
console.log($gamePlayer.y);
console.log($gamePlayer.direction());
}

};

Game_Temp.prototype.THstoreMapPos = function(mapId, x, y, faceDir) {
if (this._history === undefined) this.THhistoryInit();

if (this._historySize >= historyMaxSize) {
this._historySize = this._historySize - 1;
this._history.shift();
}

var newItem = new LastPosition(mapId, x, y, faceDir);
this._historySize = this._history.push(newItem);

if (debugging) {
console.log("Pushed: ");
console.log(mapId);
console.log(x);
console.log(y);
console.log(faceDir);
}
};

Game_Temp.prototype.THstoreCurrent = function() {
if ($gameMap === undefined) return;
if ($gamePlayer === undefined) return;

this.THstoreMapPos($gameMap._mapId, $gamePlayer.x, $gamePlayer.y, $gamePlayer._direction);

if (debugging) {
console.log("Pushed: ");
console.log($gameMap._mapId);
console.log($gamePlayer.x);
console.log($gamePlayer.y);
console.log($gamePlayer._direction);
}
};

Game_Temp.prototype.THpopMapPos = function() {
if (this._historySize > 0) {
this._historySize = this._historySize - 1;
this._history.pop();
}
};

Game_Temp.prototype.THgetHead = function() {
if (this._historySize > 0) {
var headIndex = this._historySize - 1;

var nextMapId = this._history[headIndex].mapId;
var nextX = this._history[headIndex].x;
var nextY = this._history[headIndex].y;
var nextFD = this._history[headIndex].faceDir;
}
else {
// If no entries in history, revert to saved co-ordinates.
var nextMapId = $gameVariables.value(lastMapVar);
var nextX = $gameVariables.value(lastXVar);
var nextY = $gameVariables.value(lastYVar);
var nextFD = $gameVariables.value(lastFDirVar);
}

$gameVariables.setValue(nextMapVar, nextMapId);
$gameVariables.setValue(nextXVar, 0);
$gameVariables.setValue(nextYVar, 0);
$gameVariables.setValue(nextFDirVar, 2);

// Override defaults with values, if they were provided.
if (nextXVar > 0) {
$gameVariables.setValue(nextXVar, nextX);
}

if (nextYVar > 0) {
$gameVariables.setValue(nextYVar, nextY);
}

if (nextFDirVar > 0) {
$gameVariables.setValue(nextFDirVar, nextFD);
}

if (debugging) {
console.log("Dest: ");
console.log(nextMapId);
console.log(nextX);
console.log(nextY);
console.log(nextFD);
}
};

Game_Temp.prototype.THsetNext = function(mapId, x, y, faceDir) {
$gameVariables.setValue(nextMapVar, mapId);
$gameVariables.setValue(nextXVar, 0);
$gameVariables.setValue(nextYVar, 0);
$gameVariables.setValue(nextFDirVar, 2);

// Override defaults with values, if they were provided.
if (nextXVar > 0) {
$gameVariables.setValue(nextXVar, x);
}

if (nextYVar > 0) {
$gameVariables.setValue(nextYVar, y);
}

if (nextFDirVar > 0) {
$gameVariables.setValue(nextFDirVar, faceDir);
}

};

Game_Temp.prototype.THclearHistory = function() {
while (this._historySize > 0) {
this._historySize = this._historySize - 1;
this._history.pop();
}
}

})(BBS.TransitionHistory);
//=============================================================================
// End of File
//=============================================================================



Change Log


1.04


  - Fixed bad logic error where changing maps would break transition history.  Ouch.
1.03


  - Added store current function as store ... was ***bersome.
1.02


  - If history array is empty, getHead will return saved map pos (see: saveMapPos).  Can't pop if history array empty.
1.01


  - Plugin finished.


Known Bugs / TODO


Suggestions, bug reports, and feature requests are welcomed!


Compatibility Issues


None known.  Compatible with Klaus Map Overlays, Terrax Lighting System, and most (if not all) of Yanfly's plugins.


FAQ


Q: The demo won't open, how to open it?
A: Download Winrar and try again.


Credit and Thanks
- Micheal Morris @Blue Booth Studios
- So much credit to Tsukihime and all of those who have been supportive of my scripts and put up with my incessant barrage of questions.


Author's Notes
Free for non-commercial usage as long as credit is given, contact me for commercial use.


View attachment BBS_TransitionHistory.js
 
Last edited by a moderator:

Cvrtis

Veteran
Veteran
Joined
Sep 9, 2013
Messages
238
Reaction score
120
First Language
Russian
Primarily Uses
Thank You! Exactly what i need for random locations.
 

bluebooth

Veteran
Veteran
Joined
Nov 12, 2015
Messages
95
Reaction score
115
First Language
English
Primarily Uses
N/A
You're welcome, Cvrtis!  Let me know if you have any questions using it.
 

Cvrtis

Veteran
Veteran
Joined
Sep 9, 2013
Messages
238
Reaction score
120
First Language
Russian
Primarily Uses
Need Demo!


Just NEED Demo. Urgently (for me, of course)!


I decided that I could cope without instruction.


But I was wrong, and just wasted my time  ;_;
 
Last edited by a moderator:

bluebooth

Veteran
Veteran
Joined
Nov 12, 2015
Messages
95
Reaction score
115
First Language
English
Primarily Uses
N/A
Hey Cvrtis, sorry you're having trouble.  This script is actually used as part of my World Map plugin:






It is used in the demo there.  I'll put together a demo for Player Position History by itself as well, but I'll need a bit more time for that. :)
 

Cvrtis

Veteran
Veteran
Joined
Sep 9, 2013
Messages
238
Reaction score
120
First Language
Russian
Primarily Uses
Oh, THANK You! I just didn't know, THAT You already have demo.


I tasted the demo and effect, when I press "Cansel" - it exactly what I need ) Yay.


Thank You!
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

40 card deck size, the plugin seems more sluggish.
Village theme I composed for a commission!
Stream will be live shortly with some Haunting Ground! Feel free to drop by!
welp, the stock market went down by 7% because of corona today. Time to buy stocks?

Forum statistics

Threads
94,563
Messages
921,916
Members
124,429
Latest member
sheepman
Top