diff --git a/game/game.cpp b/game/game.cpp index fc72ef1..1ebb6d8 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -65,25 +65,36 @@ int CTTRTSGame::SimulateToNextTurn() // Add a unit, nonzero return value indicates error int CTTRTSGame::AddUnit( std::shared_ptr unit ) -{ +{ + // Verify the unit + 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(); + if( (pos.x < cols) && (pos.y < rows) ) + return 1; + + m_allUnits.push_back(unit); + + return 0; } // Add a units, nonzero return value indicates error int CTTRTSGame::AddUnits( sharedUnitVector_t units ) -{ - return 0; -} +{ + sharedUnitVector_t::iterator it; -// Simulate all movements -int CTTRTSGame::SimulateMovements() -{ - return 0; -} + for ( it = units.begin(); it != units.end(); it++ ) + { + // Attempt the unit add + if ( AddUnit(*it) ) + return 1; + } -// Simulate all actions -int CTTRTSGame::SimulateActions() -{ + // All units added successfully return 0; } @@ -106,4 +117,16 @@ int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order ) // for now, as long as the unit exists we can attempt the order return unitFound; -} \ No newline at end of file +} + +// Simulate all movements +int CTTRTSGame::SimulateMovements() +{ + return 0; +} + +// Simulate all actions +int CTTRTSGame::SimulateActions() +{ + return 0; +} diff --git a/game/game.h b/game/game.h index 0c40383..b4d0857 100644 --- a/game/game.h +++ b/game/game.h @@ -62,6 +62,9 @@ private: // Verify any order int VerifyOrder( player_id_t player, const COrder& order ); + // Verify any order + int VerifyUnit( const CUnit& unit ); + // Vector to store points to all units sharedUnitVector_t m_allUnits; diff --git a/game/unit.cpp b/game/unit.cpp index 517c239..524941c 100644 --- a/game/unit.cpp +++ b/game/unit.cpp @@ -3,6 +3,25 @@ // Unit types #include "unitv.h" +namespace +{ + // Helper function for generating unique unit ids during static init + unit_id_t get_unique_unit_id() + { + static unit_id_t p = 0; + return p++; + } +} + +CUnit::CUnit() +: unit_id ( get_unique_unit_id() ) +, team_id ( team_id_invalid ) +, player_id ( player_id_invalid ) +, unit_vis ( unitVis_invalid ) +{ + +}; + std::unique_ptr CUnit::getUnitFromVis( unitVis_c vis ) { switch( vis ) diff --git a/game/unit.h b/game/unit.h index 98d3c04..51764af 100644 --- a/game/unit.h +++ b/game/unit.h @@ -13,29 +13,61 @@ class CUnit public: virtual ~CUnit() = default; - virtual unitVis_c getVisual() const = 0; + + inline const unit_id_t& getID() const { return unit_id; } + inline const team_id_t& getTeam() const { return team_id; } + inline const player_id_t& getPlayer() const { return player_id; } + inline const unitVis_c& getVisual() const { return unit_vis; } + + // Return non-zero values on error + inline int setTeam(const team_id_t& v) { return (v == team_id_invalid) ? -1 : (( team_id = v ), 0); } + inline int setPlayer(const player_id_t& v) { return (v == player_id_invalid) ? -1 : (( player_id = v ), 0); } + inline int setVisual(const unitVis_c& v) { return (v == unitVis_invalid) ? -1 : (( unit_vis = v ), 0); } + + inline const uvector2& getPos() const { return pos; } + inline void setPos(const uvector2& v) { pos = v; } + + // Check unit is valid + 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; + // Factory function for creating units from a visual static std::unique_ptr getUnitFromVis( unitVis_c vis ); - inline unit_id_t getID() const { return id; } - protected: - CUnit() = default; + + // Protected constructor, cannot be constructed as base type + CUnit(); private: // Unit ID - unit_id_t id; + const unit_id_t unit_id; + + // Visual + unitVis_c unit_vis; // Team ID team_id_t team_id; // Owner ID - player_id_t owner_id; + player_id_t player_id; + // All units must have position uvector2 pos; }; +// Simple validation +inline bool CUnit::valid() const +{ + return (unit_id != unit_id_invalid ) + && (team_id != team_id_invalid ) + && (player_id != player_id_invalid) + && (unit_vis != unitVis_invalid); +} + #endif //_UNIT_H_ \ No newline at end of file diff --git a/game/unitv.cpp b/game/unitv.cpp index 92818c3..39f0d95 100644 --- a/game/unitv.cpp +++ b/game/unitv.cpp @@ -5,40 +5,55 @@ CUnitV::CUnitV() : dir(dir_t::S) { - + updateMyVisual(); } // Map of visual representation of unitv -static const std::map< dir_t, unitVis_c > sk_visMap = -{ - {dir_t::N,'^'}, - {dir_t::E,'>'}, - {dir_t::S,'v'}, - {dir_t::W,'<'}, -}; - -unitVis_c CUnitV::getVisual() const -{ - std::map< dir_t, char >::const_iterator it = sk_visMap.find(dir); - - if( it == sk_visMap.end() ) +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() { - return 0; - } + static const dir_to_vis_map sk_visMap = + { + {dir_t::N,'^'}, + {dir_t::E,'>'}, + {dir_t::S,'v'}, + {dir_t::W,'<'}, + }; - return it->second; + 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 ) { - std::map< dir_t, char >::const_iterator it; + dir_to_vis_map::const_iterator it; - for( it = sk_visMap.begin(); it != sk_visMap.end(); it++ ) + for( it = get_vis_map().begin(); it != get_vis_map().end(); it++ ) { if( it->second == vis ) { dir == it->first; + updateMyVisual(); return true; } } diff --git a/game/unitv.h b/game/unitv.h index 2f04dc7..a1cc463 100644 --- a/game/unitv.h +++ b/game/unitv.h @@ -11,11 +11,14 @@ public: CUnitV(); virtual ~CUnitV() = default; - virtual unitVis_c getVisual() const override; - virtual bool setFromVisual( unitVis_c& vis ) override; + // 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; };