• 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 minigame trigger

Status
Not open for further replies.
hello i have this for a minigame i dont think i forgot anything just wondering if i could change something to make it faster / more efficient thanks to anyone tht helps

note : i did not make the respawn portion i may make tht in another trigger might be easier i didnt decide tht yet tho

JASS:
scope CheckersMoving initializer Init_Checkers_Commands

globals
    trigger trg_Checekrs_Commands // not sure if i need this or not
    private integer Int
    private integer c
    private string chat
    private integer g1
    private integer g2
    private integer g3
    private integer p
    private string OorE
    private unit u1
    private unit u2
endglobals

private function Kings takes nothing returns nothing
    call SetUnitScale( u1, 200, 200, 200 )
    call SetUnitUserData( u1, 2000 )
endfunction

private function bPlayer takes nothing returns nothing
    local integer p1
    if p == 0 then
        set p1 = 1
        if g2 >= 29 then
            call Kings()
        endif
    elseif p == 1 then
        set p1 = 0
        if g2 <= 4 then
            call Kings()
        endif
    endif
    set b_Checkers_Player = false
    set b_Checkers_Player[p1] = true
endfunction

private function move1 takes nothing returns nothing
    call GroupRemoveUnit( CheckersG[g1], u1 )
    call GroupAddUnit( CheckersG[g2], u1 )
    set Checkers_Boolean_Reg[g1] = true
    set Checkers_Boolean_Reg[g2] = false
    call PauseUnit( u1, false )
    call IssuePointOrder( u1, "move", CheckersX[g2], CheckersY[g2] )
    call PolledWait( 1.00 )
    call PauseUnit( u1, true )
    set CheckersCounter = 0
    call bPlayer()
endfunction

private function move2 takes nothing returns nothing
    call GroupRemoveUnit( CheckersG[g1], u1 )
    call GroupRemoveUnit( CheckersG[g3], u2 )
    call GroupAddUnit( CheckersG[g2], u1 )
    set Checkers_Boolean_Reg[g1] = true
    set Checkers_Boolean_Reg[g3] = true
    set Checkers_Boolean_Reg[g2] = false
    call PauseUnit( u1, false )
    call IssuePointOrder( u1, "attackmove", CheckersX[g2], CheckersY[g2] )
    call PolledWait( 2.00 )
    call PauseUnit( u1, true )
    set g1 = g2
    call bPlayer()
endfunction

private function Checkers_Checking takes nothing returns nothing
    local integer i
    set u1 = FirstOfGroup( CheckersG[g1] )
    set i = GetUnitUserData( u1 )
    if OorE == "odd" then
        set g3 = R2I( (I2R( g1 + g2 ) / 2.0) - 0.5 )
        set u2 = FirstOfGroup( CheckersG[g3] )
    elseif OorE == "even" then
        set g3 = R2I( (I2R( g1 + g2 ) / 2.0) + 0.5 )
        set u2 = FirstOfGroup( CheckersG[g3] )
    endif
    if g1 < g2 and IsUnitAlly( u1, Player(p) ) == true and Checkers_Boolean_Reg[g2] == true then
        call move1()
    elseif g1 < g2 and IsUnitAlly( u1, Player(p) ) == true and IsUnitEnemy( u2, Player(p) ) and Checkers_Boolean_Reg[g2] == true then
        call move2()
    elseif g1 > g2 and i == 2000 and IsUnitAlly( u1, Player(p) ) == true and Checkers_Boolean_Reg[g2] == true then
        call move1()
    elseif g1 > g2 and i == 2000 and IsUnitAlly( u1, Player(p) ) == true and IsUnitEnemy( u2, Player(p) ) and Checkers_Boolean_Reg[g2] == true then
        call move2()
    elseif g1 == g2 then
        set g1 = 0
        set g2 = 0
        set CheckersCounter = 0
        call DisplayTextToForce( GetPlayersAll(), "Cant Enter Same Coordinates Twice" )
    endif
endfunction

private function Checkers_Actions takes nothing returns nothing
    if CheckersCounter == 0 then
        set c = 0
        set g1 = Int
        set CheckersCounter = 1
    elseif CheckersCounter == 1 then
        set c = 1
        set g2 = Int
        call Checkers_Checking()
    endif
endfunction

private function Checkers_Chats_Setup takes nothing returns nothing
    set chat = GetEventPlayerChatString()
    set p = GetPlayerId( GetTriggerPlayer() )
    if b_Checkers_Player == true then
        if chat == "A1" then
            set Int = 1
            set OorE = "odd"
        elseif chat == "A3" then
            set Int = 2
            set OorE = "odd"
        elseif chat == "A5" then
            set Int = 3
            set OorE = "odd"
        elseif chat == "A7" then
            set Int = 4
            set OorE = "odd"
        elseif chat == "B2" then
            set Int = 5
            set OorE = "even"
        elseif chat == "B4" then
            set Int = 6
            set OorE = "even"
        elseif chat == "B6" then
            set Int = 7
            set OorE = "even"
        elseif chat == "B8" then
            set Int = 8
            set OorE = "even"
        elseif chat == "C1" then
            set Int = 9
            set OorE = "odd"
        elseif chat == "C3" then
            set Int = 10
            set OorE = "odd"
        elseif chat == "C5" then
            set Int = 11
            set OorE = "odd"
        elseif chat == "C7" then
            set Int = 12
            set OorE = "odd"
        elseif chat == "D2" then
            set Int = 13
            set OorE = "even"
        elseif chat == "D4" then
            set Int = 14
            set OorE = "even"
        elseif chat == "D6" then
            set Int = 15
            set OorE = "even"
        elseif chat == "D8" then
            set Int = 16
            set OorE = "even"
        elseif chat == "E1" then
            set Int = 17
            set OorE = "odd"
        elseif chat == "E3" then
            set Int = 18
            set OorE = "odd"
        elseif chat == "E5" then
            set Int = 19
            set OorE = "odd"
        elseif chat == "E7" then
            set Int = 20
            set OorE = "odd"
        elseif chat == "F2" then
            set Int = 21
            set OorE = "even"
        elseif chat == "F4" then
            set Int = 22
            set OorE = "even"
        elseif chat == "F6" then
            set Int = 23
            set OorE = "even"
        elseif chat == "F8" then
            set Int = 24
            set OorE = "even"
        elseif chat == "G1" then
            set Int = 25
            set OorE = "odd"
        elseif chat == "G3" then
            set Int = 26
            set OorE = "odd"
        elseif chat == "G5" then
            set Int = 27
            set OorE = "odd"
        elseif chat == "G7" then
            set Int = 28
            set OorE = "odd"
        elseif chat == "H2" then
            set Int = 29
            set OorE = "even"
        elseif chat == "H4" then
            set Int = 30
            set OorE = "even"
        elseif chat == "H6" then
            set Int = 31
            set OorE = "even"
        elseif chat == "H8" then
            set Int = 32
            set OorE = "even"
        elseif chat == "END" then
            set CheckersCounter = 0
        endif
    endif
    if Int > 0 then
        call Checkers_Actions()
    endif
endfunction

//===========================================================================
private function Init_Checkers_Commands takes nothing returns nothing
    local trigger t
    local integer i
    set i = 0
    set t = CreateTrigger()
    loop
        exitwhen i > 1
        call TriggerRegisterPlayerChatEvent( t, Player(i), "A1", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "A3", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "A5", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "A7", true )
        
        call TriggerRegisterPlayerChatEvent( t, Player(i), "B2", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "B4", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "B6", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "B8", true )
        
        call TriggerRegisterPlayerChatEvent( t, Player(i), "C1", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "C3", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "C5", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "C7", true )
        
        call TriggerRegisterPlayerChatEvent( t, Player(i), "D2", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "D4", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "D6", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "D8", true )
        
        call TriggerRegisterPlayerChatEvent( t, Player(i), "E1", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "E3", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "E5", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "E7", true )
        
        call TriggerRegisterPlayerChatEvent( t, Player(i), "F2", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "F4", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "F6", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "F8", true )
        
        call TriggerRegisterPlayerChatEvent( t, Player(i), "G1", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "G3", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "G5", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "G7", true )
        
        call TriggerRegisterPlayerChatEvent( t, Player(i), "H2", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "H4", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "H6", true )
        call TriggerRegisterPlayerChatEvent( t, Player(i), "H8", true )
        
        call TriggerRegisterPlayerChatEvent( t, Player(i), "END", true )
        set i = i + 1
    endloop
    call TriggerAddAction( t, function Checkers_Chats_Setup )
    set t = null
endfunction

endscope
 
Level 4
Joined
Jan 27, 2010
Messages
133
Unless I'm entirely mistaken you are making some sort of chess implementation. You are running it over Warcraft III, which has a lot of 3D-graphics, pathfinding and unit attack/notification going on. So, in order for it to run, I can safely assume you target computers with more than 20 MB ram, and more than 500 MHz. You are not making an AI (which could indeed be subject to optimization), but are simply implementing a TURN based game on top of previously mentioned Warcraft engine.

Please. Do. Not. Ask. For. Efficiency.

----------------------------------------------------------------

Your problem here will never be speed; although the code structure could be improved.

Generally you should strive to clearly separate logic from data. In this case, you have hardcoded "A1", "A2"... multiple times in the code. This could be solved with a struct:

JASS:
globals
    private constant integer SQUARE_COUNT=32
    private SquareData array squares
   // I think this can also be solved by letting the struct extending array,
  // but let's keep it simple, shall we? :)
endglobals

struct SquareData
    string id
    boolean even
    integer nr

    static method create takes string id, boolean even, integer nr returns SquareData
        local SquareData sd = SquareData.allocate()
        set sd.id = id
        set sd.even = even
        set sd.nr = nr
        return sd
    endmethod

    method getOorE takes nothing returns string
        if this.even == true then
            return "even"
        endif
        return "odd"
    endmethod
endstruct

JASS:
private function Checkers_Chats_Setup takes nothing returns nothing
    local integer i = 1

    set chat = GetEventPlayerChatString()
    set p = GetPlayerId( GetTriggerPlayer() )
    if b_Checkers_Player == true then
        loop
            exitwhen i>SQUARE_COUNT
            exitwhen squares[i].id == chat
            set i=i+1
        endloop

        if i<=SQUARE_COUNT then
            set Int = squares[i].nr
            set OorE = squares[i].getOorE()
            call Checkers_Actions()
        endif

JASS:
local integer j

// All data collected here...

set squares[1] = SquareData.create("A1",false,1)
set squares[2] = SquareData.create("A2",true,2)
set squares[3] = SquareData.create("A3",false,3)
//.... etc.

set j=1
    loop
         exitwhen j>SQUARE_COUNT
         call TriggerRegisterPlayerChatEvent( t, Player(0), squares[j].id, true )
         call TriggerRegisterPlayerChatEvent( t, Player(1), squares[j].id, true )
         set j=j+1
    endloop

Now, this can be even MORE structured...

We could make a wrapper function for initializing the data, so that we don't have to enter the number both in [] and as argument.

We could define private globals boolean ODD and EVEN, and consistently use those. It's unnecessary to use strings all the time.
 
Status
Not open for further replies.
Top