diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index f9b861e..13b4d89 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -16,6 +16,7 @@ set( SOURCES board.cpp unitv.cpp unit.cpp + orders.cpp ) add_library( game ${SOURCES} ) \ No newline at end of file diff --git a/game/orders.cpp b/game/orders.cpp new file mode 100644 index 0000000..4cae3d2 --- /dev/null +++ b/game/orders.cpp @@ -0,0 +1,34 @@ +#include "orders.h" + +// Convert an order to a string +std::string GetStringFromOrder( COrder& order ) +{ + std::string ret; + ret += std::to_string(order.unit); + ret += ORDER_DELIMITER; + ret += order.order; + + return ret; +} + +// Convert a string to an order +COrder GetOrderFromString( std::string order ) +{ + COrder ret; + + int pos = order.find(ORDER_DELIMITER); + if( pos != std::string::npos ) + { + const std::string order_unit = order.substr(0, pos); + + ret.unit = atoi( order_unit.c_str() ); + + // Erase everything up to and including the delimiter + order.erase(0, pos + 1); + + // Next single char is the order + ret.order = order[0]; + } + + return ret; +} \ No newline at end of file diff --git a/game/orders.h b/game/orders.h new file mode 100644 index 0000000..d737935 --- /dev/null +++ b/game/orders.h @@ -0,0 +1,42 @@ +#ifndef _ORDERS_H_ +#define _ORDERS_H_ + +#include +#include + +#include "gametypes.h" + +#define ORDER_DELIMITER ' ' + +// Type for all orders ( as a char ) +typedef char order_c; + +// Container for an order +struct COrder +{ + // Unit order is for + unit_id_t unit; + + // Order command issued + order_c order; + + inline bool operator==( const COrder& rhs ) const; + inline bool operator!=( const COrder& rhs ) const { return !(*this==rhs); } +}; + +inline bool COrder::operator== ( const COrder& rhs ) const +{ + return ( unit == rhs.unit ) && ( order == rhs.order ); +} + +// Typedef a vector of orders +typedef std::vector COrderVector; + +// Order strings stored as simply "[unit id] [order char]" + +// string <--> order conversion functions +std::string GetStringFromOrder( COrder& order ); +COrder GetOrderFromString( std::string order ); + + +#endif //_ORDERS_H_ \ No newline at end of file