1 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
2 // Copyright (C) 2008 Ole Laursen
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Library General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include "herotemplates.h"
22 #include "armysetlist.h"
23 #include "playerlist.h"
25 #include "heroproto.h"
26 #include "xmlhelper.h"
29 HeroTemplates* HeroTemplates::d_instance = 0;
31 HeroTemplates* HeroTemplates::getInstance()
34 d_instance = new HeroTemplates();
39 void HeroTemplates::deleteInstance()
48 HeroTemplates::HeroTemplates()
53 HeroTemplates::~HeroTemplates()
55 for (unsigned int i = 0; i < MAX_PLAYERS; ++i)
56 for (std::vector<HeroProto *>::iterator j = d_herotemplates[i].begin();
57 j != d_herotemplates[i].end(); ++j)
61 HeroProto *HeroTemplates::getRandomHero(Hero::Gender gender, int player_id)
63 std::vector<HeroProto*> heroes;
64 for (unsigned int i = 0; i < d_herotemplates[player_id].size(); i++)
66 if (Hero::Gender(d_herotemplates[player_id][i]->getGender()) == gender)
67 heroes.push_back (d_herotemplates[player_id][i]);
69 if (heroes.size() == 0)
70 return getRandomHero(player_id);
72 int num = rand() % heroes.size();
76 HeroProto *HeroTemplates::getRandomHero(int player_id)
78 int num = rand() % d_herotemplates[player_id].size();
79 return d_herotemplates[player_id][num];
82 int HeroTemplates::loadHeroTemplates()
84 const Armysetlist* al = Armysetlist::getInstance();
86 d_male_heroes.clear();
87 d_female_heroes.clear();
89 // list all the army types that are heroes.
90 Player *p = Playerlist::getInstance()->getNeutral();
91 for (unsigned int j = 0; j < al->getSize(p->getArmyset()); j++)
93 const ArmyProto *a = al->getArmy (p->getArmyset(), j);
96 if (a->getGender() == Hero::FEMALE)
97 d_female_heroes.push_back(a);
99 d_male_heroes.push_back(a);
102 XML_Helper helper(File::getMiscFile("heronames.xml"), std::ios::in, false);
104 helper.registerTag("herotemplate", sigc::mem_fun((*this), &HeroTemplates::load));
108 std::cerr << "Error, while loading a template from heronames.xml" <<std::endl <<std::flush;
117 bool HeroTemplates::load(std::string tag, XML_Helper *helper)
119 if (tag == "herotemplate")
122 helper->getData(name, "name");
124 helper->getData(owner, "owner");
125 std::string gender_str;
126 if (owner >= (int) MAX_PLAYERS)
128 helper->getData(gender_str, "gender");
130 gender = Hero::genderFromString(gender_str);
132 const ArmyProto *herotype = NULL;
133 if (gender == Hero::MALE)
135 if (d_male_heroes.size() > 0)
136 herotype = d_male_heroes[rand() % d_male_heroes.size()];
138 else if (gender == Hero::FEMALE)
140 if (d_female_heroes.size() > 0)
141 herotype = d_female_heroes[rand() % d_female_heroes.size()];
143 if (herotype == NULL)
145 if (d_male_heroes.size() > 0)
146 herotype = d_male_heroes[rand() % d_male_heroes.size()];
147 else if (d_female_heroes.size() > 0)
148 herotype = d_female_heroes[rand() % d_female_heroes.size()];
150 if (herotype == NULL)
152 HeroProto *newhero = new HeroProto (*herotype);
153 newhero->setOwnerId(owner);
155 newhero->setName (_(name.c_str()));
156 d_herotemplates[owner].push_back (newhero);