Advertisement
section31-tech

LeTBS.js

Dec 17th, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. #=============================================================================
  3. # Lecode's Tactical Battle System
  4. # LeTBS.js
  5. # By Lecode
  6. # Version 0.6
  7. #-----------------------------------------------------------------------------
  8. # TERMS OF USE
  9. #-----------------------------------------------------------------------------
  10. # This plugin and all its add-ons made by Lecode is under the MIT License.
  11. # (http://choosealicense.com/licenses/mit/)
  12. # In addition, you should keep this header.
  13. #-----------------------------------------------------------------------------
  14. # Version History
  15. #-----------------------------------------------------------------------------
  16. # - 0.0 : Beta started.
  17. # - 0.1 : Battles start and end correctly.
  18. #         Troop events are correctly triggered in battle. (mostly)
  19. #         Added an end window which appears when pressing ESC on the command window.
  20. #         That window allows you to escape and change options.
  21. #         Added the tags and parameters for one time move only and one time offense only.
  22. #         Fixed some errors in the demo.
  23. # - 0.2 : The Item command is functional.
  24. #         Added custom move scopes and options.
  25. #         Downgraded the turn order visual for now to fix some bugs.
  26. #         Added a new option for turn order: "fair repartition".
  27. #         Status window is updated when a target is selected.
  28. #         There's now a help window. Status window is shifted if needed.
  29. # - 0.3 : The mouse is supported.
  30. #         Prototype of the projectiles system.
  31. #         Prototype of the AI.
  32. #         Fixed a bug and improved the Turn Order Visual Version A.
  33. #         85% of the tile effects and marks system.
  34. #         Fixed a bug with the line of sight.
  35. # - 0.4 : Now there is ony one unique tag.
  36. #         Support custom sprites. (Yay !)
  37. #         AI improved.
  38. #         Added AI patterns. They can be used in AI commands.
  39. #         Now each entity has his own sequence system. This allows simultaneous sequences.
  40. #         Added some sequence commands.
  41. #         Now a sequence is played for the following events:
  42. #         battle start, turn start, victory, death, damaged, healed, buffed, weakened.
  43. # - 0.5 : Projectile system completed
  44. #         Tile, Mark & Aura effects completed
  45. #         Fixed and added some sequence commands
  46. #         Fixed some bugs
  47. #         Created DamagePopupEX
  48. # - 0.6 : Added these sequence commands: call_for_every_cell, call_for_every_entity
  49. #         Added these sequence commands: save_cells, save_entities
  50. #         Support directional scope
  51. #         Fixed a bug where custom scopes ignore line of sight
  52. #=============================================================================
  53. */
  54. var Imported = Imported || {};
  55. Imported["LeTBS"] = true;
  56.  
  57. var Lecode = Lecode || {};
  58. Lecode.S_TBS = {};
  59. /*:
  60.  * @plugindesc A tactical battle system with awesome features
  61.  * @author Lecode
  62.  * @version 0.5
  63.  *
  64.  * @param Ally Color Cell
  65.  * @desc Color of actor cells.
  66.  * @default #0FC50B
  67.  *
  68.  * @param Enemy Color Cell
  69.  * @desc Color of enemy cells.
  70.  * @default #C50B1B
  71.  *
  72.  * @param Cell Opacity Color
  73.  * @desc Opacity of the placement cells.
  74.  * @default 175
  75.  *
  76.  * @param Placed Animation
  77.  * @desc Animation ID when a battler is placed.
  78.  * @default 124
  79.  *
  80.  * @param -- Misc --
  81.  * @desc ...
  82.  * @default
  83.  *
  84.  * @param Exploration Input
  85.  * @desc Input to trigger exploration.
  86.  * @default shift
  87.  *
  88.  * @param Opacity Input
  89.  * @desc Input to change windows' opacity.
  90.  * @default control
  91.  *
  92.  * @param Min Input Opacity
  93.  * @desc Minimum opacity of windows.
  94.  * @default 0
  95.  *
  96.  * @param Opacity Steps
  97.  * @desc Value of opacity to reduce when inputed.
  98.  * @default 10
  99.  *
  100.  * @param Battle Start Sprite Delay
  101.  * @desc Duration of the battle start sprite.
  102.  * @default 50
  103.  *
  104.  * @param Turn Order Fair Repartition ?
  105.  * @desc Allow a fair repartition of the turn order ?
  106.  * @default true
  107.  *
  108.  * @param Destination Duration
  109.  * @desc Duration of the destination sprite (when a cell is selected).
  110.  * @default 60
  111.  *
  112.  * @param -- Scopes --
  113.  * @desc ...
  114.  * @default
  115.  *
  116.  * @param Scope Cell Width
  117.  * @desc Width of cells.
  118.  * @default 46
  119.  *
  120.  * @param Scope Cell Height
  121.  * @desc Height of cells.
  122.  * @default 46
  123.  *
  124.  * @param Obstacle Region Id
  125.  * @desc Region ID for obstacles.
  126.  * @default 250
  127.  *
  128.  * @param -- Move Action --
  129.  * @desc ...
  130.  * @default
  131.  *
  132.  * @param Default Move Scope
  133.  * @desc Default move scope data.
  134.  * @default circle(_mp_)
  135.  *
  136.  * @param Default Move Points
  137.  * @desc Default amount of move points.
  138.  * @default 3
  139.  *
  140.  * @param Move Scope Color
  141.  * @desc Color of the move scope.
  142.  * @default #0A5C85
  143.  *
  144.  * @param Move Scope Opacity
  145.  * @desc Opacity of the move scope.
  146.  * @default 175
  147.  *
  148.  * @param Invalid Move Scope Opacity
  149.  * @desc Opacity of the move scope when cells are invalid.
  150.  * @default 60
  151.  *
  152.  * @param Selected Move Scope Opacity
  153.  * @desc Opacity of the move scope when cells are selected.
  154.  * @default 255
  155.  *
  156.  * @param Enable Directional Facing
  157.  * @desc Battlers will be allowed to change their direction.
  158.  * @default true
  159.  *
  160.  * @param -- Attack Action --
  161.  * @desc ...
  162.  * @default
  163.  *
  164.  * @param Default Attack Animation
  165.  * @desc Default attack animation.
  166.  * @default 1
  167.  *
  168.  * @param Default Attack Sequence
  169.  * @desc Default attack sequence.
  170.  * @default atk
  171.  *
  172.  * @param Default Attack Scope
  173.  * @desc Default attack scope data.
  174.  * @default circle(1)
  175.  *
  176.  * @param Default Attack Ao E
  177.  * @desc Default attack aoe data.
  178.  * @default circle(0)
  179.  *
  180.  * @param Attack Scope Color
  181.  * @desc Color of the attack scope.
  182.  * @default #DF3A01
  183.  *
  184.  * @param Attack Scope Opacity
  185.  * @desc Opacity of the attack scope.
  186.  * @default 175
  187.  *
  188.  * @param Invalid Attack Scope Opacity
  189.  * @desc Opacity of the attack scope when cells are invalid.
  190.  * @default 60
  191.  *
  192.  * @param Selected Attack Scope Opacity
  193.  * @desc Opacity of the attack scope when cells are selected.
  194.  * @default 255
  195.  *
  196.  * @param -- Skill Action --
  197.  * @desc ...
  198.  * @default
  199.  *
  200.  * @param Default Skill Sequence
  201.  * @desc Default skill sequence.
  202.  * @default skill
  203.  *
  204.  * @param Default Skill Scope
  205.  * @desc Default skill scope data.
  206.  * @default circle(3)
  207.  *
  208.  * @param Default Skill Ao E
  209.  * @desc Default skill aoe data.
  210.  * @default circle(0)
  211.  *
  212.  * @param Skill Scope Color
  213.  * @desc Color of the skill scope.
  214.  * @default #DF3A01
  215.  *
  216.  * @param Skill Scope Opacity
  217.  * @desc Opacity of the skill scope.
  218.  * @default 175
  219.  *
  220.  * @param Invalid Skill Scope Opacity
  221.  * @desc Opacity of the skill scope when cells are invalid.
  222.  * @default 60
  223.  *
  224.  * @param Selected Skill Scope Opacity
  225.  * @desc Opacity of the skill scope when cells are selected.
  226.  * @default 255
  227.  *
  228.  * @param -- Item Action --
  229.  * @desc ...
  230.  * @default
  231.  *
  232.  * @param Default Item Sequence
  233.  * @desc Default item sequence.
  234.  * @default item
  235.  *
  236.  * @param Default Item Scope
  237.  * @desc Default item scope data.
  238.  * @default circle(3)
  239.  *
  240.  * @param Default Item Ao E
  241.  * @desc Default item aoe data.
  242.  * @default circle(0)
  243.  *
  244.  * @param Item Scope Color
  245.  * @desc Color of the item scope.
  246.  * @default #DF01D7
  247.  *
  248.  * @param Item Scope Opacity
  249.  * @desc Opacity of the item scope.
  250.  * @default 175
  251.  *
  252.  * @param Invalid Item Scope Opacity
  253.  * @desc Opacity of the item scope when cells are invalid.
  254.  * @default 60
  255.  *
  256.  * @param Selected Item Scope Opacity
  257.  * @desc Opacity of the item scope when cells are selected.
  258.  * @default 255
  259.  *
  260.  * @param -- Directional Damage --
  261.  * @desc ...
  262.  * @default
  263.  *
  264.  * @param Back Directional Damage Effects
  265.  * @desc Damage % when a battler is hit on the back.
  266.  * @default 15
  267.  *
  268.  * @param Side Directional Damage Effects
  269.  * @desc Damage % when a battler is hit on the sides.
  270.  * @default 0
  271.  *
  272.  * @param Face Directional Damage Effects
  273.  * @desc Damage % when a battler is hit on the face.
  274.  * @default -10
  275.  *
  276.  * @param -- Collision Damage --
  277.  * @desc ...
  278.  * @default
  279.  *
  280.  * @param Default Collision Formula
  281.  * @desc Formula to evaluate collision damage.
  282.  * @default b.mhp * 0.05 * (distance-covered)
  283.  *
  284.  * @param Collission Damage Chain Rate
  285.  * @desc Collision damage chain rate.
  286.  * @default 0.3
  287.  *
  288.  * @param -- Motions --
  289.  * @desc ...
  290.  * @default
  291.  *
  292.  * @param Battlers Move Speed
  293.  * @desc Default move speed.
  294.  * @default 4
  295.  *
  296.  * @param Battlers Frame Delay
  297.  * @desc Default delay value between sprites frames.
  298.  * @default 10
  299.  *
  300.  * @param -- AI --
  301.  * @desc ...
  302.  * @default
  303.  *
  304.  * @param Default Ai Pattern
  305.  * @desc Default AI pattern.
  306.  * @default melee_fighter
  307.  *
  308.  * @param Ai Wait Time
  309.  * @desc AI wait time.
  310.  * @default 5
  311.  *
  312.  * @param -- Actions Restrictions --
  313.  * @desc ...
  314.  * @default
  315.  *
  316.  * @param One Time Move
  317.  * @desc Enable the one time move feature. (See doc)
  318.  * @default false
  319.  *
  320.  * @param One Time Offense
  321.  * @desc Enable the one time offense feature. (See doc)
  322.  * @default true
  323.  *
  324.  * @param Auto Pass
  325.  * @desc Enable the auto pass feature. (See doc)
  326.  * @default true
  327.  *
  328.  * @param -- Battle End --
  329.  * @desc ...
  330.  * @default
  331.  *
  332.  * @param Escape Sound
  333.  * @desc Sound when the party try to escape.
  334.  * @default Buzzer2
  335.  *
  336.  * @param End Of Battle Wait
  337.  * @desc Wait amount before the end of the battle.
  338.  * @default 60
  339.  *
  340.  * @param Collapse Animation
  341.  * @desc Default collapse animation.
  342.  * @default 136
  343.  *
  344.  * @help
  345.  * See the documentation
  346.  */
  347. //#=============================================================================
  348.  
  349.  
  350. /*-------------------------------------------------------------------------
  351. * Get Parameters
  352. -------------------------------------------------------------------------*/
  353. var parameters = PluginManager.parameters('LeTBS');
  354.  
  355. Lecode.S_TBS.allyColorCell = String(parameters["Ally Color Cell"] || "#0FC50B"); //  (): Color of actor cells.
  356. Lecode.S_TBS.enemyColorCell = String(parameters["Enemy Color Cell"] || "#C50B1B"); //  (): Color of enemy cells.
  357. Lecode.S_TBS.placementCellOpacity = Number(parameters["Cell Opacity Color"] || 175); //  (Cell Opacity Color): Opacity of the placement cells.
  358. Lecode.S_TBS.placedBattlerAnim = Number(parameters["Placed Animation"] || 124); //  (Placed Animation): Animation ID when a battler is placed.
  359. // Divider: -- Misc --
  360. Lecode.S_TBS.explorationInput = String(parameters["Exploration Input"] || "shift"); //  (): Input to trigger exploration.
  361. Lecode.S_TBS.opacityInput = String(parameters["Opacity Input"] || "control"); //  (): Input to change windows' opacity.
  362. Lecode.S_TBS.minInputOpacity = Number(parameters["Min Input Opacity"] || 0); //  (): Minimum opacity of windows.
  363. Lecode.S_TBS.inputOpacityDecreaseSteps = Number(parameters["Opacity Steps"] || 10); //  (Opacity Steps): Value of opacity to reduce when inputed.
  364. Lecode.S_TBS.battleStartSpriteDelay = Number(parameters["Battle Start Sprite Delay"] || 50); //  (): Duration of the battle start sprite.
  365. Lecode.S_TBS.turnOrderFairRepartition = String(parameters["Turn Order Fair Repartition ?"] || 'true') === 'true'; //  (Turn Order Fair Repartition ?): Allow a fair repartition of the turn order ?
  366. Lecode.S_TBS.destinationDuration = Number(parameters["Destination Duration"] || 60); //  (): Duration of the destination sprite (when a cell is selected).
  367. // Divider: -- Scopes --
  368. Lecode.S_TBS.scopeCellWidth = Number(parameters["Scope Cell Width"] || 46); //  (): Width of cells.
  369. Lecode.S_TBS.scopeCellHeight = Number(parameters["Scope Cell Height"] || 46); //  (): Height of cells.
  370. Lecode.S_TBS.obstacleRegionId = Number(parameters["Obstacle Region Id"] || 250); //  (): Region ID for obstacles.
  371. // Divider: -- Move Action --
  372. Lecode.S_TBS.defaultMoveScope = String(parameters["Default Move Scope"] || "circle(_mp_)"); //  (): Default move scope data.
  373. Lecode.S_TBS.defaultMovePoints = Number(parameters["Default Move Points"] || 3); //  (): Default amount of move points.
  374. Lecode.S_TBS.moveColorCell = String(parameters["Move Scope Color"] || "#0A5C85"); //  (Move Scope Color): Color of the move scope.
  375. Lecode.S_TBS.moveCellOpacity = Number(parameters["Move Scope Opacity"] || 175); //  (Move Scope Opacity): Opacity of the move scope.
  376. Lecode.S_TBS.moveInvalidCellOpacity = Number(parameters["Invalid Move Scope Opacity"] || 60); //  (Invalid Move Scope Opacity): Opacity of the move scope when cells are invalid.
  377. Lecode.S_TBS.moveSelectedCellOpacity = Number(parameters["Selected Move Scope Opacity"] || 255); //  (Selected Move Scope Opacity): Opacity of the move scope when cells are selected.
  378. Lecode.S_TBS.enableDirectionalFacing = String(parameters["Enable Directional Facing"] || 'true') === 'true'; //  (): Battlers will be allowed to change their direction.
  379. // Divider: -- Attack Action --
  380. Lecode.S_TBS.defaultAttackAnimation = Number(parameters["Default Attack Animation"] || 1); //  (): Default attack animation.
  381. Lecode.S_TBS.defaultAttackSequence = String(parameters["Default Attack Sequence"] || "atk"); //  (): Default attack sequence.
  382. Lecode.S_TBS.defaultAttackScope = String(parameters["Default Attack Scope"] || "circle(1)"); //  (): Default attack scope data.
  383. Lecode.S_TBS.defaultAttackAoE = String(parameters["Default Attack Ao E"] || "circle(0)"); //  (): Default attack aoe data.
  384. Lecode.S_TBS.attackColorCell = String(parameters["Attack Scope Color"] || "#DF3A01"); //  (Attack Scope Color): Color of the attack scope.
  385. Lecode.S_TBS.attackCellOpacity = Number(parameters["Attack Scope Opacity"] || 175); //  (Attack Scope Opacity): Opacity of the attack scope.
  386. Lecode.S_TBS.attackInvalidCellOpacity = Number(parameters["Invalid Attack Scope Opacity"] || 60); //  (Invalid Attack Scope Opacity): Opacity of the attack scope when cells are invalid.
  387. Lecode.S_TBS.attackSelectedCellOpacity = Number(parameters["Selected Attack Scope Opacity"] || 255); //  (Selected Attack Scope Opacity): Opacity of the attack scope when cells are selected.
  388. // Divider: -- Skill Action --
  389. Lecode.S_TBS.defaultSkillSequence = String(parameters["Default Skill Sequence"] || "skill"); //  (): Default skill sequence.
  390. Lecode.S_TBS.defaultSkillScope = String(parameters["Default Skill Scope"] || "circle(3)"); //  (): Default skill scope data.
  391. Lecode.S_TBS.defaultSkillAoE = String(parameters["Default Skill Ao E"] || "circle(0)"); //  (): Default skill aoe data.
  392. Lecode.S_TBS.skillColorCell = String(parameters["Skill Scope Color"] || "#DF3A01"); //  (Skill Scope Color): Color of the skill scope.
  393. Lecode.S_TBS.skillCellOpacity = Number(parameters["Skill Scope Opacity"] || 175); //  (Skill Scope Opacity): Opacity of the skill scope.
  394. Lecode.S_TBS.skillInvalidCellOpacity = Number(parameters["Invalid Skill Scope Opacity"] || 60); //  (Invalid Skill Scope Opacity): Opacity of the skill scope when cells are invalid.
  395. Lecode.S_TBS.skillSelectedCellOpacity = Number(parameters["Selected Skill Scope Opacity"] || 255); //  (Selected Skill Scope Opacity): Opacity of the skill scope when cells are selected.
  396. // Divider: -- Item Action --
  397. Lecode.S_TBS.defaultItemSequence = String(parameters["Default Item Sequence"] || "item"); //  (): Default item sequence.
  398. Lecode.S_TBS.defaultItemScope = String(parameters["Default Item Scope"] || "circle(3)"); //  (): Default item scope data.
  399. Lecode.S_TBS.defaultItemAoE = String(parameters["Default Item Ao E"] || "circle(0)"); //  (): Default item aoe data.
  400. Lecode.S_TBS.ItemColorCell = String(parameters["Item Scope Color"] || "#DF01D7"); //  (Item Scope Color): Color of the item scope.
  401. Lecode.S_TBS.ItemCellOpacity = Number(parameters["Item Scope Opacity"] || 175); //  (Item Scope Opacity): Opacity of the item scope.
  402. Lecode.S_TBS.ItemInvalidCellOpacity = Number(parameters["Invalid Item Scope Opacity"] || 60); //  (Invalid Item Scope Opacity): Opacity of the item scope when cells are invalid.
  403. Lecode.S_TBS.ItemSelectedCellOpacity = Number(parameters["Selected Item Scope Opacity"] || 255); //  (Selected Item Scope Opacity): Opacity of the item scope when cells are selected.
  404. // Divider: -- Directional Damage --
  405. Lecode.S_TBS.backDirectionalDamageEffects = Number(parameters["Back Directional Damage Effects"] || 15); //  (): Damage % when a battler is hit on the back.
  406. Lecode.S_TBS.sideDirectionalDamageEffects = Number(parameters["Side Directional Damage Effects"] || 0); //  (): Damage % when a battler is hit on the sides.
  407. Lecode.S_TBS.faceDirectionalDamageEffects = Number(parameters["Face Directional Damage Effects"] || -10); //  (): Damage % when a battler is hit on the face.
  408. // Divider: -- Collision Damage --
  409. Lecode.S_TBS.defaultCollisionFormula = String(parameters["Default Collision Formula"] || "b.mhp * 0.05 * (distance-covered)"); //  (): Formula to evaluate collision damage.
  410. Lecode.S_TBS.collissionDamageChainRate = Number(parameters["Collission Damage Chain Rate"] || 0.3); //  (): Collision damage chain rate.
  411. // Divider: -- Motions --
  412. Lecode.S_TBS.battlersMoveSpeed = Number(parameters["Battlers Move Speed"] || 4); //  (): Default move speed.
  413. Lecode.S_TBS.battlersFrameDelay = Number(parameters["Battlers Frame Delay"] || 10); //  (): Default delay value between sprites frames.
  414. // Divider: -- AI --
  415. Lecode.S_TBS.defaultAiPattern = String(parameters["Default Ai Pattern"] || "melee_fighter"); //  (): Default AI pattern.
  416. Lecode.S_TBS.aiWaitTime = Number(parameters["Ai Wait Time"] || 5); //  (): AI wait time.
  417. // Divider: -- Actions Restrictions --
  418. Lecode.S_TBS.oneTimeMove = String(parameters["One Time Move"] || 'false') === 'true'; //  (): Enable the one time move feature. (See doc)
  419. Lecode.S_TBS.oneTimeOffense = String(parameters["One Time Offense"] || 'true') === 'true'; //  (): Enable the one time offense feature. (See doc)
  420. Lecode.S_TBS.autoPass = String(parameters["Auto Pass"] || 'true') === 'true'; //  (): Enable the auto pass feature. (See doc)
  421. // Divider: -- Battle End --
  422. Lecode.S_TBS.escapeSound = String(parameters["Escape Sound"] || "Buzzer2"); //  (): Sound when the party try to escape.
  423. Lecode.S_TBS.endOfBattleWait = Number(parameters["End Of Battle Wait"] || 60); //  (): Wait amount before the end of the battle.
  424. Lecode.S_TBS.collapseAnimation = Number(parameters["Collapse Animation"] || 136); //  (): Default collapse animation.
  425.  
  426.  
  427. Lecode.S_TBS.drawLimits = false;
  428.  
  429. /*-------------------------------------------------------------------------
  430. * Spriteset_BattleTBS
  431. -------------------------------------------------------------------------*/
  432. function Spriteset_BattleTBS() {
  433.     this.initialize.apply(this, arguments);
  434. }
  435.  
  436. Spriteset_BattleTBS.prototype = Object.create(Spriteset_Map.prototype);
  437. Spriteset_BattleTBS.prototype.constructor = Spriteset_BattleTBS;
  438.  
  439. Spriteset_BattleTBS.prototype.initialize = function () {
  440.     Spriteset_Map.prototype.initialize.call(this);
  441.     this.createBattleLayers();
  442. };
  443.  
  444. Spriteset_BattleTBS.prototype.createCharacters = function () {
  445.     this._characterSprites = [];
  446.     $gameMap.events().forEach(function (event) {
  447.         if (event.event().note.match(/tbs_event/i))
  448.             this._characterSprites.push(new Sprite_Character(event));
  449.     }, this);
  450.  
  451.     for (var i = 0; i < this._characterSprites.length; i++) {
  452.         this._tilemap.addChild(this._characterSprites[i]);
  453.     }
  454. };
  455.  
  456. Spriteset_BattleTBS.prototype.createBattleLayers = function () {
  457.     //-Ground entities
  458.     this._groundEntitiesLayer = new Sprite();
  459.     this._groundEntitiesLayer.z = 0;
  460.     this._tilemap.addChild(this._groundEntitiesLayer);
  461.     //-Scopes
  462.     this._scopesLayer = new TBSScopeLayer();
  463.     this._scopesLayer.z = 2;
  464.     this._tilemap.addChild(this._scopesLayer);
  465.     this._scopesLayer.createSelectionLayer();
  466.     //-Ground
  467.     this._groundLayer = new Sprite();
  468.     this._groundLayer.z = 2;
  469.     this._tilemap.addChild(this._groundLayer);
  470.     //-Battlers
  471.     this._battlersLayer = new Sprite();
  472.     this._battlersLayer.z = 2;
  473.     this._tilemap.addChild(this._battlersLayer);
  474.     //-Animations
  475.     this._animationsLayer = new TBSMapAnimation();
  476.     this._animationsLayer.z = 4;
  477.     this._tilemap.addChild(this._animationsLayer);
  478.     //-Movable Infos
  479.     this._movableInfoLayer = new Sprite();
  480.     this._movableInfoLayer.z = 6;
  481.     this._tilemap.addChild(this._movableInfoLayer);
  482.     //-Fixed Infos
  483.     this._fixedInfoLayer = new Sprite();
  484.     this._fixedInfoLayer.z = 6;
  485.     this._tilemap.addChild(this._fixedInfoLayer);
  486.     //-Debug
  487.     var bitmap = new Bitmap(Graphics.width, Graphics.height);
  488.     this._debugLayer = new Sprite(bitmap);
  489.     this._debugLayer.z = 7;
  490.     this._tilemap.addChild(this._debugLayer);
  491. };
  492.  
  493.  
  494. /*-------------------------------------------------------------------------
  495. * TBSScopeLayer
  496. -------------------------------------------------------------------------*/
  497. function TBSScopeLayer() {
  498.     this.initialize.apply(this, arguments);
  499. }
  500. TBSScopeLayer.prototype = Object.create(Sprite.prototype);
  501. TBSScopeLayer.prototype.constructor = TBSScopeLayer;
  502.  
  503. TBSScopeLayer.prototype.initialize = function () {
  504.     var w = $gameMap.width() * $gameMap.tileWidth();
  505.     var h = $gameMap.height() * $gameMap.tileHeight();
  506.     var bitmap = new Bitmap(w, h);
  507.     Sprite.prototype.initialize.call(this, bitmap);
  508.     this._selectionLayer = null;
  509. };
  510.  
  511. TBSScopeLayer.prototype.createSelectionLayer = function () {
  512.     this._selectionLayer = new TBSScopeLayer();
  513.     this._selectionLayer.z = 2;
  514.     this.parent.addChild(this._selectionLayer);
  515. };
  516.  
  517. TBSScopeLayer.prototype.update = function () {
  518.     Sprite.prototype.update.call(this);
  519.     if (this._selectionLayer) {
  520.         this._selectionLayer.x = this.x;
  521.         this._selectionLayer.y = this.y;
  522.     }
  523. };
  524.  
  525. TBSScopeLayer.prototype.clear = function () {
  526.     this.bitmap.clear();
  527. };
  528.  
  529. TBSScopeLayer.prototype.clearSelection = function () {
  530.     this._selectionLayer.clear();
  531. };
  532.  
  533. TBSScopeLayer.prototype.drawCell = function (x, y, opacity, color) {
  534.     var w = Lecode.S_TBS.scopeCellWidth;
  535.     var h = Lecode.S_TBS.scopeCellHeight;
  536.     var sx = $gameMap.tileWidth() - w;
  537.     var sy = $gameMap.tileHeight() - h;
  538.     w -= sx;
  539.     h -= sy;
  540.     sx += x * $gameMap.tileWidth();
  541.     sy += y * $gameMap.tileHeight();
  542.     this.bitmap.paintOpacity = opacity;
  543.     this.bitmap.fillRect(sx, sy, w, h, color);
  544. };
  545.  
  546. TBSScopeLayer.prototype.drawSelectionCell = function (x, y, opacity, color) {
  547.     this._selectionLayer.drawCell(x, y, opacity, color);
  548. };
  549.  
  550.  
  551. /*-------------------------------------------------------------------------
  552. * TBSMapAnimation
  553. -------------------------------------------------------------------------*/
  554. function TBSMapAnimation() {
  555.     this.initialize.apply(this, arguments);
  556. }
  557.  
  558. TBSMapAnimation.prototype = Object.create(Sprite_Base.prototype);
  559. TBSMapAnimation.prototype.constructor = TBSMapAnimation;
  560.  
  561. TBSMapAnimation.prototype.initialize = function (battler) {
  562.     Sprite_Base.prototype.initialize.call(this);
  563.     this._cell = null;
  564.     this._target = null;
  565. };
  566.  
  567. TBSMapAnimation.prototype.update = function () {
  568.     Sprite_Base.prototype.update.call(this);
  569. };
  570.  
  571. TBSMapAnimation.prototype.newAnimation = function (id, mirror, delay, cell, target) {
  572.     this._cell = cell;
  573.     this._target = target;
  574.     this.startAnimation($dataAnimations[id], mirror, delay);
  575. };
  576.  
  577. TBSMapAnimation.prototype.startAnimation = function (animation, mirror, delay) {
  578.     var sprite = new Sprite_TBSAnimation();
  579.     sprite.setup(this._target || this._effectTarget, animation, mirror, delay, this._cell);
  580.     this.addChild(sprite);
  581.     this._animationSprites.push(sprite);
  582. };
  583.  
  584.  
  585. /*-------------------------------------------------------------------------
  586. * Sprite_TBSAnimation
  587. -------------------------------------------------------------------------*/
  588. function Sprite_TBSAnimation() {
  589.     this.initialize.apply(this, arguments);
  590. }
  591.  
  592. Sprite_TBSAnimation.prototype = Object.create(Sprite_Animation.prototype);
  593. Sprite_TBSAnimation.prototype.constructor = Sprite_TBSAnimation;
  594.  
  595. Sprite_TBSAnimation.prototype.initialize = function (battler) {
  596.     Sprite_Animation.prototype.initialize.call(this);
  597.     this._cell = null;
  598. };
  599.  
  600. Sprite_TBSAnimation.prototype.update = function () {
  601.     Sprite_Animation.prototype.update.call(this);
  602. };
  603.  
  604. Sprite_TBSAnimation.prototype.setup = function (target, animation, mirror, delay, cell) {
  605.     this._cell = cell;
  606.     this._target = target;
  607.     this._animation = animation;
  608.     this._mirror = mirror;
  609.     this._delay = delay;
  610.     if (this._animation) {
  611.         this.remove();
  612.         this.setupRate();
  613.         this.setupDuration();
  614.         this.loadBitmaps();
  615.         this.createSprites();
  616.     }
  617. };
  618.  
  619. Sprite_TBSAnimation.prototype.updatePosition = function () {
  620.     if (this._animation.position === 3) {
  621.         this.x = this.parent.width / 2;
  622.         this.y = this.parent.height / 2;
  623.     } else {
  624.         var w = $gameMap.tileWidth();
  625.         var h = $gameMap.tileHeight();
  626.         this.x = this._cell.x * $gameMap.tileWidth() + w / 2;
  627.         this.y = this._cell.y * $gameMap.tileHeight() + h / 2;
  628.         if (this._animation.position === 2)
  629.             this.y += h / 2;
  630.         else if (this._animation.position === 0)
  631.             this.y -= h / 2;
  632.  
  633.     }
  634. };
  635.  
  636.  
  637. /*-------------------------------------------------------------------------
  638. * Scene_Battle
  639. -------------------------------------------------------------------------*/
  640. Lecode.S_TBS.oldSB_create = Scene_Battle.prototype.create;
  641. Scene_Battle.prototype.create = function () {
  642.     if (Lecode.S_TBS.commandOn) {
  643.         Scene_Base.prototype.create.call(this);
  644.         this.createDisplayObjects();
  645.     } else {
  646.         Lecode.S_TBS.oldSB_create.call(this);
  647.     }
  648. };
  649.  
  650. Lecode.S_TBS.oldSB_createDisplayObjects = Scene_Battle.prototype.createDisplayObjects;
  651. Scene_Battle.prototype.createDisplayObjects = function () {
  652.     if (Lecode.S_TBS.commandOn) {
  653.         this.createSpriteset();
  654.         this.createWindowLayer();
  655.         this.createAllWindows();
  656.     } else {
  657.         Lecode.S_TBS.oldSB_createDisplayObjects.call(this);
  658.     }
  659. };
  660.  
  661. Lecode.S_TBS.oldSB_start = Scene_Battle.prototype.start;
  662. Scene_Battle.prototype.start = function () {
  663.     if (Lecode.S_TBS.commandOn) {
  664.         Scene_Base.prototype.start.call(this);
  665.         this.startFadeIn(this.fadeSpeed(), false);
  666.  
  667.         BattleManager.playBattleBgm();
  668.         BattleManagerTBS._spriteset = this._spriteset;
  669.         InputHandlerTBS.setup();
  670.  
  671.         InputHandlerTBS.addWindowBlockingTouch(this._windowConfirm);
  672.         InputHandlerTBS.addWindowBlockingTouch(this._windowCommand);
  673.         InputHandlerTBS.addWindowBlockingTouch(this._windowSkill);
  674.         InputHandlerTBS.addWindowBlockingTouch(this._windowItem);
  675.         InputHandlerTBS.addWindowBlockingTouch(this._windowStatus);
  676.         InputHandlerTBS.addWindowBlockingTouch(this._helpWindow);
  677.  
  678.         BattleManagerTBS.startBattle();
  679.     } else {
  680.         Lecode.S_TBS.oldSB_start.call(this);
  681.     }
  682. };
  683.  
  684. Lecode.S_TBS.oldSB_update = Scene_Battle.prototype.update;
  685. Scene_Battle.prototype.update = function () {
  686.     if (Lecode.S_TBS.commandOn) {
  687.         $gameMap.update();
  688.         $gameTimer.update(true);
  689.         $gameScreen.update();
  690.         InputHandlerTBS.update();
  691.         BattleManagerTBS.update();
  692.         Scene_Base.prototype.update.call(this);
  693.     } else {
  694.         Lecode.S_TBS.oldSB_update.call(this);
  695.     }
  696. };
  697.  
  698. Lecode.S_TBS.oldSB_createSpriteset = Scene_Battle.prototype.createSpriteset;
  699. Scene_Battle.prototype.createSpriteset = function () {
  700.     if (Lecode.S_TBS.commandOn) {
  701.         this._spriteset = new Spriteset_BattleTBS();
  702.         this.addChild(this._spriteset);
  703.     } else {
  704.         Lecode.S_TBS.oldSB_createSpriteset.call(this);
  705.     }
  706. };
  707.  
  708. Lecode.S_TBS.oldSB_createAllWindows = Scene_Battle.prototype.createAllWindows;
  709. Scene_Battle.prototype.createAllWindows = function () {
  710.     if (Lecode.S_TBS.commandOn) {
  711.         //this.createLogWindow();
  712.         this.createPlacementWindow();
  713.         this.createConfirmationWindow();
  714.         this.createStatusWindow();
  715.         this.createCommandWindow();
  716.         this.createHelpWindow();
  717.         this.createSkillWindow();
  718.         this.createItemWindow();
  719.         this.createEndCommandWindow();
  720.         this.createMessageWindow();
  721.     } else {
  722.         Lecode.S_TBS.oldSB_createAllWindows.call(this);
  723.     }
  724. };
  725.  
  726. Scene_Battle.prototype.createPlacementWindow = function () {
  727.     this._windowPlacement = new Window_TBSPlacementInfo();
  728.     this._windowPlacement.hide();
  729.     this._windowPlacement.deactivate();
  730.     this.addWindow(this._windowPlacement);
  731. };
  732.  
  733. Scene_Battle.prototype.createConfirmationWindow = function () {
  734.     this._windowConfirm = new Window_TBSConfirm();
  735.     this._windowConfirm.setHandler('ok', this.onConfirmationOK.bind(this));
  736.     this._windowConfirm.setHandler('cancel', this.onConfirmationCancel.bind(this));
  737.     this._windowConfirm.hide();
  738.     this._windowConfirm.deactivate();
  739.     this.addWindow(this._windowConfirm);
  740. };
  741.  
  742. Lecode.S_TBS.oldSB_createStatusWindow = Scene_Battle.prototype.createStatusWindow;
  743. Scene_Battle.prototype.createStatusWindow = function () {
  744.     if (Lecode.S_TBS.commandOn) {
  745.         this._windowStatus = new Window_TBSStatus();
  746.         this._windowStatus.hide();
  747.         this.addWindow(this._windowStatus);
  748.     } else {
  749.         Lecode.S_TBS.oldSB_createStatusWindow.call(this);
  750.     }
  751. };
  752.  
  753. Lecode.S_TBS.oldSB_createCommandWindow = Scene_Battle.prototype.createCommandWindow;
  754. Scene_Battle.prototype.createCommandWindow = function () {
  755.     if (Lecode.S_TBS.commandOn) {
  756.         this._windowCommand = new Window_TBSCommand();
  757.         this._windowCommand.setHandler('move', this.onCommandInput.bind(this, "move"));
  758.         this._windowCommand.setHandler('attack', this.onCommandInput.bind(this, "attack"));
  759.         this._windowCommand.setHandler('skill', this.onCommandInput.bind(this, "skill"));
  760.         this._windowCommand.setHandler('item', this.onCommandInput.bind(this, "item"));
  761.         this._windowCommand.setHandler('pass', this.onCommandInput.bind(this, "pass"));
  762.         this._windowCommand.setHandler('cancel', this.onCommandInput.bind(this, "cancel"));
  763.         this._windowCommand.hide();
  764.         this._windowCommand.deactivate();
  765.         this.addWindow(this._windowCommand);
  766.     } else {
  767.         Lecode.S_TBS.oldSB_createCommandWindow.call(this);
  768.     }
  769. };
  770.  
  771. Lecode.S_TBS.oldSB_createSkillWindow = Scene_Battle.prototype.createSkillWindow;
  772. Scene_Battle.prototype.createSkillWindow = function () {
  773.     if (Lecode.S_TBS.commandOn) {
  774.         this._windowSkill = new Window_TBSSkillList();
  775.         this._windowSkill.setHandler('ok', this.onSkillInput.bind(this, "ok"));
  776.         this._windowSkill.setHandler('cancel', this.onSkillInput.bind(this, "cancel"));
  777.         this._windowSkill._helpWindow = this._helpWindow;
  778.         this._windowSkill.hide();
  779.         this._windowSkill.deactivate();
  780.         this.addWindow(this._windowSkill);
  781.     } else {
  782.         Lecode.S_TBS.oldSB_createSkillWindow.call(this);
  783.     }
  784. };
  785.  
  786. Lecode.S_TBS.oldSB_createItemWindow = Scene_Battle.prototype.createItemWindow;
  787. Scene_Battle.prototype.createItemWindow = function () {
  788.     if (Lecode.S_TBS.commandOn) {
  789.         this._windowItem = new Window_TBSItemList();
  790.         this._windowItem.setHandler('ok', this.onItemInput.bind(this, "ok"));
  791.         this._windowItem.setHandler('cancel', this.onItemInput.bind(this, "cancel"));
  792.         this._windowItem._helpWindow = this._helpWindow;
  793.         this._windowItem.hide();
  794.         this._windowItem.deactivate();
  795.         this.addWindow(this._windowItem);
  796.     } else {
  797.         Lecode.S_TBS.oldSB_createItemWindow.call(this);
  798.     }
  799. };
  800.  
  801. Scene_Battle.prototype.createEndCommandWindow = function () {
  802.     this._windowEndCommand = new Window_TBSEndCommand();
  803.     this._windowEndCommand.setHandler('options', this.onEndCommandInput.bind(this, "options"));
  804.     this._windowEndCommand.setHandler('escape', this.onEndCommandInput.bind(this, "escape"));
  805.     this._windowEndCommand.setHandler('cancel', this.onEndCommandInput.bind(this, "cancel"));
  806.     this._windowEndCommand.hide();
  807.     this._windowEndCommand.deactivate();
  808.     this.addWindow(this._windowEndCommand);
  809. };
  810.  
  811. Scene_Battle.prototype.showPlacementWindow = function (cell, battler) {
  812.     this.placeWindowOverCell(this._windowPlacement, cell);
  813.     this._windowPlacement.show();
  814.     this._windowPlacement.open();
  815.     this._windowPlacement._battler = battler;
  816.     this._windowPlacement.refresh();
  817. };
  818.  
  819. Scene_Battle.prototype.showStatusWindow = function (battler) {
  820.     //if (battler === this._windowStatus._battler) return;
  821.     var window = this._windowStatus;
  822.     this._windowStatus._battler = battler;
  823.     this._windowStatus.show();
  824.     this._windowStatus.open();
  825.     this._windowStatus.refresh();
  826. };
  827.  
  828. Scene_Battle.prototype.showCommandWindow = function () {
  829.     var entity = BattleManagerTBS.activeEntity();
  830.     var cell = entity.getCell();
  831.     var battler = BattleManagerTBS.activeBattler();
  832.     this.placeWindowOverCell(this._windowCommand, cell);
  833.     this._windowCommand.setup(battler, entity);
  834. };
  835.  
  836. Scene_Battle.prototype.showSkillWindow = function () {
  837.     var entity = BattleManagerTBS.activeEntity();
  838.     var cell = entity.getCell();
  839.     var battler = BattleManagerTBS.activeBattler();
  840.     this.placeWindowOverCell(this._windowSkill, cell);
  841.     this._windowSkill.setActor(battler);
  842.     this._windowSkill.show();
  843.     this._windowSkill.open();
  844.     this._windowSkill.refresh();
  845.     this._windowSkill.activate();
  846.     this.showHelpWindow();
  847. };
  848.  
  849. Scene_Battle.prototype.showItemWindow = function () {
  850.     var entity = BattleManagerTBS.activeEntity();
  851.     var cell = entity.getCell();
  852.     var battler = BattleManagerTBS.activeBattler();
  853.     this.placeWindowOverCell(this._windowItem, cell);
  854.     this._windowItem.show();
  855.     this._windowItem.open();
  856.     this._windowItem.refresh();
  857.     this._windowItem.activate();
  858.     this.showHelpWindow();
  859. };
  860.  
  861. Scene_Battle.prototype.showHelpWindow = function () {
  862.     var status = this._windowStatus;
  863.     var help = this._helpWindow;
  864.     help.x = 0;
  865.     help.y = Graphics.height - help.height;
  866.     while ((status.y + status.height) > help.y)
  867.         status.y--;
  868.     status.y++;
  869.     help.visible = true;
  870. };
  871.  
  872. Scene_Battle.prototype.showEndCommandWindow = function () {
  873.     this._windowEndCommand.show();
  874.     this._windowEndCommand.open();
  875.     this._windowEndCommand.activate();
  876. };
  877.  
  878. Scene_Battle.prototype.placeWindowOverCell = function (window, cell) {
  879.     var x = $gameMap.adjustX(cell.x) * $gameMap.tileWidth();
  880.     var y = $gameMap.adjustY(cell.y) * $gameMap.tileHeight();
  881.     var w = window.width;
  882.     var h = window.height;
  883.  
  884.     x += $gameMap.tileWidth() / 2 - w / 2;
  885.     while (x < 0) x++;
  886.     while ((x + w) > Graphics.width) x--;
  887.  
  888.     if (y <= h)
  889.         y += $gameMap.tileHeight() / 2 + 10;
  890.     else
  891.         y -= h - 10;
  892.  
  893.     window.move(x, y, w, h);
  894.     window._leU_float = true;
  895.     window._leU_floatData.ini_pos = [x, y];
  896. };
  897.  
  898. Scene_Battle.prototype.hidePlacementWindow = function (cell, battler) {
  899.     this._windowPlacement.close();
  900. };
  901.  
  902. Scene_Battle.prototype.hideHelpWindow = function () {
  903.     var status = this._windowStatus;
  904.     var help = this._helpWindow;
  905.     if ((status.y + status.height) == (help.y + 1))
  906.         status.y += help.height;
  907.     help.y += help.height;
  908.     help.visible = false;
  909. };
  910.  
  911. Scene_Battle.prototype.showConfirmationWindow = function () {
  912.     this._windowConfirm.show();
  913.     this._windowConfirm.open();
  914.     this._windowConfirm.activate();
  915. };
  916.  
  917. Scene_Battle.prototype.hideConfirmationWindow = function () {
  918.     this._windowConfirm.close();
  919.     this._windowConfirm.deactivate();
  920. };
  921.  
  922. Scene_Battle.prototype.onConfirmationOK = function () {
  923.     if (this._windowConfirm.openness == 255);
  924.     BattleManagerTBS.onConfirmationWindowOK();
  925. };
  926.  
  927. Scene_Battle.prototype.onConfirmationCancel = function () {
  928.     if (this._windowConfirm.openness == 255);
  929.     BattleManagerTBS.onConfirmationWindowCancel();
  930. };
  931.  
  932. Scene_Battle.prototype.onCommandInput = function (command) {
  933.     this._windowCommand.close();
  934.     this._windowCommand.deactivate();
  935.     BattleManagerTBS.onCommandInput(command);
  936. };
  937.  
  938. Scene_Battle.prototype.onSkillInput = function (command) {
  939.     this._windowSkill.close();
  940.     this._windowSkill.deactivate();
  941.     this.hideHelpWindow();
  942.     BattleManagerTBS.onSkillInput(command);
  943. };
  944.  
  945. Scene_Battle.prototype.onItemInput = function (command) {
  946.     this._windowItem.close();
  947.     this._windowItem.deactivate();
  948.     this.hideHelpWindow();
  949.     BattleManagerTBS.onItemInput(command);
  950. };
  951.  
  952. Scene_Battle.prototype.onEndCommandInput = function (command) {
  953.     this._windowEndCommand.close();
  954.     this._windowEndCommand.deactivate();
  955.     BattleManagerTBS.onEndCommandInput(command);
  956. };
  957.  
  958. Scene_Battle.prototype.getWindowsWChangeableOpa = function () {
  959.     return [this._windowConfirm, this._windowPlacement, this._windowCommand, this._windowStatus,
  960.         this._windowSkill, this._helpWindow
  961.     ];
  962. };
  963.  
  964. Scene_Battle.prototype.stop = function () {
  965.     Scene_Base.prototype.stop.call(this);
  966.     if (this.needsSlowFadeOut()) {
  967.         this.startFadeOut(this.slowFadeSpeed(), false);
  968.     } else {
  969.         this.startFadeOut(this.fadeSpeed(), false);
  970.     }
  971.     if (Lecode.S_TBS.commandOn) {
  972.         this.getWindowsWChangeableOpa().forEach(function (window) {
  973.             window.close();
  974.         }.bind(this));
  975.     } else {
  976.         this._statusWindow.close();
  977.         this._partyCommandWindow.close();
  978.         this._actorCommandWindow.close();
  979.     }
  980. };
  981.  
  982.  
  983. /*-------------------------------------------------------------------------
  984. * InputHandlerTBS
  985. -------------------------------------------------------------------------*/
  986. function InputHandlerTBS() {
  987.     throw new Error('This is a static class');
  988. }
  989.  
  990. InputHandlerTBS.setup = function () {
  991.     this._active = true;
  992.     this._lastSelectedCell = null;
  993.     this._windowsBlocking = [];
  994. };
  995.  
  996. InputHandlerTBS.isActive = function () {
  997.     return this._active;
  998. };
  999.  
  1000. InputHandlerTBS.lastSelectedCell = function () {
  1001.     return this._lastSelectedCell;
  1002. };
  1003.  
  1004. InputHandlerTBS.setActive = function (active) {
  1005.     this._active = active;
  1006. };
  1007.  
  1008. InputHandlerTBS.addWindowBlockingTouch = function (window) {
  1009.     this._windowsBlocking.push(window);
  1010. };
  1011.  
  1012. InputHandlerTBS.setOnTouchCallback = function (callback) {
  1013.     this._onTouchCallback = callback;
  1014. };
  1015.  
  1016. InputHandlerTBS.setOnTouchCancelCallback = function (callback) {
  1017.     this._onTouchCancelCallback = callback;
  1018. };
  1019.  
  1020. InputHandlerTBS.setOnUpCallback = function (callback) {
  1021.     this._onUpCallback = callback;
  1022. };
  1023.  
  1024. InputHandlerTBS.setOnRightCallback = function (callback) {
  1025.     this._onRightCallback = callback;
  1026. };
  1027.  
  1028. InputHandlerTBS.setOnDownCallback = function (callback) {
  1029.     this._onDownCallback = callback;
  1030. };
  1031.  
  1032. InputHandlerTBS.setOnLeftCallback = function (callback) {
  1033.     this._onLeftCallback = callback;
  1034. };
  1035.  
  1036. InputHandlerTBS.setOnOkCallback = function (callback) {
  1037.     this._onOkCallback = callback;
  1038. };
  1039.  
  1040. InputHandlerTBS.setOnCancelCallback = function (callback) {
  1041.     this._onCancelCallback = callback;
  1042. };
  1043.  
  1044. InputHandlerTBS.update = function () {
  1045.     if (!this.isActive()) return;
  1046.     if (BattleManagerTBS.isWaiting()) return;
  1047.  
  1048.     if (Input.isPressed(Lecode.S_TBS.explorationInput) && !BattleManagerTBS.isExploring()) {
  1049.         BattleManagerTBS.startMapExploration();
  1050.         return;
  1051.     }
  1052.     if (!Input.isPressed(Lecode.S_TBS.explorationInput) && BattleManagerTBS.isExploring()) {
  1053.         BattleManagerTBS.endMapExploration();
  1054.         return;
  1055.     }
  1056.     if (BattleManagerTBS.isExploring()) {
  1057.         this.updateExplorationInput();
  1058.         return;
  1059.     }
  1060.  
  1061.     if (Input.isPressed(Lecode.S_TBS.opacityInput))
  1062.         BattleManagerTBS.setInputOpacity();
  1063.     else
  1064.         BattleManagerTBS._opacityInputed = false;
  1065.  
  1066.     if (TouchInput.isTriggered()) {
  1067.         if (!this.isTouchBlocked()) {
  1068.             var cell = this.touchSelectedCell();
  1069.             this.processCallback("Touch", cell);
  1070.             this._lastSelectedCell = cell;
  1071.             var x = $gameMap.canvasToMapX(TouchInput.x);
  1072.             var y = $gameMap.canvasToMapY(TouchInput.y);
  1073.             $gameTemp.setDestination(x, y);
  1074.             BattleManagerTBS.resetDestinationCount();
  1075.         }
  1076.         return;
  1077.     }
  1078.  
  1079.     if (TouchInput.isCancelled()) {
  1080.         this.processCallback("TouchCancel");
  1081.         return;
  1082.     }
  1083.  
  1084.     if (Input.isTriggered("right"))
  1085.         this.processCallback("Right");
  1086.     if (Input.isTriggered("down"))
  1087.         this.processCallback("Down");
  1088.     if (Input.isTriggered("left"))
  1089.         this.processCallback("Left");
  1090.     if (Input.isTriggered("up"))
  1091.         this.processCallback("Up");
  1092.     if (Input.isTriggered("ok"))
  1093.         this.processCallback("Ok");
  1094.     if (Input.isTriggered("cancel"))
  1095.         this.processCallback("Cancel");
  1096. };
  1097.  
  1098. InputHandlerTBS.processCallback = function (key, arg) {
  1099.     var func = eval("this._on" + key + "Callback");
  1100.     if (func)
  1101.         func(arg);
  1102. };
  1103.  
  1104. InputHandlerTBS.updateExplorationInput = function () {
  1105.     if (Input.isPressed("right"))
  1106.         BattleManagerTBS.scrollRight(1);
  1107.     if (Input.isPressed("down"))
  1108.         BattleManagerTBS.scrollDown(1);
  1109.     if (Input.isPressed("up"))
  1110.         BattleManagerTBS.scrollUp(1);
  1111.     if (Input.isPressed("left"))
  1112.         BattleManagerTBS.scrollLeft(1);
  1113. };
  1114.  
  1115. InputHandlerTBS.touchSelectedCell = function () {
  1116.     var w = $gameMap.tileWidth();
  1117.     var h = $gameMap.tileHeight();
  1118.     var x = TouchInput.x;
  1119.     var y = TouchInput.y;
  1120.     var cells = BattleManagerTBS._groundCells;
  1121.     for (var i = 0; i < cells.length; i++) {
  1122.         var cell = cells[i];
  1123.         if (x >= cell.x * w && x <= (cell.x * w + w)) {
  1124.             if (y >= cell.y * h && y <= (cell.y * h + h))
  1125.                 return cell;
  1126.         }
  1127.     }
  1128.     return null;
  1129. };
  1130.  
  1131. InputHandlerTBS.isTouchBlocked = function () {
  1132.     var x = TouchInput.x;
  1133.     var y = TouchInput.y;
  1134.     for (var i = 0; i < this._windowsBlocking.length; i++) {
  1135.         var w = this._windowsBlocking[i];
  1136.         if (!w.visible || w.opacity === 0 || w.openness === 0) continue;
  1137.         if (x >= w.x && x <= (w.x + w.width)) {
  1138.             if (y >= w.y && y <= (w.y + w.height))
  1139.                 return true;
  1140.         }
  1141.     }
  1142.     return false;
  1143. };
  1144.  
  1145.  
  1146. /*-------------------------------------------------------------------------
  1147. * BattleManager
  1148. -------------------------------------------------------------------------*/
  1149. Lecode.S_TBS.oldBM_setup = BattleManager.setup;
  1150. BattleManager.setup = function (troopId, canEscape, canLose) {
  1151.     Lecode.S_TBS.oldBM_setup.call(this, troopId, canEscape, canLose);
  1152.     BattleManagerTBS.setup();
  1153. };
  1154.  
  1155. Lecode.S_TBS.oldBM_isTurnEnd = BattleManager.isTurnEnd;
  1156. BattleManager.isTurnEnd = function () {
  1157.     return Lecode.S_TBS.oldBM_isTurnEnd.call(this) || BattleManagerTBS._subPhase == "turn_end";
  1158. };
  1159.  
  1160. Lecode.S_TBS.oldBM_canEscape = BattleManager.canEscape;
  1161. BattleManager.canEscape = function () {
  1162.     var entity = BattleManagerTBS.activeEntity();
  1163.     if (entity) {
  1164.         var bool = entity._movePerformed || entity._actionPerformed;
  1165.         return Lecode.S_TBS.oldBM_canEscape.call(this) && !bool;
  1166.     } else {
  1167.         return Lecode.S_TBS.oldBM_canEscape.call(this);
  1168.     }
  1169. };
  1170.  
  1171.  
  1172. /*-------------------------------------------------------------------------
  1173. * BattleManagerTBS
  1174. -------------------------------------------------------------------------*/
  1175. function BattleManagerTBS() {
  1176.     throw new Error('This is a static class');
  1177. }
  1178.  
  1179. BattleManagerTBS.setup = function () {
  1180.     this.initMembers();
  1181. };
  1182.  
  1183. BattleManagerTBS.initMembers = function () {
  1184.     this._phase = "init";
  1185.     this._subPhase = "";
  1186.     this._startCells = [];
  1187.     this._groundCells = [];
  1188.     this._cursor = null;
  1189.     this._activeCell = null;
  1190.     this._spriteset = null;
  1191.     this._battlerEntities = [];
  1192.     this._explorationIniCell = null;
  1193.     this._exploring = false;
  1194.     this._inputOpacity = 0;
  1195.     this._opacityInputed = false;
  1196.     this._waitTime = 0;
  1197.     this._turnOrder = [];
  1198.     this._activeIndex = 0;
  1199.     this._activeAction = null;
  1200.     this._dummyChara = new Game_Character();
  1201.     this._moveScope = null;
  1202.     this._movePath = null;
  1203.     this._actionScope = null;
  1204.     this._destinationCount = 0;
  1205. };
  1206.  
  1207. BattleManagerTBS.cursor = function () {
  1208.     return this._cursor;
  1209. };
  1210.  
  1211. BattleManagerTBS.allEntities = function () {
  1212.     return this._battlerEntities;
  1213. };
  1214.  
  1215. BattleManagerTBS.dummyCharacter = function () {
  1216.     return this._dummyChara;
  1217. };
  1218.  
  1219. BattleManagerTBS.moveScope = function () {
  1220.     return this._moveScope;
  1221. };
  1222.  
  1223. BattleManagerTBS.movePath = function () {
  1224.     return this._movePath;
  1225. };
  1226.  
  1227. BattleManagerTBS.actionScope = function () {
  1228.     return this._actionScope;
  1229. };
  1230.  
  1231. BattleManagerTBS.lastSelectedCell = function () {
  1232.     return this._lastSelectedCell;
  1233. };
  1234.  
  1235. BattleManagerTBS.isExploring = function () {
  1236.     return this._exploring;
  1237. };
  1238.  
  1239. BattleManagerTBS.activeAction = function () {
  1240.     return this._activeAction;
  1241. };
  1242.  
  1243. BattleManagerTBS.getLayer = function (id) {
  1244.     var layer = eval("this._spriteset._" + id + "Layer");
  1245.     return layer;
  1246. };
  1247.  
  1248. BattleManagerTBS.wait = function (time) {
  1249.     this._waitTime += time;
  1250. };
  1251.  
  1252. BattleManagerTBS.isWaiting = function () {
  1253.     return this._waitTime > 0;
  1254. };
  1255.  
  1256. BattleManagerTBS.startBattle = function () {
  1257.     this.prepare();
  1258.     this.processPlacementPhase();
  1259. };
  1260.  
  1261. BattleManagerTBS.prepare = function () {
  1262.     this.createStartCells();
  1263.     this.createGroundCells();
  1264.     this.createTBSObjects();
  1265. };
  1266.  
  1267. BattleManagerTBS.createTBSObjects = function () {
  1268.     this.createDirectionSelector();
  1269.     this.createCursor();
  1270.     this.createBattleStartSprite();
  1271.     this.createTurnOrderVisual();
  1272.     this.createProjectilesManager();
  1273.     this.createAiManager();
  1274.     this._easystar = new EasyStar.js();
  1275. };
  1276.  
  1277. BattleManagerTBS.createDirectionSelector = function () {
  1278.     var layer = this.getLayer("movableInfo");
  1279.     this._directionSelector = new TBSDirectionSelector(layer);
  1280. };
  1281.  
  1282. BattleManagerTBS.createCursor = function () {
  1283.     var bitmap = ImageManager.loadLeTBS("MapCursor");
  1284.     this._cursor = new SpriteCursorTBS(bitmap);
  1285.     this.getLayer("ground").addChild(this.cursor());
  1286. };
  1287.  
  1288. BattleManagerTBS.createBattleStartSprite = function () {
  1289.     var bitmap = ImageManager.loadLeTBS("Battle_Start");
  1290.     this._startSprite = new Sprite(bitmap);
  1291.     this._startSprite.visible = false;
  1292.     this.getLayer("fixedInfo").addChild(this._startSprite);
  1293. };
  1294.  
  1295. BattleManagerTBS.createProjectilesManager = function () {
  1296.     var layer = this.getLayer("animations");
  1297.     this._projectilesManager = new TBSProjectilesManager(layer);
  1298. };
  1299.  
  1300. BattleManagerTBS.createAiManager = function () {
  1301.     this._aiManager = new TBSAiManager();
  1302. };
  1303. BattleManagerTBS.createTurnOrderVisual = function () {
  1304.     var layer = this.getLayer("fixedInfo");
  1305.     this._turnOrderVisual = new TBSTurnOrderVisual(layer);
  1306. };
  1307.  
  1308. BattleManagerTBS.createStartCells = function () {
  1309.     this.getLayer("scopes").clear();
  1310.     this._startCells = [];
  1311.     $gameMap.events().forEach(function (eventObj) {
  1312.         var x = eventObj.event().x;
  1313.         var y = eventObj.event().y;
  1314.         var name = eventObj.event().name;
  1315.         var cell = new TBSCell(x, y);
  1316.         cell._event = eventObj.event();
  1317.         if (name == "ally" || name == "enemy")
  1318.             this.createPlacementCell(name, x, y, cell);
  1319.         this._startCells.push(cell);
  1320.     }.bind(this));
  1321. };
  1322.  
  1323. BattleManagerTBS.createGroundCells = function () {
  1324.     this._groundCells = [];
  1325.     for (var i = 0; i < $gameMap.width(); i++) {
  1326.         for (var j = 0; j < $gameMap.height(); j++) {
  1327.             var cell = new TBSCell(i, j);
  1328.             cell._regionId = $gameMap.regionId(i, j);
  1329.             this._groundCells.push(cell);
  1330.         }
  1331.     }
  1332. };
  1333.  
  1334. BattleManagerTBS.update = function () {
  1335.     this.waitForMessages();
  1336.     this.updateWait();
  1337.     this.updateWindowsInputOpacity();
  1338.     this.updateTBSObjects();
  1339.     this.updatePhase();
  1340.     this.updateBattlers();
  1341.     this.updateDestination();
  1342. };
  1343.  
  1344. BattleManagerTBS.updateDestination = function () {
  1345.     this._destinationCount++;
  1346.     if (this._destinationCount >= Lecode.S_TBS.destinationDuration) {
  1347.         this._destinationCount = 0;
  1348.         $gameTemp.clearDestination();
  1349.     }
  1350. };
  1351.  
  1352. BattleManagerTBS.resetDestinationCount = function () {
  1353.     this._destinationCount = 0;
  1354. };
  1355.  
  1356. BattleManagerTBS.waitForMessages = function () {
  1357.     if ($gameMessage.isBusy()) {
  1358.         this.wait(1);
  1359.         var window = LeUtilities.getScene()._windowCommand;
  1360.         if (window.active) {
  1361.             //window.close();
  1362.             window.deactivate();
  1363.             this._windowToResume = window;
  1364.         }
  1365.     } else {
  1366.         if (this._windowToResume) {
  1367.             this._windowToResume.open();
  1368.             this._windowToResume.activate();
  1369.             this._windowToResume = null;
  1370.         }
  1371.     }
  1372. };
  1373.  
  1374. BattleManagerTBS.updateWait = function () {
  1375.     if (this.isWaiting())
  1376.         this._waitTime--;
  1377. };
  1378.  
  1379. BattleManagerTBS.updateWindowsInputOpacity = function () {
  1380.     if (this._inputOpacity === 0)
  1381.         return;
  1382.  
  1383.     var windows = LeUtilities.getScene().getWindowsWChangeableOpa();
  1384.     windows.forEach(function (window) {
  1385.         window.opacity = 255 - this._inputOpacity;
  1386.         window.backOpacity = 255 - this._inputOpacity;
  1387.         window.contentsOpacity = 255 - this._inputOpacity;
  1388.     }.bind(this));
  1389.  
  1390.     if (!this._opacityInputed) {
  1391.         var steps = Lecode.S_TBS.inputOpacityDecreaseSteps;
  1392.         var max = 255 - Lecode.S_TBS.minInputOpacity;
  1393.         this._inputOpacity -= steps;
  1394.         this._inputOpacity = this._inputOpacity.clamp(0, max);
  1395.     }
  1396. };
  1397.  
  1398. BattleManagerTBS.updateTBSObjects = function () {
  1399.     this.updateCursor();
  1400.     this._turnOrderVisual.update();
  1401.     this._projectilesManager.update();
  1402. };
  1403.  
  1404. BattleManagerTBS.updateCursor = function () {
  1405.     if (!this._activeCell || !this.cursor()) return;
  1406.     var x = this._activeCell.x;
  1407.     var y = this._activeCell.y;
  1408.     this.cursor().cellX = x;
  1409.     this.cursor().cellY = y;
  1410.     x *= $gameMap.tileWidth();
  1411.     y *= $gameMap.tileHeight();
  1412.     this.cursor().x = x;
  1413.     this.cursor().y = y;
  1414.     this.cursor().update();
  1415. };
  1416.  
  1417. BattleManagerTBS.startMapExploration = function () {
  1418.     this._exploring = true;
  1419.     this._explorationIniCell = this._activeCell;
  1420. };
  1421.  
  1422. BattleManagerTBS.endMapExploration = function () {
  1423.     this._exploring = false;
  1424.     this.centerCell(this._explorationIniCell);
  1425. };
  1426.  
  1427. BattleManagerTBS.setInputOpacity = function () {
  1428.     this._opacityInputed = true;
  1429.     var steps = Lecode.S_TBS.inputOpacityDecreaseSteps;
  1430.     var max = 255 - Lecode.S_TBS.minInputOpacity;
  1431.     this._inputOpacity += steps;
  1432.     this._inputOpacity = this._inputOpacity.clamp(0, max);
  1433. };
  1434.  
  1435. BattleManagerTBS.updatePhase = function () {
  1436.     if ($gameMessage.isBusy()) return;
  1437.     switch (this._phase) {
  1438.         case "placement":
  1439.             this.updatePlacementPhase();
  1440.             break;
  1441.         case "battle_beginning":
  1442.             this.updateBeginningPhase();
  1443.             break;
  1444.         case "battle_processing":
  1445.             this.updateBattleProcessing();
  1446.             break;
  1447.         case "battle_stopping":
  1448.             this.updateBattleStopping();
  1449.             break;
  1450.         case "battle_end":
  1451.             this.updateBattleEnd();
  1452.             break;
  1453.     }
  1454. };
  1455.  
  1456. BattleManagerTBS.updateBattlers = function () {
  1457.     this.allEntities().forEach(function (entity) {
  1458.         entity.update();
  1459.     }.bind(this));
  1460. };
  1461.  
  1462. BattleManagerTBS.createPlacementCell = function (type, x, y, cell) {
  1463.     var opacity = Lecode.S_TBS.placementCellOpacity;
  1464.     var color = (type == "ally") ? Lecode.S_TBS.allyColorCell : Lecode.S_TBS.enemyColorCell;
  1465.     this.getLayer("scopes").drawCell(x, y, opacity, color);
  1466.     cell._type = type;
  1467. };
  1468.  
  1469. BattleManagerTBS.drawTileLimits = function (cell, x, y) {
  1470.     if (!Lecode.S_TBS.drawLimits) return false;
  1471.     var lines = 0;
  1472.     if ($gameMap.isPassable(x, y, 2)) { //-Down
  1473.         cell._bitmap.fillRect(0, 0, $gameMap.tileWidth(), 1, "#FFFFFF");
  1474.         lines++;
  1475.     }
  1476.     if ($gameMap.isPassable(x, y, 4)) { //-Left
  1477.         cell._bitmap.fillRect($gameMap.tileWidth() - 1, 0, 1, $gameMap.tileHeight(), "#FFFFFF");
  1478.         lines++;
  1479.     }
  1480.     if ($gameMap.isPassable(x, y, 6)) { //-Right
  1481.         cell._bitmap.fillRect(0, 0, 1, $gameMap.tileHeight(), "#FFFFFF");
  1482.         lines++;
  1483.     }
  1484.     if ($gameMap.isPassable(x, y, 8)) { //-Up
  1485.         cell._bitmap.fillRect(0, $gameMap.tileHeight() - 1, $gameMap.tileWidth(), 1, "#FFFFFF");
  1486.         lines++;
  1487.     }
  1488.     return lines;
  1489. };
  1490.  
  1491. BattleManagerTBS.processPlacementPhase = function () {
  1492.     this._phase = "placement";
  1493.     this._subPhase = "";
  1494.     this._placeAllies = [];
  1495.     this._placeEnemies = [];
  1496.     $gameParty.battleMembers().forEach(function (mem) {
  1497.         this._placeAllies.push(mem);
  1498.     }.bind(this));
  1499.     $gameTroop.members().forEach(function (mem) {
  1500.         this._placeEnemies.push(mem);
  1501.     }.bind(this));
  1502.     this.processEnemyPlacement();
  1503.  
  1504.     InputHandlerTBS.setOnTouchCallback(this.placementPhaseOnTouchInput.bind(this));
  1505.     InputHandlerTBS.setOnTouchCancelCallback(this.placementPhaseOnInputCancel.bind(this));
  1506.     InputHandlerTBS.setOnOkCallback(this.placementPhaseOnInputOk.bind(this));
  1507.     InputHandlerTBS.setOnCancelCallback(this.placementPhaseOnInputCancel.bind(this));
  1508.     InputHandlerTBS.setOnRightCallback(this.placementPhaseOnInputRight.bind(this));
  1509.     InputHandlerTBS.setOnLeftCallback(this.placementPhaseOnInputLeft.bind(this));
  1510.     InputHandlerTBS.setOnDownCallback(this.placementPhaseOnInputDown.bind(this));
  1511.     InputHandlerTBS.setOnUpCallback(this.placementPhaseOnInputUp.bind(this));
  1512. };
  1513.  
  1514. BattleManagerTBS.placementPhaseOnTouchInput = function (selectedCell) {
  1515.     switch (this._subPhase) {
  1516.         case "input":
  1517.             this.selectStartCellByTouch(selectedCell);
  1518.             break;
  1519.         case "directionSelector_input":
  1520.             this.setDirectionSelectionDirByTouch(selectedCell);
  1521.             break;
  1522.     }
  1523. };
  1524.  
  1525. BattleManagerTBS.placementPhaseOnInputOk = function () {
  1526.     switch (this._subPhase) {
  1527.         case "input":
  1528.             this.placementPhaseOk();
  1529.             break;
  1530.         case "directionSelector_input":
  1531.             this.directionSelectorValidatePlacement();
  1532.             break;
  1533.     }
  1534. };
  1535.  
  1536. BattleManagerTBS.placementPhaseOnInputCancel = function () {
  1537.     switch (this._subPhase) {
  1538.         case "input":
  1539.             this.placementPhaseCancel();
  1540.             break;
  1541.         case "directionSelector_input":
  1542.             this.directionSelectorCancelPlacement();
  1543.             break;
  1544.     }
  1545. };
  1546.  
  1547. BattleManagerTBS.placementPhaseOnInputLeft = function () {
  1548.     switch (this._subPhase) {
  1549.         case "input":
  1550.             this.selectStartCellByDir("left");
  1551.             break;
  1552.         case "directionSelector_input":
  1553.             this.setDirectionSelectorLeft();
  1554.             break;
  1555.     }
  1556. };
  1557.  
  1558. BattleManagerTBS.placementPhaseOnInputRight = function () {
  1559.     switch (this._subPhase) {
  1560.         case "input":
  1561.             this.selectStartCellByDir("right");
  1562.             break;
  1563.         case "directionSelector_input":
  1564.             this.setDirectionSelectorRight();
  1565.             break;
  1566.     }
  1567. };
  1568.  
  1569. BattleManagerTBS.placementPhaseOnInputDown = function () {
  1570.     switch (this._subPhase) {
  1571.         case "input":
  1572.             this.selectStartCellByDir("down");
  1573.             break;
  1574.         case "directionSelector_input":
  1575.             this.setDirectionSelectorDown();
  1576.             break;
  1577.     }
  1578. };
  1579.  
  1580. BattleManagerTBS.placementPhaseOnInputUp = function () {
  1581.     switch (this._subPhase) {
  1582.         case "input":
  1583.             this.selectStartCellByDir("up");
  1584.             break;
  1585.         case "directionSelector_input":
  1586.             this.setDirectionSelectorUp();
  1587.             break;
  1588.     }
  1589. };
  1590.  
  1591. BattleManagerTBS.updatePlacementPhase = function () {
  1592.     if (this._subPhase === "troop") {
  1593.         if (!this.getLayer("animations").isAnimationPlaying()) {
  1594.             if (this._placeEnemies.length === 0)
  1595.                 this.processAllyPlacement();
  1596.             else
  1597.                 this.placeNextEnemy();
  1598.         }
  1599.     }
  1600. };
  1601.  
  1602. BattleManagerTBS.processEnemyPlacement = function () {
  1603.     this._subPhase = "troop";
  1604.     this.placeNextEnemy();
  1605. };
  1606.  
  1607. BattleManagerTBS.placeNextEnemy = function () {
  1608.     var enemy = this._placeEnemies.shift();
  1609.     var cells = this.enemyStartCells().filter(function (c) {
  1610.         return Number(c._event.note) === enemy.index() + 1 && this.isCellFree(c);
  1611.     }.bind(this));
  1612.     var cell = LeUtilities.getRandomValueInArray(cells);
  1613.     if (cell) {
  1614.         cell.select();
  1615.         this.centerActiveCell();
  1616.         this.updateCursor();
  1617.         var entity = new TBSEntity(enemy, this.getLayer("battlers"));
  1618.         this.getLayer("animations").newAnimation(Lecode.S_TBS.placedBattlerAnim, false, 0, cell, entity._sprite);
  1619.         entity.setCell(cell);
  1620.         this._battlerEntities.push(entity);
  1621.     }
  1622. };
  1623.  
  1624. BattleManagerTBS.processAllyPlacement = function () {
  1625.     InputHandlerTBS.setActive(true);
  1626.     this._subPhase = "input";
  1627.     this.placementSelect(this.allyStartCells()[0]);
  1628. };
  1629.  
  1630. BattleManagerTBS.placementSelect = function (cell) {
  1631.     cell.select();
  1632.     this.centerActiveCell();
  1633.     this.updateCursor();
  1634.     var battler = this._placeAllies[0];
  1635.     LeUtilities.getScene().showPlacementWindow(cell, battler);
  1636. };
  1637.  
  1638. BattleManagerTBS.selectStartCellByDir = function (dir) {
  1639.     var cell = this._activeCell;
  1640.     var cells = this.allyStartCells().filter(function (c) {
  1641.         return c != cell;
  1642.     });
  1643.     var found = null;
  1644.     switch (dir) {
  1645.         case "up":
  1646.             cells = cells.filter(function (c) {
  1647.                 return c.y < cell.y;
  1648.             });
  1649.             found = LeUtilities.closestByDistance(cell, cells);
  1650.             break;
  1651.         case "down":
  1652.             cells = cells.filter(function (c) {
  1653.                 return c.y > cell.y;
  1654.             });
  1655.             found = LeUtilities.closestByDistance(cell, cells);
  1656.             break;
  1657.         case "right":
  1658.             cells = cells.filter(function (c) {
  1659.                 return c.x > cell.x;
  1660.             });
  1661.             found = LeUtilities.closestByDistance(cell, cells);
  1662.             break;
  1663.         case "left":
  1664.             cells = cells.filter(function (c) {
  1665.                 return c.x < cell.x;
  1666.             });
  1667.             found = LeUtilities.closestByDistance(cell, cells);
  1668.             break;
  1669.     }
  1670.     if (found && found != cell) {
  1671.         this.placementSelect(found);
  1672.         SoundManager.playCursor();
  1673.     }
  1674. };
  1675.  
  1676. BattleManagerTBS.selectStartCellByTouch = function (cell) {
  1677.     if (cell) {
  1678.         var scope = this.allyStartCells();
  1679.         var currentCell = this._activeCell;
  1680.         if (this.isCellInScope(cell, scope)) {
  1681.             if (currentCell.isSame(cell)) {
  1682.                 this.placementPhaseOk();
  1683.             } else {
  1684.                 this.placementSelect(cell);
  1685.             }
  1686.         } else {
  1687.             this.placementPhaseOk();
  1688.         }
  1689.     }
  1690. };
  1691.  
  1692. BattleManagerTBS.selectNextStartCell = function () {
  1693.     var currentIndex = this.allyStartCells().indexOf(this._activeCell);
  1694.     var cell = this._activeCell;
  1695.     var currentEntity = cell.getEntity();
  1696.     var index = currentIndex;
  1697.     do {
  1698.         if (currentIndex === this.allyStartCells().length - 1)
  1699.             index = 0;
  1700.         else
  1701.             index += 1;
  1702.         cell = this.allyStartCells()[index];
  1703.         currentEntity = cell.getEntity();
  1704.     } while (currentEntity);
  1705.     this.placementSelect(cell);
  1706.     SoundManager.playCursor();
  1707. };
  1708.  
  1709. BattleManagerTBS.selectPreviousStartCell = function () {
  1710.     var currentIndex = this.allyStartCells().indexOf(this._activeCell);
  1711.     var index = currentIndex === 0 ? this.allyStartCells().length - 1 : currentIndex - 1;
  1712.     this.placementSelect(this.allyStartCells()[index]);
  1713.     SoundManager.playCursor();
  1714. };
  1715.  
  1716. BattleManagerTBS.placementPhaseOk = function () {
  1717.     var cell = this._activeCell;
  1718.     var battler = this._placeAllies.shift();
  1719.     var currentEntity = cell.getEntity();
  1720.     if (currentEntity) {
  1721.         this.removeBattlerEntity(currentEntity);
  1722.     }
  1723.     var entity = this.placeBattler(battler, cell);
  1724.     this.callDirectionSelector(entity, cell);
  1725. };
  1726.  
  1727. BattleManagerTBS.placementPhaseCancel = function () {
  1728.     var cell = this._activeCell;
  1729.     var currentEntity = cell.getEntity();
  1730.     if (currentEntity) {
  1731.         this.removeBattlerEntity(currentEntity);
  1732.         this.placementSelect(this._activeCell);
  1733.         SoundManager.playCancel();
  1734.     } else
  1735.         SoundManager.playBuzzer();
  1736. };
  1737.  
  1738. BattleManagerTBS.placeBattler = function (battler, cell) {
  1739.     var entity = new TBSEntity(battler, this.getLayer("battlers"));
  1740.     this.getLayer("animations").newAnimation(Lecode.S_TBS.placedBattlerAnim, false, 0, cell, entity._sprite);
  1741.     entity.setCell(cell);
  1742.     this._battlerEntities.push(entity);
  1743.     SoundManager.playOk();
  1744.     this.updateEnemiesDirectionWhilePlacement();
  1745.     return entity;
  1746. };
  1747.  
  1748. BattleManagerTBS.removeBattlerEntity = function (entity) {
  1749.     this._placeAllies.unshift(entity._battler);
  1750.     LeUtilities.removeInArray(this.allEntities(), entity);
  1751.     this.getLayer("battlers").removeChild(entity._sprite);
  1752. };
  1753.  
  1754. BattleManagerTBS.updateEnemiesDirectionWhilePlacement = function () {
  1755.     this.enemyEntities().forEach(function (ent) {
  1756.         ent.lookClosestBattler(this.allyEntities());
  1757.     }.bind(this));
  1758. };
  1759.  
  1760. BattleManagerTBS.callDirectionSelector = function (battler, cell) {
  1761.     this._subPhase = "directionSelector_input";
  1762.     this._directionSelector.set(cell, battler);
  1763.     LeUtilities.getScene().hidePlacementWindow(cell, battler);
  1764. };
  1765.  
  1766. BattleManagerTBS.setDirectionSelectorUp = function () {
  1767.     this._directionSelector.setDir(8);
  1768. };
  1769.  
  1770. BattleManagerTBS.setDirectionSelectorDown = function () {
  1771.     this._directionSelector.setDir(2);
  1772. };
  1773.  
  1774. BattleManagerTBS.setDirectionSelectorLeft = function () {
  1775.     this._directionSelector.setDir(4);
  1776. };
  1777.  
  1778. BattleManagerTBS.setDirectionSelectorRight = function () {
  1779.     this._directionSelector.setDir(6);
  1780. };
  1781.  
  1782. BattleManagerTBS.directionSelectorValidatePlacement = function () {
  1783.     SoundManager.playOk();
  1784.     this._directionSelector.hide();
  1785.     this._subPhase = "input";
  1786.     if (this.canEndPlacementPhase())
  1787.         this.placementPhaseEnd();
  1788.     else
  1789.         this.selectNextStartCell();
  1790. };
  1791.  
  1792. BattleManagerTBS.setDirectionSelectionDirByTouch = function (selectedCell) {
  1793.     var entity = this._directionSelector._battlerEntity;
  1794.     entity.lookAt(selectedCell);
  1795.     this.directionSelectorValidatePlacement();
  1796. };
  1797.  
  1798. BattleManagerTBS.directionSelectorCancelPlacement = function () {
  1799.     this._directionSelector.hide();
  1800.     this._subPhase = "input";
  1801.     this.placementPhaseCancel();
  1802. };
  1803.  
  1804. BattleManagerTBS.placementPhaseEnd = function () {
  1805.     this._subPhase = "confirm";
  1806.     Input.clear();
  1807.     LeUtilities.getScene().showConfirmationWindow();
  1808. };
  1809.  
  1810. BattleManagerTBS.resumePlacementPhase = function () {
  1811.     LeUtilities.getScene().hideConfirmationWindow();
  1812.     this.placementPhaseCancel();
  1813.     this._subPhase = "input";
  1814. };
  1815.  
  1816. BattleManagerTBS.onConfirmationWindowOK = function () {
  1817.     switch (this._phase) {
  1818.         case "placement":
  1819.             this.battlebeginning();
  1820.             break;
  1821.     }
  1822. };
  1823.  
  1824. BattleManagerTBS.onConfirmationWindowCancel = function () {
  1825.     switch (this._phase) {
  1826.         case "placement":
  1827.             this.resumePlacementPhase();
  1828.             break;
  1829.     }
  1830. };
  1831.  
  1832. BattleManagerTBS.battlebeginning = function () {
  1833.     this._phase = "battle_beginning";
  1834.     LeUtilities.getScene().hideConfirmationWindow();
  1835.  
  1836.     this._startSprite.visible = true;
  1837.     this._startSprite.x = Graphics.width / 2 - this._startSprite.width / 2;
  1838.     this._startSprite.y = Graphics.height / 2 - this._startSprite.height / 2;
  1839.     this._startSprite.opacity = 0;
  1840.     this._subPhase = "in";
  1841. };
  1842.  
  1843. BattleManagerTBS.updateBeginningPhase = function () {
  1844.     var opa;
  1845.     if (this._subPhase == "in") {
  1846.         opa = this._startSprite.opacity;
  1847.         this._startSprite.opacity = (opa + 6).clamp(0, 255);
  1848.         if (this._startSprite.opacity == 255) {
  1849.             this._subPhase = "wait";
  1850.             this.wait(Lecode.S_TBS.battleStartSpriteDelay);
  1851.         }
  1852.     } else if (this._subPhase == "wait") {
  1853.         if (!this.isWaiting())
  1854.             this._subPhase = "out";
  1855.     } else if (this._subPhase == "out") {
  1856.         opa = this._startSprite.opacity;
  1857.         this._startSprite.opacity = (opa - 6).clamp(0, 255);
  1858.         if (this._startSprite.opacity === 0) {
  1859.             this.beginningPhaseEnd();
  1860.         }
  1861.     }
  1862. };
  1863.  
  1864. BattleManagerTBS.beginningPhaseEnd = function () {
  1865.     this._startSprite.visible = false;
  1866.     this.processBattle();
  1867. };
  1868.  
  1869. BattleManagerTBS.processBattle = function () {
  1870.     this._phase = "battle_processing";
  1871.     this._subPhase = "";
  1872.  
  1873.     BattleManager.startBattle();
  1874.     this.allEntities().forEach(function (entity) {
  1875.         entity.onBattleStart();
  1876.     }.bind(this));
  1877.     this.hidePlacementCells();
  1878.     this.determineTurnOrder();
  1879.     $gameTroop.increaseTurn();
  1880.     this.startTurn();
  1881.  
  1882.     InputHandlerTBS.setOnTouchCallback(this.battlePhaseOnTouchInput.bind(this));
  1883.     InputHandlerTBS.setOnTouchCancelCallback(this.battlePhaseOnInputCancel.bind(this));
  1884.     InputHandlerTBS.setOnOkCallback(this.battlePhaseOnInputOk.bind(this));
  1885.     InputHandlerTBS.setOnCancelCallback(this.battlePhaseOnInputCancel.bind(this));
  1886.     InputHandlerTBS.setOnUpCallback(this.battlePhaseOnInputUp.bind(this));
  1887.     InputHandlerTBS.setOnRightCallback(this.battlePhaseOnInputRight.bind(this));
  1888.     InputHandlerTBS.setOnDownCallback(this.battlePhaseOnInputDown.bind(this));
  1889.     InputHandlerTBS.setOnLeftCallback(this.battlePhaseOnInputLeft.bind(this));
  1890. };
  1891.  
  1892. BattleManagerTBS.battlePhaseOnTouchInput = function (selectedCell) {
  1893.     switch (this._subPhase) {
  1894.         case "move":
  1895.             this.touchMoveSelection(selectedCell);
  1896.             break;
  1897.         case "directionSelector_input":
  1898.             this.passByTouch(selectedCell);
  1899.             break;
  1900.         case "attack":
  1901.         case "skill":
  1902.         case "item":
  1903.             this.touchActionSelection(selectedCell);
  1904.             break;
  1905.     }
  1906. };
  1907.  
  1908. BattleManagerTBS.battlePhaseOnInputOk = function () {
  1909.     switch (this._subPhase) {
  1910.         case "move":
  1911.             this.validateMoveSelection();
  1912.             break;
  1913.         case "directionSelector_input":
  1914.             this.validatePass();
  1915.             break;
  1916.         case "attack":
  1917.         case "skill":
  1918.         case "item":
  1919.             this.validateActionSelection();
  1920.             break;
  1921.     }
  1922. };
  1923.  
  1924. BattleManagerTBS.battlePhaseOnInputCancel = function () {
  1925.     switch (this._subPhase) {
  1926.         case "move":
  1927.             this.onMoveCancel();
  1928.             break;
  1929.         case "directionSelector_input":
  1930.             this.cancelPass();
  1931.             break;
  1932.         case "attack":
  1933.         case "skill":
  1934.         case "item":
  1935.             this.onActionCancel();
  1936.             break;
  1937.     }
  1938. };
  1939.  
  1940. BattleManagerTBS.battlePhaseOnInputLeft = function () {
  1941.     switch (this._subPhase) {
  1942.         case "directionSelector_input":
  1943.             this.setDirectionSelectorLeft();
  1944.             break;
  1945.         case "move":
  1946.         case "attack":
  1947.         case "skill":
  1948.         case "item":
  1949.         case "examine":
  1950.             this.moveCursor("left");
  1951.             break;
  1952.     }
  1953. };
  1954.  
  1955. BattleManagerTBS.battlePhaseOnInputRight = function () {
  1956.     switch (this._subPhase) {
  1957.         case "directionSelector_input":
  1958.             this.setDirectionSelectorRight();
  1959.             break;
  1960.         case "move":
  1961.         case "attack":
  1962.         case "skill":
  1963.         case "item":
  1964.         case "examine":
  1965.             this.moveCursor("right");
  1966.             break;
  1967.     }
  1968. };
  1969.  
  1970. BattleManagerTBS.battlePhaseOnInputDown = function () {
  1971.     switch (this._subPhase) {
  1972.         case "directionSelector_input":
  1973.             this.setDirectionSelectorDown();
  1974.             break;
  1975.         case "move":
  1976.         case "attack":
  1977.         case "skill":
  1978.         case "item":
  1979.         case "examine":
  1980.             this.moveCursor("down");
  1981.             break;
  1982.     }
  1983. };
  1984.  
  1985. BattleManagerTBS.battlePhaseOnInputUp = function () {
  1986.     switch (this._subPhase) {
  1987.         case "directionSelector_input":
  1988.             this.setDirectionSelectorUp();
  1989.             break;
  1990.         case "move":
  1991.         case "attack":
  1992.         case "skill":
  1993.         case "item":
  1994.         case "examine":
  1995.             this.moveCursor("up");
  1996.             break;
  1997.     }
  1998. };
  1999.  
  2000. BattleManagerTBS.updateBattleProcessing = function () {
  2001.     this.updateSequences();
  2002.     if (this._subPhase == "moving") {
  2003.         if (!this.activeEntity().isMoving() && !this.isWaiting())
  2004.             this.onActiveEntityMoveEnd();
  2005.     } else if (this._subPhase == "obj_invokation") {
  2006.         if (!this.anySequenceRunning())
  2007.             this.onActionEnd();
  2008.     } else if (this._subPhase == "ai") {
  2009.         this._aiManager.update();
  2010.     } else if (this._subPhase == "turn_end")
  2011.         this.updateEndOfTurnEvents();
  2012. };
  2013.  
  2014. BattleManagerTBS.hidePlacementCells = function () {
  2015.     this.getLayer("scopes").clear();
  2016. };
  2017.  
  2018. BattleManagerTBS.determineTurnOrder = function () {
  2019.     if (Lecode.S_TBS.turnOrderFairRepartition)
  2020.         this.determineTurnOrderFair();
  2021.     else
  2022.         this.determineTurnOrderSimple();
  2023. };
  2024.  
  2025. BattleManagerTBS.determineTurnOrderSimple = function () {
  2026.     var array = [];
  2027.     this._turnOrder = [];
  2028.     this._activeIndex = 0;
  2029.  
  2030.     this.allEntities().forEach(function (entity) {
  2031.         array.push(entity);
  2032.     });
  2033.     array = array.sort(function (a, b) {
  2034.         return (a._battler.agi > b._battler.agi) ? 1 : ((a._battler.agi < b._battler.agi) ? -1 : 0);
  2035.     });
  2036.     array.reverse();
  2037.  
  2038.     this._turnOrder = array;
  2039.     this._turnOrderVisual.set(this._turnOrder);
  2040. };
  2041.  
  2042. BattleManagerTBS.determineTurnOrderFair = function () {
  2043.     var array = [];
  2044.     var actors = [];
  2045.     var enemies = [];
  2046.     this._turnOrder = [];
  2047.     this._activeIndex = 0;
  2048.  
  2049.     this.allyEntities().forEach(function (entity) {
  2050.         actors.push(entity);
  2051.     });
  2052.     actors = actors.sort(function (a, b) {
  2053.         return (a._battler.agi > b._battler.agi) ? 1 : ((a._battler.agi < b._battler.agi) ? -1 : 0);
  2054.     });
  2055.     actors.reverse();
  2056.  
  2057.     this.enemyEntities().forEach(function (entity) {
  2058.         enemies.push(entity);
  2059.     });
  2060.     enemies = enemies.sort(function (a, b) {
  2061.         return (a._battler.agi > b._battler.agi) ? 1 : ((a._battler.agi < b._battler.agi) ? -1 : 0);
  2062.     });
  2063.     enemies.reverse();
  2064.  
  2065.     if (actors[0]._battler.agi >= enemies[0]._battler.agi)
  2066.         array.push(actors.shift());
  2067.     else
  2068.         array.push(enemies.shift());
  2069.     var max = actors.length + enemies.length;
  2070.     for (var i = 1; i <= max; i++) {
  2071.         var last = array.leU_last();
  2072.         if (last._battler.isActor()) {
  2073.             if (enemies.length > 0)
  2074.                 array.push(enemies.shift());
  2075.             else
  2076.                 array.push(actors.shift());
  2077.         } else {
  2078.             if (actors.length > 0)
  2079.                 array.push(actors.shift());
  2080.             else
  2081.                 array.push(enemies.shift());
  2082.         }
  2083.     }
  2084.  
  2085.     this._turnOrder = array;
  2086.     this._turnOrderVisual.set(this._turnOrder);
  2087. };
  2088.  
  2089. BattleManagerTBS.activeEntity = function () {
  2090.     return this._turnOrder[this._activeIndex];
  2091. };
  2092.  
  2093. BattleManagerTBS.activeBattler = function () {
  2094.     return this.activeEntity()._battler;
  2095. };
  2096.  
  2097. BattleManagerTBS.startTurn = function () {
  2098.     this._subPhase = "";
  2099.     var entity = this.activeEntity();
  2100.     entity.onTurnStart();
  2101.  
  2102.     var battler = this.activeBattler();
  2103.     this.newAction(battler);
  2104.     LeUtilities.getScene().showStatusWindow(battler);
  2105.  
  2106.     var cell = entity.getCell();
  2107.     this.setCursorCell(cell);
  2108.  
  2109.     if (battler.isActor()) {
  2110.         LeUtilities.getScene().showCommandWindow();
  2111.     } else {
  2112.         this._subPhase = "ai";
  2113.         this._aiManager.process(entity);
  2114.     }
  2115. };
  2116.  
  2117. BattleManagerTBS.updateSequences = function () {
  2118.     this.allEntities().forEach(function (entity) {
  2119.         entity._sequenceManager.update();
  2120.     });
  2121. };
  2122.  
  2123. BattleManagerTBS.anySequenceRunning = function () {
  2124.     return this.allEntities().some(function (entity) {
  2125.         return entity._sequenceManager.isRunning();
  2126.     });
  2127. };
  2128.  
  2129. BattleManagerTBS.newAction = function (battler) {
  2130.     this._activeAction = new Game_Action(battler, false);
  2131. };
  2132.  
  2133. BattleManagerTBS.moveCursor = function (dir) {
  2134.     var x = this._activeCell.x,
  2135.         y = this._activeCell.y;
  2136.     switch (dir) {
  2137.         case "up":
  2138.             y--;
  2139.             break;
  2140.         case "down":
  2141.             y++;
  2142.             break;
  2143.         case "right":
  2144.             x++;
  2145.             break;
  2146.         case "left":
  2147.             x--;
  2148.             break;
  2149.     }
  2150.     var cell = this.getCellAt(x, y);
  2151.     this.setCursorCell(cell);
  2152.     SoundManager.playCursor();
  2153. };
  2154.  
  2155. BattleManagerTBS.setCursorCell = function (cell) {
  2156.     if (!cell) return;
  2157.     cell.select();
  2158.     this.centerCell(cell);
  2159.     this.updateCursor();
  2160.     this.updateScopeSelection();
  2161.     this.updateTargetStatus();
  2162. };
  2163.  
  2164. BattleManagerTBS.updateScopeSelection = function () {
  2165.     if (this.cursorOnMoveScope())
  2166.         this.updateMoveSelection();
  2167.     else if (this.cursorOnActionScope()) {
  2168.         this.updateActionSelection();
  2169.     } else {
  2170.         this.clearActionSelection();
  2171.         this.clearMoveSelection();
  2172.     }
  2173. };
  2174.  
  2175. BattleManagerTBS.updateTargetStatus = function () {
  2176.     var cell = this._activeCell;
  2177.     var entity = cell.getEntity();
  2178.     var scene = LeUtilities.getScene();
  2179.     if (entity) {
  2180.         scene.showStatusWindow(entity._battler);
  2181.     } else {
  2182.         scene.showStatusWindow(this.activeBattler());
  2183.     }
  2184. };
  2185.  
  2186. BattleManagerTBS.onCommandInput = function (command) {
  2187.     switch (command) {
  2188.         case "move":
  2189.             this.processCommandMove();
  2190.             break;
  2191.         case "attack":
  2192.             this.processCommandAttack();
  2193.             break;
  2194.         case "skill":
  2195.             this.processCommandSkill();
  2196.             break;
  2197.         case "item":
  2198.             this.processCommandItem();
  2199.             break;
  2200.         case "pass":
  2201.             this.processCommandPass();
  2202.             break;
  2203.         case "cancel":
  2204.             this.processCommandCancel();
  2205.             break;
  2206.     }
  2207. };
  2208.  
  2209. BattleManagerTBS.processCommandMove = function () {
  2210.     this._subPhase = "move";
  2211.     var points = this.activeEntity().getMovePoints();
  2212.     this.drawMoveScope(this.activeEntity(), points);
  2213. };
  2214.  
  2215. BattleManagerTBS.drawMoveScope = function (entity, points) {
  2216.     var center = entity.getCell().toCoords();
  2217.     var param = this.makeMoveScopeParam(entity);
  2218.     var data = entity.getMoveScopeData();
  2219.     var scope = this.getScopeFromData(data, center, param, points);
  2220.     var color = Lecode.S_TBS.moveColorCell;
  2221.     var opa = Lecode.S_TBS.moveCellOpacity;
  2222.     var invalidOpa = Lecode.S_TBS.moveInvalidCellOpacity;
  2223.     var invalidCondition = "!cell._walkable";
  2224.     this.getLayer("scopes").clear();
  2225.     this.drawScope(scope, color, opa, invalidOpa, invalidCondition);
  2226.     this._moveScope = {};
  2227.     this._moveScope.cells = scope;
  2228.     this._moveScope.center = center;
  2229. };
  2230.  
  2231. BattleManagerTBS.makeMoveScopeParam = function (entity) {
  2232.     var param = {
  2233.         dir: entity.getDir(),
  2234.         exclude_center: true,
  2235.         can_select_obstacles: false,
  2236.         cells_reachable: true,
  2237.     };
  2238.     var data = entity.getMoveScopeParamData();
  2239.     data = LeUtilities.stringSplit(data, ",");
  2240.     data.forEach(function (arg) {
  2241.         if (arg.match(/through_obstacles/i))
  2242.             param.cells_reachable = false;
  2243.     }.bind(this));
  2244.     return param;
  2245. };
  2246.  
  2247. BattleManagerTBS.cursorOnMoveScope = function () {
  2248.     if (!this.isMoveScopeAvailable()) return false;
  2249.     for (var i = 0; i < this.moveScope().cells.length; i++) {
  2250.         var cell = this.moveScope().cells[i];
  2251.         if (cell._walkable) // && !cell.isObstacle())
  2252.             if (cell.x == this.cursor().cellX && cell.y == this.cursor().cellY)
  2253.             return true;
  2254.     }
  2255.     return false;
  2256. };
  2257.  
  2258. BattleManagerTBS.updateMoveSelection = function () {
  2259.     var sx = this.moveScope().center.x,
  2260.         sy = this.moveScope().center.y,
  2261.         dx = this.cursor().cellX,
  2262.         dy = this.cursor().cellY;
  2263.     this._movePath = this.getPathFromAToB(sx, sy, dx, dy, "walkable");
  2264.     this.drawMoveSelection();
  2265. };
  2266.  
  2267. BattleManagerTBS.drawMoveSelection = function () {
  2268.     this.clearMoveSelection();
  2269.     var color = Lecode.S_TBS.moveColorCell;
  2270.     var opacity = Lecode.S_TBS.moveSelectedCellOpacity;
  2271.     for (var i = 0; i < this.movePath().length; i++) {
  2272.         var cell = this.movePath()[i];
  2273.         this.getLayer("scopes").drawSelectionCell(cell.x, cell.y, opacity, color);
  2274.     }
  2275. };
  2276.  
  2277. BattleManagerTBS.clearMoveSelection = function () {
  2278.     this.getLayer("scopes").clearSelection();
  2279. };
  2280.  
  2281. BattleManagerTBS.validateMoveSelection = function () {
  2282.     if (!this.cursorOnMoveScope()) {
  2283.         SoundManager.playBuzzer();
  2284.         return;
  2285.     }
  2286.     SoundManager.playOk();
  2287.     this.activeEntity().processMovement(this.movePath());
  2288.     this._subPhase = "moving";
  2289. };
  2290.  
  2291. BattleManagerTBS.touchMoveSelection = function (selectedCell) {
  2292.     var oldActiveCell = this._activeCell;
  2293.     this.setCursorCell(selectedCell);
  2294.     if (this.cursorOnMoveScope() && InputHandlerTBS.lastSelectedCell().isSame(selectedCell)) {
  2295.         this.validateMoveSelection();
  2296.     }
  2297. };
  2298.  
  2299. BattleManagerTBS.onActiveEntityMoveEnd = function () {
  2300.     this._subPhase = "";
  2301.     this.getLayer("scopes").clear();
  2302.     this.clearMoveSelection();
  2303.     this._moveScope = {};
  2304.  
  2305.     this.activeEntity()._movePerformed = true;
  2306.     if (this.activeBattler().isActor()) {
  2307.         if (Lecode.S_TBS.autoPass && !this.activeEntity().canMoveCommand() && this.activeEntity()._actionPerformed)
  2308.             this.processCommandPass();
  2309.         else
  2310.             LeUtilities.getScene().showCommandWindow();
  2311.     }
  2312.     LeUtilities.getScene().showStatusWindow(this.activeBattler());
  2313.     var cell = this.activeEntity().getCell();
  2314.     this.setCursorCell(cell);
  2315.  
  2316.     this.updateEndOfActionEvents();
  2317. };
  2318.  
  2319. BattleManagerTBS.onMoveCancel = function () {
  2320.     this._subPhase = "";
  2321.     this.getLayer("scopes").clear();
  2322.     this.clearMoveSelection();
  2323.     this._moveScope = {};
  2324.  
  2325.     LeUtilities.getScene().showCommandWindow();
  2326.     var cell = this.activeEntity().getCell();
  2327.     this.setCursorCell(cell);
  2328.     Input.clear();
  2329.  
  2330.     SoundManager.playCancel();
  2331. };
  2332.  
  2333. BattleManagerTBS.isMoveScopeAvailable = function () {
  2334.     return this.moveScope() && this.moveScope().cells && this.moveScope().center;
  2335. };
  2336.  
  2337. BattleManagerTBS.processCommandAttack = function () {
  2338.     this._subPhase = "attack";
  2339.     this.activeAction().setAttack();
  2340.     this.drawAttackScope(this.activeEntity());
  2341. };
  2342.  
  2343. BattleManagerTBS.drawAttackScope = function (entity) {
  2344.     var data = entity.getAttackScopeData();
  2345.     this._actionScopeParam = {
  2346.         color: Lecode.S_TBS.attackColorCell,
  2347.         opacity: Lecode.S_TBS.attackCellOpacity,
  2348.         invalidOpa: Lecode.S_TBS.attackInvalidCellOpacity
  2349.     };
  2350.     this.drawActionScope(entity, data);
  2351.     this.updateScopeSelection();
  2352. };
  2353.  
  2354. BattleManagerTBS.updateAttackSelection = function () {
  2355.     this._actionAoE = this.getAttackAoE();
  2356.     this.drawActionSelection();
  2357. };
  2358.  
  2359. BattleManagerTBS.getAttackAoE = function () {
  2360.     var data = this.activeEntity().getAttackAoEData();
  2361.     var center = this._activeCell.toCoords();
  2362.     var param = this.makeObjAoEParam(null, this.activeEntity(), center);
  2363.     return this.getScopeFromData(data, center, param);
  2364. };
  2365.  
  2366. BattleManagerTBS.processCommandSkill = function () {
  2367.     LeUtilities.getScene().showSkillWindow();
  2368. };
  2369.  
  2370. BattleManagerTBS.onSkillInput = function (command) {
  2371.     switch (command) {
  2372.         case "ok":
  2373.             this.onSkillSelected();
  2374.             break;
  2375.         case "cancel":
  2376.             this.onActionCancel();
  2377.             break;
  2378.     }
  2379. };
  2380.  
  2381. BattleManagerTBS.onSkillSelected = function () {
  2382.     var skill = LeUtilities.getScene()._windowSkill.item();
  2383.     if (skill) {
  2384.         this._subPhase = "skill";
  2385.         this.activeAction().setItemObject(skill);
  2386.         this.drawSkillScope(this.activeEntity());
  2387.     }
  2388. };
  2389.  
  2390. BattleManagerTBS.drawSkillScope = function (entity) {
  2391.     var obj = this.activeAction().item();
  2392.     var data = entity.getObjectScopeData(obj);
  2393.     this._actionScopeParam = {
  2394.         color: Lecode.S_TBS.skillColorCell,
  2395.         opacity: Lecode.S_TBS.skillCellOpacity,
  2396.         invalidOpa: Lecode.S_TBS.skillInvalidCellOpacity,
  2397.         selectedOpacity: Lecode.S_TBS.skillSelectedCellOpacity
  2398.     };
  2399.     this.drawActionScope(entity, data);
  2400.     this.updateScopeSelection();
  2401. };
  2402.  
  2403. BattleManagerTBS.processCommandItem = function () {
  2404.     LeUtilities.getScene().showItemWindow();
  2405. };
  2406.  
  2407. BattleManagerTBS.onItemInput = function (command) {
  2408.     switch (command) {
  2409.         case "ok":
  2410.             this.onItemSelected();
  2411.             break;
  2412.         case "cancel":
  2413.             this.onActionCancel();
  2414.             break;
  2415.     }
  2416. };
  2417.  
  2418. BattleManagerTBS.onItemSelected = function () {
  2419.     var item = LeUtilities.getScene()._windowItem.item();
  2420.     if (item) {
  2421.         this._subPhase = "item";
  2422.         this.activeAction().setItemObject(item);
  2423.         this.drawItemScope(this.activeEntity());
  2424.     }
  2425. };
  2426.  
  2427. BattleManagerTBS.drawItemScope = function (entity) {
  2428.     var obj = this.activeAction().item();
  2429.     var data = entity.getObjectScopeData(obj);
  2430.     this._actionScopeParam = {
  2431.         color: Lecode.S_TBS.ItemColorCell,
  2432.         opacity: Lecode.S_TBS.ItemCellOpacity,
  2433.         invalidOpa: Lecode.S_TBS.ItemInvalidCellOpacity,
  2434.         selectedOpacity: Lecode.S_TBS.ItemSelectedCellOpacity
  2435.     };
  2436.     this.drawActionScope(entity, data);
  2437.     this.updateScopeSelection();
  2438. };
  2439.  
  2440. BattleManagerTBS.updateActionSelection = function () {
  2441.     this._actionAoE = this.getActionAoE();
  2442.     this.drawActionSelection();
  2443. };
  2444.  
  2445. BattleManagerTBS.getActionAoE = function () {
  2446.     var obj = this.activeAction().item();
  2447.     var data = this.activeEntity().getObjectAoEData(obj);
  2448.     var center = this._activeCell.toCoords();
  2449.     var param = this.makeObjAoEParam(obj, this.activeEntity(), center);
  2450.     return this.getScopeFromData(data, center, param);
  2451. };
  2452.  
  2453. BattleManagerTBS.validateActionSelection = function () {
  2454.     if (!this.cursorOnActionScope()) {
  2455.         SoundManager.playBuzzer();
  2456.         return;
  2457.     }
  2458.     SoundManager.playOk();
  2459.  
  2460.     this.getLayer("scopes").clear();
  2461.     this.clearActionSelection();
  2462.  
  2463.     var cell = this.getCellAt(this.cursor().cellX, this.cursor().cellY);
  2464.     this.activeEntity().lookAt(cell);
  2465.     this.processAction();
  2466. };
  2467.  
  2468. BattleManagerTBS.touchActionSelection = function (selectedCell) {
  2469.     var oldActiveCell = this._activeCell;
  2470.     this.setCursorCell(selectedCell);
  2471.     if (this.cursorOnActionScope() && InputHandlerTBS.lastSelectedCell().isSame(selectedCell)) {
  2472.         this.validateActionSelection();
  2473.     }
  2474. };
  2475.  
  2476. BattleManagerTBS.onActionCancel = function () {
  2477.     this._subPhase = "";
  2478.     this.getLayer("scopes").clear();
  2479.     this.clearActionSelection();
  2480.     this._actionScope = {};
  2481.  
  2482.     LeUtilities.getScene().showCommandWindow();
  2483.     var cell = this.activeEntity().getCell();
  2484.     this.setCursorCell(cell);
  2485.  
  2486.     Input.clear();
  2487.     SoundManager.playCancel();
  2488. };
  2489.  
  2490. BattleManagerTBS.isActionScopeAvailable = function () {
  2491.     return this.actionScope() && this.actionScope().cells && this.actionScope().center;
  2492. };
  2493.  
  2494. BattleManagerTBS.processAction = function () {
  2495.     this._subPhase = "obj_invokation";
  2496.     var action = this.activeAction();
  2497.     var item = action.item();
  2498.     var entity = this.activeEntity();
  2499.     var id = action.isAttack() ? entity.getWeaponSequenceData() : entity.getObjectSequenceData(item);
  2500.     this.activeBattler().useItem(item);
  2501.     action.applyGlobal();
  2502.     entity.startSequence(id, action);
  2503. };
  2504.  
  2505. BattleManagerTBS.applyObjEffects = function (user, item, targets, hitAnim, animDelay) {
  2506.     this.activeAction().setItemObject(item);
  2507.     this.prepareDirectionalDamageBonus(user, targets, item);
  2508.     targets.forEach(function (target) {
  2509.         this.activeAction().apply(target.battler());
  2510.         if (target.battler().result().isHit()) {
  2511.             if (hitAnim) {
  2512.                 var cell = target.getCell();
  2513.                 this.getLayer("animations").newAnimation(hitAnim, false, animDelay, cell, target._sprite);
  2514.             }
  2515.             if (target.battler().result().hpDamage > 0)
  2516.                 target.callSequence("damaged");
  2517.         } else {
  2518.             target.callSequence("evaded");
  2519.         }
  2520.         target.addPopup();
  2521.         target.checkDeath();
  2522.     }.bind(this));
  2523.     this.resetDirectionalDamageBonus(targets);
  2524.     this.refreshBattlersStatus();
  2525. };
  2526.  
  2527. BattleManagerTBS.applyObjEffectsOnMap = function (user, item, cellTargets, hitAnim, animDelay) {
  2528.     this.activeAction().setItemObject(item);
  2529.     item = this.activeAction().item();
  2530.     cellTargets.forEach(function (cell) {
  2531.         var target = cell.getEntity();
  2532.         var sprite;
  2533.         if (target) {
  2534.             this.prepareDirectionalDamageBonus(user, [target], item);
  2535.             this.activeAction().apply(target.battler());
  2536.             if (target.battler().result().isHit()) {
  2537.                 sprite = target._sprite;
  2538.                 if (target.battler().result().hpDamage > 0)
  2539.                     target.callSequence("damaged");
  2540.             } else {
  2541.                 target.callSequence("evaded");
  2542.             }
  2543.             target.addPopup();
  2544.             target.checkDeath();
  2545.             this.resetDirectionalDamageBonus([target]);
  2546.         }
  2547.         if (hitAnim)
  2548.             this.getLayer("animations").newAnimation(hitAnim, false, animDelay, cell, sprite);
  2549.     }.bind(this));
  2550.     this.refreshBattlersStatus();
  2551. };
  2552.  
  2553. BattleManagerTBS.applyFloatingDamage = function (amount, target) {
  2554.     target.battler().gainHp(-amount);
  2555.     target.addPopup();
  2556.     target.callSequence("damaged");
  2557.     target.checkDeath();
  2558. };
  2559.  
  2560. BattleManagerTBS.refreshBattlersStatus = function () {
  2561.     LeUtilities.getScene()._windowStatus.refresh();
  2562. };
  2563.  
  2564. BattleManagerTBS.onActionEnd = function () {
  2565.     this._subPhase = "";
  2566.     this._actionScope = {};
  2567.     var cell = this.activeEntity().getCell();
  2568.     this.setCursorCell(cell);
  2569.     Input.clear();
  2570.     this.activeEntity()._actionPerformed = true;
  2571.     LeUtilities.getScene().showStatusWindow(this.activeBattler());
  2572.  
  2573.     var obj = this.activeAction().item();
  2574.     if (this.activeEntity().passAfterObjUse(obj))
  2575.         this.processCommandPass();
  2576.     else if (this.activeBattler().isActor()) {
  2577.         if (Lecode.S_TBS.autoPass && !this.activeEntity().canMoveCommand())
  2578.             this.processCommandPass();
  2579.         else
  2580.             LeUtilities.getScene().showCommandWindow();
  2581.     }
  2582.  
  2583.     this.getLayer("scopes").clear();
  2584.     this.getLayer("scopes").clearSelection();
  2585.  
  2586.     this.updateEndOfActionEvents();
  2587. };
  2588.  
  2589. BattleManagerTBS.processCommandPass = function () {
  2590.     var entity = this.activeEntity();
  2591.     var cell = entity.getCell();
  2592.     if (this.activeBattler().isActor() && Lecode.S_TBS.enableDirectionalFacing) {
  2593.         this._subPhase = "directionSelector_input";
  2594.         this._beforePassDir = entity.getDir();
  2595.         this._directionSelector.set(cell, entity);
  2596.     } else {
  2597.         this.validatePass();
  2598.     }
  2599. };
  2600.  
  2601. BattleManagerTBS.cancelPass = function () {
  2602.     this._subPhase = "";
  2603.     this._directionSelector.hide();
  2604.     LeUtilities.getScene().showCommandWindow();
  2605.     Input.clear();
  2606.     SoundManager.playCancel();
  2607.     this.activeEntity().setDir(this._beforePassDir);
  2608. };
  2609.  
  2610. BattleManagerTBS.validatePass = function () {
  2611.     this._directionSelector.hide();
  2612.     this.turnEnd();
  2613. };
  2614.  
  2615. BattleManagerTBS.passByTouch = function (selectedCell) {
  2616.     var entity = this._directionSelector._battlerEntity;
  2617.     entity.lookAt(selectedCell);
  2618.     this.validatePass();
  2619. };
  2620.  
  2621. BattleManagerTBS.turnEnd = function () {
  2622.     this._subPhase = "turn_end";
  2623.     this.activeEntity().onTurnEnd();
  2624. };
  2625.  
  2626. BattleManagerTBS.updateEndOfActionEvents = function () {
  2627.     this.checkDeathAndVictory();
  2628. };
  2629.  
  2630. BattleManagerTBS.updateEndOfTurnEvents = function () {
  2631.     var canContinue = this.updateEvents() && this.checkDeathAndVictory();
  2632.     if (canContinue) {
  2633.         this.nextTurn();
  2634.     }
  2635. };
  2636.  
  2637. BattleManagerTBS.updateEvents = function () {
  2638.     $gameTroop.updateInterpreter();
  2639.     $gameParty.requestMotionRefresh();
  2640.     if ($gameTroop.isEventRunning()) {
  2641.         return false;
  2642.     }
  2643.     $gameTroop.setupBattleEvent();
  2644.     if ($gameTroop.isEventRunning() || SceneManager.isSceneChanging()) {
  2645.         return false;
  2646.     }
  2647.     return true;
  2648. };
  2649.  
  2650. BattleManagerTBS.nextTurn = function () {
  2651.     do {
  2652.         if (this._activeIndex === 0)
  2653.             $gameTroop.increaseTurn();
  2654.         this._activeIndex++;
  2655.         if (this._activeIndex >= this._turnOrder.length)
  2656.             this._activeIndex = 0;
  2657.     } while (this.activeEntity()._dead);
  2658.     this._turnOrderVisual.updateOnNextTurn(this._turnOrder, this._activeIndex);
  2659.     this.startTurn();
  2660. };
  2661.  
  2662. BattleManagerTBS.processCommandCancel = function () {
  2663.     LeUtilities.getScene().showEndCommandWindow();
  2664. };
  2665.  
  2666. BattleManagerTBS.onEndCommandInput = function (command) {
  2667.     switch (command) {
  2668.         case "options":
  2669.             break;
  2670.         case "escape":
  2671.             this.processEscape();
  2672.             break;
  2673.         case "cancel":
  2674.             this.resumeBattle();
  2675.             break;
  2676.     }
  2677. };
  2678.  
  2679. BattleManagerTBS.resumeBattle = function () {
  2680.     LeUtilities.getScene().showCommandWindow();
  2681.     Input.clear();
  2682. };
  2683.  
  2684. BattleManagerTBS.processEscape = function () {
  2685.     $gameParty.performEscape();
  2686.     var success = BattleManager._preemptive ? true : (Math.random() < BattleManager._escapeRatio);
  2687.     if (success) {
  2688.         BattleManager._escaped = true;
  2689.         SoundManager.playEscape();
  2690.         this.prepareAbort();
  2691.     } else {
  2692.         var audio = {};
  2693.         audio.name = Lecode.S_TBS.escapeSound;
  2694.         audio.pitch = 100;
  2695.         audio.volume = 90;
  2696.         audio.pan = 0;
  2697.         AudioManager.playSe(audio);
  2698.         BattleManager._escapeRatio += 0.1;
  2699.         this.resumeBattle();
  2700.         this.nextTurn();
  2701.     }
  2702. };
  2703.  
  2704. BattleManagerTBS.checkDeathAndVictory = function () {
  2705.     if ($gameParty.isAllDead()) {
  2706.         this.prepareDefeat();
  2707.         return false;
  2708.     } else if ($gameTroop.isAllDead()) {
  2709.         this.prepareVictory();
  2710.         return false;
  2711.     }
  2712.     return true;
  2713. };
  2714.  
  2715. BattleManagerTBS.prepareAbort = function () {
  2716.     this._battleStopStatus = "abort";
  2717.     this.stopBattle();
  2718. };
  2719.  
  2720. BattleManagerTBS.prepareDefeat = function () {
  2721.     this._battleStopStatus = "defeat";
  2722.     this.stopBattle();
  2723. };
  2724.  
  2725. BattleManagerTBS.prepareVictory = function () {
  2726.     this._battleStopStatus = "victory";
  2727.     this.stopBattle();
  2728.     this.allyEntities().forEach(function (entity) {
  2729.         entity.startSequence("victory");
  2730.     });
  2731. };
  2732.  
  2733. BattleManagerTBS.processDefeat = function () {
  2734.     BattleManager.processDefeat();
  2735.     this._phase = "battle_end";
  2736.     this.enemyEntities().forEach(function (entity) {
  2737.         entity.startSequence("victory");
  2738.     });
  2739. };
  2740.  
  2741. BattleManagerTBS.processAbort = function () {
  2742.     BattleManager.processAbort();
  2743.     this._phase = "battle_end";
  2744. };
  2745.  
  2746. BattleManagerTBS.processVictory = function () {
  2747.     BattleManager.processVictory();
  2748.     this._phase = "battle_end";
  2749. };
  2750.  
  2751. BattleManagerTBS.stopBattle = function () {
  2752.     InputHandlerTBS.setActive(false);
  2753.     this._phase = "battle_stopping";
  2754.     this.wait(Lecode.S_TBS.endOfBattleWait);
  2755. };
  2756.  
  2757. BattleManagerTBS.updateBattleStopping = function () {
  2758.     LeUtilities.getScene()._windowCommand.close();
  2759.     this._directionSelector.hide();
  2760.     var waiting = this.isWaiting();
  2761.     switch (this._battleStopStatus) {
  2762.         case "abort":
  2763.             waiting = waiting || this.isWaitingForAbortEvents();
  2764.             break;
  2765.         case "victory":
  2766.             waiting = waiting || this.isWaitingForVictoryEvents();
  2767.             break;
  2768.         case "defeat":
  2769.             waiting = waiting || this.isWaitingForDefeatEvents();
  2770.             break;
  2771.     }
  2772.     if (!waiting) {
  2773.         switch (this._battleStopStatus) {
  2774.             case "abort":
  2775.                 this.processAbort();
  2776.                 break;
  2777.             case "victory":
  2778.                 this.processVictory();
  2779.                 break;
  2780.             case "defeat":
  2781.                 this.processDefeat();
  2782.                 break;
  2783.         }
  2784.     }
  2785. };
  2786.  
  2787. BattleManagerTBS.isWaitingForAbortEvents = function () {
  2788.     return false;
  2789. };
  2790.  
  2791. BattleManagerTBS.isWaitingForVictoryEvents = function () {
  2792.     return false;
  2793. };
  2794.  
  2795. BattleManagerTBS.isWaitingForDefeatEvents = function () {
  2796.     return false;
  2797. };
  2798.  
  2799. BattleManagerTBS.updateBattleEnd = function () {
  2800.     BattleManager.updateBattleEnd();
  2801.     this._phase = null;
  2802. };
  2803.  
  2804. BattleManagerTBS.onEntityDeath = function (entity) {
  2805.     if (this.activeEntity() === entity)
  2806.         this.turnEnd();
  2807.     this._turnOrderVisual.updateOnEntityDeath(this._turnOrder, this._activeIndex);
  2808. };
  2809.  
  2810.  
  2811.  
  2812. BattleManagerTBS.drawActionScope = function (entity, data) {
  2813.     var center = entity.getCell().toCoords();
  2814.     var color = this._actionScopeParam.color;
  2815.     var opacity = this._actionScopeParam.opacity;
  2816.     var invalidOpa = this._actionScopeParam.invalidOpa;
  2817.     var param = this.makeObjScopeParam(null, entity, center);
  2818.     var scope = this.getScopeFromData(data, center, param);
  2819.     var invalidCondition = "!cell._scopeVisible || (cell.isObstacle() && !cell.isThereEntity())";
  2820.     this.getLayer("scopes").clear();
  2821.     this.drawScope(scope, color, opacity, invalidOpa, invalidCondition);
  2822.     this._actionScope = {};
  2823.     this._actionScope.cells = scope;
  2824.     this._actionScope.center = center;
  2825. };
  2826.  
  2827. BattleManagerTBS.makeObjScopeParam = function (obj, entity, center) {
  2828.     obj = obj || this.activeAction().item();
  2829.     var param = {
  2830.         user: entity,
  2831.         dir: dir = entity.getDirectionTo(center),
  2832.         need_check_los: true,
  2833.         exclude_center: true,
  2834.         line_of_sight: true,
  2835.         remove_nonvisibleCells: false
  2836.     };
  2837.     if (!obj) return param;
  2838.     var data = obj.leTbs_scopeParam;
  2839.     data = LeUtilities.stringSplit(data, ",");
  2840.     data.forEach(function (arg) {
  2841.         if (arg.match(/include_center/i))
  2842.             param.exclude_center = false;
  2843.         else if (arg.match(/through_obstacles/i))
  2844.             param.line_of_sight = false;
  2845.         else if (arg.match(/need_free_cell/i))
  2846.             param.need_free_cells = true;
  2847.     }.bind(this));
  2848.     return param;
  2849. };
  2850.  
  2851. BattleManagerTBS.makeObjAoEParam = function (obj, entity, center) {
  2852.     obj = obj || this.activeAction().item();
  2853.     var param = {
  2854.         user: entity,
  2855.         dir: dir = entity.getDirectionTo(center)
  2856.     };
  2857.     return param;
  2858. };
  2859.  
  2860. BattleManagerTBS.cursorOnActionScope = function () {
  2861.     if (!this.isActionScopeAvailable()) return false;
  2862.     for (var i = 0; i < this.actionScope().cells.length; i++) {
  2863.         var cell = this.actionScope().cells[i];
  2864.         if (cell._scopeVisible && !(cell.isObstacle() && !cell.isThereEntity())) {
  2865.             if (cell.x == this.cursor().cellX && cell.y == this.cursor().cellY) {
  2866.                 console.log("Visible: ", cell.x, " ", cell.y);
  2867.                 return true;
  2868.             }
  2869.         }
  2870.     }
  2871.     return false;
  2872. };
  2873.  
  2874. BattleManagerTBS.drawActionSelection = function () {
  2875.     this.clearActionSelection();
  2876.     var color = this._actionScopeParam.color;
  2877.     var opacity = this._actionScopeParam.selectedOpacity;
  2878.     for (var i = 0; i < this._actionAoE.length; i++) {
  2879.         var cell = this._actionAoE[i];
  2880.         this.getLayer("scopes").drawSelectionCell(cell.x, cell.y, opacity, color);
  2881.     }
  2882. };
  2883.  
  2884. BattleManagerTBS.clearActionSelection = function () {
  2885.     this.getLayer("scopes").clearSelection();
  2886. };
  2887.  
  2888. BattleManagerTBS.getScopeFromData = function (data, center, param, points) {
  2889.     var scope = [];
  2890.     if (data.match(/custom\((.+)\)/i)) {
  2891.         var scopeData = Lecode.S_TBS.Config.Custom_Scopes[String(RegExp.$1)];
  2892.         scope = this.getScopeFromRawData(scopeData, center, param);
  2893.     } else if (data.match(/circle\((.+)\)/i)) {
  2894.         scope = this.makeCircleScope(center, Number(RegExp.$1), param, points);
  2895.     } else if (data.match(/line\((.+)\)/i)) {
  2896.         scope = this.makeLineScope(center, Number(RegExp.$1), param, points);
  2897.     } else if (data.match(/square\((.+)\)/i)) {
  2898.         scope = this.makeSquareScope(center, Number(RegExp.$1), param, points);
  2899.     } else if (data.match(/path/i)) {
  2900.         scope = this.makePathScope(param);
  2901.     } else {
  2902.         var cx = center.x;
  2903.         var cy = center.y;
  2904.         var aoe = eval("[" + data + "]");
  2905.         for (var i = 0; i < aoe.length; i++) {
  2906.             var cell = this.getCellAt(aoe[i][0], aoe[i][1]);
  2907.             if (cell)
  2908.                 scope.push(cell);
  2909.         }
  2910.     }
  2911.     scope = LeUtilities.uniqArray(scope);
  2912.     scope = this.applyParamToScope(scope, center, points, param);
  2913.     return scope;
  2914. };
  2915.  
  2916. BattleManagerTBS.getScopeFromRawData = function (scopeData, center, param) {
  2917.     var scope = [];
  2918.     var cx = center.x;
  2919.     var cy = center.y;
  2920.     var ux = this.activeEntity()._cellX;
  2921.     var uy = this.activeEntity()._cellY;
  2922.     var dir = param.dir;
  2923.     var dirData = eval("scopeData.data_" + LeUtilities.directionCodeToText(dir));
  2924.     var data = dirData ? dirData : scopeData.data;
  2925.     var array = eval("[" + data + "]");
  2926.     array.forEach(function (arr) {
  2927.         var cell = this.getCellAt(arr[0], arr[1]);
  2928.         if (cell)
  2929.             scope.push(cell);
  2930.     }.bind(this));
  2931.     return LeUtilities.uniqArray(scope);
  2932. };
  2933.  
  2934. BattleManagerTBS.makeCircleScope = function (center, range, param, points) {
  2935.     points = points || range;
  2936.     var cells = [];
  2937.     var start = param.exclude_center ? 1 : 0;
  2938.     var x = center.x,
  2939.         y = center.y;
  2940.     for (var i = start; i <= range; i++) {
  2941.         cells.push(this.getCellAt(x + i, y));
  2942.         cells.push(this.getCellAt(x - i, y));
  2943.         cells.push(this.getCellAt(x, y + i));
  2944.         cells.push(this.getCellAt(x, y - i));
  2945.         for (var a = start; a <= range - i; a++) {
  2946.             cells.push(this.getCellAt(x - i, y - a));
  2947.             cells.push(this.getCellAt(x - i, y + a));
  2948.             cells.push(this.getCellAt(x + i, y - a));
  2949.             cells.push(this.getCellAt(x + i, y + a));
  2950.         }
  2951.     }
  2952.     return cells;
  2953. };
  2954.  
  2955. BattleManagerTBS.makeSquareScope = function (center, range, param, points) {
  2956.     points = points || range;
  2957.     var cells = [];
  2958.     var x = center.x,
  2959.         y = center.y;
  2960.     for (var i = -range; i <= range; i++) {
  2961.         for (var j = -range; j <= range; j++) {
  2962.             if (param.exclude_center && i === 0 && j === 0)
  2963.                 continue;
  2964.             cells.push(this.getCellAt(x + i, y + j));
  2965.         }
  2966.     }
  2967.     return cells;
  2968. };
  2969.  
  2970. BattleManagerTBS.makeLineScope = function (center, range, param, points) {
  2971.     points = points || range;
  2972.     var cells = [];
  2973.     var start = param.exclude_center ? 1 : 0;
  2974.     var x = center.x,
  2975.         y = center.y;
  2976.     for (var i = start; i <= range; i++) {
  2977.         cells.push(this.getCellAt(x + i, y));
  2978.         cells.push(this.getCellAt(x - i, y));
  2979.         cells.push(this.getCellAt(x, y + i));
  2980.         cells.push(this.getCellAt(x, y - i));
  2981.     }
  2982.     return cells;
  2983. };
  2984.  
  2985. BattleManagerTBS.makeCrossScope = function (center, range, param, points) {
  2986.     points = points || range;
  2987.     var cells = [];
  2988.     var start = param.exclude_center ? 1 : 0;
  2989.     var x = center.x,
  2990.         y = center.y;
  2991.     for (var i = start; i <= range; i++) {
  2992.         cells.push(this.getCellAt(x + i, y + i));
  2993.         cells.push(this.getCellAt(x + i, y - i));
  2994.         cells.push(this.getCellAt(x - i, y + i));
  2995.         cells.push(this.getCellAt(x - i, y - i));
  2996.     }
  2997.     return cells;
  2998. };
  2999.  
  3000. BattleManagerTBS.makePathScope = function (param) {
  3001.     var sx = param.user.getCell().x;
  3002.     var sy = param.user.getCell().y;
  3003.     var dx = this._activeCell.x;
  3004.     var dy = this._activeCell.y;
  3005.     return this.getPathFromAToB(sx, sy, dx, dy, "free");
  3006. };
  3007.  
  3008. BattleManagerTBS.applyParamToScope = function (cells, center, points, param) {
  3009.     cells = this.removeInvalidCells(cells);
  3010.     if (!param.can_select_obstacles) {
  3011.         if (param.remove_obstacles)
  3012.             cells = this.removeObstaclesFromScope(cells);
  3013.     }
  3014.     if (param.cells_reachable) {
  3015.         this.checkScopeWalkable(cells, points, center);
  3016.         if (param.remove_unreachableCells)
  3017.             cells = this.makeScopeReachable(cells, points, center);
  3018.     }
  3019.     if (param.need_check_los) {
  3020.         cells.forEach(function (cell) {
  3021.             cell._scopeVisible = true;
  3022.         }.bind(this));
  3023.         if (param.line_of_sight) {
  3024.             this.checkScopeVisibility(cells, center);
  3025.             if (param.remove_nonvisibleCells)
  3026.                 cells = this.makeScopeVisible(cells, center);
  3027.         }
  3028.     }
  3029.     if (param.need_free_cells) {
  3030.         cells = this.removeObstaclesFromScope(cells);
  3031.         cells = this.removeEntitiesFromScope(cells);
  3032.     }
  3033.     return cells;
  3034. };
  3035.  
  3036. BattleManagerTBS.removeInvalidCells = function (cells) {
  3037.     return cells.filter(function (cell) {
  3038.         return cell;
  3039.     });
  3040. };
  3041.  
  3042. BattleManagerTBS.removeObstaclesFromScope = function (cells) {
  3043.     return cells.filter(function (cell) {
  3044.         return !cell.isObstacle();
  3045.     });
  3046. };
  3047.  
  3048. BattleManagerTBS.removeEntitiesFromScope = function (cells) {
  3049.     return cells.filter(function (cell) {
  3050.         return !cell.isThereEntity();
  3051.     });
  3052. };
  3053.  
  3054. BattleManagerTBS.checkScopeWalkable = function (cells, range, center) {
  3055.     for (var i = 0; i < cells.length; i++) {
  3056.         cells[i]._walkable = false;
  3057.     }
  3058.  
  3059.     var grid = this.getWalkableGridForEasyStar();
  3060.     this._easystar.setGrid(grid);
  3061.     this._easystar.setAcceptableTiles([0]);
  3062.     this._easystar.enableSync();
  3063.  
  3064.     var scope = cells.sort(function (cella, cellb) {
  3065.         var cellaDist = LeUtilities.distanceBetween(cella, center);
  3066.         var cellbDist = LeUtilities.distanceBetween(cellb, center);
  3067.         return (cellaDist > cellbDist) ? 1 : ((cellaDist < cellbDist) ? -1 : 0);
  3068.     });
  3069.     scope.reverse();
  3070.     var reachables = [];
  3071.     for (i = 0; i < scope.length; i++) {
  3072.         var cell = scope[i];
  3073.         var isReachable = false;
  3074.         for (var j = 0; j < reachables.length; j++) {
  3075.             var coord = reachables[j];
  3076.             if (coord[0] == cell.x && coord[1] == cell.y) {
  3077.                 isReachable = true;
  3078.                 break;
  3079.             }
  3080.         }
  3081.         if (isReachable)
  3082.             cell._walkable = true;
  3083.         else
  3084.             cell._walkable = this.isCellReachable(cell, range, center, reachables);
  3085.         reachables = LeUtilities.uniqArray(reachables);
  3086.     }
  3087. };
  3088.  
  3089. BattleManagerTBS.makeScopeReachable = function (cells, range, center) {
  3090.     return cells.filter(function (cell) {
  3091.         return cell._walkable;
  3092.     }.bind(this));
  3093. };
  3094.  
  3095. BattleManagerTBS.isCellReachable = function (cell, range, center, reachables) {
  3096.     if (cell.getEntity()) {
  3097.         if (!cell.getEntity().entitiesCanLayOnMe())
  3098.             return false;
  3099.     } else if (cell.isObstacle())
  3100.         return false;
  3101.     var path = [];
  3102.     var pathResult = null;
  3103.     var sx = center.x;
  3104.     var sy = center.y;
  3105.     var dx = cell.x;
  3106.     var dy = cell.y;
  3107.     this._easystar.findPath(sx, sy, dx, dy, function (result) {
  3108.         pathResult = result;
  3109.     });
  3110.     this._easystar.calculate();
  3111.     if (!pathResult)
  3112.         return false;
  3113.     pathResult.shift();
  3114.     if (pathResult.length > range)
  3115.         return false;
  3116.     return true;
  3117. };
  3118.  
  3119. BattleManagerTBS.checkScopeVisibility = function (cells, center) {
  3120.     var w = $gameMap.tileWidth();
  3121.     var h = $gameMap.tileHeight();
  3122.     var cx = center.x * w + w / 2;
  3123.     var cy = center.y * h + h / 2;
  3124.     var obstacles = [];
  3125.     var boundaries = this.getScopeBoundaries(cells);
  3126.     for (var x = boundaries.left; x <= boundaries.right; x++) {
  3127.         for (var y = boundaries.top; y <= boundaries.bottom; y++) {
  3128.             var cell = this.getCellAt(x, y);
  3129.             if (cell.isObstacle())
  3130.                 obstacles.push(cell);
  3131.         }
  3132.     }
  3133.     /*for (var k = 0; k < cells.length; k++) {
  3134.         if (cells[k].isObstacle())
  3135.             obstacles.push(cells[k]);
  3136.     }*/
  3137.     var nonVisible = [];
  3138.  
  3139.     for (var i = 0; i < cells.length; i++) {
  3140.         cells[i]._scopeVisible = true;
  3141.     }
  3142.  
  3143.     for (i = 0; i < obstacles.length; i++) {
  3144.         var cellsToCheck = this.cellsToCheckNearObstacle(obstacles[i], cells, center);
  3145.         for (var j = 0; j < cellsToCheck.length; j++) {
  3146.             var cellToCheck = cellsToCheck[j];
  3147.             if (obstacles[i].x == cellToCheck.x && obstacles[i].y == cellToCheck.y)
  3148.                 continue;
  3149.             var dx = cellToCheck.x * w + w / 2;
  3150.             var dy = cellToCheck.y * h + h / 2;
  3151.             //- var sprite = SceneManager._scene._spriteset._debugLayer;
  3152.             var pixels = LeUtilities.getPixelsOfLine(cx, cy, dx, dy);
  3153.             for (var k = 0; k < obstacles.length; k++) {
  3154.                 var obstacle = obstacles[k];
  3155.                 if (obstacle.x == center.x && obstacle.y == center.y)
  3156.                     continue;
  3157.                 if (obstacle.isSame(cellToCheck))
  3158.                     continue;
  3159.                 var x = obstacle.x * w;
  3160.                 var y = obstacle.y * h;
  3161.                 for (var m = 0; m < pixels.length; m++) {
  3162.                     if (LeUtilities.doesRectIncludeCoord(x, y, w, h, pixels[m])) {
  3163.                         nonVisible.push([cellToCheck.x, cellToCheck.y]);
  3164.                         m = pixels.length;
  3165.                     }
  3166.                 }
  3167.             }
  3168.         }
  3169.     }
  3170.  
  3171.     for (i = 0; i < nonVisible.length; i++) {
  3172.         for (var j = 0; j < cells.length; j++) {
  3173.             if (cells[j].x === nonVisible[i][0] && cells[j].y === nonVisible[i][1]) {
  3174.                 cells[j]._scopeVisible = false;
  3175.             }
  3176.         }
  3177.     }
  3178. };
  3179.  
  3180. BattleManagerTBS.checkSingleCellVisibility = function (cell, center) {
  3181.     var w = $gameMap.tileWidth();
  3182.     var h = $gameMap.tileHeight();
  3183.     var cx = center.x * w + w / 2;
  3184.     var cy = center.y * h + h / 2;
  3185.  
  3186.     var obstacles = [];
  3187.     for (var i = 0; i < this._groundCells.length; i++) {
  3188.         if (this._groundCells[i].isObstacle())
  3189.             obstacles.push(this._groundCells[i]);
  3190.     }
  3191.     cell._scopeVisible = true;
  3192.  
  3193.     var dx = cell.x * w + w / 2;
  3194.     var dy = cell.y * h + h / 2;
  3195.     var pixels = LeUtilities.getPixelsOfLine(cx, cy, dx, dy);
  3196.     for (var k = 0; k < obstacles.length; k++) {
  3197.         var obstacle = obstacles[k];
  3198.         if (obstacle.x === center.x && obstacle.y === center.y)
  3199.             continue;
  3200.         if (obstacle.isSame(cell))
  3201.             continue;
  3202.         var x = obstacle.x * w;
  3203.         var y = obstacle.y * h;
  3204.         for (var m = 0; m < pixels.length; m++) {
  3205.             if (LeUtilities.doesRectIncludeCoord(x, y, w, h, pixels[m])) {
  3206.                 cell._scopeVisible = false;
  3207.                 return;
  3208.             }
  3209.         }
  3210.     }
  3211. };
  3212.  
  3213. BattleManagerTBS.cellsToCheckNearObstacle = function (obstacle, cells, center) {
  3214.     var cx = center.x,
  3215.         cy = center.y;
  3216.     var result = [];
  3217.     var condition;
  3218.     if (obstacle.y > cy && obstacle.x > cx)
  3219.         condition = "cell.x >= obstacle.x && cell.y >= obstacle.y";
  3220.     else if (obstacle.y > cy && obstacle.x < cx)
  3221.         condition = "cell.x <= obstacle.x && cell.y >= obstacle.y";
  3222.     else if (obstacle.y < cy && obstacle.x > cx)
  3223.         condition = "cell.x >= obstacle.x && cell.y <= obstacle.y";
  3224.     else if (obstacle.y < cy && obstacle.x < cx)
  3225.         condition = "cell.x <= obstacle.x && cell.y <= obstacle.y";
  3226.     else if (obstacle.y == cy && obstacle.x < cx)
  3227.         condition = "cell.x <= obstacle.x";
  3228.     else if (obstacle.y == cy && obstacle.x > cx)
  3229.         condition = "cell.x >= obstacle.x";
  3230.     else if (obstacle.x == cx && obstacle.y < cy)
  3231.         condition = "cell.y <= obstacle.y";
  3232.     else if (obstacle.x == cx && obstacle.y > cy)
  3233.         condition = "cell.y >= obstacle.y";
  3234.     for (var i = 0; i < cells.length; i++) {
  3235.         var cell = cells[i];
  3236.         if (eval(condition))
  3237.             result.push(cell);
  3238.     }
  3239.     return result;
  3240. };
  3241.  
  3242. BattleManagerTBS.makeScopeVisible = function (cells, center) {
  3243.     return cells.filter(function (cell) {
  3244.         return !!cell._scopeVisible;
  3245.     }.bind(this));
  3246. };
  3247.  
  3248. BattleManagerTBS.getScopeBoundaries = function (cells) {
  3249.     var cellsByX = cells.sort(function (cellA, cellB) {
  3250.         return (cellA.x > cellB.x) ? 1 : ((cellA.x < cellB.x) ? -1 : 0);
  3251.     });
  3252.     var rightCell = cellsByX.leU_last();
  3253.     var leftCell = cellsByX[0];
  3254.     var cellsByY = cells.sort(function (cellA, cellB) {
  3255.         return (cellA.y > cellB.y) ? 1 : ((cellA.y < cellB.y) ? -1 : 0);
  3256.     });
  3257.     var bottomCell = cellsByY.leU_last();
  3258.     var topCell = cellsByY[0];
  3259.     return {
  3260.         left: leftCell.x,
  3261.         right: rightCell.x,
  3262.         top: topCell.y,
  3263.         bottom: bottomCell.y
  3264.     };
  3265. };
  3266.  
  3267. BattleManagerTBS.getPathFromAToB = function (sx, sy, dx, dy, gridType) {
  3268.     var path = [];
  3269.     var pathResult = null;
  3270.     var grid = this.getGridForEasyStar();
  3271.     if (gridType === "walkable")
  3272.         grid = this.getWalkableGridForEasyStar();
  3273.     else if (gridType === "free")
  3274.         grid = this.getFreeGridForEasyStar();
  3275.     this._easystar.setGrid(grid);
  3276.     this._easystar.setAcceptableTiles([0]);
  3277.     this._easystar.enableSync();
  3278.     this._easystar.findPath(sx, sy, dx, dy, function (result) {
  3279.         pathResult = result;
  3280.     });
  3281.     this._easystar.calculate();
  3282.     if (!pathResult) return [];
  3283.     pathResult.shift();
  3284.     for (var i = 0; i < pathResult.length; i++) {
  3285.         var cellArr = pathResult[i];
  3286.         var cell = this.getCellAt(cellArr.x, cellArr.y);
  3287.         path.push(cell);
  3288.     }
  3289.     return path;
  3290. };
  3291.  
  3292. BattleManagerTBS.getGridForEasyStar = function () {
  3293.     var grid = [];
  3294.     for (var y = 0; y < $gameMap.height(); y++) {
  3295.         var arr = [];
  3296.         for (var x = 0; x < $gameMap.width(); x++) {
  3297.             var cell = this.getCellAt(x, y);
  3298.             if (!cell || cell.isObstacle())
  3299.                 arr.push(1);
  3300.             else
  3301.                 arr.push(0);
  3302.         }
  3303.         grid.push(arr);
  3304.     }
  3305.     return grid;
  3306. };
  3307.  
  3308. BattleManagerTBS.getFreeGridForEasyStar = function () {
  3309.     var grid = [];
  3310.     for (var y = 0; y < $gameMap.height(); y++) {
  3311.         var arr = [];
  3312.         for (var x = 0; x < $gameMap.width(); x++) {
  3313.             var cell = this.getCellAt(x, y);
  3314.             if (!cell)
  3315.                 arr.push(1);
  3316.             else
  3317.                 arr.push(0);
  3318.         }
  3319.         grid.push(arr);
  3320.     }
  3321.     return grid;
  3322. };
  3323.  
  3324. BattleManagerTBS.getWalkableGridForEasyStar = function () {
  3325.     var grid = [];
  3326.     for (var y = 0; y < $gameMap.height(); y++) {
  3327.         var arr = [];
  3328.         for (var x = 0; x < $gameMap.width(); x++) {
  3329.             var cell = this.getCellAt(x, y);
  3330.             if (cell) {
  3331.                 var entity = cell.getEntity();
  3332.                 if (entity) {
  3333.                     arr.push(entity.isPassable() ? 0 : 1);
  3334.                 } else {
  3335.                     arr.push(cell.isObstacle() ? 1 : 0);
  3336.                 }
  3337.             } else
  3338.                 arr.push(1);
  3339.         }
  3340.         grid.push(arr);
  3341.     }
  3342.     return grid;
  3343. };
  3344.  
  3345. BattleManagerTBS.drawScope = function (cells, color, opa, invalidOpa, invalidCondition) {
  3346.     for (var i = 0; i < cells.length; i++) {
  3347.         var cell = cells[i];
  3348.         var opacity;
  3349.         if (eval(invalidCondition))
  3350.             opacity = invalidOpa;
  3351.         else
  3352.             opacity = opa;
  3353.         this.getLayer("scopes").drawCell(cell.x, cell.y, opacity, color);
  3354.     }
  3355. };
  3356.  
  3357. BattleManagerTBS.isCellInScope = function (cell, scope) {
  3358.     for (var i = 0; i < scope.length; i++) {
  3359.         if (scope[i].isSame(cell))
  3360.             return true;
  3361.     }
  3362.     return false;
  3363. };
  3364.  
  3365. BattleManagerTBS.getEntitiesInScope = function (scope) {
  3366.     var entities = [];
  3367.     for (var i = 0; i < scope.length; i++) {
  3368.         var cell = scope[i];
  3369.         var entity = cell.getEntity();
  3370.         if (entity)
  3371.             entities.push(entity);
  3372.     }
  3373.     return entities;
  3374. };
  3375.  
  3376. BattleManagerTBS.processCollisionEffects = function (entity) {
  3377.     var collisionData = entity._collisionData;
  3378.     if (!collisionData.endCell) return;
  3379.  
  3380.     var distance = collisionData.distance;
  3381.     var covered = collisionData.covered;
  3382.     var user = collisionData.user;
  3383.     var obj = collisionData.obj;
  3384.     var a = user.battler();
  3385.     var b = entity.battler();
  3386.     var formula = Lecode.S_TBS.defaultCollisionFormula;
  3387.     if (obj && obj.leTbs_collisionFormula) {
  3388.         formula = obj.leTbs_collisionFormula;
  3389.     }
  3390.     var damage = Math.floor(eval(formula));
  3391.     var dmgBonus = user.getCollisionDamageBonus(damage);
  3392.     var dmgMinus = entity.getCollisionDamageReduction(damage);
  3393.     damage += dmgBonus - dmgMinus;
  3394.     if (damage < 0) damage = 0;
  3395.     this.applyFloatingDamage(Math.floor(damage), entity);
  3396.  
  3397.     var endCell = collisionData.endCell;
  3398.     var next = endCell.getEntity();
  3399.     while (next) {
  3400.         damage -= damage * Lecode.S_TBS.collissionDamageChainRate;
  3401.         this.applyFloatingDamage(Math.floor(damage), next);
  3402.         var oldDir = next.getDir();
  3403.         next.setDir(collisionData.dir);
  3404.         endCell = next.getForwardCell();
  3405.         next.setDir(oldDir);
  3406.         next = endCell.getEntity();
  3407.     }
  3408.     entity._collisionData = null;
  3409. };
  3410.  
  3411. BattleManagerTBS.prepareDirectionalDamageBonus = function (user, targets, item) {
  3412.     var oldUserDir = user.getDir();
  3413.     for (var i = 0; i < targets.length; i++) {
  3414.         var entity = targets[i];
  3415.         if (entity === user) continue;
  3416.         var dir = entity.getDir();
  3417.         var effects = 0;
  3418.         user.lookAt(entity.getCell());
  3419.         if (user.getDir() === dir) {
  3420.             effects = Lecode.S_TBS.backDirectionalDamageEffects;
  3421.             effects += item.leTbs_directionalDmgBonus.back;
  3422.             effects += user.getDirectionalDmgBonus("back");
  3423.             effects -= entity.getDirectionalDmgReduction("back");
  3424.         } else if (user.getDir() === 2 && dir === 8 || user.getDir() === 8 && dir === 2 ||
  3425.             user.getDir() === 4 && dir === 6 || user.getDir() === 6 && dir === 4) {
  3426.             effects = Lecode.S_TBS.faceDirectionalDamageEffects;
  3427.             effects += item.leTbs_directionalDmgBonus.face;
  3428.             effects += user.getDirectionalDmgBonus("face");
  3429.             effects -= entity.getDirectionalDmgReduction("face");
  3430.         } else {
  3431.             effects = Lecode.S_TBS.sideDirectionalDamageEffects;
  3432.             effects += item.leTbs_directionalDmgBonus.side;
  3433.             effects += user.getDirectionalDmgBonus("side");
  3434.             effects -= entity.getDirectionalDmgReduction("side");
  3435.         }
  3436.         entity.battler().leTBS_setDirectionalDmgEffects(effects * 0.01);
  3437.     }
  3438.     user.setDir(oldUserDir);
  3439. };
  3440.  
  3441. BattleManagerTBS.resetDirectionalDamageBonus = function (targets) {
  3442.     for (var i = 0; i < targets.length; i++) {
  3443.         targets[i].battler().leTBS_setDirectionalDmgEffects(0);
  3444.     }
  3445. };
  3446.  
  3447.  
  3448. BattleManagerTBS.allyStartCells = function () {
  3449.     return this._startCells.filter(function (cell) {
  3450.         return cell._type === "ally";
  3451.     });
  3452. };
  3453.  
  3454. BattleManagerTBS.enemyStartCells = function () {
  3455.     return this._startCells.filter(function (cell) {
  3456.         return cell._type === "enemy";
  3457.     });
  3458. };
  3459.  
  3460. BattleManagerTBS.allyEntities = function () {
  3461.     return this.allEntities().filter(function (ent) {
  3462.         return ent._battler.isActor();
  3463.     });
  3464. };
  3465.  
  3466. BattleManagerTBS.enemyEntities = function () {
  3467.     return this.allEntities().filter(function (ent) {
  3468.         return !ent._battler.isActor();
  3469.     });
  3470. };
  3471.  
  3472. BattleManagerTBS.isCellFree = function (cell) {
  3473.     if (cell == null) return true;
  3474.     return cell.getEntity() == null;
  3475. };
  3476.  
  3477. BattleManagerTBS.getEntityCell = function (entity) {
  3478.     return this._groundCells.leU_find(function (c) {
  3479.         return entity._cellX === c.x && entity._cellY === c.y;
  3480.     }.bind(this));
  3481. };
  3482.  
  3483. BattleManagerTBS.getCellAt = function (x, y) {
  3484.     return this._groundCells.leU_find(function (c) {
  3485.         return x == c.x && y == c.y;
  3486.     }.bind(this));
  3487. };
  3488.  
  3489. BattleManagerTBS.getEntitiesXY = function (excludeActive) {
  3490.     var arr = [];
  3491.     if (!this.allEntities()) return arr;
  3492.     for (var i = 0; i < this.allEntities().length; i++) {
  3493.         var entity = this.allEntities()[i];
  3494.         if (!(excludeActive && entity == this.activeEntity()))
  3495.             arr.push([entity._cellX, entity._cellY]);
  3496.     }
  3497.     return arr;
  3498. };
  3499.  
  3500. BattleManagerTBS.battlerEntities = function () {
  3501.     return this.allEntities();
  3502. };
  3503.  
  3504. BattleManagerTBS.getEntityByBattler = function (battler) {
  3505.     var entities = this.allEntities();
  3506.     for (var i = 0; i < entities.length; i++) {
  3507.         var entity = entities[i];
  3508.         if (entity.battler() === battler)
  3509.             return entity;
  3510.     }
  3511. };
  3512.  
  3513. BattleManagerTBS.canEndPlacementPhase = function () {
  3514.     return this._placeAllies.length === 0;
  3515. };
  3516.  
  3517. BattleManagerTBS.centerActiveCell = function () {
  3518.     this.centerCell(this._activeCell);
  3519. };
  3520.  
  3521. BattleManagerTBS.centerCell = function (cell) {
  3522.     var oldX = $gameMap._displayX;
  3523.     var oldY = $gameMap._displayY;
  3524.     $gamePlayer.center(cell.x, cell.y);
  3525.     this.adaptLayersPos();
  3526.     this.adaptMapVisuals(oldX, oldY);
  3527. };
  3528.  
  3529. BattleManagerTBS.scrollRight = function (distance) {
  3530.     var oldX = $gameMap._displayX;
  3531.     var oldY = $gameMap._displayY;
  3532.     $gameMap.setDisplayPos(oldX + distance, oldY);
  3533.     this.adaptLayersPos();
  3534.     this.adaptMapVisuals(oldX, oldY);
  3535. };
  3536.  
  3537. BattleManagerTBS.scrollDown = function (distance) {
  3538.     var oldX = $gameMap._displayX;
  3539.     var oldY = $gameMap._displayY;
  3540.     $gameMap.setDisplayPos(oldX, oldY + distance);
  3541.     this.adaptLayersPos();
  3542.     this.adaptMapVisuals(oldX, oldY);
  3543. };
  3544.  
  3545. BattleManagerTBS.scrollLeft = function (distance) {
  3546.     this.scrollRight(-distance);
  3547. };
  3548.  
  3549. BattleManagerTBS.scrollUp = function (distance) {
  3550.     this.scrollDown(-distance);
  3551. };
  3552.  
  3553. BattleManagerTBS.adaptLayersPos = function () {
  3554.     this.movableLayers().forEach(function (layer) {
  3555.         layer.x = -$gameMap._displayX * $gameMap.tileWidth();
  3556.         layer.y = -$gameMap._displayY * $gameMap.tileHeight();
  3557.     });
  3558. };
  3559.  
  3560. BattleManagerTBS.adaptMapVisuals = function (oldX, oldY) {
  3561.     var diffX = oldX - $gameMap._displayX;
  3562.     var diffY = oldY - $gameMap._displayY;
  3563.     this.mapVisuals().forEach(function (visual) {
  3564.         visual.x += diffX * $gameMap.tileWidth();
  3565.         visual.y += diffY * $gameMap.tileHeight();
  3566.     });
  3567. };
  3568.  
  3569. BattleManagerTBS.movableLayers = function () {
  3570.     return [this.getLayer("scopes"), this.getLayer("ground"), this.getLayer("groundEntities"), this.getLayer("battlers"),
  3571.         this.getLayer("animations"), this.getLayer("movableInfo")
  3572.     ];
  3573. };
  3574.  
  3575. BattleManagerTBS.mapVisuals = function () {
  3576.     var scene = LeUtilities.getScene();
  3577.     return [scene._windowPlacement, scene._windowCommand, scene._windowSkill, scene._windowItem];
  3578. };
  3579.  
  3580.  
  3581. /*-------------------------------------------------------------------------
  3582. * AI Manager
  3583. -------------------------------------------------------------------------*/
  3584. function TBSAiManager() {
  3585.     this.initialize.call(this, arguments);
  3586. }
  3587.  
  3588. TBSAiManager.prototype.initialize = function () {
  3589.     this._entity = null;
  3590.     this._battler = null;
  3591.     this._commandRunning = null;
  3592.     this._ifArray = [];
  3593.     this._targetData = null;
  3594.     this._cellTarget = null;
  3595.     this._failureCode = "";
  3596.     this._actionData = null;
  3597. };
  3598.  
  3599. TBSAiManager.prototype.process = function (entity) {
  3600.     this._entity = entity;
  3601.     this._battler = entity._battler;
  3602.     this._phase = 0;
  3603.     this._commands = this.getBehavior().slice();
  3604.     this._commandPhase = "init";
  3605.     this._commandNextPhaseCallBack = null;
  3606.     this._commandExtraData = {};
  3607.     this.makeOffenseData();
  3608. };
  3609.  
  3610. TBSAiManager.prototype.makeOffenseData = function () {
  3611.     var user = this._battler;
  3612.     var entity = this._entity;
  3613.     var center = entity.getCell().toCoords();
  3614.     var enemies = this.getEnemiesOf(user, true);
  3615.     var offenses = [];
  3616.     var movePoints = entity.getMovePoints();
  3617.     var skills = this.getUsableSkills(user);
  3618.     //console.log("====== ", user.name());
  3619.     //console.log("USABLE SKILLS: ", skills);
  3620.  
  3621.     var offenseData = [];
  3622.  
  3623.     for (var i = 0; i < skills.length; i++) {
  3624.         var skill = skills[i];
  3625.         if (skill.id === user.attackSkillId() || skill.leTbs_aiOffenseType) {
  3626.             offenses.push(skill);
  3627.         }
  3628.     }
  3629.  
  3630.     enemies = enemies.sort(function (enta, entb) {
  3631.         var cellaDist = LeUtilities.distanceBetween(enta, entity);
  3632.         var cellbDist = LeUtilities.distanceBetween(entb, entity);
  3633.         return (cellaDist > cellbDist) ? 1 : ((cellaDist < cellbDist) ? -1 : 0);
  3634.     });
  3635.  
  3636.     //- For each enemy in the map
  3637.     for (var i = 0; i < enemies.length; i++) {
  3638.         var enemy = enemies[i];
  3639.         //- For each offense usable by the battler
  3640.         for (var j = 0; j < offenses.length; j++) {
  3641.             var offense = offenses[j];
  3642.             /* THE EASIEST BUT SLOW WAY
  3643.             var cellToMoveTo = this.getCellToMoveToForAction(entity, enemy, 0, offense, center);
  3644.             if (cellToMoveTo) {
  3645.                 var action = {};
  3646.                 var reqMp = LeUtilities.distanceBetweenCells(entity.getCell(), cellToMoveTo);
  3647.                 action.requiredMp = reqMp;
  3648.                 action.cellToMoveTo = cellToMoveTo;
  3649.                 action.target = enemy;
  3650.                 action.userCell = entity.getCell();
  3651.                 action.obj = offense;
  3652.                 offenseData.push(action);
  3653.             }*/
  3654.             var closestCellInAoE = this.getClosestActionCellToTarget(enemy, offense, center);
  3655.             var distance = Math.round(LeUtilities.distanceBetweenCells(enemy.getCell(), closestCellInAoE));
  3656.             //- If that enemy is reachable by that offense
  3657.             if (distance - movePoints <= 0) {
  3658.                 var reqMp = distance;
  3659.                 distance = Math.round(LeUtilities.distanceBetweenCells(entity.getCell(), enemy.getCell()));
  3660.                 var cellToMoveTo = null;
  3661.                 if (reqMp === 0 && distance <= 1) {
  3662.                     // No need to move
  3663.                     cellToMoveTo = entity.getCell();
  3664.                     //console.log("NO NEED TO MOVE");
  3665.                 } else {
  3666.                     //- Check line of sight
  3667.                     cellToMoveTo = this.getCellToMoveToForAction(entity, enemy, reqMp, offense, center);
  3668.                 }
  3669.                 //console.log("cellToMoveTo: ", cellToMoveTo);
  3670.                 if (cellToMoveTo) {
  3671.                     var action = {};
  3672.                     action.requiredMp = reqMp;
  3673.                     action.cellToMoveTo = cellToMoveTo;
  3674.                     action.target = enemy;
  3675.                     action.userCell = entity.getCell();
  3676.                     action.obj = offense;
  3677.                     offenseData.push(action);
  3678.                 }
  3679.             }
  3680.         }
  3681.     }
  3682.     this._offenseData = offenseData;
  3683.     //console.log("OFFENSEDATA: ", this._offenseData);
  3684. };
  3685.  
  3686. TBSAiManager.prototype.getClosestActionCellToTarget = function (target, obj, center) {
  3687.     var entity = this._entity;
  3688.     var data = obj.id === entity._battler.attackSkillId() ? entity.getAttackScopeData() : entity.getObjectScopeData(obj);
  3689.     var param = this.BM().makeObjScopeParam(obj, entity, center);
  3690.     var scope = this.BM().getScopeFromData(data, center, param);
  3691.     var closestCell = LeUtilities.closestByDistance(target.getCell(), scope);
  3692.  
  3693.     var aoeData = obj.id === entity._battler.attackSkillId() ? entity.getAttackAoEData() : entity.getObjectAoEData(obj);
  3694.     var aoeCenter = closestCell.toCoords();
  3695.     param = this.BM().makeObjAoEParam(obj, entity, aoeCenter);
  3696.     var aoe = this.BM().getScopeFromData(aoeData, aoeCenter, param);
  3697.     var closestCellInAoE = LeUtilities.closestByDistance(target.getCell(), aoe);
  3698.     return closestCellInAoE;
  3699. };
  3700.  
  3701. TBSAiManager.prototype.getCellToMoveToForAction = function (entity, target, reqMp, obj, center) {
  3702.     var movePoints = entity.getMovePoints();
  3703.     var moveParam = this.BM().makeMoveScopeParam(entity);
  3704.     var moveData = entity.getMoveScopeData();
  3705.     var scope = this.BM().getScopeFromData(moveData, center, moveParam, movePoints);
  3706.  
  3707.     scope = scope.filter(function (cell) {
  3708.         return cell._walkable; // && LeUtilities.distanceBetweenCells(cell, entity.getCell()) === reqMp;
  3709.     }.bind(this));
  3710.  
  3711.     var cellsToCheck = scope.sort(function (a, b) {
  3712.         var obj_aDist = LeUtilities.distanceBetween(a, entity.getCell());
  3713.         var obj_bDist = LeUtilities.distanceBetween(b, entity.getCell());
  3714.         return (obj_aDist > obj_bDist) ? 1 : ((obj_aDist < obj_bDist) ? -1 : 0);
  3715.     }.bind(this));
  3716.  
  3717.     var cellToMoveTo = null;
  3718.     while (cellsToCheck.length > 0) {
  3719.         var cell = cellsToCheck.shift();
  3720.         var currentCenter = cell.toCoords();
  3721.         var closestCell = this.getClosestActionCellToTarget(target, obj, currentCenter);
  3722.         var oldCell = entity.getCell();
  3723.         entity.setCell(cell);
  3724.         this.BM().checkSingleCellVisibility(closestCell, currentCenter);
  3725.         entity.setCell(oldCell);
  3726.         if (closestCell.isSame(target.getCell()) && closestCell._scopeVisible) {
  3727.             cellToMoveTo = cell;
  3728.             break;
  3729.         }
  3730.     }
  3731.     return cellToMoveTo;
  3732. };
  3733.  
  3734. TBSAiManager.prototype.getBehavior = function () {
  3735.     return Lecode.S_TBS.Config.AI["attack"];
  3736. };
  3737.  
  3738. TBSAiManager.prototype.currentCommand = function () {
  3739.     return this._commands[this._phase];
  3740. };
  3741.  
  3742. TBSAiManager.prototype.nextCommand = function () {
  3743.     this._phase++;
  3744.     this._commandRunning = null;
  3745.     this._commandPhase = "init";
  3746.     this._commandNextPhaseCallBack = null;
  3747.     this._commandExtraData = {};
  3748. };
  3749.  
  3750. TBSAiManager.prototype.areConditionsOkay = function () {
  3751.     if (this._ifArray.length === 0) return true;
  3752.     for (var i = 0; i < this._ifArray.length; i++) {
  3753.         if (!this._ifArray[i]) return false;
  3754.     }
  3755.     return true;
  3756. };
  3757.  
  3758. TBSAiManager.prototype.update = function () {
  3759.     if (this._commandRunning) {
  3760.         this.updateRunningCommand();
  3761.         return;
  3762.     }
  3763.     var command = this.currentCommand();
  3764.     if (command) {
  3765.         this.runCommand(command);
  3766.     } else
  3767.         this.forceEnd();
  3768. };
  3769.  
  3770. TBSAiManager.prototype.forceEnd = function () {
  3771.     this.BM().processCommandPass();
  3772. };
  3773.  
  3774. TBSAiManager.prototype.runCommand = function (command) {
  3775.     if (command === "else") {
  3776.         this.commandElse();
  3777.         this.nextCommand();
  3778.         return;
  3779.     } else if (command === "endif") {
  3780.         this.commandEndIf();
  3781.         this.nextCommand();
  3782.         return;
  3783.     }
  3784.  
  3785.     if (!command.match(/(.+)\s?:\s?(.+)/i)) {
  3786.         this.nextCommand();
  3787.         return;
  3788.     }
  3789.  
  3790.     var main = String(RegExp.$1).trim();
  3791.     var paramStr = String(RegExp.$2).trim();
  3792.     var param = LeUtilities.stringSplit(paramStr, ",");
  3793.     param.forEach(function (p) {
  3794.         p = p.trim().replace(" ", "");
  3795.     }.bind(this));
  3796.  
  3797.     if (main === "if") {
  3798.         this.commandIf(paramStr);
  3799.         this.nextCommand();
  3800.         return;
  3801.     }
  3802.  
  3803.     if (this.areConditionsOkay()) {
  3804.         var functionName = LeUtilities.shrinkTextWithUnderscores(main);
  3805.         var func = eval("this.command" + functionName + ".bind(this,param)");
  3806.         func();
  3807.         this._commandRunning = main;
  3808.     } else {
  3809.         this.nextCommand();
  3810.     }
  3811. };
  3812.  
  3813. TBSAiManager.prototype.commandIf = function (condition) {
  3814.     var user = this._battler;
  3815.     var entity = this._entity;
  3816.     var canUseOffense = this.func_CanUseOffense.bind(this);
  3817.     var isTargetValid = this.func_IsTargetValid.bind(this);
  3818.     var chance = this.func_Chance.bind(this);
  3819.     var failureCode = this.func_FailureCode.bind(this);
  3820.     var battlerInRange = this.func_BattlerInRange.bind(this);
  3821.     var distanceBetween = this.func_DistanceBetween.bind(this);
  3822.     var isInMeleeWith = this.func_IsInMeleeWith.bind(this);
  3823.     var pattern = function (str) {
  3824.         return this.func_GetPattern() === str;
  3825.     }.bind(this);
  3826.  
  3827.     var result = eval(condition);
  3828.     this._ifArray.push(result);
  3829. };
  3830.  
  3831. TBSAiManager.prototype.commandElse = function () {
  3832.     this._ifArray[this._ifArray.length - 1] = !this._ifArray.leU_last();
  3833. };
  3834.  
  3835. TBSAiManager.prototype.commandEndIf = function () {
  3836.     this._ifArray.pop();
  3837. };
  3838.  
  3839. TBSAiManager.prototype.commandWait = function (param) {
  3840.     var value = Number(param[0]);
  3841.     this.BM().wait(value);
  3842. };
  3843.  
  3844. TBSAiManager.prototype.commandCallBehavior = function (param) {
  3845.     var id = param[0].trim();
  3846.     var commands = Lecode.S_TBS.Config.AI[id].slice();
  3847.     commands.reverse();
  3848.     for (var i = 0; i < commands.length; i++) {
  3849.         this._commands.splice(this._phase + 1, 0, commands[i]);
  3850.     }
  3851. };
  3852.  
  3853. TBSAiManager.prototype.commandSearchTarget = function (param) {
  3854.     var search = param[0];
  3855.     var maxPm = param[1];
  3856.  
  3857.     if (maxPm.match(/(.+)%/i))
  3858.         maxPm = Number(RegExp.$1) * 0.01 * this._entity.getMovePoints();
  3859.     else
  3860.         maxPm = Number(maxPm);
  3861.     maxPm = Math.floor(maxPm);
  3862.     if (LeUtilities.isNumeric(maxPm)) {
  3863.         maxPm = Math.floor(maxPm);
  3864.         if (maxPm < 1)
  3865.             maxPm = 1;
  3866.         if (maxPm > this._entity.getMovePoints())
  3867.             maxPm = this._entity.getMovePoints();
  3868.     }
  3869.  
  3870.     var data;
  3871.     if (search.match(/enemy/i))
  3872.         data = this._offenseData;
  3873.  
  3874.     var group = this.getGroupFromActionData(data, maxPm);
  3875.     if (!group || group.length === 0) {
  3876.         this._targetData = null;
  3877.         return;
  3878.     }
  3879.  
  3880.     if (search.match(/lowest/i)) {
  3881.         group = group.sort(function (a, b) {
  3882.             a = a._battler;
  3883.             b = b._battler;
  3884.             return (a.hpRate() > b.hpRate()) ? 1 : ((a.hpRate() < b.hpRate()) ? -1 : 0);
  3885.         });
  3886.     } else if (search.match(/closest/i)) {
  3887.         group = group.sort(function (a, b) {
  3888.             var obj_aDist = LeUtilities.distanceBetween(a, this._entity);
  3889.             var obj_bDist = LeUtilities.distanceBetween(b, this._entity);
  3890.             return (obj_aDist > obj_bDist) ? 1 : ((obj_aDist < obj_bDist) ? -1 : 0);
  3891.         }.bind(this));
  3892.     }
  3893.  
  3894.     this._targetData = null;
  3895.     //- Make sure the target is reachable (previously calculated, taken into account when making action data)
  3896.     var entity = group[0];
  3897.     //console.log("SELECTED TARGET = ", entity._battler.name());
  3898.     for (var i = 0; i < data.length; i++) {
  3899.         var action = data[i];
  3900.         if (action.target === entity) {
  3901.             this._targetData = {};
  3902.             this._targetData.userCell = action.userCell;
  3903.             this._targetData.entity = entity;
  3904.             break;
  3905.         }
  3906.     }
  3907. };
  3908.  
  3909. TBSAiManager.prototype.commandDrawMoveScope = function (param) {
  3910.     this.BM().drawMoveScope(this._entity, this._entity.getMovePoints());
  3911. };
  3912.  
  3913. TBSAiManager.prototype.commandMove = function (param) {
  3914.     var search = param[0];
  3915.     var maxPm = param[1];
  3916.  
  3917.     if (!this._entity.canMoveCommand()) return;
  3918.  
  3919.     if (maxPm.match(/(.+)%/i))
  3920.         maxPm = Number(RegExp.$1) * 0.01 * this._entity.getMovePoints();
  3921.     else if (maxPm.match(/minimum/i))
  3922.         maxPm = "minimum";
  3923.     else
  3924.         maxPm = Number(maxPm);
  3925.     if (LeUtilities.isNumeric(maxPm)) {
  3926.         maxPm = Math.floor(maxPm);
  3927.         if (maxPm < 1)
  3928.             maxPm = 1;
  3929.         if (maxPm > this._entity.getMovePoints())
  3930.             maxPm = this._entity.getMovePoints();
  3931.     }
  3932.  
  3933.     this.commandDrawMoveScope();
  3934.     var wait = Lecode.S_TBS.aiWaitTime;
  3935.     this.BM().wait(wait);
  3936.  
  3937.     this._commandPhase = "move_scope_wait";
  3938.     this._commandNextPhaseCallBack = this.commandMoveP2.bind(this, param, search, maxPm);
  3939. };
  3940.  
  3941. TBSAiManager.prototype.commandMoveP2 = function (param, search, maxPm) {
  3942.     var target;
  3943.     if (search.match(/toward_target/i))
  3944.         target = this._targetData.entity;
  3945.     else if (search.match(/enemies/i)) {
  3946.         target = LeUtilities.closestByDistance(this._entity, this.getEnemiesOf(this._battler, true));
  3947.     } else if (search.match(/allies/i)) {
  3948.         target = LeUtilities.closestByDistance(this._entity, this.getAlliesOf(this._battler, true));
  3949.     }
  3950.  
  3951.     if (maxPm === "minimum") {
  3952.         maxPm = this._actionData.requiredMp;
  3953.     }
  3954.  
  3955.     var scope = this.BM().moveScope().cells;
  3956.     scope = scope.filter(function (cell) {
  3957.         return cell._walkable && LeUtilities.distanceBetweenCells(cell, this._entity.getCell()) <= maxPm;
  3958.     }.bind(this));
  3959.  
  3960.     var closestCell;
  3961.     if (search.match(/toward/i))
  3962.         closestCell = LeUtilities.closestByDistance(target.getCell(), scope);
  3963.     else
  3964.         closestCell = LeUtilities.farthestByDistance(target.getCell(), scope);
  3965.  
  3966.     this.BM().setCursorCell(closestCell);
  3967.     var wait = Lecode.S_TBS.aiWaitTime;
  3968.     this.BM().wait(wait);
  3969.  
  3970.     this._commandPhase = "move_selection_wait";
  3971.     this._commandNextPhaseCallBack = this.commandMoveP3.bind(this);
  3972.     this._commandExtraData.destCell = closestCell;
  3973.     this._commandExtraData.savedParams = param.splice();
  3974. };
  3975.  
  3976. TBSAiManager.prototype.commandMoveP3 = function () {
  3977.     this._entity.processMovement(this.BM().movePath());
  3978.     this._commandPhase = "moving";
  3979. };
  3980.  
  3981. TBSAiManager.prototype.commandMoveForAction = function (param) {
  3982.     var cellToMoveTo = this._actionData.cellToMoveTo;
  3983.     if (cellToMoveTo.isSame(this._entity.getCell()))
  3984.         return;
  3985.     this.commandDrawMoveScope();
  3986.     var wait = Lecode.S_TBS.aiWaitTime;
  3987.     this.BM().wait(wait);
  3988.  
  3989.     this._commandPhase = "move_scope_wait";
  3990.     this._commandNextPhaseCallBack = this.commandMoveForActionP2.bind(this);
  3991. };
  3992.  
  3993. TBSAiManager.prototype.commandMoveForActionP2 = function () {
  3994.     var cellToMoveTo = this._actionData.cellToMoveTo;
  3995.  
  3996.     this.BM().setCursorCell(cellToMoveTo);
  3997.     var wait = Lecode.S_TBS.aiWaitTime;
  3998.     this.BM().wait(wait);
  3999.  
  4000.     this._commandPhase = "move_selection_wait";
  4001.     this._commandNextPhaseCallBack = this.commandMoveP3.bind(this);
  4002.     this._commandExtraData.destCell = cellToMoveTo;
  4003.     this._commandExtraData.savedParams = param;
  4004. };
  4005.  
  4006. TBSAiManager.prototype.commandSetAction = function (param) {
  4007.     var type = param[0];
  4008.     var priority = param[1];
  4009.  
  4010.     var searchedTarget = this._targetData.entity;
  4011.     var possibilities = [];
  4012.  
  4013.     if (type.match(/damage/i)) {
  4014.         var data = this._offenseData;
  4015.         for (var i = 0; i < data.length; i++) {
  4016.             var action = data[i];
  4017.             if (action.target === searchedTarget) {
  4018.                 possibilities.push(action);
  4019.             }
  4020.         }
  4021.         possibilities = this.sortActionsByDmg(possibilities);
  4022.     }
  4023.  
  4024.     var action;
  4025.     if (type.match(/average/i)) {
  4026.         if (possibilities.length < 2) {
  4027.             action = possibilities[0];
  4028.         } else {
  4029.             var array = [];
  4030.             for (var i = 1; i < possibilities.length - 1; i++) {
  4031.                 array.push(possibilities[i]);
  4032.             }
  4033.             action = LeUtilities.getRandomValueInArray(array);
  4034.         }
  4035.     } else if (type.match(/best/i)) {
  4036.         action = possibilities[0];
  4037.     } else {
  4038.         action = possibilities.leU_last();
  4039.     }
  4040.  
  4041.     this._actionData = action;
  4042. };
  4043.  
  4044. TBSAiManager.prototype.sortActionsByDmg = function (actionsData) {
  4045.     return actionsData.sort(function (data1, data2) {
  4046.         var obj1 = data1.obj;
  4047.         var obj2 = data2.obj;
  4048.         var dummyAction1 = new Game_Action(this._battler, false);
  4049.         dummyAction1.setItemObject(obj1);
  4050.         var dummyAction2 = new Game_Action(this._battler, false);
  4051.         dummyAction2.setItemObject(obj2);
  4052.         var value1 = dummyAction1.evaluate();
  4053.         var value2 = dummyAction2.evaluate();
  4054.         return (value1 < value2) ? 1 : ((value1 > value2) ? -1 : 0);
  4055.     }.bind(this));
  4056. };
  4057.  
  4058. TBSAiManager.prototype.commandUse = function (param) {
  4059.     var info = param[0];
  4060.  
  4061.     if (!this._entity.canObjCommand()) return;
  4062.  
  4063.     var obj;
  4064.     if (info.match(/defined_action/i))
  4065.         obj = this._actionData.obj;
  4066.  
  4067.     if (obj) {
  4068.         this.BM().activeAction().setItemObject(obj);
  4069.         if (this.BM().activeAction().isAttack())
  4070.             this.BM().drawAttackScope(this._entity);
  4071.         else
  4072.             this.BM().drawSkillScope(this._entity, obj);
  4073.         var wait = 40;
  4074.         this.BM().wait(wait);
  4075.  
  4076.         this._commandPhase = "obj_scope_wait";
  4077.         this._commandNextPhaseCallBack = this.commandUseP2.bind(this, obj);
  4078.     }
  4079. };
  4080.  
  4081. TBSAiManager.prototype.commandUseP2 = function (obj) {
  4082.     var enemy = this._targetData.entity;
  4083.     var center = this._entity.getCell().toCoords();
  4084.     var cell = this.getClosestActionCellToTarget(enemy, obj, center);
  4085.  
  4086.     this.BM().setCursorCell(cell);
  4087.     this.BM().updateActionSelection();
  4088.     this._entity.lookAt(cell);
  4089.     var wait = Lecode.S_TBS.aiWaitTime;
  4090.     this.BM().wait(wait);
  4091.  
  4092.     this._commandPhase = "obj_selection_wait";
  4093.     this._commandNextPhaseCallBack = this.commandUseP3.bind(this, obj);
  4094. };
  4095.  
  4096. TBSAiManager.prototype.commandUseP3 = function (obj) {
  4097.     this.BM().getLayer("scopes").clear();
  4098.     this.BM().clearActionSelection();
  4099.     this.BM().processAction();
  4100.     BattleManagerTBS._subPhase = "ai";
  4101.     this._commandPhase = "invoking";
  4102. };
  4103.  
  4104. TBSAiManager.prototype.commandPass = function (param) {
  4105.     var look = param[0];
  4106.     switch (look) {
  4107.         case "look_closest_enemy":
  4108.             this._entity.lookClosestBattler(this.getEnemiesOf(this._battler, true));
  4109.             break;
  4110.         case "look_closest_ally":
  4111.             this._entity.lookClosestBattler(this.getAlliesOf(this._battler, true));
  4112.             break;
  4113.     }
  4114.     this.BM().processCommandPass();
  4115. };
  4116.  
  4117. TBSAiManager.prototype.commandScript = function (param) {
  4118.     var script = param[0];
  4119.     eval(script);
  4120. };
  4121.  
  4122. TBSAiManager.prototype.updateRunningCommand = function () {
  4123.     var functionName = LeUtilities.shrinkTextWithUnderscores(this._commandRunning);
  4124.     var func = eval("this.updateCommand" + functionName);
  4125.     if (func) {
  4126.         func.call(this);
  4127.     } else {
  4128.         this.nextCommand();
  4129.     }
  4130.     /*switch(this._commandRunning) {
  4131.         case "wait":
  4132.             this.updateCommandWait();
  4133.             break;
  4134.         case "move":
  4135.             this.updateCommandMove();
  4136.             break;
  4137.         case "move_for_action":
  4138.             this.updateCommandMoveForAction();
  4139.             break;
  4140.         case "use":
  4141.             this.updateCommandUse();
  4142.             break;
  4143.         default:
  4144.             this.nextCommand();
  4145.             break;
  4146.     }*/
  4147. };
  4148.  
  4149. TBSAiManager.prototype.updateCommandWait = function () {
  4150.     if (!this.BM().isWaiting())
  4151.         this.nextCommand();
  4152. };
  4153.  
  4154. TBSAiManager.prototype.updateCommandMove = function () {
  4155.     var phase = this._commandPhase;
  4156.     switch (phase) {
  4157.         case "move_scope_wait":
  4158.         case "move_selection_wait":
  4159.             if (!this.BM().isWaiting())
  4160.                 this._commandNextPhaseCallBack();
  4161.             break;
  4162.         case "moving":
  4163.             if (!this._entity.isMoving()) {
  4164.                 this.BM().onActiveEntityMoveEnd();
  4165.                 this.BM()._subPhase = "ai";
  4166.                 if (this._entity.getCell() === this._commandExtraData.destCell)
  4167.                     this.nextCommand();
  4168.                 else
  4169.                     this.commandMove(this._commandExtraData.savedParams);
  4170.             }
  4171.             break;
  4172.         default:
  4173.             this.nextCommand();
  4174.             break;
  4175.     }
  4176. };
  4177.  
  4178. TBSAiManager.prototype.updateCommandMoveForAction = function () {
  4179.     var phase = this._commandPhase;
  4180.     switch (phase) {
  4181.         case "move_scope_wait":
  4182.         case "move_selection_wait":
  4183.             if (!this.BM().isWaiting())
  4184.                 this._commandNextPhaseCallBack();
  4185.             break;
  4186.         case "moving":
  4187.             if (!this._entity.isMoving()) {
  4188.                 this.BM().onActiveEntityMoveEnd();
  4189.                 this.BM()._subPhase = "ai";
  4190.                 if (this._entity.getCell() === this._commandExtraData.destCell)
  4191.                     this.nextCommand();
  4192.                 else
  4193.                     this.commandMoveForAction();
  4194.             }
  4195.             break;
  4196.         default:
  4197.             this.nextCommand();
  4198.             break;
  4199.     }
  4200. };
  4201.  
  4202. TBSAiManager.prototype.updateCommandUse = function () {
  4203.     var phase = this._commandPhase;
  4204.     switch (phase) {
  4205.         case "obj_scope_wait":
  4206.         case "obj_selection_wait":
  4207.             if (!this.BM().isWaiting())
  4208.                 this._commandNextPhaseCallBack();
  4209.             break;
  4210.         case "invoking":
  4211.             if (!this.BM().anySequenceRunning()) {
  4212.                 this.BM().onActionEnd();
  4213.                 this.BM()._subPhase = "ai";
  4214.                 this.nextCommand();
  4215.             }
  4216.             break;
  4217.         default:
  4218.             this.nextCommand();
  4219.             break;
  4220.     }
  4221. };
  4222.  
  4223. TBSAiManager.prototype.BM = function () {
  4224.     return BattleManagerTBS;
  4225. };
  4226.  
  4227. TBSAiManager.prototype.func_Chance = function (value) {
  4228.     return Math.random() < value * 0.01;
  4229. };
  4230.  
  4231. TBSAiManager.prototype.func_IsTargetValid = function () {
  4232.     return this._targetData != null;
  4233. };
  4234.  
  4235. TBSAiManager.prototype.func_FailureCode = function () {
  4236.     console.log("Code: ", this._failureCode);
  4237.     return this._failureCode;
  4238. };
  4239.  
  4240. TBSAiManager.prototype.func_CanUseOffense = function () {
  4241.     //this._failureCode = "";
  4242.     if (this.getUsableSkills(this._battler).length === 0) {
  4243.         this._failureCode = "no_usable_skill";
  4244.         return false;
  4245.     }
  4246.     if (this._offenseData.length <= 0) {
  4247.         this._failureCode = "out_of_range";
  4248.         return false;
  4249.     }
  4250.     return true;
  4251. };
  4252.  
  4253. TBSAiManager.prototype.func_BattlerInRange = function (target, action) {
  4254.     var entity, obj;
  4255.     if (action.match(/defined_action/i)) {
  4256.         if (this._actionData)
  4257.             obj = this._actionData.obj;
  4258.         else
  4259.             return false;
  4260.     }
  4261.     if (target.match(/defined_target/i)) {
  4262.         if (this._targetData)
  4263.             entity = this._targetData.entity;
  4264.         else
  4265.             return false;
  4266.     }
  4267.  
  4268.     var data = (obj.id === this._battler.attackSkillId()) ? this._entity.getAttackScopeData() : this._entity.getObjectScopeData(obj);
  4269.     var center = this._entity.getCell().toCoords();
  4270.     var param = this.BM().makeObjScopeParam(obj, this._entity, center);
  4271.     var scope = this.BM().getScopeFromData(data, center, param);
  4272.     for (var i = 0; i < scope.length; i++) {
  4273.         var cell = scope[i];
  4274.         if (entity.getCell().isSame(cell)) {
  4275.             return true;
  4276.         }
  4277.     }
  4278.     return false;
  4279. };
  4280.  
  4281. TBSAiManager.prototype.func_DistanceBetween = function (target) {
  4282.     var entity;
  4283.     if (target.match(/defined_target/i)) {
  4284.         if (this._targetData)
  4285.             entity = this._targetData.entity;
  4286.         else
  4287.             return false;
  4288.     }
  4289.     return LeUtilities.distanceBetweenCells(entity.getCell(), this._entity.getCell());
  4290. };
  4291.  
  4292. TBSAiManager.prototype.func_IsInMeleeWith = function (target) {
  4293.     return this.func_DistanceBetween(target) <= 1;
  4294. };
  4295.  
  4296. TBSAiManager.prototype.func_GetPattern = function () {
  4297.     return this._entity.getAiPattern();
  4298. };
  4299.  
  4300. TBSAiManager.prototype.getEnemiesOf = function (battler, needAlive) {
  4301.     var group = battler.isActor() ? this.BM().enemyEntities() : this.BM().allyEntities();
  4302.     if (needAlive) {
  4303.         return group.filter(function (ent) {
  4304.             return !ent.battler().isDead();
  4305.         });
  4306.     } else {
  4307.         return group;
  4308.     }
  4309. };
  4310.  
  4311. TBSAiManager.prototype.getAlliesOf = function (battler) {
  4312.     var group = battler.isEnemy() ? this.BM().enemyEntities() : this.BM().allyEntities();
  4313.     if (needAlive) {
  4314.         return group.filter(function (ent) {
  4315.             return !ent.battler().isDead();
  4316.         });
  4317.     } else {
  4318.         return group;
  4319.     }
  4320. };
  4321.  
  4322. TBSAiManager.prototype.getUsableSkills = function (user) {
  4323.     if (user.isActor())
  4324.         return user.usableSkills();
  4325.     var actionList = user.enemy().actions.filter(function (a) {
  4326.         return user.isActionValid(a);
  4327.     });
  4328.     var skills = [];
  4329.     for (var i = 0; i < actionList.length; i++) {
  4330.         var action = actionList[i];
  4331.         skills.push($dataSkills[action.skillId]);
  4332.     }
  4333.     return skills;
  4334. };
  4335.  
  4336. TBSAiManager.prototype.getGroupFromActionData = function (data, maxPm) {
  4337.     var group = [];
  4338.     for (var i = 0; i < data.length; i++) {
  4339.         if (data[i].requiredMp <= maxPm) {
  4340.             var target = data[i].target;
  4341.             if (!target._battler.isDead())
  4342.                 group.push(data[i].target);
  4343.         }
  4344.     }
  4345.     return group;
  4346. };
  4347.  
  4348.  
  4349. /*-------------------------------------------------------------------------
  4350. * Sequence Manager
  4351. -------------------------------------------------------------------------*/
  4352. function TBSSequenceManager() {
  4353.     this.initialize.call(this, arguments);
  4354. }
  4355.  
  4356. TBSSequenceManager.prototype.initialize = function () {
  4357.     this._sequence = null;
  4358.     this._commandRunning = null;
  4359.     this._waitRequested = false;
  4360.     this._users = [];
  4361.     this._lastBattlerTargets = [];
  4362.     this._lastCellTargets = [];
  4363.     this._savedEntities = {};
  4364.     this._savedCells = {};
  4365.     this._sequenceQueue = [];
  4366.     this._obj = null;
  4367. };
  4368.  
  4369. TBSSequenceManager.prototype.start = function (sequence, user, action) {
  4370.     this._sequence = this.parseSequence(sequence).slice();
  4371.     this._commandRunning = null;
  4372.     this._sequenceQueue = [];
  4373.     this._users = [user];
  4374.     if (action)
  4375.         this._obj = action.item();
  4376. };
  4377.  
  4378. TBSSequenceManager.prototype.append = function (sequence, user) {
  4379.     this._sequenceQueue.push([sequence, user]);
  4380. };
  4381.  
  4382. TBSSequenceManager.prototype.getUser = function () {
  4383.     return this._users.leU_last();
  4384. };
  4385.  
  4386. TBSSequenceManager.prototype.getObj = function () {
  4387.     return this._obj;
  4388. };
  4389.  
  4390. TBSSequenceManager.prototype.parseSequence = function (sequence) {
  4391.     if (sequence instanceof Array) return sequence;
  4392.     sequence = sequence.replace("\n", "");
  4393.     return LeUtilities.stringSplit(sequence, ";");
  4394. };
  4395.  
  4396. TBSSequenceManager.prototype.update = function () {
  4397.     if (!this.isRunning()) return;
  4398.     if (this._commandRunning) {
  4399.         this.updateRunningCommand();
  4400.         return;
  4401.     }
  4402.     var command = this._sequence.shift();
  4403.     if (command) {
  4404.         this.runCommand(command);
  4405.     } else
  4406.         this.endOfSequence();
  4407. };
  4408.  
  4409. TBSSequenceManager.prototype.runCommand = function (command) {
  4410.     if (!command.match(/(.+)\s?:\s?(.+)/i)) return;
  4411.     var main = String(RegExp.$1).trim(),
  4412.         param = LeUtilities.stringSplit(String(RegExp.$2).trim(), ",");
  4413.     for (var i = 0; i < param.length; i++) {
  4414.         param[i] = param[i].trim();
  4415.     }
  4416.  
  4417.     this._waitRequested = false;
  4418.     var functionName = LeUtilities.shrinkTextWithUnderscores(main);
  4419.     var func = function () {};
  4420.     try {
  4421.         func = eval("this.command" + functionName + ".bind(this,param)");
  4422.     } catch (e) {
  4423.         console.log("[LeTBS]    Can't find command \"", functionName);
  4424.         console.log(e);
  4425.     }
  4426.     func();
  4427.  
  4428.     this._commandRunning = main;
  4429. };
  4430.  
  4431. TBSSequenceManager.prototype.commandPlayPose = function (param) {
  4432.     var targetData = param[0];
  4433.     var pose = param[1];
  4434.     var afterPose = param[2];
  4435.     var wait = false;
  4436.     if (afterPose === "true") {
  4437.         wait = true;
  4438.         afterPose = null;
  4439.     }
  4440.     var targets = this.readTargets(targetData);
  4441.  
  4442.     targets.forEach(function (target) {
  4443.         target.requestPlayPose();
  4444.         target.setPose(pose, afterPose);
  4445.     }.bind(this));
  4446.  
  4447.     this._waitRequested = wait;
  4448. };
  4449.  
  4450. TBSSequenceManager.prototype.commandSetFrame = function (param) {
  4451.     var targetData = param[0];
  4452.     var pose = param[1];
  4453.     var frame = param[2];
  4454.     var targets = this.readTargets(targetData);
  4455.  
  4456.     targets.forEach(function (target) {
  4457.         target.setFixedPose(pose, frame);
  4458.     }.bind(this));
  4459. };
  4460.  
  4461. TBSSequenceManager.prototype.commandSetSpeed = function (param) {
  4462.     var targetData = param[0];
  4463.     var value = param[1];
  4464.     var targets = this.readTargets(targetData);
  4465.  
  4466.     if (value === "reset") {
  4467.         targets.forEach(function (target) {
  4468.             target.initializeSpeed();
  4469.         }.bind(this));
  4470.     } else if (value.match(/\+(.+)/i)) {
  4471.         targets.forEach(function (target) {
  4472.             target.changeSpeed(Number(RegExp.$1));
  4473.         }.bind(this));
  4474.     } else if (value.match(/\-(.+)/i)) {
  4475.         targets.forEach(function (target) {
  4476.             target.changeSpeed(-Number(RegExp.$1));
  4477.         }.bind(this));
  4478.     } else {
  4479.         targets.forEach(function (target) {
  4480.             target.setSpeed(Number(value));
  4481.         }.bind(this));
  4482.     }
  4483. };
  4484.  
  4485. TBSSequenceManager.prototype.commandSetFrameDelay = function (param) {
  4486.     var targetData = param[0];
  4487.     var value = param[1];
  4488.     var targets = this.readTargets(targetData);
  4489.  
  4490.  
  4491.     if (value === "reset") {
  4492.         targets.forEach(function (target) {
  4493.             target.sprite().initializeFrameDelay();
  4494.         }.bind(this));
  4495.     } else if (value.match(/\+(.+)/i)) {
  4496.         targets.forEach(function (target) {
  4497.             target.sprite().changeFrameDelay(Number(RegExp.$1));
  4498.         }.bind(this));
  4499.     } else if (value.match(/\-(.+)/i)) {
  4500.         targets.forEach(function (target) {
  4501.             target.sprite().changeFrameDelay(-Number(RegExp.$1));
  4502.         }.bind(this));
  4503.     } else {
  4504.         targets.forEach(function (target) {
  4505.             target.sprite().setFrameDelay(Number(value));
  4506.         }.bind(this));
  4507.     }
  4508. };
  4509.  
  4510. TBSSequenceManager.prototype.commandSpriteShake = function (param) {
  4511.     var targetData = param[0];
  4512.     var power = Number(param[1]);
  4513.     var duration = Number(param[2]);
  4514.     var wait = (param[3] === "true");
  4515.     var targets = this.readTargets(targetData);
  4516.  
  4517.     for (var i = 0; i < targets.length; i++) {
  4518.         targets[i].startShake(power, duration);
  4519.     }
  4520.     if (wait) {
  4521.         this.commandWait([duration]);
  4522.     }
  4523. };
  4524.  
  4525. TBSSequenceManager.prototype.commandWait = function (param) {
  4526.     var duration = Number(param[0]);
  4527.  
  4528.     BattleManagerTBS.wait(duration);
  4529. };
  4530.  
  4531. TBSSequenceManager.prototype.commandEffects = function (param) {
  4532.     var targetData = param[0];
  4533.     var objData = param[1];
  4534.     var obj = this.readObject(objData);
  4535.     var hitAnim = param[2] || null;
  4536.     var animDelay = Number(param[3] || 0);
  4537.     var wait = String(param[4] || "false");
  4538.     wait = (wait === "true");
  4539.     var targets = this.readTargets(targetData);
  4540.  
  4541.     if (hitAnim && hitAnim.match(/obj_anim/i)) {
  4542.         if (BattleManagerTBS.activeAction().isAttack())
  4543.             hitAnim = BattleManagerTBS.activeEntity().getAttackAnimation();
  4544.         else
  4545.             hitAnim = BattleManagerTBS.activeAction().item().animationId;
  4546.     }
  4547.     BattleManagerTBS.applyObjEffects(this.getUser(), obj, targets, hitAnim, animDelay);
  4548.  
  4549.     this._waitRequested = wait;
  4550. };
  4551.  
  4552. TBSSequenceManager.prototype.commandMapEffects = function (param) {
  4553.     var targetData = param[0];
  4554.     var objData = param[1];
  4555.     var obj = this.readObject(objData);
  4556.     var hitAnim = param[2] || null;
  4557.     var animDelay = Number(param[3] || 0);
  4558.     var wait = String(param[4] || "false");
  4559.     wait = (wait == "true");
  4560.     var cellTargets = this.readCellTargets(targetData);
  4561.  
  4562.     if (hitAnim && hitAnim.match(/obj_anim/i)) {
  4563.         if (BattleManagerTBS.activeAction().isAttack())
  4564.             hitAnim = BattleManagerTBS.activeEntity().getAttackAnimation();
  4565.         else
  4566.             hitAnim = BattleManagerTBS.activeAction().item().animationId;
  4567.     }
  4568.     BattleManagerTBS.applyObjEffectsOnMap(this.getUser(), obj, cellTargets, hitAnim, animDelay);
  4569.  
  4570.     this._waitRequested = wait;
  4571. };
  4572.  
  4573. TBSSequenceManager.prototype.commandPerformCollapse = function (param) {
  4574.     var targetData = param[0];
  4575.     var targets = this.readTargets(targetData);
  4576.  
  4577.     for (var i = 0; i < targets.length; i++) {
  4578.         targets[i].battler().performCollapse();
  4579.     }
  4580. };
  4581.  
  4582. TBSSequenceManager.prototype.commandAnim = function (param) {
  4583.     var targetData = param[0];
  4584.     var anim = param[1];
  4585.     var animDelay = Number(param[2] || 0);
  4586.     var wait = String(param[3] || "false");
  4587.     wait = (wait == "true");
  4588.     var targets = this.readTargets(targetData);
  4589.  
  4590.     if (anim.match(/obj_anim/i)) {
  4591.         if (BattleManagerTBS.activeAction().isAttack())
  4592.             anim = BattleManagerTBS.activeEntity().getAttackAnimation();
  4593.         else
  4594.             anim = BattleManagerTBS.activeAction().item().animationId;
  4595.     } else if (anim.match(/collapse_anim/i)) {
  4596.         var collapse_anim = true;
  4597.     } else {
  4598.         anim = Number(anim);
  4599.     }
  4600.  
  4601.     targets.forEach(function (target) {
  4602.         if (collapse_anim)
  4603.             anim = target.getCollapseAnimation();
  4604.         BattleManagerTBS.getLayer("animations").newAnimation(anim, false, animDelay, target.getCell(), target._sprite);
  4605.     }.bind(this));
  4606.  
  4607.     this._waitRequested = wait;
  4608. };
  4609.  
  4610. TBSSequenceManager.prototype.commandMapAnim = function (param) {
  4611.     var targetData = param[0];
  4612.     var anim = param[1];
  4613.     var animDelay = Number(param[2] || 0);
  4614.     var wait = String(param[3] || "false");
  4615.     wait = (wait == "true");
  4616.     var cellTargets = this.readCellTargets(targetData);
  4617.  
  4618.     if (anim.match(/obj_anim/i)) {
  4619.         if (BattleManagerTBS.activeAction().isAttack())
  4620.             anim = BattleManagerTBS.activeEntity().getAttackAnimation();
  4621.         else
  4622.             anim = BattleManagerTBS.activeAction().item().animationId;
  4623.     } else {
  4624.         anim = Number(anim);
  4625.     }
  4626.     cellTargets.forEach(function (cell) {
  4627.         BattleManagerTBS.getLayer("animations").newAnimation(anim, false, animDelay, cell);
  4628.     }.bind(this));
  4629.  
  4630.     this._waitRequested = wait;
  4631. };
  4632.  
  4633. TBSSequenceManager.prototype.commandDirectionalAnim = function (param) {
  4634.     var targetData1 = param[0];
  4635.     var targetData2 = param[1];
  4636.     var anim_2 = param[2];
  4637.     var anim_4 = param[3];
  4638.     var anim_6 = param[4];
  4639.     var anim_8 = param[5];
  4640.     var animDelay = Number(param[6] || 0);
  4641.     var wait = String(param[7] || "false");
  4642.     var targets1 = this.readTargets(targetData1);
  4643.  
  4644.     if (targets1.length > 0) {
  4645.         var anim;
  4646.         switch (targets1[0].getDir()) {
  4647.             case 2:
  4648.                 anim = anim_2;
  4649.                 break;
  4650.             case 4:
  4651.                 anim = anim_4;
  4652.                 break;
  4653.             case 6:
  4654.                 anim = anim_6;
  4655.                 break;
  4656.             case 8:
  4657.                 anim = anim_8;
  4658.                 break;
  4659.         }
  4660.         param[0] = targetData2;
  4661.         param[1] = anim;
  4662.         param[2] = animDelay;
  4663.         param[3] = wait;
  4664.         this.commandAnim(param);
  4665.     }
  4666. };
  4667.  
  4668. TBSSequenceManager.prototype.commandLookAt = function (param) {
  4669.     var targetData = param[0];
  4670.     var cellTargetData = param[1];
  4671.     var targets = this.readTargets(targetData);
  4672.     var cellTargets = this.readCellTargets(cellTargetData);
  4673.  
  4674.     if (cellTargets.length > 0) {
  4675.         var cell = cellTargets[0];
  4676.         targets.forEach(function (target) {
  4677.             target.lookAt(cell);
  4678.         }.bind(this));
  4679.     }
  4680. };
  4681.  
  4682. TBSSequenceManager.prototype.commandLookAway = function (param) {
  4683.     var targetData = param[0];
  4684.     var cellTargetData = param[1];
  4685.     var targets = this.readTargets(targetData);
  4686.     var cellTargets = this.readCellTargets(cellTargetData);
  4687.  
  4688.     if (cellTargets.length > 0) {
  4689.         var cell = cellTargets[0];
  4690.         targets.forEach(function (target) {
  4691.             target.lookAway(cell);
  4692.         }.bind(this));
  4693.     }
  4694. };
  4695.  
  4696. TBSSequenceManager.prototype.commandMoveToCell = function (param) {
  4697.     var targetData = param[0];
  4698.     var cellTargetData = param[1];
  4699.     var isInstant = String(param[2] || "false");
  4700.     isInstant = (isInstant.match(/true/i));
  4701.     var targets = this.readTargets(targetData);
  4702.     var cellTargets = this.readCellTargets(cellTargetData);
  4703.  
  4704.     if (targets.length > 0 && cellTargetData.length > 0) {
  4705.         var target = targets[0];
  4706.         var cell = cellTargets[0];
  4707.         if (isInstant) {
  4708.             target.teleport(cell);
  4709.         } else {
  4710.             var path = BattleManagerTBS.getPathFromAToB(target._cellX, target._cellY, cell.x, cell.y, "walkable");
  4711.             target.processMovement(path);
  4712.             this._waitRequested = true;
  4713.         }
  4714.     }
  4715. };
  4716.  
  4717. TBSSequenceManager.prototype.commandJumpToCell = function (param) {
  4718.     var targetData = param[0];
  4719.     var cellTargetData = param[1];
  4720.     var height = param[2] || 120;
  4721.     var targets = this.readTargets(targetData);
  4722.     var cellTargets = this.readCellTargets(cellTargetData);
  4723.  
  4724.     if (targets.length > 0 && cellTargetData.length > 0) {
  4725.         var target = targets[0];
  4726.         var cell = cellTargets[0];
  4727.         var sx = target.x;
  4728.         var sy = target.y;
  4729.         var dx = cell.x * $gameMap.tileWidth();
  4730.         var dy = cell.y * $gameMap.tileHeight();
  4731.         var trajectory = LeUtilities.getPixelsOfJump(sx, sy, dx, dy, height);
  4732.         target.followTrajectory(trajectory, cell);
  4733.         this._waitRequested = true;
  4734.     }
  4735.  
  4736. };
  4737.  
  4738. TBSSequenceManager.prototype.commandMoveStraight = function (param) {
  4739.     var targetData = param[0];
  4740.     var nbr = Number(param[1] || 1);
  4741.     var targets = this.readTargets(targetData);
  4742.  
  4743.     targets.forEach(function (target) {
  4744.         target.forceMoveStraight(nbr, false);
  4745.     }.bind(this));
  4746.     this._waitRequested = true;
  4747. };
  4748.  
  4749. TBSSequenceManager.prototype.commandPush = function (param) {
  4750.     var targetData = param[0];
  4751.     var targetData2 = param[1];
  4752.     var distance = Number(param[2]);
  4753.     var damage = !param[3] || param[3] === "false";
  4754.     var targets = this.readTargets(targetData);
  4755.     var sourceCell = this.readCellTargets(targetData2)[0];
  4756.  
  4757.     for (var i = 0; i < targets.length; i++) {
  4758.         var entity = targets[i];
  4759.         entity.forcePush(this.getUser(), sourceCell, distance, this.getObj(), damage);
  4760.     }
  4761. };
  4762.  
  4763. TBSSequenceManager.prototype.commandPull = function (param) {
  4764.     var targetData = param[0];
  4765.     var targetData2 = param[1];
  4766.     var distance = Number(param[2]);
  4767.     var damage = !param[3] || param[3] === "false";
  4768.     var targets = this.readTargets(targetData);
  4769.     var sourceCell = this.readCellTargets(targetData2)[0];
  4770.  
  4771.     for (var i = 0; i < targets.length; i++) {
  4772.         var entity = targets[i];
  4773.         entity.forcePull(this.getUser(), sourceCell, distance, this.getObj(), damage);
  4774.     }
  4775. };
  4776.  
  4777. TBSSequenceManager.prototype.commandProjectile = function (param) {
  4778.     var id = param[0];
  4779.     var cellTargetData1 = param[1];
  4780.     var cellTargetData2 = param[2];
  4781.     var cellTargets1 = this.readCellTargets(cellTargetData1);
  4782.     var cellTargets2 = this.readCellTargets(cellTargetData2);
  4783.  
  4784.     if (cellTargets1.length > 0 && cellTargets2.length > 0) {
  4785.         var cellStart = cellTargets1[0];
  4786.         var cellEnd = cellTargets2[0];
  4787.         var w = $gameMap.tileWidth();
  4788.         var h = $gameMap.tileHeight();
  4789.         var sx = cellStart.x * w + w / 2;
  4790.         var sy = cellStart.y * h + h / 2;
  4791.         var dx = cellEnd.x * w + w / 2;
  4792.         var dy = cellEnd.y * h + h / 2;
  4793.         BattleManagerTBS._projectilesManager.newProjectile(id, [sx, sy], [dx, dy]);
  4794.     }
  4795. };
  4796.  
  4797. TBSSequenceManager.prototype.commandSetBattlerTargets = function (param) {
  4798.     var targetData = param[0];
  4799.     var targets = this.readTargets(targetData);
  4800.     this._lastBattlerTargets = targets;
  4801. };
  4802.  
  4803. TBSSequenceManager.prototype.commandSetCellTargets = function (param) {
  4804.     var targetData = param[0];
  4805.     var targets = this.readCellTargets(targetData);
  4806.     this._lastCellTargets = targets;
  4807. };
  4808.  
  4809. TBSSequenceManager.prototype.commandSaveEntities = function (param) {
  4810.     var id = param[0];
  4811.     var targetData = param[1];
  4812.     var option = param[2];
  4813.     var targets = this.readTargets(targetData);
  4814.     this._savedEntities[id] = targets;
  4815.     if (option === "shift") {
  4816.         this._savedEntities[id] = [targets.shift()];
  4817.     } else {
  4818.         this._savedEntities[id] = targets;
  4819.     }
  4820. };
  4821.  
  4822. TBSSequenceManager.prototype.commandSaveCells = function (param) {
  4823.     var id = param[0];
  4824.     var targetData = param[1];
  4825.     var option = param[2];
  4826.     var targets = this.readCellTargets(targetData);
  4827.     if (option === "shift") {
  4828.         this._savedCells[id] = [targets.shift()];
  4829.     } else {
  4830.         this._savedCells[id] = targets;
  4831.     }
  4832. };
  4833.  
  4834. TBSSequenceManager.prototype.commandCall = function (param) {
  4835.     var seq = param[0];
  4836.     var times = Number(param[1] || 1);
  4837.  
  4838.     var seqArray = Lecode.S_TBS.Config.Sequences[seq].slice();
  4839.     for (var i = 0; i < times; i++) {
  4840.         for (var j = seqArray.length - 1; j >= 0; j--) {
  4841.             var command = seqArray[j];
  4842.             this._sequence.unshift(command);
  4843.         }
  4844.     }
  4845. };
  4846.  
  4847. TBSSequenceManager.prototype.commandCallForEveryCell = function (param) {
  4848.     var seq = param[0];
  4849.     var targetData = param[1];
  4850.     var sortType = param[2];
  4851.     var targets = this.readCellTargets(targetData);
  4852.  
  4853.     var aoe = this.readCellTargets("aoe");
  4854.     if (sortType === "close->far") {
  4855.         aoe = aoe.sort(function (a, b) {
  4856.             var obj_aDist = LeUtilities.distanceBetween(a, this.getUser());
  4857.             var obj_bDist = LeUtilities.distanceBetween(b, this.getUser());
  4858.             return (obj_aDist > obj_bDist) ? 1 : ((obj_aDist < obj_bDist) ? -1 : 0);
  4859.         }.bind(this));
  4860.     } else if (sortType === "far->close") {
  4861.         aoe = aoe.sort(function (a, b) {
  4862.             var obj_aDist = LeUtilities.distanceBetween(a, this.getUser());
  4863.             var obj_bDist = LeUtilities.distanceBetween(b, this.getUser());
  4864.             return (obj_aDist > obj_bDist) ? -1 : ((obj_aDist < obj_bDist) ? 1 : 0);
  4865.         }.bind(this));
  4866.     } else if (sortType === "random") {
  4867.         aoe = aoe.filter(function (cell) {
  4868.             return true;
  4869.         });
  4870.     }
  4871.     if (aoe.length > 0) {
  4872.         this._savedCells["every_cell_base"] = aoe;
  4873.         //this._savedCells["every_cell"] = aoe.shift();
  4874.  
  4875.         var seqArray = Lecode.S_TBS.Config.Sequences[seq].slice();
  4876.         for (var i = 0; i < aoe.length; i++) {
  4877.             for (var j = seqArray.length - 1; j >= 0; j--) {
  4878.                 var command = seqArray[j];
  4879.                 this._sequence.unshift(command);
  4880.             }
  4881.             this._sequence.unshift("save_cells: every_cell, saved(every_cell_base), shift");
  4882.         }
  4883.     }
  4884. };
  4885.  
  4886. TBSSequenceManager.prototype.commandDelegateCall = function (param) {
  4887.     var seq = param[0];
  4888.     var targetData = param[1];
  4889.     var targets = this.readTargets(targetData);
  4890.  
  4891.     if (targets.length > 0) {
  4892.         this._users.push(targets[0]);
  4893.         param[1] = 1;
  4894.         this.commandCall(param);
  4895.     }
  4896. };
  4897.  
  4898. TBSSequenceManager.prototype.commandEndDelegatedCall = function () {
  4899.     this._users.pop();
  4900. };
  4901.  
  4902. TBSSequenceManager.prototype.commandAskCall = function (param) {
  4903.     var seq = param[0];
  4904.     var targetData = param[1];
  4905.     var targets = this.readTargets(targetData);
  4906.  
  4907.     for (var i = 0; i < targets.length; i++) {
  4908.         targets[i].startSequence(seq);
  4909.     }
  4910. };
  4911.  
  4912. TBSSequenceManager.prototype.commandScript = function (param) {
  4913.     var code = param[0];
  4914.     var user = this.getUser();
  4915.     eval(code);
  4916. };
  4917.  
  4918. TBSSequenceManager.prototype.updateRunningCommand = function () {
  4919.     var command = this._commandRunning;
  4920.     switch (command) {
  4921.         case "wait":
  4922.             this.updateCommandWait();
  4923.             break;
  4924.         case "play_pose":
  4925.             this.updateCommandPlayPose();
  4926.             break;
  4927.         case "anim":
  4928.         case "map_anim":
  4929.         case "effects":
  4930.         case "map_effects":
  4931.             this.updateCommandAnim();
  4932.             break;
  4933.         case "move_to_cell":
  4934.             this.updateCommandMoveToCell();
  4935.             break;
  4936.         case "move_straight":
  4937.             this.updateCommandMoveStraight();
  4938.             break;
  4939.         case "jump_to_cell":
  4940.             this.updateCommandJumpToCell();
  4941.             break;
  4942.         case "projectile":
  4943.             this.updateCommandProjectile();
  4944.             break;
  4945.         default:
  4946.             this._commandRunning = null;
  4947.             break;
  4948.     }
  4949. };
  4950.  
  4951. TBSSequenceManager.prototype.updateCommandWait = function () {
  4952.     if (!BattleManagerTBS.isWaiting())
  4953.         this._commandRunning = null;
  4954. };
  4955.  
  4956. TBSSequenceManager.prototype.updateCommandPlayPose = function () {
  4957.     if (this._waitRequested) {
  4958.         var entities = BattleManagerTBS.battlerEntities();
  4959.         for (var i = 0; i < entities.length; i++) {
  4960.             var entity = entities[i];
  4961.             if (!entity.isRequestedPosePlayed())
  4962.                 return;
  4963.         }
  4964.         this._commandRunning = null;
  4965.         this.update();
  4966.     } else
  4967.         this._commandRunning = null;
  4968. };
  4969.  
  4970. TBSSequenceManager.prototype.updateCommandAnim = function () {
  4971.     if (this._waitRequested) {
  4972.         if (!BattleManagerTBS.getLayer("animations").isAnimationPlaying())
  4973.             this._commandRunning = null;
  4974.     } else
  4975.         this._commandRunning = null;
  4976. };
  4977.  
  4978. TBSSequenceManager.prototype.updateCommandMoveToCell = function () {
  4979.     if (this._waitRequested) {
  4980.         var entities = BattleManagerTBS.battlerEntities();
  4981.         for (var i = 0; i < entities.length; i++) {
  4982.             var entity = entities[i];
  4983.             if (entity.isMoving())
  4984.                 return;
  4985.         }
  4986.         this._commandRunning = null;
  4987.     } else
  4988.         this._commandRunning = null;
  4989. };
  4990.  
  4991. TBSSequenceManager.prototype.updateCommandMoveStraight = function () {
  4992.     if (this._waitRequested) {
  4993.         var entities = BattleManagerTBS.battlerEntities();
  4994.         for (var i = 0; i < entities.length; i++) {
  4995.             var entity = entities[i];
  4996.             if (entity.isMoving())
  4997.                 return;
  4998.         }
  4999.         this._commandRunning = null;
  5000.     } else
  5001.         this._commandRunning = null;
  5002. };
  5003.  
  5004. TBSSequenceManager.prototype.updateCommandJumpToCell = function () {
  5005.     if (this._waitRequested) {
  5006.         var entities = BattleManagerTBS.battlerEntities();
  5007.         for (var i = 0; i < entities.length; i++) {
  5008.             var entity = entities[i];
  5009.             if (entity._sprite.hasTrajectory())
  5010.                 return;
  5011.         }
  5012.         this._commandRunning = null;
  5013.     } else
  5014.         this._commandRunning = null;
  5015. };
  5016.  
  5017. TBSSequenceManager.prototype.updateCommandProjectile = function () {
  5018.     if (!BattleManagerTBS._projectilesManager.isRunning())
  5019.         this._commandRunning = null;
  5020. };
  5021.  
  5022. TBSSequenceManager.prototype.readTargets = function (data) {
  5023.     var targets = [];
  5024.     if (data.match(/aoe_all_(.+)/i)) {
  5025.         var info = String(RegExp.$1);
  5026.         BattleManagerTBS._actionAoE.forEach(function (cell) {
  5027.             var target = cell.getEntity();
  5028.             if (target != null) {
  5029.                 if (info.match(/battlers/i) || info.match(/allies/i) && target.battler().isActor() || info.match(/enemies/i) && !target.battler().isActor())
  5030.                     targets.push(target);
  5031.             }
  5032.         }.bind(this));
  5033.     } else if (data.match(/(\d+)_random_battlers_in_aoe/i)) {
  5034.         var battlersInAoe = [];
  5035.         BattleManagerTBS._actionAoE.forEach(function (cell) {
  5036.             var entity = cell.getEntity();
  5037.             if (entity)
  5038.                 battlersInAoe.push(entity);
  5039.         }.bind(this));
  5040.         targets = LeUtilities.getXRandomValuesInArray(battlersInAoe, Number(RegExp.$1));
  5041.     } else if (data.match(/battler_toward_user/i)) {
  5042.         var cell = this.getUser().getForwardCell();
  5043.         if (cell) {
  5044.             var target = cell.getEntity();
  5045.             if (target)
  5046.                 targets = [target];
  5047.         }
  5048.     } else if (data.match(/user/i)) {
  5049.         targets = [this.getUser()];
  5050.     } else if (data.match(/last_battler_targets/i)) {
  5051.         targets = this._lastBattlerTargets;
  5052.     } else if (data.match(/saved\((.+)\)/i)) {
  5053.         targets = this._savedEntities[RegExp.$1] || [];
  5054.     } else if (data.match(/(.+)_at_distance_(.+)_from_(.+)/i)) {
  5055.         var info = String(RegExp.$1);
  5056.         var maxDist = Number(RegExp.$2);
  5057.         var cell = this.readCellTargets(String(RegExp.$3))[0];
  5058.         BattleManagerTBS.allEntities().forEach(function (entity) {
  5059.             if (info.match(/battlers/i) || info.match(/allies/i) && entity.battler().isActor() || info.match(/enemies/i) && !entity.battler().isActor()) {
  5060.                 var distance = LeUtilities.distanceBetweenCells(cell, entity.getCell());
  5061.                 if (distance <= maxDist)
  5062.                     targets.push(entity);
  5063.             }
  5064.         });
  5065.     }
  5066.  
  5067.     if (data.match(/-user/i)) {
  5068.         var ux = this.getUser()._cellX;
  5069.         var uy = this.getUser()._cellY;
  5070.         targets = targets.filter(function (entity) {
  5071.             return entity._cellX != ux || entity._cellY != uy;
  5072.         }.bind(this));
  5073.     }
  5074.     this._lastBattlerTargets = targets;
  5075.     return targets;
  5076. };
  5077.  
  5078. TBSSequenceManager.prototype.readCellTargets = function (data) {
  5079.     var targets = [];
  5080.     if (data.match(/(\d+)_random_cells_in_aoe/i)) {
  5081.         var cells = BattleManagerTBS._actionAoE;
  5082.         var randomCells = LeUtilities.getXRandomValuesInArray(cells, Number(RegExp.$1));
  5083.         targets = randomCells;
  5084.     } else if (data.match(/last_cell_targets/i)) {
  5085.         targets = this._lastCellTargets;
  5086.     } else if (data.match(/saved\((.+)\)/i)) {
  5087.         console.log("Saved(", RegExp.$1, "): ", this._savedCells[RegExp.$1]);
  5088.         targets = this._savedCells[RegExp.$1] || [];
  5089.     } else if (data.match(/aoe/i)) {
  5090.         targets = BattleManagerTBS._actionAoE;
  5091.     } else if (data.match(/cursor_cell/i)) {
  5092.         var x = BattleManagerTBS.cursor().cellX;
  5093.         var y = BattleManagerTBS.cursor().cellY;
  5094.         targets = [BattleManagerTBS.getCellAt(x, y)];
  5095.     } else if (data.match(/user_cell/i)) {
  5096.         var x = this.getUser()._cellX;
  5097.         var y = this.getUser()._cellY;
  5098.         targets = [BattleManagerTBS.getCellAt(x, y)];
  5099.     }
  5100.  
  5101.     if (data.match(/-user_cell/i)) {
  5102.         targets = targets.filter(function (cell) {
  5103.             return !this.getUser().getCell().isSame(cell);
  5104.         }.bind(this));
  5105.     }
  5106.     this._lastCellTargets = targets;
  5107.     return targets;
  5108. };
  5109.  
  5110. TBSSequenceManager.prototype.readObject = function (data) {
  5111.     if (data.match(/current_obj/i))
  5112.         return BattleManagerTBS.activeAction().item();
  5113.     return null;
  5114. };
  5115.  
  5116. TBSSequenceManager.prototype.endOfSequence = function () {
  5117.     this._sequence = null;
  5118.     this._savedEntities = {};
  5119.     this._savedCells = {};
  5120.     var data = this._sequenceQueue.shift();
  5121.     if (data) {
  5122.         this.start(data[0], data[1]);
  5123.         this.update();
  5124.     }
  5125. };
  5126.  
  5127. TBSSequenceManager.prototype.isRunning = function () {
  5128.     return this._sequence != null;
  5129. };
  5130.  
  5131. /*-------------------------------------------------------------------------
  5132. * TBSProjectilesManager
  5133. -------------------------------------------------------------------------*/
  5134. function TBSProjectilesManager() {
  5135.     this.initialize.apply(this, arguments);
  5136. }
  5137.  
  5138. TBSProjectilesManager.prototype.initialize = function (layer) {
  5139.     this._layer = layer;
  5140.     this._projectiles = [];
  5141.     this._running = false;
  5142. };
  5143.  
  5144. TBSProjectilesManager.prototype.isRunning = function () {
  5145.     return this._projectiles.length > 0;
  5146. };
  5147.  
  5148. TBSProjectilesManager.prototype.newProjectile = function (id, start, end) {
  5149.     var proj = new TBSProjectileSprite(id, start, end);
  5150.     this._projectiles.push(proj);
  5151.     this._layer.addChild(proj);
  5152. };
  5153.  
  5154. TBSProjectilesManager.prototype.update = function () {
  5155.     //this.updateProjectiles();
  5156.     this.destoryProjectiles();
  5157. };
  5158.  
  5159. TBSProjectilesManager.prototype.updateProjectiles = function () {
  5160.     for (var i = 0; i < this._projectiles.length; i++) {
  5161.         this._projectiles[i].update();
  5162.     }
  5163. };
  5164.  
  5165. TBSProjectilesManager.prototype.destoryProjectiles = function () {
  5166.     this._projectiles = this._projectiles.filter(function (proj) {
  5167.         if (proj.finished()) {
  5168.             this._layer.removeChild(proj);
  5169.             return false;
  5170.         }
  5171.         return true;
  5172.     }.bind(this));
  5173. };
  5174.  
  5175. /*-------------------------------------------------------------------------
  5176. * TBSProjectileSprite
  5177. -------------------------------------------------------------------------*/
  5178. function TBSProjectileSprite() {
  5179.     this.initialize.apply(this, arguments);
  5180. }
  5181. TBSProjectileSprite.prototype = Object.create(Sprite_Base.prototype);
  5182. TBSProjectileSprite.prototype.constructor = TBSProjectileSprite;
  5183.  
  5184. TBSProjectileSprite.prototype.initialize = function (id, start, end) {
  5185.     Sprite_Base.prototype.initialize.call(this);
  5186.     this._start = start;
  5187.     this._end = end;
  5188.     this._trajectory = [];
  5189.     this._speedFrame = 0;
  5190.     this.readData(id);
  5191.     this.iniPositions();
  5192.     this.makeTrajectory();
  5193. };
  5194.  
  5195. TBSProjectileSprite.prototype.readData = function (id) {
  5196.     var data = Lecode.S_TBS.Config.Projectiles[id];
  5197.     if (data.filename) {
  5198.         var bitmap = ImageManager.loadLeTBSProjectile(data.filename);
  5199.         this.bitmap = bitmap;
  5200.     } else if (data.anim) {
  5201.         this.bitmap = new Bitmap(data.anim[1], data.anim[2]);
  5202.         this.startAnimation(data.anim[0]);
  5203.     }
  5204.     this._speed = data.speed || 3;
  5205.     this._adaptAngle = false || data.adapt_angle;
  5206.     this._jump = data.jump || 0;
  5207.     this.anchor.x = 0.5;
  5208.     this.anchor.y = 0.5;
  5209. };
  5210.  
  5211. TBSProjectileSprite.prototype.iniPositions = function () {
  5212.     this.x = this._start[0];
  5213.     this.y = this._start[1];
  5214. };
  5215.  
  5216. TBSProjectileSprite.prototype.makeTrajectory = function () {
  5217.     var sx = this._start[0];
  5218.     var sy = this._start[1];
  5219.     var dx = this._end[0];
  5220.     var dy = this._end[1];
  5221.     if (this._jump != 0) {
  5222.         this._trajectory = LeUtilities.getPixelsOfJump(sx, sy, dx, dy, this._jump);
  5223.     } else {
  5224.         this._trajectory = LeUtilities.getPixelsOfLine(sx, sy, dx, dy);
  5225.     }
  5226.     if (this._trajectory[0][0] === dx && this._trajectory[0][1] === dy)
  5227.         this._trajectory = this._trajectory.reverse();
  5228. };
  5229.  
  5230. TBSProjectileSprite.prototype.update = function () {
  5231.     Sprite_Base.prototype.update.call(this);
  5232.     if (!this.finished()) {
  5233.         this.updateAngle();
  5234.         this.updateMove();
  5235.     }
  5236. };
  5237.  
  5238. TBSProjectileSprite.prototype.updateMove = function () {
  5239.     var frame = this._speedFrame;
  5240.     if (frame >= this._trajectory.length - 1) {
  5241.         frame = this._trajectory.length - 1;
  5242.         this._finished = true;
  5243.     }
  5244.     var newPos = this._trajectory[frame];
  5245.     this.x = newPos[0];
  5246.     this.y = newPos[1];
  5247.     this._speedFrame += this._speed;
  5248. };
  5249.  
  5250. TBSProjectileSprite.prototype.updateAngle = function () {
  5251.     if (this._adaptAngle) {
  5252.         var sx, sy, dx, dy;
  5253.         sx = this.x;
  5254.         sy = this.y;
  5255.         if (this._jump === 0) {
  5256.             dx = this._end[0];
  5257.             dy = this._end[1];
  5258.         } else {
  5259.             var topFrame = parseInt(this._trajectory.length / 2);
  5260.             if (this._speedFrame > topFrame) {
  5261.                 dx = this._end[0];
  5262.                 dy = this._end[1];
  5263.             } else {
  5264.                 var topCoord = this._trajectory[topFrame];
  5265.                 dx = topCoord[0];
  5266.                 dy = topCoord[1];
  5267.             }
  5268.         }
  5269.         var rotation = Math.atan2(dy - sy, dx - sx);
  5270.         this.rotation = rotation;
  5271.     }
  5272. };
  5273.  
  5274. TBSProjectileSprite.prototype.startAnimation = function (animation) {
  5275.     animation = $dataAnimations[animation];
  5276.     var sprite = new Sprite_ProjectileAnimation();
  5277.     sprite.setup(this, animation);
  5278.     this.addChild(sprite);
  5279.     this._animationSprites.push(sprite);
  5280. };
  5281.  
  5282. TBSProjectileSprite.prototype.finished = function () {
  5283.     return this._finished;
  5284. };
  5285.  
  5286. /*-------------------------------------------------------------------------
  5287. * Sprite_ProjectileAnimation
  5288. -------------------------------------------------------------------------*/
  5289. function Sprite_ProjectileAnimation() {
  5290.     this.initialize.apply(this, arguments);
  5291. }
  5292. Sprite_ProjectileAnimation.prototype = Object.create(Sprite_Animation.prototype);
  5293. Sprite_ProjectileAnimation.prototype.constructor = Sprite_ProjectileAnimation;
  5294.  
  5295. Sprite_ProjectileAnimation.prototype.initialize = function () {
  5296.     Sprite_Animation.prototype.initialize.call(this);
  5297. };
  5298.  
  5299. Sprite_ProjectileAnimation.prototype.update = function () {
  5300.     Sprite_Animation.prototype.update.call(this);
  5301.     if (!this.isPlaying() && this._animation)
  5302.         this.setupDuration();
  5303. };
  5304.  
  5305. Sprite_ProjectileAnimation.prototype.setup = function (target, animation) {
  5306.     this._target = target;
  5307.     this._animation = animation;
  5308.     this._mirror = false;
  5309.     this._delay = false;
  5310.     if (this._animation) {
  5311.         this.remove();
  5312.         this.setupRate();
  5313.         this.setupDuration();
  5314.         this.loadBitmaps();
  5315.         this.createSprites();
  5316.     }
  5317. };
  5318.  
  5319. Sprite_ProjectileAnimation.prototype.updatePosition = function () {
  5320.     this.x = 0; //this._target.x;
  5321.     this.y = 0; //this._target.y;
  5322. };
  5323.  
  5324. Sprite_ProjectileAnimation.prototype.updateCellSprite = function (sprite, cell) {
  5325.     Sprite_Animation.prototype.updateCellSprite.call(this, sprite, cell);
  5326.     sprite.rotation += this._target.rotation * Math.PI / 180;
  5327. };
  5328.  
  5329.  
  5330. /*-------------------------------------------------------------------------
  5331. * Cell entity
  5332. -------------------------------------------------------------------------*/
  5333. function TBSCell(x, y) {
  5334.     this.x = x;
  5335.     this.y = y;
  5336.     this._regionId = -1;
  5337.     this._type = null;
  5338.     this._event = null;
  5339.     this._visible = false;
  5340.     this._opacity = 0;
  5341.     this._color = "";
  5342. }
  5343.  
  5344. TBSCell.prototype.isSame = function (otherCell) {
  5345.     return this.x === otherCell.x && this.y === otherCell.y;
  5346. };
  5347.  
  5348. TBSCell.prototype.select = function () {
  5349.     BattleManagerTBS._activeCell = this;
  5350. };
  5351.  
  5352. TBSCell.prototype.isImpassableTile = function () {
  5353.     var x = this.x,
  5354.         y = this.y;
  5355.     if (!$gameMap.isValid(x, y)) return true;
  5356.     if (!$gameMap.isPassable(x, y, 2)) return true;
  5357.     if (!$gameMap.isPassable(x, y, 4)) return true;
  5358.     if (!$gameMap.isPassable(x, y, 6)) return true;
  5359.     if (!$gameMap.isPassable(x, y, 8)) return true;
  5360.     return false;
  5361. };
  5362.  
  5363. TBSCell.prototype.isObstacle = function () {
  5364.     if (this.isThereEntity()) return true;
  5365.     if (this._regionId == Lecode.S_TBS.obstacleRegionId) return true;
  5366.     return false;
  5367. };
  5368.  
  5369. TBSCell.prototype.isThereEntity = function () {
  5370.     return this.getEntity() != null;
  5371. };
  5372.  
  5373. TBSCell.prototype.getEntity = function () {
  5374.     return BattleManagerTBS.allEntities().leU_find(function (entity) {
  5375.         return entity._cellX == this.x && entity._cellY == this.y;
  5376.     }.bind(this));
  5377. };
  5378.  
  5379. TBSCell.prototype.toCoords = function () {
  5380.     return {
  5381.         x: this.x,
  5382.         y: this.y
  5383.     };
  5384. };
  5385.  
  5386. /*-------------------------------------------------------------------------
  5387. * SpriteCursorTBS
  5388. -------------------------------------------------------------------------*/
  5389. function SpriteCursorTBS() {
  5390.     this.initialize.apply(this, arguments);
  5391. }
  5392.  
  5393. SpriteCursorTBS.prototype = Object.create(Sprite.prototype);
  5394. SpriteCursorTBS.prototype.constructor = SpriteCursorTBS;
  5395.  
  5396. SpriteCursorTBS.prototype.initialize = function (bitmap) {
  5397.     Sprite.prototype.initialize.call(this, bitmap);
  5398.     this.cellX = this.cellY = 0;
  5399.     this._leU_loopFlash = true;
  5400.     this._leU_loopFlashData = {
  5401.         color: [255, 255, 255, 255],
  5402.         duration: 30
  5403.     };
  5404. };
  5405.  
  5406. /*-------------------------------------------------------------------------
  5407. * TBSTurnOrderVisual
  5408. -------------------------------------------------------------------------*/
  5409. function TBSTurnOrderVisual() {
  5410.     this.initialize.apply(this, arguments);
  5411. }
  5412.  
  5413.  
  5414. /*-------------------------------------------------------------------------
  5415. * TBSEntity
  5416. -------------------------------------------------------------------------*/
  5417. function TBSEntity() {
  5418.     this.initialize.apply(this, arguments);
  5419.  
  5420.     Object.defineProperty(this, 'x', {
  5421.         get: function () {
  5422.             return this._posX;
  5423.         },
  5424.         set: function (value) {
  5425.             this.setPosition(value, this.y);
  5426.         },
  5427.         configurable: true
  5428.     });
  5429.  
  5430.     Object.defineProperty(this, 'y', {
  5431.         get: function () {
  5432.             return this._posY;
  5433.         },
  5434.         set: function (value) {
  5435.             this.setPosition(this.x, value);
  5436.         },
  5437.         configurable: true
  5438.     });
  5439. }
  5440.  
  5441. TBSEntity.prototype.initialize = function (battler, layer) {
  5442.     this._battler = battler;
  5443.     this._cell = null;
  5444.     this._cellX = 0;
  5445.     this._cellY = 0;
  5446.     this._moving = false;
  5447.     this._movePoints = 0;
  5448.     this._movingDistance = [0, 0];
  5449.     this._movePath = [];
  5450.     this._movingNextCell = null;
  5451.     this._moveReducePoints = false;
  5452.     this._movePerformed = false;
  5453.     this._actionPerformed = false;
  5454.     this._dead = false;
  5455.     this.createSprite(battler, layer);
  5456.     this.createComponents();
  5457.     this.setMovePoints();
  5458.     this.initializeSpeed();
  5459. };
  5460.  
  5461. TBSEntity.prototype.createSprite = function (battler, layer) {
  5462.     this._sprite = new TBSEntity_Sprite(battler, this);
  5463.     this._posX = 0;
  5464.     this._posY = 0;
  5465.     this._dir = 2;
  5466.     this._lastDir = 2;
  5467.     this._pose = "idle";
  5468.     this._afterPose = "idle";
  5469.     this._poseLoop = false;
  5470.     this._fixedPose = null;
  5471.     layer.addChild(this._sprite);
  5472. };
  5473.  
  5474. TBSEntity.prototype.createComponents = function () {
  5475.     this._sequenceManager = new TBSSequenceManager();
  5476. };
  5477.  
  5478. TBSEntity.prototype.initializeSpeed = function () {
  5479.     this.setSpeed(Lecode.S_TBS.battlersMoveSpeed);
  5480. };
  5481.  
  5482. TBSEntity.prototype.setSpeed = function (speed) {
  5483.     this._speed = speed;
  5484. };
  5485.  
  5486. TBSEntity.prototype.changeSpeed = function (speed) {
  5487.     this._speed += speed;
  5488. };
  5489.  
  5490. TBSEntity.prototype.startSequence = function (id, action) {
  5491.     var sequence = Lecode.S_TBS.Config.Sequences[id];
  5492.     this._sequenceManager.start(sequence, this, action);
  5493. };
  5494.  
  5495. TBSEntity.prototype.callSequence = function (id) {
  5496.     if (this._sequenceManager.isRunning()) {
  5497.         this._sequenceManager.commandCall([id, 1]);
  5498.     } else {
  5499.         this.startSequence(id);
  5500.     }
  5501. };
  5502.  
  5503. TBSEntity.prototype.appendSequence = function (id) {
  5504.     if (this._sequenceManager.isRunning()) {
  5505.         var sequence = Lecode.S_TBS.Config.Sequences[id];
  5506.         this._sequenceManager.append(sequence, this);
  5507.     } else {
  5508.         this.startSequence(id);
  5509.     }
  5510. };
  5511.  
  5512. TBSEntity.prototype.requestPlayPose = function () {
  5513.     this._requestPlayPose = true;
  5514. };
  5515.  
  5516. TBSEntity.prototype.isRequestedPosePlayed = function () {
  5517.     return !this._requestPlayPose;
  5518. };
  5519.  
  5520. TBSEntity.prototype.setPose = function (pose, afterPose) {
  5521.     this._fixedPose = null;
  5522.     this._pose = pose;
  5523.     this._afterPose = afterPose || "idle";
  5524.     this._poseLoop = (afterPose === pose);
  5525.     this._sprite.updatePose();
  5526.     this._sprite.resetFrameCount();
  5527.     this._sprite.setFixedFrame(null);
  5528.     this._sprite.updateBitmap();
  5529.     this._sprite.updatePosition();
  5530. };
  5531.  
  5532. TBSEntity.prototype.setFixedPose = function (pose, frame) {
  5533.     this._pose = pose;
  5534.     this._sprite.updatePose();
  5535.     if (frame === "last")
  5536.         frame = this._sprite._maxFrame[this._sprite.getPose()];
  5537.     this._fixedPose = pose;
  5538.     this._afterPose = null;
  5539.     this._poseLoop = false;
  5540.     this._sprite.resetFrameCount();
  5541.     this._sprite.setFixedFrame(Number(frame));
  5542.     this._sprite.updateBitmap();
  5543.     this._sprite.updatePosition();
  5544. };
  5545.  
  5546. TBSEntity.prototype.onPosePlayed = function (oldPose) {
  5547.     if (this._fixedPose) {
  5548.         this._pose = this._fixedPose;
  5549.         this._sprite.updatePose();
  5550.         return;
  5551.     }
  5552.     if (this._poseLoop) {
  5553.         this.setPose(oldPose, oldPose);
  5554.     } else {
  5555.         this.setPose(this._afterPose);
  5556.         this._afterPose = "idle";
  5557.     }
  5558.     this._requestPlayPose = false;
  5559. };
  5560.  
  5561. TBSEntity.prototype.setDir = function (dir) {
  5562.     if (![2, 4, 6, 8].contains(dir)) return;
  5563.     this._lastDir = this.getDir();
  5564.     this._dir = dir;
  5565.     this._sprite.update();
  5566. };
  5567.  
  5568. TBSEntity.prototype.getDir = function () {
  5569.     return this._dir;
  5570. };
  5571.  
  5572. TBSEntity.prototype.setCell = function (cell) {
  5573.     var x = cell.x * $gameMap.tileWidth();
  5574.     var y = cell.y * $gameMap.tileHeight();
  5575.     this.setPosition(x, y);
  5576.     this._cellX = cell.x;
  5577.     this._cellY = cell.y;
  5578.     this._cell = cell;
  5579. };
  5580.  
  5581. TBSEntity.prototype.setPosition = function (x, y) {
  5582.     this._posX = x;
  5583.     this._posY = y;
  5584. };
  5585.  
  5586. TBSEntity.prototype.shiftPosition = function (x, y) {
  5587.     this._posX += x;
  5588.     this._posY += y;
  5589. };
  5590.  
  5591. TBSEntity.prototype.update = function () {
  5592.     this.checkDeath();
  5593.     this.updateMove();
  5594. };
  5595.  
  5596. TBSEntity.prototype.checkDeath = function () {
  5597.     if (this._battler.isDead() && !this._dead) {
  5598.         this.onDeath();
  5599.     } else {
  5600.         this.onRevive();
  5601.     }
  5602. };
  5603.  
  5604. TBSEntity.prototype.onBattleStart = function () {
  5605.     this._battler.onBattleStart();
  5606.     this.setMovePoints();
  5607. };
  5608.  
  5609. TBSEntity.prototype.onTurnStart = function () {
  5610.     if (this._battler.onTurnStart) //- Compatibility for YEA
  5611.         this._battler.onTurnStart();
  5612.     this.setMovePoints();
  5613.     this.callSequence("turn_start");
  5614. };
  5615.  
  5616. TBSEntity.prototype.onTurnEnd = function () {
  5617.     this._battler.onTurnEnd();
  5618.     this.addPopup();
  5619.     this.setMovePoints();
  5620.     this._movePerformed = false;
  5621.     this._actionPerformed = false;
  5622. };
  5623.  
  5624. TBSEntity.prototype.onDeath = function () {
  5625.     this._dead = true;
  5626.     this.appendSequence("dead");
  5627.     BattleManagerTBS.onEntityDeath(this);
  5628. };
  5629.  
  5630. TBSEntity.prototype.onRevive = function () {
  5631.  
  5632. };
  5633.  
  5634. TBSEntity.prototype.lookAt = function (cell) {
  5635.     var downD = cell.y - this._cellY;
  5636.     var upD = this._cellY - cell.y;
  5637.     var rightD = cell.x - this._cellX;
  5638.     var leftD = this._cellX - cell.x;
  5639.     var maxD = LeUtilities.getMaxInArray([downD, upD, rightD, leftD]);
  5640.     switch (maxD) {
  5641.         case downD:
  5642.             this.setDir(2);
  5643.             break;
  5644.         case upD:
  5645.             this.setDir(8);
  5646.             break;
  5647.         case leftD:
  5648.             this.setDir(4);
  5649.             break;
  5650.         case rightD:
  5651.             this.setDir(6);
  5652.             break;
  5653.     }
  5654. };
  5655.  
  5656. TBSEntity.prototype.lookAway = function (cell) {
  5657.     this.lookAt(cell);
  5658.     switch (this.getDir()) {
  5659.         case 2:
  5660.             this.setDir(8);
  5661.             break;
  5662.         case 4:
  5663.             this.setDir(6);
  5664.             break;
  5665.         case 6:
  5666.             this.setDir(4);
  5667.             break;
  5668.         case 8:
  5669.             this.setDir(2);
  5670.             break;
  5671.     }
  5672. };
  5673.  
  5674. TBSEntity.prototype.getDirectionTo = function (cell) {
  5675.     var oldDir = this.getDir();
  5676.     this.lookAt(cell);
  5677.     var dir = this.getDir();
  5678.     this.setDir(oldDir);
  5679.     return dir;
  5680. };
  5681.  
  5682. TBSEntity.prototype.getDirectionAwayFrom = function (cell) {
  5683.     var oldDir = this.getDir();
  5684.     this.lookAway(cell);
  5685.     var dir = this.getDir();
  5686.     this.setDir(oldDir);
  5687.     return dir;
  5688. };
  5689.  
  5690. TBSEntity.prototype.startShake = function (power, duration) {
  5691.     this._sprite._shakeEffect = {
  5692.         power: power,
  5693.         duration: duration
  5694.     };
  5695. };
  5696.  
  5697. TBSEntity.prototype.lookClosestBattler = function (entities) {
  5698.     if (entities.length === 0) return;
  5699.     var entity = LeUtilities.closestByDistance(this, entities);
  5700.     var cell = BattleManagerTBS.getEntityCell(entity);
  5701.     this.lookAt(cell);
  5702. };
  5703.  
  5704. TBSEntity.prototype.processMovement = function (path) {
  5705.     if (path.length === 0) return;
  5706.     this._moving = true;
  5707.     this._movePath = path;
  5708.     this._movePath.reverse();
  5709.     this.setPose("move", "move");
  5710.     this._moveReducePoints = true;
  5711.     this._moveUpdateDir = true;
  5712. };
  5713.  
  5714. TBSEntity.prototype.forceMoveStraight = function (nbrCells, checkCollision) {
  5715.     this._movePath = [];
  5716.     var cx = this._cellX;
  5717.     var cy = this._cellY;
  5718.     this._collisionData = {
  5719.         distance: nbrCells,
  5720.         covered: 0,
  5721.         dir: this.getDir(),
  5722.         endCell: null
  5723.     };
  5724.     for (var i = 0; i < nbrCells; i++) {
  5725.         var cell = this.getForwardCell(cx, cy);
  5726.         if (!cell || cell.isObstacle()) { //  || cell.isImpassableTile()
  5727.             this._collisionData.endCell = cell;
  5728.             break;
  5729.         }
  5730.         this._collisionData.covered++;
  5731.         this._movePath.push(cell);
  5732.         switch (this.getDir()) {
  5733.             case 2:
  5734.                 cy++;
  5735.                 break;
  5736.             case 4:
  5737.                 cx--;
  5738.                 break;
  5739.             case 6:
  5740.                 cx++;
  5741.                 break;
  5742.             case 8:
  5743.                 cy--;
  5744.                 break;
  5745.         }
  5746.     }
  5747.     if (!checkCollision) this._collisionData = null;
  5748.     if (this._movePath.length === 0) return;
  5749.     this._moving = true;
  5750.     this._movingStraight = true;
  5751.     this._movePath.reverse();
  5752.     this._moveReducePoints = false;
  5753.     this._moveUpdateDir = true;
  5754. };
  5755.  
  5756. TBSEntity.prototype.forcePush = function (user, sourceCell, distance, obj, damage) {
  5757.     if (this.isImmuneToKnockback())
  5758.         return;
  5759.     distance += user.getKnockbackBonus() - this.getKnockbackReduction();
  5760.     var old = this.getDir();
  5761.     this.lookAway(sourceCell);
  5762.     this.forceMoveStraight(distance, damage);
  5763.     this.setDir(old);
  5764.     this._moveUpdateDir = false;
  5765.     if (this._collisionData) {
  5766.         this._collisionData.user = user;
  5767.         this._collisionData.obj = obj;
  5768.     }
  5769.  
  5770.     if (this._movePath.length === 0 && this._collisionData)
  5771.         BattleManagerTBS.processCollisionEffects(this);
  5772. };
  5773.  
  5774. TBSEntity.prototype.forcePull = function (user, sourceCell, distance, obj, damage) {
  5775.     var old = this.getDir();
  5776.     this.lookAt(sourceCell);
  5777.     this.forceMoveStraight(distance, damage);
  5778.     this.setDir(old);
  5779.     this._moveUpdateDir = false;
  5780.     if (this._collisionData) {
  5781.         this._collisionData.user = user;
  5782.         this._collisionData.obj = obj;
  5783.     }
  5784.  
  5785.     if (this._movePath.length === 0 && this._collisionData)
  5786.         BattleManagerTBS.processCollisionEffects(this);
  5787. };
  5788.  
  5789. TBSEntity.prototype.getForwardCell = function (cx, cy) {
  5790.     cx = cx || this._cellX;
  5791.     cy = cy || this._cellY;
  5792.     switch (this.getDir()) {
  5793.         case 2:
  5794.             return BattleManagerTBS.getCellAt(cx, cy + 1);
  5795.         case 4:
  5796.             return BattleManagerTBS.getCellAt(cx - 1, cy);
  5797.         case 6:
  5798.             return BattleManagerTBS.getCellAt(cx + 1, cy);
  5799.         case 8:
  5800.             return BattleManagerTBS.getCellAt(cx, cy - 1);
  5801.     }
  5802.     return null;
  5803. };
  5804.  
  5805. TBSEntity.prototype.updateMove = function () {
  5806.     if (!this._moving) return;
  5807.     if (!this._movingNextCell) {
  5808.         this._movingNextCell = this._movePath.pop();
  5809.         this._movingDistance = [0, 0];
  5810.     }
  5811.     var cell = this._movingNextCell;
  5812.     var oldDir = this.getDir();
  5813.     this.lookAt(cell);
  5814.     this.moveForward(cell);
  5815.     if (!this._moveUpdateDir)
  5816.         this.setDir(oldDir);
  5817.     if (this._cellX == cell.x && this._cellY == cell.y) {
  5818.         var stop = this.onCellCovered();
  5819.         if (this._movePath.length === 0 || stop)
  5820.             this.onMoveEnd(!stop);
  5821.     }
  5822. };
  5823.  
  5824. TBSEntity.prototype.onCellCovered = function () {
  5825.     // When returning true, the movement is stoped
  5826.     this._movingNextCell = null;
  5827.     if (this._moveReducePoints) {
  5828.         this._movePoints--;
  5829.         if (this.getMovePoints() === 0)
  5830.             return true;
  5831.     }
  5832.     return false;
  5833. };
  5834.  
  5835. TBSEntity.prototype.moveForward = function (cell) {
  5836.     var speed = this._speed;
  5837.     var dir = this._sprite._dir;
  5838.     switch (dir) {
  5839.         case 2:
  5840.             this.shiftPosition(0, speed);
  5841.             this._movingDistance[1] += speed;
  5842.             break;
  5843.         case 4:
  5844.             this.shiftPosition(-speed, 0);
  5845.             this._movingDistance[0] += speed;
  5846.             break;
  5847.         case 6:
  5848.             this.shiftPosition(speed, 0);
  5849.             this._movingDistance[0] += speed;
  5850.             break;
  5851.         case 8:
  5852.             this.shiftPosition(0, -speed);
  5853.             this._movingDistance[1] += speed;
  5854.             break;
  5855.     }
  5856.     if (this._movingDistance[0] >= $gameMap.tileWidth() || this._movingDistance[1] >= $gameMap.tileHeight())
  5857.         this.setCell(cell);
  5858. };
  5859.  
  5860. TBSEntity.prototype.onMoveEnd = function () {
  5861.     this._moving = false;
  5862.     if (this._dead)
  5863.         this.setPose("dead", "dead");
  5864.     else
  5865.         this.setPose("idle");
  5866.     this._moveReducePoints = false;
  5867.  
  5868.     if (this._collisionData)
  5869.         BattleManagerTBS.processCollisionEffects(this);
  5870.     this._collisionData = null;
  5871.     this._movingStraight = false;
  5872. };
  5873.  
  5874. TBSEntity.prototype.teleport = function (cell) {
  5875.     this.setCell(cell);
  5876. };
  5877.  
  5878. TBSEntity.prototype.followTrajectory = function (trajectory, cell) {
  5879.     if (cell.x < this.getCell().x) {
  5880.         trajectory = trajectory.reverse();
  5881.     }
  5882.     this._sprite.setTrajectory(trajectory, cell);
  5883. };
  5884.  
  5885. TBSEntity.prototype.setMovePoints = function () {
  5886.     var obj = this._battler.isActor() ? this._battler.actor() : this._battler.enemy();
  5887.     this._movePoints = obj.leTbs_movePoints;
  5888.     this._battler.states().forEach(function (state) {
  5889.         if (state)
  5890.             this._movePoints += state.leTbs_movePointsPlus;
  5891.     }.bind(this));
  5892.     if (this._battler.isActor()) {
  5893.         this._battler.equips().forEach(function (equip) {
  5894.             if (equip)
  5895.                 this._movePoints += equip.leTbs_movePointsPlus;
  5896.         }.bind(this));
  5897.     }
  5898. };
  5899.  
  5900. TBSEntity.prototype.changeMovePoints = function (plus) {
  5901.     this._movePoints += plus;
  5902. };
  5903.  
  5904. TBSEntity.prototype.getMovePoints = function () {
  5905.     return this._movePoints.clamp(0, this._movePoints);
  5906. };
  5907.  
  5908. TBSEntity.prototype.canMoveCommand = function () {
  5909.     if (this.getMovePoints() === 0) return false;
  5910.     if (this.oneTimeMove() && this._movePerformed) return false;
  5911.     return true;
  5912. };
  5913.  
  5914. TBSEntity.prototype.canAttackCommand = function () {
  5915.     if (!this._battler.canAttack()) return false;
  5916.     if (!this.canObjCommand()) return false;
  5917.     return true;
  5918. };
  5919.  
  5920. TBSEntity.prototype.canSkillCommand = function () {
  5921.     if (!this.canObjCommand()) return false;
  5922.     return true;
  5923. };
  5924.  
  5925. TBSEntity.prototype.canItemCommand = function () {
  5926.     if (!this.canObjCommand()) return false;
  5927.     return true;
  5928. };
  5929.  
  5930. TBSEntity.prototype.canObjCommand = function () {
  5931.     if (this.oneTimeOffense() && this._actionPerformed) return false;
  5932.     return true;
  5933. };
  5934.  
  5935. TBSEntity.prototype.getAttackAnimation = function () {
  5936.     if (this._battler.isActor()) {
  5937.         return this._battler.attackAnimationId1();
  5938.     } else {
  5939.         return this._battler.enemy().leTbs_atkAnim;
  5940.     }
  5941. };
  5942.  
  5943. TBSEntity.prototype.getAttackScopeData = function () {
  5944.     if (this._battler.isEnemy())
  5945.         return this._battler.enemy().leTbs_atkScopeData;
  5946.     else {
  5947.         for (var i = 0; i < this._battler.weapons().length; i++) {
  5948.             var weapon = this._battler.weapons()[i];
  5949.             if (weapon && weapon.leTbs_scopeData)
  5950.                 return weapon.leTbs_scopeData;
  5951.         }
  5952.         return this._battler.actor().leTbs_atkScopeData;
  5953.     }
  5954. };
  5955.  
  5956. TBSEntity.prototype.getObjectScopeData = function (obj) {
  5957.     var defaultScope = DataManager.isSkill(obj) ? Lecode.S_TBS.defaultSkillScope : Lecode.S_TBS.defaultItemScope;
  5958.     return obj.leTbs_scopeData || defaultScope;
  5959. };
  5960.  
  5961. TBSEntity.prototype.getAttackAoEData = function () {
  5962.     if (this._battler.isEnemy())
  5963.         return this._battler.enemy().leTbs_atkAoeData;
  5964.     else {
  5965.         for (var i = 0; i < this._battler.weapons().length; i++) {
  5966.             var weapon = this._battler.weapons()[i];
  5967.             if (weapon && weapon.leTbs_aoeData)
  5968.                 return weapon.leTbs_aoeData;
  5969.         }
  5970.         return this._battler.actor().leTbs_atkAoeData;
  5971.     }
  5972. };
  5973.  
  5974. TBSEntity.prototype.getObjectAoEData = function (obj) {
  5975.     var defaultAoe = DataManager.isSkill(obj) ? Lecode.S_TBS.defaultSkillAoE : Lecode.S_TBS.defaultItemAoE;
  5976.     return obj.leTbs_aoeData || defaultAoe;
  5977. };
  5978.  
  5979. TBSEntity.prototype.getMoveScopeData = function () {
  5980.     return this.getMoveScopeRawData().replace("_mp_", String(this.getMovePoints()));
  5981. };
  5982.  
  5983. TBSEntity.prototype.getMoveScopeRawData = function () {
  5984.     for (var i = 0; i < this._battler.states().length; i++) {
  5985.         var state = this._battler.states()[i];
  5986.         if (state && state.leTbs_moveScopeData)
  5987.             return state.leTbs_moveScopeData;
  5988.     }
  5989.     if (this._battler.isEnemy())
  5990.         return this._battler.enemy().leTbs_moveScopeData;
  5991.     else {
  5992.         for (var i = 0; i < this._battler.equips().length; i++) {
  5993.             var equip = this._battler.equips()[i];
  5994.             if (equip && equip.leTbs_moveScopeData)
  5995.                 return equip.leTbs_moveScopeData;
  5996.         }
  5997.         return this._battler.actor().leTbs_moveScopeData;
  5998.     }
  5999. };
  6000.  
  6001. TBSEntity.prototype.getMoveScopeParamData = function () {
  6002.     for (var i = 0; i < this._battler.states().length; i++) {
  6003.         var state = this._battler.states()[i];
  6004.         if (state && state.leTbs_moveScopeParam)
  6005.             return state.leTbs_moveScopeParam;
  6006.     }
  6007.     if (this._battler.isEnemy())
  6008.         return this._battler.enemy().leTbs_moveScopeParam;
  6009.     else {
  6010.         for (var i = 0; i < this._battler.equips().length; i++) {
  6011.             var equip = this._battler.equips()[i];
  6012.             if (equip && equip.leTbs_moveScopeParam)
  6013.                 return equip.leTbs_moveScopeParam;
  6014.         }
  6015.         return this._battler.actor().leTbs_moveScopeParam;
  6016.     }
  6017. };
  6018.  
  6019. TBSEntity.prototype.getWeaponSequenceData = function () {
  6020.     if (this._battler.isEnemy())
  6021.         return this._battler.enemy().leTbs_atkSequenceData;
  6022.     else {
  6023.         for (var i = 0; i < this._battler.weapons().length; i++) {
  6024.             var weapon = this._battler.weapons()[i];
  6025.             if (weapon && weapon.leTbs_sequenceData)
  6026.                 return weapon.leTbs_sequenceData;
  6027.         }
  6028.         return this._battler.actor().leTbs_atkSequenceData;
  6029.     }
  6030. };
  6031.  
  6032. TBSEntity.prototype.getObjectSequenceData = function (obj) {
  6033.     var defaultSeqId = DataManager.isSkill(obj) ? Lecode.S_TBS.defaultSkillSequence : Lecode.S_TBS.defaultItemSequence;
  6034.     return obj.leTbs_sequenceData || defaultSeqId;
  6035. };
  6036.  
  6037. TBSEntity.prototype.passAfterObjUse = function (obj) {
  6038.     if (DataManager.isSkill(obj)) {
  6039.         if (this._battler.isEnemy())
  6040.             return !!this._battler.leTbs_passOnAtkUse;
  6041.         if (obj.id == this._battler.attackSkillId()) {
  6042.             if (this._battler.weapons().length > 0)
  6043.                 return !!this._battler.weapons()[0].leTbs_passOnUse;
  6044.         }
  6045.     }
  6046.     return !!obj.leTbs_passOnUse;
  6047. };
  6048.  
  6049. TBSEntity.prototype.oneTimeMove = function () {
  6050.     return this.rpgObject().leTbs_oneTimeMove;
  6051. };
  6052.  
  6053. TBSEntity.prototype.oneTimeOffense = function () {
  6054.     return this.rpgObject().leTbs_oneTimeOffense;
  6055. };
  6056.  
  6057. TBSEntity.prototype.getCollapseAnimation = function () {
  6058.     return this.rpgObject().leTbs_collapseAnim;
  6059. };
  6060.  
  6061. TBSEntity.prototype.getAiPattern = function () {
  6062.     return this.rpgObject().leTbs_aiPattern;
  6063. };
  6064.  
  6065. TBSEntity.prototype.isPassableOnDeath = function () {
  6066.     var test = this.battler().states().some(function (state) {
  6067.         return state && state.leTbs_passableOnDeath;
  6068.     });
  6069.     if (test) return true;
  6070.     return this.rpgObject().leTbs_passableOnDeath;
  6071. };
  6072.  
  6073. TBSEntity.prototype.isPassable = function () {
  6074.     var test = this._battler.isDead() && this.isPassableOnDeath();
  6075.     if (test) return true;
  6076.     test = this.battler().states().some(function (state) {
  6077.         return state && state.leTbs_passable;
  6078.     });
  6079.     if (test) return true;
  6080.     return this.rpgObject().leTbs_passable;
  6081. };
  6082.  
  6083. TBSEntity.prototype.entitiesCanLayOnMe = function () {
  6084.     var test = this._battler.isDead() && this.isPassableOnDeath();
  6085.     if (test) return true;
  6086.     test = this.battler().states().some(function (state) {
  6087.         return state && state.leTbs_canLayOnMe;
  6088.     });
  6089.     if (test) return true;
  6090.     return this.rpgObject().leTbs_canLayOnMe;
  6091. };
  6092.  
  6093. TBSEntity.prototype.getCollisionDamageBonus = function (damage) {
  6094.     var bonus = 0;
  6095.     this.battler().states().forEach(function (state) {
  6096.         if (state) {
  6097.             bonus += state.leTbs_collisionDmgBonus;
  6098.             bonus += state.leTbs_collisionDmgBonusRate * 0.01 * damage;
  6099.         }
  6100.     });
  6101.     if (this.battler().isActor()) {
  6102.         this.battler().equips().forEach(function (equip) {
  6103.             if (equip) {
  6104.                 bonus += equip.leTbs_collisionDmgBonus;
  6105.                 bonus += equip.leTbs_collisionDmgBonusRate * 0.01 * damage;
  6106.             }
  6107.         });
  6108.     }
  6109.     return bonus;
  6110. };
  6111.  
  6112. TBSEntity.prototype.getCollisionDamageReduction = function (damage) {
  6113.     var reduction = 0;
  6114.     this.battler().states().forEach(function (state) {
  6115.         if (state) {
  6116.             reduction += state.leTbs_collisionDmgReduction;
  6117.             reduction += state.leTbs_collisionDmgReductionRate * 0.01 * damage;
  6118.         }
  6119.     });
  6120.     if (this.battler().isActor()) {
  6121.         this.battler().equips().forEach(function (equip) {
  6122.             if (equip) {
  6123.                 reduction += equip.leTbs_collisionDmgReduction;
  6124.                 reduction += equip.leTbs_collisionDmgReductionRate * 0.01 * damage;
  6125.             }
  6126.         });
  6127.     }
  6128.     return reduction;
  6129. };
  6130.  
  6131. TBSEntity.prototype.getDirectionalDmgBonus = function (type) {
  6132.     var value = this.rpgObject().leTbs_directionalDmgBonus[type];
  6133.     this.battler().states().forEach(function (state) {
  6134.         if (state) {
  6135.             value += state.leTbs_directionalDmgBonus[type];
  6136.         }
  6137.     });
  6138.     if (this.battler().isActor()) {
  6139.         this.battler().equips().forEach(function (equip) {
  6140.             if (equip) {
  6141.                 value += equip.leTbs_directionalDmgBonus[type];
  6142.             }
  6143.         });
  6144.     }
  6145.     return value;
  6146. };
  6147.  
  6148. TBSEntity.prototype.getDirectionalDmgReduction = function (type) {
  6149.     var value = this.rpgObject().leTbs_directionalDmgReduction[type];
  6150.     this.battler().states().forEach(function (state) {
  6151.         if (state) {
  6152.             value += state.leTbs_directionalDmgReduction[type];
  6153.         }
  6154.     });
  6155.     if (this.battler().isActor()) {
  6156.         this.battler().equips().forEach(function (equip) {
  6157.             if (equip) {
  6158.                 value += equip.leTbs_directionalDmgReduction[type];
  6159.             }
  6160.         });
  6161.     }
  6162.     return value;
  6163. };
  6164.  
  6165. TBSEntity.prototype.isImmuneToKnockback = function () {
  6166.     return this.rpgObject().leTbs_immuneKnockback;
  6167. };
  6168.  
  6169. TBSEntity.prototype.getKnockbackBonus = function () {
  6170.     var bonus = 0;
  6171.     bonus += this.rpgObject().leTbs_knockbackBonus;
  6172.     this.battler().states().forEach(function (state) {
  6173.         if (state) {
  6174.             bonus += state.leTbs_knockbackBonus;
  6175.         }
  6176.     });
  6177.     if (this.battler().isActor()) {
  6178.         this.battler().equips().forEach(function (equip) {
  6179.             if (equip) {
  6180.                 bonus += equip.leTbs_knockbackBonus;
  6181.             }
  6182.         });
  6183.     }
  6184.     return bonus;
  6185. };
  6186.  
  6187. TBSEntity.prototype.getKnockbackReduction = function () {
  6188.     var reduction = 0;
  6189.     reduction += this.rpgObject().leTbs_knockbackReduction;
  6190.     this.battler().states().forEach(function (state) {
  6191.         if (state) {
  6192.             reduction += state.leTbs_knockbackReduction;
  6193.         }
  6194.     });
  6195.     if (this.battler().isActor()) {
  6196.         this.battler().equips().forEach(function (equip) {
  6197.             if (equip) {
  6198.                 reduction += equip.leTbs_knockbackReduction;
  6199.             }
  6200.         });
  6201.     }
  6202.     return reduction;
  6203. };
  6204.  
  6205.  
  6206. TBSEntity.prototype.addPopup = function () {
  6207.     this._sprite.addPopup();
  6208. };
  6209.  
  6210. TBSEntity.prototype.battler = function () {
  6211.     return this._battler;
  6212. };
  6213.  
  6214. TBSEntity.prototype.sprite = function () {
  6215.     return this._sprite;
  6216. };
  6217.  
  6218. TBSEntity.prototype.width = function () {
  6219.     return this._sprite.width;
  6220. };
  6221.  
  6222. TBSEntity.prototype.height = function () {
  6223.     return this._sprite.height;
  6224. };
  6225.  
  6226. TBSEntity.prototype.getCell = function () {
  6227.     return this._cell;
  6228. };
  6229.  
  6230. TBSEntity.prototype.isMoving = function () {
  6231.     return this._moving;
  6232. };
  6233.  
  6234. TBSEntity.prototype.rpgObject = function () {
  6235.     return this.battler().isActor() ? this.battler().actor() : this.battler().enemy();
  6236. };
  6237.  
  6238. /*-------------------------------------------------------------------------
  6239. * TBSEntity_Sprite
  6240. -------------------------------------------------------------------------*/
  6241. function TBSEntity_Sprite() {
  6242.     this.initialize.apply(this, arguments);
  6243. }
  6244.  
  6245. TBSEntity_Sprite.prototype = Object.create(Sprite_Base.prototype);
  6246. TBSEntity_Sprite.prototype.constructor = TBSEntity_Sprite;
  6247.  
  6248. TBSEntity_Sprite.prototype.initialize = function (battler, entity) {
  6249.     Sprite_Base.prototype.initialize.call(this);
  6250.     this._battler = battler;
  6251.     this._entity = entity;
  6252.     this._bitmaps = {};
  6253.     this._frameCount = 0;
  6254.     this._maxFrame = {};
  6255.     this._fixedFrame = null;
  6256.     this._updateCount = 0;
  6257.     this._poses = [];
  6258.     this._frameLoaded = 0;
  6259.     this._popups = [];
  6260.     this._shakeEffect = {
  6261.         power: 0,
  6262.         duration: 0
  6263.     };
  6264.     this._trajectoryIndex = 0;
  6265.     this._trajectory = [];
  6266.     this._trajectoryEndCell = null;
  6267.     this.initializeFrameDelay();
  6268.     this.createBitmaps(battler);
  6269. };
  6270.  
  6271. TBSEntity_Sprite.prototype.initializeFrameDelay = function () {
  6272.     this.setFrameDelay(Lecode.S_TBS.battlersFrameDelay);
  6273. };
  6274.  
  6275. TBSEntity_Sprite.prototype.setFrameDelay = function (delay) {
  6276.     this._frameDelay = delay;
  6277. };
  6278.  
  6279. TBSEntity_Sprite.prototype.changeFrameDelay = function (delay) {
  6280.     this._frameDelay += delay;
  6281. };
  6282.  
  6283. TBSEntity_Sprite.prototype.onPosePlayed = function () {
  6284.     this._frameCount = 0;
  6285.     this._entity.onPosePlayed(this.getPose());
  6286. };
  6287.  
  6288. TBSEntity_Sprite.prototype.resetFrameCount = function () {
  6289.     this._frameCount = 0;
  6290. };
  6291.  
  6292. TBSEntity_Sprite.prototype.setFixedFrame = function (frame) {
  6293.     this._fixedFrame = frame;
  6294. };
  6295.  
  6296. TBSEntity_Sprite.prototype.setTrajectory = function (trajectory, cell) {
  6297.     this._trajectoryIndex = 0;
  6298.     this._trajectory = trajectory;
  6299.     this._trajectoryEndCell = cell;
  6300. };
  6301.  
  6302. TBSEntity_Sprite.prototype.hasTrajectory = function () {
  6303.     return !!this._trajectory[this._trajectoryIndex];
  6304. };
  6305.  
  6306. TBSEntity_Sprite.prototype.update = function () {
  6307.     Sprite_Base.prototype.update.call(this);
  6308.     if (!this.isReady()) return;
  6309.     this.updateDirection();
  6310.     this.updatePose();
  6311.     this.updateFrameDelay();
  6312.     this.updatePosition();
  6313.     this.updatePopups();
  6314. };
  6315.  
  6316. TBSEntity_Sprite.prototype.updateFrameDelay = function () {
  6317.     this._updateCount++;
  6318.     if (this._updateCount === this._frameDelay) {
  6319.         this._updateCount = 0;
  6320.         this.onNextFrame();
  6321.     }
  6322. };
  6323.  
  6324. TBSEntity_Sprite.prototype.onNextFrame = function () {
  6325.     this.updateBitmap();
  6326.     this.updateFrameCount();
  6327. };
  6328.  
  6329. TBSEntity_Sprite.prototype.updateDirection = function () {
  6330.     this._dir = this._entity._dir;
  6331. };
  6332.  
  6333. TBSEntity_Sprite.prototype.updatePose = function () {
  6334.     this._pose = this._entity._pose;
  6335. };
  6336.  
  6337. TBSEntity_Sprite.prototype.updatePosition = function () {
  6338.     this.updateTrajectoryMove();
  6339.     this.x = this._entity._posX;
  6340.     this.y = this._entity._posY;
  6341.     if (this.bitmap) {
  6342.         var w = this.bitmap.width / (this._maxFrame[this.getPose()] + 1);
  6343.         var h = this.bitmap.height / 4;
  6344.         this.x += $gameMap.tileWidth() / 2 - w / 2;
  6345.         this.y += $gameMap.tileHeight() - h;
  6346.     }
  6347.     this.updateShakeEffect();
  6348. };
  6349.  
  6350. TBSEntity_Sprite.prototype.updateTrajectoryMove = function () {
  6351.     if (this.hasTrajectory()) {
  6352.         var pos = this._trajectory[this._trajectoryIndex];
  6353.         this._trajectoryIndex += this._entity._speed;
  6354.         if (!this.hasTrajectory()) {
  6355.             this._entity.setCell(this._trajectoryEndCell);
  6356.         } else {
  6357.             this._entity.setPosition(pos[0], pos[1]);
  6358.         }
  6359.     }
  6360. };
  6361.  
  6362. TBSEntity_Sprite.prototype.updateShakeEffect = function () {
  6363.     var min = this._shakeEffect.power * 0.6;
  6364.     var max = this._shakeEffect.power * 1.6;
  6365.     var d = this._shakeEffect.duration--;
  6366.     if (d > 0) {
  6367.         this.x += LeUtilities.randValueBetween(min, max) * LeUtilities.randValueBetween(-1, 1);
  6368.         this.y += LeUtilities.randValueBetween(min, max) * LeUtilities.randValueBetween(-1, 1);
  6369.     }
  6370. };
  6371.  
  6372. TBSEntity_Sprite.prototype.updateBitmap = function () {
  6373.     var pose = this.getPose();
  6374.     this.bitmap = this._bitmaps[pose];
  6375.     var w = this.bitmap.width / (this._maxFrame[pose] + 1);
  6376.     var h = this.bitmap.height / 4;
  6377.     var x;
  6378.     var y;
  6379.     var frame = this._fixedFrame != null ? this._fixedFrame : this._frameCount;
  6380.     x = w * frame;
  6381.     y = (h * this._dir) / 2 - h;
  6382.     this.setFrame(x, y, w, h);
  6383. };
  6384.  
  6385. TBSEntity_Sprite.prototype.updateFrameCount = function () {
  6386.     var pose = this.getPose();
  6387.     if (this._frameCount > this._maxFrame[pose]) {
  6388.         this.onPosePlayed();
  6389.     } else {
  6390.         this._frameCount++;
  6391.     }
  6392. };
  6393.  
  6394. TBSEntity_Sprite.prototype.updatePopups = function () {
  6395.     if (this._popups.length === 0) return;
  6396.     this._popups.forEach(function (popup) {
  6397.         popup.update();
  6398.     }.bind(this));
  6399.     if (!this._popups[0].isPlaying()) {
  6400.         this.removePopup(this._popups[0]);
  6401.     }
  6402. };
  6403.  
  6404. TBSEntity_Sprite.prototype.removePopup = function (popup) {
  6405.     BattleManagerTBS.getLayer("movableInfo").removeChild(popup);
  6406.     this._popups.shift();
  6407. };
  6408.  
  6409. TBSEntity_Sprite.prototype.createBitmaps = function (battler) {
  6410.     var config = this.getConfig(battler);
  6411.     config.forEach(function (info) {
  6412.         var pose = info[0];
  6413.         var filename = this.filenameID(battler) + info[1];
  6414.         var hue = info[3] || 0;
  6415.         this._poses.push(pose);
  6416.         var fullBitmap = ImageManager.loadLeTBSBattler(filename, hue);
  6417.         fullBitmap.addLoadListener(this.createPoseBitmaps.bind(this, fullBitmap, pose, info));
  6418.     }.bind(this));
  6419. };
  6420.  
  6421. TBSEntity_Sprite.prototype.createPoseBitmaps = function (fbitmap, pose, info) {
  6422.     var frames = info[2];
  6423.     this._maxFrame[pose] = frames - 1;
  6424.     this._bitmaps[pose] = fbitmap;
  6425.     this._frameLoaded++;
  6426. };
  6427.  
  6428. TBSEntity_Sprite.prototype.addPopup = function () {
  6429.     var sprite = new Sprite_Damage();
  6430.     sprite.x = this.x + this.width / 2;
  6431.     sprite.y = this.y;
  6432.     sprite.setup(this._battler);
  6433.     this._popups.push(sprite);
  6434.     BattleManagerTBS.getLayer("movableInfo").addChild(sprite);
  6435. };
  6436.  
  6437. TBSEntity_Sprite.prototype.isReady = function () {
  6438.     return this._frameLoaded === this._poses.length;
  6439. };
  6440.  
  6441. TBSEntity_Sprite.prototype.getPose = function () {
  6442.     return this.isValidPose(this._pose) ? this._pose : "idle";
  6443. };
  6444.  
  6445. TBSEntity_Sprite.prototype.getConfig = function (battler) {
  6446.     var id = battler.isActor() ? battler.name() : battler.originalName();
  6447.     var config = Lecode.S_TBS.Config.Battler_Sprites[id];
  6448.     if (!config) config = Lecode.S_TBS.Config.Battler_Sprites["Default"];
  6449.     return config;
  6450. };
  6451.  
  6452. TBSEntity_Sprite.prototype.isValidPose = function (pose) {
  6453.     return this._poses.indexOf(pose) >= 0;
  6454. };
  6455.  
  6456. TBSEntity_Sprite.prototype.filenameID = function (battler) {
  6457.     if (battler.isActor()) return battler.name();
  6458.     return battler.originalName();
  6459. };
  6460.  
  6461. /*-------------------------------------------------------------------------
  6462. * TBSDirectionSelector
  6463. -------------------------------------------------------------------------*/
  6464. function TBSDirectionSelector() {
  6465.     this.initialize.apply(this, arguments);
  6466. }
  6467.  
  6468. TBSDirectionSelector.prototype.initialize = function (layer) {
  6469.     this._cell = null;
  6470.     this._battlerEntity = null;
  6471.     this.createSprite(layer);
  6472. };
  6473.  
  6474. TBSDirectionSelector.prototype.createSprite = function (layer) {
  6475.     var bitmap = ImageManager.loadLeTBS("DirectionSelector", 0);
  6476.     this._sprite = new Sprite(bitmap);
  6477.     this._sprite.anchor.x = 0.5;
  6478.     this._sprite.anchor.y = 0.5;
  6479.     this._sprite.opacity = 0;
  6480.     layer.addChild(this._sprite);
  6481. };
  6482.  
  6483. TBSDirectionSelector.prototype.set = function (cell, battler) {
  6484.     this._cell = cell;
  6485.     this._battlerEntity = battler;
  6486.     this._sprite.x = cell.x * $gameMap.tileWidth() + $gameMap.tileWidth() / 2;
  6487.     this._sprite.y = cell.y * $gameMap.tileHeight() + $gameMap.tileHeight() / 2;
  6488.     this._sprite.opacity = 255;
  6489.     this.setDir(battler.getDir());
  6490. };
  6491.  
  6492. TBSDirectionSelector.prototype.setDir = function (dir) {
  6493.     this._battlerEntity.setDir(dir);
  6494.     var r = 0;
  6495.     switch (dir) {
  6496.         case 2:
  6497.             r = 0;
  6498.             break;
  6499.         case 4:
  6500.             r = 90;
  6501.             break;
  6502.         case 6:
  6503.             r = 90 * 3;
  6504.             break;
  6505.         case 8:
  6506.             r = 90 * 2;
  6507.             break;
  6508.     }
  6509.     this._sprite.rotation = r * Math.PI / 180;
  6510.     SoundManager.playCursor();
  6511. };
  6512.  
  6513. TBSDirectionSelector.prototype.hide = function () {
  6514.     this._sprite.opacity = 0;
  6515. };
  6516.  
  6517.  
  6518. /*-------------------------------------------------------------------------
  6519. * Window_TBSConfirm
  6520. -------------------------------------------------------------------------*/
  6521. function Window_TBSConfirm() {
  6522.     this.initialize.apply(this, arguments);
  6523. }
  6524.  
  6525. /*-------------------------------------------------------------------------
  6526. * Window_TBSPlacementInfo
  6527. -------------------------------------------------------------------------*/
  6528.  
  6529. function Window_TBSPlacementInfo() {
  6530.     this.initialize.apply(this, arguments);
  6531. }
  6532. /*-------------------------------------------------------------------------
  6533. * Window_TBSStatus
  6534. -------------------------------------------------------------------------*/
  6535. function Window_TBSStatus() {
  6536.     this.initialize.apply(this, arguments);
  6537. }
  6538.  
  6539. /*-------------------------------------------------------------------------
  6540. * Window_TBSCommand
  6541. -------------------------------------------------------------------------*/
  6542. function Window_TBSCommand() {
  6543.     this.initialize.apply(this, arguments);
  6544. }
  6545.  
  6546. /*-------------------------------------------------------------------------
  6547. * Window_TBSEndCommand
  6548. -------------------------------------------------------------------------*/
  6549. function Window_TBSEndCommand() {
  6550.     this.initialize.apply(this, arguments);
  6551. }
  6552.  
  6553.  
  6554. /*=========================================================================
  6555. *   RTPs MODIFS
  6556. =========================================================================*/
  6557.  
  6558.  
  6559. /*-------------------------------------------------------------------------
  6560. * Game_Battler
  6561. -------------------------------------------------------------------------*/
  6562. Lecode.S_TBS.oldGameBattler_initMembers = Game_Battler.prototype.initMembers;
  6563. Game_Battler.prototype.initMembers = function () {
  6564.     Lecode.S_TBS.oldGameBattler_initMembers.call(this);
  6565.     this._leTbsDirectionalDmg = 0;
  6566. };
  6567.  
  6568. Game_Battler.prototype.leTBS_setDirectionalDmgEffects = function (amount) {
  6569.     this._leTbsDirectionalDmg = amount;
  6570. };
  6571.  
  6572. Lecode.S_TBS.oldGameBattler_addNewState = Game_Battler.prototype.addNewState;
  6573. Game_Battler.prototype.addNewState = function (stateId) {
  6574.     if (LeUtilities.isScene("Scene_Battle") && Lecode.S_TBS.commandOn) {
  6575.         var state = $dataStates[stateId];
  6576.         var entity = BattleManagerTBS.getEntityByBattler(this);
  6577.         entity.changeMovePoints(state.leTbs_movePointsPlus);
  6578.     }
  6579.     Lecode.S_TBS.oldGameBattler_addNewState.call(this, stateId);
  6580. };
  6581.  
  6582.  
  6583. /*-------------------------------------------------------------------------
  6584. * Game_Action
  6585. -------------------------------------------------------------------------*/
  6586. Lecode.S_TBS.oldGameAction_executeDamage = Game_Action.prototype.executeDamage;
  6587. Game_Action.prototype.executeDamage = function (target, value) {
  6588.     value += value * target._leTbsDirectionalDmg;
  6589.     Lecode.S_TBS.oldGameAction_executeDamage.call(this, target, Math.floor(value));
  6590. };
  6591.  
  6592.  
  6593. /*-------------------------------------------------------------------------
  6594. * Game_Interpreter
  6595. -------------------------------------------------------------------------*/
  6596. Lecode.S_TBS.old_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  6597. Game_Interpreter.prototype.pluginCommand = function (command, args) {
  6598.     Lecode.S_TBS.old_pluginCommand.call(this, command, args);
  6599.     if (command === 'LeTBS') {
  6600.         switch (args[0]) {
  6601.             case 'ON':
  6602.                 Lecode.S_TBS.commandOn = true;
  6603.                 break;
  6604.             case 'OFF':
  6605.                 Lecode.S_TBS.commandOn = false;
  6606.                 break;
  6607.         }
  6608.     }
  6609. };
  6610.  
  6611. /*-------------------------------------------------------------------------
  6612. * DataManager
  6613. -------------------------------------------------------------------------*/
  6614. Lecode.S_TBS.oldDMisDatabaseLoaded_method = DataManager.isDatabaseLoaded;
  6615. DataManager.isDatabaseLoaded = function () {
  6616.     if (!Lecode.S_TBS.oldDMisDatabaseLoaded_method.call(this)) return false;
  6617.     this.processLeTBSTags();
  6618.     return true;
  6619. };
  6620.  
  6621. DataManager.processLeTBSTags = function () {
  6622.     this.processLeTBSTagsForBattlers();
  6623.     this.processLeTBSTagsForEquipmentsAndStates();
  6624.     this.processLeTBSTagsForObjects();
  6625. };
  6626.  
  6627. DataManager.processLeTBSTagsForBattlers = function () {
  6628.     var groups = [$dataActors, $dataEnemies, $dataClasses];
  6629.     for (var i = 0; i < groups.length; i++) {
  6630.         var group = groups[i];
  6631.         for (var j = 1; j < group.length; j++) {
  6632.             var obj = group[j];
  6633.             var notedata = obj.note.split(/[\r\n]+/);
  6634.             var letbs = false;
  6635.  
  6636.             obj.leTbs_movePoints = Lecode.S_TBS.defaultMovePoints;
  6637.             obj.leTbs_moveScopeData = Lecode.S_TBS.defaultMoveScope;
  6638.             obj.leTbs_moveScopeParam = "";
  6639.             obj.leTbs_atkAnim = Lecode.S_TBS.defaultAttackAnimation;
  6640.             obj.leTbs_atkScopeData = Lecode.S_TBS.defaultAttackScope;
  6641.             obj.leTbs_atkAoeData = Lecode.S_TBS.defaultAttackAoE;
  6642.             obj.leTbs_atkSequenceData = Lecode.S_TBS.defaultAttackSequence;
  6643.             obj.leTbs_oneTimeMove = Lecode.S_TBS.oneTimeMove;
  6644.             obj.leTbs_oneTimeOffense = Lecode.S_TBS.oneTimeOffense;
  6645.             obj.leTbs_collapseAnim = Lecode.S_TBS.collapseAnimation;
  6646.             obj.leTbs_passOnAtkUse = false;
  6647.             obj.leTbs_aiPattern = Lecode.S_TBS.defaultAiPattern;
  6648.             obj.leTbs_passable = false;
  6649.             obj.leTbs_passableOnDeath = false;
  6650.             obj.leTbs_canLayOnMe = false;
  6651.             obj.leTbs_collisionDmgBonus = 0;
  6652.             obj.leTbs_collisionDmgBonusRate = 0;
  6653.             obj.leTbs_collisionDmgReduction = 0;
  6654.             obj.leTbs_collisionDmgReductionRate = 0;
  6655.             obj.leTbs_immuneKnockback = false;
  6656.             obj.leTbs_knockbackBonus = 0;
  6657.             obj.leTbs_knockbackReduction = 0;
  6658.             obj.leTbs_directionalDmgBonus = {
  6659.                 face: 0,
  6660.                 back: 0,
  6661.                 side: 0
  6662.             };
  6663.             obj.leTbs_directionalDmgReduction = {
  6664.                 face: 0,
  6665.                 back: 0,
  6666.                 side: 0
  6667.             };
  6668.  
  6669.             for (var k = 0; k < notedata.length; k++) {
  6670.                 var line = notedata[k];
  6671.                 if (line.match(/<letbs>/i))
  6672.                     letbs = true;
  6673.                 else if (line.match(/<\/letbs>/i))
  6674.                     letbs = false;
  6675.  
  6676.                 if (letbs) {
  6677.                     if (line.match(/move_points\s?:\s?(.+)/i))
  6678.                         obj.leTbs_movePoints = Number(RegExp.$1);
  6679.                     else if (line.match(/move_scope\s?:\s?(.+)/i))
  6680.                         obj.leTbs_moveScopeData = String(RegExp.$1);
  6681.                     else if (line.match(/move_scope_options\s?:\s?((.|\n)*)/i))
  6682.                         obj.leTbs_moveScopeParam = String(RegExp.$1);
  6683.                     else if (line.match(/atk_anim\s?:\s?(.+)/i))
  6684.                         obj.leTbs_atkAnim = Number(RegExp.$1);
  6685.                     else if (line.match(/atk_scope\s?:\s?(.+)/i))
  6686.                         obj.leTbs_atkScopeData = String(RegExp.$1);
  6687.                     else if (line.match(/atk_aoe\s?:\s?(.+)/i))
  6688.                         obj.leTbs_atkAoeData = String(RegExp.$1);
  6689.                     else if (line.match(/atk_sequence\s?:\s?(.+)/i))
  6690.                         obj.leTbs_atkSequence = String(RegExp.$1);
  6691.                     else if (line.match(/one_time_move/i))
  6692.                         obj.leTbs_oneTimeMove = true;
  6693.                     else if (line.match(/one_time_offense/i))
  6694.                         obj.leTbs_oneTimeOffense = true;
  6695.                     else if (line.match(/collapse_anim\s?:\s?(.+)/i))
  6696.                         obj.leTbs_collapseAnim = Number(RegExp.$1);
  6697.                     else if (line.match(/pass_on_atk_use/i))
  6698.                         obj.leTbs_passOnAtkUse = true;
  6699.                     else if (line.match(/ai_pattern\s?:\s?(.+)/i))
  6700.                         obj.leTbs_aiPattern = String(RegExp.$1);
  6701.                     else if (line.match(/passable_on_death/i))
  6702.                         obj.leTbs_passableOnDeath = true;
  6703.                     else if (line.match(/passable/i))
  6704.                         obj.leTbs_passable = true;
  6705.                     else if (line.match(/entities_can_lay_on_me/i))
  6706.                         obj.leTbs_canLayOnMe = true;
  6707.                     else if (line.match(/collision_damage_bonus\s?:\s?(.+)/i))
  6708.                         obj.leTbs_collisionDmgBonus = Number(RegExp.$1);
  6709.                     else if (line.match(/collision_damage_bonus\%\s?:\s?(.+)/i))
  6710.                         obj.leTbs_collisionDmgBonusRate = Number(RegExp.$1);
  6711.                     else if (line.match(/collision_damage_reduction\s?:\s?(.+)/i))
  6712.                         obj.leTbs_collisionDmgReduction = Number(RegExp.$1);
  6713.                     else if (line.match(/collision_damage_reduction\%\s?:\s?(.+)/i))
  6714.                         obj.leTbs_collisionDmgReductionRate = Number(RegExp.$1);
  6715.                     else if (line.match(/immune_knockback/i))
  6716.                         obj.leTbs_immuneKnockback = true;
  6717.                     else if (line.match(/knockback_bonus\s?:\s?(.+)/i))
  6718.                         obj.leTbs_knockbackBonus = Number(RegExp.$1);
  6719.                     else if (line.match(/knockback_reduction\s?:\s?(.+)/i))
  6720.                         obj.leTbs_knockbackReduction = Number(RegExp.$1);
  6721.                     else if (line.match(/back_dmg_bonus\s?:\s?(.+)/i))
  6722.                         obj.leTbs_directionalDmgBonus.back = Number(RegExp.$1);
  6723.                     else if (line.match(/back_dmg_reduction\s?:\s?(.+)/i))
  6724.                         obj.leTbs_directionalDmgReduction.back = Number(RegExp.$1);
  6725.                     else if (line.match(/face_dmg_bonus\s?:\s?(.+)/i))
  6726.                         obj.leTbs_directionalDmgBonus.face = Number(RegExp.$1);
  6727.                     else if (line.match(/face_dmg_reduction\s?:\s?(.+)/i))
  6728.                         obj.leTbs_directionalDmgReduction.face = Number(RegExp.$1);
  6729.                     else if (line.match(/side_dmg_bonus\s?:\s?(.+)/i))
  6730.                         obj.leTbs_directionalDmgBonus.side = Number(RegExp.$1);
  6731.                     else if (line.match(/side_dmg_reduction\s?:\s?(.+)/i))
  6732.                         obj.leTbs_directionalDmgReduction.side = Number(RegExp.$1);
  6733.                 }
  6734.             }
  6735.         }
  6736.     }
  6737. };
  6738.  
  6739. DataManager.processLeTBSTagsForEquipmentsAndStates = function () {
  6740.     var groups = [$dataWeapons, $dataArmors, $dataStates];
  6741.     for (var i = 0; i < groups.length; i++) {
  6742.         var group = groups[i];
  6743.         for (var j = 1; j < group.length; j++) {
  6744.             var obj = group[j];
  6745.             var notedata = obj.note.split(/[\r\n]+/);
  6746.             var letbs = false;
  6747.  
  6748.             obj.leTbs_movePointsPlus = 0;
  6749.             obj.leTbs_scopeData = Lecode.S_TBS.defaultAttackScope;
  6750.             obj.leTbs_aoeData = null;
  6751.             obj.leTbs_sequenceData = null;
  6752.             obj.leTbs_moveScopeData = null;
  6753.             obj.leTbs_moveScopeParam = null;
  6754.             obj.leTbs_passable = false;
  6755.             obj.leTbs_passableOnDeath = false;
  6756.             obj.leTbs_canLayOnMe = false;
  6757.             obj.leTbs_collisionDmgBonus = 0;
  6758.             obj.leTbs_collisionDmgBonusRate = 0;
  6759.             obj.leTbs_collisionDmgReduction = 0;
  6760.             obj.leTbs_collisionDmgReductionRate = 0;
  6761.             obj.leTbs_immuneKnockback = false;
  6762.             obj.leTbs_knockbackBonus = 0;
  6763.             obj.leTbs_knockbackReduction = 0;
  6764.             obj.leTbs_directionalDmgBonus = {
  6765.                 face: 0,
  6766.                 back: 0,
  6767.                 side: 0
  6768.             };
  6769.             obj.leTbs_directionalDmgReduction = {
  6770.                 face: 0,
  6771.                 back: 0,
  6772.                 side: 0
  6773.             };
  6774.  
  6775.             for (var k = 0; k < notedata.length; k++) {
  6776.                 var line = notedata[k];
  6777.                 if (line.match(/<letbs>/i))
  6778.                     letbs = true;
  6779.                 else if (line.match(/<\/letbs>/i))
  6780.                     letbs = false;
  6781.  
  6782.                 if (letbs) {
  6783.                     if (line.match(/move_points_plus\s?:\s?(.+)/i))
  6784.                         obj.leTbs_movePointsPlus = Number(RegExp.$1);
  6785.                     else if (line.match(/scope\s?:\s?(.+)/i))
  6786.                         obj.leTbs_scopeData = String(RegExp.$1);
  6787.                     else if (line.match(/aoe\s?:\s?(.+)/i))
  6788.                         obj.leTbs_aoeData = String(RegExp.$1);
  6789.                     else if (line.match(/sequence\s?:\s?(.+)/i))
  6790.                         obj.leTbs_sequenceData = String(RegExp.$1);
  6791.                     else if (line.match(/move_scope\s?:\s?(.+)/i))
  6792.                         obj.leTbs_moveScopeData = String(RegExp.$1);
  6793.                     else if (line.match(/move_scope_options\s?:\s?((.|\n)*)/i))
  6794.                         obj.leTbs_moveScopeParam = String(RegExp.$1);
  6795.                     else if (line.match(/passable_on_death/i))
  6796.                         obj.leTbs_passableOnDeath = true;
  6797.                     else if (line.match(/passable/i))
  6798.                         obj.leTbs_passable = true;
  6799.                     else if (line.match(/entities_can_lay_on_me/i))
  6800.                         obj.leTbs_canLayOnMe = true;
  6801.                     else if (line.match(/collision_damage_bonus\s?:\s?(.+)/i))
  6802.                         obj.leTbs_collisionDmgBonus = Number(RegExp.$1);
  6803.                     else if (line.match(/collision_damage_bonus\%\s?:\s?(.+)/i))
  6804.                         obj.leTbs_collisionDmgBonusRate = Number(RegExp.$1);
  6805.                     else if (line.match(/collision_damage_reduction\s?:\s?(.+)/i))
  6806.                         obj.leTbs_collisionDmgReduction = Number(RegExp.$1);
  6807.                     else if (line.match(/collision_damage_reduction\%\s?:\s?(.+)/i))
  6808.                         obj.leTbs_collisionDmgReductionRate = Number(RegExp.$1);
  6809.                     else if (line.match(/immune_knockback/i))
  6810.                         obj.leTbs_immuneKnockback = true;
  6811.                     else if (line.match(/knockback_bonus\s?:\s?(.+)/i))
  6812.                         obj.leTbs_knockbackBonus = Number(RegExp.$1);
  6813.                     else if (line.match(/knockback_reduction\s?:\s?(.+)/i))
  6814.                         obj.leTbs_knockbackReduction = Number(RegExp.$1);
  6815.                     else if (line.match(/knockback_bonus\s?:\s?(.+)/i))
  6816.                         obj.leTbs_knockbackBonus = Number(RegExp.$1);
  6817.                     else if (line.match(/knockback_reduction\s?:\s?(.+)/i))
  6818.                         obj.leTbs_knockbackReduction = Number(RegExp.$1);
  6819.                     else if (line.match(/back_dmg_bonus\s?:\s?(.+)/i))
  6820.                         obj.leTbs_directionalDmgBonus.back = Number(RegExp.$1);
  6821.                     else if (line.match(/back_dmg_reduction\s?:\s?(.+)/i))
  6822.                         obj.leTbs_directionalDmgReduction.back = Number(RegExp.$1);
  6823.                     else if (line.match(/face_dmg_bonus\s?:\s?(.+)/i))
  6824.                         obj.leTbs_directionalDmgBonus.face = Number(RegExp.$1);
  6825.                     else if (line.match(/face_dmg_reduction\s?:\s?(.+)/i))
  6826.                         obj.leTbs_directionalDmgReduction.face = Number(RegExp.$1);
  6827.                     else if (line.match(/side_dmg_bonus\s?:\s?(.+)/i))
  6828.                         obj.leTbs_directionalDmgBonus.side = Number(RegExp.$1);
  6829.                     else if (line.match(/side_dmg_reduction\s?:\s?(.+)/i))
  6830.                         obj.leTbs_directionalDmgReduction.side = Number(RegExp.$1);
  6831.                 }
  6832.             }
  6833.         }
  6834.     }
  6835. };
  6836.  
  6837. DataManager.processLeTBSTagsForObjects = function () {
  6838.     var groups = [$dataSkills, $dataItems, $dataWeapons];
  6839.     for (var i = 0; i < groups.length; i++) {
  6840.         var group = groups[i];
  6841.         for (var j = 1; j < group.length; j++) {
  6842.             var obj = group[j];
  6843.             var notedata = obj.note.split(/[\r\n]+/);
  6844.             var letbs = false;
  6845.  
  6846.             obj.leTbs_scopeData = null;
  6847.             obj.leTbs_aoeData = null;
  6848.             obj.leTbs_sequenceData = null;
  6849.             obj.leTbs_passOnUse = false;
  6850.             obj.leTbs_maxUse = 1;
  6851.             obj.leTbs_scopeParam = [];
  6852.             obj.leTbs_aiOffenseType = null;
  6853.             obj.leTbs_collisionFormula = null;
  6854.             obj.leTbs_directionalDmgBonus = {
  6855.                 face: 0,
  6856.                 back: 0,
  6857.                 side: 0
  6858.             };
  6859.             obj.leTbs_directionalDmgReduction = {
  6860.                 face: 0,
  6861.                 back: 0,
  6862.                 side: 0
  6863.             };
  6864.  
  6865.             for (var k = 0; k < notedata.length; k++) {
  6866.                 var line = notedata[k];
  6867.                 if (line.match(/<letbs>/i))
  6868.                     letbs = true;
  6869.                 else if (line.match(/<\/letbs>/i))
  6870.                     letbs = false;
  6871.  
  6872.                 if (letbs) {
  6873.                     if (line.match(/scope\s?:\s?(.+)/i))
  6874.                         obj.leTbs_scopeData = String(RegExp.$1);
  6875.                     else if (line.match(/aoe\s?:\s?(.+)/i))
  6876.                         obj.leTbs_aoeData = String(RegExp.$1);
  6877.                     else if (line.match(/sequence\s?:\s?(.+)/i))
  6878.                         obj.leTbs_sequenceData = String(RegExp.$1);
  6879.                     else if (line.match(/pass_on_use/i))
  6880.                         obj.leTbs_passOnUse = true;
  6881.                     else if (line.match(/max_use:\s?:\s?(.+)/i))
  6882.                         obj.leTbs_maxUse = Number(RegExp.$1);
  6883.                     else if (line.match(/scope_options\s?:\s?((.|\n)*)/i))
  6884.                         obj.leTbs_scopeParam = String(RegExp.$1);
  6885.                     else if (line.match(/ai_offense_type/i))
  6886.                         obj.leTbs_aiOffenseType = true;
  6887.                     else if (line.match(/collision_formula\s?:\s?(.+)/i))
  6888.                         obj.leTbs_collisionFormula = String(RegExp.$1);
  6889.                     else if (line.match(/knockback_bonus\s?:\s?(.+)/i))
  6890.                         obj.leTbs_knockbackBonus = Number(RegExp.$1);
  6891.                     else if (line.match(/knockback_reduction\s?:\s?(.+)/i))
  6892.                         obj.leTbs_knockbackReduction = Number(RegExp.$1);
  6893.                     else if (line.match(/back_dmg_bonus\s?:\s?(.+)/i))
  6894.                         obj.leTbs_directionalDmgBonus.back = Number(RegExp.$1);
  6895.                     else if (line.match(/back_dmg_reduction\s?:\s?(.+)/i))
  6896.                         obj.leTbs_directionalDmgReduction.back = Number(RegExp.$1);
  6897.                     else if (line.match(/face_dmg_bonus\s?:\s?(.+)/i))
  6898.                         obj.leTbs_directionalDmgBonus.face = Number(RegExp.$1);
  6899.                     else if (line.match(/face_dmg_reduction\s?:\s?(.+)/i))
  6900.                         obj.leTbs_directionalDmgReduction.face = Number(RegExp.$1);
  6901.                     else if (line.match(/side_dmg_bonus\s?:\s?(.+)/i))
  6902.                         obj.leTbs_directionalDmgBonus.side = Number(RegExp.$1);
  6903.                     else if (line.match(/side_dmg_reduction\s?:\s?(.+)/i))
  6904.                         obj.leTbs_directionalDmgReduction.side = Number(RegExp.$1);
  6905.                 }
  6906.             }
  6907.         }
  6908.     }
  6909. };
  6910.  
  6911. /*-------------------------------------------------------------------------
  6912. * ImageManager
  6913. -------------------------------------------------------------------------*/
  6914. ImageManager.loadLeTBS = function (filename, hue) {
  6915.     return this.loadBitmap('img/leTBS/', filename, hue, true);
  6916. };
  6917.  
  6918. ImageManager.loadLeTBSTurnOrder = function (filename, hue) {
  6919.     return this.loadBitmap('img/leTBS/TurnOrder/', filename, hue, true);
  6920. };
  6921.  
  6922. ImageManager.loadLeTBSBattler = function (filename, hue) {
  6923.     return this.loadBitmap('img/leTBS/Battlers/', filename, hue, true);
  6924. };
  6925.  
  6926. ImageManager.loadLeTBSStatus = function (filename, hue) {
  6927.     return this.loadBitmap('img/leTBS/Status/', filename, hue, true);
  6928. };
  6929.  
  6930. ImageManager.loadLeTBSProjectile = function (filename, hue) {
  6931.     return this.loadBitmap('img/leTBS/Projectiles/', filename, hue, true);
  6932. };
  6933.  
  6934. ImageManager.loadLeTBS("Battle_Start");
  6935.  
  6936. /*-------------------------------------------------------------------------
  6937. * Game_Map
  6938. -------------------------------------------------------------------------*/
  6939. Lecode.S_TBS.oldIsPassable_method = Game_Map.prototype.isPassable;
  6940. Game_Map.prototype.isPassable = function (x, y, d) {
  6941.     var result = Lecode.S_TBS.oldIsPassable_method.call(this, x, y, d);
  6942.     if (LeUtilities.isScene("Scene_Battle") && BattleManagerTBS.activeEntity()) {
  6943.         var entityCell = BattleManagerTBS.activeEntity().getCell();
  6944.         var cell = BattleManagerTBS.getCellAt(x, y);
  6945.         if (cell && !(entityCell.x == x && entityCell.y == y) && cell.isThereEntity())
  6946.             return false;
  6947.     }
  6948.     return result;
  6949. };
  6950.  
  6951.  
  6952. /*-------------------------------------------------------------------------
  6953. * TBS_FloatingAction
  6954. -------------------------------------------------------------------------*/
  6955. function TBS_FloatingAction() {
  6956.     this.initialize.apply(this, arguments);
  6957. }
  6958. TBS_FloatingAction.prototype = Object.create(Game_Action.prototype);
  6959. TBS_FloatingAction.prototype.constructor = TBS_FloatingAction;
  6960.  
  6961. TBS_FloatingAction.prototype.initialize = function () {
  6962.     Game_Action.prototype.initialize.call(this, arguments);
  6963. };
  6964.  
  6965. TBS_FloatingAction.prototype.setSubject = function (subject) {
  6966.     this._subject = subject;
  6967. };
  6968.  
  6969. TBS_FloatingAction.prototype.subject = function () {
  6970.     return this._subject;
  6971. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement