• Index
  •  » C++
  •  » Bed system na wlasnym ots'ie by kikiz

#1 2007-03-18 21:32:52

MrGi

Zielony

Zarejestrowany: 2007-03-05
Posty: 26
Punktów :   

Bed system na wlasnym ots'ie by kikiz

Może się komu¶ przydać :)
Ale trzeba chwilke posiedzieć nad Ľródełkami ;)

Pliki do edycji:

game.h
game.cpp
player.h
player.cpp
protocol75.cpp
otserv.cpp


No to zabieramy się do pracy :P
Otwieramy plik game.h i szukamy w nim tej linijki:
bool creatureSaySpell(Creature *creature, const std::string &text);
... i wklepujemy pod ni± to :
//----------- Bed System Begin -----------

std::string getBedSleeper(const Position pos);
unsigned int getBedID(const Position pos);
Position getBedPos(std::string name);
bool changeBed(const Position pos, unsigned int oldid, std::string sleepname);
bool loadBeds(std::string file);

//----------- Bed System End -----------
Zapisujemy zmiany i otwieramy plik game.cpp szukamy tam:
#ifdef __DEBUG_PLAYERS__
std::cout << (uint32_t)getPlayersOnline() << " players online." << std::endl;

#endif
... i zamieniamy na:
#ifdef __DEBUG_PLAYERS__
std::cout << (uint32_t)getPlayersOnline() << " players online." << std::endl;
if (p->isSleeping()){
changeBed(getBedPos(p->getName()), getBedID(getBedPos(p->getName())), "Nobody");
}
#endif

Dalej plik game.cpp znajdĽ około wiersza 4100:
bool Game::playerUseItem(Player *player, const Position& pos, const unsigned char stackpos, const unsigned short itemid, unsigned char index)
{
OTSYS_THREAD_LOCK_CLASS lockClass(gameLock, "Game::playerUseItem()");
if(player->isRemoved)
return false;
actions.UseItem(player,pos,stackpos,itemid,index);
return true;
}
I zamień na:
bool Game::playerUseItem(Player *player, const Position& pos, const unsigned char stackpos, const unsigned short itemid, unsigned char index)
{
OTSYS_THREAD_LOCK_CLASS lockClass(gameLock, "Game::playerUseItem()");

if(itemid == 2455 || itemid == 2457 || itemid == 2459 || itemid == 2461 || itemid == 2471 || itemid == 2473 /*|| itemid == 1762 || itemid == 1764 || itemid == 1766 || itemid == 1768*/){
if (changeBed(pos, itemid, player->getName())){
teleport(player, pos);
player->sendLogout();
return true;
}
else{
player->sendCancel("Sorry, not possible.");
return false;
}
}

if(player->isRemoved)
return false;
actions.UseItem(player,pos,stackpos,itemid,index);
return true;
}

Jeszcze tylko to (dodaj na samym końcu pliku):


bool Game::loadBeds(std::string file)
{
xmlDocPtr doc;
doc = xmlParseFile(file.c_str());
if (doc){
xmlNodePtr root, p, tmp;
root = xmlDocGetRootElement(doc);
if (xmlStrcmp(root->name, (const xmlChar*)"beds")) {
xmlFreeDoc(doc);
return -1;
}
tmp = root->children;
int x,y,z,id;
while(tmp){
if (strcmp((char*) tmp->name, "bed")==0){
x = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "x"));
y = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "y"));
z = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "z"));
id = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "id"));
Position mainPos(x, y, z);
Item *mainItem = Item::CreateItem(id);
Tile *mainTile = getTile(mainPos.x, mainPos.y, mainPos.z);
if (mainTile && mainItem){
Position nextPos(x, y, z);
Item *nextItem = Item::CreateItem(id+1);
if (id == 2455 || id == 2459 || id == 2463 || id == 2467 || id == 2471 || id == 2475){
nextPos.y++;
}
else if(id == 2457 || id == 2461 || id == 2465 || id == 2469 || id == 2473 || id == 2477){
nextPos.x++;
}
Tile *nextTile = getTile(nextPos.x, nextPos.y, nextPos.z);
if (nextTile && nextItem){
mainTile->addThing(mainItem);
mainItem->pos = mainPos;
nextTile->addThing(nextItem);
nextItem->pos = nextPos;
}
}
}
tmp = tmp->next;
}
xmlFreeDoc(doc);
return 0;
}
return -1;
}
std::string Game::getBedSleeper(const Position pos)
{
std::string file="data/world/beds.xml";
xmlDocPtr doc;
doc = xmlParseFile(file.c_str());
if (doc){
xmlNodePtr root, tmp;
root = xmlDocGetRootElement(doc);
if (xmlStrcmp(root->name, (const xmlChar*)"beds")) {
xmlFreeDoc(doc);
return "Nobody";
}
tmp = root->children;
while(tmp){
if (strcmp((const char*) tmp->name, "bed")==0){
int x = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "x"));
int y = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "y"));
int z = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "z"));
if (x == pos.x && y == pos.y && z == pos.z){
return (const char*)xmlGetProp(tmp, (const xmlChar *)"name");
}
}
tmp = tmp->next;
}
xmlFreeDoc(doc);
}
return "Nobody";
}

unsigned int Game::getBedID(const Position pos)
{
std::string file="data/world/beds.xml";
xmlDocPtr doc;
doc = xmlParseFile(file.c_str());
if (doc){
xmlNodePtr root, tmp;
root = xmlDocGetRootElement(doc);
if (xmlStrcmp(root->name, (const xmlChar*)"beds")) {
xmlFreeDoc(doc);
return 0;
}
tmp = root->children;
while(tmp){
if (strcmp((const char*) tmp->name, "bed")==0){
int x = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "x"));
int y = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "y"));
int z = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "z"));
if (x == pos.x && y == pos.y && z == pos.z){
return atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "id"));
}
}
tmp = tmp->next;
}
xmlFreeDoc(doc);
}
return 0;
}

bool Game::changeBed(const Position pos, unsigned int oldid, std::string sleepname)
{
Tile *maintile = getTile(pos.x,pos.y,pos.z);
Item *mainitem = dynamic_cast<Item*>(maintile->getThingByStackPos(maintile->getThingCount()-1));
if (mainitem && maintile->isPz()){
Position tilePos(pos.x, pos.y, pos.z);
if (oldid == 2455 || oldid == 2459 || oldid == 2463 || oldid == 2467 || oldid == 2471 || oldid == 2475){
tilePos.y++;
}
else if(oldid == 2457 || oldid == 2461 || oldid == 2465 || oldid == 2469 || oldid == 2473 || oldid == 2477){
tilePos.x++;
}
Tile *nexttile = getTile(tilePos.x,tilePos.y,tilePos.z);
Item *nextitem = dynamic_cast<Item*>(nexttile->getThingByStackPos(maintile->getThingCount()-1));
if (nextitem && nexttile->isPz()){
if (oldid == 2455 || oldid == 2459){
mainitem->setID(oldid+8);
}
else if(oldid == 2457){
mainitem->setID(2469);
}
else if(oldid == 2461){
mainitem->setID(2465);
}
else if(oldid == 2463 || oldid == 2467){
mainitem->setID(oldid-8);
}
else if(oldid == 2475 || oldid == 2477){
mainitem->setID(oldid-8);
}
else if(oldid == 2465){
mainitem->setID(2461);
}
else if(oldid == 2469){
mainitem->setID(2457);
}
else if(oldid == 2475){
mainitem->setID(2471);
}
else if(oldid == 2477){
mainitem->setID(2473);
}

nextitem->setID(mainitem->getID()+1);

SpectatorVec list;
SpectatorVec::iterator it;
getSpectators(Range(tilePos, true), list);
//players
for(it = list.begin(); it != list.end(); ++it) {
if(dynamic_cast<Player*>(*it)) {
(*it)->onTileUpdated(pos);
(*it)->onTileUpdated(tilePos);
}
}


//Player *player = dynamic_cast<Player*>(player);
//player->onTileUpdated(pos);
//player->onTileUpdated(tilePos);

std::string file="data/world/beds.xml";
xmlDocPtr doc;
doc = xmlParseFile(file.c_str());
if (doc){
xmlNodePtr root, tmp;
root = xmlDocGetRootElement(doc);
if (xmlStrcmp(root->name, (const xmlChar*)"beds")) {
xmlFreeDoc(doc);
return false;
}
Position bedPos[1000];// 1000 = number of beds
unsigned int id[1000];
std::string name[1000];
int i = 0;
tmp = root->children;
while(tmp){
if (strcmp((const char*) tmp->name, "bed")==0){
i++;
bedPos[i].x = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "x" ));
bedPos[i].y = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "y" ));
bedPos[i].z = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "z" ));
id[i] = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "id" ));
name[i] = (const char*) xmlGetProp(tmp, (const xmlChar*) "name");
if (bedPos[i] == pos){
id[i] = mainitem->getID();
name[i] = sleepname;
}
}
tmp = tmp->next;
}
doc = xmlNewDoc((const xmlChar*)"1.0");
doc->children = xmlNewDocNode(doc, NULL, (const xmlChar*)"beds", NULL);
root = doc->children;

std::stringstream sb;
for(int x = 1; x <= i; x++){
tmp = xmlNewNode(NULL,(const xmlChar*)"bed");
sb << bedPos[x].x; xmlSetProp(tmp, (const xmlChar*) "x" , (const xmlChar*)sb.str().c_str()); sb.str("");
sb << bedPos[x].y; xmlSetProp(tmp, (const xmlChar*) "y" , (const xmlChar*)sb.str().c_str()); sb.str("");
sb << bedPos[x].z; xmlSetProp(tmp, (const xmlChar*) "z" , (const xmlChar*)sb.str().c_str()); sb.str("");
sb << id[x]; xmlSetProp(tmp, (const xmlChar*) "id", (const xmlChar*)sb.str().c_str()); sb.str("");
sb << name[x]; xmlSetProp(tmp, (const xmlChar*) "name", (const xmlChar*)sb.str().c_str()); sb.str("");
xmlAddChild(root, tmp);
}
xmlSaveFile(file.c_str(), doc);
xmlFreeDoc(doc);
return true;
}
return false;
}
}
}
Position Game::getBedPos(std::string name)
{
std::string file="data/world/beds.xml";
xmlDocPtr doc;
doc = xmlParseFile(file.c_str());
if (doc){
xmlNodePtr root, tmp;
root = xmlDocGetRootElement(doc);
if (xmlStrcmp(root->name, (const xmlChar*)"beds")) {
xmlFreeDoc(doc);
return Position(0xFFFF,0xFFFF,0xFF);
}
tmp = root->children;
while(tmp){
if (strcmp((const char*) tmp->name, "bed")==0){
std::string sleepname = (const char*)xmlGetProp(tmp, (const xmlChar *)"name");
if (sleepname == name){
int x = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "x"));
int y = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "y"));
int z = atoi((const char*) xmlGetProp(tmp, (const xmlChar*) "z"));
return Position(x,y,z);
}
}
tmp = tmp->next;
}
xmlFreeDoc(doc);
}
return Position(0xFFFF,0xFFFF,0xFF);
}



Zapisujemy zmiany i otwieramy plik player.h do klasy player w sekcji public dodajemy:
bool isSleeping();
void sendLogout();
Otwieramy plik player.cpp szukamy linijki:
#endif //YUR_LIGHT_ITEM
... podspodem dodajemy:
void Player::sendLogout()
{
client->logout();
}

bool Player::isSleeping()
{
std::string file="data/world/beds.xml";
xmlDocPtr doc;
doc = xmlParseFile(file.c_str());
if (doc){
xmlNodePtr root, tmp;
root = xmlDocGetRootElement(doc);
if (xmlStrcmp(root->name, (const xmlChar*)"beds")) {
xmlFreeDoc(doc);
return false;
}
tmp = root->children;
while(tmp){
if (strcmp((const char*) tmp->name, "bed")==0){
std::string sleepname = (const char*)xmlGetProp(tmp, (const xmlChar *)"name");
if (sleepname == this->name){
return true;
}
}
tmp = tmp->next;
}
xmlFreeDoc(doc);
}
return false;
}


Zapisujemy zmiany i otwieramy plik protocol75.cpp szukamy:
#endif //TLM_HOUSE_SYSTEM
{
... zamieniamy na :
#endif //TLM_HOUSE_SYSTEM
{
if(ItemNum >= 2463 && ItemNum <= 2470 || ItemNum >= 2475 && ItemNum <= 2477){
Position pos(LookPos.x, LookPos.y, LookPos.z);
std::stringstream bedtext;

if (ItemNum == 2464 || ItemNum == 2468 || ItemNum == 2472 ){
pos.y--;
}
else if(ItemNum == 2466 || ItemNum == 2470 || ItemNum == 2474){
pos.x--;
}
bedtext << "You see a bed"/*<< item->getName()*/ << ".\n " << game->getBedSleeper(pos) << " is sleeping there.";
AddTextMessage(newmsg, MSG_INFO, bedtext.str().c_str());
}
if(ItemNum == 2455 || ItemNum == 2461){
Position pos(LookPos.x, LookPos.y, LookPos.z);
std::stringstream bedtext2;
bedtext2 << "You see a bed\n Nobody is sleeping there.";
AddTextMessage(newmsg, MSG_INFO, bedtext2.str().c_str());
}
if(ItemNum == 2457 || ItemNum == 2459){
Position pos(LookPos.x, LookPos.y, LookPos.z);
std::stringstream bedtext2;
bedtext2 << "You see a cot\n Nobody is sleeping there.";
AddTextMessage(newmsg, MSG_INFO, bedtext2.str().c_str());
}
if(ItemNum == 2471 || ItemNum == 2473){
Position pos(LookPos.x, LookPos.y, LookPos.z);
std::stringstream bedtext2;
bedtext2 << "You see a hammock\n Nobody is sleeping there.";
AddTextMessage(newmsg, MSG_INFO, bedtext2.str().c_str());
}
Został nam ostatni plik otserv.cpp szykamy tam :
// Call to WSA Startup on Windows Systems...
... i zamieniamy na:
// Call to WSA Startup on Windows Systems...
//load beds
std::cout << ":: Loaded Beds ... ";;
if(g_game.loadBeds("data/world/beds.xml")){;
ErrorMessage("Could not load data/world/beds.xml!");
return -1;
}
std::cout << "[done]" << std::endl;

W katalogu world dodajemy plik beds.xml:
<?xml version="1.0"?>
<beds>
<bed x="151" y="29" z="7" id="2455" name="Nobody"/>
</beds>
Gdzie x,y,z to współrzędne, id="2455" to numerek łóżka, a name="Nobody" oznacza czy kto¶ w nim ¶pi :)

Offline

 

#2 2007-03-19 12:58:37

Fox

Moderator

Zarejestrowany: 2007-02-18
Posty: 216
Punktów :   

Re: Bed system na wlasnym ots'ie by kikiz

hehe ^^ Działa? Devil zrob to ^^

Offline

 

#3 2007-03-19 17:26:37

Devil

Administrator

Zarejestrowany: 2006-12-22
Posty: 152
Punktów :   
WWW

Re: Bed system na wlasnym ots'ie by kikiz

@up
Wlasnie robie ;]
@
ale to jest pod 7.5 to niezadziala chyba

Offline

 

#4 2007-03-19 17:58:41

Fox

Moderator

Zarejestrowany: 2007-02-18
Posty: 216
Punktów :   

Re: Bed system na wlasnym ots'ie by kikiz

I wszytsko dupnelo

Offline

 
  • Index
  •  » C++
  •  » Bed system na wlasnym ots'ie by kikiz

Stopka forum

RSS
Powered by PunBB
© Copyright 2002–2008 PunBB
Polityka cookies - Wersja Lo-Fi


Darmowe Forum | Ciekawe Fora | Darmowe Fora