initial commit, lordsawar source, slightly modified
[lordsawar] / src / bridge.cpp
1 //  Copyright (C) 2007, 2008 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 "bridge.h"
19 #include "GameMap.h"
20
21 std::string Bridge::d_tag = "bridge";
22
23 Bridge::Bridge(Vector<int> pos, int type)
24   :Location(pos), d_type(type)
25 {
26     //mark the location on the game map as occupied by a bridge
27     GameMap::getInstance()->getTile(getPos())->setBuilding(Maptile::BRIDGE);
28 }
29
30 Bridge::Bridge(XML_Helper* helper)
31     :Location(helper)
32 {
33   std::string type_str;
34   helper->getData(type_str, "type");
35   d_type = bridgeTypeFromString(type_str);
36     
37   //mark the location on the game map as occupied by a bridge
38   GameMap::getInstance()->getTile(getPos())->setBuilding(Maptile::BRIDGE);
39 }
40
41 Bridge::Bridge(const Bridge& s)
42   :Location(s), d_type(s.d_type)
43 {
44 }
45
46 Bridge::Bridge(const Bridge& s, Vector<int> pos)
47   :Location(s, pos), d_type(s.d_type)
48 {
49 }
50
51 Bridge::~Bridge()
52 {
53 }
54
55 bool Bridge::save(XML_Helper* helper) const
56 {
57     bool retval = true;
58
59     retval &= helper->openTag(Bridge::d_tag);
60     retval &= helper->saveData("id", d_id);
61     retval &= helper->saveData("x", getPos().x);
62     retval &= helper->saveData("y", getPos().y);
63     std::string type_str = bridgeTypeToString(Bridge::Type(d_type));
64     retval &= helper->saveData("type", type_str);
65     retval &= helper->closeTag();
66     
67     return retval;
68 }
69
70 std::string Bridge::bridgeTypeToString(const Bridge::Type type)
71 {
72   switch (type)
73     {
74     case Bridge::CONNECTS_TO_EAST:
75       return "Bridge::CONNECTS_TO_EAST";
76     case Bridge::CONNECTS_TO_NORTH:
77       return "Bridge::CONNECTS_TO_NORTH";
78     case Bridge::CONNECTS_TO_WEST:
79       return "Bridge::CONNECTS_TO_WEST";
80     case Bridge::CONNECTS_TO_SOUTH:
81       return "Bridge::CONNECTS_TO_SOUTH";
82     }
83   return "Bridge::CONNECTS_TO_EAST";
84 }
85
86 Bridge::Type Bridge::bridgeTypeFromString(const std::string str)
87 {
88   if (str.size() > 0 && isdigit(str.c_str()[0]))
89     return Bridge::Type(atoi(str.c_str()));
90   if (str == "Bridge::CONNECTS_TO_EAST")
91     return Bridge::CONNECTS_TO_EAST;
92   else if (str == "Bridge::CONNECTS_TO_NORTH")
93     return Bridge::CONNECTS_TO_NORTH;
94   else if (str == "Bridge::CONNECTS_TO_WEST")
95     return Bridge::CONNECTS_TO_WEST;
96   else if (str == "Bridge::CONNECTS_TO_SOUTH")
97     return Bridge::CONNECTS_TO_SOUTH;
98   return Bridge::CONNECTS_TO_EAST;
99 }