EntityManagerEncounters.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <cstdlib>
  16. #include "engine/config/ConfigVar.h"
  17. #include "engine/entity/EntityManager.h"
  18. #include "common/Logger.h"
  19. #include "engine/script/ScriptManager.h"
  20. #include "engine/Timer.h"
  21. ConfigVar cv_encounter_multiplier(
  22. "encounter_multiplier", "Multiplier of the random encounter rate (0 disables them)", "1"
  23. );
  24. ConfigVar cv_debug_encounters("debug_encounters", "Log random encounter checks", "false");
  25. /**
  26. * Frames of walking (at 30 frames per second) between two encounter checks. The original game
  27. * checks every 8 frames.
  28. */
  29. static const float ENCOUNTER_CHECK_FRAMES = 8.0f;
  30. /**
  31. * Value added to the encounter accumulator in each check, before dividing it by the rate of the
  32. * table. The original game uses the speed of the player, which is not known here, so this is tuned
  33. * to a battle every 15-30 seconds of walking in most fields. The encounter_multiplier cvar scales
  34. * it.
  35. */
  36. static const float ENCOUNTER_STEP_VALUE = 5.0f;
  37. float EntityManager::GetEncounterMultiplier() const{return cv_encounter_multiplier.GetF();}
  38. bool EntityManager::IsDebugEncounters() const{return cv_debug_encounters.GetB();}
  39. void EntityManager::SetRandomEncounters(bool active){random_encounters_ = active;}
  40. void EntityManager::ClearEncounterTables(){
  41. encounter_tables_.clear();
  42. encounter_table_ = 1;
  43. encounter_distance_ = 0.0f;
  44. encounter_last_position_valid_ = false;
  45. encounter_accumulator_ = 0;
  46. encounter_last_battle_ = -1;
  47. }
  48. void EntityManager::AddEncounterTable(
  49. const int id, const int rate, const std::vector<std::pair<int, int>>& battles,
  50. const std::vector<std::pair<int, int>>& specials
  51. ){
  52. EncounterTable table;
  53. table.rate = rate;
  54. table.battles = battles;
  55. table.specials = specials;
  56. encounter_tables_[id] = table;
  57. }
  58. void EntityManager::ScriptSetEncounterTable(const int id){encounter_table_ = id;}
  59. int EntityManager::ScriptRollEncounterBattle() const{
  60. auto found = encounter_tables_.find(encounter_table_);
  61. if (found == encounter_tables_.end()) return -1;
  62. const EncounterTable& table = found->second;
  63. // Like the original game: a roll out of 64 is checked against the special battles one after
  64. // the other (each one adds its probability). If none is hit, a new roll chooses a standard
  65. // battle, the last one being the default.
  66. int roll = std::rand() % 64;
  67. int sum = 0;
  68. for (const auto& special : table.specials){
  69. sum += special.second;
  70. if (roll < sum) return special.first;
  71. }
  72. if (table.battles.empty()) return -1;
  73. roll = std::rand() % 64;
  74. sum = 0;
  75. for (const auto& battle : table.battles){
  76. sum += battle.second;
  77. if (roll < sum) return battle.first;
  78. }
  79. return table.battles.back().first;
  80. }
  81. void EntityManager::UpdateEncounters(const bool walked){
  82. if (player_entity_ == nullptr){
  83. encounter_last_position_valid_ = false;
  84. return;
  85. }
  86. const Ogre::Vector3 position = player_entity_->GetPosition();
  87. const float distance
  88. = encounter_last_position_valid_ ? (position - encounter_last_position_).length() : 0.0f;
  89. encounter_last_position_ = position;
  90. encounter_last_position_valid_ = true;
  91. // Only walking counts: not scripted movements, and not jumps to another place.
  92. if (!walked || player_lock_ || distance > 2.0f) return;
  93. if (!random_encounters_ || encounter_tables_.empty()) return;
  94. auto found = encounter_tables_.find(encounter_table_);
  95. if (found == encounter_tables_.end()) return;
  96. const float multiplier = cv_encounter_multiplier.GetF();
  97. if (multiplier <= 0.0f) return;
  98. // Same as the original game. Every few frames of walking the accumulator grows by a value
  99. // divided by the rate (the lower the rate, the more often the encounters), and an encounter
  100. // starts if a random number is lower than the accumulator.
  101. const int rate = found->second.rate;
  102. if (rate <= 0) return;
  103. encounter_distance_ += Timer::getSingleton().GetGameTimeDelta() * 30.0f;
  104. while (encounter_distance_ >= ENCOUNTER_CHECK_FRAMES){
  105. encounter_distance_ -= ENCOUNTER_CHECK_FRAMES;
  106. // Enemy Away and Enemy Lure materia change the rate of the party (see Party.get_encounter_modifier).
  107. const float materia_multiplier
  108. = ScriptManager::getSingleton().EvaluateNumber("Party.get_encounter_modifier()", 1.0f);
  109. encounter_accumulator_ += static_cast<int>(ENCOUNTER_STEP_VALUE * multiplier * materia_multiplier * 256.0f) / rate;
  110. const int roll = std::rand() % 256 * 256;
  111. if (cv_debug_encounters.GetB()){
  112. LOG_TRIVIAL(
  113. "Encounter check: rate " + std::to_string(rate) + ", accumulator "
  114. + std::to_string(encounter_accumulator_ / 256.0f) + "/256, roll "
  115. + std::to_string(roll / 256)
  116. );
  117. }
  118. if (roll >= encounter_accumulator_) continue;
  119. int battle = ScriptRollEncounterBattle();
  120. // The same battle is not repeated: it's rolled again once.
  121. if (battle == encounter_last_battle_) battle = ScriptRollEncounterBattle();
  122. if (battle < 0) return;
  123. encounter_distance_ = 0.0f;
  124. encounter_accumulator_ = 0;
  125. encounter_last_battle_ = battle;
  126. LOG_TRIVIAL("Random encounter: battle " + std::to_string(battle));
  127. // The battle is started by a script, as it has to change the module of every manager.
  128. ScriptManager::getSingleton().RunString("Encounter.start(" + std::to_string(battle) + ")");
  129. return;
  130. }
  131. }