QGearsTriggersFile.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <memory>
  17. #include <array>
  18. #include <OgreResourceManager.h>
  19. #include "QGearsPrerequisites.h"
  20. #include "common/QGearsResource.h"
  21. #include "QGearsSerializer.h"
  22. namespace QGears{
  23. /**
  24. * A manager for trigger files.
  25. */
  26. class TriggersFileManager :
  27. public Ogre::ResourceManager, public Ogre::Singleton<TriggersFileManager>
  28. {
  29. // TODO: Move this to it's own file?
  30. public:
  31. /**
  32. * Constructor.
  33. */
  34. TriggersFileManager();
  35. /**
  36. * Destructor.
  37. */
  38. virtual ~TriggersFileManager();
  39. /**
  40. * Retrieves a singleton to the manager.
  41. */
  42. static TriggersFileManager& GetSingleton();
  43. /**
  44. * Retrieves a pointer to the manager singleton.
  45. */
  46. static TriggersFileManager* GetSingletonPtr();
  47. protected:
  48. /**
  49. * Loads the manager.
  50. *
  51. * @param name[in] The unique name of the manager.
  52. * @param handle[in] @todo Understand and document.
  53. * @param group[in] The name of the resource group to which this
  54. * resource belong.
  55. * @param is_manual[in] True if the resource is manually loaded,
  56. * false otherwise.
  57. * @param loader[in] Pointer to a ManualResourceLoader
  58. * implementation which will be called when the Resource wishes to
  59. * load (should be supplied if is_manual is set to true). It can be
  60. * null, but the Resource will never be able to reload if anything
  61. * ever causes it to unload. Therefore provision of a proper
  62. * ManualResourceLoader instance is strongly recommended.
  63. * @param create_params[in] Unused.
  64. */
  65. Ogre::Resource *createImpl(
  66. const Ogre::String &name, Ogre::ResourceHandle handle,
  67. const Ogre::String &group, bool is_manual,
  68. Ogre::ManualResourceLoader *loader,
  69. const Ogre::NameValuePairList *create_params
  70. );
  71. };
  72. /**
  73. * Handles trigger files.
  74. */
  75. class TriggersFile : public Resource{
  76. public:
  77. /**
  78. * Constructor.
  79. *
  80. * @param creator[in] Pointer to the ResourceManager that is
  81. * creating this resource.
  82. * @param name[in] The unique name of the resource.
  83. * @param handle[in] @todo Understand and document.
  84. * @param group[in] The name of the resource group to which this
  85. * resource belong.
  86. * @param is_manual[in] True if the resource is manually loaded,
  87. * false otherwise.
  88. * @param loader[in] Pointer to a ManualResourceLoader
  89. * implementation which will be called when the Resource wishes to
  90. * load (should be supplied if is_manual is set to true). It can be
  91. * null, but the Resource will never be able to reload if anything
  92. * ever causes it to unload. Therefore provision of a proper
  93. * ManualResourceLoader instance is strongly recommended.
  94. */
  95. TriggersFile(
  96. Ogre::ResourceManager* creator, const String &name,
  97. Ogre::ResourceHandle handle, const String& group,
  98. bool is_manual = false,
  99. Ogre::ManualResourceLoader* loader = nullptr
  100. );
  101. /**
  102. * Destructor.
  103. */
  104. virtual ~TriggersFile();
  105. /**
  106. * The type pf resource.
  107. */
  108. static const String RESOURCE_TYPE;
  109. /**
  110. * Trigger range.
  111. *
  112. * @todo What range?
  113. */
  114. struct Range{
  115. /**
  116. * Left range.
  117. */
  118. s16 left;
  119. /**
  120. * Top range.
  121. */
  122. s16 top;
  123. /**
  124. * Right range.
  125. */
  126. s16 right;
  127. /**
  128. * Bottom range.
  129. */
  130. s16 bottom;
  131. };
  132. /**
  133. * Each of the trigger vertices.
  134. */
  135. struct TriggerVertex{
  136. /**
  137. * X coordinate.
  138. */
  139. s16 x;
  140. /**
  141. * Y coordinate.
  142. */
  143. s16 y;
  144. /**
  145. * Z coordinate.
  146. */
  147. s16 z;
  148. };
  149. /**
  150. * A gateway.
  151. *
  152. * Gateways are a special kind of trigger.
  153. */
  154. struct Gateway{
  155. /**
  156. * The gateway line, between two vertices.
  157. */
  158. std::array<TriggerVertex, 2> exit_line;
  159. /**
  160. * The destination point in the target map.
  161. */
  162. TriggerVertex destination;
  163. /**
  164. * ID of the destination field.
  165. */
  166. u16 destination_field_id;
  167. /**
  168. * @todo Understand and document.
  169. */
  170. u8 dir;
  171. /**
  172. * @todo Understand and document.
  173. */
  174. u8 dir_copy1;
  175. /**
  176. * @todo Understand and document.
  177. */
  178. u8 dir_copy2;
  179. /**
  180. * @todo Understand and document.
  181. */
  182. u8 dir_copy3;
  183. };
  184. /**
  185. * A trigger.
  186. */
  187. struct Trigger{
  188. /**
  189. * The trigger line, defined by two vertices.
  190. */
  191. std::array<TriggerVertex, 2> trigger_line;
  192. /**
  193. * @todo Understand and document.
  194. */
  195. u8 background_parameter;
  196. /**
  197. * @todo Understand and document.
  198. */
  199. u8 background_state;
  200. /**
  201. * The trigger behavior.
  202. *
  203. * 0 - OnTrigger - ON.
  204. * 1 - OnTrigger - OFF.
  205. * 2 - OnTrigger - ON, AwayFromTrigger - OFF.
  206. * 3 - OnTrigger - OFF, AwayFromTrigger - ON.
  207. * 4 - OnTrigger - ON, AwayFromTriggerOnPlusSide - OFF.
  208. * 5 - OnTrigger - OFF, AwayFromTriggerOnPlusSide - ON.
  209. */
  210. u8 behavior;
  211. /**
  212. * Trigger sound.
  213. */
  214. u8 sound_id;
  215. };
  216. /**
  217. * A map arrow.
  218. */
  219. struct Arrow{
  220. /**
  221. * X coordinate.
  222. */
  223. s32 x;
  224. /**
  225. * Z coordinate.
  226. */
  227. s32 z;
  228. /**
  229. * Y coordinate.
  230. */
  231. s32 y;
  232. /**
  233. * Arrow type
  234. *
  235. * 0 - Invisible, 1 - Red, 2 - Green.
  236. */
  237. u32 type;
  238. };
  239. /**
  240. * Trigger data.
  241. */
  242. struct TriggerData{
  243. /**
  244. * Trigger name.
  245. */
  246. std::array<char, 9> name;
  247. /**
  248. * @todo Understand and document.
  249. */
  250. u8 control;
  251. /**
  252. * @todo Understand and document.
  253. */
  254. s16 camera_focus_height;
  255. /**
  256. * @todo Understand and document.
  257. *
  258. * 8 bytes.
  259. */
  260. Range camera_range;
  261. /**
  262. * Flag for the background layer 1.
  263. */
  264. u8 bg_layer1_flag;
  265. /**
  266. * Flag for the background layer 2.
  267. */
  268. u8 bg_layer2_flag;
  269. /**
  270. * Flag for the background layer 3.
  271. */
  272. u8 bg_layer3_flag;
  273. /**
  274. * Flag for the background layer 4.
  275. */
  276. u8 bg_layer4_flag;
  277. /**
  278. * @todo Understand and document.
  279. */
  280. s16 bg_layer3_width;
  281. /**
  282. * @todo Understand and document.
  283. */
  284. s16 bg_layer3_height;
  285. /**
  286. * @todo Understand and document.
  287. */
  288. s16 bg_layer4_width;
  289. /**
  290. * @todo Understand and document.
  291. */
  292. s16 bg_layer4_height;
  293. /**
  294. * Unknown data.
  295. */
  296. std::array<u8, 24> unknown;
  297. /**
  298. * Doors (gateways).
  299. *
  300. * A door is 24 * 12 bytes.
  301. */
  302. std::array<Gateway, 12> doors;
  303. /**
  304. * Triggers.
  305. *
  306. * A trigger is 16 * 12 bytes.
  307. */
  308. std::array<Trigger, 12> triggers;
  309. /**
  310. * The map arrows
  311. *
  312. * Only in occidental/international version.
  313. */
  314. std::array<u8, 12> display_arrow;
  315. /**
  316. * The map arrows
  317. *
  318. * Only in occidental/international version. 16 * 12 bytes.
  319. */
  320. std::array<Arrow, 12> arrows;
  321. };
  322. /**
  323. * Retrieves the camera range.
  324. *
  325. * @return The camera range.
  326. */
  327. const Range& GetCameraRange() const{
  328. return trigger_data_->camera_range;
  329. }
  330. /**
  331. * Retrieves the movement rotation.
  332. *
  333. * The movement rotation is the angle in which the player moves
  334. * when "up" is pressed.
  335. *
  336. * @return The movement rotation.
  337. */
  338. float MovementRotation() const{
  339. return
  340. 180.0f
  341. * (static_cast<float>(trigger_data_->control) - 128.0f)
  342. / 128.0f;
  343. }
  344. /**
  345. * Retrieves the list of gateways.
  346. */
  347. const std::array<Gateway, 12>& GetGateways() const{
  348. return trigger_data_->doors;
  349. }
  350. protected:
  351. /**
  352. * Loads the file.
  353. */
  354. virtual void loadImpl() override;
  355. /**
  356. * Unloads the file.
  357. */
  358. virtual void unloadImpl() override;
  359. /**
  360. * Calculates the size of the palette.
  361. *
  362. * @return The size of the palette.
  363. * @todo Units?
  364. */
  365. virtual size_t calculateSize(void) const override;
  366. private:
  367. /**
  368. * The trigger data.
  369. */
  370. std::unique_ptr<TriggerData> trigger_data_;
  371. friend class TriggerFileSerializer;
  372. };
  373. /**
  374. * Handles the serialization of trigger files.
  375. */
  376. class TriggerFileSerializer : public Serializer{
  377. // TODO Move this to it's own file.
  378. public:
  379. /**
  380. * Constructor.
  381. */
  382. TriggerFileSerializer();
  383. /**
  384. * Imports a trigger file.
  385. *
  386. * @param stream[in] The contents of the trigger file.
  387. * @param dest[out] The formed trigger file.
  388. */
  389. void ImportTriggerFile(
  390. Ogre::DataStreamPtr &stream, TriggersFile *dest
  391. );
  392. private:
  393. /**
  394. * Reads trigger vertex data from the trigger file.
  395. *
  396. * @param stream[in] The contents of the trigger file.
  397. * @param vertex[out] The vertex data.
  398. */
  399. void ReadTriggerVertex(
  400. Ogre::DataStreamPtr& stream, TriggersFile::TriggerVertex& vertex
  401. );
  402. /**
  403. * Reads range data from the trigger file.
  404. *
  405. * @param stream[in] The contents of the trigger file.
  406. * @param vertex[out] The range data.
  407. */
  408. void ReadRange(
  409. Ogre::DataStreamPtr& stream, TriggersFile::Range& range
  410. );
  411. /**
  412. * Reads gateway data from the trigger file.
  413. *
  414. * @param stream[in] The contents of the trigger file.
  415. * @param vertex[out] The gateway data.
  416. */
  417. void ReadGateway(
  418. Ogre::DataStreamPtr& stream, TriggersFile::Gateway& exit
  419. );
  420. /**
  421. * Reads arrow data from the trigger file.
  422. *
  423. * @param stream[in] The contents of the trigger file.
  424. * @param vertex[out] The arrow data.
  425. */
  426. void ReadArrow(
  427. Ogre::DataStreamPtr& stream, TriggersFile::Arrow& arrow
  428. );
  429. /**
  430. * Reads trigger data from the trigger file.
  431. *
  432. * @param stream[in] The contents of the trigger file.
  433. * @param vertex[out] The trigger data.
  434. */
  435. void ReadTrigger(
  436. Ogre::DataStreamPtr& stream, TriggersFile::Trigger& trigger
  437. );
  438. };
  439. typedef Ogre::SharedPtr<TriggersFile> TriggersFilePtr;
  440. }