PDA

View Full Version : Starting with C++



Nika
04-08-08, 02:21 PM
Stepping into C++ by Mager1794

Table of Contents

1. Introduction to C++ or Introduction to Suicide
2. Writing Gossip Scripts
3. Game Object AI...
4. Artificial Intelligence <not as detailed but will update>
5. Playing with Hooks... Is it dangerous?
6. C++ Command info
7. Credits

1. Introduction to C++ or Introduction to Suicide

In this guide im gonna help you learn C++. C++ is an easy language just
everyone needs there stepping stone into it. So here is where i come in
im teaching everything that i learned on my own to step into C++ and makin
it easier for you.
Follow this tutorial to make a project http://www.mmopro.net/forums/c-modules/34-tut-how-to-compile-a-dll.html (http://www.************/forums/emulator-server-guides/133969-how-compile-dll-video.html)
dont do the DLL compiling part just get your self a project file fully set up with the Setup.h and Setup.cpp

2. Writing Gossip Scripts

Now C++ Gossip Scripts is kinda like LUA Gossip scripts only im good at the C++
ones lol
well lets start with a basic Gossip Script a simple WarpNPC
Ok well every Gossip Script must begin like this


Code:
#include "StdAfx.h"
#include "Setup.h"
#ifdef WIN32
#pragma warning(disable:4305)
#endif

class SCRIPT_DECL GlobalNPC : public GossipScript
{
public:
void GossipHello(Object * pObject, Player* Plr, bool AutoSend);
void GossipSelectOption(Object * pObject, Player* Plr, uint32 Id, uint32 IntId, const char * Code);
void GossipEnd(Object * pObject, Player* Plr);
void Destroy()
{
delete this;
}
}; notice "class SCRIPT_DECL GlobalNPC : public" creates a new class if your
using a C++ editor programm you can probably click a"-" button that hides it
every gossip script requires this code above but note the "GlobalNPC" thats the
name of it you can change that to what ever you wish as long as you change the
setup at the bottom which we will get to later
now lets add this to our script


Code:
void GlobalNPC::GossipHello(Object * pObject, Player* Plr, bool AutoSend)
{
GossipMenu *Menu;
objmgr.CreateGossipMenuForPlayer(&Menu, pObject->GetGUID(), 1, Plr);

if(AutoSend)
Menu->SendTo(Plr);
}now i know thats practically completely empty this si the part where we get to
add choices to our script lets make it look like this

Code:
void GlobalNPC::GossipHello(Object * pObject, Player* Plr, bool AutoSend)
{
GossipMenu *Menu;
objmgr.CreateGossipMenuForPlayer(&Menu, pObject->GetGUID(), 1, Plr);
Menu->AddItem(5, "Shattrath", 1);
if(AutoSend)
Menu->SendTo(Plr);
} Note how that looks think do you think you understand it good..im telling you
what it means anyway
Menu->AddItem([Put a random number between 1 and 10 this doesn't matter], "[Name of the Selection], [What case it activates when click])
Dont edit anything until we finish this section

Code:
void GlobalNPC::GossipSelectOption(Object * pObject, Player* Plr, uint32 Id, uint32 IntId, const char * Code)
{
Creature * pCreature = (pObject->GetTypeId()==TYPEID_UNIT)?((Creature*)pObject):NUL L;
if(pCreature==NULL)
return;
GossipMenu * Menu;
switch(IntId)
{
case 0:
GossipHello(pObject, Plr, true);
reak;
}
} Ok lets make it look like this

Code:

void GlobalNPC::GossipSelectOption(Object * pObject, Player* Plr, uint32 Id, uint32 IntId, const char * Code)
{
Creature * pCreature = (pObject->GetTypeId()==TYPEID_UNIT)?((Creature*)pObject):NUL L;
if(pCreature==NULL)
return;
GossipMenu * Menu;
switch(IntId)
{
case 0:
GossipHello(pObject, Plr, true);
break;
case 1: // Add some random comment after a //
Plr->
break;
}
} the Plr is basically saying "Person who clicked it"
now Add this after it [no spaces] EventTeleport(mapid, x, y, z);
now change out the (mapid, x, y, z,); with what ever you wish
or to make it shattrath do this (530, -1887.510010, 5359.379883, -12.427300);
Great now were only learning so write now what i want you to do is go out and write your own script for a warp NPC
close the NPC Script off like this

Code:
void GlobalNPC::GossipEnd(Object * pObject, Player* Plr)
{
GossipScript::GossipEnd(pObject, Plr);
}
void SetupGlobalNPC(ScriptMgr * mgr)
{
GossipScript * gs = (GossipScript*) new GlobalNPC();
mgr->register_gossip_script([NPCID], gs);
}

look at "void SetupGlobalNPC(ScriptMgr * mgr)" you should have those setup files made for this to work if not in setup.cpp
make it like this
void SetupGlobalNPC(ScriptMgr * mgr)
and setup.h like this
SetupGlobalNPC(mgr)
that ends our gossip NPC scripts chapter remember theres so much more you can
do with gossip scripts just play around with it

3. Game Object AI...

Game Object AI isn't quite as long as Gossip Script and well truly not much
more advanced
im gonna give you a full gameobject script and i want you to study it and then
i will show you how to make them
Code:

class TutorialPortal : public GameObjectAIScript
{
public:
TutorialPortal(GameObject* goinstance) : GameObjectAIScript(goinstance) {}
void OnActivate(Player * Plr)
{
Plr->SafeTeleport(1, 0, 4627.442383, -3831.523438, 943.386353, 1.220509);
}
static GameObjectAIScript *Create(GameObject * GO) { return new TutorialPortal(GO); }
};
GameObjectAIScript * create_go[GOID](GameObject * GO) { return new TutorialPortal(GO); }
void SetupPortalHandler(ScriptMgr * mgr)
{
mgr->register_gameobject_script([GOID], &TutorialPortal::Create);
} now you see where pPlayer->SafeTeleport(1, 0, 4627.442383, -3831.523438, 943.386353, 1.220509); is
simply edit that to the (1, 0, 4627.442383, -3831.523438, 943.386353, 1.220509); to your custom portals
(map, zone, x, y, z, o)
now say you want to limit things to certain people like maybe make guild house compile
you can easily make if functions like this


Code:
if(Plr->GetGUID() == 1)
{ after the "{"
simple start writing code like Plr->SafeTeleport
and stuff
lets look at the registering

Code:
void SetupPortalHandler(ScriptMgr * mgr)
{
mgr->register_gameobject_script([GOID], &TutorialPortal::Create);
} its a gameobject script this time so its not gossip scripts like the last one
as long as you do the setups right and the base of the script right then the chances of errors will be highly slim
in setup.h add this under the one in there already
SetupGlobalNPC(mgr)
do the same with setup.cpp
void SetupGlobalNPC(ScriptMgr * mgr)
and whala your done with a gameobject script

4. Artificial Intelligence

ok this section is AI i absolutely suck at this but when i get better i will fil this in with info or if someone
would like to fill it in for me just contact me on MSN at [email protected] ill put your credits and everything
ill even give a link to give you http://www.mmopro.net/forums/images/orangefox/buttons/reputation.gif

5. Playing with Hooks... Is it dangerous?

ok well server hooks are where most people get all there cool stuff like insta 70 and gold on startup and more so
im gonna give you a server hook script taht i wrote and give you every server hook there is for you to look at
[in the C++ Commands chapter]
ok lets begin

Code:
void OnLoginForFirstTime(Player* plr)
{ now that name trully has no effect whatso ever other than thats what i wil register it as
what really matters is the server hook it gets registered as lets continue make it like this

Code:
void OnLoginForFirstTime(Player* plr)
{
plr->BroadcastMessage(" Welcome to [Server name] %s", plr->GetName()
} high chance you understand that except maybe the %s. what the %s does is uses that function i added to the end
of function and gets the players name its fairly simple
now lets do the setup

Code:
void SetupFirstLogon(ScriptMgr * mgr)
{
mgr->register_hook(SERVER_HOOK_EVENT_ON_FIRST_ENTER_WOR LD, (void*)OnLoginForFirstTime);
} in setup.h add this under the one in there already
SetupFirstLogon(mgr)
do the same with setup.cpp
void SetupFirstLogon(ScriptMgr * mgr)
and whala your done with a gameobject script

6. C++ Command info


SERVER HOOKS:
SERVER_HOOK_EVENT_ON_NEW_CHARACTER
SERVER_HOOK_EVENT_ON_KILL_PLAYER
SERVER_HOOK_EVENT_ON_FIRST_ENTER_WORLD
SERVER_HOOK_EVENT_ON_ENTER_WORLD
SERVER_HOOK_EVENT_ON_GUILD_JOIN
SERVER_HOOK_EVENT_ON_DEATH
SERVER_HOOK_EVENT_ON_REPOP
SERVER_HOOK_EVENT_ON_EMOTE
SERVER_HOOK_EVENT_ON_ENTER_COMBAT
SERVER_HOOK_EVENT_ON_CAST_SPELL
SERVER_HOOK_EVENT_ON_TICK
SERVER_HOOK_EVENT_ON_LOGOUT_REQUEST
SERVER_HOOK_EVENT_ON_LOGOUT
SERVER_HOOK_EVENT_ON_QUEST_ACCEPT
SERVER_HOOK_EVENT_ON_ZONE
SERVER_HOOK_EVENT_ON_CHAT
SERVER_HOOK_EVENT_ON_LOOT
SERVER_HOOK_EVENT_ON_GUILD_CREATE
SERVER_HOOK_EVENT_ON_ENTER_WORLD_2
SERVER_HOOK_EVENT_ON_CHARACTER_CREATE
SERVER_HOOK_EVENT_ON_QUEST_CANCELLED
SERVER_HOOK_EVENT_ON_QUEST_FINISHED
SERVER_HOOK_EVENT_ON_HONORABLE_KILL
SERVER_HOOK_EVENT_ON_ARENA_FINISH
SERVER_HOOK_EVENT_ON_OBJECTLOOT
SERVER_HOOK_EVENT_ON_AREATRIGGER
SERVER_HOOK_EVENT_ON_POST_LEVELUP
SERVER_HOOK_EVENT_ON_PRE_DIE - general unit die, not only based on players
SERVER_HOOK_EVENT_ON_ADVANCE_SKILLLINE

You get the C++ info by user Plr->
most the time it brings up a box and lets you choose a command comes in handy alot

7. Final Tips

Get the SVN for Sun++ and AspireDev and study there scripts all have great quality and use alot of functions

Guide is Fully Written by me - Mager1794

All Credits go to Mager1794

mager1794
07-07-09, 01:34 AM
Thanks for posting my guide +Rep for actually giving me credit =D

but this one is kinda out dated so i'll post the most recent one lol