Advertisement
ezmash

Lazy Tilesets (MV)

Oct 24th, 2015
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Lazy Tilesets
  3. // by Shaz
  4. // Last Updated: 2019.02.10
  5. //=============================================================================
  6.  
  7. /*:
  8. * @plugindesc Allows you to export/import/copy tilesets and all their data
  9. * @author Shaz
  10. *
  11. * @help This plugin does not provide plugin commands.
  12. *
  13. * Tileset Note:
  14. *   <lzexport: filename>     # Export settings to img/tilesets/filename.MVTileset
  15. *   <lzimport: filename>     # Import settings from img/tilesets/filename.MVTileset
  16. *   <lzcopy: ID>             # Copy settings from tileset with ID
  17. *
  18. *   <lzimport: filename A1 A2 B C>
  19. *   <lzcopy: ID A1 A2 B C>   # Import/copy slots A1, A2, B and C
  20. *
  21. *   <lzimport: filename B:D C:E>
  22. *   <lzcopy: ID B:D C:E>     # Import/copy slots B and C, placing them into D and E
  23. *
  24. * Remember to remove the commands after use to prevent loss of custom changes
  25. * This plugin only runs in DEBUG mode (from PlayTest)
  26. *
  27. *
  28. *-----------------------------------------------------------------------------
  29. * Versions
  30. *
  31. * 2019.02.10 Fixed issue with path name
  32. * 2015.10.21 Initial release
  33. */
  34.  
  35. (function() {
  36.  
  37.   var slots = {
  38.     'A1': [0, 2048, 2815],
  39.     'A2': [1, 2816, 4351],
  40.     'A3': [2, 4352, 5887],
  41.     'A4': [3, 5888, 8191],
  42.     'A5': [4, 1536, 1663],
  43.     'B':  [5, 0, 255],
  44.     'C':  [6, 256, 511],
  45.     'D':  [7, 512, 767],
  46.     'E':  [8, 768, 1023]
  47.   };
  48.  
  49.   var _Scene_Boot_start = Scene_Boot.prototype.start;
  50.   Scene_Boot.prototype.start = function() {
  51.     _Scene_Boot_start.call(this);
  52.  
  53.     if (!Utils.isOptionValid('test')) return;
  54.  
  55.     var saveMods = false;
  56.     var fs = require('fs');
  57.  
  58.     for (var i = 0; i < $dataTilesets.length; i++) {
  59.       ts = $dataTilesets[i];
  60.  
  61.       if (!ts) continue;
  62.  
  63.       if (ts.meta.lzexport) {
  64.         filename = ts.meta.lzexport.trim();
  65.         fs.writeFileSync(StorageManager.lzFile(filename), JSON.stringify(ts));
  66.       }
  67.  
  68.       if (ts.meta.lzimport) {
  69.         var options = ts.meta.lzimport.trim().split(' ');
  70.         filename = options.shift();
  71.         data = JSON.parse(fs.readFileSync(StorageManager.lzFile(filename)));
  72.         $dataTilesets[i] = this.lzReadData(data, options, i);
  73.         saveMods = true;
  74.       }
  75.  
  76.       if (ts.meta.lzcopy) {
  77.         var options = ts.meta.lzcopy.trim().split(' ');
  78.         tilesetId = options.shift();
  79.         data = $dataTilesets[Number(tilesetId)];
  80.         $dataTilesets[i] = this.lzReadData(data, options, i);
  81.         saveMods = true;
  82.       }
  83.     }
  84.  
  85.     if (saveMods) {
  86.       fs.writeFileSync(StorageManager.lzData('Tilesets'), JSON.stringify($dataTilesets));
  87.     }
  88.   };
  89.  
  90.   Scene_Boot.prototype.lzReadData = function(data, options, tilesetId) {
  91.     if (options.length < 1) {
  92.       return data;
  93.     } else {
  94.       var rtnData = $dataTilesets[tilesetId];
  95.       msgprfx = 'Tileset #' + String(tilesetId) + ' - ' + rtnData.name + ': ';
  96.  
  97.       for (i = 0; i < options.length; i++) {
  98.         var group = options[i].toUpperCase().split(':');
  99.         var fromSlot = group[0];
  100.         var toSlot = group[1] || group[0];
  101.  
  102.         // 'from' slot not valid
  103.         if ((slots[fromSlot] || 'X') === 'X') {
  104.           window.alert(msgprfx + 'Invalid slot ' + fromSlot);
  105.           return rtnData;
  106.         }
  107.  
  108.         // 'to' slot not valid
  109.         if ((slots[toSlot] || 'X') === 'X') {
  110.           window.alert(msgprfx + 'Invalid slot ' + toSlot);
  111.           return rtnData;
  112.         }
  113.  
  114.         // trying to move something invalid into/out of one of the A slots
  115.         if (fromSlot !== toSlot && ('BCDE'.indexOf(fromSlot) < 0 || 'BCDE'.indexOf(toSlot) < 0)) {
  116.           window.alert(msgprfx + 'A1-A5 slots cannot be moved');
  117.           return rtnData;
  118.         }
  119.  
  120.         // all good - copy the slot data in
  121.         fromSlotId = slots[fromSlot][0];
  122.         fromSlotStart = slots[fromSlot][1];
  123.         fromSlotEnd = slots[fromSlot][2];
  124.  
  125.         toSlotId = slots[toSlot][0];
  126.         k = slots[toSlot][1];
  127.  
  128.         rtnData.tilesetNames[toSlotId] = data.tilesetNames[fromSlotId];
  129.         for (j = fromSlotStart; j <= fromSlotEnd; j++) {
  130.           rtnData.flags[k] = data.flags[j];
  131.           k++;
  132.         }
  133.  
  134.       }
  135.     }
  136.     return rtnData;
  137.   };
  138.  
  139.   StorageManager.lzFile = function(filename) {
  140.     var path = require('path');
  141.     var base = path.dirname(process.mainModule.filename);
  142.     return path.join(base, 'img/tilesets', filename.replace(/[^a-zA-Z0-9]/,'') + '.MVTileset');
  143. //    var path = window.location.pathname.replace(/\/[^\/]*$/, '/img/tilesets/');
  144. //   if (path.match(/^\/([A-Z]\:)/)) {
  145. //        path = path.slice(1);
  146. //    }
  147. //    return decodeURIComponent(path) + filename.replace(/[^a-zA-Z0-9]/,'') + '.MVTileset';
  148.   };
  149.  
  150.   StorageManager.lzData = function(filename) {
  151.     var path = require('path');
  152.     var base = path.dirname(process.mainModule.filename);
  153.     return path.join(base, 'data', filename.replace(/[^a-zA-Z0-9]/,'') + '.json');
  154.     // var path = window.location.pathname.replace(/\/[^\/]*$/, '/data/');
  155.     // if (path.match(/^\/([A-Z]\:)/)) {
  156.     //     path = path.slice(1);
  157.     // }
  158.     // return decodeURIComponent(path) + filename.replace(/[^a-zA-Z0-9]/,'') + '.json';
  159.   }
  160. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement