|
//===========================================================================
//
// 只是另外一个魔兽争霸III的AI脚本
//
// Warcraft III AI script
// Generated by the Warcraft III World Editor
// Date: Sat Feb 15 18:41:14 2014
//
//===========================================================================
//***************************************************************************
//*
//* Global Variables
//*
//***************************************************************************
#define USE_BJ_ANTI_LEAK
#include <YDTrigger/Import.h>
#include <YDTrigger/YDTrigger.h>
globals
integer attackWave = 1
integer nextDelay = 0
integer awGold = 0
integer awWood = 0
#include <YDTrigger/Globals.h>
endglobals
#include <YDTrigger/Function.h>
//***************************************************************************
//*
//* Utility Functions
//*
//***************************************************************************
//===========================================================================
function CheckLastCommand takes boolean pop returns integer
local integer cmd = GetLastCommand()
if (pop) then
call PopLastCommand( )
endif
return cmd
endfunction
//===========================================================================
function CheckLastCommandData takes boolean pop returns integer
local integer data = GetLastData()
if (pop) then
call PopLastCommand( )
endif
return data
endfunction
//===========================================================================
function TotalFoodProduced takes nothing returns integer
return GetPlayerState(ai_player,PLAYER_STATE_RESOURCE_FOOD_CAP)
endfunction
//===========================================================================
function ExpansionNeeded takes nothing returns boolean
return take_exp
endfunction
//===========================================================================
function BuildExpansion takes integer hallID, integer mineID returns nothing
if (HallsCompleted(hallID)) then
call SetBuildExpa( TownCount(hallID) + 1, mineID )
endif
endfunction
//===========================================================================
function CurrentAttackWave takes nothing returns integer
return attackWave
endfunction
//===========================================================================
function ResetAttackUnits takes nothing returns nothing
set awGold = 0
set awWood = 0
call InitAssaultGroup( )
endfunction
//===========================================================================
function AddAttackUnit takes integer minQty, integer maxQty, integer unitID returns nothing
// Track attacking gold workers
if (unitID == 'hpea') then
set awGold = awGold + minQty
endif
// Track attacking wood workers
if (unitID == 'hpea') then
set awWood = awWood + minQty
endif
call SetAssaultGroup( minQty, maxQty, unitID )
endfunction
//***************************************************************************
//*
//* Basic Options
//*
//***************************************************************************
//===========================================================================
function InitOptions takes nothing returns nothing
call SetCampaignAI( )
call SetDefendPlayer( false )
call SetRandomPaths( false )
call SetTargetHeroes( false )
call SetPeonsRepair( false )
call SetHeroesFlee( false )
call SetHeroesBuyItems( false )
call SetUnitsFlee( false )
call SetGroupsFlee( false )
call SetWatchMegaTargets( false )
call SetIgnoreInjured( false )
call SetHeroesTakeItems( false )
call SetSlowChopping( false )
call SetCaptainChanges( false )
call SetSmartArtillery( false )
endfunction
//***************************************************************************
//*
//* Conditions
//*
//***************************************************************************
//===========================================================================
// Updates the values of all preset conditions
//===========================================================================
function UpdateConditions takes nothing returns nothing
endfunction
//***************************************************************************
//*
//* Heroes
//*
//***************************************************************************
//===========================================================================
// Stores hero ID and skills
//===========================================================================
function SetHero takes integer order, integer heroid returns nothing
if (order == 1) then
set hero_id = heroid
if (heroid == 'Hpal') then
set skills1[ 1] = 'AHhb'
set skills1[ 2] = 'AHds'
set skills1[ 3] = 'AHad'
set skills1[ 4] = 'AHhb'
set skills1[ 5] = 'AHds'
set skills1[ 6] = 'AHre'
set skills1[ 7] = 'AHad'
set skills1[ 8] = 'AHhb'
set skills1[ 9] = 'AHds'
set skills1[10] = 'AHad'
elseif (heroid == 'Hamg') then
set skills1[ 1] = 'AHbz'
set skills1[ 2] = 'AHab'
set skills1[ 3] = 'AHwe'
set skills1[ 4] = 'AHbz'
set skills1[ 5] = 'AHab'
set skills1[ 6] = 'AHmt'
set skills1[ 7] = 'AHwe'
set skills1[ 8] = 'AHbz'
set skills1[ 9] = 'AHab'
set skills1[10] = 'AHwe'
elseif (heroid == 'Hmkg') then
set skills1[ 1] = 'AHtc'
set skills1[ 2] = 'AHtb'
set skills1[ 3] = 'AHbh'
set skills1[ 4] = 'AHtc'
set skills1[ 5] = 'AHtb'
set skills1[ 6] = 'AHav'
set skills1[ 7] = 'AHbh'
set skills1[ 8] = 'AHtc'
set skills1[ 9] = 'AHtb'
set skills1[10] = 'AHbh'
endif
elseif (order == 2) then
set hero_id2 = heroid
if (heroid == 'Hpal') then
set skills2[ 1] = 'AHhb'
set skills2[ 2] = 'AHds'
set skills2[ 3] = 'AHad'
set skills2[ 4] = 'AHhb'
set skills2[ 5] = 'AHds'
set skills2[ 6] = 'AHre'
set skills2[ 7] = 'AHad'
set skills2[ 8] = 'AHhb'
set skills2[ 9] = 'AHds'
set skills2[10] = 'AHad'
elseif (heroid == 'Hamg') then
set skills2[ 1] = 'AHbz'
set skills2[ 2] = 'AHab'
set skills2[ 3] = 'AHwe'
set skills2[ 4] = 'AHbz'
set skills2[ 5] = 'AHab'
set skills2[ 6] = 'AHmt'
set skills2[ 7] = 'AHwe'
set skills2[ 8] = 'AHbz'
set skills2[ 9] = 'AHab'
set skills2[10] = 'AHwe'
elseif (heroid == 'Hmkg') then
set skills2[ 1] = 'AHtc'
set skills2[ 2] = 'AHtb'
set skills2[ 3] = 'AHbh'
set skills2[ 4] = 'AHtc'
set skills2[ 5] = 'AHtb'
set skills2[ 6] = 'AHav'
set skills2[ 7] = 'AHbh'
set skills2[ 8] = 'AHtc'
set skills2[ 9] = 'AHtb'
set skills2[10] = 'AHbh'
endif
elseif (order == 3) then
set hero_id3 = heroid
if (heroid == 'Hpal') then
set skills3[ 1] = 'AHhb'
set skills3[ 2] = 'AHds'
set skills3[ 3] = 'AHad'
set skills3[ 4] = 'AHhb'
set skills3[ 5] = 'AHds'
set skills3[ 6] = 'AHre'
set skills3[ 7] = 'AHad'
set skills3[ 8] = 'AHhb'
set skills3[ 9] = 'AHds'
set skills3[10] = 'AHad'
elseif (heroid == 'Hamg') then
set skills3[ 1] = 'AHbz'
set skills3[ 2] = 'AHab'
set skills3[ 3] = 'AHwe'
set skills3[ 4] = 'AHbz'
set skills3[ 5] = 'AHab'
set skills3[ 6] = 'AHmt'
set skills3[ 7] = 'AHwe'
set skills3[ 8] = 'AHbz'
set skills3[ 9] = 'AHab'
set skills3[10] = 'AHwe'
elseif (heroid == 'Hmkg') then
set skills3[ 1] = 'AHtc'
set skills3[ 2] = 'AHtb'
set skills3[ 3] = 'AHbh'
set skills3[ 4] = 'AHtc'
set skills3[ 5] = 'AHtb'
set skills3[ 6] = 'AHav'
set skills3[ 7] = 'AHbh'
set skills3[ 8] = 'AHtc'
set skills3[ 9] = 'AHtb'
set skills3[10] = 'AHbh'
endif
endif
endfunction
//===========================================================================
// Selects hero IDs for three possible heroes
//===========================================================================
function SelectHeroes takes nothing returns nothing
local integer roll = GetRandomInt(1,100)
if (roll <= 17) then
call SetHero( 1, 'Hpal' )
call SetHero( 2, 'Hamg' )
call SetHero( 3, 'Hmkg' )
elseif (roll <= 34) then
call SetHero( 1, 'Hpal' )
call SetHero( 2, 'Hmkg' )
call SetHero( 3, 'Hamg' )
elseif (roll <= 50) then
call SetHero( 1, 'Hamg' )
call SetHero( 2, 'Hpal' )
call SetHero( 3, 'Hmkg' )
elseif (roll <= 67) then
call SetHero( 1, 'Hamg' )
call SetHero( 2, 'Hmkg' )
call SetHero( 3, 'Hpal' )
elseif (roll <= 84) then
call SetHero( 1, 'Hmkg' )
call SetHero( 2, 'Hpal' )
call SetHero( 3, 'Hamg' )
else
call SetHero( 1, 'Hmkg' )
call SetHero( 2, 'Hamg' )
call SetHero( 3, 'Hpal' )
endif
endfunction
//===========================================================================
// Returns the hero skill for the given hero and level
//===========================================================================
function ChooseHeroSkill takes nothing returns integer
local integer curHero = GetHeroId()
local integer level = GetHeroLevelAI()
if (level > max_hero_level) then
set max_hero_level = level
endif
if (curHero == hero_id) then
return skills1[level]
elseif (curHero == hero_id2) then
return skills2[level]
elseif (curHero == hero_id3) then
return skills3[level]
endif
return 0
endfunction
//***************************************************************************
//*
//* Building and Harvesting
//*
//***************************************************************************
//===========================================================================
// Specifies building priorities for workers
//===========================================================================
function BuildPriorities takes nothing returns nothing
local integer mine = TownWithMine()
call SetBuildAll( BUILD_UNIT, 1, 'htow', -1 )
call SetBuildAll( BUILD_UNIT, 1, 'hpea', -1 )
call SetBuildAll( BUILD_UNIT, 2, 'hpea', -1 )
call SetBuildAll( BUILD_UNIT, 3, 'hpea', -1 )
call SetBuildAll( BUILD_UNIT, 4, 'hpea', -1 )
call SetBuildAll( BUILD_UNIT, 5, 'hpea', -1 )
endfunction
//===========================================================================
// Specifies harvesting priorities for workers
//===========================================================================
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
//===========================================================================
// Determines all building and harvesting assignments for workers
//===========================================================================
function WorkerAssignment takes nothing returns nothing
loop
call UpdateConditions( )
// Harvesting
call ClearHarvestAI( )
call HarvestPriorities( )
// Building
call InitBuildArray( )
call BuildPriorities( )
call Sleep( 2 )
endloop
endfunction
//***************************************************************************
//*
//* Attacking
//*
//***************************************************************************
//===========================================================================
// Returns true if the minimum forces for an attack exist
//===========================================================================
function HaveMinimumAttackers takes nothing returns boolean
local integer count
// Check for attack wave limit
if (attackWave > 1) then
return false
endif
// First Hero Only
if (GetUnitCountDone(hero_id) < 1) then
return false
endif
return true
endfunction
//===========================================================================
// Assigns units to attack based on the given attack group
//===========================================================================
function PrepareAttackGroup takes integer groupID returns nothing
local integer all
// Attack Group #1: 所有单位
if (groupID == 1) then
set all = GetUnitCountDone( hero_id )
call AddAttackUnit( all, all, hero_id )
set all = GetUnitCountDone( hero_id2 )
call AddAttackUnit( all, all, hero_id2 )
set all = GetUnitCountDone( hero_id3 )
call AddAttackUnit( all, all, hero_id3 )
set all = GetUnitCountDone( 'hdhw' )
call AddAttackUnit( all, all, 'hdhw' )
set all = GetUnitCountDone( 'hfoo' )
call AddAttackUnit( all, all, 'hfoo' )
set all = GetUnitCountDone( 'hgry' )
call AddAttackUnit( all, all, 'hgry' )
set all = GetUnitCountDone( 'hgyr' )
call AddAttackUnit( all, all, 'hgyr' )
set all = GetUnitCountDone( 'hkni' )
call AddAttackUnit( all, all, 'hkni' )
set all = GetUnitCountDone( 'hmpr' )
call AddAttackUnit( all, all, 'hmpr' )
set all = GetUnitCountDone( 'hmtm' )
call AddAttackUnit( all, all, 'hmtm' )
set all = GetUnitCountDone( 'hmtt' )
call AddAttackUnit( all, all, 'hmtt' )
set all = GetUnitCountDone( 'hrif' )
call AddAttackUnit( all, all, 'hrif' )
set all = GetUnitCountDone( 'hsor' )
call AddAttackUnit( all, all, 'hsor' )
set all = GetUnitCountDone( 'hspt' )
call AddAttackUnit( all, all, 'hspt' )
endif
endfunction
//===========================================================================
// Prepares an attack group based on the current attack wave
//===========================================================================
function PrepareForces takes nothing returns nothing
if (attackWave == 1) then
call PrepareAttackGroup( 1 )
endif
endfunction
//===========================================================================
// Sleep delays for each attack wave
//===========================================================================
function AttackWaveDelay takes integer inWave returns nothing
if (inWave < nextDelay) then
return
endif
set nextDelay = inWave + 1
endfunction
//===========================================================================
// Advances attack wave counter
//===========================================================================
function AttackWaveUpdate takes nothing returns nothing
call AttackWaveDelay( attackWave )
set attackWave = attackWave + 1
if (attackWave > 1) then
set attackWave = 1
set nextDelay = attackWave + 1
endif
endfunction
//===========================================================================
// Basic attack functionality
//===========================================================================
function AttackTarget takes unit target, boolean addAlliance returns nothing
if (target == null) then
return
endif
if (addAlliance) then
call SetAllianceTarget( target )
endif
call FormGroup( 3, true )
call AttackMoveKillA( target )
if (not addAlliance) then
call SetAllianceTarget( null )
endif
endfunction
//===========================================================================
// Initiates an attack based on target priorities
//===========================================================================
function LaunchAttack takes nothing returns nothing
local unit target = null
local boolean setAlly = true
// Don't launch any attack while town is threatened
if (TownThreatened()) then
call Sleep( 2 )
return
endif
// Target Priority #1
if (target == null) then
set target = GetAllianceTarget()
if (target != null) then
set setAlly = false
endif
endif
// Target Priority #2
if (target == null) then
set target = GetExpansionFoe()
if (target != null) then
set take_exp = false
endif
endif
// Target Priority #3
if (target == null) then
set target = GetMegaTarget()
endif
// Target Priority #4
if (target == null) then
set target = GetEnemyExpansion()
endif
// Target Priority #5
if (target == null) then
set target = GetEnemyExpansion()
if (target == null) then
call StartGetEnemyBase( )
loop
exitwhen (not WaitGetEnemyBase())
call SuicideSleep( 1 )
endloop
set target = GetEnemyBase()
endif
endif
// Target Priority #6
if (target == null) then
set target = GetCreepCamp( 0, 9, false )
endif
// Target Priority #7
if (target == null) then
set target = GetCreepCamp( 10, 100, true )
endif
// Attack the target and increment attack wave
if (target != null) then
call AttackTarget( target, setAlly )
call AttackWaveUpdate( )
else
// If no target was found, sleep a bit before trying again
call Sleep( 20 )
endif
endfunction
//===========================================================================
// Determines all attacking assignments
//===========================================================================
function AttackAssignment takes nothing returns nothing
call StaggerSleep( 0, 2 )
if (attackWave == 1) then
call AttackWaveDelay( 0 )
endif
loop
loop
call UpdateConditions( )
exitwhen (HaveMinimumAttackers() and not CaptainRetreating())
call Sleep( 2 )
endloop
call ResetAttackUnits( )
call PrepareForces( )
call LaunchAttack( )
endloop
endfunction
//***************************************************************************
//*
//* Main Entry Point
//*
//***************************************************************************
//===========================================================================
function main takes nothing returns nothing
call InitAI( )
call SetPlayerName( ai_player, "只是另外一个魔兽争霸III的AI脚本" )
call InitOptions( )
call SelectHeroes( )
call CreateCaptains( )
call SetHeroLevels( function ChooseHeroSkill )
call Sleep( 0.1 )
call StartThread( function WorkerAssignment )
call StartThread( function AttackAssignment )
call PlayGame( )
endfunction
|
|