From 4a68ceaeb7561f6d00cd25d80b075956c8f6fd39 Mon Sep 17 00:00:00 2001
From: scrawl <scrawl@baseoftrash.de>
Date: Wed, 19 Aug 2015 19:06:24 +0200
Subject: [PATCH 1/3] Restrict the OS X cursor workaround to Intel graphics
 systems

---
 components/sdlutil/sdlcursormanager.cpp | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/components/sdlutil/sdlcursormanager.cpp b/components/sdlutil/sdlcursormanager.cpp
index 70a1593066..b64b68132a 100644
--- a/components/sdlutil/sdlcursormanager.cpp
+++ b/components/sdlutil/sdlcursormanager.cpp
@@ -127,11 +127,13 @@ namespace
         glViewport(0, 0, width, height);
 
 #if defined(__APPLE__)
-        // FIXME: why are the extra flips needed on Mac? glReadPixels bug?
-        osg::ref_ptr<osg::Geometry> geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,1,0), osg::Vec3(2,0,0), osg::Vec3(0,-2,0));
-#else
-        osg::ref_ptr<osg::Geometry> geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,-1,0), osg::Vec3(2,0,0), osg::Vec3(0,2,0));
+        // Extra flip needed on Intel graphics OS X systems due to a driver bug
+        std::string vendorString = (const char*)glGetString(GL_VENDOR);
+        if (vendorString.find("Intel") != std::string::npos)
+            osg::ref_ptr<osg::Geometry> geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,1,0), osg::Vec3(2,0,0), osg::Vec3(0,-2,0));
+        else
 #endif
+        osg::ref_ptr<osg::Geometry> geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,-1,0), osg::Vec3(2,0,0), osg::Vec3(0,2,0));
 
         geom->drawImplementation(renderInfo);
 

From 166df28906dcfa1e7758d9e97e5e4e0b6de781f7 Mon Sep 17 00:00:00 2001
From: Nikolay Kasyanov <corrmage@gmail.com>
Date: Wed, 19 Aug 2015 21:23:16 +0200
Subject: [PATCH 2/3] OS X cursor workaround build fix

---
 components/sdlutil/sdlcursormanager.cpp | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/components/sdlutil/sdlcursormanager.cpp b/components/sdlutil/sdlcursormanager.cpp
index b64b68132a..e1a67aff81 100644
--- a/components/sdlutil/sdlcursormanager.cpp
+++ b/components/sdlutil/sdlcursormanager.cpp
@@ -126,14 +126,16 @@ namespace
 
         glViewport(0, 0, width, height);
 
+        osg::ref_ptr<osg::Geometry> geom;
+
 #if defined(__APPLE__)
         // Extra flip needed on Intel graphics OS X systems due to a driver bug
         std::string vendorString = (const char*)glGetString(GL_VENDOR);
         if (vendorString.find("Intel") != std::string::npos)
-            osg::ref_ptr<osg::Geometry> geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,1,0), osg::Vec3(2,0,0), osg::Vec3(0,-2,0));
+            geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,1,0), osg::Vec3(2,0,0), osg::Vec3(0,-2,0));
         else
 #endif
-        osg::ref_ptr<osg::Geometry> geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,-1,0), osg::Vec3(2,0,0), osg::Vec3(0,2,0));
+        geom = osg::createTexturedQuadGeometry(osg::Vec3(-1,-1,0), osg::Vec3(2,0,0), osg::Vec3(0,2,0));
 
         geom->drawImplementation(renderInfo);
 

From c0d3804b4f3371d4498b771943121a1d249d4c62 Mon Sep 17 00:00:00 2001
From: dteviot <dteviot@gmail.com>
Date: Thu, 20 Aug 2015 21:50:58 +1200
Subject: [PATCH 3/3] Correctly handle disjoint pathgrid (Fixes #2871)

Bugfix:
When
1. Cell has multiple subgrids  (i.e. path grid is disjoint)
2. Distance between destination and pathgrid point 0 is less than distance to points of subgrid closest to start point
Then getClosestReachablePoint() returns pathgrid point 0 as the end point.
This is invalid, this end point cannot be reached from the start point.
---
 apps/openmw/mwmechanics/pathfinding.cpp | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/apps/openmw/mwmechanics/pathfinding.cpp b/apps/openmw/mwmechanics/pathfinding.cpp
index 798a0ea21c..9013d32699 100644
--- a/apps/openmw/mwmechanics/pathfinding.cpp
+++ b/apps/openmw/mwmechanics/pathfinding.cpp
@@ -53,7 +53,8 @@ namespace
     {
         assert(grid && !grid->mPoints.empty());
 
-        float distanceBetween = distanceSquared(grid->mPoints[0], pos);
+        float closestDistanceBetween = distanceSquared(grid->mPoints[0], pos);
+        float closestDistanceReachable = closestDistanceBetween;
         int closestIndex = 0;
         int closestReachableIndex = 0;
         // TODO: if this full scan causes performance problems mapping pathgrid
@@ -61,17 +62,25 @@ namespace
         for(unsigned int counter = 1; counter < grid->mPoints.size(); counter++)
         {
             float potentialDistBetween = distanceSquared(grid->mPoints[counter], pos);
-            if(potentialDistBetween < distanceBetween)
+            if (potentialDistBetween < closestDistanceReachable)
             {
                 // found a closer one
-                distanceBetween = potentialDistBetween;
-                closestIndex = counter;
                 if (cell->isPointConnected(start, counter))
                 {
+                    closestDistanceReachable = potentialDistBetween;
                     closestReachableIndex = counter;
                 }
+                if (potentialDistBetween < closestDistanceBetween)
+                {
+                    closestDistanceBetween = potentialDistBetween;
+                    closestIndex = counter;
+                }
             }
         }
+
+        // invariant: start and endpoint must be connected
+        assert(cell->isPointConnected(start, closestReachableIndex));
+
         // AiWander has logic that depends on whether a path was created, deleting
         // allowed nodes if not.  Hence a path needs to be created even if the start
         // and the end points are the same.