From 98abd5c184f1a191d1a1a1479945a272cf5a8441 Mon Sep 17 00:00:00 2001 From: suhrke Date: Wed, 7 Jun 2017 01:46:43 -0700 Subject: [PATCH] EntityConverter Target handled by pre-scan --- ReflexToQ3/includes/EntityConverter.cpp | 95 +++++++++++++++++++------ ReflexToQ3/includes/EntityConverter.hpp | 11 ++- ReflexToQ3/includes/oopless-parser.cpp | 1 + 3 files changed, 82 insertions(+), 25 deletions(-) diff --git a/ReflexToQ3/includes/EntityConverter.cpp b/ReflexToQ3/includes/EntityConverter.cpp index 4e28b22..4fb38ee 100644 --- a/ReflexToQ3/includes/EntityConverter.cpp +++ b/ReflexToQ3/includes/EntityConverter.cpp @@ -24,28 +24,68 @@ -EntityConverter::EntityConverter(std::string entityMapFile) +EntityConverter::EntityConverter(std::string entityMapFile, std::string reflexMapFile) { //Open .ent mapping file - std::ifstream fin; - fin.open(entityMapFile); + std::ifstream entFin; + entFin.open(entityMapFile); - if ( ! fin.is_open() ) { - throw std::ios::failure( "Error opening .ent file" ); - } - - //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" ); + if ( entFin.is_open() ) { + //Read .ent contents into pickup map + std::string line; + while (std::getline(entFin, 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(id, pickup) ); } - pickupMapping.insert ( std::pair(id, pickup) ); } + else { + throw std::ios::failure( "Error: EntityConverter failed to open .ent file" ); + } + entFin.close(); + + + //Open .map file + std::ifstream mapFin; + mapFin.open(reflexMapFile); + + if ( mapFin.is_open() ) { + //Extract the source type of targets (teleporters or jump pads) + std::string line; + std::string trash; + std::string targetName; + while (std::getline(mapFin, line)) { + if ( line.find("type Teleporter") != std::string::npos) { + std::getline(mapFin, line); + std::istringstream iss(line); + if ( ! (iss >> trash >> trash >> targetName)) { + throw std::runtime_error( "format error in .map file"); + } + targetMap_.insert ( std::pair(targetName, "Teleporter") ); + } + else if ( line.find("type JumpPad") != std::string::npos) { + std::getline(mapFin, line); + std::istringstream iss(line); + if ( ! (iss >> trash >> trash >> targetName)) { + throw std::runtime_error( "format error in .map file"); + } + targetMap_.insert ( std::pair(targetName, "JumpPad") ); + } + } + } + else { + throw std::ios::failure( "Error: EntityConverter failed to open .map file" ); + } + mapFin.close(); + + //DEBUG + //printMapping(); + //printTargetSources(); } @@ -53,10 +93,17 @@ EntityConverter::EntityConverter(std::string entityMapFile) void EntityConverter::printMapping() { std::map::iterator it; - for (it=pickupMapping.begin(); it!=pickupMapping.end(); ++it) + for (it=pickupMapping_.begin(); it!=pickupMapping_.end(); ++it) std::cout << it->first << " => " << it->second << std::endl; } +// DEBUG +void EntityConverter::printTargetSources() +{ + std::map::iterator it; + for (it=targetMap_.begin(); it!=targetMap_.end(); ++it) + std::cout << it->first << " => " << it->second << std::endl; +} std::string EntityConverter::getAttributeType(std::string line) @@ -127,7 +174,7 @@ std::vector EntityConverter::convert(std::vector lines if ( havePosition && havePickupID ) { std::stringstream oss; - oss << "\"classname\" \"" << pickupMapping[pickupID] << "\"" << std::endl; + oss << "\"classname\" \"" << pickupMapping_[pickupID] << "\"" << std::endl; convertedLines.push_back ( oss.str() ); // coordinates reordered to x, z, y std::stringstream oss2; @@ -157,7 +204,7 @@ std::vector EntityConverter::convert(std::vector lines convertedLines.push_back ( oss.str() ); } else if ( type == "Teleporter" ) { ///TELEPORTER - /* if ( lines.size() < 2 ) { + if ( lines.size() < 2 ) { throw std::runtime_error("error: Teleport entity requires at least 2 lines"); } std::istringstream iss2(lines[1]); @@ -215,7 +262,12 @@ std::vector EntityConverter::convert(std::vector lines if ( havePosition && haveName) { //**! no way to tell if teleporter or jump pad dest from targetName alone - convertedLines.push_back ( "\"classname\" \"misc_teleporter_dest\"\n" ); + if ( targetMap_[targetName] == "Teleporter") { + convertedLines.push_back ( "\"classname\" \"misc_teleporter_dest\"\n" ); + } + else if ( targetMap_[targetName] == "JumpPad") { + convertedLines.push_back ( "\"classname\" \"target_push\"\n" ); + } std::stringstream oss; oss << "\"targetname\" \"" << targetName << "\"\n"; convertedLines.push_back ( oss.str() ); @@ -232,7 +284,6 @@ std::vector EntityConverter::convert(std::vector lines convertedLines.push_back (oss3.str() ); } } - */ } else if ( type == "Effect" ) { ///EFFECT // to be implemented diff --git a/ReflexToQ3/includes/EntityConverter.hpp b/ReflexToQ3/includes/EntityConverter.hpp index 0b9428d..239d654 100644 --- a/ReflexToQ3/includes/EntityConverter.hpp +++ b/ReflexToQ3/includes/EntityConverter.hpp @@ -63,13 +63,18 @@ class EntityConverter { public: - EntityConverter(std::string entityMapFile); + EntityConverter(std::string entityMapFile, std::string reflexMapFile); std::vector convert(std::vector lines); - void printMapping(); //DEBUG protected: private: std::string getAttributeType(std::string line); - std::map pickupMapping; + void printMapping(); //DEBUG + void printTargetSources(); //DEBUG + + // Map Reflex pickup IDs to Xonotic pickup identifiers + std::map pickupMapping_; + // Map targets (by name) to their source type + std::map targetMap_; }; #endif //ENTITY_CONVERTER_HPP diff --git a/ReflexToQ3/includes/oopless-parser.cpp b/ReflexToQ3/includes/oopless-parser.cpp index e615808..072092c 100644 --- a/ReflexToQ3/includes/oopless-parser.cpp +++ b/ReflexToQ3/includes/oopless-parser.cpp @@ -9,6 +9,7 @@ #include "oopless-parser.hpp" #include "brushdef.hpp" +#include "EntityConverter.hpp" #include #include using namespace std;