aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/MultiPlayerLocalPlayer.cpp
blob: bb2630e5dcbac23416c53e1fb5063fcf712c0bed (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "stdafx.h"
//#include "..\Minecraft.World\JavaMath.h"
#include "MultiplayerLocalPlayer.h"
#include "ClientConnection.h"
#include "..\Minecraft.World\net.minecraft.world.level.h"
#include "..\Minecraft.World\net.minecraft.network.h"
#include "..\Minecraft.World\Mth.h"
#include "..\Minecraft.World\AABB.h"
#include "..\Minecraft.World\net.minecraft.stats.h"
#include "..\Minecraft.World\net.minecraft.world.inventory.h"
#include "..\Minecraft.World\net.minecraft.world.level.dimension.h"
#include "..\Minecraft.World\net.minecraft.world.effect.h"
#include "..\Minecraft.World\LevelData.h"
#include "..\Minecraft.World\net.minecraft.world.entity.item.h"




MultiplayerLocalPlayer::MultiplayerLocalPlayer(Minecraft *minecraft, Level *level, User *user, ClientConnection *connection) : LocalPlayer(minecraft, level, user, level->dimension->id)
{
	// 4J - added initialisers
    flashOnSetHealth = false;
	xLast = yLast1 = yLast2 = zLast = 0;
    yRotLast = xRotLast = 0;
	lastOnGround = false;
    lastSneaked = false;
	lastIdle = false;
	lastSprinting = false;
    positionReminder = 0;

    this->connection = connection;
}

bool MultiplayerLocalPlayer::hurt(DamageSource *source, int dmg)
{
	return false;
}

void MultiplayerLocalPlayer::heal(int heal)
{
}

void MultiplayerLocalPlayer::tick()
{
	// 4J Added
	// 4J-PB - changing this to a game host option ot hide gamertags
	//bool bIsisPrimaryHost=g_NetworkManager.IsHost() && (ProfileManager.GetPrimaryPad()==m_iPad);

	/*if((app.GetGameSettings(m_iPad,eGameSetting_PlayerVisibleInMap)!=0) != m_bShownOnMaps)
	{
		m_bShownOnMaps = (app.GetGameSettings(m_iPad,eGameSetting_PlayerVisibleInMap)!=0);
		if (m_bShownOnMaps) connection->send( shared_ptr<PlayerCommandPacket>( new PlayerCommandPacket(shared_from_this(), PlayerCommandPacket::SHOW_ON_MAPS) ) );
        else connection->send( shared_ptr<PlayerCommandPacket>( new PlayerCommandPacket(shared_from_this(), PlayerCommandPacket::HIDE_ON_MAPS) ) );
	}*/

	if (!level->hasChunkAt(Mth::floor(x), 0, Mth::floor(z))) return;

	double tempX = x, tempY = y, tempZ = z;
    LocalPlayer::tick();
	
	//if( !minecraft->localgameModes[m_iPad]->isTutorial() || minecraft->localgameModes[m_iPad]->getTutorial()->canMoveToPosition(tempX, tempY, tempZ, x, y, z) )
	if(minecraft->localgameModes[m_iPad]->getTutorial()->canMoveToPosition(tempX, tempY, tempZ, x, y, z))
	{		
		sendPosition();
	}
	else
	{
		//app.Debugprintf("Cannot move to position (%f, %f, %f), falling back to (%f, %f, %f)\n", x, y, z, tempX, y, tempZ);
		this->setPos(tempX, y, tempZ);
	}
}

void MultiplayerLocalPlayer::sendPosition()
{
	bool sprinting = isSprinting();
	if (sprinting != lastSprinting)
	{
		if (sprinting) connection->send(shared_ptr<PlayerCommandPacket>( new PlayerCommandPacket(shared_from_this(), PlayerCommandPacket::START_SPRINTING)));
		else connection->send(shared_ptr<PlayerCommandPacket>( new PlayerCommandPacket(shared_from_this(), PlayerCommandPacket::STOP_SPRINTING)));

		lastSprinting = sprinting;
	}

    bool sneaking = isSneaking();
    if (sneaking != lastSneaked)
	{
        if (sneaking) connection->send( shared_ptr<PlayerCommandPacket>( new PlayerCommandPacket(shared_from_this(), PlayerCommandPacket::START_SNEAKING) ) );
        else connection->send( shared_ptr<PlayerCommandPacket>( new PlayerCommandPacket(shared_from_this(), PlayerCommandPacket::STOP_SNEAKING) ) );

        lastSneaked = sneaking;
    }

	bool idle = isIdle();
	if (idle != lastIdle)
	{
		if (idle) connection->send( shared_ptr<PlayerCommandPacket>( new PlayerCommandPacket(shared_from_this(), PlayerCommandPacket::START_IDLEANIM) ) );
		else connection->send( shared_ptr<PlayerCommandPacket>( new PlayerCommandPacket(shared_from_this(), PlayerCommandPacket::STOP_IDLEANIM) ) );

		lastIdle = idle;
	}

    double xdd = x - xLast;
    double ydd1 = bb->y0 - yLast1;
    double zdd = z - zLast;

    double rydd = yRot - yRotLast;
    double rxdd = xRot - xRotLast;

	bool move = (xdd * xdd + ydd1 * ydd1 + zdd * zdd) > 0.03 * 0.03 || positionReminder >= POSITION_REMINDER_INTERVAL;
    bool rot = rydd != 0 || rxdd != 0;
    if (riding != NULL)
	{
		connection->send( shared_ptr<MovePlayerPacket>( new MovePlayerPacket::PosRot(xd, -999, -999, zd, yRot, xRot, onGround, abilities.flying) ) );
        move = false;
    }
	else
	{
        if (move && rot)
		{
            connection->send( shared_ptr<MovePlayerPacket>( new MovePlayerPacket::PosRot(x, bb->y0, y, z, yRot, xRot, onGround, abilities.flying) ) );
        }
		else if (move)
		{
            connection->send( shared_ptr<MovePlayerPacket>( new MovePlayerPacket::Pos(x, bb->y0, y, z, onGround, abilities.flying) ) );
        }
		else if (rot)
		{
            connection->send( shared_ptr<MovePlayerPacket>( new MovePlayerPacket::Rot(yRot, xRot, onGround, abilities.flying) ) );
        }
		else
		{
		  connection->send( shared_ptr<MovePlayerPacket>( new MovePlayerPacket(onGround, abilities.flying) ) );
        }
    }

	positionReminder++;
    lastOnGround = onGround;

    if (move)
	{
        xLast = x;
        yLast1 = bb->y0;
        yLast2 = y;
        zLast = z;
		positionReminder = 0;
    }
    if (rot)
	{
        yRotLast = yRot;
        xRotLast = xRot;
    }

}

shared_ptr<ItemEntity> MultiplayerLocalPlayer::drop()
{
	connection->send( shared_ptr<PlayerActionPacket>( new PlayerActionPacket(PlayerActionPacket::DROP_ITEM, 0, 0, 0, 0) ) );
	return nullptr;
}

void MultiplayerLocalPlayer::reallyDrop(shared_ptr<ItemEntity> itemEntity)
{
}

void MultiplayerLocalPlayer::chat(const wstring& message)
{
	connection->send( shared_ptr<ChatPacket>( new ChatPacket(message) ) );
}

void MultiplayerLocalPlayer::swing()
{
	LocalPlayer::swing();
	connection->send( shared_ptr<AnimatePacket>( new AnimatePacket(shared_from_this(), AnimatePacket::SWING) ) );

}

void MultiplayerLocalPlayer::respawn()
{
	connection->send( shared_ptr<ClientCommandPacket>( new ClientCommandPacket(ClientCommandPacket::PERFORM_RESPAWN)));
}


void MultiplayerLocalPlayer::actuallyHurt(DamageSource *source, int dmg)
{
	setHealth(getHealth() - dmg);
}

// 4J Added override to capture event for tutorial messages
void MultiplayerLocalPlayer::completeUsingItem()
{
	Minecraft *pMinecraft = Minecraft::GetInstance();
	if(useItem != NULL && pMinecraft->localgameModes[m_iPad] != NULL )
	{
		TutorialMode *gameMode = (TutorialMode *)pMinecraft->localgameModes[m_iPad];
		Tutorial *tutorial = gameMode->getTutorial();
		tutorial->completeUsingItem(useItem);
	}
	Player::completeUsingItem();
}

void MultiplayerLocalPlayer::onEffectAdded(MobEffectInstance *effect)
{
	Minecraft *pMinecraft = Minecraft::GetInstance();
	if(pMinecraft->localgameModes[m_iPad] != NULL )
	{
		TutorialMode *gameMode = (TutorialMode *)pMinecraft->localgameModes[m_iPad];
		Tutorial *tutorial = gameMode->getTutorial();
		tutorial->onEffectChanged(MobEffect::effects[effect->getId()]);
	}
	Player::onEffectAdded(effect);
}


void MultiplayerLocalPlayer::onEffectUpdated(MobEffectInstance *effect)
{
	Minecraft *pMinecraft = Minecraft::GetInstance();
	if(pMinecraft->localgameModes[m_iPad] != NULL )
	{
		TutorialMode *gameMode = (TutorialMode *)pMinecraft->localgameModes[m_iPad];
		Tutorial *tutorial = gameMode->getTutorial();
		tutorial->onEffectChanged(MobEffect::effects[effect->getId()]);
	}
	Player::onEffectUpdated(effect);
}


void MultiplayerLocalPlayer::onEffectRemoved(MobEffectInstance *effect)
{
	Minecraft *pMinecraft = Minecraft::GetInstance();
	if(pMinecraft->localgameModes[m_iPad] != NULL )
	{
		TutorialMode *gameMode = (TutorialMode *)pMinecraft->localgameModes[m_iPad];
		Tutorial *tutorial = gameMode->getTutorial();
		tutorial->onEffectChanged(MobEffect::effects[effect->getId()],true);
	}
	Player::onEffectRemoved(effect);
}

void MultiplayerLocalPlayer::closeContainer()
{
    connection->send( shared_ptr<ContainerClosePacket>( new ContainerClosePacket(containerMenu->containerId) ) );
    inventory->setCarried(nullptr);
    LocalPlayer::closeContainer();
}

void MultiplayerLocalPlayer::hurtTo(int newHealth, ETelemetryChallenges damageSource)
{
	if (flashOnSetHealth)
	{
		LocalPlayer::hurtTo(newHealth, damageSource);
	}
	else
	{
		setHealth(newHealth);
		flashOnSetHealth = true;
	}
}

void MultiplayerLocalPlayer::awardStat(Stat *stat, byteArray param)
{
    if (stat == NULL)
	{
		delete [] param.data;
        return;
    }

    if (stat->awardLocallyOnly)
	{
		LocalPlayer::awardStat(stat, param);
	}
	else
	{
		delete [] param.data;
        return;
    }
}

void MultiplayerLocalPlayer::awardStatFromServer(Stat *stat, byteArray param)
{
	if ( stat != NULL && !stat->awardLocallyOnly )
	{
		LocalPlayer::awardStat(stat, param);
	}
	else delete [] param.data;
}

void MultiplayerLocalPlayer::onUpdateAbilities()
{
	connection->send(shared_ptr<PlayerAbilitiesPacket>(new PlayerAbilitiesPacket(&abilities)));
}

bool MultiplayerLocalPlayer::isLocalPlayer()
{
	return true;
}

void MultiplayerLocalPlayer::ride(shared_ptr<Entity> e)
{
	bool wasRiding = riding != NULL;
	LocalPlayer::ride(e);
	bool isRiding = riding != NULL;

	if( isRiding )
	{
		ETelemetryChallenges eventType = eTelemetryChallenges_Unknown;
		if( this->riding != NULL )
		{
			switch(riding->GetType())
			{
			case eTYPE_BOAT:
				eventType = eTelemetryInGame_Ride_Boat;
				break;
			case eTYPE_MINECART:
				eventType = eTelemetryInGame_Ride_Minecart;
				break;
			case eTYPE_PIG:
				eventType = eTelemetryInGame_Ride_Pig;
				break;
			};
		}
		TelemetryManager->RecordEnemyKilledOrOvercome(GetXboxPad(), 0, y, 0, 0, 0, 0, eventType);
	}

	updateRichPresence();

	Minecraft *pMinecraft = Minecraft::GetInstance();
	
	if( pMinecraft->localgameModes[m_iPad] != NULL )
	{
		TutorialMode *gameMode = (TutorialMode *)pMinecraft->localgameModes[m_iPad];
		if(wasRiding && !isRiding)
		{
			gameMode->getTutorial()->changeTutorialState(e_Tutorial_State_Gameplay);
		}
		else if (!wasRiding && isRiding)
		{
			if(dynamic_pointer_cast<Minecart>(e) != NULL)
				gameMode->getTutorial()->changeTutorialState(e_Tutorial_State_Riding_Minecart);
			else if(dynamic_pointer_cast<Boat>(e) != NULL)
				gameMode->getTutorial()->changeTutorialState(e_Tutorial_State_Riding_Boat);
		}
	}
}

void MultiplayerLocalPlayer::StopSleeping()
{
	connection->send( shared_ptr<PlayerCommandPacket>( new PlayerCommandPacket(shared_from_this(), PlayerCommandPacket::STOP_SLEEPING) ) );
}

// 4J Added
void MultiplayerLocalPlayer::setAndBroadcastCustomSkin(DWORD skinId)
{
	DWORD oldSkinIndex = getCustomSkin();
	LocalPlayer::setCustomSkin(skinId);
#ifndef _CONTENT_PACKAGE
	wprintf(L"Skin for local player %ls has changed to %ls (%d)\n", name.c_str(), customTextureUrl.c_str(), getPlayerDefaultSkin() );
#endif
	if(getCustomSkin() != oldSkinIndex) connection->send( shared_ptr<TextureAndGeometryChangePacket>( new TextureAndGeometryChangePacket( shared_from_this(), app.GetPlayerSkinName(GetXboxPad()) ) ) );
}

void MultiplayerLocalPlayer::setAndBroadcastCustomCape(DWORD capeId)
{
	DWORD oldCapeIndex = getCustomCape();
	LocalPlayer::setCustomCape(capeId);
#ifndef _CONTENT_PACKAGE
	wprintf(L"Cape for local player %ls has changed to %ls\n", name.c_str(), customTextureUrl2.c_str());
#endif
	if(getCustomCape() != oldCapeIndex) connection->send( shared_ptr<TextureChangePacket>( new TextureChangePacket( shared_from_this(), TextureChangePacket::e_TextureChange_Cape, app.GetPlayerCapeName(GetXboxPad()) ) ) );
}