encapsulate constants for EC

This commit is contained in:
suhrke 2017-07-05 16:00:22 -07:00
parent dbcd7c59d8
commit ff47d28870
3 changed files with 138 additions and 128 deletions

View File

@ -24,26 +24,13 @@
#include <iterator>
#include <sstream>
// shifts the position of an entity in a given axis.
// for discrepancies between origin points between games.
#define OFFSET_PLAYER 32 // in reflex is origin the feet; in quake it's the center
#define OFFSET_PICKUP 2 // reflex items are on-ground, while quake's are floating
std::string offset(std::string axis, float amount) {
std::istringstream iss(axis);
float c;
iss >> c;
c += amount;
std::stringstream ss;
ss << std::fixed << std::setprecision(5) << c;
return ss.str();
}
/*-----------------------------------------------------------------------------
* PUBLIC
*-----------------------------------------------------------------------------*/
EntityConverter::EntityConverter(std::string entityMapFile)
EntityConverter::EntityConverter(std::string entityMapFile) : OFFSET_PLAYER(32.0), OFFSET_PICKUP(2.0)
{
//MUST RUN matchRelated method after this constructor
areEntitiesMatched_ = false;
@ -52,7 +39,7 @@ EntityConverter::EntityConverter(std::string entityMapFile)
EntityConverter::EntityConverter(std::string entityMapFile, std::string reflexMapFile)
EntityConverter::EntityConverter(std::string entityMapFile, std::string reflexMapFile) : OFFSET_PLAYER(32.0), OFFSET_PICKUP(2.0)
{
mapEntities(entityMapFile);
@ -169,79 +156,8 @@ EntityConverter::convert(std::vector<std::string> lines)
/*-----------------------------------------------------------------------------
* PRIVATE
* PROTECTED
*-----------------------------------------------------------------------------*/
std::string
EntityConverter::getAttributeType(std::string line)
{
std::string type;
std::string dataType;
std::istringstream iss(line);
if ( ! (iss >> dataType >> type )) {
return std::string();
}
return type;
}
void
EntityConverter::mapEntities(std::string mapFile)
{
std::ifstream fin;
fin.open(mapFile);
if ( fin.is_open() ) {
//Read .ent contents into pickup map
std::string line;
while (std::getline(fin, line)) {
std::istringstream iss(line);
// Reflex ID corresponds to xonotic pickup name
int id;
std::string pickup;
if ( ! (iss >> id >> pickup)) {
throw std::runtime_error( "format error in .ent file" );
}
pickupMapping_.insert ( std::pair<int, std::string>(id, pickup) );
}
}
else {
throw std::ios::failure( "Error: EntityConverter failed to open .ent file" );
}
fin.close();
}
void
EntityConverter::addIfRelated(std::string &line, std::istream &is)
{
std::string trash;
std::string targetName;
if ( line.find("type Teleporter") != std::string::npos) {
std::getline(is, line);
std::istringstream iss(line);
if ( ! (iss >> trash >> trash >> targetName)) {
throw std::runtime_error( "format error in .map file");
}
targetMap_.insert ( std::pair<std::string, std::string>(targetName, "Teleporter") );
}
else if ( line.find("type JumpPad") != std::string::npos) {
std::getline(is, line);
std::istringstream iss(line);
if ( ! (iss >> trash >> trash >> targetName)) {
throw std::runtime_error( "format error in .map file");
}
targetMap_.insert ( std::pair<std::string, std::string>(targetName, "JumpPad") );
}
}
std::vector<std::string>
EntityConverter::convertPickup(std::vector<std::string> &lines)
{
@ -552,6 +468,96 @@ EntityConverter::convertRaceFinish(std::vector<std::string> &lines)
return convertedLines;
}
/*-----------------------------------------------------------------------------
* PRIVATE
*-----------------------------------------------------------------------------*/
std::string
EntityConverter::getAttributeType(std::string line)
{
std::string type;
std::string dataType;
std::istringstream iss(line);
if ( ! (iss >> dataType >> type )) {
return std::string();
}
return type;
}
void
EntityConverter::mapEntities(std::string mapFile)
{
std::ifstream fin;
fin.open(mapFile);
if ( fin.is_open() ) {
//Read .ent contents into pickup map
std::string line;
while (std::getline(fin, line)) {
std::istringstream iss(line);
// Reflex ID corresponds to xonotic pickup name
int id;
std::string pickup;
if ( ! (iss >> id >> pickup)) {
throw std::runtime_error( "format error in .ent file" );
}
pickupMapping_.insert ( std::pair<int, std::string>(id, pickup) );
}
}
else {
throw std::ios::failure( "Error: EntityConverter failed to open .ent file" );
}
fin.close();
}
void
EntityConverter::addIfRelated(std::string &line, std::istream &is)
{
std::string trash;
std::string targetName;
if ( line.find("type Teleporter") != std::string::npos) {
std::getline(is, line);
std::istringstream iss(line);
if ( ! (iss >> trash >> trash >> targetName)) {
throw std::runtime_error( "format error in .map file");
}
targetMap_.insert ( std::pair<std::string, std::string>(targetName, "Teleporter") );
}
else if ( line.find("type JumpPad") != std::string::npos) {
std::getline(is, line);
std::istringstream iss(line);
if ( ! (iss >> trash >> trash >> targetName)) {
throw std::runtime_error( "format error in .map file");
}
targetMap_.insert ( std::pair<std::string, std::string>(targetName, "JumpPad") );
}
}
std::string EntityConverter::offset(std::string value, float amount) {
std::istringstream iss(value);
float c;
iss >> c;
c += amount;
std::stringstream ss;
ss << std::fixed << std::setprecision(5) << c;
return ss.str();
}
// DEBUG
void
EntityConverter::printMapping()

View File

@ -31,15 +31,9 @@
#include <queue>
#include <vector>
/*
*--------------------------------------------------------------------------------------
* Description: Reflex coordinates place entities at the ground and Xonotic entities
* are at about center of player height. Offset accordingly.
* Paramater: string coordinate, float value passed as string
* Return: string, float value passed as string
*--------------------------------------------------------------------------------------
*/
std::string offset(std::string coordinate);
class EntityConverter
{
@ -97,18 +91,38 @@ class EntityConverter
protected:
private:
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: getOffsetHeight
* Description: Returns a constant
* Method: EntityConverter :: convert~EntityName~
* Description: Multiple methods to convert entity from reflex to xonotic format
* Parameter: vector of strings entity, multi-lined entity
* Return: vector of strings, the converted entity
*--------------------------------------------------------------------------------------
*/
float getHeightOffset() { return 32.0; }
std::vector<std::string> convertPickup(std::vector<std::string> &entity);
std::vector<std::string> convertPlayerSpawn(std::vector<std::string> &entity);
std::vector<std::string> convertJumpPad(std::vector<std::string> &entity);
std::vector<std::string> convertTeleporter(std::vector<std::string> &entity);
std::vector<std::string> convertTarget(std::vector<std::string> &entity);
std::vector<std::string> convertRaceStart(std::vector<std::string> &entity);
std::vector<std::string> convertRaceFinish(std::vector<std::string> &entity);
// Related entities must be matched prior to entity conversion
bool areEntitiesMatched_;
// Map Reflex pickup IDs to Xonotic pickup identifiers
std::map<int, std::string> pickupMapping_;
// Map targets (by name) to their source type
std::map<std::string, std::string> targetMap_;
// Offsets for item/spawn height
const float OFFSET_PLAYER;
const float OFFSET_PICKUP;
private:
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
@ -139,34 +153,24 @@ class EntityConverter
*--------------------------------------------------------------------------------------
*/
void addIfRelated(std::string &line, std::istream &is);
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: convert~EntityName~
* Description: Multiple methods to convert entity from reflex to xonotic format
* Parameter: vector of strings entity, multi-lined entity
* Return: vector of strings, the converted entity
* Method: EntityConverter :: offset
* Description: Reflex coordinates place entities at the ground and Xonotic entities
* are at about center of player height. Offset accordingly.
* Paramater: string value, float value passed as string
* Parameter: float offset, amount to add to value
* Return: string, float value passed as string
*--------------------------------------------------------------------------------------
*/
std::vector<std::string> convertPickup(std::vector<std::string> &entity);
std::vector<std::string> convertPlayerSpawn(std::vector<std::string> &entity);
std::vector<std::string> convertJumpPad(std::vector<std::string> &entity);
std::vector<std::string> convertTeleporter(std::vector<std::string> &entity);
std::vector<std::string> convertTarget(std::vector<std::string> &entity);
std::vector<std::string> convertRaceStart(std::vector<std::string> &entity);
std::vector<std::string> convertRaceFinish(std::vector<std::string> &entity);
std::string offset(std::string value, float offset);
void printMapping(); //DEBUG
void printTargetSources(); //DEBUG
// Related entities must be matched prior to entity conversion
bool areEntitiesMatched_;
// Map Reflex pickup IDs to Xonotic pickup identifiers
std::map<int, std::string> pickupMapping_;
// Map targets (by name) to their source type
std::map<std::string, std::string> targetMap_;
};
#endif //ENTITY_CONVERTER_HPP

View File

@ -78,7 +78,7 @@ TEST_CASE( "r2x: a single Pickup entity can be converted", "[EntityConverter]" )
REQUIRE( converted[0] == "\"classname\" \"weapon_grenadelauncher\"\n" );
// The z (vertical) is offset
// The z (vertical) is offset by +2
std::istringstream iss(converted[1]);
std::string attribute;
std::string coords[2];
@ -88,7 +88,7 @@ TEST_CASE( "r2x: a single Pickup entity can be converted", "[EntityConverter]" )
REQUIRE( attribute == "\"origin\"" );
REQUIRE( coords[0] == "\"-216.00000" );
REQUIRE( coords[1] == "-1488.000488" );
REQUIRE( fabs(-100.00000 - offsetCoord) <= DELTA );
REQUIRE( fabs(-130.00000 - offsetCoord) <= DELTA );
}
@ -119,7 +119,7 @@ TEST_CASE( "r2x: a single PlayerSpawn (teamA) entity can be converted", "[Entity
REQUIRE( converted[0] == "\"classname\" \"info_player_team1\"\n" );
REQUIRE( converted[2] == "\"angle\" \"180.00000\"\n" );
// The z (vertical) is offset
// The z (vertical) is offset by +32
std::istringstream iss(converted[1]);
std::string attribute;
std::string coords[2];
@ -219,7 +219,7 @@ TEST_CASE( "r2x: a single Teleporter and related Target can be converted", "[Ent
REQUIRE( converted2[0] == "\"classname\" \"misc_teleporter_dest\"\n" );
REQUIRE( converted2[2] == "\"targetname\" \"tp1\"\n" );
//
// The z (vertical) is offset
// The z (vertical) is offset by +32
std::istringstream iss(converted2[1]);
std::string attribute;
std::string coords[2];