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/>.
27 #include "defparse/defparse.hh"
32 #include <sys/types.h>
42 std::map
<ID
, Kingdom
*> Kingdom::kingdomList
;
45 Kingdom::Kingdom(const char *name
):
46 name(std::string(name
))
54 std::map
<ID
, Type
*>::iterator type
;
55 for (type
= typeList
.begin (); type
!= typeList
.end (); type
++)
56 destroyType (type
->second
);
58 std::map
<ID
, Entity
*>::iterator entity
;
59 for (entity
= entityList
.begin (); entity
!= entityList
.end (); entity
++)
60 destroyEntity (entity
->second
);
65 Kingdom::getEntity (ID index
)
67 if (entityExists (index
))
68 return entityList
[index
];
75 Kingdom::getType (ID index
)
77 if (typeExists (index
))
78 return typeList
[index
];
85 Kingdom::entityExists (ID entity
)
87 if (entityList
.find (entity
) != entityList
.end ())
95 Kingdom::typeExists (ID type
)
97 if (typeList
.find (type
) != typeList
.end ())
105 Kingdom::appendEntity (ID type
)
107 return entityList
[++highestEntityID
] = createEntity (type
);
112 Kingdom::insertEntity (ID type
, ID entity
)
114 if (!entityExists (entity
))
115 entityList
[entity
] = createEntity (type
);
117 if (entity
> highestEntityID
)
118 highestEntityID
= entity
;
120 return entityList
[entity
];
125 Kingdom::insertType (const char* name
, ID type
)
127 if (!typeExists (type
))
128 typeList
[type
] = createType (name
);
129 return typeList
[type
];
134 Kingdom::insertType (ID type
)
136 return this->insertType ("", type
);
141 Kingdom::createEntity (ID type
)
143 return new Entity (type
);
148 Kingdom::destroyEntity (Entity
*entity
)
155 Kingdom::createType (const char* name
)
157 return new Type (name
);
162 Kingdom::destroyType (Type
*type
)
168 * Functions for the kingdomList
169 * TODO: In terms of beauty and modularity,
170 * these functions should be outsourced into another file
173 Kingdom::initKingdomList (std::string mod
)
175 kingdomList
[S2_OBJECT
] = new ObjectKingdom
;
176 kingdomList
[S2_UNIT
] = new UnitKingdom
;
177 kingdomList
[S2_ITEM
] = new ItemKingdom
;
180 parseEntityTypes(mod
, "object");
185 Kingdom::uninitKingdomList ()
187 std::map
<ID
, Kingdom
*>::iterator iter
;
188 for (iter
= kingdomList
.begin (); iter
!= kingdomList
.end (); iter
++)
189 delete (iter
->second
);
193 Kingdom::parseEntityTypes(std::string mod
, std::string name
)
195 std::string sysdir
= std::string("mods/") + mod
+ "/sys/";
196 // Search for entity files within sys/
198 DIR *dir
= opendir(sysdir
.c_str());
201 std::cerr
<< "Could not read sys/.\n";
202 std::cerr
<< sysdir
<< "\n";
208 struct dirent
*dirent
= readdir(dir
);
210 if (strcmp(dirent
->d_name
, "..") && strcmp(dirent
->d_name
, "."))
212 std::string filename
= sysdir
+ dirent
->d_name
;
213 int file
= open(filename
.c_str(), O_RDONLY
);
216 Out::msg
.printf("Could not open file \"%s\".\n", filename
.c_str());
223 if (stat
.st_mode
& S_IFREG
)
225 if (!strncmp(dirent
->d_name
, name
.c_str(), strlen(name
.c_str()))
226 && !strcmp(dirent
->d_name
+ strlen(dirent
->d_name
) - 4, ".inf"))
228 Out::msg
.printf("Parsing %s\n", filename
.c_str());
229 def::object::parse(filename
.c_str());
236 std::string pattern
= sysdir
+ name
+ "s_*.inf";
237 WIN32_FIND_DATA finddata
;
238 HANDLE findhandle
= FindFirstFile(pattern
.c_str(), &finddata
);
239 if (findhandle
!= INVALID_HANDLE_VALUE
)
241 def::object::parse(finddata
.cFileName
);
242 while (FindNextFile(findhandle
, &finddata
))
244 def::object::parse(finddata
.cFileName
);
246 FindClose(findhandle
);