https://code.wildfiregames.com/D2459#inline-47983