All pastes #918245 Raw Edit

Mob Event Code?

public text v1 · immutable
#918245 ·published 2008-02-26 00:32 UTC
rendered paste body
/* Copyright (C) 2006 - 2008 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "../sc_defines.h"
#include "../../../../game/Player.h"
#include "../../../../game/GossipDef.h"
#include "../../../../game/World.h"
extern DatabaseMysql ScriptDev2DB;

//This function is called when the player opens the gossip menubool 
bool GossipHello_mob_code_event(Player *player, Creature *_Creature)
{
    player->PlayerTalkClass->GetGossipMenu()->AddMenuItem(0, "Please enter your code", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+1, "", 0, true);
    player->ADD_GOSSIP_ITEM(0, "I'm not interested", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+2);

    player->PlayerTalkClass->SendGossipMenu(907,_Creature->GetGUID());
    return true;
}

//This function is called when the player clicks an option on the gossip menubool 
bool GossipSelect_mob_code_event(Player *player, Creature *_Creature, uint32 sender, uint32 action )
{
    if(action == GOSSIP_ACTION_INFO_DEF+2)
    {
        _Creature->Say("I guess you're not interested then.", LANG_UNIVERSAL, 0);
        player->CLOSE_GOSSIP_MENU();
    }
    return true;
}

uint32 CheckCode(const char* sCode)
{
    if(!sCode)
        return 0;

    QueryResult *result = NULL;
    result = ScriptDev2DB.PQuery("SELECT `item` FROM `event_prizes` WHERE `code` = %c AND `rewarded` < `total` LIMIT 1", sCode);
    if(result)
    {
        Field *fields = result->Fetch();
        uint32 item = fields->GetUInt32();
        if(item)
        {
            ScriptDev2DB.PQuery("UPDATE `event_prizes` SET `rewarded` = `rewarded` + 1 WHERE `item` = %u", item);
            return item;
        }
    }
    return 0;
}

bool GossipSelectWithCode_mob_code_event( Player *player, Creature *_Creature, uint32 sender, uint32 action, const char* sCode )
{
    if(sender == GOSSIP_SENDER_MAIN)
    {
        if(action == GOSSIP_ACTION_INFO_DEF+1)
        {
            uint32 item = CheckCode(sCode);
            if(item)
            {
                Item* newItem = NULL;
                uint16 dest;
                uint32 countForStore = 1;
                uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, item, countForStore, false );

                // if can add all countForStore items
                if( msg == EQUIP_ERR_OK )
                {
                    newItem = player->StoreNewItem( dest, item, countForStore, true, Item::GenerateItemRandomPropertyId(item));
                    player->SendNewItem(newItem, 1, false, true, true);
                    char inform[400];
                    sprintf(inform, "Player %s has just received an event prize!", player->GetName());
                    if(inform)
                        sWorld.SendWorldText(inform);
                }
            }
            else
                _Creature->Say("Please input a valid code.", LANG_UNIVERSAL, 0);

            player->CLOSE_GOSSIP_MENU();
            return true;
        }
    }
    return false;
}

void AddSC_mob_code_event()
{
    Script *newscript;

    newscript = new Script;
    newscript->Name="mob_code_event";
    newscript->pGossipHello =           &GossipHello_mob_code_event;
    newscript->pGossipSelect =          &GossipSelect_mob_code_event;
    newscript->pGossipSelectWithCode =  &GossipSelectWithCode_mob_code_event;
    m_scripts[nrscripts++] = newscript;
}