1 // Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
2 // Copyright (C) 2004 Andrea Paternesi
3 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
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.
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.
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
23 #include "QuestsManager.h"
25 #include "playerlist.h"
26 #include "stacklist.h"
29 std::string Quest::d_tag = "quest";
32 #define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
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)
39 Hero *h = getHeroById(hero);
42 setOwnerId(h->getOwner()->getId());
43 d_hero_name = h->getName();
48 Quest::Quest(QuestsManager& q_mgr, XML_Helper* helper)
49 :OwnerId(helper), d_q_mgr(q_mgr)
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");
59 Hero* Quest::getHeroById(guint32 hero, Stack** stack)
61 Playerlist* pl = Playerlist::getInstance();
62 Playerlist::const_iterator pit;
63 for (pit = pl->begin(); pit != pl->end(); pit++)
65 Stacklist* sl = (*pit)->getStacklist();
66 for (Stacklist::const_iterator it = sl->begin(); it != sl->end(); it++)
68 for (Stack::const_iterator sit = (*it)->begin(); sit != (*it)->end(); sit++)
70 if ( ((*sit)->isHero()) && ((*sit)->getId() == hero) )
74 // isHero is TRUE, so dynamic_cast should succeed
75 return dynamic_cast<Hero*>(*sit);
83 bool Quest::save(XML_Helper* helper) const
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);
98 std::string Quest::getHeroNameForDeadHero() const
100 return getHeroNameForDeadHero(d_hero);
103 std::string Quest::getHeroNameForDeadHero(guint32 id)
105 std::list<History *>events;
106 events = Playerlist::getInstance()->getHistoryForHeroId(id);
107 if (events.size() == 0)
109 History *history = events.front();
110 History_HeroEmerges *event = dynamic_cast<History_HeroEmerges*>(history);
111 return event->getHeroName();
114 std::string Quest::questTypeToString(const Quest::Type type)
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";
133 return "Quest::KILLHERO";
136 Quest::Type Quest::questTypeFromString(std::string str)
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;
155 return Quest::KILLHERO;