• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

moveing goods trigger help

Status
Not open for further replies.
Level 40
Joined
Dec 14, 2005
Messages
10,532
I did a system exactly like that for a friend a little while ago.

The code would need to be configured for your purpose (could be done later), but here's how it looks;

(Uses I2U, H2I, and a global game cache called GameCache)

JASS:
function ConvoyReachesTargetConds takes nothing returns boolean
    return GetSpellAbilityId() == 'A003'
endfunction

function ConvoyReachesTarget takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local player p = GetOwningPlayer(u)
    local string s = I2S(H2I(u))
    local unit u2 = I2U(GetStoredInteger(udg_GameCache,s,"sender"))
    local unit u3 = I2U(GetStoredInteger(udg_GameCache,s,"target"))
    if GetOwningPlayer(u3) != p then
        call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)+R2I(GetUnitState(u,UNIT_STATE_MANA)*.75))
        call SetPlayerState(GetOwningPlayer(u3),PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(GetOwningPlayer(u3),PLAYER_STATE_RESOURCE_GOLD)+R2I(GetUnitState(u,UNIT_STATE_MANA)/4))
    else
        call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)+R2I(GetUnitState(u,UNIT_STATE_MANA)/4))
    endif
    if GetWidgetLife(u2) > 0 and GetWidgetLife(u3) > 0 then
        call SendConvoy(p,u2,u3)
    endif
    call FlushStoredMission(udg_GameCache,s)
    call RemoveUnit(u)
    set u = null
    set u2 = null
    set u3 = null
endfunction

function InitTrig_ConvoyReachesTarget takes nothing returns nothing
    set gg_trg_ConvoyReachesTarget = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(gg_trg_ConvoyReachesTarget,EVENT_PLAYER_UNIT_SPELL_CAST)
    call TriggerAddCondition(gg_trg_ConvoyReachesTarget,Condition(function ConvoyReachesTargetConds))
    call TriggerAddAction(gg_trg_ConvoyReachesTarget,function ConvoyReachesTarget)
endfunction

function SetConvoyDestinationConds takes nothing returns boolean
    return GetSpellAbilityId() == 'A004'
endfunction

function SetConvoyDestination takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local player p = GetOwningPlayer(u)
    if GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD) > 299 and GetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER) > 9 then
        call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)-300)
        call SetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER,GetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER)-10)
        call SendConvoy(p,u,GetSpellTargetUnit())
    endif
    set u = null
endfunction

function InitTrig_SetConvoyDestination takes nothing returns nothing
    set gg_trg_SetConvoyDestination = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(gg_trg_SetConvoyDestination,EVENT_PLAYER_UNIT_SPELL_EFFECT)
    call TriggerAddCondition(gg_trg_SetConvoyDestination,Condition(function SetConvoyDestinationConds))
    call TriggerAddAction(gg_trg_SetConvoyDestination,function SetConvoyDestination)
endfunction

function ReorderConvoyConds takes nothing returns boolean
    return GetUnitTypeId(GetTriggerUnit()) == 'h00Z' and GetIssuedOrderId() == 851971
endfunction

function ReorderConvoy takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local unit u2 = I2U(GetStoredInteger(udg_GameCache,I2S(H2I(u)),"target"))
    call PauseUnit(u,true)
    call IssueImmediateOrder(u,"stop")
    call PauseUnit(u,false)
    call IssueImmediateOrder(u,"stop")
    if GetWidgetLife(u2) > 0 then
        call IssueTargetOrder(u,"cripple",u2)
    endif
    set u = null
    set u2 = null
endfunction

function InitTrig_ReorderConvoy takes nothing returns nothing
    set gg_trg_ReorderConvoy = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(gg_trg_ReorderConvoy,EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER)
    call TriggerRegisterAnyUnitEventBJ(gg_trg_ReorderConvoy,EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER)
    call TriggerAddCondition(gg_trg_ReorderConvoy,Condition(function ReorderConvoyConds))
    call TriggerAddAction(gg_trg_ReorderConvoy,function ReorderConvoy)
endfunction

function ConvoyDeathConds takes nothing returns boolean
    return GetUnitTypeId(GetTriggerUnit()) == 'h00Z'
endfunction

function ConvoyDeath takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local player p = GetOwningPlayer(u)
    local string s = I2S(H2I(u))
    local unit u2 = I2U(GetStoredInteger(udg_GameCache,s,"sender"))
    local unit u3 = I2U(GetStoredInteger(udg_GameCache,s,"target"))
    if GetWidgetLife(u2) > 0 and GetWidgetLife(u3) > 0 and u3 == I2U(GetStoredInteger(udg_GameCache,I2S(H2I(u2)),"target")) then
        call SendConvoy(p,u2,u3)
    endif
    call FlushStoredMission(udg_GameCache,s)
    set u = null
    set u2 = null
    set u3 = null
endfunction

function CleanupUselessConvoysFilter takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit()) == 'h00Z' and GetUnitCurrentOrder(GetFilterUnit()) != 852189
endfunction

function CleanupUselessConvoys takes nothing returns nothing
    local unit u
    call GroupEnumUnitsInRect(udg_enumGrp,bj_mapInitialPlayableArea,Filter(function CleanupUselessConvoysFilter))
    loop
        set u = FirstOfGroup(udg_enumGrp)
        exitwhen u == null
        call FlushStoredMission(udg_GameCache,I2S(H2I(u)))
        call RemoveUnit(u)
        call GroupRemoveUnit(udg_enumGrp,u)
    endloop
endfunction

function InitTrig_CleanupUselessConvoys takes nothing returns nothing
    set gg_trg_CleanupUselessConvoys = CreateTrigger()
    call TriggerRegisterTimerEvent(gg_trg_CleanupUselessConvoys,1,true)
    call TriggerAddAction(gg_trg_CleanupUselessConvoys,function CleanupUselessConvoys)
endfunction

~Moved to Spells And Systems
 
Last edited:
Level 40
Joined
Dec 14, 2005
Messages
10,532
Now that I've got a second, I'll explain it --

The script contains five triggers:

  • ConvoyReachesTarget - This trigger detects when the convoy reaches its destination point, which is tracked by the convoy having been ordered to cast an ability on the target building. The trigger then gives the gold, and sends a new convoy (A convoy is sent every time the previous one reached the target)

  • SetConvoyDestination - This trigger starts sending convoys.

  • ReorderConvoy - Since convoys are selectable (so they can be killed), but should not be controllable by the player, the Ward classification is given to them, and this trigger blocks right-click orders.

  • ConvoyDeath - This trigger is an anti-leak trigger to get rid of the attached data from dying convoys

  • CleanupUselessConvoys - This trigger periodically finds convoys that are not doing their job (ie their target is removed) and removes them.

This system needs a few configuration changes at the moment -

  • Gold given - the current gold given is determined withing ConvoyReachesTarget, with
    JASS:
        if GetOwningPlayer(u3) != p then
            call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)+R2I(GetUnitState(u,UNIT_STATE_MANA)*.75))
            call SetPlayerState(GetOwningPlayer(u3),PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(GetOwningPlayer(u3),PLAYER_STATE_RESOURCE_GOLD)+R2I(GetUnitState(u,UNIT_STATE_MANA)/4))
        else
            call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)+R2I(GetUnitState(u,UNIT_STATE_MANA)/4))
        endif

    (Note that it gives less if the player owning the convoy is trading with themself, you may want to also have that changed)

  • Ability and unit IDs, and orderstrings - You would need to specifically configure all ability and unit IDs as well as orderstrings used by the system to whatever they are in your map

  • Sending system - For example, if you want to send convoys periodically as opposed to when one reaches its destination, or if you don't want it to cost money to set up a route

    Also, currently the convoys cost gold to send, in the following block in the SetConvoyDestination trigger:

    JASS:
        if GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD) > 299 and GetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER) > 9 then
            call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)-300)
            call SetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER,GetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER)-10)
            call SendConvoy(p,u,GetSpellTargetUnit())
        endif

I added some easy configuration settings to the code, tell me if you need other configurability options.

JASS:
//Constants
constant function ConvoyAbilityOrderString takes nothing returns string
    return "cripple" //orderstring of the ability the convoys cast on the target markets
endfunction

constant function ConvoyUnitTypeId takes nothing returns integer
    return 'h00Z' //unit id of the convoy
endfunction

constant function ConvoyAbilityId takes nothing returns integer
    return 'A003' //ability id of the ability the convoys cast on the target markets
endfunction

constant function ConvoySendAbilityId takes nothing returns integer
    return 'A004' //ability id of the ability to establish a trade route
endfunction

//Map Header
function H2I takes handle h returns integer
    return h
    return 0
endfunction

function I2U takes integer i returns unit
    return i
    return null
endfunction

function SendConvoy takes player p, unit send, unit targ returns nothing
    local unit u = CreateUnit(p,ConvoyUnitTypeId(),GetUnitX(send),GetUnitY(send),0)
    local string s = I2S(H2I(u))
    call IssueTargetOrder(u,ConvoyAbilityOrderString(),targ)
    call StoreInteger(udg_GameCache,s,"sender",H2I(send))
    call StoreInteger(udg_GameCache,s,"target",H2I(targ))
    set u = null
endfunction 

//Triggers

function ConvoyReachesTargetConds takes nothing returns boolean
    return GetSpellAbilityId() == ConvoyAbilityId()
endfunction

function ConvoyReachesTarget takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local player p = GetOwningPlayer(u)
    local string s = I2S(H2I(u))
    local unit u2 = I2U(GetStoredInteger(udg_GameCache,s,"sender"))
    local unit u3 = I2U(GetStoredInteger(udg_GameCache,s,"target"))
    if GetOwningPlayer(u3) != p then
        call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)+R2I(GetUnitState(u,UNIT_STATE_MANA)*.75))
        call SetPlayerState(GetOwningPlayer(u3),PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(GetOwningPlayer(u3),PLAYER_STATE_RESOURCE_GOLD)+R2I(GetUnitState(u,UNIT_STATE_MANA)/4))
    else
        call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)+R2I(GetUnitState(u,UNIT_STATE_MANA)/4))
    endif
    if GetWidgetLife(u2) > 0 and GetWidgetLife(u3) > 0 then
        call SendConvoy(p,u2,u3)
    endif
    call FlushStoredMission(udg_GameCache,s)
    call RemoveUnit(u)
    set u = null
    set u2 = null
    set u3 = null
endfunction

function InitTrig_ConvoyReachesTarget takes nothing returns nothing
    set gg_trg_ConvoyReachesTarget = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(gg_trg_ConvoyReachesTarget,EVENT_PLAYER_UNIT_SPELL_CAST)
    call TriggerAddCondition(gg_trg_ConvoyReachesTarget,Condition(function ConvoyReachesTargetConds))
    call TriggerAddAction(gg_trg_ConvoyReachesTarget,function ConvoyReachesTarget)
endfunction

function SetConvoyDestinationConds takes nothing returns boolean
    return GetSpellAbilityId() == ConvoySendAbilityId()
endfunction

function SetConvoyDestination takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local player p = GetOwningPlayer(u)
    if GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD) > 299 and GetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER) > 9 then
        call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD)-300)
        call SetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER,GetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER)-10)
        call SendConvoy(p,u,GetSpellTargetUnit())
    endif
    set u = null
endfunction

function InitTrig_SetConvoyDestination takes nothing returns nothing
    set gg_trg_SetConvoyDestination = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(gg_trg_SetConvoyDestination,EVENT_PLAYER_UNIT_SPELL_EFFECT)
    call TriggerAddCondition(gg_trg_SetConvoyDestination,Condition(function SetConvoyDestinationConds))
    call TriggerAddAction(gg_trg_SetConvoyDestination,function SetConvoyDestination)
endfunction

function ReorderConvoyConds takes nothing returns boolean
    return GetUnitTypeId(GetTriggerUnit()) == ConvoyUnitTypeId() and GetIssuedOrderId() == 851971
endfunction

function ReorderConvoy takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local unit u2 = I2U(GetStoredInteger(udg_GameCache,I2S(H2I(u)),"target"))
    call PauseUnit(u,true)
    call IssueImmediateOrder(u,"stop")
    call PauseUnit(u,false)
    call IssueImmediateOrder(u,"stop")
    if GetWidgetLife(u2) > 0 then
        call IssueTargetOrder(u,ConvoyAbilityOrderString(),u2)
    endif
    set u = null
    set u2 = null
endfunction

function InitTrig_ReorderConvoy takes nothing returns nothing
    set gg_trg_ReorderConvoy = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(gg_trg_ReorderConvoy,EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER)
    call TriggerRegisterAnyUnitEventBJ(gg_trg_ReorderConvoy,EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER)
    call TriggerAddCondition(gg_trg_ReorderConvoy,Condition(function ReorderConvoyConds))
    call TriggerAddAction(gg_trg_ReorderConvoy,function ReorderConvoy)
endfunction

function ConvoyDeathConds takes nothing returns boolean
    return GetUnitTypeId(GetTriggerUnit()) == ConvoyUnitTypeId()
endfunction

function ConvoyDeath takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local player p = GetOwningPlayer(u)
    local string s = I2S(H2I(u))
    local unit u2 = I2U(GetStoredInteger(udg_GameCache,s,"sender"))
    local unit u3 = I2U(GetStoredInteger(udg_GameCache,s,"target"))
    if GetWidgetLife(u2) > 0 and GetWidgetLife(u3) > 0 and u3 == I2U(GetStoredInteger(udg_GameCache,I2S(H2I(u2)),"target")) then
        call SendConvoy(p,u2,u3)
    endif
    call FlushStoredMission(udg_GameCache,s)
    set u = null
    set u2 = null
    set u3 = null
endfunction

function CleanupUselessConvoysFilter takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit()) == ConvoyUnitTypeId() and GetUnitCurrentOrder(GetFilterUnit()) != 852189
endfunction

function CleanupUselessConvoys takes nothing returns nothing
    local unit u
    call GroupEnumUnitsInRect(udg_enumGrp,bj_mapInitialPlayableArea,Filter(function CleanupUselessConvoysFilter))
    loop
        set u = FirstOfGroup(udg_enumGrp)
        exitwhen u == null
        call FlushStoredMission(udg_GameCache,I2S(H2I(u)))
        call RemoveUnit(u)
        call GroupRemoveUnit(udg_enumGrp,u)
    endloop
endfunction

function InitTrig_CleanupUselessConvoys takes nothing returns nothing
    set gg_trg_CleanupUselessConvoys = CreateTrigger()
    call TriggerRegisterTimerEvent(gg_trg_CleanupUselessConvoys,1,true)
    call TriggerAddAction(gg_trg_CleanupUselessConvoys,function CleanupUselessConvoys)
endfunction
 
Status
Not open for further replies.
Top