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/>.
26 #include "defparse/defparse.hh"
31 #include <sys/types.h>
41 std::map
<ID
, Kingdom
*> Kingdom::kingdomList
;
44 Kingdom::Kingdom(const char *name
):
45 name(std::string(name
))
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
);
64 Kingdom::getEntity (ID index
)
66 if (entityExists (index
))
67 return entityList
[index
];
74 Kingdom::getType (ID index
)
76 if (typeExists (index
))
77 return typeList
[index
];
84 Kingdom::entityExists (ID entity
)
86 if (entityList
.find (entity
) != entityList
.end ())
94 Kingdom::typeExists (ID type
)
96 if (typeList
.find (type
) != typeList
.end ())
104 Kingdom::appendEntity (ID type
)
106 return entityList
[++highestEntityID
] = createEntity (type
);
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
];
124 Kingdom::insertType (const char* name
, ID type
)
126 if (!typeExists (type
))
127 typeList
[type
] = createType (name
);
128 return typeList
[type
];
133 Kingdom::insertType (ID type
)
135 return this->insertType ("", type
);
140 Kingdom::createEntity (ID type
)
142 return new Entity (type
);
147 Kingdom::destroyEntity (Entity
*entity
)
154 Kingdom::createType (const char* name
)
156 return new Type (name
);
161 Kingdom::destroyType (Type
*type
)
167 * Functions for the kingdomList
168 * TODO: In terms of beauty and modularity,
169 * these functions should be outsourced into another file
172 Kingdom::initKingdomList (std::string mod
)
174 kingdomList
[S2_OBJECT
] = new ObjectKingdom
;
175 kingdomList
[S2_UNIT
] = new UnitKingdom
;
176 kingdomList
[S2_ITEM
] = new ItemKingdom
;
179 parseEntityTypes(mod
, "object");
184 Kingdom::uninitKingdomList ()
186 std::map
<ID
, Kingdom
*>::iterator iter
;
187 for (iter
= kingdomList
.begin (); iter
!= kingdomList
.end (); iter
++)
188 delete (iter
->second
);
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/
197 DIR *dir
= opendir(sysdir
.c_str());
200 std::cerr
<< "Could not read sys/.\n";
201 std::cerr
<< sysdir
<< "\n";
207 struct dirent
*dirent
= readdir(dir
);
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
);
215 printf("Could not open file \"%s\".\n", filename
.c_str());
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());
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
);