WorldmapVehicles.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 <OgreLogManager.h>
  18. #include <OgreEntity.h>
  19. #include <OgreSceneNode.h>
  20. #include "engine/entity/EntityManager.h"
  21. #include "engine/entity/EntityModel.h"
  22. #include "engine/input/InputManager.h"
  23. #include "modules/worldmap/WorldmapConstants.h"
  24. #include "modules/worldmap/WorldmapData.h"
  25. namespace VGears {
  26. using namespace Ogre;
  27. void WorldmapModule::ClearVehicleTerrains(){
  28. active_vehicle_terrain_types_.clear();
  29. }
  30. void WorldmapModule::AddVehicleTerrain(const char* terrain_name){
  31. if (terrain_name == nullptr) return;
  32. const auto terrain = GetWorldMapTerrainTypes().find(terrain_name);
  33. if (terrain == GetWorldMapTerrainTypes().end()){
  34. LogManager::getSingleton().logMessage(
  35. "Unknown world-map vehicle terrain '" + Ogre::String(terrain_name) + "'."
  36. );
  37. return;
  38. }
  39. if (std::find(
  40. active_vehicle_terrain_types_.begin(), active_vehicle_terrain_types_.end(),
  41. terrain->second
  42. ) == active_vehicle_terrain_types_.end()){
  43. active_vehicle_terrain_types_.push_back(terrain->second);
  44. }
  45. }
  46. bool WorldmapModule::IsActiveTerrainAllowed(uint8 walkability_type) const{
  47. if (current_vehicle_ == VehicleType::ON_FOOT){
  48. return IsFootWalkable(walkability_type);
  49. }
  50. return std::find(
  51. active_vehicle_terrain_types_.begin(), active_vehicle_terrain_types_.end(),
  52. walkability_type
  53. ) != active_vehicle_terrain_types_.end();
  54. }
  55. /// Turning speed of the flying vehicles, in radians per second.
  56. static const float FLIGHT_TURN_SPEED = 1.5f;
  57. /// Speed up and down of the flying vehicles, in world units per second.
  58. static const float FLIGHT_CLIMB_SPEED = 1.0f;
  59. /// Highest height over the ground where the Highwind can be unboarded. It lands there.
  60. static const float HIGHWIND_MAX_LANDING_HEIGHT = 1.0f;
  61. /// Highest point the Highwind can reach.
  62. static const float HIGHWIND_MAX_HEIGHT = 1.0f;
  63. /**
  64. * Animations of a vehicle in the world map, from the A files of the model: when it's parked,
  65. * when it's boarded, and when it's boarded and moving. An empty name is no animation (the
  66. * model in its pose). The roles were found by playing them, and for the models with only one
  67. * animation it's the one of the model.
  68. */
  69. struct VehicleAnimations{
  70. const char* parked;
  71. const char* boarded;
  72. const char* moving;
  73. /// The parked animation shows its first frame and doesn't move.
  74. bool parked_paused;
  75. };
  76. static const VehicleAnimations CHOCOBO_ANIMATIONS = {"asc", "asc", "asd", false};
  77. static const VehicleAnimations BUGGY_ANIMATIONS = {"age", "aha", "aha", false};
  78. static const VehicleAnimations TINY_BRONCO_ANIMATIONS = {"dya", "dya", "dya", true};
  79. static const VehicleAnimations HIGHWIND_ANIMATIONS = {"cic", "cic", "cic", true};
  80. static const VehicleAnimations SUBMARINE_ANIMATIONS = {"dfe", "dfe", "dfe", true};
  81. /**
  82. * Plays the animation of a vehicle in its current state.
  83. */
  84. static void PlayVehicleAnimation(
  85. EntityModel* vehicle, const ::Entity* active_entity, const bool moving,
  86. const VehicleAnimations& animations
  87. ){
  88. if (vehicle == nullptr) return;
  89. const bool parked = vehicle != active_entity;
  90. const char* name = parked ? animations.parked
  91. : (moving ? animations.moving : animations.boarded);
  92. if (name[0] == '\0'){
  93. vehicle->StopAnimation();
  94. return;
  95. }
  96. if (parked && animations.parked_paused){
  97. vehicle->PlayAnimationPaused(name);
  98. return;
  99. }
  100. vehicle->UnfreezeAnimation();
  101. if (vehicle->HasAnimation(name)) vehicle->PlayAnimationContinue(name);
  102. }
  103. void WorldmapModule::UpdateVehicleAnimations(const bool moving){
  104. const ::Entity* active_entity = EntityManager::getSingleton().ScriptGetPlayerEntity();
  105. PlayVehicleAnimation(chocobo_entity_, active_entity, moving, CHOCOBO_ANIMATIONS);
  106. PlayVehicleAnimation(buggy_entity_, active_entity, moving, BUGGY_ANIMATIONS);
  107. PlayVehicleAnimation(tiny_bronco_entity_, active_entity, moving, TINY_BRONCO_ANIMATIONS);
  108. PlayVehicleAnimation(highwind_entity_, active_entity, moving, HIGHWIND_ANIMATIONS);
  109. PlayVehicleAnimation(submarine_entity_, active_entity, moving, SUBMARINE_ANIMATIONS);
  110. }
  111. void WorldmapModule::UpdateFlight(
  112. EntityModel* entity, const Ogre::Real delta_time, const bool highwind
  113. ){
  114. Ogre::SceneNode* node = entity->GetModel()->getParentSceneNode()->getParentSceneNode();
  115. if (flight_entity_ != entity){
  116. // Starts flying: continue from the current heading and height.
  117. flight_entity_ = entity;
  118. flight_heading_ = node->getOrientation().getYaw().valueRadians();
  119. flight_y_ = entity->GetPosition().y;
  120. camera_yaw_ = 0;
  121. }
  122. float turn = 0;
  123. float climb = 0;
  124. bool forward = false;
  125. // Scripts lock the player during events and transitions.
  126. if (!EntityManager::getSingleton().IsPlayerLocked()){
  127. InputManager& input = InputManager::getSingleton();
  128. // The heading decreases to turn right: the model faces +Z at heading 0 and the
  129. // right side of the camera behind it is -X.
  130. if (input.IsGameEventPressed("walk_left")) turn += 1.0f;
  131. if (input.IsGameEventPressed("walk_right")) turn -= 1.0f;
  132. // Inverted, like the controls of a plane: down climbs and up descends.
  133. if (input.IsGameEventPressed("walk_up")) climb -= 1.0f;
  134. if (input.IsGameEventPressed("walk_down")) climb += 1.0f;
  135. forward = input.IsGameEventPressed("CONFIRM");
  136. }
  137. flight_heading_ += turn * FLIGHT_TURN_SPEED * delta_time;
  138. if (flight_heading_ > Ogre::Math::PI) flight_heading_ -= Ogre::Math::TWO_PI;
  139. else if (flight_heading_ < -Ogre::Math::PI) flight_heading_ += Ogre::Math::TWO_PI;
  140. node->setOrientation(Ogre::Quaternion(Ogre::Radian(flight_heading_), Ogre::Vector3::UNIT_Y));
  141. const Ogre::Vector3 current_position = entity->GetPosition();
  142. Ogre::Vector3 requested_position = current_position;
  143. if (forward){
  144. const Ogre::Vector3 direction(
  145. Ogre::Math::Sin(Ogre::Radian(flight_heading_)), 0.0f,
  146. Ogre::Math::Cos(Ogre::Radian(flight_heading_))
  147. );
  148. requested_position += direction * delta_time * GetSpeed(
  149. highwind ? "highwind" : "submarine", From16BitFixedPoint(8182) * 0.45f * 2.0f
  150. );
  151. }
  152. // The terrain below. If the vehicle can't go there, it stays but can still go up or down.
  153. Ogre::Vector3 surface;
  154. size_t triangle_index = 0;
  155. if (!FindWalkableSurface(requested_position, surface, false, true, nullptr, &triangle_index)){
  156. requested_position = current_position;
  157. if (!FindWalkableSurface(
  158. requested_position, surface, false, false, nullptr, &triangle_index
  159. )) return;
  160. forward = false;
  161. }
  162. // The ground has priority over the maximum height, so the vehicle never goes through it.
  163. const Ogre::Real ceiling = highwind ? HIGHWIND_MAX_HEIGHT : UNDERWATER_MAX_HEIGHT;
  164. Ogre::Real new_y = flight_y_ + climb * FLIGHT_CLIMB_SPEED * delta_time;
  165. new_y = std::min(new_y, ceiling);
  166. new_y = std::max(new_y, surface.y + FLIGHT_MIN_CLEARANCE);
  167. const Ogre::Vector3 new_position(requested_position.x, new_y, requested_position.z);
  168. if (EntityManager::getSingleton().IsWorldPositionBlocked(entity, new_position)) return;
  169. flight_y_ = new_y;
  170. entity->SetPosition(new_position);
  171. if (forward) EnterTriangle(terrain_.GetTriangles()[triangle_index]);
  172. }
  173. /// Name of a vehicle in the scripts (WorldMapSettings).
  174. const char* WorldmapModule::GetVehicleScriptName(const VehicleType vehicle){
  175. switch (vehicle){
  176. case VehicleType::CHOCOBO: return "chocobo";
  177. case VehicleType::BUGGY: return "buggy";
  178. case VehicleType::HIGHWIND: return "highwind";
  179. case VehicleType::SUBMARINE: return "submarine";
  180. case VehicleType::TINY_BRONCO: return "tiny_bronco";
  181. default: return "walk";
  182. }
  183. }
  184. bool WorldmapModule::UnboardVehicle(const char* vehicle_name){
  185. if (vehicle_name == nullptr) return false;
  186. EntityManager& entity_manager = EntityManager::getSingleton();
  187. ::Entity* player_entity = entity_manager.ScriptGetPlayerEntity();
  188. ::Entity* vehicle_entity = entity_manager.GetEntity(vehicle_name);
  189. if (
  190. player_entity == nullptr || vehicle_entity == nullptr
  191. || player_entity != vehicle_entity || player_entity == player_entity_
  192. ){
  193. return false;
  194. }
  195. const Ogre::Vector3 vehicle_position = vehicle_entity->GetPosition();
  196. const Ogre::Real unboard_distance
  197. = vehicle_entity->GetSolidRadius() + player_entity_->GetSolidRadius() + 0.1f;
  198. const Ogre::Real diagonal_distance = unboard_distance * 0.70710678f;
  199. const Ogre::Vector3 directions[] = {
  200. Ogre::Vector3(1, 0, 0), Ogre::Vector3(-1, 0, 0),
  201. Ogre::Vector3(0, 0, 1), Ogre::Vector3(0, 0, -1),
  202. Ogre::Vector3(1, 0, 1), Ogre::Vector3(-1, 0, 1),
  203. Ogre::Vector3(1, 0, -1), Ogre::Vector3(-1, 0, -1)
  204. };
  205. for (size_t i = 0; i < sizeof(directions) / sizeof(directions[0]); ++i){
  206. const Ogre::Real distance = i < 4 ? unboard_distance : diagonal_distance;
  207. const Ogre::Vector3 requested_position
  208. = vehicle_position + directions[i] * distance;
  209. Ogre::Vector3 surface;
  210. uint8 walkability_type = 0;
  211. if (!FindWalkableSurface(
  212. requested_position, surface, false, false, &walkability_type
  213. ) || !IsFootWalkable(walkability_type)){
  214. continue;
  215. }
  216. surface.y += player_height_offset_;
  217. if (entity_manager.IsWorldPositionBlocked(player_entity_, surface)) continue;
  218. if (vehicle_entity == highwind_entity_){
  219. // The Highwind lands. It can't be left higher up, Cloud couldn't board it again.
  220. Ogre::Vector3 ground;
  221. if (!FindWalkableSurface(vehicle_position, ground, false, false)) return false;
  222. if (vehicle_position.y - ground.y > HIGHWIND_MAX_LANDING_HEIGHT) return false;
  223. vehicle_entity->SetPosition(Ogre::Vector3(vehicle_position.x, ground.y, vehicle_position.z));
  224. // Landing on the terrain of the northern cave enters it (the function of the triangle runs).
  225. uint8 landing_terrain = 0;
  226. size_t landing_triangle = 0;
  227. if (FindWalkableSurface(vehicle_position, ground, false, false, &landing_terrain, &landing_triangle)
  228. && landing_terrain == VGears::WorldmapFileSerializer::NORTHERND_CAVE
  229. && terrain_.GetTriangles()[landing_triangle].function_id >= 3){
  230. landing_after_unboard_ = &terrain_.GetTriangles()[landing_triangle];
  231. }
  232. }
  233. player_entity_->SetPosition(surface);
  234. // The boarding scripts hide Cloud and make him non-solid.
  235. player_entity_->SetVisible(true);
  236. player_entity_->SetSolid(true);
  237. vehicle_entity->SetTalkable(true);
  238. entity_manager.ScriptSetPlayerEntity("WorldMapCloud");
  239. ClearVehicleTerrains();
  240. if (landing_after_unboard_ != nullptr){
  241. // The Highwind is what entered (the vehicle of the trigger).
  242. current_vehicle_ = VehicleType::HIGHWIND;
  243. landing_trigger_ = true;
  244. TriggerTriangleFunction(*landing_after_unboard_);
  245. landing_trigger_ = false;
  246. landing_after_unboard_ = nullptr;
  247. }
  248. current_vehicle_ = VehicleType::ON_FOOT;
  249. return true;
  250. }
  251. return false;
  252. }
  253. } // namespace VGears