NPCs and Random MovementIn a really well-done game, NPCs will probably be doing something suitable to who they are. Old men and women won't move around much, kids will run through the town playing, and thieves will quickly walk away whenever they notice anyone paying too much attention. But to do all that, we either need to hard-code a bunch of behavior for each NPC type (which is not a good idea), or use a script (which we don't know how to do yet). So for now, let's start off our NPCs by implementing the simplest form of behavior that's found in lots of old RPGs -- random movement. The nice part about implementing NPCs is that we can use a lot of the same code that we used for setting up the player. Our function that checks whether or not a character can move to a certain position can be used for NPCs, and the code we just wrote for actually plotting characters can also be used for NPCs, with one minor change. We'd just have to stick it in a function that takes one argument: the y-value to use for the source So what's left to do? First we'll need to set up some sort of system that determines random movements for the NPC. The simplest way to do it is just to choose a random number, and if it falls in a certain range, or is divisible by a certain number, then the character should move, so we pick a random direction and that's it! The code would look something like this: // choose a random number int nDir; int nRand = rand(); // move if 128 divides nRand (bits 0-6 used) and NPC is still if (((nRand & 0x0000007F) == 0) && (move.xMove == 0) && (move.yMove == 0)) { // use bit 7 to determine positive (south, east) or negative (north, west) movement nDir = ((nRand & 0x00000080) >> 6) - 1; // use bit 8 to determine horizontal or vertical movement if ((nRand & 0x00000100) == 0) { // move vertically if (MoveOK(move.xTile, move.yTile + nDir)) { move.yMove = nDir; move.yTile += nDir; move.yOffset -= (nDir << 5); // remember, this is multiplication by 32 move.nFace = ((nDir == 1) ? 0 : 3); } } else { // move horizontally if (MoveOK(move.xTile + nDir, move.yTile)) { move.xMove = nDir; move.xTile += nDir; move.xOffset -= (nDir << 5); move.nFace = ((nDir == 1) ? 6 : 9); } } } All right, I'm using some pretty odd-looking code in there to people who don't use bitwise operations a lot, so I'll clear it up a little. The bitwise AND operator Anyway, this is all it takes to get NPCs moving around, and we already said that we could use our player drawing function to draw NPCs as well. So we're finished, right? Well, not quite. |