<commit_before>#include "Environment.hpp"
#include "Entity.hpp"
#include "Ball.hpp"
#include "Field.hpp"
#include "Robot.hpp"

#include <QDomDocument>
#include <QDomAttr>
#include <QDebug>
#include <QFile>
#include <stdexcept>

#include <protobuf/messages_robocup_ssl_detection.pb.h>
#include <protobuf/messages_robocup_ssl_geometry.pb.h>
#include <protobuf/messages_robocup_ssl_wrapper.pb.h>

#include <iostream>
#include <sys/time.h>
#include <Constants.hpp>
#include <Network.hpp>
#include <boost/foreach.hpp>
#include <Geometry2d/util.h>

using namespace std;
using namespace Geometry2d;
using namespace Packet;

static const QHostAddress LocalAddress(QHostAddress::LocalHost);
static const QHostAddress MulticastAddress(SharedVisionAddress);

const int Oversample = 1;

Environment::Environment(const QString& configFile, bool sendShared_, SimEngine* engine)
:	_dropFrame(false),
 	_configFile(configFile),
 	_frameNumber(0),
 	_stepCount(0),
 	_simEngine(engine),
 	sendShared(sendShared_),
 	ballVisibility(100)
{
	// NOTE: does not start simulation/thread until triggered
	_field = new Field(this);
	_field->initPhysics();
	loadConfigFile(_configFile);
}

Environment::~Environment() {
	delete _field;
}

void Environment::connectSockets() {
	// Bind sockets
	assert(_visionSocket.bind(SimCommandPort));
	assert(_radioSocketYellow.bind(RadioTxPort));
	assert(_radioSocketBlue.bind(RadioTxPort + 1));

	gettimeofday(&_lastStepTime, 0);

	connect(&_timer, SIGNAL(timeout()), SLOT(step()));
	_timer.start(16 / Oversample);
}

void Environment::preStep(float deltaTime){
	BOOST_FOREACH(Robot *robot, _yellow)
	{
		robot->applyEngineForces(deltaTime);
	}

	BOOST_FOREACH(Robot *robot, _blue)
	{
		robot->applyEngineForces(deltaTime);
	}
}

void Environment::step()
{

	// Check for SimCommands
	while (_visionSocket.hasPendingDatagrams())
	{
		SimCommand cmd;
		if (!loadPacket<SimCommand>(_visionSocket, cmd))
			continue;
		handleSimCommand(cmd);
	}<commit_msg>Improved error message for simulator socked bind failure<commit_after>#include "Environment.hpp"
#include "Entity.hpp"
#include "Ball.hpp"
#include "Field.hpp"
#include "Robot.hpp"

#include <QDomDocument>
#include <QDomAttr>
#include <QDebug>
#include <QFile>
#include <stdexcept>

#include <protobuf/messages_robocup_ssl_detection.pb.h>
#include <protobuf/messages_robocup_ssl_geometry.pb.h>
#include <protobuf/messages_robocup_ssl_wrapper.pb.h>

#include <iostream>
#include <sys/time.h>
#include <Constants.hpp>
#include <Network.hpp>
#include <boost/foreach.hpp>
#include <Geometry2d/util.h>

using namespace std;
using namespace Geometry2d;
using namespace Packet;

static const QHostAddress LocalAddress(QHostAddress::LocalHost);
static const QHostAddress MulticastAddress(SharedVisionAddress);

const int Oversample = 1;

Environment::Environment(const QString& configFile, bool sendShared_, SimEngine* engine)
:	_dropFrame(false),
 	_configFile(configFile),
 	_frameNumber(0),
 	_stepCount(0),
 	_simEngine(engine),
 	sendShared(sendShared_),
 	ballVisibility(100)
{
	// NOTE: does not start simulation/thread until triggered
	_field = new Field(this);
	_field->initPhysics();
	loadConfigFile(_configFile);
}

Environment::~Environment() {
	delete _field;
}

void Environment::connectSockets() {
	// Bind sockets
	bool success = (
		_visionSocket.bind(SimCommandPort)
		&& _radioSocketYellow.bind(RadioTxPort)
		&& _radioSocketBlue.bind(RadioTxPort + 1)
	);
	if ( !success ) {
		printf("Unable to bind sockets.  Is there another instance of simulator already running?");
		throw std::exception();
	}

	gettimeofday(&_lastStepTime, 0);

	connect(&_timer, SIGNAL(timeout()), SLOT(step()));
	_timer.start(16 / Oversample);
}

void Environment::preStep(float deltaTime){
	BOOST_FOREACH(Robot *robot, _yellow)
	{
		robot->applyEngineForces(deltaTime);
	}

	BOOST_FOREACH(Robot *robot, _blue)
	{
		robot->applyEngineForces(deltaTime);
	}
}

void Environment::step()
{

	// Check for SimCommands
	while (_visionSocket.hasPendingDatagrams())
	{
		SimCommand cmd;
		if (!loadPacket<SimCommand>(_visionSocket, cmd))
			continue;
		handleSimCommand(cmd);
	}
{'fraction_non_alphanumeric': 0.12812424168891046, 'fraction_numerical': 0.005823829167677748, 'mean_word_length': 6.549450549450549, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 39, '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': '6181029', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1386, 'n_words': 281}