Endless-Guide v.2
This forum as just been made please give us time while our staff members work on it we are still lookin for moderators(staffs) please email ''endless-guide@hotmail.com'' for application


The Guide for Endless-Online Players
 
HomeCalendarGalleryFAQSearchMemberlistUsergroupsRegisterLog in
Search
 
 

Display results as :
 
Rechercher Advanced Search
Latest topics
» Ruthless Aggression
Tue Oct 05, 2010 9:21 am by Microchip

» Copycat command
Tue Oct 05, 2010 9:16 am by Microchip

» #title command
Fri Sep 03, 2010 5:49 pm by michael032777

» Rev 187 character creation error
Fri Sep 03, 2010 5:38 pm by michael032777

» Modernwarfare2pwns.no-ip.org
Tue Aug 24, 2010 4:29 pm by artsyfartsy

» Want Hacks for Endless Online? Join EO-Hackers!
Fri Aug 06, 2010 7:48 am by Slymask3

» Want Hacks for Endless Online? Join EO-Hackers!
Wed Aug 04, 2010 11:21 am by Slymask3

» Want Hacks for Endless Online? Join EO-Hackers!
Wed Aug 04, 2010 11:21 am by Slymask3

» Want Hacks for Endless Online? Join EO-Hackers!
Wed Aug 04, 2010 11:21 am by Slymask3

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search
Forum
Affiliates
free forum
 

Share | 
 

 RELEASE: Pet system [tutorial]

Go down 
AuthorMessage
oxyda
Admin
avatar

Posts : 38
Join date : 2010-03-26

PostSubject: RELEASE: Pet system [tutorial]   Sun Mar 28, 2010 11:44 am

There is my tutorial how to write a pet system on EOSERV Smile 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 Razz 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.
Back to top Go down
View user profile
 
RELEASE: Pet system [tutorial]
Back to top 
Page 1 of 1
 Similar topics
-
» Haunted- Release Date
» Does anyone know why the "Pupil" system isnt working
» release dates. how do they work (for the wii)
» Chess Rating Management System
» Curl Tutorial!

Permissions in this forum:You cannot reply to topics in this forum
Endless-Guide v.2 :: Softwares :: EOSERV-
Jump to: