Eliminated some problems resulting from last merge
[openstranded.git] / src / kingdom.cc
blobbbffcdd3b5f547eaa6752a0d8c3f0be02b6fe305
1 /*
2 * See kingdom.hh for a brief description of kingdoms.
4 * Copyright (C) 2008 Hermann Walth
6 * This file is part of OpenStranded
8 * OpenStranded is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * OpenStranded is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
22 #include "kingdom.hh"
23 #include "object.hh"
24 #include "unit.hh"
25 #include "item.hh"
27 std::map <ID, Kingdom*> Kingdom::kingdomList;
30 Kingdom::Kingdom (const char *name):
31 name (std::string (name))
33 highestEntityID = 0;
37 Kingdom::~Kingdom ()
39 std::map <ID, Type*>::iterator type;
40 for (type = typeList.begin (); type != typeList.end (); type++)
41 destroyType (type->second);
43 std::map <ID, Entity*>::iterator entity;
44 for (entity = entityList.begin (); entity != entityList.end (); entity++)
45 destroyEntity (entity->second);
49 Entity*
50 Kingdom::getEntity (ID index)
52 if (entityExists (index))
53 return entityList[index];
54 else
55 return NULL;
59 Type*
60 Kingdom::getType (ID index)
62 if (typeExists (index))
63 return typeList[index];
64 else
65 return NULL;
69 bool
70 Kingdom::entityExists (ID entity)
72 if (entityList.find (entity) != entityList.end ())
73 return true;
74 else
75 return false;
79 bool
80 Kingdom::typeExists (ID type)
82 if (typeList.find (type) != typeList.end ())
83 return true;
84 else
85 return false;
89 Entity*
90 Kingdom::appendEntity (ID type)
92 return entityList[++highestEntityID] = createEntity (type);
96 Entity*
97 Kingdom::insertEntity (ID type, ID entity)
99 if (!entityExists (entity))
100 entityList[entity] = createEntity (type);
102 if (entity > highestEntityID)
103 highestEntityID = entity;
105 return entityList[entity];
109 Type*
110 Kingdom::insertType (const char* name, ID type)
112 if (!typeExists (type))
113 typeList[type] = createType (name);
114 return typeList[type];
118 Type*
119 Kingdom::insertType (ID type)
121 return this->insertType ("", type);
125 Entity*
126 Kingdom::createEntity (ID type)
128 return new Entity (type);
132 void
133 Kingdom::destroyEntity (Entity *entity)
135 delete entity;
139 Type*
140 Kingdom::createType (const char* name)
142 return new Type (name);
146 void
147 Kingdom::destroyType (Type *type)
149 delete type;
154 * Functions for the kingdomList
155 * TODO: In terms of beauty and modularity,
156 * these functions should be outsourced into another file
158 void
159 Kingdom::initKingdomList ()
161 kingdomList[S2_OBJECT] = new ObjectKingdom;
162 kingdomList[S2_UNIT] = new UnitKingdom;
163 kingdomList[S2_ITEM] = new ItemKingdom;
167 void
168 Kingdom::uninitKingdomList ()
170 std::map <ID, Kingdom*>::iterator iter;
171 for (iter = kingdomList.begin (); iter != kingdomList.end (); iter++)
172 delete (iter->second);