Reorganization to more typical project structure

This commit is contained in:
suhrke
2017-08-23 20:30:27 -07:00
parent afb329cac4
commit 79d1513ebd
9 changed files with 5 additions and 5 deletions

254
include/EntityConverter.hpp Normal file
View File

@@ -0,0 +1,254 @@
/*
* =====================================================================================
*
* Filename: EntityConverter.hpp
*
* Description: Convert reflex entities to xonotic entities
* - Simple; operates on single entity at a time
* - Only context provided is information on what entities are related.
* (i.e. a teleport and it's destination) Can get this information
* through the pre-scan constructor of the .map file or by providing
* a queue of all the entities in the file.
* - Throws exceptions upon encountering malformed entities and when
* IO errors occur during object instantiation
*
* Version: 1.0
* Created: 05/27/2017 08:21:14 AM
* Revision: none
* Compiler: gcc
*
* Author: surkeh@protonmail.com
*
* =====================================================================================
*/
#ifndef ENTITY_CONVERTER_HPP
#define ENTITY_CONVERTER_HPP
#include <map>
#include <string>
#include <queue>
#include <vector>
struct WorldSpawn
{
bool cts;
bool ctf;
bool ffa;
bool tdm;
bool duel;
};
class EntityConverter
{
public:
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: Constructor
* Description: Creates entity format mapping
* CAUTION: Requires extractMapInfo method to be called after this
* Requires: .ent filename for mapping entities from reflex format to xonotic format
* THROWS: runtime_error on .ent format error
* THROWS: std::ios::failure on IO failure
*--------------------------------------------------------------------------------------
*/
EntityConverter(const std::string &entityMapFile);
/* *--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: Constructor
* Description: Creates entity format mapping and pre-scans for map info
* Parameter: string entityMapFile, file maps source to target entity formats
* Parameter: string reflexMapFile, for pre-scan
* THROWS: runtime_error on .ent format error
* THROWS: std::ios::failure on IO failure
*--------------------------------------------------------------------------------------
*/
EntityConverter(const std::string &entityMapFile, const std::string &reflexMapFile);
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: convert
* Description: Converts a single entity from reflex to xonotic format
* Parameter: vector of strings lines, lines that comprise a single entity
* Return: vector of strings, single entity in the converted format
* *IF entity is not supported, returns EMPTY vector
* THROWS: runtime_error on malformed .map file
* THROWS: runtime_error when called before map info has been extracted
*--------------------------------------------------------------------------------------
*/
std::vector<std::string> convert(const std::vector<std::string> &lines);
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: extractMapInfo
* Description: Get information needed by the converter that can't be obtained
* in entity-by-entity conversion (teleport and jump pad
* Parameter: queue of vector of string entities, ALL entities in a .map file
* THROWS: runtime_error when encountering malformed entity
*--------------------------------------------------------------------------------------
*/
void extractMapInfo(std::queue<std::vector<std::string>> entities);
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: extractMapInfo
* Description: Get information needed by the converter that can't be obtained
* in entity-by-entity conversion (teleport and jump pad
* Parameter: vector of vector of string entities, ALL entities in a .map file
* THROWS: runtime_error when encountering malformed entity
*--------------------------------------------------------------------------------------
*/
void extractMapInfo(const std::vector<std::vector<std::string>> &entities);
protected:
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: convert~EntityName~
* Description: Multiple methods to convert entity from reflex to xonotic format
* Parameter: vector of strings lines, multi-lined entity
* Return: vector of strings, the converted entity
*--------------------------------------------------------------------------------------
*/
std::vector<std::string> convertPickup(const std::vector<std::string> &lines) const;
std::vector<std::string> convertPlayerSpawn(const std::vector<std::string> &lines) const;
std::vector<std::string> convertJumpPad(const std::vector<std::string> &lines) const;
std::vector<std::string> convertTeleporter(const std::vector<std::string> &lines) const;
std::vector<std::string> convertTarget(const std::vector<std::string> &lines) const;
std::vector<std::string> convertRaceStart(const std::vector<std::string> &lines) const;
std::vector<std::string> convertRaceFinish(const std::vector<std::string> &lines) const;
std::vector<std::string> convertPointLight(const std::vector<std::string> &lines) const;
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: getAttributeType
* Description: Extracts the type from a line
* Parameter: string "line", entity keyword followed by the type
*--------------------------------------------------------------------------------------
*/
std::string getAttributeType(const std::string &line) const;
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: mapEntities
* Description: Prepare pickupMap
* Parameter: string mapFile, filename of pickup mapping
* Return: true if no error, false if error
*--------------------------------------------------------------------------------------
*/
void mapEntities(const std::string &mapFile);
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: haveRequiredMappings
* Description: Check that required mappings exist
*--------------------------------------------------------------------------------------
*/
bool haveRequiredMappings();
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: extractFromEntity
* Description: Get map info from a single entity
* Paramater: string line, the previous line (contains entity type)
* Parameter: istream is, an ifstream or a stringstream containing a
* single entity
*--------------------------------------------------------------------------------------
*/
void extractFromEntity(const std::string &line, std::istream &is);
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* 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::string offset(const std::string &value, const float offset) const;
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: adjustAngleForHandedness
* Description: Axis swaps require angles to take this into account
* Parameter: string angle, an angle in degrees
* Return: string, the adjusted angle in degrees
*--------------------------------------------------------------------------------------
*/
std::string adjustAngleForHandedness(const std::string &angle) const;
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: hexToRGB
* Description: Convert 8 digit hex value into separate red, green, and blue values
* Parameter: string hex, inputted hex RGBA value (leftmost byte is alpha, then RGB)
* Parameter: float r, RETURN BY REFERENCE: converted red value
* Parameter: float g, RETURN BY REFERENCE: converted green value
* Parameter: float b, RETURN BY REFERENCE: converted blue value
*--------------------------------------------------------------------------------------
*/
void hexToRGB(const std::string &hex, float &r, float &g, float &b) const;
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: adjustBrightness
* Description: Reflex uses significantly smaller values than Xonotic -> adjust
* Parameter: string value, original brightness value
*--------------------------------------------------------------------------------------
*/
int adjustBrightness(const std::string &value) const;
/*
*--------------------------------------------------------------------------------------
* Class: EntityConverter
* Method: EntityConverter :: makeErrorMessage
* Description: Combine a message and the entity responsible into an error message
*--------------------------------------------------------------------------------------
*/
std::string makeErrorMessage(const std::string message,
const std::vector<std::string> entity) const;
// Map Reflex pickup IDs to Xonotic pickup identifiers
std::map<std::string, std::string> entityMap_;
// Map targets (by name) to their source type
std::map<std::string, std::string> targetMap_;
// Related entities must be matched prior to entity conversion
bool haveMapInfo_;
WorldSpawn ws_;
// Offsets for item/spawn height
const float OFFSET_PLAYER;
const float OFFSET_PICKUP;
// Brightness adjustment factor
const float BRIGHTNESS_ADJUST;
// Floating point precision for output
const int OUTPUT_PRECISION;
private:
void printMapping() const; //DEBUG
void printTargetSources() const; //DEBUG
};
#endif //ENTITY_CONVERTER_HPP

21
include/brushdef.hpp Normal file
View File

@@ -0,0 +1,21 @@
#ifndef BRUSHDEF_HPP
#define BRUSHDEF_HPP
#include <string>
#include <vector>
#include <sstream>
#include "worldspawn.h"
#include "planes.h"
std::string texdef_gtk(const TPlanePoints&);
std::string texdef_net(const TPlanePoints&);
void brushdef_gtk(std::stringstream&, const std::vector<TPlanePoints>&);
void brushdef_net(std::stringstream&, const std::vector<TPlanePoints>&);
std::string GetBrushString(const TBrush&,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &));
#endif

183
include/oopless-parser.hpp Normal file
View File

@@ -0,0 +1,183 @@
#ifndef __OOPLESS_PARSER__
#define __OOPLESS_PARSER__
#include "planes.h"
#include "worldspawn.h"
#include <string>
#include <vector>
#include <fstream>
#include <queue>
#include "EntityConverter.hpp"
bool is_ebrush(const std::vector<std::string> &);
std::vector<std::string> extract_brush(const std::vector<std::string> &);
struct TBrush override_textures(struct TBrush, const char*);
bool write(const struct TBrush &,
std::ofstream &,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &));
void write(const std::vector<std::string> &,
std::ofstream &,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
EntityConverter &);
void parse_prefab(std::ifstream &,
std::ofstream &,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
std::vector<std::vector<std::string> > &);
bool convertmap(const char * const,
const char * const,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
std::vector<std::vector<std::string> > &);
#define KEYWORD_ENTITY "entity"
#define KEYWORD_BRUSH "brush"
#define KEYWORD_VERTICES "vertices"
#define KEYWORD_FACES "faces"
#define KEYWORD_GLOBAL "global"
#define KEYWORD_PREFAB "prefab"
template <class STREAMOBJ>
std::vector<std::string> get_entity(STREAMOBJ &f) {
using namespace std;
vector<string> output;
string line;
unsigned int pos = f.tellg();
while (getline(f, line)) {
// stop when
// entity or a prefab keyword
// brush keyword and the entity is not of the type teleporter or jumppad.
if ((line.find(KEYWORD_ENTITY) != string::npos ||
line.find(KEYWORD_PREFAB) != string::npos) || (
line.find(KEYWORD_BRUSH) != string::npos && !is_ebrush(output))) {
f.seekg(pos);
return output;
} else {
output.push_back(line);
}
pos = f.tellg();
}
}
template <class STREAMOBJ>
std::vector<Eigen::Vector3f> parse_vertices(STREAMOBJ &f) {
using namespace std;
#define FIRSTCH(x) x[0]
vector<Eigen::Vector3f> output;
string line;
unsigned int pos = f.tellg();
while(getline(f, line)) {
if (line.find(KEYWORD_FACES) != string::npos ||
line.find(KEYWORD_BRUSH) != string::npos ||
line.find(KEYWORD_ENTITY) != string::npos ||
line.find(KEYWORD_PREFAB) != string::npos) {
f.seekg(pos);
return output;
} else {
Eigen::Vector3f v;
stringstream ss(line);
string vdata;
unsigned int i = 0;
while (ss >> vdata) {
if (isdigit(FIRSTCH(vdata)) || FIRSTCH(vdata) == '-') {
double dvalue = stod(vdata);
v[i] = (float)dvalue;
}
// note: this prevents the index growing out of vector capacity
// alternatively can throw an exception when that happens instead
// to make it clear that an illegal line has been hit.
i = (i + 1) % 3;
}
output.push_back(v);
}
pos = f.tellg();
}
return output;
}
template <class STREAMOBJ>
std::vector<struct TFace> parse_face(STREAMOBJ &f) {
using namespace std;
#define FIRSTCH(x) x[0]
#define SECONDCH(x) x[1]
// it is possible for the next line to be unrelated to faces
// so it is needed to reset the stream prior to reading
// the new label, e.g '\tbrush'.
vector<struct TFace> output;
string line;
unsigned int pos = f.tellg();
while(getline(f, line)) {
if (line.find(KEYWORD_VERTICES) != string::npos ||
line.find(KEYWORD_BRUSH) != string::npos ||
line.find(KEYWORD_ENTITY) != string::npos ||
line.find(KEYWORD_PREFAB) != string::npos ||
line.find(KEYWORD_GLOBAL) != string::npos) {
f.seekg(pos);
return output;
} else {
struct TFace x;
float *f = &x.m_fXOffset;;
stringstream ss(line);
string fdata;
bool hex = false;
unsigned int i = 0;
while (ss >> fdata) {
if (i < 5) {
if (isdigit(FIRSTCH(fdata)) || FIRSTCH(fdata) == '-') {
double dvalue = stod(fdata);
*f = (float)dvalue;
f++;
}
// note: if there is a non-digit in the first 5 fields
// then it qualifies as an illegal line.
} else if (4 < i && i < 8) {
x.m_Indices.push_back(stoi(fdata));
} else if (8 <= i) {
if (!hex && fdata.length() > 1) {
if (SECONDCH(fdata) == 'x') {
// this is the unidentified hex digit.
// out of range for stoi
x.hex = fdata;
hex = true;
}
} else if (!hex) {
x.m_Indices.push_back(stoi(fdata));
} else if (hex) {
x.m_Material = fdata;
} else {
;
}
}
i++;
} // end, per field iteration
output.push_back(x);
} // end else case, if line did not contain other keywords
pos = f.tellg();
} // end, per line iteration
// the odd case when the map file ends with a face indent.
return output;
}
template <class STREAMOBJ>
struct TBrush parse_brush(STREAMOBJ &f) {
using namespace std;
struct TBrush output;
string l;
getline(f, l);
if (l.find(KEYWORD_VERTICES) != string::npos) {
output.m_Vertices = parse_vertices<STREAMOBJ>(f);
}
getline(f, l);
if (l.find(KEYWORD_FACES) != string::npos) {
output.m_Faces = parse_face<STREAMOBJ>(f);
}
return output;
}
#endif

32
include/planes.h Normal file
View File

@@ -0,0 +1,32 @@
#ifndef __PLANES_H__
#define __PLANES_H__
#include "worldspawn.h"
#include <Eigen/Core>
#include <Eigen/Geometry>
struct TPlanePoints {
Eigen::Vector3f m_A;
Eigen::Vector3f m_B;
Eigen::Vector3f m_C;
std::string m_material;
float hscale, hshift; // horizontal
float vscale, vshift; // vertical
float rotation;
};
Eigen::Vector3f get_polygon_normal(const Eigen::Vector3f*, const size_t);
Eigen::Vector3f get_center(const Eigen::Vector3f*, const size_t);
float get_width(const Eigen::Vector3f*, const size_t, const Eigen::Vector3f);
Eigen::Vector3f get_tangent(const Eigen::Vector3f*, float);
Eigen::Vector3f get_bitangent(const Eigen::Vector3f&, const Eigen::Vector3f&, float);
TPlanePoints GetPlanePoints(const Eigen::Vector3f*, const size_t);
std::vector<TPlanePoints> GetBrushPlanes(const TBrush&);
#endif

37
include/worldspawn.h Normal file
View File

@@ -0,0 +1,37 @@
//
// Author: Michael Cameron
// Email: chronokun@hotmail.com
//
#ifndef __WORLDSPAWN_H__
#define __WORLDSPAWN_H__
#include <vector>
#include <string>
#include <Eigen/Core>
struct TFace
{
float m_fXOffset;
float m_fYOffset;
float m_fXScale;
float m_fYScale;
float m_fRotation;
std::vector<int> m_Indices;
std::string hex;
std::string m_Material;
};
struct TBrush
{
std::vector<Eigen::Vector3f> m_Vertices;
std::vector<TFace> m_Faces;
};
struct TWorldSpawn
{
std::vector<TBrush> m_Brushes;
};
#endif