aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/CropTile.cpp
blob: 0c19d889bb8fc8c2ca4b98cf016deda50289401d (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "stdafx.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.entity.item.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.h"
#include "CropTile.h"

CropTile::CropTile(int id) : Bush(id)
{
	setTicking(true);
	updateDefaultShape();
	icons = NULL;

	setDestroyTime(0.0f);
	setSoundType(SOUND_GRASS);
	setNotCollectStatistics();
	sendTileData();
}

// 4J Added override
void CropTile::updateDefaultShape()
{
	float ss = 0.5f;
	this->setShape(0.5f - ss, 0, 0.5f - ss, 0.5f + ss, 0.25f, 0.5f + ss);
}

bool CropTile::mayPlaceOn(int tile)
{
	return tile == Tile::farmland_Id;
}

void CropTile::tick(Level *level, int x, int y, int z, Random *random)
{
	Bush::tick(level, x, y, z, random);
	if (level->getRawBrightness(x, y + 1, z) >= Level::MAX_BRIGHTNESS - 6)
	{

		int age = level->getData(x, y, z);
		if (age < 7)
		{
			float growthSpeed = getGrowthSpeed(level, x, y, z);

            if (random->nextInt((int) (25 / growthSpeed) + 1) == 0)
			{
				age++;
				level->setData(x, y, z, age);
			}
		}
	}
}

void CropTile::growCropsToMax(Level *level, int x, int y, int z)
{
	level->setData(x, y, z, 7);
}

float CropTile::getGrowthSpeed(Level *level, int x, int y, int z)
{
	float speed = 1;

	int n = level->getTile(x, y, z - 1);
	int s = level->getTile(x, y, z + 1);
	int w = level->getTile(x - 1, y, z);
	int e = level->getTile(x + 1, y, z);

	int d0 = level->getTile(x - 1, y, z - 1);
	int d1 = level->getTile(x + 1, y, z - 1);
	int d2 = level->getTile(x + 1, y, z + 1);
	int d3 = level->getTile(x - 1, y, z + 1);

	bool horizontal = w == this->id || e == this->id;
	bool vertical = n == this->id || s == this->id;
	bool diagonal = d0 == this->id || d1 == this->id || d2 == this->id || d3 == this->id;

	for (int xx = x - 1; xx <= x + 1; xx++)
		for (int zz = z - 1; zz <= z + 1; zz++)
		{
			int t = level->getTile(xx, y - 1, zz);

			float tileSpeed = 0;
			if (t == Tile::farmland_Id)
			{
				tileSpeed = 1;
				if (level->getData(xx, y - 1, zz) > 0) tileSpeed = 3;
			}

			if (xx != x || zz != z) tileSpeed /= 4;

			speed += tileSpeed;
		}

	if (diagonal || (horizontal && vertical)) speed /= 2;

	return speed;

}

Icon *CropTile::getTexture(int face, int data)
{
	if (data < 0 || data > 7) data = 7;
	return icons[data];
}

int CropTile::getRenderShape()
{
	return Tile::SHAPE_ROWS;
}

int CropTile::getBaseSeedId()
{
	return Item::seeds_wheat_Id;
}

int CropTile::getBasePlantId()
{
	return Item::wheat_Id;
}

/**
	* Using this method instead of destroy() to determine if seeds should be
	* dropped
	*/
void CropTile::spawnResources(Level *level, int x, int y, int z, int data, float odds, int playerBonus)
{
	Bush::spawnResources(level, x, y, z, data, odds, 0);

	if (level->isClientSide)
	{
		return;
	}
	if (data >= 7) {
		int count = 3 + playerBonus;
		for (int i = 0; i < count; i++)
		{
			if (level->random->nextInt(5 * 3) > data) continue;
			popResource(level, x, y, z, shared_ptr<ItemInstance>(new ItemInstance(getBaseSeedId(), 1, 0)));
		}
	}
}

int CropTile::getResource(int data, Random *random, int playerBonusLevel)
{
	if (data == 7)
	{
		return getBasePlantId();
	}

	return getBaseSeedId();
}

int CropTile::getResourceCount(Random *random)
{
	return 1;
}

int CropTile::cloneTileId(Level *level, int x, int y, int z)
{
	return getBaseSeedId();
}

void CropTile::registerIcons(IconRegister *iconRegister)
{
	icons = new Icon*[8];

	for (int i = 0; i < 8; i++)
	{
		icons[i] = iconRegister->registerIcon(L"crops_" + _toString(i));
	}
}