aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/TrackedEntity.cpp
blob: c372bdaf609096752b92346f642011001cbd42b8 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
#include "stdafx.h"
#include "TrackedEntity.h"
#include "ServerPlayer.h"
#include "PlayerConnection.h"
#include "..\Minecraft.World\Mth.h"
#include "..\Minecraft.World\net.minecraft.world.entity.h"
#include "..\Minecraft.World\net.minecraft.world.entity.item.h"
#include "..\Minecraft.World\net.minecraft.world.entity.monster.h"
#include "..\Minecraft.World\net.minecraft.world.entity.player.h"
#include "..\Minecraft.World\net.minecraft.world.entity.animal.h"
#include "..\Minecraft.World\net.minecraft.world.entity.global.h"
#include "..\Minecraft.World\net.minecraft.world.entity.projectile.h"
#include "..\Minecraft.World\net.minecraft.network.packet.h"
#include "..\Minecraft.World\net.minecraft.world.item.h"
#include "..\Minecraft.World\net.minecraft.world.level.saveddata.h"
#include "MinecraftServer.h"
#include "ServerLevel.h"
#include "PlayerList.h"
#include "EntityTracker.h"
#include "PlayerChunkMap.h"
#include <qnet.h>

TrackedEntity::TrackedEntity(std::shared_ptr<Entity> e, int range, int updateInterval, bool trackDelta)
{
	// 4J added initialisers
    xap = yap = zap = 0;
    tickCount = 0;
	xpu = ypu = zpu = 0;
    updatedPlayerVisibility = false;
    teleportDelay = 0;
	moved = false;

	this->e = e;
    this->range = range;
    this->updateInterval = updateInterval;
    this->trackDelta = trackDelta;

    xp = Mth::floor(e->x * 32);
    yp = Mth::floor(e->y * 32);
    zp = Mth::floor(e->z * 32);

    yRotp = Mth::floor(e->yRot * 256 / 360);
    xRotp = Mth::floor(e->xRot * 256 / 360);
	yHeadRotp = Mth::floor(e->getYHeadRot() * 256 / 360);
}

int c0a = 0, c0b = 0, c1a = 0, c1b = 0, c1c = 0, c2a = 0, c2b = 0;

void TrackedEntity::tick(EntityTracker *tracker, vector<std::shared_ptr<Player> > *players)
{
    moved = false;
    if (!updatedPlayerVisibility || e->distanceToSqr(xpu, ypu, zpu) > 4 * 4)
	{
        xpu = e->x;
        ypu = e->y;
        zpu = e->z;
        updatedPlayerVisibility = true;
        moved = true;
        updatePlayers(tracker, players);
    }

	if (wasRiding != e->riding)
	{
		wasRiding = e->riding;
		broadcast(std::shared_ptr<SetRidingPacket>(new SetRidingPacket(e, e->riding)));
	}

	// Moving forward  special case for item frames
	if (e->GetType()== eTYPE_ITEM_FRAME && tickCount % 10 == 0)
	{
		std::shared_ptr<ItemFrame> frame = dynamic_pointer_cast<ItemFrame> (e);
		std::shared_ptr<ItemInstance> item = frame->getItem();

		if (item != NULL && item->getItem()->id == Item::map_Id && !e->removed)
		{
 			std::shared_ptr<MapItemSavedData> data = Item::map->getSavedData(item, e->level);
 			for (AUTO_VAR(it,players->begin() ); it != players->end(); ++it)
			{
 				std::shared_ptr<ServerPlayer> player = dynamic_pointer_cast<ServerPlayer>(*it);
 				data->tickCarriedBy(player, item);

 				if (!player->removed && player->connection && player->connection->countDelayedPackets() <= 5)
 				{
 					std::shared_ptr<Packet> packet = Item::map->getUpdatePacket(item, e->level, player);
 					if (packet != NULL) player->connection->send(packet);
 				}
 			}
		}

		std::shared_ptr<SynchedEntityData> entityData = e->getEntityData();
		if (entityData->isDirty())
		{
			broadcastAndSend( std::shared_ptr<SetEntityDataPacket>( new SetEntityDataPacket(e->entityId, entityData, false) ) );
		}
	}
	else
	{
		if(e->riding == NULL)
		{
			teleportDelay++;
			if (tickCount++ % updateInterval == 0 || e->hasImpulse)
			{
				int xn = Mth::floor(this->e->x * 32.0);
				int yn = Mth::floor(this->e->y * 32.0);
				int zn = Mth::floor(this->e->z * 32.0);
				int yRotn = Mth::floor(e->yRot * 256 / 360);
				int xRotn = Mth::floor(e->xRot * 256 / 360);


				int xa = xn - xp;
				int ya = yn - yp;
				int za = zn - zp;

				std::shared_ptr<Packet> packet = nullptr;

				// 4J - this pos flag used to be set based on abs(xn) etc. but that just seems wrong
				bool pos = abs(xa) >= TOLERANCE_LEVEL || abs(ya) >= TOLERANCE_LEVEL || abs(za) >= TOLERANCE_LEVEL;
				// 4J - changed rotation to be generally sent as a delta as well as position
				int yRota = yRotn - yRotp;
				int xRota = xRotn - xRotp;
				// Keep rotation deltas in +/- 180 degree range
				while( yRota > 127 ) yRota -= 256;
				while( yRota < -128 ) yRota += 256;
				while( xRota > 127 ) xRota -= 256;
				while( xRota < -128 ) xRota += 256;

				bool rot = abs(yRota) >= TOLERANCE_LEVEL || abs(xRota) >= TOLERANCE_LEVEL;

				if (xa < -128 || xa >= 128 || ya < -128 || ya >= 128 || za < -128 || za >= 128
					// 4J Stu - I fixed the initialisation of teleportDelay in the ctor, but we managed this far without out
						// and would prefer not to have all the extra traffix so ignore it
							// 4J Stu - Fix for #9579 - GAMEPLAY: Boats with a player in them slowly sink under the water over time, and with no player in them they float into the sky.
								|| (e->GetType() == eTYPE_BOAT && teleportDelay > 20 * 20)
								)
				{
					teleportDelay = 0;
					packet = std::shared_ptr<TeleportEntityPacket>( new TeleportEntityPacket(e->entityId, xn, yn, zn, (byte) yRotn, (byte) xRotn) );
					//			printf("%d: New teleport rot %d\n",e->entityId,yRotn);
					yRotp = yRotn;
					xRotp = xRotn;
				}
				else
				{
					if (pos && rot)
					{
						// 4J If the movement is small enough, and there's no xrot, then use the new smaller packets
						if( ( xa >= -16 ) && ( xa <= 15 ) &&
							( za >= -16 ) && ( za <= 15 ) &&
							( ya >= -32 ) && ( ya <= 31 ) &&
							( xRota == 0 ))
						{
							// Clamp rotations that are too big
							if( yRota < -16 )
							{
								yRota = -16;
								yRotn = yRotp + yRota;
							}
							else if( yRota > 15 )
							{
								yRota = 15;
								yRotn = yRotp + yRota;
							}
							// 5 bits each for x & z, and 6 for y
							packet = std::shared_ptr<MoveEntityPacketSmall>( new MoveEntityPacketSmall::PosRot(e->entityId, (char) xa, (char) ya, (char) za, (char) yRota, 0 ) );
							c0a++;
						}
						else
						{
							packet = std::shared_ptr<MoveEntityPacket>( new MoveEntityPacket::PosRot(e->entityId, (char) xa, (char) ya, (char) za, (char) yRota, (char) xRota) );
							//					printf("%d: New posrot %d + %d = %d\n",e->entityId,yRotp,yRota,yRotn);
							c0b++;
						}
					}
					else if (pos)
					{
						// 4J If the movement is small enough, then use the new smaller packets
						if( ( xa >= -8 ) && ( xa <= 7 ) &&
							( za >= -8 ) && ( za <= 7 ) &&
							( ya >= -16 ) && ( ya <= 15 ) )
						{
							// 4 bits each for x & z, and 5 for y
							packet = std::shared_ptr<MoveEntityPacketSmall>( new MoveEntityPacketSmall::Pos(e->entityId, (char) xa, (char) ya, (char) za) );
							c1a++;
						}

						else if( ( xa >= -16 ) && ( xa <= 15 ) &&
							( za >= -16 ) && ( za <= 15 ) &&
							( ya >= -32 ) && ( ya <= 31 ) )
						{
							// use the packet with small packet with rotation if we can - 5 bits each for x & z, and 6 for y - still a byte less than the alternative
							packet = std::shared_ptr<MoveEntityPacketSmall>( new MoveEntityPacketSmall::PosRot(e->entityId, (char) xa, (char) ya, (char) za, 0, 0 ));
							c1b++;
						}
						else
						{
							packet = std::shared_ptr<MoveEntityPacket>( new MoveEntityPacket::Pos(e->entityId, (char) xa, (char) ya, (char) za) );
							c1c++;
						}
					}
					else if (rot)
					{
						// 4J If there's no x rotation, then use the new smaller packet type
						if( xRota == 0 )
						{
							// Clamp rotations that are too big
							if( yRota < -16 )
							{
								yRota = -16;
								yRotn = yRotp + yRota;
							}
							else if( yRota > 15 )
							{
								yRota = 15;
								yRotn = yRotp + yRota;
							}
							packet = std::shared_ptr<MoveEntityPacketSmall>( new MoveEntityPacketSmall::Rot(e->entityId, (char) yRota, 0) );
							c2a++;
						}
						else
						{
							//					printf("%d: New rot %d + %d = %d\n",e->entityId,yRotp,yRota,yRotn);
							packet = std::shared_ptr<MoveEntityPacket>( new MoveEntityPacket::Rot(e->entityId, (char) yRota, (char) xRota) );
							c2b++;
						}
					}
				}

				if (trackDelta)
				{
					double xad = e->xd - xap;
					double yad = e->yd - yap;
					double zad = e->zd - zap;

					double max = 0.02;

					double diff = xad * xad + yad * yad + zad * zad;

					if (diff > max * max || (diff > 0 && e->xd == 0 && e->yd == 0 && e->zd == 0))
					{
						xap = e->xd;
						yap = e->yd;
						zap = e->zd;
						broadcast( std::shared_ptr<SetEntityMotionPacket>( new SetEntityMotionPacket(e->entityId, xap, yap, zap) ) );
					}

				}

				if (packet != NULL)
				{
					broadcast(packet);
				}

				std::shared_ptr<SynchedEntityData> entityData = e->getEntityData();

				if (entityData->isDirty())
				{
					broadcastAndSend( std::shared_ptr<SetEntityDataPacket>( new SetEntityDataPacket(e->entityId, entityData, false) ) );
				}

				int yHeadRot = Mth::floor(e->getYHeadRot() * 256 / 360);
				if (abs(yHeadRot - yHeadRotp) >= TOLERANCE_LEVEL)
				{
					broadcast(std::shared_ptr<RotateHeadPacket>(new RotateHeadPacket(e->entityId, (byte) yHeadRot)));
					yHeadRotp = yHeadRot;
				}

				if (pos)
				{
					xp = xn;
					yp = yn;
					zp = zn;
				}
				if (rot)
				{
					yRotp = yRotn;
					xRotp = xRotn;
				}

				//		if( dynamic_pointer_cast<ServerPlayer>(e) != NULL )
				//		{
				//			printf("%d: %d + %d = %d (%f)\n",e->entityId,xRotp,xRota,xRotn,e->xRot);
				//		}
			}

		}
		else // 4J-JEV: Added: Mobs in minecarts weren't synching their invisibility.
		{
			std::shared_ptr<SynchedEntityData> entityData = e->getEntityData();
			if (entityData->isDirty())
				broadcastAndSend( std::shared_ptr<SetEntityDataPacket>( new SetEntityDataPacket(e->entityId, entityData, false) ) );
		}
		e->hasImpulse = false;
	}

    if (e->hurtMarked)
	{
        // broadcast(new AnimatePacket(e, AnimatePacket.HURT));
        broadcastAndSend( std::shared_ptr<SetEntityMotionPacket>( new SetEntityMotionPacket(e) ) );
        e->hurtMarked = false;
    }

}

void TrackedEntity::broadcast(std::shared_ptr<Packet> packet)
{
	if( Packet::canSendToAnyClient( packet ) )
	{
		// 4J-PB - due to the knockback on a player being hit, we need to send to all players, but limit the network traffic here to players that have not already had it sent to their system
		vector< std::shared_ptr<ServerPlayer> > sentTo;

		// 4J - don't send to a player we've already sent this data to that shares the same machine.
		// EntityMotionPacket used to limit themselves to sending once to each machine
		// by only sending to the primary player on each machine. This was causing trouble for split screen
		// as only the primary player would get a knockback velocity. Now these packets can be sent to any
		// player, but we try to restrict the network impact this has by not resending to the one machine

		for( AUTO_VAR(it, seenBy.begin()); it != seenBy.end(); it++ )
		{
			std::shared_ptr<ServerPlayer> player = *it;
			bool dontSend = false;
			if( sentTo.size() )
			{
				INetworkPlayer *thisPlayer =player->connection->getNetworkPlayer();
				if( thisPlayer == NULL )
				{
					dontSend = true;
				}
				else
				{
					for(unsigned int j = 0; j < sentTo.size(); j++ )
					{
						std::shared_ptr<ServerPlayer> player2 = sentTo[j];
						INetworkPlayer *otherPlayer = player2->connection->getNetworkPlayer();
						if( otherPlayer != NULL && thisPlayer->IsSameSystem(otherPlayer) )
						{
							dontSend = true;
		// #ifdef _DEBUG
		// 					std::shared_ptr<SetEntityMotionPacket> emp= dynamic_pointer_cast<SetEntityMotionPacket> (packet);
		// 					if(emp!=NULL)
		// 					{
		// 						app.DebugPrintf("Not sending this SetEntityMotionPacket to player - it's already been sent to a player on their console\n");
		// 					}
		// #endif
						}
					}
				}
			}
			if( dontSend )
			{
				continue;
			}


			(*it)->connection->send(packet);
			sentTo.push_back(player);
		}
	}
	else
	{
		// This packet hasn't got canSendToAnyClient set, so just send to everyone here, and it

		for( AUTO_VAR(it, seenBy.begin()); it != seenBy.end(); it++ )
		{
			(*it)->connection->send(packet);
		}
	}
}

void TrackedEntity::broadcastAndSend(std::shared_ptr<Packet> packet)
{
	vector< std::shared_ptr<ServerPlayer> > sentTo;
    broadcast(packet);
	std::shared_ptr<ServerPlayer> sp = dynamic_pointer_cast<ServerPlayer>(e);
	if (sp != NULL && sp->connection)
	{
        sp->connection->send(packet);
    }
}

void TrackedEntity::broadcastRemoved()
{
	for( AUTO_VAR(it, seenBy.begin()); it != seenBy.end(); it++ )
	{
		(*it)->entitiesToRemove.push_back(e->entityId);
	}
}

void TrackedEntity::removePlayer(std::shared_ptr<ServerPlayer> sp)
{
	AUTO_VAR(it, seenBy.find( sp ));
	if( it != seenBy.end() )
	{
		seenBy.erase( it );
	}
}

// 4J-JEV: Added for code reuse.
TrackedEntity::eVisibility TrackedEntity::isVisible(EntityTracker *tracker, std::shared_ptr<ServerPlayer> sp, bool forRider)
{
	// 4J Stu - We call update players when the entity has moved more than a certain amount at the start of it's tick
	// Before this call we set xpu, ypu and zpu to the entities new position, but xp,yp and zp are the old position until later in the tick.
	// Therefore we should use the new position for visibility checks
    double xd = sp->x - xpu; //xp / 32;
    double zd = sp->z - zpu; //zp / 32;

	int playersRange = range;
	if( playersRange > TRACKED_ENTITY_MINIMUM_VIEW_DISTANCE  )
	{
		playersRange -= sp->getPlayerViewDistanceModifier();
	}

	bool bVisible = xd >= -playersRange && xd <= playersRange && zd >= -playersRange && zd <= playersRange;
	bool canBeSeenBy = canBySeenBy(sp);

	// 4J - added. Try and find other players who are in the same dimension as this one and on the same machine, and extend our visibility
	// so things are consider visible to this player if they are near the other one. This is because we only send entity tracking info to
	// players who canReceiveAllPackets().
	if(!bVisible)
	{
		MinecraftServer *server = MinecraftServer::getInstance();
		INetworkPlayer *thisPlayer = sp->connection->getNetworkPlayer();
		if( thisPlayer )
		{
			for( unsigned int i = 0; i < server->getPlayers()->players.size(); i++ )
			{
				// Consider extra players, but not if they are the entity we are tracking, or the player we've been passed as input, or in another dimension
				std::shared_ptr<ServerPlayer> ep = server->getPlayers()->players[i];
				if( ep == sp ) continue;
				if( ep == e ) continue;
				if( ep->dimension != sp->dimension ) continue;

				INetworkPlayer * otherPlayer = ep->connection->getNetworkPlayer();
				if( otherPlayer != NULL && thisPlayer->IsSameSystem(otherPlayer) )
				{
					// 4J Stu - We call update players when the entity has moved more than a certain amount at the start of it's tick
					// Before this call we set xpu, ypu and zpu to the entities new position, but xp,yp and zp are the old position until later in the tick.
					// Therefore we should use the new position for visibility checks
					double xd = ep->x - xpu; //xp / 32;
					double zd = ep->z - zpu; //zp / 32;
					bVisible |= ( xd >= -playersRange && xd <= playersRange && zd >= -playersRange && zd <= playersRange );
					canBeSeenBy |= canBySeenBy(ep);
				}
			}
		}
	}

	// 4J Stu - We need to ensure that we send the mount before the rider, so check that the player has been added to the seenBy list
	if(forRider)
	{
		canBeSeenBy = canBeSeenBy && (seenBy.find(sp) != seenBy.end());
	}

	// 4J-JEV: ADDED! An entities mount has to be visible before the entity visible,
	// this is to ensure that the mount is already in the client's game when the rider is added.
	if (canBeSeenBy && bVisible && e->riding != NULL)
	{
		return tracker->getTracker(e->riding)->isVisible(tracker, sp, true);
	}
	else if (canBeSeenBy && bVisible)	return eVisibility_SeenAndVisible;
	else if (bVisible)					return eVisibility_IsVisible;
	else								return eVisibility_NotVisible;
}

void TrackedEntity::updatePlayer(EntityTracker *tracker, std::shared_ptr<ServerPlayer> sp)
{
    if (sp == e) return;

	eVisibility visibility = this->isVisible(tracker, sp);

    if (	visibility == eVisibility_SeenAndVisible
		&&	seenBy.find(sp) == seenBy.end()				)
	{
        seenBy.insert(sp);
		std::shared_ptr<Packet> packet = getAddEntityPacket();
        sp->connection->send(packet);

		xap = e->xd;
		yap = e->yd;
		zap = e->zd;

		std::shared_ptr<Player> plr = dynamic_pointer_cast<Player>(e);
		if (plr != NULL)
		{
			app.DebugPrintf( "TrackedEntity:: Player '%ls' is now visible to player '%ls', %s.\n",
				plr->name.c_str(), sp->name.c_str(),
				(e->riding==NULL?"not riding minecart":"in minecart")
				);
		}

		// 4J Stu brought forward to fix when Item Frames
		if (!e->getEntityData()->isEmpty() && !(dynamic_pointer_cast<AddMobPacket>(packet)))
		{
			sp->connection->send(std::shared_ptr<SetEntityDataPacket>( new SetEntityDataPacket(e->entityId, e->getEntityData(), true)));
		}

        if (this->trackDelta)
		{
            sp->connection->send( std::shared_ptr<SetEntityMotionPacket>( new SetEntityMotionPacket(e->entityId, e->xd, e->yd, e->zd) ) );
        }

		if (e->riding != NULL)
		{
			sp->connection->send(std::shared_ptr<SetRidingPacket>(new SetRidingPacket(e, e->riding)));
		}

        ItemInstanceArray equipped = e->getEquipmentSlots();
        if (equipped.data != NULL)
		{
            for (unsigned int i = 0; i < equipped.length; i++)
			{
                sp->connection->send( std::shared_ptr<SetEquippedItemPacket>( new SetEquippedItemPacket(e->entityId, i, equipped[i]) ) );
            }
        }

        if (dynamic_pointer_cast<Player>(e) != NULL)
		{
            std::shared_ptr<Player> spe = dynamic_pointer_cast<Player>(e);
            if (spe->isSleeping())
			{
                sp->connection->send( std::shared_ptr<EntityActionAtPositionPacket>( new EntityActionAtPositionPacket(e, EntityActionAtPositionPacket::START_SLEEP, Mth::floor(e->x), Mth::floor(e->y), Mth::floor(e->z)) ) );
            }
        }

		if (dynamic_pointer_cast<Mob>(e) != NULL)
		{
			std::shared_ptr<Mob> mob = dynamic_pointer_cast<Mob>(e);
			vector<MobEffectInstance *> *activeEffects = mob->getActiveEffects();
			for(AUTO_VAR(it, activeEffects->begin()); it != activeEffects->end(); ++it)
			{
				MobEffectInstance *effect = *it;

				sp->connection->send(std::shared_ptr<UpdateMobEffectPacket>( new UpdateMobEffectPacket(e->entityId, effect) ) );
			}
			delete activeEffects;
		}
    }
	else if (visibility == eVisibility_NotVisible)
	{
		AUTO_VAR(it, seenBy.find(sp));
        if (it != seenBy.end())
		{
            seenBy.erase(it);
            sp->entitiesToRemove.push_back(e->entityId);
        }
    }

}

bool TrackedEntity::canBySeenBy(std::shared_ptr<ServerPlayer> player)
{
	// 4J - for some reason this isn't currently working, and is causing players to not appear until we are really close to them. Not sure
	// what the conflict is between the java & our version, but removing for now as it is causing issues and we shouldn't *really* need it
	// TODO - investigate further

	return true;
//	return player->getLevel()->getChunkMap()->isPlayerIn(player, e->xChunk, e->zChunk);
}

void TrackedEntity::updatePlayers(EntityTracker *tracker, vector<std::shared_ptr<Player> > *players)
{
    for (unsigned int i = 0; i < players->size(); i++)
	{
        updatePlayer(tracker, dynamic_pointer_cast<ServerPlayer>( players->at(i) ) );
    }
}

std::shared_ptr<Packet> TrackedEntity::getAddEntityPacket()
{
	if (e->removed)
	{
		app.DebugPrintf("Fetching addPacket for removed entity - %ls\n", e->getAName().c_str());
	}

	// 4J-PB - replacing with a switch, rather than tons of ifs
	if (dynamic_pointer_cast<Creature>(e) != NULL)
	{
		yHeadRotp = Mth::floor(e->getYHeadRot() * 256 / 360);
		return std::shared_ptr<AddMobPacket>( new AddMobPacket(dynamic_pointer_cast<Mob>(e), yRotp, xRotp, xp, yp, zp, yHeadRotp) );
	}

	switch(e->GetType())
	{
	case eTYPE_ITEMENTITY:
		{
			std::shared_ptr<AddEntityPacket> packet = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::ITEM, 1, yRotp, xRotp, xp, yp, zp) );
			return packet;
		}
		break;
	case eTYPE_SERVERPLAYER:
		{
			std::shared_ptr<ServerPlayer> player = dynamic_pointer_cast<ServerPlayer>(e);
			PlayerUID xuid = INVALID_XUID;
			PlayerUID OnlineXuid = INVALID_XUID;
			if( player != NULL )
			{
				xuid = player->getXuid();
				OnlineXuid = player->getOnlineXuid();
			}
			// 4J Added yHeadRotp param to fix #102563 - TU12: Content: Gameplay: When one of the Players is idle for a few minutes his head turns 180 degrees.
			return std::shared_ptr<AddPlayerPacket>( new AddPlayerPacket(dynamic_pointer_cast<Player>(e), xuid, OnlineXuid, xp, yp, zp, yRotp, xRotp, yHeadRotp ) );
		}
		break;
	case eTYPE_MINECART:
		{
			std::shared_ptr<Minecart> minecart = dynamic_pointer_cast<Minecart>(e);
			if (minecart->type == Minecart::RIDEABLE) return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::MINECART_RIDEABLE, yRotp, xRotp, xp, yp, zp) );
			if (minecart->type == Minecart::CHEST) return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::MINECART_CHEST, yRotp, xRotp, xp, yp, zp) );
			if (minecart->type == Minecart::FURNACE) return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::MINECART_FURNACE, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_BOAT:
		{
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::BOAT, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_ENDERDRAGON:
		{
			yHeadRotp = Mth::floor(e->getYHeadRot() * 256 / 360);
			return std::shared_ptr<AddMobPacket>( new AddMobPacket(dynamic_pointer_cast<Mob>(e), yRotp, xRotp, xp, yp, zp, yHeadRotp ) );
		}
		break;
	case eTYPE_FISHINGHOOK:
		{
			std::shared_ptr<Entity> owner = dynamic_pointer_cast<FishingHook>(e)->owner;
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FISH_HOOK, owner != NULL ? owner->entityId : e->entityId, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_ARROW:
		{
			std::shared_ptr<Entity> owner = (dynamic_pointer_cast<Arrow>(e))->owner;
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::ARROW, owner != NULL ? owner->entityId : e->entityId, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_SNOWBALL:
		{
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::SNOWBALL, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_THROWNPOTION:
		{
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::THROWN_POTION, ((dynamic_pointer_cast<ThrownPotion>(e))->getPotionValue()), yRotp, xRotp, xp, yp, zp));
		}
		break;
	case eTYPE_THROWNEXPBOTTLE:
		{
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::THROWN_EXPBOTTLE, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_THROWNENDERPEARL:
		{
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::THROWN_ENDERPEARL, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_EYEOFENDERSIGNAL:
		{
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::EYEOFENDERSIGNAL, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_SMALL_FIREBALL:
		{
			std::shared_ptr<SmallFireball> fb = dynamic_pointer_cast<SmallFireball>(e);
			std::shared_ptr<AddEntityPacket> aep = nullptr;
			if (fb->owner != NULL)
			{
				aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::SMALL_FIREBALL, fb->owner->entityId, yRotp, xRotp, xp, yp, zp) );
			}
			else
			{
				aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::SMALL_FIREBALL, 0, yRotp, xRotp, xp, yp, zp) );
			}
			aep->xa = (int) (fb->xPower * 8000);
			aep->ya = (int) (fb->yPower * 8000);
			aep->za = (int) (fb->zPower * 8000);
			return aep;
		}
		break;
	case eTYPE_DRAGON_FIREBALL:
		{
			std::shared_ptr<DragonFireball> fb = dynamic_pointer_cast<DragonFireball>(e);
			std::shared_ptr<AddEntityPacket> aep = nullptr;
			if (fb->owner != NULL)
			{
				aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::DRAGON_FIRE_BALL, fb->owner->entityId, yRotp, xRotp, xp, yp, zp) );
			}
			else
			{
				aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::DRAGON_FIRE_BALL, 0, yRotp, xRotp, xp, yp, zp) );
			}
			aep->xa = (int) (fb->xPower * 8000);
			aep->ya = (int) (fb->yPower * 8000);
			aep->za = (int) (fb->zPower * 8000);
			return aep;
		}
		break;
	case eTYPE_FIREBALL:
		{
			std::shared_ptr<Fireball> fb = dynamic_pointer_cast<Fireball>(e);
			std::shared_ptr<AddEntityPacket> aep = nullptr;
			if (fb->owner != NULL)
			{
				aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FIREBALL, fb->owner->entityId, yRotp, xRotp, xp, yp, zp) );
			}
			else
			{
				aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FIREBALL, 0, yRotp, xRotp, xp, yp, zp) );
			}
			aep->xa = (int) (fb->xPower * 8000);
			aep->ya = (int) (fb->yPower * 8000);
			aep->za = (int) (fb->zPower * 8000);
			return aep;
		}
		break;
	case eTYPE_THROWNEGG:
		{
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::EGG, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_PRIMEDTNT:
		{
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::PRIMED_TNT, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_ENDER_CRYSTAL:
		{
			return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::ENDER_CRYSTAL, yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_FALLINGTILE:
		{
			std::shared_ptr<FallingTile> ft = dynamic_pointer_cast<FallingTile>(e);
            return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FALLING, ft->tile | (ft->data << 16), yRotp, xRotp, xp, yp, zp) );
		}
		break;
	case eTYPE_PAINTING:
		{
			return std::shared_ptr<AddPaintingPacket>( new AddPaintingPacket(dynamic_pointer_cast<Painting>(e)) );
		}
		break;
	case eTYPE_ITEM_FRAME:
		{
			std::shared_ptr<ItemFrame> frame = dynamic_pointer_cast<ItemFrame>(e);
			{

				int ix= (int)frame->xTile;
				int iy= (int)frame->yTile;
				int iz= (int)frame->zTile;
				app.DebugPrintf("eTYPE_ITEM_FRAME xyz %d,%d,%d\n",ix,iy,iz);
			}

			std::shared_ptr<AddEntityPacket> packet = std::shared_ptr<AddEntityPacket>(new AddEntityPacket(e, AddEntityPacket::ITEM_FRAME, frame->dir, yRotp, xRotp, xp, yp, zp));
			packet->x = Mth::floor(frame->xTile * 32.0f);
			packet->y = Mth::floor(frame->yTile * 32.0f);
			packet->z = Mth::floor(frame->zTile * 32.0f);
			return packet;
		}
		break;
	case eTYPE_EXPERIENCEORB:
		{
			return std::shared_ptr<AddExperienceOrbPacket>( new AddExperienceOrbPacket(dynamic_pointer_cast<ExperienceOrb>(e)) );
		}
		break;
	default:
		assert(false);
		break;
	}
/*
	if (e->GetType() == eTYPE_ITEMENTITY)
	{
		std::shared_ptr<ItemEntity> itemEntity = dynamic_pointer_cast<ItemEntity>(e);
        std::shared_ptr<AddItemEntityPacket> packet = std::shared_ptr<AddItemEntityPacket>( new AddItemEntityPacket(itemEntity, xp, yp, zp) );
        itemEntity->x = packet->x / 32.0;
        itemEntity->y = packet->y / 32.0;
        itemEntity->z = packet->z / 32.0;
        return packet;
    }

    if (e->GetType() == eTYPE_SERVERPLAYER )
	{
		std::shared_ptr<ServerPlayer> player = dynamic_pointer_cast<ServerPlayer>(e);
		XUID xuid = INVALID_XUID;
		XUID OnlineXuid = INVALID_XUID;
		if( player != NULL )
		{
			xuid = player->getXuid();
			OnlineXuid = player->getOnlineXuid();
		}
		return std::shared_ptr<AddPlayerPacket>( new AddPlayerPacket(dynamic_pointer_cast<Player>(e), xuid, OnlineXuid, xp, yp, zp, yRotp, xRotp ) );
    }
    if (e->GetType() == eTYPE_MINECART)
	{
        std::shared_ptr<Minecart> minecart = dynamic_pointer_cast<Minecart>(e);
        if (minecart->type == Minecart::RIDEABLE) return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::MINECART_RIDEABLE, yRotp, xRotp, xp, yp, zp) );
        if (minecart->type == Minecart::CHEST) return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::MINECART_CHEST, yRotp, xRotp, xp, yp, zp) );
        if (minecart->type == Minecart::FURNACE) return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::MINECART_FURNACE, yRotp, xRotp, xp, yp, zp) );
    }
	if (e->GetType() == eTYPE_BOAT)
	{
        return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::BOAT, yRotp, xRotp, xp, yp, zp) );
    }
    if (dynamic_pointer_cast<Creature>(e) != NULL)
	{
        return std::shared_ptr<AddMobPacket>( new AddMobPacket(dynamic_pointer_cast<Mob>(e), yRotp, xRotp, xp, yp, zp) );
    }
	if (e->GetType() == eTYPE_ENDERDRAGON)
	{
		return std::shared_ptr<AddMobPacket>( new AddMobPacket(dynamic_pointer_cast<Mob>(e), yRotp, xRotp, xp, yp, zp ) );
	}
	if (e->GetType() == eTYPE_FISHINGHOOK)
	{
		std::shared_ptr<Entity> owner = dynamic_pointer_cast<FishingHook>(e)->owner;
		return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FISH_HOOK, owner != NULL ? owner->entityId : e->entityId, yRotp, xRotp, xp, yp, zp) );
    }
    if (e->GetType() == eTYPE_ARROW)
	{
        std::shared_ptr<Entity> owner = (dynamic_pointer_cast<Arrow>(e))->owner;
        return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::ARROW, owner != NULL ? owner->entityId : e->entityId, yRotp, xRotp, xp, yp, zp) );
    }
	if (e->GetType() == eTYPE_SNOWBALL)
	{
        return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::SNOWBALL, yRotp, xRotp, xp, yp, zp) );
    }
	if (e->GetType() == eTYPE_THROWNPOTION)
	{
		return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::THROWN_POTION, ((dynamic_pointer_cast<ThrownPotion>(e))->getPotionValue()), yRotp, xRotp, xp, yp, zp));
	}
	if (e->GetType() == eTYPE_THROWNEXPBOTTLE)
	{
		return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::THROWN_EXPBOTTLE, yRotp, xRotp, xp, yp, zp) );
	}
	if (e->GetType() == eTYPE_THROWNENDERPEARL)
	{
		return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::THROWN_ENDERPEARL, yRotp, xRotp, xp, yp, zp) );
	}
	if (e->GetType() == eTYPE_EYEOFENDERSIGNAL)
	{
		return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::EYEOFENDERSIGNAL, yRotp, xRotp, xp, yp, zp) );
	}
	if (e->GetType() == eTYPE_SMALL_FIREBALL)
	{
		std::shared_ptr<SmallFireball> fb = dynamic_pointer_cast<SmallFireball>(e);
		std::shared_ptr<AddEntityPacket> aep = NULL;
		if (fb->owner != NULL)
		{
			aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::SMALL_FIREBALL, fb->owner->entityId, yRotp, xRotp, xp, yp, zp) );
		}
		else
		{
			aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::SMALL_FIREBALL, 0, yRotp, xRotp, xp, yp, zp) );
		}
		aep->xa = (int) (fb->xPower * 8000);
		aep->ya = (int) (fb->yPower * 8000);
		aep->za = (int) (fb->zPower * 8000);
		return aep;
	}
	if (e->GetType() == eTYPE_FIREBALL)
	{
        std::shared_ptr<Fireball> fb = dynamic_pointer_cast<Fireball>(e);
		std::shared_ptr<AddEntityPacket> aep = NULL;
		if (fb->owner != NULL)
		{
			aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FIREBALL, fb->owner->entityId, yRotp, xRotp, xp, yp, zp) );
		}
		else
		{
			aep = std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FIREBALL, 0, yRotp, xRotp, xp, yp, zp) );
		}
        aep->xa = (int) (fb->xPower * 8000);
        aep->ya = (int) (fb->yPower * 8000);
        aep->za = (int) (fb->zPower * 8000);
        return aep;
    }
	if (e->GetType() == eTYPE_THROWNEGG)
	{
        return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::EGG, yRotp, xRotp, xp, yp, zp) );
    }
	if (e->GetType() == eTYPE_PRIMEDTNT)
	{
        return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::PRIMED_TNT, yRotp, xRotp, xp, yp, zp) );
    }
	if (e->GetType() == eTYPE_ENDER_CRYSTAL)
	{
		return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::ENDER_CRYSTAL, yRotp, xRotp, xp, yp, zp) );
	}
	if (e->GetType() == eTYPE_FALLINGTILE)
	{
        std::shared_ptr<FallingTile> ft = dynamic_pointer_cast<FallingTile>(e);
        if (ft->tile == Tile::sand_Id) return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FALLING_SAND, yRotp, xRotp, xp, yp, zp) );
        if (ft->tile == Tile::gravel_Id) return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FALLING_GRAVEL, yRotp, xRotp, xp, yp, zp) );
		if (ft->tile == Tile::dragonEgg_Id) return std::shared_ptr<AddEntityPacket>( new AddEntityPacket(e, AddEntityPacket::FALLING_EGG, yRotp, xRotp, xp, yp, zp) );
    }
	if (e->GetType() == eTYPE_PAINTING)
	{
        return std::shared_ptr<AddPaintingPacket>( new AddPaintingPacket(dynamic_pointer_cast<Painting>(e)) );
    }
	if (e->GetType() == eTYPE_ITEM_FRAME)
	{
		std::shared_ptr<ItemFrame> frame = dynamic_pointer_cast<ItemFrame>(e);
		{

			int ix= (int)frame->xTile;
			int iy= (int)frame->yTile;
			int iz= (int)frame->zTile;
		app.DebugPrintf("eTYPE_ITEM_FRAME xyz %d,%d,%d\n",ix,iy,iz);
		}

		std::shared_ptr<AddEntityPacket> packet = std::shared_ptr<AddEntityPacket>(new AddEntityPacket(e, AddEntityPacket::ITEM_FRAME, frame->dir, yRotp, xRotp, xp, yp, zp));
		packet->x = Mth::floor(frame->xTile * 32.0f);
		packet->y = Mth::floor(frame->yTile * 32.0f);
		packet->z = Mth::floor(frame->zTile * 32.0f);
		return packet;
	}
	if (e->GetType() == eTYPE_EXPERIENCEORB)
	{
        return std::shared_ptr<AddExperienceOrbPacket>( new AddExperienceOrbPacket(dynamic_pointer_cast<ExperienceOrb>(e)) );
    }
	assert(false);
	*/
	return nullptr;
}

void TrackedEntity::clear(std::shared_ptr<ServerPlayer> sp)
{
	AUTO_VAR(it, seenBy.find(sp));
    if (it != seenBy.end())
	{
        seenBy.erase(it);
        sp->entitiesToRemove.push_back(e->entityId);
    }
}