~ubuntu-branches/ubuntu/precise/lordsawar/precise

« back to all changes in this revision

Viewing changes to src/stack.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese, Barry deFreese
  • Date: 2009-10-21 08:02:12 UTC
  • mfrom: (1.1.8 upstream) (5.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20091021080212-wpmd6xdaxrgnn63y
Tags: 0.1.6-1
[ Barry deFreese ]
* New upstream release.
  + Drop libsdl-image1.2 from build-deps, no longer needed.
* Add README.source for quilt patch system.
* Clean up debian/copyright some.
* Bump Standards Version to 3.8.3. (No changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
4
4
// Copyright (C) 2004 John Farrell
5
5
// Copyright (C) 2004, 2005 Andrea Paternesi
6
 
// Copyright (C) 2006, 2007, 2008 Ben Asselstine
 
6
// Copyright (C) 2006, 2007, 2008, 2009 Ben Asselstine
7
7
// Copyright (C) 2007, 2008 Ole Laursen
8
8
//
9
9
//  This program is free software; you can redistribute it and/or modify
10
10
//  it under the terms of the GNU General Public License as published by
11
 
//  the Free Software Foundation; either version 2 of the License, or
 
11
//  the Free Software Foundation; either version 3 of the License, or
12
12
//  (at your option) any later version.
13
13
//
14
14
//  This program is distributed in the hope that it will be useful,
31
31
#include "armysetlist.h"
32
32
#include "counter.h"
33
33
#include "army.h"
34
 
#include "templelist.h"
35
34
#include "hero.h"
36
35
#include "GameMap.h"
37
36
#include "vector.h"
53
52
    d_path = new Path();
54
53
}
55
54
 
56
 
Stack::Stack(Uint32 id, Player* player, Vector<int> pos)
 
55
Stack::Stack(guint32 id, Player* player, Vector<int> pos)
57
56
    : UniquelyIdentified(id), Movable(pos), Ownable(player), 
58
57
    d_defending(false), d_parked(false), d_deleting(false)
59
58
{
62
61
 
63
62
Stack::Stack(const Stack& s)
64
63
    : UniquelyIdentified(s), Movable(s), Ownable(s), 
65
 
    d_defending(s.d_defending), d_parked(s.d_parked), d_deleting(false)
 
64
    d_defending(s.d_defending), d_parked(s.d_parked), 
 
65
    d_deleting(false)
66
66
{
67
 
    clear();
68
 
    d_path = new Path();
69
 
    //deep copy the other stack's armies
70
 
    
 
67
    d_path = new Path(*s.d_path);
 
68
 
71
69
    for (const_iterator sit = s.begin(); sit != s.end(); sit++)
72
70
    {
73
71
        if ((*sit)->isHero())
117
115
{
118
116
  debug("moveOneStep()");
119
117
 
120
 
  Vector<int> dest = *getPath()->front();
 
118
  Vector<int> dest = getFirstPointInPath();
121
119
  moveToDest(dest, skipping);
122
120
 
123
121
  //now remove first point of the path
186
184
        (*it)->setInShip(false);
187
185
    }
188
186
 
189
 
  Uint32 maptype = GameMap::getInstance()->getTile(dest.x,dest.y)->getMaptileType();
 
187
  guint32 maptype = GameMap::getInstance()->getTile(dest.x,dest.y)->getMaptileType();
190
188
  //how many moves does the stack need to travel to dest?
191
189
  int needed_moves = calculateTileMovementCost(dest);
192
190
 
205
203
    }
206
204
 
207
205
  //update position and status
 
206
  smoving.emit(this);
208
207
  setPos(dest);
 
208
  smoved.emit(this);
209
209
 
210
210
  setFortified(false);
211
211
  setDefending(false);
235
235
}
236
236
 
237
237
// return the maximum moves of this stack by checking the moves of each army
238
 
Uint32 Stack::getGroupMoves() const
 
238
guint32 Stack::getGroupMoves() const
239
239
{
240
240
  if (empty())
241
241
    return 0;
291
291
 
292
292
// decrement each armys moves by needed moves to travel
293
293
 
294
 
void Stack::decrementMoves(Uint32 moves)
 
294
void Stack::decrementMoves(guint32 moves)
295
295
{
296
296
  debug("decrement_moves()");
297
297
 
319
319
Army* Stack::getStrongestArmy(bool hero) const
320
320
{
321
321
  Army *strongest = 0;
322
 
  Uint32 highest_strength = 0;
 
322
  guint32 highest_strength = 0;
323
323
 
324
324
  for (const_iterator it = begin(); it != end(); ++it)
325
325
    {
335
335
  return strongest;
336
336
}
337
337
 
338
 
Army *Stack::getArmyById(Uint32 id) const
 
338
Army *Stack::getArmyById(guint32 id) const
339
339
{
340
340
  for (Stack::const_iterator i = begin(), e = end(); i != e; ++i)
341
341
    if ((*i)->getId() == id)
344
344
  return 0;
345
345
}
346
346
 
347
 
Uint32 Stack::countGroupedArmies() const
 
347
guint32 Stack::countGroupedArmies() const
348
348
{
349
 
  Uint32 count = 0;
 
349
  guint32 count = 0;
350
350
  if (empty())
351
351
    return 0;
352
352
  for (const_iterator it = begin(); it != end(); ++it)
362
362
    return;
363
363
  for (iterator it = begin(); it != end(); ++it)
364
364
    (*it)->setGrouped(true);
 
365
  sgrouped.emit(this, true);
365
366
  return;
366
367
}
367
368
 
374
375
    (*it)->setGrouped(false);
375
376
  //set first army to be in the group
376
377
  (*(begin()))->setGrouped(true);
 
378
  sgrouped.emit(this, false);
377
379
  return;
378
380
}
379
381
 
395
397
  return 0;
396
398
}
397
399
 
398
 
void Stack::getHeroes(std::vector<Uint32>& dst) const
 
400
void Stack::getHeroes(std::vector<guint32>& dst) const
399
401
{
400
402
  debug("getHeroes - stack = " << this)
401
403
    for (const_iterator it = begin(); it != end(); ++it)
412
414
  int count = 0;
413
415
  for (iterator it = begin(); it != end(); it++)
414
416
    {
415
 
      Temple *temple = Templelist::getInstance()->getObjectAt(getPos());
 
417
      Temple *temple = GameMap::getTemple(getPos());
416
418
      if ((*it)->bless(temple))
417
419
        count++;
418
420
    }
419
421
  return count;
420
422
}
421
423
 
422
 
Uint32 Stack::calculateTileMovementCost(Vector<int> pos) const
 
424
guint32 Stack::calculateTileMovementCost(Vector<int> pos) const
423
425
{
424
426
  Maptile* tile = GameMap::getInstance()->getTile(pos);
425
 
  Uint32 moves = tile->getMoves();
426
 
  Uint32 bonus = calculateMoveBonus();
 
427
  guint32 moves = tile->getMoves();
 
428
  guint32 bonus = calculateMoveBonus();
427
429
  if (bonus & tile->getMaptileType() && moves > 1)
428
430
    moves = 2;
429
431
  else if (isFlying() && moves > 1)
431
433
  return moves;
432
434
}
433
435
 
 
436
Vector<int> Stack::getFirstPointInPath() const
 
437
{
 
438
  Vector<int> p = *(d_path->begin());
 
439
  return p;
 
440
}
 
441
 
 
442
Vector<int> Stack::getLastReachablePointInPath() const
 
443
{
 
444
  if (d_path->size() == 0)
 
445
    return Vector<int>(-1,-1);
 
446
  unsigned int count = 0;
 
447
  for (Path::iterator it = d_path->begin(); it != d_path->end(); it++)
 
448
    {
 
449
      count++;
 
450
      if (count == d_path->getMovesExhaustedAtPoint())
 
451
        return (*it);
 
452
    }
 
453
  return Vector<int>(-1,-1);
 
454
}
 
455
Vector<int> Stack::getLastPointInPath() const
 
456
{
 
457
  if (d_path->size() == 0)
 
458
    return Vector<int>(-1,-1);
 
459
  Vector<int> p = d_path->back();
 
460
  return p;
 
461
}
 
462
 
434
463
bool Stack::enoughMoves() const
435
464
{
436
465
  if (d_path->size() == 0)
437
466
    return true; //we have enough moves to move nowhere!
438
467
 
439
 
  Vector<int> p = **(d_path->begin());
440
 
  Uint32 needed = calculateTileMovementCost(p);
441
 
 
442
 
  // now check if all armies fulfill this requirement
443
 
  for (const_iterator it = begin(); it != end(); it++)
444
 
    if ((*it)->getMoves() < needed)
445
 
      return false;
446
 
 
447
 
  return true;
 
468
  Vector<int> p = getFirstPointInPath();
 
469
  guint32 needed = calculateTileMovementCost(p);
 
470
 
 
471
  if (getGroupMoves() >= needed)
 
472
    return true;
 
473
 
 
474
  return false;
448
475
}
449
476
 
450
477
bool Stack::canMove() const
468
495
  return 0;
469
496
}
470
497
 
471
 
Uint32 Stack::getMaxSight() const
 
498
guint32 Stack::getMaxSight() const
472
499
{
473
 
  Uint32 max = 0;
 
500
  guint32 max = 0;
474
501
  for (const_iterator it = begin(); it != end(); it++)
475
502
    if ((*it)->getStat(Army::SIGHT) > max)
476
503
      max = (*it)->getStat(Army::SIGHT);
486
513
 
487
514
void Stack::nextTurn()
488
515
{
489
 
  Uint32 movement_multiplier = 1;
 
516
  guint32 movement_multiplier = 1;
490
517
  setParked(false);
491
518
 
492
519
  //count the number of items that double the movement in the stack.
494
521
    if ((*it)->isHero())
495
522
      {
496
523
        Hero *hero = dynamic_cast<Hero*>(*it);
497
 
        Uint32 bonus = hero->getBackpack()->countMovementDoublers();
498
 
        for (Uint32 i = 0; i < bonus; i++)
 
524
        guint32 bonus = hero->getBackpack()->countMovementDoublers();
 
525
        for (guint32 i = 0; i < bonus; i++)
499
526
          movement_multiplier*=2;
500
527
      }
501
528
 
513
540
    }
514
541
 
515
542
  //recalculate paths
516
 
  getPath()->recalculate(this);
 
543
 
 
544
  d_path->recalculate(this);
517
545
 
518
546
}
519
547
 
588
616
  return erase(object);
589
617
}
590
618
 
591
 
Uint32 Stack::calculateMoveBonus() const
 
619
guint32 Stack::calculateMoveBonus() const
592
620
{
593
 
  Uint32 d_bonus = 0;
 
621
  guint32 d_bonus = 0;
594
622
 
595
623
  bool landed = false;
596
 
  Uint32 bonus;
 
624
  guint32 bonus;
597
625
  // check to see if we're all flying
598
626
  int num_landedhero = 0;
599
627
  int num_flyer = 0;
658
686
 
659
687
bool Stack::isFlying () const
660
688
{
661
 
  Uint32 d_bonus = calculateMoveBonus();
 
689
  guint32 d_bonus = calculateMoveBonus();
662
690
  if (d_bonus == Tile::isFlying())
663
691
    return true;
664
692
  else
679
707
  return false;
680
708
}
681
709
 
682
 
Uint32 getFightOrder(std::list<Uint32> values, Uint32 value)
 
710
guint32 getFightOrder(std::list<guint32> values, guint32 value)
683
711
{
684
 
  Uint32 count = 0;
685
 
  for (std::list<Uint32>::const_iterator it = values.begin(); 
 
712
  guint32 count = 0;
 
713
  for (std::list<guint32>::const_iterator it = values.begin(); 
686
714
       it != values.end(); it++)
687
715
    {
688
716
      count++;
694
722
 
695
723
bool Stack::armyCompareFightOrder (const Army *lhs, const Army *rhs)  
696
724
{
697
 
  std::list<Uint32> lhs_fight_order = lhs->getOwner()->getFightOrder();
698
 
  std::list<Uint32> rhs_fight_order = rhs->getOwner()->getFightOrder();
699
 
  Uint32 lhs_rank = getFightOrder (lhs_fight_order, lhs->getTypeId());
700
 
  Uint32 rhs_rank = getFightOrder (rhs_fight_order, rhs->getTypeId());
 
725
  std::list<guint32> lhs_fight_order = lhs->getOwner()->getFightOrder();
 
726
  std::list<guint32> rhs_fight_order = rhs->getOwner()->getFightOrder();
 
727
  guint32 lhs_rank = getFightOrder (lhs_fight_order, lhs->getTypeId());
 
728
  guint32 rhs_rank = getFightOrder (rhs_fight_order, rhs->getTypeId());
701
729
  return lhs_rank < rhs_rank; 
702
730
}
703
731
 
737
765
  return false;
738
766
}
739
767
 
740
 
Uint32 Stack::getUpkeep()
 
768
guint32 Stack::getUpkeep()
741
769
{
742
 
  Uint32 upkeep = 0;
 
770
  guint32 upkeep = 0;
743
771
  for (iterator it = begin(); it != end(); it++)
744
772
    upkeep += (*it)->getUpkeep();
745
773
  return upkeep;
747
775
 
748
776
bool Stack::canJoin(const Stack *stack) const
749
777
{
750
 
  Uint32 joinSize = countGroupedArmies();
 
778
  guint32 joinSize = countGroupedArmies();
751
779
  if (joinSize == 0)
752
780
    joinSize = size();
753
781
 
761
789
 
762
790
}
763
791
 
764
 
std::vector<Uint32> Stack::determineReachableArmies(Vector<int> dest)
 
792
std::vector<guint32> Stack::determineReachableArmies(Vector<int> dest)
765
793
{
766
 
  std::vector<Uint32> ids;
 
794
  std::vector<guint32> ids;
767
795
  //try each army individually to see if it reaches
768
796
  for (iterator it = begin(); it != end(); it++)
769
797
    {
793
821
          Stack *stack = Stack::createNonUniqueStack(getOwner(), getPos());
794
822
          stack->push_back(*it);
795
823
          //also push back the rest of the known reachables
796
 
          std::vector<Uint32>::iterator iit = ids.begin();
 
824
          std::vector<guint32>::iterator iit = ids.begin();
797
825
          for (; iit != ids.end(); iit++)
798
826
            {
799
827
              Army *army = getArmyById(*iit);
811
839
  return ids;
812
840
}
813
841
 
814
 
Uint32 Stack::countArmiesBlessedAtTemple(Uint32 temple_id)
 
842
guint32 Stack::countArmiesBlessedAtTemple(guint32 temple_id)
815
843
{
816
 
  Uint32 blessed = 0;
 
844
  guint32 blessed = 0;
817
845
  for (iterator it = begin(); it != end(); it++)
818
846
    {
819
847
      if ((*it)->blessedAtTemple(temple_id))
822
850
    return blessed;
823
851
}
824
852
        
825
 
Uint32 Stack::scout(Player *p, Vector<int> src, Vector<int> dest, 
826
 
                    const ArmyProdBase *prodbase)
827
 
{
828
 
  Stack *stack = Stack::createNonUniqueStack(p, src);
829
 
 
830
 
  Army *army;
831
 
  if (!prodbase)
832
 
    {
833
 
      ArmyProto *proto = Armysetlist::getInstance()->getScout(p->getArmyset());
834
 
      if (!proto)
835
 
        return 0;
836
 
      army = Army::createNonUniqueArmy (*proto, p);
837
 
    }
838
 
  else
839
 
    army = Army::createNonUniqueArmy (*prodbase, p);
840
 
 
841
 
  if (!army)
842
 
    return 0;
843
 
  stack->push_back(army);
844
 
  Uint32 mp = stack->getPath()->calculate(stack, dest);
845
 
  delete stack;
846
 
  return mp;
847
 
}
848
 
 
849
 
Uint32 Stack::scout(Stack *stack, Vector<int> dest)
850
 
{
851
 
  Stack *scout_stack = new Stack(*stack);
852
 
  Uint32 mp = scout_stack->getPath()->calculate(scout_stack, dest);
853
 
  delete scout_stack;
854
 
  return mp;
855
 
}
856
 
 
857
853
Stack* Stack::createNonUniqueStack(Player *player, Vector<int> pos)
858
854
{
859
855
  return new Stack(0, player, pos);
860
856
}
861
857
 
862
 
Uint32 Stack::getMaxGroupLandMoves() const
 
858
guint32 Stack::getMaxGroupLandMoves() const
863
859
{
864
860
  if (empty())
865
861
    return 0;
868
864
 
869
865
  //copy the stack, reset the moves and return the group moves
870
866
  Stack *copy = new Stack (*this);
 
867
  copy->getPath()->clear(); //this prevents triggering path recalc in nextTurn
871
868
  if (copy->countGroupedArmies() == 0)
872
869
    copy->group();
873
870
  copy->decrementMoves(copy->getGroupMoves());
874
871
  copy->nextTurn();
875
 
  Uint32 moves = copy->getGroupMoves();
 
872
  guint32 moves = copy->getGroupMoves();
876
873
  if (isFlying() == true)
877
874
    {
878
875
      delete copy;
891
888
  return moves;
892
889
}
893
890
 
894
 
Uint32 Stack::getMaxGroupBoatMoves() const
 
891
guint32 Stack::getMaxGroupBoatMoves() const
895
892
{
896
893
  if (empty())
897
894
    return 0;
900
897
 
901
898
  //copy the stack, reset the moves and return the group moves
902
899
  Stack *copy = new Stack (*this);
 
900
  copy->getPath()->clear(); //this prevents triggering path recalc in nextTurn
903
901
  if (copy->countGroupedArmies() == 0)
904
902
    copy->group();
905
903
  copy->nextTurn();
906
 
  Uint32 moves = copy->getGroupMoves();
 
904
  guint32 moves = copy->getGroupMoves();
907
905
  if (isFlying() == true)
908
906
    {
909
907
      delete copy;
925
923
  delete copy;
926
924
  return moves;
927
925
}
 
926
        
 
927
void Stack::setPath(const Path p)
 
928
{
 
929
  if (d_path)
 
930
    delete d_path;
 
931
  d_path = new Path(p);
 
932
}
928
933
// End of file