Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // Astfgl's Loot tables
- // by Astfgl
- // Date: 08/04/2017
- // V1.1 allowed to refer to items notetags in the game Map call.
- // V1.2 allowed monster encounters and returning the loot table without the loot
- // element.
- // V1.3 allowed shops to sell loot tables that trigger instantly
- // V1.4 allowed loot table items to make several rolls
- // V1.4.1 fixed a bug causing consumables items to no longer be consumed upon use
- // V1.5 allowed loot tables to roll other loot tables
- // Free to use both commercially and non commercially.
- // Edits allowed, but the product must be released with the same license.
- // Credits required: any of Astfgl/Pierre MATEO/Pierre MATEO (Astfgl)
- // Reposts allowed.
- //=============================================================================
- /*:
- * @plugindesc v1.5; Generate loot from loot tables;
- * @author Astfgl
- * @help
- * How to make an item act as a bag of loot:
- * 1- Set item scope to none.
- * 2- Use the notetag <lootTable:[lootTable]>
- * ex: <lootTable:[[0,1,1,30],[1,1,1,30],[2,1,1,30],[3,0,500,30]]>
- * 3- If you want to display a message: use a common event with the
- * variables defined as parameters to help you display the item name
- * type id and quantity.
- * Be aware that using a common event will boot you out of the menu.
- * If not using a common event a random item will be given without
- * returning you to the map.
- * 4- If you want a bag of loot to give another item, you can add loot
- * tables like this: <lootTable2:>, <lootTable3:>,...
- * The program will roll one time on each loot table provided that way.
- *
- * How to call for random loot on the map:
- * 1- In event command: script : $gameMap.loot(lootTable,bool*,id*)
- * ex: $gameMap.loot([[0,1,1,30],[1,1,1,30],[2,1,1,30],[3,0,500,30]])
- *
- * If you use bool, the plugin will return the loot table without the
- * picked element and store it in the variable of id provided. If you
- * use true, you have to provide a variable id. If you don't you can
- * ignore them both.
- *
- * ex: $gameMap.loot([[0,1,1,30],[1,1,1,30],[2,1,1,30],[3,0,500,30]], true, 1)
- * if item 1 is picked it will return and store in variable 1:
- * [[1,1,1,30],[2,1,1,30],[3,0,500,30]]
- * now you can use $gameMap.loot($gameVariables.value(1),true,1) to continue
- * using the modified table.
- *
- * How to make a shop sell random loot and have it trigger instantly:
- * Add the following notetag to a loot table item: <LTPurchase>
- * If you buy an item with the following notetag and it's a loot table, it
- * will be opened instantly once bought.
- *
- * Loot table structure:
- * [item1, item2,...,itemN]
- * Each item is defined this way [itemType, itemId, quantity, probability]
- *
- * itemType:
- * 0 -> Weapon
- * 1 -> Armor
- * 2 -> Item
- * 3 -> Gold
- * 4 -> Monster [4, encounterId, canEscape, probability, canLose]
- * 5 -> Loot table [5,loot table, 0, probability]
- *
- * itemId: the id of the item in the database;
- *
- * quantity: how many of the item you wish to give
- *
- * probability: the probability that particular item will be picked
- * in relation to the other items. Not a percentage, unless your sum
- * of probabilities for all items is = 100.
- *
- * For monsters: encounter ID: the id of the troop you'd like to trigger in the database.
- * canEscape: true if the player can escape, false if not
- * canLose: true if the player can lose, false if not
- *
- * Ex: In event command: script
- * var lootTable = [[0,1,1,30],[1,1,1,30],[2,1,1,30],[3,0,500,30]]
- * $gameMap.loot(lootTable);
- *
- * First the plugin will sum all the probabilities (120 in this case)
- * Then it will generate a random number between 1 and the sum.
- * Let's say we roll a 45.
- * It will compare that number to the sum of probabilities step by step:
- * 45 is > 0 but >= 30 so it's not the first item that will be picked.
- * 45 is > 30 and <= 60 so falls within the sum of probabilities
- * of the first two items: item 2 will be picked.
- * 45 is not > 60 and <= 90 so it won't pick item 3;
- * 45 is not > 90 and <= 120 so it won't pick item 4;
- *
- * Item 2 is picked: type = 1, so 1 armor id 1 will be given to the party.
- *
- * If we rolled 100:
- * 100 is > 90 and <= 120 so item 4 will be picked:
- * type = 3, so 500 gold will be given to the party.
- *
- * @param ItemId
- * @desc The id of the variable where the item id will be stored. (0 = none)
- * @default 0
- *
- * @param ItemQty
- * @desc The id of the variable where the item quantity will be stored. (0 = none)
- * @default 0
- *
- * @param ItemName
- * @desc The id of the variable where the item name will be stored. (0 = none)
- * @default 0
- *
- * @param ItemType
- * @desc The id of the variable where the item type will be stored. (0 = none)
- * @default 0
- *
- * @param TypeWeapon
- * @desc The text that will be stored in the ItemType variable if the rolled item is a weapon.
- * @default Weapon
- *
- * @param TypeArmor
- * @desc The text that will be stored in the ItemType variable if the rolled item is an armor.
- * @default Armor
- *
- * @param TypeItem
- * @desc The text that will be stored in the ItemType variable if the rolled item is an item.
- * @default Item
- *
- * @param TypeGold
- * @desc The text that will be stored in the ItemType variable if the rolled item is gold.
- * @default Gold
- *
- */
- (function() {
- var parameters = PluginManager.parameters('AstfglLT');
- //LT structure [[itemType,itemId,quantity,proba],...]
- Game_Map.prototype.loot = function(lt,bool, id) {
- var bool = bool || false;
- var item = function(id) {
- return $dataItems[id].meta.lootTable
- }
- //calculate the sum of probabilities
- var sumProba = sum(lt,lt.length)
- //generate a random Number
- var randN = Math.floor(Math.random()* sumProba + 1);
- var index = 0;
- //calculate which item will be picked from the random number
- for (i = 0; i < lt.length; i++) {
- if (i === lt.length) {
- break;
- }
- if ( randN > sum(lt,i) && randN <= sum(lt, i+1) ) {
- index = i;
- break;
- }
- }
- //set variables
- var qty = lt[index][2];
- var itemId = lt[index][1];
- var itemIdVar = Number(parameters.ItemId);
- var itemQtyVar = Number(parameters.ItemQty);
- var itemNameVar = Number(parameters.ItemName);
- var itemTypeVar = Number(parameters.ItemType);
- //set display variables
- if (itemIdVar !== 0) {
- $gameVariables.setValue(itemIdVar, itemId)
- }
- if (itemQtyVar !== 0) {
- $gameVariables.setValue(itemQtyVar, qty)
- }
- //give the item and set type dependant display variables
- if (lt[index][0] === 0) { //weapon
- if (itemNameVar !== 0) {
- $gameVariables.setValue(itemNameVar, $dataWeapons[itemId].name)
- }
- if (itemTypeVar !== 0) {
- $gameVariables.setValue(itemTypeVar, parameters.TypeWeapon)
- }
- $gameParty.gainItem($dataWeapons[itemId], qty);
- } else if (lt[index][0] === 1) { //armor
- if (itemNameVar !== 0) {
- $gameVariables.setValue(itemNameVar, $dataArmors[itemId].name)
- }
- if (itemTypeVar !== 0) {
- $gameVariables.setValue(itemTypeVar, parameters.TypeArmor)
- }
- $gameParty.gainItem($dataArmors[itemId], qty);
- } else if (lt[index][0] === 2) { //item
- if (itemNameVar !== 0) {
- $gameVariables.setValue(itemNameVar, $dataItems[itemId].name)
- }
- if (itemTypeVar !== 0) {
- $gameVariables.setValue(itemTypeVar, parameters.TypeItem)
- }
- $gameParty.gainItem($dataItems[itemId], qty);
- } else if (lt[index][0] === 3) { //gold
- if (itemNameVar !== 0) {
- $gameVariables.setValue(itemNameVar, parameters.TypeGold)
- }
- if (itemTypeVar !== 0) {
- $gameVariables.setValue(itemTypeVar, parameters.TypeGold)
- }
- $gameParty.gainGold(qty);
- } else if (lt[index][0] === 4) {// monster
- BattleManager.setup(lt[index][1], lt[index][2], lt[index][4]);
- $gamePlayer.makeEncounterCount();
- SceneManager.push(Scene_Battle);
- } else if (lt[index][0] === 5) {//loot table
- $gameMap.loot(lt[index][1]);
- }
- if (bool) {
- lt.splice(index,1);
- $gameVariables.setValue(id,lt)
- }
- }
- //Sum an array from 0 to given index
- var sum = function(lt, index) {
- var num = 0;
- for (var i = 0; i < index; i++) {
- num += lt[i][3];
- }
- return num
- }
- //Game_Battler modifications to handle random loot as bags of loot.
- var _Astfgl_newGBUI = Game_Battler.prototype.useItem
- Game_Battler.prototype.useItem = function(item) {
- if (DataManager.isItem(item)) {
- if (item.meta.lootTable) {
- var lt = eval(item.meta.lootTable)
- $gameMap.loot(lt);
- $gameParty.gainItem(item, -1);
- var max = 3;
- for (var i = 2; i < max; i++) {
- if (item.meta["lootTable" + i]) {
- max += 1;
- lt = eval(item.meta["lootTable" + i]);
- $gameMap.loot(lt);
- }
- }
- } else {
- _Astfgl_newGBUI.call(this,item);
- }
- } else {
- _Astfgl_newGBUI.call(this,item);
- }
- };
- //Scene Shop modifications to make loot bags instantly trigger when sold.
- var _Astfgl_SSDB = Scene_Shop.prototype.doBuy
- Scene_Shop.prototype.doBuy = function(number) {
- if (this._item.meta.LTPurchase) {
- $gameParty.loseGold(number * this.buyingPrice());
- for (var i = 0; i < number; i++) {
- var lt = eval(this._item.meta.lootTable)
- $gameMap.loot(lt);
- var max = 3;
- for (var i = 2; i < max; i++) {
- if (this._item.meta["lootTable" + i]) {
- max += 1;
- lt = eval(this._item.meta["lootTable" + i]);
- $gameMap.loot(lt);
- }
- }
- }
- } else {
- _Astfgl_SSDB.call(this,number)
- }
- };
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement