I'm getting odd behavior from this script, then a crash after j becomes greater than 2ish. Not sure why. Nothing out of Allegro log either. Am I doing something wrong here? This is the first time I've been messing with enemy arrays.


Sorry for the single letter variable names, I know that it is bad practice, but I'm in a hurry. ...So basically:

i,j - variables for for loops
d,c - variables for the enemies X movement
spacing - X distance the enemy waves are apart from each other.
Code:
ffc script enemy_test{

	void run(){

		int d;int c=240;
		npc e[100];
		int i;int j;int k;
		int spacing=24;
		int loops=spacing*9;

		for(i=0;i<=99;i++)
		{
			e[i]=Screen->CreateNPC(22);
			e[i]->X=-16;e[i]->Y=-16;
		}

		while(true){

			d=0;k=0;

			for(j=0;j<10;j++)
			{

				d=0;k=0;c=240;

				while(d++<540+256)
				{

					Waitframe();

					c-=2;d++;
					if(d%spacing<2 && k<100)k+=10;

					for(i=0;i<100 && k>i;i++)
					{
						if(i%2==0){
							if(e[i]->isValid())
							{
								if(i<=10)e[i]->X=d;
								else if(i<=20)e[i]->X=d-spacing;
								else if(i<=30)e[i]->X=d-spacing*2;
								else if(i<=40)e[i]->X=d-spacing*3;
								else if(i<=50)e[i]->X=d-spacing*4;
								else if(i<=60)e[i]->X=d-spacing*5;
								else if(i<=70)e[i]->X=d-spacing*6;
								else if(i<=80)e[i]->X=d-spacing*7;
								else if(i<=90)e[i]->X=d-spacing*8;
								else e[i]->X=d-spacing*9;


								e[i]->Y=(i%9)*16;
							}
						}
						else{
							if(e[i]->isValid())
							{
								if(i<=10)e[i]->X=c;
								else if(i<=20)e[i]->X=c+spacing;
								else if(i<=30)e[i]->X=c+spacing*2;
								else if(i<=40)e[i]->X=c+spacing*3;
								else if(i<=50)e[i]->X=c+spacing*4;
								else if(i<=60)e[i]->X=c+spacing*5;
								else if(i<=70)e[i]->X=c+spacing*6;
								else if(i<=80)e[i]->X=c+spacing*7;
								else if(i<=90)e[i]->X=c+spacing*8;
								else e[i]->X=c+spacing*9;


								e[i]->Y=((i%9)+1)*16;
							}
						}

					}
					for(i=k;i<100;i++)
					{
						if(e[i]->isValid())
						{
							e[i]->X=-16;e[i]->Y=-16;
						}
					}
				}

				for(i=0;i<100;i++)
				{
					if(e[i]->isValid())
					{
						e[i]->X=-16;e[i]->Y=-16;
					}
					else
					{
						e[i]=Screen->CreateNPC(22);
						e[i]->X=-16;e[i]->Y=-16;
					}
				}
			}

		}
	}
}