File: QEnemyArmytype.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 (180 lines) | stat: -rw-r--r-- 5,140 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
//  Copyright (C) 2007, 2008, 2009, 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 <sstream>
#include <sigc++/functors/mem_fun.h>
#include "ucompose.hpp"

#include "xmlhelper.h"
#include "stack.h"
#include "QEnemyArmytype.h"
#include "QuestsManager.h"
#include "playerlist.h"
#include "stacklist.h"
#include "armysetlist.h"
#include "GameMap.h"
#include "player.h"
#include "armyproto.h"
#include "hero.h"
#include "rnd.h"

//go get an existing army type,
//with the stipluation that player P's armies are not taken into consideration
int getVictimArmytype(Player *p, std::list<Vector<int> >&targets)
{
  std::vector<Army*> specials;
  Stacklist::const_iterator sit;
  Stack::iterator it;
  Stacklist *sl;
  for (auto pit: *Playerlist::getInstance())
    {
      if (pit == p)
	continue;
      sl = pit->getStacklist();
      for (sit = sl->begin(); sit != sl->end(); sit++)
	{
	  //is this stack not in a city?  no?  it's a target.
	  if (GameMap::getCity((*sit)->getPos()) == NULL)
	    targets.push_back((*sit)->getPos());
	  for (it = (*sit)->begin(); it != (*sit)->end(); it++)
	    {
	      if ((*it)->getAwardable())
                specials.push_back((*it));
	    }
	}
    }
  if (specials.size() == 0)
    return -1;
  else
    return specials[Rnd::rand() % specials.size()]->getTypeId();
}

//#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
#define debug(x)
QuestEnemyArmytype::QuestEnemyArmytype(QuestsManager& q_mgr, guint32 hero)
  : Quest(q_mgr, hero, Quest::KILLARMYTYPE)
{
  Player *p = getHero()->getOwner();

  // pick a victim
  d_type_to_kill = getVictimArmytype (p, d_targets);

  initDescription();
}

QuestEnemyArmytype::QuestEnemyArmytype(QuestsManager& q_mgr, XML_Helper* helper) 
  : Quest(q_mgr, helper)
{
  helper->getData(d_type_to_kill, "type_to_kill");

  initDescription();
}

QuestEnemyArmytype::QuestEnemyArmytype(QuestsManager& q_mgr, guint32 hero,
				       guint32 type_to_kill)
  : Quest(q_mgr, hero, Quest::KILLARMYTYPE)
{
  // pick a victim
  d_type_to_kill = type_to_kill;

  initDescription();
}

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

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

  return retval;
}

Glib::ustring QuestEnemyArmytype::getProgress() const
{
  guint32 set = Playerlist::getInstance()->getActiveplayer()->getArmyset();
  const ArmyProto *a = Armysetlist::getInstance()->getArmy(set, d_type_to_kill);
  return String::ucompose(
			  _("You have not killed a unit of enemy %1 yet."), a->getName());
}

void QuestEnemyArmytype::getSuccessMsg(std::queue<Glib::ustring>& msgs) const
{
  guint32 set = Playerlist::getInstance()->getActiveplayer()->getArmyset();
  const ArmyProto *a = Armysetlist::getInstance()->getArmy(set, d_type_to_kill);
  msgs.push(String::ucompose(_("You have killed a unit of enemy %1."), a->getName()));
  msgs.push(_("Well done!"));
}

void QuestEnemyArmytype::getExpiredMsg(std::queue<Glib::ustring>& msgs) const
{
  (void) msgs;
  // This quest should never expire, so this is just a dummy function
}

void QuestEnemyArmytype::initDescription()
{
  guint32 set = Playerlist::getInstance()->getActiveplayer()->getArmyset();
  const ArmyProto *a = Armysetlist::getInstance()->getArmy(set, d_type_to_kill);
  d_description = String::ucompose(_("You must destroy a unit of enemy %1."), 
				   a->getName());
}

bool QuestEnemyArmytype::isFeasible(guint32 heroId)
{
  std::list< Vector<int> >targets;
  int type = getVictimArmytype(getHeroById(heroId)->getOwner(), targets);
  if (type >= 0)
    return true;
  return false;
}

void QuestEnemyArmytype::armyDied(Army *a, bool heroIsCulprit)
{
  //was it the army type we were after?

  debug("QuestEnemyArmytype: armyDied - pending = " << (int)d_pending);

  if (!isPendingDeletion())
    return;
  Hero *h = getHero();
  if (!h || h->getHP() <= 0)
    {
      deactivate();
      return;
    }

  if (a->getTypeId() == d_type_to_kill)
    {
      if (heroIsCulprit)
	{
	  debug("CONGRATULATIONS: QUEST 'KILL ENEMY ARMYTYPE' IS COMPLETED!");
	  d_q_mgr.questCompleted(d_hero);
	}
    }
}

void QuestEnemyArmytype::cityAction(City *c, CityDefeatedAction action, 
				    bool heroIsCulprit, int gold)
{
  (void) c;
  (void) action;
  (void) heroIsCulprit;
  (void) gold;
}