bool isValid;
const int TIME_PER_MP = 15; //How many frames 1 unit of MP lasts items.
bool gogglesOn;
int gogglesTimer;

global script active{
    void run(){
    int whipCooldownTimer = 0;
	LinkX = Link->X;
    LinkY = Link->Y;
	int fastWalkTimer;
    int lastScreen = Game->GetCurDMapScreen();
    int lastDMap = Game->GetCurDMap();
	Game->MCounter[CR_LIVES] = 99;
	Game->MCounter[CR_POINTS] = 999999;
	
        while(true){
		
		
		if ( LinkSeen ){
			Link->Invisible = false;
			}
		else if ( !LinkSeen ){
			Link->Invisible = true;
			}
		
		//Whip Cooldown Timer
            if ( whipCooldownTimer > 0 )
                whipCooldownTimer--;
			
            if ( gogglesOn && Link->MP ){                
                //Decrement magic
                gogglesTimer = (gogglesTimer+1) % TIME_PER_MP;
                if ( !gogglesTimer ) Link->MP--;
				Screen->Rectangle(6, 0, 0, 256, 172, 4, 1, 0, 0, 0, true, 64);
                
                if ( Link->MP <= 0 //If magic ran out
               
                ){ //Take off goggles.
				
                    gogglesOn = false;
					if(Link->Item[I_AMULET1] != IsValid){
					Link->Item[I_AMULET1] = IsValid;
					}
                    Link->Item[I_AMULET1] = false;
                }

            }
            else if ( !gogglesOn ){ //Disable Goggles
                Link->Item[I_AMULET1] = false;
				Waitframes(1);
            }
			
			
	
        
            //Exp gain and level up
            if ( Game->Counter[CR_LEVEL] < EXP_MAX_LEVEL ){ //Disable if level is maxed
                setMaxHP();
                checkEnemiesKilled();
            }
		lastScreen = Game->GetCurDMapScreen(); //Update last screen/DMap
		lastDMap = Game->GetCurDMap();
            
            //Fast walk
            if ( Link->Item[I_FASTWALK] ){
                fastWalkTimer++;
                fastWalkTimer %= EXP_FASTWALKRATE;
                if ( fastWalkTimer == 0 )
                    fastWalk(1);
            }
            
            //Treasure Hunter
            if ( Link->Item[I_TREASUREHUNTER] //If skill learned
                && (lastScreen != Game->GetCurDMapScreen() || lastDMap != Game->GetCurDMap()) //If new screen
                && checkScreenFlag(SF_TREASURE) //And flag active
                && !Screen->State[ST_ITEM] //And item not taken
                && !Screen->State[ST_CHEST]
                && !Screen->State[ST_LOCKEDCHEST]
                && !Screen->State[ST_BOSSCHEST]
            ){
                Game->PlaySound(SFX_TREASURESCREEN); //Play SFX
            }
            lastScreen = Game->GetCurDMapScreen(); //Update last screen/DMap
            lastDMap = Game->GetCurDMap();
			
			UpdateEWeapons();
			CleanUpGhostFFCs(); // Only needed if __GH_USE_DRAWCOMBO is 0
			UpdateGhostZHData();
			Cooldown_Update();
			
	  
			Game->Counter[CR_SCRIPT2] = Link->HP;
			Game->Counter[CR_SCRIPT3] = Link->MP;
			Game->Counter[CR_SCRIPT4] = Link->MaxHP;
			Game->Counter[CR_SCRIPT5] = Link->MaxMP;
			Game->Counter[CR_LIVES] = Game->Counter[16];
			Game->Counter[CR_POINTS] = Game->Counter[15];
			LivesUpdate();
			Waitdraw();
            
			AutoGhost();
			DrawGhostFFCs();
			Waitframe();
        }
    }
}