WorldmapModule.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 <OgreFrameListener.h>
  17. #include <OgreSingleton.h>
  18. #include <OgreVector3.h>
  19. #include <deque>
  20. #include <map>
  21. #include <set>
  22. #include <vector>
  23. #include "common/TypeDefine.h"
  24. #include "engine/WorldMapInterface.h"
  25. class EntityModel;
  26. namespace VGears {
  27. /**
  28. * The world map module.
  29. */
  30. class WorldmapModule :
  31. public Ogre::Singleton<WorldmapModule>, public Ogre::FrameListener, public WorldMapInterface{
  32. public:
  33. enum class VehicleType{
  34. ON_FOOT,
  35. CHOCOBO,
  36. BUGGY,
  37. HIGHWIND,
  38. SUBMARINE,
  39. TINY_BRONCO
  40. };
  41. WorldmapModule();
  42. virtual ~WorldmapModule();
  43. /**
  44. * Initializes the world map module
  45. *
  46. * @param[in] map_id 0 is the overworld (WM0), 1 is WM2 and 2 is WM3. If another map
  47. * was already loaded, it is replaced.
  48. */
  49. void Init(const int map_id = 0);
  50. void Suspend() override;
  51. bool frameStarted(const Ogre::FrameEvent& event) override;
  52. void ClearVehicleTerrains();
  53. void AddVehicleTerrain(const char* terrain_name);
  54. Ogre::String GetTerrainTypeUnderEntity(const char* entity_name) const override;
  55. int GetTerrainTriangleUnderEntity(const char* entity_name) const override;
  56. bool UnboardVehicle(const char* vehicle_name);
  57. /**
  58. * Hides the world map and stops it while a battle is on.
  59. */
  60. void PauseForBattle() override;
  61. /**
  62. * Shows the world map again after a battle.
  63. */
  64. void ResumeAfterBattle() override;
  65. /**
  66. * Prevents or allows random encounters (the SetBattleLock opcode).
  67. *
  68. * @param[in] locked True to prevent them.
  69. */
  70. void SetEncountersLocked(const bool locked);
  71. /**
  72. * Sets the orientation of the model of a world map entity, in degrees. It's the
  73. * orientation of the model itself, so the heading of the entity is added to it. It is
  74. * for finding the right orientation of a model.
  75. *
  76. * @param[in] entity_name Name of the entity, like "WorldMapHighwind".
  77. * @param[in] x Rotation in X.
  78. * @param[in] y Rotation in Y.
  79. * @param[in] z Rotation in Z.
  80. */
  81. /**
  82. * Moves the model of a world map entity up or down, in world units, without moving
  83. * the entity. It is for finding the right height of a model.
  84. *
  85. * @param[in] entity_name Name of the entity, like "WorldMapSubmarine".
  86. * @param[in] y Height of the model. Negative is down, 0 is the position of the entity.
  87. */
  88. void SetModelOffset(const char* entity_name, const float y);
  89. /**
  90. * Puts a world map entity at a fixed height of the world, keeping its x and z.
  91. *
  92. * @param[in] entity_name Name of the entity, like "WorldMapemerald_30".
  93. * @param[in] y Height in world units, not relative to the ground.
  94. */
  95. void SetEntityAltitude(const char* entity_name, const float y);
  96. /**
  97. * Sets where an entity of the overworld appears when the map is loaded, instead of its
  98. * default spawn point. It is used by the debug data, see debug_data.lua.
  99. *
  100. * @param[in] entity_name Name of the entity: "Cloud", "Chocobo", "Highwind", "Buggy",
  101. * "Submarine" or "TinyBronco".
  102. * @param[in] x Position in X, in world units.
  103. * @param[in] z Position in Z, in world units.
  104. */
  105. void SetSpawn(const char* entity_name, const float x, const float z);
  106. /// Sets a movement speed (world units per second) by name: walk, chocobo, buggy, tiny_bronco,
  107. /// highwind or submarine (WorldMapSettings.speed in world_settings.lua).
  108. void SetSpeed(const char* name, const float speed);
  109. /// Sets the camera of a vehicle by name (the ones of the speeds, and submarine_underwater for the
  110. /// submarine of WM2): distance to the vehicle in world units and angle over the ground in degrees
  111. /// (WorldMapSettings.camera in world_settings.lua).
  112. void SetCamera(const char* name, const float distance, const float pitch_degrees);
  113. /**
  114. * Hides a vehicle of the overworld that the story hasn't given yet: it is not visible,
  115. * solid nor talkable, so it can't be boarded. Has to be called before the map is created.
  116. *
  117. * @param[in] entity_name Name of the vehicle: "Chocobo", "Highwind", "Buggy",
  118. * "Submarine" or "TinyBronco".
  119. */
  120. void SetVehicleHidden(const char* entity_name);
  121. /**
  122. * Enters a world map from a field: the map is always built again (its entities, scripts and
  123. * models are for the story of now), unlike Init, which keeps the one that was suspended.
  124. *
  125. * @param[in] map_id Map to enter, 0 WM0, 1 WM2, 2 WM3.
  126. */
  127. void Enter(const int map_id);
  128. /**
  129. * Creates a model of the world that the scripts place (scenery like the rocket or the
  130. * Gold Saucer), at a position of the world. The model is not solid.
  131. *
  132. * @param[in] name Name of the entity, without the prefix "WorldMap".
  133. * @param[in] hrc Name of the HRC file of the model, like "cfc".
  134. * @param[in] x Position in X, in world units.
  135. * @param[in] z Position in Z, in world units.
  136. * @param[in] raw_scale Scale that the game stores with the model: 0x200 is 1.
  137. * @param[in] direction Direction that the script gives, from 0 to 255 for a full turn.
  138. * @param[in] rotation_x Rotation of the model itself in X, in degrees.
  139. * @param[in] rotation_y Rotation of the model itself in Y, in degrees.
  140. * @param[in] rotation_z Rotation of the model itself in Z, in degrees.
  141. */
  142. void AddWorldModelFromScript(
  143. const char* name, const char* hrc, const float x, const float z, const int raw_scale,
  144. const int direction, const float rotation_x, const float rotation_y,
  145. const float rotation_z
  146. );
  147. void SetModelOrientation(const char* entity_name, const float x, const float y, const float z);
  148. /// Turns a model of the scripts to a direction of the game (a full turn is 256).
  149. void SetModelHeading(const char* entity_name, const float direction);
  150. /// The direction of the game (0 to 255) that a model faces, 0 if it doesn't exist.
  151. int GetModelHeading(const char* entity_name);
  152. private:
  153. struct TerrainTriangle{
  154. Ogre::Vector3 vertices[3];
  155. uint8 walkability_type;
  156. uint8 function_id;
  157. int mesh_x;
  158. int mesh_y;
  159. /// Encounter region of the triangle, 0-15.
  160. uint8 region;
  161. };
  162. /**
  163. * Random encounters of a region and terrain group, from ENC_W.BIN.
  164. */
  165. struct EncounterRecord{
  166. bool enabled;
  167. /// Lower values mean more frequent encounters.
  168. int rate;
  169. /// Walkability type that selects this record.
  170. int terrain;
  171. /// Words 1-15 of the record: battle ID (10 bits) and probability (6 bits).
  172. uint16 words[15];
  173. };
  174. void DestroyWorldMap();
  175. /**
  176. * State of an entity of the overworld, kept while another map is loaded.
  177. */
  178. struct EntityState{
  179. Ogre::String name;
  180. Ogre::Vector3 position;
  181. Ogre::Quaternion orientation;
  182. bool visible;
  183. bool solid;
  184. bool talkable;
  185. };
  186. /**
  187. * Saves the state of the entities of the overworld, to restore it when it is loaded
  188. * again.
  189. */
  190. void SaveOverworldSnapshot();
  191. /**
  192. * Puts the entities of the overworld where they were when the snapshot was saved.
  193. */
  194. void RestoreOverworldSnapshot();
  195. /**
  196. * Gets the spawn point of an entity: the one that the debug data sets, or the default.
  197. */
  198. Ogre::Vector3 GetSpawn(
  199. const Ogre::String& entity_name, const Ogre::Vector3& default_spawn
  200. ) const;
  201. /// Spawn points that replace the default ones.
  202. std::map<Ogre::String, Ogre::Vector2> spawn_overrides_;
  203. /// Vehicles that the story hasn't given.
  204. std::set<Ogre::String> hidden_vehicles_;
  205. void CreateWorldMap(Ogre::SceneManager* scene_manager);
  206. void CreatePlayer();
  207. void CreateCompanionModels();
  208. void AddWorldModel(
  209. const Ogre::String& name, const Ogre::String& mesh_name,
  210. const Ogre::Vector3& desired_position, Ogre::Real target_size,
  211. const Ogre::Quaternion& orientation,
  212. Ogre::Real collision_radius_scale = 1.0f,
  213. bool talkable = false,
  214. Ogre::Real explicit_scale = 0.0f
  215. );
  216. void UpdateCamera(Ogre::Real delta_time, bool snap_to_player);
  217. /// Snow map (WM3): the camera turns by itself every few seconds.
  218. void UpdateSnowCamera(Ogre::Real delta_time);
  219. /// Snow map (WM3): puts a pole where the player is (only 4 poles exist, the oldest goes away).
  220. void PlaceSnowPole();
  221. bool FindWalkableSurface(
  222. const Ogre::Vector3& position, Ogre::Vector3& surface,
  223. bool grass_only = false, bool require_walkable = true,
  224. uint8* walkability_type = nullptr,
  225. size_t* triangle_index = nullptr,
  226. bool use_index = true
  227. ) const;
  228. /// Groups the triangles of the terrain by the cell of a grid they touch, so the search of the
  229. /// triangle under a position only checks a few of them.
  230. void BuildTerrainIndex();
  231. bool IsActiveTerrainAllowed(uint8 walkability_type) const;
  232. /**
  233. * Runs the function of a triangle when it is entered, if it changed.
  234. */
  235. void EnterTriangle(const TerrainTriangle& triangle);
  236. /**
  237. * Plays the animation that each vehicle has in its state: parked, boarded, or boarded
  238. * and moving.
  239. *
  240. * @param[in] moving True if the active entity is moving.
  241. */
  242. void UpdateVehicleAnimations(const bool moving);
  243. /**
  244. * Takes the submarine from WM0 to WM2, if it is over deep water inside the underwater
  245. * map. It keeps the position and the heading.
  246. *
  247. * @param[in] submarine The submarine. The entities are replaced if it dives.
  248. * @return True if it dived.
  249. */
  250. bool DiveToUnderwaterMap(EntityModel* submarine);
  251. /**
  252. * Takes the submarine from WM2 back to WM0, if the same position is deep water in
  253. * WM0. It keeps the position and the heading.
  254. *
  255. * @param[in] submarine The submarine. The entities are replaced if it surfaces.
  256. * @return True if it surfaced.
  257. */
  258. bool SurfaceToOverworld(EntityModel* submarine);
  259. /**
  260. * Starts the animation of a dive or a surfacing: the submarine goes under the surface
  261. * (or up), the screen fades to black, the map changes, and the screen fades back while
  262. * the submarine arrives. The request of the dive or the surfacing has to be set.
  263. *
  264. * @param[in] dive True for a dive from WM0 to WM2, false for a surfacing.
  265. * @param[in] submarine The submarine.
  266. */
  267. void BeginTransition(const bool dive, EntityModel* submarine);
  268. /**
  269. * Moves the animation of the dive or the surfacing forward. It changes the map in the
  270. * middle of it.
  271. *
  272. * @param[in] delta_time Seconds since the last frame.
  273. */
  274. void UpdateTransition(const Ogre::Real delta_time);
  275. /**
  276. * Ends the animation: the screen is shown and the player can move again.
  277. */
  278. void EndTransition();
  279. /**
  280. * Puts the submarine at the position of a surfacing and makes it the one in control.
  281. */
  282. void PlaceSurfacedSubmarine();
  283. /**
  284. * Moves a flying vehicle: forward with the action button, left and right turn, up and
  285. * down change the height.
  286. *
  287. * @param[in] entity The vehicle.
  288. * @param[in] delta_time Seconds since the last frame.
  289. * @param[in] highwind True for the Highwind, false for the submarine of WM2.
  290. */
  291. void UpdateFlight(EntityModel* entity, const Ogre::Real delta_time, const bool highwind);
  292. /**
  293. * Gets the name of a vehicle in the scripts (WorldMapSettings).
  294. *
  295. * @param[in] vehicle The vehicle.
  296. * @return Its name, "walk" on foot.
  297. */
  298. static const char* GetVehicleScriptName(const VehicleType vehicle);
  299. void TriggerTriangleFunction(const TerrainTriangle& triangle);
  300. void LoadEncounters();
  301. void UpdateEncounters(const float delta_time, const TerrainTriangle& triangle);
  302. int RollEncounterBattle(const EncounterRecord& record) const;
  303. std::vector<EncounterRecord> encounter_records_;
  304. float encounter_frames_;
  305. int encounter_accumulator_;
  306. int encounter_last_battle_;
  307. bool encounters_locked_;
  308. bool paused_for_battle_;
  309. std::vector<TerrainTriangle> terrain_triangles_;
  310. /// Indices of the triangles (ascending) that touch each cell of the grid, row by row.
  311. std::vector<std::vector<uint32>> terrain_grid_;
  312. float terrain_grid_min_x_ = 0.0f;
  313. float terrain_grid_min_z_ = 0.0f;
  314. float terrain_grid_cell_ = 1.0f;
  315. int terrain_grid_columns_ = 0;
  316. int terrain_grid_rows_ = 0;
  317. Ogre::SceneManager* scene_manager_;
  318. Ogre::SceneNode* world_map_node_;
  319. EntityModel* player_entity_;
  320. EntityModel* highwind_entity_;
  321. EntityModel* buggy_entity_;
  322. EntityModel* chocobo_entity_;
  323. EntityModel* submarine_entity_;
  324. EntityModel* tiny_bronco_entity_;
  325. VehicleType current_vehicle_;
  326. std::vector<uint8> active_vehicle_terrain_types_;
  327. Ogre::Real player_height_offset_;
  328. int active_function_mesh_index_;
  329. int active_function_id_;
  330. /// Loaded map ID, -1 if none.
  331. int map_id_;
  332. /// Size of the map in blocks.
  333. size_t grid_width_;
  334. size_t grid_height_;
  335. /// Rotation of the camera around the active entity, in radians. Positive is to the right.
  336. float camera_yaw_;
  337. /// True while the active entity is a flying vehicle.
  338. bool flight_active_;
  339. /// Flying vehicle that the flight state belongs to.
  340. EntityModel* flight_entity_;
  341. /// Rotation of the vehicle around the vertical axis, in radians.
  342. float flight_heading_;
  343. /// Height of the vehicle.
  344. float flight_y_;
  345. /// Position of the first block of the map in the 9x7 block grid of the world.
  346. size_t origin_x_;
  347. size_t origin_y_;
  348. bool initialized_;
  349. bool frame_listener_attached_;
  350. bool cancel_was_pressed_;
  351. bool confirm_was_pressed_;
  352. /// True while the function of a triangle is triggered by the landing of the Highwind.
  353. bool landing_trigger_ = false;
  354. /// Triangle of the northern cave that the Highwind has just landed on (a trigger waiting).
  355. const TerrainTriangle* landing_after_unboard_ = nullptr;
  356. /// Seconds since the music was checked, and the vehicle it was checked with.
  357. float music_timer_ = 1.0f;
  358. VehicleType music_vehicle_ = VehicleType::ON_FOOT;
  359. /// Speeds set by the scripts, by name (the ones that are not set use the default ones).
  360. std::map<std::string, float> speeds_;
  361. float GetSpeed(const char* name, float default_speed) const;
  362. struct CameraSettings{
  363. float distance;
  364. float pitch_degrees;
  365. };
  366. /// Cameras set by the scripts, by name (the ones that are not set use the default one).
  367. std::map<std::string, CameraSettings> camera_settings_;
  368. /// Snow map: seconds until the next automatic turn of the camera.
  369. float snow_camera_timer_ = 0.0f;
  370. /// Snow map: radians left of the turn that is going on, with the sign of its direction.
  371. float snow_camera_remaining_ = 0.0f;
  372. /// Snow map: the poles that exist (names of the entities), the oldest first.
  373. std::deque<std::string> snow_poles_;
  374. unsigned int snow_pole_counter_ = 0;
  375. enum class TransitionStage{
  376. /// No animation going on.
  377. NONE,
  378. /// The submarine moves away, in the map where it was.
  379. LEAVING,
  380. /// The screen goes black.
  381. FADING_OUT,
  382. /// The submarine comes into the new map and the screen is shown again.
  383. ARRIVING
  384. };
  385. TransitionStage transition_stage_;
  386. /// True for a dive, false for a surfacing.
  387. bool transition_dive_;
  388. float transition_timer_;
  389. /// Heights where the submarine is moving between in the current stage.
  390. float transition_start_y_;
  391. float transition_target_y_;
  392. /// State of the entities of WM0 while WM2 is loaded.
  393. std::vector<EntityState> overworld_snapshot_;
  394. /// A surfacing to WM0 is going on: the submarine appears at the position and heading
  395. /// below.
  396. bool surface_requested_;
  397. Ogre::Vector3 surface_position_;
  398. float surface_heading_;
  399. /// A dive to WM2 is going on: the submarine appears at the position and heading below.
  400. bool dive_requested_;
  401. Ogre::Vector3 dive_position_;
  402. float dive_heading_;
  403. };
  404. }