• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Dungeon Finder

subyth

New Member
Joined
May 1, 2009
Messages
56
Reaction score
4
TFS: 0.3.6 (OTX 2)


I have a problem with Dungeon Finder.
When I use the / queue join command on the 5 characters, nothing happens (no error in the distro).
When typing / queue join it adds in the DB and the message I entered QUEUE appears, so far, all right. I think the problem must be in the globalevents, as it had not starting out having 5 characters in the queue: /


Dungeon.lua
Lua:
local DUNGEONS = {
[1] = {NAME = "Serpent Dungeon", LEVEL = 30, POS = {x = 30317, y = 32307, z = 10}},
}

local condition = createConditionObject(CONDITION_INFIGHT)
setConditionParam(condition, CONDITION_PARAM_TICKS, 15000)


function onThink(cid, interval)

    DUNGEON = DUNGEONS[math.random(1, table.maxn(DUNGEONS))]
    players = {}
    for i = 1, 1000 do
        if(table.maxn(players) == 5) then
            break
        end
       
    query = db.getResult("SELECT * FROM `dungeon_finder` WHERE `id` = " .. i .. ";")
       
        if(query:getID() > -1) then
            pid = getPlayerByName(getPlayerNameByGUID(query:getDataInt("player_id")))
            if(getPlayerStorageValue(pid, CONFIG.DUNGEON_STORAGE) > 1) then
                return true
            end

            if(getPlayerLevel(pid) > DUNGEON.LEVEL and getPlayerLevel(pid) < DUNGEON.LEVEL + 50) then
                table.insert(players, getPlayerGUID(pid))
            end
           
            query:free()
        end
    end

    if(table.maxn(players) == 5) then
        for i = 1, 5 do
            pid = getPlayerByName(getPlayerNameByGUID(players[i]))
            if(i == 1) then
                doPlayerSendTextMessage(pid, MESSAGE_STATUS_CONSOLE_BLUE, "You were chosen to be a dungeon guide.")
                addEvent(doCreatureSay, 15200, pid, "You and your team were teleported to the " .. DUNGEON.NAME .. ".", TALKTYPE_ORANGE_1)
                for j = 2, 5 do
                    lid = getPlayerByName(getPlayerNameByGUID(players[j]))
                    doPlayerInviteToParty(pid, lid)
                end
            else
                doPlayerJoinParty(pid, getPlayerByName(getPlayerNameByGUID(players[1])))
            end
           
            delay = 0
            for i = 1, 15 do
                addEvent(doPlayerSendTextMessage, delay + 1000, pid, MESSAGE_STATUS_CONSOLE_BLUE, "A dungeon group for you has been found. You'll be teleported to the dungeon in " .. 15 - i .. " seconds.")
                delay = delay + 1000
            end

            doAddCondition(pid, condition)
            addEvent(doTeleportThing, 15000, pid, DUNGEON.POS)
            addEvent(doSendMagicEffect, 15000, DUNGEON.POS, CONST_ME_TELEPORT)
            db.executeQuery("DELETE FROM `dungeon_finder` WHERE `player_id` = " .. players[i] .. ";")

            if(CONFIG.QUIT_POS) then
                setPlayerStorageValue(pid, 42250, getPlayerPosition(pid).x)
                setPlayerStorageValue(pid, 42251, getPlayerPosition(pid).y)
                setPlayerStorageValue(pid, 42252, getPlayerPosition(pid).z)
            end
           
            setPlayerStorageValue(pid, CONFIG.DUNGEON_STORAGE, 1)
        end
    end
    return true
end

Code:
<globalevent name="dungeon" interval="1000" event="script" value="dungeon.lua"/>

Dungeon.lua
Lua:
function onLogout(cid)

    query = db.getResult("SELECT * FROM `dungeon_finder` WHERE `player_id` = " ..  getPlayerGUID(cid) .. ";")
    if(query:getID() == -1) then
        return true
    end

    db.executeQuery("DELETE FROM `dungeon_finder` WHERE `player_id` = " .. getPlayerGUID(cid) .. ";")
    return true
end

DungeonLeave.lua
Lua:
function onLeaveParty(cid)

    if(getPlayerStorageValue(cid, CONFIG.DUNGEON_STORAGE) == 1) then
        setPlayerStorageValue(cid, CONFIG.DUNGEON_STORAGE, -1)
       
        if(CONFIG.DESERTER_DEBUFF_TIME) then
            setPlayerStorageValue(cid, CONFIG.DUNGEON_STORAGE, os.time() + CONFIG.DESERTER_DEBUFF_TIME)
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You've been marked with deserter debuff for " .. CONFIG.DESERTER_DEBUFF_TIME / 3600 .. " hour/s. For this time you can't join the queue again.")
        end

        TMP_POS = CONFIG.QUIT_POS and {x = getPlayerStorageValue(cid, 42250), y = getPlayerStorageValue(cid, 42251), z = getPlayerStorageValue(cid, 42252)} or getPlayerMasterPos(cid)
        doTeleportThing(cid, TMP_POS)
        doSendMagicEffect(TMP_POS, CONST_ME_TELEPORT)
        for i = 42250, 42252 do
            setPlayerStorageValue(cid, i, -1)
        end
    end
    return true
end

Lua:
registerCreatureEvent(cid, "PlayerLogout")
    registerCreatureEvent(cid, "PlayerLeavePT")

Code:
<event type="logout" name="PlayerLogout" event="script" value="dungeon/dungeon.lua"/>
    <event type="leaveparty" name="PlayerLeavePT" event="script" value="dungeon/dungeononleave.lua"/>

Dungeon.lua
Lua:
function onSay(cid, words, param, channel)

if(param == "join") then

    query = db.getResult("SELECT * FROM `dungeon_finder` WHERE `player_id` = " ..  getPlayerGUID(cid) .. "")   
    if(getPlayerStorageValue(cid, CONFIG.DUNGEON_STORAGE) > 1) then
        if(getPlayerStorageValue(cid, CONFIG.DUNGEON_STORAGE) > os.time()) then
            doPlayerSendCancel(cid, "You can't join queue with deserter debuff.")
            doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
            return true
        end
    end
   
    if(CONFIG.AREA) then
        if(not(isInArea(getPlayerPosition(cid), (CONFIG.AREA).FROMPOS, (CONFIG.AREA).TOPOS))) then
            doPlayerSendCancel(cid, "You're not in required area to join the queue.")
            doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
            return true
        end
    end

    if(CONFIG.PZ_REQUIRED) then
        if(not(getTilePzInfo(getPlayerPosition(cid)))) then
            doPlayerSendCancel(cid, "You're not in protection zone to join the queue.")
            doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
            return true
        end
    end

    if(CONFIG.REQUIRED_LEVEL) then
        if(getPlayerLevel(cid) < CONFIG.REQUIRED_LEVEL) then
            doPlayerSendCancel(cid, "You don't have required level to join the queue. The minimum level to join the queue is " .. CONFIG.REQUIRED_LEVEL .. ".")
            doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
            return true
        end
    end

    if(CONFIG.SKULL) then
        if(getPlayerSkullType(cid) >= CONFIG.SKULL) then
            doPlayerSendCancel(cid, "Murderers are not allowed to join the queue.")
            doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
            return true
        end
    end

    if(CONFIG.IN_FIGHT) then
        if(getCreatureCondition(cid, CONDITION_INFIGHT)) then
            doPlayerSendCancel(cid, "You can't be in combat in order to join the queue.")
            doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
            return true
        end
    end

    if(isInParty(cid)) then
        doPlayerSendCancel(cid, "You can't join queue while you are in party group.")
        doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
        return true
    end

    if(query:getID() == 0) then
        doPlayerSendCancel(cid, "You are already listed in queue.")
        doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
        return true
    end

    db.executeQuery("INSERT INTO `dungeon_finder` SET `player_id` = " .. getPlayerGUID(cid) .. ";")
    doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_BLUE)
    doCreatureSay(cid, "You've beed queued in dungeon finder - random mode.", TALKTYPE_ORANGE_1)

elseif(param == "remove") then

    query = db.getResult("SELECT * FROM `dungeon_finder` WHERE `player_id` = " ..  getPlayerGUID(cid) .. "")
    if(query:getID() == -1) then
        doPlayerSendCancel(cid, "You are not listed in the queue.")
        doSendMagicEffect(getPlayerPosition(cid), CONST_ME_POFF)
        return true
    end

    db.executeQuery("DELETE FROM `dungeon_finder` WHERE `player_id` = " .. getPlayerGUID(cid) .. ";")
    doCreatureSay(cid, "You've beed removed from queue.", TALKTYPE_ORANGE_1)
    doSendMagicEffect(getPlayerPosition(cid), CONST_ME_MAGIC_RED)
end
return true
end

Code:
<talkaction access="0-6" words="/queue;!queue" event="script" value="dungeon.lua"/>

DB
CREATE TABLE `dungeon_finder` (`id` INT(8) AUTO_INCREMENT PRIMARY KEY, `player_id` INT(255))

Lib/Dungeon.lua
CONFIG = {
AREA = {FROMPOS = {x = 32256, y = 32195, z = 9}, TOPOS = {x = 32264, y = 32205, z = 9}}, -- if false then everyone can join queue everywhere, if you want certain area add AREA = {FROMPOS = {}, TOPOS = {}}
PZ_REQUIRED = true, -- requirement of standing in pz, if you don't want it just set PZ_REQUIRED = false
REQUIRED_LEVEL = 30, -- required level to join the queue, if you don't want it just set REQUIRED_LEVEL = false
SKULL = 1, -- skull that is not acceptable while joining queue, if you want players join with skulls just set SKULL = false
IN_FIGHT = true, -- if player is in fight he can't join the queue, IN_FIGHT = false to disable
QUIT_POS = true, -- if you want player to go back to his previous position (from which he got teleported to the dungeon) set QUIT_POS = true, if set to false it'll teleport player to his temple
DESERTER_DEBUFF_TIME = 3600, -- 60 = 1 min, 3600 = 1h
DUNGEON_STORAGE = 42253
}
LuaScript.h
C++:
static int32_t luaDoPlayerInviteToParty(lua_State * L);
LuaScript.cpp
C++:
//doPlayerInviteToParty(cid, pid)
lua_register(m_luaState, "doPlayerInviteToParty", LuaInterface::luaDoPlayerInviteToParty);
C++:
int32_t LuaInterface::luaDoPlayerInviteToParty(lua_State* L)
{
    //doPlayerInviteToParty(cid, pid)
    ScriptEnviroment* env = getEnv();

    Player* leader = env->getPlayerByUID(popNumber(L));
    if(!leader)
    {
        errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
        lua_pushboolean(L, false);
    }

    Player* player = env->getPlayerByUID(popNumber(L));
    if(!player)
    {
        errorEx(getError(LUA_ERROR_PLAYER_NOT_FOUND));
        lua_pushboolean(L, false);
    }

    g_game.playerInviteToParty(player->getID(), leader->getID());
    lua_pushboolean(L, true);
    return 1;
}

Creatureevent.h
C++:
CREATURE_EVENT_LEAVEPARTY
Lua:
uint32_t executeLeaveParty(Player* player);
C++:
uint32_t executeLeaveParty(Player* player);

Creatureevent.cpp
C++:
else if(type== "leaveparty")
        _type = CREATURE_EVENT_LEAVEPARTY;
C++:
case CREATURE_EVENT_LEAVEPARTY:
            return "onLeaveParty";
C++:
case CREATURE_EVENT_LEAVEPARTY:
            return "cid";
C++:
uint32_t CreatureEvent::executeLeaveParty(Player* player)
{
    //onLeaveParty(cid)
    if(m_interface->reserveEnv())
    {
        ScriptEnviroment* env = m_interface->getEnv();
        #ifdef __DEBUG_LUASCRIPTS__
            std::stringstream desc;
            desc << player->getName();
            env->setEventDesc(desc.str());
        #endif

        env->setScriptId(m_scriptId, m_interface);
        env->setRealPos(player->getPosition());

        lua_State* L = m_interface->getState();
        m_interface->pushFunction(m_scriptId);

        lua_pushnumber(L, env->addThing(player));

        bool result = m_interface->callFunction(1);
        m_interface->releaseEnv();
        return result;
    }
    else
    {
        std::cout << "[Error - CreatureEvent::executeAdvance] Call stack overflow." << std::endl;
        return 0;
    }
}

Game.cpp
C++:
bool Game::playerLeaveParty(uint32_t playerId, bool forced/* = false*/)
{
    Player* player = getPlayerByID(playerId);
    if(!player || player->isRemoved() || !player->getParty() || (player->hasCondition(CONDITION_INFIGHT) && !forced))
        return false;

    CreatureEventList leavePartyEvents = player->getCreatureEvents(CREATURE_EVENT_LEAVEPARTY);
    for(CreatureEventList::iterator it = leavePartyEvents.begin(); it != leavePartyEvents.end(); ++it)
        (*it)->executeLeaveParty(player);
   
    return player->getParty()->leave(player);
}

Thx! Sorry my bad english.
 
Back
Top