/src/EnemyGroupManager.cpp

http://github.com/clintbellanger/flare · C++ · 122 lines · 82 code · 18 blank · 22 comment · 34 complexity · e3093f043427ca6782daae252e8d2ce3 MD5 · raw file

  1. /*
  2. Copyright 2011 Thane Brimhall
  3. Manuel A. Fernandez Montecelo <manuel.montezelo@gmail.com>
  4. This file is part of FLARE.
  5. FLARE is free software: you can redistribute it and/or modify it under the terms
  6. of the GNU General Public License as published by the Free Software Foundation,
  7. either version 3 of the License, or (at your option) any later version.
  8. FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  10. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along with
  12. FLARE. If not, see http://www.gnu.org/licenses/
  13. */
  14. #include "EnemyGroupManager.h"
  15. #include "SharedResources.h"
  16. using namespace std;
  17. EnemyGroupManager* EnemyGroupManager::_instance = 0;
  18. EnemyGroupManager::EnemyGroupManager() {
  19. generate();
  20. }
  21. EnemyGroupManager::~EnemyGroupManager() {
  22. }
  23. EnemyGroupManager& EnemyGroupManager::instance() {
  24. if (_instance == 0) {
  25. _instance = new EnemyGroupManager;
  26. }
  27. return *(_instance);
  28. }
  29. void EnemyGroupManager::generate() {
  30. // load each enemies folder. Individual enemies can be overwritten with mods.
  31. for (unsigned int i = 0; i < mods->mod_list.size(); i++) {
  32. string dir = PATH_DATA + "mods/" + mods->mod_list[i] + "/enemies";
  33. vector<string> files;
  34. getFileList(dir, ".txt", files);
  35. for (size_t j = 0; j < files.size(); ++j) {
  36. parseEnemyFileAndStore(dir, files[j]);
  37. }
  38. }
  39. }
  40. void EnemyGroupManager::parseEnemyFileAndStore(const string& dir, const string& filename) {
  41. FileParser infile;
  42. if (infile.open(mods->locate("enemies/" + filename))) {
  43. Enemy_Level new_enemy;
  44. new_enemy.type = filename.substr(0, filename.length()-4); //removes the ".txt" from the filename
  45. while (infile.next()) {
  46. if (infile.key == "level") {
  47. new_enemy.level = atoi(infile.val.c_str());
  48. }
  49. else if (infile.key == "rarity") {
  50. new_enemy.rarity = infile.val.c_str();
  51. }
  52. else if (infile.key == "categories") {
  53. string cat;
  54. while ( (cat = infile.nextValue()) != "") {
  55. _categories[cat].push_back(new_enemy);
  56. }
  57. }
  58. }
  59. }
  60. infile.close();
  61. }
  62. Enemy_Level EnemyGroupManager::getRandomEnemy(const std::string& category, int minlevel, int maxlevel) const {
  63. vector<Enemy_Level> enemyCategory;
  64. map<string, vector<Enemy_Level> >::const_iterator it = _categories.find(category);
  65. if (it != _categories.end()) {
  66. enemyCategory = it->second;
  67. } else {
  68. return Enemy_Level();
  69. }
  70. // load only the data that fit the criteria
  71. vector<Enemy_Level> enemyCandidates;
  72. for (size_t i = 0; i < enemyCategory.size(); ++i) {
  73. Enemy_Level new_enemy = enemyCategory[i];
  74. if ((new_enemy.level >= minlevel) && (new_enemy.level <= maxlevel)) {
  75. // add more than one time to increase chance of getting
  76. // this enemy as result, "rarity" property
  77. int add_times = 0;
  78. if (new_enemy.rarity == "common") {
  79. add_times = 6;
  80. } else if (new_enemy.rarity == "uncommon") {
  81. add_times = 3;
  82. } else if (new_enemy.rarity == "rare") {
  83. add_times = 1;
  84. } else {
  85. fprintf(stderr,
  86. "ERROR: 'rarity' property for enemy '%s' not valid (common|uncommon|rare): %s\n",
  87. new_enemy.type.c_str(), new_enemy.rarity.c_str());
  88. }
  89. // do add, the given number of times
  90. for (int j = 0; j < add_times; ++j) {
  91. enemyCandidates.push_back(new_enemy);
  92. }
  93. }
  94. }
  95. if (enemyCandidates.empty()) {
  96. return Enemy_Level();
  97. } else {
  98. return enemyCandidates[rand() % enemyCandidates.size()];
  99. }
  100. }