HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

I need to know the basic jist of how an AI runs

05-05-2004, 12:10 AM#1
Narwanza
I already know all of the code things for AI, and I can figure out the natives myself, but how does your basic AI run. I know they don't just execute once, and that they need a main function (reminds me of JAVA) but, I don't quite know how everything goes together.
05-05-2004, 12:57 PM#2
AIAndy
Actually AI only runs once. When you start the script, the function main is called. From there it is the job of the AI script. There is no further invocation (actually there is one kind of callback event, the one for own heroes leveling, but that is all).
You can start up to 5 other threads though. Usually you will then have loops in the AI with sleeps to keep it alive.
05-09-2004, 04:20 AM#3
Narwanza
Andy, could you specify what you mean by threads? I am officially turning this into my Narwanza's questions about AI. I am going to be scripting an AI for a map that I am going to do, so I am going to need a lot of questions answered. I hope you guys can help me. Here are some of my first questions. How can I specifiy different towns for SetProduce? Also, I will add a list of common.j natives that I need to have a better explanation abou them, or an explanation period. I'm so tired right now I am just going to go to bed, but later I will post some more of my questions.
05-09-2004, 11:59 AM#4
AIAndy
A thread is a function that runs pseudo parallel to the other threads. So if you use StartThread(function foo) in function bar then foo and bar will run pseudo parallel.
SetProduce takes an integer as last parameter that represents the town to build in. The main town has 0 I think (untested) and then the next town gets 1 and so on.
05-09-2004, 07:43 PM#5
Vidstige
Yes, the main town (the starting town) has index 0, the expansions 1, 2 and so on. If you don't really care in which town something is produced, then specify -1 as town index. (This is true for melee games, and it probably works the same in campign games...)

Edit: Don't forget to put calls to Sleep(secs) in your threads, otherwise the game will kill them (most likely the complete AI script).
05-09-2004, 08:02 PM#6
Narwanza
How does warcraft specify a town basically is what I am asking. What does it require to be known as town 1, or 2, and how can I know which town halls are in town 1 or 2?
05-09-2004, 09:13 PM#7
Vidstige
I believe that it works something like this (seen from an AI script): Each town is defined by an (x,y) position. Town 0 has the some position as the starting town hall. Town 1 has the same position as the first expansion, the second town hall (built by the AI). The only way to really know which buildings are in which town and which town is where might by to keep track of the positions of each towns (the same as the town halls). This is probably easiest done storing away the positions of the town halls as they are created.

To get hold of where town 0 is, somthing like the following should work:
Code:
function HomeTownLocation takes nothing returns nothing
  local group grp = CreateGroup()
  local unit town = null
  call GroupEnumUnitsOfType(grp, "townhall", null)  // For human hall
  set town = FirstOfGroup(grp)
  return GetUnitLoc(town)
endfunction
if it is run in the beginning of the game.

When a second hall is built, this should create town 1 and town 1's location may be found by figuring out which hall is the new one.

The answer to your question is therefore (as far as I know): You can only know which hall is in which town if you keep track of which order you build the halls in.

The following natives could be useful for verification purposes:
Code:
native TownHasMine          takes integer townid                        returns boolean
native TownHasHall          takes integer townid                        returns boolean
05-09-2004, 10:09 PM#8
AIAndy
Threads in AI are not killed even if they don't sleep. They are just forced to sleep after they reach the limit.
05-09-2004, 11:14 PM#9
Narwanza
Ah, crap. This is a stupid question and I feel dumb asking it, but are common.j locals able to be used in .ai text files? What if the town halls are already Preplaced? I am trying to write an AOS .ai file, and the towns are probably going to be preplaced. If there is no way around it I will just have the AI expand to those extremely quickly in the first bit of the game and have them kept track of as expansions so I can train and produce units from outposts.
05-10-2004, 06:31 AM#10
AIAndy
What do you mean with common.j locals ?
I think preplaced town halls will get towns assigned too and that distribution is likely constant so best run the map with a test AI that builds some things in the different towns to see which town gets what id.
05-10-2004, 02:07 PM#11
Narwanza
Doh, not locals, natives. Sorry, I was tired when I posted last.
05-10-2004, 08:09 PM#12
AIAndy
Yes, you can use stuff from common.j . Just those which take code or return strings do not work.
05-10-2004, 10:52 PM#13
Narwanza
Those sound like goofy things to eliminate to me. Blizzard never ceases to confuse me.
05-10-2004, 11:52 PM#14
Narwanza
Geez, I feel at a complete loss trying to code AI. Good thing I have about 6 weeks to get this written, I am going to need all the help and time I can get. What native would you use to tell the AI to build a tower? What native would you use to tell the AI to make sure all buildings are at full health even if the peopns have to repair them. What on earth are captains? Is Sleep() any more accurate than TriggerSleepAction()? How do AI's decide where to build buildings? How is the next expansion point caculated? Help me please.
05-11-2004, 09:52 AM#15
Vidstige
Sleep() is used in .ai scripts, TriggerSleepAction() in Triggers. The AI seems to select building spots kind of randomly when calling SetProduce(). You could ofcourse choose a worker and tell him exactly where to build, but then you would need to scan the map, and such things. While scanning the map is rather easy from a trigger, it is not any fun to try from an .ai script. I think that it is done in AMAI though (AIAndy?).

call SetProduce(1, 'htow', 0) should build a watch tower at starting base.

The next expansion point is choosen among the gold-mine locations. I am not sure how it select which goldmine it should expand to, but it is likely a higher probability to choose a closer one.