EOSERV Forum > EOSERV > Pet system [tutorial] (outdated)
Topic is locked.
Page: << 1 2 3 ... 7 8 >>
Pet system [tutorial] (outdated)
Author Message
Post #33019 Pet system [tutorial] (outdated)

~EDIT 2: You shouldn't use it since this code is outdated!


There is my tutorial how to write a pet system on EOSERV :) I don't think it's something special.


1. NPC class.

First we have to add some important variables to NPC class:



(npc.hpp)

public:

bool pet;
Character *owner;
bool attack_command;



Now remake class constructor to support new NPC type:



Declaration (npc.hpp):

NPC(Map *map, short id, unsigned char x, unsigned char y, unsigned char spawn_type, short spawn_time, unsigned char index, bool temporary = false, bool pet = false);


Implementation (npc.cpp):

NPC::NPC(Map *map, short id, unsigned char x, unsigned char y, unsigned char spawn_type, short spawn_time, unsigned char index, bool temporary, bool pet)
{
this->map = map;
this->temporary = temporary;
this->index = index;
this->id = id;
this->spawn_x = this->x = x;
this->spawn_y = this->y = y;
this->alive = false;
this->attack = false;
this->totaldamage = 0;

if (spawn_type > 7)
{
spawn_type = 7;
}

this->spawn_type = spawn_type;
this->spawn_time = spawn_time;
this->walk_idle_for = 0;

if (spawn_type == 7)
{
this->direction = static_cast<Direction>(spawn_time & 0x03);
this->spawn_time = 0;
}
else
{
this->direction = DIRECTION_DOWN;
}

this->parent = 0;

this->pet = pet;
this->attack_command = !pet;

  if(!pet) // don't load shop and drop data if it's pet
this->LoadShopDrop();
}



..and now we need to remake NPC::Act() function to handle pet actions:



Too much code :P Get it HERE.



This function will let you set new owner for pet:



void NPC::SetOwner(Character *character)
{
this->owner = character;
}



2. Pet transfer between maps.

Add some variables to Character class that we will need:



public:
bool has_pet;
bool pet_transfer;

NPC *pet;



At class constructor add:



this->pet = 0;
this->has_pet = false;
this->pet_transfer = false;



Now add fucntion that will transfer pet:



Declaration (character.hpp):

(as public) void PetTransfer();


Implementation: (character.cpp):

void Character::PetTransfer()
{
if(this->has_pet && !this->pet_transfer)
{
UTIL_PTR_LIST_FOREACH(this->map->characters, Character, character)
{
if (this->InRange(*character))
{
this->pet->RemoveFromView(*character);
}
}
erase_first(this->map->npcs, this->pet);
this->pet->Release();
this->has_pet = false;

this->pet_transfer = true;
}
else if(this->pet_transfer)
{
if(pet_transfer)
{
unsigned char index = this->map->GenerateNPCIndex();
if (index > 250)
{
return;
}
this->pet = new NPC(this->map, this->pet->id, this->x, this->y, 1, 1, index, true, true);
this->pet->SetOwner(this);
this->map->npcs.push_back(this->pet);
this->pet->Spawn();
this->has_pet = true;
this->pet_transfer = false;
}
}
}



Go to map.cpp, find Map::Walk(Character *from, Direction direction, bool admin) function and find map warp code, you have to call Character::PetTransfer() here:



Map_Warp *warp;
if (!admin && (warp = this->GetWarp(target_x, target_y)))
{
if (from->level >= warp->levelreq && (warp->spec == Map_Warp::NoDoor || warp->open))
{
from->PetTransfer();
from->Warp(warp->map, warp->x, warp->y);
from->PetTransfer();
}

return false;
}



3. Pet initialization

Now we need to make safe pet initialization on player login, go to handlers/Welcome.cpp, find case PACKET_MSG and after client state is set to Playing add code:



unsigned char index = this->player->character->map->GenerateNPCIndex();
if (index > 250)
{
return false;
}
this->player->character->pet = new NPC(this->player->character->map, 1, this->player->character->x, this->player->character->y, 1, 1, index, false, true);
this->player->character->pet->Die();
this->player->character->pet->Release();



4. Player commands

There are 2 example commands for player to controll pet:

#attack - set pet to attack closest player in range.

#follow - if pet is attacking someone, it will make it following you.



else if (command.length() >= 6 && command.compare(0,6,"follow") == 0 && this->player->character->has_pet)
{
this->player->character->pet->attack_command = false;
}
else if (command.length() >= 6 && command.compare(0,6,"attack") == 0 && this->player->character->has_pet)
{
this->player->character->pet->attack_command = true;
}



5. (finally) Spawn a pet

There are admin commands for spawn and delete pets from players:



else if (command.length() >= 2 && command.compare(0,2,"sp") == 0 && arguments.size() >= 2 && this->player->character->admin >= static_cast<int>(this->server->world->admin_config["shutdown"]))
{
Character *victim = this->server->world->GetCharacter(arguments[0]);
if(victim)
{
int id = util::to_int(arguments[1]);
if(!victim->has_pet)
{
unsigned char index = victim->map->GenerateNPCIndex();
if (index > 250)
{
return false;
}
victim->pet = new NPC(victim->map, id, victim->x, victim->y, 1, 1, index, true, true);
victim->pet->SetOwner(victim);
victim->map->npcs.push_back(victim->pet);
victim->pet->Spawn();
victim->has_pet = true;
}
}
}
else if (command.length() >= 2 && command.compare(0,2,"dp") == 0 && arguments.size() >= 1 && this->player->character->admin >= static_cast<int>(this->server->world->admin_config["shutdown"]))
{
Character *victim = this->server->world->GetCharacter(arguments[0]);
if(victim)
{
UTIL_PTR_LIST_FOREACH(victim->map->characters, Character, character)
{
if (victim->InRange(*character))
{
victim->pet->RemoveFromView(*character);
}
}
erase_first(victim->map->npcs, victim->pet);
victim->pet->Release();
victim->has_pet = false;
}
}



6. Example codes

You can also make your pet immortal when it follows you, and able to die when it fight with some player, you have to add that statement in NPC::Damage() function:



if(from != this->owner && this->attack_command)
{

      //rest of code

}



That's all, have fun guys ;D Leave comment how it works. If i forgot something or did something wrong, let me know it here.


~EDIT 1: Tutorial updated, added SetOwner() function code.

14 years, 11 weeks ago
Post #33020 Re: RELEASE: Pet system [tutorial]

Looks pretty rock solid to me.

14 years, 11 weeks ago
Post #33021 Re: RELEASE: Pet system [tutorial]

I have just tested this code again and everything works for me :P

14 years, 11 weeks ago
Post #33022 Re: RELEASE: Pet system [tutorial]

In what file do i have to put that?



public:
bool has_pet;
bool pet_transfer;

NPC *pet;



At class constructor add:



this->pet = 0;
this->has_pet = false;
this->pet_transfer = false;


14 years, 11 weeks ago
Post #33023 Re: RELEASE: Pet system [tutorial]
EpicAngel posted: (21st Feb 2010 03:22 pm)

In what file do i have to put that?



public:
bool has_pet;
bool pet_transfer;

NPC *pet;



At class constructor add:



this->pet = 0;
this->has_pet = false;
this->pet_transfer = false;


I guess here
At class constructor add:

14 years, 11 weeks ago
Post #33024 Re: RELEASE: Pet system [tutorial]

Yeah but where IS class constructor?

14 years, 11 weeks ago
Post #33026 Re: RELEASE: Pet system [tutorial]
EpicAngel posted: (21st Feb 2010 04:05 pm)

Yeah but where IS class constructor?


God damn I can't figure it out either.
---
Web developer, currently looking for graphic artists / designers.
14 years, 11 weeks ago
Post #33027 Re: RELEASE: Pet system [tutorial]

i am working on this with epicangel but where is class constructor plz be more specific

---
Live and let live. Hurt but do not kill. Finish what you start. NEVER GIVE UP!!!
14 years, 11 weeks ago
Post #33039 Re: RELEASE: Pet system [tutorial]

Character::Character()

14 years, 11 weeks ago
Post #33040 Re: RELEASE: Pet system [tutorial]

Seriously i cant find the class constructor ffs..


14 years, 11 weeks ago
Post #33041 Re: RELEASE: Pet system [tutorial]

Really ? How you want to write it if you don't know what's class constructor ?

14 years, 11 weeks ago
Post #33042 Re: RELEASE: Pet system [tutorial]

Well everything seems to be fine for me, but some 1 single error you might can help me out:


C:\(path)\src\player.hpp|1341|error: 'class NPC' has no member named 'SetOwner'|


How do i fix this :)

14 years, 11 weeks ago
Post #33043 Re: RELEASE: Pet system [tutorial]
Jimmyee posted: (21st Feb 2010 07:52 pm)

Really ? How you want to write it if you don't know what's class constructor ?


If you just tell me wtf it is i can finish it alright?
14 years, 11 weeks ago
Post #33044 Re: RELEASE: Pet system [tutorial]

Look in the character.cpp There you should see lines with

this->

entrys.

There you add

this->pet = 0;
this->has_pet = false;
this->pet_transfer = false;

14 years, 11 weeks ago
Post #33045 Re: RELEASE: Pet system [tutorial]

I got:

..\player.hpp||In member function 'void Character::PetTransfer()':|
..\player.hpp|1441|error: no matching function for call to 'NPC::NPC(Map*&, short int&, unsigned char&, unsigned char&, int, int, unsigned char&, bool, bool)'|
..\player.hpp|1442|error: 'class NPC' has no member named 'SetOwner'|
||=== Build finished: 2 errors, 0 warnings ===|

---
Web developer, currently looking for graphic artists / designers.
14 years, 11 weeks ago
Page: << 1 2 3 ... 7 8 >>
Topic is locked.
EOSERV Forum > EOSERV > Pet system [tutorial] (outdated)