Use snprintf and sscanf instead of std::string tomfoolery
Rename order_c to command_c for better readability
This commit is contained in:
parent
b77411121a
commit
8a486fdd40
6 changed files with 71 additions and 127 deletions
128
game/unit.cpp
128
game/unit.cpp
|
@ -1,3 +1,4 @@
|
|||
#include <string.h>
|
||||
#include "unit.h"
|
||||
|
||||
#include <map> // for std::map
|
||||
|
@ -38,108 +39,53 @@ CUnit GetUnitFromVis( unitVis_c vis )
|
|||
}
|
||||
|
||||
// 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]"
|
||||
// "U id:[unit_id] team:[team_id] player:[player_id] vis:[unit_vis] dir:[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);
|
||||
static char buff[128];
|
||||
memset(buff,0,sizeof(buff));
|
||||
|
||||
return ret;
|
||||
snprintf(buff,128, UNIT_FORMATTER,
|
||||
unit.getID(),
|
||||
(int)unit.getTeam(),
|
||||
unit.getPlayer(),
|
||||
unit.getVisual(),
|
||||
unit.getDir(),
|
||||
unit.getPos().x,
|
||||
unit.getPos().y );
|
||||
|
||||
return buff;
|
||||
}
|
||||
|
||||
// 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 )
|
||||
// "U id:[unit_id] team:[team_id] player:[player_id] vis:[unit_vis] dir:[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
|
||||
unsigned int id;
|
||||
int team;
|
||||
unsigned int player;
|
||||
char vis;
|
||||
char dir;
|
||||
unsigned int posx;
|
||||
unsigned int posy;
|
||||
|
||||
pos = unit.find(' ');
|
||||
if( pos == std::string::npos )
|
||||
return CUnit();
|
||||
sscanf(unit.c_str(), UNIT_FORMATTER,
|
||||
&id,
|
||||
&team,
|
||||
&player,
|
||||
&vis,
|
||||
&dir,
|
||||
&posx,
|
||||
&posy );
|
||||
|
||||
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
|
||||
ret.setIDForced((unit_id_t)id);
|
||||
ret.setTeam((Team)team);
|
||||
ret.setPlayer((player_id_t)player);
|
||||
ret.setVisual((unitVis_c)vis);
|
||||
ret.setDir((dir_t)dir);
|
||||
ret.setPos(uvector2(posx,posy));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue