1 // Copyright (C) 2009, 2010, 2014, 2015 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 "stackreflist.h"
18 #include "stack.h"
19 #include "stacklist.h"
20 #include "player.h"
21 
StackReflist()22 StackReflist::StackReflist()
23 {
24 }
25 
StackReflist(Stacklist * sl,bool skip_parked_stacks)26 StackReflist::StackReflist(Stacklist *sl, bool skip_parked_stacks)
27 {
28   for (Stacklist::iterator it = sl->begin(); it != sl->end(); it++)
29     {
30       if (skip_parked_stacks == true && (*it)->getParked() == true)
31         continue;
32       addStack(*it);
33     }
34 }
35 
StackReflist(std::list<Stack * > s,bool skip_parked_stacks)36 StackReflist::StackReflist(std::list<Stack*> s, bool skip_parked_stacks)
37 {
38   for (std::list<Stack*>::iterator it = s.begin(); it != s.end(); it++)
39     {
40       if (skip_parked_stacks == true && (*it)->getParked() == true)
41         continue;
42       addStack(*it);
43     }
44 }
45 
getStackById(guint32 id) const46 Stack *StackReflist::getStackById(guint32 id) const
47 {
48   IdMap::const_iterator it = d_id.find(id);
49   if (it != d_id.end())
50     return (*it).second;
51   else
52     return NULL;
53 }
54 
addStack(Stack * stack)55 void StackReflist::addStack(Stack *stack)
56 {
57   push_back(stack);
58   d_id[stack->getId()] = stack;
59 }
60 
contains(guint32 id) const61 bool StackReflist::contains(guint32 id) const
62 {
63   if (getStackById(id) != NULL)
64     return true;
65   return false;
66 }
67 
removeStack(guint32 id)68 bool StackReflist::removeStack(guint32 id)
69 {
70   Stack *s = getStackById(id);
71   if (s)
72     {
73       d_id.erase(d_id.find(s->getId()));
74       remove(s);
75       return true;
76     }
77   return false;
78 }
79 
eraseStack(StackReflist::iterator it)80 StackReflist::iterator StackReflist::eraseStack(StackReflist::iterator it)
81 {
82   if (it != end())
83     {
84       Stack *s = *it;
85       if (s)
86         {
87           IdMap::iterator i = d_id.find(s->getId());
88           if (i != d_id.end())
89             d_id.erase(i);
90         }
91     }
92   return erase(it);
93 }
94 
eraseStack(StackReflist::iterator it,guint32 id)95 StackReflist::iterator StackReflist::eraseStack(StackReflist::iterator it, guint32 id)
96 {
97   if (it != end())
98     {
99       IdMap::iterator i = d_id.find(id);
100       if (i != d_id.end())
101         d_id.erase(i);
102     }
103   return erase(it);
104 }
105 
countArmies() const106 guint32 StackReflist::countArmies() const
107 {
108   guint32 count = 0;
109   for (const_iterator it = begin(); it != end(); it++)
110     count += (*it)->size();
111   return count;
112 }
113 
changeOwnership(Player * new_player)114 void StackReflist::changeOwnership(Player *new_player)
115 {
116   for (IdMap::iterator it = d_id.begin(); it != d_id.end(); it++)
117     {
118       guint32 id = (*it).first;
119       Stack *new_stack = new_player->getStacklist()->getStackById(id);
120       if (new_stack)
121         (*it).second = new_stack;
122     }
123 }
124 
getIdOfStack(Stack * stack,guint32 & id)125 bool StackReflist::getIdOfStack(Stack *stack, guint32 &id)
126 {
127   for (IdMap::iterator it = d_id.begin(); it != d_id.end(); it++)
128     {
129       if ((*it).second == stack)
130         {
131           id = (*it).first;
132           return true;
133         }
134     }
135   return false;
136 }
137