<commit_before>#include "DumbReceive.hpp"
#include "Utils.hpp"
#include <math.h>

namespace Gameplay { REGISTER_CONFIGURABLE(DumbReceive) }

ConfigDouble *Gameplay::DumbReceive::_backoffDistance;
ConfigDouble *Gameplay::DumbReceive::_maxExecutionError;
ConfigDouble *Gameplay::DumbReceive::_kickTimeout;

ConfigBool *Gameplay::DumbReceive::_debug;

void Gameplay::DumbReceive::createConfiguration(Configuration *cfg)
{
	_backoffDistance = new ConfigDouble(cfg, "DumbReceive/Backoff Distance", 0.078674155);
	_maxExecutionError = new ConfigDouble(cfg, "DumbReceive/Max Execution Error", 0.015);
	_kickTimeout = new ConfigDouble(cfg, "DumbReceive/Kick Timeout", 3.0);

	_debug = new ConfigBool(cfg, "StablePivotKick/Debug", true);
}

Gameplay::DumbReceive::DumbReceive(GameplayModule *gameplay)
	: ActionBehavior(gameplay),
	  _state(Setup),
	  _success(false),
	  _kickDelta(0)
{
}

bool Gameplay::DumbReceive::isSettingUp()
{
	return _state == Setup;
}

bool Gameplay::DumbReceive::isSetup()
{
	return isAtReceivePosition() && _state == Setup;
}

bool Gameplay::DumbReceive::isActing()
{
	return _state == Receive_PassKicking || _state == Receive_PassDone;
}

bool Gameplay::DumbReceive::isDone()
{
	return _state == Done;
}

void Gameplay::DumbReceive::restart()
{
	_state = Setup;
	_success = false;
	_kickDelta = 0;
}

Geometry2d::Point Gameplay::DumbReceive::receivePosition()
{
	using namespace Geometry2d;

	Point receiveTarget;  // Point at which to place robot mouth
	Point passVector;     // Direction of pass
	if(_state == Setup) {
		receiveTarget = actionTarget;
		passVector = actionTarget - ball().pos;
	} else if(_state == Receive_PassKicking && partner) {
		OurRobot* passer = partner->robot;
		// The direction passer faces is the passing direction
		passVector = passer->kickerBar().center() - passer->pos;
		Line passLine(ball().pos, ball().pos + passVector);
		// Put mouth on the line
		// Using robot->pos would have caused the receiver to move backwards
		receiveTarget = passLine.nearestPoint(robot->kickerBar().center());
	} else if(_state == Receive_PassDone) {
		Line passLine(ball().pos, ball().pos + ball().vel);
		receiveTarget = passLine.nearestPoint(robot->kickerBar().center());
		passVector = ball().vel;
	} else if(_state == Done) {
		// Return start, if nowhere else
		receiveTarget = actionTarget;
		// passVector is (0,0)
	}

	// Calculate location of robot center
	receiveTarget += passVector **_backoffDistance;

	return receiveTarget;
}

float Gameplay::DumbReceive::receivePositionError()
{
	using namespace Geometry2d;
	float dist = (receivePosition() - robot->pos).mag();
	float distAngle = Line(receivePosition(), ball().pos).distTo(robot->pos);

	return std::max(dist, distAngle);
}

bool Gameplay::DumbReceive::isAtReceivePosition()
{
	return receivePositionError() < *_maxExecutionError;
}

bool Gameplay::DumbReceive::run()
{
	using namespace Geometry2d;

	if(!robot || !robot->visible) {
		return false;
	}

	// State changing
	if(_state == Setup) {
		if(isAtReceivePosition()) {
			if(partner && partner->isActing()) {
				_state = Receive_PassKicking;
			} else if(partner && partner->isDone()) {
				_state = Receive_PassDone;
			} else if(!partner) {
				_state = Receive_PassDone;
			}
		}
	} else if(_state == Receive_PassKicking) {
		if(partner && partner->isSettingUp()) {
			_state = Setup;
		} else if(partner && partner->isDone()) {
			_state = Receive_PassDone;
		} else if(!partner) {
			_state = Receive_PassDone;
		}
	} else if(_state == Receive_PassDone) {
		if(_kickDelta == 0) {
			_kickDelta = timestamp();
		}

		if(robot->hasBall()) {
			_state = Done;
		} else if(timestamp() - _kickDelta > *_kickTimeout * 1000000) {  // Micro-seconds
			_state = Done;
		}
	} else if(_state == Done) {
		// Do nothing
	}

	// Debug visualization
	if(*_debug) {
		if(_state == Setup) {
			robot->addText(QString("DR: Setup"), Qt::yellow, QString("DumbReceive"));
			robot->addText(QString("DR error: %1").arg(receivePositionError()), Qt::yellow, QString("DumbReceive"));
			// Draw circle at pass position
			state()->drawCircle(receivePosition(), Robot_Radius + 0.05, Qt::yellow, QString("DumbReceive"));<commit_msg>DumbReceive doesnt exit now when it doesnt have a partner<commit_after>#include "DumbReceive.hpp"
#include "Utils.hpp"
#include <math.h>

namespace Gameplay { REGISTER_CONFIGURABLE(DumbReceive) }

ConfigDouble *Gameplay::DumbReceive::_backoffDistance;
ConfigDouble *Gameplay::DumbReceive::_maxExecutionError;
ConfigDouble *Gameplay::DumbReceive::_kickTimeout;

ConfigBool *Gameplay::DumbReceive::_debug;

void Gameplay::DumbReceive::createConfiguration(Configuration *cfg)
{
	_backoffDistance = new ConfigDouble(cfg, "DumbReceive/Backoff Distance", 0.078674155);
	_maxExecutionError = new ConfigDouble(cfg, "DumbReceive/Max Execution Error", 0.015);
	_kickTimeout = new ConfigDouble(cfg, "DumbReceive/Kick Timeout", 3.0);

	_debug = new ConfigBool(cfg, "StablePivotKick/Debug", true);
}

Gameplay::DumbReceive::DumbReceive(GameplayModule *gameplay)
	: ActionBehavior(gameplay),
	  _state(Setup),
	  _success(false),
	  _kickDelta(0)
{
}

bool Gameplay::DumbReceive::isSettingUp()
{
	return _state == Setup;
}

bool Gameplay::DumbReceive::isSetup()
{
	return isAtReceivePosition() && _state == Setup;
}

bool Gameplay::DumbReceive::isActing()
{
	return _state == Receive_PassKicking || _state == Receive_PassDone;
}

bool Gameplay::DumbReceive::isDone()
{
	return _state == Done;
}

void Gameplay::DumbReceive::restart()
{
	_state = Setup;
	_success = false;
	_kickDelta = 0;
}

Geometry2d::Point Gameplay::DumbReceive::receivePosition()
{
	using namespace Geometry2d;

	Point receiveTarget;  // Point at which to place robot mouth
	Point passVector;     // Direction of pass
	if(_state == Setup) {
		receiveTarget = actionTarget;
		passVector = actionTarget - ball().pos;
	} else if(_state == Receive_PassKicking && partner) {
		OurRobot* passer = partner->robot;
		// The direction passer faces is the passing direction
		passVector = passer->kickerBar().center() - passer->pos;
		Line passLine(ball().pos, ball().pos + passVector);
		// Put mouth on the line
		// Using robot->pos would have caused the receiver to move backwards
		receiveTarget = passLine.nearestPoint(robot->kickerBar().center());
	} else if(_state == Receive_PassDone) {
		Line passLine(ball().pos, ball().pos + ball().vel);
		receiveTarget = passLine.nearestPoint(robot->kickerBar().center());
		passVector = ball().vel;
	} else if(_state == Done) {
		// Return start, if nowhere else
		receiveTarget = actionTarget;
		// passVector is (0,0)
	}

	// Calculate location of robot center
	receiveTarget += passVector **_backoffDistance;

	return receiveTarget;
}

float Gameplay::DumbReceive::receivePositionError()
{
	using namespace Geometry2d;
	float dist = (receivePosition() - robot->pos).mag();
	float distAngle = Line(receivePosition(), ball().pos).distTo(robot->pos);

	return std::max(dist, distAngle);
}

bool Gameplay::DumbReceive::isAtReceivePosition()
{
	return receivePositionError() < *_maxExecutionError;
}

bool Gameplay::DumbReceive::run()
{
	using namespace Geometry2d;

	if(!robot || !robot->visible) {
		return false;
	}

	// State changing
	if(_state == Setup) {
		if(isAtReceivePosition()) {
			if(partner && partner->isActing()) {
				_state = Receive_PassKicking;
			} else if(partner && partner->isDone()) {
				_state = Receive_PassDone;
			}
			// else if(!partner) {
			// 	_state = Receive_PassDone;
			// }
		}
	} else if(_state == Receive_PassKicking) {
		if(partner && partner->isSettingUp()) {
			_state = Setup;
		} else if(partner && partner->isDone()) {
			_state = Receive_PassDone;
		} else if(!partner) {
			_state = Receive_PassDone;
		}
	} else if(_state == Receive_PassDone) {
		if(_kickDelta == 0) {
			_kickDelta = timestamp();
		}

		if(robot->hasBall()) {
			_state = Done;
		} else if(timestamp() - _kickDelta > *_kickTimeout * 1000000) {  // Micro-seconds
			_state = Done;
		}
	} else if(_state == Done) {
		// Do nothing
	}

	// Debug visualization
	if(*_debug) {
		if(_state == Setup) {
			robot->addText(QString("DR: Setup"), Qt::yellow, QString("DumbReceive"));
			robot->addText(QString("DR error: %1").arg(receivePositionError()), Qt::yellow, QString("DumbReceive"));
			// Draw circle at pass position
			state()->drawCircle(receivePosition(), Robot_Radius + 0.05, Qt::yellow, QString("DumbReceive"));
{'fraction_non_alphanumeric': 0.145, 'fraction_numerical': 0.008571428571428572, 'mean_word_length': 4.983618233618234, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '117429', 'n_tokens_mistral': 3071, 'n_tokens_neox': 2740, 'n_words': 590}