initial commit, lordsawar source, slightly modified
[lordsawar] / src / QPillageGold.cpp
1 //  Copyright (C) 2007, 2008, 2009 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 <sstream>
19 #include <sigc++/functors/mem_fun.h>
20 #include "ucompose.hpp"
21
22 #include "QPillageGold.h"
23 #include "QuestsManager.h"
24 #include "playerlist.h"
25
26 using namespace std;
27
28 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
29 #define debug(x)
30
31 QuestPillageGold::QuestPillageGold(QuestsManager& q_mgr, guint32 hero)
32   : Quest(q_mgr, hero, Quest::PILLAGEGOLD), d_pillaged(0)
33 {
34   //pick an amount of gold to sack and pillage
35   d_to_pillage = 850 + (rand() % 630);
36
37   initDescription();
38 }
39
40 QuestPillageGold::QuestPillageGold(QuestsManager& q_mgr, XML_Helper* helper) 
41   : Quest(q_mgr, helper)
42 {
43   helper->getData(d_to_pillage, "to_pillage");
44   helper->getData(d_pillaged,  "pillaged");
45
46   initDescription();
47 }
48
49 QuestPillageGold::QuestPillageGold(QuestsManager& q_mgr, guint32 hero, guint32 gold)
50   : Quest(q_mgr, hero, Quest::PILLAGEGOLD), d_pillaged(0)
51 {
52   d_to_pillage = gold;
53   initDescription();
54 }
55
56 bool QuestPillageGold::save(XML_Helper *helper) const
57 {
58   bool retval = true;
59
60   retval &= helper->openTag(Quest::d_tag);
61   retval &= Quest::save(helper);
62   retval &= helper->saveData("to_pillage", d_to_pillage);
63   retval &= helper->saveData("pillaged",  d_pillaged);
64   retval &= helper->closeTag();
65
66   return retval;
67 }
68
69 std::string QuestPillageGold::getProgress() const
70 {
71   return String::ucompose(_("You have already stolen %1 gold."), d_pillaged);
72 }
73
74 void QuestPillageGold::getSuccessMsg(std::queue<std::string>& msgs) const
75 {
76   msgs.push(String::ucompose(_("You have managed to sack and pillage %1 gold."), d_pillaged));
77   msgs.push(_("Well done!"));
78 }
79
80 void QuestPillageGold::getExpiredMsg(std::queue<std::string>& msgs) const
81 {
82     // This quest should never expire, so this is just a dummy function
83 }
84
85 void QuestPillageGold::initDescription()
86 {
87   d_description = String::ucompose(_("You shall sack and pillage %1 gold from thy mighty foes."), d_to_pillage);
88 }
89         
90 void QuestPillageGold::armyDied(Army *a, bool heroIsCulprit)
91 {
92   ;
93   //this quest does nothing when an army dies
94 }
95
96 void QuestPillageGold::cityAction(City *c, CityDefeatedAction action, 
97                                   bool heroIsCulprit, int gold)
98 {
99   if (isPendingDeletion())
100     return;
101   Hero *h = getHero();
102   if (!h || h->getHP() <= 0)
103     {
104       deactivate();
105       return;
106     }
107   if (action == CITY_DEFEATED_SACK || action == CITY_DEFEATED_PILLAGE)
108     {
109       if (heroIsCulprit)
110         {
111           d_pillaged += gold;
112           if (d_pillaged > d_to_pillage)
113             {
114               d_pillaged = d_to_pillage;
115               d_q_mgr.questCompleted(d_hero);
116             }
117         }
118     }
119 }