reformat
This commit is contained in:
27
src/ant.cpp
27
src/ant.cpp
@ -6,19 +6,19 @@
|
||||
#include <random>
|
||||
|
||||
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
|
||||
: 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
|
||||
|
||||
std::uniform_real_distribution<> degree_distribution(0, 2 * std::numbers::pi);
|
||||
direction = degree_distribution(gen);
|
||||
}
|
||||
|
||||
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
|
||||
: 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
|
||||
|
||||
std::uniform_int_distribution<> width_distribution(0, WIDTH);
|
||||
x = width_distribution(generator);
|
||||
@ -30,9 +30,9 @@ Ant::Ant(PheromoneMap& pheromones, unsigned short direction)
|
||||
}
|
||||
|
||||
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
|
||||
: 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
|
||||
|
||||
std::uniform_int_distribution<> width_distribution(0, WIDTH);
|
||||
x = width_distribution(generator);
|
||||
@ -78,11 +78,10 @@ void Ant::move() {
|
||||
attractor = NONE;
|
||||
}
|
||||
for (const Pheromone& pheromone : pheromones.getInVision(*this, attractor, view_distance)) {
|
||||
|
||||
}
|
||||
|
||||
std::random_device device; // obtain a random number from hardware
|
||||
std::mt19937 generator(device()); // seed the generator
|
||||
std::random_device device; // obtain a random number from hardware
|
||||
std::mt19937 generator(device()); // seed the generator
|
||||
|
||||
// Move
|
||||
std::uniform_real_distribution<> degree_distribution(-std::numbers::pi,
|
||||
@ -123,7 +122,7 @@ void Ant::dropPheromone() {
|
||||
}
|
||||
|
||||
void Ant::updateUmwelt() {
|
||||
for (std::shared_ptr<WorldObject> const& obj: umwelt) {
|
||||
for (std::shared_ptr<WorldObject> const& obj : umwelt) {
|
||||
if (obj->collides(*this)) {
|
||||
pheromone_type = obj->getPheromoneType();
|
||||
}
|
||||
|
||||
21
src/ant.hpp
21
src/ant.hpp
@ -1,29 +1,28 @@
|
||||
#ifndef H_ANT
|
||||
#define H_ANT
|
||||
|
||||
#include <memory>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include "world_object.hpp"
|
||||
#include "pheromone_map.hpp"
|
||||
#include "colony.hpp"
|
||||
#include "food.hpp"
|
||||
#include "pheromone_map.hpp"
|
||||
#include "world_object.hpp"
|
||||
|
||||
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, (0, 1]
|
||||
const unsigned short pheromone_interval = 5; // updates between drops
|
||||
|
||||
const unsigned short view_angle = 45; // angle degrees to each side
|
||||
const unsigned short view_angle = 45; // angle degrees to each side
|
||||
const unsigned short view_distance = 25;
|
||||
|
||||
class Ant : public WorldObject
|
||||
{
|
||||
double direction; // in radians
|
||||
class Ant : public WorldObject {
|
||||
double direction; // in radians
|
||||
|
||||
PheromoneMap& pheromones;
|
||||
unsigned short next_pheromone_drop = 0;
|
||||
PheroType pheromone_type = NONE; // FOOD, HOME, NONE
|
||||
PheroType pheromone_type = NONE; // FOOD, HOME, NONE
|
||||
|
||||
public:
|
||||
std::vector<std::shared_ptr<WorldObject>> umwelt;
|
||||
@ -41,7 +40,7 @@ public:
|
||||
void move();
|
||||
void updateAppearance();
|
||||
void updatePheromones();
|
||||
void dropPheromone(); // red
|
||||
void dropPheromone(); // red
|
||||
void updateUmwelt();
|
||||
};
|
||||
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
#ifndef __COLONY_H_
|
||||
#define __COLONY_H_
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "world_object.hpp"
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
class Colony : public WorldObject
|
||||
{
|
||||
class Colony : public WorldObject {
|
||||
|
||||
public:
|
||||
Colony(double x, double y);
|
||||
@ -15,4 +14,4 @@ public:
|
||||
PheroType getPheromoneType() const override;
|
||||
};
|
||||
|
||||
#endif // __COLONY_H_
|
||||
#endif // __COLONY_H_
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
#ifndef __FOOD_H_
|
||||
#define __FOOD_H_
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "world_object.hpp"
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
class Food : public WorldObject
|
||||
{
|
||||
class Food : public WorldObject {
|
||||
|
||||
public:
|
||||
Food(double x, double y);
|
||||
@ -15,4 +14,4 @@ public:
|
||||
PheroType getPheromoneType() const override;
|
||||
};
|
||||
|
||||
#endif // __FOOD_H_
|
||||
#endif // __FOOD_H_
|
||||
|
||||
21
src/main.cpp
21
src/main.cpp
@ -1,15 +1,15 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include "pheromone_map.hpp"
|
||||
#include "ant.hpp"
|
||||
#include "colony.hpp"
|
||||
#include "food.hpp"
|
||||
#include "pheromone.hpp"
|
||||
#include "pheromone_map.hpp"
|
||||
|
||||
const unsigned short HEIGHT = 500;
|
||||
const unsigned short WIDTH = 500;
|
||||
@ -22,13 +22,13 @@ int main(int argc, char* argv[]) {
|
||||
settings.antialiasingLevel = 8;
|
||||
|
||||
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Ants", sf::Style::Close, settings);
|
||||
window.setFramerateLimit(FPS); // Limit FPS
|
||||
window.setFramerateLimit(FPS); // Limit FPS
|
||||
|
||||
float t = 0.0; // Verstrichene Zeit in ms
|
||||
float dt = 1.0 / FPS; // Schrittweite in ms
|
||||
float t = 0.0; // Verstrichene Zeit in ms
|
||||
float dt = 1.0 / FPS; // Schrittweite in ms
|
||||
|
||||
PheromoneMap pheromones;
|
||||
std::vector<std::unique_ptr<Ant>> ants; // Use pointer bc we can't instatiate abstract classes
|
||||
std::vector<std::unique_ptr<Ant>> ants; // Use pointer bc we can't instatiate abstract classes
|
||||
ants.reserve(ANTCOUNT);
|
||||
|
||||
std::shared_ptr<Colony> colony = std::make_shared<Colony>(WIDTH / 2, HEIGHT / 2);
|
||||
@ -42,8 +42,9 @@ int main(int argc, char* argv[]) {
|
||||
ant->addToUmwelt(foodA);
|
||||
}
|
||||
|
||||
// Main event loop
|
||||
while (window.isOpen()) {
|
||||
sf::Event event{};
|
||||
sf::Event event {};
|
||||
|
||||
while (window.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed) {
|
||||
@ -54,7 +55,7 @@ int main(int argc, char* argv[]) {
|
||||
// Update
|
||||
t += dt;
|
||||
pheromones.update();
|
||||
for (std::unique_ptr<Ant> const& obj: ants) {
|
||||
for (std::unique_ptr<Ant> const& obj : ants) {
|
||||
obj->update();
|
||||
}
|
||||
for (Pheromone& pheromone : pheromones.pheromones) {
|
||||
@ -66,7 +67,7 @@ int main(int argc, char* argv[]) {
|
||||
for (Pheromone& pheromone : pheromones.pheromones) {
|
||||
window.draw(pheromone.appearance);
|
||||
}
|
||||
for (std::unique_ptr<Ant> const& obj: ants) {
|
||||
for (std::unique_ptr<Ant> const& obj : ants) {
|
||||
window.draw(obj->appearance);
|
||||
}
|
||||
window.draw(colony->appearance);
|
||||
|
||||
@ -2,12 +2,12 @@
|
||||
// Created by christoph on 11.04.21.
|
||||
//
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "world_object.hpp"
|
||||
#include "pheromone.hpp"
|
||||
#include "world_object.hpp"
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
Pheromone::Pheromone(double x, double y, PheroType type)
|
||||
: WorldObject(x, y, 2, sf::Color::Transparent) {
|
||||
: WorldObject(x, y, 2, sf::Color::Transparent) {
|
||||
if (type == HOME) {
|
||||
appearance.setFillColor(sf::Color::Red);
|
||||
} else if (type == FOOD) {
|
||||
@ -30,10 +30,8 @@ PheroType Pheromone::getPheromoneType() const {
|
||||
void Pheromone::update() {
|
||||
intensity = std::max(0, intensity - decay);
|
||||
appearance.setFillColor(sf::Color(
|
||||
appearance.getFillColor().r,
|
||||
appearance.getFillColor().g,
|
||||
appearance.getFillColor().b,
|
||||
intensity
|
||||
));
|
||||
appearance.getFillColor().r,
|
||||
appearance.getFillColor().g,
|
||||
appearance.getFillColor().b,
|
||||
intensity));
|
||||
}
|
||||
|
||||
|
||||
@ -5,13 +5,12 @@
|
||||
#ifndef ANTSIMULATOR_PHEROMONE_HPP
|
||||
#define ANTSIMULATOR_PHEROMONE_HPP
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "world_object.hpp"
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
const unsigned short decay = 1;
|
||||
|
||||
class Pheromone : public WorldObject
|
||||
{
|
||||
class Pheromone : public WorldObject {
|
||||
public:
|
||||
unsigned short intensity = 255;
|
||||
|
||||
@ -22,4 +21,4 @@ public:
|
||||
void update() override;
|
||||
};
|
||||
|
||||
#endif //ANTSIMULATOR_PHEROMONE_HPP
|
||||
#endif //ANTSIMULATOR_PHEROMONE_HPP
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#include <iostream>
|
||||
#include "pheromone_map.hpp"
|
||||
#include "ant.hpp"
|
||||
#include <iostream>
|
||||
|
||||
void PheromoneMap::place(double x, double y, PheroType type) {
|
||||
pheromones.emplace_back(x, y, type);
|
||||
@ -10,8 +10,7 @@ std::vector<Pheromone> PheromoneMap::getInVision(const Ant& ant, PheroType type,
|
||||
std::vector<Pheromone> umwelt;
|
||||
|
||||
for (const Pheromone& pheromone : pheromones) {
|
||||
if (pheromone.getPheromoneType() == type
|
||||
&& pheromone.distance(ant) <= radius) {
|
||||
if (pheromone.getPheromoneType() == type && pheromone.distance(ant) <= radius) {
|
||||
umwelt.push_back(pheromone);
|
||||
}
|
||||
}
|
||||
@ -22,7 +21,7 @@ std::vector<Pheromone> PheromoneMap::getInVision(const Ant& ant, PheroType type,
|
||||
void PheromoneMap::update() {
|
||||
for (size_t i = 0; i < pheromones.size(); ++i) {
|
||||
if (pheromones[i].appearance.getFillColor().a == 0) {
|
||||
// pheromones.erase(pheromones.begin() + i);
|
||||
// pheromones.erase(pheromones.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
#ifndef __PHEROMONES_H_
|
||||
#define __PHEROMONES_H_
|
||||
|
||||
#include <vector>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "pheromone.hpp"
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <vector>
|
||||
|
||||
class Ant;
|
||||
|
||||
class PheromoneMap
|
||||
{
|
||||
class PheromoneMap {
|
||||
public:
|
||||
std::vector<Pheromone> pheromones;
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include "world_object.hpp"
|
||||
|
||||
WorldObject::WorldObject(double x, double y, unsigned short radius, sf::Color color)
|
||||
: x(x), y(y), radius(radius) {
|
||||
: x(x), y(y), radius(radius) {
|
||||
appearance = sf::CircleShape(radius);
|
||||
appearance.setFillColor(color);
|
||||
appearance.setPosition(x, y);
|
||||
|
||||
@ -8,11 +8,12 @@ extern const unsigned short WIDTH;
|
||||
extern const unsigned short HEIGHT;
|
||||
|
||||
enum PheroType {
|
||||
FOOD, HOME, NONE
|
||||
FOOD,
|
||||
HOME,
|
||||
NONE
|
||||
};
|
||||
|
||||
class WorldObject
|
||||
{
|
||||
class WorldObject {
|
||||
protected:
|
||||
double x, y;
|
||||
const unsigned short radius;
|
||||
@ -24,15 +25,15 @@ 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; // virtual: late-binding, no static linkage
|
||||
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
|
||||
virtual void update() = 0; // pure virtual: has to be overridden
|
||||
virtual PheroType getPheromoneType() const = 0;
|
||||
};
|
||||
|
||||
#endif // __OBJECT_H_
|
||||
#endif // __OBJECT_H_
|
||||
|
||||
Reference in New Issue
Block a user