I replaced your formula.

Sorry. The old one had bugs like troops going off into nowhere. No, I
did not get this from the web. My dad helped me. I'm not very good at
trig.