PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/EnemyGroupManager.cpp

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