I'm making a top down action rpg with a tiled terrain. This isn't exactly a pixelart question but since so many here seem to be creating similar things my hope is that someone will have already figured this out (or figured out that it is impossible).

The game generates a terrain from a height map and it can create terrain such as the first image. It also generates colliders for different height levels that the player will collide with. The yellow lines show the colliders for the player when standing on the ground level and the red lines show the colliders when standing on top of the terrain, shown in the second image. The blue diamond is the players own collider.
One thing to note is that the top corners are actually two tiles on top of each other with the ground grass tile drawn behind and the cliff corner drawn on top.
What I would like is for the player sprite to be able to stand in front of the terrain and be drawn on top, stand to the side of the terrain and be drawn below, stand behind and be drawn below and finally also be able to stand on top of the terrain and then be drawn on top, as shown in the second image.
Another important thing to note is that the player sprite can have pixels both to the left and right of the player collider, when walking the most extended leg pose is the most extreme but when have equipment in hand it becomes even further extended outside the collider. Also the player is about 2,5 tiles tall, but I would like to find a solution that allows much taller sprites to walk around the terrain.
If I sort the terrain according to its height as shown in the third image the player can stand on top, to the left and behind the terrain and be drawn correctly but its head gets drawn below the tiles with height 1 when standing infront.
To fix this I added y-value to the sorting order, shown in the fourth image, combined with height, shown in the fifth image, this fixed the problem of standing in front of the terrain. But sadly it added the issue shown in the sixth image when standing on the side of terrain, parts of the sprite gets drawn over terrain tiles higher up on the y-axis.
The player gets drawn after all tiles with the same sorting order as the one the player is standing on has been drawn but before any with higher sorting order is drawn.
Is there any way of sorting the tiles draw order to fix all the cases or is this just impossible? I've been trying to figure this out for 3 days non stop now and I just can't crack it (also starting to get a bit insane

). But I feel a bit like the fact that the sprites extend outside the collision bounds messes it all up, but that's sort of a must for me to have spears and what not as equipment.
Any and all help is very much appreciated!