Human decision patterns
I started coding a huge world filled with human NPCs. Unlike regular RPG games, hmm. How should I put this without being doubted it is possible, the NPCs assume the human characteristics. For example:
Some RPG:
1) You enter this game world
2) You do this, beat blah, go to the next boss, and finish the game
Engine I am working on in my SPARE TIMES:
1) You enter this game world
2) You do what you want to do
It''s like Grand Theft Auto freedom.
Problem is. I can make the AI be like GTA so easily.
if(player->shoot==TRUE)
{
NPC->run();
}
And so on.
Some things to clear up. I am gonna have the game be 2D and in fantasy times. (Yup, dragons are real)
Problem is, I need a way to break down the human thinking process. Cause the AI needs to be able to live life and adjust to the player''s actions.
So far, I have basicly broken down the human survival functions. For example, if a fire is in the building, 95% of us will go towards the way we came in through. Some other things like horse theft (this ain''t the future like GTA, lol) should cause different reactions to different people.
All I need to know is a basic list of human functions to include in my code.
October 01, 2003 08:02 PM
Well, it depends on what the goal of your game is. Is it to have a game where the player can do whatever he wants to, and other people just be simulated like GTA, or to simulate the NPC''s behavior as well as possible. If you notice, in GTA, the life simulation isn''t all that complicated. Yeah, people walk around streets and sometimes steal cars, but have you ever followed any of them? They don''t really do anything. So, GTA is not really a good example of realistic human behavior simulation, rather a rather varied experience for the player since it allows for many different actions. So, if you are shooting for something like that, have the simulation part fairly trivial, and concentrate on coding a lot of different things for the player to do. You know, when you say the player can do whatever he wants? Well, he can''t do that if you don''t put it in. 
Now, if you are shooting for a realistuc human simulation behavior, that''s a whole different beast. As a matter of fact, I am doing a program like that for my dessertation. Essentially, it''s a 2D world with human agents in it. The way I went about it is, every agent has a set of basic needs that need to be satisfied, or he dies (like, food, and shelter), and also a set of wants that don''t really have to be satisfied, but the agent wants to satisfy them nonetheless. Now, for each need and want, there is an indicator of how bad the agent wants it or needs it. So, then there is basically a neural net for each agent that chooses which of the needs or wants we want to satisfy next. After a certain number of agents go off to the better world, their nets are collected, put through a fitness function, which evaluates how "well off" the agent was, using a few formulas on how satisfied the needs andwants were, etc. Then, after each net is given a score, they are encoded and put through a genetic algorithm that evolves the topology of the net and the weights on the nodes. That way, the new generation of nets will hopefully be "smarter" and their owners will be better off in the world. So, then, when a new agent is born, he is given a net out of the new generation.
That way, I have achieved a few cool effects: fuzzy logic - the agent doesn''t just choose to satisfy the want with the highest index, but also weighs the possibilities around his place; non-hard-coded behavior - since the behavior of agents is determinde by a neural net, each of them behaves in a unique way (well, that''s not exactly true, of course, but the behavioral differences are noticable and it is very interesting to see the agents form "communities" even though I didn''t code them that way - that''s the way the nets evolve and the agents learn to help each other); also another thing that I achieve that way, is I can view the "personalitites" of each agent. Suppose his net has a high weight on food want. Then he is a gourmand. Suppose the net has high weights on reading. Then the agent could tend to get a scientific0related vocation.
Also, another thing I introduced into the simulation is, when the average fitness of neural nets reaches a certain threshhold, something new is introduced into the world, like a new invention. That way, the agents get new wants, have more choices for occupation, and relaxation, etc. That ultimately lowers the fitness function a little bit, because, say the TV is introduced. Not many people know how to make one. So, few can get it. But everybody wants it. Thus, only the richest agents will be able to satisfy that want and thus will be "better off" or more fit than those that can''t afford a set. Eventually, many people will learn to make TV''s and the market will become saturated, and thus the fitness of the individuals will tend to converge again. That''s when its time for something new.
Ah, any way, that''s a long ramble. Yeah, that''s how I am doing it. But note that in this case, there is no user interaction whatsoever. (Well, you can view stats or change the things that manipulate neural net evolution and such, but you aren''t really participating in the life of the world.) And indeed, I suppose for a game, such a system would definitely be an overkill. That, and it consumes a lot of resources. So you can''t really put fancy graphics on such an engine.

Now, if you are shooting for a realistuc human simulation behavior, that''s a whole different beast. As a matter of fact, I am doing a program like that for my dessertation. Essentially, it''s a 2D world with human agents in it. The way I went about it is, every agent has a set of basic needs that need to be satisfied, or he dies (like, food, and shelter), and also a set of wants that don''t really have to be satisfied, but the agent wants to satisfy them nonetheless. Now, for each need and want, there is an indicator of how bad the agent wants it or needs it. So, then there is basically a neural net for each agent that chooses which of the needs or wants we want to satisfy next. After a certain number of agents go off to the better world, their nets are collected, put through a fitness function, which evaluates how "well off" the agent was, using a few formulas on how satisfied the needs andwants were, etc. Then, after each net is given a score, they are encoded and put through a genetic algorithm that evolves the topology of the net and the weights on the nodes. That way, the new generation of nets will hopefully be "smarter" and their owners will be better off in the world. So, then, when a new agent is born, he is given a net out of the new generation.
That way, I have achieved a few cool effects: fuzzy logic - the agent doesn''t just choose to satisfy the want with the highest index, but also weighs the possibilities around his place; non-hard-coded behavior - since the behavior of agents is determinde by a neural net, each of them behaves in a unique way (well, that''s not exactly true, of course, but the behavioral differences are noticable and it is very interesting to see the agents form "communities" even though I didn''t code them that way - that''s the way the nets evolve and the agents learn to help each other); also another thing that I achieve that way, is I can view the "personalitites" of each agent. Suppose his net has a high weight on food want. Then he is a gourmand. Suppose the net has high weights on reading. Then the agent could tend to get a scientific0related vocation.
Also, another thing I introduced into the simulation is, when the average fitness of neural nets reaches a certain threshhold, something new is introduced into the world, like a new invention. That way, the agents get new wants, have more choices for occupation, and relaxation, etc. That ultimately lowers the fitness function a little bit, because, say the TV is introduced. Not many people know how to make one. So, few can get it. But everybody wants it. Thus, only the richest agents will be able to satisfy that want and thus will be "better off" or more fit than those that can''t afford a set. Eventually, many people will learn to make TV''s and the market will become saturated, and thus the fitness of the individuals will tend to converge again. That''s when its time for something new.

Ah, any way, that''s a long ramble. Yeah, that''s how I am doing it. But note that in this case, there is no user interaction whatsoever. (Well, you can view stats or change the things that manipulate neural net evolution and such, but you aren''t really participating in the life of the world.) And indeed, I suppose for a game, such a system would definitely be an overkill. That, and it consumes a lot of resources. So you can''t really put fancy graphics on such an engine.

Very, very interesting. I messed up on my structures though. I just have 3 status variables: happyness, health, and energy
But I did use a gene data (Basic human genes) like hair color so when they reproduce that they are "different".
Also I added a friend array. All friends are set to 0x80 which means the friendship has 0% interest. 0x0 means that the friend is hated much, and 0xFF means the friend is loved very much.
Now, if(total_good_friends{
if(happy<=20) //Highest happiness is 0xFF
{
//We have a killer
//BTW, total_bad_friends is the total of the friends that "betrayed" this human simu. If you have more people that give you a bad time then a good time, you are gonna kill. (Note, happyness can change this because if one likes to listen to music and gives them enough happynesss to avoid being <= to 20, then they won''t end up like killers.)
Common theory of life.
}
}
I need to redo the human structure and also the loadHUMAN function. But, thanks for the ideas. It''s gonna take a lot of pocessing power as you said, but it''s gonna be fun.
But I did use a gene data (Basic human genes) like hair color so when they reproduce that they are "different".
Also I added a friend array. All friends are set to 0x80 which means the friendship has 0% interest. 0x0 means that the friend is hated much, and 0xFF means the friend is loved very much.
Now, if(total_good_friends
if(happy<=20) //Highest happiness is 0xFF
{
//We have a killer
//BTW, total_bad_friends is the total of the friends that "betrayed" this human simu. If you have more people that give you a bad time then a good time, you are gonna kill. (Note, happyness can change this because if one likes to listen to music and gives them enough happynesss to avoid being <= to 20, then they won''t end up like killers.)
Common theory of life.
}
}
I need to redo the human structure and also the loadHUMAN function. But, thanks for the ideas. It''s gonna take a lot of pocessing power as you said, but it''s gonna be fun.
October 02, 2003 12:10 PM
Hrm, you should get around to learning some of the more common AI algorithms, such as genetic algorithms or the basics of neural networks; your solutions appear too black-and-white (that is, all or nothing). Though I suppose for a small-scale project where "perfect" realism is far from being important, it could work. After all, the general populace doesn''t have access to the source of your program while playing around with it. They won''t know if it turns out looking good enough. 
Though in real life, there''s never such a thing as a black and white behavioral system. While deciding that 95% of the people in a building will run for the nearest exit when there''s a fire and the other 5% will act otherwise can lead to very realistic looking results, having a series of internal states (bravery, mood, intelligence, quick-thinking, so forth) coupled with an awareness of every (at least major) aspect of the immediate vicinity could lead to everyone trying to stop the fire if there were someone charismatic enough leading them as much as them all panicking and dying in the fire.
Look into neural networks, they seem to be a pretty good approximation of what you want to do. You''ll have to do a lot of coding to get the results you want though: like the AP said, if you haven''t coded it, it can''t be done by the player. The game won''t magically generate code to allow the theft of houses if you haven''t coded it

Though in real life, there''s never such a thing as a black and white behavioral system. While deciding that 95% of the people in a building will run for the nearest exit when there''s a fire and the other 5% will act otherwise can lead to very realistic looking results, having a series of internal states (bravery, mood, intelligence, quick-thinking, so forth) coupled with an awareness of every (at least major) aspect of the immediate vicinity could lead to everyone trying to stop the fire if there were someone charismatic enough leading them as much as them all panicking and dying in the fire.

Look into neural networks, they seem to be a pretty good approximation of what you want to do. You''ll have to do a lot of coding to get the results you want though: like the AP said, if you haven''t coded it, it can''t be done by the player. The game won''t magically generate code to allow the theft of houses if you haven''t coded it

You should try and make as much behaviour ''emergent'' as possible.
What I mean by this is that you should start with really really simple rules, and by the invokation of these rules, complex behaviour amongst your NPC''s should emerge.
Take horse stealing for example. Rather than hard code in theft of horses, why not create a simple structure for for all object and assign attributes to an object that would make horse theft a possibility..
People can associate values with objects. Perhaps the owner of an object places a much higher value on it than other people. Someone with low regard for social order may find the object valueable enough to take. This way you encapsulate stealing, and fighting over things, in one shot.
Maybe the horse (an object, which is animate, and has other properties) doesn''t want to stay in the same place and so it runs away...In this way, the horse has stolen itself. The owner of the horse has lost something valuable so pursues it.
So by creating many general attributes and rules you can get some sophisticated looking actions. Just think of a whole bunch of things that you want your world to be capable of enacting, and break those things down in to simple structures and processe
An AI would come in to effect to help your NPC''s plan long term goals, and make their way around the world.
Good luck with your project,
Will
What I mean by this is that you should start with really really simple rules, and by the invokation of these rules, complex behaviour amongst your NPC''s should emerge.
Take horse stealing for example. Rather than hard code in theft of horses, why not create a simple structure for for all object and assign attributes to an object that would make horse theft a possibility..
People can associate values with objects. Perhaps the owner of an object places a much higher value on it than other people. Someone with low regard for social order may find the object valueable enough to take. This way you encapsulate stealing, and fighting over things, in one shot.
Maybe the horse (an object, which is animate, and has other properties) doesn''t want to stay in the same place and so it runs away...In this way, the horse has stolen itself. The owner of the horse has lost something valuable so pursues it.
So by creating many general attributes and rules you can get some sophisticated looking actions. Just think of a whole bunch of things that you want your world to be capable of enacting, and break those things down in to simple structures and processe
An AI would come in to effect to help your NPC''s plan long term goals, and make their way around the world.
Good luck with your project,
Will
------------------http://www.nentari.com
October 02, 2003 01:46 PM
Yes, I totally agree with Will here. (I am AP#1, btw.) I started my project really small. The only things I put in were the need for food, shelter, and the want for reproduction. The world that resulted seemed more like a colony of bacteria than a human world, but as I started adding more and more simple behavioral rules, it seemed that the complexity of the actual behavior increased exponentially! So, it is a very good idea to not try to make this as complex as possible intentionally. Believe me, it will get so complex, you''ll wish it had stayed simple.
So, like with many things in programming, start small, get a solid base, and then expand.

I haven''t had time to add the code to my laptop, but here is the basic component of "living" things.
//Struct #0000
typedef struct _organism organism;
struct _organism
{
organism *prev;
organism *next;
word id;
byte type;
byte happy;
byte energy;
byte IQ;
byte food;
byte freedom;
byte exploration;
byte sleep;
byte love;
byte hate;
byte unique;
byte want;
byte draw;
byte music;
byte read;
byte write;
byte lead;
byte act;
byte fight;
byte unite;
byte op;
byte style;
byte hair;
byte eye;
byte body;
byte function;
byte item[16];
group *friends;
word x;
word y;
word elevation;
byte rotation;
};
...
//Struct Ref-#0000
/*
prev = linked list navigation
next = linked list navigation
id = organism''s order in the linked list
type = type of organism, can be human and so on
happy = happyness
energy = energy supply
IQ = helps organism improvise
food = need for food
expl~1 = need for exploration
sleep = need for sleep
love = need for loving others
hate = need for expressing hate
unique = need to be different from parents or majority
want = need for something which mostly leads to stealing
draw = drawing skill %
music = music skill %
read = reading skill %
write = write skill %
lead = leading capability
act = acting/faking skill %
fight = damage %
unite = uniting skill %
op = operational skills for transportation or tools
style = body setup
hair = hair color
eye = eye color
body = body size
func~1 = occupation or function for animals
item = items the organism has with them
frie~1 = group which this organism relates to
x = x position
y = y position
elev~1 = elevation
rota~1 = rotation
*/
//Struct #0000
typedef struct _organism organism;
struct _organism
{
organism *prev;
organism *next;
word id;
byte type;
byte happy;
byte energy;
byte IQ;
byte food;
byte freedom;
byte exploration;
byte sleep;
byte love;
byte hate;
byte unique;
byte want;
byte draw;
byte music;
byte read;
byte write;
byte lead;
byte act;
byte fight;
byte unite;
byte op;
byte style;
byte hair;
byte eye;
byte body;
byte function;
byte item[16];
group *friends;
word x;
word y;
word elevation;
byte rotation;
};
...
//Struct Ref-#0000
/*
prev = linked list navigation
next = linked list navigation
id = organism''s order in the linked list
type = type of organism, can be human and so on
happy = happyness
energy = energy supply
IQ = helps organism improvise
food = need for food
expl~1 = need for exploration
sleep = need for sleep
love = need for loving others
hate = need for expressing hate
unique = need to be different from parents or majority
want = need for something which mostly leads to stealing
draw = drawing skill %
music = music skill %
read = reading skill %
write = write skill %
lead = leading capability
act = acting/faking skill %
fight = damage %
unite = uniting skill %
op = operational skills for transportation or tools
style = body setup
hair = hair color
eye = eye color
body = body size
func~1 = occupation or function for animals
item = items the organism has with them
frie~1 = group which this organism relates to
x = x position
y = y position
elev~1 = elevation
rota~1 = rotation
*/
I have a little problem with the mapping. Let''s say an NPC needed to find the closest horse. Now, what way should I do this? My original plan was to go through the linked list of organisms and then use the distance formula to find the closest one. Another idea was to use a map. That would be easier, but it would take a lot of memory to map out each person''s X and Y.
So, it''s SPEED VS MEMORY.
Any ideas?
So, it''s SPEED VS MEMORY.
Any ideas?
Thats kind of a tough call.. Having a list of every object in the game world, with their current coordinates, and giving each NPC in the game world total knowledge of this is a little unrealistic, but pretty easy to implement.
As an alternative you could try giving your NPC''s a form of limited memory, or an associative memory.
By limited memory you would only allow them to remember things that are high priority, and also remember regualr things in the short term.. So the NPC would, for a short while, remember everything he runs across, but would also be able to remember a very limited number of things in his long term memory. So if he were hungry, the location of food would stick in his long term memory at the expense of say, the location of a horse, but he would remember everything he ran across in the past 60 seconds.
By associative I mean that rather than remember where a horse is exactly, the NPC would know or remember where horses tend to be (like the stable). So if an NPC were hungry he would go to the market, not to a specific piece of bread.
You could also combine the two methods, and let your NPCs trade information. So if a horse thief wanted a horse he would go to the stables. If there were none there he would ask somebody if he knows where any horses are. The person being asked would spill the contents of his short term memory, and our horse thief would be on his way.
I hope this has made sense.
Will
As an alternative you could try giving your NPC''s a form of limited memory, or an associative memory.
By limited memory you would only allow them to remember things that are high priority, and also remember regualr things in the short term.. So the NPC would, for a short while, remember everything he runs across, but would also be able to remember a very limited number of things in his long term memory. So if he were hungry, the location of food would stick in his long term memory at the expense of say, the location of a horse, but he would remember everything he ran across in the past 60 seconds.
By associative I mean that rather than remember where a horse is exactly, the NPC would know or remember where horses tend to be (like the stable). So if an NPC were hungry he would go to the market, not to a specific piece of bread.
You could also combine the two methods, and let your NPCs trade information. So if a horse thief wanted a horse he would go to the stables. If there were none there he would ask somebody if he knows where any horses are. The person being asked would spill the contents of his short term memory, and our horse thief would be on his way.
I hope this has made sense.
Will
------------------http://www.nentari.com
Hmm, Should i waste 32 bytes for memory, Bunch of MB for maps, or a bunch of processing time for naviagting the linked list :/
I am gonna do what you said. I also started setting up SDL and got myself Dev-C++, but in Dev-C++ it complains about variables inside a function and gives an error. So, I am gonna download the new Dev-C++ cause a I heard they got the problem fixed. (Last time I had the new one, but if fux0red my floppy drive because the program kept trying to access it) So now, I am gonna use another C IDE, but keep the libs.
About the memory solution.
An organism needs to remember these things:
struct memblock
{
byte time; //Time left till memory fades
byte type; //Type of object being remembered: organism, building, resource (food, water, gold, etc.)
word id; //Linked list offset
word x;
word y;
};
That''s 8 bytes rite there, 4*8=32. So 4 things to remember per organism. Good, bad??
I am gonna do what you said. I also started setting up SDL and got myself Dev-C++, but in Dev-C++ it complains about variables inside a function and gives an error. So, I am gonna download the new Dev-C++ cause a I heard they got the problem fixed. (Last time I had the new one, but if fux0red my floppy drive because the program kept trying to access it) So now, I am gonna use another C IDE, but keep the libs.
About the memory solution.
An organism needs to remember these things:
struct memblock
{
byte time; //Time left till memory fades
byte type; //Type of object being remembered: organism, building, resource (food, water, gold, etc.)
word id; //Linked list offset
word x;
word y;
};
That''s 8 bytes rite there, 4*8=32. So 4 things to remember per organism. Good, bad??
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement