//1. Find sound effects for the sentry detecting and shooting Link, and set the SFX_ constants at the top to their IDs
//2. Compile the script and remember the FFC script slot you put it in
//3. (Optional) Make a combo for the sentry's graphics. It can be an animated, spinning head, an eyeball, or whatever you want
//4. Make a new enemy, and set the following data:
//  Type: Other
//  Sprite: Even if using the combo for its graphics, choose the right CSet here
//  HP and Weapon damage: Whatever you please
//  Misc Attribute 1: Rotation speed in degrees per frame (Recommended 3)
//  Misc Attribute 2: Sight radius in pixels (Recommend 70)
//  Misc Attribute 3: Delay between sighting and shooting (0 for instant shot)
//  Misc Attribute 4: Cooldown between shooting and looking again (Highly recommend more than 0, like 120)
//  Misc Attribute 5: Laser color (CSet# * 16 + color number in CSet)
//  Misc Attribute 11: Either the combo from step 3, or -1 to use the enemy's graphics
// NOTE: If your version of ghost.zh doesn't have __GH_INVISIBLE_ALT, set this to the value of GH_INVISIBLE_COMBO to use the enemy graphics
//  Misc Attribute 12: The FFC script slot number from step 2

bool LinkInAngleOfVision2 ( npc ghost, int angle ){
    //Check angle to Link against angle of vision
    int angleToLink = Angle(CenterX(ghost), CenterY(ghost), CenterLinkX(), CenterLinkY());
    int facingAngle = Dir4ToDeg(Ghost_Dir);
    int difference = Abs(angleToLink - facingAngle);
    
    return (difference <= angle/2);
}

ffc script laserSentry{
        void run ( int ID ){
                npc ghost = Ghost_InitAutoGhost(this, ID, GHF_SET_DIRECTION | GHF_4WAY); //Initialize ghost NPC
                int rotSpeed = ghost->Attributes[LS_ATTRIB_ROTSPEED]; //Load its attributes
                int radius = ghost->Attributes[LS_ATTRIB_RADIUS];
                int delay = ghost->Attributes[LS_ATTRIB_DELAY];
                int cooldown = ghost->Attributes[LS_ATTRIB_COOLDOWN];
                int color = ghost->Attributes[LS_ATTRIB_LASERCOLOR];
                int angle = ghost->Attributes[8];
                while(ghost->isValid()){
                        Screen->Arc(3, ghost->X+8, ghost->Y+8, radius , -1*angle , -1*angle+1, color, 1, 0, 0, 0, false,false, 128); //Draw laser pointer
                        if ( DistanceLink(ghost->X+8, ghost->Y+ 8) <= radius //If Link is within its radius
                        && AnglePos(ghost->X, ghost->Y, Link->X, Link->Y) >= angle //And between the two angles (new and old)
                        && AnglePos(ghost->X, ghost->Y, Link->X, Link->Y) <= angle+rotSpeed
                        ){
                                Game->PlaySound(SFX_ALARM);
                                int x = Link->X+8; //Save Link's position
                                int y = Link->Y+8;
                                Ghost_Waitframes(this, ghost, true, true, delay); //Wait before firing
                                fireLaserLine( ghost->X+8, ghost->Y+8, 600, ghost->WeaponDamage, x, y, color, this, ghost ); //Shoot where Link was
                                Ghost_Waitframes(this, ghost, true, true, cooldown); //Wait before resumed operation
                        }
                        angle = (angle+rotSpeed) % 360;
                        Ghost_Waitframe(this, ghost, true, true);
                }
        }
}

void fireLaserLine( int origX, int origY, int speed, int damage, int targX, int targY, int color, ffc this, npc ghost ){
                int lastX;
                int lastY;
				int angle = ghost->Attributes[8];
                if ( targX < 0 ) targX = Link->X;
                if ( targY < 0 ) targX = Link->Y;
                Game->PlaySound(SFX_LASER); //Play firing sound
                eweapon laser = CreateEWeaponAt(EW_ARROW, origX, origY); //Create the laser eweapon (invisible)
                laser->DrawXOffset = 999;
                laser->Step = speed;
                laser->Damage = damage;
                laser->Angular = true;
                laser->Angle = RadianAngle(origX, origY, targX, targY);
                while ( laser->isValid() ){
                                Screen->Line(3, origX, origY, laser->X, laser->Y, color, 1, 0, 0, 0, 128);
                                lastX = laser->X;
                                lastY = laser->Y;
                                Ghost_Waitframe(this, ghost, true, true);
                }
                for ( int f = 20; f > 0; f-- ){ //Let laser fade out after hit
                                Screen->Line(3, origX, origY, lastX, lastY, color, 1, 0, 0, 0, 128);
                                Ghost_Waitframe(this, ghost, true, true);
                }
				
				 //Beam side damage
                if ( WLS_LASERSIDEDAMAGE > 0 //If side deals damage
                 && LinkInAngleOfVision2 ( ghost, RadianAngle ) //And Link is still in angle of vision
                 && DistanceLink(ghost) <= Distance(laser->X, laser->Y, Ghost_X, Ghost_Y) //And is within beam length
                ){
                    //Fire invisible, unblockable, undetectable weapon at Link
                    eweapon beamExtra = FireAimedEWeapon(EW_SCRIPT1, origX, origY, 0, 9999, ghost->WeaponDamage * WLS_LASERSIDEDAMAGE, 0, 0, EWF_UNBLOCKABLE);
                    //It will leave the screen almost instantly
					beamExtra->DrawXOffset = 999; //Draw off-screen
                }
            
				
				
}

//Returns value from 0 to 360 rather than -180 to 180
//float AnglePos(int x1, int y1, int x2, int y2) {
//        float angle = ArcTan(x2-x1, y2-y1)*57.2958;
//        if ( angle < 0 )
//                angle += 360;
//        return angle;
//}

//int DistanceLink ( int x, int y ){
//        return Distance ( Link->X+8, Link->Y+8, x, y );
//}
