ezmash

Upper Autotiles (MV)

Aug 5th, 2016
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // UpperAutotiles.js
  3. //=============================================================================
  4.  
  5. /*:
  6. * @plugindesc Allows you to use B-E tiles as autotile placeholders
  7. * @author Shaz
  8. *
  9. * @param Autotile Folder
  10. * @desc The img/ folder where autotiles will be saved.
  11. * @default autotiles
  12. *
  13. * @param Autotile Shift Region Start
  14. * @desc First region in a series of 48 to 'shift map' special autotiles
  15. * @default 200
  16. *
  17. * @help This plugin does not provide plugin commands.
  18. *
  19. * Notes:
  20. * This plugin does not affect the editor.  You can specify a B-E tile to
  21. * behave as an autotile, but it will only do so while playing the game.  The
  22. * editor will treat it as a regular B-E tile.
  23.  
  24. * As the editor does not allow B-E autotiles, there is no shift-mapping.  The
  25. * workaround is to use regions to override the default pattern selection.
  26.  
  27. * If using two B-E 'autotiles' on the same tile, with a region id, the region id
  28. * will affect both tiles (two tile layers but only one region layer).
  29.  
  30. * You cannot place A tiles on top of a B-E autotile, in the same way as you can
  31. * place A tiles on top of each other (eg fences, long grass).  If you draw
  32. * with a B-E autotile and then try to draw with an A tile on top, the B-E tile
  33. * will be replaced.
  34.  
  35. * All tile settings for the autotile will be EXACTLY as for the B-E tile -
  36. * passage, 4dir, ladder, bush, counter, damage floor and terrain tag.  Passage
  37. * should be X, O or star.  Strange things should be expected if you try to use
  38. * 4dir passage on B-E autotiles and fill an area with them.
  39. *
  40. *----------------------------------------------------------------------
  41. *
  42. * Compatibility: This plugin modifies (overwrites) the following functions:
  43. *   TileMap._paintTiles
  44. *   TileMap._drawAutotile
  45. * Plugins that alias either/both of these functions should be placed below
  46. * this plugin.  There will be compatibility issues with other plugins that
  47. * overwrite either/both of these functions.
  48. *
  49. *----------------------------------------------------------------------
  50. *
  51. * HOW TO USE:
  52. * 1. Create autotile images
  53. * 2. Add autotile images to B-E tilesheet images
  54. * 3. Add notes to tileset to define B-E autotiles
  55. * 4. Map with B-E autotiles
  56. * 5. Adjust (shift-map) using regions
  57. *
  58. *----------------------------------------------------------------------
  59. *
  60. * IN MORE DETAIL:
  61. * STEP 1 - Create autotile images
  62. * . Autotile images are saved as individual files (one file per autotile), not
  63. * combined into tilesheets similar to the A1-A4 tiles.
  64. * . Each autotile image should follow the standard 2x2 or 2x3 autotile layout.
  65. * . For animated tiles, repeat the standard layout for as many frames as
  66. * required.
  67. * . Save the images in the folder specified in the plugin parameters (it is
  68. * recommended to NOT use the tilesets folder).
  69. *
  70. * STEP 2 - Add autotile images to the B-E tilesheet images
  71. * . Use your autotile image to create a 'representative' single tile to be used
  72. * on the tile palette (a 48x48 tile).  This would normally be a combination of
  73. * the corner 24x24 pieces of the autotile, and shows how the autotile would look
  74. * if you used it on a single tile.
  75. * . Copy this image and paste it into your B-E tilesheet.  It is recommended to
  76. * use some sort of indicator to show, in the editor, that this will be treated
  77. * as an autotile when the game is run.  For example, a yellow star in the lower
  78. * left corner of the tile.
  79. *
  80. * STEP 3 - Add notes to tileset to define B-E autotiles
  81. * . For each 'autotile' tile on the B-E tabs, add a line to the tileset notes as
  82. * follows:
  83. *   <autotile: tileId size frames filename>
  84. * TILEID is the tile id of the real tile on your tileset.  To obtain this, you
  85. * can either count from the top left of the appropriate tab (tab B starts at 0,
  86. * C at 256, D at 512, and E at 768), or you can draw with that tile on a map
  87. * and add an event that sets a variable to the following script value:
  88. *   $gameMap.tileId(x, y, 3)
  89. * where x and y are the positions of the tile.  Then debug or show the variable
  90. * in a text box.  This must be done BEFORE adding the note to the tileset.
  91. * SIZE is either 2x2 or 2x3
  92. * FRAMES is how many frames of animation (will be 1 for non-animated tiles)
  93. * FILENAME is the name of the file in the autotile folder, without the extension
  94. * Example: <autotile: 8 2x2 4 waterfall> will use the waterfall autotile, which
  95. * is a 2x2 autotile with 4 frames of animation, in place of tile 8 (the first
  96. * tile on row 2 of the B tab).
  97. *
  98. * STEP 4 - Map with B-E autotiles
  99. * . Just draw with your autotiles.  In the editor, it will look like you're
  100. * placing individual tiles and they will not auto-shape like the A tiles do.
  101. * The magic happens when you play the game.
  102. * . Remember there are two layers of upper tiles, so you can place combinations
  103. * of regular tiles and 'autotiles' on top of each other.
  104. *
  105. * STEP 5 - Adjust (shift-map) using regions
  106. * . As the editor doesn't see these as autotiles, there is no shift-mapping
  107. * ability.  To overcome this issue, you can use regions to override the
  108. * automatic pattern building and force a specific pattern to be drawn.
  109. * . Allocate a set of 6 full rows of regions for this purpose, and put the first
  110. * region id into the plugin parameter.
  111. * . To determine what region id you should use to override the pattern, create
  112. * a new map and draw a filled rectangle, 8 columns wide by 6 rows high, with
  113. * the B-E tile.  Now grab the 6 rows of regions and draw over the tiles.  Set
  114. * the starting position and play so you can see how the tiles will be changed.
  115. * Find the correct tile, then look back at your map to see what region id was
  116. * used.  OR create an event that waits for a button press, then sets a variable
  117. * to the following script call:
  118. *   $gamePlayer.regionId()
  119. * Then debug or show the variable in a text box.
  120. *
  121. */
  122.  
  123. /*
  124. RESOURCE LIST - DO NOT MODIFY BELOW THIS LINE
  125. RESOURCE LIST - DO NOT MODIFY ABOVE THIS LINE
  126. */
  127.  
  128. (function() {
  129.  
  130.   var parameters = PluginManager.parameters('UpperAutotiles');
  131.   var autotileFolder = String(parameters['Autotile Folder'] || 'autotiles');
  132.   var autotileShiftRegionStart = Number(parameters['Autotile Shift Region Start'] || 200);
  133.   var autotileShiftRegionEnd = autotileShiftRegionStart + 47;
  134.  
  135.   /* Each autotile can make up to 48 different tile patterns.  In RMXP you can
  136.   see these by double-clicking on an autotile in the editor.  The arrays
  137.   below list the offset of the pattern, based on what surrounding tiles
  138.   have the same tile ID.  Only the 2x2 and 2x3 keys will be used in this
  139.   plugin - I have not included waterfall type tiles as per the A1 layout.
  140.   */
  141.  
  142.   Tilemap.AUTOTILE_GRAPH = {
  143.     '2x2': [15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,11,11,11,11,11,11,
  144.       11,11,11,11,11,11,11,11,11,11,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  145.       7,7,3,3,3,3,7,7,7,7,3,3,3,3,14,14,14,14,14,14,14,14,14,14,14,14,
  146.       14,14,14,14,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,7,7,
  147.       7,7,7,7,7,7,6,6,6,6,6,6,6,6,7,7,7,7,3,3,3,3,6,6,6,6,2,2,2,2,13,13,
  148.       13,13,13,13,13,13,13,13,13,13,13,13,13,13,9,9,9,9,9,9,9,9,9,9,9,9,
  149.       9,9,9,9,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,1,1,1,1,5,5,5,5,1,
  150.       1,1,1,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,8,8,8,8,8,8,
  151.       8,8,8,8,8,8,8,8,8,8,5,5,5,5,5,5,5,5,4,4,4,4,4,4,4,4,5,5,5,5,1,1,1,
  152.       1,4,4,4,4,0,0,0,0],
  153.     '2x3': [46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,43,43,43,43,43,43,
  154.       43,43,43,43,43,43,43,43,43,43,42,42,42,42,42,42,42,42,42,42,42,42,
  155.       42,42,42,42,35,35,35,35,34,34,34,34,35,35,35,35,34,34,34,34,45,45,
  156.       45,45,45,45,45,45,45,45,45,45,45,45,45,45,33,33,33,33,33,33,33,33,
  157.       33,33,33,33,33,33,33,33,37,37,37,37,37,37,37,37,36,36,36,36,36,36,
  158.       36,36,23,23,23,23,22,22,22,22,21,21,21,21,20,20,20,20,44,44,44,44,
  159.       44,44,44,44,44,44,44,44,44,44,44,44,41,41,40,40,41,41,40,40,41,41,
  160.       40,40,41,41,40,40,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
  161.       19,19,18,18,17,17,16,16,19,19,18,18,17,17,16,16,39,38,39,38,39,38,
  162.       39,38,39,38,39,38,39,38,39,38,31,30,29,28,31,30,29,28,31,30,29,28,
  163.       31,30,29,28,27,25,27,25,27,25,27,25,26,24,26,24,26,24,26,24,15,14,
  164.       13,12,11,10,9,8,7,6,5,4,3,2,1,0]
  165.   };
  166.  
  167.   function Autotile_Info() {
  168.     this.initialize.apply(this, arguments);
  169.   };
  170.  
  171.   Autotile_Info.prototype.initialize = function() {
  172.     this.startingTileId = 0;
  173.     this.imageSize = null;
  174.     this.frames = 0;
  175.     this.filename = null;
  176.   };
  177.  
  178.   var _upperAutotiles_DataManager_onLoad = DataManager.onLoad;
  179.   DataManager.onLoad = function(object) {
  180.     _upperAutotiles_DataManager_onLoad.call(this, object);
  181.     if (object === $dataMap) {
  182.       this.replaceSpecialAutotiles();
  183.     }
  184.     if (object === $dataTilesets) {
  185.       this.loadSpecialAutotileData();
  186.     }
  187.   };
  188.  
  189.   DataManager.loadSpecialAutotileData = function() {
  190.     for (var i = 0; i < $dataTilesets.length; i++) {
  191.       var tileset = $dataTilesets[i]
  192.       if (tileset) {
  193.         tileset.autotiles = {};
  194.         tileset.autotileInfos = [];
  195.         if (tileset.note !== undefined) {
  196.           this.extractSpecialAutotileInfo(i);
  197.         } else {
  198.           tileset.tileIdMax = Tilemap.TILE_ID_MAX;
  199.         }
  200.       }
  201.     }
  202.   };
  203.  
  204.   DataManager.extractSpecialAutotileInfo = function(tilesetId) {
  205.     var re = /<(autotile:)(\s*)(\d+)(\s*)(\d*x\d*)(\s*)(\d+)(\s*)(.*)>/g;
  206.     var tileset = $dataTilesets[tilesetId];
  207.     var startingTileId = Tilemap.TILE_ID_MAX;
  208.     var autotileInfoId = tileset.tilesetNames.length;
  209.     for (;;) {
  210.       var match = re.exec(tileset.note);
  211.       if (match) {
  212.         info = new Autotile_Info();
  213.         var tileId = Number(match[3]);
  214.         info.startingTileId = startingTileId;
  215.         info.imageSize = match[5];
  216.         info.frames = Number(match[7]);
  217.         info.filename = match[9];
  218.         tileset.autotiles[tileId] = autotileInfoId;
  219.         tileset.autotileInfos[autotileInfoId] = info;
  220.         for (var i = 0; i < 48; i++) {
  221.           tileset.flags.push(tileset.flags[tileId]);
  222.         }
  223.         startingTileId += 48;
  224.         autotileInfoId++;
  225.       } else {
  226.         break;
  227.       }
  228.     }
  229.     tileset.tileIdMax = startingTileId;
  230.   };
  231.  
  232.   DataManager.replaceSpecialAutotiles = function() {
  233.     var replacements = [];
  234.     var tileset = $dataTilesets[$dataMap.tilesetId];
  235.     var autotile = tileset.autotiles;
  236.  
  237.     // look at every tile for top two layers only (B-E tiles)
  238.     for (var z = 2; z < 4; z++) {
  239.       for (var x = 0; x < $dataMap.width; x++) {
  240.         for (var y = 0; y < $dataMap.height; y++) {
  241.  
  242.           // is this a special tile that needs to be replaced by an autotile?
  243.           var tileId = this.tileId(x, y, z);
  244.           if (autotile[tileId] !== undefined) {
  245.             var autotileInfo = tileset.autotileInfos[autotile[tileId]];
  246.  
  247.             // calculate new tile id
  248.             var newTileId = autotileInfo.startingTileId;
  249.             var regionId = this.regionId(x, y);
  250.  
  251.             // if there's a region in the expected range, just use that as an offset
  252.             // - this allows the equivalent of shift-mapping
  253.             if (regionId >= autotileShiftRegionStart && regionId <= autotileShiftRegionEnd) {
  254.               newTileId += regionId - autotileShiftRegionStart;
  255.             } else {
  256.               // otherwise calculate the offset based on surrounding tiles
  257.               var offset = this.isSameTile(x, y-1, z, tileId);
  258.               offset = offset << 1 | this.isSameTile(x-1, y, z, tileId)
  259.               offset = offset << 1 | this.isSameTile(x, y+1, z, tileId);
  260.               offset = offset << 1 | this.isSameTile(x+1, y, z, tileId);
  261.               offset = offset << 1 | this.isSameTile(x-1, y+1, z, tileId);
  262.               offset = offset << 1 | this.isSameTile(x+1, y+1, z, tileId);
  263.               offset = offset << 1 | this.isSameTile(x+1, y-1, z, tileId);
  264.               offset = offset << 1 | this.isSameTile(x-1, y-1, z, tileId);
  265.               newTileId += Tilemap.AUTOTILE_GRAPH[autotileInfo.imageSize][offset];
  266.             }
  267.  
  268.             // and save it away for later replacement (need to do this as a separate step)
  269.             replacements.push([x, y, z, newTileId]);
  270.           }
  271.         }
  272.       }
  273.     }
  274.  
  275.     // now go through and make all replacements in $dataMap
  276.     var j = replacements.length;
  277.     for (i = 0; i < j; i++)  {
  278.       var data = replacements[i]; // [x, y, z, newTileId]
  279.       this.setTileId(data[0], data[1], data[2], data[3]);
  280.     }
  281.   };
  282.  
  283.   DataManager.isSameTile = function(x, y, z, tileId) {
  284.     return (this.tileId(x, y, z) === tileId || this.tileId(x, y, 5-z) === tileId) ? 1 : 0;
  285.   };
  286.  
  287.   DataManager.regionId = function(x, y) {
  288.     return this.isCoordinateValid(x, y) ? this.tileId(x, y, 5) : 0;
  289.   };
  290.  
  291.   DataManager.isCoordinateValid = function(x, y) {
  292.     return x >= 0 && x < $dataMap.width && y >= 0 && y < $dataMap.height;
  293.   };
  294.  
  295.   DataManager.tileId = function(x, y, z) {
  296.     var width = $dataMap.width;
  297.     var height = $dataMap.height;
  298.     return $dataMap.data[(z * height + y) * width + x];
  299.   };
  300.  
  301.   DataManager.setTileId = function(x, y, z, tileId) {
  302.     var width = $dataMap.width;
  303.     var height = $dataMap.height;
  304.     $dataMap.data[(z * height + y) * width + x] = tileId;
  305.   };
  306.  
  307.   ImageManager.loadAutotile = function(filename, hue) {
  308.     return this.loadBitmap('img/' + autotileFolder + '/', filename, hue, true);
  309.   };
  310.  
  311.   var _upperAutotiles_Spriteset_Map_loadTileset = Spriteset_Map.prototype.loadTileset;
  312.   Spriteset_Map.prototype.loadTileset = function() {
  313.     _upperAutotiles_Spriteset_Map_loadTileset.call(this);
  314.     var tileset = this._tileset;
  315.     if (tileset) {
  316.       for (var i = tileset.tilesetNames.length; i < tileset.autotileInfos.length; i++) {
  317.         this._tilemap.bitmaps[i] = ImageManager.loadAutotile(tileset.autotileInfos[i].filename);
  318.       }
  319.       this._tilemap.refresh();
  320.     }
  321.   };
  322.  
  323.   var _upperAutotiles_Tilemap_isVisibleTile = Tilemap.isVisibleTile;
  324.   Tilemap.isVisibleTile = function(tileId) {
  325.     return _upperAutotiles_Tilemap_isVisibleTile || tileId < $dataTilesets[$dataMap.tilesetId].tileIdMax;
  326.   };
  327.  
  328.   Tilemap.isAnimatedUpperAutotile = function(args) {
  329.     for (i = 0; i < args.length; i++) {
  330.       var setNumber = this.getUpperAutotileSetNumber(args[i]);
  331.       if (setNumber > 0 && $dataTilesets[$dataMap.tilesetId].autotileInfos[setNumber].frames > 1) {
  332.         return true;
  333.       }
  334.     }
  335.     return false;
  336.   }
  337.  
  338.   Tilemap.getUpperAutotileSetNumber = function(tileId) {
  339.     if (tileId >= this.TILE_ID_MAX) {
  340.       return Math.floor((tileId - this.TILE_ID_MAX) / 48) + $dataTilesets[$dataMap.tilesetId].tilesetNames.length
  341.     } else {
  342.       return 0;
  343.     }
  344.   }
  345.  
  346.   /**
  347.   * @method _paintTiles
  348.   * @param {Number} startX
  349.   * @param {Number} startY
  350.   * @param {Number} x
  351.   * @param {Number} y
  352.   * @private
  353.   */
  354.   Tilemap.prototype._paintTiles = function(startX, startY, x, y) {
  355.     var tableEdgeVirtualId = 10000;
  356.     var mx = startX + x;
  357.     var my = startY + y;
  358.     var dx = (mx * this._tileWidth).mod(this._layerWidth);
  359.     var dy = (my * this._tileHeight).mod(this._layerHeight);
  360.     var lx = dx / this._tileWidth;
  361.     var ly = dy / this._tileHeight;
  362.     var tileId0 = this._readMapData(mx, my, 0);
  363.     var tileId1 = this._readMapData(mx, my, 1);
  364.     var tileId2 = this._readMapData(mx, my, 2);
  365.     var tileId3 = this._readMapData(mx, my, 3);
  366.     var shadowBits = this._readMapData(mx, my, 4);
  367.     var upperTileId1 = this._readMapData(mx, my - 1, 1);
  368.     var lowerTiles = [];
  369.     var upperTiles = [];
  370.     // only using tileId2 and tileId3 as 0 and 1 are for A tileset
  371.     // var animatedUpperTile = Tilemap.isAnimatedUpperAutotile([tileId0, tileId1, tileId2, tileId3]);
  372.     var animatedUpperTile = Tilemap.isAnimatedUpperAutotile([tileId2, tileId3]);
  373.  
  374.     if (this._isHigherTile(tileId0)) {
  375.       upperTiles.push(tileId0);
  376.     } else {
  377.       lowerTiles.push(tileId0);
  378.     }
  379.     if (this._isHigherTile(tileId1)) {
  380.       upperTiles.push(tileId1);
  381.     } else {
  382.       lowerTiles.push(tileId1);
  383.     }
  384.  
  385.     lowerTiles.push(-shadowBits);
  386.  
  387.     if (this._isTableTile(upperTileId1) && !this._isTableTile(tileId1)) {
  388.       if (!Tilemap.isShadowingTile(tileId0)) {
  389.         lowerTiles.push(tableEdgeVirtualId + upperTileId1);
  390.       }
  391.     }
  392.  
  393.     if (this._isOverpassPosition(mx, my)) {
  394.       upperTiles.push(tileId2);
  395.       upperTiles.push(tileId3);
  396.     } else {
  397.       if (this._isHigherTile(tileId2)) {
  398.         upperTiles.push(tileId2);
  399.       } else {
  400.         lowerTiles.push(tileId2);
  401.       }
  402.       if (this._isHigherTile(tileId3)) {
  403.         upperTiles.push(tileId3);
  404.       } else {
  405.         lowerTiles.push(tileId3);
  406.       }
  407.     }
  408.  
  409.     var lastLowerTiles = this._readLastTiles(0, lx, ly);
  410.     if (!lowerTiles.equals(lastLowerTiles) ||
  411.     (this._frameUpdated && (Tilemap.isTileA1(tileId0) || animatedUpperTile ))) {
  412.       this._lowerBitmap.clearRect(dx, dy, this._tileWidth, this._tileHeight);
  413.       for (var i = 0; i < lowerTiles.length; i++) {
  414.         var lowerTileId = lowerTiles[i];
  415.         if (lowerTileId < 0) {
  416.           this._drawShadow(this._lowerBitmap, shadowBits, dx, dy);
  417.         } else if (lowerTileId >= tableEdgeVirtualId) {
  418.           this._drawTableEdge(this._lowerBitmap, upperTileId1, dx, dy);
  419.         } else {
  420.           this._drawTile(this._lowerBitmap, lowerTileId, dx, dy);
  421.         }
  422.       }
  423.       this._writeLastTiles(0, lx, ly, lowerTiles);
  424.     }
  425.  
  426.     var lastUpperTiles = this._readLastTiles(1, lx, ly);
  427.     if (!upperTiles.equals(lastUpperTiles) ||
  428.     (this._frameUpdated && animatedUpperTile)) {
  429.       this._upperBitmap.clearRect(dx, dy, this._tileWidth, this._tileHeight);
  430.       for (var j = 0; j < upperTiles.length; j++) {
  431.         this._drawTile(this._upperBitmap, upperTiles[j], dx, dy);
  432.       }
  433.       this._writeLastTiles(1, lx, ly, upperTiles);
  434.     }
  435.   };
  436.  
  437.   /**
  438.   * @method _drawAutotile
  439.   * @param {Bitmap} bitmap
  440.   * @param {Number} tileId
  441.   * @param {Number} dx
  442.   * @param {Number} dy
  443.   * @private
  444.   */
  445.   Tilemap.prototype._drawAutotile = function(bitmap, tileId, dx, dy) {
  446.     var autotileTable = Tilemap.FLOOR_AUTOTILE_TABLE;
  447.     var kind = Tilemap.getAutotileKind(tileId);
  448.     var shape = Tilemap.getAutotileShape(tileId);
  449.     var tx = kind % 8;
  450.     var ty = Math.floor(kind / 8);
  451.     var bx = 0;
  452.     var by = 0;
  453.     var setNumber = 0;
  454.     var isTable = false;
  455.  
  456.     if (Tilemap.isTileA1(tileId)) {
  457.       var waterSurfaceIndex = [0, 1, 2, 1][this.animationFrame % 4];
  458.       setNumber = 0;
  459.       if (kind === 0) {
  460.         bx = waterSurfaceIndex * 2;
  461.         by = 0;
  462.       } else if (kind === 1) {
  463.         bx = waterSurfaceIndex * 2;
  464.         by = 3;
  465.       } else if (kind === 2) {
  466.         bx = 6;
  467.         by = 0;
  468.       } else if (kind === 3) {
  469.         bx = 6;
  470.         by = 3;
  471.       } else {
  472.         bx = Math.floor(tx / 4) * 8;
  473.         by = ty * 6 + Math.floor(tx / 2) % 2 * 3;
  474.         if (kind % 2 === 0) {
  475.           bx += waterSurfaceIndex * 2;
  476.         }
  477.         else {
  478.           bx += 6;
  479.           autotileTable = Tilemap.WATERFALL_AUTOTILE_TABLE;
  480.           by += this.animationFrame % 3;
  481.         }
  482.       }
  483.     } else if (Tilemap.isTileA2(tileId)) {
  484.       setNumber = 1;
  485.       bx = tx * 2;
  486.       by = (ty - 2) * 3;
  487.       isTable = this._isTableTile(tileId);
  488.     } else if (Tilemap.isTileA3(tileId)) {
  489.       setNumber = 2;
  490.       bx = tx * 2;
  491.       by = (ty - 6) * 2;
  492.       autotileTable = Tilemap.WALL_AUTOTILE_TABLE;
  493.     } else if (Tilemap.isTileA4(tileId)) {
  494.       setNumber = 3;
  495.       bx = tx * 2;
  496.       by = Math.floor((ty - 10) * 2.5 + (ty % 2 === 1 ? 0.5 : 0));
  497.       if (ty % 2 === 1) {
  498.         autotileTable = Tilemap.WALL_AUTOTILE_TABLE;
  499.       }
  500.     } else {
  501.       var tileset = $dataTilesets[$dataMap.tilesetId];
  502.       if (tileId >= Tilemap.TILE_ID_MAX && tileId <= tileset.tileIdMax) {
  503.         setNumber = Tilemap.getUpperAutotileSetNumber(tileId);
  504.         switch (tileset.autotileInfos[setNumber].imageSize) {
  505.           case '2x2': autotileTable = Tilemap.WALL_AUTOTILE_TABLE; break;
  506.           case '2x3': autotileTable = Tilemap.FLOOR_AUTOTILE_TABLE; break;
  507.         }
  508.         if (tileset.autotileInfos[setNumber].frames === 1) {
  509.           bx = 0;
  510.         } else {
  511.           bx = this.animationFrame % tileset.autotileInfos[setNumber].frames * 2;
  512.         }
  513.       }
  514.     }
  515.  
  516.     var table = autotileTable[shape];
  517.     var source = this.bitmaps[setNumber];
  518.  
  519.     if (table && source) {
  520.       var w1 = this._tileWidth / 2;
  521.       var h1 = this._tileHeight / 2;
  522.       for (var i = 0; i < 4; i++) {
  523.         var qsx = table[i][0];
  524.         var qsy = table[i][1];
  525.         var sx1 = (bx * 2 + qsx) * w1;
  526.         var sy1 = (by * 2 + qsy) * h1;
  527.         var dx1 = dx + (i % 2) * w1;
  528.         var dy1 = dy + Math.floor(i / 2) * h1;
  529.         if (isTable && (qsy === 1 || qsy === 5)) {
  530.           var qsx2 = qsx;
  531.           var qsy2 = 3;
  532.           if (qsy === 1) {
  533.             qsx2 = [0,3,2,1][qsx];
  534.           }
  535.           var sx2 = (bx * 2 + qsx2) * w1;
  536.           var sy2 = (by * 2 + qsy2) * h1;
  537.           bitmap.blt(source, sx2, sy2, w1, h1, dx1, dy1, w1, h1);
  538.           dy1 += h1/2;
  539.           bitmap.blt(source, sx1, sy1, w1, h1/2, dx1, dy1, w1, h1/2);
  540.         } else {
  541.           bitmap.blt(source, sx1, sy1, w1, h1, dx1, dy1, w1, h1);
  542.         }
  543.       }
  544.     }
  545.   };
  546.  
  547.   // Add image files to required assets list, to ensure they are included
  548.   // in a deployed project
  549.   var _upperAutotiles_Scene_Boot_start = Scene_Boot.prototype.start;
  550.   Scene_Boot.prototype.start = function() {
  551.     _upperAutotiles_Scene_Boot_start.call(this);
  552.  
  553.     if (Utils.isOptionValid('test') && !DataManager.isBattleTest() &&
  554.       !DataManager.isEventTest()) {
  555.  
  556.       // Read in this script
  557.       var fs = require('fs');
  558.       var path = window.location.pathname.replace(/(\/www|)\/[^\/]*$/, '/');
  559.       if (path.match(/^\/([A-Z]\:)/)) {
  560.         path = path.slice(1);
  561.       }
  562.       path = decodeURIComponent(path);
  563.       path = path + 'js/plugins/UpperAutotiles.js';
  564.       if (fs.existsSync(path)) {
  565.         var source = fs.readFileSync(path, { encoding: 'utf8' });
  566.         source = source.split('\r\n');
  567.         var resListStart = source.indexOf('RESOURCE LIST - DO NOT MODIFY BELOW THIS LINE');
  568.         var resListEnd = source.indexOf('RESOURCE LIST - DO NOT MODIFY ABOVE THIS LINE');
  569.         if (resListStart === -1 || resListEnd === -1) {
  570.           console.log('ERROR - Unable to create resource list for UpperAutotiles.js');
  571.           console.log('Please redownload the script and paste over your current version');
  572.         } else {
  573.           // remove previous list of resources
  574.           source.splice(resListStart + 1, resListEnd - resListStart - 1);
  575.           // build list of new resources
  576.           var resources = [];
  577.           $dataTilesets.forEach(function(tileset) {
  578.             if (tileset) {
  579.               tileset.autotileInfos.forEach(function(autotileInfo) {
  580.                 if (resources.indexOf(autotileInfo.filename) === -1) {
  581.                   resources.push(autotileInfo.filename);
  582.                 }
  583.               });
  584.             }
  585.           });
  586.           // add resource list to script
  587.           var inspos = resListStart + 1;
  588.           resources.forEach(function(filename) {
  589.             var tag = '* @requiredAssets img/' + autotileFolder + '/' + filename;
  590.             source.splice(inspos, 0, tag);
  591.           });
  592.           // and save the file
  593.           source = source.join('\r\n');
  594.           fs.writeFileSync(path, source, { encoding: 'utf8' });
  595.         }
  596.       }
  597.     }
  598.   };
  599.  
  600. })();
Advertisement
Add Comment
Please, Sign In to add comment