Maliki79

MalMZMutableItems

Oct 12th, 2020 (edited)
2,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Maliki's Mutable Items MZ
  3. // MalMZMutableItems.js
  4. // version 1.5
  5. //=============================================================================
  6. /*:
  7.  * @target MZ
  8.  *  
  9.  * @plugindesc ver1.5 - Allows you to have items in your inventory automatically change to other items after a set amount of player steps
  10.  * @author Maliki79
  11.  *
  12.  * @param defaultStepReduction
  13.  * @desc The default amount of "step-points" an items mutate score will drop before it changes.
  14.  * Default: 1
  15.  * @default 1
  16.  *
  17.  * @param skipStep
  18.  * @desc Whether or not the step timer activates upon player steps.
  19.  * Default: 1
  20.  * @default 1
  21.  *
  22.  * @help You need two steps to use this plugin:
  23.  *
  24.  * 1: Setup the item(s) you want your olditem to potentially change into.
  25.  * You do this with the following notetag:
  26.  *
  27.  * <nextId: x, y, z>
  28.  *
  29.  * with x being a letter i, w, or a.  (Item, Weapon, or Armor respectively)
  30.  * y being the id number in the database and z being the percentange chance as an integer.
  31.  * Example: <nextId: i, 8, 20> will give this item a 20% chance to change into item 8 from your database.
  32.  * You can have as many of these tags on a single item as you want.
  33.  * Note that if the total percentage chance does not equal 100, there will be a chance for failure equal
  34.  * to the left over percentage.
  35.  * Also note that if the total percentage goes beyond 100%, it becomes a "rating" chance instead.
  36.  * In other words, you can safely go beyond 100% total chance.
  37.  *
  38.  * 2: Set the timing for this change via the notetag
  39.  *
  40.  * <newItemStepCount: x>
  41.  *
  42.  * with x being the number of step-points required before the item will change.
  43.  * Every time the player moves a step, the total steps required for the item to change (x) will be reduced
  44.  * by the number of step-points chosen. (Default is 1).
  45.  *
  46.  * 3a: (Optional) Evals have been added so advanced users can input different effects for successful and failing changes.
  47.  * Use the notetags <newItemSuccessEval: >>> or <newItemFailureEval: >>> noting the space after the : as well as the 3 >.
  48.  *
  49.  * 3b: The <stepPointAdjustEval: >>> tag has been added to allow different influences to change how fast or slow the
  50.  * Step-points are reduced.
  51.  * Add an eval that returns a number and that number will be added to the step-point total.
  52.  * Higher numbers make the item change sooner, while lower ones make the change slower.
  53.  * You can reduce the number to 0 to stall the change indefinately, but cannot reduce below that.
  54.  *
  55.  * Using skipStep Param
  56.  *
  57.  * If you set skipStep to a number other than 1, Step-Points will NOT be calculated when players take steps in-game.
  58.  * This can be used for games that do not want to use steps to countdown the item changing.
  59.  *
  60.  * To force the count change, use the script call: $gameParty.muteStepCountdown();
  61.  * (this call can be used regardless of the skipStep param.)
  62.  */
  63.  
  64. var MalMuteableDatabaseLoad = DataManager.isDatabaseLoaded;
  65. DataManager.isDatabaseLoaded = function() {
  66.   if (!MalMuteableDatabaseLoad.call(this)) return false;
  67.   if (!DataManager._malMute_DatabaseLoaded) {
  68.     this.processMuteNotetags($dataItems);
  69.     this.processMuteNotetags($dataWeapons);
  70.     this.processMuteNotetags($dataArmors);
  71.     DataManager._malMute_DatabaseLoaded = true;
  72.   }
  73.   return true;
  74. };
  75.  
  76. DataManager.processMuteNotetags = function(group) {
  77.     for (var n = 1; n < group.length; n++) {
  78.         var obj = group[n];
  79.         if(!obj) continue;
  80.         obj.newItemSet = [];
  81.         obj.newItemSuccessEval  = [];
  82.         obj.newItemFailureEval  = [];
  83.         obj.stepPointAdjustEval = [];
  84.         var noteread = obj.note;
  85.         while(noteread.indexOf("nextId") > -1) {
  86.             var match = noteread.split("<nextId: ");
  87.             var match2 = match[1].split(">");
  88.             obj.newItemSet.push(match2[0]);
  89.             noteread = noteread.replace("<nextId: ", " ");
  90.         }
  91.         obj.newItemStepCount = -2;
  92.         if(obj.meta.newItemStepCount) obj.newItemStepCount = parseInt(obj.meta.newItemStepCount);
  93.        
  94.         var noteread = obj.note;
  95.         while(noteread.indexOf("newItemSuccessEval") > -1) 
  96.         {  
  97.             var notereg = noteread.split("<newItemSuccessEval: "); 
  98.             var match = notereg[1].split(">>>");   
  99.             obj.newItemSuccessEval.push(match[0]); 
  100.             noteread = noteread.replace("<newItemSuccessEval: ", " "); 
  101.         }
  102.         noteread = obj.note;
  103.         while(noteread.indexOf("newItemFailureEval") > -1) 
  104.         {  
  105.             var notereg = noteread.split("<newItemFailureEval: "); 
  106.             var match = notereg[1].split(">>>");   
  107.             obj.newItemFailureEval.push(match[0]); 
  108.             noteread = noteread.replace("<newItemFailureEval: ", " "); 
  109.         }
  110.         noteread = obj.note;
  111.         while(noteread.indexOf("stepPointAdjustEval") > -1)
  112.         {  
  113.             var notereg = noteread.split("<stepPointAdjustEval: ");
  114.             var match = notereg[1].split(">>>");   
  115.             obj.stepPointAdjustEval.push(match[0]);
  116.             noteread = noteread.replace("<stepPointAdjustEval: ", " ");
  117.         }
  118.     };
  119. };
  120.  
  121. var MalMuteGPInit = Game_Party.prototype.initialize
  122. Game_Party.prototype.initialize = function() {
  123.     MalMuteGPInit.call(this);
  124.     this.newItemSet = this.newItemSet || [];
  125. };
  126.  
  127. var MalMuteGPGainItem = Game_Party.prototype.gainItem
  128. Game_Party.prototype.gainItem = function(item, amount, includeEquip){
  129.     var plusAmount = amount;
  130.     var current = this.numItems(item);
  131.     var max = this.maxItems(item);
  132.     if(current + plusAmount > max && plusAmount > 0) plusAmount = max - current;
  133.     if (!$gameTemp.skipmutables) if(item.newItemStepCount != -2 && plusAmount > 0) this.addNewItemTimer(item, plusAmount);
  134.     MalMuteGPGainItem.call(this, item, amount, includeEquip);
  135. };
  136.  
  137. Game_Party.prototype.addNewItemTimer = function(item, amount) {
  138.     for (i =0; i < amount; i++) {
  139.         this.newItemSet.push([item, item.newItemStepCount]);
  140.     }
  141. };
  142.  
  143. var MalMuteGPWalk = Game_Party.prototype.onPlayerWalk;
  144. Game_Party.prototype.onPlayerWalk = function() {
  145.     MalMuteGPWalk.call(this);
  146.     var useStepCount = Number(PluginManager.parameters('MalMZMutableItems')['skipStep']);
  147.     if (useStepCount == 1) this.muteStepCountdown();
  148. };
  149.  
  150. Game_Party.prototype.muteStepCountdown = function() {
  151.     for(i = 0; i < this.newItemSet.length; i++){
  152.         var item = this.newItemSet[i][0];
  153.         if(item.itypeId !== undefined) item = $dataItems[item.id];
  154.         if(item.wtypeId !== undefined) item = $dataWeapons[item.id];
  155.         if(item.atypeId !== undefined) item = $dataArmors[item.id];
  156.         var offset = this.countOffset(item) + Number(PluginManager.parameters('MalMZMutableItems')['defaultStepReduction']);
  157.         if(offset < 0) offset = 0;
  158.         this.newItemSet[i][1] -= offset;
  159.         if(this.newItemSet[i][1] <= 0) {
  160.             if($gameParty.hasItem(item, true)) {
  161.                 this.grantMutedItem(item);
  162.             }
  163.         }
  164.        
  165.     };
  166.     this.muteCleanup();
  167. };
  168.  
  169. Game_Party.prototype.muteCleanup = function() {
  170.     var firstCount = -1;
  171.     while (firstCount != this.newItemSet.length) {
  172.         firstCount = this.newItemSet.length;
  173.         for(i = 0; i < this.newItemSet.length; i++){
  174.             var item = this.newItemSet[i][0];
  175.             if(this.newItemSet[i][1] <= 0) {
  176.                 this.newItemSet.splice(i, 1);
  177.             };
  178.         }
  179.     }
  180. };
  181.  
  182. Game_Party.prototype.countOffset = function(item) {
  183.     var set = item.stepPointAdjustEval;
  184.     var count = 0;
  185.     var item = item;
  186.     var s = $gameSwitches._data;   
  187.     var v = $gameVariables._data;
  188.     if (set.length > 0){
  189.         for(k = 0; k < set.length; k++){
  190.             count += parseInt(eval(set[k])) || 0;
  191.         };
  192.     };
  193.     return count;
  194. };
  195.  
  196. Game_Party.prototype.grantMutedItem = function(item) {
  197.     var oldItem = item;
  198.     if(!oldItem) return;
  199.     var newItem = null;
  200.     var done = false;
  201.     var set = oldItem.newItemSet;
  202.     var pick = 0;
  203.     var chance = 0;
  204.     for (i = 0; i < set.length; i++) {
  205.         chance += parseInt(set[i][2])
  206.     };
  207.     if(chance > 100) {chance = Math.randomInt(chance)} else { chance = Math.randomInt(100)};
  208.     for (i = 0; i < set.length; i++) {
  209.         if(done) continue;
  210.         var splitSet = set[i].split(",");
  211.         pick += parseInt(splitSet[2]);
  212.         if(pick > chance) {
  213.             if(splitSet[0] === "i") newItem = $dataItems[parseInt(splitSet[1])];  
  214.             if(splitSet[0] === "w") newItem = $dataWeapons[parseInt(splitSet[1])];
  215.             if(splitSet[0] === "a") newItem = $dataArmors[parseInt(splitSet[1])];
  216.             this.gainItem(newItem, 1);
  217.             this.processSuccessEvals(oldItem, newItem);
  218.             done = true;
  219.         }
  220.     }
  221.     this.loseItem(oldItem, 1, true);
  222.     if(done == false) this.processFailureEvals(oldItem);
  223. };
  224.  
  225. var MalMuteGPLoseItem = Game_Party.prototype.loseItem
  226. Game_Party.prototype.loseItem = function(item, amount, includeEquip) {
  227.     if (!$gameTemp.skipmutables) this.removeItemTimer(item, amount);
  228.     MalMuteGPLoseItem.call(this, item, amount, includeEquip);
  229. };
  230.  
  231. Game_Party.prototype.removeItemTimer = function (item, amount){
  232.     var times = amount;
  233.     for (j = 0; j < times; j++){
  234.         for(i = 0; i < this.newItemSet.length; i++){
  235.             var timerItem = this.newItemSet[i][0];
  236.             var discardItem = item;
  237.             if(timerItem.itypeId && discardItem.itypeId){
  238.                 if(timerItem.itypeId == discardItem.itypeId) {
  239.                     this.newItemSet[i][1] = -1;
  240.                     break;
  241.                 };
  242.             };
  243.             if(timerItem.wtypeId && discardItem.wtypeId){
  244.                 if(timerItem.wtypeId == discardItem.wtypeId) {
  245.                     this.newItemSet[i][1] = -1;
  246.                     break;
  247.                 };
  248.             };
  249.             if(timerItem.atypeId && discardItem.atypeId){
  250.                 if(timerItem.atypeId == discardItem.atypeId) {
  251.                     this.newItemSet[i][1] = -1;
  252.                     break;
  253.                 };
  254.             };
  255.         };
  256.     };
  257. };
  258.  
  259. Game_Party.prototype.processSuccessEvals = function (oldItem, newItem){
  260.     var evals = oldItem.newItemSuccessEval;
  261.     var oldItem = oldItem;
  262.     var newItem = newItem;
  263.     var s = $gameSwitches._data;   
  264.     var v = $gameVariables._data;  
  265.     for (var i = 0; i < evals.length; i++) {   
  266.         var SEval = evals[i];  
  267.         eval(SEval);   
  268.     }; 
  269. };
  270.  
  271. Game_Party.prototype.processFailureEvals = function (oldItem){ 
  272.     var evals = oldItem.newItemFailureEval;
  273.     var oldItem = oldItem;
  274.     var s = $gameSwitches._data;   
  275.     var v = $gameVariables._data;  
  276.     for (var i = 0; i < evals.length; i++) {   
  277.         var SEval = evals[i];  
  278.         eval(SEval);   
  279.     }; 
  280. };
  281.  
  282. Game_Actor.prototype.tradeItemWithParty = function(newItem, oldItem) {
  283.     if (newItem && !$gameParty.hasItem(newItem)) {
  284.         return false;
  285.     } else {
  286.         $gameTemp.skipmutables = true;
  287.         $gameParty.gainItem(oldItem, 1);
  288.         $gameParty.loseItem(newItem, 1);
  289.         $gameTemp.skipmutables = false;
  290.         return true;
  291.     }
  292. };
Add Comment
Please, Sign In to add comment