initial commit, lordsawar source, slightly modified
[lordsawar] / src / QCityRaze.cpp
1 //  Copyright (C) 2007, 2008 Ben Asselstine
2 //
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.
7 //
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.
12 //
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 
16 //  02110-1301, USA.
17
18 #include <iostream>
19 #include <sstream>
20 #include <assert.h>
21 #include <sigc++/functors/mem_fun.h>
22 #include "ucompose.hpp"
23
24 #include "QCityRaze.h"
25 #include "QuestsManager.h"
26 #include "citylist.h"
27 #include "playerlist.h"
28 #include "stack.h"
29 #include "xmlhelper.h"
30
31 using namespace std;
32
33 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
34 #define debug(x)
35
36 QuestCityRaze::QuestCityRaze (QuestsManager& mgr, guint32 hero) 
37   : Quest(mgr, hero, Quest::CITYRAZE)
38 {
39   // find us a victim
40   City* c = chooseToRaze(getHero()->getOwner());
41   assert(c);      // should never fail because isFeasible is checked first
42
43   d_city = c->getId();
44   d_targets.push_back(c->getPos());
45   debug("city_id = " << d_city);
46   initDescription();
47 }
48
49 QuestCityRaze::QuestCityRaze (QuestsManager& q_mgr, XML_Helper* helper) 
50   : Quest(q_mgr, helper)
51 {
52   helper->getData(d_city, "city");
53   d_targets.push_back(getCity()->getPos());
54   initDescription();
55 }
56
57 QuestCityRaze::QuestCityRaze (QuestsManager& mgr, guint32 hero, guint32 target) 
58   : Quest(mgr, hero, Quest::CITYRAZE)
59 {
60   d_city = target;
61   d_targets.push_back(getCity()->getPos());
62   initDescription();
63 }
64
65 bool QuestCityRaze::isFeasible(guint32 heroId)
66 {
67   if (QuestCityRaze::chooseToRaze(getHeroById(heroId)->getOwner()))
68     return true;
69
70   return false;
71 }
72
73 bool QuestCityRaze::save(XML_Helper* helper) const
74 {
75   bool retval = true;
76
77   retval &= helper->openTag(Quest::d_tag);
78   retval &= Quest::save(helper);
79   retval &= helper->saveData("city", d_city);
80   retval &= helper->closeTag();
81
82   return retval;
83 }
84
85 std::string QuestCityRaze::getProgress() const
86 {
87   return _("You aren't afraid of doing it, are you?");
88 }
89
90 void QuestCityRaze::getSuccessMsg(std::queue<std::string>& msgs) const
91 {
92   msgs.push(_("The priests thank you for razing this evil place."));
93 }
94
95 void QuestCityRaze::getExpiredMsg(std::queue<std::string>& msgs) const
96 {
97   const City* c = getCity();
98   msgs.push(String::ucompose
99             (_("The razing of city \"%1\" could not be accomplished."), 
100              c->getName()));
101 }
102
103 City* QuestCityRaze::getCity() const
104 {
105   Citylist* cl = Citylist::getInstance();
106   for (Citylist::iterator it = cl->begin(); it != cl->end(); it++)
107     if ((*it)->getId() == d_city)
108       return (*it);
109
110   return 0;
111 }
112
113 void QuestCityRaze::initDescription()
114 {
115   const City* c = getCity();
116
117   d_description = String::ucompose 
118     ( _("You must conquer the city \"%1\" and burn it to the ground."),
119       c->getName());
120 }
121
122 City* QuestCityRaze::chooseToRaze(Player *p)
123 {
124   std::vector<City*> cities;
125
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));
132
133   // Find a suitable city for us to raze 
134   if (cities.empty())
135     return 0;
136
137   return cities[rand() % cities.size()];
138 }
139
140 void QuestCityRaze::armyDied(Army *a, bool heroIsCulprit)
141 {
142   ;
143   //this quest does nothing when an army dies
144 }
145
146 void QuestCityRaze::cityAction(City *c, CityDefeatedAction action, 
147                                bool heroIsCulprit, int gold)
148 {
149   if (isPendingDeletion())
150     return;
151   Hero *h = getHero();
152   if (!h || h->getHP() <= 0)
153     {
154       deactivate();
155       return;
156     }
157   if (!c)
158     return;
159   if (c->getId() != d_city)
160     return;
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
165   switch (action)
166     {
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);
172       break;
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);
180       break;
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       break;
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);
192       break;
193     }
194 }