InsaneMonster
Hosted Project: W3RR
- Joined
- Jul 20, 2011
- Messages
- 503
Hi all fellow coders!
I want to share an AI script (JASS) which I made for my current project, which you can find on my signature. The script is used on the second map for the Alliance Naval Base forces.
Why could it be interesting? I add a behaviour to command the AI to stop producing a certain unit and spare them if possible once a certain condition is met. In game this is done to fake the blacksmith prerequisite for the rifleman. It also supports command (and data) to start/stop attack waves.
Nothing special really, but since I found quite the shortage of working AI scripts sample I thought that could be useful to someone willing to code an AI.
Beside that, sharing this, I hope some veteran could also partecipate in the discussion, share some tips and things like that
Cheers and keep up the good work everyone!
I want to share an AI script (JASS) which I made for my current project, which you can find on my signature. The script is used on the second map for the Alliance Naval Base forces.
Why could it be interesting? I add a behaviour to command the AI to stop producing a certain unit and spare them if possible once a certain condition is met. In game this is done to fake the blacksmith prerequisite for the rifleman. It also supports command (and data) to start/stop attack waves.
Nothing special really, but since I found quite the shortage of working AI scripts sample I thought that could be useful to someone willing to code an AI.
Beside that, sharing this, I hope some veteran could also partecipate in the discussion, share some tips and things like that
JASS:
//============================================================================
// RR Prologue 02 -- Alliance Blue Human -- AI Script
// by InsaneMonster (Luca Pasqualini)
//============================================================================
// GLOBALS
//------------------------------------------------
globals
player User = PlayerEx(1)
boolean UseRiflemen = false
boolean AttackEnabled = false
constant integer COMMAND_RIFLEMEN = 1
constant integer DATA_RIFLEMEN_ENABLE = 1
constant integer DATA_RIFLEMEN_DISABLE = 0
constant integer COMMAND_ATTACK = 2
constant integer DATA_ATTACK_ENABLE = 1
constant integer DATA_ATTACK_DISABLE = 0
endglobals
// BUILD ORDER
//------------------------------------------------
function SetBuildOrder takes nothing returns nothing
call SetBuildUnit(2, PEASANT)
endfunction
// DEFENDERS
//------------------------------------------------
function SetDefenders takes nothing returns nothing
if UseRiflemen then
call CampaignDefenderEx(1, 2, 3, FOOTMAN)
call CampaignDefenderEx(0, 1, 2, RIFLEMAN)
else
call CampaignDefenderEx(1, 2, 3, FOOTMAN)
endif
endfunction
// ATTACK LOOP
//------------------------------------------------
function AttackWavesWithRiflemen takes nothing returns nothing
local boolean AttackWaveUseRiflemen = UseRiflemen
loop
// *** WAVE 1 ***
call InitAssaultGroup()
call CampaignAttackerEx(2, 3, 4, FOOTMAN)
call SuicideOnPlayerEx(M6, M5, M4, User)
exitwhen AttackWaveUseRiflemen != UseRiflemen
// *** WAVE 2 ***
call InitAssaultGroup()
call CampaignAttackerEx(2, 3, 4, RIFLEMAN)
call SuicideOnPlayerEx(M6, M5, M4, User)
exitwhen AttackWaveUseRiflemen != UseRiflemen
// *** WAVE 3 ***
call InitAssaultGroup()
call CampaignAttackerEx(1, 2, 2, FOOTMAN)
call CampaignAttackerEx(1, 1, 2, RIFLEMAN)
call SuicideOnPlayerEx(M6, M5, M4, User)
exitwhen AttackWaveUseRiflemen != UseRiflemen
endloop
endfunction
function AttackWavesWithoutRiflemen takes nothing returns nothing
local boolean AttackWaveUseRiflemen = UseRiflemen
loop
// *** WAVE 1 ***
call InitAssaultGroup()
call CampaignAttackerEx(2, 3, 4, FOOTMAN)
call SuicideOnPlayerEx(M6, M5, M4, User)
exitwhen AttackWaveUseRiflemen != UseRiflemen
endloop
endfunction
function AttackLoop takes nothing returns nothing
loop
// Attack only if attack is enabled
if AttackEnabled then
// Attack with riflemen or not depending on the current condition
if UseRiflemen then
call AttackWavesWithRiflemen()
else
call AttackWavesWithoutRiflemen()
endif
endif
// Wait a little time before looping again
call Sleep(10)
endloop
endfunction
// COMMAND LOOP
//------------------------------------------------
function CommandFetch takes nothing returns nothing
local integer Command
local integer Data
// Check if there is any command waiting
if CommandsWaiting() > 0 then
// Get command and data
set Command = GetLastCommand()
set Data = GetLastData()
call PopLastCommand()
// Update rifleman behaviour command (to simulate having a blacksmith)
if Command == COMMAND_RIFLEMEN then
if Data == DATA_RIFLEMEN_ENABLE then
set UseRiflemen = true
elseif Data == DATA_RIFLEMEN_DISABLE then
set UseRiflemen = false
else
call DisplayTextToPlayer(User, 0, 0, "AI Warning: Invalid riflemen command data received!")
endif
// Reset build order and defenders
call InitBuildArray()
call InitDefenseGroup()
call SetBuildOrder()
call SetDefenders()
// Update attack behaviour command (to start/stop attack waves)
elseif Command == COMMAND_ATTACK then
if Data == DATA_ATTACK_ENABLE then
set AttackEnabled = true
elseif Data == DATA_ATTACK_DISABLE then
set AttackEnabled = false
else
call DisplayTextToPlayer(User, 0, 0, "AI Warning: Invalid attack command data received!")
endif
else
call DisplayTextToPlayer(User, 0, 0, "AI Warning: Unknown command received!")
endif
endif
endfunction
function CommandLoop takes nothing returns nothing
// Keep fetching commands while the AI is active
call CommandFetch()
call StaggerSleep(1, 5)
loop
call CommandFetch()
call Sleep(2)
endloop
endfunction
// MAIN
//------------------------------------------------
function main takes nothing returns nothing
call CampaignAI(LUMBER_MILL, null)
// Define AI properties
call DoCampaignFarms(false)
call SetReplacements(0, 1, 2)
// Define build order
call SetBuildOrder()
// Define defenders
call SetDefenders()
// Start command loop for external signals
call StartThread(function CommandLoop)
// Launch attack waves as soon as possible (it depends on the commands received)
call AttackLoop()
endfunction
Cheers and keep up the good work everyone!
Last edited: