EntityConverter Target handled by pre-scan

This commit is contained in:
suhrke 2017-06-07 01:46:43 -07:00
parent 096cffb115
commit 98abd5c184
3 changed files with 82 additions and 25 deletions

View File

@ -24,19 +24,16 @@
EntityConverter::EntityConverter(std::string entityMapFile) EntityConverter::EntityConverter(std::string entityMapFile, std::string reflexMapFile)
{ {
//Open .ent mapping file //Open .ent mapping file
std::ifstream fin; std::ifstream entFin;
fin.open(entityMapFile); entFin.open(entityMapFile);
if ( ! fin.is_open() ) {
throw std::ios::failure( "Error opening .ent file" );
}
if ( entFin.is_open() ) {
//Read .ent contents into pickup map //Read .ent contents into pickup map
std::string line; std::string line;
while (std::getline(fin, line)) { while (std::getline(entFin, line)) {
std::istringstream iss(line); std::istringstream iss(line);
// Reflex ID corresponds to xonotic pickup name // Reflex ID corresponds to xonotic pickup name
int id; int id;
@ -44,8 +41,51 @@ EntityConverter::EntityConverter(std::string entityMapFile)
if ( ! (iss >> id >> pickup)) { if ( ! (iss >> id >> pickup)) {
throw std::runtime_error( "format error in .ent file" ); throw std::runtime_error( "format error in .ent file" );
} }
pickupMapping.insert ( std::pair<int, std::string>(id, pickup) ); pickupMapping_.insert ( std::pair<int, std::string>(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<std::string, std::string>(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<std::string, std::string>(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() void EntityConverter::printMapping()
{ {
std::map<int, std::string>::iterator it; std::map<int, std::string>::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; std::cout << it->first << " => " << it->second << std::endl;
} }
// DEBUG
void EntityConverter::printTargetSources()
{
std::map<std::string, std::string>::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) std::string EntityConverter::getAttributeType(std::string line)
@ -127,7 +174,7 @@ std::vector<std::string> EntityConverter::convert(std::vector<std::string> lines
if ( havePosition && havePickupID ) { if ( havePosition && havePickupID ) {
std::stringstream oss; std::stringstream oss;
oss << "\"classname\" \"" << pickupMapping[pickupID] << "\"" << std::endl; oss << "\"classname\" \"" << pickupMapping_[pickupID] << "\"" << std::endl;
convertedLines.push_back ( oss.str() ); convertedLines.push_back ( oss.str() );
// coordinates reordered to x, z, y // coordinates reordered to x, z, y
std::stringstream oss2; std::stringstream oss2;
@ -157,7 +204,7 @@ std::vector<std::string> EntityConverter::convert(std::vector<std::string> lines
convertedLines.push_back ( oss.str() ); convertedLines.push_back ( oss.str() );
} }
else if ( type == "Teleporter" ) { ///TELEPORTER 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"); throw std::runtime_error("error: Teleport entity requires at least 2 lines");
} }
std::istringstream iss2(lines[1]); std::istringstream iss2(lines[1]);
@ -215,7 +262,12 @@ std::vector<std::string> EntityConverter::convert(std::vector<std::string> lines
if ( havePosition && haveName) { if ( havePosition && haveName) {
//**! no way to tell if teleporter or jump pad dest from targetName alone //**! no way to tell if teleporter or jump pad dest from targetName alone
if ( targetMap_[targetName] == "Teleporter") {
convertedLines.push_back ( "\"classname\" \"misc_teleporter_dest\"\n" ); convertedLines.push_back ( "\"classname\" \"misc_teleporter_dest\"\n" );
}
else if ( targetMap_[targetName] == "JumpPad") {
convertedLines.push_back ( "\"classname\" \"target_push\"\n" );
}
std::stringstream oss; std::stringstream oss;
oss << "\"targetname\" \"" << targetName << "\"\n"; oss << "\"targetname\" \"" << targetName << "\"\n";
convertedLines.push_back ( oss.str() ); convertedLines.push_back ( oss.str() );
@ -232,7 +284,6 @@ std::vector<std::string> EntityConverter::convert(std::vector<std::string> lines
convertedLines.push_back (oss3.str() ); convertedLines.push_back (oss3.str() );
} }
} }
*/
} }
else if ( type == "Effect" ) { ///EFFECT else if ( type == "Effect" ) { ///EFFECT
// to be implemented // to be implemented

View File

@ -63,13 +63,18 @@
class EntityConverter class EntityConverter
{ {
public: public:
EntityConverter(std::string entityMapFile); EntityConverter(std::string entityMapFile, std::string reflexMapFile);
std::vector<std::string> convert(std::vector<std::string> lines); std::vector<std::string> convert(std::vector<std::string> lines);
void printMapping(); //DEBUG
protected: protected:
private: private:
std::string getAttributeType(std::string line); std::string getAttributeType(std::string line);
std::map<int, std::string> pickupMapping; void printMapping(); //DEBUG
void printTargetSources(); //DEBUG
// 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 #endif //ENTITY_CONVERTER_HPP

View File

@ -9,6 +9,7 @@
#include "oopless-parser.hpp" #include "oopless-parser.hpp"
#include "brushdef.hpp" #include "brushdef.hpp"
#include "EntityConverter.hpp"
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
using namespace std; using namespace std;