#include "hostAPI.txt"

#include "global.txt"

var count;

var i;

var iEndCount;

var tmpEnemyID;

var bStageAVG;

struct sEnemy1 {
	var eid;
	
	var x;
	var y;
};

#define MAX_ENEMY_COUNT 256;

var iEnemyIDs[MAX_ENEMY_COUNT];

func updateEndState() {	
	if(iEndCount != -1) {
		++iEndCount;
		if(iEndCount > 90) {
			htStageEnd();
			iEndCount = -1;
		}
	}
}

func onBossDie() {
	htClearBackgroundCover();
	iEndCount = 0;
}

func createBossQiyu() {
	htCreateBoss("Qiyu", 10, "7yu.png", 0, 0, 128, 128, 3);
	htSetBossPos(400, 100);
	htSetBossMovementRect(128, 0, 128, 128, 2, 256, 0, 128, 128, 2);

	htSetBossDieAnim(512, 0, 128, 128, 1);
	htAddBossAnim(0, 384, 0, 128, 128, 1);
	
	htSetBossMagicCircleColor(0.0, 0.0, 0.5, 1.0);
	
	htAddBossAttackScript("scripts/stage1/qiyu/qiyu-1-hard.txt", 600, 1000, 200000, true);
//		htAddBossAttackScript("scripts/myscript/qiyu-1-normal.txt", 500, 1000, 50000, false);
	htAddBossAttackScript("scripts/stage1/qiyu/qiyu-3-hard.txt", 600, 1000, 250000, true);

	htSetBossNameboxColor(1.0, 0.2, 0.5, 1.0);
}


func init() {
	count = 0;
	i = 0;
	
	bStageAVG = false;
	
	iEndCount = -1;
	
	iEnemyIDs[0] = createCake(300, -50, 30, "", 0, 0, 180);
	htMoveEnemyTo(iEnemyIDs[0], 300, 150, 0, 60);
	iEnemyIDs[1] = createCake(500, -50, 30, "", 0, 0, 180);
	htMoveEnemyTo(iEnemyIDs[1], 500, 150, 0, 60);
	
	for(i=0; i<8; ++i) {
		tmpEnemyID = createCake(200, 620, 5, "cakeFanAttack1_3_60_t120", 40, 5, 450+i*20);
		htMoveEnemyTo(tmpEnemyID, 200, -50, 0, 180);
		tmpEnemyID = createCake(600, 620, 5, "cakeFanAttack1_3_60_t120", 40, 5, 450+i*20);
		htMoveEnemyTo(tmpEnemyID, 600, -50, 0, 180);
	}
	
	iEnemyIDs[2] = createCake(250, -50, 40, "", 0, 0, 870);
	htMoveEnemyTo(iEnemyIDs[2], 250, 150, 0, 60);
	iEnemyIDs[3] = createCake(350, -50, 40, "", 0, 0, 870);
	htMoveEnemyTo(iEnemyIDs[3], 350, 200, 0, 60);
	htBindDieFuncToEnemy(iEnemyIDs[3], "item_c");
	iEnemyIDs[4] = createCake(450, -50, 40, "", 0, 0, 870);
	htMoveEnemyTo(iEnemyIDs[4], 450, 200, 0, 60);
	iEnemyIDs[5] = createCake(550, -50, 40, "", 0, 0, 870);
	htMoveEnemyTo(iEnemyIDs[5], 550, 150, 0, 60);
	htBindDieFuncToEnemy(iEnemyIDs[5], "item_h");
	
	iEnemyIDs[6] = createCake(250, -50, 40, "", 0, 0, 1080);
	htMoveEnemyTo(iEnemyIDs[6], 250, 200, 0, 60);
	iEnemyIDs[7] = createCake(350, -50, 40, "", 0, 0, 1080);
	htMoveEnemyTo(iEnemyIDs[7], 350, 150, 0, 60);
	htBindDieFuncToEnemy(iEnemyIDs[7], "item_p");
	iEnemyIDs[8] = createCake(450, -50, 40, "", 0, 0, 1080);
	htMoveEnemyTo(iEnemyIDs[8], 450, 150, 0, 60);
	iEnemyIDs[9] = createCake(550, -50, 40, "", 0, 0, 1080);
	htMoveEnemyTo(iEnemyIDs[9], 550, 200, 0, 60);
	htBindDieFuncToEnemy(iEnemyIDs[9], "item_p");
	
	for(i=0; i<6; ++i) {
		tmpEnemyID = createCake(150, 100, 5, "cakeAttackToPlayerPy", 40, 5, 1440+i*20);
		htSetEnemySpeed(tmpEnemyID, 2, 0);
		htSetEnemyDeflection(tmpEnemyID, -0.05, 0, 200);
		tmpEnemyID = createCake(650, 150, 5, "cakeAttackCircle_24", 40, 5, 1440+i*20);
		htSetEnemySpeed(tmpEnemyID, -2, 0);
		htSetEnemyDeflection(tmpEnemyID, 0.05, 0, 200);
	}
	for(i=0; i<6; ++i) {
		tmpEnemyID = createCake(650, 100, 5, "cakeAttackCircle_24", 40, 5, 1680+i*20);
		htSetEnemySpeed(tmpEnemyID, -2, 0);
		htSetEnemyDeflection(tmpEnemyID, 0.05, 0, 200);
		htBindDieFuncToEnemy(tmpEnemyID, "item_p");
		tmpEnemyID = createCake(150, 150, 5, "cakeAttackToPlayerPy", 40, 5, 1680+i*20);
		htSetEnemySpeed(tmpEnemyID, 2, 0);
		htSetEnemyDeflection(tmpEnemyID, -0.05, 0, 200);
	}
	
	tmpEnemyID = createCake_Fade(400, 220, 40, "cake2000_1", 60, 1, 60, 2000);
	htMoveEnemyTo(tmpEnemyID, 400, -50, 120, 120);
	htBindDieFuncToEnemy(tmpEnemyID, "item_c");
	
	tmpEnemyID = createCake_Fade(300, 150, 40, "cake2000_1", 60, 1, 60, 2300);
	htMoveEnemyTo(tmpEnemyID, 300, -50, 120, 120);
	tmpEnemyID = createCake_Fade(500, 150, 40, "cake2000_1", 60, 1, 60, 2300);
	htMoveEnemyTo(tmpEnemyID, 500, -50, 120, 120);
	
	tmpEnemyID = createCake_Fade(250, 200, 40, "cake2000_1", 60, 1, 60, 2540);
	htMoveEnemyTo(tmpEnemyID, 250, -50, 120, 120);
	htBindDieFuncToEnemy(tmpEnemyID, "item_c");
	tmpEnemyID = createCake_Fade(550, 200, 40, "cake2000_1", 60, 1, 60, 2540);
	htMoveEnemyTo(tmpEnemyID, 550, -50, 120, 120);
	htBindDieFuncToEnemy(tmpEnemyID, "item_h");

	for(i=0; i<6; ++i) {
		tmpEnemyID = createCake(650, 100, 5, "", 0, 0, 2660+i*20);
		htSetEnemySpeed(tmpEnemyID, -2, 0);
		htSetEnemyDeflection(tmpEnemyID, 0.1, 0, 200);
		tmpEnemyID = createCake(150, 150, 5, "", 0, 0, 2660+i*20);
		htSetEnemySpeed(tmpEnemyID, 2, 0);
		htSetEnemyDeflection(tmpEnemyID, -0.1, 0, 200);
	}

	for(i=0; i<6; ++i) {
		tmpEnemyID = createCake(650, 100, 5, "cakeAttackToPlayerWithCircle", 30, 10, 2900+i*20);
		htSetEnemySpeed(tmpEnemyID, -2, 0);
		htSetEnemyDeflection(tmpEnemyID, 0.5, 120, 120);
		tmpEnemyID = createCake(150, 150, 5, "cakeAttackToPlayerPy", 30, 10, 2900+i*20);
		htSetEnemySpeed(tmpEnemyID, 2, 0);
		htSetEnemyDeflection(tmpEnemyID, 0.5, 120, 120);
	}
	
	for(i=0; i<6; ++i) {
		tmpEnemyID = createCake(200, -30, 5, "cakeAttackCircle_12_nr", 40, 5, 3200+i*20);
		htSetEnemySpeed(tmpEnemyID, 0.1, 2);
		tmpEnemyID = createCake(600, -30, 5, "cakeAttackToPlayer", 40, 5, 3200+i*20);
		htSetEnemySpeed(tmpEnemyID, 0.1, 2);
	}
	for(i=0; i<6; ++i) {
		tmpEnemyID = createCake(250, -30, 5, "cakeAttackCircle_12_nr", 40, 5, 3400+i*20);
		htSetEnemySpeed(tmpEnemyID, 0, 2);
		tmpEnemyID = createCake(550, -30, 5, "cakeAttackToPlayer", 40, 5, 3400+i*20);
		htSetEnemySpeed(tmpEnemyID, 0, 2);
	}
	for(i=0; i<6; ++i) {
		tmpEnemyID = createCake(300, -30, 5, "cakeAttackCircle_12_nr", 40, 5, 3600+i*20);
		htSetEnemySpeed(tmpEnemyID, 0, 2);
		tmpEnemyID = createCake(500, -30, 5, "cakeAttackToPlayer", 40, 5, 3600+i*20);
		htSetEnemySpeed(tmpEnemyID, 0, 2);
		htBindDieFuncToEnemy(tmpEnemyID, "item_p");
	}
	
	tmpEnemyID = createCake_Fade(400, 220, 100, "cake2000_1_2", 30, 4, 60, 3900);
	htMoveEnemyTo(tmpEnemyID, 400, -50, 180, 120);
	htBindDieFuncToEnemy(tmpEnemyID, "item_b");
	
	htStopSeaSimulation();
	htSet2DBackground("tydjnh.png", 2, 0);
	htSetBackgroundAlpha(0.7);
	htEnable2DFog1(true, 3);
	
	htSetBackgroundMusic("魔法少女-出击!.mp3");
}

func update() {
	++count;
	
	updateEndState();
	
	if(count == 100)
		htCreatePlayer();
	
	// welcome to the world of magic!
	if(count == 240) {
		htBindScriptFuncToEnemy(iEnemyIDs[0], "cakeWelcome1", 2, 45);
		htBindScriptFuncToEnemy(iEnemyIDs[1], "cakeWelcome1_2", 2, 45);
	}
	if(count == 420) {
		htMoveEnemyTo(iEnemyIDs[0], 300, -50, 0, 120);
		htMoveEnemyTo(iEnemyIDs[1], 500, -50, 0, 120);
	}
	if(count == 450) {
		htSetBackgroundSpeed(3);
		htSetFogSpeed(5);
		htAddFloatingText("Stage 1", "Simhei.ttf", 40, 1.0, 0.6, 0.0, 1.0, 400, 300, 400, 0, 180, 30, 60);
	}
	if(count == 930) {
		htBindScriptFuncToEnemy(iEnemyIDs[2], "cakeWelcome1", 2, 30);
		htBindScriptFuncToEnemy(iEnemyIDs[3], "cakeWelcome2", 3, 45);
		htBindScriptFuncToEnemy(iEnemyIDs[4], "cakeWelcome2_2", 3, 45);
		htBindScriptFuncToEnemy(iEnemyIDs[5], "cakeWelcome1_2", 2, 30);
	}
	if(count == 1020) {
		htMoveEnemyTo(iEnemyIDs[2], 250, -50, 0, 120);
		htMoveEnemyTo(iEnemyIDs[3], 350, -50, 0, 150);
		htMoveEnemyTo(iEnemyIDs[4], 450, -50, 0, 150);
		htMoveEnemyTo(iEnemyIDs[5], 550, -50, 0, 120);
	}
	
	if(count == 1140) {
		htBindScriptFuncToEnemy(iEnemyIDs[6], "cakeWelcome2", 2, 30);
		htBindScriptFuncToEnemy(iEnemyIDs[7], "cakeWelcome1", 3, 45);
		htBindScriptFuncToEnemy(iEnemyIDs[8], "cakeWelcome1_2", 3, 45);
		htBindScriptFuncToEnemy(iEnemyIDs[9], "cakeWelcome2_2", 2, 30);
	}
	if(count == 1230) {
		htMoveEnemyTo(iEnemyIDs[6], 250, -50, 0, 120);
		htMoveEnemyTo(iEnemyIDs[7], 350, -50, 0, 150);
		htMoveEnemyTo(iEnemyIDs[8], 450, -50, 0, 150);
		htMoveEnemyTo(iEnemyIDs[9], 550, -50, 0, 120);
	}
		
		
	if(count == 4140) {
		htDisplayBossTitle("qiyu-title.png", 60);
		htSetBackgroundSpeed(6);
		htSetFogSpeed(7);
	}
	if(count == 4280) {
		createBossQiyu();
	}
	if(count == 4340) {
		htRunAVGScriptInSTG("scripts/stage1/daozhong1.script");
	}
	if(count == 4370) {
		htBossStart();
		htSetBackgroundMusic("天魔降伏.mp3");
		
		bStageAVG = true;
		
		htEnable2DFog1(false, 0);
		htSetSCBackground("qiyu_bg_sc1.png", 450, 600, 1);
		htAddBackgroundCover("qiyu_bg_cover1.png", 0.01, 400, 130, 30, BACKGROUND_TYPE_TURN);
	}
}

func cakeWelcome1(id, x, y, timecount) {
	for(i=0; i<4; ++i) {
		htInsertBulletVec(5, 0, x, y, 4+i*0.5, 180+timecount*8);
	}
}

func cakeWelcome1_2(id, x, y, timecount) {
	for(i=0; i<4; ++i) {
		htInsertBulletVec(5, 0, x, y, 4+i*0.5, -timecount*8);
	}
}

func cakeWelcome2(id, x, y, timecount) {
	for(i=0; i<6; ++i) {
		htInsertBulletVec(5, 1, x, y, 4+i*0.5, 180+timecount*5);
	}
}

func cakeWelcome2_2(id, x, y, timecount) {
	for(i=0; i<6; ++i) {
		htInsertBulletVec(5, 1, x, y, 4+i*0.5, -timecount*5);
	}
}

func cake2000_explode(x, y, dgr) {
	for(i=0; i<=6; ++i) {
		htInsertBulletVec(5, 4, x, y, 4, dgr-90+30*i);
	}
}

func cake2000_explode_2(x, y, dgr) {
	for(i=0; i<=6; ++i) {
		htInsertBulletVec(5, 4, x, y, 4, dgr+90+30*i);
	}
}

func cake2000_1(id, x, y, timeCount) {
	for(i=0; i<18; ++i) {
		htInsertBulletVec(5, 0, x, y, 3, i*20);
		htBindScriptFuncToBullet("cake2000_explode", 20, 1);
	}
}

func cake2000_1_2(id, x, y, timeCount) {
	for(i=0; i<18; ++i) {
		htInsertBulletVec(5, 0, x, y, 3, i*20+timeCount/10);
		htBindScriptFuncToBullet("cake2000_explode_2", 20, 1);
	}
}

func onAVGEnd() {
	if(bStageAVG == true)
		htRunSTGScript("scripts/stage2/stage2_hard.txt");
}

func onScoreBoardEnd() {
	htRunAVGScript("scripts/AVG/part2.script");
}


func cakeAttackCircle_12(id, x, y, timeCount) {
	for(i=0; i<12; ++i) {
		htInsertBulletVec(0, 0, x, y, 4, i*30+randomFloat(0, 60));
	}
}  	

func cakeAttackCircle_12_nr(id, x, y, timeCount) {
	for(i=0; i<24; ++i) {
		htInsertBulletVec(0, 0, x, y, 4, i*15);
	}
}

func cakeAttackCircle_8_r_green(id, x, y, timeCount) {
	for(i=0; i<8; ++i) {
		htInsertBulletVec(0, 2, x, y, 4, i*30+randomFloat(0, 60));
	}
}  	

func cakeAttackCircle_24(id, x, y, timeCount) {
	for(i=0; i<24; ++i) {
		htInsertBulletVec(3, 0, x, y, 4, i*15);
	}
}  

func cakeAttackCircle_24_r(id, x, y, timeCount) {
	for(i=0; i<24; ++i) {
		htInsertBulletVec(3, 0, x, y, 4, i*15+randomFloat(0, 60));
	}
}  

func cakeFanAttack1_3_60_t120(id, x, y, timeCount) {
	if(timeCount >= 60) {
		for(i=0; i<=5; ++i) {
			htInsertBulletVec(2, 3, x, y, 3, randomFloat(30, 90)+i*15);
		}
	}
}

func cakeFanAttack_3_30(id, x, y, timeCount) {
	for(i=1; i<=3; ++i) {
		htInsertBulletVec(2, 4, x, y, 6, 30+i*30);
	}
}

func cakeAttackToPlayer(id, x, y, timeCount) {
	htShootBulletToPlayer(3, 0, x, y, 5);
}

func toPlayerPy(x, y, dgr) {
	htSetBulletLifeTime(1);
	for(i=0; i<4; ++i) {
		htInsertBulletVec(5, 0, x, y, 4, dgr-10+i*5);
	}
}

func cakeAttackToPlayerPy(id, x, y, timeCount) {
	htShootBulletToPlayer(3, 3, x, y, 5);
	htBindScriptFuncToBullet("toPlayerPy", 30, 1);
}

func cakeAttackToPlayer_3red(id, x, y, timeCount) {
	htShootBulletToPlayer(3, 1, x, y, 5);
}

func cakeAttackToPlayer_waterRed(id, x, y, timeCount) {
	htShootBulletToPlayer(0, 0, x, y, 5);
}

func cakeAttackToPlayerWithCircle(id, x, y, timeCount) {
	htShootBulletToPlayer(3, 0, x, y, 5);
	
	if(timeCount % 20 == 0) {
		var px;
		var py;
		px = htGetPlayerX();
		py = htGetPlayerY();
		var dgr;
		dgr = GetDegreeBetweenTwoPoints(x, y, px, py);
		for(i=0; i<16; ++i) {
			htInsertBulletVec(5, 2, x, y, 5, dgr+10+i*21);
		}
	}
}

