aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/SetEntityMotionPacket.cpp
blob: 20d841f172678ef6cc68b9edf5f005b30d743db4 (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
#include "stdafx.h"
#include <iostream>
#include "InputOutputStream.h"
#include "net.minecraft.world.entity.h"
#include "PacketListener.h"
#include "SetEntityMotionPacket.h"



void SetEntityMotionPacket::_init(int id, double xd, double yd, double zd)
{
	this->id = id;
	double m = 3.9;
	if (xd < -m) xd = -m;
	if (yd < -m) yd = -m;
	if (zd < -m) zd = -m;
	if (xd > m) xd = m;
	if (yd > m) yd = m;
	if (zd > m) zd = m;
	xa = static_cast<int>(xd * 8000.0);
	ya = static_cast<int>(yd * 8000.0);
	za = static_cast<int>(zd * 8000.0);
	// 4J - if we could transmit this as bytes (in 1/16 accuracy) then flag to do so
	if( ( xa >= (-128 * 16 ) ) && ( ya >= (-128 * 16 ) ) && ( za >= (-128 * 16 ) ) &&
		( xa < (128 * 16 ) ) && ( ya < (128 * 16 ) ) && ( za < (128 * 16 ) ) )
	{
		useBytes = true;
	}
	else
	{
		useBytes = false;
	}
}

SetEntityMotionPacket::SetEntityMotionPacket() 
{
	_init(0, 0.0f, 0.0f, 0.0f);
}

SetEntityMotionPacket::SetEntityMotionPacket(shared_ptr<Entity> e)
{
	_init(e->entityId, e->xd, e->yd, e->zd);
}

SetEntityMotionPacket::SetEntityMotionPacket(int id, double xd, double yd, double zd)
{
	_init(id, xd, yd, zd);   
}

void SetEntityMotionPacket::read(DataInputStream *dis) //throws IOException 
{
	short idAndFlag = dis->readShort();
	id = idAndFlag & 0x07ff;
	if( idAndFlag & 0x0800 )
	{
		xa = static_cast<int>(dis->readByte());
		ya = static_cast<int>(dis->readByte());
		za = static_cast<int>(dis->readByte());
		xa = ( xa << 24 ) >> 24;
		ya = ( ya << 24 ) >> 24;
		za = ( za << 24 ) >> 24;
		xa *= 16;
		ya *= 16;
		za *= 16;
		useBytes = true;
	}
	else
	{
		xa = dis->readShort();
		ya = dis->readShort();
		za = dis->readShort();
		useBytes = false;
	}
}

void SetEntityMotionPacket::write(DataOutputStream *dos) //throws IOException 
{
	if( useBytes )
	{
		dos->writeShort(id | 0x800);
		dos->writeByte(xa/16);
		dos->writeByte(ya/16);
		dos->writeByte(za/16);
	}
	else
	{
		dos->writeShort(id);
		dos->writeShort(xa);
		dos->writeShort(ya);
		dos->writeShort(za);
	}
}

void SetEntityMotionPacket::handle(PacketListener *listener)
{
	listener->handleSetEntityMotion(shared_from_this());
}

int SetEntityMotionPacket::getEstimatedSize()
{
	return useBytes ? 5 : 8;
}

bool SetEntityMotionPacket::canBeInvalidated()
{
	return true;
}

bool SetEntityMotionPacket::isInvalidatedBy(shared_ptr<Packet> packet)
{
	shared_ptr<SetEntityMotionPacket> target = dynamic_pointer_cast<SetEntityMotionPacket>(packet);
	return target->id == id;
}