aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/EnchantmentTableEntity.cpp
blob: 15131aa290473b83bad73ba446f1c7afe37455a0 (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
#include "stdafx.h"
#include "EnchantmentTableEntity.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.level.h"



EnchantmentTableEntity::EnchantmentTableEntity()
{
	random = new Random();

	time = 0;
	flip = 0.0f;
	oFlip = 0.0f;
	flipT = 0.0f;
	flipA = 0.0f;
	open = 0.0f;
	oOpen = 0.0f;
	rot = 0.0f;
	oRot = 0.0f;
	tRot = 0.0f;
	name = L"";
}

EnchantmentTableEntity::~EnchantmentTableEntity()
{
	delete random;
}

void EnchantmentTableEntity::save(CompoundTag *base)
{
	TileEntity::save(base);
	if (hasCustomName()) base->putString(L"CustomName", name);
}

void EnchantmentTableEntity::load(CompoundTag *base)
{
	TileEntity::load(base);
	if (base->contains(L"CustomName")) name = base->getString(L"CustomName");
}

void EnchantmentTableEntity::tick()
{
	TileEntity::tick();
	oOpen = open;
	oRot = rot;

	shared_ptr<Player> player = level->getNearestPlayer(x + 0.5f, y + 0.5f, z + 0.5f, 3);
	if (player != NULL)
	{
		double xd = player->x - (x + 0.5f);
		double zd = player->z - (z + 0.5f);

		tRot = (float) atan2(zd, xd);

		open += 0.1f;

		if (open < 0.5f || random->nextInt(40) == 0)
		{
			float old = flipT;
			do
			{
				flipT += random->nextInt(4) - random->nextInt(4);
			} while (old == flipT);
		}

	}
	else
	{
		tRot += 0.02f;
		open -= 0.1f;
	}

	while (rot >= PI)
		rot -= PI * 2;
	while (rot < -PI)
		rot += PI * 2;
	while (tRot >= PI)
		tRot -= PI * 2;
	while (tRot < -PI)
		tRot += PI * 2;
	float rotDir = tRot - rot;
	while (rotDir >= PI)
		rotDir -= PI * 2;
	while (rotDir < -PI)
		rotDir += PI * 2;

	rot += rotDir * 0.4f;

	if (open < 0) open = 0;
	if (open > 1) open = 1;

	time++;
	oFlip = flip;

	float diff = (flipT - flip) * 0.4f;
	float max = 0.2f;
	if (diff < -max) diff = -max;
	if (diff > +max) diff = +max;
	flipA += (diff - flipA) * 0.9f;

	flip = flip + flipA;
}

wstring EnchantmentTableEntity::getName()
{
	return hasCustomName() ? name : app.GetString(IDS_ENCHANT);
}

wstring EnchantmentTableEntity::getCustomName()
{
	return hasCustomName() ? name : L"";
}

bool EnchantmentTableEntity::hasCustomName()
{
	return !name.empty();
}

void EnchantmentTableEntity::setCustomName(const wstring &name)
{
	this->name = name;
}

shared_ptr<TileEntity> EnchantmentTableEntity::clone()
{
	shared_ptr<EnchantmentTableEntity> result = shared_ptr<EnchantmentTableEntity>( new EnchantmentTableEntity() );
	TileEntity::clone(result);

	result->time = time;
	result->flip = flip;
	result->oFlip = oFlip;
	result->flipT = flipT;
	result->flipA = flipA;
	result->open = open;
	result->oOpen = oOpen;
	result->rot = rot;
	result->oRot = oRot;
	result->tRot = tRot;

	return result;
}