Commit 646da987 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

use radians everywhere

parent 81039e49
......@@ -11,7 +11,6 @@ Bot::Bot(Field *field, uint32_t startFrame, std::unique_ptr<db::BotScript> dbDat
, m_dbData(std::move(dbData))
{
m_snake = std::make_shared<Snake>(field, startPos, 5, startHeading);
m_heading = rand() * 360.0f / RAND_MAX;
m_lua_bot = std::make_unique<LuaBot>(*this, m_dbData->code);
}
......@@ -31,18 +30,13 @@ bool Bot::init(std::string& initErrorMessage)
std::size_t Bot::move(void)
{
bool boost;
float new_heading;
if (m_lua_bot->step(new_heading, boost))
real_t directionChange;
if (!m_lua_bot->step(directionChange, boost))
{
new_heading = fmod(new_heading, 360);
if (new_heading<0)
{
new_heading += 360;
}
m_heading = new_heading;
boost = false;
directionChange = 0;
}
return m_snake->move(m_heading, boost); // direction in degrees
return m_snake->move(directionChange, boost);
}
std::shared_ptr<Bot> Bot::checkCollision(void) const
......
......@@ -25,7 +25,6 @@ class Bot : public IdentifyableObject
std::unique_ptr<db::BotScript> m_dbData;
std::shared_ptr<Snake> m_snake;
std::unique_ptr<LuaBot> m_lua_bot;
real_t m_heading;
std::vector<std::string> m_logMessages;
real_t m_logCredit = config::LOG_INITIAL_CREDITS;
......@@ -71,7 +70,7 @@ class Bot : public IdentifyableObject
std::shared_ptr<Snake> getSnake(void) const { return m_snake; }
const std::string &getName(void) const { return m_dbData->bot_name; }
real_t getHeading() { return m_heading; }
real_t getHeading() { return m_snake->getHeading(); }
Field* getField() { return m_field; }
int getDatabaseId() { return m_dbData->bot_id; }
int getDatabaseVersionId() { return m_dbData->version_id; }
......
......@@ -39,11 +39,10 @@ void Field::setupRandomness(void)
m_positionXDistribution =
std::make_unique< std::uniform_real_distribution<real_t> >(0, m_width);
m_positionYDistribution =
std::make_unique< std::uniform_real_distribution<real_t> >(0, m_height);
m_angleDegreesDistribution =
std::make_unique< std::uniform_real_distribution<real_t> >(-180, 180);
m_angleRadDistribution =
std::make_unique< std::uniform_real_distribution<real_t> >(-M_PI, M_PI);
......@@ -80,7 +79,7 @@ std::shared_ptr<Bot> Field::newBot(std::unique_ptr<db::BotScript> data, std::str
{
real_t x = (*m_positionXDistribution)(*m_rndGen);
real_t y = (*m_positionYDistribution)(*m_rndGen);
real_t heading = (*m_angleDegreesDistribution)(*m_rndGen);
real_t heading = (*m_angleRadDistribution)(*m_rndGen);
std::shared_ptr<Bot> bot = std::make_shared<Bot>(
this,
......
......@@ -53,7 +53,6 @@ class Field
std::unique_ptr< std::normal_distribution<real_t> > m_foodSizeDistribution;
std::unique_ptr< std::uniform_real_distribution<real_t> > m_positionXDistribution;
std::unique_ptr< std::uniform_real_distribution<real_t> > m_positionYDistribution;
std::unique_ptr< std::uniform_real_distribution<real_t> > m_angleDegreesDistribution;
std::unique_ptr< std::uniform_real_distribution<real_t> > m_angleRadDistribution;
std::unique_ptr< std::uniform_real_distribution<real_t> > m_simple0To1Distribution;
......
......@@ -59,8 +59,7 @@ void Snake::ensureSizeMatchesMass(void)
real_t Snake::maxRotationPerStep(void)
{
// TODO: make this better?
return 10.0 / (m_segmentRadius/3.0 + 1);
return (10.0*M_PI/180.0) / (m_segmentRadius/3.0 + 1);
}
void Snake::consume(const Food& food)
......@@ -68,25 +67,11 @@ void Snake::consume(const Food& food)
m_mass += food.getValue();
}
std::size_t Snake::move(real_t targetAngle, bool boost)
std::size_t Snake::move(real_t deltaAngle, bool boost)
{
// calculate delta angle
real_t deltaAngle = targetAngle - m_heading;
// normalize delta angle
if(deltaAngle > 180) {
deltaAngle -= 360;
} else if(deltaAngle < -180) {
deltaAngle += 360;
}
// limit rotation rate
real_t maxDelta = maxRotationPerStep();
if(deltaAngle > maxDelta) {
deltaAngle = maxDelta;
} else if(deltaAngle < -maxDelta) {
deltaAngle = -maxDelta;
}
deltaAngle = std::min(deltaAngle, maxDelta);
deltaAngle = std::max(deltaAngle, -maxDelta);
std::size_t oldSize = m_segments.size();
......@@ -113,8 +98,7 @@ std::size_t Snake::move(real_t targetAngle, bool boost)
// calculate new segment offset
m_heading += deltaAngle;
real_t headingRad = m_heading * M_PI / 180;
Vector2D movementVector2D(cos(headingRad), sin(headingRad));
Vector2D movementVector2D(cos(m_heading), sin(m_heading));
movementVector2D *= config::SNAKE_DISTANCE_PER_STEP;
headSegment.setPos(headSegment.pos() + movementVector2D);
......@@ -140,10 +124,10 @@ std::size_t Snake::move(real_t targetAngle, bool boost)
m_segments.push_front(headSegment);
// normalize heading
if(m_heading > 180) {
m_heading -= 360;
} else if(m_heading < -180) {
m_heading += 360;
if(m_heading > M_PI) {
m_heading -= 2*M_PI;
} else if(m_heading < -M_PI) {
m_heading += 2*M_PI;
}
// force size to previous size (removes end segments)
......
......@@ -42,7 +42,7 @@ class Snake
real_t m_mass; //!< Mass (length) of the snake
real_t m_heading; //!< Heading of the snake in degrees (-180°..180°) from x axis
real_t m_heading; //!< Heading of the snake in radians from x axis
real_t m_segmentRadius; //!< Segment radius (calculated from m_mass; cached)
real_t m_targetSegmentDistance; //!< Distance between the segments
......@@ -78,7 +78,7 @@ class Snake
* \param boost Whether to apply boost
* \returns The number of segments created during the operation.
*/
std::size_t move(real_t targetAngle, bool boost);
std::size_t move(real_t deltaAngle, bool boost);
/*!
* Get the list of segments.
......@@ -125,6 +125,8 @@ class Snake
*/
void dropFood(float_t value);
real_t getHeading(void) { return m_heading; }
real_t getMass(void) { return m_mass; }
real_t getConsumeRadius(void);
......
......@@ -56,14 +56,9 @@ bool LuaBot::init(std::string& initErrorMessage)
}
}
bool LuaBot::step(float &next_heading, bool &boost)
bool LuaBot::step(float &directionChange, bool &boost)
{
real_t last_heading = m_bot.getHeading();
next_heading = last_heading;
boost = false;
bool retval = false;
try {
setQuota(1000000, 0.1);
sol::protected_function step = m_lua_safe_env["step"];
......@@ -71,23 +66,19 @@ bool LuaBot::step(float &next_heading, bool &boost)
auto result = step();
if (result.valid())
{
next_heading = result.get<real_t>(0);
directionChange = result.get<real_t>(0);
if ((result.return_count()<1) || (result.return_count()>2))
{
throw std::runtime_error("step() must return a direction (as float, in radiens) and optionally if the bot should boost (as bool)");
}
if (!std::isfinite(next_heading))
if (!std::isfinite(directionChange))
{
throw std::runtime_error("step() did not return a finite number. did you divide by zero or return a string?");
}
boost = (result.return_count()>1) && result.get<bool>(1);
next_heading = result.get<float>(0);
next_heading = 180 * (next_heading / M_PI);
next_heading += last_heading;
retval = true;
}
else
......@@ -176,7 +167,7 @@ std::vector<LuaFoodInfo>& LuaBot::apiFindFood(real_t radius, real_t min_size)
m_luaFoodInfoTable.clear();
auto head_pos = m_bot.getSnake()->getHeadPosition();
real_t heading_rad = static_cast<real_t>(2.0 * M_PI * (m_bot.getHeading() / 360.0));
real_t heading = m_bot.getHeading();
radius = std::min(radius, m_bot.getSightRadius());
......@@ -186,7 +177,7 @@ std::vector<LuaFoodInfo>& LuaBot::apiFindFood(real_t radius, real_t min_size)
if (food.getValue()>=min_size)
{
Vector2D relPos = field->unwrapRelativeCoords(food.pos() - head_pos);
real_t direction = static_cast<real_t>(atan2(relPos.y(), relPos.x())) - heading_rad;
real_t direction = static_cast<real_t>(atan2(relPos.y(), relPos.x())) - heading;
while (direction < -M_PI) { direction += 2*M_PI; }
while (direction > M_PI) { direction -= 2*M_PI; }
m_luaFoodInfoTable.emplace_back(
......@@ -213,7 +204,7 @@ std::vector<LuaSegmentInfo>& LuaBot::apiFindSegments(real_t radius, bool include
m_luaSegmentInfoTable.clear();
auto pos = m_bot.getSnake()->getHeadPosition();
real_t heading_rad = 2*M_PI * (m_bot.getHeading() / 360.0);
real_t heading = m_bot.getHeading();
radius = std::min(radius, m_bot.getSightRadius());
auto self_id = m_bot.getGUID();
......@@ -226,9 +217,9 @@ std::vector<LuaSegmentInfo>& LuaBot::apiFindSegments(real_t radius, bool include
real_t distance = relPos.norm();
if (distance > (radius+segmentRadius)) { continue; }
real_t direction = atan2(relPos.y(), relPos.x()) - heading_rad;
while (direction < -M_PI) { direction += 2*M_PI; }
while (direction > M_PI) { direction -= 2*M_PI; }
real_t direction = atan2(relPos.y(), relPos.x()) - heading;
if (direction < -M_PI) { direction += 2*M_PI; }
if (direction > M_PI) { direction -= 2*M_PI; }
m_luaSegmentInfoTable.emplace_back(
segmentInfo.bot.get(),
relPos.x(),
......
......@@ -12,7 +12,7 @@ class LuaBot
public:
LuaBot(Bot &bot, std::string script);
bool init(std::string &initErrorMessage);
bool step(float &next_heading, bool &boost);
bool step(float &directionChange, bool &boost);
std::vector<uint32_t> getColors() { return m_self.colors; }
uint32_t getFace() { return m_self.getFace(); }
uint32_t getDogTag() { return m_self.getDogTag(); }
......
......@@ -131,5 +131,5 @@ real_t LuaSelfInfo::getConsumedFoodHuntedBySelf()
real_t LuaSelfInfo::getMaxStepAngle()
{
return (M_PI * m_bot.getSnake()->maxRotationPerStep()) / 180.0;
return m_bot.getSnake()->maxRotationPerStep();
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment