1 // Copyright (C) 2001, 2002, 2003 Michael Bartl
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Ulf Lorenz
3 // Copyright (C) 2004, 2005 Andrea Paternesi
4 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2014, 2015, 2020 Ben Asselstine
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Library General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 // 02110-1301, USA.
20
21 #include <iostream>
22 #include <gtkmm.h>
23 #include <sigc++/functors/mem_fun.h>
24 #include <assert.h>
25
26 #include "rectangle.h"
27 #include "armysetlist.h"
28 #include "armyset.h"
29 #include "File.h"
30 #include "defs.h"
31 #include "ucompose.hpp"
32 #include "PixMask.h"
33 #include "tarhelper.h"
34 #include "setlist.h"
35
36 //#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
37 #define debug(x)
38
39 Armysetlist* Armysetlist::s_instance = 0;
40
getInstance()41 Armysetlist* Armysetlist::getInstance()
42 {
43 if (!s_instance)
44 s_instance = new Armysetlist();
45
46 return s_instance;
47 }
48
deleteInstance()49 void Armysetlist::deleteInstance()
50 {
51 if (s_instance)
52 delete s_instance;
53
54 s_instance = 0;
55 }
56
Armysetlist()57 Armysetlist::Armysetlist()
58 : SetList(Armyset::file_extension)
59 {
60 signal_add().connect(sigc::mem_fun(this, &Armysetlist::on_armyset_added));
61 signal_reload().connect(sigc::mem_fun(this, &Armysetlist::on_armyset_reloaded));
62 loadSets(SetList::scan(Armyset::file_extension));
63 loadSets(SetList::scan(Armyset::file_extension, false));
64 }
65
on_armyset_added(Armyset * armyset)66 void Armysetlist::on_armyset_added(Armyset *armyset)
67 {
68 for (Armyset::iterator ait = armyset->begin(); ait != armyset->end(); ait++)
69 d_armies[armyset->getId()][(*ait)->getId()] = (*ait);
70 }
71
on_armyset_reloaded(Armyset * armyset)72 void Armysetlist::on_armyset_reloaded(Armyset *armyset)
73 {
74 d_armies[armyset->getId()].clear();
75 for (Armyset::iterator ait = armyset->begin(); ait != armyset->end(); ait++)
76 d_armies[armyset->getId()][(*ait)->getId()] = (*ait);
77 }
78
~Armysetlist()79 Armysetlist::~Armysetlist()
80 {
81 uninstantiateImages();
82 for (iterator it = begin(); it != end(); it++)
83 delete *it;
84 clear();
85 // remove all army entries
86 /*
87 for (ArmyPrototypeMap::iterator it = d_armies.begin();
88 it != d_armies.end(); it++)
89 {
90 while (!(*it).second.empty())
91 delete (*((*it).second).begin()).second;
92 }
93 */
94 }
95
getArmy(guint32 id,guint32 type_id) const96 ArmyProto* Armysetlist::getArmy(guint32 id, guint32 type_id) const
97 {
98 // always use ArmyProtoMap::find for searching, else a default entry is
99 // created, which can produce really bad results!!
100 ArmyPrototypeMap::const_iterator it = d_armies.find(id);
101
102 // armyset does not exist
103 if (it == d_armies.end())
104 return NULL;
105
106 IdArmyPrototypeMap::const_iterator j = (*it).second.find(type_id);
107 if (j == (*it).second.end())
108 return NULL;
109 return (*j).second;
110 }
111
lookupWeakestQuickestArmy(guint32 id) const112 ArmyProto* Armysetlist::lookupWeakestQuickestArmy(guint32 id) const
113 {
114 Armyset *a = get(id);
115 if (a)
116 return a->lookupWeakestQuickestArmy();
117 return NULL;
118 }
119
getShipMask(guint32 id)120 PixMask* Armysetlist::getShipMask (guint32 id)
121 {
122 for (iterator it = begin(); it != end(); it++)
123 {
124 if ((*it)->getId() == id)
125 return (*it)->getShipMask();
126 }
127 return NULL;
128 }
129
getTileSize(guint32 id)130 guint32 Armysetlist::getTileSize(guint32 id)
131 {
132 for (iterator it = begin(); it != end(); it++)
133 {
134 if ((*it)->getId() == id)
135 return (*it)->getTileSize();
136 }
137 return 0;
138 }
139
getBagPic(guint32 id)140 PixMask* Armysetlist::getBagPic (guint32 id)
141 {
142 for (iterator it = begin(); it != end(); it++)
143 {
144 if ((*it)->getId() == id)
145 return (*it)->getBagPic();
146 }
147 return NULL;
148 }
149
getStandardPic(guint32 id)150 PixMask* Armysetlist::getStandardPic (guint32 id)
151 {
152 for (iterator it = begin(); it != end(); it++)
153 {
154 if ((*it)->getId() == id)
155 return (*it)->getStandardPic();
156 }
157 return NULL;
158 }
159
getStandardMask(guint32 id)160 PixMask* Armysetlist::getStandardMask (guint32 id)
161 {
162 for (iterator it = begin(); it != end(); it++)
163 {
164 if ((*it)->getId() == id)
165 return (*it)->getStandardMask();
166 }
167 return NULL;
168 }
169
instantiateImages(bool & broken)170 void Armysetlist::instantiateImages(bool &broken)
171 {
172 broken = false;
173 for (iterator it = begin(); it != end(); it++)
174 {
175 if (!broken)
176 {
177 if ((*it)->validate () == true)
178 (*it)->instantiateImages(true, broken);
179 }
180 }
181 }
182
uninstantiateImages()183 void Armysetlist::uninstantiateImages()
184 {
185 for (iterator it = begin(); it != end(); it++)
186 (*it)->uninstantiateImages();
187 }
188
getShipPic(guint32 id)189 PixMask* Armysetlist::getShipPic (guint32 id)
190 {
191 for (iterator it = begin(); it != end(); it++)
192 {
193 if ((*it)->getId() == id)
194 return (*it)->getShipPic();
195 }
196 return NULL;
197 }
198
199