How to script ai movement without a path
by Hans Van Halteren · in Torque Game Engine · 10/04/2004 (2:15 am) · 10 replies
I am creating my first steps in the scripting of games in Torque. After loads of documentation, and after starting to read Kenneth Finneys book, I now am trying to really create my plans.
One of the is that I want to create a game where bots are moving according certain rules in stead of a path like the example fps game.
I presume that I need to script in the AI script file instead of the player script file but how do I script that a bot moves all the time the way I want it. Which means it can go right, but next time it goes left. Or that it goes into a house under certain cirmustances.
One of the is that I want to create a game where bots are moving according certain rules in stead of a path like the example fps game.
I presume that I need to script in the AI script file instead of the player script file but how do I script that a bot moves all the time the way I want it. Which means it can go right, but next time it goes left. Or that it goes into a house under certain cirmustances.
About the author
#2
This function isn't linked to the path. It's linked to the setMoveDestination function. The path is only a structure with a graphic representation that helps you setting it on your world. But you might as well forget it and use :
bot.setMoveDestination(%point1);
OnReachDestination(...)
{
if(I'm in %point1)
setMoveDestination(%point2);
else
setMove Destination(%point1);
}
this will make your bot move back and forth from Point1 to Point2
HTH
10/04/2004 (6:39 am)
Whenever a bot reaches it's destination the OnReachDestination function is called.This function isn't linked to the path. It's linked to the setMoveDestination function. The path is only a structure with a graphic representation that helps you setting it on your world. But you might as well forget it and use :
bot.setMoveDestination(%point1);
OnReachDestination(...)
{
if(I'm in %point1)
setMoveDestination(%point2);
else
setMove Destination(%point1);
}
this will make your bot move back and forth from Point1 to Point2
HTH
#3
So there is a high level of ai involved and the bot should actually decide where to go. So instead of following a path, I should be able to program an algorithm of decisions. The bot should live it's own life in the game.
10/04/2004 (10:49 pm)
What I want to achieve is that a bot in static mode remains on a certain spot doing his thing (eg. smoking a cigaret). Then, when it hears a sound in his surroundings, it might (or might not) want to check what is going on. Then, when it sees me, it might (or might not) want to track me down to kill me.So there is a high level of ai involved and the bot should actually decide where to go. So instead of following a path, I should be able to program an algorithm of decisions. The bot should live it's own life in the game.
#4
10/05/2004 (6:19 am)
You will have to implement a Finite State Machine for that, but it's perfectly achievable with torque.
#5
So, for your example:
if(%ai.bored < 10 && %ai.hateplayer < 5)
%ai.state = "Stand and Smoke";
else if(%ai.bored >= 10 && %ai.hateplayer < 5)
%ai.state = "Walk and Smoke";
else if(%ai.hateplayer >= 5 && %ai.confidence > 10)
{
%ai.state = "Drop Cigarette and Pull Out Gun";
}
else if(%ai.hateplayer >= 5 && %ai.confidence < 5)
{
%ai.state = "Run, Hide, and Burn self with Cigarette";
}
You get the idea. Of course you could have the player in a state and only check how to move out of that state:
if(%ai.state == "Bored")
{
if(%ai.hateplayer > 10)
{
%ai.state = "Shoot Player";
}
}
else if(%ai.state == "Dancing on Players Grave Dance")
{
%ai.hateplayer = 0;
if(%ai.bored > 10)
{
%ai.state = "bored";
}
}
This allows you to control if states can be achieved based on previous states achieved. It can also reduce the logic required in your machine tremendously. I have used this second kind of state machine to control real world machines and it reduces the interlocking code required.
You get the idea. It just switches "state" based upon the stimilus (input) you give to the AI player. There are other methods for AI besides state machines, but these are very simple to implement. You might even consider multiple machines to govern lets say movement in one machine and shooting in another. Each state could control its own set of animations to some extent.
Hope this helps,
Frank
10/05/2004 (7:00 pm)
In case you don't know what a Finite State Machine is... Ignore the word finite and just call it a state machine. Now state just means condition or current action (inaction). So, for your example:
if(%ai.bored < 10 && %ai.hateplayer < 5)
%ai.state = "Stand and Smoke";
else if(%ai.bored >= 10 && %ai.hateplayer < 5)
%ai.state = "Walk and Smoke";
else if(%ai.hateplayer >= 5 && %ai.confidence > 10)
{
%ai.state = "Drop Cigarette and Pull Out Gun";
}
else if(%ai.hateplayer >= 5 && %ai.confidence < 5)
{
%ai.state = "Run, Hide, and Burn self with Cigarette";
}
You get the idea. Of course you could have the player in a state and only check how to move out of that state:
if(%ai.state == "Bored")
{
if(%ai.hateplayer > 10)
{
%ai.state = "Shoot Player";
}
}
else if(%ai.state == "Dancing on Players Grave Dance")
{
%ai.hateplayer = 0;
if(%ai.bored > 10)
{
%ai.state = "bored";
}
}
This allows you to control if states can be achieved based on previous states achieved. It can also reduce the logic required in your machine tremendously. I have used this second kind of state machine to control real world machines and it reduces the interlocking code required.
You get the idea. It just switches "state" based upon the stimilus (input) you give to the AI player. There are other methods for AI besides state machines, but these are very simple to implement. You might even consider multiple machines to govern lets say movement in one machine and shooting in another. Each state could control its own set of animations to some extent.
Hope this helps,
Frank
#6
Clear story.
Do I need to program this all in the aiplayer.cs file ?
10/05/2004 (9:55 pm)
Thanks Frank, Clear story.
Do I need to program this all in the aiplayer.cs file ?
#7
10/06/2004 (12:25 am)
You don't have to. You can create a whole new script file called "myAIstuff.cs" if you want and do it all there. Just make sure it gets loaded.
#8
Back to state machines. I do not know if you have noticed, but a lot of simple in concept techniques are wrapped up in jargon. Hence "finite state machine". This stuff scares people away. I am running into this in books on fuzzy logic, neural nets, etc. It drives me crazy when they call something weird or use math symbols, but never explain what it represents. Sorry about the rant.
Another method/concept people might find interesting is that of error. You can control how the AI trys to correct a bad situation by using the difference of where you want to be from where you are. This value is then multiplied by a gain (power) to the output to correct for that error.
Example:
%pos_error = %current_pos - %target_pos;
%proportional = %pos_error * %pos_gain;
%ai.angle = %proportional;
The variables are obviously made up. This idea is the basis of a Proportional only control loop. In control theory you have what is call a PID loop (Proportional, Integral, Derivative). The proportional error is multiplied by a proportional gain (gain associated with the proportional part of the loop). The other two parts are calculated like this:
%integral = %pos_error * %pos_gain * %change_in_time / %integral_gain + %integral;
%derivative = (%pos_error - %last_pos_error) * %pos_gain * %derivative_gain / %change_in_time;
The end result is the sum of the %integral, %derivative, and %proportional gains. However, for the most part and since you are probably not going to need the complexity of the ID of PID just ignore it. This algorithm BTW has been controlling nearly everything for more than 100 years. If anybody says it is old hat and everyone is using XYZ methods instead they are full of crap.
Hope this helps someone do something useful,
Frank
10/06/2004 (9:41 pm)
When I get to it I will be playing around with the AI player. I plan on using more sophisticated techniques. Right now I am building up the code so I can edit the AI from inside Torque. This is taking some time, but it should prove very interesting. I don't want to spill the beans just yet on what I am doing. My goal is to have AI players that rely less on cheating to defeat the player. Back to state machines. I do not know if you have noticed, but a lot of simple in concept techniques are wrapped up in jargon. Hence "finite state machine". This stuff scares people away. I am running into this in books on fuzzy logic, neural nets, etc. It drives me crazy when they call something weird or use math symbols, but never explain what it represents. Sorry about the rant.
Another method/concept people might find interesting is that of error. You can control how the AI trys to correct a bad situation by using the difference of where you want to be from where you are. This value is then multiplied by a gain (power) to the output to correct for that error.
Example:
%pos_error = %current_pos - %target_pos;
%proportional = %pos_error * %pos_gain;
%ai.angle = %proportional;
The variables are obviously made up. This idea is the basis of a Proportional only control loop. In control theory you have what is call a PID loop (Proportional, Integral, Derivative). The proportional error is multiplied by a proportional gain (gain associated with the proportional part of the loop). The other two parts are calculated like this:
%integral = %pos_error * %pos_gain * %change_in_time / %integral_gain + %integral;
%derivative = (%pos_error - %last_pos_error) * %pos_gain * %derivative_gain / %change_in_time;
The end result is the sum of the %integral, %derivative, and %proportional gains. However, for the most part and since you are probably not going to need the complexity of the ID of PID just ignore it. This algorithm BTW has been controlling nearly everything for more than 100 years. If anybody says it is old hat and everyone is using XYZ methods instead they are full of crap.
Hope this helps someone do something useful,
Frank
#9
PID controls are great when you are monitoring one variable, say the temperature of a reaction that can be adjusted by a valve that controls the heat. But you'll need one for every variable you'll be tracking.
AI is a step further than that. Neural nets are in fact several PID controllers piped together so that the results make some sense. But games usually don't use neural nets for the simple reason that they take too much time to process. They USUALLY use finite state machines.
Finite State Machines (FSM), Neural Nets, Fuzzy Logic, etc. aren't jargon. They are specific names that you come across while studying computer science. FSMs aren't really simple. A good explanation would take you back to Turing Machines and computational theory.
Now jargon is something different, for example : if you would like more info on FSMs, you should check the dragon :-)
10/07/2004 (7:39 pm)
@FrankPID controls are great when you are monitoring one variable, say the temperature of a reaction that can be adjusted by a valve that controls the heat. But you'll need one for every variable you'll be tracking.
AI is a step further than that. Neural nets are in fact several PID controllers piped together so that the results make some sense. But games usually don't use neural nets for the simple reason that they take too much time to process. They USUALLY use finite state machines.
Finite State Machines (FSM), Neural Nets, Fuzzy Logic, etc. aren't jargon. They are specific names that you come across while studying computer science. FSMs aren't really simple. A good explanation would take you back to Turing Machines and computational theory.
Now jargon is something different, for example : if you would like more info on FSMs, you should check the dragon :-)
#10
If he knew what a finite state machine was then he would not have asked how to solve the problem in the first place. The reason I got after the jargon is because you stated a method for solving the problem, but you did not explain it. Finite State Machine is jargon from the CS field.
Neural nets are not PID. They are a learned mapping system used mostly for non-linear systems. PID is a frequency filter of sorts for linear systems. Hence if you change the mass, power, etc you have to retune. Using a proportional only loop is not math intensive and can be used for very simple problems such as realisticly turning and aiming at an enemy, or steering a vehicle. In addition I just recently saw an article in Game Developer magazine that used PID for more realistic animation control for animals. I also talked to another fellow that created a helicopter sim that was so realistic it was hard to fly. So he implemented a PID loop to control the altitude. After that it was a lot easier to fly.
The reason I mentioned PID or the concept of correcting error is because FSMs are discreet while PID is continuous. Sometimes a continuous solution can help solve certain problems. I did not want to suggest it could replace an FSM. It could used with it for certain aspects of AI control. The way I see it is the more tools in the box the better chance of success.
If I want jargon I will just open my discreet mathematics book! : )
10/08/2004 (3:33 pm)
Bruno,If he knew what a finite state machine was then he would not have asked how to solve the problem in the first place. The reason I got after the jargon is because you stated a method for solving the problem, but you did not explain it. Finite State Machine is jargon from the CS field.
Neural nets are not PID. They are a learned mapping system used mostly for non-linear systems. PID is a frequency filter of sorts for linear systems. Hence if you change the mass, power, etc you have to retune. Using a proportional only loop is not math intensive and can be used for very simple problems such as realisticly turning and aiming at an enemy, or steering a vehicle. In addition I just recently saw an article in Game Developer magazine that used PID for more realistic animation control for animals. I also talked to another fellow that created a helicopter sim that was so realistic it was hard to fly. So he implemented a PID loop to control the altitude. After that it was a lot easier to fly.
The reason I mentioned PID or the concept of correcting error is because FSMs are discreet while PID is continuous. Sometimes a continuous solution can help solve certain problems. I did not want to suggest it could replace an FSM. It could used with it for certain aspects of AI control. The way I see it is the more tools in the box the better chance of success.
If I want jargon I will just open my discreet mathematics book! : )
Katrina Rose
Default Studio Name
With the available pathing in the game all that is already possible. Why would you want to reinvent the wheel? You can make a bot do just about anything. You can make the rules and when they reach a path node you can have them decide (by your rules) to go right or left. You could even change there path so they go off on a new direction. Look in the Aiplayer.cs file in your game/server/scripts directory. There are not many limits to where you can move bots. If you can be more specific I might be able to help further.
Marrion