Advertisement
Astfgl

AstfglLT

Dec 3rd, 2016
2,354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Astfgl's Loot tables
  3. // by Astfgl
  4. // Date: 08/04/2017
  5. // V1.1 allowed to refer to items notetags in the game Map call.
  6. // V1.2 allowed monster encounters and returning the loot table without the loot
  7. // element.
  8. // V1.3 allowed shops to sell loot tables that trigger instantly
  9. // V1.4 allowed loot table items to make several rolls
  10. // V1.4.1 fixed a bug causing consumables items to no longer be consumed upon use
  11. // V1.5 allowed loot tables to roll other loot tables
  12. // Free to use both commercially and non commercially.
  13. // Edits allowed, but the product must be released with the same license.
  14. // Credits required: any of Astfgl/Pierre MATEO/Pierre MATEO (Astfgl)
  15. // Reposts allowed.
  16. //=============================================================================
  17.  
  18.  
  19. /*:
  20.  * @plugindesc v1.5; Generate loot from loot tables;
  21.  * @author Astfgl
  22.  * @help
  23.  * How to make an item act as a bag of loot:
  24.  *  1- Set item scope to none.
  25.  *  2- Use the notetag <lootTable:[lootTable]>
  26.  * ex: <lootTable:[[0,1,1,30],[1,1,1,30],[2,1,1,30],[3,0,500,30]]>
  27.  *  3- If you want to display a message: use a common event with the
  28.  * variables defined as parameters to help you display the item name
  29.  * type id and quantity.
  30.  * Be aware that using a common event will boot you out of the menu.
  31.  * If not using a common event a random item will be given without
  32.  * returning you to the map.
  33.  * 4- If you want a bag of loot to give another item, you can add loot
  34.  * tables like this: <lootTable2:>, <lootTable3:>,...
  35.  * The program will roll one time on each loot table provided that way.
  36.  *
  37.  * How to call for random loot on the map:
  38.  *  1- In event command: script : $gameMap.loot(lootTable,bool*,id*)
  39.  * ex: $gameMap.loot([[0,1,1,30],[1,1,1,30],[2,1,1,30],[3,0,500,30]])
  40.  *
  41.  * If you use bool, the plugin will return the loot table without the
  42.  * picked element and store it in the variable of id provided. If you
  43.  * use true, you have to provide a variable id. If you don't you can
  44.  * ignore them both.
  45.  *
  46.  * ex: $gameMap.loot([[0,1,1,30],[1,1,1,30],[2,1,1,30],[3,0,500,30]], true, 1)
  47.  * if item 1 is picked it will return and store in variable 1:
  48.  * [[1,1,1,30],[2,1,1,30],[3,0,500,30]]
  49.  * now you can use $gameMap.loot($gameVariables.value(1),true,1) to continue
  50.  * using the modified table.
  51.  *
  52.  * How to make a shop sell random loot and have it trigger instantly:
  53.  * Add the following notetag to a loot table item: <LTPurchase>
  54.  * If you buy an item with the following notetag and it's a loot table, it
  55.  * will be opened instantly once bought.
  56.  *
  57.  * Loot table structure:
  58.  * [item1, item2,...,itemN]
  59.  * Each item is defined this way [itemType, itemId, quantity, probability]
  60.  *
  61.  * itemType:
  62.  * 0 -> Weapon
  63.  * 1 -> Armor
  64.  * 2 -> Item
  65.  * 3 -> Gold
  66.  * 4 -> Monster [4, encounterId, canEscape, probability, canLose]
  67.  * 5 -> Loot table [5,loot table, 0, probability]
  68.  *
  69.  * itemId: the id of the item in the database;
  70.  *
  71.  * quantity: how many of the item you wish to give
  72.  *
  73.  * probability: the probability that particular item will be picked
  74.  * in relation to the other items. Not a percentage, unless your sum
  75.  * of probabilities for all items is = 100.
  76.  *
  77.  * For monsters: encounter ID: the id of the troop you'd like to trigger in the database.
  78.  * canEscape: true if the player can escape, false if not
  79.  * canLose: true if the player can lose, false if not
  80.  *
  81.  * Ex: In event command: script
  82.  * var lootTable = [[0,1,1,30],[1,1,1,30],[2,1,1,30],[3,0,500,30]]
  83.  * $gameMap.loot(lootTable);
  84.  *
  85.  * First the plugin will sum all the probabilities (120 in this case)
  86.  * Then it will generate a random number between 1 and the sum.
  87.  * Let's say we roll a 45.
  88.  * It will compare that number to the sum of probabilities step by step:
  89.  * 45 is > 0 but >= 30  so it's not the first item that will be picked.
  90.  * 45 is > 30 and <= 60 so falls within the sum of probabilities
  91.  * of the first two items: item 2 will be picked.
  92.  * 45 is not > 60 and <= 90 so it won't pick item 3;
  93.  * 45 is not > 90 and <= 120 so it won't pick item 4;
  94.  *
  95.  * Item 2 is picked: type = 1, so 1 armor id 1 will be given to the party.
  96.  *
  97.  * If we rolled 100:
  98.  * 100 is > 90 and <= 120 so item 4 will be picked:
  99.  * type = 3, so 500 gold will be given to the party.
  100.  *
  101.  * @param ItemId
  102.  * @desc The id of the variable where the item id will be stored. (0 = none)
  103.  * @default 0
  104.  *
  105.  * @param ItemQty
  106.  * @desc The id of the variable where the item quantity will be stored. (0 = none)
  107.  * @default 0
  108.  *
  109.  * @param ItemName
  110.  * @desc The id of the variable where the item name will be stored. (0 = none)
  111.  * @default 0
  112.  *
  113.  * @param ItemType
  114.  * @desc The id of the variable where the item type will be stored. (0 = none)
  115.  * @default 0
  116.  *
  117.  * @param TypeWeapon
  118.  * @desc The text that will be stored in the ItemType variable if the rolled item is a weapon.
  119.  * @default Weapon
  120.  *
  121.  * @param TypeArmor
  122.  * @desc The text that will be stored in the ItemType variable if the rolled item is an armor.
  123.  * @default Armor
  124.  *
  125.  * @param TypeItem
  126.  * @desc The text that will be stored in the ItemType variable if the rolled item is an item.
  127.  * @default Item
  128.  *
  129.  * @param TypeGold
  130.  * @desc The text that will be stored in the ItemType variable if the rolled item is gold.
  131.  * @default Gold
  132.  *
  133.  */
  134.  (function() {
  135.     var parameters = PluginManager.parameters('AstfglLT');
  136.    
  137.    
  138.     //LT structure [[itemType,itemId,quantity,proba],...]
  139.     Game_Map.prototype.loot = function(lt,bool, id) {
  140.         var bool = bool || false;
  141.         var item = function(id) {
  142.             return $dataItems[id].meta.lootTable
  143.         }
  144.        
  145.         //calculate the sum of probabilities
  146.         var sumProba = sum(lt,lt.length)
  147.         //generate a random Number
  148.         var randN = Math.floor(Math.random()* sumProba + 1);
  149.         var index = 0;
  150.        
  151.         //calculate which item will be picked from the random number
  152.         for (i = 0; i < lt.length; i++) {
  153.             if (i === lt.length) {
  154.                 break;
  155.             }
  156.             if ( randN > sum(lt,i) && randN <= sum(lt, i+1) ) {
  157.                 index = i;
  158.                 break;
  159.             }
  160.         }
  161.         //set variables
  162.         var qty = lt[index][2];
  163.         var itemId = lt[index][1];
  164.         var itemIdVar = Number(parameters.ItemId);
  165.         var itemQtyVar = Number(parameters.ItemQty);
  166.         var itemNameVar = Number(parameters.ItemName);
  167.         var itemTypeVar = Number(parameters.ItemType);
  168.        
  169.         //set display variables
  170.         if (itemIdVar !== 0) {
  171.             $gameVariables.setValue(itemIdVar, itemId)
  172.         }
  173.         if (itemQtyVar !== 0) {
  174.             $gameVariables.setValue(itemQtyVar, qty)
  175.         }
  176.        
  177.         //give the item and set type dependant display variables
  178.         if (lt[index][0] === 0) { //weapon
  179.             if (itemNameVar !== 0) {
  180.                 $gameVariables.setValue(itemNameVar, $dataWeapons[itemId].name)
  181.             }
  182.             if (itemTypeVar !== 0) {
  183.                 $gameVariables.setValue(itemTypeVar, parameters.TypeWeapon)
  184.             }
  185.             $gameParty.gainItem($dataWeapons[itemId], qty);
  186.            
  187.         } else if (lt[index][0] === 1) { //armor
  188.             if (itemNameVar !== 0) {
  189.                 $gameVariables.setValue(itemNameVar, $dataArmors[itemId].name)
  190.             }
  191.             if (itemTypeVar !== 0) {
  192.                 $gameVariables.setValue(itemTypeVar, parameters.TypeArmor)
  193.             }
  194.             $gameParty.gainItem($dataArmors[itemId], qty);
  195.            
  196.         } else if (lt[index][0] === 2) { //item
  197.             if (itemNameVar !== 0) {
  198.                 $gameVariables.setValue(itemNameVar, $dataItems[itemId].name)
  199.             }
  200.             if (itemTypeVar !== 0) {
  201.                 $gameVariables.setValue(itemTypeVar, parameters.TypeItem)
  202.             }
  203.             $gameParty.gainItem($dataItems[itemId], qty);
  204.            
  205.         } else if (lt[index][0] === 3) { //gold
  206.             if (itemNameVar !== 0) {
  207.                 $gameVariables.setValue(itemNameVar, parameters.TypeGold)
  208.             }
  209.             if (itemTypeVar !== 0) {
  210.                 $gameVariables.setValue(itemTypeVar, parameters.TypeGold)
  211.             }
  212.             $gameParty.gainGold(qty);
  213.         } else if (lt[index][0] === 4) {// monster
  214.             BattleManager.setup(lt[index][1], lt[index][2], lt[index][4]);
  215.             $gamePlayer.makeEncounterCount();
  216.             SceneManager.push(Scene_Battle);
  217.         } else if (lt[index][0] === 5) {//loot table
  218.             $gameMap.loot(lt[index][1]);
  219.         }
  220.         if (bool) {
  221.             lt.splice(index,1);
  222.             $gameVariables.setValue(id,lt)
  223.         }
  224.        
  225.     }
  226.    
  227.     //Sum an array from 0 to given index
  228.     var sum = function(lt, index) {
  229.         var num = 0;
  230.         for (var i = 0; i < index; i++) {
  231.             num += lt[i][3];
  232.         }
  233.         return num
  234.     }
  235.    
  236.    
  237.     //Game_Battler modifications to handle random loot as bags of loot.
  238.     var _Astfgl_newGBUI = Game_Battler.prototype.useItem
  239.     Game_Battler.prototype.useItem = function(item) {
  240.         if (DataManager.isItem(item)) {
  241.             if (item.meta.lootTable) {
  242.                 var lt = eval(item.meta.lootTable)
  243.                 $gameMap.loot(lt);
  244.                 $gameParty.gainItem(item, -1);
  245.                 var max = 3;
  246.                 for (var i = 2; i < max; i++) {
  247.                     if (item.meta["lootTable" + i]) {
  248.                         max += 1;
  249.                         lt = eval(item.meta["lootTable" + i]);
  250.                         $gameMap.loot(lt);
  251.                     }
  252.                 }
  253.             } else {
  254.                 _Astfgl_newGBUI.call(this,item);
  255.             }
  256.         } else {
  257.             _Astfgl_newGBUI.call(this,item);
  258.         }
  259.     };
  260.    
  261.     //Scene Shop modifications to make loot bags instantly trigger when sold.
  262.     var _Astfgl_SSDB = Scene_Shop.prototype.doBuy
  263.     Scene_Shop.prototype.doBuy = function(number) {
  264.         if (this._item.meta.LTPurchase) {
  265.             $gameParty.loseGold(number * this.buyingPrice());
  266.             for (var i = 0; i < number; i++) {
  267.                 var lt = eval(this._item.meta.lootTable)
  268.                 $gameMap.loot(lt);
  269.                 var max = 3;
  270.                 for (var i = 2; i < max; i++) {
  271.                     if (this._item.meta["lootTable" + i]) {
  272.                         max += 1;
  273.                         lt = eval(this._item.meta["lootTable" + i]);
  274.                         $gameMap.loot(lt);
  275.                     }
  276.                 }
  277.             }
  278.         } else {
  279.             _Astfgl_SSDB.call(this,number)
  280.         }
  281.     };
  282.    
  283. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement