Advertisement
ludovicodes

LudoSavePathing.js

Dec 13th, 2017
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. //LudoSavePathing.js
  3. //=============================================================================
  4.  
  5. /*:
  6.  * @plugindesc plugin that tracks movement of the player when entering a given map and outputs that information to a file.
  7.  * @author Alessio De Santis
  8.  *
  9.  * @help Use <Track:true> on Map metadata to enable tracking.
  10.  * @param Default SwitchId
  11.  * @desc Number of the switch that will trigger the file output when turned on for the first time
  12.  *
  13.  * Default : 20
  14.  * @default 20
  15.  *
  16.  * @param Max Saves
  17.  * @desc Max number of Saves allowed per game
  18.  * Default : 1
  19.  * @default 1
  20.  *
  21.  * @param Save on Title Screen
  22.  * @desc Set to true to automatically save on title screen, false otherwise (lowercase true)
  23.  * Default : true
  24.  * @default true
  25.  */  
  26.  
  27.  
  28.  //-----------------------------------------------------------------------------
  29.  
  30.  // Scene_Map_Pathing_Check
  31. //
  32. //
  33.  
  34. $testing = [];
  35. $defaultSwitchId = Number(PluginManager.parameters("LudoSavePathing")["Default SwitchId"]) || 20;
  36. $msaves = Number(PluginManager.parameters("LudoSavePathing")["Max Saves"]) || 1;
  37. $titlesave = (PluginManager.parameters("LudoSavePathing")["Save on Title Screen"] == "true");
  38.  
  39. Scene_Map.prototype.onMapLoaded = function() {
  40.     if (this._transfer) {
  41.         $gamePlayer.performTransfer();
  42.     }
  43.     this.createDisplayObjects();
  44. };
  45.  
  46. Game_Player.prototype.increaseSteps = function() {
  47.     Game_Character.prototype.increaseSteps.call(this);
  48.     if (this.isNormal()) {
  49.         $gameParty.increaseSteps();
  50.         if($msaves > 0 && $dataMap.meta.Track){
  51.         $testing.filter(function(test){ return test.id === $gameMap._mapId })[0].pathing.push([$gameSystem.playtimeText(), this._x, this._y]);
  52.         }
  53.     }
  54. };
  55.  
  56. Game_Player.prototype.performTransfer = function() {
  57.     if (this.isTransferring()) {
  58.         this.setDirection(this._newDirection);
  59.         if (this._newMapId !== $gameMap.mapId() || this._needsMapReload) {
  60.             $gameMap.setup(this._newMapId);
  61.             this._needsMapReload = false;
  62.         }
  63.         this.locate(this._newX, this._newY);
  64.         if($dataMap.meta.Track && $msaves > 0){
  65.             console.log("runs");
  66.             var Ludoexample = $testing.filter(function(test){ return test.id === $gameMap._mapId });
  67.             if(Ludoexample.length == 0){
  68.             var b = {
  69.                 id : this._newMapId,
  70.                 pathing : [[$gameSystem.playtimeText(), this._newX,this._newY]],
  71.             }
  72.             $testing.push(b);
  73.             }
  74.             else Ludoexample[0].pathing.push([$gameSystem.playtimeText(), this._newX, this._newY]);
  75.         }
  76.        
  77.         this.refresh();
  78.         this.clearTransferInfo();
  79.     }
  80. };
  81.  
  82.  
  83. Game_Switches.prototype.onChange = function() {
  84.     $gameMap.requestRefresh();
  85.     Game_Switches.saveFile(this._data[$defaultSwitchId]);
  86. };
  87.  
  88. Game_Switches.saveFile = function(sw) {
  89.     if($msaves > 0 && sw){
  90.         console.log("Outputting file");
  91.         json = "";
  92.         $testing.forEach(function(element){
  93.             json += "{\n";
  94.             json += "\t" + '"Map_id" : "' + element.id + '",\n';
  95.             json += "\t" + '"Pathing" : ' + '[\n';
  96.             element.pathing.forEach(function(array){
  97.                 if(element.pathing.indexOf(array) == element.pathing.length-1){
  98.                     json += "\t\t[" + '"' + array[0] + '"' + ", " + array[1] + ", " + array[2] + "]";
  99.                 }
  100.                 else json += "\t\t[" + '"' + array[0] + '"' + ", " + array[1] + ", " + array[2] + "],\n";
  101.             });
  102.             json += '\n\t]\n';
  103.             json += "}";
  104.             json += "\n";
  105.         });
  106.  
  107.         StorageManager.saveToTestFile(json);
  108.  
  109.         $msaves--;
  110.     }
  111. }
  112.  
  113. StorageManager.saveToTestFile = function(json) {
  114.     var fs = require('fs');
  115.     var dirPath = this.localFileDirectoryPath();
  116.     var ref = Number(PluginManager.parameters("LudoSavePathing")["Max Saves"]) - $msaves + 1;
  117.     var filePath = this.localFileDirectoryPath() + "test" + ref + ".txt";
  118.     if (!fs.existsSync(dirPath)) {
  119.         fs.mkdirSync(dirPath);
  120.     }
  121.     fs.writeFileSync(filePath, json);
  122. };
  123.  
  124. Scene_GameEnd.prototype.commandToTitle = function() {    
  125.     if($titlesave) Game_Switches.saveFile(true);  
  126.     Scene_GameEnd.clearTrackInfo();  
  127.     this.fadeOutAll();
  128.     SceneManager.goto(Scene_Title);
  129. };
  130.  
  131. Scene_Gameover.prototype.gotoTitle = function() {
  132.     if($titlesave) Game_Switches.saveFile(true);  
  133.     Scene_GameEndne.clearTrackInfo();      
  134.     SceneManager.goto(Scene_Title);
  135. };
  136.  
  137. Scene_GameEnd.clearTrackInfo = function(){
  138.     for(var i of $testing){
  139.         i.pathing = [];
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement