• 🏆 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!

[JASS] Sell all items

Status
Not open for further replies.
Level 6
Joined
Oct 14, 2010
Messages
58
hi

I create system of manipulations for player which leave from the game

It is necessary to make so the unit would sell all available things in his inventory

Here is my code of test triggers:
JASS:
function AddWaitTime takes nothing returns nothing
    set udg_wait = udg_wait + 0.01
endfunction
function SimpleSellWithSleep takes nothing returns nothing
    local item Item
    local unit u = gg_unit_Hpal_0000 //SellTarget
    local unit c = gg_unit_ngme_0002 //Shop
    local integer i = 1
    local real x = GetUnitX(c)
    local real y = GetUnitY(c)
    loop
        set Item = UnitItemInSlot(u,i)
        if Item != null then
            call IssueTargetOrder(u,"smart",Item)
            call IssueTargetOrder(u,"smart",c)
            call TriggerSleepAction(udg_wait)
        endif
        set i = i + 1
        exitwhen i > 5
    endloop
    set u = null
    set c = null
    set Item = null
endfunction
function SimpleSell takes nothing returns nothing
    local item Item
    local unit u = gg_unit_Hpal_0000 //SellTarget
    local unit c = gg_unit_ngme_0002 //Shop
    local integer i = 1
    local real x = GetUnitX(c)
    local real y = GetUnitY(c)
    loop
        set Item = UnitItemInSlot(u,i)
        if Item != null then
            call IssueTargetOrder(u,"smart",Item)
            call IssueTargetOrder(u,"smart",c)
        endif
        set i = i + 1
        exitwhen i > 5
    endloop
    set u = null
    set c = null
    set Item = null
endfunction
function InitTrig_Test takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterPlayerChatEvent(t,Player(0),"sell",false)
    call TriggerAddAction(t, function SimpleSell)
    set t = CreateTrigger()
    call TriggerRegisterPlayerChatEvent(t,Player(0),"sellwait",false)
    call TriggerAddAction(t, function SimpleSellWithSleep)
    set t = CreateTrigger()
    call TriggerRegisterPlayerChatEvent(t,Player(0),"addwait",false)
    call TriggerAddAction(t, function AddWaitTime)
    set t = null
    call SetPlayerState(Player(0),PLAYER_STATE_RESOURCE_GOLD, 1000000)
endfunction

sorry for bad English
Please help me
 
Level 37
Joined
Mar 6, 2006
Messages
9,240
JASS:
library Sell initializer Init_Sell   

    globals

        private hashtable hash = InitHashtable()
        private group units = CreateGroup()
        private constant real TIMEOUT = 0.05 // If this is too low, the unit may skip an order
        private timer loopTimer = CreateTimer()
        private integer sellers = 0
    
    endglobals
    
    
    private function LoopSelling takes nothing returns nothing
        local unit u = GetEnumUnit()
        local integer id = GetHandleId(u)
        local integer index = LoadInteger( hash , id , StringHash("index") )
        local item it
        
        loop
            set it = UnitItemInSlot( u , index )
            set index = index + 1
            exitwhen it != null or index > 5
        endloop
        
        if it != null then
            call UnitDropItemTarget( u , it , LoadUnitHandle( hash , id , StringHash("shop") )  )
            set it = null
        endif
        
        if index > 5 then
            call GroupRemoveUnit( units , u )
            call FlushChildHashtable( hash , id )
            set sellers = sellers - 1
            if sellers == 0 then
                call PauseTimer(loopTimer)
            endif
        else
            call SaveInteger( hash , id , StringHash("index") , index )
        endif
        
        set u = null
    endfunction
    
    
    private function loopUnits takes nothing returns nothing
        call ForGroup( units , function LoopSelling )
    endfunction
    
    
    private function StartSelling takes unit whichUnit , unit whichShop returns nothing
        if not(IsUnitInGroup( whichUnit , units )) then
            call GroupAddUnit ( units , whichUnit )
            call SaveUnitHandle( hash , GetHandleId(whichUnit) , StringHash("shop") , whichShop )
            set sellers = sellers + 1
            if sellers == 1 then
                call TimerStart( loopTimer , TIMEOUT , true , function loopUnits )
            endif
        endif
    endfunction
    
    
    private function SetUp takes nothing returns nothing
        call StartSelling( gg_unit_Hpal_0003 , gg_unit_ngme_0043 )
    endfunction


    private function Init_Sell takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerAddAction( t , function SetUp )
        call TriggerRegisterPlayerEvent(t , Player(0) , EVENT_PLAYER_END_CINEMATIC)
    endfunction
    
endlibrary

This works nicely for me.

The setup could set the x and y of the seller next to the shop.
 
Level 6
Joined
Oct 14, 2010
Messages
58
Maker; hehe, its very big big big big system, rly

I make it soo:
JASS:
unit gg_unit_n002_0015 - shop

function Diod takes unit u returns nothing
    local item Item
    local real x = GetUnitX(gg_unit_n002_0015)
    local real y = GetUnitY(gg_unit_n002_0015)
    local integer i = 0
    call SetUnitPosition(u,x,y)
    loop
        set Item = UnitItemInSlot(u,i)
        if Item != null
           call UnitDropItemTarget(u,Item,gg_unit_n002_0015)
           call TriggerSleepAction(0.00) //Time for uncrease unit facing
           exitwhen Item == null
        else
            set i = i + 1
            exitwhen i > 5
        endif
    endloop
    call UnitDropItemTarget(u,UnitItemInSlot(u,1),gg_unit_n002_0015)
    call UnitDropItemTarget(u,UnitItemInSlot(u,2),gg_unit_n002_0015)
    call UnitDropItemTarget(u,UnitItemInSlot(u,3),gg_unit_n002_0015)
    call UnitDropItemTarget(u,UnitItemInSlot(u,4),gg_unit_n002_0015)
    call UnitDropItemTarget(u,UnitItemInSlot(u,5),gg_unit_n002_0015)
    set Item = null
    set u = null
endfunction

Works not bad, function in Russian Style :D

Thanks for answers, friends
 
Level 6
Joined
Oct 14, 2010
Messages
58
Update version:
JASS:
unit gg_unit_n002_0015 - shop

function Diod takes unit u returns nothing
    local item Item
    local real x = GetUnitX(gg_unit_n002_0015)
    local real y = GetUnitY(gg_unit_n002_0015)
    local integer i = 0
    call SetUnitPosition(u,x,y)
    loop
        set Item = UnitItemInSlot(u,i)
        if Item != null
           call UnitDropItemTarget(u,Item,gg_unit_n002_0015)
           call TriggerSleepAction(0.00)
           exitwhen Item == null
        else
            set i = i + 1
            exitwhen i > 5
        endif
    endloop
    set Item = null
    set u = null
endfunction
 
Status
Not open for further replies.
Top