2024-07-21 22:02:19 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
2024-07-25 22:42:54 +03:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
|
|
#include "Map.hpp"
|
|
|
|
|
#include "Snake.hpp"
|
|
|
|
|
|
2024-07-26 19:29:35 +03:00
|
|
|
class Bot : public Snake {
|
2024-07-21 22:02:19 +03:00
|
|
|
public:
|
2024-07-25 22:42:54 +03:00
|
|
|
struct Config {
|
|
|
|
|
float time_between_targets;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-26 19:29:35 +03:00
|
|
|
Bot(canvas::SnakeObject::Config canvas_config, Snake::Config snake_config,
|
|
|
|
|
Config bot_config, Map &map)
|
|
|
|
|
: Snake(canvas_config, snake_config, map), bot_config_(bot_config) {}
|
2024-07-25 22:42:54 +03:00
|
|
|
|
2024-07-26 19:29:35 +03:00
|
|
|
protected:
|
|
|
|
|
void change_direction(float dt) override {
|
2024-07-25 22:42:54 +03:00
|
|
|
time_from_target_set_ += dt;
|
|
|
|
|
|
|
|
|
|
if (not target_.has_value() or
|
2024-07-26 19:29:35 +03:00
|
|
|
time_from_target_set_ >= bot_config_.time_between_targets or
|
|
|
|
|
(target_.value() - get_pos()).len_sq() <
|
|
|
|
|
canvas_config_.radius * canvas_config_.radius) {
|
2024-07-25 22:42:54 +03:00
|
|
|
time_from_target_set_ = 0;
|
|
|
|
|
|
2024-07-26 19:29:35 +03:00
|
|
|
target_ = map_.find_nearest_food(get_pos());
|
2024-07-25 22:42:54 +03:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 22:55:59 +03:00
|
|
|
// check for case when no food found
|
|
|
|
|
if (target_.has_value()) {
|
|
|
|
|
direction_ = (Vecf(target_.value()) - real_pos_).norm();
|
|
|
|
|
// TODO: manually change direction
|
|
|
|
|
}
|
2024-07-25 22:42:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2024-07-26 19:29:35 +03:00
|
|
|
const Config bot_config_;
|
2024-07-25 22:42:54 +03:00
|
|
|
|
2024-07-25 22:55:59 +03:00
|
|
|
std::optional<Veci> target_ = {};
|
2024-07-25 22:42:54 +03:00
|
|
|
float time_from_target_set_ = 0;
|
2024-07-21 22:02:19 +03:00
|
|
|
};
|