From 45b0f034c3a415180be2625c1fd27e5929d556b1 Mon Sep 17 00:00:00 2001 From: Nelsson Huotari Date: Thu, 3 Oct 2019 23:22:19 +0300 Subject: [PATCH] Split complex if-logic into multiple reasonably named functions --- apps/opencs/view/render/terrainstorage.cpp | 83 +++++++++++++++++----- apps/opencs/view/render/terrainstorage.hpp | 12 ++++ 2 files changed, 77 insertions(+), 18 deletions(-) diff --git a/apps/opencs/view/render/terrainstorage.cpp b/apps/opencs/view/render/terrainstorage.cpp index 3b314935d..8938866e3 100644 --- a/apps/opencs/view/render/terrainstorage.cpp +++ b/apps/opencs/view/render/terrainstorage.cpp @@ -78,27 +78,74 @@ namespace CSVRender throw std::runtime_error("getBounds not implemented"); } + int TerrainStorage::getThisHeight(int col, int row, const ESM::Land::LandData *heightData) const + { + return heightData->mHeights[col*ESM::Land::LAND_SIZE + row] + + mAlteredHeight[static_cast(col*ESM::Land::LAND_SIZE + row)]; + } + + int TerrainStorage::getLeftHeight(int col, int row, const ESM::Land::LandData *heightData) const + { + return heightData->mHeights[(col)*ESM::Land::LAND_SIZE + row - 1] + + mAlteredHeight[static_cast((col)*ESM::Land::LAND_SIZE + row - 1)]; + } + + int TerrainStorage::getRightHeight(int col, int row, const ESM::Land::LandData *heightData) const + { + return heightData->mHeights[col*ESM::Land::LAND_SIZE + row + 1] + + mAlteredHeight[static_cast(col*ESM::Land::LAND_SIZE + row + 1)]; + } + + int TerrainStorage::getUpHeight(int col, int row, const ESM::Land::LandData *heightData) const + { + return heightData->mHeights[(col - 1)*ESM::Land::LAND_SIZE + row] + + mAlteredHeight[static_cast((col - 1)*ESM::Land::LAND_SIZE + row)]; + } + + int TerrainStorage::getDownHeight(int col, int row, const ESM::Land::LandData *heightData) const + { + return heightData->mHeights[(col + 1)*ESM::Land::LAND_SIZE + row] + + mAlteredHeight[static_cast((col + 1)*ESM::Land::LAND_SIZE + row)]; + } + + int TerrainStorage::getHeightDifferenceToLeft(int col, int row, const ESM::Land::LandData *heightData) const + { + return abs(getThisHeight(col, row, heightData) - getLeftHeight(col, row, heightData)); + } + + int TerrainStorage::getHeightDifferenceToRight(int col, int row, const ESM::Land::LandData *heightData) const + { + return abs(getThisHeight(col, row, heightData) - getRightHeight(col, row, heightData)); + } + + int TerrainStorage::getHeightDifferenceToUp(int col, int row, const ESM::Land::LandData *heightData) const + { + return abs(getThisHeight(col, row, heightData) - getUpHeight(col, row, heightData)); + } + + int TerrainStorage::getHeightDifferenceToDown(int col, int row, const ESM::Land::LandData *heightData) const + { + return abs(getThisHeight(col, row, heightData) - getDownHeight(col, row, heightData)); + } + + bool TerrainStorage::LeftOrUpIsOverTheLimit(int col, int row, int heightWarningLimit, const ESM::Land::LandData *heightData) const + { + return getHeightDifferenceToLeft(col, row, heightData) >= heightWarningLimit || + getHeightDifferenceToUp(col, row, heightData) >= heightWarningLimit; + } + + bool TerrainStorage::RightOrDownIsOverTheLimit(int col, int row, int heightWarningLimit, const ESM::Land::LandData *heightData) const + { + return getHeightDifferenceToRight(col, row, heightData) >= heightWarningLimit || + getHeightDifferenceToDown(col, row, heightData) >= heightWarningLimit; + } + void TerrainStorage::adjustColor(int col, int row, const ESM::Land::LandData *heightData, osg::Vec4ub& color) const { // Highlight broken height changes - if ( ((col > 0 && row > 0) && - ((abs(heightData->mHeights[col*ESM::Land::LAND_SIZE + row] + - mAlteredHeight[static_cast(col*ESM::Land::LAND_SIZE + row)] - - (heightData->mHeights[(col)*ESM::Land::LAND_SIZE + row - 1] + - mAlteredHeight[static_cast((col)*ESM::Land::LAND_SIZE + row - 1)])) >= 1024 ) || - abs(heightData->mHeights[col*ESM::Land::LAND_SIZE + row] + - mAlteredHeight[static_cast(col*ESM::Land::LAND_SIZE + row)] - - (heightData->mHeights[(col - 1)*ESM::Land::LAND_SIZE + row] + - mAlteredHeight[static_cast((col - 1)*ESM::Land::LAND_SIZE + row)])) >= 1024 )) || - ((col < ESM::Land::LAND_SIZE - 1 && row < ESM::Land::LAND_SIZE - 1) && - ((abs(heightData->mHeights[col*ESM::Land::LAND_SIZE + row] + - mAlteredHeight[static_cast(col*ESM::Land::LAND_SIZE + row)] - - (heightData->mHeights[(col)*ESM::Land::LAND_SIZE + row + 1] + - mAlteredHeight[static_cast((col)*ESM::Land::LAND_SIZE + row + 1)])) >= 1024 ) || - abs(heightData->mHeights[col*ESM::Land::LAND_SIZE + row] + - mAlteredHeight[static_cast(col*ESM::Land::LAND_SIZE + row)] - - (heightData->mHeights[(col + 1)*ESM::Land::LAND_SIZE + row] + - mAlteredHeight[static_cast((col + 1)*ESM::Land::LAND_SIZE + row)])) >= 1024 ))) + int heightWarningLimit = 1024; + if (((col > 0 && row > 0) && LeftOrUpIsOverTheLimit(col, row, heightWarningLimit, heightData)) || + ((col < ESM::Land::LAND_SIZE - 1 && row < ESM::Land::LAND_SIZE - 1) && RightOrDownIsOverTheLimit(col, row, heightWarningLimit, heightData))) { color.r() = 255; color.g() = 0; diff --git a/apps/opencs/view/render/terrainstorage.hpp b/apps/opencs/view/render/terrainstorage.hpp index fe2d94b4a..703cd9be2 100644 --- a/apps/opencs/view/render/terrainstorage.hpp +++ b/apps/opencs/view/render/terrainstorage.hpp @@ -30,6 +30,18 @@ namespace CSVRender virtual void getBounds(float& minX, float& maxX, float& minY, float& maxY) override; + int getThisHeight(int col, int row, const ESM::Land::LandData *heightData) const; + int getLeftHeight(int col, int row, const ESM::Land::LandData *heightData) const; + int getRightHeight(int col, int row, const ESM::Land::LandData *heightData) const; + int getUpHeight(int col, int row, const ESM::Land::LandData *heightData) const; + int getDownHeight(int col, int row, const ESM::Land::LandData *heightData) const; + int getHeightDifferenceToLeft(int col, int row, const ESM::Land::LandData *heightData) const; + int getHeightDifferenceToRight(int col, int row, const ESM::Land::LandData *heightData) const; + int getHeightDifferenceToUp(int col, int row, const ESM::Land::LandData *heightData) const; + int getHeightDifferenceToDown(int col, int row, const ESM::Land::LandData *heightData) const; + bool LeftOrUpIsOverTheLimit(int col, int row, int heightWarningLimit, const ESM::Land::LandData *heightData) const; + bool RightOrDownIsOverTheLimit(int col, int row, int heightWarningLimit, const ESM::Land::LandData *heightData) const; + void adjustColor(int col, int row, const ESM::Land::LandData *heightData, osg::Vec4ub& color) const override; float getAlteredHeight(int col, int row) const override; };