From b9fdaf8a942ea2f79e48e3f735ba8916f3364e70 Mon Sep 17 00:00:00 2001 From: Bond-009 Date: Sun, 6 May 2018 19:07:34 +0200 Subject: [PATCH] Use clang-tidy to check more code conventions (#4214) * Create clang-tidy.sh * Add clang-tidy to circle.yml * Fixed some naming violations Fixes #4164 --- .clang-tidy | 17 +++++ .gitignore | 5 ++ circle.yml | 10 ++- clang-tidy.sh | 19 +++++ src/IniFile.cpp | 136 ++++++++++++++++----------------- src/IniFile.h | 18 ++--- src/Mobs/Enderman.cpp | 4 +- src/Mobs/Enderman.h | 8 +- src/NetherPortalScanner.cpp | 8 ++ src/NetherPortalScanner.h | 4 +- src/Root.cpp | 1 - src/Tracer.cpp | 148 ++++++++++++++++++------------------ src/Tracer.h | 14 ++-- 13 files changed, 224 insertions(+), 168 deletions(-) create mode 100644 .clang-tidy create mode 100755 clang-tidy.sh diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 000000000..b86487175 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,17 @@ +Checks: '-*,readability-identifier-naming' +CheckOptions: +- key: readability-identifier-naming.PrivateMemberPrefix + value: 'm_' +- key: readability-identifier-naming.ClassConstantCase + value: aNy_CasE +# an empty *Prefix needs a *Case to work +- key: readability-identifier-naming.ClassConstantPrefix + value: '' +#- key: readability-identifier-naming.PrivateMemberCase +# value: CamelCase +#- key: readability-identifier-naming.FunctionCase +# value: CamelCase +#- key: readability-identifier-naming.EnumCase +# value: camelBack +WarningsAsErrors: '*' +HeaderFilterRegex: '/cuberite/src/\.?[^\.]' diff --git a/.gitignore b/.gitignore index 83d3eaaec..cb520408f 100644 --- a/.gitignore +++ b/.gitignore @@ -111,6 +111,11 @@ Server/cuberite_api.lua Server/official_undocumented.lua Server/NewlyUndocumented.lua Server/.luacheckrc +compile_commands.json # compile.sh folder build-cuberite + +# clang-tidy +tidy-build +run-clang-tidy.py diff --git a/circle.yml b/circle.yml index f13d02ce8..3a8cac12b 100644 --- a/circle.yml +++ b/circle.yml @@ -4,9 +4,17 @@ dependencies: pre: - - sudo apt-get install lua5.1 + - sudo apt-get update + - sudo apt-get install lua5.1 cmake clang-3.9 clang-tidy-3.9 + - sudo ln -s /usr/bin/clang-tidy-3.9 /usr/bin/clang-tidy + - sudo ln -s /usr/bin/clang-apply-replacements-3.9 /usr/bin/clang-apply-replacements + - pip install PyYAML test: override: - cd src && find . -name \*.cpp -or -name \*.h > AllFiles.lst && lua CheckBasicStyle.lua - cd src/Bindings && lua CheckBindingsDependencies.lua + - curl -o "$HOME/bin/run-clang-tidy.py" "https://raw.githubusercontent.com/llvm-mirror/clang-tools-extra/45fb9b20ed7da2f6b95d83e5ef45bb536f49d8ca/clang-tidy/tool/run-clang-tidy.py" + - chmod +x "$HOME/bin/run-clang-tidy.py" + - git submodule update --init + - ./clang-tidy.sh diff --git a/clang-tidy.sh b/clang-tidy.sh new file mode 100755 index 000000000..34cfb26e7 --- /dev/null +++ b/clang-tidy.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +set -e + +FIXES_FILE="tidy-fixes.yaml" +REGEX="/cuberite/src/\.?[^\.]" +# TODO: Add -quiet when Circle CI is updated to 2.0 with a newer version of Ubuntu +ARGS="-header-filter $REGEX -export-fixes $FIXES_FILE "$@" $REGEX" + +mkdir -p tidy-build +cd tidy-build +cmake --target Cuberite -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. + +if run-clang-tidy.py $ARGS; then + echo "clang-tidy: No violations found" +else + echo "clang-tidy: Found violations" + exit 1 +fi diff --git a/src/IniFile.cpp b/src/IniFile.cpp index 4a1c8e735..2113af0e2 100644 --- a/src/IniFile.cpp +++ b/src/IniFile.cpp @@ -154,7 +154,7 @@ bool cIniFile::ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect) case ';': case '#': { - if (names.empty()) + if (m_Names.empty()) { AddHeaderComment(line.substr(pLeft + 1)); } @@ -168,7 +168,7 @@ bool cIniFile::ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect) } // while (getline()) f.close(); - if (keys.empty() && names.empty() && comments.empty()) + if (m_Keys.empty() && m_Names.empty() && m_Comments.empty()) { // File be empty or unreadable, equivalent to nonexistant return false; @@ -199,10 +199,10 @@ bool cIniFile::WriteFile(const AString & a_FileName) const } // Write header comments. - size_t NumComments = comments.size(); + size_t NumComments = m_Comments.size(); for (size_t commentID = 0; commentID < NumComments; ++commentID) { - f << ';' << comments[commentID] << iniEOL; + f << ';' << m_Comments[commentID] << iniEOL; } if (NumComments > 0) { @@ -210,20 +210,20 @@ bool cIniFile::WriteFile(const AString & a_FileName) const } // Write keys and values. - for (size_t keyID = 0; keyID < keys.size(); ++keyID) + for (size_t keyID = 0; keyID < m_Keys.size(); ++keyID) { - f << '[' << names[keyID] << ']' << iniEOL; + f << '[' << m_Names[keyID] << ']' << iniEOL; // Comments. - for (size_t commentID = 0; commentID < keys[keyID].comments.size(); ++commentID) + for (size_t commentID = 0; commentID < m_Keys[keyID].m_Comments.size(); ++commentID) { - f << ';' << keys[keyID].comments[commentID] << iniEOL; + f << ';' << m_Keys[keyID].m_Comments[commentID] << iniEOL; } // Values. - for (size_t valueID = 0; valueID < keys[keyID].names.size(); ++valueID) + for (size_t valueID = 0; valueID < m_Keys[keyID].m_Names.size(); ++valueID) { - f << keys[keyID].names[valueID] << '=' << keys[keyID].values[valueID] << iniEOL; + f << m_Keys[keyID].m_Names[valueID] << '=' << m_Keys[keyID].m_Values[valueID] << iniEOL; } f << iniEOL; } @@ -239,9 +239,9 @@ bool cIniFile::WriteFile(const AString & a_FileName) const int cIniFile::FindKey(const AString & a_KeyName) const { AString CaseKeyName = CheckCase(a_KeyName); - for (size_t keyID = 0; keyID < names.size(); ++keyID) + for (size_t keyID = 0; keyID < m_Names.size(); ++keyID) { - if (CheckCase(names[keyID]) == CaseKeyName) + if (CheckCase(m_Names[keyID]) == CaseKeyName) { return static_cast(keyID); } @@ -255,15 +255,15 @@ int cIniFile::FindKey(const AString & a_KeyName) const int cIniFile::FindValue(const int keyID, const AString & a_ValueName) const { - if (!keys.size() || (keyID >= static_cast(keys.size()))) + if (!m_Keys.size() || (keyID >= static_cast(m_Keys.size()))) { return noID; } AString CaseValueName = CheckCase(a_ValueName); - for (size_t valueID = 0; valueID < keys[static_cast(keyID)].names.size(); ++valueID) + for (size_t valueID = 0; valueID < m_Keys[static_cast(keyID)].m_Names.size(); ++valueID) { - if (CheckCase(keys[static_cast(keyID)].names[valueID]) == CaseValueName) + if (CheckCase(m_Keys[static_cast(keyID)].m_Names[valueID]) == CaseValueName) { return int(valueID); } @@ -277,9 +277,9 @@ int cIniFile::FindValue(const int keyID, const AString & a_ValueName) const int cIniFile::AddKeyName(const AString & keyname) { - names.resize(names.size() + 1, keyname); - keys.resize(keys.size() + 1); - return static_cast(names.size()) - 1; + m_Names.resize(m_Names.size() + 1, keyname); + m_Keys.resize(m_Keys.size() + 1); + return static_cast(m_Names.size()) - 1; } @@ -288,9 +288,9 @@ int cIniFile::AddKeyName(const AString & keyname) AString cIniFile::GetKeyName(const int keyID) const { - if (keyID < static_cast(names.size())) + if (keyID < static_cast(m_Names.size())) { - return names[static_cast(keyID)]; + return m_Names[static_cast(keyID)]; } else { @@ -304,9 +304,9 @@ AString cIniFile::GetKeyName(const int keyID) const int cIniFile::GetNumValues(const int keyID) const { - if (keyID < static_cast(keys.size())) + if (keyID < static_cast(m_Keys.size())) { - return static_cast(keys[static_cast(keyID)].names.size()); + return static_cast(m_Keys[static_cast(keyID)].m_Names.size()); } return 0; } @@ -322,7 +322,7 @@ int cIniFile::GetNumValues(const AString & keyname) const { return 0; } - return static_cast(keys[static_cast(keyID)].names.size()); + return static_cast(m_Keys[static_cast(keyID)].m_Names.size()); } @@ -331,9 +331,9 @@ int cIniFile::GetNumValues(const AString & keyname) const AString cIniFile::GetValueName(const int keyID, const int valueID) const { - if ((keyID < static_cast(keys.size())) && (valueID < static_cast(keys[static_cast(keyID)].names.size()))) + if ((keyID < static_cast(m_Keys.size())) && (valueID < static_cast(m_Keys[static_cast(keyID)].m_Names.size()))) { - return keys[static_cast(keyID)].names[static_cast(valueID)]; + return m_Keys[static_cast(keyID)].m_Names[static_cast(valueID)]; } return ""; } @@ -364,8 +364,8 @@ void cIniFile::AddValue(const AString & a_KeyName, const AString & a_ValueName, keyID = int(AddKeyName(a_KeyName)); } - keys[static_cast(keyID)].names.push_back(a_ValueName); - keys[static_cast(keyID)].values.push_back(a_Value); + m_Keys[static_cast(keyID)].m_Names.push_back(a_ValueName); + m_Keys[static_cast(keyID)].m_Values.push_back(a_Value); } @@ -392,11 +392,11 @@ void cIniFile::AddValueF(const AString & a_KeyName, const AString & a_ValueName, bool cIniFile::SetValue(const int keyID, const int valueID, const AString & value) { - if ((static_cast(keyID) >= keys.size()) || (static_cast(valueID) >= keys[static_cast(keyID)].names.size())) + if ((static_cast(keyID) >= m_Keys.size()) || (static_cast(valueID) >= m_Keys[static_cast(keyID)].m_Names.size())) { return false; } - keys[static_cast(keyID)].values[static_cast(valueID)] = value; + m_Keys[static_cast(keyID)].m_Values[static_cast(valueID)] = value; return true; } @@ -423,12 +423,12 @@ bool cIniFile::SetValue(const AString & a_KeyName, const AString & a_ValueName, { return false; } - keys[static_cast(keyID)].names.push_back(a_ValueName); - keys[static_cast(keyID)].values.push_back(a_Value); + m_Keys[static_cast(keyID)].m_Names.push_back(a_ValueName); + m_Keys[static_cast(keyID)].m_Values.push_back(a_Value); } else { - keys[static_cast(keyID)].values[static_cast(valueID)] = a_Value; + m_Keys[static_cast(keyID)].m_Values[static_cast(valueID)] = a_Value; } return true; @@ -467,9 +467,9 @@ bool cIniFile::SetValueF(const AString & a_KeyName, const AString & a_ValueName, AString cIniFile::GetValue(const int keyID, const int valueID, const AString & defValue) const { - if ((keyID < static_cast(keys.size())) && (valueID < static_cast(keys[static_cast(keyID)].names.size()))) + if ((keyID < static_cast(m_Keys.size())) && (valueID < static_cast(m_Keys[static_cast(keyID)].m_Names.size()))) { - return keys[static_cast(keyID)].values[static_cast(valueID)]; + return m_Keys[static_cast(keyID)].m_Values[static_cast(valueID)]; } return defValue; } @@ -492,7 +492,7 @@ AString cIniFile::GetValue(const AString & keyname, const AString & valuename, c return defValue; } - return keys[static_cast(keyID)].values[static_cast(valueID)]; + return m_Keys[static_cast(keyID)].m_Values[static_cast(valueID)]; } @@ -537,7 +537,7 @@ AString cIniFile::GetValueSet(const AString & keyname, const AString & valuename return defValue; } - return keys[static_cast(keyID)].values[static_cast(valueID)]; + return m_Keys[static_cast(keyID)].m_Values[static_cast(valueID)]; } @@ -586,13 +586,13 @@ Int64 cIniFile::GetValueSetI(const AString & keyname, const AString & valuename, bool cIniFile::DeleteValueByID(const int keyID, const int valueID) { - if ((keyID < static_cast(keys.size())) && (valueID < static_cast(keys[static_cast(keyID)].names.size()))) + if ((keyID < static_cast(m_Keys.size())) && (valueID < static_cast(m_Keys[static_cast(keyID)].m_Names.size()))) { // This looks strange, but is neccessary. - vector::iterator npos = keys[static_cast(keyID)].names.begin() + valueID; - vector::iterator vpos = keys[static_cast(keyID)].values.begin() + valueID; - keys[static_cast(keyID)].names.erase(npos, npos + 1); - keys[static_cast(keyID)].values.erase(vpos, vpos + 1); + vector::iterator npos = m_Keys[static_cast(keyID)].m_Names.begin() + valueID; + vector::iterator vpos = m_Keys[static_cast(keyID)].m_Values.begin() + valueID; + m_Keys[static_cast(keyID)].m_Names.erase(npos, npos + 1); + m_Keys[static_cast(keyID)].m_Values.erase(vpos, vpos + 1); return true; } return false; @@ -631,10 +631,10 @@ bool cIniFile::DeleteKey(const AString & keyname) return false; } - vector::iterator npos = names.begin() + keyID; - vector::iterator kpos = keys.begin() + keyID; - names.erase(npos, npos + 1); - keys.erase(kpos, kpos + 1); + vector::iterator npos = m_Names.begin() + keyID; + vector::iterator kpos = m_Keys.begin() + keyID; + m_Names.erase(npos, npos + 1); + m_Keys.erase(kpos, kpos + 1); return true; } @@ -645,9 +645,9 @@ bool cIniFile::DeleteKey(const AString & keyname) void cIniFile::Clear(void) { - names.clear(); - keys.clear(); - comments.clear(); + m_Names.clear(); + m_Keys.clear(); + m_Comments.clear(); } @@ -674,7 +674,7 @@ bool cIniFile::HasValue(const AString & a_KeyName, const AString & a_ValueName) void cIniFile::AddHeaderComment(const AString & comment) { - comments.push_back(comment); + m_Comments.push_back(comment); // comments.resize(comments.size() + 1, comment); } @@ -684,9 +684,9 @@ void cIniFile::AddHeaderComment(const AString & comment) AString cIniFile::GetHeaderComment(const int commentID) const { - if (commentID < static_cast(comments.size())) + if (commentID < static_cast(m_Comments.size())) { - return comments[static_cast(commentID)]; + return m_Comments[static_cast(commentID)]; } return ""; } @@ -697,10 +697,10 @@ AString cIniFile::GetHeaderComment(const int commentID) const bool cIniFile::DeleteHeaderComment(int commentID) { - if (commentID < static_cast(comments.size())) + if (commentID < static_cast(m_Comments.size())) { - vector::iterator cpos = comments.begin() + commentID; - comments.erase(cpos, cpos + 1); + vector::iterator cpos = m_Comments.begin() + commentID; + m_Comments.erase(cpos, cpos + 1); return true; } return false; @@ -712,9 +712,9 @@ bool cIniFile::DeleteHeaderComment(int commentID) int cIniFile::GetNumKeyComments(const int keyID) const { - if (keyID < static_cast(keys.size())) + if (keyID < static_cast(m_Keys.size())) { - return static_cast(keys[static_cast(keyID)].comments.size()); + return static_cast(m_Keys[static_cast(keyID)].m_Comments.size()); } return 0; } @@ -730,7 +730,7 @@ int cIniFile::GetNumKeyComments(const AString & keyname) const { return 0; } - return static_cast(keys[static_cast(keyID)].comments.size()); + return static_cast(m_Keys[static_cast(keyID)].m_Comments.size()); } @@ -739,9 +739,9 @@ int cIniFile::GetNumKeyComments(const AString & keyname) const bool cIniFile::AddKeyComment(const int keyID, const AString & comment) { - if (keyID < static_cast(keys.size())) + if (keyID < static_cast(m_Keys.size())) { - keys[static_cast(keyID)].comments.resize(keys[static_cast(keyID)].comments.size() + 1, comment); + m_Keys[static_cast(keyID)].m_Comments.resize(m_Keys[static_cast(keyID)].m_Comments.size() + 1, comment); return true; } return false; @@ -767,9 +767,9 @@ bool cIniFile::AddKeyComment(const AString & keyname, const AString & comment) AString cIniFile::GetKeyComment(const int keyID, const int commentID) const { - if ((keyID < static_cast(keys.size())) && (commentID < static_cast(keys[static_cast(keyID)].comments.size()))) + if ((keyID < static_cast(m_Keys.size())) && (commentID < static_cast(m_Keys[static_cast(keyID)].m_Comments.size()))) { - return keys[static_cast(keyID)].comments[static_cast(commentID)]; + return m_Keys[static_cast(keyID)].m_Comments[static_cast(commentID)]; } return ""; } @@ -794,10 +794,10 @@ AString cIniFile::GetKeyComment(const AString & keyname, const int commentID) co bool cIniFile::DeleteKeyComment(const int keyID, const int commentID) { - if ((keyID < static_cast(keys.size())) && (commentID < static_cast(keys[static_cast(keyID)].comments.size()))) + if ((keyID < static_cast(m_Keys.size())) && (commentID < static_cast(m_Keys[static_cast(keyID)].m_Comments.size()))) { - vector::iterator cpos = keys[static_cast(keyID)].comments.begin() + commentID; - keys[static_cast(keyID)].comments.erase(cpos, cpos + 1); + vector::iterator cpos = m_Keys[static_cast(keyID)].m_Comments.begin() + commentID; + m_Keys[static_cast(keyID)].m_Comments.erase(cpos, cpos + 1); return true; } return false; @@ -823,9 +823,9 @@ bool cIniFile::DeleteKeyComment(const AString & keyname, const int commentID) bool cIniFile::DeleteKeyComments(const int keyID) { - if (keyID < static_cast(keys.size())) + if (keyID < static_cast(m_Keys.size())) { - keys[static_cast(keyID)].comments.clear(); + m_Keys[static_cast(keyID)].m_Comments.clear(); return true; } return false; @@ -910,9 +910,9 @@ std::vector> cIniFile::GetValues(AString a_keyName) { return ret; } - for (size_t valueID = 0; valueID < keys[static_cast(keyID)].names.size(); ++valueID) + for (size_t valueID = 0; valueID < m_Keys[static_cast(keyID)].m_Names.size(); ++valueID) { - ret.emplace_back(keys[static_cast(keyID)].names[valueID], keys[static_cast(keyID)].values[valueID]); + ret.emplace_back(m_Keys[static_cast(keyID)].m_Names[valueID], m_Keys[static_cast(keyID)].m_Values[valueID]); } return ret; } diff --git a/src/IniFile.h b/src/IniFile.h index 3a5359d99..057691cba 100644 --- a/src/IniFile.h +++ b/src/IniFile.h @@ -42,14 +42,14 @@ private: struct key { - std::vector names; - std::vector values; - std::vector comments; + std::vector m_Names; + std::vector m_Values; + std::vector m_Comments; } ; - std::vector keys; - std::vector names; - std::vector comments; + std::vector m_Keys; + std::vector m_Names; + std::vector m_Comments; /** If the object is case-insensitive, returns s as lowercase; otherwise returns s as-is */ AString CheckCase(const AString & s) const; @@ -109,7 +109,7 @@ public: int FindValue(const int keyID, const AString & valuename) const; /** Returns number of keys currently in the ini */ - int GetNumKeys(void) const { return static_cast(keys.size()); } + int GetNumKeys(void) const { return static_cast(m_Keys.size()); } /** Add a key name */ int AddKeyName(const AString & keyname) override; @@ -184,7 +184,7 @@ public: // Header comments are those comments before the first key. /** Returns the number of header comments */ - int GetNumHeaderComments(void) {return static_cast(comments.size());} + int GetNumHeaderComments(void) {return static_cast(m_Comments.size());} /** Adds a header comment */ void AddHeaderComment(const AString & comment); @@ -196,7 +196,7 @@ public: bool DeleteHeaderComment(int commentID); /** Deletes all header comments */ - void DeleteHeaderComments(void) {comments.clear();} + void DeleteHeaderComments(void) {m_Comments.clear();} // Key comment functions. diff --git a/src/Mobs/Enderman.cpp b/src/Mobs/Enderman.cpp index 01e13713d..71ba4e79a 100644 --- a/src/Mobs/Enderman.cpp +++ b/src/Mobs/Enderman.cpp @@ -75,8 +75,8 @@ protected: cEnderman::cEnderman(void) : super("Enderman", mtEnderman, "entity.endermen.hurt", "entity.endermen.death", 0.5, 2.9), m_bIsScreaming(false), - CarriedBlock(E_BLOCK_AIR), - CarriedMeta(0) + m_CarriedBlock(E_BLOCK_AIR), + m_CarriedMeta(0) { } diff --git a/src/Mobs/Enderman.h b/src/Mobs/Enderman.h index d8731b709..4f3f26119 100644 --- a/src/Mobs/Enderman.h +++ b/src/Mobs/Enderman.h @@ -24,8 +24,8 @@ public: virtual void Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) override; bool IsScreaming(void) const {return m_bIsScreaming; } - BLOCKTYPE GetCarriedBlock(void) const {return CarriedBlock; } - NIBBLETYPE GetCarriedMeta(void) const {return CarriedMeta; } + BLOCKTYPE GetCarriedBlock(void) const {return m_CarriedBlock; } + NIBBLETYPE GetCarriedMeta(void) const {return m_CarriedMeta; } /** Returns if the current sky light level is sufficient for the enderman to become aggravated */ bool CheckLight(void); @@ -33,7 +33,7 @@ public: private: bool m_bIsScreaming; - BLOCKTYPE CarriedBlock; - NIBBLETYPE CarriedMeta; + BLOCKTYPE m_CarriedBlock; + NIBBLETYPE m_CarriedMeta; } ; diff --git a/src/NetherPortalScanner.cpp b/src/NetherPortalScanner.cpp index e78af73d9..d329200d9 100644 --- a/src/NetherPortalScanner.cpp +++ b/src/NetherPortalScanner.cpp @@ -9,6 +9,14 @@ + +const double cNetherPortalScanner::OutOffset = 2; +const double cNetherPortalScanner::AcrossOffset = 0.5; + + + + + cNetherPortalScanner::cNetherPortalScanner(cEntity * a_MovingEntity, cWorld * a_DestinationWorld, Vector3d a_DestPosition, int a_MaxY) : m_Entity(a_MovingEntity), m_World(a_DestinationWorld), diff --git a/src/NetherPortalScanner.h b/src/NetherPortalScanner.h index 92af06f98..f3df57c0d 100644 --- a/src/NetherPortalScanner.h +++ b/src/NetherPortalScanner.h @@ -39,8 +39,8 @@ private: static const int SearchSolidBaseWidth = 3; /** Where to place the player out from the face and across the face */ - const double OutOffset = 2; - const double AcrossOffset = 0.5; + static const double OutOffset; + static const double AcrossOffset; /** Builds a portal. */ void BuildNetherPortal(Vector3i a_Location, Direction a_Direction, bool a_IncludePlatform); diff --git a/src/Root.cpp b/src/Root.cpp index 8372e8699..b35d7229b 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -36,7 +36,6 @@ #include "LoggerListeners.h" #include "BuildInfo.h" #include "IniFile.h" -#include "SettingsRepositoryInterface.h" #include "OverridesSettingsRepository.h" #include "Logger.h" #include "ClientHandle.h" diff --git a/src/Tracer.cpp b/src/Tracer.cpp index 913f25e01..5bf50bbfc 100644 --- a/src/Tracer.cpp +++ b/src/Tracer.cpp @@ -75,78 +75,78 @@ void cTracer::SetValues(const Vector3f & a_Start, const Vector3f & a_Direction) ASSERT(a_Direction.HasNonZeroLength()); // calculate the direction of the ray (linear algebra) - dir = a_Direction; + m_Dir = a_Direction; // decide which direction to start walking in - step.x = SigNum(dir.x); - step.y = SigNum(dir.y); - step.z = SigNum(dir.z); + m_Step.x = SigNum(m_Dir.x); + m_Step.y = SigNum(m_Dir.y); + m_Step.z = SigNum(m_Dir.z); // normalize the direction vector - dir.Normalize(); + m_Dir.Normalize(); // how far we must move in the ray direction before // we encounter a new voxel in x-direction // same but y-direction - if (dir.x != 0.f) + if (m_Dir.x != 0.f) { - tDelta.x = 1 / std::abs(dir.x); + m_tDelta.x = 1 / std::abs(m_Dir.x); } else { - tDelta.x = 0; + m_tDelta.x = 0; } - if (dir.y != 0.f) + if (m_Dir.y != 0.f) { - tDelta.y = 1 / std::abs(dir.y); + m_tDelta.y = 1 / std::abs(m_Dir.y); } else { - tDelta.y = 0; + m_tDelta.y = 0; } - if (dir.z != 0.f) + if (m_Dir.z != 0.f) { - tDelta.z = 1 / std::abs(dir.z); + m_tDelta.z = 1 / std::abs(m_Dir.z); } else { - tDelta.z = 0; + m_tDelta.z = 0; } // start voxel coordinates - pos.x = static_cast(floorf(a_Start.x)); - pos.y = static_cast(floorf(a_Start.y)); - pos.z = static_cast(floorf(a_Start.z)); + m_Pos.x = static_cast(floorf(a_Start.x)); + m_Pos.y = static_cast(floorf(a_Start.y)); + m_Pos.z = static_cast(floorf(a_Start.z)); // calculate distance to first intersection in the voxel we start from - if (dir.x < 0) + if (m_Dir.x < 0) { - tMax.x = (static_cast(pos.x) - a_Start.x) / dir.x; + m_tMax.x = (static_cast(m_Pos.x) - a_Start.x) / m_Dir.x; } else { - tMax.x = (static_cast(pos.x + 1) - a_Start.x) / dir.x; // TODO: Possible division by zero + m_tMax.x = (static_cast(m_Pos.x + 1) - a_Start.x) / m_Dir.x; // TODO: Possible division by zero } - if (dir.y < 0) + if (m_Dir.y < 0) { - tMax.y = (static_cast(pos.y) - a_Start.y) / dir.y; + m_tMax.y = (static_cast(m_Pos.y) - a_Start.y) / m_Dir.y; } else { - tMax.y = (static_cast(pos.y + 1) - a_Start.y) / dir.y; // TODO: Possible division by zero + m_tMax.y = (static_cast(m_Pos.y + 1) - a_Start.y) / m_Dir.y; // TODO: Possible division by zero } - if (dir.z < 0) + if (m_Dir.z < 0) { - tMax.z = (static_cast(pos.z) - a_Start.z) / dir.z; + m_tMax.z = (static_cast(m_Pos.z) - a_Start.z) / m_Dir.z; } else { - tMax.z = (static_cast(pos.z + 1) - a_Start.z) / dir.z; // TODO: Possible division by zero + m_tMax.z = (static_cast(m_Pos.z + 1) - a_Start.z) / m_Dir.z; // TODO: Possible division by zero } } @@ -169,21 +169,21 @@ bool cTracer::Trace(const Vector3f & a_Start, const Vector3f & a_Direction, int SetValues(a_Start, a_Direction); - Vector3f End = a_Start + (dir * static_cast(a_Distance)); + Vector3f End = a_Start + (m_Dir * static_cast(a_Distance)); if (End.y < 0) { - float dist = -a_Start.y / dir.y; // No division by 0 possible - End = a_Start + (dir * dist); + float dist = -a_Start.y / m_Dir.y; // No division by 0 possible + End = a_Start + (m_Dir * dist); } // end voxel coordinates - end1.x = static_cast(floorf(End.x)); - end1.y = static_cast(floorf(End.y)); - end1.z = static_cast(floorf(End.z)); + m_End1.x = static_cast(floorf(End.x)); + m_End1.y = static_cast(floorf(End.y)); + m_End1.z = static_cast(floorf(End.z)); // check if first is occupied - if (pos.Equals(end1)) + if (m_Pos.Equals(m_End1)) { return false; } @@ -194,54 +194,54 @@ bool cTracer::Trace(const Vector3f & a_Start, const Vector3f & a_Direction, int while (Iterations < a_Distance) { Iterations++; - if ((tMax.x < tMax.y) && (tMax.x < tMax.z)) + if ((m_tMax.x < m_tMax.y) && (m_tMax.x < m_tMax.z)) { - tMax.x += tDelta.x; - pos.x += step.x; + m_tMax.x += m_tDelta.x; + m_Pos.x += m_Step.x; } - else if (tMax.y < tMax.z) + else if (m_tMax.y < m_tMax.z) { - tMax.y += tDelta.y; - pos.y += step.y; + m_tMax.y += m_tDelta.y; + m_Pos.y += m_Step.y; } else { - tMax.z += tDelta.z; - pos.z += step.z; + m_tMax.z += m_tDelta.z; + m_Pos.z += m_Step.z; } - if (step.x > 0.0f) + if (m_Step.x > 0.0f) { - if (pos.x >= end1.x) + if (m_Pos.x >= m_End1.x) { reachedX = true; } } - else if (pos.x <= end1.x) + else if (m_Pos.x <= m_End1.x) { reachedX = true; } - if (step.y > 0.0f) + if (m_Step.y > 0.0f) { - if (pos.y >= end1.y) + if (m_Pos.y >= m_End1.y) { reachedY = true; } } - else if (pos.y <= end1.y) + else if (m_Pos.y <= m_End1.y) { reachedY = true; } - if (step.z > 0.0f) + if (m_Step.z > 0.0f) { - if (pos.z >= end1.z) + if (m_Pos.z >= m_End1.z) { reachedZ = true; } } - else if (pos.z <= end1.z) + else if (m_Pos.z <= m_End1.z) { reachedZ = true; } @@ -251,17 +251,17 @@ bool cTracer::Trace(const Vector3f & a_Start, const Vector3f & a_Direction, int return false; } - if ((pos.y < 0) || (pos.y >= cChunkDef::Height)) + if ((m_Pos.y < 0) || (m_Pos.y >= cChunkDef::Height)) { return false; } - BLOCKTYPE BlockID = m_World->GetBlock(pos.x, pos.y, pos.z); + BLOCKTYPE BlockID = m_World->GetBlock(m_Pos); // Block is counted as a collision if we are not doing a line of sight and it is solid, // or if the block is not air and not water. That way mobs can still see underwater. if ((!a_LineOfSight && cBlockInfo::IsSolid(BlockID)) || (a_LineOfSight && (BlockID != E_BLOCK_AIR) && !IsBlockWater(BlockID))) { - BlockHitPosition = pos; - int Normal = GetHitNormal(a_Start, End, pos); + BlockHitPosition = m_Pos; + int Normal = GetHitNormal(a_Start, End, m_Pos); if (Normal > 0) { HitNormal = m_NormalTable()[static_cast(Normal - 1)]; @@ -346,7 +346,7 @@ int cTracer::intersect3D_SegmentPlane(const Vector3f & a_Origin, const Vector3f -int cTracer::GetHitNormal(const Vector3f & start, const Vector3f & end, const Vector3i & a_BlockPos) +int cTracer::GetHitNormal(const Vector3f & a_Start, const Vector3f & a_End, const Vector3i & a_BlockPos) { Vector3i SmallBlockPos = a_BlockPos; BLOCKTYPE BlockID = static_cast(m_World->GetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z)); @@ -359,19 +359,19 @@ int cTracer::GetHitNormal(const Vector3f & start, const Vector3f & end, const Ve Vector3f BlockPos; BlockPos = Vector3f(SmallBlockPos); - Vector3f Look = (end - start); + Vector3f Look = (a_End - a_Start); Look.Normalize(); float dot = Look.Dot(Vector3f(-1, 0, 0)); // first face normal is x -1 if (dot < 0) { - int Lines = LinesCross(start.x, start.y, end.x, end.y, BlockPos.x, BlockPos.y, BlockPos.x, BlockPos.y + 1); + int Lines = LinesCross(a_Start.x, a_Start.y, a_End.x, a_End.y, BlockPos.x, BlockPos.y, BlockPos.x, BlockPos.y + 1); if (Lines == 1) { - Lines = LinesCross(start.x, start.z, end.x, end.z, BlockPos.x, BlockPos.z, BlockPos.x, BlockPos.z + 1); + Lines = LinesCross(a_Start.x, a_Start.z, a_End.x, a_End.z, BlockPos.x, BlockPos.z, BlockPos.x, BlockPos.z + 1); if (Lines == 1) { - intersect3D_SegmentPlane(start, end, BlockPos, Vector3f(-1, 0, 0)); + intersect3D_SegmentPlane(a_Start, a_End, BlockPos, Vector3f(-1, 0, 0)); return 1; } } @@ -379,13 +379,13 @@ int cTracer::GetHitNormal(const Vector3f & start, const Vector3f & end, const Ve dot = Look.Dot(Vector3f(0, 0, -1)); // second face normal is z -1 if (dot < 0) { - int Lines = LinesCross(start.z, start.y, end.z, end.y, BlockPos.z, BlockPos.y, BlockPos.z, BlockPos.y + 1); + int Lines = LinesCross(a_Start.z, a_Start.y, a_End.z, a_End.y, BlockPos.z, BlockPos.y, BlockPos.z, BlockPos.y + 1); if (Lines == 1) { - Lines = LinesCross(start.z, start.x, end.z, end.x, BlockPos.z, BlockPos.x, BlockPos.z, BlockPos.x + 1); + Lines = LinesCross(a_Start.z, a_Start.x, a_End.z, a_End.x, BlockPos.z, BlockPos.x, BlockPos.z, BlockPos.x + 1); if (Lines == 1) { - intersect3D_SegmentPlane(start, end, BlockPos, Vector3f(0, 0, -1)); + intersect3D_SegmentPlane(a_Start, a_End, BlockPos, Vector3f(0, 0, -1)); return 2; } } @@ -393,13 +393,13 @@ int cTracer::GetHitNormal(const Vector3f & start, const Vector3f & end, const Ve dot = Look.Dot(Vector3f(1, 0, 0)); // third face normal is x 1 if (dot < 0) { - int Lines = LinesCross(start.x, start.y, end.x, end.y, BlockPos.x + 1, BlockPos.y, BlockPos.x + 1, BlockPos.y + 1); + int Lines = LinesCross(a_Start.x, a_Start.y, a_End.x, a_End.y, BlockPos.x + 1, BlockPos.y, BlockPos.x + 1, BlockPos.y + 1); if (Lines == 1) { - Lines = LinesCross(start.x, start.z, end.x, end.z, BlockPos.x + 1, BlockPos.z, BlockPos.x + 1, BlockPos.z + 1); + Lines = LinesCross(a_Start.x, a_Start.z, a_End.x, a_End.z, BlockPos.x + 1, BlockPos.z, BlockPos.x + 1, BlockPos.z + 1); if (Lines == 1) { - intersect3D_SegmentPlane(start, end, BlockPos + Vector3f(1, 0, 0), Vector3f(1, 0, 0)); + intersect3D_SegmentPlane(a_Start, a_End, BlockPos + Vector3f(1, 0, 0), Vector3f(1, 0, 0)); return 3; } } @@ -407,13 +407,13 @@ int cTracer::GetHitNormal(const Vector3f & start, const Vector3f & end, const Ve dot = Look.Dot(Vector3f(0, 0, 1)); // fourth face normal is z 1 if (dot < 0) { - int Lines = LinesCross(start.z, start.y, end.z, end.y, BlockPos.z + 1, BlockPos.y, BlockPos.z + 1, BlockPos.y + 1); + int Lines = LinesCross(a_Start.z, a_Start.y, a_End.z, a_End.y, BlockPos.z + 1, BlockPos.y, BlockPos.z + 1, BlockPos.y + 1); if (Lines == 1) { - Lines = LinesCross(start.z, start.x, end.z, end.x, BlockPos.z + 1, BlockPos.x, BlockPos.z + 1, BlockPos.x + 1); + Lines = LinesCross(a_Start.z, a_Start.x, a_End.z, a_End.x, BlockPos.z + 1, BlockPos.x, BlockPos.z + 1, BlockPos.x + 1); if (Lines == 1) { - intersect3D_SegmentPlane(start, end, BlockPos + Vector3f(0, 0, 1), Vector3f(0, 0, 1)); + intersect3D_SegmentPlane(a_Start, a_End, BlockPos + Vector3f(0, 0, 1), Vector3f(0, 0, 1)); return 4; } } @@ -421,13 +421,13 @@ int cTracer::GetHitNormal(const Vector3f & start, const Vector3f & end, const Ve dot = Look.Dot(Vector3f(0, 1, 0)); // fifth face normal is y 1 if (dot < 0) { - int Lines = LinesCross(start.y, start.x, end.y, end.x, BlockPos.y + 1, BlockPos.x, BlockPos.y + 1, BlockPos.x + 1); + int Lines = LinesCross(a_Start.y, a_Start.x, a_End.y, a_End.x, BlockPos.y + 1, BlockPos.x, BlockPos.y + 1, BlockPos.x + 1); if (Lines == 1) { - Lines = LinesCross(start.y, start.z, end.y, end.z, BlockPos.y + 1, BlockPos.z, BlockPos.y + 1, BlockPos.z + 1); + Lines = LinesCross(a_Start.y, a_Start.z, a_End.y, a_End.z, BlockPos.y + 1, BlockPos.z, BlockPos.y + 1, BlockPos.z + 1); if (Lines == 1) { - intersect3D_SegmentPlane(start, end, BlockPos + Vector3f(0, 1, 0), Vector3f(0, 1, 0)); + intersect3D_SegmentPlane(a_Start, a_End, BlockPos + Vector3f(0, 1, 0), Vector3f(0, 1, 0)); return 5; } } @@ -435,13 +435,13 @@ int cTracer::GetHitNormal(const Vector3f & start, const Vector3f & end, const Ve dot = Look.Dot(Vector3f(0, -1, 0)); // sixth face normal is y -1 if (dot < 0) { - int Lines = LinesCross(start.y, start.x, end.y, end.x, BlockPos.y, BlockPos.x, BlockPos.y, BlockPos.x + 1); + int Lines = LinesCross(a_Start.y, a_Start.x, a_End.y, a_End.x, BlockPos.y, BlockPos.x, BlockPos.y, BlockPos.x + 1); if (Lines == 1) { - Lines = LinesCross(start.y, start.z, end.y, end.z, BlockPos.y, BlockPos.z, BlockPos.y, BlockPos.z + 1); + Lines = LinesCross(a_Start.y, a_Start.z, a_End.y, a_End.z, BlockPos.y, BlockPos.z, BlockPos.y, BlockPos.z + 1); if (Lines == 1) { - intersect3D_SegmentPlane(start, end, BlockPos, Vector3f(0, -1, 0)); + intersect3D_SegmentPlane(a_Start, a_End, BlockPos, Vector3f(0, -1, 0)); return 6; } } diff --git a/src/Tracer.h b/src/Tracer.h index 151869e3a..19901ee67 100644 --- a/src/Tracer.h +++ b/src/Tracer.h @@ -63,7 +63,7 @@ private: /** Determines which face on the block a collision occured, if it does occur Returns 0 if the block is air, water or no collision occured Return 1 through 6 for the following block faces, repectively: -x, -z, x, z, y, -y */ - int GetHitNormal(const Vector3f & start, const Vector3f & end, const Vector3i & a_BlockPos); + int GetHitNormal(const Vector3f &a_Start, const Vector3f & a_End, const Vector3i & a_BlockPos); /** Signum function */ int SigNum(float a_Num); @@ -72,12 +72,12 @@ private: static const std::array & m_NormalTable(void); - Vector3f dir; - Vector3f tDelta; - Vector3i pos; - Vector3i end1; - Vector3i step; - Vector3f tMax; + Vector3f m_Dir; + Vector3f m_tDelta; + Vector3i m_Pos; + Vector3i m_End1; + Vector3i m_Step; + Vector3f m_tMax; }; // tolua_end