From 725c69e54a6d1b5f7c46582ca5ed1280760f42d5 Mon Sep 17 00:00:00 2001 From: churl Date: Sat, 14 May 2022 23:30:58 +0200 Subject: [PATCH] Initial code findings --- src/ant.cpp | 17 +++++++++-------- src/ant.hpp | 28 ++++++++++++++++++---------- src/colony.hpp | 1 - src/food.hpp | 1 - src/main.cpp | 15 ++++++++------- src/pheromone.cpp | 4 +--- src/pheromone.hpp | 7 ++++--- src/pheromone_map.cpp | 2 -- src/pheromone_map.hpp | 8 +++++--- src/world_object.cpp | 2 +- src/world_object.hpp | 21 ++++++++++----------- 11 files changed, 56 insertions(+), 50 deletions(-) diff --git a/src/ant.cpp b/src/ant.cpp index ab8832b..c44e258 100644 --- a/src/ant.cpp +++ b/src/ant.cpp @@ -1,10 +1,6 @@ #include "ant.hpp" -#include "pheromone_map.hpp" -#include -#include -#include -#include +// TODO: Should every ant have its own random generators? Why not initialize with coordinates and put the random numbers in there (from main) 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 @@ -14,7 +10,7 @@ Ant::Ant(PheromoneMap& pheromones, double x, double y) direction = degree_distribution(gen); } -Ant::Ant(PheromoneMap& pheromones, unsigned short direction) +Ant::Ant(PheromoneMap& pheromones, unsigned int direction) : WorldObject(0, 0, 3, sf::Color::Black), direction(direction), pheromones(pheromones) { std::random_device device; // obtain a random number from hardware @@ -46,6 +42,7 @@ Ant::Ant(PheromoneMap& pheromones) updateAppearance(); } +// TODO: Unnecessary, only used for food/base void Ant::addToUmwelt(const std::shared_ptr& object) { umwelt.push_back(object); } @@ -78,8 +75,10 @@ void Ant::move() { attractor = NONE; } for (const Pheromone& pheromone : pheromones.getInVision(*this, attractor, view_distance)) { + // TODO: What is this supposed to do? } + // TODO: Should this random generator be created on every move? std::random_device device; // obtain a random number from hardware std::mt19937 generator(device()); // seed the generator @@ -87,7 +86,8 @@ void Ant::move() { std::uniform_real_distribution<> degree_distribution(-std::numbers::pi, std::numbers::pi); - direction += degree_distribution(generator) * (1 / determination); + direction += degree_distribution(generator) * (1 / determination); // Normalize with determination to smooth movement + // TODO: Use modulo if (direction > 2 * std::numbers::pi) { direction -= 2 * std::numbers::pi; } @@ -105,7 +105,7 @@ void Ant::updatePheromones() { dropPheromone(); next_pheromone_drop = pheromone_interval + 1; } - next_pheromone_drop = std::max(0, next_pheromone_drop - 1); + next_pheromone_drop = std::max(0U, next_pheromone_drop - 1); } PheroType Ant::getPheromoneType() const { @@ -121,6 +121,7 @@ void Ant::dropPheromone() { pheromones.place(x, y, pheromone_type); } +// TODO: This is weird void Ant::updateUmwelt() { for (std::shared_ptr const& obj : umwelt) { if (obj->collides(*this)) { diff --git a/src/ant.hpp b/src/ant.hpp index 9759cc3..dd444df 100644 --- a/src/ant.hpp +++ b/src/ant.hpp @@ -1,37 +1,43 @@ #ifndef H_ANT #define H_ANT -#include -#include -#include - #include "colony.hpp" #include "food.hpp" #include "pheromone_map.hpp" #include "world_object.hpp" +#include +#include +#include +#include +#include +#include +#include const double speed = 1; -const double determination = 25; // straightness of the path, (0, 1] -const unsigned short pheromone_interval = 5; // updates between drops +const double determination = 25; // straightness of the path +const unsigned int pheromone_interval = 5; // how many updates between drops -const unsigned short view_angle = 45; // angle degrees to each side -const unsigned short view_distance = 25; +const unsigned int view_angle = 45; // angle degrees to each side +const unsigned int view_distance = 25; // more like smell distance class Ant : public WorldObject { double direction; // in radians + // TODO: Should this be here or just global? PheromoneMap& pheromones; - unsigned short next_pheromone_drop = 0; + unsigned int next_pheromone_drop = 0; PheroType pheromone_type = NONE; // FOOD, HOME, NONE public: + // TODO: Why is this stored here? I guess it can be removed std::vector> umwelt; public: Ant(PheromoneMap& pheromones, double x, double y); - Ant(PheromoneMap& pheromones, unsigned short direction); + Ant(PheromoneMap& pheromones, unsigned int direction); explicit Ant(PheromoneMap& pheromones); + // TODO: Regarding umwelt void addToUmwelt(const std::shared_ptr& object); void update() override; @@ -41,6 +47,8 @@ public: void updateAppearance(); void updatePheromones(); void dropPheromone(); // red + + // TODO: Regarding umwelt void updateUmwelt(); }; diff --git a/src/colony.hpp b/src/colony.hpp index 526acfa..54010d2 100644 --- a/src/colony.hpp +++ b/src/colony.hpp @@ -10,7 +10,6 @@ public: Colony(double x, double y); void update() override; - PheroType getPheromoneType() const override; }; diff --git a/src/food.hpp b/src/food.hpp index 8c1822f..a1d182e 100644 --- a/src/food.hpp +++ b/src/food.hpp @@ -10,7 +10,6 @@ public: Food(double x, double y); void update() override; - PheroType getPheromoneType() const override; }; diff --git a/src/main.cpp b/src/main.cpp index 925a815..7b3e0b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,11 +11,11 @@ #include "pheromone.hpp" #include "pheromone_map.hpp" -const unsigned short HEIGHT = 500; -const unsigned short WIDTH = 500; -const unsigned short FPS = 60; +const unsigned int HEIGHT = 500; +const unsigned int WIDTH = 500; +const unsigned int FPS = 60; -const unsigned short ANTCOUNT = 100; +const unsigned int ANTCOUNT = 100; int main(int argc, char* argv[]) { sf::ContextSettings settings; @@ -24,8 +24,8 @@ int main(int argc, char* argv[]) { sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Ants", sf::Style::Close, settings); window.setFramerateLimit(FPS); // Limit FPS - float t = 0.0; // Verstrichene Zeit in ms - float dt = 1.0 / FPS; // Schrittweite in ms + double t = 0.0; // Verstrichene Zeit in ms + double dt = 1.0 / FPS; // Schrittweite in ms PheromoneMap pheromones; std::vector> ants; // Use pointer bc we can't instatiate abstract classes @@ -42,8 +42,9 @@ int main(int argc, char* argv[]) { ant->addToUmwelt(foodA); } - // Main event loop while (window.isOpen()) { + // Main event loop + sf::Event event {}; while (window.pollEvent(event)) { diff --git a/src/pheromone.cpp b/src/pheromone.cpp index e4f183e..648cda2 100644 --- a/src/pheromone.cpp +++ b/src/pheromone.cpp @@ -3,8 +3,6 @@ // #include "pheromone.hpp" -#include "world_object.hpp" -#include Pheromone::Pheromone(double x, double y, PheroType type) : WorldObject(x, y, 2, sf::Color::Transparent) { @@ -28,7 +26,7 @@ PheroType Pheromone::getPheromoneType() const { } void Pheromone::update() { - intensity = std::max(0, intensity - decay); + intensity = std::max(0U, intensity - decay); appearance.setFillColor(sf::Color( appearance.getFillColor().r, appearance.getFillColor().g, diff --git a/src/pheromone.hpp b/src/pheromone.hpp index a75c07a..bd0eadb 100644 --- a/src/pheromone.hpp +++ b/src/pheromone.hpp @@ -8,13 +8,14 @@ #include "world_object.hpp" #include -const unsigned short decay = 1; +// TODO: Use percentage +const unsigned int decay = 1; class Pheromone : public WorldObject { public: - unsigned short intensity = 255; + // TODO: Use 1.0 to 0.0 double + unsigned int intensity = 255; -public: Pheromone(double x, double y, PheroType type); PheroType getPheromoneType() const override; diff --git a/src/pheromone_map.cpp b/src/pheromone_map.cpp index b3dae78..4a0ccaa 100644 --- a/src/pheromone_map.cpp +++ b/src/pheromone_map.cpp @@ -1,6 +1,4 @@ #include "pheromone_map.hpp" -#include "ant.hpp" -#include void PheromoneMap::place(double x, double y, PheroType type) { pheromones.emplace_back(x, y, type); diff --git a/src/pheromone_map.hpp b/src/pheromone_map.hpp index 590f8c7..f519af4 100644 --- a/src/pheromone_map.hpp +++ b/src/pheromone_map.hpp @@ -1,7 +1,9 @@ #ifndef __PHEROMONES_H_ #define __PHEROMONES_H_ +#include "ant.hpp" #include "pheromone.hpp" +#include #include #include @@ -9,12 +11,12 @@ class Ant; class PheromoneMap { public: + // TODO: Use fixed size matrix (and only int locations) for this, currently it's just slow std::vector pheromones; -public: - void place(double x, double y, PheroType type); + // TODO: Move this to ant std::vector getInVision(const Ant& ant, PheroType type, unsigned short radius); - + void place(double x, double y, PheroType type); void update(); }; diff --git a/src/world_object.cpp b/src/world_object.cpp index d1e1a0f..b5f0962 100644 --- a/src/world_object.cpp +++ b/src/world_object.cpp @@ -1,6 +1,6 @@ #include "world_object.hpp" -WorldObject::WorldObject(double x, double y, unsigned short radius, sf::Color color) +WorldObject::WorldObject(double x, double y, unsigned int radius, sf::Color color) : x(x), y(y), radius(radius) { appearance = sf::CircleShape(radius); appearance.setFillColor(color); diff --git a/src/world_object.hpp b/src/world_object.hpp index 8766ce6..3b11806 100644 --- a/src/world_object.hpp +++ b/src/world_object.hpp @@ -4,9 +4,11 @@ #include #include -extern const unsigned short WIDTH; -extern const unsigned short HEIGHT; +// TODO: Move definitions to header and use constexpr/macro +extern const unsigned int WIDTH; +extern const unsigned int HEIGHT; +// TODO: Just use FOOD enum PheroType { FOOD, HOME, @@ -16,24 +18,21 @@ enum PheroType { class WorldObject { protected: double x, y; - const unsigned short radius; + const unsigned int radius; + + WorldObject(double x, double y, unsigned int radius, sf::Color color); public: sf::CircleShape appearance; -protected: - WorldObject(double x, double y, unsigned short radius, sf::Color color); - -public: - bool isOffScreen() const; // virtual: late-binding, no static linkage + bool isOffScreen() const; bool collides(const WorldObject& other) const; - double distance(const WorldObject& other) const; - double angle(const WorldObject& other) const; - virtual void update() = 0; // pure virtual: has to be overridden + // TODO: Switch to only food pheromones virtual PheroType getPheromoneType() const = 0; + virtual void update() = 0; // pure virtual: has to be overridden }; #endif // __OBJECT_H_