• 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.

[vJASS] optimizing tow minigame

Status
Not open for further replies.
hello i made a tug of war minigame it works very good and smooth just wondering if there is anything i can improve on ? also note tht the trigger is normally off and it is executed and turned on in another trigger. the winner function is also not completed. and i plan on turning it into a bracket style game (tournament style). i use lightning as a rope currently looking to see what lightning looks the best. well here is the code thx for ur comments.
JASS:
scope TugOfWar initializer TugofWar

globals
    trigger trg_TugOfWar
    unit array TOWunit
    private real array unitPos
    private integer counter = 0
    private region left
    private region right
    private lightning Lightning1
    private lightning Lightning2
endglobals

private function settingUnits takes nothing returns nothing
    local integer p = 0
    local real array x
    local real array y
    set x[0] = GetRectCenterX( gg_rct_TugOfWar_SidelinesP0)
    set y[0] = GetRectCenterY( gg_rct_TugOfWar_SidelinesP0)
    set x[1] = GetRectCenterX( gg_rct_TugOfWar_SidelinesP1)
    set y[1] = GetRectCenterY( gg_rct_TugOfWar_SidelinesP1)
    set x[2] = GetRectCenterX( gg_rct_TugOfWar_SidelinesP2)
    set y[2] = GetRectCenterY( gg_rct_TugOfWar_SidelinesP2)
    set x[3] = GetRectCenterX( gg_rct_TugOfWar_SidelinesP3)
    set y[3] = GetRectCenterY( gg_rct_TugOfWar_SidelinesP3)
    loop
        exitwhen p > 3
        call SetUnitX( TOWunit, x)
        call SetUnitY( TOWunit, y)
        call SetUnitFacing( TOWunit, 270)
        set p = p + 1
    endloop
endfunction

private function winner takes integer p returns nothing
    call textall( "Player " + I2S(p+1) + " has won the game.")
    call settingUnits()
endfunction

private function getunitinregionleft takes integer p returns integer
    local integer L = 0
    loop 
        exitwhen L > 3
        if IsUnitOwnedByPlayer( TOWunit[L], Player(L)) and IsUnitInRegion( left, TOWunit[L]) then
            return L
        endif
        set L = L + 1
    endloop
    return 11
endfunction

private function getunitinregionright takes integer p returns integer
    local integer L = 0
    loop 
        exitwhen L > 3
        if IsUnitOwnedByPlayer( TOWunit[L], Player(L)) and IsUnitInRegion( right, TOWunit[L]) then
            return L
        endif
        set L = L + 1
    endloop
    return 11
endfunction

private function Actions takes nothing returns nothing
    local integer p = GetPlayerId( GetTriggerPlayer())
    local integer l = getunitinregionleft( p)
    local integer r = getunitinregionright( p)
    local real Real = 16 // use to set distance moved per click
    if p == l then
        set Real = -Real
    elseif p == r then
    endif
    set unitPos[l] = unitPos[l] + Real
    set unitPos[r] = unitPos[r] + Real
    call SetUnitX( TOWunit[l], unitPos[l])
    call SetUnitX( TOWunit[r], unitPos[r])
    call MoveLightning( Lightning1, true, unitPos[l], 1920, unitPos[r], 1920)
    call MoveLightning( Lightning2, true, unitPos[r], 1920, unitPos[l], 1920)
    if unitPos[l] >= 15744 or unitPos[r] <= 15744 then
        call winner( p)
    endif
endfunction

private function setup takes nothing returns nothing
    local real lx = 15488
    local real rx = 16000
    local real y = GetRectCenterY( gg_rct_TugOfWarCenter)
    if counter == 0 then
        set Lightning1 = AddLightning( "LEAS", true, lx, y, rx, y)
        set Lightning2 = AddLightning( "LEAS", true, rx, y, lx, y)
        set counter = 1
        call SetUnitX( TOWunit[0], lx)
        call SetUnitY( TOWunit[0], y)
        call SetUnitX( TOWunit[1], rx)
        call SetUnitY( TOWunit[1], y)
        call SetUnitFacing( TOWunit[0], 0)
        call SetUnitFacing( TOWunit[1], 180)
    else
        call Actions()
    endif
endfunction

private function setupUnits takes nothing returns nothing
    local real tx = GetRectCenterX( gg_rct_TugOfWarCenter) //15744
    local real ty = GetRectCenterY( gg_rct_TugOfWarCenter) //1920
    local destructable d = CreateDestructable( 'B000', tx, ty, 0, 1, 1)
    set left = CreateRegion()
    call RegionAddRect( left, gg_rct_TugOfWarLeft)
    set right = CreateRegion()
    call RegionAddRect( right, gg_rct_TugOfWarRight)
    set TOWunit[0] = gg_unit_hpea_0128
    set TOWunit[1] = gg_unit_hpea_0129
    set TOWunit[2] = gg_unit_hpea_0130
    set TOWunit[3] = gg_unit_hpea_0131
    set unitPos[0] = 15488
    set unitPos[1] = 16000
    call SetDestructableInvulnerable( d, true)
    call settingUnits()
    set d = null
endfunction

//===========================================================================
function TugofWar takes nothing returns nothing
    set trg_TugOfWar = CreateTrigger()
    call setupUnits()
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(0), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(1), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(2), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(3), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerAddAction( trg_TugOfWar, function setup )
endfunction

endscope

JASS:
scope TugOfWar initializer TugofWar

globals
    trigger trg_TugOfWar
    unit array TOWunit
    private real array unitPos
    private integer counter = 0
    private region left
    private region right
    private lightning Lightning1
    private lightning Lightning2
    private rect array TOW_Rects
endglobals

private function rectSetting takes nothing returns nothing
    set TOW_Rects[0] = gg_rct_TugOfWar_SidelinesP0
    set TOW_Rects[1] = gg_rct_TugOfWar_SidelinesP1
    set TOW_Rects[2] = gg_rct_TugOfWar_SidelinesP2
    set TOW_Rects[3] = gg_rct_TugOfWar_SidelinesP3
endfunction

private function settingUnits takes nothing returns nothing
    local integer p = 0
    set unitPos[0] = 15488
    set unitPos[1] = 16000
    set counter = 0
    loop
        exitwhen p > 3
        call SetUnitX( TOWunit, GetRectCenterX( TOW_Rects))
        call SetUnitY( TOWunit, GetRectCenterY( TOW_Rects))
        call SetUnitFacing( TOWunit, 270)
        set p = p + 1
    endloop
endfunction

private function winner takes integer p returns nothing
    call textall( "Player " + I2S(p+1) + " has won the game.", p)
    call settingUnits()
    call pauseall( false)
endfunction

private function getunitinregionleft takes integer p returns integer
    local integer L = 0
    loop 
        exitwhen L > 3
        if IsUnitOwnedByPlayer( TOWunit[L], Player(L)) and IsUnitInRegion( left, TOWunit[L]) then
            return L
        endif
        set L = L + 1
    endloop
    return 11
endfunction

private function getunitinregionright takes integer p returns integer
    local integer L = 0
    loop 
        exitwhen L > 3
        if IsUnitOwnedByPlayer( TOWunit[L], Player(L)) and IsUnitInRegion( right, TOWunit[L]) then
            return L
        endif
        set L = L + 1
    endloop
    return 11
endfunction

private function Actions takes nothing returns nothing
    local integer p = GetPlayerId( GetTriggerPlayer())
    local integer l = getunitinregionleft( p)
    local integer r = getunitinregionright( p)
    local real Real = 16 // use to set distance moved per click
    if p == l then
        set Real = -Real
    elseif p == r then
    endif
    set unitPos[l] = unitPos[l] + Real
    set unitPos[r] = unitPos[r] + Real
    call SetUnitX( TOWunit[l], unitPos[l])
    call SetUnitX( TOWunit[r], unitPos[r])
    call MoveLightning( Lightning1, true, unitPos[l], 1920, unitPos[r], 1920)
    call MoveLightning( Lightning2, true, unitPos[r], 1920, unitPos[l], 1920)
    if unitPos[l] >= 15744 or unitPos[r] <= 15744 then
        call winner( p)
    endif
endfunction

private function setup takes nothing returns nothing
    local real lx = 15488
    local real rx = 16000
    local real y = GetRectCenterY( gg_rct_TugOfWarCenter)
    if counter == 0 then
        set Lightning1 = AddLightning( "LEAS", true, lx, y, rx, y)
        set Lightning2 = AddLightning( "LEAS", true, rx, y, lx, y)
        set counter = 1
        call SetUnitX( TOWunit[0], lx)
        call SetUnitY( TOWunit[0], y)
        call SetUnitX( TOWunit[1], rx)
        call SetUnitY( TOWunit[1], y)
        call SetUnitFacing( TOWunit[0], 0)
        call SetUnitFacing( TOWunit[1], 180)
    else
        call Actions()
    endif
endfunction

private function setupUnits takes nothing returns nothing
    local real tx = GetRectCenterX( gg_rct_TugOfWarCenter) //15744
    local real ty = GetRectCenterY( gg_rct_TugOfWarCenter) //1920
    local destructable d = CreateDestructable( 'B000', tx, ty, 0, 1, 1)
    set left = CreateRegion()
    call RegionAddRect( left, gg_rct_TugOfWarLeft)
    set right = CreateRegion()
    call RegionAddRect( right, gg_rct_TugOfWarRight)
    set TOWunit[0] = gg_unit_hpea_0128
    set TOWunit[1] = gg_unit_hpea_0129
    set TOWunit[2] = gg_unit_hpea_0130
    set TOWunit[3] = gg_unit_hpea_0131
    call SetDestructableInvulnerable( d, true)
    call settingUnits()
    set d = null
endfunction

//===========================================================================
function TugofWar takes nothing returns nothing
    set trg_TugOfWar = CreateTrigger()
    call rectSetting()
    call setupUnits()
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(0), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(1), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(2), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(3), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerAddAction( trg_TugOfWar, function setup )
endfunction

endscope
 
Last edited:
Level 4
Joined
Jan 27, 2010
Messages
133
Looking good!

I would consider using wrappers to get rid of the hard-coded pre-set rects and units. It would use more code, but the editor is a bit more friendly with triggers when it comes to pre-sets.

JASS:
// Hardcoded, difficult to manage
// Compilation errors on removal, have to find out new hard-coded id on change...
gg_rct_TugOfWar_SidelinesP0

JASS:
globals
    private rect sidelineP0
endglobals

function TOW_SetSidelineP0 takes rect r returns nothing
    set sidelineP0 = r
endfunction

  • Actions
    • Set TempRect = (My Region <gen>)
    • Custom Script: call TOW_SetSidelineP0(udg_TempRect)
 
thx for the comments. and ok i think ik what u mean by wrappers but i would still have to use them to set them tho right ? umm i changed it to this hope its what u meant but i didnt do the takes rect r i thought this might be better

JASS:
scope TugOfWar initializer TugofWar

globals
    trigger trg_TugOfWar
    unit array TOWunit
    private real array unitPos
    private integer counter = 0
    private region left
    private region right
    private lightning Lightning1
    private lightning Lightning2
    private rect array TOW_Rects
endglobals

private function rectSetting takes nothing returns nothing
    set TOW_Rects[0] = gg_rct_TugOfWar_SidelinesP0
    set TOW_Rects[1] = gg_rct_TugOfWar_SidelinesP1
    set TOW_Rects[2] = gg_rct_TugOfWar_SidelinesP2
    set TOW_Rects[3] = gg_rct_TugOfWar_SidelinesP3
endfunction

private function settingUnits takes nothing returns nothing
    local integer p = 0
    set unitPos[0] = 15488
    set unitPos[1] = 16000
    set counter = 0
    loop
        exitwhen p > 3
        call SetUnitX( TOWunit, GetRectCenterX( TOW_Rects))
        call SetUnitY( TOWunit, GetRectCenterY( TOW_Rects))
        call SetUnitFacing( TOWunit, 270)
        set p = p + 1
    endloop
endfunction

private function winner takes integer p returns nothing
    call textall( "Player " + I2S(p+1) + " has won the game.", p)
    call settingUnits()
    call pauseall( false)
endfunction

private function getunitinregionleft takes integer p returns integer
    local integer L = 0
    loop 
        exitwhen L > 3
        if IsUnitOwnedByPlayer( TOWunit[L], Player(L)) and IsUnitInRegion( left, TOWunit[L]) then
            return L
        endif
        set L = L + 1
    endloop
    return 11
endfunction

private function getunitinregionright takes integer p returns integer
    local integer L = 0
    loop 
        exitwhen L > 3
        if IsUnitOwnedByPlayer( TOWunit[L], Player(L)) and IsUnitInRegion( right, TOWunit[L]) then
            return L
        endif
        set L = L + 1
    endloop
    return 11
endfunction

private function Actions takes nothing returns nothing
    local integer p = GetPlayerId( GetTriggerPlayer())
    local integer l = getunitinregionleft( p)
    local integer r = getunitinregionright( p)
    local real Real = 16 // use to set distance moved per click
    if p == l then
        set Real = -Real
    elseif p == r then
    endif
    set unitPos[l] = unitPos[l] + Real
    set unitPos[r] = unitPos[r] + Real
    call SetUnitX( TOWunit[l], unitPos[l])
    call SetUnitX( TOWunit[r], unitPos[r])
    call MoveLightning( Lightning1, true, unitPos[l], 1920, unitPos[r], 1920)
    call MoveLightning( Lightning2, true, unitPos[r], 1920, unitPos[l], 1920)
    if unitPos[l] >= 15744 or unitPos[r] <= 15744 then
        call winner( p)
    endif
endfunction

private function setup takes nothing returns nothing
    local real lx = 15488
    local real rx = 16000
    local real y = GetRectCenterY( gg_rct_TugOfWarCenter)
    if counter == 0 then
        set Lightning1 = AddLightning( "LEAS", true, lx, y, rx, y)
        set Lightning2 = AddLightning( "LEAS", true, rx, y, lx, y)
        set counter = 1
        call SetUnitX( TOWunit[0], lx)
        call SetUnitY( TOWunit[0], y)
        call SetUnitX( TOWunit[1], rx)
        call SetUnitY( TOWunit[1], y)
        call SetUnitFacing( TOWunit[0], 0)
        call SetUnitFacing( TOWunit[1], 180)
    else
        call Actions()
    endif
endfunction

private function setupUnits takes nothing returns nothing
    local real tx = GetRectCenterX( gg_rct_TugOfWarCenter) //15744
    local real ty = GetRectCenterY( gg_rct_TugOfWarCenter) //1920
    local destructable d = CreateDestructable( 'B000', tx, ty, 0, 1, 1)
    set left = CreateRegion()
    call RegionAddRect( left, gg_rct_TugOfWarLeft)
    set right = CreateRegion()
    call RegionAddRect( right, gg_rct_TugOfWarRight)
    set TOWunit[0] = gg_unit_hpea_0128
    set TOWunit[1] = gg_unit_hpea_0129
    set TOWunit[2] = gg_unit_hpea_0130
    set TOWunit[3] = gg_unit_hpea_0131
    call SetDestructableInvulnerable( d, true)
    call settingUnits()
    set d = null
endfunction

//===========================================================================
function TugofWar takes nothing returns nothing
    set trg_TugOfWar = CreateTrigger()
    call rectSetting()
    call setupUnits()
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(0), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(1), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(2), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerRegisterPlayerEvent( trg_TugOfWar, Player(3), EVENT_PLAYER_ARROW_UP_DOWN)
    call TriggerAddAction( trg_TugOfWar, function setup )
endfunction

endscope
 
Last edited:
Status
Not open for further replies.
Top