From 32974519492cd46e8392e4f3de0a818957a48b47 Mon Sep 17 00:00:00 2001
From: Kalle Olavi Niemitalo <kon@iki.fi>
Date: Sat, 20 Feb 2010 18:51:32 +0200
Subject: [PATCHv4 1/6] Adjust what gets drawn in front of what

Make DrawLevelCompare ignore the sprite size and consider only the
tiles.  An engineer standing in front of an artil now gets properly
drawn in the front, rather than lying below.

The engineer had DrawLevel = 25 specified twice.  Remove the
duplicate.

Move morels and rocks from DrawLevel = 2 to DrawLevel = 25 so that
people who should be walking behind them don't get drawn in the front.
---
 engine/unit/unit_draw.cpp    |   12 +++++++-----
 units/engineer/engineer.lua  |    1 -
 units/morels/unit-morels.lua |   14 +++++++-------
 units/rocks/unit-rocks.lua   |   12 ++++++------
 4 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/engine/unit/unit_draw.cpp b/engine/unit/unit_draw.cpp
index 70b2f89..0df95b5 100644
--- a/engine/unit/unit_draw.cpp
+++ b/engine/unit/unit_draw.cpp
@@ -910,11 +910,13 @@ static int DrawLevelCompare(const void *v1, const void *v2) {
 	}
 
 	if (drawlevel1 == drawlevel2) {
-		// diffpos compares unit's Y positions (bottom of sprite) on the map
-		// and uses X position in case Y positions are equal.
-		// FIXME: Use BoxHeight?
-		diffpos = c1->Y * TileSizeY + c1->IY + c1->Type->Height -
-			(c2->Y * TileSizeY + c2->IY + c2->Type->Height);
+		// diffpos compares unit's Y positions (center tile)
+		// on the map and uses X position in case Y positions
+		// are equal.  The size of the sprite (->Type->Height)
+		// plays no part in this because many frames typically
+		// have a transpart area at the bottom.
+		diffpos = (c1->Y * TileSizeY + c1->IY + c1->Type->TileHeight * TileSizeY / 2)
+			- (c2->Y * TileSizeY + c2->IY + c2->Type->TileHeight * TileSizeY / 2);
 		return diffpos ? diffpos : c1->X - c2->X ? c1->X - c2->X : c1->Slot - c2->Slot;
 	} else {
 		return drawlevel1 <= drawlevel2 ? -1 : 1;
diff --git a/units/engineer/engineer.lua b/units/engineer/engineer.lua
index 18803a3..c4e9c01 100644
--- a/units/engineer/engineer.lua
+++ b/units/engineer/engineer.lua
@@ -65,7 +65,6 @@ DefineUnitType("unit-engineer", {
     Name = "Engineer",
     Image = {"file", GetCurrentLuaPath().."/unit_engineer.png", "size", {56, 56}},
     Shadow = {"file", GetCurrentLuaPath().."/unit_engineer_s.png", "size", {56, 56}},
-    DrawLevel = 25,
     Animations = "animations-engineer",
     Icon = "icon-engineer",
     Flip = true,
diff --git a/units/morels/unit-morels.lua b/units/morels/unit-morels.lua
index 2e712d1..e551043 100644
--- a/units/morels/unit-morels.lua
+++ b/units/morels/unit-morels.lua
@@ -49,7 +49,7 @@ DefineUnitType("unit-morel-1", {
     Icon = "icon-morel-1",
     Construction = "construction-morels",
     HitPoints = 50,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -86,7 +86,7 @@ DefineUnitType("unit-morel-2", {
     Icon = "icon-morel-2",
     Construction = "construction-morels",
     HitPoints = 50,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -123,7 +123,7 @@ DefineUnitType("unit-morel-3", {
     Icon = "icon-morel-3",
     Construction = "construction-morels",
     HitPoints = 50,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -160,7 +160,7 @@ DefineUnitType("unit-morel-4", {
     Icon = "icon-morel-4",
     Construction = "construction-morels",
     HitPoints = 50,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -197,7 +197,7 @@ DefineUnitType("unit-morel-5", {
     Icon = "icon-morel-5",
     Construction = "construction-morels",
     HitPoints = 50,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -234,7 +234,7 @@ DefineUnitType("unit-morel-6", {
     Icon = "icon-morel-6",
     Construction = "construction-morels",
     HitPoints = 50,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -271,7 +271,7 @@ DefineUnitType("unit-morel-7", {
     Icon = "icon-morel-7",
     Construction = "construction-morels",
     HitPoints = 50,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
diff --git a/units/rocks/unit-rocks.lua b/units/rocks/unit-rocks.lua
index 51a8db3..1c0b6a0 100644
--- a/units/rocks/unit-rocks.lua
+++ b/units/rocks/unit-rocks.lua
@@ -50,7 +50,7 @@ DefineUnitType("unit-rock-1", {
     Construction = "construction-rocks",
     MagmaValue = 5000,
     HitPoints = 3000,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -87,7 +87,7 @@ DefineUnitType("unit-rock-2", {
     Construction = "construction-rocks",
     MagmaValue = 4000,
     HitPoints = 2500,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -124,7 +124,7 @@ DefineUnitType("unit-rock-3", {
     Construction = "construction-rocks",
     MagmaValue = 3000,
     HitPoints = 2500,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -161,7 +161,7 @@ DefineUnitType("unit-rock-4", {
     Construction = "construction-rocks",
     MagmaValue = 2000,
     HitPoints = 2500,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -198,7 +198,7 @@ DefineUnitType("unit-rock-5", {
     Construction = "construction-rocks",
     MagmaValue = 1000,
     HitPoints = 1500,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
@@ -235,7 +235,7 @@ DefineUnitType("unit-rock-6", {
     Construction = "construction-rocks",
     MagmaValue = 1200,
     HitPoints = 1500,
-    DrawLevel = 2,
+    DrawLevel = 25,
     VisibleUnderFog = true,
     NeutralMinimapColor = {196, 196, 196},
     TileSize = {1, 1},
-- 
1.7.0

