redo pheromones

This commit is contained in:
churl
2021-04-12 01:35:42 +02:00
parent 8d704ec1fc
commit edbcaa0a6e
10 changed files with 75 additions and 79 deletions

View File

@ -1,12 +1,12 @@
#include "ant.hpp"
#include "pheromones.hpp"
#include "pheromone_map.hpp"
#include <cmath>
#include <iostream>
#include <numbers>
#include <random>
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<WorldObject> object) {
void Ant::addToUmwelt(const std::shared_ptr<WorldObject>& 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;
}

View File

@ -6,7 +6,7 @@
#include <memory>
#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<std::shared_ptr<WorldObject>> 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<WorldObject> object);
void addToUmwelt(const std::shared_ptr<WorldObject>& object);
void update() override;
sf::Color getPheromoneType() const override;
PheroType getPheromoneType() const override;
void move();
void updateAppearance();

View File

@ -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;
}

View File

@ -12,7 +12,7 @@ public:
void update() override;
sf::Color getPheromoneType() const override;
PheroType getPheromoneType() const override;
};
#endif // __COLONY_H_

View File

@ -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;
}

View File

@ -12,7 +12,7 @@ public:
void update() override;
sf::Color getPheromoneType() const override;
PheroType getPheromoneType() const override;
};
#endif // __FOOD_H_

View File

@ -5,21 +5,21 @@
#include <SFML/Graphics.hpp>
#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<std::unique_ptr<Ant>> 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<Ant> 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<Ant> const& obj: ants) {
window.draw(obj->appearance);
}

View File

@ -1,41 +1,28 @@
#include <iostream>
#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<Pheromone> PheromoneMap::getInVision(const Ant& ant, PheroType type, unsigned short radius) {
std::vector<Pheromone> 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);
}
}
}

View File

@ -5,21 +5,16 @@
#include <SFML/Graphics.hpp>
#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<Pheromone
std::vector<Pheromone> 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<Pheromone> getInVision(const Ant& ant, PheroType type, unsigned short radius);
void update();
};

View File

@ -1,5 +1,5 @@
#ifndef __OBJECT_H_
#define __OBJECT_H_
#ifndef __WORLD_OBJECT_H_
#define __WORLD_OBJECT_H_
#include <cmath>
#include <SFML/Graphics.hpp>
@ -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_