#include <io.h>
#include <system.h>
#include <stdio.h>

#define IOWR_SRAM_DATA(base, offset, data) \
  IOWR_16DIRECT(base, (offset) * 2, data)
#define IORD_SRAM_DATA(base, offset) \
  IORD_16DIRECT(base, (offset) * 2)
#define IOWR_SRAM_SPEED(base, data) \
  IOWR_16DIRECT(base + 32, 0, data)

int main()
{

    int flag = 0;   // use to determine what state are we


    int time = 0;   // time count
    int score = 0;  // score count

    int deadFlag;  //indicates player has been created
    int playerXCoordinate;   //player's leftmost upmost coordinate
    int playerYCoordinate;
    int life;   //indicate how many life still left

    int bossFlag;    //indicates boss has been created
    int bossXCoordinate; //boss's leftmost upmost coordinate
    int bossYCoordinate;

    int enemyCount; // number of enemies in the screen up to 8 enemies

    int enemyFlag[9];
    int enemyXCoordinate[9];  //enemies' leftmost upmost coordinate
    int enemyYCoordinate[9];

    int bulletFlag; //indicate bullet created
    int bulletXCoordinate;
    int bulletYCoordinate;


    int hitXCoordinate; //where to put on hit effect
    int hitYCoordinate;

    int collisionXCoordinate;   //where to put on collision effect
    int collisionYCoordinate;

    int gameoverXCoordinate;    //where to put on game over picture
    int gameoverYCoordinate;

    int playerXSpeed;
    int playerYSpeed;
    int enemyXSpeed;
    int enemyYSpeed;
    int bossXSpeed;
    int bossYSpeed;
    int bulletXSpeed;

    int xLowBound = 0;
    int xHighBound = 640;
    int yLowBound = 0;
    int yHighBound = 480;
    //int sound = 0;

    int counter;	//increase the time of the loop
    int counter2;
    int shine =0;
    //int silence = 0;
    //int sound = 0;


    //int soundByte = IORD_16DIRECT(audio_BASE, 0);
    //printf(soundByte);

    //flag = GET_FLAG get flag from hardware


    playerXCoordinate = 40;
	playerYCoordinate = 250;
	bossXCoordinate = 600;
	bossYCoordinate = 1700;
	bulletXCoordinate = 600;
	bulletYCoordinate = 480;

	playerYSpeed = 0;
	enemyXSpeed = 2;
	bossXSpeed = 1;
	bossYSpeed = 0;
	bulletXSpeed = 2;

	deadFlag = 0;

	counter = 10000;
	counter2 = 40000;
	int i = 0;

	for(i = 0; i < 10; i++){
		enemyFlag[i] = 0;
	}
	enemyCount = 0;
	bossFlag = 0;







    if (flag == 0) {

        //implement normal procedure(move down the player)




        for (; ; ) {

        	//printf("%d", time);
        	counter--;
        	counter2--;

        	if(counter2 == 0){
        		counter2 = 215000;
        		time = time + 1;

        		printf("%d seconds since game starts \n", time);
        	}


        	if(counter == 0){

        		counter = 10000;

        		//increment score
				score = score + 10;
				//display score
				//IOWR_SRAM_DATA(VGA_BASE,,score);




				//player moving
				if(playerYCoordinate - yLowBound <= 10){
					playerYSpeed = - playerYSpeed;
				}
				if(yHighBound - playerYCoordinate <= 90){
					playerYSpeed = - playerYSpeed;
				}

				playerYCoordinate = playerYCoordinate - playerYSpeed;

				//super mode after dead
				if(deadFlag == 1){
					if(shine == 30 || shine == 45 || shine == 60 || shine == 75 || shine ==90 || shine == 105 || shine == 120 || shine == 135 || shine ==150 ){
						IOWR_SRAM_DATA(VGA_BASE, 2, 800);
						IOWR_SRAM_DATA(VGA_BASE, 13, 800);
						shine = shine + 1;
					}else if(shine == 180){
						IOWR_SRAM_DATA(VGA_BASE, 2, 800);
						IOWR_SRAM_DATA(VGA_BASE, 13, 800);
						deadFlag = 0;
					}else{
						IOWR_SRAM_DATA(VGA_BASE, 2, playerXCoordinate);
						IOWR_SRAM_DATA(VGA_BASE, 13, playerYCoordinate);
						shine = shine + 1;
					}
				}else{
					IOWR_SRAM_DATA(VGA_BASE, 2, playerXCoordinate);
					IOWR_SRAM_DATA(VGA_BASE, 13, playerYCoordinate);
				}




				int randNumber = rand()%100;
				//enemy created
				if(randNumber  > 97 && enemyCount < 10){
					//rand is from 0-99, thus 0.01% runs created enemy
					//create enemy of No.(enemyCount + 1)
					enemyCount++;
					if(randNumber == 98){
						enemyXCoordinate[enemyCount] = 620;
						enemyYCoordinate[enemyCount] = 120;

						printf("an up enemy emerge, there are %d enemies \n", enemyCount);
					}

					if(randNumber == 99){
						enemyXCoordinate[enemyCount] = 620;
						enemyYCoordinate[enemyCount] = 360;

						printf("an down enemy emerge, there are %d enemies \n", enemyCount);
					}
					//assign enemy position
					enemyFlag[enemyCount] = 1;

				}


				//enemy engaging
				for(i = 0; i < 10; i++){
					if(enemyFlag[i] == 1){
						enemyXCoordinate[i] = enemyXCoordinate[i] - enemyXSpeed;
						if(enemyXCoordinate[i] < 0){
							enemyFlag[i] = 0;
							enemyCount --;

							printf("an enemy passed, there are %d enemies \n", enemyCount);
						}
						//give it to hardware
					}
				}



				//boss created
				if(time > 5 && time < 10 && bossFlag == 0){
					//create boss
					bossFlag = 1;
					//assign position

			        IOWR_SRAM_DATA(VGA_BASE, 0, bossXCoordinate);
			        IOWR_SRAM_DATA(VGA_BASE, 15, bossYCoordinate);
				}

				//boss engaging
				if(bossFlag == 1){

			        if(bossYCoordinate - yLowBound <= 10){
			        	bossYSpeed = -bossYSpeed;
			        }
			        if(yHighBound - bossYCoordinate <= 90){
			        	bossYSpeed = -bossYSpeed;
			        }


					bossXCoordinate = bossXCoordinate - bossXSpeed;
					bossYCoordinate = bossYCoordinate + bossYSpeed;

					if(bossXCoordinate < 0){
						bossFlag = 0;

				        IOWR_SRAM_DATA(VGA_BASE, 0, 800);
				        IOWR_SRAM_DATA(VGA_BASE, 15, 800);
					}else
					{

						//give it to hardware
				        IOWR_SRAM_DATA(VGA_BASE, 0, bossXCoordinate);
				        IOWR_SRAM_DATA(VGA_BASE, 15, bossYCoordinate);
					}


				}



				// create bullet
		        if(bulletXCoordinate == 600){
		        	bulletXCoordinate = 80;
		        	bulletYCoordinate = playerYCoordinate + 50;
		        	bulletFlag = 1;
		        }

				//bullet engaging
				if(bulletFlag == 1){

					bulletXCoordinate = bulletXCoordinate + bulletXSpeed;

//					if(bulletXCoordinate > xHighLimit){
//						bulletFlag = 0;
//					}
					//give it to hardware

			        IOWR_SRAM_DATA(VGA_BASE, 1, bulletXCoordinate);
			        IOWR_SRAM_DATA(VGA_BASE, 14, bulletYCoordinate);
				}


				//hit happens
				//f (bossFlag == 1 && bulletXCoordinate == enemyXCoordinate1 && bulletYcoordinate == enemyYcoordinate1) {

						//assign hit position

						//score = score + 100;

						//create hitposition

						//give it to hardware
					//}

					//implement rest of the enemy hit

					//}


				//collision happens
				if(deadFlag == 0 && bossFlag == 1 && bossXCoordinate - playerXCoordinate>0 && bossXCoordinate - playerXCoordinate <= 50 && abs(bossYCoordinate - playerYCoordinate) <= 70){
					//assign collision happens
					printf("player is dead");
					deadFlag = 1;

					life = life - 1;

					//create collision effect
					//give it to hardware
				}

				//game over happen
				if(life == 0){
					//create game over
					printf("no life. Game over");
					//give it to hardware
				}



				//finish game
				if(time > 10000){
					// player survive
					score = score + life * 1000;
					//printf("250s time has past. Game over");
					// give it to hardware
				}


        	}


        }











    }else if(flag == 1){
        //implement move procedure(move up the player)
    }
    else{
        //implement shot procedure(create bullet)
    }
}
