Warcraft III resources & community, 2003–2006 · archived

InitExtendedItemAPI

not rated
Submitted by weaaddarFunctions0 downloads
Preface: NO IDEA WHAT SECTION THIS GOES UNDER.

This function extends the API of items and very accurately tries "to guess" one of 14 possible happenings of an item.
Its made up of a few things. First to start things off you need to call InitExtendedItemAPI(), perferably at game init. Now you can use calls to getExtendedState() on any of those item related events, and the code will try to "guess" what actually happened. It will loop for at most about a second until it finally gets a result. If it does not get a result it returns 0 (error).
This can allow you some interesting results. You can set up a trigger to handle the guy who traded an item (Use the call in a lose irwm event) and give him some bonus for being nice, and you can also punish the reciever (use the call in an aquire item event) for not giving something in return.
Here is a list of the 15 handled states:
00=="Error : No Event happened"
01=="The item was dropped"
02=="The item was destroyed by a trigger"
03=="The item was given by a trigger"
04=="The item was traded"
05=="The item was an autoconsumed that was given by a trigger"
06=="The item was used"
07=="The item was used to consumption"
08=="The item was sold to a shop"
09=="The item was bought at a shop"
10=="The item was bought at a shop and was autoconsumed"
11=="The item was ordered to be picked up but somewhere along the line the order was canceled"
12=="The item was picked up"
13=="The item was an autoconsumable that was picked up."
14 =="The item was moved in your inventory"

Source

// This function tries to determine what happened and although very accurate it has some small chance of mistake.
// For the most part the main issue is the damn Issued Order one because I can only tell straight line distance which may
// Be off because of pathfinding. Since the Issued Order Time out has to happen with in 10 iterations getExtendedState will
// probably never return that state and just return an error. 
 
function getExtendedState takes nothing returns integer
    local integer time=0 //I set a time out after 10 iterations of the loop
    loop //this has to wait until the reference is found
      exitwhen GetItemUserData(bj_lastCreatedItem)!=0 or time==10
      call TriggerSleepAction( 0.1 )
      set time=time+1
    endloop
    set time=GetItemUserData(bj_lastCreatedItem)
    return time   
endfunction

function ExtendedItemAPI_child takes item it returns nothing
    local item temp=CreateItem( 'texp', 0,0 )
    set bj_lastCreatedItem=temp
    call SetItemVisibleBJ( false, GetLastCreatedItem() )
    if ( GetItemUserData(it) == 0  ) then
        call SetItemUserData(temp,0)   // "Error : No Event happened"

    elseif (GetItemUserData(it) == 1 ) then
        // "The item was dropped"
        call SetItemUserData(temp,1)  // "The item was dropped"

    elseif (GetItemUserData(it) == 2 ) then
        call SetItemUserData(temp,2) // "The item was destroyed by a trigger"

    elseif (GetItemUserData(it) == 4 ) then
        call SetItemUserData(temp,3) // "The item was given by a trigger" 

    elseif (GetItemUserData(it) == 5 ) then
        call SetItemUserData(temp,4) // "The item was traded" 

    elseif (GetItemUserData(it) == 6 ) then
        call SetItemUserData(temp,5) // "The item was an autoconsumed that was given by a trigger"

    elseif (GetItemUserData(it) == 8 ) then
        call SetItemUserData(temp,6) //"The item was used" 

    elseif (GetItemUserData(it) == 10 ) then
        call SetItemUserData(temp,7) //"The item was used to consumption"

    elseif (GetItemUserData(it) == 17 ) then
        call SetItemUserData(temp,8) // "The item was sold to a shop" 

    elseif (GetItemUserData(it) == 36 ) then
        call SetItemUserData(temp,9) // "The item was bought at a shop"

    elseif (GetItemUserData(it) == 38 ) then
        call SetItemUserData(temp,10) //"The item was bought at a shop and was autoconsumed" 

    elseif (GetItemUserData(it) == 64 ) then
        call SetItemUserData(temp,11) //"The item was ordered to be picked up but somewhere along the line the order was canceled"

    elseif (GetItemUserData(it) == 68 ) then
        call SetItemUserData(temp,12) //"The item was picked up"

    elseif (GetItemUserData(it) == 70 ) then
        call SetItemUserData(temp,13) // "The item was an autoconsumable that was picked up."

    elseif (GetItemUserData(it) >= 128 ) then
        call SetItemUserData(temp,14) //"The item was moved in your inventory"
    endif
    call SetItemUserData(it,0)
    set it=null
    call TriggerSleepAction( 10.0 ) //I give you ten seconds to reference it then I destroy it.
    call RemoveItem( temp )
    set temp=null
endfunction

function ExtendedItemAPI_Actions takes nothing returns nothing
    local eventid e = GetTriggerEventId()
    local real deltaD //this is to detect when to stop spamming the check for smart.
    local item it
    if ( e==EVENT_PLAYER_UNIT_DROP_ITEM) then
        set it=GetManipulatedItem()
        if ( GetItemLifeBJ(it) > 0.00 ) then
           call SetItemUserData( it, ( GetItemUserData(it) + 1 ) )
        else
           call SetItemUserData( it, ( GetItemUserData(it) + 2 ) ) 
           call ExtendedItemAPI_child(it) //this is always reached last. no wait can be involved as it'll set the value to zero.
        endif
        if (GetItemUserData(it)>=17) then //sell is also a special affair.
           call ExtendedItemAPI_child(it)
        endif 
    elseif(e==EVENT_PLAYER_UNIT_PICKUP_ITEM) then
        set it=GetManipulatedItem()
        call SetItemUserData( GetManipulatedItem(), ( GetItemUserData(GetManipulatedItem()) + 4 ) )
    elseif(e==EVENT_PLAYER_UNIT_USE_ITEM) then
        set it=GetManipulatedItem() 
        call SetItemUserData( GetManipulatedItem(), ( GetItemUserData(GetManipulatedItem()) + 8 ) )
    elseif(e==EVENT_PLAYER_UNIT_PAWN_ITEM) then
        set it = GetSoldItem()
        call SetItemUserData( it, ( GetItemUserData(it) + 16 ) )
    elseif(e==EVENT_PLAYER_UNIT_SELL_ITEM) then
        set it = GetSoldItem()
        call SetItemUserData( it, ( GetItemUserData(it) + 32 ) )
    elseif(e==EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER) then
        set it = GetOrderTargetItem()
        if ( GetIssuedOrderIdBJ() ==851971 ) then 
           call SetItemUserData( it, ( 64 ) ) //never set this twice incase the guy spams the order.
           loop
              set deltaD=DistanceBetweenPoints( LocationMakeTemporary(GetUnitLoc(GetTriggerUnit())), LocationMakeTemporary(GetItemLoc(it)) )
              call TriggerSleepAction( 0.10 ) //check every tenth of second that the guy keeps moving for the item.
              exitwhen GetUnitCurrentOrder(GetTriggerUnit()) != 851971 or GetItemUserData(it)==0 or deltaD<=DistanceBetweenPoints( LocationMakeTemporary(GetUnitLoc(GetTriggerUnit())), LocationMakeTemporary(GetItemLoc(it)) )
           endloop
        elseif(GetIssuedOrderId() >= 852002) and (GetIssuedOrderId() <= 852007) then
           call SetItemUserData( it, ( GetItemUserData(it) + 128 ) )
           call ExtendedItemAPI_child(it) //the move order can never overlaps so just fire the calculator now.    
        endif
    endif
        call TriggerSleepAction( 0.00 )
        if (GetItemUserData(it)>=1) then //In case none of the special cases get handled wait a very small amount of time and try this.
           call ExtendedItemAPI_child(it)
        endif
    set it=null       
endfunction

//===========================================================================
function InitExtendedItemAPI takes nothing returns nothing
    local trigger ExtendedItemAPI = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( ExtendedItemAPI, EVENT_PLAYER_UNIT_PICKUP_ITEM )
    call TriggerRegisterAnyUnitEventBJ( ExtendedItemAPI, EVENT_PLAYER_UNIT_USE_ITEM )
    call TriggerRegisterAnyUnitEventBJ( ExtendedItemAPI, EVENT_PLAYER_UNIT_PAWN_ITEM )
    call TriggerRegisterAnyUnitEventBJ( ExtendedItemAPI, EVENT_PLAYER_UNIT_SELL_ITEM )
    call TriggerRegisterAnyUnitEventBJ( ExtendedItemAPI, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER )
    call TriggerRegisterAnyUnitEventBJ( ExtendedItemAPI, EVENT_PLAYER_UNIT_DROP_ITEM )
    call TriggerAddAction( ExtendedItemAPI, function ExtendedItemAPI_Actions )
endfunction

Comments

0

No comments.