FieldEncounters.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <map>
  17. #include <utility>
  18. #include <vector>
  19. #include <OgreVector3.h>
  20. class Entity;
  21. /**
  22. * The random battles of a field: its encounter tables, and the count of the steps of the player
  23. * that starts them.
  24. */
  25. class FieldEncounters{
  26. public:
  27. /**
  28. * Gets the multiplier of the random encounter rate (the encounter_multiplier cvar).
  29. *
  30. * @return The multiplier. 0 prevents random encounters.
  31. */
  32. static float GetMultiplier();
  33. /**
  34. * Checks if the random encounter checks are logged (the debug_encounters cvar).
  35. *
  36. * @return True if they are logged.
  37. */
  38. static bool IsDebug();
  39. /**
  40. * Enables or disables random encounters in the field.
  41. *
  42. * @param[in] active True to enable encounters, false to deactivate them.
  43. */
  44. void SetActive(bool active);
  45. /**
  46. * Removes the random encounter tables of the field, and forgets the walked distance.
  47. */
  48. void Clear();
  49. /**
  50. * Adds a random encounter table to the field.
  51. *
  52. * @param[in] id Table ID (1 or 2).
  53. * @param[in] rate Encounter rate, as in the original game (24 is very low, 240 is high).
  54. * @param[in] battles Standard encounters, pairs of battle ID and probability (out of 64).
  55. * @param[in] specials Special encounters, with the same format.
  56. */
  57. void AddTable(
  58. const int id, const int rate, const std::vector<std::pair<int, int>>& battles,
  59. const std::vector<std::pair<int, int>>& specials
  60. );
  61. /**
  62. * Selects the encounter table to use.
  63. *
  64. * @param[in] id Table ID (1 or 2). If it doesn't exist, no encounters happen.
  65. */
  66. void SetTable(const int id);
  67. /**
  68. * Picks the battle of a random encounter from the current encounter table.
  69. *
  70. * @return The battle ID, -1 if the field has no encounters.
  71. */
  72. int RollBattle() const;
  73. /**
  74. * Counts the steps of the player and starts random encounters.
  75. *
  76. * It has to be called once per frame.
  77. *
  78. * @param[in] player The player entity, or nullptr if there is none.
  79. * @param[in] player_locked True if the player can't be controlled.
  80. * @param[in] walked True if the player was moved by the player's input in this frame.
  81. */
  82. void Update(const Entity* player, const bool player_locked, const bool walked);
  83. private:
  84. /**
  85. * Random encounter table of a field.
  86. */
  87. struct Table{
  88. /**
  89. * Encounter rate, as in the original game.
  90. */
  91. int rate;
  92. /**
  93. * Standard encounters: battle ID and probability (out of 64).
  94. */
  95. std::vector<std::pair<int, int>> battles;
  96. /**
  97. * Special encounters: battle ID and probability (out of 64).
  98. */
  99. std::vector<std::pair<int, int>> specials;
  100. };
  101. /**
  102. * Indicates if random encounters happen in the map.
  103. */
  104. bool active_ = false;
  105. /**
  106. * Random encounter tables of the field, by ID.
  107. */
  108. std::map<int, Table> tables_;
  109. /**
  110. * ID of the encounter table in use.
  111. */
  112. int table_ = 1;
  113. /**
  114. * Frames walked by the player since the last encounter check.
  115. */
  116. float distance_ = 0.0f;
  117. /**
  118. * Position of the player in the last frame, to know how much it has walked.
  119. */
  120. Ogre::Vector3 last_position_ = Ogre::Vector3::ZERO;
  121. /**
  122. * Indicates if {@see last_position_} is valid.
  123. */
  124. bool last_position_valid_ = false;
  125. /**
  126. * Accumulated chance of a random encounter, out of 256 * 256. It grows while walking.
  127. */
  128. int accumulator_ = 0;
  129. /**
  130. * Last random battle, which is not repeated twice in a row. -1 if none.
  131. */
  132. int last_battle_ = -1;
  133. };