CodeBox Npc
This script was inspired by stoneharry's CodeBox Npc script he did in lua, i saw that he had like 6 possible options that you could use, and i felt sometimes its just not enough, and to add more to the lua you'd be doing alot of recoding of it.
so i wrote this script
Code:
#include "StdAfx.h"
#include "Setup.h"
#ifdef WIN32
#pragma warning(disable:4305)
#endif
#define NPC 55555
/*
##############################
#######Fully Aware This ######
#######Code isn't very ######
#######Efficient, but it######
#######was a shot ######
##############################
##############################
##########Created By##########
##########Mager1794 ##########
##############################
*/
class SCRIPT_DECL CodeBox : 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;
}
struct CodeBoxes;
};
struct CodeBox::CodeBoxes
{
const char * OptionText; //String for gossip to display
string EntryCode; //Code to enter for reward
uint32 ItemId; //Item Id for reward
};
void CodeBox::GossipHello(Object * pObject, Player* Plr, bool AutoSend)
{
GossipMenu *Menu;
Menu->AddItem(10, "I would like to enter a code for a reward", 2, 1);
if(AutoSend)
Menu->SendTo(Plr);
}
void CodeBox::GossipSelectOption(Object * pObject, Player* Plr, uint32 Id, uint32 IntId, const char * Code)
{
//Define your code box information
///How many code boxes you want
//CodeBoxes CodeBox[2];//must be equal to int codes below
CodeBoxes CodeBox[2];
int codes = 2;//must be equal to integer inside brackets
//Define Code Options 1- ect.
CodeBox[1].OptionText = "Test1";
CodeBox[1].EntryCode = "1Test";
CodeBox[1].ItemId = 22253;
CodeBox[2].OptionText = "Test2";
CodeBox[2].EntryCode = "2Test";
CodeBox[2].ItemId = 4325;
bool AutoSend = true;
GossipMenu *Menu;
switch(IntId)
{
case 0:
GossipHello(pObject, Plr, true);
break;
case 1:
{
for(int i=1;i<codes;i++)
{
if(Code == CodeBox[i].EntryCode)
{
Item * item = objmgr.CreateItem(CodeBox[i].ItemId, Plr);
Plr->GetItemInterface()->AddItemToFreeSlot(item);
}
}
}break;
case 2:
{
for(int i=0;i<codes;i++)
{
Menu->AddItem(10, CodeBox[i].OptionText, i, 1);
}
}break;
}
}
void CodeBox::GossipEnd(Object * pObject, Player* Plr)
{
GossipScript::GossipEnd(pObject, Plr);
}
void SetupCode(ScriptMgr * mgr)
{
GossipScript * gs = (GossipScript*) new CodeBox();
mgr->register_gossip_script(NPC, gs);
}
How to Use
first off just use the find method and search for //Define Code Options 1- ect.
you'll see
Code:
//Define Code Options 1- ect.
CodeBox[1].OptionText = "Test1";
CodeBox[1].EntryCode = "1Test";
CodeBox[1].ItemId = 22253;
CodeBox[2].OptionText = "Test2";
CodeBox[2].EntryCode = "2Test";
CodeBox[2].ItemId = 4325;
now its simple to use how ever many Options you want on the gossip NPC just edit the number 2 for Code[2], to what ever number you choose
and edit "codes = 2" to what ever number you choose.
then just edit "Test1", to what ever and the "Test2"
if you have more than 2, just hit Enter and type this
Code[Number].OptionText = "MyMessage";
also you have to edit the other values
EntryCode = "The string they type in code box to gain reward"
ItemId = the Id of the Item used as reward
Database Version
basically the same exact script only instead of using a structure i connected it to the database instead, enjoy
A gm can make a 14 character long code to and then type it into the code box, and it will insert it into the database. The GM can then give the Player who earned the code, the code and he will enter it in, and it will seach database for the code, if not found then it doesn't work....
this may be bugged i'm not sure but it might give all rewards.......
C++ Script
Code:
#include "StdAfx.h"
#include "Setup.h"
#ifdef WIN32
#pragma warning(disable:4305)
#endif
#define NPC 55555
/*
##############################
#######Fully Aware This ######
#######Code isn't very ######
#######Efficient, but it######
#######was a shot ######
##############################
##############################
##########Created By##########
##########Mager1794 ##########
##############################
*/
class SCRIPT_DECL CodeBox : 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;
}
struct CodeBoxes;
};
struct CodeBox::CodeBoxes
{
const char * OptionText; //String for gossip to display
uint32 ItemId; //Item Id for reward
};
void CodeBox::GossipHello(Object * pObject, Player* Plr, bool AutoSend)
{
GossipMenu *Menu;
Menu->AddItem(10, "I would like to enter a code for a reward", 2, 1);
if(Plr->GetSession()->HasGMPermissions())
{
Menu->AddItem(2, "Add new Code Entry", 10, 1);
}
if(AutoSend)
Menu->SendTo(Plr);
}
void CodeBox::GossipSelectOption(Object * pObject, Player* Plr, uint32 Id, uint32 IntId, const char * Code)
{
//Define your code box information
///How many code boxes you want
//CodeBoxes CodeBox[2];//must be equal to int codes below
CodeBoxes CodeBox[2];
int codes = 2;//must be equal to integer inside brackets
//Define Code Options 1- ect.
CodeBox[1].OptionText = "Test1";
CodeBox[1].ItemId = 22253;
CodeBox[2].OptionText = "Test2";
CodeBox[2].ItemId = 4325;
bool AutoSend = true;
GossipMenu *Menu;
switch(IntId)
{
case 0:
//GossipHello(pObject, Plr, true);
Menu->AddItem(10, "I would like to enter a code for a reward", 2, 1);
if(AutoSend)
Menu->SendTo(Plr);
break;
case 1:
{
for(int i=1;i<codes;i++)
{
QueryResult * qres = WorldDatabase.Query("SELECT * FROM entry_codes WHERE code = '%s'", Code);
if(!qres == NULL)
{
Item * item = objmgr.CreateItem(CodeBox[i].ItemId, Plr);
Plr->GetItemInterface()->AddItemToFreeSlot(item);
WorldDatabase.Query("DELETE * FROM entry_codes WHERE code = '%s'", Code);
}
else
{
Plr->BroadcastMessage("Incorrect Code");
}
}break;
case 2:
{
for(int i=0;i<codes;i++)
{
Menu->AddItem(10, CodeBox[i].OptionText, i, 1);
}
}break;
case 10:
{
int length;
length = strlen(Code);
if(length == 14)
{
WorldDatabase.Query("INSERT INTO `entry_codes` VALUES ('%s')", Code);
}
else
{
Plr->BroadcastMessage("Code entry must be 14 characters long");
}
}
}
}
}
void CodeBox::GossipEnd(Object * pObject, Player* Plr)
{
GossipScript::GossipEnd(pObject, Plr);
}
void SetupCode(ScriptMgr * mgr)
{
GossipScript * gs = (GossipScript*) new CodeBox();
mgr->register_gossip_script(NPC, gs);
}
SQL
Code:
USE 'world';
CREATE TABLE "entry_codes" (
"code" char(14) NOT NULL,
UNIQUE KEY "code" ("code")
);
this script is untested please leave feed back if it doesn't work so i can fix it