EntityManager.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 <chrono>
  17. #include <set>
  18. #include <map>
  19. #include <utility>
  20. #include <vector>
  21. #include <OgreSingleton.h>
  22. #include "engine/graphics/Background2D.h"
  23. #include "Entity.h"
  24. #include "EntityPoint.h"
  25. #include "EntityTrigger.h"
  26. #include "engine/Event.h"
  27. #include "engine/Manager.h"
  28. #include "engine/graphics/MoviePlayer.h"
  29. #include "engine/graphics/ScreenFade.h"
  30. #include "Walkmesh.h"
  31. class EntityModel;
  32. /**
  33. * The entity manager.
  34. */
  35. class EntityManager : public Manager, public Ogre::Singleton<EntityManager>{
  36. public:
  37. /**
  38. * Constructor.
  39. */
  40. EntityManager();
  41. /**
  42. * Destructor.
  43. */
  44. virtual ~EntityManager();
  45. /**
  46. * Limits a scripted movement step to its remaining distance.
  47. *
  48. * @param[in] distance Distance to the target.
  49. * @param[in] stop_distance Desired distance from the target.
  50. * @param[in] max_step Maximum distance to move this frame.
  51. * @return Distance to move this frame.
  52. */
  53. static float ScriptedMoveStep(float distance, float stop_distance, float max_step);
  54. /**
  55. * Decides whether an entity is a better target to interact with than the best one so far.
  56. *
  57. * The one the player faces more directly wins. If the angles are the same, the nearest
  58. * one does.
  59. *
  60. * @param[in] angle Angle in degrees between where the player faces and the candidate.
  61. * @param[in] distance Squared distance to the candidate.
  62. * @param[in] best_angle Angle of the best candidate so far, or the largest one allowed.
  63. * @param[in] best_distance Squared distance to the best candidate so far.
  64. * @return True if the candidate is better.
  65. */
  66. static bool IsBetterInteractionTarget(
  67. float angle, float distance, float best_angle, float best_distance
  68. );
  69. /**
  70. * Retrieves how close to a line an entity has to be to trigger it.
  71. *
  72. * It's the solid radius of the entity, unless a script has made it too small, as scripts
  73. * do while a scene is playing. Lines couldn't be triggered at all then.
  74. *
  75. * @param[in] solid_radius Solid radius of the entity.
  76. * @return The distance to the line from which it triggers.
  77. */
  78. static float TriggerReach(float solid_radius);
  79. /**
  80. * Checks if the edge of the walkmesh stops an entity.
  81. *
  82. * It stops the player and the characters that wander. The characters that a script walks
  83. * to a position are not stopped: the original game lets them walk off the walkmesh.
  84. *
  85. * @param[in] is_player Whether the entity is the player.
  86. * @param[in] scripted_move Whether a script asked the entity to walk to a position.
  87. * @return True if the edge of the walkmesh blocks the entity.
  88. */
  89. static bool WalkmeshEdgeBlocks(bool is_player, bool scripted_move);
  90. /**
  91. * Handles an input event.
  92. *
  93. * @param[in] event Event to handle.
  94. */
  95. void Input(const VGears::Event& event) override;
  96. /**
  97. * Updates the entities in the manager with debug information.
  98. *
  99. * It's automatically called from {@see Update}.
  100. */
  101. void UpdateDebug() override;
  102. /**
  103. * Handles resizing events
  104. */
  105. void OnResize() override;
  106. /**
  107. * Clears all field information in the entity manager.
  108. *
  109. * It clears the background, the walkmesh, any pending actions and all the field entities.
  110. */
  111. void ClearField() override;
  112. /**
  113. * Clears all battle information in the entity manager.
  114. *
  115. * It clears any pending actions and all the battle entities.
  116. */
  117. void ClearBattle() override;
  118. /**
  119. * Removes a battle entity, for example one made for a visual effect.
  120. *
  121. * If the manager is not in the battle module, or the entity doesn't exist, it will do
  122. * nothing.
  123. *
  124. * @param[in] name Entity name.
  125. */
  126. void RemoveBattleEntity(const Ogre::String& name);
  127. /**
  128. * Removes a world map entity (like a pole that was added while playing).
  129. *
  130. * If the manager is not in the world module, or the entity doesn't exist, it will do nothing.
  131. *
  132. * @param[in] name Entity name.
  133. */
  134. void RemoveWorldMapEntity(const Ogre::String& name);
  135. /**
  136. * Clears all world map information in the entity manager.
  137. *
  138. * It clears the background, the walkmesh, any pending actions and all the world entities.
  139. */
  140. void ClearWorld() override;
  141. /**
  142. * Retrieves the walkmesh.
  143. *
  144. * @return The walkmesh.
  145. */
  146. Walkmesh* GetWalkmesh();
  147. /**
  148. * Checks a proposed world-map position against managed solid entities.
  149. *
  150. * @param[in] moving_entity Entity being moved.
  151. * @param[in] position Proposed base position in world-map coordinates.
  152. * @return True if the entity would overlap another solid entity.
  153. */
  154. bool IsWorldPositionBlocked(
  155. const Entity* moving_entity, const Ogre::Vector3& position
  156. ) const;
  157. /**
  158. * Retrieves the 2D background.
  159. *
  160. * @return The 2D background.
  161. */
  162. Background2D* GetBackground2D();
  163. /**
  164. * Retrieves the coloured fade of the screen.
  165. *
  166. * @return The screen fade.
  167. */
  168. ScreenFade* GetScreenFade();
  169. /**
  170. * Retrieves the movie player.
  171. *
  172. * @return The movie player.
  173. */
  174. MoviePlayer* GetMoviePlayer();
  175. /**
  176. * Adds an entity to the manager.
  177. *
  178. * The entity will be added to the EntityManager current module entity list.
  179. *
  180. * @param[in] name Entity name.
  181. * @param[in] file_name Path to the entity model file.
  182. * @param[in] position Entity position.
  183. * @param[in] direction Entity face direction.
  184. * @param[in] index Index of the entity on the manager.
  185. */
  186. void AddEntity(
  187. const Ogre::String& name, const Ogre::String& file_name,
  188. const Ogre::Vector3& position, const Ogre::Degree& direction, int index
  189. );
  190. /**
  191. * Adds a field entity to the manager.
  192. *
  193. * The entity will be added to the EntityManager current module entity list.
  194. *
  195. * @param[in] name Entity name.
  196. * @param[in] file_name Path to the entity model file.
  197. * @param[in] position Entity position.
  198. * @param[in] rotation Entity face direction.
  199. * @param[in] scale Entity scale.
  200. * @param[in] root_orientation Entity orientation. Unused for battle and world map entities.
  201. * @param[in] index Index of the entity on the manager.
  202. */
  203. void AddEntity(
  204. const Ogre::String& name, const Ogre::String& file_name,
  205. const Ogre::Vector3& position, const Ogre::Degree& rotation,
  206. const Ogre::Vector3& scale, const Ogre::Quaternion& root_orientation, const int index
  207. );
  208. /**
  209. * Adds a managed world-map entity using an explicit resource group.
  210. *
  211. * @param[in] name Entity name.
  212. * @param[in] file_name Mesh resource name.
  213. * @param[in] position Initial position.
  214. * @param[in] rotation Initial rotation.
  215. * @param[in] scale Initial scale.
  216. * @param[in] resource_group Ogre resource group containing the mesh.
  217. * @return The managed entity, or nullptr if the manager is not in world mode.
  218. */
  219. EntityModel* AddWorldMapEntity(
  220. const Ogre::String& name, const Ogre::String& file_name,
  221. const Ogre::Vector3& position, const Ogre::Degree& rotation,
  222. const Ogre::Vector3& scale, const Ogre::String& resource_group
  223. );
  224. /**
  225. * Adds an entity to the manager.
  226. *
  227. * Simplified version of {@see AddEntity}, to be called from Lua scripts.
  228. *
  229. * @param[in] name Entity name.
  230. * @param[in] file_name Path to the entity model file.
  231. * @param[in] x X coordinate of the entity position in the map.
  232. * @param[in] y Y coordinate of the entity position in the map.
  233. * @param[in] z Z coordinate of the entity position in the map.
  234. * @param[in] direction Entity face direction.
  235. * @param[in] index Index of the entity on the manager.
  236. */
  237. void ScriptAddEntity(
  238. const char* name, const char* file_name,
  239. const float x, const float y, const float z, const float direction, int index
  240. );
  241. /**
  242. * Adds an entity trigger to the manager.
  243. *
  244. * A trigger is a line that does something when approached or crossed. If the Battle manager
  245. * is in battle mode, nothing will be done.
  246. *
  247. * @param[in] name Entity trigger name.
  248. * @param[in] point1 One point of the trigger line.
  249. * @param[in] point2 One point of the trigger line.
  250. * @param[in] enabled True to enable the trigger, false to leave it disabled.
  251. */
  252. void AddEntityTrigger(
  253. const Ogre::String& name, const Ogre::Vector3& point1,
  254. const Ogre::Vector3& point2, const bool enabled
  255. );
  256. /**
  257. * Adds an entity point to the manager.
  258. *
  259. * If the Battle manager is in battle mode, nothing will be done.
  260. *
  261. * @param[in] name Entity point name.
  262. * @param[in] position Entity point position.
  263. * @param[in] rotation The point orientation.
  264. */
  265. void AddEntityPoint(
  266. const Ogre::String& name, const Ogre::Vector3& position, const float rotation
  267. );
  268. /**
  269. * Adds an entity script to the manager.
  270. *
  271. * @param[in] name Entity script name.
  272. */
  273. void AddEntityScript(const Ogre::String& name);
  274. /**
  275. * Adds an entity script to the manager.
  276. *
  277. * @param[in] name Entity script name.
  278. */
  279. void ScriptAddEntityScript(const char* name);
  280. /**
  281. * Retrieves an entity by name.
  282. *
  283. * Only entities of the current active module will be searched.
  284. *
  285. * @param[in] name Name of the entity to retrieve.
  286. * @return The entity by the specified name, or nullptr if there is no one.
  287. */
  288. Entity* GetEntity(const Ogre::String& name) const;
  289. /**
  290. * Retrieves an entity by it's index in the field.
  291. *
  292. * Only entities of the current active module will be searched.
  293. *
  294. * @param[in] id Index of the entity to retrieve.
  295. * @return The entity with the ID, or nullptr if there is no one.
  296. */
  297. Entity* GetEntityFromIndex(const int id) const;
  298. /**
  299. * Retrieves an entity by it's assigned character ID.
  300. *
  301. * Only entities of the current active module will be searched.
  302. *
  303. * @param[in] id Character ID of the entity to retrieve.
  304. * @return The entity assigned to the character, or nullptr if there is no one.
  305. */
  306. Entity* GetEntityFromCharacterId(const int id) const;
  307. /**
  308. * Retrieves an entity by name.
  309. *
  310. * Only entities of the current active module will be searched.
  311. *
  312. * @param[in] name Name of the entity to retrieve.
  313. * @return The entity by the specified name, or nullptr if there is no one.
  314. */
  315. Entity* ScriptGetEntity(const char* name) const;
  316. /**
  317. * Retrieves an entity point by name.
  318. *
  319. * @param[in] name Name of the entity point to retrieve.
  320. * @return The entity point by the specified name, or nullptr if there is no one or the
  321. * manager is in battle mode.
  322. */
  323. EntityPoint* ScriptGetEntityPoint(const char* name) const;
  324. /**
  325. * Sets the playable entity.
  326. *
  327. * If no entities are found by name, no one will be assigned, the previous playable entity
  328. * will remain so, and no warning will be issued. Also, if the EntityManager is in battle
  329. * mode, nothing will be done.
  330. *
  331. * @param[in] name Name of the entity to make playable.
  332. */
  333. void ScriptSetPlayerEntity(const char* name);
  334. void ScriptPlacePlayerAtFieldEntry(
  335. const float x, const float y, const int triangle_id, const float direction
  336. );
  337. /**
  338. * Retrieves the playable entity.
  339. *
  340. * @return The playable entity, or nullptr if it's not defined or the EntityManager is in
  341. * battle mode.
  342. */
  343. Entity* ScriptGetPlayerEntity() const;
  344. /**
  345. * Unsets any playable entities.
  346. *
  347. * If the EntityManager is in battle mode, nothing will be done.
  348. */
  349. void ScriptUnsetPlayerEntity();
  350. /**
  351. * Locks or unlocks player control of the playable entity.
  352. *
  353. * If the EntityManager is in battle mode, nothing will be done.
  354. *
  355. * @param[in] lock True to lock, false to unlock.
  356. */
  357. void ScriptPlayerLock(const bool lock);
  358. /**
  359. * Sets if the player can slide along the walls (SLIP in the field scripts).
  360. *
  361. * When it is off, a move of the player that is blocked by a wall is not turned into a
  362. * move along the wall. It is on by default and when a field is loaded.
  363. *
  364. * @param[in] slip True to let the player slide.
  365. */
  366. void ScriptSetPlayerSlip(const bool slip);
  367. /**
  368. * Retrieves the height of a point of a walkmesh triangle.
  369. *
  370. * @param[in] triangle The triangle.
  371. * @param[in] x X coordinate of the point.
  372. * @param[in] y Y coordinate of the point.
  373. * @param[in] fallback Value returned if there is no such triangle.
  374. * @return The height of the triangle at the point.
  375. */
  376. float GetTriangleHeight(int triangle, float x, float y, float fallback) const;
  377. /**
  378. * Sets if the player moves running by default.
  379. *
  380. * If true, the player runs unless the run key is held, which makes it walk. If false (the
  381. * default), the player walks unless the run key is held. To be called from Lua scripts
  382. * (movement setting of the config menu).
  383. *
  384. * @param[in] run True to run by default, false to walk by default.
  385. */
  386. void SetPlayerDefaultRun(const bool run);
  387. /**
  388. * Checks if the player movement is locked.
  389. *
  390. * @return True if it is locked.
  391. */
  392. bool IsPlayerLocked() const;
  393. /**
  394. * Sets the baseline rotation for the player controlled entity.
  395. *
  396. * If the EntityManager is in battle mode, nothing will be done.
  397. *
  398. * @param[in] rotation Baseline rotation.
  399. * @todo Verify this description.
  400. */
  401. void SetPlayerMoveRotation(const Ogre::Radian rotation);
  402. /**
  403. * Gets the multiplier of the random encounter rate (the encounter_multiplier cvar).
  404. *
  405. * @return The multiplier. 0 prevents random encounters.
  406. */
  407. float GetEncounterMultiplier() const;
  408. /**
  409. * Checks if the random encounter checks are logged (the debug_encounters cvar).
  410. *
  411. * @return True if they are logged.
  412. */
  413. bool IsDebugEncounters() const;
  414. /**
  415. * Enables or disables random encounters in the field.
  416. *
  417. * @param[in] active True to enable encounters, false to deactivate them.
  418. */
  419. void SetRandomEncounters(bool active);
  420. /**
  421. * Removes the random encounter tables of the field.
  422. */
  423. void ClearEncounterTables();
  424. /**
  425. * Hides the entities of the field or world map, while a battle takes place.
  426. *
  427. * Only the visible ones are hidden. They are shown again with
  428. * {@see ShowFieldEntities}.
  429. */
  430. void HideFieldEntities();
  431. /**
  432. * Shows the entities hidden by {@see HideFieldEntities}.
  433. */
  434. void ShowFieldEntities();
  435. /**
  436. * Adds a random encounter table to the field.
  437. *
  438. * @param[in] id Table ID (1 or 2).
  439. * @param[in] rate Encounter rate, as in the original game (24 is very low, 240 is high).
  440. * @param[in] battles Standard encounters, pairs of battle ID and probability (out of 64).
  441. * @param[in] specials Special encounters, with the same format.
  442. */
  443. void AddEncounterTable(
  444. const int id, const int rate, const std::vector<std::pair<int, int>>& battles,
  445. const std::vector<std::pair<int, int>>& specials
  446. );
  447. /**
  448. * Selects the encounter table to use.
  449. *
  450. * To be called from Lua scripts.
  451. *
  452. * @param[in] id Table ID (1 or 2). If it doesn't exist, no encounters happen.
  453. */
  454. void ScriptSetEncounterTable(const int id);
  455. /**
  456. * Picks the battle of a random encounter from the current encounter table.
  457. *
  458. * To be called from Lua scripts, also used by the engine.
  459. *
  460. * @return The battle ID, -1 if the field has no encounters.
  461. */
  462. int ScriptRollEncounterBattle() const;
  463. /**
  464. * Checks if a key is being pressed.
  465. *
  466. * @param[in] key_code The code of the key to test.
  467. * @return True if the key is being pressed, false otherwise.
  468. */
  469. bool IsKeyOn(unsigned int key_code);
  470. /**
  471. * Checks if a key is not being pressed.
  472. *
  473. * @param[in] key_code The code of the key to test.
  474. * @return False if the key is being pressed, true otherwise.
  475. */
  476. bool IsKeyOff(unsigned int key_code);
  477. /**
  478. * Checks if a script has asked for the Start button a moment ago.
  479. *
  480. * The scenes that use Start for their own ends (blin70_3, to end the choice of the party)
  481. * ask for it every frame, so the pause menu must not open with the same press.
  482. *
  483. * @return True if a script is asking for the Start button.
  484. */
  485. bool IsStartKeyRead() const;
  486. /**
  487. * Moves the line of a line entity (SLINE).
  488. *
  489. * @param[in] name Name of the entity.
  490. * @param[in] x1 First vertex of the line.
  491. * @param[in] y1 First vertex of the line.
  492. * @param[in] z1 First vertex of the line.
  493. * @param[in] x2 Second vertex of the line.
  494. * @param[in] y2 Second vertex of the line.
  495. * @param[in] z2 Second vertex of the line.
  496. */
  497. void ScriptSetLine(
  498. const char* name, const float x1, const float y1, const float z1, const float x2,
  499. const float y2, const float z2
  500. );
  501. /**
  502. * Assigns a character to an entity.
  503. *
  504. * Only entities of the currently active module can be assigned
  505. *
  506. * @param[in] entity_name The entity name.
  507. * @param[in] char_id The character ID.
  508. */
  509. void SetEntityToCharacter(const char* entity_name, unsigned int char_id);
  510. /**
  511. * Sets the 3D model for the background.
  512. *
  513. * @param[in] name name for the background entity, for debugging purposes only.
  514. * @param[in] file_name Path to the 3D model file.
  515. */
  516. void SetBackground3D(const Ogre::String& name, const Ogre::String& file_name);
  517. private:
  518. /**
  519. * Calculates the point elevation over a plane.
  520. *
  521. * @param[in] point Point to calculate the elevation of.
  522. * @param[in] a First point of the plane.
  523. * @param[in] b Second point of the plane.
  524. * @param[in] c Third point of the plane.
  525. * @return The elevation of the point.
  526. */
  527. static float PointElevation(
  528. const Ogre::Vector2& point,
  529. const Ogre::Vector3& a, const Ogre::Vector3& b, const Ogre::Vector3& c
  530. );
  531. /**
  532. * Determines at which side of a line a point is.
  533. *
  534. * @param[in] point The point to evaluate.
  535. * @param[in] p1 The first point of the line.
  536. * @param[in] p2 The second point of the line.
  537. * @return A negative value if the point is on the "left" of the line vector, a positive
  538. * value if the point is on the "right" of the vector line, or 0 if the point is part of
  539. * the line. "Left" and "right" are determined by going from p1 to p2.
  540. */
  541. static float SideOfVector(
  542. const Ogre::Vector2& point, const Ogre::Vector2& p1, const Ogre::Vector2& p2
  543. );
  544. /**
  545. * Calculates the square distance between a point and a line.
  546. *
  547. * The calculated distance is the vector orthogonal to the line that passes by the point,
  548. * i.e. the shortest distance.
  549. *
  550. * @param[in] point The point.
  551. * @param[in] point_a First point of the line.
  552. * @param[in] point_b Second point of the line.
  553. * @param[out] proj Vector representing the shortest distance.
  554. * @return The distance from the point to the line.
  555. */
  556. static float SquareDistanceToLine(
  557. const Ogre::Vector3& point, const Ogre::Vector3& point_a, const Ogre::Vector3& point_b,
  558. Ogre::Vector3& proj
  559. );
  560. /**
  561. * Calculates the direction degree between to points.
  562. *
  563. * @param[in] current_point Origin point.
  564. * @param[in] direction_point Next point in the direction.
  565. * @return Degrees between the two points
  566. */
  567. static Ogre::Degree GetDirectionToPoint(
  568. const Ogre::Vector3& current_point, const Ogre::Vector3& direction_point
  569. );
  570. /**
  571. * Scale factor for battle background models.
  572. */
  573. static const float SCENE_SCALE;
  574. /**
  575. * Entity ID for battle background entity.
  576. */
  577. static const unsigned int BATTLE_BACKGROUND_ID;
  578. /**
  579. * Entity ID for world map background entity.
  580. */
  581. static const unsigned int WORLD_MAP_BACKGROUND_ID;
  582. /**
  583. * Updates the field entities in the manager.
  584. */
  585. void UpdateField() override;
  586. /**
  587. * Updates the battle entities in the manager.
  588. */
  589. void UpdateBattle() override;
  590. /**
  591. * Updates the world map entities in the manager.
  592. */
  593. void UpdateWorld() override;
  594. /**
  595. * Adds a field entity to the manager.
  596. *
  597. * @param[in] name Entity name.
  598. * @param[in] file_name Path to the entity model file.
  599. * @param[in] position Entity position in the map.
  600. * @param[in] rotation Entity face direction.
  601. * @param[in] scale Entity scale.
  602. * @param[in] root_orientation Map orientation. Unused for world map entities.
  603. * @param[in] index Index of the entity on the map.
  604. */
  605. void AddFieldOrWorldMapEntity(
  606. const Ogre::String& name, const Ogre::String& file_name,
  607. const Ogre::Vector3& position, const Ogre::Degree& rotation,
  608. const Ogre::Vector3& scale, const Ogre::Quaternion& root_orientation, const int index
  609. );
  610. /**
  611. * Adds a battle entity to the manager.
  612. *
  613. * If the manager is not in the battle module, it will do nothing.
  614. *
  615. * @param[in] name Entity name.
  616. * @param[in] file_name Path to the entity model file.
  617. * @param[in] position Entity position.
  618. * @param[in] orientation Entity face direction.
  619. * @param[in] scale Entity scale.
  620. * @param[in] root_orientation The node orientation.
  621. * @param[in] index Index of the entity.
  622. */
  623. void AddBattleEntity(
  624. const Ogre::String& name, const Ogre::String& file_name,
  625. const Ogre::Vector3& position, const Ogre::Degree& orientation,
  626. const Ogre::Vector3& scale, const Ogre::Quaternion& root_orientation, const int index
  627. );
  628. /**
  629. * Attaches an entity to the walkmesh.
  630. *
  631. * It sets the triangle from the entity position coordinates. To account for multiple
  632. * triangles on different levels, it uses only the X and Y coordinates, and automatically
  633. * sets the Z one to the closest triangle. If the manager is in battle mode, it will do
  634. * nothing.
  635. *
  636. * @param[in] entity Entity to attach.
  637. * @return True if the entity was assigned to a walkmesh triangle, false if the entity is
  638. * not in a triangle or if the manager is in battle mode.
  639. */
  640. bool SetEntityOnWalkmesh(Entity* entity);
  641. /**
  642. * Moves an entity in the walkmesh.
  643. *
  644. * If the manager is in battle mode, it will do nothing.
  645. *
  646. * @param[in] entity Entity to move.
  647. * @param[in] speed Movement speed.
  648. * @param[in] delta_time Time elapsed for this movement step.
  649. * @return True if the movement was possible and the entity was moved, false otherwise.
  650. * If the manager is in battle mode, it will always return false.
  651. */
  652. bool PerformWalkmeshMove(Entity* entity, const float speed, const float delta_time);
  653. /**
  654. * Checks if the entity is crossing a walkmesh triangle border.
  655. *
  656. * @param[in] entity Entity to check.
  657. * @param[in] position The position. @todo document more.
  658. * @param[in] move_vector The move vector. @todo document more.
  659. * @return True if the entity is crossing a triangle border, false otherwise. If the manager
  660. * is in battle mode, it will always return false.
  661. */
  662. bool WalkmeshBorderCross(
  663. Entity* entity, Ogre::Vector3& position, const Ogre::Vector2& move_vector
  664. );
  665. /**
  666. * Checks for collisions of the entity with other entities.
  667. *
  668. * @param[in] entity Entity to check for collisions.
  669. * @param[in] position Position of the entity.
  670. * @return True if the entity is colliding with another, false otherwise. If the entity is
  671. * not solid, always false. If the manager is in battle mode, it will always return false.
  672. */
  673. bool CheckSolidCollisions(Entity* entity, Ogre::Vector3& position);
  674. /**
  675. * Checks for entity triggers near a entity.
  676. *
  677. * If there are triggers, and the conditions are met, the appropriate trigger function will
  678. * be added to the queue. This must be tested every time an entity moves. If the entity is
  679. * not the playable character, is not solid or is locked, it will do nothing. If the manager
  680. * is in battle mode, it will do nothing.
  681. *
  682. * @param[in] entity Entity to check for nearby triggers.
  683. * @param[in] position The position of the entity.
  684. */
  685. void CheckTriggers(Entity* entity, const Ogre::Vector3& position);
  686. /**
  687. * Checks if an entity can be interacted.
  688. *
  689. * It checks if there are entities that can be interacted with from the players current
  690. * position and orientation. If there are, the most appropriate one is selected and, if it
  691. * has an on_interact script, it is run. If the manager is in battle mode, it will do
  692. * nothing.
  693. */
  694. void CheckEntityInteract();
  695. /**
  696. * Runs the contact script (script 2) of the entities that the player touches.
  697. *
  698. * The script of an entity that the original game runs when the player collides with it
  699. * (the old man of condor1 starts his conversation that way, and only after it can be
  700. * talked to). It runs once for each touch.
  701. */
  702. void CheckEntityContacts();
  703. /**
  704. * Calculates the next offset step for an entity.
  705. *
  706. * @param[in] entity The entity.
  707. */
  708. void SetNextOffsetStep(Entity* entity);
  709. /**
  710. * Calculates the next turn step for an entity
  711. *
  712. * @param[in] entity The entity.
  713. */
  714. void SetNextTurnStep(Entity* entity);
  715. /**
  716. * Calculates and sets the next position during a linear movement.
  717. *
  718. * If the manager is in battle mode, it will do nothing
  719. *
  720. * @param[in] entity The moving entity.
  721. */
  722. void SetNextLinearStep(Entity* entity);
  723. /**
  724. * Calculates and sets the next position during a jump.
  725. *
  726. * If the manager is in battle mode, it will do nothing
  727. *
  728. * @param[in] entity The jump entity.
  729. */
  730. void SetNextJumpStep(Entity* entity);
  731. /**
  732. * @todo Understand and document.
  733. */
  734. void SetNextScrollStep();
  735. /**
  736. * The map walkmesh.
  737. */
  738. Walkmesh walkmesh_;
  739. /**
  740. * The map background
  741. */
  742. Background2D background_2d_;
  743. /**
  744. * The movie player. It has to be created before the screen fade, which is drawn over it.
  745. */
  746. MoviePlayer movie_player_;
  747. /**
  748. * The coloured fade of the screen. It has to be created after the background, because the
  749. * fade must be drawn over it.
  750. */
  751. ScreenFade screen_fade_;
  752. /**
  753. * The entity table name.
  754. */
  755. Ogre::String entity_table_name_;
  756. /**
  757. * The list of field or world entities.
  758. */
  759. std::vector<Entity*> entity_;
  760. /**
  761. * The list of battle entities.
  762. */
  763. std::vector<Entity*> battle_entity_;
  764. /**
  765. * A 3D background.
  766. */
  767. Entity* background_3d_;
  768. /**
  769. * The player controlled entity.
  770. */
  771. Entity* player_entity_;
  772. /**
  773. * The playable entity current movement indicator.
  774. *
  775. * Applies only for manual movement.
  776. */
  777. Ogre::Vector3 player_move_;
  778. /**
  779. * The playable entity current movement turn indicator.
  780. *
  781. * Applies only for manual movement.
  782. */
  783. Ogre::Radian player_move_rotation_;
  784. /**
  785. * Indicates if player control is locked.
  786. */
  787. bool player_lock_;
  788. /**
  789. * Indicates if the player slides along the walls (see ScriptSetPlayerSlip).
  790. */
  791. bool player_slip_ = true;
  792. /**
  793. * Indicates if the playable is being moved by running.
  794. *
  795. * Applies only for manual movement.
  796. */
  797. bool player_run_;
  798. /// After entering a field from the world map, the walk keys that were held are ignored until
  799. /// they are released, so the player starts standing still.
  800. bool ignore_held_walk_ = false;
  801. /// Entities that the player is touching.
  802. std::set<const Entity*> entities_in_contact_;
  803. /// Last time that a script asked for the Start button.
  804. std::chrono::steady_clock::time_point start_key_read_time_;
  805. /**
  806. * Indicates if the player runs by default, and the run key makes it walk instead.
  807. */
  808. bool player_default_run_ = false;
  809. /**
  810. * List of triggers.
  811. */
  812. std::vector<EntityTrigger*> entity_triggers_;
  813. /**
  814. * List of points.
  815. */
  816. std::vector<EntityPoint*> entity_points_;
  817. /**
  818. * List of scripts.
  819. */
  820. std::vector<Ogre::String> entity_scripts_;
  821. /**
  822. * The scene node.
  823. */
  824. Ogre::SceneNode* scene_node_;
  825. /**
  826. * @todo Understand and document.
  827. */
  828. Ogre::Entity* grid_;
  829. /**
  830. * @todo Understand and document.
  831. */
  832. Ogre::Entity* axis_;
  833. /**
  834. * Indicates if random encounters happen in the map.
  835. */
  836. bool random_encounters_;
  837. /**
  838. * The encounter rate of the map.
  839. */
  840. float encounter_rate_;
  841. /**
  842. * Random encounter table of a field.
  843. */
  844. struct EncounterTable{
  845. /**
  846. * Encounter rate, as in the original game.
  847. */
  848. int rate;
  849. /**
  850. * Standard encounters: battle ID and probability (out of 64).
  851. */
  852. std::vector<std::pair<int, int>> battles;
  853. /**
  854. * Special encounters: battle ID and probability (out of 64).
  855. */
  856. std::vector<std::pair<int, int>> specials;
  857. };
  858. /**
  859. * Random encounter tables of the field, by ID.
  860. */
  861. std::map<int, EncounterTable> encounter_tables_;
  862. /**
  863. * Entities that were visible before a battle, hidden while it lasts.
  864. */
  865. std::vector<Entity*> entities_hidden_by_battle_;
  866. /**
  867. * ID of the encounter table in use.
  868. */
  869. int encounter_table_;
  870. /**
  871. * Distance walked by the player since the last step.
  872. */
  873. float encounter_distance_;
  874. /**
  875. * Position of the player in the last frame, to know how much it has walked.
  876. */
  877. Ogre::Vector3 encounter_last_position_;
  878. /**
  879. * Indicates if {@see encounter_last_position_} is valid.
  880. */
  881. bool encounter_last_position_valid_;
  882. /**
  883. * Steps that must be taken before encounters can happen again.
  884. */
  885. int encounter_safe_steps_;
  886. /**
  887. * Accumulated chance of a random encounter, out of 256 * 256. It grows while walking.
  888. */
  889. int encounter_accumulator_ = 0;
  890. /**
  891. * Last random battle, which is not repeated twice in a row. -1 if none.
  892. */
  893. int encounter_last_battle_ = -1;
  894. /**
  895. * Counts the steps of the player and starts random encounters.
  896. *
  897. * @param[in] walked True if the player was moved by the player's input in this frame.
  898. */
  899. void UpdateEncounters(const bool walked);
  900. };