import "std.zh"

const int CR_SCORE = 7; 			//CR_SCRIPT1
const int CR_TIME = 8;			  //CR_SCRIPT2
const int CR_TIMER = 8;			 //also CR_SCRIPT2
const int CR_LIVES = 9;			 //CR_SCRIPT3
const int CR_LEVEL = 10;			//CR_SCRIPT4
const int CR_CLOCKMUSIC = 11;		//CR_SCRIPT5, but unused idea to play music during invincibility
const int CR_DEBUG = 12;			//CR_SCRIPT6
const int CR_HIGHSCORE = 13;		//CR_SCRIPT7
const int CR_COIN = 14;			//CR_SCRIPT8
const int CR_REUSE = 15;			//CR_SCRIPT9
const int CR_WONGAME = 16;		//CR_SCRIPT10
const int CR_HEROMODE = 17;		//CR_SCRIPT11
const int CR_DEMOPLAYED = 18;		//CR_SCRIPT12
const int CR_COINWAIT = 19;		//CR_SCRIPT13
const int I_SWORDBEAMS = 6;		//I_SWORD2
const int I_ARCADEBOMB = 7;		//I_SWORD3
const int I_BLANKSPACE = 124;
const int I_GANNONSWORD = 36;		//I_SWORD4
const int WS_HEDED = 23;
const int WS_FIFTYP = 88;
const int WS_HUNDREDP = 89;
const int WS_THOUSANDP = 90;
const int NPC_NEWGANNON = 179;
const int SFX_ONEUP = 63;
const int I_CROWN = 143;
const int I_MAXSCORE = 144;
const int SFX_COININSERT = 64;
const int SFX_COINREJECT = 65;
const int SFX_DIPSWITCH = 66;
const int SFX_DEMOMUSIC = 67;
const int SFX_WINDOWSCHIME = 68;
const int SFX_WINDOWSTADA = 69; // Win Tostada
const int I_ZELDAHACK = 145;


ffc script SetTimer{
	void run(int Amount){
	Game->Counter[CR_TIMER] = Amount;
	}
}
ffc script MakeItem{
	void run(int WhatItem){
	item NewItem = CreateItemAt(WhatItem,this->X,this->Y);
	}
}

item script PickupPoints{
	void run (int PlayMusic){
		lweapon PointPopup = CreateLWeaponAt(LW_SPARKLE,Link->X,Link->Y-16);
		PointPopup->UseSprite(WS_FIFTYP);
		Game->DCounter[CR_SCORE] = 5;
		if (PlayMusic != 0)
		{
			int musicB[32] = "FengYinDao.nsf";
			Game->PlayEnhancedMusic(musicB,1);
		}
	}
}

ffc script AutoSave{
	void run(){
		Waitframes(4);
		Game->Save();
		int KillScreen;
		Game->Counter[CR_DEMOPLAYED] = 0;

		if (Game->Counter[CR_LEVEL] >= 1024)
		{
			while (true)
			{
				Waitframe();
				Game->PlayMIDI(0);
				Game->PlaySound(14);
				Screen->ComboD[Rand(176)] = Rand(2567,2655);
				Screen->ComboC[Rand(176)] = Rand(2,9);
				if (KillScreen == 0) KillScreen = 255;
				else KillScreen --;
				if (KillScreen == 1)
				{
					Link->MaxHP = 64;
					Game->Save();
					Game->End();
				}
			}
		}
	}
}

ffc script RecolorBlocks{
	void run(){
		if (Game->Counter[CR_LEVEL] > 63 && Game->Counter[CR_LEVEL] < 128 || Game->Counter[CR_HEROMODE] == 1)
		{
			for (int q = 0; q < 176; ++q)
			{
				if (Screen->ComboC[q] == 8) Screen->ComboC[q] = 7;
			}
		}
		else if (Game->Counter[CR_LEVEL] > 127 || Game->Counter[CR_HEROMODE] == 2)
		{
			for (int q = 0; q < 176; ++q)
			{
				if (Screen->ComboC[q] == 8) Screen->ComboC[q] = 9;
			}
		}
	}
}

ffc script SecretSixtyFour{
	void run (){
		if (Game->Counter[CR_LEVEL] >= 64) Screen->TriggerSecrets();
	}
}

ffc script MakeEnemy{
	void run (int WhatEnemy, int HowOften){
		int SpawnRate = HowOften;
		while (true)
		{
			Waitframe();
			if (Screen->NumNPCs() < 8)
			{
				if (SpawnRate > 0) SpawnRate --;
				else
				{
					if (!LinkCollision(this))
					{
						npc NewEnemy = CreateNPCAt(WhatEnemy,this->X,this->Y);
						SpawnRate = HowOften;
					}
				}
			}
		}
	}
}

ffc script IntroDemo{
	void run (){

		int DemoTimer;
		Game->PlayMIDI(0);
		Game->Counter[CR_LEVEL] = 60;
		//Link->Item[I_ARCADEBOMB] = true; 
		if (Game->Counter[CR_DEMOPLAYED] < 2) Game->Counter[CR_DEMOPLAYED] ++;
		else Game->Counter[CR_DEMOPLAYED] = 0;

		while (true)
		{
			Waitframe();
			if (DemoTimer < 640) DemoTimer++;
			if (Link->PressStart && Game->Counter[CR_COINWAIT] == 0)
			{
				Game->Counter[CR_COINWAIT] = 60;
				if (Game->Counter[CR_COIN] < 4) Game->PlaySound(SFX_COININSERT);
				else Game->PlaySound(SFX_COINREJECT);
			}
			if (DemoTimer >= 640 || Link->HP < 32 || Game->Counter[CR_TIMER] == 1 || Link->PressStart || Link->PressA && Game->Counter[CR_COIN] > 0)
			{
				Link->PressA = false;
				Link->InputA = false;
				Game->Counter[CR_LEVEL] = 1;
				Game->Counter[CR_TIMER] = 0;
				Game->Counter[CR_SCORE] = 0;
				Game->Counter[CR_CLOCKMUSIC] = 0;
				Game->Counter[CR_LIVES] = 0;
				Link->Item[7] = false;
				Link->Item[6] = false;
				Link->HP = Link->MaxHP;
				Link->Warp(1,2);
			}			
			NoAction();
			if (DemoTimer < 128) Link->InputDown = true;
			if (DemoTimer == 132) Link->PressA = true;
			if (DemoTimer == 144) Link->PressRight = true;
			if (DemoTimer == 160) Link->PressA = true;
			if (DemoTimer > 180 && DemoTimer < 224) Link->InputRight = true;;
			if (DemoTimer > 224 && DemoTimer < 320) Link->InputUp = true;
			if (DemoTimer == 360) Link->PressRight = true;
			if (DemoTimer == 380) Link->PressA = true;
			if (DemoTimer > 400 && DemoTimer < 511) Link->InputDown = true;
			if (DemoTimer > 511 && DemoTimer < 590) Link->InputRight = true;
			if (DemoTimer == 600) Link->PressA = true;
			if (DemoTimer == 610) Link->PressUp = true;
			if (DemoTimer == 620) Link->PressA = true;
			if (DemoTimer == 630) Link->PressDown = true;
		}
	}
}

ffc script IntroGuide{
	void run (){

		int str_enemyA[32] = "IT'S DANGEROUS TO GO ALONE";
		int str_enemyB[32] = "   TRAP    KEKE   SKULL   ROPER ";
		int str_enemyC[32] = " GRUMBLE  LANMOLD GHINI  DODONGA";
		int str_enemyD[32] = "   GOMA    PATRA  PABBIT  WIZARD";
		int str_enemyE[32] = "           NAKKU  GANAKKU       ";

		int str_itemA[32] = "TAKE THIS";
		int str_itemB[32] = "  SWORD  BOMB  TIME  GOLD  HEART";
		int SkipGuide = 60;
		int WaitForSecrets = 600;

		Game->PlaySound(SFX_DEMOMUSIC);

		while (true)
		{
			Waitframe();
			if (WaitForSecrets > 1) 
			{
				WaitForSecrets --;
				Screen->DrawString(6,128,0,FONT_Z1,129,0,TF_CENTERED, str_enemyA,128);			
				Screen->DrawString(6,128,32,FONT_Z1,1,0,TF_CENTERED, str_enemyB,128);			
				Screen->DrawString(6,128,72,FONT_Z1,1,0,TF_CENTERED, str_enemyC,128);			
				Screen->DrawString(6,128,112,FONT_Z1,1,0,TF_CENTERED, str_enemyD,128);			
				Screen->DrawString(6,128,152,FONT_Z1,1,0,TF_CENTERED, str_enemyE,128);
			}
			else
			{
				Screen->TriggerSecrets();
				Screen->DrawString(6,128,32,FONT_Z1,129,0,TF_CENTERED, str_itemA,128);				
				Screen->DrawString(6,128,104,FONT_Z1,1,0,TF_CENTERED, str_itemB,128);	
				WaitForSecrets = 0;
			}

			Game->Counter[CR_TIMER] = 60;
			if (SkipGuide > 0) SkipGuide --;
			else if (Link->PressA || Link->PressStart)
			{
				if (WaitForSecrets > 1) WaitForSecrets = 1;
				else Link->Warp(3,0);
			}
			Link->PressA = false;
			Link->InputA = false;
			Link->PressStart = false;
			Link->InputStart = false;
		}
	}
}		

ffc script IntroCodes{
	void run(){
		int str_dipswitch[32] = "Secret dipswitch modes:";
		int str_dipswitchA[32] = "EX1 = Free Play";
		int str_dipswitchB[48] = "EX2 = Apply character hack IPS patch";
		int str_dipswitchC[48] = "EX3 = Start 'Hero Mode' early";
		//int str_dipswitchCa[48] = "      + level skip";
		int str_dipswitchD[48] = "EX4 = Infinite lives + level skip";
		//int str_dipswitchE[32] = "Map = Atari 2600 port";		

		int str_zeroes[48] = "00000000000000000000000000000000";

		int IntroTimer = 728;

		while (true)
		{
			Waitframe();

			Game->Counter[CR_TIMER] = 60;
			if (IntroTimer > 60)
			{
				Screen->DrawString(6,128,8,FONT_S,4,0,TF_CENTERED, str_dipswitch,128);
	
				Screen->DrawString(6,16,32,FONT_S,1,0,TF_NORMAL, str_dipswitchA,128);
				Screen->DrawString(6,16,64,FONT_S,1,0,TF_NORMAL, str_dipswitchB,128);
				Screen->DrawString(6,16,96,FONT_S,1,0,TF_NORMAL, str_dipswitchC,128);
				//Screen->DrawString(6,16,104,FONT_S,1,0,TF_NORMAL, str_dipswitchCa,128);	
				Screen->DrawString(6,16,128,FONT_S,1,0,TF_NORMAL, str_dipswitchD,128);
				//Screen->DrawString(6,16,160,FONT_S,1,0,TF_NORMAL, str_dipswitchE,128);
			}
			else if (IntroTimer < 36 && IntroTimer > 4)
			{
				Screen->DrawString(6,0,0,FONT_Z1,129,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,8,FONT_Z1,1,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,16,FONT_Z1,146,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,24,FONT_Z1,1,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,32,FONT_Z1,1,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,40,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,48,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,56,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,64,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,72,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,80,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,88,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,96,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,104,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,112,FONT_Z1,34,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,120,FONT_Z1,1,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,136,FONT_Z1,1,0,TF_NORMAL, str_zeroes,128);
				Screen->DrawString(6,0,144,FONT_Z1,1,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,152,FONT_Z1,4,0,TF_NORMAL, str_zeroes,128);		
				Screen->DrawString(6,0,160,FONT_Z1,4,0,TF_NORMAL, str_zeroes,128);		
			}
			if (IntroTimer > 0) IntroTimer --;
			else Link->Warp(1,2);
			//if (Link->PressA || Link->PressB || Link->PressStart || Link->PressEx1 || Link->PressEx2 || Link->PressEx3 || Link->PressEx4 || Link->PressMap)
			if (Link->PressA || Link->PressStart)
			{
				Link->InputB = false;
				Link->PressB = false;
				Link->InputA = false;
				Link->PressA = false;
				Link->InputStart = false;
				Link->PressStart = false;
				if (IntroTimer > 60) IntroTimer = 60;
			}
			Link->PressA = false;
			Link->InputA = false;
			Link->PressStart = false;
			Link->InputStart = false;
		}
	}
}

ffc script IntroAnimation{
	void run (){
		int AniSlow;
		int AniFast;
		int HorizontalPos = -112;
		int GannonZeldaAni = 312;
		int LinkAni = 370;
		int str_storyA[32] = "BAD WIZARD ' GANNON ' TOOK";
		int str_storyB[32] = "PRINCESS ' ZELDA ' TO DUNGEON.";
		int str_storyC[32] = "HERO ' SWORDMAN '! SAVE ZELDA!";
		int str_storyAA[32] = "           ' GANNON '     ";
		int str_storyBB[32] = "         ' ZELDA '           ";
		int str_storyCC[32] = "     ' SWORDMAN '            ";
		int str_storyBhack[32] = "PRINCE   ' LINK  ' TO DUNGEON.";
		int str_storyChack[32] = "HERO ' SWORDGIRL'! SAVE LINK !";
		int str_storyBBhack[32] = "         ' LINK  '           ";
		int str_storyCChack[32] = "     ' SWORDGIRL'            ";

		int SkipStory = 48;
		Link->Item[I_MAXSCORE] = false;
		Link->Item[I_WALLETA] = false;

		while (true)
		{
			Waitframe();
			Game->Counter[CR_TIMER] = 60;
			if (SkipStory > 0) SkipStory --;
			else if (Link->PressA || Link->PressStart) HorizontalPos = 428;
			HorizontalPos ++;
			if (HorizontalPos < 380 && HorizontalPos > -80)
			{
				Screen->DrawString(6,24,32,FONT_Z1,1,0,TF_NORMAL, str_storyA,128);			
				Screen->DrawString(6,24,32,FONT_Z1,146,-1,TF_NORMAL, str_storyAA,128);			
				if (!Link->Item[I_ZELDAHACK])
				{
					Screen->DrawString(6,8,40,FONT_Z1,1,0,TF_NORMAL, str_storyB,128);			
					Screen->DrawString(6,8,128,FONT_Z1,1,0,TF_NORMAL, str_storyC,128);
					Screen->DrawString(6,8,40,FONT_Z1,129,-1,TF_NORMAL, str_storyBB,128);			
					Screen->DrawString(6,8,128,FONT_Z1,97,-1,TF_NORMAL, str_storyCC,128);								
				}
				else
				{
					Screen->DrawString(6,8,40,FONT_Z1,1,0,TF_NORMAL, str_storyBhack,128);			
					Screen->DrawString(6,8,128,FONT_Z1,1,0,TF_NORMAL, str_storyChack,128);
					Screen->DrawString(6,8,40,FONT_Z1,129,-1,TF_NORMAL, str_storyBBhack,128);			
					Screen->DrawString(6,8,128,FONT_Z1,97,-1,TF_NORMAL, str_storyCChack,128);								
				}
				Screen->DrawTile(6,HorizontalPos,this->Y,GannonZeldaAni,5,2,6,-1,-1,0,0,0,0,false,128);
				Screen->DrawTile(6,HorizontalPos-56,this->Y+16,LinkAni,1,1,6,-1,-1,0,0,0,0,false,128);
				if (AniFast == 0)
				{
					AniFast = 5;
					if (LinkAni == 370) LinkAni = 371;
					else LinkAni = 370;
				}
				else AniFast --;
				if (AniSlow == 0)
				{
					AniSlow = 24;
					if (GannonZeldaAni == 312) GannonZeldaAni = 352;
					else GannonZeldaAni = 312;
				}
				else AniSlow --;
			}
			Link->PressStart = false;
			Link->InputStart = false;
			Link->PressA = false;
			Link->InputA = false;
			if (HorizontalPos >= 420) Link->Warp(3,118);
		}
	}
}

ffc script Zelda{
	void run(int HoldTime){
		int HoldupTimer;
		int EndTheScene;
		eweapon FirstEWeapon;
		ffc FFCTwo = Screen->LoadFFC(2);
		Waitframes(5);
		while (true)
		{
			Waitframe();

			if (FFCTwo->Data != 2572)
			{

				if (HoldupTimer == 0)
				{
					if (Link->HP > 0)
					{
						if (Screen->NumNPCs() == 0)
						{
							Link->X = this->X;
							Link->Y = this->Y;
						}
						if (LinkCollision(this))
						{
							HoldupTimer = HoldTime;
							Game->PlaySound(34);
							if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
							else Game->Counter[CR_SCORE] += 100;
							Game->PlaySound(SFX_ONEUP);
						}
					}
				}
				else
				{
					FirstEWeapon = Screen->LoadEWeapon(1);
					if (FirstEWeapon->isValid()) Remove(FirstEWeapon);
					Link->DrawYOffset = 256;
					Screen->FastTile(6,this->X,Link->Y,32,6,128);
					Link->X = this->X;
					this->Y = Link->Y-16;
					if (HoldupTimer > 1) HoldupTimer --;
					Link->PressA = false;
					Link->InputA = false;
					Link->PressStart = false;
					Link->InputStart = false;
				}
				if (HoldupTimer == 1)
				{
					if (Game->Counter[CR_TIME] > 0)
					{
						Game->Counter[CR_TIME] --;
						Game->Counter[CR_SCORE] ++;
						Game->PlaySound(SFX_REFILL);
						if (Link->HP < Link->MaxHP) Link->HP ++;
					}
					else if (EndTheScene == 0) EndTheScene = 181;
				}
				if (EndTheScene > 0)
				{
					EndTheScene --;
					Screen->ComboD[Rand(176)] = 2563;
					Screen->ComboD[Rand(176)] = 2563;
					Screen->ComboD[Rand(176)] = 2563;
					Screen->ComboD[Rand(176)] = 2563;
					Screen->ComboD[Rand(176)] = 2563;
					Screen->ComboD[Rand(176)] = 2563;
					if (EndTheScene == 1)
					{
						Game->Counter[CR_LEVEL] ++;
						Link->Warp(3,0);
					}
				}
			}
		}		
	}
}
ffc script Gannon{
	void run(){
		Waitframes(4);
		npc Gannon = Screen->LoadNPC(1);
		Gannon->DrawYOffset = 0;
		Gannon->Extend = 3;
		Gannon->TileWidth = 2;
		Gannon->TileHeight = 2;
		Gannon->HitWidth = 32;
		Gannon->HitHeight = 32;
		Gannon->HitYOffset = -16;
		Gannon->HitXOffset = -8;
		Gannon->DrawXOffset = -8;
		Gannon->DrawYOffset = -16;
		int RememberHP = Gannon->HP;
		int Teleport = 180;
		int TeleportTo;
		int FlashTimer;
		ffc TP1 = this;
		ffc TP2 = Screen->LoadFFC(2);
		ffc TP3 = Screen->LoadFFC(3);
		ffc TP4 = Screen->LoadFFC(4);
		ffc TP5 = Screen->LoadFFC(5);
		ffc TP6 = Screen->LoadFFC(6);

		while (true)
		{
			Waitframe();
			if (Gannon->isValid())
			{
				if (Gannon->Defense[NPCD_BEAM] != NPCDT_BLOCK)
				{
					if (Game->Counter[CR_LEVEL] > 64 || Game->Counter[CR_HEROMODE] > 0) Gannon->Defense[NPCD_BEAM] = NPCDT_BLOCK;
				}
				
				if (TP2-> Data != 2572) // Don't do anything while paused
				{
					if (Game->Counter[CR_LEVEL] > 63 || Game->Counter[CR_HEROMODE] > 0)
					{
						if (NumNPCsOf(NPC_KEESE1) == 0)
						{
							npc GanBat = CreateNPCAt(NPC_KEESE1,Choose(0,240),Choose(0,144));
						}
						if (Game->Counter[CR_HEROMODE] >= 2)
						{
							if (Gannon->Misc[2] == 0)
							{
								Gannon->HP = Gannon->HP * 1.5;
								Gannon->Misc[2] = 1;
							}
						}
					}
					if (FlashTimer > 0)
					{
						FlashTimer --;
						if (Gannon->DrawYOffset == -16) Gannon->DrawYOffset = 256;
						else Gannon->DrawYOffset = -16;
					}
					if (FlashTimer == 1) Gannon->CollDetection = 1;				
					if (Gannon->DrawYOffset == 256)
					{
						if (FlashTimer == 0) Gannon->DrawYOffset = -16;
					}
					if (Teleport > 0)
					{
						if (Gannon->HP < RememberHP && Gannon->HP > 0)
						{
							Teleport = 0;
							RememberHP = Gannon->HP;
							Gannon->DrawYOffset = -16;
							Gannon->CSet = 9;
						}
						if (Teleport == 96)
						{
							Gannon->CSet = 8;
							Game->PlaySound(SFX_ROAR);
						}
						if (Teleport == 32)
						{
							eweapon GannonFire1 = CreateEWeaponAt(Gannon->Weapon,Gannon->X,Gannon->Y);
							GannonFire1->Damage = 4;
							GannonFire1->Dir = DIR_UP;
							GannonFire1->Step = 70;
							eweapon GannonFire2 = CreateEWeaponAt(Gannon->Weapon,Gannon->X,Gannon->Y);
							GannonFire2->Damage = 4;
							GannonFire2->Dir = DIR_DOWN;					
							GannonFire2->Step = 70;
							eweapon GannonFire3 = CreateEWeaponAt(Gannon->Weapon,Gannon->X,Gannon->Y);
							GannonFire3->Damage = 4;
							GannonFire3->Dir = DIR_LEFT;					
							GannonFire3->Step = 70;
							eweapon GannonFire4 = CreateEWeaponAt(Gannon->Weapon,Gannon->X,Gannon->Y);
							GannonFire4->Damage = 4;
							GannonFire4->Dir = DIR_RIGHT;
							GannonFire4->Step = 70;
							eweapon GannonFire5 = CreateEWeaponAt(Gannon->Weapon,Gannon->X,Gannon->Y);
							GannonFire5->Damage = 4;
							GannonFire5->Dir = DIR_LEFTUP;	
							GannonFire5->Step = 50;
							eweapon GannonFire6 = CreateEWeaponAt(Gannon->Weapon,Gannon->X,Gannon->Y);
							GannonFire6->Damage = 4;
							GannonFire6->Dir = DIR_LEFTDOWN;
							GannonFire6->Step = 50;					
							eweapon GannonFire7 = CreateEWeaponAt(Gannon->Weapon,Gannon->X,Gannon->Y);
							GannonFire7->Damage = 4;
							GannonFire7->Dir = DIR_RIGHTUP;	
							GannonFire7->Step = 50;				
							eweapon GannonFire8 = CreateEWeaponAt(Gannon->Weapon,Gannon->X,Gannon->Y);
							GannonFire8->Damage = 4;
							GannonFire8->Dir = DIR_RIGHTDOWN;
							GannonFire8->Step = 50;	
							Gannon->CSet = 9;
							if (Game->Counter[CR_HEROMODE] == 1 || Game->Counter[CR_LEVEL] > 63)
							{
								GannonFire1->Damage = 8;
								GannonFire1->CSet = 7;
								GannonFire1->Step = 90;
								GannonFire2->Damage = 8;
								GannonFire2->CSet = 7;
								GannonFire2->Step = 90;
								GannonFire3->Damage = 8;
								GannonFire3->CSet = 7;
								GannonFire4->Damage = 8;
								GannonFire4->CSet = 7;
								GannonFire5->Damage = 8;
								GannonFire5->CSet = 7;
								GannonFire6->Damage = 8;
								GannonFire6->CSet = 7;
								GannonFire7->Damage = 8;
								GannonFire7->CSet = 7;
								GannonFire8->Damage = 8;
								GannonFire8->CSet = 7;
							}
							if (Game->Counter[CR_HEROMODE] == 2 || Game->Counter[CR_LEVEL] > 255)
							{
								GannonFire1->Damage = 16;
								GannonFire1->CSet = 9;
								GannonFire1->Step = 90;
								GannonFire2->Damage = 16;
								GannonFire2->CSet = 9;
								GannonFire2->Step = 90;
								GannonFire3->Damage = 16;
								GannonFire3->CSet = 9;
								GannonFire3->Step = 90;
								GannonFire4->Damage = 16;
								GannonFire4->CSet = 9;
								GannonFire4->Step = 90;
								GannonFire5->Damage = 16;
								GannonFire5->CSet = 9;
								GannonFire5->Step = 70;
								GannonFire6->Damage = 16;
								GannonFire6->CSet = 9;
								GannonFire6->Step = 70;
								GannonFire7->Damage = 16;
								GannonFire7->CSet = 9;
								GannonFire7->Step = 70;
								GannonFire8->Damage = 16;
								GannonFire8->CSet = 9;
								GannonFire8->Step = 70;
							}
						}
						Teleport--;
					}
					else
					{
						Teleport = Choose (180,224,280);
						FlashTimer = 90;
						Gannon->CollDetection = 0;
						TeleportTo = Rand(1,6);
						if (TeleportTo == 1)
						{
							Gannon->X = TP1->X;
							Gannon->Y = TP1->Y;
						}
						else if (TeleportTo == 2)
						{
							Gannon->X = TP2->X;
							Gannon->Y = TP2->Y;
						}
						else if (TeleportTo == 3)
						{
							Gannon->X = TP3->X;
							Gannon->Y = TP3->Y;
						}
						else if (TeleportTo == 4)
						{
							Gannon->X = TP4->X;
							Gannon->Y = TP4->Y;
						}
						else if (TeleportTo == 5)
						{
							Gannon->X = TP5->X;
							Gannon->Y = TP5->Y;
						}
						else
						{
							Gannon->X = TP6->X;
							Gannon->Y = TP6->Y;
						}
					}
					if (Gannon->HP == 0)
					{
						this->X = Gannon->X;
						this->Y = Gannon->Y;
						this->Data = 2579;
						this->Flags[FFCF_LENSVIS] = false;
					}						
				}
			}
			else
			{
				Game->PlayMIDI(0);
				if (Link->HP > 0) Game->Counter[CR_WONGAME] = 1;
				Quit();
			}
		}
	}	
}

global script ZGlobal{
	void run(){

int StageWait = 0;
int SlowTimer = 32;
bool IsDead;
bool TimeOut;
int DeathAnimation;
int DeathRate = 2;
int DeathTimer = 180;
int WinTimer;
int str_pause[8] = "PAUSE";
int str_dungeon[16] = "DUNGEON START";
int str_gannon[16] = "GANNON ATTACK";
int str_gameover[16] = "GAME OVER";
int str_highscore[16] = "HIGH SCORE";
int str_insertcoin[24] = "I N S E R T   C O I N";
int str_press[24] = "P R E S S   F I R E";
int str_infinitelives[32] = "I N F I N I T E  L I V E S";
int str_heromodeA[32] = "H E R O  M O D E  A";
int str_heromodeB[32] = "H E R O  M O D E  B";
int str_1984[32] = "1984";
int str_Nintendo[32] = "NINTENDO OF AMERICA INC.";
int musicA[32] = "Z1nosquare1.ogg";
int OneUps[255];
int str_maxscore[32] = "THANKS! YOU ARE A SUPER PLAYER";
int str_zeldaedition[32] = "ZELDA EDITION BY 1337H4XX0RZ";
int str_recbin[32] = "Recycle Bin";
int WaitForDemo = 480;
int SNESTool = 0;
int JustPickAScreen = 0;

bool FreePlay;
bool InfiniteLives;
bool IsPaused;
int RandomStages[256];
int PickStage;
int RememberStage;

if (Game->MCounter[CR_SCORE] == 0)
{
	Game->MCounter[CR_SCORE] = 65500;
	Game->MCounter[CR_TIME] = 255;
	Game->MCounter[CR_LIVES] = 9;
	Game->MCounter[CR_LEVEL] = 65535;
	Game->MCounter[CR_HIGHSCORE] = 65500;
	Game->Counter[CR_HIGHSCORE] = 2500;
	Game->MCounter[CR_COIN] = 9;
	Game->MCounter[CR_REUSE] = 65535;
}
Game->Counter[CR_LIVES] = 3;
Game->Counter[CR_LEVEL] = 1;
Game->Counter[CR_REUSE] = 1;
Game->Counter[CR_COIN] = 0;
Game->Counter[CR_COINWAIT] = 0;
Game->Counter[CR_HEROMODE] = 0;
Game->Counter[CR_SCORE] = 0;

//Game->Cheat = 4; //debug feature

while(true)
{
	Waitframe();

// Set the high score
if (Game->Counter[CR_HIGHSCORE] < Game->Counter[CR_SCORE]) Game->Counter[CR_HIGHSCORE] = Game->Counter[CR_SCORE];

if (Link->PressEx4)
{
	if (Game->Cheat != 0 || InfiniteLives)
	{
		if (Game->GetCurScreen() != 117)
		{
			if (Game->GetCurScreen() < 120)
			{
				Link->X = 192;
				Link->Y = 16;
			}
			else
			{
				npc FirstEnemy = Screen->LoadNPC(1);
				FirstEnemy->HP = 0;
			}
			//Game->Counter[CR_DEBUG] = Game->GetCurScreen();
		}
	}
}
if (Link->InputEx3)
{
	if (Game->Cheat != 0 && Game->GetCurScreen() > 2) Game->Counter[CR_SCORE] += 10;
}
if (Link->InputEx2)
{
	if (Game->Cheat != 0 && Game->GetCurScreen() > 2) Game->Counter[CR_LEVEL] ++;
}
if (Link->PressEx1)
{
	if (Game->Cheat != 0 && Game->GetCurScreen() > 2)
	{
		Game->Counter[CR_LIVES] ++;
		Game->PlaySound(SFX_ONEUP);
	}
}

// Bombs and swordbeams change Link's cset
if (!Link->Item[I_RING2])
{
	if (Link->Item[I_ARCADEBOMB]) Link->Item[I_RING2] = true;
}
else
{
	if (!Link->Item[I_ARCADEBOMB]) Link->Item[I_RING2] = false;
}
if (!Link->Item[I_RING1])
{
	if (Link->Item[I_SWORDBEAMS]) Link->Item[I_RING1] = true;
}
else
{
	if (!Link->Item[I_SWORDBEAMS]) Link->Item[I_RING1] = false;
}

// Set dip switches
	if (Game->GetCurScreen() == 116 || Game->GetCurScreen() == 2 || Game->GetCurScreen() == 118)
	{
		if (Link->PressEx4)
		{
			if (!InfiniteLives)
			{
				InfiniteLives = true;
				Link->Item[125] = true;
			}
			else
			{
				Link->Item[125] = false;
				InfiniteLives = false;
			}
			Game->PlaySound (SFX_DIPSWITCH);
			WaitForDemo = WaitForDemo + 48;
		}
		if (Link->PressEx3)
		{
			if (Game->Counter[CR_HEROMODE] < 2) Game->Counter[CR_HEROMODE] ++;
			else Game->Counter[CR_HEROMODE] = 0;
			Game->PlaySound(SFX_DIPSWITCH);
			WaitForDemo = WaitForDemo + 48;
		}
		if (Link->PressEx2)
		{
			if (!Link->Item[I_ZELDAHACK])
			{
				Link->Item[I_ZELDAHACK] = true;
				CopyTileRow(39000,0,380);
				Game->PlayMIDI(0);
				Game->PlaySound(SFX_WINDOWSCHIME);
				SNESTool = 90;
				Game->Counter[CR_COIN] = 0;
			}
			else
			{
				Link->Item[I_ZELDAHACK] = false;
				CopyTileRow(39520,0,380);
				Game->PlayMIDI(0);
				Game->PlaySound(SFX_WINDOWSTADA);
				SNESTool = 90;
				Game->Counter[CR_COIN] = 0;
			}
			WaitForDemo = WaitForDemo + 48;
		}
		if (Link->PressEx1)
		{
			if (!FreePlay) FreePlay = true;
			else FreePlay = false;
			Game->PlaySound(SFX_DIPSWITCH);
			WaitForDemo = WaitForDemo + 48;
		}
		if (SNESTool > 0) 
		{
			if (Link->Item[I_ZELDAHACK]) Screen->DrawTile(6,0,-56,40040,16,16,7,-1,-1,0,0,0,0,false,128);
			else
			{
				Screen->DrawTile(6,0,-56,1280,16,16,11,-1,-1,0,0,0,0,false,128);
				Screen->DrawString(6,28,56,FONT_P,1,-1,TF_NORMAL, str_recbin,128);
			}
			SNESTool --;
			WaitForDemo ++;
		}
	}

// Go to next level:

	if (StageWait == 0)
	{
		if (Game->GetCurScreen() == 0)
		{
			// Kill screen
			if (Game->Counter[CR_LEVEL] >= 1024) Link->Warp(1,1);
			else
			{
				if (Game->Counter[CR_LIVES] == 0) Link->Warp(1,1);
				else
				{
					 Link->Item[I_BLANKSPACE] = false;
					Game->Counter[CR_TIME] = 60;
					StageWait = 181;
					if (Game->Counter[CR_LEVEL] %9) Game->PlaySound(SFX_PICKUP);
					else Game->PlaySound(SFX_GANON);
				}
				if (Link->Item[I_ARCADEBOMB]) Link->Item[I_ARCADEBOMB] = false;
				Link->DrawXOffset = 0;
				Link->DrawYOffset = 0;
				Game->Counter[CR_CLOCKMUSIC] = 0;
				Link->CollDetection = true;
				Link->PressA = false;
				Link->PressStart = false;
				Link->InputA = false;
				Link->InputStart = false;
			}
			if (Game->Counter[CR_LEVEL] > 255 && Game->Counter[CR_LEVEL] < 512)
			{
				if (!Link->Item[I_GANNONSWORD])
				{
					Link->Item[I_GANNONSWORD] = true;
					Link->Item[I_RING3] = true;
				}
			}
			else if (Game->Counter[CR_LEVEL] == 512)
			if (Link->Item[I_GANNONSWORD])
			{
				Link->Item[I_GANNONSWORD] = false;
				Link->Item[I_RING3] = false;
			}
		}
		// Title screen
		else if (Game->GetCurScreen() == 2)
		{
			Game->Counter[CR_LIVES] = 3;
			Game->Counter[CR_LEVEL] = 1;
			Game->Counter[CR_REUSE] = 1;
			Game->Counter[CR_TIMER] = 60;
			Game->Counter [CR_WONGAME] = 1;
			//Screen->DrawString(6,128,0,FONT_Z1,1,0,TF_CENTERED, str_highscore,128);
			//Screen->DrawInteger(6,128,8,FONT_Z1,1,0,0,0,Game->Counter[CR_HIGHSCORE],6,128);
			Screen->DrawString(5,128,144,FONT_Z1,4,0,TF_CENTERED, str_1984,128);
			Screen->DrawString(5,128,152,FONT_Z1,4,0,TF_CENTERED, str_Nintendo,128);
			if (Game->Counter[CR_HEROMODE] == 1) Screen->DrawString(5,128,128,FONT_Z1,4,0,TF_CENTERED, str_heromodeA,128);
			else if (Game->Counter[CR_HEROMODE] == 2) Screen->DrawString(5,128,128,FONT_Z1,146,0,TF_CENTERED, str_heromodeB,128);
			if (InfiniteLives) Screen->DrawString(5,128,136,FONT_Z1,97,0,TF_CENTERED, str_infinitelives,128);
			if (Game->Counter[CR_DEMOPLAYED] > 0) Game->PlayMIDI(0);
			IsDead = false;
			if (Link->Item[I_GANNONSWORD])
			{
				Link->Item[I_GANNONSWORD] = false;
				Link->Item[I_RING3] = false;
			}
			if (Link->PressB) Link->Warp(1,116);

			// Actual title logo
			Screen->DrawTile(4,16,-16,20580,14,8,2,-1,-1,0,0,0,0,false,128);

			if (Link->Item[I_ZELDAHACK]) Screen->DrawString(5,128,88,FONT_Z1,1,0,TF_CENTERED, str_zeldaedition,128);

			if (Game->Counter[CR_COIN] == 0 && !FreePlay) Screen->DrawString(5,128,120,FONT_Z1,1,0,TF_CENTERED, str_insertcoin,128);
			else
			{
				Screen->DrawString(5,128,120,FONT_Z1,1,0,TF_CENTERED, str_press,128);
				if (Link->InputA)
				{
					if (!FreePlay) Game->Counter[CR_COIN] --; 
					WaitForDemo = 640;
					Link->Warp(3,119);
				}
			}
			if (Link->PressStart && Game->Counter[CR_COINWAIT] == 0) Game->Counter[CR_COINWAIT] = 60;
			if (Game->Counter[CR_COINWAIT] > 1)
			{
				Game->Counter[CR_COINWAIT] --;
				if (Game->Counter[CR_COINWAIT] == 59)
				{
					if (Game->Counter[CR_COIN] < 4) Game->PlaySound(SFX_COININSERT);
					else Game->PlaySound(SFX_COINREJECT);
				}
			}
			else if (Game->Counter[CR_COINWAIT] == 1)
			{
				if (Game->Counter[CR_COIN] < 4)
				{
					Game->Counter[CR_COIN] ++;
					Game->PlaySound(SFX_CURSOR);
					Game->PlayMIDI(0);
				}
				Game->Counter[CR_COINWAIT] = 0;
			}
			Link->PressA = false;
			Link->InputA = false;
			Link->PressStart = false;
			Link->InputStart = false;
			Game->Counter[CR_SCORE] = 0;
			Game->Counter[CR_LIVES] = 3;
			Game->Counter[CR_LEVEL] = 1;
			Link->HP = Link->MaxHP;
			if (Link->Item[I_BLANKSPACE]) Link->Item[I_BLANKSPACE] = false;
			if (WaitForDemo > 0)
			{
				WaitForDemo --;
				if (Game->Counter[CR_COINWAIT] > 0 && WaitForDemo < 30) WaitForDemo = 30; 
			}
			else
			{
				WaitForDemo = 480;
				Link->Warp(4,117);
			}			
		}
	}
	else if (Game->GetCurScreen() == 0)
	{
// Random level selection, but non-repeating until 62 stages have passed
		if (Game->Counter[CR_LEVEL] %9) Screen->DrawString(6,128,56,FONT_Z1,1,0,TF_CENTERED, str_dungeon,128);
		else Screen->DrawString(6,128,56,FONT_Z1,1,8,TF_CENTERED, str_gannon,128);
		if (StageWait == 1)
		{
			if (Game->Counter[CR_WONGAME] > 0)
			{
				if (Game->Counter[CR_LEVEL] %62) Game->Counter[CR_REUSE] = Game->Counter[CR_REUSE];
				else Game->Counter[CR_REUSE] = (Game->Counter[CR_LEVEL] / 62) + 1;
				if (Game->Counter[CR_LEVEL] %9)
				{
					PickStage = Rand(3,57);
					if (RandomStages[PickStage] < Game->Counter[CR_REUSE])
					{
						RememberStage = PickStage;
						RandomStages[PickStage]++;
						IsDead = false;
						TimeOut = false;
						StageWait = 0;
						Game->Counter[CR_WONGAME] = 0;
						if (PickStage != 0) JustPickAScreen = 0;
						Link->Warp(0,PickStage);
					}
				}
				else
				{
					PickStage = Rand(120,127);
					IsDead = false;
					TimeOut = false;
					StageWait = 0;
					Game->Counter[CR_WONGAME] = 0;
					if (PickStage != 0) JustPickAScreen = 0;
					Link->Warp(2,PickStage);					
				}
			}
			else
			{
				IsDead = false;
				TimeOut = false;
				StageWait = 0;
				if (Game->Counter[CR_LEVEL] %9) Link->Warp(0,PickStage);
				else
				{
					PickStage = Rand(120,127);
					if (PickStage != 0) JustPickAScreen = 0;
					Link->Warp(2,PickStage);
				}
			}
		}
		else
		{
			if (!IsPaused) StageWait --;
			Game->Counter[CR_TIME] = 60;
		}

		// Ugly failsafe to prevent getting stuck if something breaks
		if (JustPickAScreen > 0) JustPickAScreen --;
		if (JustPickAScreen == 1)
		{
			JustPickAScreen = 0;
			if (Game->GetCurScreen() == 0)
			{
				if (Game->Counter[CR_LEVEL] %9) Link->Warp(0,Rand(3,57));
				else Link->Warp(2,Rand(120,127));
			}
		}
	}

// No Subscreen, custom pause
	if (Link->PressStart && WinTimer == 0 && Game->GetCurScreen() != 117 && Game->GetCurScreen() > 2)
	{
		if (Game->GetCurScreen() > 2)
		{
			if (IsPaused)
			{
				ffc FFCTwo = Screen->LoadFFC(2);
				FFCTwo->Data = 0;
				Game->PlaySound(SFX_CURSOR);
				IsPaused = false;
			}
			else
			{
				ffc FFCTwo = Screen->LoadFFC(2);
				FFCTwo->Data = 2572;
				Game->PlaySound(SFX_CURSOR);
				IsPaused = true;
			}
		}
	}
	Link->PressStart = false;
	Link->InputStart = false;

	if (IsPaused)
	{
		Screen->DrawString(6,128,64,FONT_Z1, 1, 0, TF_CENTERED, str_pause,128);
	}
	

// Gold "Triforce" (Clock) music ends
	if (Game->Counter[CR_CLOCKMUSIC] > 0)
	{
		if (!IsPaused)
		{
			if (Game->Counter[CR_CLOCKMUSIC] == 1) Game->PlayEnhancedMusic(musicA, 1);	
			Game->Counter[CR_CLOCKMUSIC] --;
		}
		// Kill trap combos
		if (Screen->ComboD[ComboAt(Link->X+7,Link->Y+8)] == 2608)
		{
			Screen->ComboD[ComboAt(Link->X+7,Link->Y+8)] = 2561;
			lweapon FakeDeath = CreateLWeaponAt(LW_SPARKLE,ComboX(ComboAt(Link->X+7,Link->Y+8)),ComboY(ComboAt(Link->X+7,Link->Y+8)));
			FakeDeath->UseSprite(WS_HEDED);
			lweapon YayPoints = CreateLWeaponAt(LW_SPARKLE,Link->X,Link->Y-16);
			YayPoints->UseSprite(WS_HUNDREDP);
			Game->DCounter[CR_SCORE] = 10;
			Game->PlaySound(10);
		}
		else if (Screen->ComboD[ComboAt(Link->X+7,Link->Y+8)] == 2610)
		{
			Screen->ComboD[ComboAt(Link->X+7,Link->Y+8)] = 2562;
			lweapon FakeDeath = CreateLWeaponAt(LW_SPARKLE,ComboX(ComboAt(Link->X+7,Link->Y+8)),ComboY(ComboAt(Link->X+7,Link->Y+8)));
			FakeDeath->UseSprite(WS_HEDED);
			lweapon YayPoints = CreateLWeaponAt(LW_SPARKLE,Link->X,Link->Y-16);
			YayPoints->UseSprite(WS_HUNDREDP);
			Game->DCounter[CR_SCORE] = 10;
			Game->PlaySound(10);
		}
	}

// Lose the sword upgrade when hit
	if (Link->Action == LA_GOTHURTLAND)
	{
		if (Link->Item[I_SWORD2]) Link->Item[I_SWORD2] = false;
	}

// Items come back
	if (Screen->State[ST_ITEM])
	{
		if (Game->Counter[CR_SCORE] < 10000 || Game->GetCurScreen() == 14) Screen->State[ST_ITEM] = false;
	}

// Link's bombs

	if (Link->Item[I_ARCADEBOMB])
	{
		if (Link->PressA && WinTimer == 0)
		{
			if (NumLWeaponsOf(LW_BOMB) == 0) lweapon ArcadeBomb = NextToLink(LW_BOMB,0,0);
			ArcadeBomb->Damage = 4;
			Link->PressA = false;	
			Link->InputA = false;
			Link->Action = LA_ATTACKING;
		}
		Link->PressA = false;
		Link->InputA = false;
	}
	if (NumLWeaponsOf(LW_BOMBBLAST) > 0)
	{
		lweapon Kaboom = LoadLWeaponOf(LW_BOMBBLAST);
		if (Kaboom->isValid())
		{
			if (Screen->ComboT[ComboAt(Kaboom->X+7,Kaboom->Y+8)] == CT_SCRIPT2)
			{
				Screen->ComboD[ComboAt(Kaboom->X+7,Kaboom->Y+8)] = Screen->UnderCombo;
				Game->DCounter[CR_SCORE] += 5;
				lweapon BlockPoints = CreateLWeaponAt(LW_SPARKLE,Kaboom->X,Kaboom->Y-16);
				BlockPoints->UseSprite(WS_FIFTYP);
			}
		}
	}

// Timer
	if (Game->Counter[CR_TIME] > 0)
	{
		if (SlowTimer == 0)
		{
			if (!IsDead)
			{
				if (WinTimer == 0 && Game->GetCurScreen() > 2 && !IsPaused && Game->Counter[CR_WONGAME] == 0)
				{
					Game->Counter[CR_TIME] --;
					if (Game->Counter[CR_TIME] < 10) Game->PlaySound(61); // customly set heart beep noise
					SlowTimer = 32;
				}
			}
		}
		else SlowTimer --;
	}
	else if (!TimeOut && WinTimer == 0 && Game->Counter[CR_WONGAME] == 0)
	{
		if (Game->GetCurScreen() > 2)
		{
			Link->HP = 0;
			TimeOut = true;
		}
	}

// Link death
	if (Game->GetCurScreen() != 117)
	{
		if (Link->HP == 0)
		{
			if (!IsDead)
			{
				IsDead = true;
				Link->HP = Link->MaxHP;
				DeathAnimation = DIR_DOWN;
				Game->PlayMIDI(0);
				Game->PlaySound(SFX_SPIRAL);
				DeathTimer = 224;
				//if (Game->GetCurScreen() < 120) item KillAllEnmies = CreateItemAt(I_KILLALL,Link->X,Link->Y);
				Link->Item[I_BLANKSPACE] = true;
				Link->CollDetection = 0;
			}
		}
		if (IsDead && Game->GetCurDMap() != 1)
		{
			if (DeathTimer > 0)
			{
				NoAction();
				if (DeathRate == 0)
				{
					if (DeathAnimation == DIR_UP) DeathAnimation = DIR_RIGHT;
					else if (DeathAnimation == DIR_RIGHT) DeathAnimation = DIR_DOWN;
					else if (DeathAnimation == DIR_DOWN) DeathAnimation = DIR_LEFT;
					else DeathAnimation = DIR_UP;
					DeathRate = 2;
					Link->Dir = DeathAnimation;
				}
				else DeathRate --;
				DeathTimer --;
				if (DeathTimer == 48)
				{
					Link->DrawXOffset = 256;
					lweapon DeathSparkle = CreateLWeaponAt(LW_SPARKLE,Link->X,Link->Y);
					DeathSparkle->UseSprite(WS_HEDED);
					Game->PlaySound(SFX_MSG);
				}
			}
			else
			{
				if (Game->GetCurScreen() > 2)
				{
					if (!InfiniteLives) Game->Counter[CR_LIVES] --;
					JustPickAScreen = 920;
					Link->Warp(3,0);
				}
			}
		}
	}

// Game over screen text
	if (Game->GetCurScreen() == 1)
	{
		if (Game->Counter[CR_LEVEL] < 1024)
		{
			Screen->DrawString(6,128,56,FONT_Z1,1,0,TF_CENTERED, str_gameover,128);
			if (!Link->Item[I_BLANKSPACE]) Link->Item[I_BLANKSPACE] = true;
		}
		StageWait = 0;
		Game->Counter[CR_LIVES] = 0;
		NoAction();
	}

// Enemy effects
	for (int q = 0; q < 16; ++q)
	{
		npc CheckNPC = Screen->LoadNPC(q);
		if (CheckNPC->isValid())
		{
			// Hero Mode A, Hard enemies after level 63
			if (Game->Counter[CR_LEVEL] > 63 && Game->Counter[CR_LEVEL] < 128 || Game->Counter[CR_HEROMODE] == 1)
			{
				if (CheckNPC->Misc[2] == 0)
				{
					if (CheckNPC->ID != NPC_NEWGANNON)
					{
						CheckNPC->HP = CheckNPC->HP *2;
						if (CheckNPC->CSet == 8 || CheckNPC->CSet == 6) CheckNPC->CSet = 7;
						CheckNPC->Misc[2] = 1;
					}
				}
			}
			if (Game->Counter[CR_LEVEL] > 127 || Game->Counter[CR_HEROMODE] == 2)
			{
				if (CheckNPC->Misc[2] == 0)
				{
					if (CheckNPC->ID != NPC_NEWGANNON)
					{
						//CheckNPC->HP = CheckNPC->HP *3; //I liked the idea but with the timer it's a bit too tight, and it can be tedious
						CheckNPC->HP = CheckNPC->HP *2;
						CheckNPC->Damage = CheckNPC->Damage *2;
						if (CheckNPC->CSet == 8 || CheckNPC->CSet == 6) CheckNPC->CSet = 9;
						CheckNPC->Misc[2] = 1;
					}
				}
			}
			// Point popups
			if (CheckNPC->HP <= 0)
			{
				if (CheckNPC->Misc[1] == 0 && IsDead == false)
				{
					CheckNPC->Misc[1] = 1;
					if (CheckNPC->ID == NPC_NEWGANNON)
					{
						Game->DCounter[CR_SCORE] = 100;
						lweapon PointPopup = CreateLWeaponAt(LW_SPARKLE,CheckNPC->X,CheckNPC->Y-16);
						PointPopup->UseSprite(WS_THOUSANDP);
					}
					else
					{
						Game->DCounter[CR_SCORE] = 10;
						lweapon PointPopup = CreateLWeaponAt(LW_SPARKLE,CheckNPC->X,CheckNPC->Y-16);
						PointPopup->UseSprite(WS_HUNDREDP);
					}
				}
			}
			// Don't offset the sprites ZC, it looks stupid
			if (CheckNPC->Extend < 2)
			{
				if (CheckNPC->DrawYOffset != 0) CheckNPC->DrawYOffset = 0;
			}
			// Gold kills enemies
			if (Game->Counter[CR_CLOCKMUSIC] > 0)
			{
				if (CheckNPC->HP > 0)
				{
					if (LinkCollision(CheckNPC)) CheckNPC->HP = 0;
				}
			}
			// Patra
			if (CheckNPC->OriginalTile == 192)
			{
				if (Game->Counter[CR_LEVEL] < 64 && Game->Counter[CR_HEROMODE] == 0)
				{
					if (CheckNPC->HP > 2) CheckNPC->HP = 2;
				}
				else if (Game->Counter[CR_LEVEL] > 63 && Game->Counter[CR_LEVEL] < 128 || Game->Counter[CR_HEROMODE] == 1)
				{
					if (CheckNPC->CSet != 7) CheckNPC->CSet = 7;
					if (CheckNPC->HP > 4) CheckNPC->HP = 4;
				}
				else if (Game->Counter[CR_LEVEL] > 127 || Game->Counter[CR_HEROMODE] == 2)
				{
					if (CheckNPC->CSet != 9) CheckNPC->CSet = 9;
					if (CheckNPC->HP > 6) CheckNPC->HP = 6;
				}
			}
		}
		eweapon CheckEWeapon = Screen->LoadEWeapon(q);
		if (CheckEWeapon->isValid())
		{
			if (Game->GetCurScreen() < 119)
			{
				if (CheckEWeapon->ID == EW_FIRE2)
				{
					if (NumNPCsOf(NPC_WIZZROBE1) > 0)
					{
						if (CheckEWeapon->Step > 60) CheckEWeapon->Step = 60;
					}
				}
			}
		}
	}

// Score management & 1-ups for points
	if (Link->Item[I_CROWN])
	{
		if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Link->Item[I_CROWN] = false;
	}
	else
	{
		if (Game->Counter[CR_LIVES] >= Game->MCounter[CR_LIVES]) Link->Item[I_CROWN] = true;
	}

	if (Game->Counter[CR_SCORE] >= 65000)
	{
		Game->Counter[CR_SCORE] = 65000;
		Game->Counter[CR_HIGHSCORE] = 65000;
		Screen->DrawString(6,128,-8,FONT_Z1,129,0,TF_CENTERED, str_maxscore,128);
		if (Link->Item[I_MAXSCORE] == 0)
		{
			Link->Item[I_MAXSCORE] = 1;
			Link->Item[I_WALLETA] = 1;
		}
	}
	if (Game->Counter[CR_SCORE] >= 60000)
	{
			if (OneUps[60] == 0)
			{
				OneUps[60] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] = Game->MCounter[CR_LIVES];
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 50000)
	{
			if (OneUps[50] == 0)
			{
				OneUps[50] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] = Game->MCounter[CR_LIVES];
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 40000)
	{
			if (OneUps[40] == 0)
			{
				OneUps[40] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] = Game->MCounter[CR_LIVES];
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 30000)
	{
			if (OneUps[30] == 0)
			{
				OneUps[30] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] = Game->MCounter[CR_LIVES];
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 20000)
	{
			if (OneUps[20] == 0)
			{
				OneUps[20] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] = Game->MCounter[CR_LIVES];
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 10000)
	{
			if (OneUps[10] == 0)
			{
				OneUps[10] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] = Game->MCounter[CR_LIVES];
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 9000)
	{
			if (OneUps[9] == 0)
			{
				OneUps[9] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 8000)
	{
			if (OneUps[8] == 0)
			{
				OneUps[8] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 7000)
	{
			if (OneUps[7] == 0)
			{
				OneUps[7] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 6000)
	{
			if (OneUps[6] == 0)
			{
				OneUps[6] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 5000)
	{
			if (OneUps[5] == 0)
			{
				OneUps[5] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 4000)
	{
			if (OneUps[4] == 0)
			{
				OneUps[4] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 3000)
	{
			if (OneUps[3] == 0)
			{
				OneUps[3] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 2000)
	{
			if (OneUps[2] == 0)
			{
				OneUps[2] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
				Game->PlaySound(SFX_ONEUP);
			}
	}
	else if (Game->Counter[CR_SCORE] >= 1000)
	{
			if (OneUps[1] == 0)
			{
				OneUps[1] = 1;
				if (Game->Counter[CR_LIVES] < Game->MCounter[CR_LIVES]) Game->Counter[CR_LIVES] ++;
				Game->PlaySound(SFX_ONEUP);
			}
	}

// Win a level
	if (Screen->ComboT[ComboAt(Link->X+7,Link->Y+15)] == CT_SCRIPT1)
	{
		NoAction();
		//Link->Action = LA_NONE;
		//Link->Action = LA_ATTACKING;
		if (WinTimer == 0)
		{
			Link->Dir = DIR_RIGHT;
			WinTimer = 400;
			item Kildall = CreateItemAt (I_KILLALL, Link->X,Link->Y);
			Game->Counter[CR_CLOCKMUSIC] = 0;
			Game->PlayMIDI(0);
			Game->PlaySound(SFX_SECRET);
			Screen->ComboD[13] = 0;
			Screen->ComboD[14] = 0;
			Screen->ComboD[29] = 0;
			Screen->ComboD[30] = 0;
			Screen->ComboD[31] = 0;
			ffc FirstFFC = Screen->LoadFFC(1);
			FirstFFC->Script = 0;
			FirstFFC->X = 208;
			FirstFFC->Y = 0;
			FirstFFC->Data = 2571;
			FirstFFC->CSet = 2;
			FirstFFC->TileWidth = 3;
			FirstFFC->TileHeight = 2;
		}
		else
		{
			if (WinTimer == 320) Game->PlaySound(SFX_ROAR);
			if (WinTimer == 260)
			{
				ffc FirstFFC = Screen->LoadFFC(1);
				FirstFFC->Vx = 0.6;
			}
			if (WinTimer > 181) WinTimer --;
			else
			{
				if (Game->Counter[CR_TIME] > 0)
				{
					Game->Counter[CR_TIME] --;
					Game->Counter[CR_SCORE] ++;
					Game->PlaySound(SFX_REFILL);
				}
				else
				{
					if (WinTimer > 1) WinTimer --;
					else
					{
						WinTimer = 0;
						Game->Counter[CR_WONGAME] = 1;
						Game->Counter[CR_LEVEL] ++;
						JustPickAScreen = 920;
						Link->Warp(3,0);
					}
				}
			}
		}
	}

// end of script
}

}
}
