IIRC Polymorphing into a bad creature is intentional. It's something like a 75% chance to turn the caster into something better, and 25% to something terrible (e.g. large spider, puppy, carnivorous plant), and the inverse for hostiles.
If you're interested in learning to do more with the script files, check out the thread I made here alongside this guide which is also linked in that topic.