示例#1
0
TilePosition CoverMap::findBuildSpot(UnitType toBuild, TilePosition start)
{
	//Check start pos
	if (canBuildAt(toBuild, start)) return start;

	//Search outwards
	bool found = false;
	int cDiff = 1;
	TilePosition spot = TilePosition(-1, -1);
	while (!found) 
	{
		//Top
		TilePosition s = TilePosition(start.x() - cDiff, start.y() - cDiff);
		TilePosition e = TilePosition(start.x() + cDiff, start.y() - cDiff);
		spot = findSpotAtSide(toBuild, s, e);
		if (spot.x() != -1 && spot.y() != -1)
		{
			found = true;
			break;
		}

		//Bottom
		s = TilePosition(start.x() - cDiff, start.y() + cDiff);
		e = TilePosition(start.x() + cDiff, start.y() + cDiff);
		spot = findSpotAtSide(toBuild, s, e);
		if (spot.x() != -1 && spot.y() != -1)
		{
			found = true;
			break;
		}

		//Left
		s = TilePosition(start.x() - cDiff, start.y() - cDiff);
		e = TilePosition(start.x() - cDiff, start.y() + cDiff);
		spot = findSpotAtSide(toBuild, s, e);
		if (spot.x() != -1 && spot.y() != -1)
		{
			found = true;
			break;
		}

		//Right
		s = TilePosition(start.x() + cDiff, start.y() - cDiff);
		e = TilePosition(start.x() + cDiff, start.y() + cDiff);
		spot = findSpotAtSide(toBuild, s, e);
		if (spot.x() != -1 && spot.y() != -1)
		{
			found = true;
			break;
		}

		cDiff++;
		if (cDiff > range) found = true;
	}
	
	return spot;
}
void build_test() {

	enum {GRID_SIZE = 32};

	// build map
	GridMap *gm = new GridMap(30, 30, GRID_SIZE);
	gm->setTarget(5 * GRID_SIZE, 5 * GRID_SIZE);

	for (int i = 0; i < 20; ++i) {
		gm->setWalkableAt(10 * GRID_SIZE, i * GRID_SIZE, false);
	}

	for (int i = 10; i < 20; ++i) {
		gm->setWalkableAt(i * GRID_SIZE, 10 * GRID_SIZE, false);
	}

	gm->updateRoute();

	// test route
	gm->printRoute();

	gm->clearCreepsInfo();
	gm->addCreepsAt(1 * GRID_SIZE, 1 * GRID_SIZE);

	canBuildAt(gm, 1 * GRID_SIZE, 1 * GRID_SIZE);
	canBuildAt(gm, 10 * GRID_SIZE, 10 * GRID_SIZE);
	canBuildAt(gm, 20 * GRID_SIZE, 20 * GRID_SIZE);

	// clear grids again
	puts("Clear grids again");
	gm->clearCreepsInfo();
	canBuildAt(gm, 1 * GRID_SIZE, 1 * GRID_SIZE);
	printf("Center of (5, 15) : (%d, %d)\n", 
			gm->toGridCenterX(5), gm->toGridCenterY(15));

	CreepFactory *factory = new CreepFactory();

	Creep *creep = factory->getCreep(creep::NORMAL);
	creep->setX(500.0);
	creep->setY(500.0);
	creep->setGridMap(gm);

	printf("%d, %d\n", gm->getNextX(193, 193), gm->getNextY(193, 193));
}
示例#3
0
TilePosition CoverMap::findSpotAtSide(UnitType toBuild, TilePosition start, TilePosition end)
{
	int dX = end.x() - start.x();
	if (dX != 0) dX = 1;
	int dY = end.y() - start.y();
	if (dY != 0) dY = 1;

	TilePosition cPos = start;
	bool done = false;
	while (!done) 
	{
		if (canBuildAt(toBuild, cPos)) return cPos;
		int cX = cPos.x() + dX;
		int cY = cPos.y() + dY;
		cPos = TilePosition(cX, cY);
		if (cPos.x() == end.x() && cPos.y() == end.y()) done = true;
	}

	return TilePosition(-1, -1);
}