Device-For-Disposal-Of-Unwanted-Items (aka Trash-can) v1.2

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:

Device-For-Disposal-Of-Unwanted-Items (aka Trash-can) v1.2

Post by Xuri »

This script is basically for creating gold-dispensing trash-cans. Every single item that is dropped into a container which has this script running will produce 1 gold coin, while the item being dropped gets deleted.

Stacks produce an amount of gold equal to the size of the stack.

To use, copy & paste into a text-editor, save document with a .js file extension in your UOX3/JS/custom folder, and then assign it an unused ScriptID in jse_fileassociations.scp. Then use 'SETSCPTRIG # on containers in-game, or add the DFN-tag SCRIPT=# in the DFN sections for containers you want to function as trash-cans (replacing # with the ScriptID you assigned in jse_fileassociations.scp, of course).
// Device-For-Disposal-Of-Unwanted-Items (aka Trash-can) 
// v1.2
// by Xuri (xuri@uox3.org) 
// Last Updated: 26. November 2011 

// Assign this script to any item to make said item act as a trash-can. 

// As an added - optional - bonus, the script can be set to give out gold for every item disposed 
// If enabled, the amount of gold defaults to the stack-size of the item, but can also be based on 
// the weight of the item, or the sell-value of the item divided by two. Only one of the three options
// should be enabled at a time.

var enableGoldReward = false;
var baseGoldOnStackSize = true;
var baseGoldOnWeight = false; 
var baseGoldOnSellValue = false;

function onDropItemOnItem( iDropped, pDropper, iDroppedOn ) 
{ 
    //Safe-guard to stop trashcans from getting deleted if they're dropped into another container 
    if( iDropped.scripttrigger == 5100 ) 
        return 1; // Use hard code, don't bounce item 
     
    //Grab some vital data before dropped item is deleted 
    iPickupSpot = pDropper.socket.pickupSpot; 
    iWeight = iDropped.weight; 
    iStackSize = iDropped.amount; 
     
    //Give feedback (and potentially gold) to player 
    if( enableGoldReward == true ) 
    { 
        var goldAmount = 0;
          if( baseGoldOnStackSize == true ) //If gold-reward is based on stack-size 
             goldAmount = iStackSize;
        else if( baseGoldOnWeight == true ) //If gold-reward is based on item-weight
        { 
            if( iDropped.weight != 0 ) 
                goldAmount = ( Math.ceil( iDropped.weight / 100 )); //rounds up to nearest whole number 
            else 
                goldAmount = 1; //Default to 1 gold for items that have no weight defined
          } 
          else if( baseGoldOnSellValue == true ) //If gold-reward is based on item sell-value
          {
            if( iDropped.sellvalue != 0 )
                goldAmount = Math.ceil( iDropped.sellvalue / 2 );
          }
          
          // Now, spit out some gold for the player!
          if( goldAmount > 0 )
          {
            var newGold = CreateDFNItem( pDropper.socket, pDropper, "0x0EED", goldAmount, "ITEM", true );
            iDroppedOn.TextMessage( "The trashcan disposes of your unwanted junk and spits out "+goldAmount+" gold coins in return!", false, 0x7ef ); 
          }
        else
              iDroppedOn.TextMessage( "The trashcan disposes of your worthless junk.", false, 0x7ef );            
       } 
       else 
          iDroppedOn.TextMessage( "The trashcan disposes of your unwanted junk.", false, 0x7ef );    
    iDropped.Delete(); 
     
    // Subtract weight if item dropped was picked up from one of these spots: 
    // pickupspot 1 = ground, 3 = otherpack, 5 = bank 
    // In other cases it's handled by code when item gets deleted 
    if( iPickupSpot == 1 || iPickupSpot == 3 || iPickupSpot == 5 ) 
    { 
        if( iWeight == 0 || iWeight == 25500 ) 
            pDropper.weight = pDropper.weight - 25500;        
        else 
            pDropper.weight = pDropper.weight - iWeight; 
    }
    return 2; // Don't use hard code, don't bounce item 
} 
Last edited by Xuri on Sat Nov 26, 2011 8:41 pm, edited 3 times in total.
-= Ho Eyo He Hum =-
mihlub
UOX3 Newbie
Posts: 18
Joined: Mon Oct 10, 2011 5:57 am
Location: usa
Has thanked: 0
Been thanked: 0
Contact:

Post by mihlub »

Xuri wrote:This script is basically for creating gold-dispensing trash-cans. Every single item that is dropped into a container which has this script running will produce 1 gold coin, while the item being dropped gets deleted.

Stacks produce an amount of gold equal to the size of the stack.
===========================================

that is wonderful fo me to loot all those items for 2 days. i got another idea:

how about base the price by it's weight. 1 stone of those junks is 1 gp at the trash can would be awesome. the heavier i have to howl those junk to town the better reward at the end. that would begin the uox3environment recycle frienly nothing liter the beautifully designed artwork uox3 developer had spent major time on it. if that work i'll just need to quit this char's gold earning career of ore digger and switch it to full time monster hunter and loot them cleanly. on that topic could player auto loop the monster of all items. better yet is looting be able to drop into a specially design bag instead of the back pack. just do not want to messup that 1st level bag.
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:

Post by Xuri »

Updated the script with a new version that allows for the gold reward to be based on item-weight instead of item stacksize, if enabled at the top of the script.

DISCLAIMER: I haven't tested the "based-on-weight"-parts of the script. Let me know if it works or not =P

Auto-looting is not currently possible, though I think Razor has such a function built-in if you really want that feature. I think the game loses some of its uniqueness when that is used, though - nothing like a good, friendly "looting the dead guy"-contest. xD
-= Ho Eyo He Hum =-
mihlub
UOX3 Newbie
Posts: 18
Joined: Mon Oct 10, 2011 5:57 am
Location: usa
Has thanked: 0
Been thanked: 0
Contact:

Post by mihlub »

Xuri wrote:Updated the script with a new version that allows for the gold reward to be based on item-weight instead of item stacksize, if enabled at the top of the script.

DISCLAIMER: I haven't tested the "based-on-weight"-parts of the script. Let me know if it works or not =P

Auto-looting is not currently possible, though I think Razor has such a function built-in if you really want that feature. I think the game loses some of its uniqueness when that is used, though - nothing like a good, friendly "looting the dead guy"-contest. xD
where is the updated script i could not find it.
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:

Post by Xuri »

The original script = the updated script. I just replaced the original version enclosed in the code-tags in the first post with the updated one. :)
-= Ho Eyo He Hum =-
mihlub
UOX3 Newbie
Posts: 18
Joined: Mon Oct 10, 2011 5:57 am
Location: usa
Has thanked: 0
Been thanked: 0
Contact:

Post by mihlub »

Xuri wrote:Updated the script with a new version that allows for the gold reward to be based on item-weight instead of item stacksize, if enabled at the top of the script.
i tested the script and it works as desire. i set a code 5020 for the script and ust the 'setscptrig 5020 to a empty barrel, i then put the standard book for the new char into the barrel. it delete the book and gave me 1 gold. I continue to put a metal box weight 30 stone into the barrel it gave me 30 gold and the metal is gone by the trashcan magic.
LeoX9
UOX3 Novice
Posts: 55
Joined: Fri Nov 18, 2011 9:19 am
Location: Arizona, USA
Has thanked: 0
Been thanked: 0

Post by LeoX9 »

I've run into some issues where some items, when placed in this can will give 255 gold. (I have 'gold-by-weight' enabled)... These items are:

Arrows that fell to the ground after using my bow.
Empty Bottles AFTER using a potion, not before...
Bags that drop off Blade Spirits (Not sure why they drop a bag)...

Do you know why I might be having this problem Xuri? I know they don't actually weight 255 stones because I can walk with ease even after picking up 20-30 of these arrows/bags/bottles.
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:

Post by Xuri »

WARNING: Long post.

This problem is partly due to my incorrect assumption that all items with a weight of 0 are treated by UOX3 as if they weigh 255. The reality is slightly different, though. When UOX3 encounters an item that has 0 weight, it looks up an hardcoded weight-value from tiledata.mul for that item, and this is what is used to calculate the weight-change for your character. This means that two items that both appear to have 0 weight in-game (through checking with 'TWEAK or 'ISTATS or whatever), can still have different hardcoded weight-values - such as 255 for a floor tile and 1 for an empty potion bottle.

Since the method UOX3 uses to look up the hardcoded weights of items that have no weight specifically defined is not exposed and available to the JS system, this means I cannot do the same as the code and read the hardcoded values instead of assuming a value (in my case, 255).

You can solve the problem for the empty potion bottles yourself by doing the following change (which I'll also do for the next version anyway, as there is no reason why empty bottles shouldn't be spawned with DFN values):

In potion.js, replace the following...
var eBottle = CreateBlankItem( socket, pUser, 1, "#", 0x0F0E, 0, "ITEM", true );
...with:
var eBottle = CreateDFNItem( pUser.socket, pUser, "0x0F0E", 1, "ITEM", true );

The difference is that CreateBlankItem creates an item "from scratch", without using any of the DFN values we've specified for the item, only getting whatever hardcoded values there might be for this item in the tiledata.mul file, while CreateDFNItem will create the item based on how we've defined it in the DFNs. In this case, the empty bottle has a weight of "1" assigned to it in the DFNs.

For the arrows (and any other object you notice this behavior with) you can override the hardcoded weight values through adding the following to UOX3/DFNDATA/harditems.dfn:

Code: Select all

//Arrows and Crossbow-bolts
[0xf3fx1bfb]
{
weight=1
}
This should assign items of id 0xf3f and 0x1bfb with a weight of 1 even if they're spawned "blank" as is the case with the left-over arrows/bolts from combat.

I've updated the script in the first post with a new version that will now default to 1 gold returned for items of weight 0 (instead of 255), and also added another option; base the gold on the sellvalue of the item, divided by two (so as to not make it as profitable to throw away stuff as it would be to travel to a vendor and sell the stuff there).
Last edited by Xuri on Thu Dec 01, 2011 2:17 am, edited 1 time in total.
-= Ho Eyo He Hum =-
LeoX9
UOX3 Novice
Posts: 55
Joined: Fri Nov 18, 2011 9:19 am
Location: Arizona, USA
Has thanked: 0
Been thanked: 0

Post by LeoX9 »

Sounds great. Thank you for looking into it. I have 1 more question and it may be a little off topic for this particular post, but I'm wondering what a few things mean....

what value does 'socket' hold, (or pUser.socket), and what is it used for.

Also, what exactly are the required arguments for the CreateDFNItem.... and when I ask that I mean what kinds of values.... I can guess a few of them but not quite sure how they all work...
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:

Post by Xuri »

First, a random definition gotten from the internet:
What is a Socket?

A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.
Second, in UOX3 JS terms, a socket is basically a reference to the "connection" a player character has to the server, through which you can look up (or set) various flags and values specific for said connection. Example:

Code: Select all

iPickupSpot = pDropper.socket.pickupSpot;
The above line loads the value of the .pickupSpot socket-property for the character "pDropper", which is a reference (saved in the player's "socket") to where the player last picked up an object from (ground, backpack, bank, etc).

Further down the script it's provided as part of the requirements for the CreateDFNItem method (though it might actually be optional in this case, come to think of it, in which case it might also be replaced with NULL).

Code: Select all

var newGold = CreateDFNItem( pDropper.socket, pDropper, "0x0EED", goldAmount, "ITEM", true );
The breakdown of the above line from the trashcan-script goes like this:
pDropper.socket the "socket" object for the character pUser
pDropper is the character the script ran for when said character dropped an item on the trashcan
"0x0EED" is a string referring to an item with ID "0x0EED" - which is basically a gold coin
goldAmount is a variable set elsewhere in the script that defines how many gold coins should be created. This could have been replaced with a number directly as well.
"ITEM" is just an internal reference for UOX3, which can in theory be something else like "CHAR" or "SPAWNER" but in most cases you'll want to leave it as "ITEM".
true is a true/false flag whether to add the item in the backpack of the character specified (true), or in the gameworld at the character's feet (false).
One optional value is missing at the end, which is the color of the item to spawn. Unless you want the item spawned to be a specific hue, you can leave it at 0 or just leave it out altogether.
-= Ho Eyo He Hum =-
LeoX9
UOX3 Novice
Posts: 55
Joined: Fri Nov 18, 2011 9:19 am
Location: Arizona, USA
Has thanked: 0
Been thanked: 0

Post by LeoX9 »

Great. :D Thank you very much.
Post Reply