void AgentRocket_OneEngine_Receptor::live()
{
	if (this->castedProblem->getUserControlled() || !this->problem->getProblemLive())
		return;

	// Getting perception from body
	PerceptionWave::WAVE perception = this->castedBody->getPerception();

	// Checking error in parameters
	if (perception.amplitude < this->castedProblem->getWaveAmplitudeOffset())
	{
		perception.amplitude = this->castedProblem->getWaveAmplitudeOffset();
		//cout << "ERROR : AgentReceptorRocket::Live : amplitude was sub offset" << endl;
	}
	else if (perception.amplitude > (this->castedProblem->getWaveAmplitudeOffset() + this->castedProblem->getWaveAmplitudeRange()))
	{
		perception.amplitude = this->castedProblem->getWaveAmplitudeOffset() + this->castedProblem->getWaveAmplitudeRange();
		//cout << "ERROR : AgentReceptorRocket::Live : amplitude was bigger than range + offset" << endl;
	}

	if (perception.frequency < this->castedProblem->getWaveFrequencyOffset())
	{
		perception.frequency = this->castedProblem->getWaveFrequencyOffset();
		//cout << "ERROR : AgentReceptorRocket::Live : frequency was sub offset" << endl;
	}
	else if (perception.frequency > (this->castedProblem->getWaveFrequencyOffset() + this->castedProblem->getWaveFrequencyRange()))
	{
		perception.frequency = this->castedProblem->getWaveFrequencyOffset() + this->castedProblem->getWaveFrequencyRange();
		//cout << "ERROR : AgentReceptorRocket::Live : frequency was bigger than range + offset" << endl;
	}

	// Converting into angle and power values
	//cout << "RECEIVED : " << perception.frequency << ", " << perception.amplitude << endl;
	float angle = convertToRange(perception.amplitude,
		this->castedProblem->getWaveAmplitudeOffset(),
		this->castedProblem->getWaveAmplitudeRange(),
		0,
		PROBLEMROCKET_ROCKET_ANGLE_TILT * 2);
	angle -= PROBLEMROCKET_ROCKET_ANGLE_TILT;

	// Getting frequency back
	//perception.frequency -= this->castedProblem->getWaveFrequencyOffset();
	float power = convertToRange(perception.frequency,
		this->castedProblem->getWaveFrequencyOffset(),
		this->castedProblem->getWaveFrequencyRange(),
		0,
		this->castedProblem->getPowerMax()*2);
	power -= this->castedProblem->getPowerMax();

	//cout << "TRANSLATED : " << power << ", " << angle << endl << endl;
	
	this->castedProblem->setDesiredAngle(angle);
	this->castedProblem->setDesiredPower(0, power);
}
示例#2
0
void Link::update()
{
	float v = convertToRange(from->getBaseValue(), from->min, from->max, to->min, to->max*amount);
	to->setValue(v);
}
void AgentRocket_OneEngine_Emitter::live()
{
	if (this->castedProblem->getUserControlled() || !this->problem->getProblemLive())
	{
		if (this->castedBody->isSending())
			this->castedBody->stopSending();
		return;
	}

	// Getting problem data
	float x, y, power, angle, hSpeed, vSpeed, distanceToGround, distanceToCenterFlat, lzSize, worldWidth, worldHeight, closestTerrainX, closestTerrainY, closestTerrainDistance;

	this->castedProblem->getTerrain()->getTerrainDimensions(worldWidth, worldHeight);
	this->castedProblem->getRocketPosition(x, y);
	power = this->castedProblem->getRocketEnginesPower()->at(0);
	angle = this->castedProblem->getRocketAngle();
	angle -= PROBLEMROCKET_GUI_ANGLE_OFFSET;	// Dont forget that
	this->castedProblem->getRocketSpeed(hSpeed, vSpeed);
	distanceToGround = this->castedProblem->getRocketDistanceToGround();
	distanceToCenterFlat = this->castedProblem->getRocketDistanceToLandingZoneCenter();
	lzSize = this->castedProblem->getLandingZoneSize();

	closestTerrainX = 0;
	closestTerrainY = 0;
	closestTerrainDistance = 10000;
	this->castedProblem->getTerrain()->getClosestPointFromRocket(x, y, closestTerrainX, closestTerrainY, closestTerrainDistance);


	// Variables
	float desiredAngle = 0;
	float desiredPower = 0;
	float temp = 0;

	float amplitude;
	float frequency;
	bool ceaseTransmission = false;
	
	if ((AGENTTYPE_ROCKET_ONE)this->getType() == AGENTTYPE_ROCKET_ONE::ROCKET_ONE_DIRECTION)
	{
		desiredPower = PROBLEMROCKET_ROCKET_POWER_BASE + this->castedProblem->getPowerOffset();

		if (abs(distanceToCenterFlat) > (lzSize / 2))
		{
			// Direct the rocket towards the flatzone
			desiredAngle = convertToRange(abs(distanceToCenterFlat),
				lzSize/2,												
				worldWidth,
				PROBLEMROCKET_CRUISE_MAXANGLE * PROBLEMROCKET_DIRECTION_LZAPPROACH,
				PROBLEMROCKET_CRUISE_MAXANGLE);

			if (distanceToCenterFlat > 0)
				desiredAngle *= -1;
		}
		else
		{
			desiredAngle = convertToRange(abs(distanceToCenterFlat),
				0.0f,
				lzSize / 2,
				0.0f,
				PROBLEMROCKET_CRUISE_MAXANGLE * PROBLEMROCKET_DIRECTION_LZAPPROACH);
				
				if (distanceToCenterFlat > 0)
					desiredAngle *= -1;
		}
	}
	else if ((AGENTTYPE_ROCKET_ONE)this->getType() == AGENTTYPE_ROCKET_ONE::ROCKET_ONE_AVOIDER)
	{
		desiredPower = PROBLEMROCKET_ROCKET_POWER_BASE + this->castedProblem->getPowerOffset();
		if (abs(closestTerrainDistance) < 100.0f)
		{
			Vector direction = Vector(x - closestTerrainX, y - closestTerrainY);
 			desiredAngle = direction.getAngle() - 90;
			if (desiredAngle < -45)
				desiredAngle = -45;
			if (desiredAngle > 45)
				desiredAngle = 45;
		}
		else
			ceaseTransmission = true;
	}
	else if ((AGENTTYPE_ROCKET_ONE)this->getType() == AGENTTYPE_ROCKET_ONE::ROCKET_ONE_STABILIZER_ANGLE)
	{
		desiredPower = PROBLEMROCKET_ROCKET_POWER_BASE + this->castedProblem->getPowerOffset();
		desiredAngle = 0;
	}
	else if ((AGENTTYPE_ROCKET_ONE)this->getType() == AGENTTYPE_ROCKET_ONE::ROCKET_ONE_ALTITUDE)
	{
		// ALTITUDE STABILIZER
		if (abs(distanceToCenterFlat) < lzSize /2)
		{
			// We're in the landing zone
			
			desiredAngle = 0;

			desiredPower = convertToRange(abs(vSpeed),
				0,
				PROBLEMROCKET_LANDING_MAXVSPEED,
				0,
				this->castedProblem->getPowerMax()/2 + this->castedProblem->getPowerOffset());
		}
		else if (y < this->castedProblem->getDesiredAltitude())
		{
			// We're lower than the desired altitude : go up
			desiredAngle = 0;

			desiredPower = convertToRange(abs(y - this->castedProblem->getDesiredAltitude()),
				0,
				100,
				this->castedProblem->getPowerMax() - PROBLEMROCKET_ROCKET_POWER_BASE + this->castedProblem->getPowerOffset(),
				this->castedProblem->getPowerMax());

			desiredPower += convertToRange(abs(angle),
				0,
				PROBLEMROCKET_ROCKET_ANGLE_TILT,
				0,
				PROBLEMROCKET_ROCKET_POWER_BASE);

			if (vSpeed > 0.0f)
				desiredPower -= convertToRange(abs(vSpeed),
					0,
					this->castedProblem->getMaxVSpeed(),
					0,
					PROBLEMROCKET_ROCKET_POWER_BASE);
		}
		else
		{
			ceaseTransmission = true;
		}
	}
	else if ((AGENTTYPE_ROCKET_ONE)this->getType() == AGENTTYPE_ROCKET_ONE::ROCKET_ONE_STABILIZER_HSPEED)
	{
		if (abs(distanceToCenterFlat) < lzSize / 2)
		{
			desiredAngle = convertToRange(abs(hSpeed),
				0,
				PROBLEMROCKET_LANDING_MAXHSPEED,
				0,
				PROBLEMROCKET_CRUISE_MAXANGLE);
		}
		else
		{
			desiredAngle = convertToRange(abs(hSpeed),
				0,
				PROBLEMROCKET_CRUISE_MAXHSPEED,
				0,
				PROBLEMROCKET_CRUISE_MAXANGLE);
		}
		

		if (hSpeed < 0)
			desiredAngle *= -1;

		desiredPower = PROBLEMROCKET_ROCKET_POWER_BASE + this->castedProblem->getPowerOffset();
	}
	else if ((AGENTTYPE_ROCKET_ONE)this->getType() == AGENTTYPE_ROCKET_ONE::ROCKET_ONE_STABILIZER_VSPEED)
	{
		desiredAngle = 0;

		if (abs(distanceToCenterFlat) < lzSize / 2)
		{
			desiredPower = convertToRange(abs(vSpeed),
				0,
				PROBLEMROCKET_LANDING_MAXVSPEED,
				PROBLEMROCKET_ROCKET_POWER_BASE + this->castedProblem->getPowerOffset(),
				this->castedProblem->getPowerMax());

			desiredPower += convertToRange(abs(angle),
				0,
				PROBLEMROCKET_ROCKET_ANGLE_TILT ,
				0,
				this->castedProblem->getPowerMax() / 2);
		}
		else
		{
			desiredPower = convertToRange(abs(vSpeed),
				0,
				PROBLEMROCKET_CRUISE_MAXVSPEED,
				PROBLEMROCKET_ROCKET_POWER_BASE + this->castedProblem->getPowerOffset(),
				this->castedProblem->getPowerMax());

			desiredPower += convertToRange(abs(angle),
				0,
				PROBLEMROCKET_ROCKET_ANGLE_TILT,
				0,
				this->castedProblem->getPowerMax() / 2);
		}
		
		

		if (vSpeed > 0.0f)
		{
			if (y < this->castedProblem->getDesiredAltitude())
				desiredPower = this->castedProblem->getPowerMax() - desiredPower - this->castedProblem->getPowerOffset();
			else
				desiredPower = 30.0f;
		}
	}

	if (ceaseTransmission)
	{
		this->castedBody->stopSending();
		return;
	}
	else
	{
		// Sending
		// If we're using relative change, convert the desired angle to a relative angle
		if (this->castedProblem->getUseRelativeChange())
		{
			desiredAngle -= angle;
			desiredPower -= power;
		}
		
		//cout << "DESIRED : " << desiredPower << ", " << desiredAngle << endl;
		desiredAngle += PROBLEMROCKET_ROCKET_ANGLE_TILT;

		amplitude = convertToRange(desiredAngle,	
			0,										
			PROBLEMROCKET_ROCKET_ANGLE_TILT * 2,
			this->castedProblem->getWaveAmplitudeOffset(),
			this->castedProblem->getWaveAmplitudeRange());

		desiredPower += this->castedProblem->getPowerMax();
		frequency = convertToRange(desiredPower,
			0,
			this->castedProblem->getPowerMax() * 2,
			this->castedProblem->getWaveFrequencyOffset(),
			this->castedProblem->getWaveFrequencyRange());

		//cout << "SENDING : " << frequency << "," << amplitude << endl;
		this->castedBody->send(frequency, amplitude);
	}
}
void AgentRocket_TwoEngines_Brain::live()
{
	if (this->castedProblem->getUserControlled() || !this->problem->getProblemLive())
		return;

	map<int, int>& agentCount = this->castedProblem->getAgentCount();

	this->clearDesiredState();

	// Getting problem data
	float x, y, power, angle, hSpeed, vSpeed, distanceToGround, distanceToCenterFlat, lzSize, closestTerrainX, closestTerrainY, closestTerrainDistance;

	this->castedProblem->getRocketPosition(x, y);
	power = this->castedProblem->getRocketEnginesPower()->at(0);
	angle = this->castedProblem->getRocketAngle();
	angle -= PROBLEMROCKET_GUI_ANGLE_OFFSET;	// Dont forget that
	this->castedProblem->getRocketSpeed(hSpeed, vSpeed);
	distanceToGround = this->castedProblem->getRocketDistanceToGround();
	distanceToCenterFlat = this->castedProblem->getRocketDistanceToLandingZoneCenter();
	lzSize = this->castedProblem->getLandingZoneSize();
	float landingMaxHSPeed, landingMaxVSpeed, landingMaxAngle; // Correct landing specs
	this->castedProblem->getSafeLandingSpecs(landingMaxHSPeed, landingMaxVSpeed, landingMaxAngle);

	float highestPointX, highestPointY;
	this->castedProblem->getTerrain()->getHighestPointBeforeLandingZone(x, y, highestPointX, highestPointY);
	this->castedProblem->setDesiredAltitude(highestPointY + PROBLEMROCKET_ALTITUDE_OFFSET);
	this->castedProblem->getTerrain()->getClosestPointFromRocket(x, y, closestTerrainX, closestTerrainY, closestTerrainDistance);


	// Decision making
	if (x > this->castedProblem->getTerrain()->getWidth() - 100.0f
		|| x < 100.0f)
	{
		// We're too close to the world's edge : stabilize, and get away
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_DIRECTION) = 1;
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_AVOIDER) = 1;
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_ANGLE) = 1;
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_VSPEED) = 1;
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_HSPEED) = 1;
	}
	else if (abs(distanceToCenterFlat) < (lzSize / 2))
	{
		// Wer are in the landing zone : begin descent

		// Harder horizontal stabilization
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_HSPEED) = convertToRange(abs(hSpeed),
			0,
			this->castedProblem->getMaxHSpeed() / 2,
			1,
			4);

		// Softer vertical stabilization
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_VSPEED) = convertToRange(abs(vSpeed),
			0,
			this->castedProblem->getMaxVSpeed(),
			1,
			3);

		// Harder angle stabilization
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_ANGLE) = convertToRange(abs(angle),
			0,
			this->castedProblem->getMaxAngle() / 2,
			1,
			4);

		if (abs(closestTerrainDistance) < 25.0f)
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_AVOIDER) = 2;
		else
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_AVOIDER) = 1;

		// Emergency landing : landing values are incorrect and we are too close from the ground
		if (!this->castedProblem->checkCorrectLanding(x, y, hSpeed * 2, vSpeed * 2, angle * 2) && distanceToGround < 100.0f)
		{
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_ANGLE) = 5;
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_HSPEED) = 1;
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_VSPEED) = 1;
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_ALTITUDE) = 1;
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_DIRECTION) = 0;
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_AVOIDER) = 0;
		}
		// If landing values are correct, start the descent
		else if (this->castedProblem->checkCorrectLanding(x, y, hSpeed*2, vSpeed*2, angle*2))
		{
			// Descent
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_ALTITUDE) = convertToRange(abs(distanceToGround),
				0,
				500,
				1,
				4);

			// Direction
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_DIRECTION) = convertToRange(abs(distanceToCenterFlat),
				0,
				lzSize/2,
				0,
				3);
		}
		else
		{
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_DIRECTION) = 0;
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_ALTITUDE) = 0;
		}
	}
	else
	{
		// We're not above the landing zone
		// We need to stabilize in vertical and horizontal speed first

		// Stabilizing hspeed
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_HSPEED) = convertToRange(abs(hSpeed),
			0,
			this->castedProblem->getMaxHSpeed(),
			1,
			3);

		// Stabilizing vspeed
		if (vSpeed < 0.0f)
		{
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_VSPEED) = convertToRange(abs(vSpeed),
				0,
				this->castedProblem->getMaxVSpeed(),
				1,
				3);
		}
		else
		{
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_VSPEED) = convertToRange(abs(vSpeed),
				0,
				this->castedProblem->getMaxVSpeed()/2,
				1,
				3);
		}

		if (abs(closestTerrainDistance) < 25.0f)
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_AVOIDER) = 2;
		else
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_AVOIDER) = 1;

		// prioritize horizontal and vertical stabilization
		if (hSpeed > landingMaxHSPeed
			|| vSpeed > landingMaxVSpeed)
			return;

		// then, stabilize angle
		this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_ANGLE) = convertToRange(abs(angle),
			0,
			PROBLEMROCKET_CRUISE_MAXANGLE,
			1,
			4);

		if (y > highestPointY + PROBLEMROCKET_ALTITUDE_OFFSET)
		{
			//// Direction : stabilize before caring about direction
			//if (this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_VSPEED) > 2
			//	|| this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_HSPEED) > 2
			//	|| this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_STABILIZER_ANGLE) > 2)
			//	this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_DIRECTION) = 0;
			//else
				this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_DIRECTION) = 1;

			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_ALTITUDE) = 0;
		}
		else
		{
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_ALTITUDE) = 1;
			this->desiredState.at((int)AGENTTYPE_ROCKET_TWO::ROCKET_TWO_DIRECTION) = 1;
		}
	}
}