I was making a game with monsters that could drop multiple items with different probabilities each, and to do that I needed to use a loot table.
Eu estava fazendo um jogo com monstros que poderiam dropar vários itens com diferentes probabilidades cada, e para fazer isso eu utilizei uma loot table.
     
Before making a loot table we need I function to generate random numbers.
Antes de fazer uma Loot Table, precisamos criar uma função para gerar números aleatórios.
    int GenerateRandomInt(int min, int max) {
        // Generation New int Using Random Device As The Seed For The mt19937
        std::random_device r; 
        std::seed_seq seed{r(),r(),r(),r(),r(),r(),r(),r(),r()}; 
        std::mt19937 mt(seed);
        std::uniform_int_distribution<int32_t> dist(min, max);
        return dist(mt);
    }
Now we can create the Item class.
Agora podemos criar a classe Item.
    Class Item {
        std::string name;
        int type;

        Item();
    }
We will use a enum to choose an item type, and a struct to hold the probability of drop of the items.
Vamos usar um enum para escolher um tipo de item e um struct para manter a probabilidade de drop dos itens.
    enum ItemType {
        ITEM_1 = 0,
        ITEM_2 = 1,
        ITEM_3 = 2,
    };

    struct ItemProbability {
        ItemType type;
        int32_t weight;

        ItemProbability() = default;
        ItemProbability(ItemType type2, int32_t weight2): type(type2), weight(weight2) {}
    };
Now we create a vector of ItemProbability that will hold multiple types of items with different probabilites.
Agora criamos um vetor de ItemProbability que irá guardar vários tipos de itens com diferentes probabilidades.
    std::vector<ItemProbability> itemsType = std::vector<ItemProbability>();

    this->items.push_back({ITEM_1, 100});
    this->items.push_back({ITEM_2, 50);
    this->items.push_back({ITEM_3, 125});
Create another vector of ItemProbability that will be our loot table, and we fill the vector with ItemProbability based on the probability of the items.
Crie outro vetor de ItemProbability que erá nosso loot table, e podemos preencher com ItemProbability com base nas probabilidades dos itens.
    std::vector<ItemProbability> itemsLootTable = std::vector<ItemProbability>();

    for (auto& item : this->items) {
        for (int x = 0; x < item.weight; x++) {
            this->itemsLootTable.push_back(item);
        }
    }
Now we can using the loot table to receive a random type of item based in the drop probabilities of each item.
Agora podemos usar a loot table para receber um tipo aleatório de item com base nas probabilidades de drop de cada item.
    Item item = Item();
    item.type = this->itemsLootTable[GenerateRandomInt(0, this->itemsLootTable.size()-1)].type;

 
 
 
 
 

完了