4.2. NPCs - BASIC "THINKING" - SENSE Intro
A NPC (Non-Player Character) is an abstraction of a living being. He/She senses its surroundings [Sense], think about what he/she has sensed [Think] remembering previous events [Memory], and reacts to the stimulus of the world [Do] storing his/her behavior for future reasoning [Feedback]. In the previous lesson we saw (in a very superficial way) how a NPC acts [Do]. Now, it's time to give some hints about how Senses work. "Senses" - See Now you are looking at a computer screen (i suppose :-P). But... How do you know that your are looking at a computer screen? What does your body do? Well, I'm no biologist an I left high school a lot of time ago :-P. But as a good computer scientist, i can simplify(1) ^_- : First, your eyes "sense" the light in your viewing area (180 degrees, with 140 degrees of binocular vision [or "3D" vision]), and sends the information to your brain. Your brain is capable of creating a sort of "image". Finally, from that "image", you recognize a computer screen(2).
And how vision works for a NPC? Simplifying everything. The main purpose of the vision subsystem is to send to the NPC the entities he/she is viewing at a certain moment of time. How to make that? First, the NPC must create a set of entities he/she is looking at. But this is not made making an "image" of the world. Instead, the NPC just "throw rays" (makes a bresenham algorithm) in all the directions of his/her vision field for a certain length (which can be modified depending on your design decisions). And if the ray "hits" an entity, it's added to the entity list. This can be simplified more, making the NPC to directly try to "throw a ray" into all entities in his/her vision range. And second, the NPC must recognize the entities. But there is no image recognition here. Every entity in the universe has a sort of label (like the "cat" in the figure: "Cat - NPC - ID 2000"), and that label is used for recognizing what is the entity. A final note: If you have played any game of the "Commandos" franchise, you will remember the option of knowing the range of vision of a certain soldier. Now, you can understand a little more how it works (alarm, alarm!!! ^_-). "Senses" - Hear + Smell In nature, sound can be simplified into sound waves that travels across the air, and our ear is capable to sense those waves and translate the waves into sound. If you want a fast "example", just throw a stone into water (the stone creates a "sound" = waves), and see the waves moving in the surface (like sound waves moving through the air). Smelling things is similar: the scent of something "travels" across the air, and our nose catch it and sense it.
So, how we can abstract sounds and scents? All sounds and scents have an origin. And both of them can be abstracted into a circle (wave) that grows until the sound wave (or scent fades away). Therefore, in a game, a sound or/and scent can be a circular wave which is generated when an entity makes a sound/propagate an scent. This wave carries some information about the type of sound/scent. And when the limit of the wave hits another entity, that entity receives the information contained in the wave. Also (simplifying things), a sound/scent can be just a "ray" being thrown by the entity who generates it to all entities in the sound/scent range. Another final note: "Commandos" again. Remember the sound wave made by your commandos when you were running? ^_-. "Senses" - Touch + Taste These two senses are easy to abstract. Only when you touch or eat things you can know about the features of what you touched/ate. In that case, when the player selects an item in your inventory or steps into a certain area, you tell him/her some information. And about food, you can let the player get a feeling about the taste of things only when he/she eat them. "Senses" - Internal State of a NPC
Okay, now a NPC can see, hear, and smell its sorroundings, and can touch and taste. But there is one important sense to develop - the internal state of a NPC. It tells how the NPC feels, both physically and psychologically. If the NPC is hungry (he can try to find food), if the NPC is in danger (just look at the picture - guess the NPC should get out of that place immediately!),... For the physical feelings, you only have to check for changes in the physical statistics of the NPC (e.g. decreasing hit points). For the psycological issues... well, it's more complicated (depends on rules: If the NPC is continously losing HP, he will be veeeery veeeery scared), and i also doesn't know how to manage them properly. So take a look to the work of the professionals: Just search for the postmortem of any game with really cool AI, like "The Sims", "Black and White", and "Fable" ^_-. Notes
1 Biologists of the world and (my) old high school teachers: forgive me for what I'm going to say...
2 ...Yes, I was good at biology... no, I'm not kidding!