Page 1 of 1

Shrink System

Posted: Fri Oct 04, 2024 6:37 am
by dragon slayer
Create file called ShrinkPet.js

add this code into that file
function onUseChecked(pUser, iUsed)
{
    var socket = pUser.socket;

    // Check if the item used is the designated shrink potion (you can replace the ID or name)
    if (iUsed.name == "Shrink Potion")
    {
        socket.tempObj = iUsed;  // Store the used item temporarily
        StartShrinkTarget(pUser, iUsed);  // Call function to start shrink pet targeting
    }

    return false;  // Ensure this function does not conflict with other uses
}

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

    // Ensure the item is in the backpack or equipped
    var itemOwner = GetPackOwner(iUsed, 0);
    if (itemOwner == null || itemOwner.serial != pUser.serial)
    {
        pUser.SysMessage("The shrink potion must be in your backpack or equipped.");
        return false;
    }

    // Set up the custom target for selecting a pet
    var targMsg = "Select the pet you wish to shrink.";  // Custom message for targeting
    socket.tempObj = iUsed;  // Store the item in tempObj
    socket.CustomTarget(1, targMsg);  // Initiate target cursor
    return true;
}

// Target callback for shrinking a pet
function onCallback1(socket, Target)
{
    var iUsed = socket.tempObj;  // Retrieve the item used (shrink potion)
    var pUser = socket.currentChar;

    // Check if the target is a tamed creature
    if (Target.tamed)
    {
        if (Target.owner && Target.owner.serial == pUser.serial)  // Ensure the player owns the pet
        {
            // Shrink the pet (assuming ShrinkPet function is already defined)
            ShrinkPet(pUser, Target);
        }
        else if (Target.owner && Target.owner.serial != pUser.serial)  // Pet belongs to someone else
        {
            pUser.SysMessage("That creature is already controlled by another player!");
        }
    }
    else
    {
        pUser.SysMessage("You can only shrink tamed pets.");
    }
}

function ShrinkPet(pUser, pPet)
{
    // Check the number of shrunken pets
    var totalShrunkPets = CountShrunkenPets(pUser);

    // Limit the number of shrunken pets to 26
    if (totalShrunkPets >= 26)
    {
        pUser.SysMessage("You cannot shrink more than 26 pets.");
        return;
    }

    // Save the pet's serial and name in the data file
    var petData = pPet.serial + "," + pPet.name;
    SaveShrunkPetToFile(pUser, petData);

    // Mark the pet as shrunk and teleport it to an off-map location
    pPet.visible = 3;
    pPet.wandertype = 0;
    pPet.frozen = true;
    pPet.hunger = 6;
    pPet.hungerstatus = false;
    pPet.vulnerable = false;
    pPet.Teleport(8000, 8000, 0);

    // Remove pet as an active follower
    pUser.RemoveFollower(pPet);
    pUser.controlSlotsUsed = Math.max(0, pUser.controlSlotsUsed - pPet.controlSlots);

    pUser.SysMessage(pPet.name + " has been shrunk and stored.");
}

function CountShrunkenPets(pUser)
{
    var mFile = new UOXCFile();
    var fileName = "shrunk_pet_" + (pUser.serial).toString() + ".txt";
    var petCount = 0;

    mFile.Open(fileName, "r", "shrunkenpets", true);

    if (mFile && mFile.Length() >= 0) {
        while (!mFile.EOF()) {
            var line = mFile.ReadUntil("\n");
            if (line.length > 1)  // Only count valid lines
            {
                petCount++;
            }
        }
        mFile.Close();
        mFile.Free();
    }

    return petCount;
}

// Save pet data (serial, name) to a file
function SaveShrunkPetToFile(pUser, petData)
{
    var mFile = new UOXCFile();
    var fileName = "shrunk_pet_" + (pUser.serial).toString() + ".txt";

    // Open the file in Append mode
    mFile.Open(fileName, "a", "shrunkenpets", true);
    if (mFile != null)
    {
        mFile.Write(petData + "\n");  // Write the pet data to the file
        mFile.Close();
        mFile.Free();
    }
}
Now create a file called shrunkenpetgump.js

add this code into that file
const maxFollowers = GetServerSetting("MaxFollowers"); // The maximum amount of followers/pets a character can have at a given time
const maxControlSlots = GetServerSetting("MaxControlSlots"); // Maximum amount of control slots per player

function LoadShrunkenPetsFromFile(pUser)
{
    var mFile = new UOXCFile();
    var fileName = "shrunk_pet_" + (pUser.serial).toString() + ".txt";
    var shrunkPets = [];

    mFile.Open(fileName, "r", "shrunkenpets", true);

    if (mFile && mFile.Length() >= 0)
    {
        while (!mFile.EOF())
        {
            var line = mFile.ReadUntil("\n");
            if (line.length <= 1 || line == "")
                continue;

            var petData = line.split(",");
            if (petData.length >= 2)  // Ensure that there are at least two elements (serial, name)
            {
                var petSerial = parseInt(petData[0]);  // Parse the serial number
                var petName = petData[1];

                if (!isNaN(petSerial))  // Ensure that the serial is a valid number
                {
                    shrunkPets.push({
                        serial: petSerial,  // Store the serial number
                        name: petName
                    });
                }
            }
        }
        mFile.Close();
        mFile.Free();
    }

    return shrunkPets;
}


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

    socket.tempObj = iUsed;  // Store the used item temporarily
    ShrunkPetManagerGump(pUser, 0);  // Call function to start shrink pet targeting

    return false;  // Ensure this function does not conflict with other uses
}

function ShrunkPetManagerGump(pUser, iUsed)
{
    var socket = pUser.socket;
    var petGump = new Gump();
    var y = 45;

    // Load shrunken pets from the file
    var shrunkPets = LoadShrunkenPetsFromFile(pUser);

    if (shrunkPets.length == 0)
    {
        pUser.SysMessage("You have no shrunken pets.");
        return;
    }

    // Set up the gump's appearance
    petGump.AddPage(0);
    petGump.AddBackground(0, 0, 340, 800, 0x6DB);
    petGump.AddText(80, 10, 152, "Shrunken Pet Manager");
    petGump.AddText(60, y, 0, "Pet Name");

    // Loop through the shrunken pets and add them to the gump
    for (var i = 0; i < shrunkPets.length; i++)
    {
        var pet = shrunkPets[i];
        var yOffset = y + (i + 1) * 25;
        var hue = (i == 0) ? 1161 : 0x480;

        // Display pet name
        petGump.AddText(60, yOffset, hue, pet.name.toString());

        // Add a "Summon" button for each pet
        petGump.AddButton(280, yOffset, 1209, 1210, 1, i + 1, i + 1);
    }

    // Add a close button at the bottom
    petGump.AddButton(280, 770, 2143, 2142, 1, 0, 5);

    petGump.Send(pUser);
    petGump.Free();
}

function onGumpPress(pSock, pButton, gumpData)
{
    var pUser = pSock.currentChar;

    // Load shrunken pets from the file
    var shrunkPets = LoadShrunkenPetsFromFile(pUser);

    // Case 0 is generally reserved for closing the gump
    if (pButton == 0)
    {
        return;
    }

    // Handle page navigation buttons (IDs 1000 and above for pagination)
    if (pButton >= 1000)
    {
        var newPage = pButton - 1000;  // Calculate the new page number
        ShrunkPetManagerGump(pUser, newPage);  // Call the gump function for the new page
        return;
    }

    // Handle the summoning of pets based on button presses
    if (pButton > 0 && pButton <= shrunkPets.length)
    {
        if (pUser.followerCount < maxFollowers)
        {
            // The button corresponds to a pet in the list
            var selectedPet = shrunkPets[pButton - 1];  // Get the corresponding pet (subtract 1 due to button indexing)

            var pPet = CalcCharFromSer(selectedPet.serial);
            if (maxControlSlots > 0)
            {
                if (pUser.controlSlotsUsed + pPet.controlSlots > maxControlSlots)
                {
                    pUser.socket.SysMessage(GetDictionaryEntry(2390, pUser.socket.language)); // That would exceed your maximum pet control slots.
                    return;
                }
            }
            else if (maxFollowers > 0 && pUser.followerCount >= maxFollowers)
            {
                pUser.socket.SysMessage(GetDictionaryEntry(2780, pUser.socket.language)); // That would exceed your maximum follower count.
                return;
            }

            // Summon the pet (assuming the UnshrinkPet function is already defined)
            UnshrinkPet(pUser, selectedPet.serial);
            //Console.Error("unshrinkpet button - Pet Serial: " + selectedPet.serial);
        }
        else
        {
            pUser.socket.SysMessage("You have to many followers!");
        }
    }
    else
    {
        pUser.socket.SysMessage("Invalid selection.");
    }
}

function UnshrinkPet(pUser, pPetSerial)
{
    var pPet = CalcCharFromSer(pPetSerial);
    if (pPet)
    {
        pPet.visible = 0;
        pPet.frozen = false;
        pPet.hungerstatus = true;
        pPet.vulnerable = true;

        pPet.Teleport(pUser);
        pUser.controlSlotsUsed = pUser.controlSlotsUsed + pPet.controlSlots;
        pUser.AddFollower(pPet);
        pPet.Follow(pUser);

        pUser.SysMessage(pPet.name + " has been unshrunk and summoned.");

        // Remove the pet from the file
        RemovePetFromFile(pUser, pPetSerial);
    }
    else
    {
        Console.Error("Error: Unable to find the pet.");
    }
}

// Remove the unshrunk pet from the file
function RemovePetFromFile(pUser, petSerial)
{
    var mFile = new UOXCFile();
    var fileName = "shrunk_pet_" + (pUser.serial).toString() + ".txt";
    var shrunkPets = [];

    // Open the file once in read mode to load the current pets
    mFile.Open(fileName, "r", "shrunkenpets", true);
    if (mFile && mFile.Length() >= 0)
    {
        while (!mFile.EOF())
        {
            var line = mFile.ReadUntil("\n");
            if (line.length <= 1 || line == "")
                continue;

            var petData = line.split(",");
            if (parseInt(petData[0]) != petSerial)
            {
                shrunkPets.push(line);  // Keep the pets that are not being unshrunk
            }
        }
        mFile.Close();
        mFile.Free();
    }

    // Now, reopen the file in write mode and overwrite the contents
    var writeFile = new UOXCFile();
    writeFile.Open(fileName, "w", "shrunkenpets", true);
    if (writeFile != null)
    {
        for (var i = 0; i < shrunkPets.length; i++)
        {
            writeFile.Write(shrunkPets[i] + "\n");
        }
        writeFile.Close();
        writeFile.Free();
    }
}