I'm looking for an optimization for my loops searching for enemies in a tile based horizonal scroller. My enemy array is in a struct and I am looping through the array with 3 loops. First loop finds the enemy on the left side of the screen within the array, the second loop finds the enemy on the right side of the screen. The third loop then displays all enemies that fall within the two parameters.
This routine certainly seems to do the job, but it seems too bulky being 3 loops. Any suggestions for better optimizations to cull down the code length or improvements in speed?
void apple()
{
unsigned char baddyStart = 0;
unsigned char baddyEnd = 0;
unsigned char enemyLow = 0;
unsigned char enemyHigh = 0;
unsigned char windowStart = player.tile_X_position - 1;
unsigned char windowEnd = player.tile_X_position + NEAR_PLAYER;
unsigned char arrayScan = 0;
if (windowEnd > MAX_PLAYER_POS)
{
windowEnd = MAX_PLAYER_POS;
}
//need to find enemy closest to player in window
for (temp1 = 0; temp1 < enemies_per_level; temp1++)
{
baddyStart = enemy_locations[temp1].tile_X_position;
if (baddyStart == windowStart)
{
//found the lowest location in window
enemyLow = temp1;
break;
}
}
//find the enemy furthest to player in window
for (temp2 = temp1; temp2 < enemies_per_level; temp2++)
{
baddyEnd = enemy_locations[temp2].tile_X_position;
if (baddyEnd == windowEnd)
{
//found the highest location in window
enemyHigh = temp2;
break;
}
}
//show the ranges
printf ("enemyLow = %d\n", enemyLow);
printf ("enemyHigh = %d\n", enemyHigh);
printf ("baddyStart = %d\n", baddyStart);
printf ("baddyEnd = %d\n", baddyEnd);
arrayScan = enemyLow;
while (arrayScan <= enemyHigh)
{
baddyStart = enemy_locations[arrayScan].tile_X_position;
if(baddyStart >= baddyEnd)
{
//printf("GREATER THAN\n");
return;
}
if (baddyStart == 0)
{
//printf("NOT EXISTING\n");
//baddy deleted
}//to bypass deleted baddies
printf("arrayScan=%d \n", arrayScan);
printf("%d ", enemy_locations[arrayScan].tile_X_position);
printf("%d ", enemy_locations[arrayScan].x_displacement);
printf("%d ", enemy_locations[arrayScan].y_position);
printf("%d ", enemy_locations[arrayScan].sprite_number);
printf("%d ", enemy_locations[arrayScan].movement);
printf("%d ", enemy_locations[arrayScan].energy);
printf("%d ", enemy_locations[arrayScan].param1);
printf("%d ", enemy_locations[arrayScan].param2);
printf("\n");
arrayScan ++;
}
}
EDIT.
I can bring the function a little closer to optimization by combining the find the minimum and maximum to 1 loop instead of 2 loops.
void apple()
{
unsigned char baddyStart = 0;
unsigned char baddyEnd = 0;
unsigned char enemyLow = 0;
unsigned char enemyHigh = 0;
unsigned char windowStart = player.tile_X_position - 1;
unsigned char windowEnd = player.tile_X_position + NEAR_PLAYER;
unsigned char arrayScan = 0;
if (windowEnd > MAX_PLAYER_POS)
{
windowEnd = MAX_PLAYER_POS;
}
//need to find enemy closest and furthest to player in window
for (temp1 = 0; temp1 < enemies_per_level; temp1++)
{
baddyStart = enemy_locations[temp1].tile_X_position;
baddyEnd = enemy_locations[temp1].tile_X_position;
if (baddyStart == windowStart)
{
//found the lowest location in window
enemyLow = temp1;
}
if (baddyEnd == windowEnd)
{
//found the highest location in window
enemyHigh = temp1;
break;
}
}
//show the ranges
printf ("enemyLow = %d\n", enemyLow);
printf ("enemyHigh = %d\n", enemyHigh);
printf ("baddyStart = %d\n", baddyStart);
printf ("baddyEnd = %d\n", baddyEnd);
arrayScan = enemyLow;
while (arrayScan <= enemyHigh)
{
baddyStart = enemy_locations[arrayScan].tile_X_position;
if(baddyStart >= baddyEnd)
{
//printf("GREATER THAN\n");
return;
}//seems working
if (baddyStart == 0)
{
//printf("NOT EXISTING\n");
//baddy deleted
}//to bypass deleted baddies
printf("arrayScan=%d \n", arrayScan);
printf("%d ", enemy_locations[arrayScan].tile_X_position);
printf("%d ", enemy_locations[arrayScan].x_displacement);
printf("%d ", enemy_locations[arrayScan].y_position);
printf("%d ", enemy_locations[arrayScan].sprite_number);
printf("%d ", enemy_locations[arrayScan].movement);
printf("%d ", enemy_locations[arrayScan].energy);
printf("%d ", enemy_locations[arrayScan].param1);
printf("%d ", enemy_locations[arrayScan].param2);
printf("\n");
arrayScan ++;
}
}
```