No secrets - the C++ code is included in the add-on.
Here's a brief overview of my rather simple method - it's only a few hundred lines of code, after all.
1. Make an attitude control algorithm for the vessel, that can receive a "target flight vector" in local vessel frame, and slew the vessel to that attitude.Other algorithms will calculate the target flight vector (and set main engine throttle) and pass the target flight vector to the attitude control.
Split the task into sections:
2. Make a "boostback" burn to put the vessel on a ballistic trajectory to it's target.
How to find the flight vector?
There are an infinite number of possible ballistic trajectories from the vessel's current position to the target - I figured the "minimum energy ballistic trajectory" would be a logical choice - the less energy you put in, the less you have to take out later.
This handy and venerable document has the maths needed to calculate the vertical and tangential velocity, for a minimum energy ballistic trajectory from the vessel's current position to its target:
http://naca.central.cranfield.ac.uk/reports/arc/cp/0604.pdf It only deals with a "non-rotating Earth" case, but it's fairly easy to compensate by adding a bit to your target's longitude, according to the flight time and Earth rotation rate.
You can find the direction of your tangential velocity by taking the cross product of your target's position vector and your position vector (global frame, rel.Earth), then taking the cross product of the result with your position vector again. Normalise the resulting vector to the tangential velocity as calculated above. Then normalise your position vector to the vertical velocity as calculated previously, and add it to your tangential velocity vector. Subtract your current velocity(global frame, rel.Earth) Rotate the result into local vessel frame. This is your target flight vector.
Burn main engines until the target flight vector gets real short, then switch off.
Note: you need to over burn a little so that you can reduce velocity, and hence range, during reentry. I managed this by setting a target altitude offset i.e. aiming for a point above the target itself (typically 3-8km). The offset is adjusted according to flight vector azimuth, excess fuel mass, final impact trajectory angle.
3. Turn the aft-end into the flight vector for reentry and landing.
4. Calculate the vessel's current range(distance it will travel before it hits the target's altitude, using a lower altitude offset than before) based on current altitude and velocity. This page has the necessary maths for calculating the range using a simple Stoke's(I think) drag term:
http://en.wikipedia.org/wiki/Trajec...rajectory_of_a_projectile_with_air_resistance
From the above, given your current altitude and vertical velocity, you can calculate the required tangential velocity to reach the target. From current vessel mass, thrust and pitch, you can calculate time required to burn to achieve required tangential velocity. Start burn when time required for burn is equal to current time until target altitude(plus a margin for safety). Eliminate cross range errors by rotating flight vector about axis orthogonal to airspeed vector and horizontal.
Stop burn when vessel range is equal/less than target range.
5. Repeat process at lower altitude using an even lower target altitude offset, and slightly modified drag term for denser atmosphere.
6. Eliminate trajectory errors by using lift during last bit of freefall. Rotate flight vector around axis orthogonal to current flight vector and vector from vessel to target.
7. Start landing burn at altitude determined by fuel reserves(plus an offset). Set thrust to reduce vertical velocity to ~zero by landing. Eliminate trajectory errors by rotating flight vector towards/away from target during descent.
8. Land softly as a feather.