Advertisement
ezmash

Tile Changer (MV)

Jan 25th, 2018
3,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Tile Changer (Shaz_TileChanger.js)
  3. // by Shaz
  4. // Last Updated: 2018.03.24 v1.10
  5. //=============================================================================
  6.  
  7. /*:
  8.  * @plugindesc Change tiles on map or copy tiles from another map
  9.  * @author Shaz
  10.  *
  11.  * @help
  12.  * This plugin allows you to copy a block of tiles from another map to the
  13.  * current map, or from one place on the current map to another, or to
  14.  * change an individual tile to a specific tile id.
  15.  *
  16.  * To Use:
  17.  * -------
  18.  *
  19.  * To copy tiles from other maps, you must set up references to the map id
  20.  * in the current map's note box.  This consists of a name/map id pair, for
  21.  * each map you want to copy tiles from.  The format should be as follows (I
  22.  * suggest setting this up in something with a larger window to make it
  23.  * easier to see/modify, then copy/pasting into the map properties):
  24.  *
  25.  * <load:[{"name":"pinkroom","map":120},
  26.  *        {"name":"blueroom","map":121},
  27.  *        {"name":"greenroom","map":122}]>
  28.  *
  29.  * This will then allow you to run the CopyTiles plugin command, and refer to
  30.  * the maps by the names "pinkroom", "blueroom", and "greenroom" rather than
  31.  * their map ids.  Note - these do NOT have to be the same as the map name
  32.  * shown in the editor or the display name.
  33.  *
  34.  * If you only wish to copy tile areas from the current map to another place
  35.  * on the current map, you do not need to set up the map notes.  This is only
  36.  * required if you want to copy from other maps.
  37.  *
  38.  * The current map and the maps being copied must use the same tileset, or at
  39.  * least have the same or similar tiles in the same location on the tileset.
  40.  * This plugin does not copy the tile image, just the tile id, so if the source
  41.  * map has a vase of flowers, and the current map has a teddy bear in the same
  42.  * location on the tileset, a teddy bear is what will appear when you copy the
  43.  * tiles.
  44.  *
  45.  * If you only wish to use the ChangeTile plugin command, you do not need to
  46.  * set up the map notes.
  47.  *
  48.  *
  49.  * Plugin Commands:
  50.  * ----------------
  51.  *
  52.  * CopyTiles dx dy source sx1 sy1 sx2 sy2 z-list
  53.  * Copies tiles from another (or the same) map, where
  54.  *   dx      is the x-coordinate of the top left destination area
  55.  *   dy      is the y-coordinate of the top left destination area
  56.  *   source  is the 'name' of the source map in the map note
  57.  *           or 'self' if you want to copy a section of the current map
  58.  *   sx1     is the x-coordinate of the top left source area
  59.  *   sy1     is the y-coordinate of the top left source area
  60.  *   sx2     is the x-coordinate of the bottom right source area
  61.  *   sy2     is the y-coordinate of the bottom right source area
  62.  *   z-list  is an optional series of z values indicating which layers to copy
  63.  *           if more than one, just use spaces between
  64.  *           if omitted, all layers will be copied
  65.  *
  66.  * ChangeTile x y z tileId
  67.  * Changes the tile at the specified coordinates and layer to the tileId
  68.  * indicated.  Note - no auto-formatting of autotiles happens here.
  69.  *
  70.  * Z-layers
  71.  * 0 - ground layer (most A tiles)
  72.  * 1 - ground cover layer (A1 tiles with transparent areas, and right 4
  73.  *     columns of A2 tiles)
  74.  * 2 - upper layer 1
  75.  * 3 - upper layer 2
  76.  * 4 - shadow layer
  77.  * 5 - region layer
  78.  *
  79.  *
  80.  * Examples:
  81.  * ---------
  82.  *
  83.  * CopyTiles 3 3 destroyed 5 8 9 12
  84.  * copies the area between 5,8 and 9,12 from the 'destroyed' map to the
  85.  * current map, with the upper left at 3,3.  All layers are copied
  86.  *
  87.  * CopyTiles 3 3 wilted 5 8 9 12 2 3
  88.  * copies layers 2 and 3 (the upper layers) between 5,8 and 9,12 from the
  89.  * 'wilted' map to the current map, with the upper left at 3,3
  90.  *
  91.  * ChangeTile 3 3 2 0
  92.  * replaces the tile at coordinate 3,3 on layer 0 (upper layer 1) with tile 0
  93.  * (which is the 'erase' tile - so this is removing whatever tile may have
  94.  * previously been there)
  95.  *
  96.  *
  97.  * Change Log:
  98.  * -----------
  99.  * 2018.03.24  1.10  Save tile changes (fixes issue with returning to map
  100.  *                   after menu or battle, and loading games) - no longer any
  101.  *                   need for an event to re-do tile changes when map is
  102.  *                   reloaded.
  103.  *
  104.  */
  105.  
  106. var Imported = Imported || {};
  107. Imported.Shaz_TileChanger = true;
  108.  
  109. var Shaz = Shaz || {};
  110. Shaz.TC = Shaz.TC || {};
  111. Shaz.TC.Version = 1.10;
  112.  
  113. (function() {
  114.  
  115.     var _Shaz_TC_DataManager_onLoad = DataManager.onLoad;
  116.     DataManager.onLoad = function(object) {
  117.         _Shaz_TC_DataManager_onLoad.call(this, object);
  118.         if (object === $dataMap) {
  119.             $dataMap.extraMaps = {};
  120.             $dataMap.extraMapCount = 0;
  121.             if ($dataMap.meta.load) {
  122.                 $dataMap.meta.load = JSON.parse($dataMap.meta.load);
  123.                 for (var i = 0; i < $dataMap.meta.load.length; i++) {
  124.                     var name = $dataMap.meta.load[i].name;
  125.                     var map = parseInt($dataMap.meta.load[i].map);
  126.                     var filename = 'Map%1.json'.format(map.padZero(3));
  127.                     this.loadExtraMap(name, filename);
  128.                 }
  129.             }
  130.         }
  131.     };
  132.  
  133.     DataManager.loadExtraMap = function(name, src) {
  134.         var xhr = new XMLHttpRequest();
  135.         var url = 'data/' + src;
  136.         xhr.open('GET', url);
  137.         xhr.overrideMimeType('application/json');
  138.         xhr.onload = function() {
  139.             if (xhr.status < 400) {
  140.                 var data = JSON.parse(xhr.responseText);
  141.                 $dataMap.extraMaps[name] = {};
  142.                 $dataMap.extraMaps[name].width = data.width;
  143.                 $dataMap.extraMaps[name].height = data.height;
  144.                 $dataMap.extraMaps[name].data = data.data;
  145.                 $dataMap.extraMapCount -= 1;
  146.             }
  147.         }
  148.         xhr.onerror = this._mapLoader || function() {
  149.             DataManager._errorUrl = DataManager._errorUrl || url;
  150.         }
  151.         $dataMap.extraMaps[name] = null;
  152.         $dataMap.extraMapCount += 1;
  153.         xhr.send();
  154.     };
  155.  
  156.     var _Shaz_TC_DataManager_isMapLoaded = DataManager.isMapLoaded;
  157.     DataManager.isMapLoaded = function() {
  158.         return _Shaz_TC_DataManager_isMapLoaded.call(this) && $dataMap.extraMapCount === 0;
  159.     };
  160.  
  161.     var _Shaz_TC_Spriteset_Map_updateTilemap = Spriteset_Map.prototype.updateTilemap;
  162.     Spriteset_Map.prototype.updateTilemap = function() {
  163.         _Shaz_TC_Spriteset_Map_updateTilemap.call(this);
  164.         if ($gameTemp.needMapDataRefresh()) {
  165.             this._tilemap.setData($gameMap.width(), $gameMap.height(), $gameMap.data());
  166.             this._tilemap.refresh();
  167.             $gameTemp.setMapDataRefresh(false);
  168.         }
  169.     };
  170.  
  171.     var _Shaz_TC_Game_Temp_initialize = Game_Temp.prototype.initialize;
  172.     Game_Temp.prototype.initialize = function() {
  173.         _Shaz_TC_Game_Temp_initialize.call(this);
  174.         this._needMapDataRefresh = false;
  175.     };
  176.  
  177.     Game_Temp.prototype.needMapDataRefresh = function() {
  178.         return this._needMapDataRefresh;
  179.     };
  180.  
  181.     Game_Temp.prototype.setMapDataRefresh = function(refresh) {
  182.         this._needMapDataRefresh = refresh;
  183.     };
  184.  
  185.     var _Shaz_TC_Game_System_initialize = Game_System.prototype.initialize;
  186.     Game_System.prototype.initialize = function() {
  187.         _Shaz_TC_Game_System_initialize.call(this);
  188.         this._mapTiles = [];
  189.     };
  190.  
  191.     Game_System.prototype.saveMapTile = function(location, tileId) {
  192.         var mapId = $gameMap.mapId();
  193.         if (!this._mapTiles[mapId]) {
  194.             this._mapTiles[mapId] = {};
  195.         }
  196.         this._mapTiles[mapId][location] = tileId;
  197.     };
  198.  
  199.     Game_System.prototype.restoreMapTiles = function(mapId) {
  200.         var tiles = this._mapTiles[mapId] || {};
  201.         Object.keys(tiles).forEach(function(location) {
  202.             $dataMap.data[location] = tiles[location];
  203.         }.bind(this));
  204.         $gameTemp.setMapDataRefresh(true);
  205.     };
  206.  
  207.     Game_Map.prototype.copyTiles = function(args) {
  208.         var dtlx = eval(args.shift());
  209.         var dtly = eval(args.shift());
  210.         var src = eval(args.shift());
  211.         var stlx = eval(args.shift());
  212.         var stly = eval(args.shift());
  213.         var sbrx = eval(args.shift());
  214.         var sbry = eval(args.shift());
  215.         var zarray = [];
  216.         for (var i = 0; i < args.length; i++) {
  217.             zarray.push(eval(args[i]));
  218.         }
  219.         if (zarray.length === 0) {
  220.             zarray = [0, 1, 2, 3, 4, 5];
  221.         }
  222.  
  223.         if (src.toUpperCase() === 'SELF') {
  224.             var source = $dataMap;
  225.         } else {
  226.             var source = $dataMap.extraMaps[src];
  227.         }
  228.  
  229.         var sw = sbrx - stlx + 1;
  230.         var sh = sbry - stly + 1;
  231.  
  232.         for (var z1 = 0; z1 < zarray.length; z1++) {
  233.             for (var y = 0; y < sh; y++) {
  234.                 for (var x = 0; x < sw; x++) {
  235.                     var sx = stlx + x;
  236.                     var sy = stly + y;
  237.                     var dx = dtlx + x;
  238.                     var dy = dtly + y;
  239.                     var z = zarray[z1];
  240.                     var dIndex = this.calcIndex($dataMap, dx, dy, z);
  241.                     var sIndex = this.calcIndex(source, sx, sy, z);
  242.                     $dataMap.data[dIndex] = source.data[sIndex];
  243.                     $gameSystem.saveMapTile(dIndex, source.data[sIndex]);
  244.                 }
  245.             }
  246.         }
  247.  
  248.         $gameTemp.setMapDataRefresh(true);
  249.     };
  250.  
  251.     Game_Map.prototype.changeTile = function(args) {
  252.         var x = eval(args.shift());
  253.         var y = eval(args.shift());
  254.         var z = eval(args.shift());
  255.         var tileId = eval(args.shift());
  256.         var mapLoc = this.calcIndex($dataMap, x, y, z);
  257.  
  258.         $dataMap.data[mapLoc] = tileId;
  259.         $gameSystem.saveMapTile(mapLoc, tileId);
  260.         $gameTemp.setMapDataRefresh(true);
  261.     };
  262.  
  263.     Game_Map.prototype.calcIndex = function(dataMap, x, y, z) {
  264.         var w = dataMap.width;
  265.         var h = dataMap.height;
  266.  
  267.         return (z * w * h) + (y * w) + x;
  268.     };
  269.  
  270.     var _Shaz_TC_Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  271.     Game_Interpreter.prototype.pluginCommand = function(command, args) {
  272.         switch(command.toUpperCase()) {
  273.             case 'COPYTILES':
  274.                 $gameMap.copyTiles(args);
  275.                 break;
  276.             case 'CHANGETILE':
  277.                 $gameMap.changeTile(args);
  278.                 break;
  279.             default:
  280.                 _Shaz_TC_Game_Interpreter_pluginCommand.call(this, command, args);
  281.         }
  282.     };
  283.  
  284.     var _Shaz_TC_Scene_Map_onMapLoaded = Scene_Map.prototype.onMapLoaded;
  285.     Scene_Map.prototype.onMapLoaded = function() {
  286.         $gameSystem.restoreMapTiles(this._transfer ? $gamePlayer.newMapId() : $gameMap.mapId());
  287.         _Shaz_TC_Scene_Map_onMapLoaded.call(this);
  288.     };
  289.  
  290. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement