So we all hate having lightning blow up our stuff, right? Pertam electrical storms driving you nuts? Me too. I decided to do some research into the actual mechanics of lighting.
Here's what I've got. All data is taken directly from game files and code, so this may contradict what you think should happen.
This guide is about working with the game rules for lightning. If you don't want lightning to strike or do damage, there are mods for that!
When does lightning strike?
Let's start from when the storm blows in. First, it chooses where to strike. It generates 3 random numbers, for "next lighting", "next lightning character" and "next lightning grid" based on the max and min intervals as defined in the weather configuration in Content\Data\WeatherEffects.sbc. Those numbers are stored as seconds in the data file and are turned into frame counts in the code, and every frame it decrements the number by 1. When it hits zero, lightning strikes.
Rule 1: The timers for the ground, characters, and grids are separate, but there is always going to be a minimum and maximum time between strikes of all 3.
A single lightning strike always has a pair of true/false values defined: hit grids and hit characters. Currently, it appears that these are never both true, but they could be. Even if both were true, it will always choose a grid first if one is available.
First, the game figures out the bounding sphere of the weather - that is, an orb based on the position of the weather effect in the world with a radius of the size of the weather effect. It finds all grids that are contained within that orb, and chooses based on a few rules. The rules are: Is not immune (due to being an invulnerable admin), *is not a respawn ship*, is not a projection, and a couple other things related to grids in the process of being destroyed.
Where does lightning strike?
Rule 2: Your respawn ship (survival pod) will never be struck by lightning if it's still considered a Respawn Ship.
You can tell if your pod is still a respawn ship by the red text saying it will be removed if you spawn a new one when you try to build on it, and it has a GPS marker that follows it around.
For characters, it simply checks to see if the user is an untargetable admin or not and if so doesn't hit them. Only "online" characters are considered, so you can never die to lightning if you're disconnected.
Rule 3: Offline characters are never struck by lightning.
Both grids and characters are selected via random chance, all grids and characters within the storm's sphere are considered equally.
Game rule: This is not specific to weather, but characters inside a grid (for example, in a cockpit or cryo tube) are not considered to be "characters", they're just part of the grid. You can see this if you set a sensor to detect characters but not grids and fly a ship up to it with your cockpit in the sensor range. The sensor only sees your character once you get out of the cockpit. This can be used to further escape storms though - take a seat in a passenger seat or cockpit til it blows over!
If there are no grids or characters within the storm, it still causes lightning, but it will never do damage and is just randomly placed within the storm.
How can strikes be mitigated?
What about decoys? That's the next part! The storm found your delightfully conductive grid and wants to zap the hell out of it and destroy your hard work, but first it checks to see if there's anything that's considered a decoy nearby. How thoughtful!
Standing under a tree in a lightning storm sounds stupid, but in Space Engineers, it's actually the correct thing to do. Decoys are great, but if there's a tree within 400 meters of where the lightning is going to strike, it will always hit the tree first. Yes, it will do enough damage to the tree to one-shot it, but if you're caught out in a thunderstorm in Space Engineers without a base nearby, the safest thing to do is get to a forest.
Rule 4: Trees are the best lightning rods.
What if you don't have any trees nearby? You need a decoy. There are a couple of major checks that are done for decoys that are not called out anywhere and this is what is going to trip most people up. This is also probably the most important thing in this post. Decoys must be static grids. A decoy on a dynamic grid is not considered under any circumstances. This means it has to be embedded in the ground or attached to grid embedded in the ground. This also means the decoy must be large grid, because only large grids can be static without mods or hacks. I can only assume this is a ham-handed attempt simulate the lightning rod being "grounded".
The second big check for a decoy is that it must not be "underground". The rules to this are strange. First of all, if the decoy is under the planet's Minimum Radius, it's considered underground. If it's not under the minimum radius, then it can't be below the local surface. This means don't dig out a big pit and put your decoys in that.
Finally, the rule that most people are familiar with: The target point must be within the Safety Radius of the decoy. This is not actually the safety radius in Content\Data\WeatherEffects.sbc - it is defined on the item definition but the defaults are used since it's not defined in cubeblocks SBC. For Large Grid decoys, the radius is 50m, for small grid the radius is 10m.
Rule 5: Decoys must be static grids and therefore large grid.
Rule 6: Decoys cannot be underground.
Safety Radius: Large Grid 50m, Small Grid (pointless due to not being static) 10m.
The big question: why does my base keep getting hit underground? Well, if you remember the rules for what grids were checked, I didn't mention anything about an "underground" check for the grid, just for decoys. That's right: if you're within the Radius of the storm, your underground base might just get hit if it's not within the safety radius of a decoy that is above ground. There's a couple more checks for that later though, but as I'm sure you've seen if you've experienced this, they can fail.
Onto antennas! Antennas cannot be underground to be considered, and must be turned on. Only proper antennas are considered - beacons are not Radio Antennas for this purpose. The dish is also considered an antenna. Grid size is not checked for antennas, which means that lightning absolutely can and will target Small Grid antennas.
Much like decoys, antennas have a safety radius but it's much smaller than a decoy: Large grid is only 10m and small grid is only 2m.
Rule 7: Decoys will be hit before antennas, and beacons are not antennas for lightning rod purposes.
Safety Radius: Large Grid 10m, Small Grid 2m.
The final check before zapping your precious grid or player that just got elite tools and then ground down the medbay is a raycast. It tries to figure out a point about 50 meters "up" (relative to planet gravity) from where it chose to hit, then does a raycast to that point. If it hits anything on the way, it chooses the point that the ray hit to strike rather than the point it was going to strike originally.
I don't know why a raycast could fail to hit stuff on the way, but I'm guessing "adaptive simulation quality" may have something to do with it - for example, does underground striking happen more at high load? Maybe it spawns the ray inside the ground and then it doesn't hit the back face of the world. Who knows!
Rule 8: A final raycast check is attempted to try to make sure that the lightning hits ground or a block before hitting its target, but it's only 50m long so it may fail to correctly check if your base is too deep or your CPU is struggling.
Armed with the knowledge above, go forth, build (static grid) lightning rods, and conquer Pertam's electrical storms! Smile smugly as lightning falls all around you and never touches your grids!
Warning: Lightning has splash damage. Make sure any deformable blocks such as armor or interior wall blocks are not adjacent to the decoy in any direction if it is unprotected by a non-deformable block. If it is protected, make sure that deformable blocks are not adjacent to the protective block.
If you're just starting out, get your Basic Assembler up ASAP and build a decoy sticking out of the ground or attached to your base - that's enough. Repair it when lightning strikes it.
This is the best lightning rod I've come up with. Just fill the welder with steel plate or connect it to your base conveyor system and relax! Check it after the storm and refill if needed. A solar panel can be used to recharge the battery if needed, but a welder takes almost no power so it should last a good long time, and even if it goes out, decoys don't need power at all.
A wheel is much cheaper and seems to take less damage (3 steel plate worth of damage instead of 4-6) but a 1x1 is not ideal because of the possibility of two hits in very close succession (grid and character strike timers being close), so a 3x3 is the best.
If steel plate is not an issue, a Blast Door variant should be used.
Any non-deformable block with a bunch of steel plate is fine as the top block - even a battery can be used as a top block!