Merge branch 'sphase'
[Tsunagari.git] / src / world.cpp
blobfb23e8976d468cc0d9c3af958f019c26e6829e66
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** world.cpp **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <Gosu/Utility.hpp>
8 #include <libxml/tree.h>
10 #include "area.h"
11 #include "player.h"
12 #include "log.h"
13 #include "resourcer.h"
14 #include "window.h"
15 #include "world.h"
17 static World* globalWorld = NULL;
19 World* World::getWorld()
21 return globalWorld;
24 World::World(Resourcer* rc, GameWindow* wnd)
25 : rc(rc), wnd(wnd), area(NULL)
27 globalWorld = this;
30 World::~World()
32 delete area;
35 bool World::init()
37 if (!processDescriptor()) // Try to load in descriptor.
38 return false;
40 // FIXME The player entity doesn't have a descriptor yet.
41 player.reset(new Player(rc, NULL, xml.playerentity));
42 if (!player->init())
43 return false;
45 wnd->setCaption(Gosu::widen(xml.name));
46 return loadArea(xml.entry.area, xml.entry.coords);
49 void World::buttonDown(const Gosu::Button btn)
51 area->buttonDown(btn);
54 void World::draw()
56 area->draw();
59 bool World::needsRedraw() const
61 return area->needsRedraw();
64 void World::update()
66 area->update();
69 bool World::processDescriptor()
71 static const std::string descriptor = "world.conf";
72 xmlChar* str;
74 XMLDocRef doc = rc->getXMLDoc(descriptor, "dtd/world.dtd");
75 if (!doc)
76 return false;
77 const xmlNode* root = xmlDocGetRootElement(doc.get()); // <world>
78 if (!root)
79 return false;
81 xmlNode* node = root->xmlChildrenNode; // children of <world>
82 for (; node != NULL; node = node->next) {
83 if (!xmlStrncmp(node->name, BAD_CAST("name"), 5)) {
84 str = xmlNodeGetContent(node);
85 xml.name = (char*)str;
87 if (!xmlStrncmp(node->name, BAD_CAST("author"), 7)) {
88 str = xmlNodeGetContent(node);
89 xml.author = (char*)str;
91 if (!xmlStrncmp(node->name, BAD_CAST("player"), 7)) {
92 str = xmlNodeGetContent(node);
93 xml.playerentity = (char*)str;
95 if (!xmlStrncmp(node->name, BAD_CAST("type"), 5)) {
96 str = xmlGetProp(node, BAD_CAST("locality"));
98 if (xmlStrncmp(str, BAD_CAST("local"), 6))
99 xml.locality = LOCAL;
101 else if (xmlStrncmp(str, BAD_CAST("network"), 8))
102 xml.locality = NETWORK;
104 else {
105 Log::err(descriptor, "Invalid <type> value");
106 return false;
109 str = xmlGetProp(node, BAD_CAST("movement"));
111 if (xmlStrncmp(str, BAD_CAST("turn"), 5))
112 xml.movement = TURN;
114 else if (xmlStrncmp(str, BAD_CAST("tile"), 5))
115 xml.movement = TILE;
117 else if (xmlStrncmp(str, BAD_CAST("notile"), 7))
118 xml.movement = NOTILE;
120 else {
121 Log::err(descriptor, "Invalid <locality> value");
122 return false;
125 if (!xmlStrncmp(node->name, BAD_CAST("entrypoint"), 11)) {
126 str = xmlGetProp(node, BAD_CAST("area"));
127 xml.entry.area = (char*)str;
129 str = xmlGetProp(node, BAD_CAST("x"));
130 xml.entry.coords.x = atol((char*)str);
132 str = xmlGetProp(node, BAD_CAST("y"));
133 xml.entry.coords.y = atol((char*)str);
135 str = xmlGetProp(node, BAD_CAST("z"));
136 xml.entry.coords.z = atol((char*)str);
138 if (!xmlStrncmp(node->name, BAD_CAST("scripts"), 13)) {
139 node = node->xmlChildrenNode; // decend
141 if (!xmlStrncmp(node->name, BAD_CAST("script"), 7)) {
142 str = xmlNodeGetContent(node);
143 xml.scripts.push_back((char*)str);
146 return true;
150 bool World::loadArea(const std::string& areaName, coord_t playerPos)
152 Area* newArea = new Area(rc, this, player.get(), areaName);
153 delete area;
154 area = newArea;
155 if (!area->init())
156 return false;
157 player->setArea(area);
158 player->setCoordsByTile(playerPos);
159 return true;