1 //  Copyright (C) 2007, 2008, 2009, 2014, 2015 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 "army.h"
23 #include "QPillageGold.h"
24 #include "QuestsManager.h"
25 #include "playerlist.h"
26 #include "city.h"
27 #include "xmlhelper.h"
28 #include "hero.h"
29 #include "rnd.h"
30 #include "GameScenarioOptions.h"
31 
32 //#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
33 #define debug(x)
34 
QuestPillageGold(QuestsManager & q_mgr,guint32 hero)35 QuestPillageGold::QuestPillageGold(QuestsManager& q_mgr, guint32 hero)
36   : Quest(q_mgr, hero, Quest::PILLAGEGOLD), d_pillaged(0)
37 {
38   //pick an amount of gold to sack and pillage
39   d_to_pillage = 850 + (Rnd::rand() % 630);
40 
41   initDescription();
42 }
43 
QuestPillageGold(QuestsManager & q_mgr,XML_Helper * helper)44 QuestPillageGold::QuestPillageGold(QuestsManager& q_mgr, XML_Helper* helper)
45   : Quest(q_mgr, helper)
46 {
47   helper->getData(d_to_pillage, "to_pillage");
48   helper->getData(d_pillaged,  "pillaged");
49 
50   initDescription();
51 }
52 
QuestPillageGold(QuestsManager & q_mgr,guint32 hero,guint32 gold)53 QuestPillageGold::QuestPillageGold(QuestsManager& q_mgr, guint32 hero, guint32 gold)
54   : Quest(q_mgr, hero, Quest::PILLAGEGOLD), d_pillaged(0)
55 {
56   d_to_pillage = gold;
57   initDescription();
58 }
59 
save(XML_Helper * helper) const60 bool QuestPillageGold::save(XML_Helper *helper) const
61 {
62   bool retval = true;
63 
64   retval &= helper->openTag(Quest::d_tag);
65   retval &= Quest::save(helper);
66   retval &= helper->saveData("to_pillage", d_to_pillage);
67   retval &= helper->saveData("pillaged",  d_pillaged);
68   retval &= helper->closeTag();
69 
70   return retval;
71 }
72 
getProgress() const73 Glib::ustring QuestPillageGold::getProgress() const
74 {
75   return String::ucompose(_("You have already stolen %1 gold pieces."), d_pillaged);
76 }
77 
getSuccessMsg(std::queue<Glib::ustring> & msgs) const78 void QuestPillageGold::getSuccessMsg(std::queue<Glib::ustring>& msgs) const
79 {
80   msgs.push(String::ucompose(_("You have managed to sack and pillage %1 gold."), d_pillaged));
81   msgs.push(_("Well done!"));
82 }
83 
getExpiredMsg(std::queue<Glib::ustring> & msgs) const84 void QuestPillageGold::getExpiredMsg(std::queue<Glib::ustring>& msgs) const
85 {
86   (void) msgs;
87     // This quest should never expire, so this is just a dummy function
88 }
89 
initDescription()90 void QuestPillageGold::initDescription()
91 {
92   d_description = String::ucompose(_("You shall sack and pillage %1 gold from thy mighty foes."), d_to_pillage);
93 }
94 
armyDied(Army * a,bool heroIsCulprit)95 void QuestPillageGold::armyDied(Army *a, bool heroIsCulprit)
96 {
97   (void) a;
98   (void) heroIsCulprit;
99 }
100 
cityAction(City * c,CityDefeatedAction action,bool heroIsCulprit,int gold)101 void QuestPillageGold::cityAction(City *c, CityDefeatedAction action,
102 				  bool heroIsCulprit, int gold)
103 {
104   (void) c;
105   (void) gold;
106   if (isPendingDeletion())
107     return;
108   Hero *h = getHero();
109   if (!h || h->getHP() <= 0)
110     {
111       deactivate();
112       return;
113     }
114   if (action == CITY_DEFEATED_SACK || action == CITY_DEFEATED_PILLAGE)
115     {
116       if (heroIsCulprit)
117 	{
118 	  d_pillaged += gold;
119 	  if (d_pillaged > d_to_pillage)
120 	    {
121 	      d_pillaged = d_to_pillage;
122 	      d_q_mgr.questCompleted(d_hero);
123 	    }
124 	}
125     }
126 }
127 
isFeasible(guint32 heroId)128 bool QuestPillageGold::isFeasible(guint32 heroId)
129 {
130   if (GameScenarioOptions::s_sacking_mode == GameParameters::SACKING_NEVER ||
131       GameScenarioOptions::s_sacking_mode == GameParameters::SACKING_ON_CAPTURE)
132     return false;
133   if (heroId)
134     return true;
135   return false;
136 }
137 
138