feat: loot tables (#43)

self-explanatory

Reviewed-on: https://git.neolegacy.dev/neoStudiosLCE/neoLegacy/pulls/43
This commit is contained in:
Fireblade
2026-07-12 02:05:06 +01:00
parent 0449af2be6
commit 54528fac16
146 changed files with 6397 additions and 1277 deletions
+55 -60
View File
@@ -1,86 +1,81 @@
#include "stdafx.h"
#include "net.minecraft.world.level.tile.entity.h"
#include "net.minecraft.world.item.h"
#include "LootTableManager.h"
#include "WeighedRandom.h"
#include "WeighedTreasure.h"
#include <vector>
namespace
{
template <typename ContainerT>
void FillContainerFromDrops(Random *random, const std::vector<LootTableDropResult> &drops, shared_ptr<ContainerT> dest)
{
for (const LootTableDropResult &drop : drops)
{
if (drop.itemId == 0 || drop.count <= 0)
{
continue;
}
shared_ptr<ItemInstance> item = std::make_shared<ItemInstance>(drop.itemId, 1, drop.data);
if (item->getMaxStackSize() >= drop.count)
{
shared_ptr<ItemInstance> copy = item->copy();
copy->count = drop.count;
dest->setItem(random->nextInt(dest->getContainerSize()), copy);
}
else
{
// use multiple slots if possible
for (int c = 0; c < drop.count; c++)
{
shared_ptr<ItemInstance> copy = item->copy();
copy->count = 1;
dest->setItem(random->nextInt(dest->getContainerSize()), copy);
}
}
}
}
}
WeighedTreasure::WeighedTreasure(int itemId, int auxValue, int minCount, int maxCount, int weight) : WeighedRandomItem(weight)
{
this->item = std::make_shared<ItemInstance>(itemId, 1, auxValue);
this->minCount = minCount;
this->maxCount = maxCount;
this->item = std::make_shared<ItemInstance>(itemId, 1, auxValue);
this->minCount = minCount;
this->maxCount = maxCount;
}
WeighedTreasure::WeighedTreasure(shared_ptr<ItemInstance> item, int minCount, int maxCount, int weight) : WeighedRandomItem(weight)
{
this->item = item;
this->minCount = minCount;
this->maxCount = maxCount;
this->item = item;
this->minCount = minCount;
this->maxCount = maxCount;
}
void WeighedTreasure::addChestItems(Random *random, WeighedTreasureArray items, shared_ptr<Container> dest, int numRolls)
void WeighedTreasure::addChestItems(Random *random, const std::vector<LootTableDropResult> &drops, shared_ptr<Container> dest)
{
for (int r = 0; r < numRolls; r++)
{
WeighedTreasure *treasure = static_cast<WeighedTreasure *>(WeighedRandom::getRandomItem(random, *((WeighedRandomItemArray *)&items)));
int count = treasure->minCount + random->nextInt(treasure->maxCount - treasure->minCount + 1);
if (treasure->item->getMaxStackSize() >= count)
{
shared_ptr<ItemInstance> copy = treasure->item->copy();
copy->count = count;
dest->setItem(random->nextInt(dest->getContainerSize()), copy);
}
else
{
// use multiple slots
for (int c = 0; c < count; c++)
{
shared_ptr<ItemInstance> copy = treasure->item->copy();
copy->count = 1;
dest->setItem(random->nextInt(dest->getContainerSize()), copy);
}
}
}
FillContainerFromDrops(random, drops, dest);
}
void WeighedTreasure::addDispenserItems(Random *random, WeighedTreasureArray items, shared_ptr<DispenserTileEntity> dest, int numRolls)
void WeighedTreasure::addDispenserItems(Random *random, const std::vector<LootTableDropResult> &drops, shared_ptr<DispenserTileEntity> dest)
{
for (int r = 0; r < numRolls; r++)
{
WeighedTreasure *treasure = static_cast<WeighedTreasure *>(WeighedRandom::getRandomItem(random, *((WeighedRandomItemArray *)&items)));
int count = treasure->minCount + random->nextInt(treasure->maxCount - treasure->minCount + 1);
if (treasure->item->getMaxStackSize() >= count)
{
shared_ptr<ItemInstance> copy = treasure->item->copy();
copy->count = count;
dest->setItem(random->nextInt(dest->getContainerSize()), copy);
}
else
{
// use multiple slots
for (int c = 0; c < count; c++)
{
shared_ptr<ItemInstance> copy = treasure->item->copy();
copy->count = 1;
dest->setItem(random->nextInt(dest->getContainerSize()), copy);
}
}
}
FillContainerFromDrops(random, drops, dest);
}
WeighedTreasureArray WeighedTreasure::addToTreasure(WeighedTreasureArray items, WeighedTreasure *extra)
{
WeighedTreasureArray result(items.length + 1);
int i = 0;
WeighedTreasureArray result(items.length + 1);
int i = 0;
for (int j = 0; j < items.length; j++)
{
result[i++] = items[j];
}
for (int j = 0; j < items.length; j++)
{
result[i++] = items[j];
}
result[i++] = extra;
result[i++] = extra;
return result;
return result;
}