From edbcaa0a6e9193e1d26ada61dab3dabd5a7b9a79 Mon Sep 17 00:00:00 2001 From: churl Date: Mon, 12 Apr 2021 01:35:42 +0200 Subject: [PATCH] redo pheromones --- src/ant.cpp | 35 ++++++++++++++++++-------------- src/ant.hpp | 16 +++++++-------- src/colony.cpp | 4 ++-- src/colony.hpp | 2 +- src/food.cpp | 4 ++-- src/food.hpp | 2 +- src/main.cpp | 21 +++++++++++-------- src/pheromone_map.cpp | 47 ++++++++++++++++--------------------------- src/pheromone_map.hpp | 13 ++++-------- src/world_object.hpp | 10 ++++++--- 10 files changed, 75 insertions(+), 79 deletions(-) diff --git a/src/ant.cpp b/src/ant.cpp index 9b6b5f9..ac0aa64 100644 --- a/src/ant.cpp +++ b/src/ant.cpp @@ -1,12 +1,12 @@ #include "ant.hpp" -#include "pheromones.hpp" +#include "pheromone_map.hpp" #include #include #include #include -Ant::Ant(Pheromones& pheromones, double x, double y) - : WorldObject(x, y, 2, sf::Color::Black), pheromones(pheromones) { +Ant::Ant(PheromoneMap& pheromones, double x, double y) + : WorldObject(x, y, 3, sf::Color::Black), pheromones(pheromones) { std::random_device rd; // obtain a random number from hardware std::mt19937 gen(rd()); // seed the generator @@ -14,8 +14,8 @@ Ant::Ant(Pheromones& pheromones, double x, double y) direction = degree_distribution(gen); } -Ant::Ant(Pheromones& pheromones, unsigned short direction) - : WorldObject(0, 0, 2, sf::Color::Black), direction(direction), +Ant::Ant(PheromoneMap& pheromones, unsigned short direction) + : WorldObject(0, 0, 3, sf::Color::Black), direction(direction), pheromones(pheromones) { std::random_device device; // obtain a random number from hardware std::mt19937 generator(device()); // seed the generator @@ -29,8 +29,8 @@ Ant::Ant(Pheromones& pheromones, unsigned short direction) updateAppearance(); } -Ant::Ant(Pheromones& pheromones) - : WorldObject(0, 0, 2, sf::Color::Black), pheromones(pheromones) { +Ant::Ant(PheromoneMap& pheromones) + : WorldObject(0, 0, 3, sf::Color::Black), pheromones(pheromones) { std::random_device device; // obtain a random number from hardware std::mt19937 generator(device()); // seed the generator @@ -46,7 +46,7 @@ Ant::Ant(Pheromones& pheromones) updateAppearance(); } -void Ant::addToUmwelt(std::shared_ptr object) { +void Ant::addToUmwelt(const std::shared_ptr& object) { umwelt.push_back(object); } @@ -69,11 +69,16 @@ void Ant::update() { } void Ant::move() { - // TODO: Leftoff, add ant-vision lol - for (int i = x; i < WIDTH * HEIGHT; ++i) { - if (true) { + PheroType attractor; + if (pheromone_type == HOME) { + attractor = FOOD; + } else if (pheromone_type == FOOD) { + attractor = HOME; + } else { + attractor = NONE; + } + for (const Pheromone& pheromone : pheromones.getInVision(*this, attractor, view_distance)) { - } } std::random_device device; // obtain a random number from hardware @@ -104,13 +109,13 @@ void Ant::updatePheromones() { next_pheromone_drop = std::max(0, next_pheromone_drop - 1); } -sf::Color Ant::getPheromoneType() const { - return sf::Color::Transparent; +PheroType Ant::getPheromoneType() const { + return NONE; } void Ant::dropPheromone() { if (isOffScreen()) { - std::cout << "Ant can't drop Pheromones offscreen!" << std::endl; + std::cout << "Ant can't drop PheromoneMap offscreen!" << std::endl; return; } diff --git a/src/ant.hpp b/src/ant.hpp index fb044fc..c1aa58d 100644 --- a/src/ant.hpp +++ b/src/ant.hpp @@ -6,7 +6,7 @@ #include #include "world_object.hpp" -#include "pheromones.hpp" +#include "pheromone_map.hpp" #include "colony.hpp" #include "food.hpp" @@ -21,22 +21,22 @@ class Ant : public WorldObject { double direction; // in radians - Pheromones& pheromones; + PheromoneMap& pheromones; unsigned short next_pheromone_drop = 0; - sf::Color pheromone_type = sf::Color::Transparent; // ANT, HOME, FOOD + PheroType pheromone_type = NONE; // FOOD, HOME, NONE public: std::vector> umwelt; public: - Ant(Pheromones& pheromones, double x, double y); - Ant(Pheromones& pheromones, unsigned short direction); - explicit Ant(Pheromones& pheromones); + Ant(PheromoneMap& pheromones, double x, double y); + Ant(PheromoneMap& pheromones, unsigned short direction); + explicit Ant(PheromoneMap& pheromones); - void addToUmwelt(std::shared_ptr object); + void addToUmwelt(const std::shared_ptr& object); void update() override; - sf::Color getPheromoneType() const override; + PheroType getPheromoneType() const override; void move(); void updateAppearance(); diff --git a/src/colony.cpp b/src/colony.cpp index 2a2fb99..b8435c0 100644 --- a/src/colony.cpp +++ b/src/colony.cpp @@ -4,6 +4,6 @@ Colony::Colony(double x, double y) : WorldObject(x, y, 25, sf::Color::Red) {} void Colony::update() {} -sf::Color Colony::getPheromoneType() const { - return sf::Color::Red; +PheroType Colony::getPheromoneType() const { + return HOME; } diff --git a/src/colony.hpp b/src/colony.hpp index 9d4b556..049684c 100644 --- a/src/colony.hpp +++ b/src/colony.hpp @@ -12,7 +12,7 @@ public: void update() override; - sf::Color getPheromoneType() const override; + PheroType getPheromoneType() const override; }; #endif // __COLONY_H_ diff --git a/src/food.cpp b/src/food.cpp index 8089242..aa5eb3c 100644 --- a/src/food.cpp +++ b/src/food.cpp @@ -4,6 +4,6 @@ Food::Food(double x, double y) : WorldObject(x, y, 15, sf::Color::Green) {} void Food::update() {} -sf::Color Food::getPheromoneType() const { - return sf::Color::Green; +PheroType Food::getPheromoneType() const { + return FOOD; } diff --git a/src/food.hpp b/src/food.hpp index de935df..4cde2be 100644 --- a/src/food.hpp +++ b/src/food.hpp @@ -12,7 +12,7 @@ public: void update() override; - sf::Color getPheromoneType() const override; + PheroType getPheromoneType() const override; }; #endif // __FOOD_H_ diff --git a/src/main.cpp b/src/main.cpp index 49b3b37..63372bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,21 +5,21 @@ #include -#include "pheromones.hpp" -#include "world_object.hpp" +#include "pheromone_map.hpp" #include "ant.hpp" #include "colony.hpp" #include "food.hpp" +#include "pheromone.hpp" const unsigned short HEIGHT = 500; const unsigned short WIDTH = 500; const unsigned short FPS = 60; -const unsigned short ANTCOUNT = 500; +const unsigned short ANTCOUNT = 100; int main(int argc, char* argv[]) { sf::ContextSettings settings; -// settings.antialiasingLevel = 8; + settings.antialiasingLevel = 8; sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Ants", sf::Style::Close, settings); window.setFramerateLimit(FPS); // Limit FPS @@ -27,7 +27,7 @@ int main(int argc, char* argv[]) { float t = 0.0; // Verstrichene Zeit in ms float dt = 1.0 / FPS; // Schrittweite in ms - Pheromones pheromones; + PheromoneMap pheromones; std::vector> ants; // Use pointer bc we can't instatiate abstract classes ants.reserve(ANTCOUNT); @@ -43,7 +43,7 @@ int main(int argc, char* argv[]) { } while (window.isOpen()) { - sf::Event event; + sf::Event event{}; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { @@ -53,14 +53,19 @@ int main(int argc, char* argv[]) { // Update t += dt; + pheromones.update(); for (std::unique_ptr const& obj: ants) { obj->update(); } - pheromones.update(); + for (Pheromone& pheromone : pheromones.pheromones) { + pheromone.update(); + } // Render window.clear(sf::Color::White); - window.draw(pheromones.map); + for (Pheromone& pheromone : pheromones.pheromones) { + window.draw(pheromone.appearance); + } for (std::unique_ptr const& obj: ants) { window.draw(obj->appearance); } diff --git a/src/pheromone_map.cpp b/src/pheromone_map.cpp index 9032325..0ae349a 100644 --- a/src/pheromone_map.cpp +++ b/src/pheromone_map.cpp @@ -1,41 +1,28 @@ +#include #include "pheromone_map.hpp" +#include "ant.hpp" -extern const unsigned short WIDTH; -extern const unsigned short HEIGHT; +void PheromoneMap::place(double x, double y, PheroType type) { + pheromones.emplace_back(x, y, type); +} -PheromoneMap::PheromoneMap() { - for (unsigned short y = 0; y < HEIGHT; ++y) { - for (unsigned short x = 0; x < WIDTH; ++x) { - map[y * WIDTH + x].position.x = x; - map[y * WIDTH + x].position.y = y; - map[y * WIDTH + x].color = sf::Color(0, 0, 0, 0); +std::vector PheromoneMap::getInVision(const Ant& ant, PheroType type, unsigned short radius) { + std::vector umwelt; + + for (const Pheromone& pheromone : pheromones) { + if (pheromone.getPheromoneType() == type + && pheromone.distance(ant) <= radius) { + umwelt.push_back(pheromone); } } -} -void PheromoneMap::place(unsigned short x, unsigned short y, sf::Color col) { - map[y * WIDTH + x].color = col; - - // Have to check for off-limit-pixels - map[std::min(WIDTH * HEIGHT - 1, (y + 1) * WIDTH + x)].color = col; - map[std::max(0, (y - 1) * WIDTH + x)].color = col; - map[std::min(WIDTH * HEIGHT - 1, y * WIDTH + (x + 1))].color = col; - map[std::max(0, y * WIDTH + (x - 1))].color = col; -} - -sf::Color PheromoneMap::get(unsigned short x, unsigned short y) const { - // TODO: range-checks - return map[y * WIDTH + x].color; + return umwelt; } void PheromoneMap::update() { - for (int i = 0; i < WIDTH * HEIGHT; ++i) { - map[i].color -= sf::Color(decay, decay, decay, decay); - } - for (int x = 0; x < WIDTH; ++x) { - map[(HEIGHT / 2) * WIDTH + x].color = sf::Color::Black; - } - for (int y = 0; y < HEIGHT; ++y) { - map[y * WIDTH + (WIDTH / 2)].color = sf::Color::Black; + for (size_t i = 0; i < pheromones.size(); ++i) { + if (pheromones[i].appearance.getFillColor().a == 0) { +// pheromones.erase(pheromones.begin() + i); + } } } diff --git a/src/pheromone_map.hpp b/src/pheromone_map.hpp index 60f818d..d8918c2 100644 --- a/src/pheromone_map.hpp +++ b/src/pheromone_map.hpp @@ -5,21 +5,16 @@ #include #include "pheromone.hpp" -extern const unsigned short WIDTH; -extern const unsigned short HEIGHT; - -const unsigned short decay = 1; +class Ant; class PheromoneMap { public: - std::vector pheromones; public: - PheromoneMap(); - - void place(unsigned short x, unsigned short y, sf::Color col); - sf::Color get(unsigned short x, unsigned short y) const; + void place(double x, double y, PheroType type); + std::vector getInVision(const Ant& ant, PheroType type, unsigned short radius); void update(); }; diff --git a/src/world_object.hpp b/src/world_object.hpp index 0e3ab01..54a7b68 100644 --- a/src/world_object.hpp +++ b/src/world_object.hpp @@ -1,5 +1,5 @@ -#ifndef __OBJECT_H_ -#define __OBJECT_H_ +#ifndef __WORLD_OBJECT_H_ +#define __WORLD_OBJECT_H_ #include #include @@ -7,6 +7,10 @@ extern const unsigned short WIDTH; extern const unsigned short HEIGHT; +enum PheroType { + FOOD, HOME, NONE +}; + class WorldObject { protected: @@ -28,7 +32,7 @@ public: double angle(const WorldObject& other) const; virtual void update() = 0; // pure virtual: has to be overridden - virtual sf::Color getPheromoneType() const = 0; + virtual PheroType getPheromoneType() const = 0; }; #endif // __OBJECT_H_