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 "QCityRaze.h"
25 #include "QuestsManager.h"
27 #include "playerlist.h"
29 #include "xmlhelper.h"
33 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
36 QuestCityRaze::QuestCityRaze (QuestsManager& mgr, guint32 hero)
37 : Quest(mgr, hero, Quest::CITYRAZE)
40 City* c = chooseToRaze(getHero()->getOwner());
41 assert(c); // should never fail because isFeasible is checked first
44 d_targets.push_back(c->getPos());
45 debug("city_id = " << d_city);
49 QuestCityRaze::QuestCityRaze (QuestsManager& q_mgr, XML_Helper* helper)
50 : Quest(q_mgr, helper)
52 helper->getData(d_city, "city");
53 d_targets.push_back(getCity()->getPos());
57 QuestCityRaze::QuestCityRaze (QuestsManager& mgr, guint32 hero, guint32 target)
58 : Quest(mgr, hero, Quest::CITYRAZE)
61 d_targets.push_back(getCity()->getPos());
65 bool QuestCityRaze::isFeasible(guint32 heroId)
67 if (QuestCityRaze::chooseToRaze(getHeroById(heroId)->getOwner()))
73 bool QuestCityRaze::save(XML_Helper* helper) const
77 retval &= helper->openTag(Quest::d_tag);
78 retval &= Quest::save(helper);
79 retval &= helper->saveData("city", d_city);
80 retval &= helper->closeTag();
85 std::string QuestCityRaze::getProgress() const
87 return _("You aren't afraid of doing it, are you?");
90 void QuestCityRaze::getSuccessMsg(std::queue<std::string>& msgs) const
92 msgs.push(_("The priests thank you for razing this evil place."));
95 void QuestCityRaze::getExpiredMsg(std::queue<std::string>& msgs) const
97 const City* c = getCity();
98 msgs.push(String::ucompose
99 (_("The razing of city \"%1\" could not be accomplished."),
103 City* QuestCityRaze::getCity() const
105 Citylist* cl = Citylist::getInstance();
106 for (Citylist::iterator it = cl->begin(); it != cl->end(); it++)
107 if ((*it)->getId() == d_city)
113 void QuestCityRaze::initDescription()
115 const City* c = getCity();
117 d_description = String::ucompose
118 ( _("You must conquer the city \"%1\" and burn it to the ground."),
122 City* QuestCityRaze::chooseToRaze(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 raze
137 return cities[rand() % cities.size()];
140 void QuestCityRaze::armyDied(Army *a, bool heroIsCulprit)
143 //this quest does nothing when an army dies
146 void QuestCityRaze::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 raze 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.questExpired(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.questCompleted(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);
187 case CITY_DEFEATED_PILLAGE: //somebody pillaged
188 if (heroIsCulprit) // quest hero did
189 d_q_mgr.questExpired(d_hero);
190 else if (c->getOwner() == getHero()->getOwner()) // our stack did
191 d_q_mgr.questExpired(d_hero);