From 7bdde0ef6fbd68e5ef82ccb391bc3cf793441e9b Mon Sep 17 00:00:00 2001
From: Daniel Bradshaw <daniel+commits@the-cell.co.uk>
Date: Thu, 28 Jan 2010 04:28:31 +0000
Subject: [PATCH 2/2] Rank objects by distance when finding one close by.  Apply an offset to the pixel based position used in keyboard pickup so that the pickup is in the right place.

---
 src/flooritemmanager.cpp |   42 +++++++++++++++++++++++++++++++++++++++---
 src/game.cpp             |    6 +++++-
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/src/flooritemmanager.cpp b/src/flooritemmanager.cpp
index 502504c..3538327 100644
--- a/src/flooritemmanager.cpp
+++ b/src/flooritemmanager.cpp
@@ -79,16 +79,46 @@ FloorItem *FloorItemManager::findByCoordinates(int x, int y)
 
 FloorItem *FloorItemManager::findCloseToCoordinates(int x, int y)
 {
+    FloorItem *d[2] = {0,0};
     FloorItemIterator i;
     for (i = mFloorItems.begin(); i != mFloorItems.end(); i++)
     {
         switch ((*i)->getX() - x)
         {
-            case -2:
-            case -1:
             case 0:
+                switch ((*i)->getY() - y)
+                {
+                    case 2:
+                    case -2:
+                        d[1] = *i;
+                        break;
+                    case 1:
+                    case -1:
+                        d[0] = *i;
+                        break;
+                    case 0:
+                        return *i;
+                }
+                break;
+
             case 1:
+            case -1:
+                switch ((*i)->getY() - y)
+                {
+                    case 2:
+                    case -2:
+                        d[1] = *i;
+                        break;
+                    case 1:
+                    case -1:
+                    case 0:
+                        d[0] = *i;
+                        d[0] = *i;
+                }
+                break;
+
             case 2:
+            case -2:
                 switch ((*i)->getY() - y)
                 {
                     case -2:
@@ -96,10 +126,16 @@ FloorItem *FloorItemManager::findCloseToCoordinates(int x, int y)
                     case 0:
                     case 1:
                     case 2:
-                        return *i;
+                        d[1] = *i;;
                 }
         }
     }
 
+    if (d[0] != 0)
+        return d[0];
+
+    if (d[1] != 0)
+        return d[1];
+
     return NULL;
 }
diff --git a/src/game.cpp b/src/game.cpp
index 59227eb..bede5c0 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -749,8 +749,12 @@ void Game::handleInput()
                     case KeyboardConfig::KEY_PICKUP:
                         {
                             const Vector &pos = player_node->getPosition();
+                            /* TODO:
+                             * On eA, .x is 0.5 tiles out, .y is 1 tile out.
+                             * The -1 is a hack, find better way asap.
+                             */
                             Uint16 x = (int) pos.x / 32;
-                            Uint16 y = (int) pos.y / 32;
+                            Uint16 y = (int) (pos.y - 1) / 32;
 
                             FloorItem *item =
                                 floorItemManager->findCloseToCoordinates(x, y);
-- 
1.6.3.3

