• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Item Refund System v1.04

  • Like
Reactions: IcemanBo
A very easy to import Item Refund System. Requires Jass New Gen Pack to use.
Needs Table and GetItemCostById library (included in the Sample Map)

JASS:
library ItemRefund initializer OnInit uses Table, GetItemCostById, TimerUtilsEx
    /*
    ------------------------------------------------------------------------------------
                        Item Refund System v1.04 by Shadow Flux
    ------------------------------------------------------------------------------------
    
       Importing this system to your map will make any item have a custom selling cost
       based on the item's gold cost percentage for timeOut (10 by default) seconds
       
       NOTE:
       - If REFUND_RATE > RETURN_RATE in configurables, then a player selling an item
         will have extra gold if it was sold within the allocated TIME_OUT. Useful if
         players accidentally bought an unwanted item, allowing them to sell it immediately
         for the same value (by default) or less (depending on REFUND_RATE).
         
       - If REFUND_RATE < RETURN_RATE in configurables, then a player selling an item
         will have less gold if it was sold within the allocated TIME_OUT. Useful if you
         want players to own the item for certain time before they can sell it at a
         much greater gold.
         
       - If REFUND_RATE = RETURN_RATE, gold will not get affected.
       
       - This system will not work for items having a less than zero gold cost.
    
       ---------------------------------------------------------------------
       |   SPECIAL THANKS TO weaaddar for the GetItemCostById library       |
       |   SPECIAL THANKS TO Bribe for the New Table library                |
       |   SPECIAL THANKS TO Magtheridon96 for the TimerUtilsEx library     |
       ---------------------------------------------------------------------
       
       HOW TO USE:
         1. Just copy the 4 systems (Table, GetItemCostById, TimerUtilsEx, ItemRefund) to your map if they
            are not existing yet.
         2. Go to Advanced > Gameplay Constant > Inventory - Sell Item Return Rate.
            Make sure that the variable RETURN_RATE below is the same as the Inventory - Sell Item Return Rate.
         
    

       REQUIRES:
            Jass NewGen Pack, Table, GetItemCostById, TimerUtilsEx
    */
    
    //========================  CONFIGURABLES   =================================
    globals
        //Players has 10 seconds before the item will no longer be refundable
        private constant real TIME_OUT = 10  
        //Refund Rate of 1 means players will get 100% of the item's total cost
        //Refund Rate of 0 means players will get 0% of the item's total cost
        private constant real REFUND_RATE = 1.0
        //Input the Return Rate defined in the Gameplay Constant
        private constant real RETURN_RATE = 0.5
    endglobals
    
        //SYSTEM VARIABLES, DO NOT TOUCH UNLESS YOU KNOW WHAT YOU'RE DOING
    globals
        private Table IRS_Data   //Item Refund System Data
        private boolean b = REFUND_RATE > RETURN_RATE   //boolean for determining what string to use
    endglobals

    private function GoldText takes integer goldToAdd, unit u, string s returns nothing
        local texttag t = CreateTextTag()
        call SetTextTagPermanent(t, false)
        call SetTextTagLifespan(t, 3)
        call SetTextTagFadepoint(t, 2.5)
        call SetTextTagPos(t, GetUnitX(u), GetUnitY(u), -60)
        call SetTextTagText(t, "|cffffcc00" + s + I2S(goldToAdd), 0.024)
        call SetTextTagVelocity(t, 0, 0.03)
        set t = null
    endfunction

    
    private function OnExpire takes nothing returns nothing
        //Remove the value so that IRS_Data.has(GetHandleId(it)) = false
        local timer t = GetExpiredTimer()
        call IRS_Data.remove(GetTimerData(t)) 
        call ReleaseTimer(t)
        set t = null
    endfunction



    private function OnSellingToShop takes nothing returns boolean
        local item it = GetSoldItem()
        local integer hId = GetHandleId(it)
        local integer currentGold
        local integer goldToAdd
        local player p
        local integer itemCost
        //IF ITEM IS STILL REFUNDALBE
        if IRS_Data.has(hId) then
            call IRS_Data.remove(hId)  
            set itemCost = GetItemGoldCost(it)
            if itemCost > 0 then
                set p = GetTriggerPlayer()
                set currentGold = GetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD)
                //The equation below is not simplified because of Rounding Down of gold cost when an item is sold
                set goldToAdd = R2I(itemCost*(REFUND_RATE)) - R2I(itemCost*(RETURN_RATE))
                call SetPlayerState(p, PLAYER_STATE_RESOURCE_GOLD, currentGold + goldToAdd)
                if b then
                    call GoldText(goldToAdd, GetTriggerUnit(), "+")
                else
                    call GoldText(goldToAdd, GetTriggerUnit(), "")
                endif
                set p = null
            endif
        endif
        set it = null
        return false
    endfunction

    private function OnPurchaseFromShop takes nothing returns boolean
        local item it = GetSoldItem()
        local integer hId = GetHandleId(it)
        call TimerStart(NewTimerEx(hId), TIME_OUT, false, function OnExpire)
        set IRS_Data[hId] = 1           //Having a value means item is refundable
        set it = null
        return false
    endfunction

    //===========================================================================
    private function OnInit takes nothing returns nothing
        local integer i
        local trigger t1 = CreateTrigger()
        local trigger t2 = CreateTrigger()

        set i = 0
            loop
                call TriggerRegisterPlayerUnitEvent(t1, Player(i), EVENT_PLAYER_UNIT_SELL_ITEM, null)
                call TriggerRegisterPlayerUnitEvent(t2, Player(i), EVENT_PLAYER_UNIT_PAWN_ITEM, null)
                set i = i + 1
                exitwhen i == bj_MAX_PLAYER_SLOTS
            endloop
        call TriggerAddCondition(t1, Condition(function OnPurchaseFromShop))
        call TriggerAddCondition(t2, Condition(function OnSellingToShop))
        
        set IRS_Data = Table.create()
        
        set t1 = null
        set t2 = null
        
    endfunction

endlibrary


CREDITS:
- Bribe
- weaaddar
- Magtheridon96

Keywords:
item, refund, selling, shop
Contents

Just another Warcraft III map (Map)

Reviews
Item Refund System v1.03 | Reviewed by BPower | 26.07.2015 Concept[/COLOR]] Manipulates the "PawnItemRate" ( gameplay constant ) for a given amount of time. Very useful system if i.e. a player accidentally buys a wrong item from a shop...

Moderator

M

Moderator


Item Refund System v1.03 | Reviewed by BPower | 26.07.2015

[COLOR="gray"

[COLOR="gray"

[COLOR="gray"

[COLOR="gray"

[COLOR="gray"

Concept[/COLOR]]
126248-albums6177-picture66521.png
Manipulates the "PawnItemRate" ( gameplay constant ) for a
given amount of time.

Very useful system if i.e. a player accidentally buys a wrong item from a shop.
In terms of innovativeness and usefulness I give you a 3/5 rating,
however I have some issues with the code. Change that and you get the 3/5 rating.
Code[/COLOR]]
126248-albums6177-picture66521.png
  • The code is MUI, leakless and working.
Demo Map[/COLOR]]
126248-albums6177-picture66523.png
  • The demo map seems to be ok.
Rating[/COLOR]]
CONCEPTCODEDEMO MAPRATINGSTATUS
3/5
3/5
3/5
2/5
APPROVED
Links[/COLOR]]
126248-albums6177-picture66524.png

[COLOR="gray"

[/TD]
 
Level 22
Joined
Feb 6, 2014
Messages
2,466
CHANGELOG:

v1.04 - [27 July 2015]
- Used TimerUtilsEx for efficient use of timers.
- Optimized the system by removing the IRS_Data.item[] and using Table.has method.
- Changed constant variable names to fit to JPAG naming conventions.

v1.03 - [23 July 2015]
- Nulled a local item variable
- Change demo value of refundRate to 1.0 to see how the system works.

v1.02 - [21 July 2015]
- Fix hashtable item nulling.
- Fix issues if item has a less than zero gold cost.
- Accidentally changed refundRate to 0.5 which matches with returnRate thus nothing happens when a player sold an item.

v1.01 - [19 July 2015]
- Changed to vJass for easier implementation and use of other API
- Used GetItemCostById and Table library.
- Optimized Coding
- Rounding down of item gold cost issue fixed.

v1.00 - [17 July 2015]
- Initial Release
 
Last edited:
Level 22
Joined
Feb 6, 2014
Messages
2,466
the documentation have some outdated parts and I think the coding can still be improved. I can't change it at the moment (because im on mobile) but I will once I'm on my pc.

@btdonald, but it uses vJass. I want this to be pure Jass which doesn't require JNGP.
But I willl used the technique he did about storing the item gold cost to a variable so that it doesn't need to compute the item's cost everytime.
 
Level 22
Joined
Feb 6, 2014
Messages
2,466
I like it, the idea seems useful, but used technique seems not water proof at the moment. (comparing gold after timer)
You can handle the issue yourself or use a system. In pure JASS I found http://www.hiveworkshop.com/forums/...-a-245280/?prev=d=list&r=20&u=deathismyfriend

Thanks! That link is useful. I want to handle the issue myself. I will update and use better coding as soon as possible. Maybe I will use deathismyfriend's method on finding the item cost.
 
Level 11
Joined
Dec 3, 2011
Messages
366
the documentation have some outdated parts and I think the coding can still be improved. I can't change it at the moment (because im on mobile) but I will once I'm on my pc.

@btdonald, but it uses vJass. I want this to be pure Jass which doesn't require JNGP.
But I willl used the technique he did about storing the item gold cost to a variable so that it doesn't need to compute the item's cost everytime.

If you use Jass I think we don't need udg_*** :ogre_hurrhurr:
 
Level 22
Joined
Feb 6, 2014
Messages
2,466
Demo doesn't work at the moment. Something is wrong.

No need to differ paramter for +/- values. Make it constannt positive. Ignore negative completly.
If it's negative just make any further actions anymore.

Null the local item it onExpire.

Hmm, I'll check..
EDIT: I found out why it doesn't work, in the demo, the refundRate is 0.5 and returnRate is 0.5 that's why nothing happened. I changed the demo values in the update

If it happens that refundRate is less than returnRate, then the player will get less gold (because math). If I didn't display the negative sign, the default floating text is wrong.

Damn, I forgot to null the local item variable.
EDIT: Fixed.
 
Last edited:
Level 22
Joined
Feb 6, 2014
Messages
2,466
TimerUtils(Ex) is highly recommeneded here.

constants should be written like: THIS_IS_A_CONSTANT

I thought of using TimerUtils too but it already needs 2 libraries. Is it okay if I add another required library? I think downloaders may get irritated for using too many libraries.

Ok, I will follow the standards for writing constants in the next update.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
Review:
1. Try to stick to the JPAG ( link in my signature ) for a proper variable naming.

2. Using Table is neat, but I would do it little different. ( Combined with TimerUtils )
JASS:
    // On sell
    if table.has(GetHandleId(item)) then
        call remove(GetHandleId(item))
        // do your system stuff.
    
    
    // Callback
    function TimerExpires takes nothing returns nothing
        call table.remove(GetTimerData(GetExpiredTimer()))// Remove entry from table
    endfunction

    // On purchase
    call TimerStart(NewTimerEx(GetHandleId(item)), false, TIMEOUT, function TimerExpires)
    set table[GetHandleId(item)] = 1

3. Using TimerUtils would be a benefit for your system.
 
Top