I’m feeling a movement part 3 – Artifical Stupidity

12-29-2017
Well, we last wrote about the logic that the demo used to make the enemies bounce all around the screen.

And then, I unceremoniously deleted it.

I just didn’t want enemies to bounce all over the screen, what I wanted was to have your enemies pursue you.

So, right where we left off. In the same section that we deleted, let’s add

// to be added for artificial stupidity
playerXpos = sprites[0].x;
playerYpos = sprites[0].y;
if (sprites[1].x > playerXpos)
{
sprites[1].x--;
}
if (sprites[i].x playerYpos)
{
sprites[i].y--;
}
if (sprites[i].y < playerYpos)
{
sprites[i].y++;
}

This is for testing, as it only moves 1 sprite.

Of course, this can be expanded to encompass all of the sprites, by substituting the 1 for i, such as

// to be added for artificial stupidity
if (sprites[i].x > playerXpos)
{
sprites[i].x--;
}
if (sprites[i].x playerYpos)
{
sprites[i].y--;
}
if (sprites[i].y < playerYpos)
{
sprites[i].y++;
}

I can tell you immediately, that this needs to go into a function, it’s going to eventually be a long sequence of If’s that it will be awkward to place it in the main loop.

For right now, there need not be any variables sent to the array, it can be simply.

void enemyAI()

and of course, to call it

enemyAI();

Be sure to remove the sequence of commands from your main loop, compile and run. Works ok.

What you will now notice, is that all of the sprites will move toward you and stop right on top of you. If you move they will continue to follow you. Kinda neat. However, for a game, we need to have a variety of motion. Not just have the enemy rush you constantly. That would get kinda tiring.

What we need is for the enemies to make decisions.

We don’t need thoughtful decisions, just decisions.

Introducing our randomize function

short RND(short range) {
return (short)(((long)(range) * rand()) / (long)(RAND_MAX));
}

And if we call it via

decision = RND(2);

It’s like tossing a coin. The randomization is from 0 to 1, making the randomization even larger, will make the randomization range even longer, just be aware that it’s (0 to N) – 1, with N being the number you feed into the function.

At this juncture, we now have a basic AS (artificial stupidity). The enemy either constantly pursues you or it waits and does nothing.

So I think of other movements our enemy can make and I came up with a few.

Move the enemy toward the players X and Y
Move the enemy toward the players Y
Move the enemy toward the players X
Do nothing
If the enemy Y position is greater than the player’s Y position, move towards the players X
If the enemy X position is greater than the player’s X position, move towards the players y

Now the last two seem like a good idea, however, the effect seems to be that the enemy stumbles all over the place and appears to be drunk. While it’s kinda funny to look at, it makes for terrible game-play as it appears to be a game of shoot the drunk!

Below, you will find the code for the enemy movement including shoot the drunk.

Just for reference, I am taking out the shoot the drunk logic and reducing the random to a 5.

void enemyAI()
{
short decision;
playerXpos = sprites[0].x;
playerYpos = sprites[0].y;
decision = RND(5);//decision = 3;//
if (decision == 0)
{
//do nothing
}
if (decision == 1)//move toward player y
{
if (sprites[i].y > playerYpos)
{
sprites[i].y--;
}
if (sprites[i].y playerXpos)
{
sprites[i].x--;
}
if (sprites[i].x playerXpos)
{
sprites[i].x--;
}
if (sprites[i].x playerYpos)
{
sprites[i].y--;
}
if (sprites[i].y playerXpos + 16)
{
sprites[i].x --;
}
if (sprites[i].x playerYpos + 16)
{
sprites[i].y --;
}
if (sprites[i].y playerXpos)
{
sprites[i].x++;
}
if (sprites[i].x playerYpos)
{
sprites[i].y++;
}
if (sprites[i].y < playerYpos)
{
sprites[i].y--;
}
}*/
}

Now, if you like the shoot the drunk idea, then just keep the code as it is and enjoy it.

The next part, while interesting, does not fall readily under the function as we have defined it. Enemy retreating.

The code for it is really just an opposite of what we have done.

{
//retreat from player
if (sprites[i].x > playerXpos)
{
sprites[i].x++;
}
if (sprites[i].x playerYpos)
{
sprites[i].y++;
}
if (sprites[i].y < playerYpos)
{
sprites[i].y--;
}
}

If we include this as an option with our randomization, then again our enemies will look drunk as they can’t make an intelligent decision on if they should come towards you or run away from you and depending on the roll of the dice, they could do this action of going back and forth a number of times.

What we will need to do is reformulate our function to decide when to attack or when to retreat. That will have to wait for another day.

Advertisements

Author: andydansby

I'm a hobbyist coder working with the ZX Spectrum. Living in New York state near the Syracuse area. I grew up in Virgina. The first computer my parents bought for me was a Timex Sinclair 2068.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s