bool Houses::LoadHouseItems(Game* game) { xmlDocPtr doc; doc = xmlParseFile((g_config.DATA_DIR + "houseitems.xml").c_str()); if (doc) { xmlNodePtr root, tileNode, itemNode; root = xmlDocGetRootElement(doc); if (xmlStrcmp(root->name, (const xmlChar*)"houseitems")) { xmlFreeDoc(doc); return false; } tileNode = root->children; while (tileNode) { if (strcmp((const char*)tileNode->name, "tile") == 0) { int x = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "x")); int y = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "y")); char z = atoi((const char*) xmlGetProp(tileNode, (const xmlChar*) "z")); Tile *tile = game->getTile(x, y, z); if (tile) { itemNode = tileNode->children; while (itemNode) { if (strcmp((const char*)itemNode->name, "item") == 0) { int itemid = atoi((const char*) xmlGetProp(itemNode, (const xmlChar*) "id")); Item* item = Item::CreateItem(itemid); if (item) { item->unserialize(itemNode); item->pos = Position(x, y, z); tile->addThing(item); Container *container = dynamic_cast<Container*>(item); if (container) LoadContainer(itemNode, container); } } itemNode = itemNode->next; } } } tileNode = tileNode->next; } xmlFreeDoc(doc); return true; } return false; }
bool Map::placeCreature(Position &pos, Creature* c) { Tile* tile = getTile(pos.x, pos.y, pos.z); bool success = tile && c->canMovedTo(tile); //bool success = tile!=NULL; if(!success) { for(int cx =pos.x - 1; cx <= pos.x + 1 && !success; cx++) { for(int cy = pos.y - 1; cy <= pos.y + 1 && !success; cy++){ #ifdef __DEBUG__ std::cout << "search pos x: " << cx <<" y: "<< cy << std::endl; #endif tile = getTile(cx, cy, pos.z); success = tile && c->canMovedTo(tile); if(success) { pos.x = cx; pos.y = cy; } } } if(!success){ Player *player = dynamic_cast<Player*>(c); if(player) { pos.x = c->masterPos.x; pos.y = c->masterPos.y; pos.z = c->masterPos.z; tile = getTile(pos.x, pos.y, pos.z); success = tile && player->canMovedTo(tile); } } } if(!success || !tile) { #ifdef __DEBUG__ std::cout << "Failed to place creature onto map!" << std::endl; #endif return false; } std::cout << "POS: " << c->pos << std::endl; tile->addThing(c); c->pos = pos; return true; }