Rename all usages of Team to Player

This was confusingly inconsistent in the codebase.
fixes #7
This commit is contained in:
mdiluzio 2014-12-21 11:04:26 +00:00
parent d4f9470704
commit 9e44d5144b
9 changed files with 98 additions and 100 deletions

View file

@ -30,7 +30,7 @@ CTTRTSGame& CTTRTSGame::operator=(CTTRTSGame&& game)
}
// Interpret a string of orders
int CTTRTSGame::IssueOrders( Team team, const std::string& _orders )
int CTTRTSGame::IssueOrders( Player player, const std::string& _orders )
{
COrderVector orderVector;
@ -53,17 +53,17 @@ int CTTRTSGame::IssueOrders( Team team, const std::string& _orders )
}
// Call our add order by vector method
return IssueOrders(team,orderVector);
return IssueOrders(player,orderVector);
}
// Issue orders by vector to the game
int CTTRTSGame::IssueOrders( Team team, const COrderVector& orders )
int CTTRTSGame::IssueOrders( Player player, const COrderVector& orders )
{
// verify all the orders
for ( auto order : orders )
{
// If any order returns non-zero, back out
if ( IssueOrder(team,order) )
if ( IssueOrder(player,order) )
return 1;
}
@ -71,10 +71,10 @@ int CTTRTSGame::IssueOrders( Team team, const COrderVector& orders )
}
// Issue a single order
int CTTRTSGame::IssueOrder( Team team, const SOrder & order )
int CTTRTSGame::IssueOrder( Player player, const SOrder & order )
{
// Verify the order
if ( VerifyOrder(team,order) )
if ( VerifyOrder(player,order) )
return 1;
// Get the right unit for the order
@ -366,7 +366,7 @@ int CTTRTSGame::AddUnits( CUnitVector&& units )
}
// Verify any order
int CTTRTSGame::VerifyOrder( Team team, const SOrder & order ) const
int CTTRTSGame::VerifyOrder( Player player, const SOrder & order ) const
{
int ret = 1;
@ -378,7 +378,7 @@ int CTTRTSGame::VerifyOrder( Team team, const SOrder & order ) const
{
// Accept if we have the unit
if (pair.unit.GetID() == unitID
&& pair.unit.GetTeam() == team )
&& pair.unit.GetPlayer() == player)
{
ret = 0;
break;
@ -434,60 +434,60 @@ CUnit& CTTRTSGame::GetUnitByID( unit_id_t id )
return invalid_unit;
}
// Get a vector of the teams in the current game
std::vector<Team> CTTRTSGame::GetTeams() const
// Get a vector of the players in the current game
std::vector<Player> CTTRTSGame::GetPlayers() const
{
std::vector<Team> teams;
teams.reserve(GetNumUnits());
std::vector<Player> players;
players.reserve(GetNumUnits());
// Grab all teams
// Grab all players
for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
{
teams.push_back(pair.unit.GetTeam());
players.push_back(pair.unit.GetPlayer());
}
// Remove dupes
std::sort( teams.begin(), teams.end() );
teams.erase( std::unique( teams.begin(), teams.end() ), teams.end() );
std::sort( players.begin(), players.end() );
players.erase( std::unique( players.begin(), players.end() ), players.end() );
return teams;
return players;
}
// Check if we have a win state
Team CTTRTSGame::CheckForWin() const
Player CTTRTSGame::CheckForWin() const
{
// Array of units for each Team
unsigned int units[(int) Team::NUM_INVALID];
// Array of units for each Player
unsigned int units[(int) Player::NUM_INVALID];
memset(units,0,sizeof(units));
// Count up all the units for each Team
// Count up all the units for each Player
for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
{
const int team = (int) pair.unit.GetTeam();
units[team] += 1;
const int player = (int) pair.unit.GetPlayer();
units[player] += 1;
}
// Default winning Team to invalid (no win)
Team winningTeam = Team::NUM_INVALID;
// Default winning Player to invalid (no win)
Player winningPlayer = Player::NUM_INVALID;
// For each of the teams
// For each of the players
for ( unsigned int i = 0; i < _countof(units); i++ )
{
// if there are still units in this Team, and the winning Team hasn't been set
if( units[i] > 0 && winningTeam == Team::NUM_INVALID )
// if there are still units in this Player, and the winning Player hasn't been set
if( units[i] > 0 && winningPlayer == Player::NUM_INVALID )
{
winningTeam = (Team)i;
winningPlayer = (Player)i;
}
// Otherwise, if there are units in this Team and the winning Team HAS been set
// Otherwise, if there are units in this Player and the winning Player HAS been set
else if ( units[i] > 0 )
{
// Set back to invalid and break out of the loop
winningTeam = Team::NUM_INVALID;
winningPlayer = Player::NUM_INVALID;
break;
}
}
return winningTeam;
return winningPlayer;
}
// Get the game information as a string

View file

@ -33,15 +33,15 @@ public:
// Check for a win, returns invalid for no win state reached
// Note: this function will return invalid a draw was reached
// best practice would be to call with GetNumUnits() == 0
Team CheckForWin() const;
Player CheckForWin() const;
// Get the game information as a string
std::string GetStateAsString() const;
// Issue orders to the game, returns non-zero if orders are incorrect
int IssueOrders( Team team, const std::string& orders );
int IssueOrders( Team team, const COrderVector& orders );
int IssueOrder( Team team, const SOrder & order );
int IssueOrders( Player player, const std::string& orders );
int IssueOrders( Player player, const COrderVector& orders );
int IssueOrder( Player player, const SOrder & order );
// Add a units to the game, nonzero return value indicates error
int AddUnit( CUnit&& unit );
@ -70,8 +70,8 @@ public:
inline int SetTurn( int in ) { return (turn = in); }
inline int GetTurn() const { return turn; }
// Get a vector of the teams in the current game
std::vector<Team> GetTeams() const;
// Get a vector of the players in the current game
std::vector<Player> GetPlayers() const;
private:
@ -79,7 +79,7 @@ private:
static bool CheckForPassThrough( const CUnit& one, const CUnit& two );
// Verify any order or position - non-zero is error
int VerifyOrder( Team team, const SOrder & order ) const;
int VerifyOrder( Player player, const SOrder & order ) const;
int VerifyPos( uvector2 vec ) const;
// Get a units new position after an order

View file

@ -3,8 +3,8 @@
#include <limits> // std::numeric_limits
// Type for a team IDs
enum class Team : char
// Type for a Player IDs
enum class Player : char
{
Red = 0,
Green,

View file

@ -51,7 +51,6 @@ CUnit CUnit::GetUnitFromVis( unitvis_c vis )
}
// Get a string descriptor of a unit
// "U id:[unit_id] team:[team_id] player:[player_id] vis:[unit_vis] dir:[dir] pos:[pos.x],[pos.y]"
std::string CUnit::GetStringFromUnit(const CUnit& unit )
{
static char buff[128];
@ -59,7 +58,7 @@ std::string CUnit::GetStringFromUnit(const CUnit& unit )
snprintf(buff,128, UNIT_FORMATTER,
unit.unit_id,
(int)unit.team_id,
(int)unit.player_id,
unit.unit_vis,
unit.dir,
unit.pos.x,
@ -69,13 +68,12 @@ std::string CUnit::GetStringFromUnit(const CUnit& unit )
}
// Get a unit from a string descriptor
// "U id:[unit_id] team:[team_id] player:[player_id] vis:[unit_vis] dir:[dir] pos:[pos.x],[pos.y]"
CUnit CUnit::GetUnitFromString(const std::string& unit )
{
CUnit ret;
unsigned int id;
int team;
int player;
char vis;
char dir;
unsigned int posx;
@ -83,14 +81,14 @@ CUnit CUnit::GetUnitFromString(const std::string& unit )
sscanf(unit.c_str(), UNIT_FORMATTER,
&id,
&team,
&player,
&vis,
&dir,
&posx,
&posy );
ret.unit_id = (unit_id_t)id;
ret.team_id = (Team)team;
ret.player_id = (Player) player;
ret.unit_vis = (unitvis_c)vis;
ret.dir = (dir_t)dir;
ret.pos = uvector2(posx,posy);
@ -101,7 +99,7 @@ CUnit CUnit::GetUnitFromString(const std::string& unit )
// Plain constructor
CUnit::CUnit()
: unit_id ( get_unique_unit_id() )
, team_id ( Team::NUM_INVALID )
, player_id ( Player::NUM_INVALID )
, unit_vis (unitvis_invalid)
, dir ( dir_t::S )
, pos ( { ucoord_invalid, ucoord_invalid } )
@ -112,7 +110,7 @@ CUnit::CUnit()
// 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) )
, pos ( std::move(unit.pos) )
@ -125,7 +123,7 @@ CUnit::CUnit(CUnit&& unit)
CUnit& CUnit::operator=(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) ;
pos = std::move(unit.pos) ;
@ -136,7 +134,7 @@ CUnit& CUnit::operator=(CUnit&& unit)
bool CUnit::operator==(const CUnit& rhs)
{
return (unit_id == rhs.unit_id)
&& (team_id == rhs.team_id)
&& (player_id == rhs.player_id)
&& (unit_vis == rhs.unit_vis)
&& (dir == rhs.dir)
&& (pos == rhs.pos);

View file

@ -7,7 +7,7 @@
#include "gametypes.h"
#include "vector2.h"
#define UNIT_FORMATTER "UNIT:%u tm:%u vs:%c dr:%c ps:[%u,%u]"
#define UNIT_FORMATTER "UNIT:%u pl:%u vs:%c dr:%c ps:[%u,%u]"
// force a reset of the unit ID value
void __forceResetCUnitID();
@ -36,14 +36,14 @@ public:
// Getters for all the members
inline const unit_id_t& GetID() const { return unit_id; }
inline const Team & GetTeam() const { return team_id; }
inline const unitvis_c & GetVisual() const { return unit_vis; }
inline const Player & GetPlayer() const { return player_id; }
inline const unitvis_c & GetVisual() const { return unit_vis; }
inline const dir_t& GetDir() const { return dir; }
inline const uvector2& GetPos() const { return pos; }
// Set
inline Team SetTeam(const Team &v) { return (team_id = v); }
inline unitvis_c SetVisual(const unitvis_c &v) { return ( unit_vis = v ); }
inline Player SetPlayer(const Player &v) { return (player_id = v); }
inline unitvis_c SetVisual(const unitvis_c &v) { return (unit_vis = v); }
inline dir_t SetDir(const dir_t &v) { return (dir = v); }
inline void SetPos(const uvector2 &v) { pos = v; }
@ -70,10 +70,10 @@ private:
unit_id_t unit_id;
// Visual
unitvis_c unit_vis;
unitvis_c unit_vis;
// Team ID
Team team_id;
// Player ID
Player player_id;
// Direction
dir_t dir;
@ -90,7 +90,7 @@ typedef std::vector< unit_id_t > CUnitIDVector;
inline bool CUnit::Valid() const
{
return (unit_id != unit_id_invalid )
&& (team_id != Team::NUM_INVALID )
&& (player_id != Player::NUM_INVALID )
&& (unit_vis != unitvis_invalid);
}