From e310acfaf817aaa50681c1d5f51e383e94725e9a Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Tue, 16 Dec 2014 13:13:01 +0000 Subject: [PATCH] More skeleton code for simulating a turn Use enum classes for safe enums --- CMakeLists.txt.user | 228 ++++++++++++++ game/game.cpp | 59 +++- game/game.h | 24 +- game/orders.cpp | 6 +- game/orders.h | 18 +- maths/mathtypes.h | 6 +- test/test.cpp | 8 +- ttrts.sublime-project | 8 + ttrts.sublime-workspace | 651 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 983 insertions(+), 25 deletions(-) create mode 100644 CMakeLists.txt.user create mode 100644 ttrts.sublime-project create mode 100644 ttrts.sublime-workspace diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user new file mode 100644 index 0000000..6ec3667 --- /dev/null +++ b/CMakeLists.txt.user @@ -0,0 +1,228 @@ + + + + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + true + 1 + true + 0 + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + {4857d833-10e4-4a1a-ab3c-1fb012a1b975} + 0 + 0 + 1 + + /home/mdiluzio/Projects/ttrts/build + false + + + + + all + + false + false + true + Make + + CMakeProjectManager.MakeStep + + 1 + Build + + ProjectExplorer.BuildSteps.Build + + + + clean + + true + false + true + Make + + CMakeProjectManager.MakeStep + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + all + + CMakeProjectManager.CMakeBuildConfiguration + + 1 + + + 0 + Deploy + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy locally + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + true + + false + false + false + false + true + 0.01 + 10 + true + 25 + + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + ttrts + + false + + + ttrts + + CMakeProjectManager.CMakeRunConfiguration.ttrts + 3768 + true + false + false + true + + + true + + false + false + false + false + true + 0.01 + 10 + true + 25 + + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + ttrts-test + + false + + + ttrts-test + + CMakeProjectManager.CMakeRunConfiguration.ttrts-test + 3768 + true + false + false + true + + 2 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.EnvironmentId + {3beffcb8-4a04-420b-880b-3a95e17b2e51} + + + ProjectExplorer.Project.Updater.FileVersion + 12 + + diff --git a/game/game.cpp b/game/game.cpp index c7354f7..7d30032 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -98,7 +98,7 @@ int CTTRTSGame::AddUnits( CUnitVector&& units ) } // Verify any order -int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order ) +int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order ) const { // Grab the unit ID const unit_id_t unitID = order.unit; @@ -118,17 +118,68 @@ int CTTRTSGame::VerifyOrder( player_id_t player, const COrder& order ) return unitFound; } + +// Get unit by unit ID +const CUnit& CTTRTSGame::GetUnitByID( unit_id_t id ) const +{ + CUnitVector::const_iterator it; + + for ( it = m_allUnits.begin(); it != m_allUnits.end(); it++ ) + { + // Attempt the unit add + if ( (*it).getID() ) + return *it; + } + + // Return an invalid unit + static CUnit invalid_unit; + return invalid_unit; +} + +// Verify an order unit pair +int CTTRTSGame::VerifyOrderUnitPair( const OrderUnitPair& pair ) const +{ + switch ( pair.order.order ) + { + case order_c::F: + { + // Verify new unit position will be on the board + } + break; + case order_c::L: + case order_c::R: + case order_c::A: + // Nothing needed here, orders can always be carried out + break; + } + + return 0; +} + // Simulate all movements int CTTRTSGame::SimulateMovements() { - // Grab all movement orders - COrderVector movements; + OrderUnitPairVector movements; + + // Grab all movement orders for ( COrderVector::const_iterator it = m_orders.begin(); it != m_orders.end(); it++ ) { if( isMovementOrder(*it) ) - movements.push_back(*it); + { + const OrderUnitPair pair = { *it, GetUnitByID((*it).unit) }; + movements.push_back(pair); + } } + // Remove all orders with straight up impossible movements + for ( OrderUnitPairVector::const_iterator it = movements.begin(); it != movements.end(); it++ ) + { + if( VerifyOrderUnitPair(*it) ) + { + + } + } + // Calculate movements return 0; diff --git a/game/game.h b/game/game.h index 7f511ea..10913e0 100644 --- a/game/game.h +++ b/game/game.h @@ -9,6 +9,15 @@ typedef std::vector< CUnit > CUnitVector; +// Type for order and unit pairs +struct OrderUnitPair +{ + const COrder& order; + const CUnit& unit; +}; + +typedef std::vector< OrderUnitPair > OrderUnitPairVector; + class CTTRTSGame { public: @@ -44,6 +53,9 @@ public: // Get unit by index as above (not unit ID) inline const CUnit& GetUnitByIndex( unsigned int i ) const { return m_allUnits[i]; } + // Get unit by unit ID + const CUnit& GetUnitByID( unit_id_t id ) const; + // Get the number of order inline unsigned int GetNumOrders() const { return m_orders.size(); } @@ -51,7 +63,7 @@ public: inline const COrder& GetOrdersByIndex( unsigned int i ) const { return m_orders[i]; } // Get dimentions - inline const uvector2& GetDimentions() const { return dimentions; }; + inline const uvector2& GetDimentions() const { return dimentions; } private: @@ -61,11 +73,13 @@ private: // Simulate all actions int SimulateActions(); - // Verify any order - int VerifyOrder( player_id_t player, const COrder& order ); + int VerifyOrderUnitPair( const OrderUnitPair& pair ) const; // Verify any order - int VerifyUnit( const CUnit& unit ); + int VerifyOrder( player_id_t player, const COrder& order ) const; + + // Verify any order + int VerifyUnit( const CUnit& unit ) const; // Vector to store points to all units CUnitVector m_allUnits; @@ -77,4 +91,4 @@ private: uvector2 dimentions; }; -#endif //_GAME_H_ \ No newline at end of file +#endif //_GAME_H_ diff --git a/game/orders.cpp b/game/orders.cpp index 5928f1f..96606e9 100644 --- a/game/orders.cpp +++ b/game/orders.cpp @@ -8,7 +8,7 @@ std::string GetStringFromOrder(const COrder& order ) std::string ret; ret += std::to_string(order.unit); ret += ORDER_DELIMITER; - ret += order.order; + ret += (char)order.order; return ret; } @@ -30,7 +30,7 @@ COrder GetOrderFromString( const std::string& _order ) order.erase(0, pos + 1); // Next single char is the order - ret.order = order[0]; + ret.order = (order_c)order[0]; } return ret; @@ -56,4 +56,4 @@ bool isActionOrder( const COrder& order ) return true; } return false; -} \ No newline at end of file +} diff --git a/game/orders.h b/game/orders.h index a433235..400f2b3 100644 --- a/game/orders.h +++ b/game/orders.h @@ -9,20 +9,26 @@ #define ORDER_DELIMITER ' ' // Type for all orders ( as a char ) -typedef char order_c; +enum class order_c : char +{ + F = 'F', + L = 'L', + R = 'R', + A = 'A' +}; // Movement orders static const order_c sk_movementOrders[] = { - 'F', // Forward + order_c::F, // Forward }; // Action orders static const order_c sk_actionOrders[] = { - 'L', // Left - 'R', // Right - 'A', // Attack + order_c::L, // Left + order_c::R, // Right + order_c::A, // Attack }; // Container for an order @@ -56,4 +62,4 @@ COrder GetOrderFromString( const std::string& order ); bool isMovementOrder( const COrder& order ); bool isActionOrder( const COrder& order ); -#endif //_ORDERS_H_ \ No newline at end of file +#endif //_ORDERS_H_ diff --git a/maths/mathtypes.h b/maths/mathtypes.h index 322f78f..b7804af 100644 --- a/maths/mathtypes.h +++ b/maths/mathtypes.h @@ -17,12 +17,12 @@ static const coord_t coord_invalid = std::numeric_limits::max(); static const ucoord_t ucoord_invalid = std::numeric_limits::max(); // Direction representation -enum dir_t : char +enum class dir_t : char { N = 'N', S = 'S', E = 'E', - W = 'W', + W = 'W' }; -#endif //_BASETYPES_H_ \ No newline at end of file +#endif //_BASETYPES_H_ diff --git a/test/test.cpp b/test/test.cpp index 81fa864..c4b5864 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -34,7 +34,7 @@ const char* tests() { COrder order; - order.order = 'F'; + order.order = order_c::F; order.unit = 10; std::string order_string = GetStringFromOrder(order); COrder order2 = GetOrderFromString(order_string); @@ -45,7 +45,7 @@ const char* tests() { COrder order; - order.order = 'F'; + order.order = order_c::F; if (!isMovementOrder(order) ) return "Failed to detect a movement order"; @@ -55,7 +55,7 @@ const char* tests() { COrder order; - order.order = 'L'; + order.order = order_c::A; if (! isActionOrder(order) ) return "Failed to detect a action order"; @@ -93,4 +93,4 @@ int main() std::cout<<"Tests succeeded"<