gcsx_world.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* GCSx
  2. ** WORLD.CPP
  3. **
  4. ** World storage
  5. ** Non-editor functionality
  6. */
  7. /*****************************************************************************
  8. ** Copyright (C) 2003-2006 Janson
  9. **
  10. ** This program is free software; you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation; either version 2 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. ** This program is distributed in the hope that it will be useful,
  16. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ** GNU General Public License for more details.
  19. **
  20. ** You should have received a copy of the GNU General Public License
  21. ** along with this program; if not, write to the Free Software
  22. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  23. *****************************************************************************/
  24. #include "all.h"
  25. World::World() : title("World"), scenesById(), scenesByName(), tilesetsById(), tilesetsByName(), animgroupsById(), animgroupsByName(), scriptsById(), scriptsByNameCode(), scriptsByNameLib(), scriptsByNameNote(), globalLinks() { start_func
  26. filename = NULL;
  27. file = NULL;
  28. startingScene = 0;
  29. globalLinksDone = 0;
  30. globalLinksCount = 0;
  31. }
  32. World::World(const char* wFilename) throw_File : title(blankString), scenesById(), scenesByName(), tilesetsById(), tilesetsByName(), animgroupsById(), animgroupsByName(), scriptsById(), scriptsByNameCode(), scriptsByNameLib(), scriptsByNameNote(), globalLinks() { start_func
  33. filename = NULL;
  34. file = NULL;
  35. startingScene = 0;
  36. globalLinksDone = 0;
  37. globalLinksCount = 0;
  38. TileSet* tileset = NULL;
  39. Scene* scene = NULL;
  40. AnimGroup* animgroup = NULL;
  41. Script* script = NULL;
  42. try {
  43. filename = new string(wFilename);
  44. file = new WorldFileLoad(filename);
  45. Uint32 type;
  46. Uint32 id;
  47. // Loop pass 1- World Info, TileSets
  48. file->scanBlocks();
  49. while (file->nextBlock(&type, &id)) {
  50. switch (type) {
  51. case WorldFileLoad::BLOCKTYPE_WORLDINFO:
  52. file->claimBlock(id, this);
  53. break;
  54. case WorldFileLoad::BLOCKTYPE_TILESET:
  55. tileset = new TileSet(this);
  56. file->claimBlock(id, tileset);
  57. indexTileSet(tileset);
  58. tileset = NULL;
  59. break;
  60. }
  61. }
  62. // Loop pass 2- Animation Groups (need tilesets)
  63. file->scanBlocks();
  64. while (file->nextBlock(&type, &id)) {
  65. switch (type) {
  66. case WorldFileLoad::BLOCKTYPE_ANIMGROUP:
  67. animgroup = new AnimGroup(this);
  68. file->claimBlock(id, animgroup);
  69. indexAnimGroup(animgroup);
  70. animgroup = NULL;
  71. break;
  72. }
  73. }
  74. // Loop pass 3- Scripts (need tilesets/animgroups)
  75. file->scanBlocks();
  76. while (file->nextBlock(&type, &id)) {
  77. switch (type) {
  78. case WorldFileLoad::BLOCKTYPE_SCRIPT:
  79. script = new Script(this);
  80. file->claimBlock(id, script);
  81. indexScript(script);
  82. script = NULL;
  83. break;
  84. }
  85. }
  86. // Loop pass 4- Scenes (need tilesets/animgroups/scripts for spawns)
  87. file->scanBlocks();
  88. while (file->nextBlock(&type, &id)) {
  89. switch (type) {
  90. case WorldFileLoad::BLOCKTYPE_SCENE:
  91. scene = new Scene(this);
  92. file->claimBlock(id, scene);
  93. indexScene(scene);
  94. scene = NULL;
  95. break;
  96. }
  97. }
  98. }
  99. catch (...) {
  100. // Temporaries
  101. delete tileset;
  102. delete scene;
  103. delete animgroup;
  104. delete script;
  105. // Collections
  106. deleteCollections();
  107. // File
  108. delete filename;
  109. delete file;
  110. filename = NULL;
  111. file = NULL;
  112. throw;
  113. }
  114. }
  115. World::~World() { start_func
  116. clearGlobalLinks();
  117. // Tilesets, etc.
  118. deleteCollections();
  119. // Close world file
  120. delete file;
  121. delete filename;
  122. }
  123. void World::clearGlobalLinks() { start_func
  124. GlobalMap::iterator pos = globalLinks.begin();
  125. while (pos != globalLinks.end()) {
  126. delete[] (*pos).second.second;
  127. const char* toDel = (*pos).first;
  128. globalLinks.erase(pos);
  129. delete[] toDel;
  130. pos = globalLinks.begin();
  131. }
  132. globalLinksDone = 0;
  133. globalLinksCount = 0;
  134. }
  135. void World::deleteCollections() { start_func
  136. // Delete scenes (and layers/spawns)
  137. SceneIndex::iterator posS;
  138. SceneIndex::iterator endS = scenesById.end();
  139. for (posS = scenesById.begin(); posS != endS; ++posS) {
  140. delete (*posS).second;
  141. (*posS).second = NULL;
  142. }
  143. // Delete anim groups
  144. AnimGroupIndex::iterator posA;
  145. AnimGroupIndex::iterator endA = animgroupsById.end();
  146. for (posA = animgroupsById.begin(); posA != endA; ++posA) {
  147. delete (*posA).second;
  148. (*posA).second = NULL;
  149. }
  150. // Scenes and Anim Groups depend on tilesets
  151. // Delete tilesets
  152. TileSetIndex::iterator posT;
  153. TileSetIndex::iterator endT = tilesetsById.end();
  154. for (posT = tilesetsById.begin(); posT != endT; ++posT) {
  155. delete (*posT).second;
  156. (*posT).second = NULL;
  157. }
  158. // Many things depend on scripts
  159. // Delete scripts
  160. ScriptIndex::iterator posX;
  161. ScriptIndex::iterator endX = scriptsById.end();
  162. for (posX = scriptsById.begin(); posX != endX; ++posX) {
  163. delete (*posX).second;
  164. (*posX).second = NULL;
  165. }
  166. }
  167. void World::loadHeader(FileRead* file) throw_File { start_func
  168. if (file->getVersion() > 1) {
  169. throw FileException("Unsupported world header version %d", file->getVersion());
  170. }
  171. file->readStr(title);
  172. startingScene = file->readInt();
  173. clearGlobalLinks();
  174. globalLinksCount = file->readInt();
  175. if (globalLinksCount >= 0) {
  176. int count = globalLinksCount;
  177. set<int> posUsed;
  178. while (count--) {
  179. string name;
  180. string wart;
  181. file->readStr(name);
  182. int pos = file->readInt();
  183. file->readStr(wart);
  184. // (ensure globals aren't duplicated in name or position)
  185. if ((pos < 0) || (pos >= globalLinksCount) ||
  186. (globalLinks.find(name.c_str()) != globalLinks.end()) ||
  187. (posUsed.find(pos) != posUsed.end()))
  188. throw FileException("Corrupted global variables");
  189. // (ensure name contains a valid wart)
  190. DataType dt;
  191. const char* endWart = wartToDataType(wart.c_str(), dt);
  192. if ((!endWart) || (*endWart != 0) || (dt.baseType == DATA_VOID))
  193. throw FileException("Corrupted global variables");
  194. const char* nameC = newCpCopy(name);
  195. globalLinks[nameC].first = pos;
  196. globalLinks[nameC].second = newCpCopy(wart);
  197. posUsed.insert(pos);
  198. }
  199. globalLinksDone = 1;
  200. }
  201. else if (globalLinksCount == -1) {
  202. globalLinksCount = 0;
  203. }
  204. else {
  205. throw FileException("Corrupted world header");
  206. }
  207. }
  208. void World::loadContent(FileRead* file) { start_func
  209. // (no content; worldedit may override this)
  210. delete file;
  211. }
  212. int World::isContentCached() const { start_func
  213. // (no content; worldedit doesn't cache content)
  214. return 0;
  215. }
  216. void World::cacheLoad() { start_func
  217. // (no content; worldedit doesn't cache content)
  218. }
  219. int World::markLock() { start_func
  220. // (no content to lock)
  221. return 0;
  222. }
  223. int World::markUnlock() { start_func
  224. // (no content to lock)
  225. return 0;
  226. }
  227. int World::isLocked() const { start_func
  228. // (no content, no lock)
  229. return 0;
  230. }
  231. void World::indexScene(Scene* addScene) { start_func
  232. assert(addScene);
  233. assert(scenesByName.find(addScene->getNameL().c_str()) == scenesByName.end());
  234. assert(scenesById.find(addScene->getId()) == scenesById.end());
  235. scenesByName.insert(pair<const char*, Scene*>(addScene->getNameL().c_str(), addScene));
  236. scenesById.insert(pair<int, Scene*>(addScene->getId(), addScene));
  237. }
  238. void World::deindexScene(Scene* remScene) { start_func
  239. assert(remScene);
  240. scenesByName.erase(remScene->getNameL().c_str());
  241. scenesById.erase(remScene->getId());
  242. }
  243. Scene* World::findScene(const string& fName) const { start_func
  244. SceneMap::const_iterator loc = scenesByName.find(fName.c_str());
  245. if (loc == scenesByName.end()) return NULL;
  246. return (*loc).second;
  247. }
  248. Scene* World::findScene(int fId) const { start_func
  249. SceneIndex::const_iterator loc = scenesById.find(fId);
  250. if (loc == scenesById.end()) return NULL;
  251. return (*loc).second;
  252. }
  253. void World::indexTileSet(TileSet* addTileSet) { start_func
  254. assert(addTileSet);
  255. assert(tilesetsByName.find(addTileSet->getNameL().c_str()) == tilesetsByName.end());
  256. assert(tilesetsById.find(addTileSet->getId()) == tilesetsById.end());
  257. tilesetsByName.insert(pair<const char*, TileSet*>(addTileSet->getNameL().c_str(), addTileSet));
  258. tilesetsById.insert(pair<int, TileSet*>(addTileSet->getId(), addTileSet));
  259. }
  260. void World::deindexTileSet(TileSet* remTileSet) { start_func
  261. assert(remTileSet);
  262. tilesetsByName.erase(remTileSet->getNameL().c_str());
  263. tilesetsById.erase(remTileSet->getId());
  264. }
  265. TileSet* World::findTileSet(const string& fName) const { start_func
  266. TileSetMap::const_iterator loc = tilesetsByName.find(fName.c_str());
  267. if (loc == tilesetsByName.end()) return NULL;
  268. return (*loc).second;
  269. }
  270. TileSet* World::findTileSet(int fId) const { start_func
  271. TileSetIndex::const_iterator loc = tilesetsById.find(fId);
  272. if (loc == tilesetsById.end()) return NULL;
  273. return (*loc).second;
  274. }
  275. void World::indexAnimGroup(AnimGroup* addAnimGroup) { start_func
  276. assert(addAnimGroup);
  277. assert(animgroupsByName.find(addAnimGroup->getNameL().c_str()) == animgroupsByName.end());
  278. assert(animgroupsById.find(addAnimGroup->getId()) == animgroupsById.end());
  279. animgroupsByName.insert(pair<const char*, AnimGroup*>(addAnimGroup->getNameL().c_str(), addAnimGroup));
  280. animgroupsById.insert(pair<int, AnimGroup*>(addAnimGroup->getId(), addAnimGroup));
  281. }
  282. void World::deindexAnimGroup(AnimGroup* remAnimGroup) { start_func
  283. assert(remAnimGroup);
  284. animgroupsByName.erase(remAnimGroup->getNameL().c_str());
  285. animgroupsById.erase(remAnimGroup->getId());
  286. }
  287. AnimGroup* World::findAnimGroup(const string& fName) const { start_func
  288. AnimGroupMap::const_iterator loc = animgroupsByName.find(fName.c_str());
  289. if (loc == animgroupsByName.end()) return NULL;
  290. return (*loc).second;
  291. }
  292. AnimGroup* World::findAnimGroup(int fId) const { start_func
  293. AnimGroupIndex::const_iterator loc = animgroupsById.find(fId);
  294. if (loc == animgroupsById.end()) return NULL;
  295. return (*loc).second;
  296. }
  297. void World::indexScript(Script* addScript) { start_func
  298. assert(addScript);
  299. if (addScript->getType() == Script::SCRIPT_CODE) {
  300. assert(scriptsByNameCode.find(addScript->getNameL().c_str()) == scriptsByNameCode.end());
  301. scriptsByNameCode.insert(pair<const char*, Script*>(addScript->getNameL().c_str(), addScript));
  302. }
  303. else if (addScript->getType() == Script::SCRIPT_LIBRARY) {
  304. assert(scriptsByNameLib.find(addScript->getNameL().c_str()) == scriptsByNameLib.end());
  305. scriptsByNameLib.insert(pair<const char*, Script*>(addScript->getNameL().c_str(), addScript));
  306. }
  307. else {
  308. assert(addScript->getType() == Script::SCRIPT_NOTE);
  309. assert(scriptsByNameNote.find(addScript->getNameL().c_str()) == scriptsByNameNote.end());
  310. scriptsByNameNote.insert(pair<const char*, Script*>(addScript->getNameL().c_str(), addScript));
  311. }
  312. assert(scriptsById.find(addScript->getId()) == scriptsById.end());
  313. scriptsById.insert(pair<int, Script*>(addScript->getId(), addScript));
  314. }
  315. void World::deindexScript(Script* remScript) { start_func
  316. assert(remScript);
  317. if (remScript->getType() == Script::SCRIPT_CODE) {
  318. scriptsByNameCode.erase(remScript->getNameL().c_str());
  319. }
  320. else if (remScript->getType() == Script::SCRIPT_LIBRARY) {
  321. scriptsByNameLib.erase(remScript->getNameL().c_str());
  322. }
  323. else {
  324. assert(remScript->getType() == Script::SCRIPT_NOTE);
  325. scriptsByNameNote.erase(remScript->getNameL().c_str());
  326. }
  327. scriptsById.erase(remScript->getId());
  328. }
  329. Script* World::findScriptCode(const string& fName) const { start_func
  330. ScriptMap::const_iterator loc = scriptsByNameCode.find(fName.c_str());
  331. if (loc == scriptsByNameCode.end()) return NULL;
  332. return (*loc).second;
  333. }
  334. Script* World::findScriptLib(const string& fName) const { start_func
  335. ScriptMap::const_iterator loc = scriptsByNameLib.find(fName.c_str());
  336. if (loc == scriptsByNameLib.end()) return NULL;
  337. return (*loc).second;
  338. }
  339. Script* World::findScriptNote(const string& fName) const { start_func
  340. ScriptMap::const_iterator loc = scriptsByNameNote.find(fName.c_str());
  341. if (loc == scriptsByNameNote.end()) return NULL;
  342. return (*loc).second;
  343. }
  344. Script* World::findScript(int fId) const { start_func
  345. ScriptIndex::const_iterator loc = scriptsById.find(fId);
  346. if (loc == scriptsById.end()) return NULL;
  347. return (*loc).second;
  348. }
  349. int World::buildScripts() { start_func
  350. // Determine what to build (to prevent locking unnecessarily)
  351. vector<Script*> toBuild;
  352. ScriptIndex::iterator pos;
  353. ScriptIndex::iterator end = scriptsById.end();
  354. for (pos = scriptsById.begin(); pos != end; ++pos) {
  355. if (((*pos).second->getType() != Script::SCRIPT_NOTE) &&
  356. ((*pos).second->status() < Script::CODE_COMPILED)) {
  357. (*pos).second->markLock();
  358. toBuild.push_back((*pos).second);
  359. }
  360. }
  361. // toBuild is all locked- now parse all (anything not in toBuild
  362. // is already parsed and that parsed function list is already loaded anyway)
  363. vector<Script*>::iterator posB;
  364. vector<Script*>::iterator endB = toBuild.end();
  365. for (posB = toBuild.begin(); posB != endB; ++posB) {
  366. (*posB)->parseFuncs();
  367. }
  368. // Compile all
  369. int errors = 0;
  370. for (posB = toBuild.begin(); posB != endB; ++posB) {
  371. (*posB)->compile();
  372. errors += (*posB)->numErrors();
  373. }
  374. // Unlock all
  375. for (posB = toBuild.begin(); posB != endB; ++posB) {
  376. (*posB)->markUnlock();
  377. }
  378. // Generate global variable list
  379. if (!errors) {
  380. // only generate if 1) we compiled something or
  381. // 2) has never been generated
  382. if ((!globalLinksDone) || (!toBuild.empty()))
  383. errors = prepGlobalLinks();
  384. }
  385. return errors;
  386. }
  387. int World::prepGlobalLinks() { start_func
  388. int errors = 0;
  389. clearGlobalLinks();
  390. // Scan ALL link lists for LINK_GLOBAL(VAR)
  391. ScriptIndex::iterator pos;
  392. ScriptIndex::iterator end = scriptsById.end();
  393. for (pos = scriptsById.begin(); pos != end; ++pos) {
  394. Script* script = (*pos).second;
  395. if (script->getType() != Script::SCRIPT_NOTE) {
  396. assert(script->status() >= Script::CODE_COMPILED);
  397. assert(script->numErrors() == 0);
  398. script->collectGlobalLinks(&globalLinks, &globalLinksCount);
  399. errors += script->numErrors();
  400. assert(globalLinksCount == (int)globalLinks.size());
  401. }
  402. }
  403. if (!errors) globalLinksDone = 1;
  404. return errors;
  405. }