Skip to main content
GameDev.net gamedev.net
🔒 Locked

Adding Intelligence to a Game

Started by siliconsurfer Nov 1, 2004 at 6:57 AM 3 replies 550+ views
Original Post
siliconsurfer
siliconsurfer
Ok, I'm still designing my game, so this thread may belong in the design section. Anyway I'm trying to design a simple 2D adventure game (think zelda on the nes) So I have a level data file (ascii) this will be read in and an array will be created. The array represents the game map, and holds tile objects. The tile objects contain information on what graphic to display for the tile, and what objects are on the tile (monsters, items, etc) A user should be able to move a character around the map (need to figure out the details of this still!) Anyway I would like the core part of the game to be reusable (I guess we could call it an engine) So I plan to have a base object which can be inherted to create any object in the game such as a monster or an item (is this a good idea, I can't think of any other way to let the engine know what objects it will have to store). MY question is, when I come to add intelligence to the monsters, or someone else does, is it acceptable to inhert a class from the basic object class.. Call it monster, and write functions to give it intelligence. I guess if I was to do it this way, every object would be responsible for themselves, to carry out collision detection etc. I know another method is to use scripting, but I think this would start to make things a little to complicated for me. I'm doing this for a uni project, so I have a limited amount of time. Any thoughts?
FLAGGR
FLAGGR
Okay, I didn't completly comphrehend your question (sorry!) but I think what you meant was how you want to use OO to design all the elements in your game. Well, heres how I do it (works great)

First, you have your basic class, that everything else comes from, or at least uses (mostly) I like to start with a vector (or another word for it would a class that holds a point in space, in your case an x,y value) I like my Vector/Point class to have the ability to add. If your doing this in c++, then I can show some code, if java then your on your own :) In c++ you can object overload, in java just make a class function that adds another point. This is simply

newx = x1 + x2
newy = y1 + y2

where x1,y1 are your first points coordinates and x2,y2 are you seconds. For reference this class is called vec. Now why is this our base class? Because every object exists within space. Next, you would have a enemy class. The pseduo c++ code is:

class enemy {
public:
vec itsPos; // Where it exists in space
vec itsAccel; // It's acceleration, not 100% needed for
// top-down 2d games
vec itsPerspective; // What direction its facing
int ID; // Every player has an ID

// AI functions
int look() {
// What objects can my enemy see based on itsPerspective?
// Select which one is the best target for me to attack
return best-targets-ID
void fire(x,y) {
// Fire bullet aimed at point x,y
};

Okay, well I guess thats not a great tutorial. For your last question, it's fine to write functions for AI. Scripting isn't that hard, but there's no huge benifit for a school project, especially for a 2D game (unless your teacher asked for it) The benifits of scripting are you 1) don't have to recompile all the time. 2) allows for easy mods from players. Do you need those benifits for a uni project? Bleh, no.

Sorry if I made a fool of myself and didn't answer any of your questions right :)
siliconsurfer
siliconsurfer
Well you sort of anwsered my question. Cheers. Just a few more questions on what you wrote though. Is enemey derived (inherted) from the class vector??

If it is, what your saying is create a vector class, which the game engine knows about (I'll have a list to hold vectors), that way it can store any object inherted/derived from it, but it also allows another developer to come along in a few months, create there own objects (characters ,itemsetc) but without having to change the inner workings of the engine?
BEHOLDER192875
BEHOLDER192875
The first thing I would do is create an object class or structures with functions. The idea is to let everything you create to be as open ended as possible. Which means that it could be used for any type of application/game (or in this case a 2D game).

// the base class for all your objects
class Sprite
{
public:
// constructors
// should allow initialization of all variables, and load all
// bitmap sprites

// destructor, deallocate all memory associated with Sprite

// set the objects position
void setPosition(VECTOR2D wv, VECTOR2D lv);

// set the current frame for animation
// you could also return an int specifying whether it was a
// possible change or not, but I prefer that my programs do it
// right regardless, so I don't have an unneeded if statement
void setCurrFrame(UINT frame);

// set the state of the Sprite
void setState(UINT s);

// draws the sprite to the screen, and should check for clipping
void drawSprite(void);

private:
VECTOR2D wPos, // the position of the object in world coords
lPos; // the position of the object on world coord
int id; // the type of object, ie. monster, player, etc.
int state; // dead, alive, wounded, etc.
BITMAP *sprite; // array for each frame of animation
};

Next I would advise that all AI be in a seperate class or in seperate functions that have the sprite as a parameter.

class MonsterAI
{
public:
// constructor
// should take only one parameter, the sprite to be used

// destructor

// sets the type of AI to use, ie. full on attack, defend, etc.
void setstyle(int s);

// performs the actions needed to make the AI do what it needs
// depending on the set style. This should move the sprite in
// accordance with the set style.
void performAI(void);

private:
int style;
Sprite monster;
};

Next is the object which contains only the possibility for collisions.

class Object
{
public:
// constructor, should contain the sprite only

// destructor

// should take all parameters to create a bounding area
void setCollisionArea(int x, int y, ...);

// check for a possible collision
void checkCollision(void);

Sprite object;
};

The reason I did this was because not all Sprites have bounding areas. Only chests, walls, and characters do. Why waste a check for collision if the sprite has no collision area?

AI should be seperate from anything you do. It is it's own entity because not all Sprites will contain AI.

I would prefer to use functions alone for this, but this is just to show an example of what you could do if you wanted to really use classes.

Hope this helps.
oliii
oliii
it's always better to use composition over inheritance when you can. For example, for the BOT thing, you should have a separate A.I. object that affects the bots controls.

the AI system can be a separate object from the entity itself. As for the physics, and the render object... Composition is always better, it gives you a lot more flexibility.

// abstract base classclass CControlObject{};// player controlsclass CPlayerControls: public CControlObject{   CInputs* m_pxIntups;};// abstract AI controlsclass CAIControls: public CControlObject{    CSensors* m_pxSensors;    CMemory*  m_pxMemory;    CAIState* m_pxState;};// general NPC characterclass CAINPC: public CAIControls{};// Tank AIclass CAITank: public CAIControls{};// plane AIclass CAIPlane: public CAIControls{};//------------------------------------------------// Game entities//------------------------------------------------// abstract base classclass CEntity{    CEntity()    {}    CControlObject* m_pxControlObject;    CPhysicsObject* m_pxPhysicsObject;    CRenderObject*  m_pxRenderObject;};// playerclass CPlayer: public CEntity{   CPlayer()   {        SetControlSystem(new CPlayerControls);   }};// bot (AI teammate)class CBot: public CPlayer{   CBot()   {        SetControlSystem(new CAIBot);   }};// Enemy NPCclass CEnemyNPC: public CEntity{    CEnemy()    {        SetControlSystem(new CAINPC);    }};


as for the AI itself, there are lots of solutions. State machines are the obvious one, more fuzzy ones, like Boids (look up on google, "boids" or "autonomous steering", they are quite funky), but you can always combine them together.
Everything is better with Metal.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.