Recursive search by name

Home Forums Scripts Recursive search by name

Tagged: 

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #2048
    chester
    Participant

    Search item by name

    Purpose: Find and get an item by its name. Very helpful when you can not distinguish items by color and type.

    How to: Use select container button and target the container you wish to search in. Type the keyword (it may be part of item's name) and press search button.

    ////////////////////////////////////////////////////////////////////
    ///  Author: chester
    /// Server: Neverlands Heritage
    ////////////////////////////////////////////////////////////////////
    /// The search works with bags / backpacks / metal chests
    ////////////////////////////////////////////////////////////////////
    var containerId = 'unset';
    var loader = 1;
    var foundItems = 0;
    
    function main() {
        createGump()
    }
    
    function searchItem(name, containerId, gump) {
        Orion.Print(61, 'Searching for ' + name)
        var items = []
    
        const containers = Orion.Split(Orion.GetGlobal('globalContainers'), '|')
    
        containers.forEach(function (x, i) {
            const newItems = Orion.FindType(any, -1, x)
            items = items.concat(newItems)
        })
    
        items.forEach(function(x, i) {
            const itemName = Orion.FindObject(x)
            Orion.RequestName(x, 350)
    
            if (itemName &&Orion.Contains(itemName.FullName(), name)) {
                Orion.MoveItem(x, 0, backpack)
                foundItems++
                gump.AddText(25, 70, '0xCE9', "Found: " + foundItems , 150, 3002)
                gump.Update()
            }
    
            updateLoader(i + 1, items.length)
        })
    
        foundItems = 0;
    }
    
    function recursiveOpen(containerId) {
        var containers = []
        addContainers(containers)
        var flag = false;
    
        while (!flag && containers) {
            containers = getContainers();
            Orion.Wait(100)
    
            if (containers.length)
            const newContainers = addContainers(containers)
    
            if (!newContainers.length) flag = true
        }
    }
    
    function getContainers() {
        const globalContainers =  Orion.Split(Orion.GetGlobal('globalContainers'), '|')
        var containers = []
    
        for (var i = 0; i < globalContainers.length; i++) {
            const newContainers = Orion.FindType('0x0E76|0x0E75|0x0E41|0x0E40', -1, globalContainers[i])
            if (newContainers.length) containers = containers.concat(newContainers)
        }
    
        if (!containers.length) return []
        else return containers
    }
    
    function addContainers(containers) {
        const newContainers = []
    
        for (var i = 0; i < containers.length; i++) {
            const globalArr = Orion.GetGlobal('globalContainers')
            const added = Orion.Contains(globalArr, containers[i])
    
            if (!added) {
                Orion.SetGlobal('globalContainers', globalArr + containers[i] + '|') 
                newContainers.push(containers[i])
            }
    
            if (!Orion.GumpExists('container', containers[i])) {
                Orion.UseObject(containers[i])
                Orion.Wait(300)
            }
        }
    
        return newContainers;
    }
    
    function createGump() {
        var gump = Orion.CreateCustomGump(150);
    
        gump.SetNoClose(true);
    
        gump.Clear();
    
        gump.SetCallback('handleGumpResponse');
    
        gump.AddGumpPic(8, -37, '5171', 31, 8009, 32, 145, 40)
        gump.AddResizepic(0, -2, '9354', 235, 150, 8002);
        gump.AddGumpPic(225, -5, '5125', 137, 8006, 137, 10, 165)
        gump.AddGumpPic(-5, 147, '3601', 137, 8001, 137, 240)
        gump.AddGumpPic(-5, -15, '3601', 137, 8003, 137, 240)
        gump.AddGumpPic(-25, 0, '10422', 137, 8004, 137, 50, 165)
        gump.AddGumpPic(-19, -22, '9780', 137, 8005, 137)
        gump.AddGumpPic(103, 75, '9751', 0, 8008, 1000, 95)
    
        gump.AddGroup(1);
        gump.AddText(25, 10, 90, 'Container');
        gump.AddText(25, 35, 90, 'Keyword');
    
        gump.AddButton(9001, 151, 107, '0x2C89', '0x2C89', '0x2C8A', 0);
        gump.AddButton(9002, 25, 107, '0x2C89', '0x2C89', '0x2C8A', 0);
        gump.AddButton(9003, 205, 8, '0x3', '0x3', '0x3', 0);
    
        gump.AddText(101, 10, 152, containerId, 120,  8001);
        gump.AddColoredPolygone(101, 35, 100, 19, 'darkgreen', 0, 3, 1, 7001);
        gump.AddTextEntry(8002, 105, 35, 67, '', 100, 18, 11)
    
        gump.AddText(43, 105, 49, 'Set container');
        gump.AddText(168, 105, 49, 'Search');
        gump.AddText(25, 70, '0xCE9', "Found: " + foundItems , 150, 3002)
    
        gump.AddText(10, -35, '0x7B2', 'Neverlands Heritage');
        gump.AddText(123, 128, '0x7B2', 'author');
        gump.AddText(166, 130, '0x7B2', ':');
        gump.AddText(173, 128, '0x5F1', 'chester');
    
        gump.Update();
    }
    
    
    function handleGumpResponse() {
        const gump = Orion.CreateCustomGump(150);
        const response = CustomGumpResponse.ReturnCode();
        const mainContainer = Orion.FindObject('searchItemContainer');
        const keyword =  CustomGumpResponse.Text(8002)
    
        switch(response) {
            case 9001:
                if(mainContainer) {
                    recursiveOpen(mainContainer.Serial());
                    searchItem(keyword, mainContainer.Serial(), gump)
                }
                else Orion.Print(34, 'Set container first!')
                break;
            case 9002:
                setContainer()
                gump.AddText(101, 10, 152, mainContainer.Serial(), 120, 8001);
                gump.AddTextEntry(8003, 115, 35, 67, mainContainer.Serial(), 0, 0, 11)
                break;
            case 9003:
                gump.Close()
                if(Orion.ScriptRunning('handleGumpResponse'))
                    Orion.Terminate('handleGumpResponse')
        }
            gump.Update()
    }
    
    function setContainer() {
        Orion.Print(22, 'Select container')
        Orion.WaitForAddObject('searchItemContainer')
        const mainContainer = Orion.FindObject('searchItemContainer');
    
        if (mainContainer) {
            Orion.SetGlobal('globalContainers', mainContainer.Serial() + '|')
            Orion.UseObject(mainContainer.Serial())
        }
    }
    
    function updateLoader(i, total) {
        const gump = Orion.CreateCustomGump(150);
        gump.AddGumpPic(103, 75, '9752', 0, 8011, 0, (i / total) * 95)
        gump.Update()
    }
    
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.