[Solved] JASS script random number between 1 and 10

Level 28
Joined
Dec 3, 2020
Messages
968
So I want the AI to launch an attack wave, but it will either send 2 Grunts, or 2 Raiders or 1 Grunt and 1 Raider to attack. It will have a 40% chance to send 2 Grunts, 30% to send 2 Raiders and 30% to send 1 Grunt and 1 Raider.
I mean that it will only send 1 of these 3 different possible options (if one of them happens then the others will not).

Would I do it inside the InitAssaultGroup() like this:

//* WAVE 1 *
call InitAssaultGroup()
// 40% Chance to send 2 Grunts
call CampaignAttackerEx( 2,2,2, GRUNT )
// 30% Chance to send 2 Raiders
call CampaignAttackerEx( 2,2,2, RAIDER )
// 30% Chance to send 1 Grunt and 1 Raider
call CampaignAttackerEx( 1,1,1, GRUNT )
call CampaignAttackerEx( 1,1,1, RAIDER )
call SuicideOnPlayer(M2,user)

Or before it like this:

// // 40% Chance to send 2 Grunts
//* WAVE 1 - a *
call InitAssaultGroup()
call CampaignAttackerEx( 2,2,2, GRUNT )
call SuicideOnPlayer(M2,user)

// 30% Chance to send 2 Raiders
//* WAVE 1 - b *
call InitAssaultGroup()
call CampaignAttackerEx( 2,2,2, RAIDER )
call SuicideOnPlayer(M2,user)

// 30% Chance to send 1 Grunt and 1 Raider
//* WAVE 1 - c *
call InitAssaultGroup()
call CampaignAttackerEx( 1,1,1, GRUNT )
call CampaignAttackerEx( 1,1,1, RAIDER )
call SuicideOnPlayer(M2,user)

This is important to know, although I'm sure it doesn't matter THAT much.

My real question is how do I exactly write the code in JASS?

Example:

if (random integer between 1 and 10 less than or equal to 4) {
//* WAVE 1 - a *
call InitAssaultGroup()
call CampaignAttackerEx( 2,2,2, GRUNT )
call SuicideOnPlayer(M2,user)
} else if (random integer between 1 and 10 less than or equal to 7) {
//* WAVE 1 - b *
call InitAssaultGroup()
call CampaignAttackerEx( 2,2,2, RAIDER )
call SuicideOnPlayer(M2,user)
} else {
//* WAVE 1 - c *
call InitAssaultGroup()
call CampaignAttackerEx( 1,1,1, GRUNT )
call CampaignAttackerEx( 1,1,1, RAIDER )
call SuicideOnPlayer(M2,user)
}

I don't know what I need to write in JASS for the if, else if, else statement to work properly
 
Last edited:
So I want the AI to launch an attack wave, but it will either send 2 Grunts, or 2 Raiders or 1 Grunt and 1 Raider to attack. It will have a 40% chance to send 2 Grunts, 30% to send 2 Raiders and 30% to send 1 Grunt and 1 Raider.
I mean that it will only send 1 of these 3 different possible options (if one of them happens then the others will not).

Would I do it inside the InitAssaultGroup() like this:

//* WAVE 1 *
call InitAssaultGroup()
// 40% Chance to send 2 Grunts
call CampaignAttackerEx( 2,2,2, GRUNT )
// 30% Chance to send 2 Raiders
call CampaignAttackerEx( 2,2,2, RAIDER )
// 30% Chance to send 1 Grunt and 1 Raider
call CampaignAttackerEx( 1,1,1, GRUNT )
call CampaignAttackerEx( 1,1,1, RAIDER )
call SuicideOnPlayer(M2,user)

Or before it like this:

// // 40% Chance to send 2 Grunts
//* WAVE 1 - a *
call InitAssaultGroup()
call CampaignAttackerEx( 2,2,2, GRUNT )
call SuicideOnPlayer(M2,user)

// 30% Chance to send 2 Raiders
//* WAVE 1 - b *
call InitAssaultGroup()
call CampaignAttackerEx( 2,2,2, RAIDER )
call SuicideOnPlayer(M2,user)

// 30% Chance to send 1 Grunt and 1 Raider
//* WAVE 1 - c *
call InitAssaultGroup()
call CampaignAttackerEx( 1,1,1, GRUNT )
call CampaignAttackerEx( 1,1,1, RAIDER )
call SuicideOnPlayer(M2,user)

This is important to know, although I'm sure it doesn't matter THAT much.

My real question is how do I exactly write the code in JASS?

Example:

if (random integer between 1 and 10 less than or equal to 4) {
//* WAVE 1 - a *
call InitAssaultGroup()
call CampaignAttackerEx( 2,2,2, GRUNT )
call SuicideOnPlayer(M2,user)
} else if (random integer between 1 and 10 less than or equal to 7) {
//* WAVE 1 - b *
call InitAssaultGroup()
call CampaignAttackerEx( 2,2,2, RAIDER )
call SuicideOnPlayer(M2,user)
} else {
//* WAVE 1 - c *
call InitAssaultGroup()
call CampaignAttackerEx( 1,1,1, GRUNT )
call CampaignAttackerEx( 1,1,1, RAIDER )
call SuicideOnPlayer(M2,user)
}

I don't know what I need to write in JASS for the if, else if, else statement to work properly

local integer r = GetRandomInt(1,10)
if r == 1 or r == 2 or r == 3 or r ==4 then

else
if r == 5 or r == 6 or r ==7 then

else
if r == 8 or r == 9 or r == 10 then

endif
endif
endif
 
Last edited:
Level 30
Joined
Aug 29, 2012
Messages
1,382
I believe it's something like that?

1736531925658.png
 
This should work
JASS:
local integer r = GetRandomInt(1, 10)

call InitAssaultGroup()

// 40% Chance to send 2 Grunts
if r < 5 then
	call CampaignAttackerEx(2, 2, 2, GRUNT)
	
// 30% Chance to send 2 Raiders
elseif r > 7 then
	call CampaignAttackerEx(2, 2, 2, RAIDER)
	
// 30% Chance to send 1 Grunt and 1 Raider
else
	call CampaignAttackerEx(1, 1, 1, GRUNT)
	call CampaignAttackerEx(1, 1, 1, RAIDER)
endif

call SuicideOnPlayer(M2, user)
 
Last edited:
Level 28
Joined
Dec 3, 2020
Messages
968
Good, thank you @loktar and @Archaos !

And if I want to make the number random each time, do I call it again after the endif?

like this:


// 30% Chance to send 1 Grunt and 1 Raider
else
call CampaignAttackerEx(1, 1, 1, GRUNT)
call CampaignAttackerEx(1, 1, 1, RAIDER)
endif

r = GetRandomInt(1, 10)

call SuicideOnPlayer(M2, user)

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

Worst case scenario I can just make 10-15 random integers (r1, r2, r3...), 1 for each attack.
(I assume it should technically work but I am being extra doubtful since it's JASS lol)
 
Last edited:
And if I want to make the number random each time, do I call it again after the endif?
Not sure what you mean exactly. If you want to randomize it for subsequent attacks you would do:

JASS:
local integer r = GetRandomInt(1, 10)

call InitAssaultGroup()

// 40% Chance to send 2 Grunts
if r < 5 then
	call CampaignAttackerEx(2, 2, 2, GRUNT)
	
// 30% Chance to send 2 Raiders
elseif r > 7 then
	call CampaignAttackerEx(2, 2, 2, RAIDER)
	
// 30% Chance to send 1 Grunt and 1 Raider
else
	call CampaignAttackerEx(1, 1, 1, GRUNT)
	call CampaignAttackerEx(1, 1, 1, RAIDER)
endif

call SuicideOnPlayer(M2, user)

set r = GetRandomInt(1, 10)
call InitAssaultGroup()

// 40% Chance to send 2 Grunts
if r < 5 then
	call CampaignAttackerEx(2, 2, 2, GRUNT)
	
// 30% Chance to send 2 Raiders
elseif r > 7 then
	call CampaignAttackerEx(2, 2, 2, RAIDER)
	
// 30% Chance to send 1 Grunt and 1 Raider
else
	call CampaignAttackerEx(1, 1, 1, GRUNT)
	call CampaignAttackerEx(1, 1, 1, RAIDER)
endif

call SuicideOnPlayer(M2, user)

or something like
JASS:
local integer r
local integer index = 0

loop
	set r = GetRandomInt(1, 10)
	call InitAssaultGroup()

	// 40% Chance to send 2 Grunts
	if r < 5 then
		call CampaignAttackerEx(2, 2, 2, GRUNT)
	
	// 30% Chance to send 2 Raiders
	elseif r > 7 then
		call CampaignAttackerEx(2, 2, 2, RAIDER)
	
	// 30% Chance to send 1 Grunt and 1 Raider
	else
		call CampaignAttackerEx(1, 1, 1, GRUNT)
		call CampaignAttackerEx(1, 1, 1, RAIDER)
	endif

	call SuicideOnPlayer(M2, user)

	set index = index+1
	exitwhen index > 4 // repeats 5 times
endloop
 
Level 28
Joined
Dec 3, 2020
Messages
968
Not sure what you mean exactly. If you want to randomize it for subsequent attacks you would do:

JASS:
local integer r = GetRandomInt(1, 10)

call InitAssaultGroup()

// 40% Chance to send 2 Grunts
if r < 5 then
    call CampaignAttackerEx(2, 2, 2, GRUNT)
   
// 30% Chance to send 2 Raiders
elseif r > 7 then
    call CampaignAttackerEx(2, 2, 2, RAIDER)
   
// 30% Chance to send 1 Grunt and 1 Raider
else
    call CampaignAttackerEx(1, 1, 1, GRUNT)
    call CampaignAttackerEx(1, 1, 1, RAIDER)
endif

call SuicideOnPlayer(M2, user)

set r = GetRandomInt(1, 10)
call InitAssaultGroup()

// 40% Chance to send 2 Grunts
if r < 5 then
    call CampaignAttackerEx(2, 2, 2, GRUNT)
   
// 30% Chance to send 2 Raiders
elseif r > 7 then
    call CampaignAttackerEx(2, 2, 2, RAIDER)
   
// 30% Chance to send 1 Grunt and 1 Raider
else
    call CampaignAttackerEx(1, 1, 1, GRUNT)
    call CampaignAttackerEx(1, 1, 1, RAIDER)
endif

call SuicideOnPlayer(M2, user)

or something like
JASS:
local integer r
local integer index = 0

loop
    set r = GetRandomInt(1, 10)
    call InitAssaultGroup()

    // 40% Chance to send 2 Grunts
    if r < 5 then
        call CampaignAttackerEx(2, 2, 2, GRUNT)
   
    // 30% Chance to send 2 Raiders
    elseif r > 7 then
        call CampaignAttackerEx(2, 2, 2, RAIDER)
   
    // 30% Chance to send 1 Grunt and 1 Raider
    else
        call CampaignAttackerEx(1, 1, 1, GRUNT)
        call CampaignAttackerEx(1, 1, 1, RAIDER)
    endif

    call SuicideOnPlayer(M2, user)

    set index = index+1
    exitwhen index > 4 // repeats 5 times
endloop
Perfect!!! Exactly what I meant!
Thread solved.
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
Adding this because I'm annoying, but I prefer checking RNG like this:
vJASS:
local integer r = GetRandomInt(1, 10)

if r <= 4 then // 40 percent chance
    call CampaignAttackerEx(2, 2, 2, GRUNT)

elseif r <= 7 then // 30 percent chance
    call CampaignAttackerEx(2, 2, 2, RAIDER)

else // 30 percent chance
    call CampaignAttackerEx(1, 1, 1, GRUNT)
    call CampaignAttackerEx(1, 1, 1, RAIDER)
endif
It makes more sense to ME*, to compare the differences this way (4 -> 7 = 3).

Then when working with Reals to get more precision:
vJASS:
local real r = GetRandomReal(0, 1)

if r <= 0.45 then // 45 percent chance
    call CampaignAttackerEx(2, 2, 2, GRUNT)

elseif r <= 0.80 then // 35 percent chance
    call CampaignAttackerEx(2, 2, 2, RAIDER)

else // 20 percent chance
    call CampaignAttackerEx(1, 1, 1, GRUNT)
    call CampaignAttackerEx(1, 1, 1, RAIDER)
endif
 
Last edited:
Level 28
Joined
Dec 3, 2020
Messages
968
Adding this because I'm annoying, but I prefer checking RNG like this:
vJASS:
local integer r = GetRandomInt(1, 10)

if r <= 4 then // 40 percent chance
    call CampaignAttackerEx(2, 2, 2, GRUNT)

elseif r <= 7 then // 30 percent chance
    call CampaignAttackerEx(2, 2, 2, RAIDER)

else // 30 percent chance
    call CampaignAttackerEx(1, 1, 1, GRUNT)
    call CampaignAttackerEx(1, 1, 1, RAIDER)
endif
It makes more sense to compare the differences this way (4 -> 7 = 3).

Then when working with Reals to get more precision:
vJASS:
local real r = GetRandomReal(0, 1)

if r <= 0.45 then // 45 percent chance
    call CampaignAttackerEx(2, 2, 2, GRUNT)

elseif r <= 0.80 then // 35 percent chance
    call CampaignAttackerEx(2, 2, 2, RAIDER)

else // 20 percent chance
    call CampaignAttackerEx(1, 1, 1, GRUNT)
    call CampaignAttackerEx(1, 1, 1, RAIDER)
endif
Thank you, Uncle!
 
Level 28
Joined
Dec 3, 2020
Messages
968
So I updated the AI script with these randomized attack waves and the AI doesn't seem to work at all. Not even defending or re-training its lost units.
Here is the script:

JASS:
//============================================================================
//  Human 02 -- pink player -- AI Script
//============================================================================
globals
    player user = Player(0)
endglobals

//============================================================================
//  main
//============================================================================
function main takes nothing returns nothing

    call CampaignAI('hhou',null)
    call SetReplacements(3,3,3)
    call SetCaptainHome(DEFENSE_CAPTAIN,-3689,4275)
    call SetCaptainHome(ATTACK_CAPTAIN,-3377,4142)

    call CampaignDefenderEx( 1,1,1, 'n600'    ) // footman archer
    call CampaignDefenderEx( 2,2,2, 'hfoo'        ) // footman



    //*** WAVE 1 ***
    local integer r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 2,2,2, 'hkni'       )
    else // 30%
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
    call CampaignAttackerEx( 3,3,3, 'hfoo'       )
    endif
    call SuicideOnPlayer(M2,user)

    //*** WAVE 2 ***
    local integer r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    call CampaignAttackerEx( 2,2,2, 'n600' )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
    call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    else // 30%
    call CampaignAttackerEx( 4,4,4, 'hkni'       )
    endif
    call SuicideOnPlayer(M2,user)

    //*** WAVE 3 ***
    local integer r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 5,5,5, 'hfoo'        )    
    call CampaignAttackerEx( 3,3,3, 'n600' )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 3,3,3, 'hkni'       )
    call CampaignAttackerEx( 3,3,3, 'n600'       )
    else // 30%
    call CampaignAttackerEx( 2,2,2, 'hkni'       )
    call CampaignAttackerEx( 5,5,5, 'hfoo'        )
    endif
    call SuicideOnPlayer(M3,user)

    //*** WAVE 4 ***
    local integer r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    call CampaignAttackerEx( 3,3,3, 'n600' )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 2,2,2, 'hkni'       )
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    else // 30%
    call CampaignAttackerEx( 5,5,5, 'hkni'       )
    endif
    call SuicideOnPlayer(M2,user)

    loop
        //*** WAVE 5 ***
    local integer r = GetRandomInt(1, 10)
        call InitAssaultGroup()
        if r <= 4 then // 40%
        call CampaignAttackerEx( 9,9,9, 'hfoo'       )
        elseif r <= 7 then // 30%
        call CampaignAttackerEx( 2,2,2, 'n600'       )
        call CampaignAttackerEx( 7,7,7, 'hfoo'       )
        else // 30%
        call CampaignAttackerEx( 3,3,3, 'hkni'       )
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    endif
        call SuicideOnPlayer(M3,user)

        //*** WAVE 6 ***
    local integer r = GetRandomInt(1, 10)
        call InitAssaultGroup()
        if r <= 4 then // 40%
        call CampaignAttackerEx( 5,5,5, 'hfoo'       )
        call CampaignAttackerEx( 4,4,4, 'n600' )
        elseif r <= 7 then // 30%
        call CampaignAttackerEx( 3,3,3, 'n600'       )
        call CampaignAttackerEx( 4,4,4, 'hfoo'       )
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
        else // 30%
        call CampaignAttackerEx( 3,3,3, 'hkni'       )
    call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    endif
        call SuicideOnPlayer(M2,user)

        //*** WAVE 7 ***
    local integer r = GetRandomInt(1, 10)
        call InitAssaultGroup()
        if r <= 4 then // 40%
        call CampaignAttackerEx( 7,7,7, 'hfoo'       )
        call CampaignAttackerEx( 3,3,3, 'n600' )
        elseif r <= 7 then // 30%
        call CampaignAttackerEx( 3,3,3, 'n600'       )
        call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
        else // 30%
        call CampaignAttackerEx( 2,2,2, 'hkni'       )
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    endif
        call SuicideOnPlayer(M3,user)
    endloop
endfunction

Am I missing out something very simple?
All I changed was the attack waves, the stuff before them is exactly the same.
 
Last edited:
Level 28
Joined
Dec 3, 2020
Messages
968
View attachment 515655

Seems there are problems with the local variables, not sure how to fix that though
Seems like the "local" keyword is giving an error. I will try removing it later.
In case I can't overwrite the "r" variable, I will just create a different variable on each attack wave. It's not a problem really, there aren't that many attack waves.
 
Level 19
Joined
Oct 17, 2012
Messages
859
The issue with the local variable is that

1) you have to declare them at the top of the function body and

2) any local variable with the same name can only be declared one time.

You can set this variable to something else later in the function body.


JASS:
//============================================================================
//  Human 02 -- pink player -- AI Script
//============================================================================
globals
    player user = Player(0)
endglobals

//============================================================================
//  main
//============================================================================
function main takes nothing returns nothing
    local integer r

    call CampaignAI('hhou',null)
    call SetReplacements(3,3,3)
    call SetCaptainHome(DEFENSE_CAPTAIN,-3689,4275)
    call SetCaptainHome(ATTACK_CAPTAIN,-3377,4142)

    call CampaignDefenderEx( 1,1,1, 'n600'    ) // footman archer
    call CampaignDefenderEx( 2,2,2, 'hfoo'        ) // footman



    //*** WAVE 1 ***
    set r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 2,2,2, 'hkni'       )
    else // 30%
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
    call CampaignAttackerEx( 3,3,3, 'hfoo'       )
    endif
    call SuicideOnPlayer(M2,user)

    //*** WAVE 2 ***
    set r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    call CampaignAttackerEx( 2,2,2, 'n600' )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
    call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    else // 30%
    call CampaignAttackerEx( 4,4,4, 'hkni'       )
    endif
    call SuicideOnPlayer(M2,user)

    //*** WAVE 3 ***
    set r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 5,5,5, 'hfoo'        )  
    call CampaignAttackerEx( 3,3,3, 'n600' )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 3,3,3, 'hkni'       )
    call CampaignAttackerEx( 3,3,3, 'n600'       )
    else // 30%
    call CampaignAttackerEx( 2,2,2, 'hkni'       )
    call CampaignAttackerEx( 5,5,5, 'hfoo'        )
    endif
    call SuicideOnPlayer(M3,user)

    //*** WAVE 4 ***
    set r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    call CampaignAttackerEx( 3,3,3, 'n600' )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 2,2,2, 'hkni'       )
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    else // 30%
    call CampaignAttackerEx( 5,5,5, 'hkni'       )
    endif
    call SuicideOnPlayer(M2,user)

    loop
        //*** WAVE 5 ***
        set r = GetRandomInt(1, 10)
        call InitAssaultGroup()
        if r <= 4 then // 40%
        call CampaignAttackerEx( 9,9,9, 'hfoo'       )
        elseif r <= 7 then // 30%
        call CampaignAttackerEx( 2,2,2, 'n600'       )
        call CampaignAttackerEx( 7,7,7, 'hfoo'       )
        else // 30%
        call CampaignAttackerEx( 3,3,3, 'hkni'       )
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    endif
        call SuicideOnPlayer(M3,user)

        //*** WAVE 6 ***
        set r = GetRandomInt(1, 10)
        call InitAssaultGroup()
        if r <= 4 then // 40%
        call CampaignAttackerEx( 5,5,5, 'hfoo'       )
        call CampaignAttackerEx( 4,4,4, 'n600' )
        elseif r <= 7 then // 30%
        call CampaignAttackerEx( 3,3,3, 'n600'       )
        call CampaignAttackerEx( 4,4,4, 'hfoo'       )
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
        else // 30%
        call CampaignAttackerEx( 3,3,3, 'hkni'       )
    call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    endif
        call SuicideOnPlayer(M2,user)

        //*** WAVE 7 ***
        set r = GetRandomInt(1, 10)
        call InitAssaultGroup()
        if r <= 4 then // 40%
        call CampaignAttackerEx( 7,7,7, 'hfoo'       )
        call CampaignAttackerEx( 3,3,3, 'n600' )
        elseif r <= 7 then // 30%
        call CampaignAttackerEx( 3,3,3, 'n600'       )
        call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
        else // 30%
        call CampaignAttackerEx( 2,2,2, 'hkni'       )
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    endif
        call SuicideOnPlayer(M3,user)
    endloop
endfunction
 
Last edited:
Level 28
Joined
Dec 3, 2020
Messages
968
The issue with the local variable is that

1) you have to declare them at the top of the function body and

2) any local variable with the same name can only be declared one time.

You can set this variable to something else later in the function body.


JASS:
//============================================================================
//  Human 02 -- pink player -- AI Script
//============================================================================
globals
    player user = Player(0)
endglobals

//============================================================================
//  main
//============================================================================
function main takes nothing returns nothing
    local integer r

    call CampaignAI('hhou',null)
    call SetReplacements(3,3,3)
    call SetCaptainHome(DEFENSE_CAPTAIN,-3689,4275)
    call SetCaptainHome(ATTACK_CAPTAIN,-3377,4142)

    call CampaignDefenderEx( 1,1,1, 'n600'    ) // footman archer
    call CampaignDefenderEx( 2,2,2, 'hfoo'        ) // footman



    //*** WAVE 1 ***
    set r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 2,2,2, 'hkni'       )
    else // 30%
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
    call CampaignAttackerEx( 3,3,3, 'hfoo'       )
    endif
    call SuicideOnPlayer(M2,user)

    //*** WAVE 2 ***
    set r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    call CampaignAttackerEx( 2,2,2, 'n600' )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
    call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    else // 30%
    call CampaignAttackerEx( 4,4,4, 'hkni'       )
    endif
    call SuicideOnPlayer(M2,user)

    //*** WAVE 3 ***
    set r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 5,5,5, 'hfoo'        ) 
    call CampaignAttackerEx( 3,3,3, 'n600' )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 3,3,3, 'hkni'       )
    call CampaignAttackerEx( 3,3,3, 'n600'       )
    else // 30%
    call CampaignAttackerEx( 2,2,2, 'hkni'       )
    call CampaignAttackerEx( 5,5,5, 'hfoo'        )
    endif
    call SuicideOnPlayer(M3,user)

    //*** WAVE 4 ***
    set r = GetRandomInt(1, 10)
    call InitAssaultGroup()
    if r <= 4 then // 40%
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    call CampaignAttackerEx( 3,3,3, 'n600' )
    elseif r <= 7 then // 30%
    call CampaignAttackerEx( 2,2,2, 'hkni'       )
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    else // 30%
    call CampaignAttackerEx( 5,5,5, 'hkni'       )
    endif
    call SuicideOnPlayer(M2,user)

    loop
        //*** WAVE 5 ***
        set r = GetRandomInt(1, 10)
        call InitAssaultGroup()
        if r <= 4 then // 40%
        call CampaignAttackerEx( 9,9,9, 'hfoo'       )
        elseif r <= 7 then // 30%
        call CampaignAttackerEx( 2,2,2, 'n600'       )
        call CampaignAttackerEx( 7,7,7, 'hfoo'       )
        else // 30%
        call CampaignAttackerEx( 3,3,3, 'hkni'       )
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    endif
        call SuicideOnPlayer(M3,user)

        //*** WAVE 6 ***
        set r = GetRandomInt(1, 10)
        call InitAssaultGroup()
        if r <= 4 then // 40%
        call CampaignAttackerEx( 5,5,5, 'hfoo'       )
        call CampaignAttackerEx( 4,4,4, 'n600' )
        elseif r <= 7 then // 30%
        call CampaignAttackerEx( 3,3,3, 'n600'       )
        call CampaignAttackerEx( 4,4,4, 'hfoo'       )
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
        else // 30%
        call CampaignAttackerEx( 3,3,3, 'hkni'       )
    call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    endif
        call SuicideOnPlayer(M2,user)

        //*** WAVE 7 ***
        set r = GetRandomInt(1, 10)
        call InitAssaultGroup()
        if r <= 4 then // 40%
        call CampaignAttackerEx( 7,7,7, 'hfoo'       )
        call CampaignAttackerEx( 3,3,3, 'n600' )
        elseif r <= 7 then // 30%
        call CampaignAttackerEx( 3,3,3, 'n600'       )
        call CampaignAttackerEx( 5,5,5, 'hfoo'       )
    call CampaignAttackerEx( 1,1,1, 'hkni'       )
        else // 30%
        call CampaignAttackerEx( 2,2,2, 'hkni'       )
    call CampaignAttackerEx( 6,6,6, 'hfoo'       )
    endif
        call SuicideOnPlayer(M3,user)
    endloop
endfunction
Worked perfectly, thank you!
 
Top