Correct case for all CUnit methods

This commit is contained in:
mdiluzio 2014-12-21 10:44:08 +00:00
parent c38fb3dd41
commit f55bc5413a
5 changed files with 96 additions and 96 deletions

View file

@ -80,7 +80,7 @@ int CTTRTSGame::IssueOrder( Team team, const SOrder & order )
// Get the right unit for the order // Get the right unit for the order
for ( SOrderUnitPair & pair : m_OrderUnitPairs ) for ( SOrderUnitPair & pair : m_OrderUnitPairs )
{ {
if ( pair.unit.getID() == order.unit ) if (pair.unit.GetID() == order.unit )
{ {
pair.order = order; pair.order = order;
return 0; return 0;
@ -113,10 +113,10 @@ uvector2 CTTRTSGame::GetNewPosition( const SOrderUnitPair & pair ) const
{ {
// For forward orders, grab in front // For forward orders, grab in front
case command_c::F: case command_c::F:
return pair.unit.getInFront(); return pair.unit.GetInFront();
// For all other orders, just grab the old position // For all other orders, just grab the old position
default: default:
return pair.unit.getPos(); return pair.unit.GetPos();
} }
} }
@ -145,7 +145,7 @@ int CTTRTSGame::SimulateToNextTurn()
for ( const SOrderUnitPair & pair2 : m_OrderUnitPairs ) for ( const SOrderUnitPair & pair2 : m_OrderUnitPairs )
{ {
// Skip myself // Skip myself
if( pair.unit.getID() == pair2.unit.getID() ) continue; if(pair.unit.GetID() == pair2.unit.GetID() ) continue;
if( GetNewPosition(pair2) == newpos ) if( GetNewPosition(pair2) == newpos )
{ {
@ -158,7 +158,7 @@ int CTTRTSGame::SimulateToNextTurn()
// If the movement is still possible // If the movement is still possible
if ( possible ) if ( possible )
{ {
pair.unit.setPos(newpos); pair.unit.SetPos(newpos);
} }
} }
break; break;
@ -175,13 +175,13 @@ int CTTRTSGame::SimulateToNextTurn()
case command_c::L: case command_c::L:
{ {
// Simply turn left // Simply turn left
pair.unit.turnLeft(); pair.unit.TurnLeft();
} }
break; break;
case command_c::R: case command_c::R:
{ {
// Simply turn right // Simply turn right
pair.unit.turnRight(); pair.unit.TurnRight();
} }
break; break;
default: default:
@ -200,11 +200,11 @@ int CTTRTSGame::SimulateToNextTurn()
{ {
if ( pair.order.command == command_c::A ) if ( pair.order.command == command_c::A )
{ {
uvector2 newpos = pair.unit.getInFront(); uvector2 newpos = pair.unit.GetInFront();
// If move would be within the arena // If move would be within the arena
if ( ( newpos.x <= dimensions.x-1 ) && ( newpos.y <= dimensions.y-1 ) ) if ( ( newpos.x <= dimensions.x-1 ) && ( newpos.y <= dimensions.y-1 ) )
{ {
pair.unit.setPos(newpos); pair.unit.SetPos(newpos);
// Unit moved, so more charging needs to be done // Unit moved, so more charging needs to be done
charging = true; charging = true;
@ -218,13 +218,13 @@ int CTTRTSGame::SimulateToNextTurn()
for ( SOrderUnitPair & pair1 : m_OrderUnitPairs ) for ( SOrderUnitPair & pair1 : m_OrderUnitPairs )
if ( pair1.order.command == command_c::A ) if ( pair1.order.command == command_c::A )
for ( SOrderUnitPair & pair2 : m_OrderUnitPairs ) for ( SOrderUnitPair & pair2 : m_OrderUnitPairs )
if ( pair1.unit.getID() != pair2.unit.getID() // Don't check the same units if (pair1.unit.GetID() != pair2.unit.GetID() // Don't check the same units
&& pair2.order.command == command_c::A ) && pair2.order.command == command_c::A )
{ {
if( CheckForPassThrough(pair1.unit,pair2.unit) ) if( CheckForPassThrough(pair1.unit,pair2.unit) )
{ {
toKill.push_back(pair1.unit.getID()); toKill.push_back(pair1.unit.GetID());
toKill.push_back(pair2.unit.getID()); toKill.push_back(pair2.unit.GetID());
} }
} }
@ -236,18 +236,18 @@ int CTTRTSGame::SimulateToNextTurn()
for ( SOrderUnitPair & pair1 : m_OrderUnitPairs ) for ( SOrderUnitPair & pair1 : m_OrderUnitPairs )
for ( SOrderUnitPair & pair2 : m_OrderUnitPairs ) for ( SOrderUnitPair & pair2 : m_OrderUnitPairs )
{ {
if( pair1.unit.getID() == pair2.unit.getID() ) continue; // Don't check the same units if(pair1.unit.GetID() == pair2.unit.GetID() ) continue; // Don't check the same units
if( pair1.unit.getPos() == pair2.unit.getPos() ) if(pair1.unit.GetPos() == pair2.unit.GetPos() )
{ {
if( pair1.order.command == command_c::A ) if( pair1.order.command == command_c::A )
{ {
toKill.push_back(pair2.unit.getID()); toKill.push_back(pair2.unit.GetID());
} }
if( pair2.order.command == command_c::A ) if( pair2.order.command == command_c::A )
{ {
toKill.push_back(pair1.unit.getID()); toKill.push_back(pair1.unit.GetID());
} }
} }
} }
@ -280,7 +280,7 @@ void CTTRTSGame::KillAll( std::vector< unit_id_t >& vec )
it != m_OrderUnitPairs.end(); it != m_OrderUnitPairs.end();
it++ ) it++ )
{ {
if( (*it).unit.getID() == id ) if((*it).unit.GetID() == id )
{ {
// Remove the unit from our alive unit pairs // Remove the unit from our alive unit pairs
m_OrderUnitPairs.erase(it); m_OrderUnitPairs.erase(it);
@ -294,10 +294,10 @@ void CTTRTSGame::KillAll( std::vector< unit_id_t >& vec )
// Check if two units passed through each other // Check if two units passed through each other
bool CTTRTSGame::CheckForPassThrough( const CUnit& one, const CUnit& two ) bool CTTRTSGame::CheckForPassThrough( const CUnit& one, const CUnit& two )
{ {
uvector2 pos1 = one.getPos(); uvector2 pos1 = one.GetPos();
uvector2 pos2 = two.getPos(); uvector2 pos2 = two.GetPos();
dir_t dir1 = one.getDir(); dir_t dir1 = one.GetDir();
dir_t dir2 = two.getDir(); dir_t dir2 = two.GetDir();
if( pos1.x == pos2.x ) { // Same col if( pos1.x == pos2.x ) { // Same col
if (pos1.y == (pos2.y + 1)) { if (pos1.y == (pos2.y + 1)) {
@ -327,18 +327,18 @@ bool CTTRTSGame::CheckForPassThrough( const CUnit& one, const CUnit& two )
int CTTRTSGame::AddUnit( CUnit&& unit ) int CTTRTSGame::AddUnit( CUnit&& unit )
{ {
// Verify the unit // Verify the unit
if( !unit.valid() ) if( !unit.Valid() )
return 1; return 1;
// Verify if the unit can be placed on the current board // Verify if the unit can be placed on the current board
const uvector2 pos = unit.getPos(); const uvector2 pos = unit.GetPos();
if( (pos.x >= dimensions.x) || (pos.y >= dimensions.y) ) if( (pos.x >= dimensions.x) || (pos.y >= dimensions.y) )
return 2; return 2;
// If any unit's position matches, reject this // If any unit's position matches, reject this
for ( const SOrderUnitPair & pair: m_OrderUnitPairs ) for ( const SOrderUnitPair & pair: m_OrderUnitPairs )
{ {
if( pair.unit.getPos() == unit.getPos() ) if(pair.unit.GetPos() == unit.GetPos() )
return 3; return 3;
} }
@ -377,8 +377,8 @@ int CTTRTSGame::VerifyOrder( Team team, const SOrder & order ) const
for ( const SOrderUnitPair & pair : m_OrderUnitPairs ) for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
{ {
// Accept if we have the unit // Accept if we have the unit
if ( pair.unit.getID() == unitID if (pair.unit.GetID() == unitID
&& pair.unit.getTeam() == team ) && pair.unit.GetTeam() == team )
{ {
ret = 0; ret = 0;
break; break;
@ -395,7 +395,7 @@ const CUnit& CTTRTSGame::GetUnitByIDConst( unit_id_t id ) const
for ( const SOrderUnitPair & pair : m_OrderUnitPairs ) for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
{ {
// Attempt the unit add // Attempt the unit add
if ( pair.unit.getID() == id ) if (pair.unit.GetID() == id )
return pair.unit; return pair.unit;
} }
@ -410,7 +410,7 @@ const SOrder & CTTRTSGame::GetOrderByIDConst( unit_id_t id ) const
for ( const SOrderUnitPair & pair : m_OrderUnitPairs ) for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
{ {
// Attempt the unit add // Attempt the unit add
if ( pair.unit.getID() == id ) if (pair.unit.GetID() == id )
return pair.order; return pair.order;
} }
@ -425,7 +425,7 @@ CUnit& CTTRTSGame::GetUnitByID( unit_id_t id )
for ( SOrderUnitPair & pair : m_OrderUnitPairs ) for ( SOrderUnitPair & pair : m_OrderUnitPairs )
{ {
// Attempt the unit add // Attempt the unit add
if ( pair.unit.getID() == id ) if (pair.unit.GetID() == id )
return pair.unit; return pair.unit;
} }
@ -443,7 +443,7 @@ std::vector<Team> CTTRTSGame::GetTeams() const
// Grab all teams // Grab all teams
for ( const SOrderUnitPair & pair : m_OrderUnitPairs ) for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
{ {
teams.push_back(pair.unit.getTeam()); teams.push_back(pair.unit.GetTeam());
} }
// Remove dupes // Remove dupes
@ -463,7 +463,7 @@ Team CTTRTSGame::CheckForWin() const
// Count up all the units for each Team // Count up all the units for each Team
for ( const SOrderUnitPair & pair : m_OrderUnitPairs ) for ( const SOrderUnitPair & pair : m_OrderUnitPairs )
{ {
const int team = (int)pair.unit.getTeam(); const int team = (int) pair.unit.GetTeam();
units[team] += 1; units[team] += 1;
} }

View file

@ -36,7 +36,7 @@ namespace
} }
// force a reset of the unit ID value // force a reset of the unit ID value
void forceResetUnitId() void __forceResetCUnitID()
{ {
unit_id_t i = 0; unit_id_t i = 0;
get_unique_unit_id(&i); get_unique_unit_id(&i);
@ -46,7 +46,7 @@ void forceResetUnitId()
CUnit CUnit::GetUnitFromVis( unitVis_c vis ) CUnit CUnit::GetUnitFromVis( unitVis_c vis )
{ {
CUnit unit; CUnit unit;
unit.setFromVisual(vis); unit.SetFromVisual(vis);
return unit; return unit;
} }
@ -106,7 +106,7 @@ CUnit::CUnit()
, dir ( dir_t::S ) , dir ( dir_t::S )
, pos ( { ucoord_invalid, ucoord_invalid } ) , pos ( { ucoord_invalid, ucoord_invalid } )
{ {
updateMyVisual(); UpdateMyVisual();
} }
// Move constructor // Move constructor
@ -117,7 +117,7 @@ CUnit::CUnit(CUnit&& unit)
, dir ( std::move(unit.dir) ) , dir ( std::move(unit.dir) )
, pos ( std::move(unit.pos) ) , pos ( std::move(unit.pos) )
{ {
updateMyVisual(); UpdateMyVisual();
} }
@ -143,22 +143,22 @@ bool CUnit::operator==(const CUnit& rhs)
} }
// Update the visual representation of the unit // Update the visual representation of the unit
unitVis_c CUnit::updateMyVisual() unitVis_c CUnit::UpdateMyVisual()
{ {
// Start at invalid // Start at invalid
setVisual(unitVis_invalid); SetVisual(unitVis_invalid);
dir_to_vis_map::const_iterator it = get_vis_map_V().find(dir); dir_to_vis_map::const_iterator it = get_vis_map_V().find(dir);
// If found set to new vis // If found set to new vis
if( it != get_vis_map_V().end() ) if( it != get_vis_map_V().end() )
setVisual(it->second); SetVisual(it->second);
return getVisual(); return GetVisual();
} }
// Set the unit from visual // 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;
@ -167,7 +167,7 @@ bool CUnit::setFromVisual( const unitVis_c& vis )
if( it->second == vis ) if( it->second == vis )
{ {
dir = it->first; dir = it->first;
updateMyVisual(); UpdateMyVisual();
return true; return true;
} }
} }
@ -177,7 +177,7 @@ bool CUnit::setFromVisual( const unitVis_c& vis )
} }
// Turn unit left // Turn unit left
dir_t CUnit::turnLeft() dir_t CUnit::TurnLeft()
{ {
switch( dir ) switch( dir )
{ {
@ -198,13 +198,13 @@ dir_t CUnit::turnLeft()
break; break;
} }
updateMyVisual(); UpdateMyVisual();
return getDir(); return GetDir();
} }
// Turn unit right // Turn unit right
dir_t CUnit::turnRight() dir_t CUnit::TurnRight()
{ {
switch( dir ) switch( dir )
{ {
@ -225,13 +225,13 @@ dir_t CUnit::turnRight()
break; break;
} }
updateMyVisual(); UpdateMyVisual();
return getDir(); return GetDir();
} }
// Turn unit around // Turn unit around
dir_t CUnit::turnAround() dir_t CUnit::TurnAround()
{ {
switch( dir ) switch( dir )
{ {
@ -252,13 +252,13 @@ dir_t CUnit::turnAround()
break; break;
} }
updateMyVisual(); UpdateMyVisual();
return getDir(); return GetDir();
} }
// Get the co-ordinate infront of the unit // Get the co-ordinate infront of the unit
uvector2 CUnit::getInFront() const uvector2 CUnit::GetInFront() const
{ {
vector2 delta = vecFromDir(dir); vector2 delta = vecFromDir(dir);
return pos + delta; return pos + delta;

View file

@ -10,7 +10,7 @@
#define UNIT_FORMATTER "UNIT:%u tm:%u vs:%c dr:%c ps:[%u,%u]" #define UNIT_FORMATTER "UNIT:%u tm:%u vs:%c dr:%c ps:[%u,%u]"
// force a reset of the unit ID value // force a reset of the unit ID value
void forceResetUnitId(); void __forceResetCUnitID();
// Base unit type // Base unit type
class CUnit class CUnit
@ -35,36 +35,36 @@ public:
bool operator!=(const CUnit& rhs) { return !(*this == rhs); } bool operator!=(const CUnit& rhs) { return !(*this == rhs); }
// Getters for all the members // 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 & getTeam() const { return team_id; } inline const Team & GetTeam() const { return team_id; }
inline const unitVis_c& getVisual() const { return unit_vis; } inline const unitVis_c& GetVisual() const { return unit_vis; }
inline const dir_t& getDir() const { return dir; } inline const dir_t& GetDir() const { return dir; }
inline const uvector2& getPos() const { return pos; } inline const uvector2& GetPos() const { return pos; }
// Set // Set
inline Team setTeam(const Team & v) { return (team_id = v); } inline Team SetTeam(const Team &v) { return (team_id = v); }
inline unitVis_c setVisual(const unitVis_c& v) { return ( unit_vis = 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 dir_t SetDir(const dir_t &v) { return (dir = v); }
inline void setPos(const uvector2& v) { pos = v; } inline void SetPos(const uvector2 &v) { pos = v; }
// Get the co-ordinate in front of the unit // Get the co-ordinate in front of the unit
uvector2 getInFront() const; uvector2 GetInFront() const;
// Check unit is valid // Check unit is valid
inline bool valid() const; inline bool Valid() const;
// Set a unit based solely on it's visual // Set a unit based solely on it's visual
bool setFromVisual( const unitVis_c& vis); bool SetFromVisual(const unitVis_c &vis);
// Orientation methods // Orientation methods
dir_t turnLeft(); dir_t TurnLeft();
dir_t turnRight(); dir_t TurnRight();
dir_t turnAround(); dir_t TurnAround();
private: private:
// Update my visual must be called when setting direction // 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;
@ -83,11 +83,11 @@ private:
}; };
// Typedef for a vector of units // Typedef for a vector of units
typedef std::vector< CUnit > CUnitVector; typedef std::vector< CUnit > CUnitVector;
typedef std::vector< unit_id_t > CUnitIDVector; typedef std::vector< unit_id_t > CUnitIDVector;
// Simple validation // Simple validation
inline bool CUnit::valid() const inline bool CUnit::Valid() const
{ {
return (unit_id != unit_id_invalid ) return (unit_id != unit_id_invalid )
&& (team_id != Team::NUM_INVALID ) && (team_id != Team::NUM_INVALID )

View file

@ -6,8 +6,8 @@
void AddUnitToGame( Team team, char vis, uvector2 vec, CTTRTSGame& game ) void AddUnitToGame( Team team, char vis, uvector2 vec, CTTRTSGame& game )
{ {
CUnit unit = CUnit::GetUnitFromVis(vis); CUnit unit = CUnit::GetUnitFromVis(vis);
unit.setPos( vec ); unit.SetPos(vec);
unit.setTeam(team); unit.SetTeam(team);
game.AddUnit(std::move(unit)); game.AddUnit(std::move(unit));
} }
@ -18,7 +18,7 @@ void OutputGame( CTTRTSGame&& game )
output << game.GetStateAsString(); output << game.GetStateAsString();
output.close(); output.close();
forceResetUnitId(); __forceResetCUnitID();
} }
int main() int main()

View file

@ -8,8 +8,8 @@ const char* tests()
// Test if we can properly set a unit's visual // Test if we can properly set a unit's visual
{ {
CUnit unit; CUnit unit;
unit.setFromVisual('v'); unit.SetFromVisual('v');
if( unit.getVisual() != 'v' ) if(unit.GetVisual() != 'v' )
return "failed to properly create V unit"; return "failed to properly create V unit";
} }
@ -17,7 +17,7 @@ const char* tests()
{ {
CUnit unit; CUnit unit;
CUnit unit2; CUnit unit2;
if( unit.getID() == unit2.getID() ) if(unit.GetID() == unit2.GetID() )
return "Unit IDs the same"; return "Unit IDs the same";
} }
@ -35,9 +35,9 @@ const char* tests()
// Test custom unit conversion // Test custom unit conversion
{ {
CUnit unit1; CUnit unit1;
unit1.setFromVisual('v'); unit1.SetFromVisual('v');
unit1.setTeam(Team::Green); unit1.SetTeam(Team::Green);
unit1.setPos( uvector2(5,10) ); unit1.SetPos(uvector2(5, 10));
std::string unit1Desc = CUnit::GetStringFromUnit(unit1); std::string unit1Desc = CUnit::GetStringFromUnit(unit1);
CUnit unit2 = CUnit::GetUnitFromString(unit1Desc); CUnit unit2 = CUnit::GetUnitFromString(unit1Desc);
@ -49,7 +49,7 @@ const char* tests()
// Test if we can successfully create a unit from a visual // Test if we can successfully create a unit from a visual
{ {
CUnit unit = CUnit::GetUnitFromVis('v'); CUnit unit = CUnit::GetUnitFromVis('v');
if( unit.getVisual() != 'v' ) if(unit.GetVisual() != 'v' )
return "failed to properly create V unit with factory"; return "failed to properly create V unit with factory";
} }
@ -81,16 +81,16 @@ const char* tests()
{ {
CUnit unit = CUnit::GetUnitFromVis('^'); CUnit unit = CUnit::GetUnitFromVis('^');
unit.setPos( {2,2} ); unit.SetPos({2, 2});
unit.setTeam(Team::Red); unit.SetTeam(Team::Red);
game.AddUnit(std::move(unit)); game.AddUnit(std::move(unit));
} }
{ {
CUnit unit = CUnit::GetUnitFromVis('^'); CUnit unit = CUnit::GetUnitFromVis('^');
unit.setPos( {2,2} ); unit.SetPos({2, 2});
unit.setTeam(Team::Red); unit.SetTeam(Team::Red);
if( !game.AddUnit(std::move(unit)) ) if( !game.AddUnit(std::move(unit)) )
return "Game should have rejected unit placed on the same spot"; return "Game should have rejected unit placed on the same spot";
@ -105,11 +105,11 @@ const char* tests()
CTTRTSGame game( 5, 5 ); CTTRTSGame game( 5, 5 );
CUnit unit = CUnit::GetUnitFromVis('>'); CUnit unit = CUnit::GetUnitFromVis('>');
const unit_id_t id = unit.getID(); const unit_id_t id = unit.GetID();
SOrder order; SOrder order;
unit.setPos( {2,2} ); unit.SetPos({2, 2});
unit.setTeam(Team::Red); unit.SetTeam(Team::Red);
if ( game.AddUnit(std::move(unit)) ) if ( game.AddUnit(std::move(unit)) )
return "Game failed to add valid unit"; return "Game failed to add valid unit";
@ -123,7 +123,7 @@ const char* tests()
if (game.SimulateToNextTurn() ) if (game.SimulateToNextTurn() )
return "Game failed to simulate valid turn"; return "Game failed to simulate valid turn";
if( game.GetUnitByIDConst(id).getPos() != uvector2{3,2} ) if(game.GetUnitByIDConst(id).GetPos() != uvector2{3,2} )
return "Simple movement order failed"; return "Simple movement order failed";
} }
@ -136,11 +136,11 @@ const char* tests()
unit_id_t id; unit_id_t id;
{ {
CUnit unit = CUnit::GetUnitFromVis('>'); CUnit unit = CUnit::GetUnitFromVis('>');
id = unit.getID(); id = unit.GetID();
SOrder order; SOrder order;
unit.setPos( {0,0} ); unit.SetPos({0, 0});
unit.setTeam(Team::Blue); unit.SetTeam(Team::Blue);
if ( game.AddUnit(std::move(unit)) ) if ( game.AddUnit(std::move(unit)) )
return "Game failed to add valid unit"; return "Game failed to add valid unit";
@ -154,8 +154,8 @@ const char* tests()
{ {
CUnit unit = CUnit::GetUnitFromVis('<'); CUnit unit = CUnit::GetUnitFromVis('<');
unit.setPos( {1,0} ); unit.SetPos({1, 0});
unit.setTeam(Team::Red); unit.SetTeam(Team::Red);
if ( game.AddUnit(std::move(unit)) ) if ( game.AddUnit(std::move(unit)) )
return "Game failed to add valid unit"; return "Game failed to add valid unit";
@ -166,10 +166,10 @@ const char* tests()
if ( game.GetNumUnits() != 1 ) if ( game.GetNumUnits() != 1 )
return "Game failed to kill a unit when it logically should have"; return "Game failed to kill a unit when it logically should have";
if ( game.GetUnitByIndex(0).getDir() != dir_t::E ) if (game.GetUnitByIndex(0).GetDir() != dir_t::E )
return "Game killed the wrong unit"; return "Game killed the wrong unit";
if ( game.GetUnitByIndex(0).getID() != id ) if (game.GetUnitByIndex(0).GetID() != id )
return "Game killed the wrong unit"; return "Game killed the wrong unit";
if ( game.CheckForWin() != Team::Blue ) if ( game.CheckForWin() != Team::Blue )