WorldmapSubmarine.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include "modules/worldmap/WorldmapModule.h"
  16. #include <algorithm>
  17. #include <vector>
  18. #include <OgreEntity.h>
  19. #include <OgreLogManager.h>
  20. #include <OgreSceneNode.h>
  21. #include "engine/entity/EntityManager.h"
  22. #include "engine/entity/EntityModel.h"
  23. #include "engine/ui/UiManager.h"
  24. #include "engine/ui/UiWidget.h"
  25. #include "modules/worldmap/WorldmapData.h"
  26. namespace VGears {
  27. using namespace Ogre;
  28. /// How deep the submarine goes under the surface of WM0 when diving, in world units.
  29. static const float DIVE_SINK_DEPTH = 0.5f;
  30. /// Time the submarine takes to go under the surface of WM0, or to come up to it.
  31. static const float DIVE_SINK_TIME = 1.0f;
  32. /// Height where the submarine appears in WM2 when diving, and where it goes when surfacing.
  33. /// It's over the top layer of the map.
  34. static const float DIVE_HIGH_HEIGHT = 1.0f;
  35. /// Time the submarine takes to go down from, or up to, the height above.
  36. static const float DIVE_HIGH_TIME = 1.5f;
  37. /// Time of the fade to black before the map changes.
  38. static const float DIVE_FADE_OUT_TIME = 0.5f;
  39. /**
  40. * Gets if a position of the overworld (WM0) is over deep water. Used when the submarine
  41. * comes back from WM2, when WM0 is not loaded. Only the part of the world that WM2 covers is
  42. * read, and it's done once.
  43. */
  44. static bool IsOverworldDeepWater(const float x, const float z){
  45. struct Triangle{
  46. float x[3];
  47. float z[3];
  48. };
  49. static std::vector<Triangle> triangles;
  50. static bool loaded = false;
  51. if (!loaded){
  52. loaded = true;
  53. const MapLayout overworld = GetMapLayout(0);
  54. const MapLayout underwater = GetMapLayout(1);
  55. const float space = From16BitFixedPoint(8182);
  56. const std::vector<VGears::WorldmapFileSerializer::Block> blocks = LoadWorldMap("WM0.MAP");
  57. for (size_t block_index = 0; block_index < std::min(blocks.size(), size_t(63)); ++block_index){
  58. const int map_x = block_index % overworld.width;
  59. const int map_y = block_index / overworld.width;
  60. if (map_x < underwater.origin_x || map_x >= underwater.origin_x + underwater.width
  61. || map_y < underwater.origin_y || map_y >= underwater.origin_y + underwater.height){
  62. continue;
  63. }
  64. const VGears::WorldmapFileSerializer::Block& block = blocks[block_index];
  65. for (size_t mesh_index = 0; mesh_index < block.meshes.size(); ++mesh_index){
  66. const VGears::WorldmapFileSerializer::BlockPart& part = block.meshes[mesh_index];
  67. const float pos_x = ((mesh_index % 4) + map_x * 4) * space;
  68. const float pos_z = ((mesh_index / 4) + map_y * 4) * space;
  69. for (const VGears::WorldmapFileSerializer::BlockTriangle& triangle : part.triangles){
  70. if (triangle.walkability_info_ != VGears::WorldmapFileSerializer::SEA_DEEP_WATER){
  71. continue;
  72. }
  73. const size_t indices[3] = {
  74. triangle.vertex_0_index, triangle.vertex_1_index, triangle.vertex_2_index
  75. };
  76. Triangle result;
  77. for (int i = 0; i < 3; ++i){
  78. const VGears::WorldmapFileSerializer::Vertex& vertex = part.vertices.at(indices[i]);
  79. result.x[i] = From16BitFixedPoint(vertex.x) + pos_x;
  80. result.z[i] = From16BitFixedPoint(vertex.z) + pos_z;
  81. }
  82. triangles.push_back(result);
  83. }
  84. }
  85. }
  86. }
  87. for (const Triangle& t : triangles){
  88. const float denominator
  89. = (t.z[1] - t.z[2]) * (t.x[0] - t.x[2]) + (t.x[2] - t.x[1]) * (t.z[0] - t.z[2]);
  90. if (Ogre::Math::Abs(denominator) < 1e-6f) continue;
  91. const float a = ((t.z[1] - t.z[2]) * (x - t.x[2]) + (t.x[2] - t.x[1]) * (z - t.z[2]))
  92. / denominator;
  93. const float b = ((t.z[2] - t.z[0]) * (x - t.x[2]) + (t.x[0] - t.x[2]) * (z - t.z[2]))
  94. / denominator;
  95. const float c = 1.0f - a - b;
  96. if (a >= -1e-4f && b >= -1e-4f && c >= -1e-4f) return true;
  97. }
  98. return false;
  99. }
  100. bool WorldmapModule::DiveToUnderwaterMap(EntityModel* submarine){
  101. const Ogre::Vector3 position = submarine->GetPosition();
  102. Ogre::Vector3 surface;
  103. uint8 walkability_type = 0;
  104. if (!FindWalkableSurface(position, surface, false, false, &walkability_type)
  105. || walkability_type != VGears::WorldmapFileSerializer::SEA_DEEP_WATER){
  106. return false;
  107. }
  108. // WM2 only covers a part of the world.
  109. if (!IsInsideMap(1, position.x, position.z)){
  110. LogManager::getSingleton().logMessage(
  111. "The submarine can't dive here: it's outside the underwater map."
  112. );
  113. return false;
  114. }
  115. dive_requested_ = true;
  116. dive_position_ = position;
  117. dive_heading_ = submarine->GetModel()->getParentSceneNode()->getParentSceneNode()
  118. ->getOrientation().getYaw().valueRadians();
  119. BeginTransition(true, submarine);
  120. return true;
  121. }
  122. /**
  123. * Sets how opaque the black cover of the screen is.
  124. *
  125. * @param[in] alpha 0 shows the world map, 1 is all black.
  126. */
  127. static void SetScreenCover(const float alpha){
  128. UiWidget* cover = UiManager::getSingleton().GetWidget("Fade.Screen");
  129. if (cover == nullptr) return;
  130. cover->SetColour(0, 0, 0);
  131. cover->SetAlpha(std::max(0.0f, std::min(1.0f, alpha)));
  132. }
  133. /// Eases a progress from 0 to 1.
  134. static float Smooth(const float t){
  135. const float clamped = std::max(0.0f, std::min(1.0f, t));
  136. return clamped * clamped * (3.0f - 2.0f * clamped);
  137. }
  138. void WorldmapModule::BeginTransition(const bool dive, EntityModel* submarine){
  139. transition_stage_ = TransitionStage::LEAVING;
  140. transition_dive_ = dive;
  141. transition_timer_ = 0;
  142. transition_start_y_ = submarine->GetPosition().y;
  143. // Diving: it sinks under the surface. Surfacing: it goes up, out of the map.
  144. transition_target_y_ = dive ? transition_start_y_ - DIVE_SINK_DEPTH : DIVE_HIGH_HEIGHT;
  145. EntityManager::getSingleton().ScriptPlayerLock(true);
  146. }
  147. void WorldmapModule::EndTransition(){
  148. transition_stage_ = TransitionStage::NONE;
  149. SetScreenCover(0);
  150. EntityManager::getSingleton().ScriptPlayerLock(false);
  151. }
  152. void WorldmapModule::UpdateTransition(const Ogre::Real delta_time){
  153. transition_timer_ += delta_time;
  154. ::Entity* submarine = EntityManager::getSingleton().ScriptGetPlayerEntity();
  155. if (submarine == nullptr){
  156. EndTransition();
  157. return;
  158. }
  159. const auto set_height = [submarine](const float y){
  160. Ogre::Vector3 position = submarine->GetPosition();
  161. position.y = y;
  162. submarine->SetPosition(position);
  163. };
  164. if (transition_stage_ == TransitionStage::LEAVING){
  165. const float duration = transition_dive_ ? DIVE_SINK_TIME : DIVE_HIGH_TIME;
  166. set_height(
  167. transition_start_y_
  168. + (transition_target_y_ - transition_start_y_) * Smooth(transition_timer_ / duration)
  169. );
  170. if (transition_timer_ >= duration){
  171. transition_stage_ = TransitionStage::FADING_OUT;
  172. transition_timer_ = 0;
  173. }
  174. }
  175. else if (transition_stage_ == TransitionStage::FADING_OUT){
  176. SetScreenCover(transition_timer_ / DIVE_FADE_OUT_TIME);
  177. if (transition_timer_ >= DIVE_FADE_OUT_TIME){
  178. // The map changes under the black screen. The entities are not valid after it.
  179. Init(transition_dive_ ? 1 : 0);
  180. ::Entity* arrived = EntityManager::getSingleton().GetEntity("WorldMapSubmarine");
  181. if (arrived == nullptr){
  182. EndTransition();
  183. return;
  184. }
  185. transition_target_y_ = arrived->GetPosition().y;
  186. // Diving: it comes from high up. Surfacing: it comes from under the surface.
  187. transition_start_y_ = transition_dive_
  188. ? DIVE_HIGH_HEIGHT : transition_target_y_ - DIVE_SINK_DEPTH;
  189. Ogre::Vector3 position = arrived->GetPosition();
  190. position.y = transition_start_y_;
  191. arrived->SetPosition(position);
  192. transition_stage_ = TransitionStage::ARRIVING;
  193. transition_timer_ = 0;
  194. EntityManager::getSingleton().ScriptPlayerLock(true);
  195. UpdateCamera(0, true);
  196. }
  197. }
  198. else if (transition_stage_ == TransitionStage::ARRIVING){
  199. const float duration = transition_dive_ ? DIVE_HIGH_TIME : DIVE_SINK_TIME;
  200. set_height(
  201. transition_start_y_
  202. + (transition_target_y_ - transition_start_y_) * Smooth(transition_timer_ / duration)
  203. );
  204. // The screen is shown again by the time the submarine is at its height.
  205. SetScreenCover(1.0f - transition_timer_ / duration);
  206. if (transition_timer_ >= duration){
  207. set_height(transition_target_y_);
  208. EndTransition();
  209. }
  210. }
  211. }
  212. bool WorldmapModule::SurfaceToOverworld(EntityModel* submarine){
  213. const Ogre::Vector3 position = submarine->GetPosition();
  214. // The same position of WM0 has to be deep water.
  215. if (!IsOverworldDeepWater(position.x, position.z)){
  216. LogManager::getSingleton().logMessage(
  217. "The submarine can't surface here: it's not deep water in the overworld."
  218. );
  219. return false;
  220. }
  221. surface_requested_ = true;
  222. surface_position_ = position;
  223. surface_heading_ = submarine->GetModel()->getParentSceneNode()->getParentSceneNode()
  224. ->getOrientation().getYaw().valueRadians();
  225. BeginTransition(false, submarine);
  226. return true;
  227. }
  228. void WorldmapModule::PlaceSurfacedSubmarine(){
  229. surface_requested_ = false;
  230. if (submarine_entity_ == nullptr) return;
  231. Ogre::Vector3 surface;
  232. if (!FindWalkableSurface(surface_position_, surface, false, false)) return;
  233. submarine_entity_->SetPosition(surface);
  234. submarine_entity_->GetModel()->getParentSceneNode()->getParentSceneNode()->setOrientation(
  235. Ogre::Quaternion(Ogre::Radian(surface_heading_), Ogre::Vector3::UNIT_Y)
  236. );
  237. // Cloud boards the submarine, like the boarding script does.
  238. player_entity_->SetVisible(false);
  239. player_entity_->SetSolid(false);
  240. player_entity_->SetTalkable(false);
  241. ClearVehicleTerrains();
  242. for (const char* terrain : {"SEA_DEEP_WATER", "SEA", "SUBMARINE_PEN"}) AddVehicleTerrain(terrain);
  243. EntityManager::getSingleton().ScriptSetPlayerEntity("WorldMapSubmarine");
  244. UpdateCamera(0, true);
  245. }
  246. } // namespace VGears