1 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
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.
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.
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
18 #include <sigc++/functors/mem_fun.h>
23 #include "xmlhelper.h"
25 std::string Roadlist::d_tag = "roadlist";
26 //#define debug(x) {cerr<<__FILE__<<": "<<__LINE__<<": "<<x<<endl<<flush;}
29 Roadlist* Roadlist::s_instance=0;
31 Roadlist* Roadlist::getInstance()
34 s_instance = new Roadlist();
39 Roadlist* Roadlist::getInstance(XML_Helper* helper)
44 s_instance = new Roadlist(helper);
48 void Roadlist::deleteInstance()
60 Roadlist::Roadlist(XML_Helper* helper)
62 helper->registerTag(Road::d_tag, sigc::mem_fun(this, &Roadlist::load));
65 bool Roadlist::save(XML_Helper* helper) const
69 retval &= helper->openTag(Roadlist::d_tag);
71 for (const_iterator it = begin(); it != end(); it++)
72 retval &= (*it)->save(helper);
74 retval &= helper->closeTag();
79 bool Roadlist::load(std::string tag, XML_Helper* helper)
81 if (tag != Road::d_tag)
85 add(new Road(helper));
90 int Roadlist::calculateType (Vector<int> t) const
92 // examine neighbour tiles to discover whether there's a road on them
94 bool b = false; //bottom
95 bool l = false; //left
96 bool r = false; //right
99 u = getObjectAt(t + Vector<int>(0, -1));
100 if (t.y < GameMap::getHeight() - 1)
101 b = getObjectAt(t + Vector<int>(0, 1));
103 l = getObjectAt(t + Vector<int>(-1, 0));
104 if (t.x < GameMap::getWidth() - 1)
105 r = getObjectAt(t + Vector<int>(1, 0));
107 // then translate this to the type
109 //show road type 2 when no other road tiles are around
110 if (!u && !b && !l && !r)
112 else if (u && b && l && r)
114 else if (!u && b && l && r)
116 else if (u && !b && l && r)
118 else if (u && b && !l && r)
120 else if (u && b && l && !r)
122 else if (u && b && !l && !r)
124 else if (!u && !b && l && r)
126 else if (u && !b && l && !r)
128 else if (u && !b && !l && r)
130 else if (!u && b && l && !r)
132 else if (!u && b && !l && r)
134 else if (u && !b && !l && !r)
135 type = Road::CONNECTS_NORTH;
136 else if (!u && b && !l && !r)
137 type = Road::CONNECTS_SOUTH;
138 else if (!u && !b && l && !r)
139 type = Road::CONNECTS_WEST;
140 else if (!u && !b && !l && r)
141 type = Road::CONNECTS_EAST;