void FindTheSpotGameEngine::Prepare()
{
	if (g_pTile)
	{
		delete g_pTile;
		g_pTile = NULL;
	}

	g_distance = 0;
	Utils::GetLocation(&g_startLocation);
	IwRandSeed(s3eTimerGetMs());

	IGameHandler* pHandler = (IGameHandler*)g_pGameHandler;
	Region* pRegion = pHandler->GetBoundingRegion();

	// Calculate a random location within a given radius
	int tryCount = 20;
	do
	{
		double angle = PI / 180.0 * IwRandMinMax(-180, 180); // 360 degree range
		int32 radius = IwRandMinMax(25, 50); // 50 meters

		LiveMaps::CalculateLatLongInDirection(&g_startLocation, radius, angle, &g_randLocation);

		if (pRegion->Contains(g_randLocation))
		{
			break;
		}
	} while (tryCount-- > 0);

	int acx = LiveMaps::LongitudeToXAtZoom(g_randLocation.m_Longitude, LiveMaps::MaxZoom);
	int acy = LiveMaps::LatitudeToYAtZoom(g_randLocation.m_Latitude, LiveMaps::MaxZoom);

	int tx = acx / 256;
	int ty = acy / 256;

	int x = tx * 256;
	int y = ty * 256;

	g_tileLoc.x = (acx - x);
	g_tileLoc.y = (acy - y);

	char szQuad[20];
    int server = 0;
	LiveMaps::TileToQuadKey(szQuad, tx, ty, LiveMaps::MaxZoom);

	char szImageUrl[256];
	sprintf(szImageUrl, "http://r%i.ortho.tiles.virtualearth.net/tiles/%s%s.%s?g=22", server, "h", szQuad, "jpg");

	g_topLeft.m_Longitude = LiveMaps::XToLongitudeAtZoom(x, LiveMaps::MaxZoom);
	g_topLeft.m_Latitude = LiveMaps::YToLatitudeAtZoom(y, LiveMaps::MaxZoom);

	g_botRight.m_Longitude = LiveMaps::XToLongitudeAtZoom(x+256, LiveMaps::MaxZoom);
	g_botRight.m_Latitude = LiveMaps::YToLatitudeAtZoom(y+256, LiveMaps::MaxZoom);

	Utils::DownloadMapTile(&g_pTile, szImageUrl);
}
void CaptureGameEngine::Start()
{
	Utils::GetLocation(&g_startLocation);
	IwRandSeed(s3eTimerGetMs());

	// get the 9 tiles around our location
	int acx = LiveMaps::LongitudeToXAtZoom(g_startLocation.m_Longitude, LiveMaps::MaxZoom);
	int acy = LiveMaps::LatitudeToYAtZoom(g_startLocation.m_Latitude, LiveMaps::MaxZoom);

	int tx = acx / 256;
	int ty = acy / 256;

	int x = tx * 256;
	int y = ty * 256;

	g_tileLoc.x = (acx - x);
	g_tileLoc.y = (acy - y);

	char szQuad[20];
	char szImageUrl[256];

	g_topLeft.x = x;// + 128;
	g_topLeft.y = y;// + 128;

	for (int ttx = tx - 1; ttx <= tx + 1; ++ttx)
	{
		for (int tty = ty - 1; tty <= ty + 1; ++tty)
		{
			int server = 0;
			LiveMaps::TileToQuadKey(szQuad, ttx, tty, LiveMaps::MaxZoom);

			sprintf(szImageUrl, "http://r%i.ortho.tiles.virtualearth.net/tiles/%s%s.%s?g=22", server, "h", szQuad, "jpg");

			g_pTiles[1 + (ttx-tx)][1 + (tty-ty)] = 0;
			Utils::DownloadMapTile(&g_pTiles[1 + (ttx-tx)][1 + (tty-ty)], szImageUrl);
		}
	}

	s3eLocation bottomRight, topLeft, topRight, bottomLeft, center;
	bottomRight.m_Latitude = LiveMaps::YToLatitudeAtZoom(y+512, LiveMaps::MaxZoom);
	bottomRight.m_Longitude = LiveMaps::XToLongitudeAtZoom(x+512, LiveMaps::MaxZoom);
	topLeft.m_Latitude = LiveMaps::YToLatitudeAtZoom(y-256, LiveMaps::MaxZoom);
	topLeft.m_Longitude = LiveMaps::XToLongitudeAtZoom(x-256, LiveMaps::MaxZoom);

	if (topLeft.m_Longitude > bottomRight.m_Longitude)
	{
		float longitude = bottomRight.m_Longitude;
		bottomRight.m_Longitude = topLeft.m_Longitude;
		topLeft.m_Longitude = longitude;
	}
	if (topLeft.m_Latitude > bottomRight.m_Latitude)
	{
		float latitude = bottomRight.m_Latitude;
		bottomRight.m_Latitude = topLeft.m_Latitude;
		topLeft.m_Latitude = latitude;
	}

	topRight.m_Longitude = topLeft.m_Longitude;
	topRight.m_Latitude = bottomRight.m_Latitude;

	center.m_Latitude = (bottomRight.m_Latitude + topLeft.m_Latitude) / 2;
	center.m_Longitude = (bottomRight.m_Longitude + topLeft.m_Longitude) / 2;

	float maxDir = LiveMaps::CalculateDistance(topLeft, topRight) / 2.1;
	
	IGameHandler* pHandler = (IGameHandler*)g_pGameHandler;
	Region* pRegion = pHandler->GetBoundingRegion();

	for (int i = 0; i < 15; ++i)
	{
		s3eLocation* randLoc = new s3eLocation;
		// Calculate a random location within a given radius
		int tryCount = 20;
		do
		{
			double angle = PI / 180.0 * IwRandMinMax(-180, 180); // 360 degree range
			int32 radius = IwRandMinMax(5, maxDir); // 50 meters

			LiveMaps::CalculateLatLongInDirection(&center, radius, angle, randLoc);

			s3eLocation testLoc = *randLoc;

			if (pRegion->Contains(testLoc) && (testLoc.m_Latitude >= topLeft.m_Latitude) && (testLoc.m_Latitude <= bottomRight.m_Latitude) && (testLoc.m_Longitude >= topLeft.m_Longitude) && (testLoc.m_Longitude <= bottomRight.m_Longitude))
			{
				break;
			}
		} while (tryCount-- > 0);

		g_pCaptures.push_back(randLoc);
	}
	g_caughtAllTime = 0;
}