This is more of a suggestion rather than a tutorial because of the simplicity:
Some background: no matter which mod I played, I was always plagued by the melee bug. Once the AI enemy gets into shooting range, its units play hopskotch with each other, turn around each other, all while under fire, eventually charging you.
Actually, this is just because of lack of space for the AI. It appears to need about 60-70 yards room to shuffle about before it gets its formation correct. I doubled the ranges for all musket projectiles (160 for flintlock, 200 for light infantry, 250 for rifles), and voila, and I have played through a campaign, and not once have I encountered the melee bug.
The enemy does a little shuffle at the edge of your shooting range, and forms lines perfectly, so you encounter musket duels at 60-120 yards. There is the occasional 150 yard musket duel, but by adjusting attackpower_extreme_range_multplier (not a typo) and attackpower_long_range_multiplier to 0.2, and 0.4 respectively, musket power at 140 yards causes very few casulties.
I even tried attacking the AI on two sides on 2-3 occasions, and the AI formed a reasonable musket based defence against each attack.
So if you want to try this, use PFM, open projectiles, and adjust the flintlock, carbine, light infantry, rifle, matchlock rows under the range heading, and double them. I think you will be pleasantly surprised by the effects.