So, as promised, this is a defensive counterpart to my post about the issue of magic and sieges.
CountermeasuresSo what I'm suggesting is that fortifications would adopt a range of measures against the threat of magic (and some associated threats), including physical changes, magical countermeasures, and practices designed to limit the danger posed by wizards. This would be a natural development, in the same way that fortifications adapted in the face of gunpowder.
I'm not suggesting that castles should be entirely immune to magical attack, any more than they are to mundane attack. Everything is in balance, and gold weighs a lot. However, I think in general a fortress should have similar resistance to magic as it does to force. It doesn't especially matter if a high-level wizard can breach the castle fairly readily, because a high-level wizard is probably equivalent to a bevy of siege engines. Nor do I think each castle should be resistant across the board; as with other features, there should be strengths and weaknesses, and a canny wizard will test, discern and exploit those weaknesses. That being said, I think it's also reasonable to say that getting through those defences ought to take time, attract attention and require some genuine effort. If you think about it, if a decent wizard can breach a castle's defences and leave it vulnerable in the space of a few minutes, they render castles and siege engines entirely redundant.
Changes in approach
Some existing features of castles would be much less useful in a magical setting. Traditionally keeps would have a door only on the first floor, with wooden staircases leading up that can be demolished to keep attackers out: this would be much less useful in a world where wizards can conjure up magical stairs, create ramps from earth or repair the shattered stairs with a word and a gesture.
On a bigger scale, people tend to think in terms of bottlenecks, relying on really basic things like the difference between walls and doors. But doors become massively less crucial as strategic points if people have the ability to pass easily through walls; they can avoid bottlenecks and kill-zones, and it's far more difficult for small numbers of troops to hold off larger numbers by controlling the terrain. While it's always possible to knock holes in walls, this tends to be pretty obvious, and also carries a risk of collapsing the wall entirely, which might kill you or leave your routes even more blocked than they were before. In a magical world, people would need to think of new doctrines that didn't focus so strongly on control of routes.
Disguises, shapechangers, and above all invisible creatures are major problems for a fantasy castle.
A very straightforward countermeasure is to have a significant number of guard dogs, or equivalent creatures, relying on hearing or scent to detect and locate unwanted visitors. I can also envisage buckets of flour or paint kept at regular intervals around the site, or perhaps even remove invisibility or glitterdust items.
A wealthy castle might have permanent true seeing items, such as scopes or helms. They might even be built into a viewport in the main games. There might be a number of them, perhaps one at each of the watchtowers, or they might be portable items passed from watch to watch.
A magically-sophisticated civilisation might have more complex effect; for example, a kind of security gate might detect invisible or disguised creatures and cast counterspells on them, or simply alert nearby guards.
Speaking of which, I think it's likely that any castle worth its salt would use the cheap and cheerful alarm spell, and even mage's faithful hound. Wards would likely be placed around the perimeter, as well as in restricted areas, just as they are nowadays. Magical wards such as glyphs of warding offer the chance to identify individuals, demand passwords and so on. Wards might be purely for detection, but in the harsher sorts of societies (and considering these are military installations) they're quite likely to imprison, hurt or even kill trespassers. For some more thoughts on lethality of traps, you might want to consider this post.
Having guards taken out quietly by a long-range sleep spell is a bit of an issue - at least they might scream if an arrow gets them - and countermeasures would be sensible. One that occurred to me was actually to instigate a dead man's handle on guard posts. If the person on shift lets go of the handle, or (in another design) steps off their platform, it will immediately sound an alarm. Given magic, you could have a magical 'handle', or simply a more subtle kind of alarm. It would be entirely possible to have other guards insta-summoned to the spot.
Another option is simply to ward guards against things like sleep, perhaps with a side-effect of shielding them from physical tiredness. Wealthy rulers might provide helms, amulets, rings or simply potions that will keep their guards alert throughout the night. Some will be in a position to provide at least some sleepless guards, be they fey, undead, golems or elemental watchers - many of whom are also immune to the other cheap and cheerful enchantments that pose such a threat.
There are several ways to magically spy on a castle, but magic also offers some options to foil each one.
Much as fortresses may use camouflage paint and thermal paint to protect against aerial photography, they can invoke techniques against flying spies. One fairly simple one would be a roof. In a world with easily-available magical light, you don't especially need to have natural light. It'll be harder to use the inner castle to raise animals or crops, but perhaps you're willing to accept that (or call on further magical resources to compensate). For very swish places, you might have a roof imbued with magic, so that it's transparent from below but opaque from above. Another option is a big old illusion that blocks the castle from above - you could adapt hallucinatory terrain or just devise your own spell.
Thinking about it, a really huge sheet of gauze would actually tend to work. The light from above will come through the fabric, but (as with net curtains) you won't be able to see through to the inside unless it's dark and there's loads of lights on. Magically-reinforced gauze, anyone? On the downside, it would cut down on the light getting through.
For scrying, countermagic is needed. Any ruler worth their salt is likely to have invested in developing versions detect scrying that will protect a whole area, as well as permanent versions of screen. A cheaper (lower-level) variant might dispense with deception and simply blur an area or cover it with obvious illusory patterns. A nastier version might involve a full-blown counterattack: a counterscry on the scrier, a psychic backlash, domination tracing back the magical thread to its source, or summoning a couple of fire elementals on top of the wizard in question.
Even detection is a good start - this would warn defenders that something was up, and give them a chance to prepare for attack and start trying to track down the wizard.
Worried about charms and other enchantments? Key personnel could be equipped with anti-enchantment gear, so that even when wandering in town at night there's no chance of them being compromised. For a subtle version, don't block the enchantment, but rather have the ward immediately alert castle personnel so they can prepare suitable countermeasures while leaving the attackers thinking they've succeeded. For a surprise version, have the amulets reflect enchantments back onto their casters!
One very obvious area for magical intervention is in making the castle plain old tougher. I've discussed a number of ways that magic allows easy damage or destruction to castles, but by the same token magic can prevent that sort of thing.
For starters, it's very likely that any magic-heavy universe would be using magic for construction. This would allow for structures well beyond the capabilities of the mediaeval period: bigger, taller, more complicated. If you can teleport stone, transform the local geography or levitate segments into place, you can do a lot more, very likely in a shorter time and on a smaller budget. Magic might allow the use of something like reinforced steel frames without centuries of research into metallurgy, or develop concrete earlier. Vast moats can be excavated, and filled with water, spikes shaped from the living rock, horrific poisonous plants or fire direct from the Elemental Plane. Almost any castle can be placed on a 100m-high plateau, even in the middle of your Netherlands-equivalent. They can be surrounded by marshes, and reachable only by bridges that magically detect your religious affiliation or ancestry.
Next, you can soup up the castle in various ways. Spells that grant resistance to attacks are widespread, and would certainly be adapted for use against siege engines. Imagine a castle wall that reflects boulders and cannonballs like rubber, hurling them back on attackers. Wooden palisades could easily be rendered fireproof, or imbued with druidic magic that causes them to sprout vicious thorns when someone attempts to climb them - perhaps they regrow when damaged, too.
Attacks coming from above are a problem for any castle, let alone one faced with flying enemies. As a castle-builder, I'd immediately have my wizards develop spells that produce some kind of force bubble, somewhere between a tiny hut and a resilient sphere. A permanent force wall would do the trick, too.
Similarly, mining is a traditional anti-castle tactic that magic can deal with far better than force. Have your transmuters turn all the ground beneath your castle into granite, so mining will take several decades.
Plus, poisoning and infecting castles is one of the favoured ways to destroy a garrison. But there's no need to worry about poisoned water if you have portals to the elemental planes, or magical water. Similarly, a few suitable artefacts or clerics can render ordinary disease a non-issue.
At some point, you need to get into actually defending the castle.
For starters, you want to ward the castle against the most obvious attack spells. These will by no means provide absolute protection, any more than a wall is absolute protection against physical attack, but the wards will make it much slower and tougher to demolish your castle. The wards will be long-lasting in most cases, and wizards aligned with the castle-owner will come to do regular maintenance, just as architects do. Quite likely, several powerful wizards will create the initial ward, but less skilled ones can maintain it by following the plans. Perhaps have the walls reflect spells if possible, so that fireballs and so on will rebound to fry the wizards in question. If you're very good, perhaps you can actually absorb and use the magic for your own purposes.
Conjure up earth elementals and similar beasts, and bargain or force them into patrolling your territory and devouring any would-be miners. Bury objects with protective glyphs all around the castle's outer walls, enchanted to activate when miners approach and cast the nastiest spells against them - although subtler ones, like messing with their sense of direction, could be handy too.
Next, consider ordinary attackers. Some dock-off enchantments around the walls could be more effective than any physical defences, disheartening or bewildering soldiers that come nearby. I'm picturing this as just massive glyphs carved into the walls - your symbols of fear or whatever, but in a siege-specific form that can be activated by the defenders.
Then, too, your attackers aren't the only ones that can use summoning spells. A thinly-garrisoned fortress could receive reinforcements immediately with a few well-placed triggerable summoning spells, ranging from troops stationed elsewhere, to simple swarms of rats, all the way up to major demons. Similarly, a teleportation gate could allow you to travel rapidly between a network of fortresses. This could be a weakness, of course, and would need careful handling. I'd suggest having it in a location that can itself be locked down, where allied troops must be allowed out, and enemy intruders can be penned in. Of course, something like an alignment lock on the gate would potentially do. Or have a password that controls whether you end up in a deep pit covered by a grating, or in the main courtyard.
Flight has been a major concern, and so you may want to take special action against flying enemies. A nice idea you could borrow is the doubling crows from Jack of Fables (the only volume I read in that series), which are basically crows that split in two repeatedly and devour anything they see in the sky when unleashed. Alternatively, just have standard nasties like air elementals, giant eagles, small dragons, griffons and so on patrolling. Air elementals are a good option simply because they're tireless, largely invisible and don't require food. A flying golem could also be good.
Worried about teleportation? Add in some spells to mess with that. Perhaps any teleportation spell that doesn't incorporate a special control word with drop people into a pocket dimension. While "inside a volcano" is tempting, remember that your own wizards will have to bespell this stuff, and will also be the ones most likely to cast teleportation spells around the place - they aren't likely to like a setup where a slip on their part will be fatal.
In a setting like this, you'd be likely to have magic items crafted specifically for use in sieges, as well as siege-specific spells. As well as their tactical value, important members of the garrison might carry items that let them override certain spells, pass guards and so on, which provides a new point of vulnerability.
It also occurs to me that many magical creatures might act like Swiss mercenaries in the old wars, taking on contracts to defend or attack castles for a fee. The ability to hire enough earth elementals, or even ghosts, might be a crucial factor in deciding wars. Clerics and wizards of certain spheres might offer a vast advantage in particular tactics: dwarves' tendency towards Earth magic might well give them impregnable fortresses and make them terrifying besiegers.
If you're planning to introduce these kinds of setting elements, you'll eventually want some way to handle the mechanics. I'm inclined to say you should add more stages to things like, as in our first example, using transmute rock to mud to demolish walls. Perhaps there is a top layer of general protective spells, which must first be dispelled, and then a specific anti-spell to overcome. Each stage in the process might call for an extended magical "duel", with the caster matching their strength against the original warder to try and break down the protective spells. Alarms might be built into the spells, and so care is needed (perhaps an Intelligence, Spellcraft or even Stealth check) to avoid and defuse them - you might want to allow some means to shut down or intercept these spells if the check is failed, just to avoid one-roll failures. Basically, make it a more complex process that calls for more interesting decisions and - crucially - takes some time. It is, in many ways, the speed of spellcasting that made it an issue in the first place.
There's probably a lot more you could say here, but I'm tired now and this seems long enough. Hope someone enjoys it.