bool PathFindingApp::update(yam2d::ESContext* ctx, float deltaTime)
{
	if (!m_appRunning)
		return false;

	if (deltaTime > 0.1f)
		deltaTime = 0.1f;

#if defined(_WIN32)
	if( isKeyReleased(yam2d::KEY_SPACE) )
	{
		quit();
	}
	
	// Restart search if r pressed
	if (isKeyReleased(yam2d::KEY_R))
	{
		m_textureStartCase = 0;
		m_texturePathFound = 0;
	}
#endif

	if (m_textureStartCase == 0)
	{
		// Delete old and load new
		m_texturePathFound = 0;
		const char* const inFileName = "input.png";

		char buf[100];
		sprintf_s(buf, "Start finding path from input image: \"%s\"", inFileName);
		yam2d::esLogMessage(buf);
		m_text->setText(buf);
		m_textureStartCase = new yam2d::Texture(inFileName, true);

		// Copy input data to map.
		m_searchTimer = 0.0f;
		int width = m_textureStartCase->getWidth();
		int height = m_textureStartCase->getHeight();
		int bpp = m_textureStartCase->getBytesPerPixel();
		yam2d::Ref<yam2d::StreamTexture> newTexture = new yam2d::StreamTexture();
		newTexture->setData(m_textureStartCase->getData(), width, height, bpp);
		m_texturePathFound = newTexture;
		m_searchCompleted = false;
	}

	if (!m_searchCompleted)
	{
		// Find start and end
		int startX, startY, endX, endY;
		startX = startY = endX = endY = -1;
		for (int y = 0; y < m_textureStartCase->getHeight(); ++y)
		{
			for (int x = 0; x < m_textureStartCase->getWidth(); ++x)
			{
				unsigned char* p = m_textureStartCase->getPixel(x, y);
				if (isRed(p))
				{
					// Red pixel
					startX = x;
					startY = y;
				}
				else if (isGreen(p))
				{
					// Green pixel
				}
				else if (isBlue(p))
				{
					// Blue pixel
					endX = x;
					endY = y;
				}
			}
		}

		// Update path find!! Set m_searchCompleted to true, when path found, so the texture data is updated.
		if (startX >= 0 && startY >= 0 && endX >= 0 && endY >= 0)
		{
			yam2d::ElapsedTimer timer;
			timer.reset();
			m_searchCompleted = doPathfinding(startX, startY, endX, endY);
			m_searchTimer += timer.getTime();
			// 	Update new data to the GPU
			m_texturePathFound->updateData();
		}
		else
		{
			assert(0);
		}

		if (m_searchCompleted)
		{
			char buf[100];
			sprintf_s(buf, "Path find done. Time spent %.3f seconds", m_searchTimer);
			yam2d::esLogMessage("%s\n",buf);
			m_text->setText(buf);
		}
	} // if (!m_searchCompleted)

	// Clear sprite before add new dynamic sprites.
	m_batch->clear();

	// Add sprites. 
	m_batch->addSprite(m_textureStartCase, m_spriteStartCase, yam2d::vec2(-256.0f - 20.0f, 0.0f), 0.0f, yam2d::vec2(512.0f, 512.0f));
	m_batch->addSprite(m_texturePathFound, m_spritePathFound, yam2d::vec2(256.0f + 20.0f, 0.0f), 0.0f, yam2d::vec2(512.0f, 512.0f));

	// Add text to position -400,300
	m_batch->addText(m_fontTexture, m_text, yam2d::vec2(0, ((float)ctx->height) / 2.0f - 20.0f), 0.0f);

	return true;
}
Exemple #2
0
int main(int argc, char* argv[])
{
	Map map;
	Car cars[3] = {NULL, NULL, NULL};
	int x, y, i, rounds = 0, needPathfinding = 0, firstLoop = 1;
	List path;
	Position position;
	Vector acc;
	int fuel;

	srand(time(NULL));

	freopen("pshr4log.txt", "w+", stderr);
	// freopen("map.txt", "r", stdin);

	LOGINFO("Starting");

	initGC();

	map = Map_load(stdin);

	LOGINFO("Map loaded");

	while(!feof(stdin))
	{
		LOGINFO1I("===== Round number %d =====", rounds);

		for(i = 0; i < 3; i++)
		{
			fscanf(stdin, "%d %d", &x, &y);

			LOGINFO3I("Car number %i, position : %d %d", i, x, y);

			if(cars[i] == NULL)
				cars[i] = Car_new(x, y);
			else
				Car_updatePosition(cars[i], x, y);

			if(Car_updateArrivedStatus(cars[i], map))
			{
				recomputeDistances(map, cars);
				LOGINFO("I see you've arrived, let me see where I can park now...");
				needPathfinding = 1;
			}
		}

		if(firstLoop)
		{
			firstLoop = 0;
			path = doPathfinding(map, cars);
			List_tail(path);
			List_prev(path);
		}

		position = List_getCurrent(path);

		for(i = 1; i < 3; i++)
		{
			if(Position_equal(position, Car_getPosition(cars[i])))
			{
				needPathfinding = 1;
				LOGINFO("YOU TOOK MY SPOT JERK");
			}
		}

		if(needPathfinding)
		{
			freePath(path);
			path = doPathfinding(map, cars);
			List_tail(path);
			List_prev(path);
			needPathfinding = 0;
			position = List_getCurrent(path);
		}

		if(position == NULL)
		{
			goRandom();
		}
		else
		{
			LOGINFO2I("Going to %d %d", position->x, position->y);
			acc = Car_getAccelerationToReach(cars[0], position);
			go(acc->x, acc->y);

			if(Vector_squaredLength(acc) > 2)
			{
				Car_useBoost(cars[0]);
				LOGINFO("Using a boost");
			}

			Vector_delete(acc);
		}

		List_prev(path);

		rounds++;
	}

	LOGINFO("End");

	freePath(path);

	for(i = 0; i < 3; i++)
	{
		Car_delete(cars[i]);
	}

	Map_delete(map);
	freeGC();

	return 0;
}