So a vessel with no tanks would have 0 propellant mass. Each tank 's propellant mass is 5000.
Not 0...lets say, anything. name anything x. you have a defined maximum propellant mass possible in the main or sump tanks of the vessel. This tank gathers fuel from the external tanks for use in the engines.
So if I attach 1 tank I need to copy the propellant mass of the tank to a shadow tank. So now a shadow tank has a mass of 5000, right
Yes, but you can get the parameters "maximum propellant mass" and "current propellant mass" of the external tank from Orbiters API, no need to use constants, which would cause dependencies.
How does the fuel get from the shadow tanks to the main tank?
Simplest way: You refuel it to maximum every timestep, if possible.
First, you determine the difference between maximum propellant mass in your main tank and current propellant mass.
Then, you determine the available fuel tanks via the shadow tanks (you can include on/off masking here to say that a fuel tank is attached but not used) and count how much fuel they can provide in total. if zero, you can already abort here.
If the external fuel is not enough, increase the propellant mass of the main tank by the amount of fuel available in the external tanks. set all external tanks via their shadows to zero.
Otherwise, distribute the load among the tanks: Calculate the ratio between current fuel mass in the shadow tank and total fuel mass in external/shadow tanks for each external tank. Multiply this ratio by the amount of fuel needed to be transfered to the main tank. The resulting number is the amount of fuel you will transfer from this tank to the main tank. Reduce propellant mass in shadow tank by this amount, repeat for every tank (you have selected).
Now finally increase main propellant mass by the amount fuel transfered - or to maximum, both should be the same.
This assumes a greater pump capacity as the consumption of your engines, but you can easily include a limit there, by setting the maximum amount of fuel needed to be transfered to the maximum amount of fuel that can be transfered in one timestep (amount per second multiplied by delta-T).
Then every timestep set the mass of external tank to that of the shadow tank.
Correct.