initial commit, lordsawar source, slightly modified
[lordsawar] / src / QCityOccupy.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 "QCityOccupy.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 //=======================================================================
37 QuestCityOccupy::QuestCityOccupy (QuestsManager& mgr, guint32 hero) 
38     : Quest(mgr, hero, Quest::CITYOCCUPY)
39 {
40     // find us a victim
41     City* c = chooseToOccupy(getHero()->getOwner());
42     assert(c);      // should never fail because isFeasible is checked first
43
44     d_city = c->getId();
45     d_targets.push_back(c->getPos());
46     debug("city_id = " << d_city);
47     initDescription();
48 }
49 //=======================================================================
50 QuestCityOccupy::QuestCityOccupy (QuestsManager& q_mgr, XML_Helper* helper) 
51      : Quest(q_mgr, helper)
52 {
53     helper->getData(d_city, "city");
54     d_targets.push_back(getCity()->getPos());
55     initDescription();
56 }
57 //=======================================================================
58 QuestCityOccupy::QuestCityOccupy (QuestsManager& mgr, guint32 hero, guint32 target) 
59     : Quest(mgr, hero, Quest::CITYOCCUPY)
60 {
61     d_city = target;
62     d_targets.push_back(getCity()->getPos());
63     initDescription();
64 }
65 //=======================================================================
66 bool QuestCityOccupy::isFeasible(guint32 heroId)
67 {
68   if (QuestCityOccupy::chooseToOccupy(getHeroById(heroId)->getOwner()))
69     return true;
70
71   return false;
72 }
73 //=======================================================================
74 bool QuestCityOccupy::save(XML_Helper* helper) const
75 {
76     bool retval = true;
77
78     retval &= helper->openTag(Quest::d_tag);
79     retval &= Quest::save(helper);
80     retval &= helper->saveData("city", d_city);
81     retval &= helper->closeTag();
82
83     return retval;
84 }
85 //=======================================================================
86 std::string QuestCityOccupy::getProgress() const
87 {
88     return _("You aren't afraid of doing it, are you?");
89 }
90 //=======================================================================
91 void QuestCityOccupy::getSuccessMsg(std::queue<std::string>& msgs) const
92 {
93     msgs.push(_("The priests thank you for occupying this evil place."));
94 }
95 //=======================================================================
96 void QuestCityOccupy::getExpiredMsg(std::queue<std::string>& msgs) const
97 {
98     const City* c = getCity();
99
100     msgs.push(String::ucompose
101               (_("The occupation of city \"%1\" could not be accomplished."), 
102                c->getName()));
103 }
104 //=======================================================================
105 City* QuestCityOccupy::getCity() const
106 {
107     Citylist* cl = Citylist::getInstance();
108     for (Citylist::iterator it = cl->begin(); it != cl->end(); it++)
109         if ((*it)->getId() == d_city)
110             return (*it);
111
112     return 0;
113 }
114 //=======================================================================
115 void QuestCityOccupy::initDescription()
116 {
117   const City* c = getCity();
118   d_description = String::ucompose
119     (_("You must take over the city \"%1\" and occupy it."), c->getName());
120 }
121 //=======================================================================
122 City * QuestCityOccupy::chooseToOccupy(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 occupy
134   if (cities.empty())
135     return 0;
136
137   return cities[rand() % cities.size()];
138 }
139          
140 void QuestCityOccupy::armyDied(Army *a, bool heroIsCulprit)
141 {
142   ;
143   //this quest does nothing when an army dies
144 }
145
146 void QuestCityOccupy::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 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
165   switch (action)
166     {
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);
172       break;
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);
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       else // their stack did
187         d_q_mgr.questExpired(d_hero);
188       break;
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);
194       break;
195     }
196 }