~ubuntu-branches/ubuntu/maverick/lordsawar/maverick

« back to all changes in this revision

Viewing changes to src/stacklist.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2010-04-10 09:29:33 UTC
  • mfrom: (1.1.9 upstream) (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100410092933-23uq4dxig30kmtcw
Tags: 0.1.8-1
* New upstream release.
* Add misc:Depends for -data package.
* Bump Standards Version to 3.8.4. (No changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
3
3
// Copyright (C) 2004, 2005 Andrea Paternesi
4
4
// Copyright (C) 2004 John Farrell
5
 
// Copyright (C) 2007, 2008, 2009 Ben Asselstine
 
5
// Copyright (C) 2007, 2008, 2009, 2010 Ben Asselstine
6
6
// Copyright (C) 2007, 2008 Ole Laursen
7
7
//
8
8
//  This program is free software; you can redistribute it and/or modify
37
37
#include "hero.h"
38
38
#include "Backpack.h"
39
39
#include "LocationList.h"
 
40
#include "GameMap.h"
 
41
#include "stacktile.h"
40
42
 
41
43
std::string Stacklist::d_tag = "stacklist";
42
44
using namespace std;
44
46
//#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
45
47
#define debug(x)
46
48
 
47
 
//the static functions first
48
 
Stack* Stacklist::getObjectAt(int x, int y)
49
 
{
50
 
    for (Playerlist::iterator pit = Playerlist::getInstance()->begin();
51
 
        pit != Playerlist::getInstance()->end(); pit++)
52
 
    {
53
 
        Stacklist* mylist = (*pit)->getStacklist();
54
 
        Stack *s1 = NULL;
55
 
        Stack *s2 = NULL;
56
 
        mylist->getObjectAt(Vector<int>(x,y), s1, s2);
57
 
        if (s1)
58
 
          return s1;
59
 
        else if (s2)
60
 
          return s2;
61
 
    }
62
 
    return 0;
63
 
}
64
 
 
65
49
Vector<int> Stacklist::getPosition(guint32 id)
66
50
{
67
51
    for (Playerlist::iterator pit = Playerlist::getInstance()->begin();
83
67
//It also happens when two stacks fight.
84
68
Stack* Stacklist::getAmbiguity(Stack* s)
85
69
{
86
 
    for (Playerlist::iterator pit = Playerlist::getInstance()->begin();
87
 
        pit != Playerlist::getInstance()->end(); pit++)
88
 
    {
89
 
        Stacklist* mylist = (*pit)->getStacklist();
90
 
        Stack *s1 = NULL;
91
 
        Stack *s2 = NULL;
92
 
        mylist->getObjectAt(s->getPos(), s1, s2);
93
 
        if (s1 && s1->getId() != s->getId())
94
 
          return s1;
95
 
        else if (s2 && s2->getId() != s->getId())
96
 
          return s2;
97
 
    }
98
 
 
99
 
    return 0;
 
70
  return GameMap::getInstance()->getTile(s->getPos())->getStacks()->getOtherStack(s);
100
71
}
101
72
 
102
73
//search all player's stacklists to find this stack
131
102
    (*it)->payUpkeep(p);
132
103
}
133
104
 
 
105
bool Stacklist::check()
 
106
{
 
107
  //printf("checking stacks of the current player.  the current player is %s\n", Playerlist::getActiveplayer()->getName().c_str());
 
108
  //if (size())
 
109
    //printf("stacklist stacks belong to %s\n", front()->getOwner()->getName().c_str());
 
110
    for (iterator it = begin(); it != end(); it++)
 
111
      {
 
112
        if ((*it)->getOwner()->isComputer() == false)
 
113
          continue;
 
114
        std::list<Stack*> f = GameMap::getFriendlyStacks((*it)->getPos());
 
115
        if (f.size() > 1)
 
116
          {
 
117
              fprintf (stderr, "%d stacks found on %d,%d\n", f.size(),
 
118
                       (*it)->getPos().x, (*it)->getPos().y);
 
119
              for (std::list<Stack*>::iterator t = f.begin(); t != f.end(); t++)
 
120
                {
 
121
                  Stack *stack = *t;
 
122
                  if (stack)
 
123
                    {
 
124
                    printf("stack id: %d\n", stack->getId());
 
125
                    printf("\tsize is %d\n", stack->size());
 
126
                    }
 
127
                  else
 
128
                    printf("null stack\n");
 
129
                }
 
130
              return false;
 
131
          }
 
132
      }
 
133
    return true;
 
134
}
 
135
 
134
136
void Stacklist::nextTurn()
135
137
{
136
138
    debug("nextTurn()");
146
148
              fprintf (stderr, "duplicate army id %d found\n", (*it)->getId());
147
149
              exit (1);
148
150
            }
 
151
    //printf("checking at next-turn time\n");
 
152
    //check();
149
153
}
150
154
 
151
 
vector<Stack*> Stacklist::defendersInCity(City *city)
 
155
vector<Stack*> Stacklist::getDefendersInCity(const City *city)
152
156
{
153
 
    debug("defendersInCity()");
 
157
    debug("getDefendersInCity()");
154
158
 
155
159
    vector<Stack*> stackvector;
156
160
    Vector<int> pos = city->getPos();
159
163
    {
160
164
        for (unsigned int j = pos.y; j < pos.y + city->getSize(); j++)
161
165
        {
162
 
            Stack *stack;
163
 
            stack = city->getOwner()->getStacklist()->getOwnObjectAt(i, j);
164
 
            if (stack)
165
 
            {
166
 
              stackvector.push_back(stack);
167
 
            }
 
166
            Vector<int> p = Vector<int>(i,j);
 
167
            std::list<Stack *>stacks = 
 
168
              GameMap::getFriendlyStacks(p, city->getOwner());
 
169
            for (std::list<Stack*>::iterator it = stacks.begin(); 
 
170
                 it != stacks.end(); it++)
 
171
                stackvector.push_back(*it);
168
172
        }
169
173
    }
170
174
 
184
188
    return mysize;
185
189
}
186
190
 
187
 
unsigned int Stacklist::countArmies()
188
 
{
189
 
    unsigned int mysize = 0;
190
 
 
191
 
    for (iterator it = begin(); it != end(); it++)
 
191
unsigned int Stacklist::getNoOfArmies()
 
192
{
 
193
    unsigned int mysize = 0;
 
194
 
 
195
    for (Playerlist::iterator pit = Playerlist::getInstance()->begin();
 
196
        pit != Playerlist::getInstance()->end(); pit++)
 
197
    {
 
198
        mysize += (*pit)->getStacklist()->countArmies();
 
199
    }
 
200
 
 
201
    return mysize;
 
202
}
 
203
 
 
204
unsigned int Stacklist::countArmies() const
 
205
{
 
206
    unsigned int mysize = 0;
 
207
 
 
208
    for (const_iterator it = begin(); it != end(); it++)
192
209
      mysize += (*it)->size();
193
210
 
194
211
    return mysize;
195
212
}
196
213
 
197
 
unsigned int Stacklist::countHeroes()
 
214
unsigned int Stacklist::countAllies() const
198
215
{
199
 
  std::vector<guint32> heroes;
200
 
  getHeroes(heroes);
201
 
  return heroes.size();
 
216
    unsigned int mysize = 0;
 
217
 
 
218
    for (const_iterator it = begin(); it != end(); it++)
 
219
      {
 
220
        mysize += (*it)->countAllies();
 
221
      }
 
222
 
 
223
    return mysize;
202
224
}
203
225
 
204
226
Stacklist::Stacklist()
240
262
 
241
263
}
242
264
 
243
 
Stack* Stacklist::getNextMovable()
 
265
Stack* Stacklist::getNextMovable() const
244
266
{
245
267
    Player *player = Playerlist::getInstance()->getActiveplayer();
246
268
    
247
 
    iterator it = begin();
 
269
    const_iterator it = begin();
248
270
    
249
271
    //first, if we already have an active stack, loop through until we meet it
250
272
    if (d_activestack)
283
305
        return d_activestack;
284
306
}
285
307
 
286
 
Stack *Stacklist::getStackById(guint32 id)
 
308
Stack *Stacklist::getStackById(guint32 id) const
287
309
{
288
 
  for (Stacklist::iterator i = begin(), e = end(); i != e; ++i)
289
 
    if ((*i)->getId() == id)
290
 
      return *i;
291
 
    
292
 
  return 0;
 
310
  IdMap::const_iterator it = d_id.find(id);
 
311
  if (it != d_id.end())
 
312
    return (*it).second;
 
313
  else
 
314
    return NULL;
293
315
}
294
316
 
295
 
Stack *Stacklist::getArmyStackById(guint32 army)
 
317
Stack *Stacklist::getArmyStackById(guint32 army) const
296
318
{
297
 
  for (Stacklist::iterator i = begin(), e = end(); i != e; ++i)
 
319
  for (Stacklist::const_iterator i = begin(), e = end(); i != e; ++i)
298
320
    if ((*i)->getArmyById(army))
299
321
      return *i;
300
322
  
393
415
    if (tag == Stack::d_tag)
394
416
    {
395
417
        Stack* s = new Stack(helper);
396
 
        if ((active != 0) && (s->getId() == active))
397
 
        {
398
 
            d_activestack = s;
399
 
        }
 
418
        if (active != 0 && s->getId() == active)
 
419
          d_activestack = s;
400
420
 
401
421
        add(s);
402
422
        return true;
405
425
    return false;
406
426
}
407
427
 
408
 
Stack* Stacklist::getOwnObjectAt(int x, int y)
409
 
{
410
 
  for (const_iterator it = begin(); it != end(); it++)
411
 
    if (((*it)->getPos().x == x) && ((*it)->getPos().y == y))
412
 
      return *it;
413
 
  return 0;
414
 
}
415
 
 
416
 
void Stacklist::getHeroes(std::vector<guint32>& dst)
417
 
{
418
 
  for (Stacklist::iterator it = begin(); it != end(); it++)
 
428
void Stacklist::getHeroes(std::vector<guint32>& dst) const
 
429
{
 
430
  for (Stacklist::const_iterator it = begin(); it != end(); it++)
419
431
    (*it)->getHeroes(dst);
420
432
}
421
433
 
422
 
void Stacklist::collectTaxes(Player *p, guint32 num_cities)
 
434
void Stacklist::collectTaxes(Player *p, guint32 num_cities) const
423
435
{
424
436
  std::vector<guint32> hero_ids;
425
437
  getHeroes(hero_ids);
446
458
bool Stacklist::canJumpOverTooLargeStack(Stack *s)
447
459
{
448
460
  bool found = false;
449
 
  guint32 mp = s->getGroupMoves();
 
461
  guint32 mp = s->getMoves();
450
462
  for (Path::iterator it = s->getPath()->begin(); it != s->getPath()->end(); it++)
451
463
    {
452
464
      guint32 moves = s->calculateTileMovementCost(*it);
453
465
      if (moves > mp)
454
466
        return false;
455
467
      mp -= moves;
456
 
      Stack *another_stack = getObjectAt(*it);
457
 
      if (another_stack)
458
 
        {
459
 
          if (another_stack->getOwner() != s->getOwner())
460
 
            return false;
461
 
          if (s->canJoin(another_stack) == true)
462
 
            {
463
 
              found = true;
464
 
              break;
465
 
            }
466
 
        }
467
 
      else
468
 
        {
469
 
          found = true;
470
 
          break;
471
 
        }
 
468
      if (GameMap::getEnemyCity(*it) != NULL)
 
469
        return false;
 
470
      if (GameMap::getEnemyStack(*it) != NULL)
 
471
        return false;
 
472
      if (GameMap::canJoin(s, *it) == true)
 
473
        return true;
472
474
    }
473
475
  return found;
474
476
}
475
477
 
476
 
std::list<Hero*> Stacklist::getHeroes()
 
478
std::list<Hero*> Stacklist::getHeroes() const
477
479
{
478
480
  std::list<Hero*> heroes;
479
481
  std::vector<guint32> hero_ids;
480
482
  getHeroes(hero_ids);
481
 
  for (std::vector<guint32>::iterator it = hero_ids.begin(); 
 
483
  for (std::vector<guint32>::const_iterator it = hero_ids.begin(); 
482
484
       it != hero_ids.end(); it++)
483
485
    {
484
486
        Stack *s = getArmyStackById(*it);
492
494
  return heroes;
493
495
}
494
496
        
495
 
Hero *Stacklist::getNearestHero(Vector<int> pos, int dist)
 
497
Hero *Stacklist::getNearestHero(Vector<int> pos, int dist) const
496
498
{
497
499
  std::list<Hero*> heroes = getHeroes();
498
500
  LocationList<Location*> hero_locales;
515
517
 
516
518
bool Stacklist::addPositionToMap(Stack *stack)
517
519
{
518
 
  if (d_object1.find(stack->getPos()) == d_object1.end())
519
 
    {
520
 
      d_object1[stack->getPos()] = stack;
521
 
      return true;
522
 
    }
523
 
 
524
 
  if (d_object2.find(stack->getPos()) == d_object2.end())
525
 
    {
526
 
      d_object2[stack->getPos()] = stack;
527
 
      return true;
528
 
    }
529
 
  
530
 
  return false;
 
520
  snewpos.emit(stack, stack->getPos());
 
521
  return true;
531
522
}
532
523
 
533
524
bool Stacklist::deletePositionFromMap(Stack *stack)
534
525
{
535
 
  Stack *found = NULL;
536
 
  PositionMap::iterator it = d_object1.find(stack->getPos());
537
 
  if (it != d_object1.end())
538
 
    {
539
 
      found = (*it).second;
540
 
      if (found->getId() == stack->getId())
541
 
        {
542
 
          d_object1.erase(it);
543
 
          //d_object1[stack->getPos()] = NULL;
544
 
          return true;
545
 
        }
546
 
    }
547
 
 
548
 
  it = d_object2.find(stack->getPos());
549
 
  if (it != d_object2.end())
550
 
    {
551
 
      found = (*it).second;
552
 
      if (found->getId() == stack->getId())
553
 
        {
554
 
          //d_object2[stack->getPos()] = NULL;
555
 
          d_object2.erase(it);
556
 
          return true;
557
 
        }
558
 
    }
559
 
  
560
 
  return false;
 
526
  soldpos.emit(stack, stack->getPos());
 
527
  return true;
561
528
}
562
529
 
563
530
void Stacklist::add(Stack *stack)
564
531
{
565
532
  push_back(stack);
 
533
  d_id[stack->getId()] = stack;
566
534
  if (stack->getPos() != Vector<int>(-1,-1))
567
535
    {
568
536
      bool added = addPositionToMap(stack);
595
563
      for (std::list<sigc::connection>::iterator lit = list.begin(); lit != list.end(); lit++)
596
564
        (*lit).disconnect();
597
565
    }
 
566
  d_id.erase(d_id.find(stack->getId()));
598
567
  return;
599
568
}
600
569
void Stacklist::on_stack_starts_moving (Stack *stack)
607
576
  addPositionToMap(stack);
608
577
  return;
609
578
}
610
 
void Stacklist::getObjectAt(Vector<int> pos, Stack *& s1, Stack *&s2)
611
 
{
612
 
  PositionMap::iterator it = d_object1.find(pos);
613
 
  if (it == d_object1.end())
614
 
    s1 = NULL;
615
 
  else
616
 
    s1 = (*it).second;
617
 
  it = d_object2.find(pos);
618
 
  if (it == d_object2.end())
619
 
    s2 = NULL;
620
 
  else
621
 
    s2 = (*it).second;
622
 
}
623
 
        
624
579
void Stacklist::setActivestack(Stack* activestack)
625
580
{
626
581
  d_activestack = activestack;
632
587
    for (Stack::iterator ait = (*it)->begin(); ait != (*it)->end(); ait++)
633
588
      (*ait)->decrementMoves((*ait)->getMoves());
634
589
}
 
590
 
 
591
void Stacklist::changeOwnership(Stack *stack, Player *new_owner)
 
592
{
 
593
  if (new_owner != stack->getOwner())
 
594
    {
 
595
      Stack *new_stack = new Stack(*stack);
 
596
      stack->getOwner()->getStacklist()->flRemove(stack);
 
597
      new_owner->addStack(new_stack);
 
598
    }
 
599
}
 
600
 
 
601
std::list<Vector<int> > Stacklist::getPositions() const
 
602
{
 
603
  std::list<Vector<int> > points;
 
604
  for (const_iterator it = begin(); it != end(); it++)
 
605
    {
 
606
      if (std::find(points.begin(), points.end(), (*it)->getPos()) == 
 
607
          points.end())
 
608
        points.push_back((*it)->getPos());
 
609
    }
 
610
  return points;
 
611
}
 
612
 
 
613
std::list<Stack*> Stacklist::getStacksWithItems() const
 
614
{
 
615
  std::list<Stack*> stacks;
 
616
  for (const_iterator it = begin(); it != end(); it++)
 
617
    {
 
618
      if ((*it)->countItems() > 0)
 
619
        stacks.push_back((*it));
 
620
    }
 
621
  return stacks;
 
622
}
635
623
// End of file