More cleanup and remove of unneeded functions

This commit is contained in:
Marc Di Luzio 2014-12-16 13:13:02 +00:00
parent 005896b13c
commit 4c439a383e
6 changed files with 27 additions and 84 deletions

View file

@ -5,10 +5,6 @@
#include "gametypes.h" #include "gametypes.h"
#include "orders.h" #include "orders.h"
#include <memory> // unique_ptr and shared_ptr
typedef std::vector< CUnit > CUnitVector;
// Type for order and unit pairs // Type for order and unit pairs
struct OrderUnitPair struct OrderUnitPair
{ {
@ -16,25 +12,22 @@ struct OrderUnitPair
OrderUnitPair( OrderUnitPair&& other ) OrderUnitPair( OrderUnitPair&& other )
: unit ( std::move(other.unit) ) : unit ( std::move(other.unit) )
, order ( other.order ) , order ( other.order )
{ {}
}
// Multi parameter constructor // Multi parameter constructor
OrderUnitPair( CUnit&& u, COrder o ) OrderUnitPair( CUnit&& u, COrder o )
: unit ( std::move(u) ) : unit ( std::move(u) )
, order ( o ) , order ( o )
{ {}
}
// Move asignment operator // Move asignment operator
inline OrderUnitPair& operator=( OrderUnitPair&& rhs ) { this->unit = std::move(rhs.unit);this->order = rhs.order;rhs.order = COrder(); return *this; } inline OrderUnitPair& operator=( OrderUnitPair&& rhs ) { this->unit = std::move(rhs.unit);this->order = rhs.order;rhs.order = COrder(); return *this; }
CUnit unit; CUnit unit; // The unit
COrder order; COrder order; // Order for this unit from this turn
}; };
// Typedef for a vector of these unit pairs
typedef std::vector< OrderUnitPair > OrderUnitPairVector; typedef std::vector< OrderUnitPair > OrderUnitPairVector;
class CTTRTSGame class CTTRTSGame
@ -54,6 +47,7 @@ public:
int IssueOrders( player_id_t player, const std::string& orders ); int IssueOrders( player_id_t player, const std::string& orders );
int IssueOrders( player_id_t player, const COrderVector& orders ); int IssueOrders( player_id_t player, const COrderVector& orders );
// Issue a single order, returns non-zero for rejection
int IssueOrder( player_id_t player, const COrder& order ); int IssueOrder( player_id_t player, const COrder& order );
// Simulate and progress to the next turn // Simulate and progress to the next turn

View file

@ -35,25 +35,3 @@ COrder GetOrderFromString( const std::string& _order )
return ret; return ret;
} }
bool isMovementOrder( const COrder& order )
{
const order_c c = order.order;
for ( unsigned int i = 0; i < _countof(sk_movementOrders); i++ )
{
if ( c == sk_movementOrders[i] )
return true;
}
return false;
}
bool isActionOrder( const COrder& order )
{
const order_c c = order.order;
for ( unsigned int i = 0; i < _countof(sk_actionOrders); i++ )
{
if ( c == sk_actionOrders[i] )
return true;
}
return false;
}

View file

@ -18,29 +18,14 @@ enum class order_c : char
INVALID INVALID
}; };
// Movement orders
static const order_c sk_movementOrders[] =
{
order_c::F, // Forward
};
// Action orders
static const order_c sk_actionOrders[] =
{
order_c::L, // Left
order_c::R, // Right
order_c::A, // Attack
};
// Container for an order // Container for an order
struct COrder struct COrder
{ {
// Base constructor makes invalid order
COrder() COrder()
: unit ( unit_id_invalid ) : unit ( unit_id_invalid )
, order ( order_c::INVALID ) , order ( order_c::INVALID )
{ {}
}
// Unit order is for // Unit order is for
unit_id_t unit; unit_id_t unit;
@ -67,7 +52,4 @@ typedef std::vector<COrder> COrderVector;
std::string GetStringFromOrder(const COrder& order ); std::string GetStringFromOrder(const COrder& order );
COrder GetOrderFromString( const std::string& order ); COrder GetOrderFromString( const std::string& order );
bool isMovementOrder( const COrder& order );
bool isActionOrder( const COrder& order );
#endif //_ORDERS_H_ #endif //_ORDERS_H_

View file

@ -39,7 +39,7 @@ CUnit::CUnit()
, pos ( { ucoord_invalid, ucoord_invalid } ) , pos ( { ucoord_invalid, ucoord_invalid } )
{ {
updateMyVisual(); updateMyVisual();
}; }
// Move constructor // Move constructor
CUnit::CUnit(CUnit&& unit) CUnit::CUnit(CUnit&& unit)
@ -54,6 +54,7 @@ CUnit::CUnit(CUnit&& unit)
} }
// Move asignment operator
CUnit& CUnit::operator=(CUnit&& unit) CUnit& CUnit::operator=(CUnit&& unit)
{ {
unit_id = std::move(unit.unit_id) ; unit_id = std::move(unit.unit_id) ;
@ -65,6 +66,7 @@ CUnit& CUnit::operator=(CUnit&& unit)
return *this; return *this;
} }
// Get a unit from a visual
CUnit CUnit::getUnitFromVis( unitVis_c vis ) CUnit CUnit::getUnitFromVis( unitVis_c vis )
{ {
CUnit unit; CUnit unit;
@ -87,6 +89,7 @@ unitVis_c CUnit::updateMyVisual()
return getVisual(); return getVisual();
} }
// Set the unit from visual
bool CUnit::setFromVisual( const unitVis_c& vis ) bool CUnit::setFromVisual( const unitVis_c& vis )
{ {
dir_to_vis_map::const_iterator it; dir_to_vis_map::const_iterator it;

View file

@ -2,7 +2,7 @@
#define _UNIT_H_ #define _UNIT_H_
#include <string> #include <string>
#include <memory> #include <vector>
#include "gametypes.h" #include "gametypes.h"
#include "vector2.h" #include "vector2.h"
@ -12,11 +12,17 @@ class CUnit
{ {
public: public:
// Constructor
CUnit(); CUnit();
// Move constructor and move asignement. CUnit cannot be copied
CUnit(CUnit&& unit); CUnit(CUnit&& unit);
CUnit& operator=(CUnit&& unit); CUnit& operator=(CUnit&& unit);
// Default dtor
~CUnit() = default; ~CUnit() = default;
// Getters for all the members
inline const unit_id_t& getID() const { return unit_id; } inline const unit_id_t& getID() const { return unit_id; }
inline const team_id_t& getTeam() const { return team_id; } inline const team_id_t& getTeam() const { return team_id; }
inline const player_id_t& getPlayer() const { return player_id; } inline const player_id_t& getPlayer() const { return player_id; }
@ -27,6 +33,8 @@ public:
inline int setTeam(const team_id_t& v) { return (v == team_id_invalid) ? -1 : (( team_id = v ), 0); } 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 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 int setVisual(const unitVis_c& v) { return (v == unitVis_invalid) ? -1 : (( unit_vis = v ), 0); }
// Set unit direction
inline dir_t setDir(const dir_t& v) { return (dir = v); } inline dir_t setDir(const dir_t& v) { return (dir = v); }
inline const uvector2& getPos() const { return pos; } inline const uvector2& getPos() const { return pos; }
@ -44,18 +52,15 @@ public:
// Factory function for creating units from a visual // Factory function for creating units from a visual
static CUnit getUnitFromVis( unitVis_c vis ); static CUnit getUnitFromVis( unitVis_c vis );
// Orientation methods
dir_t turnLeft(); dir_t turnLeft();
dir_t turnRight(); dir_t turnRight();
dir_t turnAround(); dir_t turnAround();
protected:
private: private:
// Update the visual of V // Update my visual must be called when setting direction
unitVis_c updateMyVisual(); unitVis_c updateMyVisual();
// Unit ID // Unit ID
unit_id_t unit_id; unit_id_t unit_id;
@ -76,6 +81,9 @@ private:
uvector2 pos; uvector2 pos;
}; };
// Typedef for a vector of units
typedef std::vector< CUnit > CUnitVector;
// Simple validation // Simple validation
inline bool CUnit::valid() const inline bool CUnit::valid() const
{ {

View file

@ -41,28 +41,6 @@ const char* tests()
return "failed order string conversion test"; return "failed order string conversion test";
} }
// Test if movement order is correctly recognised
{
COrder order;
order.order = order_c::F;
if (!isMovementOrder(order) )
return "Failed to detect a movement order";
if (isActionOrder(order) )
return "Wrongly detected an action order";
}
// Test if Attack order is correctly recognised
{
COrder order;
order.order = order_c::A;
if (! isActionOrder(order) )
return "Failed to detect a action order";
if (isMovementOrder(order) )
return "Wrongly detected an movement order";
}
// Test of the game can logically handle a blank game // Test of the game can logically handle a blank game
{ {
CTTRTSGame game( 15, 10 ); CTTRTSGame game( 15, 10 );