1 // Copyright (C) 2003, 2004, 2005, 2006 Ulf Lorenz
2 // Copyright (C) 2004 Andrea Paternesi
3 // Copyright (C) 2007, 2008, 2009, 2014 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 #include "army.h"
22 #include "xmlhelper.h"
23 #include "stack.h"
24 #include "Quest.h"
25 #include "QuestsManager.h"
26 #include "hero.h"
27 #include "playerlist.h"
28 #include "stacklist.h"
29 #include "history.h"
30
31 Glib::ustring Quest::d_tag = "quest";
32 #define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
33 //#define debug(x)
34
35
Quest(QuestsManager & q_mgr,guint32 hero,Type type)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
Quest(QuestsManager & q_mgr,XML_Helper * helper)48 Quest::Quest(QuestsManager& q_mgr, XML_Helper* helper)
49 :OwnerId(helper), d_q_mgr(q_mgr)
50 {
51 Glib::ustring 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
getHeroById(guint32 hero,Stack ** stack)59 Hero* Quest::getHeroById(guint32 hero, Stack** stack)
60 {
61 for (auto pit: *Playerlist::getInstance())
62 for (auto it: *pit->getStacklist())
63 for (auto sit: *it)
64 {
65 if ( (sit->isHero()) && (sit->getId() == hero))
66 {
67 if (stack)
68 *stack = it;
69 // isHero is TRUE, so dynamic_cast should succeed
70 return dynamic_cast<Hero*>(sit);
71 }
72 }
73 return NULL;
74 }
75
save(XML_Helper * helper) const76 bool Quest::save(XML_Helper* helper) const
77 {
78 bool retval = true;
79
80 Glib::ustring s;
81 s = questTypeToString(Quest::Type(d_type));
82 retval &= helper->saveData("type", s);
83 retval &= helper->saveData("hero", d_hero);
84 retval &= helper->saveData("hero_name", d_hero_name);
85 retval &= helper->saveData("pending_deletion", d_pending);
86 retval &= OwnerId::save(helper);
87
88 return retval;
89 }
90
getHeroNameForDeadHero() const91 Glib::ustring Quest::getHeroNameForDeadHero() const
92 {
93 return getHeroNameForDeadHero(d_hero);
94 }
95
getHeroNameForDeadHero(guint32 id)96 Glib::ustring Quest::getHeroNameForDeadHero(guint32 id)
97 {
98 std::list<History *>events;
99 events = Playerlist::getInstance()->getHistoryForHeroId(id);
100 if (events.size() == 0)
101 return "";
102 History *history = events.front();
103 History_HeroEmerges *event = dynamic_cast<History_HeroEmerges*>(history);
104 return event->getHeroName();
105 }
106
questTypeToString(const Quest::Type type)107 Glib::ustring Quest::questTypeToString(const Quest::Type type)
108 {
109 switch (type)
110 {
111 case Quest::KILLHERO: return "Quest::KILLHERO";
112 case Quest::KILLARMIES: return "Quest::KILLARMIES";
113 case Quest::CITYSACK: return "Quest::CITYSACK";
114 case Quest::CITYRAZE: return "Quest::CITYRAZE";
115 case Quest::CITYOCCUPY: return "Quest::CITYOCCUPY";
116 case Quest::KILLARMYTYPE: return "Quest::KILLARMYTYPE";
117 case Quest::PILLAGEGOLD: return "Quest::PILLAGEGOLD";
118 }
119 return "Quest::KILLHERO";
120 }
121
questTypeFromString(Glib::ustring str)122 Quest::Type Quest::questTypeFromString(Glib::ustring str)
123 {
124 if (str.size() > 0 && isdigit(str.c_str()[0]))
125 return Quest::Type(atoi(str.c_str()));
126 if (str == "Quest::KILLHERO") return Quest::KILLHERO;
127 else if (str == "Quest::KILLARMIES") return Quest::KILLARMIES;
128 else if (str == "Quest::CITYSACK") return Quest::CITYSACK;
129 else if (str == "Quest::CITYRAZE") return Quest::CITYRAZE;
130 else if (str == "Quest::CITYOCCUPY") return Quest::CITYOCCUPY;
131 else if (str == "Quest::KILLARMYTYPE") return Quest::KILLARMYTYPE;
132 else if (str == "Quest::PILLAGEGOLD") return Quest::PILLAGEGOLD;
133
134 return Quest::KILLHERO;
135 }
136