HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Explanation of every native declared in common.ai

06-30-2004, 04:37 PM#1
anctan
After looking at common.ai I saw that there are 113 hard-coded native functions / commands. I guess that every other function consists mainly of these native commands plus certain calculation, evaluation of conditions etc. Then I thought how good it would be to have some sort of a list (tutorial) that explains all of these natives. If we had such a list then first of all, beginners would not have to spend ages finding out the functionality of these natives and, people will be able to learn JASS (or at least AI) more quickly. Of course, several natives are self-explanatory others are not, though. Even one of the simplest - Sleep() - took me half an hour to figure out that it is the same GUI Wait action. And some natives call for a lot of comments. I know that there are a lot of people round here who know most natives so why not, I thought, combine this knowledge into a single list? My idea is that everyone could tell what they know thus we would have a complete list. If this turns out successful, we could do the same with common.j and blizzard.j.

Here is my start at it. I will try to explain several natives declared in common.ai.

GetUnitCount takes integer unitid returns integer

This native function counts all units of specified type that are on the map (including units that are still being trained / constructed!) at that moment regardless which player owns them. UnitId (unit Identity) needs to be replaced with the name of the type you want to count. You can either use a four-digit code such as "hfoo" (which refers to footman) or a global integer. In this case, it would be FOOTMAN (case-sensitive!). You can find the four-digit codes and the constant integers alike in the same common.ai. However, constant integers are defined only for melee (standard) units, meaning you would not be able to deal with custom tech-tree in your AI scripts. For this, you will h a v e to use the four-digit code. Every unit in Warcraft III (including all neutrals, even tents(!) have their own four-digit code. They are given in UnitData.slk within the War3.mpq (the path is: War3.mpq\Units\UnitData.slk).

GetUnitCountDone takes integer unitid returns integer

This native functions exactly as the one described above but there is one difference. The function has the Done tag attached which means that it will only count those units that are on the map at that moment (units still in production will be excluded).

Comment: notice that there is no native that counts units only in production and in rare circumstances you might need such count. Now think: the number of units on the map at a given time including units in production is always greater than the number of units excluding units in production which means you can find the number of units in production by subtracting the number of units on the map from the number of all units of that type. The scheme would go like this:

Number of Units in Production = GetUnitCount() - GetUnitCountDone()

Sleep takes real seconds returns nothing

This is the already mentioned function. It corresponds to the GUI Wait action and makes the script execution stop and wait for the given amount of real-time seconds (not in-game seconds). This native (or rather its GUI counterpart) is mainly used in cutscenes therefore not very common in AI scripts. However, you would need it in series of repeated actions (in cycles, or loops if in JASS) because without any break the engine will immediately start executing the same cycle again and since computers can do like millions of operations every second the cycle will happen so often that this will ultimately crash the game.

GetUnitGoldCost takes integer unitid returns integer

This native determines how much gold producing a certain unit will cost. Replace the unitid parameter with the mentioned four-digit code or constant integer of the unit whose gold cost you want the script to determine. No doubt, sometimes you would want your AI script build / train a batch of units. But it might be that the player that is to build that batch does not have enough gold for production. In this case, the command that tells the player to train some units will be either completely ignored or not enough units will be built (please confirm which is right, or both?). This native comes in handy when you want to prevent this from happening: you would first check if the player has enough gold and if they do not, wait unitl enough resources are gathered and issue the production command then.



The "official" part is over now: here I have described 4 natives - 109 to go! Notice the extremely heavy commenting. It is my belief that only by commenting upon every native like this (by fully explaining the functionality, aim, uses) can we clear things up. If you decide to describe a native, please try to include everything that you know about it: from classic uses to less frequent, more specific ones along with known problems etc. Everything must be most accurate hence if you find any mistakes in my explanations, please report. So we have a start and I hope that many people will join this and soon we will have a complete list. ;) Thanks for your time.
06-30-2004, 05:16 PM#2
-={tWiStÄr}=-
We should go in order. I dont know every function by heart tho so i will go with my most used function for testing
DisplayTextToPlayer takes player toPlayer, real x, real y, string message returns nothing
This function displays text to a player. the toPlayer argument is replaced with the player using Player(number of player) remember in jass that 1 is 0 though. You can use an integer variable in place of the number of player. you can also just use a player variable instead of the whole player part. the next argument, real x, set how far to the right you want the text to show. a value of 0 is where text normaly shows, just off the left side of the screen. the next argument, real y, is how far up the screen you want the text to show. a value of 0 will display text just off the top of the minimap. Next is the all important string message! this is what will actually show. you put the message you want to display in qoutes " ". you can do concanate strings by using a + sign. "He" + "llo" will display as Hello. You can put variables in here also but dont put any qoutes around them. String15 + "i" + String9 will display the text value of String15 then it will display an i with no space, then finaly String9 with no space. This function is most commonly used to display text to a certain player, whether it be a help message, directions, insults whatever. It can also be used to test you functions and make sure that they are actually running or to figure out which part isnt working. Part of the beauty of this function is how long the text is displayed. depending on how long the string of text is, changes how long it is displayed for, giving the user plenty of time to read the message.

There. was that too in depth how i went into the whole how to put in a string? 108 to go
edit: oops i forgot some stuff.
edit2: DOH common.ai not .j should i just delete this?
06-30-2004, 05:51 PM#3
anctan
common.ai contains DisplayText takes integer p, string str returns nothing and DisplayTextI takes integer p, string str, integer val returns nothing, so it is OK since part of what you described applies to these two natives as well. And it was just in my style, I like it. Maybe shed some more light on coordinates? The coordinates can be written like this (x;y) - like in mathematics. So

(x1;y1)--------------(x2;y2)
iiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
(x3;y3)--------------(x4;y4)

The four points correspond to the upper left, upper right, lower left and lower right corners of the screen. From what you write,

iiii (0;0)-------------------------(x(max);0)
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
(0;y(max))------------------(x(max);y(max))

Is this correct (=the point with coords (0;0) is the very left uppermost point and then the coords increase until the text reaches the sides. What are the max values?)
06-30-2004, 07:24 PM#4
-={tWiStÄr}=-
actually its like a normal grid this time where (0,0) is the bottom left.
iiii (x;y(max))-----------(x(max);y(max))
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
iiiiiiiiii|iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii|
(0;0)------------------(x(max);y)

and sorry, but i dont know the maximum values. You can probably make it as high as you want then it will run off the screen. also this probably doesnt have that many great AI uses besides testing and maybe something like in AMAI where the computers send messages every now and then.
07-01-2004, 10:13 AM#5
AIAndy
Some comments to the natives already mentioned:
GetUnitCount includes buildings, that a peon has been given order to build, not only those that are already in construction. 4-letter-codes need to be written like 'hfoo' , not "hfoo" (the first is an integer, the second a string). You can also add additional constant integers as alias for the 4-letter-code if you like.
When a thread runs too many statements without a sleep, it is forced to sleep and unlike with triggers that does not mean the thread is cancelled.
GetUnitGoldCost does not only work for units that are constructed/trained, but also for those that are bought (e.g. mercs).

There have already been some threads with explanations for a lot of natives, so from there the info could be copied here.