aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/BonusChestFeature.cpp
blob: 6d083f5478f86bf23ab9680ec897e3c33562bc4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "stdafx.h"
#include "BonusChestFeature.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.level.tile.entity.h"
#include "WeighedTreasure.h"
#include "StructurePiece.h"

BonusChestFeature::BonusChestFeature(WeighedTreasureArray treasureList, int numRolls)	: treasureList(treasureList), numRolls(numRolls)
{
}

// 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 );
}

// 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
// chest as it doesn't necessarily find somewhere in the original java. This method is called multple times for different x,y,z round the
// spawn point and force signifies that this is the last time this will be called. In this case, just place the chest exactly where the input
// parameters requested we place it (we know this will be one tile above the top solid block of a randomn column), and then do our best to place
// any surrounding torches where appropriate.

bool BonusChestFeature::place(Level *level, Random *random, int x, int y, int z, bool force)
{
	if( !force )
	{
		int t = 0;
		while (((t = level->getTile(x, y, z)) == 0 || t == Tile::leaves_Id) && y > 1)
			y--;

		if (y < 1)
		{
			return false;
		}
		y++;
	}

	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 || ( 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 != NULL)
			{
				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;
}