Reagent Counter v1.0

Got any custom JavaScript additions/tweaks you think other people would like to see? Post 'em here!
Post Reply
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Reagent Counter v1.0

Post by Xuri »

This is primarily a way to showcase the fact that you can "refresh" (force-close and reopen) gumps, but can also function as an alternative to the reagent counter functionality in Razor :P

Copy & paste script into a new text document in your favourite text editor, save it as UOX3/js/custom/reagentCounter.js (make sure it doesn't have .js.txt extension), then open UOX3/js/jse_fileassociations.scp and add the following somewhere in the [SCRIPT_LIST] section:

Code: Select all

5100=custom/reagentCounter.js
Save, close the files, reload JavaScripts in UOX3 console or restart UOX3 - then finally assign the script to an item in-game that you can use to trigger the script using this command:
'setscptrig 5100

// Reagent Counter v1.0
// by Xuri (xuri@uox3.org)
// Last Updated: September 27th 2020
//
// Script for counting the number of reagents a player carries.
// By default, gump refreshes every 2 seconds

const verNum = "1.0";
const refreshTimer = 1000; // Refresh every 1.000 seconds
const lowThreshold = 20; // Threshold for when reagent count is considered low

// Variables that keep count of the reagents
var pearl = 0;
var root = 0;
var blood = 0;
var garlic = 0;
var ginseng = 0;
var shade = 0;
var ash = 0;
var silk = 0;

function onUseChecked( pUser, iUsed )
{
    var socket = pUser.socket;

    // Calculate gumpID and store it in a custom tag for later
    var gumpID = iUsed.scripttrigger + 0xffff;
    pUser.SetTag( "regCountGumpID", gumpID );

    // Set default value for tag that stops script from updating
    pUser.SetTag( "regCountGumpStop", 0 );

    // Do initial reagent count
    CountReagents( pUser, socket );

    // Fetch which direction of the gump to show
    var gumpDir = pUser.GetTag( "regCountGumpDir" );
    switch( gumpDir )
    {
        case 0:
            showCounterGump0( pUser, socket );
            break;
        case 1:
            showCounterGump1( pUser, socket );
            break;
        case 2:
            showCounterGump2( pUser, socket );
            break;
        default:
            showCounterGump0( pUser, socket );
            break;
    }

    // Start timer to refresh gump
    pUser.StartTimer( refreshTimer, gumpDir, true );
    return false;
}

function onTimer( timerObj, timerID )
{
    var socket = timerObj.socket;
    if( socket == NULL )
        return;

    // Fetch gumpID from regCountGumpID tag
    var gumpID = timerObj.GetTag( "regCountGumpID" );

    // Create packet to force-close gump
    var pStream = new Packet;
    pStream.ReserveSize( 13 );
    pStream.WriteByte( 0, 0xBF ); // Command: Packet 0xBF - General Information Packet
    pStream.WriteShort( 1, 13 ); // Packet length
    pStream.WriteShort( 3, 0x04 ); // Subcommand 0x04 - Close Generic Gump
    pStream.WriteLong( 5, gumpID ); // dialogID - which gump to destroy
    pStream.WriteLong( 9, 0 );      // buttonID // response buttonID for packet 0xB1
    socket.Send( pStream );
    pStream.Free();

    var stopRefresh = timerObj.GetTag( "regCountGumpStop" );
    if( stopRefresh == 0 )
    {
        // Count reagents
        CountReagents( timerObj, socket );

        // Reopen same gump!
        var gumpDir = timerObj.GetTag( "regCountGumpDir" );
        switch( gumpDir )
        {
            case 0:
                showCounterGump0( timerObj, socket );
                break;
            case 1:
                showCounterGump1( timerObj, socket );
                break;
            case 2:
                showCounterGump2( timerObj, socket );
                break;
            default:
                showCounterGump0( timerObj, socket );
                break;
        }

        // Start another timer to refresh the gump
        timerObj.StartTimer( refreshTimer, gumpDir, true );
    }
}

function CountReagents( pUser, socket )
{
    // Count the number of reagents in player's backpack
    pearl = pUser.ResourceCount( 0xF7A, 0 );
    root = pUser.ResourceCount( 0xF86, 0);
    blood = pUser.ResourceCount( 0xF7B, 0);
    garlic = pUser.ResourceCount( 0xF84, 0);
    ginseng = pUser.ResourceCount( 0xF85, 0);
    shade = pUser.ResourceCount( 0xF88, 0);
    ash = pUser.ResourceCount( 0xF8C, 0);
    silk = pUser.ResourceCount( 0xF8D, 0);
}

function showCounterGump0( pUser, socket )
{
    // Construct and send main gump
    var countDataGump0 = new Gump;
    countDataGump0.NoClose();
    countDataGump0.AddPage( 1 );
    countDataGump0.AddBackground( 0, 0, 125, 25, 0x1400 );
    countDataGump0.AddBackground( 0, 0, 0, 0, 0x053 ); //Transparent version
    countDataGump0.AddText( 5, 25, 5, "Black Pearl:" );
    countDataGump0.AddText( 5, 40, 5, "Mandrake Root:" );
    countDataGump0.AddText( 5, 55, 5, "Blood Moss:" );
    countDataGump0.AddText( 5, 70, 5, "Garlic:" );
    countDataGump0.AddText( 5, 85, 5, "Ginseng:" );
    countDataGump0.AddText( 5, 100, 5, "Nightshade:" );
    countDataGump0.AddText( 5, 115, 5, "Sulphurous Ash:" );
    countDataGump0.AddText( 5, 130, 5, "Spider's Silk:" );
    countDataGump0.AddText( 105, 25, (pearl > 0 ? ( pearl > lowThreshold ? 2000 : 1500 ) : 37), ""+pearl+"" );
    countDataGump0.AddText( 105, 40, (root > 0 ? ( root > lowThreshold ? 2000 : 1500 ) : 37), ""+root+"" );
    countDataGump0.AddText( 105, 55, (blood > 0 ? ( blood > lowThreshold ? 2000 : 1500 ) : 37), ""+blood+"" );
    countDataGump0.AddText( 105, 70, (garlic > 0 ? ( garlic > lowThreshold ? 2000 : 1500 ) : 37), ""+garlic+"" );
    countDataGump0.AddText( 105, 85, (ginseng > 0 ? ( ginseng > lowThreshold ? 2000 : 1500 ) : 37), ""+ginseng+"" );
    countDataGump0.AddText( 105, 100, (shade > 0 ? ( shade > lowThreshold ? 2000 : 1500 ) : 37), ""+shade+"" );
    countDataGump0.AddText( 105, 115, (ash > 0 ? ( ash > lowThreshold ? 2000 : 1500 ) : 37), ""+ash+"" );
    countDataGump0.AddText( 105, 130, (silk > 0 ? ( silk > lowThreshold ? 2000 : 1500 ) : 37), ""+silk+"" );
    countDataGump0.AddButton( 105, 6, 0x3, 1, 0, 2 ); // Close button
    countDataGump0.AddButton( 5, 4, 0x1459, 1, 0, 3 );
    countDataGump0.AddButton( 30, 4, 0x1468, 1, 0, 4 );
    countDataGump0.Send( socket );
    countDataGump0.Free();
}

function showCounterGump1( pUser, socket )
{
    // Construct and send vertical gump
    var countDataGump1 = new Gump;
    countDataGump1.NoClose();
    countDataGump1.AddPage( 1 );
    countDataGump1.AddBackground( 0, 0, 65, 25, 0x1400 );
    countDataGump1.AddBackground( 0, 0, 0, 0, 0x053 ); //Transparent version
    countDataGump1.AddText( 5, 25, 5, "BP:" );
    countDataGump1.AddText( 5, 40, 5, "MR:" );
    countDataGump1.AddText( 5, 55, 5, "BM:" );
    countDataGump1.AddText( 5, 70, 5, "GA:" );
    countDataGump1.AddText( 5, 85, 5, "GI:" );
    countDataGump1.AddText( 5, 100, 5, "NS:" );
    countDataGump1.AddText( 5, 115, 5, "SA:" );
    countDataGump1.AddText( 5, 130, 5, "SS:" );
    countDataGump1.AddText( 45, 25, (pearl > 0 ? ( pearl > lowThreshold ? 2000 : 1500 ) : 37), ""+pearl+"" );
    countDataGump1.AddText( 45, 40, (root > 0 ? ( root > lowThreshold ? 2000 : 1500 ) : 37), ""+root+"" );
    countDataGump1.AddText( 45, 55, (blood > 0 ? ( blood > lowThreshold ? 2000 : 1500 ) : 37), ""+blood+"" );
    countDataGump1.AddText( 45, 70, (garlic > 0 ? ( garlic > lowThreshold ? 2000 : 1500 ) : 37), ""+garlic+"" );
    countDataGump1.AddText( 45, 85, (ginseng > 0 ? ( ginseng > lowThreshold ? 2000 : 1500 ) : 37), ""+ginseng+"" );
    countDataGump1.AddText( 45, 100, (shade > 0 ? ( shade > lowThreshold ? 2000 : 1500 ) : 37), ""+shade+"" );
    countDataGump1.AddText( 45, 115, (ash > 0 ? ( ash > lowThreshold ? 2000 : 1500 ) : 37), ""+ash+"" );
    countDataGump1.AddText( 45, 130, (silk > 0 ? ( silk > lowThreshold ? 2000 : 1500 ) : 37), ""+silk+"" );
    countDataGump1.AddButton( 45, 6, 0x3, 1, 0, 2 ); // Close button
    countDataGump1.AddButton( 5, 4, 0x1458, 1, 0, 1 );
    countDataGump1.Send( socket );
    countDataGump1.Free();
}

function showCounterGump2( pUser, socket )
{
    // Construct and send horizontal gump
    var countDataGump2 = new Gump;
    countDataGump2.NoClose();
    countDataGump2.AddPage( 1 );
    countDataGump2.AddBackground( 0, 0, 305, 25, 0x1400 );
    countDataGump2.AddBackground( 0, 0, 0, 0, 0x053 ); //Transparent version
    countDataGump2.AddText( 80, 2, 86, "Reagent Counter v" + verNum );
    countDataGump2.AddText( 5, 25, 5, "BP:" );
    countDataGump2.AddText( 45, 25, 5, "MR:" );
    countDataGump2.AddText( 85, 25, 5, "BM:" );
    countDataGump2.AddText( 125, 25, 5, "GA:" );
    countDataGump2.AddText( 165, 25, 5, "GI:" );
    countDataGump2.AddText( 205, 25, 5, "NS:" );
    countDataGump2.AddText( 245, 25, 5, "SA:" );
    countDataGump2.AddText( 285, 25, 5, "SS:" );
    countDataGump2.AddText( 5, 40, (pearl > 0 ? ( pearl > lowThreshold ? 2000 : 1500 ) : 37), ""+pearl+"" );
    countDataGump2.AddText( 45, 40, (root > 0 ? ( root > lowThreshold ? 2000 : 1500 ) : 37), ""+root+"" );
    countDataGump2.AddText( 85, 40, (blood > 0 ? ( blood > lowThreshold ? 2000 : 1500 ) : 37), ""+blood+"" );
    countDataGump2.AddText( 125, 40, (garlic > 0 ? ( garlic > lowThreshold ? 2000 : 1500 ) : 37), ""+garlic+"" );
    countDataGump2.AddText( 165, 40, (ginseng > 0 ? ( ginseng > lowThreshold ? 2000 : 1500 ) : 37), ""+ginseng+"" );
    countDataGump2.AddText( 205, 40, (shade > 0 ? ( shade > lowThreshold ? 2000 : 1500 ) : 37), ""+shade+"" );
    countDataGump2.AddText( 245, 40, (ash > 0 ? ( ash > lowThreshold ? 2000 : 1500 ) : 37), ""+ash+"" );
    countDataGump2.AddText( 285, 40, (silk > 0 ? ( silk > lowThreshold ? 2000 : 1500 ) : 37), ""+silk+"" );
    countDataGump2.AddButton( 285, 6, 0x3, 1, 0, 2 ); // Close button
    countDataGump2.AddButton( 5, 4, 0x1467, 1, 0, 1 );
    countDataGump2.Send( socket );
    countDataGump2.Free();
}

function onGumpPress( socket, pButton, gumpData )
{
    var pUser = socket.currentChar;
    switch( pButton )
    {
        case 1: // Change view to main gump
            pUser.SetTag( "regCountGumpDir", 0 );
            showCounterGump0( pUser, socket );
            break;
        case 2: //Closes gump, stops refreshing
            pUser.SetTag( "regCountGumpStop", 1 );
            break;
        case 3: // Change view to vertical gump
            pUser.SetTag( "regCountGumpDir", 1 );
            showCounterGump1( pUser, socket );
            break;
        case 4: // Change view to horizontal gump
            pUser.SetTag( "regCountGumpDir", 2 );
            showCounterGump2( pUser, socket );
            break;
        default:
            pUser.SetTag( "regCountGumpDir", 0 );
            showCounterGump0( pUser, socket );
            break;
    }
}
-= Ho Eyo He Hum =-
Post Reply