Advertisement
ezmash

Terrain Tags (MV)

Nov 30th, 2015
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Terrain Tags
  3. // by Shaz
  4. // Last Updated: 2015.12.04
  5. //-----------------------------------------------------------------------------
  6. // This plugin is still under development
  7. //=============================================================================
  8.  
  9. /*:
  10.  * @plugindesc Uses terrain tags to change map behaviour
  11.  * @author Shaz
  12.  *
  13.  * @param Passability Number
  14.  * @desc Terrain tag to indicate "use passability from tile below"
  15.  * @default -1
  16.  *
  17.  * @param Counter Number
  18.  * @desc Terrain tag to indicate "behave like counter (allow interaction)"
  19.  * @default -1
  20.  *
  21.  * @param Offset Numbers
  22.  * @desc Terrain tag to indicate tile should be drawn higher (separate by ;)
  23.  * @default -1
  24.  *
  25.  * @param Offset Pixels
  26.  * @desc Height adjustment for Offset (one per offset number, separate by ;)
  27.  * @default
  28.  *
  29.  * @param Under Shadow Number
  30.  * @descr Draws the tile below the auto shadow
  31.  * @default -1
  32.  *
  33.  * @help This plugin does not provide plugin commands.
  34.  *
  35.  * Set the terrain tag on the parameters you want to use to the relevant
  36.  * terrain tag, then flag tiles with that terrain tag as necessary.  Any
  37.  * parameters left as -1 indicate those parts of the plugin will not be
  38.  * active.
  39.  *
  40.  * Passability Number
  41.  * ------------------
  42.  * This terrain tag allows you to ignore the passage settings on that tile
  43.  * and to look at the tile below it.  For example, a cliff with a small flower
  44.  * right on the upper edge, with the flower having a O passage setting, would
  45.  * allow the player to walk off the upper edge of the cliff.  With this
  46.  * parameter, the game ignores the O passage setting on the flower and looks
  47.  * at the setting on the cliff tile below it.
  48.  *
  49.  * Counter Number
  50.  * --------------
  51.  * This terrain tag allows you to assign a tile to act like a counter tile,
  52.  * without looking like a counter tile.  Normally, autotiles flagged as counters
  53.  * are drawn so that they extend over the tile below, which looks okay for
  54.  * tables, but not for other things, like fences or narrow streams.  With this
  55.  * parameter, the player can interact with NPC events with a tile between them.
  56.  *
  57.  * Offset Numbers and Pixels
  58.  * -------------------------
  59.  * This terrain tag and the paired pixel count allow you to move those tiles up
  60.  * by the specified number of pixels, so they aren't drawn snapped to the grid.
  61.  * If used, there must be the same number of entries in the terrain tags list
  62.  * and the pixels list, separated by a semi-colon (;).  An example of usage is
  63.  * to move pictures higher up on a wall so they are not aligned to either the
  64.  * bottom half or the top half, without needing to rearrange the tileset images.
  65.  *
  66.  * Under Shadow Number
  67.  * -------------------
  68.  * This terrain tag, used on B-E tiles, indicates that those tiles are to be
  69.  * drawn BELOW any autoshadow if one exists.  If two B-E tiles are drawn on the
  70.  * same spot, make sure one with this terrain tag is drawn first.  
  71.  *
  72.  *
  73.  *
  74.  */
  75.  
  76. (function() {
  77.  
  78.   var parameters = PluginManager.parameters('TerrainTags');
  79.   var passageTerrain = Number(parameters['Passability Number'] || -1);
  80.   var counterTerrain = Number(parameters['Counter Number'] || -1);
  81.   var offsetTerrain = String(parameters['Offset Numbers'] || -1).split(';').map(function(id) {
  82.     return Number(id);
  83.   });
  84.   var offsetPixels = String(parameters['Offset Pixels'] || 0).split(';').map(function(id) {
  85.     return Number(id);
  86.   });
  87.   var mapOffsets = {};
  88.   offsetTerrain.forEach(function(id, index) {
  89.     if (id !== -1) {
  90.       mapOffsets[id] = offsetPixels[index];
  91.     }
  92.   }.bind(this));
  93.   var underShadowTerrain = Number(parameters['Under Shadow Number'] || -1);
  94.  
  95.   var _Game_Map_checkPassage = Game_Map.prototype.checkPassage;
  96.   Game_Map.prototype.checkPassage = function(x, y, bit) {
  97.     if (passageTerrain === -1) {
  98.       return _Game_Map_checkPassage.call(this, x, y, bit);
  99.     } else {
  100.       var flags = this.tilesetFlags();
  101.       var tiles = this.allTiles(x, y);
  102.       for (var i = 0; i < tiles.length; i++) {
  103.         var flag = flags[tiles[i]];
  104.         if ((flag & 0x10) !== 0) // [*] No effect on passage
  105.           continue;
  106.         if (flag >> 12 === passageTerrain) // Take passage from tile below
  107.           continue;
  108.         if ((flag & bit) === 0)  // [o] Passable
  109.           return true;
  110.         if ((flag & bit) === bit) // [x] Impassable
  111.           return false;
  112.       }
  113.       return false;
  114.     }
  115.   };
  116.  
  117.   var _Game_Map_isCounter = Game_Map.prototype.isCounter;
  118.   Game_Map.prototype.isCounter = function(x, y) {
  119.     if (counterTerrain === -1) {
  120.       return _Game_Map_isCounter.call(this, x, y);
  121.     } else {
  122.       return _Game_Map_isCounter.call(this, x, y) || this.terrainTag(x, y) === counterTerrain;
  123.     }
  124.   };
  125.  
  126.   var _Tilemap_drawNormalTile = Tilemap.prototype._drawNormalTile;
  127.   Tilemap.prototype._drawNormalTile = function(bitmap, tileId, dx, dy) {
  128.     dy -= mapOffsets[$gameMap.tilesetFlags()[tileId] >> 12] || 0;
  129.     _Tilemap_drawNormalTile.call(this, bitmap, tileId, dx, dy);
  130.   };
  131.  
  132.   Tilemap.prototype._paintTiles = function(startX, startY, x, y) {
  133.       var tableEdgeVirtualId = 10000;
  134.       var mx = startX + x;
  135.       var my = startY + y;
  136.       var dx = (mx * this._tileWidth).mod(this._layerWidth);
  137.       var dy = (my * this._tileHeight).mod(this._layerHeight);
  138.       var lx = dx / this._tileWidth;
  139.       var ly = dy / this._tileHeight;
  140.       var tileId0 = this._readMapData(mx, my, 0);
  141.       var tileId1 = this._readMapData(mx, my, 1);
  142.       var tileId2 = this._readMapData(mx, my, 2);
  143.       var tileId3 = this._readMapData(mx, my, 3);
  144.       var shadowBits = this._readMapData(mx, my, 4);
  145.       var upperTileId1 = this._readMapData(mx, my - 1, 1);
  146.       var lowerTiles = [];
  147.       var upperTiles = [];
  148.       var flags = $gameMap.tilesetFlags();
  149.  
  150.       if (this._isHigherTile(tileId0)) {
  151.           upperTiles.push(tileId0);
  152.       } else {
  153.           lowerTiles.push(tileId0);
  154.       }
  155.       if (this._isHigherTile(tileId1)) {
  156.           upperTiles.push(tileId1);
  157.       } else {
  158.           lowerTiles.push(tileId1);
  159.       }
  160.  
  161.       var shadowDrawn = false;
  162.       if (underShadowTerrain === -1) {
  163.           lowerTiles.push(-shadowBits);
  164.           shadowDrawn = true;
  165.       }
  166.  
  167.       if (this._isTableTile(upperTileId1) && !this._isTableTile(tileId1)) {
  168.           if (!Tilemap.isShadowingTile(tileId0)) {
  169.               if (underShadowTerrain !== -1 && !shadowDrawn && flags[upperTileId1] >> 12 !== underShadowTerrain) {
  170.                 lowerTiles.push(-shadowBits);
  171.                 shadowDrawn = true;
  172.               }
  173.               lowerTiles.push(tableEdgeVirtualId + upperTileId1);
  174.           }
  175.       }
  176.  
  177.       if (this._isOverpassPosition(mx, my)) {
  178.           upperTiles.push(tileId2);
  179.           upperTiles.push(tileId3);
  180.       } else {
  181.           if (this._isHigherTile(tileId2)) {
  182.               upperTiles.push(tileId2);
  183.           } else {
  184.               if (underShadowTerrain !== -1 && !shadowDrawn && flags[tileId2] >> 12 !== underShadowTerrain) {
  185.                 lowerTiles.push(-shadowBits);
  186.                 shadowDrawn = true;
  187.               }
  188.               lowerTiles.push(tileId2);
  189.           }
  190.           if (this._isHigherTile(tileId3)) {
  191.               upperTiles.push(tileId3);
  192.           } else {
  193.               if (underShadowTerrain !== -1 && !shadowDrawn && flags[tileId3] >> 12 !== underShadowTerrain) {
  194.                 lowerTiles.push(-shadowBits);
  195.                 shadowDrawn = true;
  196.               }
  197.               lowerTiles.push(tileId3);
  198.           }
  199.       }
  200.  
  201.       if (!shadowDrawn) {
  202.         lowerTiles.push(-shadowBits);
  203.       }
  204.  
  205.       var count = 1000 + this.animationCount - my;
  206.       var frameUpdated = (count % 30 === 0);
  207.       this._animationFrame = Math.floor(count / 30);
  208.  
  209.       var lastLowerTiles = this._readLastTiles(0, lx, ly);
  210.       if (!lowerTiles.equals(lastLowerTiles) ||
  211.               (Tilemap.isTileA1(tileId0) && frameUpdated)) {
  212.           this._lowerBitmap.clearRect(dx, dy, this._tileWidth, this._tileHeight);
  213.           for (var i = 0; i < lowerTiles.length; i++) {
  214.               var lowerTileId = lowerTiles[i];
  215.               if (lowerTileId < 0) {
  216.                   this._drawShadow(this._lowerBitmap, shadowBits, dx, dy);
  217.               } else if (lowerTileId >= tableEdgeVirtualId) {
  218.                   this._drawTableEdge(this._lowerBitmap, upperTileId1, dx, dy);
  219.               } else {
  220.                   this._drawTile(this._lowerBitmap, lowerTileId, dx, dy);
  221.               }
  222.           }
  223.           this._writeLastTiles(0, lx, ly, lowerTiles);
  224.       }
  225.  
  226.       var lastUpperTiles = this._readLastTiles(1, lx, ly);
  227.       if (!upperTiles.equals(lastUpperTiles)) {
  228.           this._upperBitmap.clearRect(dx, dy, this._tileWidth, this._tileHeight);
  229.           for (var j = 0; j < upperTiles.length; j++) {
  230.               this._drawTile(this._upperBitmap, upperTiles[j], dx, dy);
  231.           }
  232.           this._writeLastTiles(1, lx, ly, upperTiles);
  233.       }
  234.   };
  235.  
  236.  
  237. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement