diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index 935bb02..79b8f0a 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -13,7 +13,6 @@ set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11" ) # Add the sources set( SOURCES game.cpp - unitv.cpp unit.cpp orders.cpp ) diff --git a/game/game.cpp b/game/game.cpp index 20c6801..c7354f7 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -64,33 +64,32 @@ int CTTRTSGame::SimulateToNextTurn() } // Add a unit, nonzero return value indicates error -int CTTRTSGame::AddUnit( std::shared_ptr unit ) +int CTTRTSGame::AddUnit( CUnit&& unit ) { // Verify the unit - const int val = unit->valid(); + const int val = unit.valid(); if( val ) return val; // Verify if the unit can be placed on the current board - const uvector2 pos = unit->getPos(); + const uvector2 pos = unit.getPos(); if( (pos.x < dimentions.x) && (pos.y < dimentions.y) ) return 1; - m_allUnits.push_back(unit); - + m_allUnits.push_back(std::move(unit)); return 0; } // Add a units, nonzero return value indicates error -int CTTRTSGame::AddUnits( sharedUnitVector_t units ) +int CTTRTSGame::AddUnits( CUnitVector&& units ) { - sharedUnitVector_t::iterator it; + CUnitVector::iterator it; for ( it = units.begin(); it != units.end(); it++ ) { // Attempt the unit add - if ( AddUnit(*it) ) + if ( AddUnit( std::move(*it) ) ) return 1; } @@ -106,9 +105,9 @@ int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order ) // Attempt to find the unit bool unitFound = false; - for ( sharedUnitVector_t::const_iterator it = m_allUnits.begin(); it != m_allUnits.end(); it++ ) + for ( CUnitVector::const_iterator it = m_allUnits.begin(); it != m_allUnits.end(); it++ ) { - if ( (*it)->getID() == unitID ) + if ( (*it).getID() == unitID ) { unitFound = true; break; diff --git a/game/game.h b/game/game.h index 883b347..7f511ea 100644 --- a/game/game.h +++ b/game/game.h @@ -7,7 +7,7 @@ #include // unique_ptr and shared_ptr -typedef std::vector< std::shared_ptr > sharedUnitVector_t; +typedef std::vector< CUnit > CUnitVector; class CTTRTSGame { @@ -33,16 +33,16 @@ public: int SimulateToNextTurn(); // Add a unit, nonzero return value indicates error - int AddUnit( std::shared_ptr unit ); + int AddUnit( CUnit&& unit ); // Add a units, nonzero return value indicates error - int AddUnits( sharedUnitVector_t units ); + int AddUnits( CUnitVector&& units ); // Get the number of units inline unsigned int GetNumUnits() const { return m_allUnits.size(); } // Get unit by index as above (not unit ID) - inline const CUnit& GetUnitByIndex( unsigned int i ) const { return *m_allUnits[i]; } + inline const CUnit& GetUnitByIndex( unsigned int i ) const { return m_allUnits[i]; } // Get the number of order inline unsigned int GetNumOrders() const { return m_orders.size(); } @@ -68,7 +68,7 @@ private: int VerifyUnit( const CUnit& unit ); // Vector to store points to all units - sharedUnitVector_t m_allUnits; + CUnitVector m_allUnits; // Orders to execute this turn COrderVector m_orders; diff --git a/game/unit.cpp b/game/unit.cpp index 524941c..03b16b7 100644 --- a/game/unit.cpp +++ b/game/unit.cpp @@ -1,7 +1,6 @@ #include "unit.h" -// Unit types -#include "unitv.h" +#include // for std::map namespace { @@ -11,37 +10,83 @@ namespace static unit_id_t p = 0; return p++; } + + // Map of visual representation of unit V + typedef std::map< dir_t, unitVis_c > dir_to_vis_map; + + // Helper function to get the vis map during static init + const dir_to_vis_map& get_vis_map_V() + { + static const dir_to_vis_map sk_visMap = + { + {dir_t::N,'^'}, + {dir_t::E,'>'}, + {dir_t::S,'v'}, + {dir_t::W,'<'}, + }; + + return sk_visMap; + } } +// Plain constructor CUnit::CUnit() -: unit_id ( get_unique_unit_id() ) -, team_id ( team_id_invalid ) +: unit_id ( get_unique_unit_id() ) +, team_id ( team_id_invalid ) , player_id ( player_id_invalid ) -, unit_vis ( unitVis_invalid ) +, unit_vis ( unitVis_invalid ) +, dir ( dir_t::S ) { - + updateMyVisual(); }; -std::unique_ptr CUnit::getUnitFromVis( unitVis_c vis ) +// Move constructor +CUnit::CUnit(CUnit&& unit) +: unit_id ( std::move(unit.unit_id) ) +, team_id ( std::move(unit.team_id) ) +, player_id ( std::move(unit.player_id) ) +, unit_vis ( std::move(unit.unit_vis) ) +, dir ( std::move(unit.dir) ) { - switch( vis ) + updateMyVisual(); +} + +CUnit&& CUnit::getUnitFromVis( unitVis_c vis ) +{ + CUnit unit; + unit.setFromVisual(vis); + return std::move(unit); +} + +// Update the visual representation of the unit +unitVis_c CUnit::updateMyVisual() +{ + // Start at invalid + setVisual(unitVis_invalid); + + dir_to_vis_map::const_iterator it = get_vis_map_V().find(dir); + + // If found set to new vis + if( it != get_vis_map_V().end() ) + setVisual(it->second); + + return getVisual(); +} + +bool CUnit::setFromVisual( const unitVis_c& vis ) +{ + dir_to_vis_map::const_iterator it; + + for( it = get_vis_map_V().begin(); it != get_vis_map_V().end(); it++ ) { - // Match with any image for a V - case '^': - case '>': - case 'v': - case '<': + if( it->second == vis ) { - // Create a V - std::unique_ptr p = std::unique_ptr(new CUnitV); - if( (bool)p && p->setFromVisual(vis) ) - { - return std::move(p); - } - break; + dir == it->first; + updateMyVisual(); + return true; } } - // No unit found, return nullptr - return std::move(std::unique_ptr(nullptr)); + // No matching direction to visual + return false; } \ No newline at end of file diff --git a/game/unit.h b/game/unit.h index 51764af..c1b51c9 100644 --- a/game/unit.h +++ b/game/unit.h @@ -11,8 +11,10 @@ class CUnit { public: - virtual ~CUnit() = default; + CUnit(); + CUnit(CUnit&& unit); + ~CUnit() = default; inline const unit_id_t& getID() const { return unit_id; } inline const team_id_t& getTeam() const { return team_id; } @@ -31,19 +33,20 @@ public: inline bool valid() const; // Set a unit based solely on it's visual - // Maybe make non-virtual at some point to avoid vtable lookups - virtual bool setFromVisual(unitVis_c& vis) = 0; + bool setFromVisual( const unitVis_c& vis); // Factory function for creating units from a visual - static std::unique_ptr getUnitFromVis( unitVis_c vis ); + static CUnit&& getUnitFromVis( unitVis_c vis ); + protected: - // Protected constructor, cannot be constructed as base type - CUnit(); private: + // Update the visual of V + unitVis_c updateMyVisual(); + // Unit ID const unit_id_t unit_id; @@ -56,8 +59,10 @@ private: // Owner ID player_id_t player_id; + // Direction + dir_t dir; - // All units must have position + // Position uvector2 pos; }; diff --git a/game/unitv.cpp b/game/unitv.cpp deleted file mode 100644 index 39f0d95..0000000 --- a/game/unitv.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "unitv.h" - -#include // for std::map - -CUnitV::CUnitV() -: dir(dir_t::S) -{ - updateMyVisual(); -} - - -// Map of visual representation of unitv -namespace -{ - typedef std::map< dir_t, unitVis_c > dir_to_vis_map; - - // Helper function to get the vis map during static init - const dir_to_vis_map& get_vis_map() - { - static const dir_to_vis_map sk_visMap = - { - {dir_t::N,'^'}, - {dir_t::E,'>'}, - {dir_t::S,'v'}, - {dir_t::W,'<'}, - }; - - return sk_visMap; - } -} - -// Update the visual representation of the unit -unitVis_c CUnitV::updateMyVisual() -{ - // Start at invalid - setVisual(unitVis_invalid); - - dir_to_vis_map::const_iterator it = get_vis_map().find(dir); - - // If found set to new vis - if( it != get_vis_map().end() ) - setVisual(it->second); - - return getVisual(); -} - -bool CUnitV::setFromVisual( unitVis_c& vis ) -{ - dir_to_vis_map::const_iterator it; - - for( it = get_vis_map().begin(); it != get_vis_map().end(); it++ ) - { - if( it->second == vis ) - { - dir == it->first; - updateMyVisual(); - return true; - } - } - - // No matching direction to visual - return false; -} \ No newline at end of file diff --git a/game/unitv.h b/game/unitv.h deleted file mode 100644 index a1cc463..0000000 --- a/game/unitv.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef _UNITV_H_ -#define _UNITV_H_ - -#include "unit.h" - -// V unit -class CUnitV -: public CUnit -{ -public: - CUnitV(); - virtual ~CUnitV() = default; - - // Set from a visual - virtual bool setFromVisual( unitVis_c& vis ) override; - -private: - - // Update the visual of V - unitVis_c updateMyVisual(); - - // V also has a direction - dir_t dir; -}; - -#endif //_UNITV_H_ \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp index 670db3b..6b111f7 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,6 +1,5 @@ #include // std::cout -#include "unitv.h" #include "board.h" #include "orders.h" #include "game.h" @@ -14,21 +13,22 @@ const char* tests() } { - CUnitV myV; - if( myV.getVisual() != 'v' && myV.getVisual() != '<' && myV.getVisual() != '^' && myV.getVisual() != '>' ) + CUnit unit; + unit.setFromVisual('v'); + if( unit.getVisual() != 118 ) return "failed to properly create V unit"; } { - CUnitV myV; - CUnitV myV2; - if( myV.getID() == myV2.getID() ) + CUnit unit; + CUnit unit2; + if( unit.getID() == unit2.getID() ) return "Unit IDs the same"; } { - std::unique_ptr myV = CUnit::getUnitFromVis('v'); - if( myV->getVisual() != 'v' ) + CUnit unit = CUnit::getUnitFromVis('v'); + if( unit.getVisual() != 'v' ) return "failed to properly create V unit"; }