Mystery array solved

12-25-2017
When I emailed Antonio, we had discussed several items. I am writing about them one at I time as I explore their significance. The last item I detailed was the placement of the main players sprite. This time however, we will discuss a mystery array near the start of the code.

Near the start of the entire code is an array that is layout in hex.

unsigned char data[20]= 
{
  0x00, 0x42, 0x11, 0,
  0x08, 0x60, 0x60, 2,
  0x09, 0x58, 0x48, 3,
  0x0a, 0x22, 0x02, 1,
  0x0c, 0x50, 0x6e, 2
};// dont know what this represents

Of course, the comment is mine, I don’t know what the array is for, but someone does and that person is Antonio. So part of my email was asking him about it. The answer came quick.

This is the initialization array for the sprites.

Well, perhaps it should have been obvious, but I was quite dull when I asked, if you look further in the code you will see.

// pass data to sprites
for ( i = 0; i < 5; i++ )
{
	sprites[i].n = data[0 | i<<2],
	sprites[i].x = data[1 | i<<2],
	sprites[i].y = data[2 | i<<2],
	sprites[i].f = data[3 | i<<2];
}

There’s that data array and we can see that the array does deal with sprites and bullets.

Antonio further commented

Each line is one sprite. The .n is the number of sprites (will draw a different sprite if change), the .x and .y are the coordinates (will change of position if modified), and the .f has no effect on the engine, but it’s used by the demo to store the direction of movement (it’s diagonal movement so only 4 directions).

I should have been blinded by the obvious.

What we will want to accomplish is something similar to our main character customer placement. We want to have a custom enemy placement per level. Since each level will be different, we want to place the enemies in an appropriate place. We don’t want the enemies to be placed right on top of your character immediately. If you do you would lose a life immediately.

With that goal in mind, let’s play around with our code, much like a toddler plays with their food.

When the game starts, the enemies move immediately, so that if we try to track the value or figure out what is going on, it moves way too fast.

And after a minute of experimentation, I find that if we place a pause immediately after the game starts, nothing updates on the screen.

Time to reintroduce our temp variable, bob.

We are going to initialize bob

unsigned char bob = 0

At the end of our main game loop, we will place the following.

if (bob == 1)
{
	Pause(0);
}
bob++;

So, the game will go through 1 loop and then pause the game forever….Bwahaaaa.

Sorry, my evil laugh came out again.

Here is a freeze frame

Now, let’s overlay a grid

And now let’s highlight our enemy sprites

Our first enemy is on the 5th square to the left and 1st block below the score box, so 5 x and 1 y.

Our second enemy is 12 x and 9 y.

Our third enemy is 13x and 12 y.

Our fouth enemy is 10x and 14 y.

As we discussed prior, Z88DK does not pass the hex data in a function, rather it passes decimal numbers.

Let’s rewrite the initilazion as whole numbers.

unsigned char data[20]= 
{
  0, 66, 17, 0,
  8, 96, 96, 2,
  9, 88, 72, 3,
  10, 34, 2, 1,
  12, 80, 110, 2
};

And, the program still runs the same way.

As a bit of guesswork, let’s look at our grid and enemy placement to see what matches.


unsigned char data[20]= 
{
  0, 66, 17, 0, <-- your character
  8, 96, 96, 2, <--3rd enemy
  9, 88, 72, 3, <--2nd enemy
  10, 34, 2, 1,  <--1st enemy
  12, 80, 110, 2 <--4th enemy
};

The numbering I have assigned to the enemy number is not really relative, we could just say that the 3rd enemy is actually enemy 1, but I named the enemy from distance from the top.

Now let’s play around with what I have labeled the first enemy

unsigned char data[20]= 
{
  0, 66, 17, 0,
  8, 96, 96, 2,
  9, 88, 72, 3,
  10, 5, 8, 1,
  12, 80, 110, 2
};

Now the enemy sprite is in the corner.

With that done, let’s place all of the enemies in corners.

unsigned char data[20]= 
{
  0, 66, 17, 0,
  8, 5, 8, 2,
  9, 230, 8, 3,
  10, 5, 150, 1,
  12, 230, 150, 2
};

Nice, that works just dandy.

Of course, now you should know where I’m going with this. Let’s place it all in a function so that we can place each enemy in a specific position per level.

We are going to also have to pay attention to passing the data to the sprites themselves.

for ( i = 0; i < 5; i++ ){
    sprites[i].n= data[0 | i<<2],
    sprites[i].x= data[1 | i<<2],
    sprites[i].y= data[2 | i<<2],
    sprites[i].f= data[3 | i<<2];
}

With that all in mind, here is the function I came up with

void enemyStart(short level)
{
	short Level = level;
	
	if (Level == 0)
	{
		//enemy 1 - 8, 8, 8, 3
		data[4] = 8;// f to store the direction of sprite
		data[5] = 8;//x position
		data[6] = 8;//y position
		data[7] = 2;//sprite number
		
		//enemy 2 - 9, 230, 8, 3		
		data[8] = 9;// f to store the direction of sprite
		data[9] = 230;//x position
		data[10] = 8;//y position
		data[11] = 3;//sprite number
		
		//enemy 3 - 10, 5, 150, 1		
		data[12] = 10;// f to store the direction of sprite
		data[13] = 5;//x position
		data[14] = 150;//y position
		data[15] = 1;//sprite number
		
		//enemy 2 - 12, 230, 150, 2		
		data[16] = 12;// f to store the direction of sprite
		data[17] = 230;//x position
		data[18] = 150;//y position
		data[19] = 2;//sprite number
		M_OUTP(0xfe, 1);//changes border color
	}

	for ( i = 0; i < 5; i++ )
	{
		sprites[i].n = data[0 | i<<2],
		sprites[i].x = data[1 | i<<2],
		sprites[i].y = data[2 | i<<2],
		sprites[i].f = data[3 | i<<2];
	}
}

And our calling function

enemyStart(level);

This works just great.

Here is what our sprites look like.

As it looks, our sprite 0, our main character, has 8 slots, our sprites 1 through 3 has 2 slots each.

I think we might explore that next.

Merry Christmas everyone.

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