Moved Printer class into Out class
[openstranded.git] / src / kingdom.cc
blob0b77c8ffa68316fc70ce1ef2e362b7c02631a742
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"
26 #include "defparse/defparse.hh"
28 #include <iostream>
30 #ifdef unix
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <dirent.h>
34 #include <fcntl.h>
35 #include <string.h>
36 #else
37 #include <windows.h>
38 #endif
41 std::map <ID, Kingdom*> Kingdom::kingdomList;
44 Kingdom::Kingdom(const char *name):
45 name(std::string(name))
47 highestEntityID = 0;
51 Kingdom::~Kingdom ()
53 std::map <ID, Type*>::iterator type;
54 for (type = typeList.begin (); type != typeList.end (); type++)
55 destroyType (type->second);
57 std::map <ID, Entity*>::iterator entity;
58 for (entity = entityList.begin (); entity != entityList.end (); entity++)
59 destroyEntity (entity->second);
63 Entity*
64 Kingdom::getEntity (ID index)
66 if (entityExists (index))
67 return entityList[index];
68 else
69 return NULL;
73 Type*
74 Kingdom::getType (ID index)
76 if (typeExists (index))
77 return typeList[index];
78 else
79 return NULL;
83 bool
84 Kingdom::entityExists (ID entity)
86 if (entityList.find (entity) != entityList.end ())
87 return true;
88 else
89 return false;
93 bool
94 Kingdom::typeExists (ID type)
96 if (typeList.find (type) != typeList.end ())
97 return true;
98 else
99 return false;
103 Entity*
104 Kingdom::appendEntity (ID type)
106 return entityList[++highestEntityID] = createEntity (type);
110 Entity*
111 Kingdom::insertEntity (ID type, ID entity)
113 if (!entityExists (entity))
114 entityList[entity] = createEntity (type);
116 if (entity > highestEntityID)
117 highestEntityID = entity;
119 return entityList[entity];
123 Type*
124 Kingdom::insertType (const char* name, ID type)
126 if (!typeExists (type))
127 typeList[type] = createType (name);
128 return typeList[type];
132 Type*
133 Kingdom::insertType (ID type)
135 return this->insertType ("", type);
139 Entity*
140 Kingdom::createEntity (ID type)
142 return new Entity (type);
146 void
147 Kingdom::destroyEntity (Entity *entity)
149 delete entity;
153 Type*
154 Kingdom::createType (const char* name)
156 return new Type (name);
160 void
161 Kingdom::destroyType (Type *type)
163 delete type;
167 * Functions for the kingdomList
168 * TODO: In terms of beauty and modularity,
169 * these functions should be outsourced into another file
171 void
172 Kingdom::initKingdomList (std::string mod)
174 kingdomList[S2_OBJECT] = new ObjectKingdom;
175 kingdomList[S2_UNIT] = new UnitKingdom;
176 kingdomList[S2_ITEM] = new ItemKingdom;
178 // Load entity types
179 parseEntityTypes(mod, "object");
183 void
184 Kingdom::uninitKingdomList ()
186 std::map <ID, Kingdom*>::iterator iter;
187 for (iter = kingdomList.begin (); iter != kingdomList.end (); iter++)
188 delete (iter->second);
191 void
192 Kingdom::parseEntityTypes(std::string mod, std::string name)
194 std::string sysdir = std::string("mods/") + mod + "/sys/";
195 // Search for entity files within sys/
196 #ifdef __unix__
197 DIR *dir = opendir(sysdir.c_str());
198 if (!dir)
200 std::cerr << "Could not read sys/.\n";
201 std::cerr << sysdir << "\n";
202 return;
205 while (1)
207 struct dirent *dirent = readdir(dir);
208 if (!dirent) break;
209 if (strcmp(dirent->d_name, "..") && strcmp(dirent->d_name, "."))
211 std::string filename = sysdir + dirent->d_name;
212 int file = open(filename.c_str(), O_RDONLY);
213 if (!file)
215 printf("Could not open file \"%s\".\n", filename.c_str());
216 continue;
218 struct stat stat;
219 fstat(file, &stat);
220 close(file);
222 if (stat.st_mode & S_IFREG)
224 if (!strncmp(dirent->d_name, name.c_str(), strlen(name.c_str()))
225 && !strcmp(dirent->d_name + strlen(dirent->d_name) - 4, ".inf"))
227 printf("Parsing %s\n", filename.c_str());
228 def::object::parse(filename.c_str());
233 closedir(dir);
234 #else
235 std::string pattern = sysdir + name + "s_*.inf";
236 WIN32_FIND_DATA finddata;
237 HANDLE findhandle = FindFirstFile(pattern.c_str(), &finddata);
238 if (findhandle != INVALID_HANDLE_VALUE)
240 def::object::parse(finddata.cFileName);
241 while (FindNextFile(findhandle, &finddata))
243 def::object::parse(finddata.cFileName);
245 FindClose(findhandle);
247 #endif