~ubuntu-branches/ubuntu/raring/lordsawar/raring

« back to all changes in this revision

Viewing changes to src/stack.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese, Gonéri Le Bouder
  • Date: 2008-06-17 11:15:26 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080617111526-yjyvu9df50zmpdo0
Tags: 0.0.9-1
[ Barry deFreese ]
* New upstream release.
  + Fixes gcc-4.3 builds so drop ftbfs_gcc-4.3_fix.diff.
  + Add new build-dependency for libgnet-dev.
* Add simple man page for new lordsawar-tile-editor.
* Add desktop file for lordsawar-tile-editor.
* Remove French translation on install.

[ Gonéri Le Bouder ]
* bump Debian Policy to 3.8.0. No change needed.
* fix wording in the 0.0.8-3 entry of the Debian changelog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2000, 2001, 2002, 2003 Michael Bartl
 
2
// Copyright (C) 2000, Anluan O'Brien
 
3
// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
 
4
// Copyright (C) 2004 John Farrell
 
5
// Copyright (C) 2004, 2005 Andrea Paternesi
 
6
// Copyright (C) 2006, 2007, 2008 Ben Asselstine
 
7
// Copyright (C) 2007, 2008 Ole Laursen
 
8
//
1
9
//  This program is free software; you can redistribute it and/or modify
2
10
//  it under the terms of the GNU General Public License as published by
3
11
//  the Free Software Foundation; either version 2 of the License, or
10
18
//
11
19
//  You should have received a copy of the GNU General Public License
12
20
//  along with this program; if not, write to the Free Software
13
 
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
21
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 
22
//  02110-1301, USA.
14
23
 
15
24
#include <sigc++/functors/mem_fun.h>
16
25
#include <assert.h>
27
36
#include "GameMap.h"
28
37
#include "vector.h"
29
38
#include "xmlhelper.h"
 
39
#include "FogMap.h"
 
40
#include "player.h"
 
41
#include "portlist.h"
 
42
#include "port.h"
30
43
 
31
44
using namespace std;
32
45
 
34
47
#define debug(x)
35
48
 
36
49
Stack::Stack(Player* player, Vector<int> pos)
37
 
    : Object(pos), d_player(player), d_defending(false), d_parked(false),
38
 
    d_deleting(false), d_moves_exhausted_at_point(0)
 
50
    : UniquelyIdentified(), Movable(pos), Ownable(player), d_defending(false), 
 
51
    d_parked(false), d_deleting(false)
39
52
{
40
53
    d_path = new Path();
41
54
}
42
55
 
43
56
Stack::Stack(Stack& s)
44
 
    : Object(s), d_player(s.d_player), d_defending(s.d_defending),
45
 
     d_parked(s.d_parked), d_deleting(false),
46
 
     d_moves_exhausted_at_point(s.d_moves_exhausted_at_point)
 
57
    : UniquelyIdentified(s), Movable(s), Ownable(s), 
 
58
    d_defending(s.d_defending), d_parked(s.d_parked), d_deleting(false)
47
59
{
48
60
    clear();
49
61
    d_path = new Path();
50
62
    //deep copy the other stack's armies
 
63
    
51
64
    for (iterator sit = s.begin(); sit != s.end(); sit++)
52
65
    {
53
66
        Army* a;
59
72
          }
60
73
        else
61
74
          {
62
 
            a = new Army((**sit), (*sit)->getPlayer());
 
75
            a = new Army((**sit), (*sit)->getOwner());
63
76
            push_back(a);
64
77
          }
65
78
    }
66
79
}
67
80
 
68
81
Stack::Stack(XML_Helper* helper)
69
 
: Object(helper), d_deleting(false)
 
82
  : UniquelyIdentified(helper), Movable(helper), Ownable(helper), 
 
83
    d_deleting(false)
70
84
{
71
85
  helper->getData(d_defending, "defending");
72
86
  helper->getData(d_parked, "parked");
73
87
 
74
 
  int i;
75
 
  helper->getData(i, "player");
76
 
  if (i == -1)
77
 
    d_player = 0;
78
 
  else
79
 
    d_player = Playerlist::getInstance()->getPlayer(i);
80
 
 
81
 
  helper->getData(d_moves_exhausted_at_point, "moves_exhausted_at_point");
82
 
 
83
88
  helper->registerTag("path", sigc::mem_fun((*this), &Stack::load));
84
89
  helper->registerTag("army", sigc::mem_fun((*this), &Stack::load));
85
90
  helper->registerTag("hero", sigc::mem_fun((*this), &Stack::load));
97
102
void Stack::setPlayer(Player* p)
98
103
{
99
104
  // we need to change the armies' loyalties as well!!
100
 
  d_player = p;
 
105
  setOwner(p);
101
106
  for (iterator it = begin(); it != end(); it++)
102
 
    (*it)->setPlayer(p);
 
107
    (*it)->setOwner(p);
103
108
}
104
109
 
105
 
bool Stack::moveOneStep()
 
110
void Stack::moveOneStep()
106
111
{
107
112
  debug("move_one_step()");
108
113
 
109
 
  d_pos = **d_path->begin();
 
114
  Vector<int> dest = *getPath()->front();
 
115
  moveToDest(dest);
 
116
 
 
117
  //now remove first point of the path
 
118
  d_path->eraseFirstPoint();
 
119
}
 
120
 
 
121
void Stack::moveToDest(Vector<int> dest)
 
122
{
 
123
  Vector<int> pos = getPos();
 
124
    
 
125
  Uint32 maptype = GameMap::getInstance()->getTile(dest.x,dest.y)->getMaptileType();
 
126
  City* to_city = Citylist::getInstance()->getObjectAt(dest.x, dest.y);
 
127
  City* on_city = Citylist::getInstance()->getObjectAt(pos.x, pos.y);
 
128
  Port* on_port = Portlist::getInstance()->getObjectAt(pos.x, pos.y);
 
129
  bool on_water = (GameMap::getInstance()->getTile(pos.x,pos.y)->getMaptileType() == Tile::WATER);
 
130
  bool to_water = (GameMap::getInstance()->getTile(dest.x,dest.y)->getMaptileType() == Tile::WATER);
 
131
  bool ship_load_unload = false;
 
132
  //here we mark the armies as being on or off a boat
 
133
  if (!isFlying())
 
134
    {
 
135
      if ((on_water && to_city) || 
 
136
          (on_water && on_port && !to_water) ||
 
137
          ((on_city || on_port) && to_water))
 
138
        {
 
139
          ship_load_unload = true;
 
140
          for (Stack::iterator it = begin(); it != end(); it++)
 
141
            {
 
142
              if (to_water && 
 
143
                  ((*it)->getStat(Army::MOVE_BONUS) & Tile::WATER) == 0)
 
144
                (*it)->setInShip(true);
 
145
              else
 
146
                (*it)->setInShip(false);
 
147
            }
 
148
        }
 
149
    }
 
150
  else
 
151
    {
 
152
      for (Stack::iterator it = begin(); it != end(); it++)
 
153
        (*it)->setInShip(false);
 
154
    }
 
155
 
 
156
  //how many moves does the stack need to travel to dest?
 
157
  int needed_moves = calculateTileMovementCost(dest);
 
158
 
 
159
  for (Stack::iterator it = begin(); it != end(); it++)
 
160
    {
 
161
      if (ship_load_unload)
 
162
        (*it)->decrementMoves((*it)->getMoves());
 
163
      else 
 
164
        {
 
165
          //maybe the army has a natural movement ability
 
166
          if ((*it)->getStat(Army::MOVE_BONUS) == maptype && needed_moves > 1)
 
167
            (*it)->decrementMoves(2);
 
168
          else
 
169
            (*it)->decrementMoves(needed_moves);
 
170
        }
 
171
    }
 
172
 
 
173
  //update position and status
 
174
  setPos(dest);
110
175
 
111
176
  setFortified(false);
112
177
  setDefending(false);
113
178
  setParked(false);
114
179
 
115
 
  //now remove first point of the path
116
 
  d_path->flErase(d_path->begin());
117
 
 
118
 
  //and decrement the point at which we exhaust our path
119
 
  if (getMovesExhaustedAtPoint())
120
 
    setMovesExhaustedAtPoint(getMovesExhaustedAtPoint()-1);
121
 
  return true;
 
180
  //update fogmap
 
181
  getOwner()->getFogMap()->alterFogRadius(dest, getMaxSight(), FogMap::OPEN);
122
182
}
123
183
 
124
 
 
125
184
bool Stack::isGrouped()
126
185
{
127
186
  if (empty())
161
220
int Stack::getMinTileMoves() const
162
221
{
163
222
  GameMap *map = GameMap::getInstance();
164
 
  Rectangle bounds = GameMap::get_boundary();
 
223
  Rectangle bounds = map->get_boundary();
165
224
 
166
225
  std::vector<Vector<int> > tiles;
167
226
  tiles.push_back(Vector<int>(getPos().x + 1, getPos().y - 1));
208
267
Army* Stack::getStrongestArmy() const
209
268
{
210
269
  assert(!empty());
211
 
  Army *strongest = 0;
212
 
  Uint32 highest_strength = 0;
213
 
 
214
 
  for (const_iterator it = begin(); it != end(); ++it)
215
 
    {
216
 
      // if hero
217
 
      if ((*it)->isHero())
218
 
        {
219
 
          return *it;
220
 
        }
221
 
      else if ((*it)->getStat(Army::STRENGTH) > highest_strength)
222
 
        {
223
 
          highest_strength = (*it)->getStat(Army::STRENGTH);
224
 
          strongest = *it;
225
 
        }
226
 
    }
227
 
  return strongest;
 
270
  return getStrongestArmy(false);
228
271
}
229
272
 
230
273
Army* Stack::getStrongestHero() const
231
274
{
 
275
  return getStrongestArmy(true);
 
276
}
 
277
 
 
278
Army* Stack::getStrongestArmy(bool hero) const
 
279
{
232
280
  Army *strongest = 0;
233
281
  Uint32 highest_strength = 0;
234
282
 
235
283
  for (const_iterator it = begin(); it != end(); ++it)
236
284
    {
237
 
      // if hero
238
 
      if ((*it)->isHero())
 
285
      if ((*it)->isHero() && hero || !hero)
239
286
        {
240
287
          if ((*it)->getStat(Army::STRENGTH) > highest_strength)
241
288
            {
247
294
  return strongest;
248
295
}
249
296
 
 
297
Army *Stack::getArmyById(Uint32 id) const
 
298
{
 
299
  for (Stack::const_iterator i = begin(), e = end(); i != e; ++i)
 
300
    if ((*i)->getId() == id)
 
301
      return *i;
 
302
  
 
303
  return 0;
 
304
}
 
305
 
 
306
 
250
307
void Stack::group()
251
308
{
252
309
  if (empty())
335
392
  int tile_moves = getMinTileMoves();
336
393
  int group_moves = getGroupMoves();
337
394
 
 
395
  assert (tile_moves != -1);
338
396
  return group_moves > 0 && tile_moves >= 0 && group_moves >= tile_moves;
339
397
}
340
398
 
350
408
  return 0;
351
409
}
352
410
 
353
 
bool Stack::isFriend(Player* player) const
354
 
{
355
 
  return d_player == player;
356
 
}
357
 
 
358
411
Uint32 Stack::getMaxSight() const
359
412
{
360
413
  Uint32 max = 0;
395
448
        std::list<Item*>::const_iterator item;
396
449
        for (item = backpack.begin(); item != backpack.end(); item++)
397
450
          {
398
 
            Player *p = d_player;
 
451
            Player *p = d_owner;
399
452
            if ((*item)->getBonus(Item::ADD2GOLDPERCITY))
400
453
              p->addGold(2 * Citylist::getInstance()->countCities(p));
401
454
            if ((*item)->getBonus(Item::ADD3GOLDPERCITY))
413
466
    {
414
467
      (*it)->resetMoves();
415
468
      // TODO: should be moved in a more appropriate place => class Player
416
 
      if (d_player)
417
 
        d_player->withdrawGold((*it)->getUpkeep());
 
469
      if (d_owner)
 
470
        d_owner->withdrawGold((*it)->getUpkeep());
418
471
      (*it)->heal();
419
472
    }
420
473
 
429
482
 
430
483
  retval &= helper->openTag("stack");
431
484
  retval &= helper->saveData("id", d_id);
432
 
  if (d_player)
433
 
    retval &= helper->saveData("player", d_player->getId());
 
485
  retval &= helper->saveData("x", getPos().x);
 
486
  retval &= helper->saveData("y", getPos().y);
 
487
  if (d_owner)
 
488
    retval &= helper->saveData("owner", d_owner->getId());
434
489
  else
435
 
    retval &= helper->saveData("player", -1);
436
 
  retval &= helper->saveData("x", d_pos.x);
437
 
  retval &= helper->saveData("y", d_pos.y);
 
490
    retval &= helper->saveData("owner", -1);
438
491
  retval &= helper->saveData("defending", d_defending);
439
492
  retval &= helper->saveData("parked", d_parked);
440
493
 
441
 
  retval &= helper->saveData("moves_exhausted_at_point", 
442
 
                             d_moves_exhausted_at_point);
443
494
 
444
495
  //save path
445
496
  retval &= d_path->save(helper);
465
516
  if (tag == "army")
466
517
    {
467
518
      Army* a = new Army(helper);
468
 
      a->setPlayer(d_player);
 
519
      a->setOwner(d_owner);
469
520
      push_back(a);
470
521
 
471
522
      return true;
474
525
  if (tag == "hero")
475
526
    {
476
527
      Hero* h = new Hero(helper);
477
 
      h->setPlayer(d_player);
 
528
      h->setOwner(d_owner);
478
529
      push_back(h);
479
530
 
480
531
      return true;
609
660
 
610
661
bool Stack::armyCompareFightOrder (const Army *lhs, const Army *rhs)  
611
662
{
612
 
  std::list<Uint32> lhs_fight_order = lhs->getPlayer()->getFightOrder();
613
 
  std::list<Uint32> rhs_fight_order = rhs->getPlayer()->getFightOrder();
 
663
  std::list<Uint32> lhs_fight_order = lhs->getOwner()->getFightOrder();
 
664
  std::list<Uint32> rhs_fight_order = rhs->getOwner()->getFightOrder();
614
665
  Uint32 lhs_rank = getFightOrder (lhs_fight_order, lhs->getType());
615
666
  Uint32 rhs_rank = getFightOrder (rhs_fight_order, rhs->getType());
616
667
  //if (lhs_rank == rhs_rank)