From e6786074d58d81a230a6d5d01a5d89fa2f5ab16e Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 00:27:42 +0200 Subject: Added cBufferedSslContext implementation. --- src/ByteBuffer.h | 40 +++++++++++++++++------------------ src/PolarSSL++/BufferedSslContext.cpp | 31 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/ByteBuffer.h b/src/ByteBuffer.h index 7656a5b13..c5ff05922 100644 --- a/src/ByteBuffer.h +++ b/src/ByteBuffer.h @@ -30,25 +30,25 @@ public: cByteBuffer(size_t a_BufferSize); ~cByteBuffer(); - /// Writes the bytes specified to the ringbuffer. Returns true if successful, false if not + /** Writes the bytes specified to the ringbuffer. Returns true if successful, false if not */ bool Write(const void * a_Bytes, size_t a_Count); - /// Returns the number of bytes that can be successfully written to the ringbuffer + /** Returns the number of bytes that can be successfully written to the ringbuffer */ size_t GetFreeSpace(void) const; - /// Returns the number of bytes that are currently in the ringbuffer. Note GetReadableBytes() + /** Returns the number of bytes that are currently in the ringbuffer. Note GetReadableBytes() */ size_t GetUsedSpace(void) const; - /// Returns the number of bytes that are currently available for reading (may be less than UsedSpace due to some data having been read already) + /** Returns the number of bytes that are currently available for reading (may be less than UsedSpace due to some data having been read already) */ size_t GetReadableSpace(void) const; - /// Returns the current data start index. For debugging purposes. + /** Returns the current data start index. For debugging purposes. */ size_t GetDataStart(void) const { return m_DataStart; } - /// Returns true if the specified amount of bytes are available for reading + /** Returns true if the specified amount of bytes are available for reading */ bool CanReadBytes(size_t a_Count) const; - /// Returns true if the specified amount of bytes are available for writing + /** Returns true if the specified amount of bytes are available for writing */ bool CanWriteBytes(size_t a_Count) const; // Read the specified datatype and advance the read pointer; return true if successfully read: @@ -65,7 +65,7 @@ public: bool ReadVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8 bool ReadLEInt (int & a_Value); - /// Reads VarInt, assigns it to anything that can be assigned from an UInt32 (unsigned short, char, Byte, double, ...) + /** Reads VarInt, assigns it to anything that can be assigned from an UInt32 (unsigned short, char, Byte, double, ...) */ template bool ReadVarInt(T & a_Value) { UInt32 v; @@ -91,37 +91,37 @@ public: bool WriteVarUTF8String (const AString & a_Value); // string length as VarInt, then string as UTF-8 bool WriteLEInt (int a_Value); - /// Reads a_Count bytes into a_Buffer; returns true if successful + /** Reads a_Count bytes into a_Buffer; returns true if successful */ bool ReadBuf(void * a_Buffer, size_t a_Count); - /// Writes a_Count bytes into a_Buffer; returns true if successful + /** Writes a_Count bytes into a_Buffer; returns true if successful */ bool WriteBuf(const void * a_Buffer, size_t a_Count); - /// Reads a_Count bytes into a_String; returns true if successful + /** Reads a_Count bytes into a_String; returns true if successful */ bool ReadString(AString & a_String, size_t a_Count); - /// Reads 2 * a_NumChars bytes and interprets it as a UTF16-BE string, converting it into UTF8 string a_String + /** Reads 2 * a_NumChars bytes and interprets it as a UTF16-BE string, converting it into UTF8 string a_String */ bool ReadUTF16String(AString & a_String, int a_NumChars); - /// Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer + /** Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer */ bool SkipRead(size_t a_Count); - /// Reads all available data into a_Data + /** Reads all available data into a_Data */ void ReadAll(AString & a_Data); - /// Reads the specified number of bytes and writes it into the destinatio bytebuffer. Returns true on success. + /** Reads the specified number of bytes and writes it into the destinatio bytebuffer. Returns true on success. */ bool ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes); - /// Removes the bytes that have been read from the ringbuffer + /** Removes the bytes that have been read from the ringbuffer */ void CommitRead(void); - /// Restarts next reading operation at the start of the ringbuffer + /** Restarts next reading operation at the start of the ringbuffer */ void ResetRead(void); - /// Re-reads the data that has been read since the last commit to the current readpos. Used by ProtoProxy to duplicate communication + /** Re-reads the data that has been read since the last commit to the current readpos. Used by ProtoProxy to duplicate communication */ void ReadAgain(AString & a_Out); - /// Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs + /** Checks if the internal state is valid (read and write positions in the correct bounds) using ASSERTs */ void CheckValid(void) const; protected: @@ -136,7 +136,7 @@ protected: size_t m_WritePos; // Where the data ends in the ringbuffer size_t m_ReadPos; // Where the next read will start in the ringbuffer - /// Advances the m_ReadPos by a_Count bytes + /** Advances the m_ReadPos by a_Count bytes */ void AdvanceReadPos(size_t a_Count); } ; diff --git a/src/PolarSSL++/BufferedSslContext.cpp b/src/PolarSSL++/BufferedSslContext.cpp index 885b30c68..2455d5781 100644 --- a/src/PolarSSL++/BufferedSslContext.cpp +++ b/src/PolarSSL++/BufferedSslContext.cpp @@ -20,6 +20,37 @@ cBufferedSslContext::cBufferedSslContext(size_t a_BufferSize): +size_t cBufferedSslContext::WriteIncoming(const void * a_Data, size_t a_NumBytes) +{ + size_t NumBytes = std::min(m_IncomingData.GetFreeSpace(), a_NumBytes); + if (NumBytes > 0) + { + m_IncomingData.Write(a_Data, NumBytes); + return a_NumBytes - NumBytes; + } + return 0; +} + + + + + +size_t cBufferedSslContext::ReadOutgoing(void * a_Data, size_t a_DataMaxSize) +{ + size_t NumBytes = std::min(m_OutgoingData.GetReadableSpace(), a_DataMaxSize); + if (NumBytes > 0) + { + m_OutgoingData.ReadBuf(a_Data, NumBytes); + m_OutgoingData.CommitRead(); + return a_DataMaxSize - NumBytes; + } + return 0; +} + + + + + int cBufferedSslContext::ReceiveEncrypted(unsigned char * a_Buffer, size_t a_NumBytes) { // Called when PolarSSL wants to read encrypted data from the SSL peer -- cgit v1.2.3 From 47feb91e57f83c81722188ec3025c3109758dd33 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 00:28:27 +0200 Subject: cSslContext supports setting own cert / privkey. --- src/PolarSSL++/PublicKey.cpp | 82 ++++++++++++++++++++++++++++++++++++++++-- src/PolarSSL++/PublicKey.h | 32 +++++++++++++++-- src/PolarSSL++/RsaPrivateKey.h | 8 +++++ src/PolarSSL++/SslContext.cpp | 50 +++++++++++++++++++++++++- src/PolarSSL++/SslContext.h | 22 +++++++++++- 5 files changed, 187 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/PolarSSL++/PublicKey.cpp b/src/PolarSSL++/PublicKey.cpp index 49794a0c8..dae026082 100644 --- a/src/PolarSSL++/PublicKey.cpp +++ b/src/PolarSSL++/PublicKey.cpp @@ -10,15 +10,44 @@ -cPublicKey::cPublicKey(const AString & a_PublicKeyDER) +cPublicKey::cPublicKey(void) { pk_init(&m_Pk); - if (pk_parse_public_key(&m_Pk, (const Byte *)a_PublicKeyDER.data(), a_PublicKeyDER.size()) != 0) + m_CtrDrbg.Initialize("rsa_pubkey", 10); +} + + + + + +cPublicKey::cPublicKey(const AString & a_PublicKeyData) +{ + pk_init(&m_Pk); + m_CtrDrbg.Initialize("rsa_pubkey", 10); + int res = ParsePublic(a_PublicKeyData.data(), a_PublicKeyData.size()); + if (res != 0) { + LOGWARNING("Failed to parse public key: -0x%x", res); + ASSERT(!"Cannot parse PubKey"); + return; + } +} + + + + + +cPublicKey::cPublicKey(const AString & a_PrivateKeyData, const AString & a_Password) +{ + pk_init(&m_Pk); + m_CtrDrbg.Initialize("rsa_privkey", 11); + int res = ParsePrivate(a_PrivateKeyData.data(), a_PrivateKeyData.size(), a_Password); + if (res != 0) + { + LOGWARNING("Failed to parse private key: -0x%x", res); ASSERT(!"Cannot parse PubKey"); return; } - m_CtrDrbg.Initialize("rsa_pubkey", 10); } @@ -36,6 +65,8 @@ cPublicKey::~cPublicKey() int cPublicKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength) { + ASSERT(IsValid()); + size_t DecryptedLen = a_DecryptedMaxLength; int res = pk_decrypt(&m_Pk, a_EncryptedData, a_EncryptedLength, @@ -55,6 +86,8 @@ int cPublicKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, int cPublicKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength) { + ASSERT(IsValid()); + size_t EncryptedLength = a_EncryptedMaxLength; int res = pk_encrypt(&m_Pk, a_PlainData, a_PlainLength, a_EncryptedData, &EncryptedLength, a_EncryptedMaxLength, @@ -71,3 +104,46 @@ int cPublicKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a + +int cPublicKey::ParsePublic(const void * a_Data, size_t a_NumBytes) +{ + ASSERT(!IsValid()); // Cannot parse a second key + + return pk_parse_public_key(&m_Pk, (const unsigned char *)a_Data, a_NumBytes); +} + + + + + + +int cPublicKey::ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password) +{ + ASSERT(!IsValid()); // Cannot parse a second key + + if (a_Password.empty()) + { + return pk_parse_key(&m_Pk, (const unsigned char *)a_Data, a_NumBytes, NULL, 0); + } + else + { + return pk_parse_key( + &m_Pk, + (const unsigned char *)a_Data, a_NumBytes, + (const unsigned char *)a_Password.c_str(), a_Password.size() + ); + } +} + + + + + +bool cPublicKey::IsValid(void) const +{ + return (pk_get_type(&m_Pk) != POLARSSL_PK_NONE); +} + + + + diff --git a/src/PolarSSL++/PublicKey.h b/src/PolarSSL++/PublicKey.h index 5a0a57147..df52a4143 100644 --- a/src/PolarSSL++/PublicKey.h +++ b/src/PolarSSL++/PublicKey.h @@ -18,9 +18,18 @@ class cPublicKey { + friend class cSslContext; + public: - /** Constructs the public key out of the DER-encoded pubkey data */ - cPublicKey(const AString & a_PublicKeyDER); + /** Constructs an empty key instance. Before use, it needs to be filled by ParsePublic() or ParsePrivate() */ + cPublicKey(void); + + /** Constructs the public key out of the DER- or PEM-encoded pubkey data */ + cPublicKey(const AString & a_PublicKeyData); + + /** Constructs the private key out of the DER- or PEM-encoded privkey data, with the specified password. + If a_Password is empty, no password is assumed. */ + cPublicKey(const AString & a_PrivateKeyData, const AString & a_Password); ~cPublicKey(); @@ -33,6 +42,20 @@ public: Both a_EncryptedData and a_DecryptedData must be at least bytes large. Returns the number of bytes decrypted, or negative number for error. */ int Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength); + + /** Parses the specified data into a public key representation. + The key can be DER- or PEM-encoded. + Returns 0 on success, PolarSSL error code on failure. */ + int ParsePublic(const void * a_Data, size_t a_NumBytes); + + /** Parses the specified data into a private key representation. + If a_Password is empty, no password is assumed. + The key can be DER- or PEM-encoded. + Returns 0 on success, PolarSSL error code on failure. */ + int ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password); + + /** Returns true if the contained key is valid. */ + bool IsValid(void) const; protected: /** The public key PolarSSL representation */ @@ -40,8 +63,13 @@ protected: /** The random generator used in encryption and decryption */ cCtrDrbgContext m_CtrDrbg; + + + /** Returns the internal context ptr. Only use in PolarSSL API calls. */ + pk_context * GetInternal(void) { return &m_Pk; } } ; +typedef SharedPtr cPublicKeyPtr; diff --git a/src/PolarSSL++/RsaPrivateKey.h b/src/PolarSSL++/RsaPrivateKey.h index ffacde11b..4d03f27df 100644 --- a/src/PolarSSL++/RsaPrivateKey.h +++ b/src/PolarSSL++/RsaPrivateKey.h @@ -19,6 +19,8 @@ /** Encapsulates an RSA private key used in PKI cryptography */ class cRsaPrivateKey { + friend class cSslContext; + public: /** Creates a new empty object, the key is not assigned */ cRsaPrivateKey(void); @@ -51,8 +53,14 @@ protected: /** The random generator used for generating the key and encryption / decryption */ cCtrDrbgContext m_CtrDrbg; + + + /** Returns the internal context ptr. Only use in PolarSSL API calls. */ + rsa_context * GetInternal(void) { return &m_Rsa; } } ; +typedef SharedPtr cRsaPrivateKeyPtr; + diff --git a/src/PolarSSL++/SslContext.cpp b/src/PolarSSL++/SslContext.cpp index 1994cf844..3d2b8cef7 100644 --- a/src/PolarSSL++/SslContext.cpp +++ b/src/PolarSSL++/SslContext.cpp @@ -40,7 +40,7 @@ int cSslContext::Initialize(bool a_IsClient, const SharedPtr & if (m_IsValid) { LOGWARNING("SSL: Double initialization is not supported."); - return POLARSSL_ERR_SSL_MALLOC_FAILED; // There is no return value well-suited for this, reuse this one. + return POLARSSL_ERR_SSL_BAD_INPUT_DATA; // There is no return value well-suited for this, reuse this one. } // Set the CtrDrbg context, create a new one if needed: @@ -80,8 +80,56 @@ int cSslContext::Initialize(bool a_IsClient, const SharedPtr & +void cSslContext::SetOwnCert(const cX509CertPtr & a_OwnCert, const cRsaPrivateKeyPtr & a_OwnCertPrivKey) +{ + ASSERT(m_IsValid); // Call Initialize() first + + // Check that both the cert and the key is valid: + if ((a_OwnCert.get() == NULL) || (a_OwnCertPrivKey.get() == NULL)) + { + LOGWARNING("SSL: Own certificate is not valid, skipping the set."); + return; + } + + // Make sure we have the cert stored for later, PolarSSL only uses the cert later on + m_OwnCert = a_OwnCert; + m_OwnCertPrivKey = a_OwnCertPrivKey; + + // Set into the context: + ssl_set_own_cert_rsa(&m_Ssl, m_OwnCert->GetInternal(), m_OwnCertPrivKey->GetInternal()); +} + + + + + +void cSslContext::SetOwnCert(const cX509CertPtr & a_OwnCert, const cPublicKeyPtr & a_OwnCertPrivKey) +{ + ASSERT(m_IsValid); // Call Initialize() first + + // Check that both the cert and the key is valid: + if ((a_OwnCert.get() == NULL) || (a_OwnCertPrivKey.get() == NULL)) + { + LOGWARNING("SSL: Own certificate is not valid, skipping the set."); + return; + } + + // Make sure we have the cert stored for later, PolarSSL only uses the cert later on + m_OwnCert = a_OwnCert; + m_OwnCertPrivKey2 = a_OwnCertPrivKey; + + // Set into the context: + ssl_set_own_cert(&m_Ssl, m_OwnCert->GetInternal(), m_OwnCertPrivKey2->GetInternal()); +} + + + + + void cSslContext::SetCACerts(const cX509CertPtr & a_CACert, const AString & a_ExpectedPeerName) { + ASSERT(m_IsValid); // Call Initialize() first + // Store the data in our internal buffers, to avoid losing the pointers later on // PolarSSL will need these after this call returns, and the caller may move / delete the data before that: m_ExpectedPeerName = a_ExpectedPeerName; diff --git a/src/PolarSSL++/SslContext.h b/src/PolarSSL++/SslContext.h index 85add5f8b..273939b9f 100644 --- a/src/PolarSSL++/SslContext.h +++ b/src/PolarSSL++/SslContext.h @@ -11,6 +11,8 @@ #include "polarssl/ssl.h" #include "../ByteBuffer.h" +#include "PublicKey.h" +#include "RsaPrivateKey.h" #include "X509Cert.h" @@ -47,7 +49,16 @@ public: /** Returns true if the object has been initialized properly. */ bool IsValid(void) const { return m_IsValid; } - /** Sets a cert chain as the trusted cert store for this context. + /** Sets the certificate to use as our own. Must be used when representing a server, optional when client. + Must be called after Initialize(). */ + void SetOwnCert(const cX509CertPtr & a_OwnCert, const cRsaPrivateKeyPtr & a_OwnCertPrivKey); + + /** Sets the certificate to use as our own. Must be used when representing a server, optional when client. + Must be called after Initialize(). + Despite the class name, a_OwnCertPrivKey is a PRIVATE key. */ + void SetOwnCert(const cX509CertPtr & a_OwnCert, const cPublicKeyPtr & a_OwnCertPrivKey); + + /** Sets a cert chain as the trusted cert store for this context. Must be called after Initialize(). Calling this will switch the context into strict cert verification mode. a_ExpectedPeerName is the CommonName that we expect the SSL peer to have in its cert, if it is different, the verification will fail. An empty string will disable the CN check. */ @@ -93,6 +104,15 @@ protected: /** The SSL context that PolarSSL uses. */ ssl_context m_Ssl; + /** The certificate that we present to the peer. */ + cX509CertPtr m_OwnCert; + + /** Private key for m_OwnCert, if initialized from a cRsaPrivateKey */ + cRsaPrivateKeyPtr m_OwnCertPrivKey; + + /** Private key for m_OwnCert, if initialized from a cPublicKey. Despite the class name, this is a PRIVATE key. */ + cPublicKeyPtr m_OwnCertPrivKey2; + /** True if the SSL handshake has been completed. */ bool m_HasHandshaken; -- cgit v1.2.3 From e2bf3783e8c8f7855532e04203bb1b46b976cfee Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 11:32:25 +0200 Subject: Fixed BufferedSslContext's buffer reading and writing. --- src/PolarSSL++/BufferedSslContext.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/PolarSSL++/BufferedSslContext.cpp b/src/PolarSSL++/BufferedSslContext.cpp index 2455d5781..9f7caeb8a 100644 --- a/src/PolarSSL++/BufferedSslContext.cpp +++ b/src/PolarSSL++/BufferedSslContext.cpp @@ -26,7 +26,7 @@ size_t cBufferedSslContext::WriteIncoming(const void * a_Data, size_t a_NumBytes if (NumBytes > 0) { m_IncomingData.Write(a_Data, NumBytes); - return a_NumBytes - NumBytes; + return NumBytes; } return 0; } @@ -42,7 +42,7 @@ size_t cBufferedSslContext::ReadOutgoing(void * a_Data, size_t a_DataMaxSize) { m_OutgoingData.ReadBuf(a_Data, NumBytes); m_OutgoingData.CommitRead(); - return a_DataMaxSize - NumBytes; + return NumBytes; } return 0; } -- cgit v1.2.3 From e1b6a169457b267c3e11bbdb9e58e9ab7b3f0136 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 11:33:29 +0200 Subject: Added a (disabled) test of low-security ciphersuites. Enabling this allows the connection to be sniffed and decoded using Wireshark, when given the SSL private key. --- src/PolarSSL++/SslContext.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src') diff --git a/src/PolarSSL++/SslContext.cpp b/src/PolarSSL++/SslContext.cpp index 3d2b8cef7..df0219610 100644 --- a/src/PolarSSL++/SslContext.cpp +++ b/src/PolarSSL++/SslContext.cpp @@ -70,6 +70,18 @@ int cSslContext::Initialize(bool a_IsClient, const SharedPtr & ssl_set_dbg(&m_Ssl, &SSLDebugMessage, this); ssl_set_verify(&m_Ssl, &SSLVerifyCert, this); */ + + /* + // Set ciphersuite to the easiest one to decode, so that the connection can be wireshark-decoded: + static const int CipherSuites[] = + { + TLS_RSA_WITH_RC4_128_MD5, + TLS_RSA_WITH_RC4_128_SHA, + TLS_RSA_WITH_AES_128_CBC_SHA, + 0, // Must be 0-terminated! + }; + ssl_set_ciphersuites(&m_Ssl, CipherSuites); + */ #endif m_IsValid = true; -- cgit v1.2.3 From a04cb6d146dfb8f04b0a8943c1e185fc25bf2829 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 11:34:15 +0200 Subject: Fixed HTTP message parsing, prepared for SSL. --- src/HTTPServer/HTTPConnection.cpp | 5 +++-- src/HTTPServer/HTTPMessage.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/HTTPServer/HTTPConnection.cpp b/src/HTTPServer/HTTPConnection.cpp index da4df0e34..8e95eff2d 100644 --- a/src/HTTPServer/HTTPConnection.cpp +++ b/src/HTTPServer/HTTPConnection.cpp @@ -26,6 +26,7 @@ cHTTPConnection::cHTTPConnection(cHTTPServer & a_HTTPServer) : cHTTPConnection::~cHTTPConnection() { + // LOGD("HTTP: Connection deleting: %p", this); delete m_CurrentRequest; } @@ -183,11 +184,11 @@ void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) // Process the rest of the incoming data into the request body: if (a_Size > BytesConsumed) { - DataReceived(a_Data + BytesConsumed, a_Size - BytesConsumed); + cHTTPConnection::DataReceived(a_Data + BytesConsumed, a_Size - BytesConsumed); } else { - DataReceived("", 0); // If the request has zero body length, let it be processed right-away + cHTTPConnection::DataReceived("", 0); // If the request has zero body length, let it be processed right-away } break; } diff --git a/src/HTTPServer/HTTPMessage.cpp b/src/HTTPServer/HTTPMessage.cpp index 4a3611050..44feda469 100644 --- a/src/HTTPServer/HTTPMessage.cpp +++ b/src/HTTPServer/HTTPMessage.cpp @@ -201,7 +201,7 @@ size_t cHTTPRequest::ParseRequestLine(const char * a_Data, size_t a_Size) return AString::npos; } // Check that there's HTTP/version at the end - if (strncmp(a_Data + URLEnd + 1, "HTTP/1.", 7) != 0) + if (strncmp(m_IncomingHeaderData.c_str() + URLEnd + 1, "HTTP/1.", 7) != 0) { m_IsValid = false; return AString::npos; -- cgit v1.2.3 From 272c232efb645c9f7d75556aeb047e13b244c9ed Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 11:48:03 +0200 Subject: Implemented SSL connection for WebAdmin. Fixes FS-319. --- src/HTTPServer/HTTPServer.cpp | 35 +++++++++++- src/HTTPServer/HTTPServer.h | 10 ++++ src/HTTPServer/SslHTTPConnection.cpp | 103 +++++++++++++++++++++++++++++++++++ src/HTTPServer/SslHTTPConnection.h | 45 +++++++++++++++ 4 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 src/HTTPServer/SslHTTPConnection.cpp create mode 100644 src/HTTPServer/SslHTTPConnection.h (limited to 'src') diff --git a/src/HTTPServer/HTTPServer.cpp b/src/HTTPServer/HTTPServer.cpp index eaf8405a3..9e3e0a17b 100644 --- a/src/HTTPServer/HTTPServer.cpp +++ b/src/HTTPServer/HTTPServer.cpp @@ -8,6 +8,7 @@ #include "HTTPMessage.h" #include "HTTPConnection.h" #include "HTTPFormParser.h" +#include "SslHTTPConnection.h" @@ -123,8 +124,30 @@ class cDebugCallbacks : cHTTPServer::cHTTPServer(void) : m_ListenThreadIPv4(*this, cSocket::IPv4, "WebServer IPv4"), m_ListenThreadIPv6(*this, cSocket::IPv6, "WebServer IPv6"), - m_Callbacks(NULL) + m_Callbacks(NULL), + m_Cert(new cX509Cert), + m_CertPrivKey(new cPublicKey) { + AString CertFile = cFile::ReadWholeFile("webadmin/httpscert.crt"); + AString KeyFile = cFile::ReadWholeFile("webadmin/httpskey.pem"); + if (!CertFile.empty() && !KeyFile.empty()) + { + int res = m_Cert->Parse(CertFile.data(), CertFile.size()); + if (res == 0) + { + int res2 = m_CertPrivKey->ParsePrivate(KeyFile.data(), KeyFile.size(), ""); + if (res2 != 0) + { + // Reading the private key failed, reset the cert: + LOGWARNING("WebAdmin: Cannot read HTTPS certificate private key: -0x%x", -res2); + m_Cert.reset(); + } + } + else + { + LOGWARNING("WebAdmin: Cannot read HTTPS certificate: -0x%x", -res); + } + } } @@ -195,7 +218,15 @@ void cHTTPServer::Stop(void) void cHTTPServer::OnConnectionAccepted(cSocket & a_Socket) { - cHTTPConnection * Connection = new cHTTPConnection(*this); + cHTTPConnection * Connection; + if (m_Cert.get() != NULL) + { + Connection = new cSslHTTPConnection(*this, m_Cert, m_CertPrivKey); + } + else + { + Connection = new cHTTPConnection(*this); + } m_SocketThreads.AddClient(a_Socket, Connection); cCSLock Lock(m_CSConnections); m_Connections.push_back(Connection); diff --git a/src/HTTPServer/HTTPServer.h b/src/HTTPServer/HTTPServer.h index 8eff7d879..eb91dd5a3 100644 --- a/src/HTTPServer/HTTPServer.h +++ b/src/HTTPServer/HTTPServer.h @@ -12,6 +12,9 @@ #include "../OSSupport/ListenThread.h" #include "../OSSupport/SocketThreads.h" #include "inifile/iniFile.h" +#include "PolarSSL++/RsaPrivateKey.h" +#include "PolarSSL++/PublicKey.h" +#include "PolarSSL++/X509Cert.h" @@ -66,6 +69,7 @@ public: protected: friend class cHTTPConnection; + friend class cSslHTTPConnection; cListenThread m_ListenThreadIPv4; cListenThread m_ListenThreadIPv6; @@ -78,6 +82,12 @@ protected: /// The callbacks to call for various events cCallbacks * m_Callbacks; + /** The server certificate to use for the SSL connections */ + cX509CertPtr m_Cert; + + /** The private key for m_Cert. Despite the class name, this is the PRIVATE key. */ + cPublicKeyPtr m_CertPrivKey; + // cListenThread::cCallback overrides: virtual void OnConnectionAccepted(cSocket & a_Socket) override; diff --git a/src/HTTPServer/SslHTTPConnection.cpp b/src/HTTPServer/SslHTTPConnection.cpp new file mode 100644 index 000000000..fff96bb2e --- /dev/null +++ b/src/HTTPServer/SslHTTPConnection.cpp @@ -0,0 +1,103 @@ + +// SslHTTPConnection.cpp + +// Implements the cSslHTTPConnection class representing a HTTP connection made over a SSL link + +#include "Globals.h" +#include "SslHTTPConnection.h" +#include "HTTPServer.h" + + + + + +cSslHTTPConnection::cSslHTTPConnection(cHTTPServer & a_HTTPServer, const cX509CertPtr & a_Cert, const cPublicKeyPtr & a_PrivateKey) : + super(a_HTTPServer), + m_Ssl(64000), + m_Cert(a_Cert), + m_PrivateKey(a_PrivateKey) +{ + m_Ssl.Initialize(false); + m_Ssl.SetOwnCert(a_Cert, a_PrivateKey); +} + + + + + +void cSslHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) +{ + // If there is outgoing data in the queue, notify the server that it should write it out: + if (!m_OutgoingData.empty()) + { + m_HTTPServer.NotifyConnectionWrite(*this); + } + + // Process the received data: + const char * Data = a_Data; + size_t Size = a_Size; + for (;;) + { + // Try to write as many bytes into Ssl's "incoming" buffer as possible: + size_t BytesWritten = 0; + if (Size > 0) + { + BytesWritten = m_Ssl.WriteIncoming(Data, Size); + Data += BytesWritten; + Size -= BytesWritten; + } + + // Try to read as many bytes from SSL's decryption as possible: + char Buffer[32000]; + int NumRead = m_Ssl.ReadPlain(Buffer, sizeof(Buffer)); + if (NumRead > 0) + { + super::DataReceived(Buffer, (size_t)NumRead); + } + + // If both failed, bail out: + if ((BytesWritten == 0) && (NumRead <= 0)) + { + return; + } + } +} + + + + + +void cSslHTTPConnection::GetOutgoingData(AString & a_Data) +{ + for (;;) + { + // Write as many bytes from our buffer to SSL's encryption as possible: + int NumWritten = 0; + if (!m_OutgoingData.empty()) + { + NumWritten = m_Ssl.WritePlain(m_OutgoingData.data(), m_OutgoingData.size()); + if (NumWritten > 0) + { + m_OutgoingData.erase(0, (size_t)NumWritten); + } + } + + // Read as many bytes from SSL's "outgoing" buffer as possible: + char Buffer[32000]; + size_t NumBytes = m_Ssl.ReadOutgoing(Buffer, sizeof(Buffer)); + if (NumBytes > 0) + { + a_Data.append(Buffer, NumBytes); + } + + // If both failed, bail out: + if ((NumWritten <= 0) && (NumBytes == 0)) + { + return; + } + } +} + + + + diff --git a/src/HTTPServer/SslHTTPConnection.h b/src/HTTPServer/SslHTTPConnection.h new file mode 100644 index 000000000..2a648e8c8 --- /dev/null +++ b/src/HTTPServer/SslHTTPConnection.h @@ -0,0 +1,45 @@ + +// SslHTTPConnection.h + +// Declared the cSslHTTPConnection class representing a HTTP connection made over a SSL link + + + + + +#pragma once + +#include "HTTPConnection.h" +#include "PolarSSL++/BufferedSslContext.h" + + + + + +class cSslHTTPConnection : + public cHTTPConnection +{ + typedef cHTTPConnection super; + +public: + /** Creates a new connection on the specified server; sends the specified cert as the server certificate, + uses the private key for decryption. a_Private key is, despite the class name, a PRIVATE key for the cert. */ + cSslHTTPConnection(cHTTPServer & a_HTTPServer, const cX509CertPtr & a_Cert, const cPublicKeyPtr & a_PrivateKey); + +protected: + cBufferedSslContext m_Ssl; + + /** The certificate to send to the client */ + cX509CertPtr m_Cert; + + /** The private key used for the certificate */ + cPublicKeyPtr m_PrivateKey; + + // cHTTPConnection overrides: + virtual void DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client + virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client +} ; + + + + -- cgit v1.2.3 From 60850fe3e8da936d5b24460f33a1bf8f4d321ace Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 15:08:15 +0200 Subject: Fixed crashes in the SSL HTTP connection. --- src/ClientHandle.cpp | 3 ++- src/ClientHandle.h | 2 +- src/HTTPServer/HTTPConnection.cpp | 14 +++++++------- src/HTTPServer/HTTPConnection.h | 12 +++++++++--- src/HTTPServer/SslHTTPConnection.cpp | 10 +++++++--- src/HTTPServer/SslHTTPConnection.h | 2 +- src/OSSupport/SocketThreads.h | 6 ++++-- src/RCONServer.cpp | 8 ++++---- src/RCONServer.h | 2 +- 9 files changed, 36 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 2362abe1e..a2bfdd381 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -2649,12 +2649,13 @@ void cClientHandle::PacketError(unsigned char a_PacketType) -void cClientHandle::DataReceived(const char * a_Data, size_t a_Size) +bool cClientHandle::DataReceived(const char * a_Data, size_t a_Size) { // Data is received from the client, store it in the buffer to be processed by the Tick thread: m_TimeSinceLastPacket = 0; cCSLock Lock(m_CSIncomingData); m_IncomingData.append(a_Data, a_Size); + return false; } diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 9f8d44129..ac44e43dc 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -387,7 +387,7 @@ private: void HandleCommandBlockMessage(const char * a_Data, unsigned int a_Length); // cSocketThreads::cCallback overrides: - virtual void DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client + virtual bool DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client virtual void SocketClosed (void) override; // The socket has been closed for any reason }; // tolua_export diff --git a/src/HTTPServer/HTTPConnection.cpp b/src/HTTPServer/HTTPConnection.cpp index 8e95eff2d..b127e7091 100644 --- a/src/HTTPServer/HTTPConnection.cpp +++ b/src/HTTPServer/HTTPConnection.cpp @@ -145,7 +145,7 @@ void cHTTPConnection::Terminate(void) -void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) +bool cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) { switch (m_State) { @@ -163,12 +163,12 @@ void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) m_CurrentRequest = NULL; m_State = wcsInvalid; m_HTTPServer.CloseConnection(*this); - return; + return true; } if (m_CurrentRequest->IsInHeaders()) { // The request headers are not yet complete - return; + return false; } // The request has finished parsing its headers successfully, notify of it: @@ -184,13 +184,12 @@ void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) // Process the rest of the incoming data into the request body: if (a_Size > BytesConsumed) { - cHTTPConnection::DataReceived(a_Data + BytesConsumed, a_Size - BytesConsumed); + return cHTTPConnection::DataReceived(a_Data + BytesConsumed, a_Size - BytesConsumed); } else { - cHTTPConnection::DataReceived("", 0); // If the request has zero body length, let it be processed right-away + return cHTTPConnection::DataReceived("", 0); // If the request has zero body length, let it be processed right-away } - break; } case wcsRecvBody: @@ -210,7 +209,7 @@ void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) { m_State = wcsInvalid; m_HTTPServer.CloseConnection(*this); - return; + return true; } delete m_CurrentRequest; m_CurrentRequest = NULL; @@ -224,6 +223,7 @@ void cHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) break; } } + return false; } diff --git a/src/HTTPServer/HTTPConnection.h b/src/HTTPServer/HTTPConnection.h index fc11f1ba6..6ea8a1ae8 100644 --- a/src/HTTPServer/HTTPConnection.h +++ b/src/HTTPServer/HTTPConnection.h @@ -91,9 +91,15 @@ protected: // cSocketThreads::cCallback overrides: - virtual void DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client - virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client - virtual void SocketClosed (void) override; // The socket has been closed for any reason + /** Data is received from the client. + Returns true if the connection has been closed as the result of parsing the data. */ + virtual bool DataReceived(const char * a_Data, size_t a_Size) override; + + /** Data can be sent to client */ + virtual void GetOutgoingData(AString & a_Data) override; + + /** The socket has been closed for any reason */ + virtual void SocketClosed(void) override; } ; typedef std::vector cHTTPConnections; diff --git a/src/HTTPServer/SslHTTPConnection.cpp b/src/HTTPServer/SslHTTPConnection.cpp index fff96bb2e..b6b222b47 100644 --- a/src/HTTPServer/SslHTTPConnection.cpp +++ b/src/HTTPServer/SslHTTPConnection.cpp @@ -25,7 +25,7 @@ cSslHTTPConnection::cSslHTTPConnection(cHTTPServer & a_HTTPServer, const cX509Ce -void cSslHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) +bool cSslHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) { // If there is outgoing data in the queue, notify the server that it should write it out: if (!m_OutgoingData.empty()) @@ -52,13 +52,17 @@ void cSslHTTPConnection::DataReceived(const char * a_Data, size_t a_Size) int NumRead = m_Ssl.ReadPlain(Buffer, sizeof(Buffer)); if (NumRead > 0) { - super::DataReceived(Buffer, (size_t)NumRead); + if (super::DataReceived(Buffer, (size_t)NumRead)) + { + // The socket has been closed, and the object is already deleted. Bail out. + return true; + } } // If both failed, bail out: if ((BytesWritten == 0) && (NumRead <= 0)) { - return; + return false; } } } diff --git a/src/HTTPServer/SslHTTPConnection.h b/src/HTTPServer/SslHTTPConnection.h index 2a648e8c8..653acbfce 100644 --- a/src/HTTPServer/SslHTTPConnection.h +++ b/src/HTTPServer/SslHTTPConnection.h @@ -36,7 +36,7 @@ protected: cPublicKeyPtr m_PrivateKey; // cHTTPConnection overrides: - virtual void DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client + virtual bool DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client virtual void GetOutgoingData(AString & a_Data) override; // Data can be sent to client } ; diff --git a/src/OSSupport/SocketThreads.h b/src/OSSupport/SocketThreads.h index 679e374e1..944f5f3bc 100644 --- a/src/OSSupport/SocketThreads.h +++ b/src/OSSupport/SocketThreads.h @@ -63,8 +63,10 @@ public: // Force a virtual destructor in all subclasses: virtual ~cCallback() {} - /** Called when data is received from the remote party */ - virtual void DataReceived(const char * a_Data, size_t a_Size) = 0; + /** Called when data is received from the remote party. + SocketThreads does not care about the return value, others can use it for their specific purpose - + for example HTTPServer uses it to signal if the connection was terminated as a result of the data received. */ + virtual bool DataReceived(const char * a_Data, size_t a_Size) = 0; /** Called when data can be sent to remote party The function is supposed to *set* outgoing data to a_Data (overwrite) */ diff --git a/src/RCONServer.cpp b/src/RCONServer.cpp index d7083ff2b..fd4b26cab 100644 --- a/src/RCONServer.cpp +++ b/src/RCONServer.cpp @@ -169,7 +169,7 @@ cRCONServer::cConnection::cConnection(cRCONServer & a_RCONServer, cSocket & a_So -void cRCONServer::cConnection::DataReceived(const char * a_Data, size_t a_Size) +bool cRCONServer::cConnection::DataReceived(const char * a_Data, size_t a_Size) { // Append data to the buffer: m_Buffer.append(a_Data, a_Size); @@ -187,12 +187,12 @@ void cRCONServer::cConnection::DataReceived(const char * a_Data, size_t a_Size) m_RCONServer.m_SocketThreads.RemoveClient(this); m_Socket.CloseSocket(); delete this; - return; + return false; } if (Length > (int)(m_Buffer.size() + 4)) { // Incomplete packet yet, wait for more data to come - return; + return false; } int RequestID = IntFromBuffer(m_Buffer.data() + 4); @@ -202,7 +202,7 @@ void cRCONServer::cConnection::DataReceived(const char * a_Data, size_t a_Size) m_RCONServer.m_SocketThreads.RemoveClient(this); m_Socket.CloseSocket(); delete this; - return; + return false; } m_Buffer.erase(0, Length + 4); } // while (m_Buffer.size() >= 14) diff --git a/src/RCONServer.h b/src/RCONServer.h index b964852ab..47c746736 100644 --- a/src/RCONServer.h +++ b/src/RCONServer.h @@ -65,7 +65,7 @@ protected: // cSocketThreads::cCallback overrides: - virtual void DataReceived(const char * a_Data, size_t a_Size) override; + virtual bool DataReceived(const char * a_Data, size_t a_Size) override; virtual void GetOutgoingData(AString & a_Data) override; virtual void SocketClosed(void) override; -- cgit v1.2.3 From 1587b21edded56dbfb88150500336c2853b460c6 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 15:21:41 +0200 Subject: Renamed cPublicKey to cCryptoKey. The class can hold both the private key and the public key, bad naming on PolarSSL's part. Also somewhat fixed the cert and key loading in cHTTPServer. --- src/HTTPServer/HTTPServer.cpp | 6 +- src/HTTPServer/HTTPServer.h | 6 +- src/HTTPServer/SslHTTPConnection.cpp | 2 +- src/HTTPServer/SslHTTPConnection.h | 8 +- src/PolarSSL++/CryptoKey.cpp | 149 +++++++++++++++++++++++++++++++++++ src/PolarSSL++/CryptoKey.h | 76 ++++++++++++++++++ src/PolarSSL++/CtrDrbgContext.h | 2 +- src/PolarSSL++/PublicKey.cpp | 149 ----------------------------------- src/PolarSSL++/PublicKey.h | 76 ------------------ src/PolarSSL++/SslContext.cpp | 2 +- src/PolarSSL++/SslContext.h | 13 ++- 11 files changed, 244 insertions(+), 245 deletions(-) create mode 100644 src/PolarSSL++/CryptoKey.cpp create mode 100644 src/PolarSSL++/CryptoKey.h delete mode 100644 src/PolarSSL++/PublicKey.cpp delete mode 100644 src/PolarSSL++/PublicKey.h (limited to 'src') diff --git a/src/HTTPServer/HTTPServer.cpp b/src/HTTPServer/HTTPServer.cpp index 9e3e0a17b..c45044c66 100644 --- a/src/HTTPServer/HTTPServer.cpp +++ b/src/HTTPServer/HTTPServer.cpp @@ -124,17 +124,17 @@ class cDebugCallbacks : cHTTPServer::cHTTPServer(void) : m_ListenThreadIPv4(*this, cSocket::IPv4, "WebServer IPv4"), m_ListenThreadIPv6(*this, cSocket::IPv6, "WebServer IPv6"), - m_Callbacks(NULL), - m_Cert(new cX509Cert), - m_CertPrivKey(new cPublicKey) + m_Callbacks(NULL) { AString CertFile = cFile::ReadWholeFile("webadmin/httpscert.crt"); AString KeyFile = cFile::ReadWholeFile("webadmin/httpskey.pem"); if (!CertFile.empty() && !KeyFile.empty()) { + m_Cert.reset(new cX509Cert); int res = m_Cert->Parse(CertFile.data(), CertFile.size()); if (res == 0) { + m_CertPrivKey.reset(new cCryptoKey); int res2 = m_CertPrivKey->ParsePrivate(KeyFile.data(), KeyFile.size(), ""); if (res2 != 0) { diff --git a/src/HTTPServer/HTTPServer.h b/src/HTTPServer/HTTPServer.h index eb91dd5a3..522b7da62 100644 --- a/src/HTTPServer/HTTPServer.h +++ b/src/HTTPServer/HTTPServer.h @@ -13,7 +13,7 @@ #include "../OSSupport/SocketThreads.h" #include "inifile/iniFile.h" #include "PolarSSL++/RsaPrivateKey.h" -#include "PolarSSL++/PublicKey.h" +#include "PolarSSL++/CryptoKey.h" #include "PolarSSL++/X509Cert.h" @@ -85,8 +85,8 @@ protected: /** The server certificate to use for the SSL connections */ cX509CertPtr m_Cert; - /** The private key for m_Cert. Despite the class name, this is the PRIVATE key. */ - cPublicKeyPtr m_CertPrivKey; + /** The private key for m_Cert. */ + cCryptoKeyPtr m_CertPrivKey; // cListenThread::cCallback overrides: diff --git a/src/HTTPServer/SslHTTPConnection.cpp b/src/HTTPServer/SslHTTPConnection.cpp index b6b222b47..d237089d9 100644 --- a/src/HTTPServer/SslHTTPConnection.cpp +++ b/src/HTTPServer/SslHTTPConnection.cpp @@ -11,7 +11,7 @@ -cSslHTTPConnection::cSslHTTPConnection(cHTTPServer & a_HTTPServer, const cX509CertPtr & a_Cert, const cPublicKeyPtr & a_PrivateKey) : +cSslHTTPConnection::cSslHTTPConnection(cHTTPServer & a_HTTPServer, const cX509CertPtr & a_Cert, const cCryptoKeyPtr & a_PrivateKey) : super(a_HTTPServer), m_Ssl(64000), m_Cert(a_Cert), diff --git a/src/HTTPServer/SslHTTPConnection.h b/src/HTTPServer/SslHTTPConnection.h index 653acbfce..c2c1585cd 100644 --- a/src/HTTPServer/SslHTTPConnection.h +++ b/src/HTTPServer/SslHTTPConnection.h @@ -22,9 +22,9 @@ class cSslHTTPConnection : typedef cHTTPConnection super; public: - /** Creates a new connection on the specified server; sends the specified cert as the server certificate, - uses the private key for decryption. a_Private key is, despite the class name, a PRIVATE key for the cert. */ - cSslHTTPConnection(cHTTPServer & a_HTTPServer, const cX509CertPtr & a_Cert, const cPublicKeyPtr & a_PrivateKey); + /** Creates a new connection on the specified server. + Sends the specified cert as the server certificate, uses the private key for decryption. */ + cSslHTTPConnection(cHTTPServer & a_HTTPServer, const cX509CertPtr & a_Cert, const cCryptoKeyPtr & a_PrivateKey); protected: cBufferedSslContext m_Ssl; @@ -33,7 +33,7 @@ protected: cX509CertPtr m_Cert; /** The private key used for the certificate */ - cPublicKeyPtr m_PrivateKey; + cCryptoKeyPtr m_PrivateKey; // cHTTPConnection overrides: virtual bool DataReceived (const char * a_Data, size_t a_Size) override; // Data is received from the client diff --git a/src/PolarSSL++/CryptoKey.cpp b/src/PolarSSL++/CryptoKey.cpp new file mode 100644 index 000000000..0763c387b --- /dev/null +++ b/src/PolarSSL++/CryptoKey.cpp @@ -0,0 +1,149 @@ + +// CryptoKey.cpp + +// Implements the cCryptoKey class representing a RSA public key in PolarSSL + +#include "Globals.h" +#include "CryptoKey.h" + + + + + +cCryptoKey::cCryptoKey(void) +{ + pk_init(&m_Pk); + m_CtrDrbg.Initialize("rsa_pubkey", 10); +} + + + + + +cCryptoKey::cCryptoKey(const AString & a_PublicKeyData) +{ + pk_init(&m_Pk); + m_CtrDrbg.Initialize("rsa_pubkey", 10); + int res = ParsePublic(a_PublicKeyData.data(), a_PublicKeyData.size()); + if (res != 0) + { + LOGWARNING("Failed to parse public key: -0x%x", res); + ASSERT(!"Cannot parse PubKey"); + return; + } +} + + + + + +cCryptoKey::cCryptoKey(const AString & a_PrivateKeyData, const AString & a_Password) +{ + pk_init(&m_Pk); + m_CtrDrbg.Initialize("rsa_privkey", 11); + int res = ParsePrivate(a_PrivateKeyData.data(), a_PrivateKeyData.size(), a_Password); + if (res != 0) + { + LOGWARNING("Failed to parse private key: -0x%x", res); + ASSERT(!"Cannot parse PubKey"); + return; + } +} + + + + + +cCryptoKey::~cCryptoKey() +{ + pk_free(&m_Pk); +} + + + + + +int cCryptoKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength) +{ + ASSERT(IsValid()); + + size_t DecryptedLen = a_DecryptedMaxLength; + int res = pk_decrypt(&m_Pk, + a_EncryptedData, a_EncryptedLength, + a_DecryptedData, &DecryptedLen, a_DecryptedMaxLength, + ctr_drbg_random, m_CtrDrbg.GetInternal() + ); + if (res != 0) + { + return res; + } + return (int)DecryptedLen; +} + + + + + +int cCryptoKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength) +{ + ASSERT(IsValid()); + + size_t EncryptedLength = a_EncryptedMaxLength; + int res = pk_encrypt(&m_Pk, + a_PlainData, a_PlainLength, a_EncryptedData, &EncryptedLength, a_EncryptedMaxLength, + ctr_drbg_random, m_CtrDrbg.GetInternal() + ); + if (res != 0) + { + return res; + } + return (int)EncryptedLength; +} + + + + + + +int cCryptoKey::ParsePublic(const void * a_Data, size_t a_NumBytes) +{ + ASSERT(!IsValid()); // Cannot parse a second key + + return pk_parse_public_key(&m_Pk, (const unsigned char *)a_Data, a_NumBytes); +} + + + + + + +int cCryptoKey::ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password) +{ + ASSERT(!IsValid()); // Cannot parse a second key + + if (a_Password.empty()) + { + return pk_parse_key(&m_Pk, (const unsigned char *)a_Data, a_NumBytes, NULL, 0); + } + else + { + return pk_parse_key( + &m_Pk, + (const unsigned char *)a_Data, a_NumBytes, + (const unsigned char *)a_Password.c_str(), a_Password.size() + ); + } +} + + + + + +bool cCryptoKey::IsValid(void) const +{ + return (pk_get_type(&m_Pk) != POLARSSL_PK_NONE); +} + + + + diff --git a/src/PolarSSL++/CryptoKey.h b/src/PolarSSL++/CryptoKey.h new file mode 100644 index 000000000..9c298e501 --- /dev/null +++ b/src/PolarSSL++/CryptoKey.h @@ -0,0 +1,76 @@ + +// CryptoKey.h + +// Declares the cCryptoKey class representing a RSA public key in PolarSSL + + + + + +#pragma once + +#include "CtrDrbgContext.h" +#include "polarssl/pk.h" + + + + + +class cCryptoKey +{ + friend class cSslContext; + +public: + /** Constructs an empty key instance. Before use, it needs to be filled by ParsePublic() or ParsePrivate() */ + cCryptoKey(void); + + /** Constructs the public key out of the DER- or PEM-encoded pubkey data */ + cCryptoKey(const AString & a_PublicKeyData); + + /** Constructs the private key out of the DER- or PEM-encoded privkey data, with the specified password. + If a_Password is empty, no password is assumed. */ + cCryptoKey(const AString & a_PrivateKeyData, const AString & a_Password); + + ~cCryptoKey(); + + /** Decrypts the data using the stored public key + Both a_EncryptedData and a_DecryptedData must be at least bytes large. + Returns the number of bytes decrypted, or negative number for error. */ + int Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength); + + /** Encrypts the data using the stored public key + Both a_EncryptedData and a_DecryptedData must be at least bytes large. + Returns the number of bytes decrypted, or negative number for error. */ + int Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength); + + /** Parses the specified data into a public key representation. + The key can be DER- or PEM-encoded. + Returns 0 on success, PolarSSL error code on failure. */ + int ParsePublic(const void * a_Data, size_t a_NumBytes); + + /** Parses the specified data into a private key representation. + If a_Password is empty, no password is assumed. + The key can be DER- or PEM-encoded. + Returns 0 on success, PolarSSL error code on failure. */ + int ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password); + + /** Returns true if the contained key is valid. */ + bool IsValid(void) const; + +protected: + /** The PolarSSL representation of the key data */ + pk_context m_Pk; + + /** The random generator used in encryption and decryption */ + cCtrDrbgContext m_CtrDrbg; + + + /** Returns the internal context ptr. Only use in PolarSSL API calls. */ + pk_context * GetInternal(void) { return &m_Pk; } +} ; + +typedef SharedPtr cCryptoKeyPtr; + + + + diff --git a/src/PolarSSL++/CtrDrbgContext.h b/src/PolarSSL++/CtrDrbgContext.h index 65e9a2374..230db8753 100644 --- a/src/PolarSSL++/CtrDrbgContext.h +++ b/src/PolarSSL++/CtrDrbgContext.h @@ -26,7 +26,7 @@ class cCtrDrbgContext { friend class cSslContext; friend class cRsaPrivateKey; - friend class cPublicKey; + friend class cCryptoKey; public: /** Constructs the context with a new entropy context. */ diff --git a/src/PolarSSL++/PublicKey.cpp b/src/PolarSSL++/PublicKey.cpp deleted file mode 100644 index dae026082..000000000 --- a/src/PolarSSL++/PublicKey.cpp +++ /dev/null @@ -1,149 +0,0 @@ - -// PublicKey.cpp - -// Implements the cPublicKey class representing a RSA public key in PolarSSL - -#include "Globals.h" -#include "PublicKey.h" - - - - - -cPublicKey::cPublicKey(void) -{ - pk_init(&m_Pk); - m_CtrDrbg.Initialize("rsa_pubkey", 10); -} - - - - - -cPublicKey::cPublicKey(const AString & a_PublicKeyData) -{ - pk_init(&m_Pk); - m_CtrDrbg.Initialize("rsa_pubkey", 10); - int res = ParsePublic(a_PublicKeyData.data(), a_PublicKeyData.size()); - if (res != 0) - { - LOGWARNING("Failed to parse public key: -0x%x", res); - ASSERT(!"Cannot parse PubKey"); - return; - } -} - - - - - -cPublicKey::cPublicKey(const AString & a_PrivateKeyData, const AString & a_Password) -{ - pk_init(&m_Pk); - m_CtrDrbg.Initialize("rsa_privkey", 11); - int res = ParsePrivate(a_PrivateKeyData.data(), a_PrivateKeyData.size(), a_Password); - if (res != 0) - { - LOGWARNING("Failed to parse private key: -0x%x", res); - ASSERT(!"Cannot parse PubKey"); - return; - } -} - - - - - -cPublicKey::~cPublicKey() -{ - pk_free(&m_Pk); -} - - - - - -int cPublicKey::Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength) -{ - ASSERT(IsValid()); - - size_t DecryptedLen = a_DecryptedMaxLength; - int res = pk_decrypt(&m_Pk, - a_EncryptedData, a_EncryptedLength, - a_DecryptedData, &DecryptedLen, a_DecryptedMaxLength, - ctr_drbg_random, m_CtrDrbg.GetInternal() - ); - if (res != 0) - { - return res; - } - return (int)DecryptedLen; -} - - - - - -int cPublicKey::Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength) -{ - ASSERT(IsValid()); - - size_t EncryptedLength = a_EncryptedMaxLength; - int res = pk_encrypt(&m_Pk, - a_PlainData, a_PlainLength, a_EncryptedData, &EncryptedLength, a_EncryptedMaxLength, - ctr_drbg_random, m_CtrDrbg.GetInternal() - ); - if (res != 0) - { - return res; - } - return (int)EncryptedLength; -} - - - - - - -int cPublicKey::ParsePublic(const void * a_Data, size_t a_NumBytes) -{ - ASSERT(!IsValid()); // Cannot parse a second key - - return pk_parse_public_key(&m_Pk, (const unsigned char *)a_Data, a_NumBytes); -} - - - - - - -int cPublicKey::ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password) -{ - ASSERT(!IsValid()); // Cannot parse a second key - - if (a_Password.empty()) - { - return pk_parse_key(&m_Pk, (const unsigned char *)a_Data, a_NumBytes, NULL, 0); - } - else - { - return pk_parse_key( - &m_Pk, - (const unsigned char *)a_Data, a_NumBytes, - (const unsigned char *)a_Password.c_str(), a_Password.size() - ); - } -} - - - - - -bool cPublicKey::IsValid(void) const -{ - return (pk_get_type(&m_Pk) != POLARSSL_PK_NONE); -} - - - - diff --git a/src/PolarSSL++/PublicKey.h b/src/PolarSSL++/PublicKey.h deleted file mode 100644 index df52a4143..000000000 --- a/src/PolarSSL++/PublicKey.h +++ /dev/null @@ -1,76 +0,0 @@ - -// PublicKey.h - -// Declares the cPublicKey class representing a RSA public key in PolarSSL - - - - - -#pragma once - -#include "CtrDrbgContext.h" -#include "polarssl/pk.h" - - - - - -class cPublicKey -{ - friend class cSslContext; - -public: - /** Constructs an empty key instance. Before use, it needs to be filled by ParsePublic() or ParsePrivate() */ - cPublicKey(void); - - /** Constructs the public key out of the DER- or PEM-encoded pubkey data */ - cPublicKey(const AString & a_PublicKeyData); - - /** Constructs the private key out of the DER- or PEM-encoded privkey data, with the specified password. - If a_Password is empty, no password is assumed. */ - cPublicKey(const AString & a_PrivateKeyData, const AString & a_Password); - - ~cPublicKey(); - - /** Decrypts the data using the stored public key - Both a_EncryptedData and a_DecryptedData must be at least bytes large. - Returns the number of bytes decrypted, or negative number for error. */ - int Decrypt(const Byte * a_EncryptedData, size_t a_EncryptedLength, Byte * a_DecryptedData, size_t a_DecryptedMaxLength); - - /** Encrypts the data using the stored public key - Both a_EncryptedData and a_DecryptedData must be at least bytes large. - Returns the number of bytes decrypted, or negative number for error. */ - int Encrypt(const Byte * a_PlainData, size_t a_PlainLength, Byte * a_EncryptedData, size_t a_EncryptedMaxLength); - - /** Parses the specified data into a public key representation. - The key can be DER- or PEM-encoded. - Returns 0 on success, PolarSSL error code on failure. */ - int ParsePublic(const void * a_Data, size_t a_NumBytes); - - /** Parses the specified data into a private key representation. - If a_Password is empty, no password is assumed. - The key can be DER- or PEM-encoded. - Returns 0 on success, PolarSSL error code on failure. */ - int ParsePrivate(const void * a_Data, size_t a_NumBytes, const AString & a_Password); - - /** Returns true if the contained key is valid. */ - bool IsValid(void) const; - -protected: - /** The public key PolarSSL representation */ - pk_context m_Pk; - - /** The random generator used in encryption and decryption */ - cCtrDrbgContext m_CtrDrbg; - - - /** Returns the internal context ptr. Only use in PolarSSL API calls. */ - pk_context * GetInternal(void) { return &m_Pk; } -} ; - -typedef SharedPtr cPublicKeyPtr; - - - - diff --git a/src/PolarSSL++/SslContext.cpp b/src/PolarSSL++/SslContext.cpp index df0219610..bc397b655 100644 --- a/src/PolarSSL++/SslContext.cpp +++ b/src/PolarSSL++/SslContext.cpp @@ -115,7 +115,7 @@ void cSslContext::SetOwnCert(const cX509CertPtr & a_OwnCert, const cRsaPrivateKe -void cSslContext::SetOwnCert(const cX509CertPtr & a_OwnCert, const cPublicKeyPtr & a_OwnCertPrivKey) +void cSslContext::SetOwnCert(const cX509CertPtr & a_OwnCert, const cCryptoKeyPtr & a_OwnCertPrivKey) { ASSERT(m_IsValid); // Call Initialize() first diff --git a/src/PolarSSL++/SslContext.h b/src/PolarSSL++/SslContext.h index 273939b9f..a4ad1a345 100644 --- a/src/PolarSSL++/SslContext.h +++ b/src/PolarSSL++/SslContext.h @@ -11,7 +11,7 @@ #include "polarssl/ssl.h" #include "../ByteBuffer.h" -#include "PublicKey.h" +#include "CryptoKey.h" #include "RsaPrivateKey.h" #include "X509Cert.h" @@ -54,9 +54,8 @@ public: void SetOwnCert(const cX509CertPtr & a_OwnCert, const cRsaPrivateKeyPtr & a_OwnCertPrivKey); /** Sets the certificate to use as our own. Must be used when representing a server, optional when client. - Must be called after Initialize(). - Despite the class name, a_OwnCertPrivKey is a PRIVATE key. */ - void SetOwnCert(const cX509CertPtr & a_OwnCert, const cPublicKeyPtr & a_OwnCertPrivKey); + Must be called after Initialize(). */ + void SetOwnCert(const cX509CertPtr & a_OwnCert, const cCryptoKeyPtr & a_OwnCertPrivKey); /** Sets a cert chain as the trusted cert store for this context. Must be called after Initialize(). Calling this will switch the context into strict cert verification mode. @@ -107,11 +106,11 @@ protected: /** The certificate that we present to the peer. */ cX509CertPtr m_OwnCert; - /** Private key for m_OwnCert, if initialized from a cRsaPrivateKey */ + /** Private key for m_OwnCert, if initialized from a cRsaPrivateKey. */ cRsaPrivateKeyPtr m_OwnCertPrivKey; - /** Private key for m_OwnCert, if initialized from a cPublicKey. Despite the class name, this is a PRIVATE key. */ - cPublicKeyPtr m_OwnCertPrivKey2; + /** Private key for m_OwnCert, if initialized from a cCryptoKey. */ + cCryptoKeyPtr m_OwnCertPrivKey2; /** True if the SSL handshake has been completed. */ bool m_HasHandshaken; -- cgit v1.2.3 From d9e5dbf16526aa102abdc818d3515928663a973c Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 1 May 2014 18:06:23 +0200 Subject: Renamed PublicKey to CryptoKey in CMakeLists.txt --- src/PolarSSL++/CMakeLists.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/PolarSSL++/CMakeLists.txt b/src/PolarSSL++/CMakeLists.txt index b0a592760..9a59cdb2c 100644 --- a/src/PolarSSL++/CMakeLists.txt +++ b/src/PolarSSL++/CMakeLists.txt @@ -1,4 +1,3 @@ - cmake_minimum_required (VERSION 2.6) project (MCServer) @@ -11,8 +10,8 @@ set(SOURCES BufferedSslContext.cpp CallbackSslContext.cpp CtrDrbgContext.cpp + CryptoKey.cpp EntropyContext.cpp - PublicKey.cpp RsaPrivateKey.cpp Sha1Checksum.cpp SslContext.cpp @@ -26,8 +25,8 @@ set(HEADERS BufferedSslContext.h CallbackSslContext.h CtrDrbgContext.h + CryptoKey.h EntropyContext.h - PublicKey.h RsaPrivateKey.h SslContext.h Sha1Checksum.h -- cgit v1.2.3 From 9221b458989512e41f8502b56ed738d143599093 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 21:23:37 +0200 Subject: cSslContext has virtual destructor now. --- src/PolarSSL++/SslContext.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/PolarSSL++/SslContext.h b/src/PolarSSL++/SslContext.h index a4ad1a345..6b4f2c1e7 100644 --- a/src/PolarSSL++/SslContext.h +++ b/src/PolarSSL++/SslContext.h @@ -40,7 +40,7 @@ public: /** Creates a new uninitialized context */ cSslContext(void); - ~cSslContext(); + virtual ~cSslContext(); /** Initializes the context for use as a server or client. Returns 0 on success, PolarSSL error on failure. */ -- cgit v1.2.3 From 30e81156eb6de46d7a374c044e26b35d83af4244 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 1 May 2014 22:02:45 +0200 Subject: Added a missing return statement. --- src/RCONServer.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/RCONServer.cpp b/src/RCONServer.cpp index fd4b26cab..cd5292ac3 100644 --- a/src/RCONServer.cpp +++ b/src/RCONServer.cpp @@ -206,6 +206,7 @@ bool cRCONServer::cConnection::DataReceived(const char * a_Data, size_t a_Size) } m_Buffer.erase(0, Length + 4); } // while (m_Buffer.size() >= 14) + return false; } -- cgit v1.2.3 From 3942ebdcb6e62d8b2fb85fad9144a993a78477a6 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Fri, 2 May 2014 23:46:06 +0200 Subject: WebAdmin outputs a log message about HTTP / HTTPS status. --- src/HTTPServer/HTTPServer.cpp | 49 +++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/HTTPServer/HTTPServer.cpp b/src/HTTPServer/HTTPServer.cpp index c45044c66..b78a538a2 100644 --- a/src/HTTPServer/HTTPServer.cpp +++ b/src/HTTPServer/HTTPServer.cpp @@ -126,6 +126,24 @@ cHTTPServer::cHTTPServer(void) : m_ListenThreadIPv6(*this, cSocket::IPv6, "WebServer IPv6"), m_Callbacks(NULL) { +} + + + + + +cHTTPServer::~cHTTPServer() +{ + Stop(); +} + + + + + +bool cHTTPServer::Initialize(const AString & a_PortsIPv4, const AString & a_PortsIPv6) +{ + // Read the HTTPS cert + key: AString CertFile = cFile::ReadWholeFile("webadmin/httpscert.crt"); AString KeyFile = cFile::ReadWholeFile("webadmin/httpskey.pem"); if (!CertFile.empty() && !KeyFile.empty()) @@ -139,32 +157,27 @@ cHTTPServer::cHTTPServer(void) : if (res2 != 0) { // Reading the private key failed, reset the cert: - LOGWARNING("WebAdmin: Cannot read HTTPS certificate private key: -0x%x", -res2); + LOGWARNING("WebServer: Cannot read HTTPS certificate private key: -0x%x", -res2); m_Cert.reset(); } } else { - LOGWARNING("WebAdmin: Cannot read HTTPS certificate: -0x%x", -res); + LOGWARNING("WebServer: Cannot read HTTPS certificate: -0x%x", -res); } } -} - - - - -cHTTPServer::~cHTTPServer() -{ - Stop(); -} - - - - - -bool cHTTPServer::Initialize(const AString & a_PortsIPv4, const AString & a_PortsIPv6) -{ + // Notify the admin about the HTTPS / HTTP status + if (m_Cert.get() == NULL) + { + LOGWARNING("WebServer: The server is running in unsecure HTTP mode."); + } + else + { + LOGINFO("WebServer: The server is running in secure HTTPS mode."); + } + + // Open up requested ports: bool HasAnyPort; HasAnyPort = m_ListenThreadIPv4.Initialize(a_PortsIPv4); HasAnyPort = m_ListenThreadIPv6.Initialize(a_PortsIPv6) || HasAnyPort; -- cgit v1.2.3 From 8f262d8a9d2d520a9c92dc0f270a88c554cb3778 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 6 May 2014 16:41:55 +0200 Subject: Initial cGridStructGen refactoring. Ref.: #987. --- src/Generating/GridStructGen.cpp | 138 +++++++++++++++++++++++++++++++++++++++ src/Generating/GridStructGen.h | 124 +++++++++++++++++++++++++++++++++++ 2 files changed, 262 insertions(+) create mode 100644 src/Generating/GridStructGen.cpp create mode 100644 src/Generating/GridStructGen.h (limited to 'src') diff --git a/src/Generating/GridStructGen.cpp b/src/Generating/GridStructGen.cpp new file mode 100644 index 000000000..975fd3fdc --- /dev/null +++ b/src/Generating/GridStructGen.cpp @@ -0,0 +1,138 @@ + +// GridStructGen.cpp + +// Implements the cGridStructGen class representing a common base class for structure generators that place structures in a semi-random grid + +#include "Globals.h" +#include "GridStructGen.h" + + + + +cGridStructGen::cGridStructGen( + int a_Seed, + int a_GridSizeX, int a_GridSizeZ, + int a_MaxStructureSizeX, int a_MaxStructureSizeZ, + size_t a_MaxCacheSize +) : + m_Seed(a_Seed), + m_GridSizeX(a_GridSizeX), + m_GridSizeZ(a_GridSizeZ), + m_MaxStructureSizeX(a_MaxStructureSizeX), + m_MaxStructureSizeZ(a_MaxStructureSizeZ), + m_MaxCacheSize(a_MaxCacheSize) +{ +} + + + + + +void cGridStructGen::GetStructuresForChunk(int a_ChunkX, int a_ChunkZ, cStructurePtrs & a_Structures) +{ + // Calculate the min and max grid coords of the structures to be returned: + int MinBlockX = a_ChunkX * cChunkDef::Width - m_MaxStructureSizeX; + int MinBlockZ = a_ChunkZ * cChunkDef::Width - m_MaxStructureSizeZ; + int MaxBlockX = a_ChunkX * cChunkDef::Width + m_MaxStructureSizeX + cChunkDef::Width - 1; + int MaxBlockZ = a_ChunkZ * cChunkDef::Width + m_MaxStructureSizeZ + cChunkDef::Width - 1; + int MinGridX = MinBlockX / m_GridSizeX; + int MinGridZ = MinBlockZ / m_GridSizeZ; + int MaxGridX = MaxBlockX / m_GridSizeX; + int MaxGridZ = MaxBlockX / m_GridSizeZ; + if (MinGridX < 0) + { + --MinGridX; + } + if (MinGridZ < 0) + { + --MinGridZ; + } + if (MaxGridX < 0) + { + --MaxGridX; + } + if (MaxGridZ < 0) + { + --MaxGridZ; + } + + // Walk the cache, move each structure that we want into a_Structures: + for (cStructurePtrs::iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end;) + { + if ( + ((*itr)->m_OriginX >= MinBlockX) && ((*itr)->m_OriginX < MaxBlockX) && + ((*itr)->m_OriginZ >= MinBlockZ) && ((*itr)->m_OriginZ < MaxBlockZ) + ) + { + // want + a_Structures.push_back(*itr); + itr = m_Cache.erase(itr); + } + else + { + // don't want + ++itr; + } + } // for itr - m_Cache[] + + // Create those structures that haven't been in the cache: + for (int x = MinGridX; x < MaxGridX; x++) + { + int OriginX = x * m_GridSizeX; + for (int z = MinGridZ; z < MaxGridZ; z++) + { + int OriginZ = z * m_GridSizeZ; + bool Found = false; + for (cStructurePtrs::const_iterator itr = a_Structures.begin(), end = a_Structures.end(); itr != end; ++itr) + { + if (((*itr)->m_OriginX == OriginX) && ((*itr)->m_OriginZ == OriginZ)) + { + Found = true; + break; + } + } // for itr - a_Structures[] + if (!Found) + { + a_Structures.push_back(CreateStructure(OriginX, OriginZ)); + } + } // for z + } // for x + + // Copy a_Forts into m_Cache to the beginning: + cStructurePtrs StructuresCopy (a_Structures); + m_Cache.splice(m_Cache.begin(), StructuresCopy, StructuresCopy.begin(), StructuresCopy.end()); + + // Trim the cache if it's too long: + size_t CacheSize = 0; + for (cStructurePtrs::iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end; ++itr) + { + CacheSize += (*itr)->GetCacheCost(); + if (CacheSize > m_MaxCacheSize) + { + // Erase all items from this one till the cache end + m_Cache.erase(itr, m_Cache.end()); + break; + } + } +} + + + + + +void cGridStructGen::GenFinish(cChunkDesc & a_ChunkDesc) +{ + int ChunkX = a_ChunkDesc.GetChunkX(); + int ChunkZ = a_ChunkDesc.GetChunkZ(); + cStructurePtrs Structures; + GetStructuresForChunk(ChunkX, ChunkZ, Structures); + for (cStructurePtrs::const_iterator itr = Structures.begin(); itr != Structures.end(); ++itr) + { + (*itr)->DrawIntoChunk(a_ChunkDesc); + } // for itr - Structures[] +} + + + + + diff --git a/src/Generating/GridStructGen.h b/src/Generating/GridStructGen.h new file mode 100644 index 000000000..234cc75c5 --- /dev/null +++ b/src/Generating/GridStructGen.h @@ -0,0 +1,124 @@ + +// GridStructGen.h + +// Declares the cGridStructGen class representing a common base class for structure generators that place structures in a semi-random grid + + + + + +#pragma once + +#include "ComposableGenerator.h" + + + + + +/** Generates structures in a semi-random grid. +Defines a grid in the XZ space with predefined cell size in each direction. Each cell then receives exactly +one structure (provided by the descendant class). The structure is placed within the cell, but doesn't need +to be bounded by the cell, it can be well outside the cell; the generator uses the MaxStructureSize parameter +to determine how far away from the cell the structure can be at most. +This class provides a cache for the structures generated for successive chunks and manages that cache. It +also provides the cFinishGen override that uses the cache to actually generate the structure into chunk data. + +After generating each chunk the cache is checked for size, each item in the cache has a cost associated with +it and the cache is trimmed (from its least-recently-used end) so that the sum of the cost in the cache is +less than m_MaxCacheSize + +To use this class, declare a descendant class that implements the overridable methods, then create an +instance of that class. The descendant must provide the CreateStructure() function that is called to generate +a structure at the specific grid cell. + +The descendant must use a specific cStructure descendant to provide the actual structure that gets generated. +The structure must provide the DrawIntoChunk() function that generates the structure into the chunk data, and +can override the GetCacheCost() function that returns the cost of that structure in the cache. +*/ +class cGridStructGen : + public cFinishGen +{ +public: + cGridStructGen( + int a_Seed, + int a_GridSizeX, int a_GridSizeZ, + int a_MaxStructureSizeX, int a_MaxStructureSizeZ, + size_t a_MaxCacheSize + ); + +protected: + /** Represents a single structure that occupies the grid point. Knows how to draw itself into a chunk. */ + class cStructure + { + public: + /** The origin (the coords of the gridpoint for which the structure is generated) */ + int m_OriginX, m_OriginZ; + + + /** Creates a structure that has its originset at the specified coords. */ + cStructure (int a_OriginX, int a_OriginZ) : + m_OriginX(a_OriginX), + m_OriginZ(a_OriginZ) + { + } + + // Force a virtual destructor in descendants: + virtual ~cStructure() {} + + /** Draws self into the specified chunk */ + virtual void DrawIntoChunk(cChunkDesc & a_ChunkDesc) = 0; + + /** Returns the cost of keeping this structure in the cache */ + virtual size_t GetCacheCost(void) const { return 1; } + } ; + typedef SharedPtr cStructurePtr; + typedef std::list cStructurePtrs; + + + /** Seed for generating the semi-random grid. */ + int m_Seed; + + /** The size of each grid's cell in the X axis */ + int m_GridSizeX; + + /** The size of each grid's cell in the Z axis */ + int m_GridSizeZ; + + /** Maximum theoretical size of the structure in the X axis. + This limits the structures considered for a single chunk, so the lesser the number, the better performance. + Structures large than this may get cropped. */ + int m_MaxStructureSizeX; + + /** Maximum theoretical size of the structure in the Z axis. + This limits the structures considered for a single chunk, so the lesser the number, the better performance. + Structures large than this may get cropped. */ + int m_MaxStructureSizeZ; + + /** Maximum allowed sum of costs for items in the cache. Items that are over this cost are removed from the + cache, oldest-first */ + size_t m_MaxCacheSize; + + /** Cache for the most recently generated structures, ordered by the recentness. */ + cStructurePtrs m_Cache; + + + /** Clears everything from the cache */ + void ClearCache(void); + + /** Returns all structures that may intersect the given chunk. + The structures are considered as intersecting iff their bounding box (defined by m_MaxStructureSize) + around their gridpoint intersects the chunk. */ + void GetStructuresForChunk(int a_ChunkX, int a_ChunkZ, cStructurePtrs & a_Structures); + + // cFinishGen overrides: + virtual void GenFinish(cChunkDesc & a_ChunkDesc) override; + + // Functions for the descendants to override: + /** Create a new structure at the specified gridpoint */ + virtual cStructurePtr CreateStructure(int a_OriginX, int a_OriginZ) = 0; +} ; + + + + + -- cgit v1.2.3 From ac1c3ba5d9ee407110700e2c5e9ce1d87e191e29 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 6 May 2014 21:43:31 +0200 Subject: Fixed an extra space. --- src/HTTPServer/HTTPServer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/HTTPServer/HTTPServer.cpp b/src/HTTPServer/HTTPServer.cpp index b78a538a2..d288c83c9 100644 --- a/src/HTTPServer/HTTPServer.cpp +++ b/src/HTTPServer/HTTPServer.cpp @@ -150,7 +150,7 @@ bool cHTTPServer::Initialize(const AString & a_PortsIPv4, const AString & a_Port { m_Cert.reset(new cX509Cert); int res = m_Cert->Parse(CertFile.data(), CertFile.size()); - if (res == 0) + if (res == 0) { m_CertPrivKey.reset(new cCryptoKey); int res2 = m_CertPrivKey->ParsePrivate(KeyFile.data(), KeyFile.size(), ""); -- cgit v1.2.3 From da5db2ddf9897d977716261e216e90ee8d61f2e7 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 7 May 2014 11:01:30 +0200 Subject: Initial cPrefabPiecePool refactoring. Ref.: #986. --- src/Generating/PrefabPiecePool.cpp | 121 +++++++++++++++++++++++++++++++++++++ src/Generating/PrefabPiecePool.h | 75 +++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 src/Generating/PrefabPiecePool.cpp create mode 100644 src/Generating/PrefabPiecePool.h (limited to 'src') diff --git a/src/Generating/PrefabPiecePool.cpp b/src/Generating/PrefabPiecePool.cpp new file mode 100644 index 000000000..ed9340815 --- /dev/null +++ b/src/Generating/PrefabPiecePool.cpp @@ -0,0 +1,121 @@ + +// PrefabPiecePool.cpp + +// Implements the cPrefabPiecePool class that represents a cPiecePool descendant that uses cPrefab instances as the pieces + +#include "Globals.h" +#include "PrefabPiecePool.h" + + + + + +cPrefabPiecePool::cPrefabPiecePool( + const cPrefab::sDef * a_PieceDefs, size_t a_NumPieceDefs, + const cPrefab::sDef * a_StartingPieceDefs, size_t a_NumStartingPieceDefs +) +{ + AddPieceDefs(a_PieceDefs, a_NumPieceDefs); + if (a_StartingPieceDefs != NULL) + { + AddStartingPieceDefs(a_StartingPieceDefs, a_NumStartingPieceDefs); + } +} + + + + + +void cPrefabPiecePool::AddPieceDefs(const cPrefab::sDef * a_PieceDefs, size_t a_NumPieceDefs) +{ + ASSERT(a_PieceDefs != NULL); + for (size_t i = 0; i < a_NumPieceDefs; i++) + { + cPrefab * Prefab = new cPrefab(a_PieceDefs[i]); + m_AllPieces.push_back(Prefab); + AddToPerConnectorMap(Prefab); + } +} + + + + + +void cPrefabPiecePool::AddStartingPieceDefs(const cPrefab::sDef * a_StartingPieceDefs, size_t a_NumStartingPieceDefs) +{ + ASSERT(a_StartingPieceDefs != NULL); + for (size_t i = 0; i < a_NumStartingPieceDefs; i++) + { + cPrefab * Prefab = new cPrefab(a_StartingPieceDefs[i]); + m_StartingPieces.push_back(Prefab); + } +} + + + + + +void cPrefabPiecePool::AddToPerConnectorMap(cPrefab * a_Prefab) +{ + cPiece::cConnectors Connectors = ((const cPiece *)a_Prefab)->GetConnectors(); + for (cPiece::cConnectors::const_iterator itr = Connectors.begin(), end = Connectors.end(); itr != end; ++itr) + { + m_PiecesByConnector[itr->m_Type].push_back(a_Prefab); + } +} + + + + +cPieces cPrefabPiecePool::GetPiecesWithConnector(int a_ConnectorType) +{ + return m_PiecesByConnector[a_ConnectorType]; +} + + + + + +cPieces cPrefabPiecePool::GetStartingPieces(void) +{ + if (m_StartingPieces.empty()) + { + return m_AllPieces; + } + else + { + return m_StartingPieces; + } +} + + + + + +int cPrefabPiecePool::GetPieceWeight(const cPlacedPiece & a_PlacedPiece, const cPiece::cConnector & a_ExistingConnector, const cPiece & a_NewPiece) +{ + return ((const cPrefab &)a_NewPiece).GetPieceWeight(a_PlacedPiece, a_ExistingConnector); +} + + + + + +void cPrefabPiecePool::PiecePlaced(const cPiece & a_Piece) +{ + // Do nothing + UNUSED(a_Piece); +} + + + + + +void cPrefabPiecePool::Reset(void) +{ + // Do nothing +} + + + + diff --git a/src/Generating/PrefabPiecePool.h b/src/Generating/PrefabPiecePool.h new file mode 100644 index 000000000..2a7993063 --- /dev/null +++ b/src/Generating/PrefabPiecePool.h @@ -0,0 +1,75 @@ + +// PrefabPiecePool.h + +// Declares the cPrefabPiecePool class that represents a cPiecePool descendant that uses cPrefab instances as the pieces + + + + + +#pragma once + +#include "PieceGenerator.h" +#include "Prefab.h" + + + + + +class cPrefabPiecePool : + public cPiecePool +{ +public: + /** Creates a piece pool with prefabs from the specified definitions. + If both a_PieceDefs and a_StartingPieceDefs are given, only the a_StartingPieceDefs are used as starting + pieces for the pool, and they do not participate in the generation any further. + If only a_PieceDefs is given, any such piece can be chosen as a starting piece, and all the pieces are used + for generating. */ + cPrefabPiecePool( + const cPrefab::sDef * a_PieceDefs, size_t a_NumPieceDefs, + const cPrefab::sDef * a_StartingPieceDefs, size_t a_NumStartingPieceDefs + ); + + /** Adds pieces from the specified definitions into m_AllPieces. Also adds the pieces into + the m_PiecesByConnector map. + May be called multiple times with different PieceDefs, will add all such pieces. */ + void AddPieceDefs(const cPrefab::sDef * a_PieceDefs, size_t a_NumPieceDefs); + + /** Adds pieces from the specified definitions into m_StartingPieces. Doesn't add to + the m_PiecesByConnector map. + May be called multiple times with different PieceDefs, will add all such pieces. */ + void AddStartingPieceDefs(const cPrefab::sDef * a_StartingPieceDefs, size_t a_NumStartingPieceDefs); + + +protected: + + /** The type used to map a connector type to the list of pieces with that connector */ + typedef std::map cPiecesMap; + + /** All the pieces that are allowed for building. + This is the list that's used for memory allocation and deallocation for the pieces. */ + cPieces m_AllPieces; + + /** The pieces that are used as starting pieces. + This list is not shared and the pieces need deallocation. */ + cPieces m_StartingPieces; + + /** The map that has all pieces by their connector types + The pieces are copies out of m_AllPieces and shouldn't be ever delete-d. */ + cPiecesMap m_PiecesByConnector; + + + /** Adds the prefab to the m_PiecesByConnector map for all its connectors. */ + void AddToPerConnectorMap(cPrefab * a_Prefab); + + // cPiecePool overrides: + virtual cPieces GetPiecesWithConnector(int a_ConnectorType) override; + virtual cPieces GetStartingPieces(void) override; + virtual int GetPieceWeight(const cPlacedPiece & a_PlacedPiece, const cPiece::cConnector & a_ExistingConnector, const cPiece & a_NewPiece) override; + virtual void PiecePlaced(const cPiece & a_Piece) override; + virtual void Reset(void) override; +} ; + + + + -- cgit v1.2.3 From 564261cfd9e385dfe6331c0585cae3b46989dd17 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 7 May 2014 22:34:06 +0200 Subject: cNetherFortGen uses cGridStructGen. WIP, this doesn't work properly yet. --- src/Generating/Caves.h | 2 +- src/Generating/GridStructGen.cpp | 12 +-- src/Generating/NetherFortGen.cpp | 223 ++++----------------------------------- src/Generating/NetherFortGen.h | 61 ++--------- src/Generating/PrefabPiecePool.h | 8 +- 5 files changed, 42 insertions(+), 264 deletions(-) (limited to 'src') diff --git a/src/Generating/Caves.h b/src/Generating/Caves.h index 7c45c056b..2a9fa01b8 100644 --- a/src/Generating/Caves.h +++ b/src/Generating/Caves.h @@ -93,7 +93,7 @@ protected: /// Returns all caves that *may* intersect the given chunk. All the caves are valid until the next call to this function. void GetCavesForChunk(int a_ChunkX, int a_ChunkZ, cCaveSystems & a_Caves); - // cStructGen override: + // cFinishGen override: virtual void GenFinish(cChunkDesc & a_ChunkDesc) override; } ; diff --git a/src/Generating/GridStructGen.cpp b/src/Generating/GridStructGen.cpp index 975fd3fdc..2f064d3a0 100644 --- a/src/Generating/GridStructGen.cpp +++ b/src/Generating/GridStructGen.cpp @@ -37,21 +37,21 @@ void cGridStructGen::GetStructuresForChunk(int a_ChunkX, int a_ChunkZ, cStructur int MaxBlockZ = a_ChunkZ * cChunkDef::Width + m_MaxStructureSizeZ + cChunkDef::Width - 1; int MinGridX = MinBlockX / m_GridSizeX; int MinGridZ = MinBlockZ / m_GridSizeZ; - int MaxGridX = MaxBlockX / m_GridSizeX; - int MaxGridZ = MaxBlockX / m_GridSizeZ; - if (MinGridX < 0) + int MaxGridX = (MaxBlockX + m_GridSizeX - 1) / m_GridSizeX; + int MaxGridZ = (MaxBlockZ + m_GridSizeZ - 1) / m_GridSizeZ; + if (MinBlockX < 0) { --MinGridX; } - if (MinGridZ < 0) + if (MinBlockZ < 0) { --MinGridZ; } - if (MaxGridX < 0) + if (MaxBlockX < 0) { --MaxGridX; } - if (MaxGridZ < 0) + if (MaxBlockZ < 0) { --MaxGridZ; } diff --git a/src/Generating/NetherFortGen.cpp b/src/Generating/NetherFortGen.cpp index d90fdeb0a..ac7fd0b7b 100644 --- a/src/Generating/NetherFortGen.cpp +++ b/src/Generating/NetherFortGen.cpp @@ -20,20 +20,21 @@ static const int NEIGHBORHOOD_SIZE = 3; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cNetherFortGen::cNetherFort: -class cNetherFortGen::cNetherFort +class cNetherFortGen::cNetherFort : + public cGridStructGen::cStructure { + typedef cGridStructGen::cStructure super; + public: cNetherFortGen & m_ParentGen; - int m_BlockX, m_BlockZ; int m_GridSize; int m_Seed; cPlacedPieces m_Pieces; - cNetherFort(cNetherFortGen & a_ParentGen, int a_BlockX, int a_BlockZ, int a_GridSize, int a_MaxDepth, int a_Seed) : + cNetherFort(cNetherFortGen & a_ParentGen, int a_OriginX, int a_OriginZ, int a_GridSize, int a_MaxDepth, int a_Seed) : + super(a_OriginX, a_OriginZ), m_ParentGen(a_ParentGen), - m_BlockX(a_BlockX), - m_BlockZ(a_BlockZ), m_GridSize(a_GridSize), m_Seed(a_Seed) { @@ -43,8 +44,8 @@ public: // Generate pieces: for (int i = 0; m_Pieces.size() < (size_t)(a_MaxDepth * a_MaxDepth / 8 + a_MaxDepth); i++) { - cBFSPieceGenerator pg(m_ParentGen, a_Seed + i); - pg.PlacePieces(a_BlockX, BlockY, a_BlockZ, a_MaxDepth, m_Pieces); + cBFSPieceGenerator pg(cNetherFortGen::m_PiecePool, a_Seed + i); + pg.PlacePieces(a_OriginX, BlockY, a_OriginZ, a_MaxDepth, m_Pieces); } } @@ -56,7 +57,7 @@ public: /** Carves the system into the chunk data */ - void ProcessChunk(cChunkDesc & a_Chunk) + virtual void DrawIntoChunk(cChunkDesc & a_Chunk) { for (cPlacedPieces::const_iterator itr = m_Pieces.begin(), end = m_Pieces.end(); itr != end; ++itr) { @@ -107,214 +108,30 @@ public: /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cNetherFortGen: +cPrefabPiecePool cNetherFortGen::m_PiecePool(g_NetherFortPrefabs, g_NetherFortPrefabsCount, g_NetherFortStartingPrefabs, g_NetherFortStartingPrefabsCount); + + + + + cNetherFortGen::cNetherFortGen(int a_Seed, int a_GridSize, int a_MaxDepth) : - m_Seed(a_Seed), - m_Noise(a_Seed), - m_GridSize(a_GridSize), + super(a_Seed, a_GridSize, a_GridSize, a_MaxDepth * 10, a_MaxDepth * 10, 200), m_MaxDepth(a_MaxDepth) { - // Initialize the prefabs: - for (size_t i = 0; i < g_NetherFortPrefabsCount; i++) - { - cPrefab * Prefab = new cPrefab(g_NetherFortPrefabs[i]); - m_AllPieces.push_back(Prefab); - if (Prefab->HasConnectorType(0)) - { - m_OuterPieces.push_back(Prefab); - } - if (Prefab->HasConnectorType(1)) - { - m_InnerPieces.push_back(Prefab); - } - } - - // Initialize the starting piece prefabs: - for (size_t i = 0; i < g_NetherFortStartingPrefabsCount; i++) - { - m_StartingPieces.push_back(new cPrefab(g_NetherFortStartingPrefabs[i])); - } - /* // DEBUG: Try one round of placement: cPlacedPieces Pieces; - cBFSPieceGenerator pg(*this, a_Seed); + cBFSPieceGenerator pg(m_PiecePool, a_Seed); pg.PlacePieces(0, 64, 0, a_MaxDepth, Pieces); - */ -} - - - - - -cNetherFortGen::~cNetherFortGen() -{ - ClearCache(); - for (cPieces::iterator itr = m_AllPieces.begin(), end = m_AllPieces.end(); itr != end; ++itr) - { - delete *itr; - } // for itr - m_AllPieces[] - m_AllPieces.clear(); + //*/ } -void cNetherFortGen::ClearCache(void) +cGridStructGen::cStructurePtr cNetherFortGen::CreateStructure(int a_OriginX, int a_OriginZ) { - // TODO + return cStructurePtr(new cNetherFort(*this, a_OriginX, a_OriginZ, m_GridSizeX, m_MaxDepth, m_Seed)); } - - - - -void cNetherFortGen::GetFortsForChunk(int a_ChunkX, int a_ChunkZ, cNetherForts & a_Forts) -{ - int BaseX = a_ChunkX * cChunkDef::Width / m_GridSize; - int BaseZ = a_ChunkZ * cChunkDef::Width / m_GridSize; - if (BaseX < 0) - { - --BaseX; - } - if (BaseZ < 0) - { - --BaseZ; - } - BaseX -= NEIGHBORHOOD_SIZE / 2; - BaseZ -= NEIGHBORHOOD_SIZE / 2; - - // Walk the cache, move each cave system that we want into a_Forts: - int StartX = BaseX * m_GridSize; - int EndX = (BaseX + NEIGHBORHOOD_SIZE + 1) * m_GridSize; - int StartZ = BaseZ * m_GridSize; - int EndZ = (BaseZ + NEIGHBORHOOD_SIZE + 1) * m_GridSize; - for (cNetherForts::iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end;) - { - if ( - ((*itr)->m_BlockX >= StartX) && ((*itr)->m_BlockX < EndX) && - ((*itr)->m_BlockZ >= StartZ) && ((*itr)->m_BlockZ < EndZ) - ) - { - // want - a_Forts.push_back(*itr); - itr = m_Cache.erase(itr); - } - else - { - // don't want - ++itr; - } - } // for itr - m_Cache[] - - // Create those forts that haven't been in the cache: - for (int x = 0; x < NEIGHBORHOOD_SIZE; x++) - { - int RealX = (BaseX + x) * m_GridSize; - for (int z = 0; z < NEIGHBORHOOD_SIZE; z++) - { - int RealZ = (BaseZ + z) * m_GridSize; - bool Found = false; - for (cNetherForts::const_iterator itr = a_Forts.begin(), end = a_Forts.end(); itr != end; ++itr) - { - if (((*itr)->m_BlockX == RealX) && ((*itr)->m_BlockZ == RealZ)) - { - Found = true; - break; - } - } // for itr - a_Mineshafts - if (!Found) - { - a_Forts.push_back(new cNetherFort(*this, RealX, RealZ, m_GridSize, m_MaxDepth, m_Seed)); - } - } // for z - } // for x - - // Copy a_Forts into m_Cache to the beginning: - cNetherForts FortsCopy (a_Forts); - m_Cache.splice(m_Cache.begin(), FortsCopy, FortsCopy.begin(), FortsCopy.end()); - - // Trim the cache if it's too long: - if (m_Cache.size() > 100) - { - cNetherForts::iterator itr = m_Cache.begin(); - std::advance(itr, 100); - for (cNetherForts::iterator end = m_Cache.end(); itr != end; ++itr) - { - delete *itr; - } - itr = m_Cache.begin(); - std::advance(itr, 100); - m_Cache.erase(itr, m_Cache.end()); - } -} - - - - - -void cNetherFortGen::GenFinish(cChunkDesc & a_ChunkDesc) -{ - int ChunkX = a_ChunkDesc.GetChunkX(); - int ChunkZ = a_ChunkDesc.GetChunkZ(); - cNetherForts Forts; - GetFortsForChunk(ChunkX, ChunkZ, Forts); - for (cNetherForts::const_iterator itr = Forts.begin(); itr != Forts.end(); ++itr) - { - (*itr)->ProcessChunk(a_ChunkDesc); - } // for itr - Forts[] -} - - - - - -cPieces cNetherFortGen::GetPiecesWithConnector(int a_ConnectorType) -{ - switch (a_ConnectorType) - { - case 0: return m_OuterPieces; - case 1: return m_InnerPieces; - default: return cPieces(); - } -} - - - - - -cPieces cNetherFortGen::GetStartingPieces(void) -{ - return m_StartingPieces; -} - - - - - -int cNetherFortGen::GetPieceWeight(const cPlacedPiece & a_PlacedPiece, const cPiece::cConnector & a_ExistingConnector, const cPiece & a_NewPiece) -{ - return ((const cPrefab &)a_NewPiece).GetPieceWeight(a_PlacedPiece, a_ExistingConnector); -} - - - - - -void cNetherFortGen::PiecePlaced(const cPiece & a_Piece) -{ - UNUSED(a_Piece); -} - - - - - -void cNetherFortGen::Reset(void) -{ - // Nothing needed -} - - - - diff --git a/src/Generating/NetherFortGen.h b/src/Generating/NetherFortGen.h index d51596b9e..f35801a3c 100644 --- a/src/Generating/NetherFortGen.h +++ b/src/Generating/NetherFortGen.h @@ -10,77 +10,34 @@ #pragma once #include "ComposableGenerator.h" -#include "PieceGenerator.h" +#include "PrefabPiecePool.h" +#include "GridStructGen.h" class cNetherFortGen : - public cFinishGen, - public cPiecePool + public cGridStructGen { + typedef cGridStructGen super; + public: cNetherFortGen(int a_Seed, int a_GridSize, int a_MaxDepth); - virtual ~cNetherFortGen(); - protected: friend class cNetherFortPerfTest; // fwd: NetherFortGen.cpp class cNetherFort; // fwd: NetherFortGen.cpp - typedef std::list cNetherForts; - - - /** The seed used for generating*/ - int m_Seed; - - /** The noise used for generating */ - cNoise m_Noise; - - /** Average spacing between the fortresses*/ - int m_GridSize; /** Maximum depth of the piece-generator tree */ int m_MaxDepth; - - /** Cache of the most recently used systems. MoveToFront used. */ - cNetherForts m_Cache; - /** All the pieces that are allowed for building. - This is the list that's used for memory allocation and deallocation for the pieces. */ - cPieces m_AllPieces; + /** The pool of pieces to use for generating. Static, so that it's shared by multiple generators. */ + static cPrefabPiecePool m_PiecePool; - /** The pieces that are used as starting pieces. - This list is not shared and the pieces need deallocation. */ - cPieces m_StartingPieces; - - /** The pieces that have an "outer" connector. - The pieces are copies out of m_AllPieces and shouldn't be ever delete-d. */ - cPieces m_OuterPieces; - - /** The pieces that have an "inner" connector. - The pieces are copies out of m_AllPieces and shouldn't be ever delete-d. */ - cPieces m_InnerPieces; - - /** Clears everything from the cache. - Also invalidates the forst returned by GetFortsForChunk(). */ - void ClearCache(void); - - /** Returns all forts that *may* intersect the given chunk. - The returned forts live within m_Cache.They are valid until the next call - to this function (which may delete some of the pointers). */ - void GetFortsForChunk(int a_ChunkX, int a_ChunkZ, cNetherForts & a_Forts); - - // cFinishGen overrides: - virtual void GenFinish(cChunkDesc & a_ChunkDesc) override; - - // cPiecePool overrides: - virtual cPieces GetPiecesWithConnector(int a_ConnectorType) override; - virtual cPieces GetStartingPieces(void) override; - virtual int GetPieceWeight(const cPlacedPiece & a_PlacedPiece, const cPiece::cConnector & a_ExistingConnector, const cPiece & a_NewPiece) override; - virtual void PiecePlaced(const cPiece & a_Piece) override; - virtual void Reset(void) override; + // cGridStructGen overrides: + virtual cStructurePtr CreateStructure(int a_OriginX, int a_OriginZ) override; } ; diff --git a/src/Generating/PrefabPiecePool.h b/src/Generating/PrefabPiecePool.h index 2a7993063..c6a5ad360 100644 --- a/src/Generating/PrefabPiecePool.h +++ b/src/Generating/PrefabPiecePool.h @@ -20,11 +20,15 @@ class cPrefabPiecePool : public cPiecePool { public: + /** Creates an empty instance. Prefabs can be added by calling AddPieceDefs() and AddStartingPieceDefs(). */ + cPrefabPiecePool(void); + /** Creates a piece pool with prefabs from the specified definitions. If both a_PieceDefs and a_StartingPieceDefs are given, only the a_StartingPieceDefs are used as starting pieces for the pool, and they do not participate in the generation any further. If only a_PieceDefs is given, any such piece can be chosen as a starting piece, and all the pieces are used - for generating. */ + for generating. + More pieces can be added to the instance afterwards by calling AddPieceDefs() and AddStartingPieceDefs(). */ cPrefabPiecePool( const cPrefab::sDef * a_PieceDefs, size_t a_NumPieceDefs, const cPrefab::sDef * a_StartingPieceDefs, size_t a_NumStartingPieceDefs @@ -35,7 +39,7 @@ public: May be called multiple times with different PieceDefs, will add all such pieces. */ void AddPieceDefs(const cPrefab::sDef * a_PieceDefs, size_t a_NumPieceDefs); - /** Adds pieces from the specified definitions into m_StartingPieces. Doesn't add to + /** Adds pieces from the specified definitions into m_StartingPieces. Doesn't add them to the m_PiecesByConnector map. May be called multiple times with different PieceDefs, will add all such pieces. */ void AddStartingPieceDefs(const cPrefab::sDef * a_StartingPieceDefs, size_t a_NumStartingPieceDefs); -- cgit v1.2.3 From c4ea25e136026004526a63516cbed498e2b6fabe Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 8 May 2014 20:16:35 +0200 Subject: Fixed MSVC 64-bit build warnings. --- src/Bindings/PluginManager.cpp | 9 +++---- src/Bindings/PluginManager.h | 2 +- src/BlockArea.cpp | 10 ++++---- src/Blocks/BlockFarmland.h | 4 ++-- src/Blocks/BlockLeaves.h | 6 ++--- src/Blocks/BlockSlab.h | 2 +- src/Generating/Caves.cpp | 10 ++++++-- src/Generating/MineShafts.cpp | 6 ++--- src/Generating/PieceGenerator.cpp | 4 ++-- src/Generating/Ravines.cpp | 10 ++++++-- src/Generating/Trees.cpp | 2 +- src/GroupManager.cpp | 35 ++++++++++++++-------------- src/HTTPServer/NameValueParser.cpp | 2 +- src/Map.cpp | 2 +- src/Map.h | 2 +- src/MapManager.cpp | 6 ++--- src/MapManager.h | 2 +- src/MobCensus.cpp | 2 +- src/MobFamilyCollecter.cpp | 2 +- src/MobSpawner.cpp | 6 ++--- src/Noise.cpp | 2 +- src/OSSupport/ListenThread.cpp | 2 +- src/OSSupport/Socket.cpp | 8 +++---- src/OSSupport/Socket.h | 4 ++-- src/OSSupport/SocketThreads.cpp | 2 +- src/ProbabDistrib.cpp | 2 +- src/Protocol/Protocol17x.cpp | 18 +++++++------- src/Protocol/ProtocolRecognizer.cpp | 4 ++-- src/RCONServer.cpp | 2 +- src/Root.cpp | 14 +++++------ src/Scoreboard.cpp | 6 ++--- src/Scoreboard.h | 6 ++--- src/Simulator/DelayedFluidSimulator.cpp | 2 +- src/Simulator/SandSimulator.cpp | 2 +- src/StringCompression.cpp | 14 +++++------ src/StringCompression.h | 4 ++-- src/StringUtils.cpp | 24 +++++++++++-------- src/StringUtils.h | 4 ++-- src/UI/SlotArea.cpp | 2 +- src/UI/Window.cpp | 2 +- src/World.cpp | 7 +++--- src/WorldStorage/FastNBT.cpp | 30 +++++++++++++----------- src/WorldStorage/FastNBT.h | 8 +++---- src/WorldStorage/SchematicFileSerializer.cpp | 4 ++-- src/WorldStorage/WSSAnvil.cpp | 8 +++---- src/WorldStorage/WSSCompact.cpp | 24 +++++++++---------- 46 files changed, 173 insertions(+), 156 deletions(-) (limited to 'src') diff --git a/src/Bindings/PluginManager.cpp b/src/Bindings/PluginManager.cpp index aaccc606d..310ecc7e8 100644 --- a/src/Bindings/PluginManager.cpp +++ b/src/Bindings/PluginManager.cpp @@ -143,13 +143,14 @@ void cPluginManager::ReloadPluginsNow(cIniFile & a_SettingsIni) } } - if (GetNumPlugins() == 0) + size_t NumLoadedPlugins = GetNumPlugins(); + if (NumLoadedPlugins) { LOG("-- No Plugins Loaded --"); } - else if (GetNumPlugins() > 1) + else if (NumLoadedPlugins > 1) { - LOG("-- Loaded %i Plugins --", GetNumPlugins()); + LOG("-- Loaded %i Plugins --", (int)NumLoadedPlugins); } else { @@ -1869,7 +1870,7 @@ void cPluginManager::AddHook(cPlugin * a_Plugin, int a_Hook) -unsigned int cPluginManager::GetNumPlugins() const +size_t cPluginManager::GetNumPlugins() const { return m_Plugins.size(); } diff --git a/src/Bindings/PluginManager.h b/src/Bindings/PluginManager.h index 58c1cebb4..3b3091957 100644 --- a/src/Bindings/PluginManager.h +++ b/src/Bindings/PluginManager.h @@ -159,7 +159,7 @@ public: // tolua_export /** Adds the plugin to the list of plugins called for the specified hook type. Handles multiple adds as a single add */ void AddHook(cPlugin * a_Plugin, int a_HookType); - unsigned int GetNumPlugins() const; // tolua_export + size_t GetNumPlugins() const; // tolua_export // Calls for individual hooks. Each returns false if the action is to continue or true if the plugin wants to abort bool CallHookBlockSpread (cWorld * a_World, int a_BlockX, int a_BlockY, int a_BlockZ, eSpreadSource a_Source); diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index 897af27c4..9e20a0983 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -738,31 +738,31 @@ void cBlockArea::Fill(int a_DataTypes, BLOCKTYPE a_BlockType, NIBBLETYPE a_Block a_DataTypes = a_DataTypes & GetDataTypes(); } - int BlockCount = GetBlockCount(); + size_t BlockCount = GetBlockCount(); if ((a_DataTypes & baTypes) != 0) { - for (int i = 0; i < BlockCount; i++) + for (size_t i = 0; i < BlockCount; i++) { m_BlockTypes[i] = a_BlockType; } } if ((a_DataTypes & baMetas) != 0) { - for (int i = 0; i < BlockCount; i++) + for (size_t i = 0; i < BlockCount; i++) { m_BlockMetas[i] = a_BlockMeta; } } if ((a_DataTypes & baLight) != 0) { - for (int i = 0; i < BlockCount; i++) + for (size_t i = 0; i < BlockCount; i++) { m_BlockLight[i] = a_BlockLight; } } if ((a_DataTypes & baSkyLight) != 0) { - for (int i = 0; i < BlockCount; i++) + for (size_t i = 0; i < BlockCount; i++) { m_BlockSkyLight[i] = a_BlockSkyLight; } diff --git a/src/Blocks/BlockFarmland.h b/src/Blocks/BlockFarmland.h index b720ccd14..3dd5bcd1d 100644 --- a/src/Blocks/BlockFarmland.h +++ b/src/Blocks/BlockFarmland.h @@ -52,9 +52,9 @@ public: return; } - int NumBlocks = Area.GetBlockCount(); + size_t NumBlocks = Area.GetBlockCount(); BLOCKTYPE * BlockTypes = Area.GetBlockTypes(); - for (int i = 0; i < NumBlocks; i++) + for (size_t i = 0; i < NumBlocks; i++) { if ( (BlockTypes[i] == E_BLOCK_WATER) || diff --git a/src/Blocks/BlockLeaves.h b/src/Blocks/BlockLeaves.h index d21227b07..495e849fa 100644 --- a/src/Blocks/BlockLeaves.h +++ b/src/Blocks/BlockLeaves.h @@ -138,14 +138,14 @@ bool HasNearLog(cBlockArea & a_Area, int a_BlockX, int a_BlockY, int a_BlockZ) { // Filter the blocks into a {leaves, log, other (air)} set: BLOCKTYPE * Types = a_Area.GetBlockTypes(); - for (int i = a_Area.GetBlockCount() - 1; i > 0; i--) + for (size_t i = a_Area.GetBlockCount() - 1; i > 0; i--) { switch (Types[i]) { - case E_BLOCK_NEW_LEAVES: - case E_BLOCK_NEW_LOG: case E_BLOCK_LEAVES: case E_BLOCK_LOG: + case E_BLOCK_NEW_LEAVES: + case E_BLOCK_NEW_LOG: { break; } diff --git a/src/Blocks/BlockSlab.h b/src/Blocks/BlockSlab.h index b6bd12588..80841b094 100644 --- a/src/Blocks/BlockSlab.h +++ b/src/Blocks/BlockSlab.h @@ -100,7 +100,7 @@ public: virtual bool CanDirtGrowGrass(NIBBLETYPE a_Meta) override { - return (a_Meta & 0x8); + return ((a_Meta & 0x8) != 0); } diff --git a/src/Generating/Caves.cpp b/src/Generating/Caves.cpp index 5cad11d2a..6df22a14a 100644 --- a/src/Generating/Caves.cpp +++ b/src/Generating/Caves.cpp @@ -239,9 +239,15 @@ void cCaveTunnel::Randomize(cNoise & a_Noise) bool cCaveTunnel::RefineDefPoints(const cCaveDefPoints & a_Src, cCaveDefPoints & a_Dst) { + if (a_Src.size() < 2) + { + // There are no midpoints, nothing to smooth + return true; + } + // Smoothing: for each line segment, add points on its 1/4 lengths bool res = false; - int Num = a_Src.size() - 2; // this many intermediary points + size_t Num = a_Src.size() - 2; // this many intermediary points a_Dst.clear(); a_Dst.reserve(Num * 2 + 2); cCaveDefPoints::const_iterator itr = a_Src.begin() + 1; @@ -251,7 +257,7 @@ bool cCaveTunnel::RefineDefPoints(const cCaveDefPoints & a_Src, cCaveDefPoints & int PrevY = Source.m_BlockY; int PrevZ = Source.m_BlockZ; int PrevR = Source.m_Radius; - for (int i = 0; i <= Num; ++i, ++itr) + for (size_t i = 0; i <= Num; ++i, ++itr) { int dx = itr->m_BlockX - PrevX; int dy = itr->m_BlockY - PrevY; diff --git a/src/Generating/MineShafts.cpp b/src/Generating/MineShafts.cpp index 231295c3f..af63e549b 100644 --- a/src/Generating/MineShafts.cpp +++ b/src/Generating/MineShafts.cpp @@ -543,7 +543,7 @@ cMineShaft * cMineShaftCorridor::CreateAndFit( { cCuboid BoundingBox(a_PivotX, a_PivotY - 1, a_PivotZ); BoundingBox.p2.y += 3; - int rnd = a_Noise.IntNoise3DInt(a_PivotX, a_PivotY + a_ParentSystem.m_MineShafts.size(), a_PivotZ) / 7; + int rnd = a_Noise.IntNoise3DInt(a_PivotX, a_PivotY + (int)a_ParentSystem.m_MineShafts.size(), a_PivotZ) / 7; int NumSegments = 2 + (rnd) % (MAX_SEGMENTS - 1); // 2 .. MAX_SEGMENTS switch (a_Direction) { @@ -985,7 +985,7 @@ cMineShaft * cMineShaftCrossing::CreateAndFit( ) { cCuboid BoundingBox(a_PivotX, a_PivotY - 1, a_PivotZ); - int rnd = a_Noise.IntNoise3DInt(a_PivotX, a_PivotY + a_ParentSystem.m_MineShafts.size(), a_PivotZ) / 7; + int rnd = a_Noise.IntNoise3DInt(a_PivotX, a_PivotY + (int)a_ParentSystem.m_MineShafts.size(), a_PivotZ) / 7; BoundingBox.p2.y += 3; if ((rnd % 4) < 2) { @@ -1127,7 +1127,7 @@ cMineShaft * cMineShaftStaircase::CreateAndFit( cNoise & a_Noise ) { - int rnd = a_Noise.IntNoise3DInt(a_PivotX, a_PivotY + a_ParentSystem.m_MineShafts.size(), a_PivotZ) / 7; + int rnd = a_Noise.IntNoise3DInt(a_PivotX, a_PivotY + (int)a_ParentSystem.m_MineShafts.size(), a_PivotZ) / 7; cCuboid Box; switch (a_Direction) { diff --git a/src/Generating/PieceGenerator.cpp b/src/Generating/PieceGenerator.cpp index db45cd08b..ce19c1c95 100644 --- a/src/Generating/PieceGenerator.cpp +++ b/src/Generating/PieceGenerator.cpp @@ -339,9 +339,9 @@ cPlacedPiece * cPieceGenerator::PlaceStartingPiece(int a_BlockX, int a_BlockY, i int NumRotations = 1; for (size_t i = 1; i < ARRAYCOUNT(Rotations); i++) { - if (StartingPiece->CanRotateCCW(i)) + if (StartingPiece->CanRotateCCW((int)i)) { - Rotations[NumRotations] = i; + Rotations[NumRotations] = (int)i; NumRotations += 1; } } diff --git a/src/Generating/Ravines.cpp b/src/Generating/Ravines.cpp index 267dcbbf9..a036cf25f 100644 --- a/src/Generating/Ravines.cpp +++ b/src/Generating/Ravines.cpp @@ -306,8 +306,14 @@ void cStructGenRavines::cRavine::GenerateBaseDefPoints(int a_BlockX, int a_Block void cStructGenRavines::cRavine::RefineDefPoints(const cRavDefPoints & a_Src, cRavDefPoints & a_Dst) { + if (a_Src.size() < 2) + { + // No midpoints, nothing to refine + return; + } + // Smoothing: for each line segment, add points on its 1/4 lengths - int Num = a_Src.size() - 2; // this many intermediary points + size_t Num = a_Src.size() - 2; // this many intermediary points a_Dst.clear(); a_Dst.reserve(Num * 2 + 2); cRavDefPoints::const_iterator itr = a_Src.begin() + 1; @@ -318,7 +324,7 @@ void cStructGenRavines::cRavine::RefineDefPoints(const cRavDefPoints & a_Src, cR int PrevR = Source.m_Radius; int PrevT = Source.m_Top; int PrevB = Source.m_Bottom; - for (int i = 0; i <= Num; ++i, ++itr) + for (size_t i = 0; i <= Num; ++i, ++itr) { int dx = itr->m_BlockX - PrevX; int dz = itr->m_BlockZ - PrevZ; diff --git a/src/Generating/Trees.cpp b/src/Generating/Trees.cpp index 4909587b1..4f1553c36 100644 --- a/src/Generating/Trees.cpp +++ b/src/Generating/Trees.cpp @@ -136,7 +136,7 @@ inline void PushSomeColumns(int a_BlockX, int a_Height, int a_BlockZ, int a_Colu { int x = a_BlockX + a_Coords[i].x; int z = a_BlockZ + a_Coords[i].z; - if (a_Noise.IntNoise3DInt(x + 64 * a_Seq, a_Height + i, z + 64 * a_Seq) <= a_Chance) + if (a_Noise.IntNoise3DInt(x + 64 * a_Seq, a_Height + (int)i, z + 64 * a_Seq) <= a_Chance) { for (int j = 0; j < a_ColumnHeight; j++) { diff --git a/src/GroupManager.cpp b/src/GroupManager.cpp index 33b601e82..3586560bf 100644 --- a/src/GroupManager.cpp +++ b/src/GroupManager.cpp @@ -79,23 +79,24 @@ void cGroupManager::CheckUsers(void) return; } - unsigned int NumKeys = IniFile.GetNumKeys(); - for (size_t i = 0; i < NumKeys; i++) + int NumKeys = IniFile.GetNumKeys(); + for (int i = 0; i < NumKeys; i++) { - AString Player = IniFile.GetKeyName( i ); + AString Player = IniFile.GetKeyName(i); AString Groups = IniFile.GetValue(Player, "Groups", ""); - if (!Groups.empty()) + if (Groups.empty()) + { + continue; + } + AStringVector Split = StringSplitAndTrim(Groups, ","); + for (AStringVector::const_iterator itr = Split.begin(), end = Split.end(); itr != end; ++itr) { - AStringVector Split = StringSplit( Groups, "," ); - for( unsigned int i = 0; i < Split.size(); i++ ) + if (!ExistsGroup(*itr)) { - if (!ExistsGroup(Split[i])) - { - LOGWARNING("The group %s for player %s was not found!", Split[i].c_str(), Player.c_str()); - } + LOGWARNING("The group %s for player %s was not found!", Split[i].c_str(), Player.c_str()); } - } - } + } // for itr - Split[] + } // for i - ini file keys } @@ -128,15 +129,15 @@ void cGroupManager::LoadGroups() IniFile.WriteFile("groups.ini"); } - unsigned int NumKeys = IniFile.GetNumKeys(); - for (size_t i = 0; i < NumKeys; i++) + int NumKeys = IniFile.GetNumKeys(); + for (int i = 0; i < NumKeys; i++) { - AString KeyName = IniFile.GetKeyName( i ); - cGroup* Group = GetGroup( KeyName.c_str() ); + AString KeyName = IniFile.GetKeyName(i); + cGroup * Group = GetGroup(KeyName.c_str()); Group->ClearPermission(); // Needed in case the groups are reloaded. - LOGD("Loading group: %s", KeyName.c_str() ); + LOGD("Loading group %s", KeyName.c_str()); Group->SetName(KeyName); AString Color = IniFile.GetValue(KeyName, "Color", "-"); diff --git a/src/HTTPServer/NameValueParser.cpp b/src/HTTPServer/NameValueParser.cpp index 3f6c17dda..f16ea1915 100644 --- a/src/HTTPServer/NameValueParser.cpp +++ b/src/HTTPServer/NameValueParser.cpp @@ -97,7 +97,7 @@ void cNameValueParser::Parse(const char * a_Data, size_t a_Size) { ASSERT(m_State != psFinished); // Calling Parse() after Finish() is wrong! - int Last = 0; + size_t Last = 0; for (size_t i = 0; i < a_Size;) { switch (m_State) diff --git a/src/Map.cpp b/src/Map.cpp index 79370b097..7721baa70 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -614,7 +614,7 @@ unsigned int cMap::GetNumPixels(void) const -unsigned int cMap::GetNumDecorators(void) const +size_t cMap::GetNumDecorators(void) const { return m_Decorators.size(); } diff --git a/src/Map.h b/src/Map.h index ee7c537b1..e23ca2c92 100644 --- a/src/Map.h +++ b/src/Map.h @@ -181,7 +181,7 @@ public: // tolua_end - unsigned int GetNumDecorators(void) const; + size_t GetNumDecorators(void) const; const cColorList & GetData(void) const { return m_Data; } diff --git a/src/MapManager.cpp b/src/MapManager.cpp index 9d02eafb4..e7df75118 100644 --- a/src/MapManager.cpp +++ b/src/MapManager.cpp @@ -86,7 +86,7 @@ cMap * cMapManager::CreateMap(int a_CenterX, int a_CenterY, int a_Scale) return NULL; } - cMap Map(m_MapData.size(), a_CenterX, a_CenterY, m_World, a_Scale); + cMap Map((unsigned)m_MapData.size(), a_CenterX, a_CenterY, m_World, a_Scale); m_MapData.push_back(Map); @@ -97,7 +97,7 @@ cMap * cMapManager::CreateMap(int a_CenterX, int a_CenterY, int a_Scale) -unsigned int cMapManager::GetNumMaps(void) const +size_t cMapManager::GetNumMaps(void) const { return m_MapData.size(); } @@ -151,7 +151,7 @@ void cMapManager::SaveMapData(void) cIDCountSerializer IDSerializer(m_World->GetName()); - IDSerializer.SetMapCount(m_MapData.size()); + IDSerializer.SetMapCount((unsigned)m_MapData.size()); if (!IDSerializer.Save()) { diff --git a/src/MapManager.h b/src/MapManager.h index 80e6d16d1..ceab8f126 100644 --- a/src/MapManager.h +++ b/src/MapManager.h @@ -53,7 +53,7 @@ public: */ bool ForEachMap(cMapCallback & a_Callback); - unsigned int GetNumMaps(void) const; // tolua_export + size_t GetNumMaps(void) const; // tolua_export /** Loads the map data from the disk */ void LoadMapData(void); diff --git a/src/MobCensus.cpp b/src/MobCensus.cpp index 9c32bf695..23f74b59e 100644 --- a/src/MobCensus.cpp +++ b/src/MobCensus.cpp @@ -64,7 +64,7 @@ void cMobCensus::CollectSpawnableChunk(cChunk & a_Chunk) int cMobCensus::GetNumChunks(void) { - return m_EligibleForSpawnChunks.size(); + return (int)m_EligibleForSpawnChunks.size(); } diff --git a/src/MobFamilyCollecter.cpp b/src/MobFamilyCollecter.cpp index e9c69e078..6da330c83 100644 --- a/src/MobFamilyCollecter.cpp +++ b/src/MobFamilyCollecter.cpp @@ -18,7 +18,7 @@ void cMobFamilyCollecter::CollectMob(cMonster & a_Monster) int cMobFamilyCollecter::GetNumberOfCollectedMobs(cMonster::eFamily a_Family) { - return m_Mobs[a_Family].size(); + return (int)m_Mobs[a_Family].size(); } diff --git a/src/MobSpawner.cpp b/src/MobSpawner.cpp index ce8e06777..de8e01b8a 100644 --- a/src/MobSpawner.cpp +++ b/src/MobSpawner.cpp @@ -104,13 +104,13 @@ cMonster::eType cMobSpawner::ChooseMobType(EMCSBiome a_Biome) } } - int allowedMobsSize = allowedMobs.size(); + size_t allowedMobsSize = allowedMobs.size(); if (allowedMobsSize > 0) { std::set::iterator itr = allowedMobs.begin(); - int iRandom = m_Random.NextInt(allowedMobsSize,a_Biome); + int iRandom = m_Random.NextInt((int)allowedMobsSize, a_Biome); - for(int i = 0; i < iRandom; i++) + for (int i = 0; i < iRandom; i++) { ++itr; } diff --git a/src/Noise.cpp b/src/Noise.cpp index 13a194938..89115d992 100644 --- a/src/Noise.cpp +++ b/src/Noise.cpp @@ -814,7 +814,7 @@ void cPerlinNoise::SetSeed(int a_Seed) void cPerlinNoise::AddOctave(float a_Frequency, float a_Amplitude) { - m_Octaves.push_back(cOctave(m_Seed * (m_Octaves.size() + 4) * 4 + 1024, a_Frequency, a_Amplitude)); + m_Octaves.push_back(cOctave(m_Seed * ((int)m_Octaves.size() + 4) * 4 + 1024, a_Frequency, a_Amplitude)); } diff --git a/src/OSSupport/ListenThread.cpp b/src/OSSupport/ListenThread.cpp index ba3198764..02e98acfc 100644 --- a/src/OSSupport/ListenThread.cpp +++ b/src/OSSupport/ListenThread.cpp @@ -214,7 +214,7 @@ void cListenThread::Execute(void) timeval tv; // On Linux select() doesn't seem to wake up when socket is closed, so let's kinda busy-wait: tv.tv_sec = 1; tv.tv_usec = 0; - if (select(Highest + 1, &fdRead, NULL, NULL, &tv) == -1) + if (select((int)Highest + 1, &fdRead, NULL, NULL, &tv) == -1) { LOG("select(R) call failed in cListenThread: \"%s\"", cSocket::GetLastErrorString().c_str()); continue; diff --git a/src/OSSupport/Socket.cpp b/src/OSSupport/Socket.cpp index 98f694a79..56835b518 100644 --- a/src/OSSupport/Socket.cpp +++ b/src/OSSupport/Socket.cpp @@ -328,18 +328,18 @@ bool cSocket::ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Por -int cSocket::Receive(char * a_Buffer, unsigned int a_Length, unsigned int a_Flags) +int cSocket::Receive(char * a_Buffer, size_t a_Length, unsigned int a_Flags) { - return recv(m_Socket, a_Buffer, a_Length, a_Flags); + return recv(m_Socket, a_Buffer, (int)a_Length, a_Flags); } -int cSocket::Send(const char * a_Buffer, unsigned int a_Length) +int cSocket::Send(const char * a_Buffer, size_t a_Length) { - return send(m_Socket, a_Buffer, a_Length, MSG_NOSIGNAL); + return send(m_Socket, a_Buffer, (int)a_Length, MSG_NOSIGNAL); } diff --git a/src/OSSupport/Socket.h b/src/OSSupport/Socket.h index bdc2babf5..35ecadfa0 100644 --- a/src/OSSupport/Socket.h +++ b/src/OSSupport/Socket.h @@ -110,8 +110,8 @@ public: /// Connects to the specified host or string IP address and port, using IPv4. Returns true if successful. bool ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Port); - int Receive(char * a_Buffer, unsigned int a_Length, unsigned int a_Flags); - int Send (const char * a_Buffer, unsigned int a_Length); + int Receive(char * a_Buffer, size_t a_Length, unsigned int a_Flags); + int Send (const char * a_Buffer, size_t a_Length); unsigned short GetPort(void) const; // Returns 0 on failure diff --git a/src/OSSupport/SocketThreads.cpp b/src/OSSupport/SocketThreads.cpp index 0bc1d6b55..0ab5b6298 100644 --- a/src/OSSupport/SocketThreads.cpp +++ b/src/OSSupport/SocketThreads.cpp @@ -406,7 +406,7 @@ void cSocketThreads::cSocketThread::Execute(void) timeval Timeout; Timeout.tv_sec = 5; Timeout.tv_usec = 0; - if (select(Highest + 1, &fdRead, &fdWrite, NULL, &Timeout) == -1) + if (select((int)Highest + 1, &fdRead, &fdWrite, NULL, &Timeout) == -1) { LOG("select() call failed in cSocketThread: \"%s\"", cSocket::GetLastErrorString().c_str()); continue; diff --git a/src/ProbabDistrib.cpp b/src/ProbabDistrib.cpp index 5fa17c276..7a5869dcc 100644 --- a/src/ProbabDistrib.cpp +++ b/src/ProbabDistrib.cpp @@ -118,7 +118,7 @@ int cProbabDistrib::MapValue(int a_OrigValue) const size_t Hi = m_Cumulative.size() - 1; while (Hi - Lo > 1) { - int Mid = (Lo + Hi) / 2; + size_t Mid = (Lo + Hi) / 2; int MidProbab = m_Cumulative[Mid].m_Probability; if (MidProbab < a_OrigValue) { diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index f6849122f..443723e40 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -197,7 +197,7 @@ void cProtocol172::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockV Pkt.WriteInt(a_ChunkX); Pkt.WriteInt(a_ChunkZ); Pkt.WriteShort((short)a_Changes.size()); - Pkt.WriteInt(a_Changes.size() * 4); + Pkt.WriteInt((int)a_Changes.size() * 4); for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) { unsigned int Coords = itr->y | (itr->z << 8) | (itr->x << 12); @@ -532,7 +532,7 @@ void cProtocol172::SendExplosion(double a_BlockX, double a_BlockY, double a_Bloc Pkt.WriteFloat((float)a_BlockY); Pkt.WriteFloat((float)a_BlockZ); Pkt.WriteFloat((float)a_Radius); - Pkt.WriteInt(a_BlocksAffected.size()); + Pkt.WriteInt((int)a_BlocksAffected.size()); for (cVector3iArray::const_iterator itr = a_BlocksAffected.begin(), end = a_BlocksAffected.end(); itr != end; ++itr) { Pkt.WriteChar((char)itr->x); @@ -698,7 +698,7 @@ void cProtocol172::SendMapDecorators(int a_ID, const cMapDecoratorList & a_Decor cPacketizer Pkt(*this, 0x34); Pkt.WriteVarInt(a_ID); - Pkt.WriteShort (1 + (3 * a_Decorators.size())); + Pkt.WriteShort ((short)(1 + (3 * a_Decorators.size()))); Pkt.WriteByte(1); @@ -1174,7 +1174,7 @@ void cProtocol172::SendTabCompletionResults(const AStringVector & a_Results) ASSERT(m_State == 3); // In game mode? cPacketizer Pkt(*this, 0x3a); // Tab-Complete packet - Pkt.WriteVarInt(a_Results.size()); + Pkt.WriteVarInt((int)a_Results.size()); for (AStringVector::const_iterator itr = a_Results.begin(), end = a_Results.end(); itr != end; ++itr) { @@ -1743,7 +1743,7 @@ void cProtocol172::HandlePacketLoginStart(cByteBuffer & a_ByteBuffer) cPacketizer Pkt(*this, 0x01); Pkt.WriteString(Server->GetServerID()); const AString & PubKeyDer = Server->GetPublicKeyDER(); - Pkt.WriteShort(PubKeyDer.size()); + Pkt.WriteShort((short)PubKeyDer.size()); Pkt.WriteBuf(PubKeyDer.data(), PubKeyDer.size()); Pkt.WriteShort(4); Pkt.WriteInt((int)(intptr_t)this); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :) @@ -2138,7 +2138,7 @@ void cProtocol172::WritePacket(cByteBuffer & a_Packet) cCSLock Lock(m_CSPacket); AString Pkt; a_Packet.ReadAll(Pkt); - WriteVarInt(Pkt.size()); + WriteVarInt((UInt32)Pkt.size()); SendData(Pkt.data(), Pkt.size()); Flush(); } @@ -2403,7 +2403,7 @@ cProtocol172::cPacketizer::~cPacketizer() AString DataToSend; // Send the packet length - UInt32 PacketLen = m_Out.GetUsedSpace(); + UInt32 PacketLen = (UInt32)m_Out.GetUsedSpace(); m_Protocol.m_OutPacketLenBuffer.WriteVarInt(PacketLen); m_Protocol.m_OutPacketLenBuffer.ReadAll(DataToSend); m_Protocol.SendData(DataToSend.data(), DataToSend.size()); @@ -2500,7 +2500,7 @@ void cProtocol172::cPacketizer::WriteItem(const cItem & a_Item) Writer.Finish(); AString Compressed; CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); - WriteShort(Compressed.size()); + WriteShort((short)Compressed.size()); WriteBuf(Compressed.data(), Compressed.size()); } @@ -2570,7 +2570,7 @@ void cProtocol172::cPacketizer::WriteBlockEntity(const cBlockEntity & a_BlockEnt AString Compressed; CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); - WriteShort(Compressed.size()); + WriteShort((short)Compressed.size()); WriteBuf(Compressed.data(), Compressed.size()); } diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 22dfe7c88..667fb5cef 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -871,7 +871,7 @@ bool cProtocolRecognizer::TryRecognizeProtocol(void) // Not enough bytes for the packet length, keep waiting return false; } - ReadSoFar -= m_Buffer.GetReadableSpace(); + ReadSoFar -= (UInt32)m_Buffer.GetReadableSpace(); if (!m_Buffer.CanReadBytes(PacketLen)) { // Not enough bytes for the packet, keep waiting @@ -961,7 +961,7 @@ bool cProtocolRecognizer::TryRecognizeLengthedProtocol(UInt32 a_PacketLengthRema { return false; } - NumBytesRead -= m_Buffer.GetReadableSpace(); + NumBytesRead -= (UInt32)m_Buffer.GetReadableSpace(); switch (ProtocolVersion) { case PROTO_VERSION_1_7_2: diff --git a/src/RCONServer.cpp b/src/RCONServer.cpp index d7083ff2b..3fbc9dcbc 100644 --- a/src/RCONServer.cpp +++ b/src/RCONServer.cpp @@ -59,7 +59,7 @@ public: virtual void Finished(void) override { - m_Connection.SendResponse(m_RequestID, RCON_PACKET_RESPONSE, m_Buffer.size(), m_Buffer.c_str()); + m_Connection.SendResponse(m_RequestID, RCON_PACKET_RESPONSE, (int)m_Buffer.size(), m_Buffer.c_str()); delete this; } diff --git a/src/Root.cpp b/src/Root.cpp index 5d32bdd87..c82b05a66 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -590,13 +590,13 @@ bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallbac { class cCallback : public cPlayerListCallback { - unsigned m_BestRating; - unsigned m_NameLength; + size_t m_BestRating; + size_t m_NameLength; const AString m_PlayerName; virtual bool Item (cPlayer * a_pPlayer) { - unsigned int Rating = RateCompareString (m_PlayerName, a_pPlayer->GetName()); + size_t Rating = RateCompareString (m_PlayerName, a_pPlayer->GetName()); if ((Rating > 0) && (Rating >= m_BestRating)) { m_BestMatch = a_pPlayer; @@ -626,7 +626,7 @@ bool cRoot::FindAndDoWithPlayer(const AString & a_PlayerName, cPlayerListCallbac cPlayer * m_BestMatch; unsigned m_NumMatches; } Callback (a_PlayerName); - ForEachPlayer( Callback ); + ForEachPlayer(Callback); if (Callback.m_NumMatches == 1) { @@ -763,8 +763,8 @@ void cRoot::LogChunkStats(cCommandOutputCallback & a_Output) { cWorld * World = itr->second; int NumInGenerator = World->GetGeneratorQueueLength(); - int NumInSaveQueue = World->GetStorageSaveQueueLength(); - int NumInLoadQueue = World->GetStorageLoadQueueLength(); + int NumInSaveQueue = (int)World->GetStorageSaveQueueLength(); + int NumInLoadQueue = (int)World->GetStorageLoadQueueLength(); int NumValid = 0; int NumDirty = 0; int NumInLighting = 0; @@ -784,8 +784,6 @@ void cRoot::LogChunkStats(cCommandOutputCallback & a_Output) a_Output.Out(" block lighting: " SIZE_T_FMT_PRECISION(6) " bytes (" SIZE_T_FMT_PRECISION(3) " KiB)", 2 * sizeof(cChunkDef::BlockNibbles), (2 * sizeof(cChunkDef::BlockNibbles) + 1023) / 1024); a_Output.Out(" heightmap: " SIZE_T_FMT_PRECISION(6) " bytes (" SIZE_T_FMT_PRECISION(3) " KiB)", sizeof(cChunkDef::HeightMap), (sizeof(cChunkDef::HeightMap) + 1023) / 1024); a_Output.Out(" biomemap: " SIZE_T_FMT_PRECISION(6) " bytes (" SIZE_T_FMT_PRECISION(3) " KiB)", sizeof(cChunkDef::BiomeMap), (sizeof(cChunkDef::BiomeMap) + 1023) / 1024); - int Rest = sizeof(cChunk) - sizeof(cChunkDef::BlockTypes) - 3 * sizeof(cChunkDef::BlockNibbles) - sizeof(cChunkDef::HeightMap) - sizeof(cChunkDef::BiomeMap); - a_Output.Out(" other: %6d bytes (%3d KiB)", Rest, (Rest + 1023) / 1024); SumNumValid += NumValid; SumNumDirty += NumDirty; SumNumInLighting += NumInLighting; diff --git a/src/Scoreboard.cpp b/src/Scoreboard.cpp index 4c89ce265..250f63aa9 100644 --- a/src/Scoreboard.cpp +++ b/src/Scoreboard.cpp @@ -261,7 +261,7 @@ void cTeam::SetDisplayName(const AString & a_Name) -unsigned int cTeam::GetNumPlayers(void) const +size_t cTeam::GetNumPlayers(void) const { return m_Players.size(); } @@ -569,7 +569,7 @@ void cScoreboard::SendTo(cClientHandle & a_Client) -unsigned int cScoreboard::GetNumObjectives(void) const +size_t cScoreboard::GetNumObjectives(void) const { return m_Objectives.size(); } @@ -578,7 +578,7 @@ unsigned int cScoreboard::GetNumObjectives(void) const -unsigned int cScoreboard::GetNumTeams(void) const +size_t cScoreboard::GetNumTeams(void) const { return m_Teams.size(); } diff --git a/src/Scoreboard.h b/src/Scoreboard.h index 2fae5e499..1e1973a10 100644 --- a/src/Scoreboard.h +++ b/src/Scoreboard.h @@ -153,7 +153,7 @@ public: // tolua_begin /** Returns the number of registered players */ - unsigned int GetNumPlayers(void) const; + size_t GetNumPlayers(void) const; bool AllowsFriendlyFire(void) const { return m_AllowsFriendlyFire; } bool CanSeeFriendlyInvisible(void) const { return m_CanSeeFriendlyInvisible; } @@ -248,9 +248,9 @@ public: cObjective * GetObjectiveIn(eDisplaySlot a_Slot); - unsigned int GetNumObjectives(void) const; + size_t GetNumObjectives(void) const; - unsigned int GetNumTeams(void) const; + size_t GetNumTeams(void) const; void AddPlayerScore(const AString & a_Name, cObjective::eType a_Type, cObjective::Score a_Value = 1); diff --git a/src/Simulator/DelayedFluidSimulator.cpp b/src/Simulator/DelayedFluidSimulator.cpp index bc5158d95..5ff736231 100644 --- a/src/Simulator/DelayedFluidSimulator.cpp +++ b/src/Simulator/DelayedFluidSimulator.cpp @@ -148,7 +148,7 @@ void cDelayedFluidSimulator::SimulateChunk(float a_Dt, int a_ChunkX, int a_Chunk { SimulateBlock(a_Chunk, itr->x, itr->y, itr->z); } - m_TotalBlocks -= Blocks.size(); + m_TotalBlocks -= (int)Blocks.size(); Blocks.clear(); } } diff --git a/src/Simulator/SandSimulator.cpp b/src/Simulator/SandSimulator.cpp index c4f57c86a..b8f34559f 100644 --- a/src/Simulator/SandSimulator.cpp +++ b/src/Simulator/SandSimulator.cpp @@ -64,7 +64,7 @@ void cSandSimulator::SimulateChunk(float a_Dt, int a_ChunkX, int a_ChunkZ, cChun a_Chunk->SetBlock(itr->x, itr->y, itr->z, E_BLOCK_AIR, 0); } } - m_TotalBlocks -= ChunkData.size(); + m_TotalBlocks -= (int)ChunkData.size(); ChunkData.clear(); } diff --git a/src/StringCompression.cpp b/src/StringCompression.cpp index 2a85649a1..71d64e71e 100644 --- a/src/StringCompression.cpp +++ b/src/StringCompression.cpp @@ -11,15 +11,15 @@ /// Compresses a_Data into a_Compressed; returns Z_XXX error constants same as zlib's compress2() -int CompressString(const char * a_Data, int a_Length, AString & a_Compressed, int a_Factor) +int CompressString(const char * a_Data, size_t a_Length, AString & a_Compressed, int a_Factor) { - uLongf CompressedSize = compressBound(a_Length); + uLongf CompressedSize = compressBound((uLong)a_Length); // HACK: We're assuming that AString returns its internal buffer in its data() call and we're overwriting that buffer! // It saves us one allocation and one memcpy of the entire compressed data // It may not work on some STL implementations! (Confirmed working on MSVC 2008 & 2010) a_Compressed.resize(CompressedSize); - int errorcode = compress2( (Bytef*)a_Compressed.data(), &CompressedSize, (const Bytef*)a_Data, a_Length, a_Factor); + int errorcode = compress2((Bytef*)a_Compressed.data(), &CompressedSize, (const Bytef *)a_Data, (uLong)a_Length, a_Factor); if (errorcode != Z_OK) { return errorcode; @@ -33,14 +33,14 @@ int CompressString(const char * a_Data, int a_Length, AString & a_Compressed, in /// Uncompresses a_Data into a_Decompressed; returns Z_XXX error constants same as zlib's uncompress() -int UncompressString(const char * a_Data, int a_Length, AString & a_Uncompressed, int a_UncompressedSize) +int UncompressString(const char * a_Data, size_t a_Length, AString & a_Uncompressed, size_t a_UncompressedSize) { // HACK: We're assuming that AString returns its internal buffer in its data() call and we're overwriting that buffer! // It saves us one allocation and one memcpy of the entire compressed data // It may not work on some STL implementations! (Confirmed working on MSVC 2008 & 2010) a_Uncompressed.resize(a_UncompressedSize); uLongf UncompressedSize = (uLongf)a_UncompressedSize; // On some architectures the uLongf is different in size to int, that may be the cause of the -5 error - int errorcode = uncompress((Bytef*)a_Uncompressed.data(), &UncompressedSize, (const Bytef*)a_Data, a_Length); + int errorcode = uncompress((Bytef*)a_Uncompressed.data(), &UncompressedSize, (const Bytef*)a_Data, (uLong)a_Length); if (errorcode != Z_OK) { return errorcode; @@ -63,7 +63,7 @@ int CompressStringGZIP(const char * a_Data, size_t a_Length, AString & a_Compres z_stream strm; memset(&strm, 0, sizeof(strm)); strm.next_in = (Bytef *)a_Data; - strm.avail_in = a_Length; + strm.avail_in = (uInt)a_Length; strm.next_out = (Bytef *)Buffer; strm.avail_out = sizeof(Buffer); @@ -127,7 +127,7 @@ extern int UncompressStringGZIP(const char * a_Data, size_t a_Length, AString & z_stream strm; memset(&strm, 0, sizeof(strm)); strm.next_in = (Bytef *)a_Data; - strm.avail_in = a_Length; + strm.avail_in = (uInt)a_Length; strm.next_out = (Bytef *)Buffer; strm.avail_out = sizeof(Buffer); diff --git a/src/StringCompression.h b/src/StringCompression.h index c3a9eca91..038240797 100644 --- a/src/StringCompression.h +++ b/src/StringCompression.h @@ -10,10 +10,10 @@ /// Compresses a_Data into a_Compressed using ZLIB; returns Z_XXX error constants same as zlib's compress2() -extern int CompressString(const char * a_Data, int a_Length, AString & a_Compressed, int a_Factor); +extern int CompressString(const char * a_Data, size_t a_Length, AString & a_Compressed, int a_Factor); /// Uncompresses a_Data into a_Uncompressed; returns Z_XXX error constants same as zlib's decompress() -extern int UncompressString(const char * a_Data, int a_Length, AString & a_Uncompressed, int a_UncompressedSize); +extern int UncompressString(const char * a_Data, size_t a_Length, AString & a_Uncompressed, size_t a_UncompressedSize); /// Compresses a_Data into a_Compressed using GZIP; returns Z_OK for success or Z_XXX error constants same as zlib extern int CompressStringGZIP(const char * a_Data, size_t a_Length, AString & a_Compressed); diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp index 33b04505f..7488a3073 100644 --- a/src/StringUtils.cpp +++ b/src/StringUtils.cpp @@ -247,18 +247,22 @@ int NoCaseCompare(const AString & s1, const AString & s2) -unsigned int RateCompareString(const AString & s1, const AString & s2 ) +size_t RateCompareString(const AString & s1, const AString & s2) { - unsigned int MatchedLetters = 0; - unsigned int s1Length = s1.length(); + size_t MatchedLetters = 0; + size_t s1Length = s1.length(); - if( s1Length > s2.length() ) return 0; // Definitely not a match + if (s1Length > s2.length()) + { + // Definitely not a match + return 0; + } - for (unsigned int i = 0; i < s1Length; i++) + for (size_t i = 0; i < s1Length; i++) { - char c1 = (char)toupper( s1[i] ); - char c2 = (char)toupper( s2[i] ); - if( c1 == c2 ) + char c1 = (char)toupper(s1[i]); + char c2 = (char)toupper(s2[i]); + if (c1 == c2) { ++MatchedLetters; } @@ -288,11 +292,11 @@ void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & // Converts a stream of BE shorts into UTF-8 string; returns a ref to a_UTF8 -AString & RawBEToUTF8(const char * a_RawData, int a_NumShorts, AString & a_UTF8) +AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8) { a_UTF8.clear(); a_UTF8.reserve(3 * a_NumShorts / 2); // a quick guess of the resulting size - for (int i = 0; i < a_NumShorts; i++) + for (size_t i = 0; i < a_NumShorts; i++) { int c = GetBEShort(&a_RawData[i * 2]); if (c < 0x80) diff --git a/src/StringUtils.h b/src/StringUtils.h index b69e47d3c..caad85aef 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -52,13 +52,13 @@ extern AString & StrToLower(AString & s); extern int NoCaseCompare(const AString & s1, const AString & s2); // tolua_export /// Case-insensitive string comparison that returns a rating of equal-ness between [0 - s1.length()] -extern unsigned int RateCompareString(const AString & s1, const AString & s2 ); +extern size_t RateCompareString(const AString & s1, const AString & s2); /// Replaces *each* occurence of iNeedle in iHayStack with iReplaceWith extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); // tolua_export /// Converts a stream of BE shorts into UTF-8 string; returns a ref to a_UTF8 -extern AString & RawBEToUTF8(const char * a_RawData, int a_NumShorts, AString & a_UTF8); +extern AString & RawBEToUTF8(const char * a_RawData, size_t a_NumShorts, AString & a_UTF8); /// Converts a UTF-8 string into a UTF-16 BE string, packing that back into AString; return a ref to a_UTF16 extern AString & UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length, AString & a_UTF16); diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 13a9f9b92..788974f9c 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -891,7 +891,7 @@ void cSlotAreaAnvil::UpdateResult(cPlayer & a_Player) while ((DamageDiff > 0) && (x < SecondInput.m_ItemCount)) { Input.m_ItemDamage -= DamageDiff; - NeedExp += std::max(1, DamageDiff / 100) + Input.m_Enchantments.Count(); + NeedExp += std::max(1, DamageDiff / 100) + (int)Input.m_Enchantments.Count(); DamageDiff = std::min((int)Input.m_ItemDamage, (int)Input.GetMaxDamage() / 4); ++x; diff --git a/src/UI/Window.cpp b/src/UI/Window.cpp index 4991f0147..46885390b 100644 --- a/src/UI/Window.cpp +++ b/src/UI/Window.cpp @@ -591,7 +591,7 @@ void cWindow::OnLeftPaintEnd(cPlayer & a_Player) const cSlotNums & SlotNums = a_Player.GetInventoryPaintSlots(); cItem ToDistribute(a_Player.GetDraggingItem()); - int ToEachSlot = (int)ToDistribute.m_ItemCount / SlotNums.size(); + int ToEachSlot = (int)ToDistribute.m_ItemCount / (int)SlotNums.size(); int NumDistributed = DistributeItemToSlots(a_Player, ToDistribute, ToEachSlot, SlotNums); diff --git a/src/World.cpp b/src/World.cpp index 5ac8e0a6e..b20e017bc 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -2402,13 +2402,13 @@ bool cWorld::DoWithPlayer(const AString & a_PlayerName, cPlayerListCallback & a_ bool cWorld::FindAndDoWithPlayer(const AString & a_PlayerNameHint, cPlayerListCallback & a_Callback) { cPlayer * BestMatch = NULL; - unsigned int BestRating = 0; - unsigned int NameLength = a_PlayerNameHint.length(); + size_t BestRating = 0; + size_t NameLength = a_PlayerNameHint.length(); cCSLock Lock(m_CSPlayers); for (cPlayerList::iterator itr = m_Players.begin(); itr != m_Players.end(); ++itr) { - unsigned int Rating = RateCompareString (a_PlayerNameHint, (*itr)->GetName()); + size_t Rating = RateCompareString (a_PlayerNameHint, (*itr)->GetName()); if (Rating >= BestRating) { BestMatch = *itr; @@ -2422,7 +2422,6 @@ bool cWorld::FindAndDoWithPlayer(const AString & a_PlayerNameHint, cPlayerListCa if (BestMatch != NULL) { - LOG("Compared %s and %s with rating %i", a_PlayerNameHint.c_str(), BestMatch->GetName().c_str(), BestRating); return a_Callback.Item (BestMatch); } return false; diff --git a/src/WorldStorage/FastNBT.cpp b/src/WorldStorage/FastNBT.cpp index ac9a21205..52e998f88 100644 --- a/src/WorldStorage/FastNBT.cpp +++ b/src/WorldStorage/FastNBT.cpp @@ -38,7 +38,7 @@ -cParsedNBT::cParsedNBT(const char * a_Data, int a_Length) : +cParsedNBT::cParsedNBT(const char * a_Data, size_t a_Length) : m_Data(a_Data), m_Length(a_Length), m_Pos(0) @@ -99,8 +99,10 @@ bool cParsedNBT::ReadString(int & a_StringStart, int & a_StringLen) bool cParsedNBT::ReadCompound(void) { + ASSERT(m_Tags.size() > 0); + // Reads the latest tag as a compound - int ParentIdx = m_Tags.size() - 1; + int ParentIdx = (int)m_Tags.size() - 1; int PrevSibling = -1; for (;;) { @@ -114,13 +116,13 @@ bool cParsedNBT::ReadCompound(void) m_Tags.push_back(cFastNBTTag(TagType, ParentIdx, PrevSibling)); if (PrevSibling >= 0) { - m_Tags[PrevSibling].m_NextSibling = m_Tags.size() - 1; + m_Tags[PrevSibling].m_NextSibling = (int)m_Tags.size() - 1; } else { - m_Tags[ParentIdx].m_FirstChild = m_Tags.size() - 1; + m_Tags[ParentIdx].m_FirstChild = (int)m_Tags.size() - 1; } - PrevSibling = m_Tags.size() - 1; + PrevSibling = (int)m_Tags.size() - 1; RETURN_FALSE_IF_FALSE(ReadString(m_Tags.back().m_NameStart, m_Tags.back().m_NameLength)); RETURN_FALSE_IF_FALSE(ReadTag()); } // while (true) @@ -146,20 +148,20 @@ bool cParsedNBT::ReadList(eTagType a_ChildrenType) } // Read items: - int ParentIdx = m_Tags.size() - 1; + int ParentIdx = (int)m_Tags.size() - 1; int PrevSibling = -1; for (int i = 0; i < Count; i++) { m_Tags.push_back(cFastNBTTag(a_ChildrenType, ParentIdx, PrevSibling)); if (PrevSibling >= 0) { - m_Tags[PrevSibling].m_NextSibling = m_Tags.size() - 1; + m_Tags[PrevSibling].m_NextSibling = (int)m_Tags.size() - 1; } else { - m_Tags[ParentIdx].m_FirstChild = m_Tags.size() - 1; + m_Tags[ParentIdx].m_FirstChild = (int)m_Tags.size() - 1; } - PrevSibling = m_Tags.size() - 1; + PrevSibling = (int)m_Tags.size() - 1; RETURN_FALSE_IF_FALSE(ReadTag()); } // for (i) m_Tags[ParentIdx].m_LastChild = PrevSibling; @@ -336,7 +338,7 @@ cFastNBTWriter::cFastNBTWriter(const AString & a_RootTagName) : m_Stack[0].m_Type = TAG_Compound; m_Result.reserve(100 * 1024); m_Result.push_back(TAG_Compound); - WriteString(a_RootTagName.data(), a_RootTagName.size()); + WriteString(a_RootTagName.data(), (UInt16)a_RootTagName.size()); } @@ -389,7 +391,7 @@ void cFastNBTWriter::BeginList(const AString & a_Name, eTagType a_ChildrenType) ++m_CurrentStack; m_Stack[m_CurrentStack].m_Type = TAG_List; - m_Stack[m_CurrentStack].m_Pos = m_Result.size() - 4; + m_Stack[m_CurrentStack].m_Pos = (int)m_Result.size() - 4; m_Stack[m_CurrentStack].m_Count = 0; m_Stack[m_CurrentStack].m_ItemType = a_ChildrenType; } @@ -493,7 +495,7 @@ void cFastNBTWriter::AddString(const AString & a_Name, const AString & a_Value) void cFastNBTWriter::AddByteArray(const AString & a_Name, const char * a_Value, size_t a_NumElements) { TagCommon(a_Name, TAG_ByteArray); - Int32 len = htonl(a_NumElements); + u_long len = htonl((u_long)a_NumElements); m_Result.append((const char *)&len, 4); m_Result.append(a_Value, a_NumElements); } @@ -505,7 +507,7 @@ void cFastNBTWriter::AddByteArray(const AString & a_Name, const char * a_Value, void cFastNBTWriter::AddIntArray(const AString & a_Name, const int * a_Value, size_t a_NumElements) { TagCommon(a_Name, TAG_IntArray); - Int32 len = htonl(a_NumElements); + u_long len = htonl((u_long)a_NumElements); size_t cap = m_Result.capacity(); size_t size = m_Result.length(); if ((cap - size) < (4 + a_NumElements * 4)) @@ -534,7 +536,7 @@ void cFastNBTWriter::Finish(void) -void cFastNBTWriter::WriteString(const char * a_Data, short a_Length) +void cFastNBTWriter::WriteString(const char * a_Data, UInt16 a_Length) { Int16 Len = htons(a_Length); m_Result.append((const char *)&Len, 2); diff --git a/src/WorldStorage/FastNBT.h b/src/WorldStorage/FastNBT.h index bcf93228f..7b0af4927 100644 --- a/src/WorldStorage/FastNBT.h +++ b/src/WorldStorage/FastNBT.h @@ -114,7 +114,7 @@ Each primitive tag also stores the length of the contained data, in bytes. class cParsedNBT { public: - cParsedNBT(const char * a_Data, int a_Length); + cParsedNBT(const char * a_Data, size_t a_Length); bool IsValid(void) const {return m_IsValid; } @@ -251,7 +251,7 @@ public: protected: const char * m_Data; - int m_Length; + size_t m_Length; std::vector m_Tags; bool m_IsValid; // True if parsing succeeded @@ -319,7 +319,7 @@ protected: bool IsStackTopCompound(void) const { return (m_Stack[m_CurrentStack].m_Type == TAG_Compound); } - void WriteString(const char * a_Data, short a_Length); + void WriteString(const char * a_Data, UInt16 a_Length); inline void TagCommon(const AString & a_Name, eTagType a_Type) { @@ -330,7 +330,7 @@ protected: { // Compound: add the type and name: m_Result.push_back((char)a_Type); - WriteString(a_Name.c_str(), (short)a_Name.length()); + WriteString(a_Name.c_str(), (UInt16)a_Name.length()); } else { diff --git a/src/WorldStorage/SchematicFileSerializer.cpp b/src/WorldStorage/SchematicFileSerializer.cpp index 9d594a084..2e356b172 100644 --- a/src/WorldStorage/SchematicFileSerializer.cpp +++ b/src/WorldStorage/SchematicFileSerializer.cpp @@ -230,7 +230,7 @@ bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cP } // Copy the block types and metas: - int NumBytes = a_BlockArea.GetBlockCount(); + int NumBytes = (int)a_BlockArea.GetBlockCount(); if (a_NBT.GetDataLength(TBlockTypes) < NumBytes) { LOG("BlockTypes truncated in the schematic file (exp %d, got %d bytes). Loading partial.", @@ -242,7 +242,7 @@ bool cSchematicFileSerializer::LoadFromSchematicNBT(cBlockArea & a_BlockArea, cP if (AreMetasPresent) { - int NumBytes = a_BlockArea.GetBlockCount(); + int NumBytes = (int)a_BlockArea.GetBlockCount(); if (a_NBT.GetDataLength(TBlockMetas) < NumBytes) { LOG("BlockMetas truncated in the schematic file (exp %d, got %d bytes). Loading partial.", diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index a98ed81f7..c6b0472d0 100644 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -96,7 +96,7 @@ cWSSAnvil::cWSSAnvil(cWorld * a_World, int a_CompressionFactor) : gzFile gz = gzopen((FILE_IO_PREFIX + fnam).c_str(), "wb"); if (gz != NULL) { - gzwrite(gz, Writer.GetResult().data(), Writer.GetResult().size()); + gzwrite(gz, Writer.GetResult().data(), (unsigned)Writer.GetResult().size()); } gzclose(gz); } @@ -252,7 +252,7 @@ bool cWSSAnvil::LoadChunkFromData(const cChunkCoords & a_Chunk, const AString & strm.next_out = (Bytef *)Uncompressed; strm.avail_out = sizeof(Uncompressed); strm.next_in = (Bytef *)a_Data.data(); - strm.avail_in = a_Data.size(); + strm.avail_in = (uInt)a_Data.size(); int res = inflate(&strm, Z_FINISH); inflateEnd(&strm); if (res != Z_STREAM_END) @@ -2682,7 +2682,7 @@ bool cWSSAnvil::cMCAFile::SetChunkData(const cChunkCoords & a_Chunk, const AStri // Store the chunk data: m_File.Seek(ChunkSector * 4096); - unsigned ChunkSize = htonl(a_Data.size() + 1); + u_long ChunkSize = htonl((u_long)a_Data.size() + 1); if (m_File.Write(&ChunkSize, 4) != 4) { LOGWARNING("Cannot save chunk [%d, %d], writing(1) data to file \"%s\" failed", a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ, GetFileName().c_str()); @@ -2706,7 +2706,7 @@ bool cWSSAnvil::cMCAFile::SetChunkData(const cChunkCoords & a_Chunk, const AStri m_File.Write(Padding, 4096 - (BytesWritten % 4096)); // Store the header: - ChunkSize = (a_Data.size() + MCA_CHUNK_HEADER_LENGTH + 4095) / 4096; // Round data size *up* to nearest 4KB sector, make it a sector number + ChunkSize = ((u_long)a_Data.size() + MCA_CHUNK_HEADER_LENGTH + 4095) / 4096; // Round data size *up* to nearest 4KB sector, make it a sector number ASSERT(ChunkSize < 256); m_Header[LocalX + 32 * LocalZ] = htonl((ChunkSector << 8) | ChunkSize); if (m_File.Seek(0) < 0) diff --git a/src/WorldStorage/WSSCompact.cpp b/src/WorldStorage/WSSCompact.cpp index 359bac4a8..6d06b8fe3 100644 --- a/src/WorldStorage/WSSCompact.cpp +++ b/src/WorldStorage/WSSCompact.cpp @@ -601,7 +601,7 @@ void cWSSCompact::cPAKFile::UpdateChunk1To2() // Decompress the data: AString UncompressedData; { - int errorcode = UncompressString(Data.data(), Data.size(), UncompressedData, UncompressedSize); + int errorcode = UncompressString(Data.data(), Data.size(), UncompressedData, (size_t)UncompressedSize); if (errorcode != Z_OK) { LOGERROR("Error %d decompressing data for chunk [%d, %d]", @@ -681,7 +681,7 @@ void cWSSCompact::cPAKFile::UpdateChunk1To2() // Re-compress data AString CompressedData; { - int errorcode = CompressString(Converted.data(), Converted.size(), CompressedData,m_CompressionFactor); + int errorcode = CompressString(Converted.data(), Converted.size(), CompressedData, m_CompressionFactor); if (errorcode != Z_OK) { LOGERROR("Error %d compressing data for chunk [%d, %d]", @@ -693,9 +693,9 @@ void cWSSCompact::cPAKFile::UpdateChunk1To2() } // Save into file's cache - Header->m_UncompressedSize = Converted.size(); - Header->m_CompressedSize = CompressedData.size(); - NewDataContents.append( CompressedData ); + Header->m_UncompressedSize = (int)Converted.size(); + Header->m_CompressedSize = (int)CompressedData.size(); + NewDataContents.append(CompressedData); } // Done converting @@ -731,7 +731,7 @@ void cWSSCompact::cPAKFile::UpdateChunk2To3() Offset += Header->m_CompressedSize; // Crude data integrity check: - const int ExpectedSize = (16*256*16)*2 + (16*256*16)/2; // For version 2 + const int ExpectedSize = (16 * 256 * 16) * 2 + (16 * 256 * 16) / 2; // For version 2 if (UncompressedSize < ExpectedSize) { LOGWARNING("Chunk [%d, %d] has too short decompressed data (%d bytes out of %d needed), erasing", @@ -745,7 +745,7 @@ void cWSSCompact::cPAKFile::UpdateChunk2To3() // Decompress the data: AString UncompressedData; { - int errorcode = UncompressString(Data.data(), Data.size(), UncompressedData, UncompressedSize); + int errorcode = UncompressString(Data.data(), Data.size(), UncompressedData, (size_t)UncompressedSize); if (errorcode != Z_OK) { LOGERROR("Error %d decompressing data for chunk [%d, %d]", @@ -829,9 +829,9 @@ void cWSSCompact::cPAKFile::UpdateChunk2To3() } // Save into file's cache - Header->m_UncompressedSize = Converted.size(); - Header->m_CompressedSize = CompressedData.size(); - NewDataContents.append( CompressedData ); + Header->m_UncompressedSize = (int)Converted.size(); + Header->m_CompressedSize = (int)CompressedData.size(); + NewDataContents.append(CompressedData); } // Done converting @@ -861,7 +861,7 @@ bool cWSSCompact::LoadChunkFromData(const cChunkCoords & a_Chunk, int a_Uncompre // Decompress the data: AString UncompressedData; - int errorcode = UncompressString(a_Data.data(), a_Data.size(), UncompressedData, a_UncompressedSize); + int errorcode = UncompressString(a_Data.data(), a_Data.size(), UncompressedData, (size_t)a_UncompressedSize); if (errorcode != Z_OK) { LOGERROR("Error %d decompressing data for chunk [%d, %d]", @@ -873,7 +873,7 @@ bool cWSSCompact::LoadChunkFromData(const cChunkCoords & a_Chunk, int a_Uncompre if (a_UncompressedSize != (int)UncompressedData.size()) { - LOGWARNING("Uncompressed data size differs (exp %d bytes, got " SIZE_T_FMT ") for chunk [%d, %d]", + LOGWARNING("Uncompressed data size differs (exp %d bytes, got " SIZE_T_FMT ") for chunk [%d, %d]", a_UncompressedSize, UncompressedData.size(), a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ ); -- cgit v1.2.3 From ee680990ba12b8d272a34e45f372893f2525c868 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Thu, 8 May 2014 21:10:55 +0200 Subject: Fixed cGridStructGen. Now cNetherFortGen works with the new architecture. --- src/Generating/GridStructGen.cpp | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/Generating/GridStructGen.cpp b/src/Generating/GridStructGen.cpp index 2f064d3a0..3bbc89054 100644 --- a/src/Generating/GridStructGen.cpp +++ b/src/Generating/GridStructGen.cpp @@ -39,29 +39,17 @@ void cGridStructGen::GetStructuresForChunk(int a_ChunkX, int a_ChunkZ, cStructur int MinGridZ = MinBlockZ / m_GridSizeZ; int MaxGridX = (MaxBlockX + m_GridSizeX - 1) / m_GridSizeX; int MaxGridZ = (MaxBlockZ + m_GridSizeZ - 1) / m_GridSizeZ; - if (MinBlockX < 0) - { - --MinGridX; - } - if (MinBlockZ < 0) - { - --MinGridZ; - } - if (MaxBlockX < 0) - { - --MaxGridX; - } - if (MaxBlockZ < 0) - { - --MaxGridZ; - } + int MinX = MinGridX * m_GridSizeX; + int MaxX = MaxGridX * m_GridSizeX; + int MinZ = MinGridZ * m_GridSizeZ; + int MaxZ = MaxGridZ * m_GridSizeZ; // Walk the cache, move each structure that we want into a_Structures: for (cStructurePtrs::iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end;) { if ( - ((*itr)->m_OriginX >= MinBlockX) && ((*itr)->m_OriginX < MaxBlockX) && - ((*itr)->m_OriginZ >= MinBlockZ) && ((*itr)->m_OriginZ < MaxBlockZ) + ((*itr)->m_OriginX >= MinX) && ((*itr)->m_OriginX < MaxX) && + ((*itr)->m_OriginZ >= MinZ) && ((*itr)->m_OriginZ < MaxZ) ) { // want -- cgit v1.2.3 From 14543aa3fc204f38d0ab402d4c257d533619a983 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 10:59:33 +0200 Subject: Mineshafts generator rewritten to use GridStructGen. --- src/Generating/MineShafts.cpp | 155 ++++++------------------------------------ src/Generating/MineShafts.h | 34 ++++----- 2 files changed, 33 insertions(+), 156 deletions(-) (limited to 'src') diff --git a/src/Generating/MineShafts.cpp b/src/Generating/MineShafts.cpp index af63e549b..7b26c2b62 100644 --- a/src/Generating/MineShafts.cpp +++ b/src/Generating/MineShafts.cpp @@ -234,10 +234,12 @@ protected: -class cStructGenMineShafts::cMineShaftSystem +class cStructGenMineShafts::cMineShaftSystem : + public cGridStructGen::cStructure { + typedef cGridStructGen::cStructure super; + public: - int m_BlockX, m_BlockZ; ///< The pivot point on which the system is generated int m_GridSize; ///< Maximum offset of the dirtroom from grid center, * 2, in each direction int m_MaxRecursion; ///< Maximum recursion level (initialized from cStructGenMineShafts::m_MaxRecursion) int m_ProbLevelCorridor; ///< Probability level of a branch object being the corridor @@ -249,17 +251,15 @@ public: cMineShafts m_MineShafts; ///< List of cMineShaft descendants that comprise this system cCuboid m_BoundingBox; ///< Bounding box into which all of the components need to fit - /// Creates and generates the entire system + + /** Creates and generates the entire system */ cMineShaftSystem( - int a_BlockX, int a_BlockZ, int a_GridSize, int a_MaxSystemSize, cNoise & a_Noise, + int a_OriginX, int a_OriginZ, int a_GridSize, int a_MaxSystemSize, cNoise & a_Noise, int a_ProbLevelCorridor, int a_ProbLevelCrossing, int a_ProbLevelStaircase ); ~cMineShaftSystem(); - /// Carves the system into the chunk data - void ProcessChunk(cChunkDesc & a_Chunk); - /** Creates new cMineShaft descendant connected at the specified point, heading the specified direction, if it fits, appends it to the list and calls its AppendBranches() */ @@ -269,8 +269,11 @@ public: int a_RecursionLevel ); - /// Returns true if none of the objects in m_MineShafts intersect with the specified bounding box and the bounding box is valid + /** Returns true if none of the objects in m_MineShafts intersect with the specified bounding box and the bounding box is valid */ bool CanAppend(const cCuboid & a_BoundingBox); + + // cGridStructGen::cStructure overrides: + virtual void DrawIntoChunk(cChunkDesc & a_Chunk); } ; @@ -281,11 +284,10 @@ public: // cStructGenMineShafts::cMineShaftSystem: cStructGenMineShafts::cMineShaftSystem::cMineShaftSystem( - int a_BlockX, int a_BlockZ, int a_GridSize, int a_MaxSystemSize, cNoise & a_Noise, + int a_OriginX, int a_OriginZ, int a_GridSize, int a_MaxSystemSize, cNoise & a_Noise, int a_ProbLevelCorridor, int a_ProbLevelCrossing, int a_ProbLevelStaircase ) : - m_BlockX(a_BlockX), - m_BlockZ(a_BlockZ), + super(a_OriginX, a_OriginZ), m_GridSize(a_GridSize), m_MaxRecursion(8), // TODO: settable m_ProbLevelCorridor(a_ProbLevelCorridor), @@ -330,7 +332,7 @@ cStructGenMineShafts::cMineShaftSystem::~cMineShaftSystem() -void cStructGenMineShafts::cMineShaftSystem::ProcessChunk(cChunkDesc & a_Chunk) +void cStructGenMineShafts::cMineShaftSystem::DrawIntoChunk(cChunkDesc & a_Chunk) { for (cMineShafts::const_iterator itr = m_MineShafts.begin(), end = m_MineShafts.end(); itr != end; ++itr) { @@ -409,15 +411,15 @@ cMineShaftDirtRoom::cMineShaftDirtRoom(cStructGenMineShafts::cMineShaftSystem & super(a_Parent, mskDirtRoom) { // Make the room of random size, min 10 x 4 x 10; max 18 x 12 x 18: - int rnd = a_Noise.IntNoise3DInt(a_Parent.m_BlockX, 0, a_Parent.m_BlockZ) / 7; + int rnd = a_Noise.IntNoise3DInt(a_Parent.m_OriginX, 0, a_Parent.m_OriginZ) / 7; int OfsX = (rnd % a_Parent.m_GridSize) - a_Parent.m_GridSize / 2; rnd >>= 12; int OfsZ = (rnd % a_Parent.m_GridSize) - a_Parent.m_GridSize / 2; - rnd = a_Noise.IntNoise3DInt(a_Parent.m_BlockX, 1000, a_Parent.m_BlockZ) / 11; - m_BoundingBox.p1.x = a_Parent.m_BlockX + OfsX; + rnd = a_Noise.IntNoise3DInt(a_Parent.m_OriginX, 1000, a_Parent.m_OriginZ) / 11; + m_BoundingBox.p1.x = a_Parent.m_OriginX + OfsX; m_BoundingBox.p2.x = m_BoundingBox.p1.x + 10 + (rnd % 8); rnd >>= 4; - m_BoundingBox.p1.z = a_Parent.m_BlockZ + OfsZ; + m_BoundingBox.p1.z = a_Parent.m_OriginZ + OfsZ; m_BoundingBox.p2.z = m_BoundingBox.p1.z + 10 + (rnd % 8); rnd >>= 4; m_BoundingBox.p1.y = 20; @@ -1287,6 +1289,7 @@ cStructGenMineShafts::cStructGenMineShafts( int a_Seed, int a_GridSize, int a_MaxSystemSize, int a_ChanceCorridor, int a_ChanceCrossing, int a_ChanceStaircase ) : + super(a_Seed, a_GridSize, a_GridSize, 120 + a_MaxSystemSize * 10, 120 + a_MaxSystemSize * 10, 100), m_Noise(a_Seed), m_GridSize(a_GridSize), m_MaxSystemSize(a_MaxSystemSize), @@ -1300,125 +1303,9 @@ cStructGenMineShafts::cStructGenMineShafts( -cStructGenMineShafts::~cStructGenMineShafts() -{ - ClearCache(); -} - - - - - -void cStructGenMineShafts::ClearCache(void) +cGridStructGen::cStructurePtr cStructGenMineShafts::CreateStructure(int a_OriginX, int a_OriginZ) { - for (cMineShaftSystems::const_iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end; ++itr) - { - delete *itr; - } // for itr - m_Cache[] - m_Cache.clear(); -} - - - - - -void cStructGenMineShafts::GetMineShaftSystemsForChunk( - int a_ChunkX, int a_ChunkZ, - cStructGenMineShafts::cMineShaftSystems & a_MineShafts -) -{ - int BaseX = a_ChunkX * cChunkDef::Width / m_GridSize; - int BaseZ = a_ChunkZ * cChunkDef::Width / m_GridSize; - if (BaseX < 0) - { - --BaseX; - } - if (BaseZ < 0) - { - --BaseZ; - } - BaseX -= NEIGHBORHOOD_SIZE / 2; - BaseZ -= NEIGHBORHOOD_SIZE / 2; - - // Walk the cache, move each cave system that we want into a_Mineshafts: - int StartX = BaseX * m_GridSize; - int EndX = (BaseX + NEIGHBORHOOD_SIZE + 1) * m_GridSize; - int StartZ = BaseZ * m_GridSize; - int EndZ = (BaseZ + NEIGHBORHOOD_SIZE + 1) * m_GridSize; - for (cMineShaftSystems::iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end;) - { - if ( - ((*itr)->m_BlockX >= StartX) && ((*itr)->m_BlockX < EndX) && - ((*itr)->m_BlockZ >= StartZ) && ((*itr)->m_BlockZ < EndZ) - ) - { - // want - a_MineShafts.push_back(*itr); - itr = m_Cache.erase(itr); - } - else - { - // don't want - ++itr; - } - } // for itr - m_Cache[] - - for (int x = 0; x < NEIGHBORHOOD_SIZE; x++) - { - int RealX = (BaseX + x) * m_GridSize; - for (int z = 0; z < NEIGHBORHOOD_SIZE; z++) - { - int RealZ = (BaseZ + z) * m_GridSize; - bool Found = false; - for (cMineShaftSystems::const_iterator itr = a_MineShafts.begin(), end = a_MineShafts.end(); itr != end; ++itr) - { - if (((*itr)->m_BlockX == RealX) && ((*itr)->m_BlockZ == RealZ)) - { - Found = true; - break; - } - } // for itr - a_Mineshafts - if (!Found) - { - a_MineShafts.push_back(new cMineShaftSystem(RealX, RealZ, m_GridSize, m_MaxSystemSize, m_Noise, m_ProbLevelCorridor, m_ProbLevelCrossing, m_ProbLevelStaircase)); - } - } // for z - } // for x - - // Copy a_MineShafts into m_Cache to the beginning: - cMineShaftSystems MineShaftsCopy(a_MineShafts); - m_Cache.splice(m_Cache.begin(), MineShaftsCopy, MineShaftsCopy.begin(), MineShaftsCopy.end()); - - // Trim the cache if it's too long: - if (m_Cache.size() > 100) - { - cMineShaftSystems::iterator itr = m_Cache.begin(); - std::advance(itr, 100); - for (cMineShaftSystems::iterator end = m_Cache.end(); itr != end; ++itr) - { - delete *itr; - } - itr = m_Cache.begin(); - std::advance(itr, 100); - m_Cache.erase(itr, m_Cache.end()); - } -} - - - - - - -void cStructGenMineShafts::GenFinish(cChunkDesc & a_ChunkDesc) -{ - int ChunkX = a_ChunkDesc.GetChunkX(); - int ChunkZ = a_ChunkDesc.GetChunkZ(); - cMineShaftSystems MineShafts; - GetMineShaftSystemsForChunk(ChunkX, ChunkZ, MineShafts); - for (cMineShaftSystems::const_iterator itr = MineShafts.begin(); itr != MineShafts.end(); ++itr) - { - (*itr)->ProcessChunk(a_ChunkDesc); - } // for itr - MineShafts[] + return cStructurePtr(new cMineShaftSystem(a_OriginX, a_OriginZ, m_GridSize, m_MaxSystemSize, m_Noise, m_ProbLevelCorridor, m_ProbLevelCrossing, m_ProbLevelStaircase)); } diff --git a/src/Generating/MineShafts.h b/src/Generating/MineShafts.h index ba32e75ad..c29b6cdac 100644 --- a/src/Generating/MineShafts.h +++ b/src/Generating/MineShafts.h @@ -9,7 +9,7 @@ #pragma once -#include "ComposableGenerator.h" +#include "GridStructGen.h" #include "../Noise.h" @@ -17,16 +17,16 @@ class cStructGenMineShafts : - public cFinishGen + public cGridStructGen { + typedef cGridStructGen super; + public: cStructGenMineShafts( int a_Seed, int a_GridSize, int a_MaxSystemSize, int a_ChanceCorridor, int a_ChanceCrossing, int a_ChanceStaircase ); - virtual ~cStructGenMineShafts(); - protected: friend class cMineShaft; friend class cMineShaftDirtRoom; @@ -34,26 +34,16 @@ protected: friend class cMineShaftCrossing; friend class cMineShaftStaircase; class cMineShaftSystem; // fwd: MineShafts.cpp - typedef std::list cMineShaftSystems; - cNoise m_Noise; - int m_GridSize; ///< Average spacing of the systems - int m_MaxSystemSize; ///< Maximum blcok size of a mineshaft system - int m_ProbLevelCorridor; ///< Probability level of a branch object being the corridor - int m_ProbLevelCrossing; ///< Probability level of a branch object being the crossing, minus Corridor - int m_ProbLevelStaircase; ///< Probability level of a branch object being the staircase, minus Crossing - cMineShaftSystems m_Cache; ///< Cache of the most recently used systems. MoveToFront used. + cNoise m_Noise; + int m_GridSize; ///< Average spacing of the systems + int m_MaxSystemSize; ///< Maximum blcok size of a mineshaft system + int m_ProbLevelCorridor; ///< Probability level of a branch object being the corridor + int m_ProbLevelCrossing; ///< Probability level of a branch object being the crossing, minus Corridor + int m_ProbLevelStaircase; ///< Probability level of a branch object being the staircase, minus Crossing - /// Clears everything from the cache - void ClearCache(void); - - /** Returns all systems that *may* intersect the given chunk. - All the systems are valid until the next call to this function (which may delete some of the pointers). - */ - void GetMineShaftSystemsForChunk(int a_ChunkX, int a_ChunkZ, cMineShaftSystems & a_MineShaftSystems); - - // cFinishGen overrides: - virtual void GenFinish(cChunkDesc & a_ChunkDesc) override; + // cGridStructGen overrides: + virtual cStructurePtr CreateStructure(int a_OriginX, int a_OriginZ) override; } ; -- cgit v1.2.3 From 95ead1128f85b2f0935c6c0b575a13acbec27b6b Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 11:16:13 +0200 Subject: cWormNestCaves rewritten using cGridStructGen. Ref.: #987. --- src/Generating/Caves.cpp | 230 +++++------------------------------------------ src/Generating/Caves.h | 22 ++--- 2 files changed, 29 insertions(+), 223 deletions(-) (limited to 'src') diff --git a/src/Generating/Caves.cpp b/src/Generating/Caves.cpp index 6df22a14a..48663c21a 100644 --- a/src/Generating/Caves.cpp +++ b/src/Generating/Caves.cpp @@ -122,27 +122,19 @@ typedef std::vector cCaveTunnels; /// A collection of connected tunnels, possibly branching. -class cStructGenWormNestCaves::cCaveSystem +class cStructGenWormNestCaves::cCaveSystem : + public cGridStructGen::cStructure { + typedef cGridStructGen::cStructure super; + public: // The generating block position; is read directly in cStructGenWormNestCaves::GetCavesForChunk() int m_BlockX; int m_BlockZ; - cCaveSystem(int a_BlockX, int a_BlockZ, int a_MaxOffset, int a_Size, cNoise & a_Noise); + cCaveSystem(int a_OriginX, int a_OriginZ, int a_MaxOffset, int a_Size, cNoise & a_Noise); ~cCaveSystem(); - /// Carves the cave system into the chunk specified - void ProcessChunk( - int a_ChunkX, int a_ChunkZ, - cChunkDef::BlockTypes & a_BlockTypes, - cChunkDef::HeightMap & a_HeightMap - ); - - #ifdef _DEBUG - AString ExportAsSVG(int a_Color, int a_OffsetX, int a_OffsetZ) const; - #endif // _DEBUG - protected: int m_Size; cCaveTunnels m_Tunnels; @@ -157,6 +149,9 @@ protected: /// Returns a radius based on the location provided. int GetRadius(cNoise & a_Noise, int a_OriginX, int a_OriginY, int a_OriginZ); + + // cGridStructGen::cStructure overrides: + virtual void DrawIntoChunk(cChunkDesc & a_ChunkDesc) override; } ; @@ -586,17 +581,16 @@ AString cCaveTunnel::ExportAsSVG(int a_Color, int a_OffsetX, int a_OffsetZ) cons /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cStructGenWormNestCaves::cCaveSystem: -cStructGenWormNestCaves::cCaveSystem::cCaveSystem(int a_BlockX, int a_BlockZ, int a_MaxOffset, int a_Size, cNoise & a_Noise) : - m_BlockX(a_BlockX), - m_BlockZ(a_BlockZ), +cStructGenWormNestCaves::cCaveSystem::cCaveSystem(int a_OriginX, int a_OriginZ, int a_MaxOffset, int a_Size, cNoise & a_Noise) : + super(a_OriginX, a_OriginZ), m_Size(a_Size) { - int Num = 1 + a_Noise.IntNoise2DInt(a_BlockX, a_BlockZ) % 3; + int Num = 1 + a_Noise.IntNoise2DInt(a_OriginX, a_OriginZ) % 3; for (int i = 0; i < Num; i++) { - int OriginX = a_BlockX + (a_Noise.IntNoise3DInt(13 * a_BlockX, 17 * a_BlockZ, 11 * i) / 19) % a_MaxOffset; - int OriginZ = a_BlockZ + (a_Noise.IntNoise3DInt(17 * a_BlockX, 13 * a_BlockZ, 11 * i) / 23) % a_MaxOffset; - int OriginY = 20 + (a_Noise.IntNoise3DInt(19 * a_BlockX, 13 * a_BlockZ, 11 * i) / 17) % 20; + int OriginX = a_OriginX + (a_Noise.IntNoise3DInt(13 * a_OriginX, 17 * a_OriginZ, 11 * i) / 19) % a_MaxOffset; + int OriginZ = a_OriginZ + (a_Noise.IntNoise3DInt(17 * a_OriginX, 13 * a_OriginZ, 11 * i) / 23) % a_MaxOffset; + int OriginY = 20 + (a_Noise.IntNoise3DInt(19 * a_OriginX, 13 * a_OriginZ, 11 * i) / 17) % 20; // Generate three branches from the origin point: // The tunnels generated depend on X, Y, Z and Branches, @@ -622,64 +616,17 @@ cStructGenWormNestCaves::cCaveSystem::~cCaveSystem() -void cStructGenWormNestCaves::cCaveSystem::ProcessChunk( - int a_ChunkX, int a_ChunkZ, - cChunkDef::BlockTypes & a_BlockTypes, - cChunkDef::HeightMap & a_HeightMap -) -{ - for (cCaveTunnels::const_iterator itr = m_Tunnels.begin(), end = m_Tunnels.end(); itr != end; ++itr) - { - (*itr)->ProcessChunk(a_ChunkX, a_ChunkZ, a_BlockTypes, a_HeightMap); - } // for itr - m_Tunnels[] -} - - - - - -#ifdef _DEBUG -AString cStructGenWormNestCaves::cCaveSystem::ExportAsSVG(int a_Color, int a_OffsetX, int a_OffsetZ) const +void cStructGenWormNestCaves::cCaveSystem::DrawIntoChunk(cChunkDesc & a_ChunkDesc) { - AString SVG; - SVG.reserve(512 * 1024); + int ChunkX = a_ChunkDesc.GetChunkX(); + int ChunkZ = a_ChunkDesc.GetChunkZ(); + cChunkDef::BlockTypes & BlockTypes = a_ChunkDesc.GetBlockTypes(); + cChunkDef::HeightMap & HeightMap = a_ChunkDesc.GetHeightMap(); for (cCaveTunnels::const_iterator itr = m_Tunnels.begin(), end = m_Tunnels.end(); itr != end; ++itr) { - SVG.append((*itr)->ExportAsSVG(a_Color, a_OffsetX, a_OffsetZ)); + (*itr)->ProcessChunk(ChunkX, ChunkZ, BlockTypes, HeightMap); } // for itr - m_Tunnels[] - - // Base point highlight: - AppendPrintf(SVG, "\n", - a_OffsetX + m_BlockX - 5, a_OffsetZ + m_BlockZ, a_OffsetX + m_BlockX + 5, a_OffsetZ + m_BlockZ - ); - AppendPrintf(SVG, "\n", - a_OffsetX + m_BlockX, a_OffsetZ + m_BlockZ - 5, a_OffsetX + m_BlockX, a_OffsetZ + m_BlockZ + 5 - ); - - // A gray line from the base point to the first point of the ravine, for identification: - AppendPrintf(SVG, "\n", - a_OffsetX + m_BlockX, a_OffsetZ + m_BlockZ, - a_OffsetX + m_Tunnels.front()->m_Points.front().m_BlockX, - a_OffsetZ + m_Tunnels.front()->m_Points.front().m_BlockZ - ); - - // Offset guides: - if (a_OffsetX > 0) - { - AppendPrintf(SVG, "\n", - a_OffsetX, a_OffsetX - ); - } - if (a_OffsetZ > 0) - { - AppendPrintf(SVG, "\n", - a_OffsetZ, a_OffsetZ - ); - } - - return SVG; } -#endif // _DEBUG @@ -750,142 +697,9 @@ int cStructGenWormNestCaves::cCaveSystem::GetRadius(cNoise & a_Noise, int a_Orig /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cStructGenWormNestCaves: -cStructGenWormNestCaves::~cStructGenWormNestCaves() -{ - ClearCache(); -} - - - - - -void cStructGenWormNestCaves::ClearCache(void) -{ - for (cCaveSystems::const_iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end; ++itr) - { - delete *itr; - } // for itr - m_Cache[] - m_Cache.clear(); -} - - - - - -void cStructGenWormNestCaves::GenFinish(cChunkDesc & a_ChunkDesc) +cGridStructGen::cStructurePtr cStructGenWormNestCaves::CreateStructure(int a_OriginX, int a_OriginZ) { - int ChunkX = a_ChunkDesc.GetChunkX(); - int ChunkZ = a_ChunkDesc.GetChunkZ(); - cCaveSystems Caves; - GetCavesForChunk(ChunkX, ChunkZ, Caves); - for (cCaveSystems::const_iterator itr = Caves.begin(); itr != Caves.end(); ++itr) - { - (*itr)->ProcessChunk(ChunkX, ChunkZ, a_ChunkDesc.GetBlockTypes(), a_ChunkDesc.GetHeightMap()); - } // for itr - Caves[] -} - - - - - -void cStructGenWormNestCaves::GetCavesForChunk(int a_ChunkX, int a_ChunkZ, cStructGenWormNestCaves::cCaveSystems & a_Caves) -{ - int BaseX = a_ChunkX * cChunkDef::Width / m_Grid; - int BaseZ = a_ChunkZ * cChunkDef::Width / m_Grid; - if (BaseX < 0) - { - --BaseX; - } - if (BaseZ < 0) - { - --BaseZ; - } - BaseX -= NEIGHBORHOOD_SIZE / 2; - BaseZ -= NEIGHBORHOOD_SIZE / 2; - - // Walk the cache, move each cave system that we want into a_Caves: - int StartX = BaseX * m_Grid; - int EndX = (BaseX + NEIGHBORHOOD_SIZE + 1) * m_Grid; - int StartZ = BaseZ * m_Grid; - int EndZ = (BaseZ + NEIGHBORHOOD_SIZE + 1) * m_Grid; - for (cCaveSystems::iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end;) - { - if ( - ((*itr)->m_BlockX >= StartX) && ((*itr)->m_BlockX < EndX) && - ((*itr)->m_BlockZ >= StartZ) && ((*itr)->m_BlockZ < EndZ) - ) - { - // want - a_Caves.push_back(*itr); - itr = m_Cache.erase(itr); - } - else - { - // don't want - ++itr; - } - } // for itr - m_Cache[] - - for (int x = 0; x < NEIGHBORHOOD_SIZE; x++) - { - int RealX = (BaseX + x) * m_Grid; - for (int z = 0; z < NEIGHBORHOOD_SIZE; z++) - { - int RealZ = (BaseZ + z) * m_Grid; - bool Found = false; - for (cCaveSystems::const_iterator itr = a_Caves.begin(), end = a_Caves.end(); itr != end; ++itr) - { - if (((*itr)->m_BlockX == RealX) && ((*itr)->m_BlockZ == RealZ)) - { - Found = true; - break; - } - } - if (!Found) - { - a_Caves.push_back(new cCaveSystem(RealX, RealZ, m_MaxOffset, m_Size, m_Noise)); - } - } - } - - // Copy a_Caves into m_Cache to the beginning: - cCaveSystems CavesCopy(a_Caves); - m_Cache.splice(m_Cache.begin(), CavesCopy, CavesCopy.begin(), CavesCopy.end()); - - // Trim the cache if it's too long: - if (m_Cache.size() > 100) - { - cCaveSystems::iterator itr = m_Cache.begin(); - std::advance(itr, 100); - for (cCaveSystems::iterator end = m_Cache.end(); itr != end; ++itr) - { - delete *itr; - } - itr = m_Cache.begin(); - std::advance(itr, 100); - m_Cache.erase(itr, m_Cache.end()); - } - - /* - // Uncomment this block for debugging the caves' shapes in 2D using an SVG export - #ifdef _DEBUG - AString SVG; - SVG.append("\n\n"); - SVG.reserve(2 * 1024 * 1024); - for (cCaveSystems::const_iterator itr = a_Caves.begin(), end = a_Caves.end(); itr != end; ++itr) - { - int Color = 0x10 * abs((*itr)->m_BlockX / m_Grid); - Color |= 0x1000 * abs((*itr)->m_BlockZ / m_Grid); - SVG.append((*itr)->ExportAsSVG(Color, 512, 512)); - } - SVG.append("\n"); - - AString fnam; - Printf(fnam, "wnc\\%03d_%03d.svg", a_ChunkX, a_ChunkZ); - cFile File(fnam, cFile::fmWrite); - File.Write(SVG.c_str(), SVG.size()); - #endif // _DEBUG - //*/ + return cStructurePtr(new cCaveSystem(a_OriginX, a_OriginZ, m_MaxOffset, m_Size, m_Noise)); } diff --git a/src/Generating/Caves.h b/src/Generating/Caves.h index 2a9fa01b8..254dcddbd 100644 --- a/src/Generating/Caves.h +++ b/src/Generating/Caves.h @@ -12,7 +12,7 @@ #pragma once -#include "ComposableGenerator.h" +#include "GridStructGen.h" #include "../Noise.h" @@ -64,10 +64,12 @@ protected: class cStructGenWormNestCaves : - public cFinishGen + public cGridStructGen { + typedef cGridStructGen super; public: cStructGenWormNestCaves(int a_Seed, int a_Size = 64, int a_Grid = 96, int a_MaxOffset = 128) : + super(a_Seed, a_Grid, a_Grid, a_Size + a_MaxOffset, a_Size + a_MaxOffset, 100), m_Noise(a_Seed), m_Size(a_Size), m_MaxOffset(a_MaxOffset), @@ -75,26 +77,16 @@ public: { } - ~cStructGenWormNestCaves(); - protected: class cCaveSystem; // fwd: Caves.cpp - typedef std::list cCaveSystems; cNoise m_Noise; int m_Size; // relative size of the cave systems' caves. Average number of blocks of each initial tunnel int m_MaxOffset; // maximum offset of the cave nest origin from the grid cell the nest belongs to int m_Grid; // average spacing of the nests - cCaveSystems m_Cache; - - /// Clears everything from the cache - void ClearCache(void); - - /// Returns all caves that *may* intersect the given chunk. All the caves are valid until the next call to this function. - void GetCavesForChunk(int a_ChunkX, int a_ChunkZ, cCaveSystems & a_Caves); - - // cFinishGen override: - virtual void GenFinish(cChunkDesc & a_ChunkDesc) override; + + // cGridStructGen override: + virtual cStructurePtr CreateStructure(int a_OriginX, int a_OriginZ) override; } ; -- cgit v1.2.3 From ea593dcaad59bf6e91da59ec5dadc64b9b803580 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 18:30:39 +0200 Subject: Ravines rewritten using cGridStructGen. Ref.: #987. --- src/Generating/Ravines.cpp | 198 +++++++-------------------------------------- src/Generating/Ravines.h | 24 +++--- 2 files changed, 40 insertions(+), 182 deletions(-) (limited to 'src') diff --git a/src/Generating/Ravines.cpp b/src/Generating/Ravines.cpp index a036cf25f..78093f5ee 100644 --- a/src/Generating/Ravines.cpp +++ b/src/Generating/Ravines.cpp @@ -42,40 +42,38 @@ typedef std::vector cRavDefPoints; -class cStructGenRavines::cRavine +class cStructGenRavines::cRavine : + public cGridStructGen::cStructure { + typedef cGridStructGen::cStructure super; + cRavDefPoints m_Points; + - /// Generates the shaping defpoints for the ravine, based on the ravine block coords and noise + /** Generates the shaping defpoints for the ravine, based on the ravine block coords and noise */ void GenerateBaseDefPoints(int a_BlockX, int a_BlockZ, int a_Size, cNoise & a_Noise); - /// Refines (adds and smooths) defpoints from a_Src into a_Dst + /** Refines (adds and smooths) defpoints from a_Src into a_Dst */ void RefineDefPoints(const cRavDefPoints & a_Src, cRavDefPoints & a_Dst); - /// Does one round of smoothing, two passes of RefineDefPoints() + /** Does one round of smoothing, two passes of RefineDefPoints() */ void Smooth(void); - /// Linearly interpolates the points so that the maximum distance between two neighbors is max 1 block + /** Linearly interpolates the points so that the maximum distance between two neighbors is max 1 block */ void FinishLinear(void); public: - // Coords for which the ravine was generated (not necessarily the center) - int m_BlockX; - int m_BlockZ; cRavine(int a_BlockX, int a_BlockZ, int a_Size, cNoise & a_Noise); - /// Carves the ravine into the chunk specified - void ProcessChunk( - int a_ChunkX, int a_ChunkZ, - cChunkDef::BlockTypes & a_BlockTypes, - cChunkDef::HeightMap & a_HeightMap - ); - #ifdef _DEBUG /// Exports itself as a SVG line definition AString ExportAsSVG(int a_Color, int a_OffsetX = 0, int a_OffsetZ = 0) const; #endif // _DEBUG + +protected: + // cGridStructGen::cStructure overrides: + virtual void DrawIntoChunk(cChunkDesc & a_ChunkDesc) override; } ; @@ -86,6 +84,7 @@ public: // cStructGenRavines: cStructGenRavines::cStructGenRavines(int a_Seed, int a_Size) : + super(a_Seed, a_Size, a_Size, a_Size * 2, a_Size * 2, 100), m_Noise(a_Seed), m_Size(a_Size) { @@ -95,139 +94,9 @@ cStructGenRavines::cStructGenRavines(int a_Seed, int a_Size) : -cStructGenRavines::~cStructGenRavines() +cGridStructGen::cStructurePtr cStructGenRavines::CreateStructure(int a_OriginX, int a_OriginZ) { - ClearCache(); -} - - - - - -void cStructGenRavines::ClearCache(void) -{ - for (cRavines::const_iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end; ++itr) - { - delete *itr; - } // for itr - m_Cache[] - m_Cache.clear(); -} - - - - - -void cStructGenRavines::GenFinish(cChunkDesc & a_ChunkDesc) -{ - int ChunkX = a_ChunkDesc.GetChunkX(); - int ChunkZ = a_ChunkDesc.GetChunkZ(); - cRavines Ravines; - GetRavinesForChunk(ChunkX, ChunkZ, Ravines); - for (cRavines::const_iterator itr = Ravines.begin(), end = Ravines.end(); itr != end; ++itr) - { - (*itr)->ProcessChunk(ChunkX, ChunkZ, a_ChunkDesc.GetBlockTypes(), a_ChunkDesc.GetHeightMap()); - } // for itr - Ravines[] -} - - - - - -void cStructGenRavines::GetRavinesForChunk(int a_ChunkX, int a_ChunkZ, cStructGenRavines::cRavines & a_Ravines) -{ - int BaseX = a_ChunkX * cChunkDef::Width / m_Size; - int BaseZ = a_ChunkZ * cChunkDef::Width / m_Size; - if (BaseX < 0) - { - --BaseX; - } - if (BaseZ < 0) - { - --BaseZ; - } - BaseX -= 4; - BaseZ -= 4; - - // Walk the cache, move each ravine that we want into a_Ravines: - int StartX = BaseX * m_Size; - int EndX = (BaseX + NEIGHBORHOOD_SIZE + 1) * m_Size; - int StartZ = BaseZ * m_Size; - int EndZ = (BaseZ + NEIGHBORHOOD_SIZE + 1) * m_Size; - for (cRavines::iterator itr = m_Cache.begin(), end = m_Cache.end(); itr != end;) - { - if ( - ((*itr)->m_BlockX >= StartX) && ((*itr)->m_BlockX < EndX) && - ((*itr)->m_BlockZ >= StartZ) && ((*itr)->m_BlockZ < EndZ) - ) - { - // want - a_Ravines.push_back(*itr); - itr = m_Cache.erase(itr); - } - else - { - // don't want - ++itr; - } - } // for itr - m_Cache[] - - for (int x = 0; x < NEIGHBORHOOD_SIZE; x++) - { - int RealX = (BaseX + x) * m_Size; - for (int z = 0; z < NEIGHBORHOOD_SIZE; z++) - { - int RealZ = (BaseZ + z) * m_Size; - bool Found = false; - for (cRavines::const_iterator itr = a_Ravines.begin(), end = a_Ravines.end(); itr != end; ++itr) - { - if (((*itr)->m_BlockX == RealX) && ((*itr)->m_BlockZ == RealZ)) - { - Found = true; - break; - } - } - if (!Found) - { - a_Ravines.push_back(new cRavine(RealX, RealZ, m_Size, m_Noise)); - } - } - } - - // Copy a_Ravines into m_Cache to the beginning: - cRavines RavinesCopy(a_Ravines); - m_Cache.splice(m_Cache.begin(), RavinesCopy, RavinesCopy.begin(), RavinesCopy.end()); - - // Trim the cache if it's too long: - if (m_Cache.size() > 100) - { - cRavines::iterator itr = m_Cache.begin(); - std::advance(itr, 100); - for (cRavines::iterator end = m_Cache.end(); itr != end; ++itr) - { - delete *itr; - } - itr = m_Cache.begin(); - std::advance(itr, 100); - m_Cache.erase(itr, m_Cache.end()); - } - - /* - #ifdef _DEBUG - // DEBUG: Export as SVG into a file specific for the chunk, for visual verification: - AString SVG; - SVG.append("\n\n"); - for (cRavines::const_iterator itr = a_Ravines.begin(), end = a_Ravines.end(); itr != end; ++itr) - { - SVG.append((*itr)->ExportAsSVG(0, 512, 512)); - } - SVG.append("\n"); - - AString fnam; - Printf(fnam, "ravines\\%03d_%03d.svg", a_ChunkX, a_ChunkZ); - cFile File(fnam, cFile::fmWrite); - File.Write(SVG.c_str(), SVG.size()); - #endif // _DEBUG - //*/ + return cStructurePtr(new cRavine(a_OriginX, a_OriginZ, m_Size, m_Noise)); } @@ -238,14 +107,13 @@ void cStructGenRavines::GetRavinesForChunk(int a_ChunkX, int a_ChunkZ, cStructGe /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cStructGenRavines::cRavine -cStructGenRavines::cRavine::cRavine(int a_BlockX, int a_BlockZ, int a_Size, cNoise & a_Noise) : - m_BlockX(a_BlockX), - m_BlockZ(a_BlockZ) +cStructGenRavines::cRavine::cRavine(int a_OriginX, int a_OriginZ, int a_Size, cNoise & a_Noise) : + super(a_OriginX, a_OriginZ) { // Calculate the ravine shape-defining points: - GenerateBaseDefPoints(a_BlockX, a_BlockZ, a_Size, a_Noise); + GenerateBaseDefPoints(a_OriginX, a_OriginZ, a_Size, a_Noise); - // Smooth the ravine. A two passes are needed: + // Smooth the ravine. Two passes are needed: Smooth(); Smooth(); @@ -263,8 +131,8 @@ void cStructGenRavines::cRavine::GenerateBaseDefPoints(int a_BlockX, int a_Block a_Size = (512 + ((a_Noise.IntNoise3DInt(19 * a_BlockX, 11 * a_BlockZ, a_BlockX + a_BlockZ) / 17) % 512)) * a_Size / 1024; // The complete offset of the ravine from its cellpoint, up to 2 * a_Size in each direction - int OffsetX = (((a_Noise.IntNoise3DInt(50 * a_BlockX, 30 * a_BlockZ, 0) / 9) % (2 * a_Size)) + ((a_Noise.IntNoise3DInt(30 * a_BlockX, 50 * m_BlockZ, 1000) / 7) % (2 * a_Size)) - 2 * a_Size) / 2; - int OffsetZ = (((a_Noise.IntNoise3DInt(50 * a_BlockX, 30 * a_BlockZ, 2000) / 7) % (2 * a_Size)) + ((a_Noise.IntNoise3DInt(30 * a_BlockX, 50 * m_BlockZ, 3000) / 9) % (2 * a_Size)) - 2 * a_Size) / 2; + int OffsetX = (((a_Noise.IntNoise3DInt(50 * a_BlockX, 30 * a_BlockZ, 0) / 9) % (2 * a_Size)) + ((a_Noise.IntNoise3DInt(30 * a_BlockX, 50 * a_BlockZ, 1000) / 7) % (2 * a_Size)) - 2 * a_Size) / 2; + int OffsetZ = (((a_Noise.IntNoise3DInt(50 * a_BlockX, 30 * a_BlockZ, 2000) / 7) % (2 * a_Size)) + ((a_Noise.IntNoise3DInt(30 * a_BlockX, 50 * a_BlockZ, 3000) / 9) % (2 * a_Size)) - 2 * a_Size) / 2; int CenterX = a_BlockX + OffsetX; int CenterZ = a_BlockZ + OffsetZ; @@ -429,15 +297,15 @@ AString cStructGenRavines::cRavine::ExportAsSVG(int a_Color, int a_OffsetX, int // Base point highlight: AppendPrintf(SVG, "\n", - a_OffsetX + m_BlockX - 5, a_OffsetZ + m_BlockZ, a_OffsetX + m_BlockX + 5, a_OffsetZ + m_BlockZ + a_OffsetX + m_OriginX - 5, a_OffsetZ + m_OriginZ, a_OffsetX + m_OriginX + 5, a_OffsetZ + m_OriginZ ); AppendPrintf(SVG, "\n", - a_OffsetX + m_BlockX, a_OffsetZ + m_BlockZ - 5, a_OffsetX + m_BlockX, a_OffsetZ + m_BlockZ + 5 + a_OffsetX + m_OriginX, a_OffsetZ + m_OriginZ - 5, a_OffsetX + m_OriginX, a_OffsetZ + m_OriginZ + 5 ); // A gray line from the base point to the first point of the ravine, for identification: AppendPrintf(SVG, "\n", - a_OffsetX + m_BlockX, a_OffsetZ + m_BlockZ, a_OffsetX + m_Points.front().m_BlockX, a_OffsetZ + m_Points.front().m_BlockZ + a_OffsetX + m_OriginX, a_OffsetZ + m_OriginZ, a_OffsetX + m_Points.front().m_BlockX, a_OffsetZ + m_Points.front().m_BlockZ ); // Offset guides: @@ -461,14 +329,10 @@ AString cStructGenRavines::cRavine::ExportAsSVG(int a_Color, int a_OffsetX, int -void cStructGenRavines::cRavine::ProcessChunk( - int a_ChunkX, int a_ChunkZ, - cChunkDef::BlockTypes & a_BlockTypes, - cChunkDef::HeightMap & a_HeightMap -) +void cStructGenRavines::cRavine::DrawIntoChunk(cChunkDesc & a_ChunkDesc) { - int BlockStartX = a_ChunkX * cChunkDef::Width; - int BlockStartZ = a_ChunkZ * cChunkDef::Width; + int BlockStartX = a_ChunkDesc.GetChunkX() * cChunkDef::Width; + int BlockStartZ = a_ChunkDesc.GetChunkZ() * cChunkDef::Width; int BlockEndX = BlockStartX + cChunkDef::Width; int BlockEndZ = BlockStartZ + cChunkDef::Width; for (cRavDefPoints::const_iterator itr = m_Points.begin(), end = m_Points.end(); itr != end; ++itr) @@ -494,7 +358,7 @@ void cStructGenRavines::cRavine::ProcessChunk( // DEBUG: Make the ravine shapepoints visible on a single layer (so that we can see with Minutor what's going on) if ((DifX + x == 0) && (DifZ + z == 0)) { - cChunkDef::SetBlock(a_BlockTypes, x, 4, z, E_BLOCK_LAPIS_ORE); + a_ChunkDesc.SetBlockType(x, 4, z, E_BLOCK_LAPIS_ORE); } #endif // _DEBUG @@ -504,7 +368,7 @@ void cStructGenRavines::cRavine::ProcessChunk( int Top = std::min(itr->m_Top, (int)(cChunkDef::Height)); // Stupid gcc needs int cast for (int y = std::max(itr->m_Bottom, 1); y <= Top; y++) { - switch (cChunkDef::GetBlock(a_BlockTypes, x, y, z)) + switch (a_ChunkDesc.GetBlockType(x, y, z)) { // Only carve out these specific block types case E_BLOCK_DIRT: @@ -522,7 +386,7 @@ void cStructGenRavines::cRavine::ProcessChunk( case E_BLOCK_REDSTONE_ORE: case E_BLOCK_REDSTONE_ORE_GLOWING: { - cChunkDef::SetBlock(a_BlockTypes, x, y, z, E_BLOCK_AIR); + a_ChunkDesc.SetBlockType(x, y, z, E_BLOCK_AIR); break; } default: break; diff --git a/src/Generating/Ravines.h b/src/Generating/Ravines.h index c76b9f19f..30b47e9ec 100644 --- a/src/Generating/Ravines.h +++ b/src/Generating/Ravines.h @@ -9,7 +9,7 @@ #pragma once -#include "ComposableGenerator.h" +#include "GridStructGen.h" #include "../Noise.h" @@ -17,28 +17,22 @@ class cStructGenRavines : - public cFinishGen + public cGridStructGen { + typedef cGridStructGen super; + public: cStructGenRavines(int a_Seed, int a_Size); - ~cStructGenRavines(); protected: class cRavine; // fwd: Ravines.cpp - typedef std::list cRavines; - - cNoise m_Noise; - int m_Size; // Max size, in blocks, of the ravines generated - cRavines m_Cache; - /// Clears everything from the cache - void ClearCache(void); + cNoise m_Noise; + int m_Size; // Max size, in blocks, of the ravines generated - /// Returns all ravines that *may* intersect the given chunk. All the ravines are valid until the next call to this function. - void GetRavinesForChunk(int a_ChunkX, int a_ChunkZ, cRavines & a_Ravines); - - // cFinishGen override: - virtual void GenFinish(cChunkDesc & a_ChunkDesc) override; + + // cGridStructGen overrides: + virtual cStructurePtr CreateStructure(int a_OriginX, int a_OriginZ) override; } ; -- cgit v1.2.3 From b997fbaf4577f0c3ff4f60f1a9ab2ca93c5f0d71 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 19:35:35 +0200 Subject: Removed an unused macro. --- src/Generating/Caves.cpp | 7 ------- 1 file changed, 7 deletions(-) (limited to 'src') diff --git a/src/Generating/Caves.cpp b/src/Generating/Caves.cpp index 48663c21a..872e3341d 100644 --- a/src/Generating/Caves.cpp +++ b/src/Generating/Caves.cpp @@ -35,13 +35,6 @@ reduced in complexity in order for this generator to be useful, so the caves' sh -/// How many nests in each direction are generated for a given chunk. Must be an even number -#define NEIGHBORHOOD_SIZE 8 - - - - - const int MIN_RADIUS = 3; const int MAX_RADIUS = 8; -- cgit v1.2.3 From 0fc60edaeb4bc54b87bc3d7af4631802c0331303 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 19:53:57 +0200 Subject: Removed an unused NetherFortGen variable. --- src/Generating/NetherFortGen.cpp | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src') diff --git a/src/Generating/NetherFortGen.cpp b/src/Generating/NetherFortGen.cpp index ac7fd0b7b..3867ec80c 100644 --- a/src/Generating/NetherFortGen.cpp +++ b/src/Generating/NetherFortGen.cpp @@ -11,12 +11,6 @@ -static const int NEIGHBORHOOD_SIZE = 3; - - - - - /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cNetherFortGen::cNetherFort: -- cgit v1.2.3 From a936736acd822b29d09b4344ece768656b4d24db Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 9 May 2014 20:13:20 +0200 Subject: Removed unused constants from MineShafts and Ravines. --- src/Generating/MineShafts.cpp | 6 ------ src/Generating/Ravines.cpp | 3 --- 2 files changed, 9 deletions(-) (limited to 'src') diff --git a/src/Generating/MineShafts.cpp b/src/Generating/MineShafts.cpp index 7b26c2b62..391e4c04f 100644 --- a/src/Generating/MineShafts.cpp +++ b/src/Generating/MineShafts.cpp @@ -25,12 +25,6 @@ in a depth-first processing. Each of the descendants will branch randomly, if no -static const int NEIGHBORHOOD_SIZE = 3; - - - - - class cMineShaft abstract { public: diff --git a/src/Generating/Ravines.cpp b/src/Generating/Ravines.cpp index 78093f5ee..2722e4ca3 100644 --- a/src/Generating/Ravines.cpp +++ b/src/Generating/Ravines.cpp @@ -9,9 +9,6 @@ -/// How many ravines in each direction are generated for a given chunk. Must be an even number -static const int NEIGHBORHOOD_SIZE = 8; - static const int NUM_RAVINE_POINTS = 4; -- cgit v1.2.3 From c8631d9a9b924c4c4d030bac3ed4e4a869ec2fd8 Mon Sep 17 00:00:00 2001 From: Howaner Date: Fri, 9 May 2014 23:10:02 +0200 Subject: Add DIG_STATUS_CANCELLED packet and add item resend, when a block can't place/break. --- src/ClientHandle.cpp | 61 +++++++++++++++++++++++++++++++++++++--------------- src/ClientHandle.h | 3 +++ src/Inventory.cpp | 10 +++++++++ src/Inventory.h | 41 +++++++++++++++++++---------------- 4 files changed, 79 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 94f031ed6..1535b0482 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -878,6 +878,7 @@ void cClientHandle::HandleLeftClick(int a_BlockX, int a_BlockY, int a_BlockZ, eB case DIG_STATUS_CANCELLED: { // Block breaking cancelled by player + HandleBlockDigCancel(); return; } @@ -916,7 +917,7 @@ void cClientHandle::HandleBlockDigStarted(int a_BlockX, int a_BlockY, int a_Bloc // It is a duplicate packet, drop it right away return; } - + if ( m_Player->IsGameModeCreative() && ItemCategory::IsSword(m_Player->GetInventory().GetEquippedItem().m_ItemType) @@ -925,14 +926,7 @@ void cClientHandle::HandleBlockDigStarted(int a_BlockX, int a_BlockY, int a_Bloc // Players can't destroy blocks with a Sword in the hand. return; } - - if (cRoot::Get()->GetPluginManager()->CallHookPlayerBreakingBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_OldBlock, a_OldMeta)) - { - // A plugin doesn't agree with the breaking. Bail out. Send the block back to the client, so that it knows: - m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); - return; - } - + // Set the last digging coords to the block being dug, so that they can be checked in DIG_FINISHED to avoid dig/aim bug in the client: m_HasStartedDigging = true; m_LastDigBlockX = a_BlockX; @@ -1004,16 +998,16 @@ void cClientHandle::HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_Blo return; } - m_HasStartedDigging = false; - if (m_BlockDigAnimStage != -1) - { - // End dig animation - m_BlockDigAnimStage = -1; - // It seems that 10 ends block animation - m_Player->GetWorld()->BroadcastBlockBreakAnimation(m_UniqueID, m_BlockDigAnimX, m_BlockDigAnimY, m_BlockDigAnimZ, 10, this); - } + HandleBlockDigCancel(); cItemHandler * ItemHandler = cItemHandler::GetItemHandler(m_Player->GetEquippedItem()); + + if (cRoot::Get()->GetPluginManager()->CallHookPlayerBreakingBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_OldBlock, a_OldMeta)) + { + // A plugin doesn't agree with the breaking. Bail out. Send the block back to the client, so that it knows: + m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); + return; + } if (a_OldBlock == E_BLOCK_AIR) { @@ -1036,6 +1030,36 @@ void cClientHandle::HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_Blo +void cClientHandle::HandleBlockDigCancel() +{ + if ( + !m_HasStartedDigging || // Hasn't received the DIG_STARTED packet + (m_LastDigBlockX == -1) || + (m_LastDigBlockY == -1) || + (m_LastDigBlockZ == -1) + ) + { + return; + } + + m_HasStartedDigging = false; + if (m_BlockDigAnimStage != -1) + { + // End dig animation + m_BlockDigAnimStage = -1; + // It seems that 10 ends block animation + m_Player->GetWorld()->BroadcastBlockBreakAnimation(m_UniqueID, m_LastDigBlockX, m_LastDigBlockY, m_LastDigBlockZ, 10, this); + } + + m_BlockDigAnimX = -1; + m_BlockDigAnimY = -1; + m_BlockDigAnimZ = -1; +} + + + + + void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, const cItem & a_HeldItem) { LOGD("HandleRightClick: {%d, %d, %d}, face %d, HeldItem: %s", @@ -1058,6 +1082,7 @@ void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, e AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); World->SendBlockTo(a_BlockX, a_BlockY + 1, a_BlockZ, m_Player); //2 block high things + m_Player->GetInventory().SendEquippedSlot(); } return; } @@ -1246,6 +1271,7 @@ void cClientHandle::HandlePlaceBlock(int a_BlockX, int a_BlockY, int a_BlockZ, e { // Handler refused the placement, send that information back to the client: World->SendBlockTo(a_BlockX, a_BlockY, a_BlockY, m_Player); + m_Player->GetInventory().SendEquippedSlot(); return; } @@ -1255,6 +1281,7 @@ void cClientHandle::HandlePlaceBlock(int a_BlockX, int a_BlockY, int a_BlockZ, e { // A plugin doesn't agree with placing the block, revert the block on the client: World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); + m_Player->GetInventory().SendEquippedSlot(); return; } diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 4dc6ab074..03a716657 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -374,6 +374,9 @@ private: /** Handles the DIG_FINISHED dig packet: */ void HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, BLOCKTYPE a_OldBlock, NIBBLETYPE a_OldMeta); + /** Handles the DIG_CANCELLED dig packet: */ + void HandleBlockDigCancel(); + /** Converts the protocol-formatted channel list (NUL-separated) into a proper string vector. */ AStringVector BreakApartPluginChannels(const AString & a_PluginChannels); diff --git a/src/Inventory.cpp b/src/Inventory.cpp index a365e4ed4..bce882c88 100644 --- a/src/Inventory.cpp +++ b/src/Inventory.cpp @@ -243,6 +243,16 @@ void cInventory::SetHotbarSlot(int a_HotBarSlotNum, const cItem & a_Item) +void cInventory::SendEquippedSlot() +{ + int EquippedSlotNum = cInventory::invArmorCount + cInventory::invInventoryCount + GetEquippedSlotNum(); + SendSlot(EquippedSlotNum); +} + + + + + const cItem & cInventory::GetSlot(int a_SlotNum) const { if ((a_SlotNum < 0) || (a_SlotNum >= invNumSlots)) diff --git a/src/Inventory.h b/src/Inventory.h index 1ad7c4776..39aef1538 100644 --- a/src/Inventory.h +++ b/src/Inventory.h @@ -56,13 +56,13 @@ public: // tolua_begin - /// Removes all items from the entire inventory + /** Removes all items from the entire inventory */ void Clear(void); - /// Returns number of items out of a_ItemStack that can fit in the storage + /** Returns number of items out of a_ItemStack that can fit in the storage */ int HowManyCanFit(const cItem & a_ItemStack, bool a_ConsiderEmptySlots); - /// Returns how many items of the specified type would fit into the slot range specified + /** Returns how many items of the specified type would fit into the slot range specified */ int HowManyCanFit(const cItem & a_ItemStack, int a_BeginSlotNum, int a_EndSlotNum, bool a_ConsiderEmptySlots); /** Adds as many items out of a_ItemStack as can fit. @@ -86,33 +86,36 @@ public: */ int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks, bool a_tryToFillEquippedFirst); - /// Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed + /** Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed */ bool RemoveOneEquippedItem(void); - /// Returns the number of items of type a_Item that are stored + /** Returns the number of items of type a_Item that are stored */ int HowManyItems(const cItem & a_Item); - /// Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack + /** Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack */ bool HasItems(const cItem & a_ItemStack); + + /** Sends the equipped item slot to the client */ + void SendEquippedSlot(); - /// Returns the cItemGrid object representing the armor slots + /** Returns the cItemGrid object representing the armor slots */ cItemGrid & GetArmorGrid(void) { return m_ArmorSlots; } - /// Returns the cItemGrid object representing the main inventory slots + /** Returns the cItemGrid object representing the main inventory slots */ cItemGrid & GetInventoryGrid(void) { return m_InventorySlots; } - /// Returns the cItemGrid object representing the hotbar slots + /** Returns the cItemGrid object representing the hotbar slots */ cItemGrid & GetHotbarGrid(void) { return m_HotbarSlots; } - /// Returns the player associated with this inventory + /** Returns the player associated with this inventory */ cPlayer & GetOwner(void) { return m_Owner; } - /// Copies the non-empty slots into a_ItemStacks; preserves the original a_Items contents + /** Copies the non-empty slots into a_ItemStacks; preserves the original a_Items contents */ void CopyToItems(cItems & a_Items); // tolua_end - /// Returns the player associated with this inventory (const version) + /** Returns the player associated with this inventory (const version) */ const cPlayer & GetOwner(void) const { return m_Owner; } // tolua_begin @@ -136,10 +139,10 @@ public: */ int ChangeSlotCount(int a_SlotNum, int a_AddToCount); - /// Adds the specified damage to the specified item; deletes the item and returns true if the item broke. + /** Adds the specified damage to the specified item; deletes the item and returns true if the item broke. */ bool DamageItem(int a_SlotNum, short a_Amount); - /// Adds the specified damage to the currently held item; deletes the item and returns true if the item broke. + /** Adds the specified damage to the currently held item; deletes the item and returns true if the item broke. */ bool DamageEquippedItem(short a_Amount = 1); const cItem & GetEquippedHelmet (void) const { return m_ArmorSlots.GetSlot(0); } @@ -149,13 +152,13 @@ public: // tolua_end - /// Sends the slot contents to the owner + /** Sends the slot contents to the owner */ void SendSlot(int a_SlotNum); - /// Update items (e.g. Maps) + /** Update items (e.g. Maps) */ void UpdateItems(void); - /// Converts an armor slot number into the ID for the EntityEquipment packet + /** Converts an armor slot number into the ID for the EntityEquipment packet */ static int ArmorSlotNumToEntityEquipmentID(short a_ArmorSlotNum); void SaveToJson(Json::Value & a_Value); @@ -172,10 +175,10 @@ protected: cPlayer & m_Owner; - /// Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return NULL for invalid SlotNum + /** Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return NULL for invalid SlotNum */ const cItemGrid * GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum) const; - /// Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return NULL for invalid SlotNum + /** Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return NULL for invalid SlotNum */ cItemGrid * GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum); // cItemGrid::cListener override: -- cgit v1.2.3 From eb0f713b6a5a7107a97284d1728858b4b2882c3d Mon Sep 17 00:00:00 2001 From: Howaner Date: Fri, 9 May 2014 23:43:00 +0200 Subject: Add block place/break distance check. --- src/ClientHandle.cpp | 21 +++++++++++++++++++++ src/Defines.h | 10 ++++++++++ 2 files changed, 31 insertions(+) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 1535b0482..d1962506d 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -1016,6 +1016,17 @@ void cClientHandle::HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_Blo } cWorld * World = m_Player->GetWorld(); + + if ( + (Diff(m_Player->GetPosX(), (double)a_BlockX) > 6) || + (Diff(m_Player->GetPosY(), (double)a_BlockY) > 6) || + (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6) + ) + { + m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); + return; + } + ItemHandler->OnBlockDestroyed(World, m_Player, m_Player->GetEquippedItem(), a_BlockX, a_BlockY, a_BlockZ); // The ItemHandler is also responsible for spawning the pickups cChunkInterface ChunkInterface(World->GetChunkMap()); @@ -1191,6 +1202,16 @@ void cClientHandle::HandlePlaceBlock(int a_BlockX, int a_BlockY, int a_BlockZ, e // The block is being placed outside the world, ignore this packet altogether (#128) return; } + + if ( + (Diff(m_Player->GetPosX(), (double)a_BlockX) > 6) || + (Diff(m_Player->GetPosY(), (double)a_BlockY) > 6) || + (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6) + ) + { + m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); + return; + } World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, ClickedBlock, ClickedBlockMeta); diff --git a/src/Defines.h b/src/Defines.h index 9fa3b3a8e..b0b209934 100644 --- a/src/Defines.h +++ b/src/Defines.h @@ -3,6 +3,7 @@ #include "ChatColor.h" #include +#include @@ -528,6 +529,15 @@ inline float GetSpecialSignf( float a_Val ) +template inline int Diff(T a_Val1, T a_Val2) +{ + return std::abs(a_Val1 - a_Val2); +} + + + + + // tolua_begin enum eMessageType -- cgit v1.2.3 From 683b839e2b3e634dd1a0a5b85327efe4ffa968fd Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 10 May 2014 09:21:29 +0200 Subject: Client cert is not requested. --- src/PolarSSL++/SslContext.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/PolarSSL++/SslContext.cpp b/src/PolarSSL++/SslContext.cpp index bc397b655..c3074f197 100644 --- a/src/PolarSSL++/SslContext.cpp +++ b/src/PolarSSL++/SslContext.cpp @@ -59,7 +59,7 @@ int cSslContext::Initialize(bool a_IsClient, const SharedPtr & return res; } ssl_set_endpoint(&m_Ssl, a_IsClient ? SSL_IS_CLIENT : SSL_IS_SERVER); - ssl_set_authmode(&m_Ssl, SSL_VERIFY_OPTIONAL); + ssl_set_authmode(&m_Ssl, a_IsClient ? SSL_VERIFY_OPTIONAL : SSL_VERIFY_NONE); // Clients ask for server's cert but don't verify strictly; servers don't ask clients for certs by default ssl_set_rng(&m_Ssl, ctr_drbg_random, &m_CtrDrbg->m_CtrDrbg); ssl_set_bio(&m_Ssl, ReceiveEncrypted, this, SendEncrypted, this); -- cgit v1.2.3 From 5c9f89526aeca7b32014cd78e323f74baddf1e36 Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 11 May 2014 11:56:15 +0200 Subject: Rename HandleBlockDigCancel to FinishDigAnimtion. --- src/ClientHandle.cpp | 6 +++--- src/ClientHandle.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index d1962506d..9238418a4 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -878,7 +878,7 @@ void cClientHandle::HandleLeftClick(int a_BlockX, int a_BlockY, int a_BlockZ, eB case DIG_STATUS_CANCELLED: { // Block breaking cancelled by player - HandleBlockDigCancel(); + FinishDigAnimation(); return; } @@ -998,7 +998,7 @@ void cClientHandle::HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_Blo return; } - HandleBlockDigCancel(); + FinishDigAnimation(); cItemHandler * ItemHandler = cItemHandler::GetItemHandler(m_Player->GetEquippedItem()); @@ -1041,7 +1041,7 @@ void cClientHandle::HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_Blo -void cClientHandle::HandleBlockDigCancel() +void cClientHandle::FinishDigAnimation() { if ( !m_HasStartedDigging || // Hasn't received the DIG_STARTED packet diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 03a716657..85d348eee 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -374,8 +374,8 @@ private: /** Handles the DIG_FINISHED dig packet: */ void HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, BLOCKTYPE a_OldBlock, NIBBLETYPE a_OldMeta); - /** Handles the DIG_CANCELLED dig packet: */ - void HandleBlockDigCancel(); + /** The clients will receive a finished dig animation */ + void FinishDigAnimation(); /** Converts the protocol-formatted channel list (NUL-separated) into a proper string vector. */ AStringVector BreakApartPluginChannels(const AString & a_PluginChannels); -- cgit v1.2.3 From c7c3724a3ee0e7a77fe9924ad25c36b6ec8fdd14 Mon Sep 17 00:00:00 2001 From: andrew Date: Sun, 11 May 2014 14:57:06 +0300 Subject: Statistic Manager --- src/Bindings/AllToLua.pkg | 1 + src/ClientHandle.cpp | 19 +++++- src/ClientHandle.h | 2 + src/Entities/Player.cpp | 21 +++++- src/Entities/Player.h | 7 ++ src/Protocol/Protocol.h | 2 + src/Protocol/Protocol125.cpp | 28 ++++++++ src/Protocol/Protocol125.h | 1 + src/Protocol/Protocol17x.cpp | 48 ++++++++++++-- src/Protocol/Protocol17x.h | 1 + src/Protocol/ProtocolRecognizer.cpp | 10 +++ src/Protocol/ProtocolRecognizer.h | 1 + src/Statistics.cpp | 123 +++++++++++++++++++++++++---------- src/Statistics.h | 48 ++++++++++++++ src/WorldStorage/StatSerializer.cpp | 126 ++++++++++++++++++++++++++++++++++++ src/WorldStorage/StatSerializer.h | 53 +++++++++++++++ 16 files changed, 448 insertions(+), 43 deletions(-) create mode 100644 src/WorldStorage/StatSerializer.cpp create mode 100644 src/WorldStorage/StatSerializer.h (limited to 'src') diff --git a/src/Bindings/AllToLua.pkg b/src/Bindings/AllToLua.pkg index 1cd7c74f8..4fe86e1c5 100644 --- a/src/Bindings/AllToLua.pkg +++ b/src/Bindings/AllToLua.pkg @@ -76,6 +76,7 @@ $cfile "../CompositeChat.h" $cfile "../Map.h" $cfile "../MapManager.h" $cfile "../Scoreboard.h" +$cfile "../Statistics.h" diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 94f031ed6..03b1b0dc2 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -31,6 +31,8 @@ #include "CompositeChat.h" #include "Items/ItemSword.h" +#include "WorldStorage/StatSerializer.h" + #include "md5/md5.h" @@ -336,7 +338,13 @@ void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID) // Send scoreboard data World->GetScoreBoard().SendTo(*this); - + +#if 0 + // Load stats + cStatSerializer StatSerializer(World->GetName(), m_Player->GetName(), &m_Player->GetStatManager()); + StatSerializer.Load(); +#endif + // 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; @@ -2437,6 +2445,15 @@ void cClientHandle::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTy +void cClientHandle::SendStatistics(const cStatManager & a_Manager) +{ + m_Protocol->SendStatistics(a_Manager); +} + + + + + void cClientHandle::SendTabCompletionResults(const AStringVector & a_Results) { m_Protocol->SendTabCompletionResults(a_Results); diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 4dc6ab074..0236d38fb 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -39,6 +39,7 @@ class cFallingBlock; class cItemHandler; class cWorld; class cCompositeChat; +class cStatManager; @@ -160,6 +161,7 @@ public: void SendSpawnMob (const cMonster & a_Mob); void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch); void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType = 0); + void SendStatistics (const cStatManager & a_Manager); void SendTabCompletionResults(const AStringVector & a_Results); void SendTeleportEntity (const cEntity & a_Entity); void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ); diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 6ac11c270..a42fe89cf 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -16,6 +16,8 @@ #include "../Items/ItemHandler.h" #include "../Vector3.h" +#include "../WorldStorage/StatSerializer.h" + #include "inifile/iniFile.h" #include "json/json.h" @@ -131,6 +133,15 @@ cPlayer::~cPlayer(void) SaveToDisk(); +#if 0 + /* Save statistics. */ + cStatSerializer StatSerializer(m_World->GetName(), m_PlayerName, &m_Stats); + if (!StatSerializer.Save()) + { + LOGERROR("Could not save stats for player %s", m_PlayerName.c_str()); + } +#endif + m_World->RemovePlayer( this ); m_ClientHandle = NULL; @@ -871,9 +882,13 @@ void cPlayer::KilledBy(cEntity * a_Killer) } else if (a_Killer->IsPlayer()) { - GetWorld()->BroadcastChatDeath(Printf("%s was killed by %s", GetName().c_str(), ((cPlayer *)a_Killer)->GetName().c_str())); + cPlayer* Killer = (cPlayer*)a_Killer; - m_World->GetScoreBoard().AddPlayerScore(((cPlayer *)a_Killer)->GetName(), cObjective::otPlayerKillCount, 1); + GetWorld()->BroadcastChatDeath(Printf("%s was killed by %s", GetName().c_str(), Killer->GetName().c_str())); + + Killer->GetStatManager().AddValue(statPlayerKills); + + m_World->GetScoreBoard().AddPlayerScore(Killer->GetName(), cObjective::otPlayerKillCount, 1); } else { @@ -883,6 +898,8 @@ void cPlayer::KilledBy(cEntity * a_Killer) GetWorld()->BroadcastChatDeath(Printf("%s was killed by a %s", GetName().c_str(), KillerClass.c_str())); } + m_Stats.AddValue(statDeaths); + m_World->GetScoreBoard().AddPlayerScore(GetName(), cObjective::otDeathCount, 1); } diff --git a/src/Entities/Player.h b/src/Entities/Player.h index 6fc7e2875..82a138290 100644 --- a/src/Entities/Player.h +++ b/src/Entities/Player.h @@ -7,6 +7,8 @@ #include "../World.h" #include "../ClientHandle.h" +#include "../Statistics.h" + @@ -174,6 +176,9 @@ public: cTeam * UpdateTeam(void); // tolua_end + + /** Return the associated statistic and achievement manager. */ + cStatManager & GetStatManager() { return m_Stats; } void SetIP(const AString & a_IP); @@ -487,6 +492,8 @@ protected: cTeam * m_Team; + cStatManager m_Stats; + void ResolvePermissions(void); diff --git a/src/Protocol/Protocol.h b/src/Protocol/Protocol.h index 8f152ad37..a543c6361 100644 --- a/src/Protocol/Protocol.h +++ b/src/Protocol/Protocol.h @@ -31,6 +31,7 @@ class cMonster; class cChunkDataSerializer; class cFallingBlock; class cCompositeChat; +class cStatManager; @@ -111,6 +112,7 @@ public: virtual void SendSpawnMob (const cMonster & a_Mob) = 0; virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) = 0; virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) = 0; + virtual void SendStatistics (const cStatManager & a_Manager) = 0; virtual void SendTabCompletionResults(const AStringVector & a_Results) = 0; virtual void SendTeleportEntity (const cEntity & a_Entity) = 0; virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) = 0; diff --git a/src/Protocol/Protocol125.cpp b/src/Protocol/Protocol125.cpp index e7873cf7a..f3bdae3ac 100644 --- a/src/Protocol/Protocol125.cpp +++ b/src/Protocol/Protocol125.cpp @@ -99,6 +99,7 @@ enum PACKET_ENCHANT_ITEM = 0x6C, PACKET_UPDATE_SIGN = 0x82, PACKET_ITEM_DATA = 0x83, + PACKET_INCREMENT_STATISTIC = 0xC8, PACKET_PLAYER_LIST_ITEM = 0xC9, PACKET_PLAYER_ABILITIES = 0xca, PACKET_PLUGIN_MESSAGE = 0xfa, @@ -992,6 +993,33 @@ void cProtocol125::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp +void cProtocol125::SendStatistics(const cStatManager & a_Manager) +{ + /* NOTE: + * Versions prior to minecraft 1.7 use an incremental statistic sync + * method. The current setup does not allow us to implement that, because + * of performance considerations. + */ +#if 0 + for (unsigned int i = 0; i < (unsigned int)statCount; ++i) + { + StatValue Value = m_Manager->GetValue((eStatistic) i); + + unsigned int StatID = cStatInfo::GetID((eStatistic) i); + + cCSLock Lock(m_CSPacket); + WriteByte(PACKET_INCREMENT_STATISTIC); + WriteInt(StatID); + WriteByte(Value); /* Can overflow! */ + Flush(); + } +#endif +} + + + + + void cProtocol125::SendTabCompletionResults(const AStringVector & a_Results) { // This protocol version doesn't support tab completion diff --git a/src/Protocol/Protocol125.h b/src/Protocol/Protocol125.h index 423e58d67..18a626a2d 100644 --- a/src/Protocol/Protocol125.h +++ b/src/Protocol/Protocol125.h @@ -84,6 +84,7 @@ public: virtual void SendSpawnMob (const cMonster & a_Mob) override; virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override; virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) override; + virtual void SendStatistics (const cStatManager & a_Manager) override; virtual void SendTabCompletionResults(const AStringVector & a_Results) override; virtual void SendTeleportEntity (const cEntity & a_Entity) override; virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override; diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 443723e40..3b21f7821 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -11,13 +11,19 @@ Implements the 1.7.x protocol classes: #include "json/json.h" #include "Protocol17x.h" #include "ChunkDataSerializer.h" +#include "PolarSSL++/Sha1Checksum.h" + #include "../ClientHandle.h" #include "../Root.h" #include "../Server.h" #include "../World.h" +#include "../StringCompression.h" +#include "../CompositeChat.h" +#include "../Statistics.h" + #include "../WorldStorage/FastNBT.h" #include "../WorldStorage/EnchantmentSerializer.h" -#include "../StringCompression.h" + #include "../Entities/ExpOrb.h" #include "../Entities/Minecart.h" #include "../Entities/FallingBlock.h" @@ -25,15 +31,15 @@ Implements the 1.7.x protocol classes: #include "../Entities/Pickup.h" #include "../Entities/Player.h" #include "../Entities/ItemFrame.h" +#include "../Entities/ArrowEntity.h" +#include "../Entities/FireworkEntity.h" + #include "../Mobs/IncludeAllMonsters.h" #include "../UI/Window.h" + #include "../BlockEntities/CommandBlockEntity.h" #include "../BlockEntities/MobHeadEntity.h" #include "../BlockEntities/FlowerPotEntity.h" -#include "../CompositeChat.h" -#include "../Entities/ArrowEntity.h" -#include "../Entities/FireworkEntity.h" -#include "PolarSSL++/Sha1Checksum.h" @@ -1169,6 +1175,28 @@ void cProtocol172::SendSpawnVehicle(const cEntity & a_Vehicle, char a_VehicleTyp +void cProtocol172::SendStatistics(const cStatManager & a_Manager) +{ + ASSERT(m_State == 3); // In game mode? + + cPacketizer Pkt(*this, 0x37); + Pkt.WriteVarInt(statCount); // TODO 2014-05-11 xdot: Optimization: Send "dirty" statistics only + + for (unsigned int i = 0; i < (unsigned int)statCount; ++i) + { + StatValue Value = a_Manager.GetValue((eStatistic) i); + + const AString & StatName = cStatInfo::GetName((eStatistic) i); + + Pkt.WriteString(StatName); + Pkt.WriteVarInt(Value); + } +} + + + + + void cProtocol172::SendTabCompletionResults(const AStringVector & a_Results) { ASSERT(m_State == 3); // In game mode? @@ -1843,13 +1871,19 @@ void cProtocol172::HandlePacketClientStatus(cByteBuffer & a_ByteBuffer) case 1: { // Request stats - // TODO + const cStatManager & Manager = m_Client->GetPlayer()->GetStatManager(); + SendStatistics(Manager); + break; } case 2: { // Open Inventory achievement - // TODO + cStatManager & Manager = m_Client->GetPlayer()->GetStatManager(); + Manager.AddValue(achOpenInv); + + SendStatistics(Manager); + break; } } diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index dc111e737..3c6a8c085 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -116,6 +116,7 @@ public: virtual void SendSpawnMob (const cMonster & a_Mob) override; virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override; virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) override; + virtual void SendStatistics (const cStatManager & a_Manager) override; virtual void SendTabCompletionResults(const AStringVector & a_Results) override; virtual void SendTeleportEntity (const cEntity & a_Entity) override; virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override; diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index 667fb5cef..b0cbb6def 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -675,6 +675,16 @@ void cProtocolRecognizer::SendSpawnVehicle(const cEntity & a_Vehicle, char a_Veh +void cProtocolRecognizer::SendStatistics(const cStatManager & a_Manager) +{ + ASSERT(m_Protocol != NULL); + m_Protocol->SendStatistics(a_Manager); +} + + + + + void cProtocolRecognizer::SendTabCompletionResults(const AStringVector & a_Results) { ASSERT(m_Protocol != NULL); diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index 37f47379d..3a291bf7a 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -119,6 +119,7 @@ public: virtual void SendSpawnMob (const cMonster & a_Mob) override; virtual void SendSpawnObject (const cEntity & a_Entity, char a_ObjectType, int a_ObjectData, Byte a_Yaw, Byte a_Pitch) override; virtual void SendSpawnVehicle (const cEntity & a_Vehicle, char a_VehicleType, char a_VehicleSubType) override; + virtual void SendStatistics (const cStatManager & a_Manager) override; virtual void SendTabCompletionResults(const AStringVector & a_Results) override; virtual void SendTeleportEntity (const cEntity & a_Entity) override; virtual void SendThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ) override; diff --git a/src/Statistics.cpp b/src/Statistics.cpp index 2c980d98e..94076f828 100644 --- a/src/Statistics.cpp +++ b/src/Statistics.cpp @@ -13,39 +13,39 @@ cStatInfo cStatInfo::ms_Info[statCount] = { // http://minecraft.gamepedia.com/Achievements /* Type | Name | Prerequisite */ - cStatInfo(achOpenInv, "openInventory"), - cStatInfo(achMineWood, "mineWood", achOpenInv), - cStatInfo(achCraftWorkbench, "buildWorkBench", achMineWood), - cStatInfo(achCraftPickaxe, "buildPickaxe", achCraftWorkbench), - cStatInfo(achCraftFurnace, "buildFurnace", achCraftPickaxe), - cStatInfo(achAcquireIron, "acquireIron", achCraftFurnace), - cStatInfo(achCraftHoe, "buildHoe", achCraftWorkbench), - cStatInfo(achMakeBread, "makeBread", achCraftHoe), - cStatInfo(achBakeCake, "bakeCake", achCraftHoe), - cStatInfo(achCraftBetterPick, "buildBetterPickaxe", achCraftPickaxe), - cStatInfo(achCookFish, "cookFish", achAcquireIron), - cStatInfo(achOnARail, "onARail", achAcquireIron), - cStatInfo(achCraftSword, "buildSword", achCraftWorkbench), - cStatInfo(achKillMonster, "killEnemy", achCraftSword), - cStatInfo(achKillCow, "killCow", achCraftSword), - cStatInfo(achFlyPig, "flyPig", achKillCow), - cStatInfo(achSnipeSkeleton, "snipeSkeleton", achKillMonster), - cStatInfo(achDiamonds, "diamonds", achAcquireIron), - cStatInfo(achEnterPortal, "portal", achDiamonds), - cStatInfo(achReturnToSender, "ghast", achEnterPortal), - cStatInfo(achBlazeRod, "blazeRod", achEnterPortal), - cStatInfo(achBrewPotion, "potion", achBlazeRod), - cStatInfo(achEnterTheEnd, "theEnd", achBlazeRod), - cStatInfo(achDefeatDragon, "theEnd2", achEnterTheEnd), - cStatInfo(achCraftEnchantTable, "enchantments", achDiamonds), - cStatInfo(achOverkill, "overkill", achCraftEnchantTable), - cStatInfo(achBookshelf, "bookcase", achCraftEnchantTable), - cStatInfo(achExploreAllBiomes, "exploreAllBiomes", achEnterTheEnd), - cStatInfo(achSpawnWither, "spawnWither", achDefeatDragon), - cStatInfo(achKillWither, "killWither", achSpawnWither), - cStatInfo(achFullBeacon, "fullBeacon", achKillWither), - cStatInfo(achBreedCow, "breedCow", achKillCow), - cStatInfo(achThrowDiamonds, "diamondsToYou", achDiamonds), + cStatInfo(achOpenInv, "achievement.openInventory"), + cStatInfo(achMineWood, "achievement.mineWood", achOpenInv), + cStatInfo(achCraftWorkbench, "achievement.buildWorkBench", achMineWood), + cStatInfo(achCraftPickaxe, "achievement.buildPickaxe", achCraftWorkbench), + cStatInfo(achCraftFurnace, "achievement.buildFurnace", achCraftPickaxe), + cStatInfo(achAcquireIron, "achievement.acquireIron", achCraftFurnace), + cStatInfo(achCraftHoe, "achievement.buildHoe", achCraftWorkbench), + cStatInfo(achMakeBread, "achievement.makeBread", achCraftHoe), + cStatInfo(achBakeCake, "achievement.bakeCake", achCraftHoe), + cStatInfo(achCraftBetterPick, "achievement.buildBetterPickaxe", achCraftPickaxe), + cStatInfo(achCookFish, "achievement.cookFish", achAcquireIron), + cStatInfo(achOnARail, "achievement.onARail", achAcquireIron), + cStatInfo(achCraftSword, "achievement.buildSword", achCraftWorkbench), + cStatInfo(achKillMonster, "achievement.killEnemy", achCraftSword), + cStatInfo(achKillCow, "achievement.killCow", achCraftSword), + cStatInfo(achFlyPig, "achievement.flyPig", achKillCow), + cStatInfo(achSnipeSkeleton, "achievement.snipeSkeleton", achKillMonster), + cStatInfo(achDiamonds, "achievement.diamonds", achAcquireIron), + cStatInfo(achEnterPortal, "achievement.portal", achDiamonds), + cStatInfo(achReturnToSender, "achievement.ghast", achEnterPortal), + cStatInfo(achBlazeRod, "achievement.blazeRod", achEnterPortal), + cStatInfo(achBrewPotion, "achievement.potion", achBlazeRod), + cStatInfo(achEnterTheEnd, "achievement.theEnd", achBlazeRod), + cStatInfo(achDefeatDragon, "achievement.theEnd2", achEnterTheEnd), + cStatInfo(achCraftEnchantTable, "achievement.enchantments", achDiamonds), + cStatInfo(achOverkill, "achievement.overkill", achCraftEnchantTable), + cStatInfo(achBookshelf, "achievement.bookcase", achCraftEnchantTable), + cStatInfo(achExploreAllBiomes, "achievement.exploreAllBiomes", achEnterTheEnd), + cStatInfo(achSpawnWither, "achievement.spawnWither", achDefeatDragon), + cStatInfo(achKillWither, "achievement.killWither", achSpawnWither), + cStatInfo(achFullBeacon, "achievement.fullBeacon", achKillWither), + cStatInfo(achBreedCow, "achievement.breedCow", achKillCow), + cStatInfo(achThrowDiamonds, "achievement.diamondsToYou", achDiamonds), // http://minecraft.gamepedia.com/Statistics @@ -57,6 +57,7 @@ cStatInfo cStatInfo::ms_Info[statCount] = { cStatInfo(statDistFallen, "stat.fallOneCm"), cStatInfo(statDistClimbed, "stat.climbOneCm"), cStatInfo(statDistFlown, "stat.flyOneCm"), + cStatInfo(statDistDove, "stat.diveOneCm"), cStatInfo(statDistMinecart, "stat.minecartOneCm"), cStatInfo(statDistBoat, "stat.boatOneCm"), cStatInfo(statDistPig, "stat.pigOneCm"), @@ -137,3 +138,59 @@ eStatistic cStatInfo::GetPrerequisite(const eStatistic a_Type) +cStatManager::cStatManager() +{ + Reset(); +} + + + + + +StatValue cStatManager::GetValue(const eStatistic a_Stat) const +{ + ASSERT((a_Stat > statInvalid) && (a_Stat < statCount)); + + return m_MainStats[a_Stat]; +} + + + + + +void cStatManager::SetValue(const eStatistic a_Stat, const StatValue a_Value) +{ + ASSERT((a_Stat > statInvalid) && (a_Stat < statCount)); + + m_MainStats[a_Stat] = a_Value; +} + + + + + +StatValue cStatManager::AddValue(const eStatistic a_Stat, const StatValue a_Delta) +{ + ASSERT((a_Stat > statInvalid) && (a_Stat < statCount)); + + m_MainStats[a_Stat] += a_Delta; + + return m_MainStats[a_Stat]; +} + + + + + +void cStatManager::Reset(void) +{ + for (unsigned int i = 0; i < (unsigned int)statCount; ++i) + { + m_MainStats[i] = 0; + } +} + + + + + diff --git a/src/Statistics.h b/src/Statistics.h index 540df38cc..f37f32e1e 100644 --- a/src/Statistics.h +++ b/src/Statistics.h @@ -9,6 +9,7 @@ +// tolua_begin enum eStatistic { // The order must match the order of cStatInfo::ms_Info @@ -77,6 +78,7 @@ enum eStatistic statCount }; +// tolua_end @@ -114,3 +116,49 @@ private: + +/* Signed (?) integral value. */ +typedef int StatValue; // tolua_export + + + + +/** Class that manages the statistics and achievements of a single player. */ +// tolua_begin +class cStatManager +{ +public: + // tolua_end + + cStatManager(); + + // tolua_begin + + /** Return the value of the specified stat. */ + StatValue GetValue(const eStatistic a_Stat) const; + + /** Set the value of the specified stat. */ + void SetValue(const eStatistic a_Stat, const StatValue a_Value); + + /** Reset everything. */ + void Reset(); + + /** Increment the specified stat. + * + * Returns the new value. + */ + StatValue AddValue(const eStatistic a_Stat, const StatValue a_Delta = 1); + + // tolua_end + +private: + + StatValue m_MainStats[statCount]; + + // TODO 10-05-2014 xdot: Use, mine, craft statistics + + +}; // tolua_export + + + diff --git a/src/WorldStorage/StatSerializer.cpp b/src/WorldStorage/StatSerializer.cpp new file mode 100644 index 000000000..5c6724c60 --- /dev/null +++ b/src/WorldStorage/StatSerializer.cpp @@ -0,0 +1,126 @@ + +// StatSerializer.cpp + + +#include "Globals.h" +#include "StatSerializer.h" + +#include "../Statistics.h" + +#include + + + + + +cStatSerializer::cStatSerializer(const AString& a_WorldName, const AString& a_PlayerName, cStatManager* a_Manager) + : m_Manager(a_Manager) +{ + AString StatsPath; + Printf(StatsPath, "%s/stats", a_WorldName.c_str()); + + m_Path = StatsPath + "/" + a_PlayerName + ".dat"; + + /* Ensure that the directory exists. */ + cFile::CreateFolder(FILE_IO_PREFIX + StatsPath); +} + + + + + +bool cStatSerializer::Load(void) +{ + AString Data = cFile::ReadWholeFile(FILE_IO_PREFIX + m_Path); + if (Data.empty()) + { + return false; + } + + Json::Value Root; + Json::Reader Reader; + + if (Reader.parse(Data, Root, false)) + { + return LoadStatFromJSON(Root); + } + + return false; +} + + + + + +bool cStatSerializer::Save(void) +{ + Json::Value Root; + SaveStatToJSON(Root); + + cFile File; + if (!File.Open(FILE_IO_PREFIX + m_Path, cFile::fmWrite)) + { + return false; + } + + Json::StyledWriter Writer; + AString JsonData = Writer.write(Root); + + File.Write(JsonData.data(), JsonData.size()); + File.Close(); + + return true; +} + + + + + +void cStatSerializer::SaveStatToJSON(Json::Value & a_Out) +{ + for (unsigned int i = 0; i < (unsigned int)statCount; ++i) + { + StatValue Value = m_Manager->GetValue((eStatistic) i); + + if (Value != 0) + { + const AString & StatName = cStatInfo::GetName((eStatistic) i); + + a_Out[StatName] = Value; + } + } +} + + + + + +bool cStatSerializer::LoadStatFromJSON(const Json::Value & a_In) +{ + m_Manager->Reset(); + + for (Json::ValueIterator it = a_In.begin() ; it != a_In.end() ; ++it) + { + AString StatName = it.key().asString(); + + eStatistic StatType = cStatInfo::GetType(StatName); + + if (StatType == statInvalid) + { + LOGWARNING("Invalid statistic type %s", StatName.c_str()); + continue; + } + + m_Manager->SetValue(StatType, (*it).asInt()); + } + + return true; +} + + + + + + + + diff --git a/src/WorldStorage/StatSerializer.h b/src/WorldStorage/StatSerializer.h new file mode 100644 index 000000000..43514465b --- /dev/null +++ b/src/WorldStorage/StatSerializer.h @@ -0,0 +1,53 @@ + +// StatSerializer.h + +// Declares the cStatSerializer class that is used for saving stats into JSON + + + + + +#pragma once + +#include "json/json.h" + + + + + +// fwd: +class cStatManager; + + + + +class cStatSerializer +{ +public: + + cStatSerializer(const AString& a_WorldName, const AString& a_PlayerName, cStatManager* a_Manager); + + bool Load(void); + + bool Save(void); + + +protected: + + void SaveStatToJSON(Json::Value & a_Out); + + bool LoadStatFromJSON(const Json::Value & a_In); + + +private: + + cStatManager* m_Manager; + + AString m_Path; + + +} ; + + + + -- cgit v1.2.3 From e3c6c8f3ddffdc368ebbc1a7688de2ca1b97167c Mon Sep 17 00:00:00 2001 From: andrew Date: Sun, 11 May 2014 20:30:54 +0300 Subject: Fixed stat serialization --- src/ClientHandle.cpp | 8 -------- src/Entities/Player.cpp | 31 +++++++++++++++++++++---------- src/Statistics.cpp | 2 +- src/WorldStorage/StatSerializer.cpp | 27 ++++++++++++++++++++++----- 4 files changed, 44 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 03b1b0dc2..c4f4e496a 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -31,8 +31,6 @@ #include "CompositeChat.h" #include "Items/ItemSword.h" -#include "WorldStorage/StatSerializer.h" - #include "md5/md5.h" @@ -339,12 +337,6 @@ void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID) // Send scoreboard data World->GetScoreBoard().SendTo(*this); -#if 0 - // Load stats - cStatSerializer StatSerializer(World->GetName(), m_Player->GetName(), &m_Player->GetStatManager()); - StatSerializer.Load(); -#endif - // 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; diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index a42fe89cf..1df473eb2 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -133,15 +133,6 @@ cPlayer::~cPlayer(void) SaveToDisk(); -#if 0 - /* Save statistics. */ - cStatSerializer StatSerializer(m_World->GetName(), m_PlayerName, &m_Stats); - if (!StatSerializer.Save()) - { - LOGERROR("Could not save stats for player %s", m_PlayerName.c_str()); - } -#endif - m_World->RemovePlayer( this ); m_ClientHandle = NULL; @@ -1638,6 +1629,12 @@ bool cPlayer::LoadFromDisk() m_Inventory.LoadFromJson(root["inventory"]); m_LoadedWorldName = root.get("world", "world").asString(); + + /* Load the player stats. + * We use the default world name (like bukkit) because stats are shared between dimensions/worlds. + */ + cStatSerializer StatSerializer(cRoot::Get()->GetDefaultWorld()->GetName(), GetName(), &m_Stats); + StatSerializer.Load(); LOGD("Player \"%s\" was read from file, spawning at {%.2f, %.2f, %.2f} in world \"%s\"", m_PlayerName.c_str(), GetPosX(), GetPosY(), GetPosZ(), m_LoadedWorldName.c_str() @@ -1709,6 +1706,17 @@ bool cPlayer::SaveToDisk() LOGERROR("ERROR WRITING PLAYER JSON TO FILE \"%s\"", SourceFile.c_str()); return false; } + + /* Save the player stats. + * We use the default world name (like bukkit) because stats are shared between dimensions/worlds. + */ + cStatSerializer StatSerializer(cRoot::Get()->GetDefaultWorld()->GetName(), m_PlayerName, &m_Stats); + if (!StatSerializer.Save()) + { + LOGERROR("Could not save stats for player %s", m_PlayerName.c_str()); + return false; + } + return true; } @@ -1723,7 +1731,10 @@ cPlayer::StringList cPlayer::GetResolvedPermissions() const PermissionMap& ResolvedPermissions = m_ResolvedPermissions; for( PermissionMap::const_iterator itr = ResolvedPermissions.begin(); itr != ResolvedPermissions.end(); ++itr ) { - if( itr->second ) Permissions.push_back( itr->first ); + if (itr->second) + { + Permissions.push_back( itr->first ); + } } return Permissions; diff --git a/src/Statistics.cpp b/src/Statistics.cpp index 94076f828..65addb0dd 100644 --- a/src/Statistics.cpp +++ b/src/Statistics.cpp @@ -114,7 +114,7 @@ eStatistic cStatInfo::GetType(const AString & a_Name) { for (unsigned int i = 0; i < ARRAYCOUNT(ms_Info); ++i) { - if (NoCaseCompare(ms_Info[i].m_Name, a_Name)) + if (NoCaseCompare(ms_Info[i].m_Name, a_Name) == 0) { return ms_Info[i].m_Type; } diff --git a/src/WorldStorage/StatSerializer.cpp b/src/WorldStorage/StatSerializer.cpp index 5c6724c60..66e5e1e9e 100644 --- a/src/WorldStorage/StatSerializer.cpp +++ b/src/WorldStorage/StatSerializer.cpp @@ -7,8 +7,6 @@ #include "../Statistics.h" -#include - @@ -19,7 +17,7 @@ cStatSerializer::cStatSerializer(const AString& a_WorldName, const AString& a_Pl AString StatsPath; Printf(StatsPath, "%s/stats", a_WorldName.c_str()); - m_Path = StatsPath + "/" + a_PlayerName + ".dat"; + m_Path = StatsPath + "/" + a_PlayerName + ".json"; /* Ensure that the directory exists. */ cFile::CreateFolder(FILE_IO_PREFIX + StatsPath); @@ -88,6 +86,8 @@ void cStatSerializer::SaveStatToJSON(Json::Value & a_Out) a_Out[StatName] = Value; } + + // TODO 2014-05-11 xdot: Save "progress" } } @@ -107,11 +107,28 @@ bool cStatSerializer::LoadStatFromJSON(const Json::Value & a_In) if (StatType == statInvalid) { - LOGWARNING("Invalid statistic type %s", StatName.c_str()); + LOGWARNING("Invalid statistic type \"%s\"", StatName.c_str()); continue; } - m_Manager->SetValue(StatType, (*it).asInt()); + Json::Value & Node = *it; + + if (Node.isInt()) + { + m_Manager->SetValue(StatType, Node.asInt()); + } + else if (Node.isObject()) + { + StatValue Value = Node.get("value", 0).asInt(); + + // TODO 2014-05-11 xdot: Load "progress" + + m_Manager->SetValue(StatType, Value); + } + else + { + LOGWARNING("Invalid statistic value for type \"%s\"", StatName.c_str()); + } } return true; -- cgit v1.2.3 From 6cb348395468020ebac978d5a4968953e49d8b90 Mon Sep 17 00:00:00 2001 From: andrew Date: Sun, 11 May 2014 21:41:25 +0300 Subject: Fixed compilation --- src/Entities/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/Entities/CMakeLists.txt b/src/Entities/CMakeLists.txt index c9ca44d38..205cb2cca 100644 --- a/src/Entities/CMakeLists.txt +++ b/src/Entities/CMakeLists.txt @@ -10,3 +10,5 @@ file(GLOB SOURCE ) add_library(Entities ${SOURCE}) + +target_link_libraries(Entities WorldStorage) -- cgit v1.2.3 From 6c57b38b741787cd53a42b905d27e2d137ae194b Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 11 May 2014 13:44:30 -0700 Subject: Fixed a warning and a complaint about a never-read variable. --- src/BlockArea.cpp | 25 +++++++++++-------------- src/WorldStorage/WorldStorage.cpp | 1 - 2 files changed, 11 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index 9e20a0983..aff0bf9c6 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -2123,7 +2123,7 @@ void cBlockArea::MergeByStrategy(const cBlockArea & a_Src, int a_RelX, int a_Rel a_Src.GetSizeX(), a_Src.GetSizeY(), a_Src.GetSizeZ(), m_Size.x, m_Size.y, m_Size.z ); - break; + return; } // case msOverwrite case cBlockArea::msFillAir: @@ -2137,7 +2137,7 @@ void cBlockArea::MergeByStrategy(const cBlockArea & a_Src, int a_RelX, int a_Rel a_Src.GetSizeX(), a_Src.GetSizeY(), a_Src.GetSizeZ(), m_Size.x, m_Size.y, m_Size.z ); - break; + return; } // case msFillAir case cBlockArea::msImprint: @@ -2151,7 +2151,7 @@ void cBlockArea::MergeByStrategy(const cBlockArea & a_Src, int a_RelX, int a_Rel a_Src.GetSizeX(), a_Src.GetSizeY(), a_Src.GetSizeZ(), m_Size.x, m_Size.y, m_Size.z ); - break; + return; } // case msImprint case cBlockArea::msLake: @@ -2165,7 +2165,7 @@ void cBlockArea::MergeByStrategy(const cBlockArea & a_Src, int a_RelX, int a_Rel a_Src.GetSizeX(), a_Src.GetSizeY(), a_Src.GetSizeZ(), m_Size.x, m_Size.y, m_Size.z ); - break; + return; } // case msLake case cBlockArea::msSpongePrint: @@ -2179,7 +2179,7 @@ void cBlockArea::MergeByStrategy(const cBlockArea & a_Src, int a_RelX, int a_Rel a_Src.GetSizeX(), a_Src.GetSizeY(), a_Src.GetSizeZ(), m_Size.x, m_Size.y, m_Size.z ); - break; + return; } // case msSpongePrint case cBlockArea::msDifference: @@ -2193,7 +2193,7 @@ void cBlockArea::MergeByStrategy(const cBlockArea & a_Src, int a_RelX, int a_Rel a_Src.GetSizeX(), a_Src.GetSizeY(), a_Src.GetSizeZ(), m_Size.x, m_Size.y, m_Size.z ); - break; + return; } // case msDifference case cBlockArea::msMask: @@ -2207,16 +2207,13 @@ void cBlockArea::MergeByStrategy(const cBlockArea & a_Src, int a_RelX, int a_Rel a_Src.GetSizeX(), a_Src.GetSizeY(), a_Src.GetSizeZ(), m_Size.x, m_Size.y, m_Size.z ); - break; + return; } // case msMask - - default: - { - LOGWARNING("Unknown block area merge strategy: %d", a_Strategy); - ASSERT(!"Unknown block area merge strategy"); - break; - } } // switch (a_Strategy) + + LOGWARNING("Unknown block area merge strategy: %d", a_Strategy); + ASSERT(!"Unknown block area merge strategy"); + return; } diff --git a/src/WorldStorage/WorldStorage.cpp b/src/WorldStorage/WorldStorage.cpp index 54eaaca5c..6867ad5bc 100644 --- a/src/WorldStorage/WorldStorage.cpp +++ b/src/WorldStorage/WorldStorage.cpp @@ -243,7 +243,6 @@ void cWorldStorage::Execute(void) bool Success; do { - Success = false; if (m_ShouldTerminate) { return; -- cgit v1.2.3 From 3f9e00a3f3486c2845115e495d66f438f90825f3 Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 11 May 2014 16:25:21 -0700 Subject: Fixed a few more switch warnings. --- src/Generating/StructGen.cpp | 28 ++++++++++++------------- src/Mobs/Monster.cpp | 2 ++ src/WorldStorage/NBTChunkSerializer.cpp | 37 ++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/Generating/StructGen.cpp b/src/Generating/StructGen.cpp index db9d5578c..636364e17 100644 --- a/src/Generating/StructGen.cpp +++ b/src/Generating/StructGen.cpp @@ -596,24 +596,22 @@ void cStructGenDirectOverhangs::GenFinish(cChunkDesc & a_ChunkDesc) // Interpolate between FloorLo and FloorHi: for (int z = 0; z < 16; z++) for (int x = 0; x < 16; x++) { - switch (a_ChunkDesc.GetBiome(x, z)) + EMCSBiome biome = a_ChunkDesc.GetBiome(x, z); + + if ((biome == biExtremeHills) || (biome == biExtremeHillsEdge)) { - case biExtremeHills: - case biExtremeHillsEdge: + int Lo = FloorLo[x + 17 * z] / 256; + int Hi = FloorHi[x + 17 * z] / 256; + for (int y = 0; y < SEGMENT_HEIGHT; y++) { - int Lo = FloorLo[x + 17 * z] / 256; - int Hi = FloorHi[x + 17 * z] / 256; - for (int y = 0; y < SEGMENT_HEIGHT; y++) + int Val = Lo + (Hi - Lo) * y / SEGMENT_HEIGHT; + if (Val < 0) { - int Val = Lo + (Hi - Lo) * y / SEGMENT_HEIGHT; - if (Val < 0) - { - a_ChunkDesc.SetBlockType(x, y + Segment, z, E_BLOCK_AIR); - } - } // for y - break; - } - } // switch (biome) + a_ChunkDesc.SetBlockType(x, y + Segment, z, E_BLOCK_AIR); + } + } // for y + break; + } // if (biome) } // for z, x // Swap the floors: diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index 62670907f..5832edb9f 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -355,6 +355,8 @@ void cMonster::Tick(float a_Dt, cChunk & a_Chunk) InStateEscaping(a_Dt); break; } + + case ATTACKING: break; } // switch (m_EMState) BroadcastMovementUpdate(); diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index 2ac1d7962..ab80dd7c8 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -391,38 +391,41 @@ void cNBTChunkSerializer::AddFallingBlockEntity(cFallingBlock * a_FallingBlock) void cNBTChunkSerializer::AddMinecartEntity(cMinecart * a_Minecart) { - const char * EntityClass = NULL; - switch (a_Minecart->GetPayload()) - { - case cMinecart::mpNone: EntityClass = "MinecartRideable"; break; - case cMinecart::mpChest: EntityClass = "MinecartChest"; break; - case cMinecart::mpFurnace: EntityClass = "MinecartFurnace"; break; - case cMinecart::mpTNT: EntityClass = "MinecartTNT"; break; - case cMinecart::mpHopper: EntityClass = "MinecartHopper"; break; - default: - { - ASSERT(!"Unhandled minecart payload type"); - return; - } - } // switch (payload) - m_Writer.BeginCompound(""); - AddBasicEntity(a_Minecart, EntityClass); + switch (a_Minecart->GetPayload()) { case cMinecart::mpChest: { + AddBasicEntity(a_Minecart, "MinecartChest"); // Add chest contents into the Items tag: AddMinecartChestContents((cMinecartWithChest *)a_Minecart); break; } - case cMinecart::mpFurnace: { + AddBasicEntity(a_Minecart, "MinecartFurnace"); // TODO: Add "Push" and "Fuel" tags break; } + case cMinecart::mpHopper: + { + AddBasicEntity(a_Minecart, "MinecartHopper"); + // TODO: Add hopper contents? + break; + } + case cMinecart::mpTNT: + { + AddBasicEntity(a_Minecart, "MinecartTNT"); + break; + } + case cMinecart::mpNone: + { + AddBasicEntity(a_Minecart, "MinecartRideable"); + break; + } } // switch (Payload) + m_Writer.EndCompound(); } -- cgit v1.2.3 From 3a5e04d118a88dc33930866546007f59fdba951f Mon Sep 17 00:00:00 2001 From: archshift Date: Sun, 11 May 2014 16:54:42 -0700 Subject: More switch warnings. --- src/Generating/Trees.cpp | 34 ++++++++++++++++++++++++++------- src/WorldStorage/NBTChunkSerializer.cpp | 7 +++++++ 2 files changed, 34 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Generating/Trees.cpp b/src/Generating/Trees.cpp index 4f1553c36..522f45148 100644 --- a/src/Generating/Trees.cpp +++ b/src/Generating/Trees.cpp @@ -174,7 +174,7 @@ void GetTreeImageByBiome(int a_BlockX, int a_BlockY, int a_BlockZ, cNoise & a_No { GetBirchTreeImage(a_BlockX, a_BlockY, a_BlockZ, a_Noise, a_Seq, a_LogBlocks, a_OtherBlocks); } - break; + return; } case biTaiga: @@ -184,14 +184,14 @@ void GetTreeImageByBiome(int a_BlockX, int a_BlockY, int a_BlockZ, cNoise & a_No { // Conifers GetConiferTreeImage(a_BlockX, a_BlockY, a_BlockZ, a_Noise, a_Seq, a_LogBlocks, a_OtherBlocks); - break; + return; } case biSwampland: { // Swamp trees: GetSwampTreeImage(a_BlockX, a_BlockY, a_BlockZ, a_Noise, a_Seq, a_LogBlocks, a_OtherBlocks); - break; + return; } case biJungle: @@ -207,21 +207,21 @@ void GetTreeImageByBiome(int a_BlockX, int a_BlockY, int a_BlockZ, cNoise & a_No { GetJungleTreeImage(a_BlockX, a_BlockY, a_BlockZ, a_Noise, a_Seq, a_LogBlocks, a_OtherBlocks); } - break; + return; } case biBirchForest: case biBirchForestHills: { GetBirchTreeImage(a_BlockX, a_BlockY, a_BlockZ, a_Noise, a_Seq, a_LogBlocks, a_OtherBlocks); - break; + return; } case biBirchForestM: case biBirchForestHillsM: { GetTallBirchTreeImage(a_BlockX, a_BlockY, a_BlockZ, a_Noise, a_Seq, a_LogBlocks, a_OtherBlocks); - break; + return; } case biRoofedForest: @@ -257,9 +257,29 @@ void GetTreeImageByBiome(int a_BlockX, int a_BlockY, int a_BlockZ, cNoise & a_No { // TODO: These need their special trees GetBirchTreeImage(a_BlockX, a_BlockY, a_BlockZ, a_Noise, a_Seq, a_LogBlocks, a_OtherBlocks); - break; + return; + } + + case biDesert: + case biDesertHills: + case biRiver: + case biBeach: + case biHell: + case biSky: + case biOcean: + case biFrozenOcean: + case biFrozenRiver: + case biVariant: + case biNumBiomes: + case biNumVariantBiomes: + case biInvalidBiome: + { + // These biomes have no trees, or are non-biome members of the enum. + return; } } + + ASSERT(!"Invalid biome type!"); } diff --git a/src/WorldStorage/NBTChunkSerializer.cpp b/src/WorldStorage/NBTChunkSerializer.cpp index ab80dd7c8..7b7cd3c7e 100644 --- a/src/WorldStorage/NBTChunkSerializer.cpp +++ b/src/WorldStorage/NBTChunkSerializer.cpp @@ -653,6 +653,13 @@ void cNBTChunkSerializer::AddHangingEntity(cHangingEntity * a_Hanging) case BLOCK_FACE_YP: m_Writer.AddByte("Dir", (unsigned char)1); break; case BLOCK_FACE_ZM: m_Writer.AddByte("Dir", (unsigned char)0); break; case BLOCK_FACE_ZP: m_Writer.AddByte("Dir", (unsigned char)3); break; + + case BLOCK_FACE_XM: + case BLOCK_FACE_XP: + case BLOCK_FACE_NONE: + { + break; + } } } -- cgit v1.2.3 From b3d2b5b2c94193fd9364b26293b7d96b748ff96d Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 12 May 2014 17:05:09 +0300 Subject: cEntity::Killed(cEntity *) Handler; Achievement triggers; cPlayer::AwardAchievement() --- src/Entities/Entity.cpp | 5 ++++ src/Entities/Entity.h | 3 ++ src/Entities/Pickup.cpp | 10 +++++++ src/Entities/Player.cpp | 70 +++++++++++++++++++++++++++++++++++++++++--- src/Entities/Player.h | 11 +++++++ src/Protocol/Protocol17x.cpp | 6 +--- src/UI/SlotArea.cpp | 42 +++++++++++++++++++++++++- src/UI/SlotArea.h | 6 ++++ 8 files changed, 143 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 4cf10a219..9eb03acde 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -370,6 +370,11 @@ bool cEntity::DoTakeDamage(TakeDamageInfo & a_TDI) if (m_Health <= 0) { KilledBy(a_TDI.Attacker); + + if (a_TDI.Attacker != NULL) + { + a_TDI.Attacker->Killed(this); + } } return true; } diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h index df03d635b..a111b128d 100644 --- a/src/Entities/Entity.h +++ b/src/Entities/Entity.h @@ -299,6 +299,9 @@ public: /// Called when the health drops below zero. a_Killer may be NULL (environmental damage) virtual void KilledBy(cEntity * a_Killer); + /// Called when the entity kills another entity + virtual void Killed(cEntity * a_Victim) {} + /// Heals the specified amount of HPs void Heal(int a_HitPoints); diff --git a/src/Entities/Pickup.cpp b/src/Entities/Pickup.cpp index 497b41683..0fd006485 100644 --- a/src/Entities/Pickup.cpp +++ b/src/Entities/Pickup.cpp @@ -192,6 +192,16 @@ bool cPickup::CollectedBy(cPlayer * a_Dest) int NumAdded = a_Dest->GetInventory().AddItem(m_Item); if (NumAdded > 0) { + // Check achievements + switch (m_Item.m_ItemType) + { + case E_BLOCK_LOG: a_Dest->AwardAchievement(achMineWood); break; + case E_ITEM_LEATHER: a_Dest->AwardAchievement(achKillCow); break; + case E_ITEM_DIAMOND: a_Dest->AwardAchievement(achDiamonds); break; + case E_ITEM_BLAZE_ROD: a_Dest->AwardAchievement(achBlazeRod); break; + default: break; + } + m_Item.m_ItemCount -= NumAdded; m_World->BroadcastCollectPickup(*this, *a_Dest); // Also send the "pop" sound effect with a somewhat random pitch (fast-random using EntityID ;) diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 1df473eb2..48bb509b9 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -17,6 +17,7 @@ #include "../Vector3.h" #include "../WorldStorage/StatSerializer.h" +#include "../CompositeChat.h" #include "inifile/iniFile.h" #include "json/json.h" @@ -876,10 +877,6 @@ void cPlayer::KilledBy(cEntity * a_Killer) cPlayer* Killer = (cPlayer*)a_Killer; GetWorld()->BroadcastChatDeath(Printf("%s was killed by %s", GetName().c_str(), Killer->GetName().c_str())); - - Killer->GetStatManager().AddValue(statPlayerKills); - - m_World->GetScoreBoard().AddPlayerScore(Killer->GetName(), cObjective::otPlayerKillCount, 1); } else { @@ -898,6 +895,33 @@ void cPlayer::KilledBy(cEntity * a_Killer) +void cPlayer::Killed(cEntity * a_Victim) +{ + cScoreboard & ScoreBoard = m_World->GetScoreBoard(); + + if (a_Victim->IsPlayer()) + { + m_Stats.AddValue(statPlayerKills); + + ScoreBoard.AddPlayerScore(GetName(), cObjective::otPlayerKillCount, 1); + } + else if (a_Victim->IsMob()) + { + if (((cMonster *)a_Victim)->GetMobFamily() == cMonster::mfHostile) + { + AwardAchievement(achKillMonster); + } + + m_Stats.AddValue(statMobKills); + } + + ScoreBoard.AddPlayerScore(GetName(), cObjective::otTotalKillCount, 1); +} + + + + + void cPlayer::Respawn(void) { m_Health = GetMaxHealth(); @@ -1116,6 +1140,44 @@ void cPlayer::SetIP(const AString & a_IP) +unsigned int cPlayer::AwardAchievement(const eStatistic a_Ach) +{ + eStatistic Prerequisite = cStatInfo::GetPrerequisite(a_Ach); + + if (Prerequisite != statInvalid) + { + if (m_Stats.GetValue(Prerequisite) == 0) + { + return 0; + } + } + + StatValue Old = m_Stats.GetValue(a_Ach); + + if (Old > 0) + { + return m_Stats.AddValue(a_Ach); + } + else + { + cCompositeChat Msg; + Msg.AddTextPart(m_PlayerName + " has just earned the achievement "); + Msg.AddTextPart(cStatInfo::GetName(a_Ach)); // TODO 2014-05-12 xdot: Use the proper cCompositeChat submessage type and send the actual title + m_World->BroadcastChat(Msg); + + StatValue New = m_Stats.AddValue(a_Ach); + + /* Achievement Get! */ + m_ClientHandle->SendStatistics(m_Stats); + + return New; + } +} + + + + + void cPlayer::TeleportToCoords(double a_PosX, double a_PosY, double a_PosZ) { SetPosition(a_PosX, a_PosY, a_PosZ); diff --git a/src/Entities/Player.h b/src/Entities/Player.h index 82a138290..b5c9d75cc 100644 --- a/src/Entities/Player.h +++ b/src/Entities/Player.h @@ -179,6 +179,15 @@ public: /** Return the associated statistic and achievement manager. */ cStatManager & GetStatManager() { return m_Stats; } + + /** Awards the player an achievement. + * + * If all prerequisites are met, this method will award the achievement and will broadcast a chat message. + * If the achievement has been already awarded to the player, this method will just increment the stat counter. + * + * Returns the _new_ stat value. (0 = Could not award achievement) + */ + unsigned int AwardAchievement(const eStatistic a_Ach); void SetIP(const AString & a_IP); @@ -311,6 +320,8 @@ public: void AbortEating(void); virtual void KilledBy(cEntity * a_Killer) override; + + virtual void Killed(cEntity * a_Victim) override; void Respawn(void); // tolua_export diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 3b21f7821..39feee16f 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -1879,11 +1879,7 @@ void cProtocol172::HandlePacketClientStatus(cByteBuffer & a_ByteBuffer) case 2: { // Open Inventory achievement - cStatManager & Manager = m_Client->GetPlayer()->GetStatManager(); - Manager.AddValue(achOpenInv); - - SendStatistics(Manager); - + m_Client->GetPlayer()->AwardAchievement(achOpenInv); break; } } diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 788974f9c..1e3d8eaa4 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -496,6 +496,8 @@ void cSlotAreaCrafting::ClickedResult(cPlayer & a_Player) DraggingItem = Result; Recipe.ConsumeIngredients(Grid); Grid.CopyToItems(PlayerSlots); + + HandleCraftItem(Result, a_Player); } else if (DraggingItem.IsEqual(Result)) { @@ -505,6 +507,8 @@ void cSlotAreaCrafting::ClickedResult(cPlayer & a_Player) DraggingItem.m_ItemCount += Result.m_ItemCount; Recipe.ConsumeIngredients(Grid); Grid.CopyToItems(PlayerSlots); + + HandleCraftItem(Result, a_Player); } } @@ -594,6 +598,27 @@ cCraftingRecipe & cSlotAreaCrafting::GetRecipeForPlayer(cPlayer & a_Player) +void cSlotAreaCrafting::HandleCraftItem(const cItem & a_Result, cPlayer & a_Player) +{ + switch (a_Result.m_ItemType) + { + case E_BLOCK_WORKBENCH: a_Player.AwardAchievement(achCraftWorkbench); break; + case E_BLOCK_FURNACE: a_Player.AwardAchievement(achCraftFurnace); break; + case E_BLOCK_CAKE: a_Player.AwardAchievement(achBakeCake); break; + case E_BLOCK_ENCHANTMENT_TABLE: a_Player.AwardAchievement(achCraftEnchantTable); break; + case E_BLOCK_BOOKCASE: a_Player.AwardAchievement(achBookshelf); break; + case E_ITEM_WOODEN_PICKAXE: a_Player.AwardAchievement(achCraftPickaxe); break; + case E_ITEM_WOODEN_SWORD: a_Player.AwardAchievement(achCraftSword); break; + case E_ITEM_STONE_PICKAXE: a_Player.AwardAchievement(achCraftBetterPick); break; + case E_ITEM_WOODEN_HOE: a_Player.AwardAchievement(achCraftHoe); break; + case E_ITEM_BREAD: a_Player.AwardAchievement(achMakeBread); break; + default: break; + } +} + + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cSlotAreaAnvil: @@ -1393,7 +1418,7 @@ void cSlotAreaFurnace::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) { // Something has changed in the window, broadcast the entire window to all clients ASSERT(a_ItemGrid == &(m_Furnace->GetContents())); - + m_ParentWindow.BroadcastWholeWindow(); } @@ -1401,6 +1426,21 @@ void cSlotAreaFurnace::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) +void cSlotAreaFurnace::HandleSmeltItem(const cItem & a_Result, cPlayer & a_Player) +{ + /** TODO 2014-05-12 xdot: Figure out when to call this method. */ + switch (a_Result.m_ItemType) + { + case E_ITEM_IRON: a_Player.AwardAchievement(achAcquireIron); break; + case E_ITEM_COOKED_FISH: a_Player.AwardAchievement(achCookFish); break; + default: break; + } +} + + + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cSlotAreaInventoryBase: diff --git a/src/UI/SlotArea.h b/src/UI/SlotArea.h index 4da6a672f..e297bcff7 100644 --- a/src/UI/SlotArea.h +++ b/src/UI/SlotArea.h @@ -254,6 +254,9 @@ protected: /// Retrieves the recipe for the specified player from the map, or creates one if not found cCraftingRecipe & GetRecipeForPlayer(cPlayer & a_Player); + + /// Called after an item has been crafted to handle statistics e.t.c. + void HandleCraftItem(const cItem & a_Result, cPlayer & a_Player); } ; @@ -397,6 +400,9 @@ protected: // cItemGrid::cListener overrides: virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override; + + /// Called after an item has been smelted to handle statistics e.t.c. + void HandleSmeltItem(const cItem & a_Result, cPlayer & a_Player); } ; -- cgit v1.2.3 From aea866f5b10d5ab0226260b4d25c70b1cfd31d2a Mon Sep 17 00:00:00 2001 From: andrew Date: Mon, 12 May 2014 21:38:52 +0300 Subject: Movement Statistics --- src/Entities/Entity.cpp | 15 +++++++-- src/Entities/Player.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++------ src/Entities/Player.h | 6 ++++ src/Item.h | 2 +- src/Mobs/Monster.cpp | 4 ++- src/Statistics.cpp | 2 +- 6 files changed, 101 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 9eb03acde..c393f89fd 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -312,12 +312,16 @@ bool cEntity::DoTakeDamage(TakeDamageInfo & a_TDI) if ((a_TDI.Attacker != NULL) && (a_TDI.Attacker->IsPlayer())) { + cPlayer * Player = (cPlayer *)a_TDI.Attacker; + // IsOnGround() only is false if the player is moving downwards - if (!((cPlayer *)a_TDI.Attacker)->IsOnGround()) // TODO: Better damage increase, and check for enchantments (and use magic critical instead of plain) + if (!Player->IsOnGround()) // TODO: Better damage increase, and check for enchantments (and use magic critical instead of plain) { a_TDI.FinalDamage += 2; m_World->BroadcastEntityAnimation(*this, 4); // Critical hit } + + Player->GetStatManager().AddValue(statDamageDealt, round(a_TDI.FinalDamage * 10)); } m_Health -= (short)a_TDI.FinalDamage; @@ -580,9 +584,16 @@ void cEntity::Tick(float a_Dt, cChunk & a_Chunk) if (m_AttachedTo != NULL) { - if ((m_Pos - m_AttachedTo->GetPosition()).Length() > 0.5) + Vector3d DeltaPos = m_Pos - m_AttachedTo->GetPosition(); + if (DeltaPos.Length() > 0.5) { SetPosition(m_AttachedTo->GetPosition()); + + if (IsPlayer()) + { + cPlayer * Player = (cPlayer *)this; + Player->UpdateMovementStats(DeltaPos); + } } } else diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 48bb509b9..3df7c4c34 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -194,6 +194,8 @@ void cPlayer::Tick(float a_Dt, cChunk & a_Chunk) return; } } + + m_Stats.AddValue(statMinutesPlayed, 1); if (!a_Chunk.IsValid()) { @@ -835,6 +837,8 @@ bool cPlayer::DoTakeDamage(TakeDamageInfo & a_TDI) // Any kind of damage adds food exhaustion AddFoodExhaustion(0.3f); SendHealth(); + + m_Stats.AddValue(statDamageTaken, round(a_TDI.FinalDamage * 10)); return true; } return false; @@ -865,6 +869,8 @@ void cPlayer::KilledBy(cEntity * a_Killer) Pickups.Add(cItem(E_ITEM_RED_APPLE)); } + m_Stats.AddValue(statItemsDropped, Pickups.Size()); + m_World->SpawnItemPickups(Pickups, GetPosX(), GetPosY(), GetPosZ(), 10); SaveToDisk(); // Save it, yeah the world is a tough place ! @@ -1262,6 +1268,9 @@ void cPlayer::MoveTo( const Vector3d & a_NewPos ) // TODO: should do some checks to see if player is not moving through terrain // TODO: Official server refuses position packets too far away from each other, kicking "hacked" clients; we should, too + + Vector3d DeltaPos = a_NewPos - GetPosition(); + UpdateMovementStats(DeltaPos); SetPosition( a_NewPos ); SetStance(a_NewPos.y + 1.62); @@ -1492,10 +1501,7 @@ void cPlayer::TossEquippedItem(char a_Amount) Drops.push_back(DroppedItem); } - double vX = 0, vY = 0, vZ = 0; - EulerToVector(-GetYaw(), GetPitch(), vZ, vX, vY); - vY = -vY * 2 + 1.f; - m_World->SpawnItemPickups(Drops, GetPosX(), GetEyeHeight(), GetPosZ(), vX * 3, vY * 3, vZ * 3, true); // 'true' because created by player + TossItems(Drops); } @@ -1511,6 +1517,7 @@ void cPlayer::TossHeldItem(char a_Amount) char OriginalItemAmount = Item.m_ItemCount; Item.m_ItemCount = std::min(OriginalItemAmount, a_Amount); Drops.push_back(Item); + if (OriginalItemAmount > a_Amount) { Item.m_ItemCount = OriginalItemAmount - a_Amount; @@ -1521,10 +1528,7 @@ void cPlayer::TossHeldItem(char a_Amount) } } - double vX = 0, vY = 0, vZ = 0; - EulerToVector(-GetYaw(), GetPitch(), vZ, vX, vY); - vY = -vY * 2 + 1.f; - m_World->SpawnItemPickups(Drops, GetPosX(), GetEyeHeight(), GetPosZ(), vX * 3, vY * 3, vZ * 3, true); // 'true' because created by player + TossItems(Drops); } @@ -1536,10 +1540,21 @@ void cPlayer::TossPickup(const cItem & a_Item) cItems Drops; Drops.push_back(a_Item); + TossItems(Drops); +} + + + + + +void cPlayer::TossItems(const cItems & a_Items) +{ + m_Stats.AddValue(statItemsDropped, a_Items.Size()); + double vX = 0, vY = 0, vZ = 0; EulerToVector(-GetYaw(), GetPitch(), vZ, vX, vY); vY = -vY * 2 + 1.f; - m_World->SpawnItemPickups(Drops, GetPosX(), GetEyeHeight(), GetPosZ(), vX * 3, vY * 3, vZ * 3, true); // 'true' because created by player + m_World->SpawnItemPickups(a_Items, GetPosX(), GetEyeHeight(), GetPosZ(), vX * 3, vY * 3, vZ * 3, true); // 'true' because created by player } @@ -1935,6 +1950,59 @@ void cPlayer::HandleFloater() +void cPlayer::UpdateMovementStats(const Vector3d & a_DeltaPos) +{ + StatValue Value = round(a_DeltaPos.Length() * 100); + + if (m_AttachedTo == NULL) + { + int PosX = POSX_TOINT; + int PosY = POSY_TOINT; + int PosZ = POSZ_TOINT; + + BLOCKTYPE Block; + NIBBLETYPE Meta; + if (!m_World->GetBlockTypeMeta(PosX, PosY, PosZ, Block, Meta)) + { + return; + } + + if ((Block == E_BLOCK_LADDER) && (a_DeltaPos.y > 0.0)) // Going up + { + m_Stats.AddValue(statDistClimbed, round(a_DeltaPos.y * 100)); + } + else + { + // TODO 2014-05-12 xdot: Other types + m_Stats.AddValue(statDistWalked, Value); + } + } + else + { + switch (m_AttachedTo->GetEntityType()) + { + case cEntity::etMinecart: m_Stats.AddValue(statDistMinecart, Value); break; + case cEntity::etBoat: m_Stats.AddValue(statDistBoat, Value); break; + case cEntity::etMonster: + { + cMonster * Monster = (cMonster *)m_AttachedTo; + switch (Monster->GetMobType()) + { + case cMonster::mtPig: m_Stats.AddValue(statDistPig, Value); break; + case cMonster::mtHorse: m_Stats.AddValue(statDistHorse, Value); break; + default: break; + } + break; + } + default: break; + } + } +} + + + + + void cPlayer::ApplyFoodExhaustionFromMovement() { if (IsGameModeCreative()) diff --git a/src/Entities/Player.h b/src/Entities/Player.h index b5c9d75cc..3de5e9c68 100644 --- a/src/Entities/Player.h +++ b/src/Entities/Player.h @@ -391,6 +391,9 @@ public: /** If true the player can fly even when he's not in creative. */ void SetCanFly(bool a_CanFly); + /** Update movement-related statistics. */ + void UpdateMovementStats(const Vector3d & a_DeltaPos); + /** Returns wheter the player can fly or not. */ virtual bool CanFly(void) const { return m_CanFly; } // tolua_end @@ -524,6 +527,9 @@ protected: /** Called in each tick if the player is fishing to make sure the floater dissapears when the player doesn't have a fishing rod as equipped item. */ void HandleFloater(void); + /** Tosses a list of items. */ + void TossItems(const cItems & a_Items); + /** Adds food exhaustion based on the difference between Pos and LastPos, sprinting status and swimming (in water block) */ void ApplyFoodExhaustionFromMovement(); diff --git a/src/Item.h b/src/Item.h index 2f65d5344..acbc880dc 100644 --- a/src/Item.h +++ b/src/Item.h @@ -228,7 +228,7 @@ public: void Add (const cItem & a_Item) {push_back(a_Item); } void Delete(int a_Idx); void Clear (void) {clear(); } - size_t Size (void) {return size(); } + size_t Size (void) const { return size(); } void Set (int a_Idx, short a_ItemType, char a_ItemCount, short a_ItemDamage); void Add (short a_ItemType, char a_ItemCount, short a_ItemDamage) diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index 62670907f..f3b408e68 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -464,8 +464,10 @@ bool cMonster::DoTakeDamage(TakeDamageInfo & a_TDI) return false; } - if((m_SoundHurt != "") && (m_Health > 0)) + if ((m_SoundHurt != "") && (m_Health > 0)) + { m_World->BroadcastSoundEffect(m_SoundHurt, (int)(GetPosX() * 8), (int)(GetPosY() * 8), (int)(GetPosZ() * 8), 1.0f, 0.8f); + } if (a_TDI.Attacker != NULL) { diff --git a/src/Statistics.cpp b/src/Statistics.cpp index 65addb0dd..5950eb96c 100644 --- a/src/Statistics.cpp +++ b/src/Statistics.cpp @@ -64,7 +64,7 @@ cStatInfo cStatInfo::ms_Info[statCount] = { cStatInfo(statDistHorse, "stat.horseOneCm"), cStatInfo(statJumps, "stat.jump"), cStatInfo(statItemsDropped, "stat.drop"), - cStatInfo(statDamageDealt, "stat.damageDealth"), + cStatInfo(statDamageDealt, "stat.damageDealt"), cStatInfo(statDamageTaken, "stat.damageTaken"), cStatInfo(statDeaths, "stat.deaths"), cStatInfo(statMobKills, "stat.mobKills"), -- cgit v1.2.3 From 466ff2204f18fda5f4f0f0b3e19f671d57747c24 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 13 May 2014 14:53:15 +0300 Subject: Fixes --- src/Entities/Player.cpp | 21 +++++++++++---------- src/Entities/Player.h | 9 +++------ src/Mobs/Monster.cpp | 2 +- src/WorldStorage/StatSerializer.cpp | 7 +++++-- src/WorldStorage/StatSerializer.h | 4 +++- 5 files changed, 23 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 3df7c4c34..632c41936 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -820,7 +820,7 @@ bool cPlayer::DoTakeDamage(TakeDamageInfo & a_TDI) if ((a_TDI.Attacker != NULL) && (a_TDI.Attacker->IsPlayer())) { - cPlayer* Attacker = (cPlayer*) a_TDI.Attacker; + cPlayer * Attacker = (cPlayer *)a_TDI.Attacker; if ((m_Team != NULL) && (m_Team == Attacker->m_Team)) { @@ -880,7 +880,7 @@ void cPlayer::KilledBy(cEntity * a_Killer) } else if (a_Killer->IsPlayer()) { - cPlayer* Killer = (cPlayer*)a_Killer; + cPlayer * Killer = (cPlayer *)a_Killer; GetWorld()->BroadcastChatDeath(Printf("%s was killed by %s", GetName().c_str(), Killer->GetName().c_str())); } @@ -1150,6 +1150,7 @@ unsigned int cPlayer::AwardAchievement(const eStatistic a_Ach) { eStatistic Prerequisite = cStatInfo::GetPrerequisite(a_Ach); + // Check if the prerequisites are met if (Prerequisite != statInvalid) { if (m_Stats.GetValue(Prerequisite) == 0) @@ -1166,14 +1167,16 @@ unsigned int cPlayer::AwardAchievement(const eStatistic a_Ach) } else { + // First time, announce it cCompositeChat Msg; Msg.AddTextPart(m_PlayerName + " has just earned the achievement "); - Msg.AddTextPart(cStatInfo::GetName(a_Ach)); // TODO 2014-05-12 xdot: Use the proper cCompositeChat submessage type and send the actual title + Msg.AddTextPart(cStatInfo::GetName(a_Ach)); // TODO 2014-05-12 xdot: Use the proper cCompositeChat part (cAchievement) m_World->BroadcastChat(Msg); + // Increment the statistic StatValue New = m_Stats.AddValue(a_Ach); - /* Achievement Get! */ + // Achievement Get! m_ClientHandle->SendStatistics(m_Stats); return New; @@ -1707,9 +1710,8 @@ bool cPlayer::LoadFromDisk() m_LoadedWorldName = root.get("world", "world").asString(); - /* Load the player stats. - * We use the default world name (like bukkit) because stats are shared between dimensions/worlds. - */ + // Load the player stats. + // We use the default world name (like bukkit) because stats are shared between dimensions/worlds. cStatSerializer StatSerializer(cRoot::Get()->GetDefaultWorld()->GetName(), GetName(), &m_Stats); StatSerializer.Load(); @@ -1784,9 +1786,8 @@ bool cPlayer::SaveToDisk() return false; } - /* Save the player stats. - * We use the default world name (like bukkit) because stats are shared between dimensions/worlds. - */ + // Save the player stats. + // We use the default world name (like bukkit) because stats are shared between dimensions/worlds. cStatSerializer StatSerializer(cRoot::Get()->GetDefaultWorld()->GetName(), m_PlayerName, &m_Stats); if (!StatSerializer.Save()) { diff --git a/src/Entities/Player.h b/src/Entities/Player.h index 3de5e9c68..78b534d83 100644 --- a/src/Entities/Player.h +++ b/src/Entities/Player.h @@ -181,12 +181,9 @@ public: cStatManager & GetStatManager() { return m_Stats; } /** Awards the player an achievement. - * - * If all prerequisites are met, this method will award the achievement and will broadcast a chat message. - * If the achievement has been already awarded to the player, this method will just increment the stat counter. - * - * Returns the _new_ stat value. (0 = Could not award achievement) - */ + If all prerequisites are met, this method will award the achievement and will broadcast a chat message. + If the achievement has been already awarded to the player, this method will just increment the stat counter. + Returns the _new_ stat value. (0 = Could not award achievement) */ unsigned int AwardAchievement(const eStatistic a_Ach); void SetIP(const AString & a_IP); diff --git a/src/Mobs/Monster.cpp b/src/Mobs/Monster.cpp index f3b408e68..b2e42445b 100644 --- a/src/Mobs/Monster.cpp +++ b/src/Mobs/Monster.cpp @@ -464,7 +464,7 @@ bool cMonster::DoTakeDamage(TakeDamageInfo & a_TDI) return false; } - if ((m_SoundHurt != "") && (m_Health > 0)) + if (!m_SoundHurt.empty() && (m_Health > 0)) { m_World->BroadcastSoundEffect(m_SoundHurt, (int)(GetPosX() * 8), (int)(GetPosY() * 8), (int)(GetPosZ() * 8), 1.0f, 0.8f); } diff --git a/src/WorldStorage/StatSerializer.cpp b/src/WorldStorage/StatSerializer.cpp index 66e5e1e9e..74113941c 100644 --- a/src/WorldStorage/StatSerializer.cpp +++ b/src/WorldStorage/StatSerializer.cpp @@ -11,15 +11,18 @@ -cStatSerializer::cStatSerializer(const AString& a_WorldName, const AString& a_PlayerName, cStatManager* a_Manager) +cStatSerializer::cStatSerializer(const AString & a_WorldName, const AString & a_PlayerName, cStatManager * a_Manager) : m_Manager(a_Manager) { + // Even though stats are shared between worlds, they are (usually) saved + // inside the folder of the default world. + AString StatsPath; Printf(StatsPath, "%s/stats", a_WorldName.c_str()); m_Path = StatsPath + "/" + a_PlayerName + ".json"; - /* Ensure that the directory exists. */ + // Ensure that the directory exists. cFile::CreateFolder(FILE_IO_PREFIX + StatsPath); } diff --git a/src/WorldStorage/StatSerializer.h b/src/WorldStorage/StatSerializer.h index 43514465b..72f8d74f1 100644 --- a/src/WorldStorage/StatSerializer.h +++ b/src/WorldStorage/StatSerializer.h @@ -25,10 +25,12 @@ class cStatSerializer { public: - cStatSerializer(const AString& a_WorldName, const AString& a_PlayerName, cStatManager* a_Manager); + cStatSerializer(const AString & a_WorldName, const AString & a_PlayerName, cStatManager * a_Manager); + /* Try to load the player statistics. Returns whether the operation was successful or not. */ bool Load(void); + /* Try to save the player statistics. Returns whether the operation was successful or not. */ bool Save(void); -- cgit v1.2.3 From 6c5ff597bbbb3abd2985eda6bf35ea2d98cf69a4 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 15 May 2014 19:58:48 +0200 Subject: Move radius check. --- src/ClientHandle.cpp | 65 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 9238418a4..cf9d0bfeb 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -815,6 +815,16 @@ void cClientHandle::HandleLeftClick(int a_BlockX, int a_BlockY, int a_BlockZ, eB return; } + if ( + (Diff(m_Player->GetPosX(), (double)a_BlockX) > 6) || + (Diff(m_Player->GetPosY(), (double)a_BlockY) > 6) || + (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6) + ) + { + m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); + return; + } + cPluginManager * PlgMgr = cRoot::Get()->GetPluginManager(); if (PlgMgr->CallHookPlayerLeftClick(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_Status)) { @@ -927,6 +937,16 @@ void cClientHandle::HandleBlockDigStarted(int a_BlockX, int a_BlockY, int a_Bloc return; } + if ( + (Diff(m_Player->GetPosX(), (double)a_BlockX) > 6) || + (Diff(m_Player->GetPosY(), (double)a_BlockY) > 6) || + (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6) + ) + { + m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, this); + return; + } + // Set the last digging coords to the block being dug, so that they can be checked in DIG_FINISHED to avoid dig/aim bug in the client: m_HasStartedDigging = true; m_LastDigBlockX = a_BlockX; @@ -1000,6 +1020,7 @@ void cClientHandle::HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_Blo FinishDigAnimation(); + cWorld * World = m_Player->GetWorld(); cItemHandler * ItemHandler = cItemHandler::GetItemHandler(m_Player->GetEquippedItem()); if (cRoot::Get()->GetPluginManager()->CallHookPlayerBreakingBlock(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_OldBlock, a_OldMeta)) @@ -1008,24 +1029,12 @@ void cClientHandle::HandleBlockDigFinished(int a_BlockX, int a_BlockY, int a_Blo m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); return; } - + if (a_OldBlock == E_BLOCK_AIR) { LOGD("Dug air - what the function?"); return; } - - cWorld * World = m_Player->GetWorld(); - - if ( - (Diff(m_Player->GetPosX(), (double)a_BlockX) > 6) || - (Diff(m_Player->GetPosY(), (double)a_BlockY) > 6) || - (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6) - ) - { - m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); - return; - } ItemHandler->OnBlockDestroyed(World, m_Player, m_Player->GetEquippedItem(), a_BlockX, a_BlockY, a_BlockZ); // The ItemHandler is also responsible for spawning the pickups @@ -1078,7 +1087,23 @@ void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, e ); cWorld * World = m_Player->GetWorld(); - + + if ( + (Diff(m_Player->GetPosX(), (double)a_BlockX) > 6) || + (Diff(m_Player->GetPosY(), (double)a_BlockY) > 6) || + (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6) + ) + { + if (a_BlockFace != BLOCK_FACE_NONE) + { + AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); + World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); + World->SendBlockTo(a_BlockX, a_BlockY + 1, a_BlockZ, m_Player); // 2 block high things + m_Player->GetInventory().SendEquippedSlot(); + } + return; + } + cPluginManager * PlgMgr = cRoot::Get()->GetPluginManager(); if (PlgMgr->CallHookPlayerRightClick(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ)) { @@ -1092,7 +1117,7 @@ void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, e { AddFaceDirection(a_BlockX, a_BlockY, a_BlockZ, a_BlockFace); World->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); - World->SendBlockTo(a_BlockX, a_BlockY + 1, a_BlockZ, m_Player); //2 block high things + World->SendBlockTo(a_BlockX, a_BlockY + 1, a_BlockZ, m_Player); // 2 block high things m_Player->GetInventory().SendEquippedSlot(); } return; @@ -1202,16 +1227,6 @@ void cClientHandle::HandlePlaceBlock(int a_BlockX, int a_BlockY, int a_BlockZ, e // The block is being placed outside the world, ignore this packet altogether (#128) return; } - - if ( - (Diff(m_Player->GetPosX(), (double)a_BlockX) > 6) || - (Diff(m_Player->GetPosY(), (double)a_BlockY) > 6) || - (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6) - ) - { - m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); - return; - } World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, ClickedBlock, ClickedBlockMeta); -- cgit v1.2.3 From 28a9c16db2212d57cca823a2b122530fdb70f210 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 15 May 2014 19:59:52 +0200 Subject: Fix compile error. --- src/ClientHandle.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index cf9d0bfeb..0c897374b 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -943,7 +943,7 @@ void cClientHandle::HandleBlockDigStarted(int a_BlockX, int a_BlockY, int a_Bloc (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6) ) { - m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, this); + m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); return; } -- cgit v1.2.3 From 5724dcc2c199a5d7aec1bf60a160106770a2a63a Mon Sep 17 00:00:00 2001 From: tonibm19 Date: Fri, 16 May 2014 19:33:57 +0200 Subject: Fixed anvil exp removing --- src/UI/SlotArea.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/UI/SlotArea.cpp b/src/UI/SlotArea.cpp index 788974f9c..8833a767a 100644 --- a/src/UI/SlotArea.cpp +++ b/src/UI/SlotArea.cpp @@ -760,7 +760,7 @@ void cSlotAreaAnvil::OnTakeResult(cPlayer & a_Player) { if (!a_Player.IsGameModeCreative()) { - a_Player.DeltaExperience(cPlayer::XpForLevel(m_MaximumCost)); + a_Player.DeltaExperience(-cPlayer::XpForLevel(m_MaximumCost)); } SetSlot(0, a_Player, cItem()); -- cgit v1.2.3 From 6951b68aaf208c7efdcc954fbc54420966fafa4a Mon Sep 17 00:00:00 2001 From: Julian Laubstein Date: Sat, 17 May 2014 18:38:33 +0200 Subject: Added load command in the cServer class --- src/Server.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/Server.cpp b/src/Server.cpp index bfb1b1cbb..91bc6853e 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -476,6 +476,12 @@ void cServer::ExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallbac a_Output.Finished(); return; } + if (split[0] == "load" && !split[1].empty()) + { + cPluginManager::Get()->LoadPlugin(split[1]); + a_Output.Out("Plugin " + split[1] + " added and activated!"); + a_Output.Finished(); + } // There is currently no way a plugin can do these (and probably won't ever be): if (split[0].compare("chunkstats") == 0) -- cgit v1.2.3 From 19f4cd05470785a1afdc764b79db3777f62fe65d Mon Sep 17 00:00:00 2001 From: Julian Laubstein Date: Sat, 17 May 2014 19:39:16 +0200 Subject: Added load cmd --- src/Server.cpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Server.cpp b/src/Server.cpp index 91bc6853e..8e222b743 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -476,13 +476,34 @@ void cServer::ExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallbac a_Output.Finished(); return; } - if (split[0] == "load" && !split[1].empty()) + if (split[0] == "load") { - cPluginManager::Get()->LoadPlugin(split[1]); - a_Output.Out("Plugin " + split[1] + " added and activated!"); - a_Output.Finished(); + if (split.size() > 1) + { + cPluginManager::Get()->LoadPlugin(split[1]); + return; + } + else + { + a_Output.Out("No plugin given! Command: load "); + a_Output.Finished(); + return; + } } - + /* + * TODO: Declare unload command + if (split[0] == "unload") + { + if (split.size() > 1) + { + + } + else + { + + } + } + */ // There is currently no way a plugin can do these (and probably won't ever be): if (split[0].compare("chunkstats") == 0) { @@ -573,6 +594,9 @@ void cServer::BindBuiltInConsoleCommands(void) PlgMgr->BindConsoleCommand("restart", NULL, " - Restarts the server cleanly"); PlgMgr->BindConsoleCommand("stop", NULL, " - Stops the server cleanly"); PlgMgr->BindConsoleCommand("chunkstats", NULL, " - Displays detailed chunk memory statistics"); + PlgMgr->BindConsoleCommand("load ", NULL, " - Adds and enables the specified plugin"); + PlgMgr->BindConsoleCommand("unload ", NULL, " - Disables the specified plugin"); + #if defined(_MSC_VER) && defined(_DEBUG) && defined(ENABLE_LEAK_FINDER) PlgMgr->BindConsoleCommand("dumpmem", NULL, " - Dumps all used memory blocks together with their callstacks into memdump.xml"); #endif -- cgit v1.2.3 From cfd3c33dfa38c946151a1c4d3ba780f7733963c0 Mon Sep 17 00:00:00 2001 From: Julian Laubstein Date: Sun, 18 May 2014 17:16:02 +0200 Subject: Added unload command --- src/Server.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Server.cpp b/src/Server.cpp index 8e222b743..e71e728a6 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -29,6 +29,7 @@ #include #include #include +#include extern "C" { #include "zlib/zlib.h" @@ -481,6 +482,7 @@ void cServer::ExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallbac if (split.size() > 1) { cPluginManager::Get()->LoadPlugin(split[1]); + return; } else @@ -490,20 +492,22 @@ void cServer::ExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallbac return; } } - /* - * TODO: Declare unload command + if (split[0] == "unload") { if (split.size() > 1) { - + cPluginManager::Get()->RemovePlugin(cPluginManager::Get()->GetPlugin(split[1])); + return; } else { - + a_Output.Out("No plugin given! Command: unload "); + a_Output.Finished(); + return; } } - */ + // There is currently no way a plugin can do these (and probably won't ever be): if (split[0].compare("chunkstats") == 0) { -- cgit v1.2.3 From 40ce737a7e372b5f3aef51cad7362a199351cb27 Mon Sep 17 00:00:00 2001 From: Julian Laubstein Date: Sun, 18 May 2014 17:30:21 +0200 Subject: removed the include --- src/Server.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/Server.cpp b/src/Server.cpp index e71e728a6..aa731cdd2 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -29,7 +29,6 @@ #include #include #include -#include extern "C" { #include "zlib/zlib.h" -- cgit v1.2.3 From e09a04a23aaa14598bff6692393f4187f59d3824 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 18 May 2014 22:36:17 +0200 Subject: Fixed datatype truncation in Diff() template. --- src/Defines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Defines.h b/src/Defines.h index b0b209934..563fc308c 100644 --- a/src/Defines.h +++ b/src/Defines.h @@ -529,7 +529,7 @@ inline float GetSpecialSignf( float a_Val ) -template inline int Diff(T a_Val1, T a_Val2) +template inline T Diff(T a_Val1, T a_Val2) { return std::abs(a_Val1 - a_Val2); } -- cgit v1.2.3 From a651c865e40ad80b52ddf69004b40a580e7069ea Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Sun, 18 May 2014 22:49:27 +0200 Subject: There's no "round" function in MSVC2008. --- src/Entities/Entity.cpp | 2 +- src/Entities/Player.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index c393f89fd..31ad66779 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -321,7 +321,7 @@ bool cEntity::DoTakeDamage(TakeDamageInfo & a_TDI) m_World->BroadcastEntityAnimation(*this, 4); // Critical hit } - Player->GetStatManager().AddValue(statDamageDealt, round(a_TDI.FinalDamage * 10)); + Player->GetStatManager().AddValue(statDamageDealt, (StatValue)floor(a_TDI.FinalDamage * 10 + 0.5)); } m_Health -= (short)a_TDI.FinalDamage; diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 632c41936..c3b763278 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -838,7 +838,7 @@ bool cPlayer::DoTakeDamage(TakeDamageInfo & a_TDI) AddFoodExhaustion(0.3f); SendHealth(); - m_Stats.AddValue(statDamageTaken, round(a_TDI.FinalDamage * 10)); + m_Stats.AddValue(statDamageTaken, (StatValue)floor(a_TDI.FinalDamage * 10 + 0.5)); return true; } return false; @@ -1953,7 +1953,7 @@ void cPlayer::HandleFloater() void cPlayer::UpdateMovementStats(const Vector3d & a_DeltaPos) { - StatValue Value = round(a_DeltaPos.Length() * 100); + StatValue Value = (StatValue)floor(a_DeltaPos.Length() * 100 + 0.5); if (m_AttachedTo == NULL) { @@ -1970,7 +1970,7 @@ void cPlayer::UpdateMovementStats(const Vector3d & a_DeltaPos) if ((Block == E_BLOCK_LADDER) && (a_DeltaPos.y > 0.0)) // Going up { - m_Stats.AddValue(statDistClimbed, round(a_DeltaPos.y * 100)); + m_Stats.AddValue(statDistClimbed, (StatValue)floor(a_DeltaPos.y * 100 + 0.5)); } else { -- cgit v1.2.3 From 6167c79e7a822cb14431985a82f4cc5b463e556f Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 18 May 2014 22:41:42 +0100 Subject: Implemented cacti damage + Implemented cacti damage * Fixed pickup tossing (PR #994 bug) --- src/ClientHandle.cpp | 5 +++-- src/Entities/Entity.cpp | 23 +++++++++++++++++++++++ src/Entities/Entity.h | 3 +++ 3 files changed, 29 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 1009cdbd6..6d0fae3ae 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -816,9 +816,10 @@ void cClientHandle::HandleLeftClick(int a_BlockX, int a_BlockY, int a_BlockZ, eB } if ( - (Diff(m_Player->GetPosX(), (double)a_BlockX) > 6) || + ((a_Status == DIG_STATUS_STARTED) || (a_Status == DIG_STATUS_FINISHED)) && // Only do a radius check for block destruction - things like pickup tossing send coordinates that are to be ignored + ((Diff(m_Player->GetPosX(), (double)a_BlockX) > 6) || (Diff(m_Player->GetPosY(), (double)a_BlockY) > 6) || - (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6) + (Diff(m_Player->GetPosZ(), (double)a_BlockZ) > 6)) ) { m_Player->GetWorld()->SendBlockTo(a_BlockX, a_BlockY, a_BlockZ, m_Player); diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 4cf10a219..b7cb2c3d9 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -601,6 +601,10 @@ void cEntity::Tick(float a_Dt, cChunk & a_Chunk) m_TicksSinceLastVoidDamage = 0; } + if (IsMob() || IsPlayer() || IsPickup() || IsExpOrb()) + { + DetectCacti(); + } if (IsMob() || IsPlayer()) { // Set swimming state @@ -998,6 +1002,25 @@ void cEntity::TickInVoid(cChunk & a_Chunk) +void cEntity::DetectCacti() +{ + int X = POSX_TOINT, Y = POSY_TOINT, Z = POSZ_TOINT; + if ( + ((X + 1) - GetPosX() < 0.3) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS) || + ((GetPosX() - (X - 1)) - 1 < 0.3) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS) || + ((Z + 1) - GetPosZ() < 0.3) && (GetWorld()->GetBlock(X, Y, Z + 1) == E_BLOCK_CACTUS) || + ((GetPosZ() - (Z - 1)) - 1 < 0.3) && (GetWorld()->GetBlock(X, Y, Z - 1) == E_BLOCK_CACTUS) || + (((Y > 0) && (Y < cChunkDef::Height)) && ((GetPosY() - Y < 1) && (GetWorld()->GetBlock(X, Y, Z) == E_BLOCK_CACTUS))) + ) + { + TakeDamage(dtCactus, NULL, 1, 0); + } +} + + + + + void cEntity::SetSwimState(cChunk & a_Chunk) { int RelY = (int)floor(GetPosY() + 0.1); diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h index df03d635b..d5e7cf5c1 100644 --- a/src/Entities/Entity.h +++ b/src/Entities/Entity.h @@ -317,6 +317,9 @@ public: /// Updates the state related to this entity being on fire virtual void TickBurning(cChunk & a_Chunk); + + /** Detects the time for application of cacti damage */ + virtual void DetectCacti(void); /// Handles when the entity is in the void virtual void TickInVoid(cChunk & a_Chunk); -- cgit v1.2.3 From 70cf4a5eafaf91f9b6d0b71f607270b7df86e6d9 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Mon, 19 May 2014 07:31:53 +0100 Subject: Fixed clanging errors. Please @tigerw make sure this is correct. --- src/Entities/Entity.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index b7cb2c3d9..1a91639ac 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -1006,14 +1006,14 @@ void cEntity::DetectCacti() { int X = POSX_TOINT, Y = POSY_TOINT, Z = POSZ_TOINT; if ( - ((X + 1) - GetPosX() < 0.3) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS) || - ((GetPosX() - (X - 1)) - 1 < 0.3) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS) || - ((Z + 1) - GetPosZ() < 0.3) && (GetWorld()->GetBlock(X, Y, Z + 1) == E_BLOCK_CACTUS) || - ((GetPosZ() - (Z - 1)) - 1 < 0.3) && (GetWorld()->GetBlock(X, Y, Z - 1) == E_BLOCK_CACTUS) || + (((X + 1) - GetPosX() < 0.3) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS)) || + (((GetPosX() - (X - 1)) - 1 < 0.3) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS)) || + (((Z + 1) - GetPosZ() < 0.3) && (GetWorld()->GetBlock(X, Y, Z + 1) == E_BLOCK_CACTUS)) || + (((GetPosZ() - (Z - 1)) - 1 < 0.3) && (GetWorld()->GetBlock(X, Y, Z - 1) == E_BLOCK_CACTUS)) || (((Y > 0) && (Y < cChunkDef::Height)) && ((GetPosY() - Y < 1) && (GetWorld()->GetBlock(X, Y, Z) == E_BLOCK_CACTUS))) ) { - TakeDamage(dtCactus, NULL, 1, 0); + TakeDamage(dtCactusContact, NULL, 1, 0); } } -- cgit v1.2.3 From dc39d88d3f5951479084c1ea4d51079015777f01 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Mon, 19 May 2014 10:35:21 +0100 Subject: Should have fixed assumptions about entity width. @madmaxoft can you comment? --- src/Entities/Entity.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 1a91639ac..71b9f63a7 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -1005,11 +1005,12 @@ void cEntity::TickInVoid(cChunk & a_Chunk) void cEntity::DetectCacti() { int X = POSX_TOINT, Y = POSY_TOINT, Z = POSZ_TOINT; + float w = m_Width / 2 if ( - (((X + 1) - GetPosX() < 0.3) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS)) || - (((GetPosX() - (X - 1)) - 1 < 0.3) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS)) || - (((Z + 1) - GetPosZ() < 0.3) && (GetWorld()->GetBlock(X, Y, Z + 1) == E_BLOCK_CACTUS)) || - (((GetPosZ() - (Z - 1)) - 1 < 0.3) && (GetWorld()->GetBlock(X, Y, Z - 1) == E_BLOCK_CACTUS)) || + (((X + 1) - GetPosX() < w) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS)) || + (((GetPosX() - (X - 1)) - 1 < w) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS)) || + (((Z + 1) - GetPosZ() < w) && (GetWorld()->GetBlock(X, Y, Z + 1) == E_BLOCK_CACTUS)) || + (((GetPosZ() - (Z - 1)) - 1 < w) && (GetWorld()->GetBlock(X, Y, Z - 1) == E_BLOCK_CACTUS)) || (((Y > 0) && (Y < cChunkDef::Height)) && ((GetPosY() - Y < 1) && (GetWorld()->GetBlock(X, Y, Z) == E_BLOCK_CACTUS))) ) { -- cgit v1.2.3 From 6de0257bbe1f0c676e9cc1401298f08b96b54e73 Mon Sep 17 00:00:00 2001 From: Alexander Harkness Date: Mon, 19 May 2014 13:31:19 +0100 Subject: Derp --- src/Entities/Entity.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 71b9f63a7..aa671bf35 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -1005,7 +1005,7 @@ void cEntity::TickInVoid(cChunk & a_Chunk) void cEntity::DetectCacti() { int X = POSX_TOINT, Y = POSY_TOINT, Z = POSZ_TOINT; - float w = m_Width / 2 + float w = m_Width / 2; if ( (((X + 1) - GetPosX() < w) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS)) || (((GetPosX() - (X - 1)) - 1 < w) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS)) || -- cgit v1.2.3 From 07baf9bdd3b04e3aec6e77f367eb38c0547f54ca Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 20 May 2014 15:52:59 +0300 Subject: Cleaned up cPlayer::UpdateMovementStats; Wither achievements --- src/Blocks/BlockMobHead.h | 27 ++++++++++++++++++ src/Entities/Player.cpp | 71 +++++++++++++++++++++++++++++++++++++--------- src/Entities/Player.h | 3 ++ src/Items/ItemFishingRod.h | 6 ++++ src/Mobs/Wither.cpp | 33 +++++++++++++++++++++ src/Mobs/Wither.h | 1 + src/Statistics.cpp | 2 +- 7 files changed, 128 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/Blocks/BlockMobHead.h b/src/Blocks/BlockMobHead.h index acd1c88fb..552c0491c 100644 --- a/src/Blocks/BlockMobHead.h +++ b/src/Blocks/BlockMobHead.h @@ -46,8 +46,29 @@ public: bool IsWither(void) const { return m_IsWither; } void Reset(void) { m_IsWither = false; } + } CallbackA, CallbackB; + class cPlayerCallback : public cPlayerListCallback + { + Vector3f m_Pos; + + virtual bool Item(cPlayer * a_Player) + { + double Dist = (a_Player->GetPosition() - m_Pos).Length(); + if (Dist < 50.0) + { + // If player is close, award achievement + a_Player->AwardAchievement(achSpawnWither); + } + return false; + } + + public: + cPlayerCallback(const Vector3f & a_Pos) : m_Pos(a_Pos) {} + + } PlayerCallback(Vector3f(a_BlockX, a_BlockY, a_BlockZ)); + a_World->DoWithMobHeadAt(a_BlockX, a_BlockY, a_BlockZ, CallbackA); if (!CallbackA.IsWither()) @@ -86,6 +107,9 @@ public: // Spawn the wither: a_World->SpawnMob(a_BlockX + 0.5, a_BlockY - 2, a_BlockZ + 0.5, cMonster::mtWither); + // Award Achievement + a_World->ForEachPlayer(PlayerCallback); + return true; } @@ -113,6 +137,9 @@ public: // Spawn the wither: a_World->SpawnMob(a_BlockX + 0.5, a_BlockY - 2, a_BlockZ + 0.5, cMonster::mtWither); + // Award Achievement + a_World->ForEachPlayer(PlayerCallback); + return true; } diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index c3b763278..0eacb67f9 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -456,8 +456,18 @@ void cPlayer::SetTouchGround(bool a_bTouchGround) else { float Dist = (float)(m_LastGroundHeight - floor(GetPosY())); + + if (Dist >= 2.0) // At least two blocks - TODO: Use m_LastJumpHeight instead of m_LastGroundHeight above + { + // Increment statistic + m_Stats.AddValue(statDistFallen, (StatValue)floor(Dist * 100 + 0.5)); + } + int Damage = (int)(Dist - 3.f); - if (m_LastJumpHeight > m_LastGroundHeight) Damage++; + if (m_LastJumpHeight > m_LastGroundHeight) + { + Damage++; + } m_LastJumpHeight = (float)GetPosY(); if (Damage > 0) @@ -1951,32 +1961,65 @@ void cPlayer::HandleFloater() +bool cPlayer::IsClimbing(void) const +{ + int PosX = POSX_TOINT; + int PosY = POSY_TOINT; + int PosZ = POSZ_TOINT; + + if ((PosY < 0) || (PosY >= cChunkDef::Height)) + { + return false; + } + + BLOCKTYPE Block = m_World->GetBlock(PosX, PosY, PosZ); + switch (Block) + { + case E_BLOCK_LADDER: + case E_BLOCK_VINES: + { + return true; + } + default: return false; + } +} + + + + + void cPlayer::UpdateMovementStats(const Vector3d & a_DeltaPos) { StatValue Value = (StatValue)floor(a_DeltaPos.Length() * 100 + 0.5); if (m_AttachedTo == NULL) { - int PosX = POSX_TOINT; - int PosY = POSY_TOINT; - int PosZ = POSZ_TOINT; - - BLOCKTYPE Block; - NIBBLETYPE Meta; - if (!m_World->GetBlockTypeMeta(PosX, PosY, PosZ, Block, Meta)) + if (IsClimbing()) { - return; + if (a_DeltaPos.y > 0.0) // Going up + { + m_Stats.AddValue(statDistClimbed, (StatValue)floor(a_DeltaPos.y * 100 + 0.5)); + } } - - if ((Block == E_BLOCK_LADDER) && (a_DeltaPos.y > 0.0)) // Going up + else if (IsSubmerged()) { - m_Stats.AddValue(statDistClimbed, (StatValue)floor(a_DeltaPos.y * 100 + 0.5)); + m_Stats.AddValue(statDistDove, Value); } - else + else if (IsSwimming()) + { + m_Stats.AddValue(statDistSwum, Value); + } + else if (IsOnGround()) { - // TODO 2014-05-12 xdot: Other types m_Stats.AddValue(statDistWalked, Value); } + else + { + if (Value >= 25) // Ignore small/slow movement + { + m_Stats.AddValue(statDistFlown, Value); + } + } } else { diff --git a/src/Entities/Player.h b/src/Entities/Player.h index 78b534d83..b7cb27d6c 100644 --- a/src/Entities/Player.h +++ b/src/Entities/Player.h @@ -127,6 +127,9 @@ public: inline const cItem & GetEquippedItem(void) const { return GetInventory().GetEquippedItem(); } // tolua_export + /** Returns whether the player is climbing (ladders, vines e.t.c). */ + bool IsClimbing(void) const; + virtual void TeleportToCoords(double a_PosX, double a_PosY, double a_PosZ) override; // tolua_begin diff --git a/src/Items/ItemFishingRod.h b/src/Items/ItemFishingRod.h index 0431b88b7..32c151db5 100644 --- a/src/Items/ItemFishingRod.h +++ b/src/Items/ItemFishingRod.h @@ -142,6 +142,8 @@ public: break; } } + + a_Player->GetStatManager().AddValue(statTreasureFished, 1); } else if (ItemCategory <= 14) // Junk 10% { @@ -190,6 +192,8 @@ public: { Drops.Add(cItem(E_BLOCK_TRIPWIRE_HOOK)); } + + a_Player->GetStatManager().AddValue(statJunkFished, 1); } else // Fish { @@ -210,6 +214,8 @@ public: { Drops.Add(cItem(E_ITEM_RAW_FISH, 1, E_META_RAW_FISH_FISH)); } + + a_Player->GetStatManager().AddValue(statFishCaught, 1); } if (cRoot::Get()->GetPluginManager()->CallHookPlayerFishing(*a_Player, Drops)) diff --git a/src/Mobs/Wither.cpp b/src/Mobs/Wither.cpp index 5b6e895e1..deb2cf34e 100644 --- a/src/Mobs/Wither.cpp +++ b/src/Mobs/Wither.cpp @@ -2,7 +2,9 @@ #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules #include "Wither.h" + #include "../World.h" +#include "../Entities/Player.h" @@ -100,3 +102,34 @@ void cWither::GetDrops(cItems & a_Drops, cEntity * a_Killer) + +void cWither::KilledBy(cEntity * a_Killer) +{ + UNUSED(a_Killer); + + class cPlayerCallback : public cPlayerListCallback + { + Vector3f m_Pos; + + virtual bool Item(cPlayer * a_Player) + { + double Dist = (a_Player->GetPosition() - m_Pos).Length(); + if (Dist < 50.0) + { + // If player is close, award achievement + a_Player->AwardAchievement(achKillWither); + } + return false; + } + + public: + cPlayerCallback(const Vector3f & a_Pos) : m_Pos(a_Pos) {} + + } PlayerCallback(GetPosition()); + + m_World->ForEachPlayer(PlayerCallback); +} + + + + diff --git a/src/Mobs/Wither.h b/src/Mobs/Wither.h index 08b460009..93b4f8bfc 100644 --- a/src/Mobs/Wither.h +++ b/src/Mobs/Wither.h @@ -29,6 +29,7 @@ public: virtual void GetDrops(cItems & a_Drops, cEntity * a_Killer = NULL) override; virtual bool DoTakeDamage(TakeDamageInfo & a_TDI) override; virtual void Tick(float a_Dt, cChunk & a_Chunk) override; + virtual void KilledBy(cEntity * a_Killer) override; private: diff --git a/src/Statistics.cpp b/src/Statistics.cpp index 5950eb96c..5c0524f9e 100644 --- a/src/Statistics.cpp +++ b/src/Statistics.cpp @@ -52,7 +52,7 @@ cStatInfo cStatInfo::ms_Info[statCount] = { /* Type | Name */ cStatInfo(statGamesQuit, "stat.leaveGame"), cStatInfo(statMinutesPlayed, "stat.playOneMinute"), - cStatInfo(statDistWalked, "stat.walkOnCm"), + cStatInfo(statDistWalked, "stat.walkOneCm"), cStatInfo(statDistSwum, "stat.swimOneCm"), cStatInfo(statDistFallen, "stat.fallOneCm"), cStatInfo(statDistClimbed, "stat.climbOneCm"), -- cgit v1.2.3 From 7aeb8ce9936e8ebd203d67100de8635d391bd8a1 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 21 May 2014 10:59:14 +0300 Subject: Fixed cWither::KilledBy --- src/Blocks/BlockMobHead.h | 1 + src/Mobs/Wither.cpp | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Blocks/BlockMobHead.h b/src/Blocks/BlockMobHead.h index 552c0491c..b7629b07c 100644 --- a/src/Blocks/BlockMobHead.h +++ b/src/Blocks/BlockMobHead.h @@ -55,6 +55,7 @@ public: virtual bool Item(cPlayer * a_Player) { + // TODO 2014-05-21 xdot: Vanilla minecraft uses an AABB check instead of a radius one double Dist = (a_Player->GetPosition() - m_Pos).Length(); if (Dist < 50.0) { diff --git a/src/Mobs/Wither.cpp b/src/Mobs/Wither.cpp index deb2cf34e..170f4fdc0 100644 --- a/src/Mobs/Wither.cpp +++ b/src/Mobs/Wither.cpp @@ -105,7 +105,7 @@ void cWither::GetDrops(cItems & a_Drops, cEntity * a_Killer) void cWither::KilledBy(cEntity * a_Killer) { - UNUSED(a_Killer); + super::KilledBy(a_Killer); class cPlayerCallback : public cPlayerListCallback { @@ -113,6 +113,7 @@ void cWither::KilledBy(cEntity * a_Killer) virtual bool Item(cPlayer * a_Player) { + // TODO 2014-05-21 xdot: Vanilla minecraft uses an AABB check instead of a radius one double Dist = (a_Player->GetPosition() - m_Pos).Length(); if (Dist < 50.0) { -- cgit v1.2.3 From 941cb88ae456f0d8b91f6b5dc835c2536eef15f6 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 23 May 2014 12:33:30 +0200 Subject: Fixed datatype conversion warning. --- src/Entities/Entity.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp index 15f456332..2567b7adc 100644 --- a/src/Entities/Entity.cpp +++ b/src/Entities/Entity.cpp @@ -1018,10 +1018,10 @@ void cEntity::TickInVoid(cChunk & a_Chunk) -void cEntity::DetectCacti() +void cEntity::DetectCacti(void) { int X = POSX_TOINT, Y = POSY_TOINT, Z = POSZ_TOINT; - float w = m_Width / 2; + double w = m_Width / 2; if ( (((X + 1) - GetPosX() < w) && (GetWorld()->GetBlock(X + 1, Y, Z) == E_BLOCK_CACTUS)) || (((GetPosX() - (X - 1)) - 1 < w) && (GetWorld()->GetBlock(X - 1, Y, Z) == E_BLOCK_CACTUS)) || -- cgit v1.2.3 From a4c964c888748e7099308dc56578244112148cdd Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 23 May 2014 13:01:06 +0200 Subject: Auto-enlargement for cGridStructGen cache. --- src/Generating/GridStructGen.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src') diff --git a/src/Generating/GridStructGen.cpp b/src/Generating/GridStructGen.cpp index 3bbc89054..bfa6bccb1 100644 --- a/src/Generating/GridStructGen.cpp +++ b/src/Generating/GridStructGen.cpp @@ -22,6 +22,15 @@ cGridStructGen::cGridStructGen( m_MaxStructureSizeZ(a_MaxStructureSizeZ), m_MaxCacheSize(a_MaxCacheSize) { + size_t NumStructuresPerQuery = (size_t)((m_MaxStructureSizeX / m_GridSizeX + 1) * (m_MaxStructureSizeZ / m_GridSizeZ + 1)); + if (NumStructuresPerQuery > m_MaxCacheSize) + { + m_MaxCacheSize = NumStructuresPerQuery * 4; + LOGINFO( + "cGridStructGen: The cache size is too small (%u), increasing the cache size to %u to avoid inefficiency.", + (unsigned)a_MaxCacheSize, (unsigned)m_MaxCacheSize + ); + } } -- cgit v1.2.3 From 97865bff7ce1595ae43be65082c1a02fed4ea8c4 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Fri, 23 May 2014 13:01:50 +0200 Subject: Fixed Mineshaft system size. --- src/Generating/MineShafts.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Generating/MineShafts.cpp b/src/Generating/MineShafts.cpp index 391e4c04f..81ae6481d 100644 --- a/src/Generating/MineShafts.cpp +++ b/src/Generating/MineShafts.cpp @@ -1283,7 +1283,7 @@ cStructGenMineShafts::cStructGenMineShafts( int a_Seed, int a_GridSize, int a_MaxSystemSize, int a_ChanceCorridor, int a_ChanceCrossing, int a_ChanceStaircase ) : - super(a_Seed, a_GridSize, a_GridSize, 120 + a_MaxSystemSize * 10, 120 + a_MaxSystemSize * 10, 100), + super(a_Seed, a_GridSize, a_GridSize, a_MaxSystemSize, a_MaxSystemSize, 100), m_Noise(a_Seed), m_GridSize(a_GridSize), m_MaxSystemSize(a_MaxSystemSize), -- cgit v1.2.3 From 8f964886e0ccbf51dac07227f0ac4c739b47d3a5 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 24 May 2014 13:33:40 +0100 Subject: Implemented style changes --- src/BlockArea.cpp | 2 +- src/Chunk.cpp | 4 +- src/Chunk.h | 4 +- src/ChunkData.cpp | 61 +++++++++++++-------------- src/ChunkData.h | 108 ++++++++++++++++++++++++++---------------------- src/ChunkDataCallback.h | 8 ++-- 6 files changed, 95 insertions(+), 92 deletions(-) (limited to 'src') diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index 87236957a..11bd76e6c 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -1908,7 +1908,7 @@ void cBlockArea::cChunkReader::ChunkData(const cChunkData & a_BlockBuffer) if (m_Area.m_BlockLight != NULL) { - a_BlockBuffer.CopyLight(m_Area.m_BlockLight); + a_BlockBuffer.CopyBlockLight(m_Area.m_BlockLight); } if (m_Area.m_BlockSkyLight != NULL) diff --git a/src/Chunk.cpp b/src/Chunk.cpp index d85b44607..e5f8f1e29 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -284,7 +284,7 @@ void cChunk::SetAllData( m_ChunkData.SetBlocks(a_BlockTypes); m_ChunkData.SetMeta(a_BlockMeta); - m_ChunkData.SetLight(a_BlockLight); + m_ChunkData.SetBlockLight(a_BlockLight); m_ChunkData.SetSkyLight(a_BlockSkyLight); m_IsLightValid = (a_BlockLight != NULL) && (a_BlockSkyLight != NULL); @@ -326,7 +326,7 @@ void cChunk::SetLight( // TODO: We might get cases of wrong lighting when a chunk changes in the middle of a lighting calculation. // Postponing until we see how bad it is :) - m_ChunkData.SetLight(a_BlockLight); + m_ChunkData.SetBlockLight(a_BlockLight); m_ChunkData.SetSkyLight(a_SkyLight); diff --git a/src/Chunk.h b/src/Chunk.h index 2de45919e..d95537acf 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -67,7 +67,7 @@ public: cChunkMap * a_ChunkMap, cWorld * a_World, // Parent objects cChunk * a_NeighborXM, cChunk * a_NeighborXP, cChunk * a_NeighborZM, cChunk * a_NeighborZP // Neighbor chunks ); - cChunk(cChunk& other); + cChunk(cChunk & other); ~cChunk(); bool IsValid(void) const {return m_IsValid; } // Returns true if the chunk block data is valid (loaded / generated) @@ -156,7 +156,7 @@ public: void FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta ); // Doesn't force block updates on neighbors, use for simple changes such as grass growing etc. BLOCKTYPE GetBlock(int a_RelX, int a_RelY, int a_RelZ) const; - BLOCKTYPE GetBlock(Vector3i a_cords) const { return GetBlock(a_cords.x,a_cords.y,a_cords.z);} + BLOCKTYPE GetBlock(Vector3i a_cords) const { return GetBlock(a_cords.x, a_cords.y, a_cords.z);} void GetBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta); void GetBlockInfo (int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_Meta, NIBBLETYPE & a_SkyLight, NIBBLETYPE & a_BlockLight); diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 86b0c431c..098f436f8 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -5,7 +5,7 @@ cChunkData cChunkData::Copy() const { cChunkData copy; - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { if (m_Sections[i] != NULL) { @@ -22,13 +22,16 @@ cChunkData cChunkData::Copy() const void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; - if (a_Idx > 0) a_Idx = a_Idx > length ? a_Idx - length : 0; + if (a_Idx > 0) + { + a_Idx = std::max(a_Idx - length, (size_t) 0); + } if (a_Idx == 0) { - size_t tocopy = length > segment_length ? segment_length : length; + size_t tocopy = std::min(segment_length, length); length -= tocopy; if (m_Sections[i] != NULL) { @@ -56,7 +59,7 @@ void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -82,9 +85,9 @@ void cChunkData::CopyMeta(NIBBLETYPE * a_dest) const -void cChunkData::CopyLight(NIBBLETYPE * a_dest) const +void cChunkData::CopyBlockLight(NIBBLETYPE * a_dest) const { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -112,7 +115,7 @@ void cChunkData::CopyLight(NIBBLETYPE * a_dest) const void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -140,7 +143,7 @@ void cChunkData::CopySkyLight(NIBBLETYPE * a_dest) const void cChunkData::SetBlocks(const BLOCKTYPE * a_src) { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; if (m_Sections[i] != NULL) @@ -153,6 +156,9 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) } else { + // j counts how many of leading zeros the buffer has + // if j == segment_length then the buffer is all zeros so there is no point + // creating the buffer. size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); @@ -180,11 +186,6 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) sizeof(m_Sections[i]->m_BlockSkyLight) ); } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } } } } @@ -194,7 +195,7 @@ void cChunkData::SetBlocks(const BLOCKTYPE * a_src) void cChunkData::SetMeta(const NIBBLETYPE * a_src) { - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -207,6 +208,9 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) } else { + // j counts how many of leading zeros the buffer has + // if j == segment_length then the buffer is all zeros so there is no point + // creating the buffer. size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); @@ -234,11 +238,6 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) sizeof(m_Sections[i]->m_BlockSkyLight) ); } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } } } } @@ -246,10 +245,10 @@ void cChunkData::SetMeta(const NIBBLETYPE * a_src) -void cChunkData::SetLight(const NIBBLETYPE * a_src) +void cChunkData::SetBlockLight(const NIBBLETYPE * a_src) { if (!a_src) return; - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -262,6 +261,9 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) } else { + // j counts how many of leading zeros the buffer has + // if j == segment_length then the buffer is all zeros so there is no point + // creating the buffer. size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0; j++); @@ -289,11 +291,6 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) sizeof(m_Sections[i]->m_BlockSkyLight) ); } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } } } } @@ -304,7 +301,7 @@ void cChunkData::SetLight(const NIBBLETYPE * a_src) void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) { if (!a_src) return; - for (size_t i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2; if (m_Sections[i] != NULL) @@ -317,6 +314,9 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) } else { + // j counts how many of leading zeros the buffer has + // if j == segment_length then the buffer is all zeros so there is no point + // creating the buffer. size_t j = 0; // do nothing whilst 0 for (; j < segment_length && a_src[i * segment_length + j] == 0xFF; j++); @@ -344,11 +344,6 @@ void cChunkData::SetSkyLight (const NIBBLETYPE * a_src) sizeof(m_Sections[i]->m_BlockLight) ); } - else - { - Free(m_Sections[i]); - m_Sections[i] = 0; - } } } } diff --git a/src/ChunkData.h b/src/ChunkData.h index 9c852ee24..5a149f95f 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -8,8 +8,7 @@ #include "ChunkDef.h" -#define CHUNK_SECTION_HEIGHT 16 -#define CHUNK_SECTION_NUM (256 / CHUNK_SECTION_HEIGHT) + #if __cplusplus < 201103L // auto_ptr style interface for memory management @@ -23,8 +22,8 @@ public: cChunkData() #if __cplusplus < 201103L - // auto_ptr style interface for memory management - : IsOwner(true) + // auto_ptr style interface for memory management + : IsOwner(true) #endif { memset(m_Sections, 0, sizeof(m_Sections)); @@ -32,72 +31,75 @@ public: ~cChunkData() { #if __cplusplus < 201103L - // auto_ptr style interface for memory management - if (!IsOwner) return; + // auto_ptr style interface for memory management + if (!IsOwner) + { + return; + } #endif - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) { if (m_Sections[i] == NULL) Free(m_Sections[i]);; } } #if __cplusplus < 201103L - // auto_ptr style interface for memory management - cChunkData(const cChunkData& other) : - IsOwner(true) - { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + // auto_ptr style interface for memory management + cChunkData(const cChunkData& other) : + IsOwner(true) { - m_Sections[i] = other.m_Sections[i]; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; } - other.IsOwner = false; - } - cChunkData& operator=(const cChunkData& other) - { - if (&other != this) + cChunkData& operator=(const cChunkData& other) { - if (IsOwner) + if (&other != this) { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + if (IsOwner) { - if (m_Sections[i]) Free(m_Sections[i]);; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + if (m_Sections[i]) Free(m_Sections[i]);; + } } + IsOwner = true; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; } - IsOwner = true; - for (int i = 0; i < CHUNK_SECTION_NUM; i++) - { - m_Sections[i] = other.m_Sections[i]; - } - other.IsOwner = false; - } - return *this; + return *this; - } + } #else - // unique_ptr style interface for memory management - cChunkData(cChunkData&& other) - { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + // unique_ptr style interface for memory management + cChunkData(cChunkData&& other) { - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = 0; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = NULL; + } } - } - cChunkData& operator=(cChunkData&& other) - { - if (&other != this) + cChunkData& operator=(cChunkData&& other) { - for (int i = 0; i < CHUNK_SECTION_NUM; i++) + if (&other != this) { - Free(m_Sections[i]);; - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = 0; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + Free(m_Sections[i]);; + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = NULL; + } } + return *this; } - return *this; - } #endif BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const @@ -150,7 +152,10 @@ public: NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const { - if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) + if ( + (a_RelX < cChunkDef::Width) && (a_RelX > -1) && + (a_RelY < cChunkDef::Height) && (a_RelY > -1) && + (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) { int Section = a_RelY / CHUNK_SECTION_HEIGHT; if (m_Sections[Section] != NULL) @@ -244,16 +249,19 @@ public: cChunkData Copy() const; void CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx = 0, size_t length = cChunkDef::NumBlocks) const; void CopyMeta (NIBBLETYPE * a_dest) const; - void CopyLight (NIBBLETYPE * a_dest) const; + void CopyBlockLight (NIBBLETYPE * a_dest) const; void CopySkyLight (NIBBLETYPE * a_dest) const; void SetBlocks (const BLOCKTYPE * a_src); void SetMeta (const NIBBLETYPE * a_src); - void SetLight (const NIBBLETYPE * a_src); + void SetBlockLight (const NIBBLETYPE * a_src); void SetSkyLight (const NIBBLETYPE * a_src); private: + static const size_t CHUNK_SECTION_HEIGHT = 16; + static const size_t CHUNK_SECTION_COUNT = (256 / CHUNK_SECTION_HEIGHT); + #if __cplusplus < 201103L // auto_ptr style interface for memory management mutable bool IsOwner; @@ -266,7 +274,7 @@ private: NIBBLETYPE m_BlockSkyLight[CHUNK_SECTION_HEIGHT * 16 * 16 / 2]; }; - sChunkSection *m_Sections[CHUNK_SECTION_NUM]; + sChunkSection *m_Sections[CHUNK_SECTION_COUNT]; sChunkSection * Allocate() const; void Free(sChunkSection * ptr) const; diff --git a/src/ChunkDataCallback.h b/src/ChunkDataCallback.h index 340582885..e916d6486 100644 --- a/src/ChunkDataCallback.h +++ b/src/ChunkDataCallback.h @@ -62,7 +62,7 @@ protected: /** A simple implementation of the cChunkDataCallback interface that collects all block data into a single buffer */ class cChunkDataArrayCollector : -public cChunkDataCallback + public cChunkDataCallback { public: @@ -75,7 +75,7 @@ protected: { a_ChunkBuffer.CopyBlocks(m_BlockData); a_ChunkBuffer.CopyMeta(m_BlockData + cChunkDef::NumBlocks); - a_ChunkBuffer.CopyLight(m_BlockData + 3 * cChunkDef::NumBlocks / 2); + a_ChunkBuffer.CopyBlockLight(m_BlockData + 3 * cChunkDef::NumBlocks / 2); a_ChunkBuffer.CopySkyLight(m_BlockData + 2 * cChunkDef::NumBlocks); } }; @@ -83,7 +83,7 @@ protected: /** A simple implementation of the cChunkDataCallback interface that collects all block data into a separate buffers */ class cChunkDataSeparateCollector : -public cChunkDataCallback + public cChunkDataCallback { public: @@ -98,7 +98,7 @@ protected: { a_ChunkBuffer.CopyBlocks(m_BlockTypes); a_ChunkBuffer.CopyMeta(m_BlockMetas); - a_ChunkBuffer.CopyLight(m_BlockLight); + a_ChunkBuffer.CopyBlockLight(m_BlockLight); a_ChunkBuffer.CopySkyLight(m_BlockSkyLight); } } ; -- cgit v1.2.3 From 7fac63cffbe226ab025435a1e01e5f2430e52806 Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 24 May 2014 13:37:25 +0100 Subject: Moved accessors to cpp file --- src/ChunkData.cpp | 230 +++++++++++++++++++++++++++++++++++++++++++++++++++++- src/ChunkData.h | 227 +++-------------------------------------------------- 2 files changed, 241 insertions(+), 216 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 098f436f8..162803292 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -2,7 +2,235 @@ #include "Globals.h" #include "ChunkData.h" -cChunkData cChunkData::Copy() const +cChunkData::cChunkData() +#if __cplusplus < 201103L + // auto_ptr style interface for memory management + : IsOwner(true) +#endif +{ + memset(m_Sections, 0, sizeof(m_Sections)); +} + + +cChunkData::~cChunkData() +{ + #if __cplusplus < 201103L + // auto_ptr style interface for memory management + if (!IsOwner) + { + return; + } + #endif + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + if (m_Sections[i] == NULL) Free(m_Sections[i]);; + } +} + +#if __cplusplus < 201103L + // auto_ptr style interface for memory management + cChunkData::cChunkData(const cChunkData& other) : + IsOwner(true) + { + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; + } + + cChunkData::cChunkData& operator=(const cChunkData& other) + { + if (&other != this) + { + if (IsOwner) + { + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + if (m_Sections[i]) Free(m_Sections[i]);; + } + } + IsOwner = true; + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + } + other.IsOwner = false; + } + return *this; + + } +#else + // unique_ptr style interface for memory management + cChunkData::cChunkData(cChunkData&& other) + { + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = NULL; + } + } + + cChunkData::cChunkData& operator=(cChunkData&& other) + { + if (&other != this) + { + for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) + { + Free(m_Sections[i]);; + m_Sections[i] = other.m_Sections[i]; + other.m_Sections[i] = NULL; + } + } + return *this; + } +#endif + +BLOCKTYPE cChunkData::GetBlock(int a_X, int a_Y, int a_Z) const +{ + ASSERT((a_X >= 0) && (a_X < cChunkDef::Width)); + ASSERT((a_Y >= 0) && (a_Y < cChunkDef::Height)); + ASSERT((a_Z >= 0) && (a_Z < cChunkDef::Width)); + int Section = a_Y / CHUNK_SECTION_HEIGHT; + if (m_Sections[Section] != NULL) + { + int Index = cChunkDef::MakeIndexNoCheck(a_X, a_Y - (Section * CHUNK_SECTION_HEIGHT), a_Z); + return m_Sections[Section]->m_BlockTypes[Index]; + } + else + { + return 0; + } +} + +void cChunkData::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block) +{ + if ( + (a_RelX >= cChunkDef::Width) || (a_RelX < 0) || + (a_RelY >= cChunkDef::Height) || (a_RelY < 0) || + (a_RelZ >= cChunkDef::Width) || (a_RelZ < 0) + ) + { + ASSERT(!"cChunkData::SetMeta(): index out of range!"); + return; + } + + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if (m_Sections[Section] == NULL) + { + if (a_Block == 0x00) + { + return; + } + m_Sections[Section] = Allocate(); + if (m_Sections[Section] == NULL) + { + ASSERT(!"Failed to allocate a new section in Chunkbuffer"); + return; + } + ZeroSection(m_Sections[Section]); + } + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + m_Sections[Section]->m_BlockTypes[Index] = a_Block; +} + +NIBBLETYPE cChunkData::GetMeta(int a_RelX, int a_RelY, int a_RelZ) const +{ + if ( + (a_RelX < cChunkDef::Width) && (a_RelX > -1) && + (a_RelY < cChunkDef::Height) && (a_RelY > -1) && + (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) + { + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if (m_Sections[Section] != NULL) + { + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + return (m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4)) & 0x0f; + } + else + { + return 0; + } + } + ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); + return 0; +} + +bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble) +{ + if ( + (a_RelX >= cChunkDef::Width) || (a_RelX < 0) || + (a_RelY >= cChunkDef::Height) || (a_RelY < 0) || + (a_RelZ >= cChunkDef::Width) || (a_RelZ < 0) + ) + { + ASSERT(!"cChunkData::SetMeta(): index out of range!"); + return false; + } + + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if (m_Sections[Section] == NULL) + { + if ((a_Nibble & 0xf) == 0x00) + { + return false; + } + m_Sections[Section] = Allocate(); + if (m_Sections[Section] == NULL) + { + ASSERT(!"Failed to allocate a new section in Chunkbuffer"); + return false; + } + ZeroSection(m_Sections[Section]); + } + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + NIBBLETYPE oldval = m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4) & 0xf; + m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( + (m_Sections[Section]->m_BlockMeta[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble + ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set + ); + return oldval == a_Nibble; +} + +NIBBLETYPE cChunkData::GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const +{ + if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) + { + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if (m_Sections[Section] != NULL) + { + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f; + } + else + { + return 0; + } + } + ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); + return 0; +} + +NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const +{ + if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) + { + int Section = a_RelY / CHUNK_SECTION_HEIGHT; + if (m_Sections[Section] != NULL) + { + int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); + return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f; + } + else + { + return 0xF; + } + } + ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); + return 0; +} + +cChunkData cChunkData::cChunkData::Copy() const { cChunkData copy; for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) diff --git a/src/ChunkData.h b/src/ChunkData.h index 5a149f95f..16fcc4d37 100644 --- a/src/ChunkData.h +++ b/src/ChunkData.h @@ -20,231 +20,28 @@ class cChunkData { public: - cChunkData() - #if __cplusplus < 201103L - // auto_ptr style interface for memory management - : IsOwner(true) - #endif - { - memset(m_Sections, 0, sizeof(m_Sections)); - } - ~cChunkData() - { - #if __cplusplus < 201103L - // auto_ptr style interface for memory management - if (!IsOwner) - { - return; - } - #endif - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - if (m_Sections[i] == NULL) Free(m_Sections[i]);; - } - } + cChunkData(); + ~cChunkData(); #if __cplusplus < 201103L // auto_ptr style interface for memory management - cChunkData(const cChunkData& other) : - IsOwner(true) - { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - m_Sections[i] = other.m_Sections[i]; - } - other.IsOwner = false; - } - - cChunkData& operator=(const cChunkData& other) - { - if (&other != this) - { - if (IsOwner) - { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - if (m_Sections[i]) Free(m_Sections[i]);; - } - } - IsOwner = true; - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - m_Sections[i] = other.m_Sections[i]; - } - other.IsOwner = false; - } - return *this; - - } + cChunkData(const cChunkData& other); + cChunkData& operator=(const cChunkData& other); #else // unique_ptr style interface for memory management - cChunkData(cChunkData&& other) - { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = NULL; - } - } - - cChunkData& operator=(cChunkData&& other) - { - if (&other != this) - { - for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) - { - Free(m_Sections[i]);; - m_Sections[i] = other.m_Sections[i]; - other.m_Sections[i] = NULL; - } - } - return *this; - } + cChunkData(cChunkData&& other); + cChunkData& operator=(cChunkData&& other); #endif - BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const - { - ASSERT((a_X >= 0) && (a_X < cChunkDef::Width)); - ASSERT((a_Y >= 0) && (a_Y < cChunkDef::Height)); - ASSERT((a_Z >= 0) && (a_Z < cChunkDef::Width)); - int Section = a_Y / CHUNK_SECTION_HEIGHT; - if (m_Sections[Section] != NULL) - { - int Index = cChunkDef::MakeIndexNoCheck(a_X, a_Y - (Section * CHUNK_SECTION_HEIGHT), a_Z); - return m_Sections[Section]->m_BlockTypes[Index]; - } - else - { - return 0; - } - } - - void SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block) - { - if ( - (a_RelX >= cChunkDef::Width) || (a_RelX < 0) || - (a_RelY >= cChunkDef::Height) || (a_RelY < 0) || - (a_RelZ >= cChunkDef::Width) || (a_RelZ < 0) - ) - { - ASSERT(!"cChunkData::SetMeta(): index out of range!"); - return; - } - - int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if (m_Sections[Section] == NULL) - { - if (a_Block == 0x00) - { - return; - } - m_Sections[Section] = Allocate(); - if (m_Sections[Section] == NULL) - { - ASSERT(!"Failed to allocate a new section in Chunkbuffer"); - return; - } - ZeroSection(m_Sections[Section]); - } - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); - m_Sections[Section]->m_BlockTypes[Index] = a_Block; - } - - NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const - { - if ( - (a_RelX < cChunkDef::Width) && (a_RelX > -1) && - (a_RelY < cChunkDef::Height) && (a_RelY > -1) && - (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) - { - int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if (m_Sections[Section] != NULL) - { - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); - return (m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4)) & 0x0f; - } - else - { - return 0; - } - } - ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); - return 0; - } - - bool SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble) - { - if ( - (a_RelX >= cChunkDef::Width) || (a_RelX < 0) || - (a_RelY >= cChunkDef::Height) || (a_RelY < 0) || - (a_RelZ >= cChunkDef::Width) || (a_RelZ < 0) - ) - { - ASSERT(!"cChunkData::SetMeta(): index out of range!"); - return false; - } + BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const; + void SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block); - int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if (m_Sections[Section] == NULL) - { - if ((a_Nibble & 0xf) == 0x00) - { - return false; - } - m_Sections[Section] = Allocate(); - if (m_Sections[Section] == NULL) - { - ASSERT(!"Failed to allocate a new section in Chunkbuffer"); - return false; - } - ZeroSection(m_Sections[Section]); - } - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); - NIBBLETYPE oldval = m_Sections[Section]->m_BlockMeta[Index / 2] >> ((Index & 1) * 4) & 0xf; - m_Sections[Section]->m_BlockMeta[Index / 2] = static_cast( - (m_Sections[Section]->m_BlockMeta[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble - ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set - ); - return oldval == a_Nibble; - } + NIBBLETYPE GetMeta(int a_RelX, int a_RelY, int a_RelZ) const; + bool SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble); - NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const - { - if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) - { - int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if (m_Sections[Section] != NULL) - { - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); - return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f; - } - else - { - return 0; - } - } - ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); - return 0; - } + NIBBLETYPE GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const; - NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const - { - if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) - { - int Section = a_RelY / CHUNK_SECTION_HEIGHT; - if (m_Sections[Section] != NULL) - { - int Index = cChunkDef::MakeIndexNoCheck(a_RelX, a_RelY - (Section * CHUNK_SECTION_HEIGHT), a_RelZ); - return (m_Sections[Section]->m_BlockLight[Index / 2] >> ((Index & 1) * 4)) & 0x0f; - } - else - { - return 0xF; - } - } - ASSERT(!"cChunkData::GetMeta(): coords out of chunk range!"); - return 0; - } + NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const; cChunkData Copy() const; void CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx = 0, size_t length = cChunkDef::NumBlocks) const; -- cgit v1.2.3 From 944d29c0ae3929471a11ea11aa98441bc31f4d7d Mon Sep 17 00:00:00 2001 From: Tycho Date: Sat, 24 May 2014 14:09:51 +0100 Subject: inject TestGlobals.h correctly --- src/ChunkData.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ChunkData.cpp b/src/ChunkData.cpp index 162803292..79029f0cf 100644 --- a/src/ChunkData.cpp +++ b/src/ChunkData.cpp @@ -1,5 +1,9 @@ +#ifdef TEST_GLOBALS +#include "TestGlobals.h" +#else #include "Globals.h" +#endif #include "ChunkData.h" cChunkData::cChunkData() @@ -39,7 +43,7 @@ cChunkData::~cChunkData() other.IsOwner = false; } - cChunkData::cChunkData& operator=(const cChunkData& other) + cChunkData& cChunkData::operator=(const cChunkData& other) { if (&other != this) { @@ -71,7 +75,7 @@ cChunkData::~cChunkData() } } - cChunkData::cChunkData& operator=(cChunkData&& other) + cChunkData& cChunkData::operator=(cChunkData&& other) { if (&other != this) { @@ -230,7 +234,7 @@ NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const return 0; } -cChunkData cChunkData::cChunkData::Copy() const +cChunkData cChunkData::Copy() const { cChunkData copy; for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) -- cgit v1.2.3