コード例 #1
0
ファイル: Squad.cpp プロジェクト: rarosu/FnulAI
BaseAgent* Squad::getCenterAgent()
{
	TilePosition center = getCenter();

	BaseAgent* cAgent = NULL;
	double dist = 10000;

	for (int i = 0; i < (int)agents.size(); i++)
	{
		if (agents.at(i)->isAlive())
		{
			if ( (isAir() && agents.at(i)->getUnitType().isFlyer()) || (isGround() && !agents.at(i)->getUnitType().isFlyer()))
			{
				double cDist = agents.at(i)->getUnit()->getTilePosition().getDistance(center);
				if (cDist < dist)
				{
					cAgent = agents.at(i);
					dist = cDist;
				}
			}
		}
	}

	return cAgent;
}
コード例 #2
0
int SimpleDeadlockDetector::init(const BoardState& s) {
	std::vector<int> targets;
	int index = 0;
	for (auto i = s.begin(); i != s.end(); ++i, ++index) {
		if (!isTarget(*i) && !isPackageOnTarget(*i)) {
			continue;
		}
		targets.push_back(index);
	}
	Log::debug(LOG_GAMEIMPL, "Found %i targets", (int)targets.size());

	BoardState copy(s);
	index = 0;
	for (auto i = s.begin(); i != s.end(); ++i, ++index) {
		const char field = *i;
		if (isPackage(field)) {
			copy.clearFieldForIndex(index);
			copy.setFieldForIndex(index, Sokoban::GROUND);
			Log::debug(LOG_GAMEIMPL, "replaced package with ground at %i", index);
		} else if (isPackageOnTarget(field)) {
			copy.clearFieldForIndex(index);
			copy.setFieldForIndex(index, Sokoban::TARGET);
			Log::debug(LOG_GAMEIMPL, "replaced packageontarget with target at %i", index);
		}
	}
#ifdef DEBUG
	Log::debug(LOG_GAMEIMPL, "board state:\n%s", copy.toString().c_str());
#endif

	for (int targetIndex : targets) {
		moveBackwards(copy, targetIndex);
	}

	index = 0;
	for (auto i = copy.begin(); i != copy.end(); ++i, ++index) {
		const char field = *i;
		if (!isGround(field) && !isPackage(field)) {
			continue;
		}
		if (_visited.find(index) != _visited.end()) {
			continue;
		}
#ifdef DEBUG
		int col;
		int row;
		s.getColRowFromIndex(index, col, row);
		Log::debug(LOG_GAMEIMPL, "Simple deadlock detected at %i:%i", col, row);
#endif
		_deadlocks.insert(index);
	}
	Log::info(LOG_GAMEIMPL, "Found %i simple deadlocks", (int)_deadlocks.size());
	Log::debug(LOG_GAMEIMPL, "Visited %i fields", (int)_visited.size());
	_visited.clear();
	return (int)_deadlocks.size();
}
コード例 #3
0
ファイル: Squad.cpp プロジェクト: rarosu/FnulAI
TilePosition Squad::getCenter()
{
	if (agents.size() == 1)
	{
		return agents.at(0)->getUnit()->getTilePosition();
	}

	int cX = 0;
	int cY = 0;
	int cnt = 0;

	//Calculate sum (x,y)
	for (int i = 0; i < (int)agents.size(); i++)
	{
		if (agents.at(i)->isAlive())
		{
			cX += agents.at(i)->getUnit()->getTilePosition().x();
			cY += agents.at(i)->getUnit()->getTilePosition().y();
			cnt++;
		}
	}

	//Calculate average (x,y)
	if(cnt > 0)
	{
		cX = cX / cnt;
		cY = cY / cnt;
	}

	//To make sure the center is in a walkable tile, we need to
	//find the unit closest to center
	TilePosition c = TilePosition(cX, cY);
	TilePosition bestSpot = c;
	double bestDist = 10000;
	for (int i = 0; i < (int)agents.size(); i++)
	{
		if (agents.at(i)->isAlive())
		{
			if ( (isAir() && agents.at(i)->getUnitType().isFlyer()) || (isGround() && !agents.at(i)->getUnitType().isFlyer()))
			{
				double dist = agents.at(i)->getUnit()->getTilePosition().getDistance(c);
				if (dist < bestDist)
				{
					bestDist = dist;
					bestSpot = agents.at(i)->getUnit()->getTilePosition();
				}
			}
		}
	}

	return bestSpot;
}
コード例 #4
0
ファイル: thing.cpp プロジェクト: Cayan/otclient
int Thing::getStackPriority()
{
    if(isGround())
        return 0;
    else if(isGroundBorder())
        return 1;
    else if(isOnBottom())
        return 2;
    else if(isOnTop())
        return 3;
    else if(asCreature())
        return 4;
    else // common items
        return 5;
}
コード例 #5
0
ファイル: Squad.cpp プロジェクト: rarosu/FnulAI
void Squad::setGoal(TilePosition mGoal)
{
	if (isAttacking())
	{
		if (goal.x() != -1)
		{
			return;
		}
	}

	if (mGoal.x() != goal.x() || mGoal.y() != goal.y())
	{
		goalSetFrame = Broodwar->getFrameCount();
		if (isGround())
		{
			int d = (int)goal.getDistance(mGoal);
			if (d >= 10)
			{
				if ((int)agents.size() > 0)
				{
					Pathfinder::Instance().requestPath(getCenter(), mGoal);
					if (!Pathfinder::Instance().isReady(getCenter(), mGoal))
					{
						return;
					}
					path = Pathfinder::Instance().getPath(getCenter(), mGoal);
					arrivedFrame = -1;
					pathIndex = 10;

					if (path.size() == 0) return;
				}
			}
		}

		this->goal = mGoal;
		setMemberGoals(goal);
	}
}
コード例 #6
0
ファイル: item.cpp プロジェクト: Pucker/otclient
void Item::calculatePatterns(int& xPattern, int& yPattern, int& zPattern)
{
    if(isStackable() && getNumPatternX() == 4 && getNumPatternY() == 2) {
        if(m_countOrSubType <= 0) {
            xPattern = 0;
            yPattern = 0;
        } else if(m_countOrSubType < 5) {
            xPattern = m_countOrSubType-1;
            yPattern = 0;
        } else if(m_countOrSubType < 10) {
            xPattern = 0;
            yPattern = 1;
        } else if(m_countOrSubType < 25) {
            xPattern = 1;
            yPattern = 1;
        } else if(m_countOrSubType < 50) {
            xPattern = 2;
            yPattern = 1;
        } else {
            xPattern = 3;
            yPattern = 1;
        }
    } else if(isHangable()) {
        const TilePtr& tile = getTile();
        if(tile) {
            if(tile->mustHookSouth())
                xPattern = getNumPatternX() >= 2 ? 1 : 0;
            else if(tile->mustHookEast())
                xPattern = getNumPatternX() >= 3 ? 2 : 0;
        }
    } else if(isSplash() || isFluidContainer()) {
        int color = Otc::FluidTransparent;
        switch(m_countOrSubType) {
            case Otc::FluidNone:
                color = Otc::FluidTransparent;
                break;
            case Otc::FluidWater:
                color = Otc::FluidBlue;
                break;
            case Otc::FluidMana:
                color = Otc::FluidPurple;
                break;
            case Otc::FluidBeer:
                color = Otc::FluidBrown;
                break;
            case Otc::FluidOil:
                color = Otc::FluidBrown;
                break;
            case Otc::FluidBlood:
                color = Otc::FluidRed;
                break;
            case Otc::FluidSlime:
                color = Otc::FluidGreen;
                break;
            case Otc::FluidMud:
                color = Otc::FluidBrown;
                break;
            case Otc::FluidLemonade:
                color = Otc::FluidYellow;
                break;
            case Otc::FluidMilk:
                color = Otc::FluidWhite;
                break;
            case Otc::FluidWine:
                color = Otc::FluidPurple;
                break;
            case Otc::FluidHealth:
                color = Otc::FluidRed;
                break;
            case Otc::FluidUrine:
                color = Otc::FluidYellow;
                break;
            case Otc::FluidRum:
                color = Otc::FluidBrown;
                break;
            case Otc::FluidFruidJuice:
                color = Otc::FluidYellow;
                break;
            case Otc::FluidCoconutMilk:
                color = Otc::FluidWhite;
                break;
            case Otc::FluidTea:
                color = Otc::FluidBrown;
                break;
            case Otc::FluidMead:
                color = Otc::FluidBrown;
                break;
            default:
                color = Otc::FluidTransparent;
                break;
        }

        xPattern = (color % 4) % getNumPatternX();
        yPattern = (color / 4) % getNumPatternY();
    } else if(isGround() || isOnBottom()) {
        xPattern = m_position.x % getNumPatternX();
        yPattern = m_position.y % getNumPatternY();
        zPattern = m_position.z % getNumPatternZ();
    }
}
コード例 #7
0
ファイル: Squad.cpp プロジェクト: BuffBuff/BTHAI_3.0
TilePosition Squad::getCenter()
{
	if (agents.size() == 1)
	{
		return agents.at(0)->getUnit()->getTilePosition();
	}

	int cX = 0;
	int cY = 0;
	int cnt = 0;

	//Calculate sum (x,y)
	for (int i = 0; i < (int)agents.size(); i++)
	{
		if (agents.at(i)->isAlive())
		{
			TilePosition unitPos = agents.at(i)->getUnit()->getTilePosition();
			if (!unitPos.isValid())
			{
				Broodwar->printf("Encountered invalid position, skipping");
				continue;
			}

			cX += unitPos.x();
			cY += unitPos.y();
			cnt++;
		}
	}

	//Calculate average (x,y)
	if(cnt > 0)
	{
		cX = cX / cnt;
		cY = cY / cnt;
	}

	//To make sure the center is in a walkable tile, we need to
	//find the unit closest to center
	TilePosition c = TilePosition(cX, cY);
	TilePosition bestSpot = c;
	double bestDist = 10000;
	for (int i = 0; i < (int)agents.size(); i++)
	{
		if (agents.at(i)->isAlive())
		{
			TilePosition unitPos = agents.at(i)->getUnit()->getTilePosition();
			if (!unitPos.isValid())
				continue;

			if ( (isAir() && agents.at(i)->getUnitType().isFlyer()) || (isGround() && !agents.at(i)->getUnitType().isFlyer()))
			{
				double dist = unitPos.getDistance(c);
				if (dist < bestDist)
				{
					bestDist = dist;
					bestSpot = unitPos;
				}
			}
		}
	}

	if (!bestSpot.isValid())
	{
		Broodwar->printf("Invalid positions has distrupted the time-space continuum, expect the universe to implode.");
	}

	return bestSpot;
}