Beispiel #1
0
Rectangle::Rectangle(Coordinates v1, Coordinates v2) {
	my_vertex[0] = v1;
	my_vertex[1] = Coordinates(v1.getAbs() , v2.getOrd());
	my_vertex[2] = Coordinates(v2.getAbs() , v1.getOrd());
	my_vertex[3] = v2;

	my_length = v1.distance(my_vertex[1]);	//determining edge length
	my_width = v2.distance(my_vertex[2]);	//determining edge length

	if(my_length<my_width ){
		float temp;
		temp=my_length;
		my_length= my_width;
		my_width = temp;
	}
	pos = (v2+v1)/2;
}
Beispiel #2
0
int AttackNearestEnemy::validateTactic(list<Action*> &newActions, Unit* squad, const vector<Unit*>& enemyUnits,
        const vector<Unit*>& alliedUnits)
{
	if (enemyUnits.size() == 0 || squad->getNShipsAlive() == 0)
		return 0;

	squad->setTarget(-1);

	int Ret = 0;
	Coordinates myPos = squad->getAveragePos();
	float minDist = 99999;

	Unit *nearestUnit = NULL;
	for (unsigned int i = 0; i < enemyUnits.size(); ++i)
	{
		Coordinates enemyAvrg = enemyUnits[i]->getAveragePos();
		if (enemyUnits[i]->getNShipsAlive() > 0)
		{
			float dist = myPos.distance(enemyAvrg);
			if (dist < minDist)
			{
				minDist = dist;
				nearestUnit = enemyUnits[i];
				squad->setTarget(i);
			}
		}
	}

	if (nearestUnit && minDist < squad->getSquadBaseStats().range)
	{
		for (unsigned int i = 0; i < squad->nShips(); ++i)
		{
			Ship *iShip = squad->getShip(i);

			if (iShip->isAlive() && iShip->getStats().currentAtkCD == 0)
			{
				while (1)
				{
					int s = rand() % nearestUnit->nShips();
					if (nearestUnit->getShip(s)->isAlive())
					{
						iShip->getStats().currentAtkCD = iShip->getBaseStats().maxAtkCD;
						newActions.push_back(new AttackAction(iShip, nearestUnit->getShip(s), squad->getUnitInfo(), nearestUnit->getUnitInfo()));
						++Ret;
						break;
					}
				}
			}
		}
	}

    // TODO: Se eu nao conseguir atacar, devo desconsiderar o target?
//	if (Ret == 0)
//        squad->setTarget(-1);

	return Ret;
}
Beispiel #3
0
int Kamikase::validateTactic(list<Action*> &newActions, Unit* squad, const vector<Unit*>& enemyUnits, const vector<Unit*>& alliedUnits)
{
	if (enemyUnits.size() == 0)
		return 0;

	int Ret = 0;
	float minDist = 99999;

	Coordinates myPos = squad->getAveragePos();
	if (squad->getNShipsAlive() == 0)
		return 0;

	Unit *nearestUnit = NULL;
	for (unsigned int i = 0; i < enemyUnits.size(); ++i)
	{
		Coordinates enemyAvrg = enemyUnits[i]->getAveragePos();
		if (enemyUnits[i]->getNShipsAlive() > 0)
		{
			float dist = myPos.distance(enemyAvrg);
			if (dist < minDist)
			{
				minDist = dist;
				nearestUnit = enemyUnits[i];
				squad->setTarget(i);
			}
		}
	}

	if (nearestUnit && minDist < squad->getSquadBaseStats().range)
	{
		for (unsigned int i = 0; i < squad->nShips(); ++i)
		{
			Ship *iShip = squad->getShip(i);

			if (iShip->isAlive() && iShip->getStats().curKamikazeCD == 0)
			{
				while (1)
				{
					int s = rand() % nearestUnit->nShips();
					if (nearestUnit->getShip(s)->isAlive())
					{
						iShip->getStats().curKamikazeCD = FRAMES_PER_SECOND * 3;
						iShip->getStats().currentAtkCD = iShip->getBaseStats().maxAtkCD * 3;
						newActions.push_back(new KamikazeAction(iShip, nearestUnit->getShip(s), NULL));
						++Ret;
						break;
					}
				}
			}
		}
	}

	return Ret;
}
Beispiel #4
0
MoveAction::MoveAction(Ship *Source, Ship* Target)
    : source(Source), target(Target)
{
    Coordinates tc = target->getPosition();
    float dist = tc.distance(source->getPosition());

    if (dist > source->getBaseStats().range)
    {
        float direction = atan2(source->getY() - target->getY(), source->getX() - target->getX());
        dist -= (source->getBaseStats().range-30);
        coord.x = source->getX() - dist * cos(direction);
        coord.y = source->getY() - dist * sin(direction);
    }
    else
    {
        coord = source->getPosition();
    }

//    printf("Move To: %lf, %lf\n", coord.x, coord.y);
}
Beispiel #5
0
int DefenseCollab::validateTactic(list<Action*> &newActions, Unit* squad, const vector<Unit*>& enemyUnits, const vector<Unit*>& alliedUnits)
{
	int Ret = 0;
	Unit *enemyUnit = NULL;

	for (unsigned int j = 0; j < enemyUnits.size(); j++)
	{
		//interrompe caso tenha encontrado unimigo a quem atacar
		if (enemyUnits[j]->getTarget() == info.allyUnitID)
		{
			enemyUnit = enemyUnits[j];
			squad->setTarget(j);
			break;
		}
	}

	if (enemyUnit == NULL || enemyUnit->getNShipsAlive() == 0)
		return 0;

	for (unsigned int i = 0; i < squad->nShips(); i++)
	{
		Ship *iShip = squad->getShip(i);
		if (iShip->isAlive() && iShip->getStats().currentAtkCD == 0)
		{
			Coordinates shipPos = iShip->getPosition();
			Ship *nearestShip = NULL;
			float dist_ = 99999;

			for (unsigned int u = 0; u < enemyUnit->nShips(); ++u)
			{
				Ship *eShip = enemyUnit->getShip(u);

				if (eShip->isAlive())
				{
					float d = shipPos.distance(enemyUnit->getShip(u)->getPosition());
					if (d < dist_)
					{
						nearestShip = eShip;
						dist_ = d;
					}
				}
			}

            if (nearestShip)
            {
                if (dist_ < iShip->getBaseStats().range)
                {
                    iShip->getStats().currentAtkCD = iShip->getBaseStats().maxAtkCD;
                    newActions.push_back(new AttackAction(iShip, nearestShip, squad->getUnitInfo(), enemyUnit->getUnitInfo()));
                    ++Ret;
                }
                else
                {
                    float direction = atan2(iShip->getY() - nearestShip->getPosition().y, iShip->getX() - nearestShip->getPosition().x);
                    iShip->move(-iShip->getBaseStats().speed * cos(direction), -iShip->getBaseStats().speed * sin(direction));
                }
            }

		}

	}
	return Ret;
}
Beispiel #6
0
int AttackWeakestEnemy::validateTactic(list<Action*> &newActions, Unit* squad, const vector<Unit*>& enemyUnits,
        const vector<Unit*>& alliedUnits)
{
	squad->setTarget(-1);
	if (enemyUnits.size() == 0)
	{
		return 0;
	}

	int Ret = 0;
	float minHP = 100;
	Coordinates enemyAvrg;
	int sumHP;

	Coordinates myPos = squad->getAveragePos();
	if (squad->getNShipsAlive() == 0)
		return 0;

	Unit *wekeastUnit = NULL;

	vector<Unit*> enemyNear;
	//cria uma lista com a unidades dentro do alcançe
	for (unsigned int i = 0; i < enemyUnits.size(); i++)
	{
		Coordinates enemyAvrg = enemyUnits[i]->getAveragePos();
		if (enemyUnits[i]->getNShipsAlive() > 0)
		{
			float dist = myPos.distance(enemyAvrg);
			if (dist < squad->getSquadBaseStats().range)
			{
				enemyNear.push_back(enemyUnits[i]);
			}
		}
	}

	//percorre a lista de unidades inimigas no alçance um busca da mais fraca
	for (unsigned int i = 0; i < enemyNear.size(); i++)
	{
        enemyAvrg = enemyNear[i]->getAveragePos();
		if (enemyNear[i]->getNShipsAlive() > 0)
		{
			sumHP = 0;
			for (unsigned int j = 0; j < enemyNear[i]->nShips(); ++j)
			{
				sumHP += enemyNear[i]->getShip(j)->getHP();
			}

			int maxHP = enemyNear[i]->getUnitInfo()->squadSize * enemyNear[i]->getUnitInfo()->stats.maxHP;

			// Considerar apenas o inteiro
			int percent = (sumHP / maxHP) * 100;
			if (percent < minHP)
			{
				minHP = percent;
				wekeastUnit = enemyNear[i];
				squad->setTarget( i );
			}
		}

	}

	//ataca as naves do esquadrao de maneira aleatoria
	if (wekeastUnit)
	{
		for (unsigned int i = 0; i < squad->nShips(); ++i)
		{
			Ship *iShip = squad->getShip(i);

			if (iShip->isAlive() && iShip->getStats().currentAtkCD == 0)
			{
				while (1)
				{
					int s = rand() % wekeastUnit->nShips();
					if (wekeastUnit->getShip(s)->isAlive())
					{
						iShip->getStats().currentAtkCD = iShip->getBaseStats().maxAtkCD;
						newActions.push_back(new AttackAction(iShip, wekeastUnit->getShip(s), squad->getUnitInfo(), wekeastUnit->getUnitInfo()));
						++Ret;
						break;
					}
				}
			}
		}
	}

    // TODO: Se eu nao conseguir atacar, devo desconsiderar o target?
//	if (Ret == 0)
//        squad->setTarget(-1);

	return Ret;
}