File: QCitySack.cpp

package info (click to toggle)
lordsawar 0.3.2%2Bfrogknows-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,716 kB
  • sloc: cpp: 103,193; sh: 4,965; xml: 2,220; makefile: 1,371
file content (199 lines) | stat: -rw-r--r-- 5,698 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//  Copyright (C) 2007, 2008, 2014, 2015 Ben Asselstine
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Library General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
//  02110-1301, USA.

#include <iostream>
#include <sstream>
#include <assert.h>
#include <sigc++/functors/mem_fun.h>
#include "ucompose.hpp"

#include "city.h"
#include "army.h"
#include "QCitySack.h"
#include "QuestsManager.h"
#include "citylist.h"
#include "playerlist.h"
#include "stack.h"
#include "xmlhelper.h"
#include "hero.h"
#include "rnd.h"
#include "GameScenarioOptions.h"

//#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
#define debug(x)

QuestCitySack::QuestCitySack (QuestsManager& mgr, guint32 hero) 
  : Quest(mgr, hero, Quest::CITYSACK)
{
  // find us a victim
  City* c = chooseToSack(getHero()->getOwner());
  assert(c);      // should never fail because isFeasible is checked first

  d_city = c->getId();
  d_targets.push_back(c->getPos());
  debug("city_id = " << d_city);
  initDescription();
}

QuestCitySack::QuestCitySack (QuestsManager& q_mgr, XML_Helper* helper) 
  : Quest(q_mgr, helper)
{
  // let us stay in touch with the world...
  helper->getData(d_city, "city");
  d_targets.push_back(getCity()->getPos());
  initDescription();
}

QuestCitySack::QuestCitySack (QuestsManager& mgr, guint32 hero, guint32 target) 
  : Quest(mgr, hero, Quest::CITYSACK)
{
  d_city = target;
  d_targets.push_back(getCity()->getPos());
  initDescription();
}

bool QuestCitySack::isFeasible(guint32 heroId)
{
  if (QuestCitySack::chooseToSack(getHeroById(heroId)->getOwner()))
    return true;
  return false;
}

bool QuestCitySack::save(XML_Helper* helper) const
{
  bool retval = true;

  retval &= helper->openTag(Quest::d_tag);
  retval &= Quest::save(helper);
  retval &= helper->saveData("city", d_city);
  retval &= helper->closeTag();

  return retval;
}

Glib::ustring QuestCitySack::getProgress() const
{
  return _("You aren't afraid of doing it, are you?");
}

void QuestCitySack::getSuccessMsg(std::queue<Glib::ustring>& msgs) const
{
  msgs.push(_("The priests thank you for sacking this evil place."));
}

void QuestCitySack::getExpiredMsg(std::queue<Glib::ustring>& msgs) const
{
  const City* c = getCity();
  msgs.push(String::ucompose(_("The sacking of \"%1\" could not be accomplished."), 
			     c->getName()));
}

City* QuestCitySack::getCity() const
{
  for (auto it: *Citylist::getInstance())
    if (it->getId() == d_city)
      return it;

  return 0;
}

void QuestCitySack::initDescription()
{
  const City* c = getCity();
  d_description = String::ucompose (_("You must take over and sack the city of \"%1\"."), c->getName());
}

City * QuestCitySack::chooseToSack(Player *p)
{
  if (GameScenarioOptions::s_sacking_mode == GameParameters::SACKING_NEVER ||
      GameScenarioOptions::s_sacking_mode == GameParameters::SACKING_ON_CAPTURE)
    return NULL;

  std::vector<City*> cities;

  // Collect all cities
  for (auto it: *Citylist::getInstance())
    if (!it->isBurnt() && it->getOwner() != p && it->getNoOfProductionBases() > 1 &&
	it->getOwner() != Playerlist::getInstance()->getNeutral())
      cities.push_back(it);

  // Find a suitable city for us to sack
  if (cities.empty())
    return 0;

  return cities[Rnd::rand() % cities.size()];
}

void QuestCitySack::armyDied(Army *a, bool heroIsCulprit)
{
  (void) a;
  (void) heroIsCulprit;
  //this quest does nothing when an army dies
}

void QuestCitySack::cityAction(City *c, CityDefeatedAction action, 
			       bool heroIsCulprit, int gold)
{
  (void) gold;
  if (isPendingDeletion())
    return;
  Hero *h = getHero();
  if (!h || h->getHP() <= 0)
    {
      deactivate();
      return;
    }
  if (!c)
    return;
  if (c->getId() != d_city)
    return;
  //did our hero sack the city? success.
  //did our hero do something else with the city?  expire.
  //did another of our stacks take the city?  expire.
  //did another player take the city? do nothing
  switch (action)
    {
    case CITY_DEFEATED_OCCUPY: //somebody occupied
      if (heroIsCulprit) //quest hero did
	d_q_mgr.questExpired(d_hero);
      else if (c->getOwner() == getHero()->getOwner()) //our stack did
	d_q_mgr.questExpired(d_hero);
      break;
    case CITY_DEFEATED_RAZE: //somebody razed
      if (heroIsCulprit) // quest hero
	d_q_mgr.questExpired(d_hero);
      else if (c->getOwner() == getHero()->getOwner()) // our stack razed
	d_q_mgr.questExpired(d_hero);
      else // their stack did
	d_q_mgr.questExpired(d_hero);
      break;
    case CITY_DEFEATED_SACK: //somebody sacked
      if (heroIsCulprit) // quest hero did
	d_q_mgr.questCompleted(d_hero);
      else if (c->getOwner() == getHero()->getOwner()) // our stack did
	d_q_mgr.questExpired(d_hero);
      else // their stack did
	d_q_mgr.questExpired(d_hero);
      break;
    case CITY_DEFEATED_PILLAGE: //somebody pillaged
      if (heroIsCulprit) // quest hero did
	d_q_mgr.questExpired(d_hero);
      else if (c->getOwner() == getHero()->getOwner()) // our stack did
	d_q_mgr.questExpired(d_hero);
      break;
    }
}