ezmash

Terrain Tags 1.6.1

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