EntityConverter.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: EntityConverter.hpp
  5. *
  6. * Description: Convert reflex entities to xonotic entities
  7. * - Simple; operates on single entity at a time
  8. * - Only context provided is information on what entities are related.
  9. * (i.e. a teleport and it's destination) Can get this information
  10. * through the pre-scan constructor of the .map file or by providing
  11. * a queue of all the entities in the file.
  12. * - Throws exceptions upon encountering malformed entities and when
  13. * IO errors occur during object instantiation
  14. *
  15. * Version: 1.0
  16. * Created: 05/27/2017 08:21:14 AM
  17. * Revision: none
  18. * Compiler: gcc
  19. *
  20. * Author: suhrke@protonmail.com
  21. *
  22. * =====================================================================================
  23. */
  24. #ifndef ENTITY_CONVERTER_HPP
  25. #define ENTITY_CONVERTER_HPP
  26. #include <map>
  27. #include <string>
  28. #include <queue>
  29. #include <vector>
  30. struct WorldSpawn
  31. {
  32. bool cts;
  33. bool ctf;
  34. bool ffa;
  35. bool tdm;
  36. bool duel;
  37. };
  38. class EntityConverter
  39. {
  40. public:
  41. /*
  42. *--------------------------------------------------------------------------------------
  43. * Class: EntityConverter
  44. * Method: Constructor
  45. * Description: Creates entity format mapping
  46. * CAUTION: Requires extractMapInfo method to be called after this
  47. * Requires: Reflex Entity Mapping filename
  48. * THROWS: runtime_error on Reflex Entity Mapping file format error
  49. * THROWS: std::ios::failure on IO failure
  50. *--------------------------------------------------------------------------------------
  51. */
  52. EntityConverter (const std::string &entityMapFile);
  53. /* *--------------------------------------------------------------------------------------
  54. * Class: EntityConverter
  55. * Method: Constructor
  56. * Description: Creates entity format mapping and pre-scans for map info
  57. * Parameter: string entityMapFile, file maps source to target entity formats
  58. * Parameter: string reflexMapFile, for pre-scan
  59. * THROWS: runtime_error on Reflex Entity Mapping file format error
  60. * THROWS: std::ios::failure on IO failure
  61. *--------------------------------------------------------------------------------------
  62. */
  63. EntityConverter (const std::string &entityMapFile, const std::string &reflexMapFile);
  64. /*
  65. *--------------------------------------------------------------------------------------
  66. * Class: EntityConverter
  67. * Method: EntityConverter :: convert
  68. * Description: Converts a single entity from reflex to xonotic format
  69. * Parameter: vector of strings lines, lines that comprise a single entity
  70. * Return: vector of strings, single entity in the converted format
  71. * *IF entity is not supported, returns EMPTY vector
  72. * THROWS: runtime_error on malformed .map file
  73. * THROWS: runtime_error when called before map info has been extracted
  74. *--------------------------------------------------------------------------------------
  75. */
  76. std::vector<std::string> convert (const std::vector<std::string> &lines);
  77. /*
  78. *--------------------------------------------------------------------------------------
  79. * Class: EntityConverter
  80. * Method: EntityConverter :: extractMapInfo
  81. * Description: Get information needed by the converter that can't be obtained
  82. * in entity-by-entity conversion (teleport and jump pad
  83. * Parameter: queue of vector of string entities, ALL entities in a .map file
  84. * THROWS: runtime_error when encountering malformed entity
  85. *--------------------------------------------------------------------------------------
  86. */
  87. void extractMapInfo (std::queue<std::vector<std::string>> entities);
  88. /*
  89. *--------------------------------------------------------------------------------------
  90. * Class: EntityConverter
  91. * Method: EntityConverter :: extractMapInfo
  92. * Description: Get information needed by the converter that can't be obtained
  93. * in entity-by-entity conversion (teleport and jump pad
  94. * Parameter: vector of vector of string entities, ALL entities in a .map file
  95. * THROWS: runtime_error when encountering malformed entity
  96. *--------------------------------------------------------------------------------------
  97. */
  98. void extractMapInfo (const std::vector<std::vector<std::string>> &entities);
  99. protected:
  100. /*
  101. *--------------------------------------------------------------------------------------
  102. * Class: EntityConverter
  103. * Method: EntityConverter :: convert~EntityName~
  104. * Description: Multiple methods to convert entity from reflex to xonotic format
  105. * Parameter: vector of strings lines, multi-lined entity
  106. * Return: vector of strings, the converted entity
  107. *--------------------------------------------------------------------------------------
  108. */
  109. std::vector<std::string> convertPickup (const std::vector<std::string> &lines) const;
  110. std::vector<std::string> convertPlayerSpawn (const std::vector<std::string> &lines) const;
  111. std::vector<std::string> convertJumpPad (const std::vector<std::string> &lines) const;
  112. std::vector<std::string> convertTeleporter (const std::vector<std::string> &lines) const;
  113. std::vector<std::string> convertTarget (const std::vector<std::string> &lines) const;
  114. std::vector<std::string> convertRaceStart (const std::vector<std::string> &lines) const;
  115. std::vector<std::string> convertRaceFinish (const std::vector<std::string> &lines) const;
  116. std::vector<std::string> convertPointLight (const std::vector<std::string> &lines) const;
  117. /*
  118. *--------------------------------------------------------------------------------------
  119. * Class: EntityConverter
  120. * Method: EntityConverter :: getAttributeType
  121. * Description: Extracts the type from a line
  122. * Parameter: string "line", entity keyword followed by the type
  123. *--------------------------------------------------------------------------------------
  124. */
  125. std::string getAttributeType (const std::string &line) const;
  126. /*
  127. *--------------------------------------------------------------------------------------
  128. * Class: EntityConverter
  129. * Method: EntityConverter :: mapEntities
  130. * Description: Prepare pickupMap
  131. * Parameter: string mapFile, filename of pickup mapping
  132. * Return: true if no error, false if error
  133. *--------------------------------------------------------------------------------------
  134. */
  135. void mapEntities (const std::string &mapFile);
  136. /*
  137. *--------------------------------------------------------------------------------------
  138. * Class: EntityConverter
  139. * Method: EntityConverter :: haveRequiredMappings
  140. * Description: Check that required mappings exist
  141. *--------------------------------------------------------------------------------------
  142. */
  143. bool haveRequiredMappings();
  144. /*
  145. *--------------------------------------------------------------------------------------
  146. * Class: EntityConverter
  147. * Method: EntityConverter :: extractFromEntity
  148. * Description: Get map info from a single entity
  149. * Paramater: string line, the previous line (contains entity type)
  150. * Parameter: istream is, an ifstream or a stringstream containing a
  151. * single entity
  152. *--------------------------------------------------------------------------------------
  153. */
  154. void extractFromEntity (const std::string &line, std::istream &is);
  155. /*
  156. *--------------------------------------------------------------------------------------
  157. * Class: EntityConverter
  158. * Method: EntityConverter :: offset
  159. * Description: Reflex coordinates place entities at the ground and Xonotic entities
  160. * are at about center of player height. Offset accordingly.
  161. * Paramater: string value, float value passed as string
  162. * Parameter: float offset, amount to add to value
  163. * Return: string, float value passed as string
  164. *--------------------------------------------------------------------------------------
  165. */
  166. std::string offset (const std::string &value, const float offset) const;
  167. /*
  168. *--------------------------------------------------------------------------------------
  169. * Class: EntityConverter
  170. * Method: EntityConverter :: adjustAngleForHandedness
  171. * Description: Axis swaps require angles to take this into account
  172. * Parameter: string angle, an angle in degrees
  173. * Return: string, the adjusted angle in degrees
  174. *--------------------------------------------------------------------------------------
  175. */
  176. std::string adjustAngleForHandedness (const std::string &angle) const;
  177. /*
  178. *--------------------------------------------------------------------------------------
  179. * Class: EntityConverter
  180. * Method: EntityConverter :: hexToRGB
  181. * Description: Convert 8 digit hex value into separate red, green, and blue values
  182. * Parameter: string hex, inputted hex RGBA value (leftmost byte is alpha, then RGB)
  183. * Parameter: float r, RETURN BY REFERENCE: converted red value
  184. * Parameter: float g, RETURN BY REFERENCE: converted green value
  185. * Parameter: float b, RETURN BY REFERENCE: converted blue value
  186. *--------------------------------------------------------------------------------------
  187. */
  188. void hexToRGB (const std::string &hex, float &r, float &g, float &b) const;
  189. /*
  190. *--------------------------------------------------------------------------------------
  191. * Class: EntityConverter
  192. * Method: EntityConverter :: adjustBrightness
  193. * Description: Reflex uses significantly smaller values than Xonotic -> adjust
  194. * Parameter: string value, original brightness value
  195. *--------------------------------------------------------------------------------------
  196. */
  197. int adjustBrightness (const std::string &value) const;
  198. /*
  199. *--------------------------------------------------------------------------------------
  200. * Class: EntityConverter
  201. * Method: EntityConverter :: makeErrorMessage
  202. * Description: Combine a message and the entity responsible into an error message
  203. *--------------------------------------------------------------------------------------
  204. */
  205. std::string makeErrorMessage (const std::string message,
  206. const std::vector<std::string> entity) const;
  207. // Map Reflex pickup IDs to Xonotic pickup identifiers
  208. std::map<std::string, std::string> entityMap_;
  209. // Map targets (by name) to their source type
  210. std::map<std::string, std::string> targetMap_;
  211. // Related entities must be matched prior to entity conversion
  212. bool haveMapInfo_;
  213. WorldSpawn ws_;
  214. // Offsets for item/spawn height
  215. const float OFFSET_PLAYER;
  216. const float OFFSET_PICKUP;
  217. // Brightness adjustment factor
  218. const float BRIGHTNESS_ADJUST;
  219. // Floating point precision for output
  220. const int OUTPUT_PRECISION;
  221. private:
  222. void printMapping() const; //DEBUG
  223. void printTargetSources() const; //DEBUG
  224. };
  225. #endif //ENTITY_CONVERTER_HPP