summaryrefslogtreecommitdiffstats
path: root/src/ClientHandle.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ClientHandle.cpp')
-rw-r--r--src/ClientHandle.cpp84
1 files changed, 46 insertions, 38 deletions
diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp
index 94bace43a..c4a620565 100644
--- a/src/ClientHandle.cpp
+++ b/src/ClientHandle.cpp
@@ -16,7 +16,6 @@
#include "Mobs/Monster.h"
#include "ChatColor.h"
#include "OSSupport/Socket.h"
-#include "OSSupport/Timer.h"
#include "Items/ItemHandler.h"
#include "Blocks/BlockHandler.h"
#include "Blocks/BlockSlab.h"
@@ -25,8 +24,6 @@
#include "Root.h"
#include "Protocol/Authenticator.h"
-#include "MersenneTwister.h"
-
#include "Protocol/ProtocolRecognizer.h"
#include "CompositeChat.h"
#include "Items/ItemSword.h"
@@ -41,18 +38,13 @@
/** Maximum number of block change interactions a player can perform per tick - exceeding this causes a kick */
#define MAX_BLOCK_CHANGE_INTERACTIONS 20
+/** The interval for sending pings to clients.
+Vanilla sends one ping every 1 second. */
+static const std::chrono::milliseconds PING_TIME_MS = std::chrono::milliseconds(1000);
-#define RECI_RAND_MAX (1.f/RAND_MAX)
-inline int fRadRand(MTRand & r1, int a_BlockCoord)
-{
- return a_BlockCoord * 32 + (int)(16 * ((float)r1.rand() * RECI_RAND_MAX) * 16 - 8);
-}
-
-
-
int cClientHandle::s_ClientCount = 0;
@@ -65,7 +57,8 @@ int cClientHandle::s_ClientCount = 0;
// cClientHandle:
cClientHandle::cClientHandle(const cSocket * a_Socket, int a_ViewDistance) :
- m_ViewDistance(a_ViewDistance),
+ m_CurrentViewDistance(a_ViewDistance),
+ m_RequestedViewDistance(a_ViewDistance),
m_IPString(a_Socket->GetIPString()),
m_OutgoingData(64 KiB),
m_Player(nullptr),
@@ -75,8 +68,6 @@ cClientHandle::cClientHandle(const cSocket * a_Socket, int a_ViewDistance) :
m_TimeSinceLastPacket(0),
m_Ping(1000),
m_PingID(1),
- m_PingStartTime(0),
- m_LastPingTime(1000),
m_BlockDigAnimStage(-1),
m_BlockDigAnimSpeed(0),
m_BlockDigAnimX(0),
@@ -93,15 +84,14 @@ cClientHandle::cClientHandle(const cSocket * a_Socket, int a_ViewDistance) :
m_UniqueID(0),
m_HasSentPlayerChunk(false),
m_Locale("en_GB"),
+ m_LastPlacedSign(0, -1, 0),
m_ProtocolVersion(0)
{
m_Protocol = new cProtocolRecognizer(this);
s_ClientCount++; // Not protected by CS because clients are always constructed from the same thread
m_UniqueID = s_ClientCount;
-
- cTimer t1;
- m_LastPingTime = t1.GetNowTime();
+ m_PingStartTime = std::chrono::steady_clock::now();
LOGD("New ClientHandle created at %p", this);
}
@@ -197,9 +187,13 @@ void cClientHandle::GenerateOfflineUUID(void)
AString cClientHandle::FormatChatPrefix(bool ShouldAppendChatPrefixes, AString a_ChatPrefixS, AString m_Color1, AString m_Color2)
{
if (ShouldAppendChatPrefixes)
+ {
return Printf("%s[%s] %s", m_Color1.c_str(), a_ChatPrefixS.c_str(), m_Color2.c_str());
+ }
else
+ {
return Printf("%s", m_Color1.c_str());
+ }
}
@@ -395,8 +389,7 @@ void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID,
// Delay the first ping until the client "settles down"
// This should fix #889, "BadCast exception, cannot convert bit to fm" error in client
- cTimer t1;
- m_LastPingTime = t1.GetNowTime() + 3000; // Send the first KeepAlive packet in 3 seconds
+ m_PingStartTime = std::chrono::steady_clock::now() + std::chrono::seconds(3); // Send the first KeepAlive packet in 3 seconds
cRoot::Get()->GetPluginManager()->CallHookPlayerSpawned(*m_Player);
}
@@ -430,7 +423,7 @@ bool cClientHandle::StreamNextChunk(void)
cCSLock Lock(m_CSChunkLists);
// High priority: Load the chunks that are in the view-direction of the player (with a radius of 3)
- for (int Range = 0; Range < m_ViewDistance; Range++)
+ for (int Range = 0; Range < m_CurrentViewDistance; Range++)
{
Vector3d Vector = Position + LookVector * cChunkDef::Width * Range;
@@ -447,7 +440,7 @@ bool cClientHandle::StreamNextChunk(void)
cChunkCoords Coords(ChunkX, ChunkZ);
// Checks if the chunk is in distance
- if ((Diff(ChunkX, ChunkPosX) > m_ViewDistance) || (Diff(ChunkZ, ChunkPosZ) > m_ViewDistance))
+ if ((Diff(ChunkX, ChunkPosX) > m_CurrentViewDistance) || (Diff(ChunkZ, ChunkPosZ) > m_CurrentViewDistance))
{
continue;
}
@@ -470,7 +463,7 @@ bool cClientHandle::StreamNextChunk(void)
}
// Low priority: Add all chunks that are in range. (From the center out to the edge)
- for (int d = 0; d <= m_ViewDistance; ++d) // cycle through (square) distance, from nearest to furthest
+ for (int d = 0; d <= m_CurrentViewDistance; ++d) // cycle through (square) distance, from nearest to furthest
{
// For each distance add chunks in a hollow square centered around current position:
cChunkCoordsList CurcleChunks;
@@ -528,7 +521,7 @@ void cClientHandle::UnloadOutOfRangeChunks(void)
{
int DiffX = Diff((*itr).m_ChunkX, ChunkPosX);
int DiffZ = Diff((*itr).m_ChunkZ, ChunkPosZ);
- if ((DiffX > m_ViewDistance) || (DiffZ > m_ViewDistance))
+ if ((DiffX > m_CurrentViewDistance) || (DiffZ > m_CurrentViewDistance))
{
ChunksToRemove.push_back(*itr);
itr = m_LoadedChunks.erase(itr);
@@ -543,7 +536,7 @@ void cClientHandle::UnloadOutOfRangeChunks(void)
{
int DiffX = Diff((*itr).m_ChunkX, ChunkPosX);
int DiffZ = Diff((*itr).m_ChunkZ, ChunkPosZ);
- if ((DiffX > m_ViewDistance) || (DiffZ > m_ViewDistance))
+ if ((DiffX > m_CurrentViewDistance) || (DiffZ > m_CurrentViewDistance))
{
itr = m_ChunksToSend.erase(itr);
}
@@ -1236,12 +1229,18 @@ void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, e
{
// TODO: Rewrite this function
- LOGD("HandleRightClick: {%d, %d, %d}, face %d, HeldItem: %s",
- a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, ItemToFullString(a_HeldItem).c_str()
+ // Distance from the block's center to the player's eye height
+ double dist = (Vector3d(a_BlockX, a_BlockY, a_BlockZ) + Vector3d(0.5, 0.5, 0.5) - m_Player->GetEyePosition()).Length();
+ LOGD("HandleRightClick: {%d, %d, %d}, face %d, HeldItem: %s; dist: %.02f",
+ a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, ItemToFullString(a_HeldItem).c_str(), dist
);
-
+
+ // Check the reach distance:
+ // _X 2014-11-25: I've maxed at 5.26 with a Survival client and 5.78 with a Creative client in my tests
+ double maxDist = m_Player->IsGameModeCreative() ? 5.78 : 5.26;
+ bool AreRealCoords = (dist <= maxDist);
+
cWorld * World = m_Player->GetWorld();
- bool AreRealCoords = (Vector3d(a_BlockX, a_BlockY, a_BlockZ) - m_Player->GetPosition()).Length() <= 5;
if (
(a_BlockFace != BLOCK_FACE_NONE) && // The client is interacting with a specific block
@@ -1500,6 +1499,7 @@ void cClientHandle::HandlePlaceBlock(int a_BlockX, int a_BlockY, int a_BlockZ, e
{
m_Player->GetInventory().RemoveOneEquippedItem();
}
+
cChunkInterface ChunkInterface(World->GetChunkMap());
NewBlock->OnPlacedByPlayer(ChunkInterface, *World, m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ, BlockType, BlockMeta);
@@ -1677,8 +1677,11 @@ void cClientHandle::HandleUpdateSign(
const AString & a_Line3, const AString & a_Line4
)
{
- cWorld * World = m_Player->GetWorld();
- World->UpdateSign(a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, m_Player);
+ if (m_LastPlacedSign.Equals(Vector3i(a_BlockX, a_BlockY, a_BlockZ)))
+ {
+ m_LastPlacedSign.Set(0, -1, 0);
+ m_Player->GetWorld()->SetSignLines(a_BlockX, a_BlockY, a_BlockZ, a_Line1, a_Line2, a_Line3, a_Line4, m_Player);
+ }
}
@@ -1767,8 +1770,7 @@ void cClientHandle::HandleKeepAlive(int a_KeepAliveID)
{
if (a_KeepAliveID == m_PingID)
{
- cTimer t1;
- m_Ping = (short)((t1.GetNowTime() - m_PingStartTime) / 2);
+ m_Ping = std::chrono::steady_clock::now() - m_PingStartTime;
}
}
@@ -1993,13 +1995,11 @@ void cClientHandle::Tick(float a_Dt)
// Send a ping packet:
if (m_State == csPlaying)
{
- cTimer t1;
- if ((m_LastPingTime + cClientHandle::PING_TIME_MS <= t1.GetNowTime()))
+ if ((m_PingStartTime + PING_TIME_MS <= std::chrono::steady_clock::now()))
{
m_PingID++;
- m_PingStartTime = t1.GetNowTime();
+ m_PingStartTime = std::chrono::steady_clock::now();
m_Protocol->SendKeepAlive(m_PingID);
- m_LastPingTime = m_PingStartTime;
}
}
@@ -2257,6 +2257,7 @@ void cClientHandle::SendDisconnect(const AString & a_Reason)
void cClientHandle::SendEditSign(int a_BlockX, int a_BlockY, int a_BlockZ)
{
+ m_LastPlacedSign.Set(a_BlockX, a_BlockY, a_BlockZ);
m_Protocol->SendEditSign(a_BlockX, a_BlockY, a_BlockZ);
}
@@ -2847,8 +2848,15 @@ void cClientHandle::SetUsername( const AString & a_Username)
void cClientHandle::SetViewDistance(int a_ViewDistance)
{
- m_ViewDistance = Clamp(a_ViewDistance, MIN_VIEW_DISTANCE, MAX_VIEW_DISTANCE);
- LOGD("Setted %s's view distance to %i", GetUsername().c_str(), m_ViewDistance);
+ m_RequestedViewDistance = a_ViewDistance;
+ LOGD("%s is requesting ViewDistance of %d!", GetUsername().c_str(), m_RequestedViewDistance);
+
+ // Set the current view distance based on the requested VD and world max VD:
+ cWorld * world = m_Player->GetWorld();
+ if (world != nullptr)
+ {
+ m_CurrentViewDistance = Clamp(a_ViewDistance, cClientHandle::MIN_VIEW_DISTANCE, world->GetMaxViewDistance());
+ }
}