WorldmapModule.h 19 KB

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