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

« back to all changes in this revision

Viewing changes to src/stackreflist.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:
 
1
// Copyright (C) 2009, 2010 Ben Asselstine
 
2
//
 
3
//  This program is free software; you can redistribute it and/or modify
 
4
//  it under the terms of the GNU General Public License as published by
 
5
//  the Free Software Foundation; either version 3 of the License, or
 
6
//  (at your option) any later version.
 
7
//
 
8
//  This program is distributed in the hope that it will be useful,
 
9
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
//  GNU Library General Public License for more details.
 
12
//
 
13
//  You should have received a copy of the GNU General Public License
 
14
//  along with this program; if not, write to the Free Software
 
15
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
 
16
//  02110-1301, USA.
 
17
#include <algorithm>
 
18
#include "stackreflist.h"
 
19
#include "stack.h"
 
20
#include "stacklist.h"
 
21
#include "player.h"
 
22
 
 
23
StackReflist::StackReflist()
 
24
{
 
25
}
 
26
 
 
27
StackReflist::StackReflist(Stacklist *sl, bool skip_parked_stacks)
 
28
{
 
29
  for (Stacklist::iterator it = sl->begin(); it != sl->end(); it++)
 
30
    {
 
31
      if (skip_parked_stacks == true && (*it)->getParked() == true)
 
32
        continue;
 
33
      addStack(*it);
 
34
    }
 
35
}
 
36
 
 
37
StackReflist::StackReflist(std::list<Stack*> s, bool skip_parked_stacks)
 
38
{
 
39
  for (std::list<Stack*>::iterator it = s.begin(); it != s.end(); it++)
 
40
    {
 
41
      if (skip_parked_stacks == true && (*it)->getParked() == true)
 
42
        continue;
 
43
      addStack(*it);
 
44
    }
 
45
}
 
46
 
 
47
StackReflist::~StackReflist()
 
48
{
 
49
}
 
50
 
 
51
Stack *StackReflist::getStackById(guint32 id) const
 
52
{
 
53
  IdMap::const_iterator it = d_id.find(id);
 
54
  if (it != d_id.end())
 
55
    return (*it).second;
 
56
  else
 
57
    return NULL;
 
58
}
 
59
 
 
60
void StackReflist::addStack(Stack *stack)
 
61
{
 
62
  push_back(stack);
 
63
  d_id[stack->getId()] = stack;
 
64
}
 
65
 
 
66
bool StackReflist::contains(guint32 id) const
 
67
{
 
68
  if (getStackById(id) != NULL)
 
69
    return true;
 
70
  return false;
 
71
}
 
72
 
 
73
bool StackReflist::removeStack(guint32 id)
 
74
{
 
75
  Stack *s = getStackById(id);
 
76
  if (s)
 
77
    {
 
78
      d_id.erase(d_id.find(s->getId()));
 
79
      remove(s);
 
80
      return true;
 
81
    }
 
82
  return false;
 
83
}
 
84
 
 
85
StackReflist::iterator StackReflist::eraseStack(StackReflist::iterator it)
 
86
{
 
87
  if (it != end())
 
88
    {
 
89
      Stack *s = *it;
 
90
      if (s)
 
91
        {
 
92
          typedef std::map<guint32, Stack*> IdMap;
 
93
          IdMap::iterator i = d_id.find(s->getId());
 
94
          if (i != d_id.end())
 
95
            d_id.erase(i);
 
96
        }
 
97
    }
 
98
  return erase(it);
 
99
}
 
100
 
 
101
StackReflist::iterator StackReflist::eraseStack(StackReflist::iterator it, guint32 id)
 
102
{
 
103
  if (it != end())
 
104
    {
 
105
      IdMap::iterator i = d_id.find(id);
 
106
      if (i != d_id.end())
 
107
        d_id.erase(i);
 
108
    }
 
109
  return erase(it);
 
110
}
 
111
 
 
112
guint32 StackReflist::countArmies() const
 
113
{
 
114
  guint32 count = 0;
 
115
  for (const_iterator it = begin(); it != end(); it++)
 
116
    count += (*it)->size();
 
117
  return count;
 
118
}
 
119
 
 
120
void StackReflist::changeOwnership(Player *old_player, Player *new_player)
 
121
{
 
122
  for (IdMap::iterator it = d_id.begin(); it != d_id.end(); it++)
 
123
    {
 
124
      guint32 id = (*it).first;
 
125
      Stack *new_stack = new_player->getStacklist()->getStackById(id);
 
126
      if (new_stack)
 
127
        (*it).second = new_stack;
 
128
    }
 
129
}
 
130
 
 
131
bool StackReflist::getIdOfStack(Stack *stack, guint32 &id)
 
132
{
 
133
  for (IdMap::iterator it = d_id.begin(); it != d_id.end(); it++)
 
134
    {
 
135
      if ((*it).second == stack)
 
136
        {
 
137
          id = (*it).first;
 
138
          return true;
 
139
        }
 
140
    }
 
141
  return false;
 
142
}