Jump to content


Photo

Explanation of Empire at War's AI engine


  • Please log in to reply
3 replies to this topic

#1 Drieick

Drieick
  • Members
  • 12 posts
  • Location:Terra
  •  Otaku

Posted 17 February 2008 - 03:08 AM

I recommend you alternatively read the updated version.

I'm not sure how much the community knows of it, but I'm basically just going to explain how it works.

Anyway, it basically just translates into a form of Lua. Or, that is what I predict.

Variables and how they're interpreted
Here are the variables, which obivously start with the word "Variable_":

Variable_Target
Variable_Enemy
Variable_Self

What they mean depends on how and where they're used; for example, if it's Galactic mode, Target could mean a planet and Variable_Self could mean the player attributes. However, in tactical mode, Variable_Target could mean a unit or a location on the map. Currently, on Target appears in the Lua source (defined in PGBaseDefitions.lua)
I'm not sure how exactly it determines locations, because there's no obvious grid, so I'mjust going to assume it's square pixel by square pixel, which is inefficient (Grid would be faster... or a bigger squareXsquare scaled grid).

Where and how are they defined? In the AI/Goals folder, as you can see here:

<Upgrade_Starbase>	<AIGoalApplicationFlags> Friendly </AIGoalApplicationFlags> <!-- Filter for who is applies to -->	<GameMode> Galactic </GameMode> <!-- Where it'll be used -->	<Category> Infrastructure </Category> <!-- The category budget it's in -->	<Reachability> Any </Reachability>	<Time_Limit> 30.0 </Time_Limit>	<Build_Time_Delay_Tolerance>1.2</Build_Time_Delay_Tolerance>	<Is_Like>		Build_Scanner,		Build_Initial_Starbase_Only,		Build_Jamming_Station	</Is_Like></Upgrade_Starbase>

What I don't know is what the Is_Like, Reachability, Time_Limit and Build_Time_Delay_Tolerance tags means; anyone know? The variables by themselves don't mean anything yet, so don't worry about it yet.

Categories, Functions, Expressions, and Values
I'm pretty sure you all know of the categories; Infrastructure, Offense, Defense, Information, MajorItem, Hero, StoryArc, and Always. Each have a budget, which are broken down into Goal functions, which are made up of Goals and Functions; Functions are made up of Expressions, which are made up of Values. It's basically just a bunch of organized Algebra, put into a simple form.

Now, you must understand, not all "budgets" are made up of credits, but more so just numbers without an indentifier (They're not "X Credits", "X Dollars", or "X Inches", and what not). The one with the highest are the one the AI will do first. Such as, if the AI is faced with two planets, it'll formulate the values and whichever one is higher, it'll attack that one. (Ex: Coruscant has a value of 10000 and Byss has a value of 9000, the AI will attack Coruscant first). What it attacks the planet with is defined in the Lua source; unfortunately, you can't tell the AI "The higher the value, the more ships you send!" Doesn't work like that; it has to attack the planet first and study the outcome. The only way to do such a thing is by increasing the desire with the Per_Failure_Desire_Adjust tag (It can increase or decrease).

Okay, so does the AI just choose the Category with the highest value too? Not that I know of. But, that kinda be stupid way for the AI to work, don't you agree? The categories don't compete, because they do entirely different things; functions inside a category compete. The categories competing would be like a shampoo company competing with a gas company; they don't even sell the same things.

So, yes, it is possible to simplify the expressions to the point that the resulting budget is doesn't surpass 4 integers, which would make the AI faster (I can't tell you how much faster it'll get).

So, where are these categories defined? In the templates, found in XML/AI/Templates. The categories that are on are defined in the <Turn_On> tag. Be catious, though; you must keep all templates up-to-date.

How variables are used in expressions
Now that we (hopely) know a little on how the AI is structured, let me tell you how exactly variables work: they are basically text to be replaced by a number. They are floating point numbers, I believe.

But, are they just numbers? Do they just mean anything? No. The value is determined by any number of functions defined in Empire at War's game engine (the library that defines what means what). For example, Variable_Target.Health will return that target's current health; Variable_Target.StarbaseLevel will return the planet's current space station level. "Health" is a function; "StarbaseLevel" is a function; they take the Target and return the specified information.

Basic expression syntax
It's quite simple, really. You basically just follow the order of operations found in math. All you really need to know are the operators:

* = Multiply/can be used as a form of an "If" statement
+ = Add
- = Minus/Subtract
/ = Divide
> Greater than
< = Less than
() = Blocks of expressions which end up as one value
# = Unknown/I'm not sure what this operator means

The mulitply, I think, is the hardest to get used to, but it's quite easy. It can be used to multiply or an "If" statement. How? Well, remember how each function returns a value? Well, if one expression in that function require something and that requirement isn't met, then it returns 0; if it's true, then returns 1. Something such as this...

(Variable_Self.BaseLevel > 0)

...will return 0 if it's false and 1 if it's true. What's the point? The multiplication. If the statement is false and it's multiplied by something, then the result is 0. For example:

<Allowed_To_Retreat_From_Space>	Game.IsCampaignGame	*	Variable_Self.CanRetreat	*	(1.0 - Variable_Self.IsFaction {Parameter_Faction = "Pirates"})</Allowed_To_Retreat_From_Space>

If Game.IsCampaignGame returns 0, then the function returns a 0, no matter what the other values are. If Variable_Self.CanRetreat returns 0, but Game.IsCampaignGame and (1.0 - Variable_Self.IsFaction {Parameter_Faction = "Pirates"}) return 1, the whole thing is 0: 1 * 0 * 1 = 0.

The difference between a goal function, a goal, and a function
Functions are what determines the value of a goal function; goal functions are used to determine what action the AI will take in whichever category. Functions are just expressions - they can be a single line, dozens of lines, or they can be a single number; doesn't matter, as long as there's a number.

How does Empire at War know which function to use? Through the goal functions. The goal functions specify which goal to use and which function to use. What is the difference? A goal describes where the function will be used; i.e., in Galactic mode. The function just defines the goal functions' value.

Basically, you the a goal function, which is made up of a goal - which describes how the goal function and function will be used - and the function, which provides the goal function with a value.

How to use a function and implementing scripts inside other functions
Once you understand the expressions' basic syntax, using functions is quite easy.

You can call a function inside a function, just like any other scripting langauge. Basically, it's just this: Function_FunctionName.Evaluate. Without Evaluate, there's no return value, and thus, a syntax error. The function can return a binary value (0 or 1) or an actual number; for example, 246.

Lua scripts can be used as well. The syntax to call a Lua script is just this: Script_FileNameTheScriptIsIn.Evaluate. Evaluate has a different purpose here: it calls the Lua function "Evaluate", which of course, returns a value. If the "Evaluate" function has parameters or arguments, you can define those in the XML as well, by using this: Script_FileNameTheScriptIsIn.Evaluate{Parameter_Script_String = VALUE}. The number of Parameter_Script_String depends on how much arguments the Evaluate function in the Lua script require.

Making use of Galactic Hints
Hints are a grand feature for Galactic mode; they define an extra value for the specified planet. You can even add new hints, that work, to the already set of hints.

Hints are basically variables that can be used in expressions to add-on to the value of a function. They are defined in a file, which has to be linked to in <Markup_Filename>FactionName, GalacticHintsFile </Markup_Filename> tag.

To call a hint in an expression, the command is simply: Variable_Target.Hints.HintName.

Templates and PlayerType
Templates are basically just blue-prints; they hold the category information, which functions that'll be used for formulating the budget, and what not. Without this template, the AI is cut off completely from any and all Goal Functions, thus making it idle. Also defines which goals or plans to turn off or on.

PlayerType is the compiler; it gathers the templates, the difficulty adjustments, any freestore scripts, and the file names for the goal functions. The name for the player is also given here; the name is used in the <Basic_AI> and <AI_Player_Control> tags.

If you ask me, they should be put together instead of seperate.

Examples that you can use
Here are some functions I wrote that you can study and use; just provide a link to this thread (or just a form of credit, but I prefer the link) if you publish any work with any of these functions in it:

<!-- Are we connected to the enemy's home planet? --><Is_Connected_To_Enemy_Home_Planet> Function_Is_Connected_To_Me.Evaluate * Function_Is_Home_Planet.Evaluate </Is_Connected_To_Enemy_Home_Planet><!-- Does this planet have a barracks? --><Has_Barracks> Variable_Target.HasStructure{Parameter_Type = "E_Ground_Barracks", Parameter_Type = "R_Ground_Barracks", Parameter_Only_Consider_Complete = 1.0} </Has_Barracks><!-- Does this planet have a light factory? --><Has_Light_Vehicle_Factory> Variable_Target.HasStructure{Parameter_Type = "E_Ground_Light_Vehicle_Factory", Parameter_Type = "R_Ground_Light_Vehicle_Factory", Parameter_Only_Consider_Complete = 1.0} </Has_Light_Vehicle_Factory><!-- Does this planet have a barracks OR a light factory? --><Has_Barracks_Or_Light_Factory> Function_Has_Barracks.Evaluate + Function_Has_Light_Vehicle_Factory.Evaluate </Has_Barracks_Or_Light_Factory><!-- Does this planet have a barracks AND a light factory? --><Has_Barracks_And_Light_Factory> Function_Has_Barracks.Evaluate * Function_Has_Light_Vehicle_Factory.Evaluate </Has_Barracks_And_Light_Factory>

The season finale
The only thing I hope is that you understood is what I wrote; if you have any questions, just post. ;)

ありがとう! ^^ (Thanks!)

Edited by Drieick, 06 May 2008 - 05:45 AM.


#2 Dr. Nick

Dr. Nick
  • Project Team
  • 181 posts

Posted 19 February 2008 - 04:33 AM

Sweet! Definitely gonna be referencing this :)
Posted Image

#3 Zarkis

Zarkis
  • Members
  • 70 posts

Posted 26 February 2008 - 04:35 PM

Well, I never got any moded perceptual equations to work in the game. While the xml files get reloaded if you change them, the game still uses the vanilla equations, at least that was always my impression.

#4 Drieick

Drieick
  • Members
  • 12 posts
  • Location:Terra
  •  Otaku

Posted 27 February 2008 - 04:39 AM

If that was the case, there wouldn't be any new units, eh?

And if you really want to test it, just make a XML parsing error, then load the game. It either won't load, or the AI won't work - depends on what kind of parsing error it is.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users