Most important rule ever in castling: 1. Your king and rook must be on the first rank. Now let that zip into your head, and you might begin to wonder.. Isn't this an useless role? Since the king and rook cannot have been moved, how is this possible? Well too prevent 0-0-0-0!! Ofc. What is 0-0-0-0? Well it's when you promote a pawn to a ROOK. And your king is still on e1 and that pawn was the e pawn. Now you're ready to castle! King nor rook has been moved (only pawn) and if you're not in check go through check and all that, you can then proudly put your king on e3 and rook on e2! Absolutely amazing.
In the following position, white plays e8=R!! Notice this will mate 1 move faster, than promoting to a queen.
1. e8=R!! Kg2 2. 0-0-0-0!!# (King goes to e3 and rook to e2+)
1. e8=Q Kg2 2. Qe2+ Kh1 3. Kd2#
You cant castle. If you are in check. And you cant be checked into castling.