King of the Hill

Got any custom JavaScript additions/tweaks you think other people would like to see? Post 'em here!
Post Reply
dragon slayer
UOX3 Guru
Posts: 776
Joined: Thu Dec 21, 2006 7:37 am
Has thanked: 4 times
Been thanked: 26 times

King of the Hill

Post by dragon slayer »

Just create an item you want to place somewhere for the event and double click the item and it will start the event.
You can change eh range of the event around the item you can change how long event goes for you can edit the npcs spawning timer or the npc you want to spawn. This event will run until the time ends. I set it for every 10 mins in my shard.


 
function onUseChecked(pUser, iUsed)
{
    var trgSock = pUser.socket;
    if (pUser.isGM)
    {
        if (iUsed.GetTag("eventActive") == false)
        {
            StartEvent(iUsed);
        }
        else
        {
            pUser.SysMessage("Event is already running.");
        }
    }

    // Prevent hard-code from running
    return false;
}

// Starts the event, initializing timers and NPC spawns
function StartEvent(iUsed)
{
    iUsed.SetTag("eventActive", true);

    // Broadcast start of event to all players
    BroadcastMessage("The King of the Hill event has begun! Reach the hill and maintain control!");

    // Reset winner tracking variables
    iUsed.SetTag("currentKOTHWinner", null); // Clear the current winner tag
    iUsed.SetTag("maxPoints", 0);
    iUsed.name = "Hill Top";

    // Set a timer for the event duration using aka set for 10 mins
    iUsed.StartTimer(600000, 0, true);

    // Start the control tracking loop every second (Timer ID: 1)
    iUsed.StartTimer(1000, 1, true);

    // Start NPC spawn loop every 30 seconds (Timer ID: 2)
    iUsed.StartTimer(30000, 2, true);
}

// Function to check control points
function TrackControl(iUsed)
{
    if (iUsed.GetTag("eventActive") == false)
        return;

    // Check for characters within the hill radius
    AreaCharacterFunction("CheckPlayerControl", iUsed, hillLocation.radius, null);
}

// Custom function for `AreaCharacterFunction` to handle each character found
function CheckPlayerControl(srcChar, trgChar, pSock)
{
    if (!trgChar.dead || !pChar.npc)
    {
        // Get existing points from the character's tag, or set to 0 if not found
        var points = trgChar.GetTag("kothControlPoints");
        if (points == null)
        {
            points = 0;
        }

        // Increment control points and update the character's tag
        points++;
        trgChar.SetTag("kothControlPoints", points);
        trgChar.SysMessage("You are gaining control points! Total: " + points);
    }
    return true; // Return true to indicate the character was processed
}

// Function to spawn NPCs randomly around the hill
function SpawnNPCs(iUsed)
{
    if (iUsed.GetTag("eventActive") == false)
        return;

    var xOffset = Math.floor(Math.random() * 10 * 2) - 10;
    var yOffset = Math.floor(Math.random() * 10 * 2) - 10;

    SpawnNPC("orc", iUsed.x + xOffset, iUsed.y + yOffset, iUsed.z, 0, 0, false);
}

// Timer handler for different event-related timers
function onTimer(iUsed, timerID)
{
    if (timerID == 0)
    {
        EndEvent(iUsed); // End event after the event duration
    }
    else if (timerID == 1)
    {
        TrackControl(iUsed); // Track player control every second
        iUsed.StartTimer(1000, 1, true);
    }
    else if (timerID == 2)
    {
        SpawnNPCs(iUsed); // Spawn NPCs periodically
        iUsed.StartTimer(30000, 2, true);// Every 30 Seconds
    }
}

// Function to end the event and declare the winner
function EndEvent(iUsed)
{
    iUsed.SetTag("eventActive", false)
    iUsed.SetTag("maxPoints", 0);

    // Find the player with the most points in the area
    var currentWinner = AreaCharacterFunction("EvaluateWinner", iUsed, hillLocation.radius, null);

    var winner = CalcCharFromSer(parseInt(iUsed.GetTag("currentKOTHWinner")));
    if (currentWinner > 0)
    {
        BroadcastMessage("The King of the Hill event has ended! " + winner.name + " is the winner!");
        GiveReward(winner);
    }
    else
    {
        BroadcastMessage("The King of the Hill event has ended with no winner.");
    }

    // Clear control points from all characters
    AreaCharacterFunction("ClearPlayerControlPoints", iUsed, hillLocation.radius, null);
    iUsed.KillJSTimer(0, 22408);
    iUsed.KillJSTimer(1, 22408);
}

// Function to evaluate which character has the most points
function EvaluateWinner(srcChar, trgChar, pSock)
{
    if (!trgChar.npc)
    {
        var points = trgChar.GetTag("kothControlPoints");
        var maxPoints = srcChar.GetTag("maxPoints");
        if (points != null && points > maxPoints)
        {
            srcChar.SetTag("maxPoints", points);
            srcChar.SetTag("currentKOTHWinner", trgChar.serial);
            srcChar.name = srcChar.name + " Winner is " + trgChar.name;
        }
    }
    return true; // Return true to indicate the character was processed
}


// Function to clear control points tag from characters after the event ends
function ClearPlayerControlPoints(srcChar, trgChar, pSock)
{
    if (!trgChar.npc)
    {
        trgChar.SetTag("kothControlPoints", null); // Clear the character's control points
    }
    return true; // Return true to indicate the character was processed
}

// Function to reward the winning character
function GiveReward(winner)
{
    if (!winner.dead || !winner.npc)
    {
        var myItem = CreateDFNItem(winner.socket, winner, "0x09d0", 10, "ITEM", true);//give them 10 apples
        winner.SysMessage("You received a reward for winning the event!");
    }
}
Post Reply