• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Trying to figure out how to make a basic AI

Status
Not open for further replies.
Level 3
Joined
Sep 29, 2010
Messages
14
ok so im trying to learn Campaign ai as i wanna make my own campaign.
So i took this template script and edited some what to try and have them attack the player who would be purple team. (reason why their all 60 seconds is for testing)

all they do is just send preplace units for the attack waves and dont build any and wont harvest lumber and if they just start with peasants and a town hall they build all the way up to a barracks dont harvest lumber and one peasant just stands there. Im very confused and would love some help just remember im new to Jass and a bit of a noob

JASS:
//******************************************************************************
//*                               Human Campaign AI                            *
//*                                 Bloody Stress                              *
//******************************************************************************
//*
globals
    player MyVictim = PlayerEx(4)
endglobals
 
function ConfigureAI takes nothing returns nothing
    call SetSlowChopping( true )
    call SetDefendPlayer( false )
    call SetRandomPaths( false )
    call SetTargetHeroes( true )
    call SetPeonsRepair( true )
    call SetHeroesFlee( false )
    call SetHeroesBuyItems( true )
    call SetUnitsFlee( false )
    call SetGroupsFlee( false )
    call SetWatchMegaTargets( true )
    call SetIgnoreInjured( true )
    call SetHeroesTakeItems( true )
    call SetSlowChopping( true )
    call SetCaptainChanges( false )
    call SetSmartArtillery( true )
endfunction
 
function hero_levels takes nothing returns integer
    local integer hero  = GetHeroId()
    local integer level = GetHeroLevelAI()
    local integer a = 0
    if hero == PALADIN then
        if level == 1 or level == 3 or level == 5 then
            set a = HOLY_BOLT
        endif
        if level == 2 or level == 4 or level == 7 then
            set a = DIVINE_SHIELD
        endif
        if level >= 8 then
            set a = DEVOTION_AURA
        endif
        if level == 6 then
            set a = RESURRECTION
        endif
    endif
    return a
endfunction

function HarvestPriorities takes nothing returns nothing
    local integer mine = TownWithMine()
    local integer allGold = GetUnitCountDone('hpea')
    local integer allWood = GetUnitCountDone('hpea')
    local integer numWorkers
    set numWorkers = 5
    call HarvestGold( 0, numWorkers )
    set numWorkers = 5
    call HarvestWood( 0, numWorkers )
    set numWorkers = 5
    call HarvestGold( 1, numWorkers )
    set numWorkers = 4
    call HarvestWood( 0, numWorkers )
endfunction
 
function main takes nothing returns nothing
    call CampaignAI( HOUSE, function hero_levels )
    call ConfigureAI( )     
    // **********************************
    // *      Building Strategy         *
    // **********************************
    //
    // Tier 1 Buildings
    call SetReplacements( 1, 2, 3 )
    call SetBuildUnitEx( 1, 1, 1, TOWN_HALL )
    call SetBuildUnit( 7, PEASANT )    
    call ClearHarvestAI(  )
    call HarvestPriorities(  )            
    call SetBuildUnitEx( 1, 2, 3, HOUSE )
    call SetBuildUnitEx( 1, 2, 3, BARRACKS )       
    call SetBuildUnit( 1, HUMAN_ALTAR ) 
    call SetBuildUnit( 1, LUMBER_MILL )  
    call SetBuildUnit( 1, BLACKSMITH )
    call CampaignDefenderEx( 3, 4, 5, FOOTMAN )
    call CampaignDefenderEx( 2, 2, 2, RIFLEMAN )
    call SetBuildUnitEx( 3, 4, 5, WATCH_TOWER )
    // Tier 2 buildings
    call SetBuildUnit( 1, KEEP )
    call SetBuildUnitEx( 1, 2, 2, SANCTUM )
    call SetBuildUnit( 1, WORKSHOP )
    // Tier 3 buildings
    call SetBuildUnit( 1, CASTLE )
    call SetBuildUnitEx( 0, 1, 1, ARCANE_VAULT )
    // **********************************
    // *    End Building Strategy       *
    // **********************************   
    // **********************************
    // *       Attack Strategy          *
    // **********************************
    //*** WAVE 1 *** AI will begin to attack in 5 minutes
    call InitAssaultGroup()
    call CampaignAttackerEx( 2, 3, 3, FOOTMAN )
    call CampaignAttackerEx( 0, 1, 2, RIFLEMAN ) 
    call SuicideOnPlayerEx( 30, 30, 30, MyVictim )
    call SetBuildUpgrEx( 1, 2, 3, UPG_MELEE )
    call SetBuildUpgrEx( 1, 2, 3, UPG_RANGED )
    call SetBuildUpgrEx( 1, 2, 3, UPG_LEATHER )
    //*** WAVE 2 *** Between 2 or 3 minutes after Wave 1
    call InitAssaultGroup()
    call CampaignAttackerEx( 3, 4, 4, FOOTMAN )
    call CampaignAttackerEx( 1, 2, 3, RIFLEMAN ) 
    call SuicideOnPlayerEx( 60, 60, 60, MyVictim )
    //*** WAVE 3 *** Between 2 or 3 minutes after Wave 2
    call InitAssaultGroup()
    call CampaignAttackerEx( 3, 4, 4, FOOTMAN )
    call CampaignAttackerEx( 1, 2, 3, RIFLEMAN )
    call CampaignAttackerEx( 1, 1, 1, PALADIN )
    call SuicideOnPlayerEx( 60, 60, 60, MyVictim )
    //*** WAVE 4 *** Between 2 or 3 minutes after Wave 3
    call InitAssaultGroup()
    call CampaignAttackerEx( 3, 4, 4, FOOTMAN )
    call CampaignAttackerEx( 1, 2, 3, RIFLEMAN )
    call CampaignAttackerEx( 0, 1, 1, PRIEST )
    call CampaignAttackerEx( 1, 1, 1, PALADIN )
    call SuicideOnPlayerEx( 60, 60, 60, MyVictim )
    call SetBuildUpgrEx( 1, 1, 1, UPG_SORCERY )
    //*** WAVE 5 *** Between 3 or 4 minutes after Wave 4
    call InitAssaultGroup()
    call CampaignAttackerEx( 3, 3, 3, FOOTMAN )
    call CampaignAttackerEx( 2, 2, 2, RIFLEMAN )
    call CampaignAttackerEx( 1, 1, 1, PALADIN )
    call CampaignAttackerEx( 0, 1, 2, PRIEST )
    call SuicideOnPlayerEx( 60, 60, 60, MyVictim )
    //*** WAVE 6 *** Between 3 or 4 minutes after Wave 5
    call InitAssaultGroup()                      
    call CampaignAttackerEx( 3, 3, 3, FOOTMAN )
    call CampaignAttackerEx( 2, 2, 2, RIFLEMAN )
    call CampaignAttackerEx( 1, 1, 1, PALADIN )
    call CampaignAttackerEx( 1, 2, 3, PRIEST )
    call CampaignAttackerEx( 0, 1, 2, SORCERESS )
    call SuicideOnPlayerEx( 60, 60, 60, MyVictim )
    //*** chimaera Upgrade
    call SetBuildUpgrEx( 0, 0, 1, UPG_BREEDING )
    loop //Init the infinite attack loop
    //*** WAVE 7 *** Between 3 or 5 minutes after Wave 6
        call InitAssaultGroup()                               
        call CampaignAttackerEx( 2, 2, 2, FOOTMAN )       
        call CampaignAttackerEx( 2, 2, 2, RIFLEMAN )
        call CampaignAttackerEx( 1, 1, 1, PALADIN )
        call CampaignAttackerEx( 1, 2, 3, PRIEST )
        call CampaignAttackerEx( 0, 1, 2, SORCERESS )
        call CampaignAttackerEx( 0, 1, 2, MORTAR )
        call SuicideOnPlayerEx( 60, 60, 60, MyVictim )
        //*** WAVE 8 *** Between 3 or 5 minutes after Wave 7
        call InitAssaultGroup()                               
        call CampaignAttackerEx( 2, 2, 2, FOOTMAN )   
        call CampaignAttackerEx( 2, 2, 2, RIFLEMAN )
        call CampaignAttackerEx( 1, 1, 1, PALADIN )
        call CampaignAttackerEx( 1, 2, 3, PRIEST )
        call CampaignAttackerEx( 0, 1, 2, SORCERESS )
        call SuicideOnPlayerEx( 60, 60, 60, MyVictim )
        //*** WAVE 9 *** Between 4 or 6 minutes after Wave 8
        call InitAssaultGroup()                               
        call CampaignAttackerEx( 2, 2, 2, FOOTMAN )   
        call CampaignAttackerEx( 2, 2, 2, RIFLEMAN )
        call CampaignAttackerEx( 1, 1, 1, PALADIN )
        call CampaignAttackerEx( 1, 2, 3, PRIEST )
        call CampaignAttackerEx( 0, 1, 2, SORCERESS )
        call CampaignAttackerEx( 0, 1, 2, MORTAR )
        call CampaignAttackerEx( 1, 1, 2, KNIGHT )
        call SuicideOnPlayerEx( 60, 60, 60, MyVictim )
    endloop
    // **********************************
    // *   Attack Strategy never ends   *
    // **********************************
endfunction
 
Level 18
Joined
Mar 16, 2008
Messages
721
If some of the script is running but the workers aren't harvesting, that is a good sign I think, it only means the worker assignment function isn't being called. The way the GUI AI Editor handles this is with a thread, which keeps running certain functions. I'm not an expert but try adding this to the script.

This is pretty much copy pasted from GUI AI Editor with somethings removed:
JASS:
//===========================================================================
// Determines all building and harvesting assignments for workers
//===========================================================================
function WorkerAssignment takes nothing returns nothing
    loop

        // Harvesting
        call ClearHarvestAI(  )
        call HarvestPriorities(  )

        call Sleep( 2 )
    endloop
endfunction

//***************************************************************************
//*
//*  Main Entry Point
//*
//***************************************************************************

//===========================================================================
function main takes nothing returns nothing
    call InitAI(  )
    call SetPlayerName( ai_player, "Bloody Stress AI" )
    call InitOptions(  )
    call SetHeroLevels( function ChooseHeroSkill )

    call Sleep( 0.1 )
    call StartThread( function WorkerAssignment )  /// this will keep running the worker assignments as long as the AI is playing
    call PlayGame(  )
endfunction

not sure if everything in the main function is necessary but you definitely want to include "call StartThread( function WorkerAssignment )" specifically to solve your worker problem.

Wish I could help you more but this is pretty much the limit of my knowledge on this topic.
 
Last edited:
Status
Not open for further replies.
Top