Example #1
0
bool Houses::Load(Game* game)
{
	if (!LoadHouseItems(game))
		return false;

	std::string file = g_config.DATA_DIR + "houses.xml";
	xmlDocPtr doc;
	doc = xmlParseFile(file.c_str());

	if (doc)
	{
		xmlNodePtr root, houseNode, tileNode;
		root = xmlDocGetRootElement(doc);
		if (xmlStrcmp(root->name, (const xmlChar*)"houses")) 
		{
			xmlFreeDoc(doc);
			return false;
		}

		houseNode = root->children;
		while (houseNode)
		{
			if (strcmp((char*) houseNode->name, "house") == 0)
			{
				std::string name = (const char*)xmlGetProp(houseNode, (const xmlChar *) "name");
				House* house = new House(name);

				if (!house->load())
				{
					xmlFreeDoc(doc);
					return false;
				}

				houses.push_back(house);
				tileNode = houseNode->children;

				while (tileNode)
				{            
					if (strcmp((const char*) tileNode->name, "tile") == 0)
					{          
						int tx = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "x"));
						int ty = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "y"));
						int tz = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "z"));	

						if (!AddHouseTile(game, house, Position(tx, ty, tz)))
						{
							xmlFreeDoc(doc);
							return false;
						}
					}
					else if (strcmp((const char*) tileNode->name, "tiles") == 0)
					{
						int fromx = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "fromx"));
						int fromy = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "fromy"));
						int fromz = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "fromz"));
						int tox = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "tox"));
						int toy = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "toy"));
						int toz = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "toz"));

						if (fromx > tox) std::swap(fromx, tox);
						if (fromy > toy) std::swap(fromy, toy);
						if (fromz > toz) std::swap(fromz, toz);

						for (int x = fromx; x <= tox; x++)
							for (int y = fromy; y <= toy; y++)
								for (int z = fromz; z <= toz; z++)
									if (!AddHouseTile(game, house, Position(x,y,z)))
									{
										xmlFreeDoc(doc);
										return false;
									}								
					}
					tileNode = tileNode->next;
				}

				Tile* doorTile = game->getTile(house->getFrontDoor());		// include front door
				if (doorTile && !doorTile->isHouse())
					doorTile->setHouse(house);
			}
			houseNode = houseNode->next;
		}
		xmlFreeDoc(doc);
		return true;
	}

	return false;
}