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
+54 -51
View File
@@ -3,17 +3,18 @@
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.level.tile.entity.h"
#include "LootTableManager.h"
#include "WeighedTreasure.h"
#include "StructurePiece.h"
BonusChestFeature::BonusChestFeature(WeighedTreasureArray treasureList, int numRolls) : treasureList(treasureList), numRolls(numRolls)
BonusChestFeature::BonusChestFeature(const std::string &lootTableName) : lootTableName(lootTableName)
{
}
// 4J - original virtual method
bool BonusChestFeature::place(Level *level, Random *random, int x, int y, int z)
{
return place(level, random, x, y, z, false );
return place(level, random, x, y, z, false);
}
// 4J - added force parameter - trying to keep this as similar as possible to the original algorithm, but would also like it to definitely place a
@@ -24,7 +25,7 @@ bool BonusChestFeature::place(Level *level, Random *random, int x, int y, int z)
bool BonusChestFeature::place(Level *level, Random *random, int x, int y, int z, bool force)
{
// check if level is corrupted first
// check if level is corrupted first
if (level == nullptr || random == nullptr)
{
return false;
@@ -33,11 +34,11 @@ bool BonusChestFeature::place(Level *level, Random *random, int x, int y, int z,
//Will only spawn a bonus chest if the world is new and has never been saved.
if (level->isNew)
{
if( !force )
if (!force)
{
int t = 0;
while (((t = level->getTile(x, y, z)) == 0 || t == Tile::leaves_Id) && y > 1)
y--;
y--;
if (y < 1)
{
@@ -46,54 +47,56 @@ bool BonusChestFeature::place(Level *level, Random *random, int x, int y, int z,
y++;
}
for (int i = 0; i < 4; i++)
{
int x2, y2, z2;
for (int i = 0; i < 4; i++)
{
int x2, y2, z2;
if( force )
{
x2 = x;
y2 = y - 1; // 4J - the position passed in is actually two above the top solid block, as the calling function adds 1 to getTopSolidBlock, and that actually returns the block above anyway.
// this would explain why there is a while loop above here (not used in force mode) to move the y back down again, shouldn't really be needed if 1 wasn't added to the getTopSolidBlock return value.
z2 = z;
}
else
{
x2 = x + random->nextInt(4) - random->nextInt(4);
y2 = y + random->nextInt(3) - random->nextInt(3);
z2 = z + random->nextInt(4) - random->nextInt(4);
if (force)
{
x2 = x;
y2 = y - 1; // 4J - the position passed in is actually two above the top solid block, as the calling function adds 1 to getTopSolidBlock, and that actually returns the block above anyway.
// this would explain why there is a while loop above here (not used in force mode) to move the y back down again, shouldn't really be needed if 1 wasn't added to the getTopSolidBlock return value.
z2 = z;
}
else
{
x2 = x + random->nextInt(4) - random->nextInt(4);
y2 = y + random->nextInt(3) - random->nextInt(3);
z2 = z + random->nextInt(4) - random->nextInt(4);
}
if (force || (level->isEmptyTile(x2, y2, z2) && level->isTopSolidBlocking(x2, y2 - 1, z2)))
{
level->setTileAndData(x2, y2, z2, Tile::chest_Id, 0, Tile::UPDATE_CLIENTS);
shared_ptr<ChestTileEntity> chest = dynamic_pointer_cast<ChestTileEntity>(level->getTileEntity(x2, y2, z2));
if (chest != nullptr)
{
std::vector<LootTableDropResult> drops = LootTableManager::Get().ResolveDrops(
lootTableName, false, 0,
[random](int bound) { return random->nextInt(bound); });
WeighedTreasure::addChestItems(random, drops, chest);
chest->isBonusChest = true; // 4J added
}
if (level->isEmptyTile(x2 - 1, y2, z2) && level->isTopSolidBlocking(x2 - 1, y2 - 1, z2))
{
level->setTileAndData(x2 - 1, y2, z2, Tile::torch_Id, 0, Tile::UPDATE_CLIENTS);
}
if (level->isEmptyTile(x2 + 1, y2, z2) && level->isTopSolidBlocking(x2 - 1, y2 - 1, z2))
{
level->setTileAndData(x2 + 1, y2, z2, Tile::torch_Id, 0, Tile::UPDATE_CLIENTS);
}
if (level->isEmptyTile(x2, y2, z2 - 1) && level->isTopSolidBlocking(x2 - 1, y2 - 1, z2))
{
level->setTileAndData(x2, y2, z2 - 1, Tile::torch_Id, 0, Tile::UPDATE_CLIENTS);
}
if (level->isEmptyTile(x2, y2, z2 + 1) && level->isTopSolidBlocking(x2 - 1, y2 - 1, z2))
{
level->setTileAndData(x2, y2, z2 + 1, Tile::torch_Id, 0, Tile::UPDATE_CLIENTS);
}
return true;
}
}
if (force || ( level->isEmptyTile(x2, y2, z2) && level->isTopSolidBlocking(x2, y2 - 1, z2)))
{
level->setTileAndData(x2, y2, z2, Tile::chest_Id, 0, Tile::UPDATE_CLIENTS);
shared_ptr<ChestTileEntity> chest = dynamic_pointer_cast<ChestTileEntity>(level->getTileEntity(x2, y2, z2));
if (chest != nullptr)
{
WeighedTreasure::addChestItems(random, treasureList, chest, numRolls);
chest->isBonusChest = true; // 4J added
}
if (level->isEmptyTile(x2 - 1, y2, z2) && level->isTopSolidBlocking(x2 - 1, y2 - 1, z2))
{
level->setTileAndData(x2 - 1, y2, z2, Tile::torch_Id, 0, Tile::UPDATE_CLIENTS);
}
if (level->isEmptyTile(x2 + 1, y2, z2) && level->isTopSolidBlocking(x2 - 1, y2 - 1, z2))
{
level->setTileAndData(x2 + 1, y2, z2, Tile::torch_Id, 0, Tile::UPDATE_CLIENTS);
}
if (level->isEmptyTile(x2, y2, z2 - 1) && level->isTopSolidBlocking(x2 - 1, y2 - 1, z2))
{
level->setTileAndData(x2, y2, z2 - 1, Tile::torch_Id, 0, Tile::UPDATE_CLIENTS);
}
if (level->isEmptyTile(x2, y2, z2 + 1) && level->isTopSolidBlocking(x2 - 1, y2 - 1, z2))
{
level->setTileAndData(x2, y2, z2 + 1, Tile::torch_Id, 0, Tile::UPDATE_CLIENTS);
}
return true;
}
}
return false;
return false;
}
}