initial commit, lordsawar source, slightly modified
[lordsawar] / src / armyproto.cpp
1 // Copyright (C) 2000, 2001, 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 Ben Asselstine
5 // Copyright (C) 2007, 2008 Ole Laursen
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License as published by
9 //  the Free Software Foundation; either version 3 of the License, or
10 //  (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU Library General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
20 //  02110-1301, USA.
21
22 #include <iostream>
23 #include <sstream>
24 #include <algorithm>
25 #include "armyproto.h"
26 #include "xmlhelper.h"
27 #include "armyset.h"
28 #include "gui/image-helpers.h"
29 #include "Tile.h"
30
31 //#define debug(x) {std::cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<std::endl<<std::flush;}
32 #define debug(x)
33 std::string ArmyProto::d_tag = "armyproto";
34
35 ArmyProto::ArmyProto(const ArmyProto& a)
36     :ArmyProtoBase(a),
37      d_defends_ruins(a.d_defends_ruins), 
38      d_awardable(a.d_awardable), d_image_name(a.d_image_name),
39      d_gender(Hero::NONE)
40 {
41   for (unsigned int c = Shield::WHITE; c <= Shield::NEUTRAL; c++)
42     {
43       d_image_name[c] = a.d_image_name[c];
44       d_image[c] = a.d_image[c];
45       d_mask[c] = a.d_mask[c];
46     }
47 }
48
49 ArmyProto::ArmyProto()
50   :ArmyProtoBase(),
51     d_defends_ruins(false), d_awardable(false), d_gender(Hero::NONE)
52 {
53   for (unsigned int c = Shield::WHITE; c <= Shield::NEUTRAL; c++)
54     {
55       d_image_name[c] = "";
56       d_image[c] = NULL;
57       d_mask[c] = NULL;
58     }
59 }
60
61 ArmyProto::ArmyProto(XML_Helper* helper)
62   :ArmyProtoBase(helper), d_defends_ruins(false), d_awardable(false)
63 {
64   helper->getData(d_image_name[Shield::WHITE], "image_white");
65   helper->getData(d_image_name[Shield::GREEN], "image_green");
66   helper->getData(d_image_name[Shield::YELLOW], "image_yellow");
67   helper->getData(d_image_name[Shield::LIGHT_BLUE], "image_light_blue");
68   helper->getData(d_image_name[Shield::RED], "image_red");
69   helper->getData(d_image_name[Shield::DARK_BLUE], "image_dark_blue");
70   helper->getData(d_image_name[Shield::ORANGE], "image_orange");
71   helper->getData(d_image_name[Shield::BLACK], "image_black");
72   helper->getData(d_image_name[Shield::NEUTRAL], "image_neutral");
73   helper->getData(d_defends_ruins,"defends_ruins");
74   helper->getData(d_awardable,"awardable");
75   std::string gender_str;
76   if (!helper->getData(gender_str, "gender"))
77     d_gender = Hero::NONE;
78   else
79     d_gender = Hero::genderFromString(gender_str);
80   for (unsigned int c = Shield::WHITE; c <= Shield::NEUTRAL; c++)
81     {
82       d_image[c] = NULL;
83       d_mask[c] = NULL;
84     }
85 }
86
87 ArmyProto::~ArmyProto()
88 {
89 }
90
91 bool ArmyProto::save(XML_Helper* helper) const
92 {
93   bool retval = true;
94
95   retval &= helper->openTag(d_tag);
96
97   retval &= saveData(helper);
98
99   retval &= helper->closeTag();
100
101   return retval;
102 }
103
104 bool ArmyProto::saveData(XML_Helper* helper) const
105 {
106   bool retval = true;
107
108   retval &= ArmyProtoBase::saveData(helper);
109   retval &= helper->saveData("image_white", d_image_name[Shield::WHITE]);
110   retval &= helper->saveData("image_green", d_image_name[Shield::GREEN]);
111   retval &= helper->saveData("image_yellow", d_image_name[Shield::YELLOW]);
112   retval &= helper->saveData("image_light_blue", 
113                              d_image_name[Shield::LIGHT_BLUE]);
114   retval &= helper->saveData("image_red", d_image_name[Shield::RED]);
115   retval &= helper->saveData("image_dark_blue", 
116                              d_image_name[Shield::DARK_BLUE]);
117   retval &= helper->saveData("image_orange", d_image_name[Shield::ORANGE]);
118   retval &= helper->saveData("image_black", d_image_name[Shield::BLACK]);
119   retval &= helper->saveData("image_neutral", d_image_name[Shield::NEUTRAL]);
120   retval &= helper->saveData("awardable", d_awardable);
121   retval &= helper->saveData("defends_ruins", d_defends_ruins);
122   std::string gender_str = Hero::genderToString(Hero::Gender(d_gender));
123   retval &= helper->saveData("gender", gender_str);
124
125   return retval;
126 }
127
128 bool ArmyProto::instantiateImages(int tilesize, Shield::Colour c, std::string image_filename)
129 {
130   std::string s;
131
132   if (image_filename == "")
133     return false;
134   // load the army picture. This is done here to avoid confusion
135   // since the armies are used as prototypes as well as actual units in the
136   // game.
137   // The army image consists of two halves. On the left is the army image, 
138   // on the right the mask.
139   std::vector<PixMask*> half;
140   half = disassemble_row(image_filename, 2);
141   PixMask::scale(half[0], tilesize, tilesize);
142   PixMask::scale(half[1], tilesize, tilesize);
143
144   setImage(c, half[0]);
145   setMask(c, half[1]);
146
147   return true;
148 }
149
150 void ArmyProto::instantiateImages(Armyset *armyset)
151 {
152   for (unsigned int c = Shield::WHITE; c <= Shield::NEUTRAL; c++)
153     {
154       std::string file = "";
155       if (getImageName(Shield::Colour(c)).empty() == false)
156         file = armyset->getFile(getImageName(Shield::Colour(c)));
157       instantiateImages(armyset->getTileSize(), Shield::Colour(c), file);
158     }
159 }
160
161 void ArmyProto::uninstantiateImages()
162 {
163   for (unsigned int c = Shield::WHITE; c <= Shield::NEUTRAL; c++)
164     {
165       if (getImage(Shield::Colour(c)))
166         {
167           delete getImage(Shield::Colour(c));
168           setImage(Shield::Colour(c), NULL);
169         }
170       if (getMask(Shield::Colour(c)))
171         {
172           delete getMask(Shield::Colour(c));
173           setMask(Shield::Colour(c), NULL);
174         }
175     }
176 }
177
178 ArmyProto * ArmyProto::createScout()
179 {
180   ArmyProto *basearmy = new ArmyProto(); 
181   basearmy->setMoveBonus(Tile::FOREST | Tile::HILLS);
182   basearmy->setMaxMoves(50);
183   return basearmy;
184 }
185
186 ArmyProto * ArmyProto::createBat()
187 {
188   //oh no, it's the bat!
189   ArmyProto *basearmy = new ArmyProto(); 
190   basearmy->setMoveBonus(Tile::FOREST | Tile::HILLS | Tile::SWAMP | 
191                          Tile::WATER | Tile::MOUNTAIN);
192   basearmy->setMaxMoves(50);
193   return basearmy;
194 }