1 // Copyright (C) 2007, 2008 Ben Asselstine
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <sigc++/functors/mem_fun.h>
22 #include "ucompose.hpp"
24 #include "QCityOccupy.h"
25 #include "QuestsManager.h"
27 #include "playerlist.h"
29 #include "xmlhelper.h"
33 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
36 //=======================================================================
37 QuestCityOccupy::QuestCityOccupy (QuestsManager& mgr, guint32 hero)
38 : Quest(mgr, hero, Quest::CITYOCCUPY)
41 City* c = chooseToOccupy(getHero()->getOwner());
42 assert(c); // should never fail because isFeasible is checked first
45 d_targets.push_back(c->getPos());
46 debug("city_id = " << d_city);
49 //=======================================================================
50 QuestCityOccupy::QuestCityOccupy (QuestsManager& q_mgr, XML_Helper* helper)
51 : Quest(q_mgr, helper)
53 helper->getData(d_city, "city");
54 d_targets.push_back(getCity()->getPos());
57 //=======================================================================
58 QuestCityOccupy::QuestCityOccupy (QuestsManager& mgr, guint32 hero, guint32 target)
59 : Quest(mgr, hero, Quest::CITYOCCUPY)
62 d_targets.push_back(getCity()->getPos());
65 //=======================================================================
66 bool QuestCityOccupy::isFeasible(guint32 heroId)
68 if (QuestCityOccupy::chooseToOccupy(getHeroById(heroId)->getOwner()))
73 //=======================================================================
74 bool QuestCityOccupy::save(XML_Helper* helper) const
78 retval &= helper->openTag(Quest::d_tag);
79 retval &= Quest::save(helper);
80 retval &= helper->saveData("city", d_city);
81 retval &= helper->closeTag();
85 //=======================================================================
86 std::string QuestCityOccupy::getProgress() const
88 return _("You aren't afraid of doing it, are you?");
90 //=======================================================================
91 void QuestCityOccupy::getSuccessMsg(std::queue<std::string>& msgs) const
93 msgs.push(_("The priests thank you for occupying this evil place."));
95 //=======================================================================
96 void QuestCityOccupy::getExpiredMsg(std::queue<std::string>& msgs) const
98 const City* c = getCity();
100 msgs.push(String::ucompose
101 (_("The occupation of city \"%1\" could not be accomplished."),
104 //=======================================================================
105 City* QuestCityOccupy::getCity() const
107 Citylist* cl = Citylist::getInstance();
108 for (Citylist::iterator it = cl->begin(); it != cl->end(); it++)
109 if ((*it)->getId() == d_city)
114 //=======================================================================
115 void QuestCityOccupy::initDescription()
117 const City* c = getCity();
118 d_description = String::ucompose
119 (_("You must take over the city \"%1\" and occupy it."), c->getName());
121 //=======================================================================
122 City * QuestCityOccupy::chooseToOccupy(Player *p)
124 std::vector<City*> cities;
126 // Collect all cities
127 Citylist* cl = Citylist::getInstance();
128 for (Citylist::iterator it = cl->begin(); it != cl->end(); ++it)
129 if (!(*it)->isBurnt() && (*it)->getOwner() != p &&
130 (*it)->getOwner() != Playerlist::getInstance()->getNeutral())
131 cities.push_back((*it));
133 // Find a suitable city for us to occupy
137 return cities[rand() % cities.size()];
140 void QuestCityOccupy::armyDied(Army *a, bool heroIsCulprit)
143 //this quest does nothing when an army dies
146 void QuestCityOccupy::cityAction(City *c, CityDefeatedAction action,
147 bool heroIsCulprit, int gold)
149 if (isPendingDeletion())
152 if (!h || h->getHP() <= 0)
159 if (c->getId() != d_city)
161 //did our hero occupy the city? success.
162 //did our hero do something else with the city? expire.
163 //did another of our stacks take the city? expire.
164 //did another player take the city? do nothing
167 case CITY_DEFEATED_OCCUPY: //somebody occupied
168 if (heroIsCulprit) //quest hero did
169 d_q_mgr.questCompleted(d_hero);
170 else if (c->getOwner() == getHero()->getOwner()) //our stack did
171 d_q_mgr.questExpired(d_hero);
173 case CITY_DEFEATED_RAZE: //somebody razed
174 if (heroIsCulprit) // quest hero
175 d_q_mgr.questExpired(d_hero);
176 else if (c->getOwner() == getHero()->getOwner()) // our stack razed
177 d_q_mgr.questExpired(d_hero);
178 else // their stack did
179 d_q_mgr.questExpired(d_hero);
181 case CITY_DEFEATED_SACK: //somebody sacked
182 if (heroIsCulprit) // quest hero did
183 d_q_mgr.questExpired(d_hero);
184 else if (c->getOwner() == getHero()->getOwner()) // our stack did
185 d_q_mgr.questExpired(d_hero);
186 else // their stack did
187 d_q_mgr.questExpired(d_hero);
189 case CITY_DEFEATED_PILLAGE: //somebody pillaged
190 if (heroIsCulprit) // quest hero did
191 d_q_mgr.questExpired(d_hero);
192 else if (c->getOwner() == getHero()->getOwner()) // our stack did
193 d_q_mgr.questExpired(d_hero);