I don't want the AI do anything, just always keep 5 peasants harvesting gold (1), 3 lumber (2) and replace them if someone kills them (3) plus always have the mentioned campaign defenders (replace them if anyone kills them as well (4)). But the AI messes around, builds tons of farms etc. and that's not in the AI script. How do I make it so that AI does only w(1), (2), (3) and (4)?
Have you removed the standard Melee - Start AI scripts for computer players? It might be the reason for the strange AI behavior.
About your script: I don't see your building queue anywhere. CampaignDefendersEx only sets the defense group and isn't really needed if you don't use an attack group. Wouldn't it be really easy to do an AI script you want by using the AI editor? Or don't you have TFT?
Also, place the original guard units in the place where you want them to guard, and the computer fills the guard positions automatically.
Yes, the standard melee AI scripts are removed. Here's the exact situation:
There are 6 peasants when the script is launched. I want the AI build 3 more peasants so that there are 9 in total than send 5 to harvest gold and 3 to harvest lumber and leave one under my command ( I use AI - Ignore unit's guard position for this). And I suppose when a peasant is killed, it will always be replaced. How do I do this? What Build commands are needed?
Add code lines like the ones below to appropriate sections of your code (e.g. local variable declarations to the start, etc.):
local integer goldpeasants = 0
local integer woodpeasants = 0
call InitBuildArray( )
call SetBuildAll( BUILD_UNIT, 9, PEASANT, -1 )
call SetBuildAll( BUILD_UNIT, 5, RIFLEMAN, -1 )
call SetBuildAll( BUILD_UNIT, 3, KNIGHT, -1 )
call SetBuildAll( BUILD_UNIT, 3, FOOTMAN, -1 )
loop
set goldpeasants = GetUnitCountDone(PEASANT)-1
if goldpeasants < 0 then
set goldpeasants = 0
elseif goldpeasants > 5 then
set goldpeasants = 5
endif
set woodpeasants = GetUnitCountDone(PEASANT)-6
if woodpeasants < 0 then
set woodpeasants = 0
elseif woodpeasants > 3 then
set woodpeasants = 3
endif
call HarvestGold( TownWithMine(), goldpeasants)
call HarvestWood( 0, woodpeasants )
call Sleep(2)
endloop
I haven't checked the syntax, but I think it's ok! You need to add buildings (e.g. barracks) to the building prioirities list too, if you want them to be rebuilt. You should put troop production buildings on the list before appropriate troops.
Yay! It works! Thanks!!! I owe you one. But there are some important questions that have risen. How exactly does SetBuildAll work? Is it the same SetProduce? Or does it show how many units the player should have AFTER CONSTRUCTION, that is if the player already has those units, do not build more?
Yes. The computer first builds 9 peasants, then 5 riflemen, then 3 knights, and then 3 footmen. They also will be rebuild in this order if they are destroyed. You might want to interlace these units. I wrote this as a brief example.
All these functions are found in common.ai.
I rechecked CampaignDefendersEx and it indeed does build units, unlike what I said before. I think it does the same as SetBuildAll, but also adds the units to the defense group (which essentially does nothing here).
The parameters in SetBuildAll are unit/upgrade (use BUILD_UNIT for units and buildings and BUILD_UPGRADE for upgrades), quantity, unit id, and town (-1 is any town).
I've been reading your AI script and it seems you're missing a code statement:
You script:
//=========================================================================== // The Human Alliance (player 10) -- AI Script //=========================================================================== globals endglobals
//=========================================================================== // main //=========================================================================== function main takes nothing returns nothing
The script you wrote isn't bad but the CampaignAI function is a very SPECIAL one, that makes creating a campaign AI easier. It does all the build queue and harvesting for you. You only need to set the corresponding variables. So:
set campaign_gold_peons = 5 set campaign_wood_peons = 3
The default values are actualy also 5 and 3, so you don't even have to set anything. Your loop has to be empty except the Sleep function.
The peasants build houses to have enough food, this is automatical as well because of the CampaignAI function.