Implement conversion of Unit to String and back
This commit is contained in:
parent
7a75fbd369
commit
b77411121a
6 changed files with 188 additions and 31 deletions
128
game/unit.cpp
128
game/unit.cpp
|
@ -29,6 +29,121 @@ namespace
|
|||
}
|
||||
}
|
||||
|
||||
// Get a unit from a visual
|
||||
CUnit GetUnitFromVis( unitVis_c vis )
|
||||
{
|
||||
CUnit unit;
|
||||
unit.setFromVisual(vis);
|
||||
return unit;
|
||||
}
|
||||
|
||||
// Get a string descriptor of a unit
|
||||
// "U:[unit_id] T:[team_id] P:[player_id] V:[unit_vis] D:[dir] POS:[pos.x],[pos.y]"
|
||||
std::string GetStringFromUnit(const CUnit& unit )
|
||||
{
|
||||
std::string ret;
|
||||
ret += "U:";
|
||||
ret += std::to_string(unit.getID());
|
||||
ret += " T:";
|
||||
ret += std::to_string((int)unit.getTeam());
|
||||
ret += " P:";
|
||||
ret += std::to_string(unit.getPlayer());
|
||||
ret += " V:";
|
||||
ret += (char)unit.getVisual();
|
||||
ret += " D:";
|
||||
ret += (char)unit.getDir();
|
||||
ret += " POS:";
|
||||
ret += std::to_string(unit.getPos().x);
|
||||
ret += ",";
|
||||
ret += std::to_string(unit.getPos().y);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Get a unit from a string descriptor
|
||||
// "U:[unit_id] T:[team_id] P:[player_id] V:[unit_vis] D:[dir] POS:[pos.x],[pos.y]"
|
||||
CUnit GetUnitFromString(const std::string& _unit )
|
||||
{
|
||||
std::string unit = _unit;
|
||||
CUnit ret;
|
||||
size_t pos;
|
||||
|
||||
// UNIT ID
|
||||
if( unit.substr(0, 2) != "U:" )
|
||||
return CUnit();
|
||||
unit.erase(0, 2); // Erase the U: prefix
|
||||
|
||||
pos = unit.find(' ');
|
||||
if( pos == std::string::npos )
|
||||
return CUnit();
|
||||
|
||||
const unit_id_t id = (unit_id_t)atoi( unit.substr(0,pos).c_str() ); // Grab the ID
|
||||
ret.setIDForced( id ); // Force set the ID
|
||||
unit.erase(0, pos+1); // Erase the ID and a space
|
||||
|
||||
// TEAM
|
||||
if( unit.substr(0, 2) != "T:" )
|
||||
return CUnit();
|
||||
unit.erase(0, 2); // Erase the T: prefix
|
||||
|
||||
pos = unit.find(' ');
|
||||
if( pos == std::string::npos )
|
||||
return CUnit();
|
||||
|
||||
const Team team = (Team)atoi( unit.substr(0,pos).c_str() ); // Grab the ID
|
||||
ret.setTeam( team ); // Force set the ID
|
||||
unit.erase(0, pos+1); // Erase the team and a space
|
||||
|
||||
// PLAYER
|
||||
if( unit.substr(0, 2) != "P:" )
|
||||
return CUnit();
|
||||
unit.erase(0, 2); // Erase the P: prefix
|
||||
|
||||
pos = unit.find(' ');
|
||||
if( pos == std::string::npos )
|
||||
return CUnit();
|
||||
|
||||
const player_id_t player = (player_id_t)atoi( unit.substr(0,pos).c_str() ); // Grab the ID
|
||||
ret.setPlayer( player ); // Force set the ID
|
||||
unit.erase(0, pos+1); // Erase the player and a space
|
||||
|
||||
// VISUAL
|
||||
if( unit.substr(0, 2) != "V:" )
|
||||
return CUnit();
|
||||
unit.erase(0, 2); // Erase the W: prefix
|
||||
|
||||
unitVis_c vis = unit[0];
|
||||
ret.setVisual(vis);
|
||||
unit.erase(0, 2); // Erase the vis and a space
|
||||
|
||||
// DIRECTION
|
||||
if( unit.substr(0, 2) != "D:" )
|
||||
return CUnit();
|
||||
unit.erase(0, 2); // Erase the D: prefix
|
||||
|
||||
dir_t dir = (dir_t)unit[0];
|
||||
ret.setDir(dir);
|
||||
unit.erase(0, 2); // Erase the ID and a space
|
||||
|
||||
// POSITION
|
||||
if( unit.substr(0, 4) != "POS:" )
|
||||
return CUnit();
|
||||
unit.erase(0, 4); // Erase the Pos: prefix
|
||||
|
||||
pos = unit.find(',');
|
||||
if( pos == std::string::npos )
|
||||
return CUnit();
|
||||
|
||||
uvector2 upos;
|
||||
upos.x = (ucoord_t)atoi( unit.substr(0,pos).c_str() ); // Grab the x
|
||||
unit.erase(0, pos+1); // Erase the x and the comma
|
||||
|
||||
upos.y = (ucoord_t)atoi( unit.c_str() ); // Grab the y from what's left
|
||||
ret.setPos( upos ); //Set the position
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Plain constructor
|
||||
CUnit::CUnit()
|
||||
: unit_id ( get_unique_unit_id() )
|
||||
|
@ -66,12 +181,15 @@ CUnit& CUnit::operator=(CUnit&& unit)
|
|||
return *this;
|
||||
}
|
||||
|
||||
// Get a unit from a visual
|
||||
CUnit CUnit::getUnitFromVis( unitVis_c vis )
|
||||
// Equals operator
|
||||
bool CUnit::operator==(const CUnit& rhs)
|
||||
{
|
||||
CUnit unit;
|
||||
unit.setFromVisual(vis);
|
||||
return unit;
|
||||
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);
|
||||
}
|
||||
|
||||
// Update the visual representation of the unit
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue