initial commit, lordsawar source, slightly modified
[lordsawar] / src / Quest.cpp
1 // Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
2 // Copyright (C) 2004 Andrea Paternesi
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
4 //
5 //  This program is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program 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 Library General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
18 //  02110-1301, USA.
19
20 #include <iostream>
21
22 #include "Quest.h"
23 #include "QuestsManager.h"
24 #include "hero.h"
25 #include "playerlist.h"
26 #include "stacklist.h"
27 #include "history.h"
28
29 std::string Quest::d_tag = "quest";
30 using namespace std;
31
32 #define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
33 //#define debug(x)
34
35
36 Quest::Quest(QuestsManager& q_mgr, guint32 hero, Type type)
37     :d_q_mgr(q_mgr), d_hero(hero), d_type(type), d_pending(false)
38 {
39   Hero *h = getHeroById(hero);
40   if (h)
41     {
42       setOwnerId(h->getOwner()->getId());
43       d_hero_name = h->getName();
44     }
45
46 }
47
48 Quest::Quest(QuestsManager& q_mgr, XML_Helper* helper)
49     :OwnerId(helper), d_q_mgr(q_mgr)
50 {
51     std::string s;
52     helper->getData(s, "type");
53     d_type = questTypeFromString(s);
54     helper->getData(d_hero, "hero");
55     helper->getData(d_hero_name, "hero_name");
56     helper->getData(d_pending, "pending_deletion");
57 }
58
59 Hero* Quest::getHeroById(guint32 hero, Stack** stack)
60 {
61     Playerlist* pl = Playerlist::getInstance();
62         Playerlist::const_iterator pit;
63     for (pit = pl->begin(); pit != pl->end(); pit++)
64     {
65         Stacklist* sl = (*pit)->getStacklist();
66         for (Stacklist::const_iterator it = sl->begin(); it != sl->end(); it++)
67         {
68             for (Stack::const_iterator sit = (*it)->begin(); sit != (*it)->end(); sit++)
69             {
70                 if ( ((*sit)->isHero()) && ((*sit)->getId() == hero) )
71                 {
72                     if (stack)
73                         *stack = (*it);
74                     // isHero is TRUE, so dynamic_cast should succeed
75                     return dynamic_cast<Hero*>(*sit);
76                 }
77             }
78         }
79     }
80     return NULL;
81 }
82
83 bool Quest::save(XML_Helper* helper) const
84 {
85     bool retval = true;
86
87     std::string s;
88     s = questTypeToString(Quest::Type(d_type));
89     retval &= helper->saveData("type", s);
90     retval &= helper->saveData("hero", d_hero);
91     retval &= helper->saveData("hero_name", d_hero_name);
92     retval &= helper->saveData("pending_deletion", d_pending);
93     retval &= OwnerId::save(helper);
94
95     return retval;
96 }
97
98 std::string Quest::getHeroNameForDeadHero() const
99 {
100   return getHeroNameForDeadHero(d_hero);
101 }
102
103 std::string Quest::getHeroNameForDeadHero(guint32 id)
104 {
105   std::list<History *>events;
106   events = Playerlist::getInstance()->getHistoryForHeroId(id);
107   if (events.size() == 0)
108     return "";
109   History *history = events.front();
110   History_HeroEmerges *event = dynamic_cast<History_HeroEmerges*>(history);
111   return event->getHeroName();
112 }
113
114 std::string Quest::questTypeToString(const Quest::Type type)
115 {
116   switch (type)
117     {
118     case Quest::KILLHERO:
119       return "Quest::KILLHERO";
120     case Quest::KILLARMIES:
121       return "Quest::KILLARMIES";
122     case Quest::CITYSACK:
123       return "Quest::CITYSACK";
124     case Quest::CITYRAZE:
125       return "Quest::CITYRAZE";
126     case Quest::CITYOCCUPY:
127       return "Quest::CITYOCCUPY";
128     case Quest::KILLARMYTYPE:
129       return "Quest::KILLARMYTYPE";
130     case Quest::PILLAGEGOLD:
131       return "Quest::PILLAGEGOLD";
132     }
133   return "Quest::KILLHERO";
134 }
135
136 Quest::Type Quest::questTypeFromString(std::string str)
137 {
138   if (str.size() > 0 && isdigit(str.c_str()[0]))
139     return Quest::Type(atoi(str.c_str()));
140   if (str == "Quest::KILLHERO")
141     return Quest::KILLHERO;
142   else if (str == "Quest::KILLARMIES")
143     return Quest::KILLARMIES;
144   else if (str == "Quest::CITYSACK")
145     return Quest::CITYSACK;
146   else if (str == "Quest::CITYRAZE")
147     return Quest::CITYRAZE;
148   else if (str == "Quest::CITYOCCUPY")
149     return Quest::CITYOCCUPY;
150   else if (str == "Quest::KILLARMYTYPE")
151     return Quest::KILLARMYTYPE;
152   else if (str == "Quest::PILLAGEGOLD")
153     return Quest::PILLAGEGOLD;
154     
155   return Quest::KILLHERO;
156 }