To learn something about the TrinityCore scripting system and c++ i wrote a npc to change faction/race or make a customize of a character.
The gossip menu has following options:
How many Faction Changes are possible?
How many Customizes are possible?
How many Race Changes are possible?
I want to change my faction!
I want to customize my character!
I want to change my race!
The count of possible race/faction changes or customizes takes the npc from the account database.
[CODE]#include “ScriptPCH.h”
class changer : public CreatureScript
{
public:
changer()
: CreatureScript("changer")
{
}
void Options(Player* pPlayer, Creature* pCreature, int type , int count, int cost)
{
QueryResult resulta;
QueryResult resultb;
QueryResult resultc;
QueryResult resultd;
resulta = LoginDatabase.PQuery("SELECT faction FROM account WHERE id = '%u'", pPlayer->GetSession()->GetAccountId());
char str_fac[200];
resultb = LoginDatabase.PQuery("SELECT customize FROM account WHERE id = '%u'", pPlayer->GetSession()->GetAccountId());
char str_custom[200];
resultc = LoginDatabase.PQuery("SELECT race FROM account WHERE id = '%u'", pPlayer->GetSession()->GetAccountId());
char str_race[200];
resultd = CharacterDatabase.PQuery("SELECT at_login FROM characters WHERE guid = '%u'", pPlayer->GetGUIDLow());
char str_atlogin[200];
Field *fielda = resulta->Fetch();
uint32 faction = fielda[0].GetUInt32();
Field *fieldb = resultb->Fetch();
uint32 custom = fieldb[0].GetUInt32();
Field *fieldc = resultc->Fetch();
uint32 race = fieldc[0].GetUInt32();
Field *fieldd = resultd->Fetch();
uint32 atlogin = fieldd[0].GetUInt32();
if (!resulta || !resultb || !resultc || !resultd)
{
sprintf(str_fac,"ERROR!! Contact an admin!");
pPlayer->PlayerTalkClass->ClearMenus();
OnGossipHello(pPlayer, pCreature);
pCreature->MonsterSay(str_fac, LANG_UNIVERSAL, pPlayer->GetGUID());
return;
}
else if (type == 1)
{
if (faction < cost)
{
sprintf(str_fac, "No Faction Changes possible!");
pPlayer->MonsterWhisper(str_fac,pPlayer->GetGUID(),true);
}
else if(atlogin == 0)
{
LoginDatabase.PQuery("UPDATE account SET faction = faction - '%u' WHERE id = '%u'", cost, pPlayer->GetSession()->GetAccountId());
CharacterDatabase.PExecute("UPDATE characters SET at_login = '64' WHERE guid = '%u'", pPlayer->GetGUIDLow());
pPlayer->SetAtLoginFlag(AT_LOGIN_CHANGE_FACTION);
sprintf(str_fac,"Faction Change successful! Please logout!");
pPlayer->MonsterWhisper(str_fac,pPlayer->GetGUID(),true);
}
else
{
sprintf(str_fac,"On this character is a faction change or a similar thing already active!!");
pPlayer->MonsterWhisper(str_fac,pPlayer->GetGUID(),true);
}
}
else if (type == 2)
{
if (custom < cost)
{
sprintf(str_custom, "No character customize possible!");
pPlayer->MonsterWhisper(str_custom,pPlayer->GetGUID(),true);
}
else if(atlogin == 0)
{
LoginDatabase.PQuery("UPDATE account SET customize = customize - '%u' WHERE id = '%u'", cost, pPlayer->GetSession()->GetAccountId());
CharacterDatabase.PExecute("UPDATE characters SET at_login = '128' WHERE guid = '%u'", pPlayer->GetGUIDLow());
pPlayer->SetAtLoginFlag(AT_LOGIN_CUSTOMIZE);
sprintf(str_custom,"Customize successful! Please logout!");
pPlayer->MonsterWhisper(str_custom,pPlayer->GetGUID(),true);
}
else
{
sprintf(str_custom,"On this character is a faction change or a similar thing already active!!");
pPlayer->MonsterWhisper(str_custom,pPlayer->GetGUID(),true);
}
}
else if (type == 3)
{
if (race < cost)
{
sprintf(str_race, "You cant get race changes");
pPlayer->MonsterWhisper(str_race,pPlayer->GetGUID(),true);
}
else if(atlogin == 0)
{
LoginDatabase.PQuery("UPDATE account SET race = race - '%u' WHERE id = '%u'", cost, pPlayer->GetSession()->GetAccountId());
CharacterDatabase.PExecute("UPDATE characters SET at_login = '8' WHERE guid = '%u'", pPlayer->GetGUIDLow());
pPlayer->SetAtLoginFlag(AT_LOGIN_CHANGE_RACE);
sprintf(str_race,"Race Change successful! Please logout!");
pPlayer->MonsterWhisper(str_race,pPlayer->GetGUID(),true);
}
else
{
sprintf(str_race,"On this character is a faction change or a similar thing already active!!");
pPlayer->MonsterWhisper(str_race,pPlayer->GetGUID(),true);
}
}
if (type == 10)
{
sprintf(str_fac,"%u Faction Changes possible", faction);
pPlayer->MonsterWhisper(str_fac,pPlayer->GetGUID(),true);
}
if (type == 11)
{
sprintf(str_custom,"%u Customizes possible", custom);
pPlayer->MonsterWhisper(str_custom,pPlayer->GetGUID(),true);
}
if (type == 12)
{
sprintf(str_race,"%u Race Changes possible", race);
pPlayer->MonsterWhisper(str_race,pPlayer->GetGUID(),true);
}
pPlayer->PlayerTalkClass->ClearMenus();
OnGossipHello(pPlayer, pCreature);
}
bool OnGossipHello(Player* pPlayer, Creature* pCreature)
{
pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "How many Faction Changes are possible?", GOSSIP_SENDER_MAIN, 1000);
pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "How many Customizes are possible?", GOSSIP_SENDER_MAIN, 1001);
pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "How many Race Changes are possible?", GOSSIP_SENDER_MAIN, 1002);
pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to change my faction!", GOSSIP_SENDER_MAIN, 2001);
pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to customize my character!", GOSSIP_SENDER_MAIN, 2002);
pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "I want to change my race!", GOSSIP_SENDER_MAIN, 2003);
pPlayer->PlayerTalkClass->SendGossipMenu(907, pCreature->GetGUID());
return true;
}
bool OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 , uint32 uiAction)
{
pPlayer->PlayerTalkClass->ClearMenus();
switch (uiAction)
{
case 1000:
Options(pPlayer, pCreature, 10, 0, 0);
break;
case 1001:
Options(pPlayer, pCreature, 11, 0, 0);
break;
case 1002:
Options(pPlayer, pCreature, 12, 0, 0);
break;
case 2001:
Options(pPlayer, pCreature,1,0,1);
break;
case 2002:
Options(pPlayer, pCreature,2,0,1);
break;
case 2003:
Options(pPlayer, pCreature,3,0,1);
break;
}
return true;
}
};
void AddSC_changer()
{
new changer();
}[/CODE]
SQL:
[CODE]ALTER TABLE account
ADD COLUMN faction
int(11) UNSIGNED default ‘5’ AFTER expansion,
ADD COLUMN customize
int(11) UNSIGNED default ‘5’ AFTER faction,
ADD COLUMN race
int(11) UNSIGNED default ‘5’ AFTER customize;
INSERT INTO creature_template (entry, modelid1, name, npcflag, faction_a, faction_h, ScriptName) VALUES (143440, 29442, ‘Changer’, 1, 35, 35, ‘changer’);[/CODE]
i hope you enjoy my little script and please excuse my bad english /emoticons/default_biggrin.png