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.
俺はそれぞれ異なる確率で複数のアイテムを落ちとす可能性があるモンスターゲームをしていました、そしてそれをすることは 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.
Loot Tableを作成する前に、乱数を生成する機能が必要です。
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.
俺達のLoot TableになるItemProbabilityの別のベクトルを作成しました、そして、アイテムの確率に基づいてアイテムプロバビリティーベクトルを埋めます。
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.
これで、Loot Tableを使用して、各項目のドロップ確率に基づいてランダムなタイプの項目を受け取ることができます。
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;
完了