initial commit, lordsawar source, slightly modified
[lordsawar] / src / shield.cpp
1 //  Copyright (C) 2008, 2009 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
18 //#include <iostream>
19 #include <algorithm>
20 #include <fstream>
21 #include <sstream>
22 #include "shield.h"
23 #include "GraphicsCache.h"
24 #include "xmlhelper.h"
25 #include "ucompose.hpp"
26 #include "shieldset.h"
27
28 std::string Shield::d_tag = "shield";
29
30 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
31 #define debug(x)
32
33 Shield::Shield(XML_Helper* helper)
34 {
35   helper->getData(d_owner, "owner");
36   helper->getData(d_color, "color");
37 }
38         
39 Shield::Shield(Shield::Colour owner, Gdk::Color color)
40 {
41   d_owner = guint32(owner);
42   d_color = color;
43 }
44
45 Shield::~Shield()
46 {
47   for (iterator it = begin(); it != end(); it++)
48       delete *it;
49 }
50
51 Gdk::Color Shield::get_default_color_for_no(int player_no)
52 {
53   Gdk::Color c;
54   switch (player_no % MAX_PLAYERS)
55     {
56     case Shield::WHITE: c.set_rgb_p(252.0/255.0,252.0/255.0,252.0/255.0); break;
57     //case 1: color.r = 80; color.b = 28; color.g = 172; break;
58     case Shield::GREEN: c.set_rgb_p(80.0/255.0, 195.0/255.0, 28.0/255.0); break;
59     case Shield::YELLOW: c.set_rgb_p(252.0/255.0,236.0/255.0,32.0/255.0); break;
60     case Shield::LIGHT_BLUE: c.set_rgb_p(0,252.0/255.0,252.0/255.0); break;
61     case Shield::RED: c.set_rgb_p(252.0/255.0,160.0/255.0,0);break;
62     case Shield::DARK_BLUE: 
63                       c.set_rgb_p(44.0/255.0,184.0/255.0,252.0/255.0); break;
64     case Shield::ORANGE: c.set_rgb_p(196.0/255.0, 28.0/255.0, 0); break;
65     case Shield::BLACK: c.set_rgb_p(0,0,0); break;
66     }
67     
68     return c;
69 }
70
71 Gdk::Color Shield::get_default_color_for_neutral()
72 {
73   Gdk::Color color;
74   color.set_rgb_p(204.0/255.0,204.0/255.0,204.0/255.0);
75   return color;
76 }
77
78 std::string Shield::colourToString(const Shield::Colour c)
79 {
80   switch (c)
81     {
82     case Shield::WHITE:
83       return "Shield::WHITE"; break;
84     case Shield::GREEN:
85       return "Shield::GREEN"; break;
86     case Shield::YELLOW:
87       return "Shield::YELLOW"; break;
88     case Shield::LIGHT_BLUE:
89       return "Shield::LIGHT_BLUE"; break;
90     case Shield::RED:
91       return "Shield::RED"; break;
92     case Shield::DARK_BLUE:
93       return "Shield::DARK_BLUE"; break;
94     case Shield::ORANGE:
95       return "Shield::ORANGE"; break;
96     case Shield::BLACK:
97       return "Shield::BLACK"; break;
98     case Shield::NEUTRAL:
99       return "Shield::NEUTRAL"; break;
100     }
101   return "Shield::NEUTRAL";
102 }
103
104 bool Shield::save(XML_Helper *helper) const
105 {
106   bool retval = true;
107
108   retval &= helper->openTag(d_tag);
109   retval &= helper->saveData("owner", d_owner);
110   retval &= helper->saveData("color", d_color);
111   for (const_iterator it = begin(); it != end(); it++)
112     (*it)->save(helper);
113   retval &= helper->closeTag();
114   return retval;
115 }
116         
117 void Shield::instantiateImages(Shieldset *s)
118 {
119   for (iterator it = begin(); it != end(); it++)
120     {
121       if ((*it)->getImageName().empty() == false)
122         (*it)->instantiateImages(s->getFile((*it)->getImageName()), s);
123     }
124 }
125 void Shield::uninstantiateImages()
126 {
127   for (iterator it = begin(); it != end(); it++)
128     (*it)->uninstantiateImages();
129 }
130
131 ShieldStyle *Shield::getFirstShieldstyle(ShieldStyle::Type type)
132 {
133   for (iterator i = begin(); i != end(); i++)
134     {
135       if ((*i)->getType() == type)
136         return *i;
137     }
138   return NULL;
139 }