This is a continuation of my previous post, here, where I went over the design side of my Loot Tables. This post will go over the actual code I am using to implement the logic from the last post.
Code
The first thing we need are our variables: our List of entries, an int to store the total of all the weights, a List to hold the weight converted to percentages, and a List to hold the cumulative percentages that we check against.private List<LootTableEntry> lootEntries = new List<LootTableEntry>(); //The sum of all of the entries' weights private int totalWeights = 0; //Item weights as overall percentages private List<int> weightPercentages = new List<int>(); //The cumulative percentage table to check against private List<int> cumulativePercentages = new List<int>();
protected void AddNewEntry(int probability, params BaseItem[] items) { lootEntries.Add(new LootTableEntry(probability, new List<BaseItem>(items))); }
using UnityEngine; using System.Collections; using System.Collections.Generic; public class LootTableEntry { public List<BaseItem> Items = new List<BaseItem>(); public int Weight = 0; public LootTableEntry(int weight, List<BaseItem> items) { Weight = weight; Items = items; } }
protected void InitTable() { GetTotalWeights(); GetWeightPercentages(); GetCumulativePercentages(); }
private void GetTotalWeights() { totalWeights = 0; //Add all of the items' Weights together foreach (LootTableEntry entry in lootEntries) { totalWeights += entry.Weight; } }
private void GetWeightPercentages() { weightPercentages.Clear(); for (int i = 0; i < lootEntries.Count; i++ ) { //Get the probability of the item based upon the total probability int percent = Mathf.RoundToInt(((float)lootEntries[i].Weight / (float)totalWeights) * 100); weightPercentages.Add(percent); } }
private void GetCumulativePercentages() { cumulativePercentages.Clear(); int cumulativePercent = 0; cumulativePercentages.Add(cumulativePercent); for (int i = 0; i < weightPercentages.Count; i++) { //Add the new item's probability cumulativePercent += weightPercentages[i]; //add the new percent as the key, and the index of the associated entry as the value cumulativePercentages.Add(cumulativePercent); } }
using UnityEngine; using System.Collections; public class LT_Test : BaseLootTable { public LT_Test() { AddNewEntry(5, new AT_SimpleClothJerkin(), new AHD_SimpleLeatherCap()); AddNewEntry(15, new WM_SimpleLongSword()); AddNewEntry(60, null); InitTable(); } }
public List<BaseItem> GetLoot() { int check = Random.Range(1, 101); for (int i = 0; i < cumulativePercentages.Count - 1; i++) { //Check if the random number we generated is between this set of values if (check > cumulativePercentages[i] && check <= cumulativePercentages[i + 1]) { return lootEntries[i].Items; } } return new List<BaseItem>(); }
using UnityEngine; using System.Collections; using System.Collections.Generic; public class BaseLootTable { private List<LootTableEntry> lootEntries = new List<LootTableEntry>(); //The sum of all of the entries' weights private int totalWeights = 0; //Item weights as overall percentages private List<int> weightPercentages = new List<int>(); //The cumulative percentage table to check against private List<int> cumulativePercentages = new List<int>(); protected void InitTable() { GetTotalWeights(); GetWeightPercentages(); GetCumulativePercentages(); } public List<BaseItem> GetLoot() { int check = Random.Range(1, 101); for (int i = 0; i < cumulativePercentages.Count - 1; i++) { //Check if the random number we generated is between this set of values if (check > cumulativePercentages[i] && check <= cumulativePercentages[i + 1]) { return lootEntries[i].Items; } } return new List<BaseItem>(); } private void GetTotalWeights() { totalWeights = 0; //Add all of the items' Weights together foreach (LootTableEntry entry in lootEntries) { totalWeights += entry.Weight; } } private void GetWeightPercentages() { weightPercentages.Clear(); for (int i = 0; i < lootEntries.Count; i++ ) { //Get the probability of the item based upon the total probability int percent = Mathf.RoundToInt(((float)lootEntries[i].Weight / (float)totalWeights) * 100); weightPercentages.Add(percent); } } private void GetCumulativePercentages() { cumulativePercentages.Clear(); int cumulativePercent = 0; cumulativePercentages.Add(cumulativePercent); for (int i = 0; i < weightPercentages.Count; i++) { //Add the new item's probability cumulativePercent += weightPercentages[i]; //add the new percent as the key, and the index of the associated entry as the value cumulativePercentages.Add(cumulativePercent); } } protected void AddNewEntry(int probability, params BaseItem[] items) { lootEntries.Add(new LootTableEntry(probability, new List<BaseItem>(items))); } }