initial commit, lordsawar source, slightly modified
[lordsawar] / src / herotemplates.cpp
1 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
2 //  Copyright (C) 2008 Ole Laursen
3 //
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.
8 //
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.
13 //
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 
17 //  02110-1301, USA.
18
19 #include "herotemplates.h"
20
21 #include "File.h"
22 #include "armysetlist.h"
23 #include "playerlist.h"
24 #include "hero.h"
25 #include "heroproto.h"
26 #include "xmlhelper.h"
27 #include <sstream>
28
29 HeroTemplates* HeroTemplates::d_instance = 0;
30
31 HeroTemplates* HeroTemplates::getInstance()
32 {
33     if (!d_instance)
34         d_instance = new HeroTemplates();
35
36     return d_instance;
37 }
38
39 void HeroTemplates::deleteInstance()
40 {
41     if (d_instance != 0)
42         delete d_instance;
43
44     d_instance = 0;
45 }
46
47
48 HeroTemplates::HeroTemplates()
49 {
50   loadHeroTemplates();
51 }
52
53 HeroTemplates::~HeroTemplates()
54 {
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)
58       delete *j;
59 }
60
61 HeroProto *HeroTemplates::getRandomHero(Hero::Gender gender, int player_id)
62 {
63   std::vector<HeroProto*> heroes;
64   for (unsigned int i = 0; i < d_herotemplates[player_id].size(); i++)
65     {
66       if (Hero::Gender(d_herotemplates[player_id][i]->getGender()) == gender)
67         heroes.push_back (d_herotemplates[player_id][i]);
68     }
69   if (heroes.size() == 0)
70     return getRandomHero(player_id);
71
72   int num = rand() % heroes.size();
73   return heroes[num];
74 }
75
76 HeroProto *HeroTemplates::getRandomHero(int player_id)
77 {
78   int num = rand() % d_herotemplates[player_id].size();
79   return d_herotemplates[player_id][num];
80 }
81
82 int HeroTemplates::loadHeroTemplates()
83 {
84   const Armysetlist* al = Armysetlist::getInstance();
85
86   d_male_heroes.clear();
87   d_female_heroes.clear();
88
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++)
92     {
93       const ArmyProto *a = al->getArmy (p->getArmyset(), j);
94       if (a->isHero())
95         {
96           if (a->getGender() == Hero::FEMALE)
97             d_female_heroes.push_back(a);
98           else
99             d_male_heroes.push_back(a);
100         }
101     }
102   XML_Helper helper(File::getMiscFile("heronames.xml"), std::ios::in, false);
103
104   helper.registerTag("herotemplate", sigc::mem_fun((*this), &HeroTemplates::load));
105
106   if (!helper.parse())
107     {
108       std::cerr << "Error, while loading a template from heronames.xml" <<std::endl <<std::flush;
109       exit(-1);
110     }
111
112   helper.close();
113   return 0;
114 }
115
116       
117 bool HeroTemplates::load(std::string tag, XML_Helper *helper)
118 {
119   if (tag == "herotemplate")
120     {
121       std::string name;
122       helper->getData(name, "name");
123       guint32 owner;
124       helper->getData(owner, "owner");
125       std::string gender_str;
126       if (owner >= (int) MAX_PLAYERS)
127         return false;
128       helper->getData(gender_str, "gender");
129       Hero::Gender gender;
130       gender = Hero::genderFromString(gender_str);
131
132       const ArmyProto *herotype = NULL;
133       if (gender == Hero::MALE)
134         {
135           if (d_male_heroes.size() > 0)
136             herotype = d_male_heroes[rand() % d_male_heroes.size()];
137         }
138       else if (gender == Hero::FEMALE)
139         {
140           if (d_female_heroes.size() > 0)
141             herotype = d_female_heroes[rand() % d_female_heroes.size()];
142         }
143       if (herotype == NULL)
144         {
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()];
149         }
150       if (herotype == NULL)
151         return false;
152       HeroProto *newhero = new HeroProto (*herotype);
153       newhero->setOwnerId(owner);
154
155       newhero->setName (_(name.c_str()));
156       d_herotemplates[owner].push_back (newhero);
157     }
158   return true;
159 }
160