Astfgl

AstfglActorAI

May 15th, 2017
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Astfgl Programmable Actor AI
  3. // by Astfgl
  4. // Date: 12/05/2017
  5. // Latest revision 27/05/2018
  6. // 20/05/2017 -> Added the possibility to delete a line
  7. // 21/03/2018 -> Fixed missing alias
  8. // 27/05/2018 -> added parameters and notetags
  9. // TOU:
  10. // Yanfly's terms of use apply, please credit me with any of the following
  11. // Astfgl66/Pierre MATEO/Pierre MATEO (Astfgl66).
  12. //=============================================================================
  13.  
  14.  
  15. /*:
  16.  * @plugindesc Actor with the autobattle flag will use a custom AI
  17.  * @author Astfgl
  18.  * @help Requires Yanfly's Battle AI core plugin.
  19.  * This plugin can do two things:
  20.  * If the parameter is set to false, actor with an autobattle flag
  21.  * set via a trait (actor, class, equipment or state) will use
  22.  * yanfly's AI notetag to determine action patterns.
  23.  * Use the actor's note box just as you would an enemy's.
  24.  *
  25.  * If the parameter is set to true, it will allow the player to
  26.  * determine the AI of his party members via a custom scene.
  27.  * To call the scene use: SceneManager.push(Scene_AI)
  28.  *
  29.  * Made with Battle AI core v1.12
  30.  *
  31.  * How to add to yanfly's main menu manager:
  32.  * Name: "AI"
  33.  * Symbol: ai
  34.  * Show: true
  35.  * Enabled: true
  36.  * Main bind: this.commandPersonal.bind(this)
  37.  * Actor bind: SceneManager.push(Scene_AI)
  38.  *
  39.  * V2-> added parameter and notetags to remove skills states and elements from
  40.  * windows.
  41.  * Parameters are global and unmodifiable.
  42.  * Notetags are on a per actor basis and dynamic.
  43.  * <AIRS: [1,2]> in harolds note box will prevent skills 1 and 2 to show, even if learned, for Harolds AI.
  44.  * <AIRE: [1,2]> same but for elements 1 and 2.
  45.  * <AIRSt: [1,2]> same but for states 1 and 2.
  46.  * These notetags are evaled each time the AI Line window is created.
  47.  * This means you can use a game variable to store the array, and modify it in game via events.
  48.  *
  49.  * @param InGameAI
  50.  * @desc If enabled, the player will be able to define AI in game.
  51.  * If not use notetags just as for an enemy to define patterns.
  52.  * @default true
  53.  *
  54.  * @param ElemRemove
  55.  * @desc Elements in this array will not appear in the
  56.  * in game list. Ex: [0,1,2,3,...]
  57.  * @default []
  58.  *
  59.  * @param SkillRemove
  60.  * @desc Skills in this array will not appear in the
  61.  * in game list. Ex: [0,1,2,3,...]
  62.  * @default []
  63.  *
  64.  * @param StateRemove
  65.  * @desc States in this array will not appear in the
  66.  * in game list. Ex: [0,1,2,3,...]
  67.  * @default []
  68.  */
  69.  
  70. var Astfgl = Astfgl || {}
  71. Astfgl.parameters = Astfgl.parameters || {}
  72. Astfgl.parameters.actorAI = {}
  73. Astfgl.parameters.actorAI.inGameAI = PluginManager.parameters("AstfglActorAI").InGameAI
  74. Astfgl.parameters.actorAI.ElemRemove = eval(PluginManager.parameters("AstfglActorAI").ElemRemove)
  75. Astfgl.parameters.actorAI.SkillRemove = eval(PluginManager.parameters("AstfglActorAI").SkillRemove)
  76. Astfgl.parameters.actorAI.StateRemove = eval(PluginManager.parameters("AstfglActorAI").StateRemove)
  77.  
  78. //=============================================================================
  79. // Game_Party
  80. //=============================================================================
  81.  
  82. Game_Party.prototype.updateAIPatterns = function() {
  83.     for (var i = 0; i < this.aliveMembers().length; ++i) {
  84.       var member = this.aliveMembers()[i];
  85.       if (member) member.setAIPattern();
  86.     }
  87. };
  88.  
  89. Game_Party.prototype.setup = function(troopId) {
  90.     this._aiKnownElementRates = this._aiKnownElementRates || {};
  91. };
  92.  
  93. Game_Party.prototype.aiElementRateKnown = function(target, elementId) {
  94.     if (target.isActor()) return true;
  95.     if (!Yanfly.Param.CoreAIElementTest) return true;
  96.     var index = target.index();
  97.     if (this._aiKnownElementRates[index] === undefined) {
  98.       this._aiKnownElementRates[index] = [];
  99.     }
  100.     return this._aiKnownElementRates[index].contains(elementId);
  101. };
  102.  
  103. Game_Party.prototype.aiRegisterElementRate = function(target, elementId) {
  104.     if (!Yanfly.Param.CoreAIElementTest) return;
  105.     var index = target.index();
  106.     if (this._aiKnownElementRates[index] === undefined) {
  107.       this._aiKnownElementRates[index] = [];
  108.     }
  109.     if (!this._aiKnownElementRates[index].contains(elementId)) {
  110.       this._aiKnownElementRates[index].push(elementId);
  111.     }
  112. };
  113.  
  114. //=============================================================================
  115. // Game_Actor
  116. //=============================================================================
  117.  
  118.  
  119. Yanfly.CoreAI.Game_Actor_makeActions = Game_Actor.prototype.makeActions;
  120. Game_Actor.prototype.makeActions = function() {
  121.     if (this.isAutoBattle() && this.actor().aiPattern.length > 0) {
  122.       this.setAIPattern();
  123.       this.setActionState('waiting');
  124.     } else {
  125.       Yanfly.CoreAI.Game_Actor_makeActions.call(this);
  126.     }
  127. };
  128.  
  129. Game_Actor.prototype.aiConsiderTaunt = function() {
  130.   if (!Imported.YEP_Taunt) return false;
  131.   return this.actor().aiConsiderTaunt;
  132. };
  133.  
  134. Game_Actor.prototype.setAIPattern = function() {
  135.     Game_Battler.prototype.setAIPattern.call(this);
  136.     if (this.numActions() <= 0) return;
  137.     AIManager.setBattler(this);
  138.     if (!Astfgl.parameters.actorAI.inGameAI) {
  139.         for (var i = 0; i < this.actor().aiPattern.length; ++i) {
  140.           if (Math.random() > this.aiLevel()) continue;
  141.           var line = this.actor().aiPattern[i];
  142.           if (AIManager.isDecidedActionAI(line)) return;
  143.         }
  144.     } else {
  145.         for (var i = 0; i < this._AILines.length; i++) {
  146.             if (Math.random() > this.aiLevel()) continue;
  147.             var line = this._AILines[i].makeOutput();
  148.             if (AIManager.isDecidedActionAI(line)) return;
  149.         }
  150.     }
  151.     Yanfly.CoreAI.Game_Actor_makeActions.call(this);
  152. };
  153.  
  154. Game_Actor.prototype.aiLevel = function() {
  155.     return this.actor().aiLevel;
  156. };
  157.  
  158. if (Yanfly.Param.CoreAIDynamic) {
  159.   BattleManager.getNextSubject = function() {
  160.     var battler = Yanfly.CoreAI.BattleManager_getNextSubject.call(this);
  161.     if (battler && battler.isEnemy()) battler.setAIPattern();
  162.     if (battler && battler.isActor() && battler.isAutoBattle()) battler.setAIPattern();
  163.     return battler;
  164.   };
  165. };
  166.  
  167. //========================================================
  168. //LoadNotetags
  169. //========================================================
  170. Yanfly.CoreAI.DataManager_isDatabaseLoaded2 = DataManager.isDatabaseLoaded;
  171. DataManager.isDatabaseLoaded = function() {
  172.   if (!Yanfly.CoreAI.DataManager_isDatabaseLoaded2.call(this)) return false;
  173.   if (!Yanfly._loaded_YEP_BattleAICore2) {
  174.     this.processCoreAINotetags1($dataActors);
  175.     Yanfly._loaded_YEP_BattleAICore2 = true;
  176.   }
  177.     return true;
  178. };
  179.  
  180. //Initialize actor enemy weakness knowledge
  181. (function(){
  182.     var _Astfgl_newSBS = Scene_Battle.prototype.initialize
  183.     Scene_Battle.prototype.initialize = function() {
  184.         _Astfgl_newSBS.call(this);
  185.         $gameParty.setup();
  186.     }
  187.     var _Astfgl_GAS = Game_Actor.prototype.setup
  188.     Game_Actor.prototype.setup = function(actorId) {
  189.         _Astfgl_GAS.call(this,actorId)
  190.         this._AILines = [0]
  191.     }
  192.    
  193. })()
  194.  
  195.  
  196. function AILine() {
  197.     this.initialize.apply(this, arguments);
  198. }
  199.  
  200. AILine.prototype = {}
  201. AILine.prototype.constructor = AILine;
  202.  
  203. AILine.prototype.initialize = function(actor) {
  204.     this._actor = actor;
  205.     this._condition = 0;
  206.     this._skill = 0;
  207.     this._param = -1;
  208.     this._numCond = -1;
  209.     this._num = 0;
  210.     this._elem = -1;
  211.     this._elemCond = -1;
  212.     this._state = 0;
  213.     this._lvlCond = -1;
  214.     this._output = "";
  215.     this._targetting = -1;
  216.     this._checks = [];
  217.     this._needsParam = false;
  218.     this._hasParam = false;
  219.     this._needsElement = false;
  220.     this._hasElement = false;
  221.     this._needsNum = false;
  222.     this._hasNum = false;
  223.     this._needsNumCond = false;
  224.     this._hasNumCond = false;
  225.     this._needsState = false;
  226.     this._hasState = false;
  227.     this._needsElemCond = false;
  228.     this._hasElemCond = false;
  229.     this._hasSkill = false;
  230.     this._needsLvlCond = false;
  231.     this._hasLvlCond = false;
  232.     this._hasTargetting = false;
  233.     this.setupConditions();
  234.     this.setupParams();
  235.     this.setupNumConds();
  236.     this.setupElemConds();
  237.     this.setupLvlConds();
  238.     this.setupTargetting();
  239. };
  240.  
  241. AILine.prototype.setupConditions = function() {
  242.     this.conditions = []
  243.     this.conditions.push("Always")
  244.     this.conditions.push("Element")
  245.     this.conditions.push("Party Alive Members")
  246.     this.conditions.push("Troop Alive Members")
  247.     this.conditions.push("Party Dead Members")
  248.     this.conditions.push("Troop Dead Members")
  249.     this.conditions.push("param")
  250.     this.conditions.push("Party Level")
  251.     this.conditions.push("Random")
  252.     this.conditions.push("State ===")
  253.     this.conditions.push("State !==")
  254.     this.conditions.push("Turn")
  255.     this.conditions.push("User param")
  256.     this.conditions.push("Delete")
  257. }
  258.  
  259. AILine.prototype.setupParams = function() {
  260.     this.params = [];
  261.     this.params.push("atk")
  262.     this.params.push("def")
  263.     this.params.push("mat")
  264.     this.params.push("mdf")
  265.     this.params.push("agi")
  266.     this.params.push("luk")
  267.     this.params.push("maxhp")
  268.     this.params.push("maxmp")
  269.     this.params.push("hp")
  270.     this.params.push("mp")
  271.     this.params.push("hp%")
  272.     this.params.push("mp%")
  273.     this.params.push("level")
  274.     this.params.push("tp")
  275.     this.params.push("maxtp")
  276.     this.params.push("tp%")
  277. }
  278.  
  279. AILine.prototype.setupNumConds = function() {
  280.     this.numConds = []
  281.     this.numConds.push(">")
  282.     this.numConds.push("<")
  283.     this.numConds.push("===")
  284.     this.numConds.push(">=")
  285.     this.numConds.push("<=")
  286.     this.numConds.push("!==")
  287. }
  288.  
  289. AILine.prototype.setupElemConds = function() {
  290.     this.elemConds = []
  291.     this.elemConds.push("Neutral")
  292.     this.elemConds.push("Weakness")
  293.     this.elemConds.push("Resistant")
  294.     this.elemConds.push("Null")
  295.     this.elemConds.push("Absorb")
  296. }
  297.  
  298. AILine.prototype.setupLvlConds = function() {
  299.     this.lvlConds = []
  300.     this.lvlConds.push("Highest");
  301.     this.lvlConds.push("Lowest");
  302.     this.lvlConds.push("Average");
  303. }
  304.  
  305. AILine.prototype.setupTargetting = function() {
  306.     this.targetting = []
  307.     this.targetting.push("None");
  308.     this.targetting.push("First");
  309.     this.targetting.push("User");
  310.     this.targetting.push("Highest MaxHP")
  311.     this.targetting.push("Highest HP")
  312.     this.targetting.push("Highest HP%")
  313.     this.targetting.push("Highest MP")
  314.     this.targetting.push("Highest MP%")
  315.     this.targetting.push("Highest MaxMP")
  316.     this.targetting.push("Highest MaxTP")
  317.     this.targetting.push("Highest TP")
  318.     this.targetting.push("Highest TP%")
  319.     this.targetting.push("Highest ATK")
  320.     this.targetting.push("Highest DEF")
  321.     this.targetting.push("Highest MAT")
  322.     this.targetting.push("Highest MDF")
  323.     this.targetting.push("Highest LUK")
  324.     this.targetting.push("Highest Level")
  325.     this.targetting.push("Lowest MaxHP")
  326.     this.targetting.push("Lowest HP")
  327.     this.targetting.push("Lowest HP%")
  328.     this.targetting.push("Lowest MP")
  329.     this.targetting.push("Lowest MP%")
  330.     this.targetting.push("Lowest MaxMP")
  331.     this.targetting.push("Lowest MaxTP")
  332.     this.targetting.push("Lowest TP")
  333.     this.targetting.push("Lowest TP%")
  334.     this.targetting.push("Lowest ATK")
  335.     this.targetting.push("Lowest DEF")
  336.     this.targetting.push("Lowest MAT")
  337.     this.targetting.push("Lowest MDF")
  338.     this.targetting.push("Lowest LUK")
  339.     this.targetting.push("Lowest Level")
  340. }
  341.  
  342. AILine.prototype.setCondition = function(id) {
  343.     if (id < 0) {return};
  344.     if (id >= this.conditions.length) {return}
  345.     this._condition = id
  346. }
  347.  
  348. AILine.prototype.setSkill = function(skill) {
  349.     this._skill = skill;
  350.     this._hasSkill = true;
  351. }
  352.  
  353. AILine.prototype.setParam = function(id) {
  354.     if (id < 0) {return};
  355.     if (id >= this.params.length) {return}
  356.     this._param = id;
  357.     this._hasParam = true;
  358. }
  359.  
  360. AILine.prototype.setElement = function(id) {
  361.     if (id < 0) {return};
  362.     if (id >= $dataSystem.elements.length) {return}
  363.     var d = $dataSystem.elements.slice(0)
  364.     d.splice(0,1)
  365.     d.forEach(function(e, i) {if (Astfgl.parameters.actorAI.ElemRemove.includes($dataSystem.elements.indexOf(e))) {d.splice(i,1)}})
  366.     var actorData = $dataActors[this._actor.actorId()]
  367.     if (actorData.meta.AIRE) {
  368.         let list = eval(actorData.meta.AIRE);
  369.         d.forEach(function(e, i) {if (list.includes($dataSystem.elements.indexOf(e))) {d.splice(i,1)}})
  370.     }
  371.     this._elem = $dataSystem.elements.indexOf(d[id]);
  372.     this._hasElement = true;
  373. }
  374.  
  375. AILine.prototype.setNumCond = function(id) {
  376.     if (id < 0) {return};
  377.     if (id >= this.numConds.length) {return}
  378.     this._numCond = id;
  379.     this._hasNumCond = true;
  380. }
  381.  
  382. AILine.prototype.setNum = function(num) {
  383.     this._num = num;
  384.     this._hasNum = true;
  385. }
  386.  
  387. AILine.prototype.setState = function(state) {
  388.     this._state = state;
  389.     this._hasState = true;
  390. }
  391.  
  392. AILine.prototype.setLvlCond = function(id) {
  393.     if (id < 0) {return};
  394.     if (id >= this.lvlConds.length) {return}
  395.     this._lvlCond = id;
  396.     this._hasLvlCond = true;
  397. }
  398.  
  399. AILine.prototype.setElemCond = function(id) {
  400.     if (id < 0) {return};
  401.     if (id >= this.elemConds.length) {return}
  402.     this._elemCond = id;
  403.     this._hasElemCond = true;
  404. }
  405.  
  406. AILine.prototype.setTargetting = function(id) {
  407.     if (id < -1) {return};
  408.     if (id >= this.targetting.length) {return}
  409.     this._targetting = id;
  410.     this._hasTargetting = true;
  411.     if (id < 1) {
  412.         this._hasTargetting = false;
  413.     }
  414. }
  415.  
  416. AILine.prototype.makeOutput = function()  {
  417.     var first, second, third, fourth
  418.     this._checks = []
  419.     if (this._condition === 0) {
  420.         this._checks.push("skill")
  421.        
  422.         if (!this.valid()) {
  423.             console.log("invalid AI condition")
  424.             return
  425.         }
  426.        
  427.         this._output = "Always: Skill " + this._skill.id
  428.     }
  429.     if (this._condition === 1) {
  430.         this._checks.push("elem");
  431.         this._checks.push("elem cond");
  432.         this._checks.push("skill");
  433.        
  434.         if (!this.valid()) {
  435.             console.log("invalid AI condition")
  436.             return
  437.         }
  438.        
  439.         this._output = "Element " + (this._elem ) + " " + ": Skill " + this._skill.id
  440.     }
  441.     if (this._condition === 2 || this._condition === 3 || this._condition === 4 || this._condition === 5 || this._condition === 11) {
  442.         this._checks.push("num cond");
  443.         this._checks.push("num");
  444.         this._checks.push("skill");
  445.         if (this._condition === 2) {first = "Party Alive Members"}
  446.         if (this._condition === 3) {first = "Troop Alive Members"}
  447.         if (this._condition === 4) {first = "Party Dead Members"}
  448.         if (this._condition === 5) {first = "Troop Dead Members"}
  449.         if (this._condition === 11) {first = "Turn"}
  450.        
  451.         if (!this.valid()) {
  452.             console.log("invalid AI condition")
  453.             return
  454.         }
  455.        
  456.         this._output = first + " " + this.numConds[this._numCond] + " " + this._num + ": Skill " + this._skill.id
  457.     }
  458.     if (this._condition === 6 || this._condition === 12) {
  459.         this._checks.push("param");
  460.         this._checks.push("num cond");
  461.         this._checks.push("num");
  462.         this._checks.push("skill");
  463.         if (this._condition === 6) {first = ""}
  464.         if (this._condition === 12) {first = "User "}
  465.        
  466.         if (!this.valid()) {
  467.             console.log("invalid AI condition")
  468.             return
  469.         }
  470.        
  471.         var numText = ""
  472.         if (this._param === 10 || this._param === 11 || this._param === 15) {
  473.             numText = "%"
  474.         }
  475.        
  476.         this._output = first + this.params[this._param] + " param " + this.numConds[this._numCond] + " " + this._num + numText + ": Skill " + this._skill.id
  477.     }
  478.     if (this._condition === 7) {
  479.         this._checks.push("lvl cond");
  480.         this._checks.push("num cond");
  481.         this._checks.push("num");
  482.         this._checks.push("skill");
  483.        
  484.         if (!this.valid()) {
  485.             console.log("invalid AI condition")
  486.             return
  487.         }
  488.        
  489.         this._output = this.lvlConds[this._lvlCond] + " Party Level " + this.numConds[this._numCond] + " " + this._num + ": Skill " + this._skill.id
  490.     }
  491.     if (this._condition === 8) {
  492.         this._checks.push("num");
  493.         this._checks.push("skill");
  494.        
  495.         if (!this.valid()) {
  496.             console.log("invalid AI condition")
  497.             return
  498.         }
  499.        
  500.         this._output = "Random " + this._num + "%: Skill " + this._skill.id
  501.     }
  502.     if (this._condition === 9 || this._condition === 10) {
  503.         this._checks.push("state");
  504.         this._checks.push("skill");
  505.        
  506.         if (!this.valid()) {
  507.             console.log("invalid AI condition")
  508.             return
  509.         }
  510.        
  511.         if (this._condition === 9) {first = "State === "}
  512.         if (this._condition === 10) {first = "State !== "}
  513.         this._output = first + this._state.id + ": Skill " + this._skill.id
  514.     }
  515.     if (this._hasTargetting) {
  516.         this._output += ", " + this.targetting[this._targetting]
  517.     }
  518.     return this._output
  519. }
  520.  
  521. AILine.prototype.valid = function() {
  522.     for (var i = 0; i < this._checks.length; i++) {
  523.         var check = this._checks[i]
  524.         if (check === "num") {
  525.             if (!this._hasNum) {return false}
  526.         }
  527.         if (check === "num cond") {
  528.             if (!this._hasNumCond) {return false}
  529.         }
  530.         if (check === "elem") {
  531.             if (!this._hasElement) {return false}
  532.         }
  533.         if (check === "elem cond") {
  534.             if (!this._hasElemCond) {return false}
  535.         }
  536.         if (check === "skill") {
  537.             if (!this._hasSkill) {return false}
  538.         }
  539.         if (check === "param") {
  540.             if (!this._hasParam) {return false}
  541.         }
  542.         if (check === "lvl cond") {
  543.             if (!this._hasLvlCond) {return false}
  544.         }
  545.         if (check === "state") {
  546.             if (!this._hasState) {return false}
  547.         }
  548.     }
  549.     return true
  550. }
  551.  
  552. //============================================================
  553. // Window_SetAI
  554. //============================================================
  555.  
  556. function Window_SetAI() {
  557.     this.initialize.apply(this, arguments);
  558. }
  559.  
  560. Window_SetAI.prototype = Object.create(Window_Selectable.prototype);
  561. Window_SetAI.prototype.constructor = Window_SetAI;
  562.  
  563. Window_SetAI.prototype.initialize = function(x, y, width, height) {
  564.     Window_Selectable.prototype.initialize.call(this, x, y, width, height);
  565.     this._actor = SceneManager._scene._actor
  566.     this._data = [];
  567.     this._line = new AILine(this._actor)
  568.     this._type = -1;
  569.     this._previousType = -1;
  570.     this._step = 0;
  571.     this._checks = [-1,9]
  572.     this.refresh();
  573. };
  574.  
  575. Window_SetAI.prototype.maxCols = function() {
  576.     if (this._type === -1) {
  577.         return 1
  578.     } else {
  579.         return 2
  580.     }
  581. };
  582.  
  583. Window_SetAI.prototype.spacing = function() {
  584.     return 48;
  585. };
  586.  
  587. Window_SetAI.prototype.maxItems = function() {
  588.     return this._data ? this._data.length : 1;
  589. };
  590.  
  591. Window_SetAI.prototype.item = function() {
  592.     return this._data && this.index() >= 0 ? this._data[this.index()] : null;
  593. };
  594.  
  595. Window_SetAI.prototype.isCurrentItemEnabled = function() {
  596.     return true;
  597. };
  598.  
  599. Window_SetAI.prototype.makeItemList = function() {
  600.     var actorData = $dataActors[this._actor.actorId()]
  601.     if (this._line) {
  602.         if (this._type === -1) {
  603.             //AILine
  604.             this._data = this._actor._AILines
  605.         }
  606.         if (this._type === 0) {
  607.             //skills
  608.             var skills = []
  609.             skills.push($dataSkills[this._line._actor.attackSkillId()])
  610.             skills.push($dataSkills[this._line._actor.guardSkillId()])
  611.             var skillList = skills.concat(this._line._actor.skills())
  612.             skillList.forEach(function(skill, i) {if (Astfgl.parameters.actorAI.SkillRemove.includes(skill.id)) {skillList.splice(i,1)}})
  613.             if (actorData.meta.AIRS) {
  614.                 let list = eval(actorData.meta.AIRS);
  615.                 skillList.forEach(function(skill, i) {if (list.includes(skill.id)) {skillList.splice(i,1)}})
  616.             }
  617.             this._data = skillList; //$dataSkills[index].name
  618.         }
  619.         if (this._type === 1) {
  620.             //numbers
  621.             this._data = [0,1,2,3,4,5,6,7,8,9,10];
  622.         }
  623.         if (this._type === 2) {
  624.             //number conditions
  625.             this._data = this._line.numConds;
  626.         }
  627.         if (this._type === 3) {
  628.             //elements
  629.             var d = $dataSystem.elements.slice(0);
  630.             d.splice(0,1);
  631.             d.forEach(function(e, i) {if (Astfgl.parameters.actorAI.ElemRemove.includes($dataSystem.elements.indexOf(e))) {d.splice(i,1)}})
  632.             if (actorData.meta.AIRE) {
  633.                 let list = eval(actorData.meta.AIRE);
  634.                 d.forEach(function(e, i) {if (list.includes($dataSystem.elements.indexOf(e))) {d.splice(i,1)}})
  635.             }
  636.             this._data = d; //remember to add 1 when gathering index
  637.         }
  638.         if (this._type === 4) {
  639.             //element conditions
  640.             this._data = this._line.elemConds;
  641.         }
  642.         if (this._type === 5) {
  643.             //parameters
  644.             this._data = this._line.params;
  645.         }
  646.         if (this._type === 6) {
  647.             //level conditions
  648.             this._data = this._line.lvlConds;
  649.         }
  650.         if (this._type === 7) {
  651.             //states
  652.             var d = $dataStates.slice(0)
  653.             d.splice(0,1)
  654.             d.forEach(function(s, i) {if (Astfgl.parameters.actorAI.StateRemove.includes(s.id)) {d.splice(i,1)}})
  655.             if (actorData.meta.AIRSt) {
  656.                 let list = eval(actorData.meta.AIRSt);
  657.                 d.forEach(function(s, i) {if (list.includes(s.id)) {d.splice(i,1)}})
  658.             }
  659.             this._data =  d; //$dataStates[index].name
  660.         }
  661.         if (this._type === 8) {
  662.             //targetting
  663.             this._data = this._line.targetting
  664.         }
  665.         if (this._type === 9) {
  666.             //conditions
  667.             this._data = this._line.conditions
  668.         }
  669.     } else {
  670.         this._data = [];
  671.     }
  672. };
  673.  
  674. Window_SetAI.prototype.setLine = function(actor) {
  675.     this._line = new AILine(actor);
  676. }
  677.  
  678. Window_SetAI.prototype.setType = function(type) {
  679.     this._previousType = this._type
  680.     if (type === 1) {
  681.         this.deactivate();
  682.         this.hide();
  683.         $gameMessage.setNumberInput(1, 5);
  684.     }
  685.     this._type = type;
  686.     this.refresh();
  687.     this.select(0)
  688. }
  689.  
  690. Window_SetAI.prototype.selectLast = function() {
  691.      this.select(0)
  692. }
  693.  
  694. Window_SetAI.prototype.drawItem = function(index) {
  695.     var item = this._data[index];
  696.     var rect = this.itemRect(index);
  697.     rect.width -= this.textPadding();
  698.     if (this._type === 0 || this._type === 7) {
  699.         this.drawText(item.name,rect.x, rect.y, rect.width);
  700.     } else if(this._type === -1) {
  701.         if (item instanceof AILine) {
  702.             var txt = this.makeLineDescription(this._data[index])
  703.             this.drawText(txt,rect.x, rect.y, rect.width);
  704.         } else {
  705.             this.drawText("Create new line",rect.x, rect.y, rect.width);
  706.         }
  707.     } else {
  708.         this.drawText(item,rect.x, rect.y, rect.width);
  709.     }
  710. }
  711.  
  712. Window_SetAI.prototype.updateHelp = function() {
  713.     this._helpWindow.clear()
  714.     this._helpWindow.secondLine = ""
  715.     if (this._step >= 2) {
  716.         for (var i = 1; i < this._step; i++) {
  717.             var txt = ""
  718.             if (this._checks[i] === 0) {
  719.                 txt = this._line._skill.name
  720.             }
  721.             if (this._checks[i] === 1) {
  722.                 txt = this._line._num
  723.             }
  724.             if (this._checks[i] === 2) {
  725.                 txt = this._line.numConds[this._line._numCond]
  726.             }
  727.             if (this._checks[i] === 3) {
  728.                 txt = $dataSystem.elements[this._line._elem]
  729.             }
  730.             if (this._checks[i] === 4) {
  731.                 txt = this._line.elemConds[this._line._elemCond]
  732.             }
  733.             if (this._checks[i] === 5) {
  734.                 txt = this._line.params[this._line._param]
  735.             }
  736.             if (this._checks[i] === 6) {
  737.                 txt = this._line.lvlConds[this._line._lvlCond]
  738.             }
  739.             if (this._checks[i] === 7) {
  740.                 txt = this._line._state.name
  741.             }
  742.             if (this._checks[i] === 8) {
  743.                 if (this._line._targetting > 1) {
  744.                     txt = this._line.targetting[this._line._targetting]
  745.                 }
  746.             }
  747.             if (this._checks[i] === 9) {
  748.                 txt = this._line.conditions[this._line._condition]
  749.             }
  750.             this._helpWindow.secondLine += txt + " "
  751.         }
  752.     }
  753.     if (this._type === -1) {
  754.         this._helpWindow.setText("Choose a line.\n" + this._helpWindow.secondLine)
  755.     }
  756.     if (this._type === 9) {
  757.         this._helpWindow.setText("Choose a condition.\n" + this._helpWindow.secondLine)
  758.     }
  759.     if (this._type === 0) {
  760.         this._helpWindow.setText("Choose a skill.\n" + this._helpWindow.secondLine)
  761.     }
  762.     if (this._type === 1) {
  763.         this._helpWindow.setText("Choose a number.\n" + this._helpWindow.secondLine)
  764.     }
  765.     if (this._type === 2) {
  766.         this._helpWindow.setText("Choose a comparison operator.\n" + this._helpWindow.secondLine)
  767.     }
  768.     if (this._type === 3) {
  769.         this._helpWindow.setText("Choose an element.\n" + this._helpWindow.secondLine)
  770.     }
  771.     if (this._type === 4) {
  772.         this._helpWindow.setText("Choose an element condition.\n" + this._helpWindow.secondLine)
  773.     }
  774.     if (this._type === 5) {
  775.         this._helpWindow.setText("Choose a parameter.\n" + this._helpWindow.secondLine)
  776.     }
  777.     if (this._type === 6) {
  778.         this._helpWindow.setText("Choose a level condition.\n" + this._helpWindow.secondLine)
  779.     }
  780.     if (this._type === 7) {
  781.         this._helpWindow.setText("Choose a state.\n" + this._helpWindow.secondLine)
  782.     }
  783.     if (this._type === 8) {
  784.         this._helpWindow.setText("Choose a targetting condition.\n" + this._helpWindow.secondLine)
  785.     }
  786.     this._helpWindow.refresh();
  787. };
  788.  
  789. Window_SetAI.prototype.refresh = function() {
  790.     this.makeItemList();
  791.     this.createContents();
  792.     this.drawAllItems();
  793. };
  794. //var temp = new Window_SetAI(0,0,800,600); SceneManager._scene.addWindow(temp); temp.activate();
  795.  
  796. Window_SetAI.prototype.makeLineDescription = function(line) {
  797.     var skill = line._skill.name;
  798.     if (line._condition === 0) {
  799.         txt = skill
  800.     } else if (line._condition === 1) {
  801.         txt = skill + ": target is " + line.elemConds[line._elemCond] + " to " + $dataSystem.elements[line._elem]
  802.     } else if (line._condition === 2) {
  803.         txt = skill + ": alive party members " + line.numConds[line._numCond] + line._num
  804.     } else if (line._condition === 3) {
  805.         txt = skill + ": alive troop members " + line.numConds[line._numCond] + line._num
  806.     } else if (line._condition === 4) {
  807.         txt = skill + ": dead party members " + line.numConds[line._numCond] + line._num
  808.     } else if (line._condition === 5) {
  809.         txt = skill + ": dead troop members " + line.numConds[line._numCond] + line._num
  810.     } else if (line._condition === 6) {
  811.         txt = skill + ": target " + line.params[line._param] + " is " + line.numConds[line._numCond] + " to " + line._num
  812.     } else if (line._condition === 7) {
  813.         txt = skill + ": target level is " + line.lvlConds[line._lvlCond] + " to " + line._num
  814.     } else if (line._condition === 8) {
  815.         txt = skill + ": " + line._num + "% chance"
  816.     } else if (line._condition === 9) {
  817.         txt = skill + ": target is afflicted by state " + line._state.name
  818.     } else if (line._condition === 10) {
  819.         txt = skill + ": target is not afflicted by state " + line._state.name
  820.     } else if (line._condition === 11) {
  821.         txt = skill + ": turn is " + line.numConds[line._numCond] + line._num
  822.     } else if (line._condition === 12) {
  823.         txt = skill + ": caster " + line.params[line._param] + " is " + line.numConds[line._numCond] + " to " + line._num
  824.     }
  825.     if (line._hasTargetting) {
  826.         txt += ", " + line.targetting[line._targetting] + "."
  827.     } else {
  828.         txt += ", random."
  829.     }
  830.     return txt
  831. }
  832.  
  833. //Window_AIDesc
  834. function Window_AIDesc() {
  835.     this.initialize.apply(this, arguments);
  836. }
  837.  
  838. Window_AIDesc.prototype = Object.create(Window_Base.prototype);
  839. Window_AIDesc.prototype.constructor = Window_AIDesc;
  840.  
  841. Window_AIDesc.prototype.initialize = function(numLines) {
  842.     var width = Graphics.boxWidth - 200;
  843.     var height = this.fittingHeight(numLines || 2);
  844.     Window_Base.prototype.initialize.call(this, 200, 0, width, height);
  845.     this._text = '';
  846. };
  847.  
  848. Window_AIDesc.prototype.setText = function(text) {
  849.     if (this._text !== text) {
  850.         this._text = text;
  851.         this.refresh();
  852.     }
  853. };
  854.  
  855. Window_AIDesc.prototype.clear = function() {
  856.     this.setText('');
  857. };
  858.  
  859. Window_AIDesc.prototype.refresh = function() {
  860.     this.contents.clear();
  861.     this.drawTextEx(this._text, this.textPadding(), 0);
  862. };
  863.  
  864. //Window_AIName
  865. function Window_AIName() {
  866.     this.initialize.apply(this, arguments);
  867. }
  868.  
  869. Window_AIName.prototype = Object.create(Window_Base.prototype);
  870. Window_AIName.prototype.constructor = Window_AIName;
  871.  
  872. Window_AIName.prototype.initialize = function(numLines) {
  873.     var width = 200;
  874.     var height = this.fittingHeight(numLines || 2);
  875.     Window_Base.prototype.initialize.call(this, 0, 0, width, height);
  876.     this._text = '';
  877. };
  878.  
  879. Window_AIName.prototype.setText = function(text) {
  880.     if (this._text !== text) {
  881.         this._text = text;
  882.         this.refresh();
  883.     }
  884. };
  885.  
  886. Window_AIName.prototype.clear = function() {
  887.     this.setText('');
  888. };
  889.  
  890. Window_AIName.prototype.refresh = function() {
  891.     this.contents.clear();
  892.     this.drawTextEx(this._text, this.textPadding(), 0);
  893. };
  894.  
  895. //Scene AI
  896. function Scene_AI() {
  897.     this.initialize.apply(this, arguments);
  898. }
  899.  
  900. Scene_AI.prototype = Object.create(Scene_MenuBase.prototype);
  901. Scene_AI.prototype.constructor = Scene_AI;
  902.  
  903. Scene_AI.prototype.initialize = function() {
  904.     Scene_MenuBase.prototype.initialize.call(this);
  905. };
  906.  
  907. Scene_AI.prototype.create = function() {
  908.     Scene_MenuBase.prototype.create.call(this);
  909.     this.createNameWindow();
  910.     this.createDescWindow();
  911.     this.createAIWindow();
  912.     this.createMessageWindow();
  913.     this._messageWindow.hide();
  914.     this._AIWindow.activate();
  915.     this._AIWindow.select(0)
  916. };
  917.  
  918. Scene_AI.prototype.createNameWindow = function() {
  919.     var win = new Window_AIName(2)
  920.     this._nameWindow = win;
  921.     this.addWindow(this._nameWindow)
  922.     if (this._actor) {
  923.         this._nameWindow.setText(this._actor.name())
  924.     }
  925. }
  926.  
  927. Scene_AI.prototype.createDescWindow = function() {
  928.     var win = new Window_AIDesc(2);
  929.     this._descWindow = win;
  930.     this.addWindow(this._descWindow)
  931. }
  932.  
  933. Scene_AI.prototype.createAIWindow = function() {
  934.     var win = new Window_SetAI(0,this._descWindow.height,Graphics.boxWidth,Graphics.boxHeight - this._descWindow.height);
  935.     this._AIWindow = win;
  936.     this._AIWindow.setHandler('ok',     this.onAIOk.bind(this));
  937.     this._AIWindow.setHandler('cancel', this.onAICancel.bind(this));
  938.     this._AIWindow.setHandler('pagedown', this.nextActor.bind(this));
  939.     this._AIWindow.setHandler('pageup',   this.previousActor.bind(this));
  940.     this.addWindow(this._AIWindow);
  941.     this._AIWindow._helpWindow = this._descWindow;
  942. }
  943.  
  944. Scene_AI.prototype.onAIOk = function() {
  945.     if (this._messageWindow._numberWindow.active) {this._messageWindow._numberWindow.processOk(); return}
  946.     var win = this._AIWindow
  947.     if (win._type === -1) {
  948.         if (win._data[win.index()] === 0) {
  949.             win._line = new AILine(win._actor)
  950.             win._actor._AILines[win.index()] = win._line
  951.             win.setType(9)
  952.             win._actor._AILines.push(0)
  953.         } else {
  954.             win._line = win._actor._AILines[win.index()]
  955.             win.setType(9)
  956.         }
  957.         win._lineIndex = win.index()
  958.         win._step = 1;
  959.         win.activate();
  960.         win.select(win._line._condition)
  961.         return
  962.     }
  963.     if (win._type === 0) {
  964.         win._line.setSkill(win._data[win.index()])
  965.         win.setType(8);
  966.         win.activate();
  967.         win.select(0);
  968.         win._step +=1;
  969.         return
  970.     }
  971.     if (win._type === 9) {
  972.         win._line.setCondition(win.index())
  973.         if (win._line._condition === 0) {
  974.             win._checks = [-1,9,0,8]
  975.             win._step = 2;
  976.             win.setType(0)
  977.             win.select(0)
  978.         }
  979.         if (win._line._condition === 1) {
  980.             win._checks = [-1,9,3,4,0,8]
  981.             win._step = 2;
  982.             win.setType(3)
  983.             win.select(win._line._elem)
  984.         }
  985.         if (win._line._condition === 2 || win._line._condition === 3 || win._line._condition ===4 || win._line._condition === 5 || win._line._condition === 11) {
  986.             win._checks = [-1,9,2,1,0,8]
  987.             win._step = 2;
  988.             win.setType(2)
  989.             win.select(win._line._numCond)
  990.         }
  991.         if (win._line._condition === 6 || win._line._condition === 12) {
  992.             win._checks = [-1,9,5,2,1,0,8]
  993.             win._step = 2;
  994.             win.setType(5)
  995.             win.select(win._line._param)
  996.         }
  997.         if (win._line._condition === 7) {
  998.             win._checks = [-1,9,6,2,1,0,8]
  999.             win._step = 2;
  1000.             win.setType(6)
  1001.             win.select(win._line._lvlCond)
  1002.         }
  1003.         if (win._line._condition === 8) {
  1004.             win._checks = [-1,9,1,0,8]
  1005.             win._step = 2;
  1006.             win.setType(1)
  1007.             win.select(win._line._num)
  1008.         }
  1009.         if (win._line._condition === 9 || win._line._condition === 10) {
  1010.             win._checks = [-1,9,7,0,8]
  1011.             win._step = 2;
  1012.             win.setType(7)
  1013.             win.select(0)
  1014.         }
  1015.         if (win._line._condition === 13) {
  1016.             win.setType(-1);
  1017.             win._actor._AILines.splice(win._lineIndex,1);
  1018.             if (win._actor._AILines.length === 0) {
  1019.                 win._actor._AILines.push(0)
  1020.             }
  1021.             win._line = new AILine(win._actor)
  1022.             win.refresh();
  1023.             win.select(0);
  1024.         }
  1025.         win.activate();
  1026.         return
  1027.     }
  1028.     if (win._type === 3) {
  1029.         win._line.setElement(win.index());
  1030.         win.setType(4);
  1031.         win.activate();
  1032.         win._step += 1;
  1033.         return
  1034.     }
  1035.     if (win._type === 4) {
  1036.         win._line.setElemCond(win.index());
  1037.         win.setType(0);
  1038.         win.activate();
  1039.         win._step += 1;
  1040.         return
  1041.     }
  1042.     if (win._type === 2) {
  1043.         win._line.setNumCond(win.index())
  1044.         win.setType(1);
  1045.         win.activate();
  1046.         win._step += 1;
  1047.         return
  1048.     }
  1049.     if (win._type === 1) {
  1050.         win._line.setNum($gameVariables.value(1));
  1051.         win.setType(0);
  1052.         win.activate();
  1053.         win._step += 1;
  1054.         return
  1055.     }
  1056.     if (win._type === 5) {
  1057.         win._line.setParam(win.index());
  1058.         win.setType(2)
  1059.         win.activate();
  1060.         win._step += 1;
  1061.         return
  1062.     }
  1063.     if (win._type === 6) {
  1064.         win._line.setLvlCond(win.index());
  1065.         win.setType(2)
  1066.         win.activate();
  1067.         win._step += 1;
  1068.         return
  1069.     }
  1070.     if (win._type === 7) {
  1071.         win._line.setState(win._data[win.index()]);
  1072.         win.setType(0);
  1073.         win.activate();
  1074.         win._step += 1;
  1075.         return
  1076.     }
  1077.     if (win._type === 8) {
  1078.         win._line.setTargetting(win.index());
  1079.         win.setType(-1);
  1080.         win.activate();
  1081.         win._step += 1;
  1082.         return
  1083.     }
  1084. }
  1085.  
  1086. Scene_AI.prototype.onAICancel = function() {
  1087.     var win = this._AIWindow;
  1088.     if (win._type === 1) {
  1089.         return
  1090.     }
  1091.     if (win._type === -1) {
  1092.         this.popScene();
  1093.     } else {
  1094.         win._step -= 1;
  1095.         win.setType(win._checks[win._step])
  1096.         win.activate();
  1097.     }
  1098. }
  1099.  
  1100. Scene_AI.prototype.createMessageWindow = function() {
  1101.     this._messageWindow = new Window_Message();
  1102.     this.addWindow(this._messageWindow);
  1103.     this._messageWindow.subWindows().forEach(function(window) {
  1104.         this.addWindow(window);
  1105.     }, this);
  1106.     this._messageWindow._numberWindow.processOk = this.onNumOk
  1107. };
  1108.  
  1109. Scene_AI.prototype.onNumOk = function() { //this is actually called in the message window number window context and not in the scene AI context
  1110.     SoundManager.playOk();
  1111.     SceneManager._scene._AIWindow._line.setNum(this._number)
  1112.     this._messageWindow.terminateMessage();
  1113.     this.updateInputData();
  1114.     this.deactivate();
  1115.     this.close();
  1116.     SceneManager._scene._AIWindow._step += 1;
  1117.     SceneManager._scene._AIWindow.setType(0);
  1118.     SceneManager._scene._AIWindow.show();
  1119.     SceneManager._scene._AIWindow.activate();
  1120. }
  1121.  
  1122. Scene_MenuBase.prototype.onActorChange = function() {
  1123.     this.updateActor();
  1124.     this._AIWindow._actor = this._actor;
  1125.     this._AIWindow._data = [];
  1126.     this._AIWindow._line = new AILine(this._actor)
  1127.     this._AIWindow._type = -1;
  1128.     this._AIWindow._previousType = -1;
  1129.     this._AIWindow._step = 0;
  1130.     this._AIWindow._checks = [-1,9]
  1131.     this._AIWindow.refresh();
  1132.     this._AIWindow.select(0);
  1133.     this._AIWindow.activate();
  1134.     this._nameWindow.setText(this._actor.name())
  1135. };
Advertisement
Add Comment
Please, Sign In to add comment