This commit is contained in:
churl
2022-05-14 22:50:33 +02:00
parent 3402170aa6
commit e939bce35c
12 changed files with 204 additions and 71 deletions

140
.clang-format Executable file
View File

@ -0,0 +1,140 @@
---
BasedOnStyle: WebKit
AccessModifierOffset: '-4'
AlignAfterOpenBracket: Align
# Don't vertically align too much to not fuck with VC
AlignArrayOfStructures: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: None
AlignEscapedNewlines: DontAlign
AlignOperands: AlignAfterOperator
AlignTrailingComments: 'true'
AllowAllArgumentsOnNextLine: 'false'
# DEPRECATED: AllowAllConstructorInitializersOnNextLine: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'false'
# Allow single line stuff
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: 'true'
AllowShortEnumsOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: 'true'
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'No'
# Don't force single line for each
BinPackArguments: 'true'
BinPackParameters: 'true'
BitFieldColonSpacing: Both
BreakBeforeBinaryOperators: None
# Braces completely attached, should be the same as BreakBeforeBraces: Attach
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: 'false'
AfterClass: 'false'
AfterControlStatement: Never
AfterEnum: 'false'
AfterFunction: 'false'
AfterNamespace: 'false'
AfterStruct: 'false'
AfterUnion: 'false'
AfterExternBlock: 'false'
BeforeCatch: 'false'
BeforeElse: 'false'
BeforeLambdaBody: 'false'
BeforeWhile: 'false'
IndentBraces: 'false'
SplitEmptyFunction: 'false'
SplitEmptyRecord: 'false'
SplitEmptyNamespace: 'false'
BreakBeforeConceptDeclarations: 'false'
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: 'false'
ColumnLimit: '0'
CompactNamespaces: 'false'
# DEPRECATED: ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
ConstructorInitializerIndentWidth: '2'
ContinuationIndentWidth: '2'
Cpp11BracedListStyle: 'true'
# Force my style onto everything :)
DeriveLineEnding: 'false'
DerivePointerAlignment: 'false'
DisableFormat: 'false'
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: Always
ExperimentalAutoDetectBinPacking: 'false'
FixNamespaceComments: 'true'
IncludeBlocks: Preserve
IndentAccessModifiers: 'false' # don't indent, use AccessModifierOffset instead
IndentCaseBlocks: 'false'
IndentCaseLabels: 'false'
IndentExternBlock: Indent
IndentGotoLabels: 'false'
IndentPPDirectives: None
IndentRequires: 'false'
IndentWidth: '4'
IndentWrappedFunctionNames: 'false'
# TODO: InsertBraces: 'true'
KeepEmptyLinesAtTheStartOfBlocks: 'true'
LambdaBodyIndentation: Signature
Language: Cpp
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: All
PointerAlignment: Left
PPIndentWidth: -1 # use IndentWidth
# TODO: PackConstructorInitializers: NextLine
# TODO: QualifierAlignment: Leave
ReferenceAlignment: Left
ReflowComments: 'false'
# TODO: RemoveBracesLLVM: 'false'
# TODO: RequiresClausePosition: WithPreceding
# TODO: SeparateDefinitionBlocks: Always
ShortNamespaceLines: 0
SortIncludes: CaseInsensitive
SortUsingDeclarations: 'true'
SpaceAfterCStyleCast: 'false'
SpaceAfterLogicalNot: 'false'
SpaceAfterTemplateKeyword: 'false'
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCaseColon: 'false'
SpaceBeforeCpp11BracedList: 'true'
SpaceBeforeCtorInitializerColon: 'true'
SpaceBeforeInheritanceColon: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyBlock: 'false'
SpaceInEmptyParentheses: 'false'
SpacesBeforeTrailingComments: '2'
SpacesInAngles: Never
SpacesInCStyleCastParentheses: 'false'
SpacesInConditionalStatement: 'false'
SpacesInContainerLiterals: 'false'
SpacesInLineCommentPrefix:
Minimum: '1'
Maximum: '1'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
Standard: c++20
TabWidth: '4'
UseCRLF: 'false'
UseTab: Never
...

View File

@ -6,19 +6,19 @@
#include <random> #include <random>
Ant::Ant(PheromoneMap& pheromones, double x, double y) Ant::Ant(PheromoneMap& pheromones, double x, double y)
: WorldObject(x, y, 3, sf::Color::Black), pheromones(pheromones) { : WorldObject(x, y, 3, sf::Color::Black), pheromones(pheromones) {
std::random_device rd; // obtain a random number from hardware std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator std::mt19937 gen(rd()); // seed the generator
std::uniform_real_distribution<> degree_distribution(0, 2 * std::numbers::pi); std::uniform_real_distribution<> degree_distribution(0, 2 * std::numbers::pi);
direction = degree_distribution(gen); direction = degree_distribution(gen);
} }
Ant::Ant(PheromoneMap& pheromones, unsigned short direction) Ant::Ant(PheromoneMap& pheromones, unsigned short direction)
: WorldObject(0, 0, 3, sf::Color::Black), direction(direction), : WorldObject(0, 0, 3, sf::Color::Black), direction(direction),
pheromones(pheromones) { pheromones(pheromones) {
std::random_device device; // obtain a random number from hardware std::random_device device; // obtain a random number from hardware
std::mt19937 generator(device()); // seed the generator std::mt19937 generator(device()); // seed the generator
std::uniform_int_distribution<> width_distribution(0, WIDTH); std::uniform_int_distribution<> width_distribution(0, WIDTH);
x = width_distribution(generator); x = width_distribution(generator);
@ -30,9 +30,9 @@ Ant::Ant(PheromoneMap& pheromones, unsigned short direction)
} }
Ant::Ant(PheromoneMap& pheromones) Ant::Ant(PheromoneMap& pheromones)
: WorldObject(0, 0, 3, sf::Color::Black), pheromones(pheromones) { : WorldObject(0, 0, 3, sf::Color::Black), pheromones(pheromones) {
std::random_device device; // obtain a random number from hardware std::random_device device; // obtain a random number from hardware
std::mt19937 generator(device()); // seed the generator std::mt19937 generator(device()); // seed the generator
std::uniform_int_distribution<> width_distribution(0, WIDTH); std::uniform_int_distribution<> width_distribution(0, WIDTH);
x = width_distribution(generator); x = width_distribution(generator);
@ -78,11 +78,10 @@ void Ant::move() {
attractor = NONE; attractor = NONE;
} }
for (const Pheromone& pheromone : pheromones.getInVision(*this, attractor, view_distance)) { for (const Pheromone& pheromone : pheromones.getInVision(*this, attractor, view_distance)) {
} }
std::random_device device; // obtain a random number from hardware std::random_device device; // obtain a random number from hardware
std::mt19937 generator(device()); // seed the generator std::mt19937 generator(device()); // seed the generator
// Move // Move
std::uniform_real_distribution<> degree_distribution(-std::numbers::pi, std::uniform_real_distribution<> degree_distribution(-std::numbers::pi,
@ -123,7 +122,7 @@ void Ant::dropPheromone() {
} }
void Ant::updateUmwelt() { void Ant::updateUmwelt() {
for (std::shared_ptr<WorldObject> const& obj: umwelt) { for (std::shared_ptr<WorldObject> const& obj : umwelt) {
if (obj->collides(*this)) { if (obj->collides(*this)) {
pheromone_type = obj->getPheromoneType(); pheromone_type = obj->getPheromoneType();
} }

View File

@ -1,29 +1,28 @@
#ifndef H_ANT #ifndef H_ANT
#define H_ANT #define H_ANT
#include <memory>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <string> #include <string>
#include <memory>
#include "world_object.hpp"
#include "pheromone_map.hpp"
#include "colony.hpp" #include "colony.hpp"
#include "food.hpp" #include "food.hpp"
#include "pheromone_map.hpp"
#include "world_object.hpp"
const double speed = 1; const double speed = 1;
const double determination = 25; // straightness of the path, (0, 1] const double determination = 25; // straightness of the path, (0, 1]
const unsigned short pheromone_interval = 5; // updates between drops 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; const unsigned short view_distance = 25;
class Ant : public WorldObject class Ant : public WorldObject {
{ double direction; // in radians
double direction; // in radians
PheromoneMap& pheromones; PheromoneMap& pheromones;
unsigned short next_pheromone_drop = 0; unsigned short next_pheromone_drop = 0;
PheroType pheromone_type = NONE; // FOOD, HOME, NONE PheroType pheromone_type = NONE; // FOOD, HOME, NONE
public: public:
std::vector<std::shared_ptr<WorldObject>> umwelt; std::vector<std::shared_ptr<WorldObject>> umwelt;
@ -41,7 +40,7 @@ public:
void move(); void move();
void updateAppearance(); void updateAppearance();
void updatePheromones(); void updatePheromones();
void dropPheromone(); // red void dropPheromone(); // red
void updateUmwelt(); void updateUmwelt();
}; };

View File

@ -1,11 +1,10 @@
#ifndef __COLONY_H_ #ifndef __COLONY_H_
#define __COLONY_H_ #define __COLONY_H_
#include <SFML/Graphics.hpp>
#include "world_object.hpp" #include "world_object.hpp"
#include <SFML/Graphics.hpp>
class Colony : public WorldObject class Colony : public WorldObject {
{
public: public:
Colony(double x, double y); Colony(double x, double y);
@ -15,4 +14,4 @@ public:
PheroType getPheromoneType() const override; PheroType getPheromoneType() const override;
}; };
#endif // __COLONY_H_ #endif // __COLONY_H_

View File

@ -1,11 +1,10 @@
#ifndef __FOOD_H_ #ifndef __FOOD_H_
#define __FOOD_H_ #define __FOOD_H_
#include <SFML/Graphics.hpp>
#include "world_object.hpp" #include "world_object.hpp"
#include <SFML/Graphics.hpp>
class Food : public WorldObject class Food : public WorldObject {
{
public: public:
Food(double x, double y); Food(double x, double y);
@ -15,4 +14,4 @@ public:
PheroType getPheromoneType() const override; PheroType getPheromoneType() const override;
}; };
#endif // __FOOD_H_ #endif // __FOOD_H_

View File

@ -1,15 +1,15 @@
#include <iostream>
#include <cmath> #include <cmath>
#include <vector> #include <iostream>
#include <memory> #include <memory>
#include <vector>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "pheromone_map.hpp"
#include "ant.hpp" #include "ant.hpp"
#include "colony.hpp" #include "colony.hpp"
#include "food.hpp" #include "food.hpp"
#include "pheromone.hpp" #include "pheromone.hpp"
#include "pheromone_map.hpp"
const unsigned short HEIGHT = 500; const unsigned short HEIGHT = 500;
const unsigned short WIDTH = 500; const unsigned short WIDTH = 500;
@ -22,13 +22,13 @@ int main(int argc, char* argv[]) {
settings.antialiasingLevel = 8; settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Ants", sf::Style::Close, settings); 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 t = 0.0; // Verstrichene Zeit in ms
float dt = 1.0 / FPS; // Schrittweite in ms float dt = 1.0 / FPS; // Schrittweite in ms
PheromoneMap pheromones; 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); ants.reserve(ANTCOUNT);
std::shared_ptr<Colony> colony = std::make_shared<Colony>(WIDTH / 2, HEIGHT / 2); 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); ant->addToUmwelt(foodA);
} }
// Main event loop
while (window.isOpen()) { while (window.isOpen()) {
sf::Event event{}; sf::Event event {};
while (window.pollEvent(event)) { while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) { if (event.type == sf::Event::Closed) {
@ -54,7 +55,7 @@ int main(int argc, char* argv[]) {
// Update // Update
t += dt; t += dt;
pheromones.update(); pheromones.update();
for (std::unique_ptr<Ant> const& obj: ants) { for (std::unique_ptr<Ant> const& obj : ants) {
obj->update(); obj->update();
} }
for (Pheromone& pheromone : pheromones.pheromones) { for (Pheromone& pheromone : pheromones.pheromones) {
@ -66,7 +67,7 @@ int main(int argc, char* argv[]) {
for (Pheromone& pheromone : pheromones.pheromones) { for (Pheromone& pheromone : pheromones.pheromones) {
window.draw(pheromone.appearance); 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(obj->appearance);
} }
window.draw(colony->appearance); window.draw(colony->appearance);

View File

@ -2,12 +2,12 @@
// Created by christoph on 11.04.21. // Created by christoph on 11.04.21.
// //
#include <SFML/Graphics.hpp>
#include "world_object.hpp"
#include "pheromone.hpp" #include "pheromone.hpp"
#include "world_object.hpp"
#include <SFML/Graphics.hpp>
Pheromone::Pheromone(double x, double y, PheroType type) 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) { if (type == HOME) {
appearance.setFillColor(sf::Color::Red); appearance.setFillColor(sf::Color::Red);
} else if (type == FOOD) { } else if (type == FOOD) {
@ -30,10 +30,8 @@ PheroType Pheromone::getPheromoneType() const {
void Pheromone::update() { void Pheromone::update() {
intensity = std::max(0, intensity - decay); intensity = std::max(0, intensity - decay);
appearance.setFillColor(sf::Color( appearance.setFillColor(sf::Color(
appearance.getFillColor().r, appearance.getFillColor().r,
appearance.getFillColor().g, appearance.getFillColor().g,
appearance.getFillColor().b, appearance.getFillColor().b,
intensity intensity));
));
} }

View File

@ -5,13 +5,12 @@
#ifndef ANTSIMULATOR_PHEROMONE_HPP #ifndef ANTSIMULATOR_PHEROMONE_HPP
#define ANTSIMULATOR_PHEROMONE_HPP #define ANTSIMULATOR_PHEROMONE_HPP
#include <SFML/Graphics.hpp>
#include "world_object.hpp" #include "world_object.hpp"
#include <SFML/Graphics.hpp>
const unsigned short decay = 1; const unsigned short decay = 1;
class Pheromone : public WorldObject class Pheromone : public WorldObject {
{
public: public:
unsigned short intensity = 255; unsigned short intensity = 255;
@ -22,4 +21,4 @@ public:
void update() override; void update() override;
}; };
#endif //ANTSIMULATOR_PHEROMONE_HPP #endif //ANTSIMULATOR_PHEROMONE_HPP

View File

@ -1,6 +1,6 @@
#include <iostream>
#include "pheromone_map.hpp" #include "pheromone_map.hpp"
#include "ant.hpp" #include "ant.hpp"
#include <iostream>
void PheromoneMap::place(double x, double y, PheroType type) { void PheromoneMap::place(double x, double y, PheroType type) {
pheromones.emplace_back(x, y, 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; std::vector<Pheromone> umwelt;
for (const Pheromone& pheromone : pheromones) { for (const Pheromone& pheromone : pheromones) {
if (pheromone.getPheromoneType() == type if (pheromone.getPheromoneType() == type && pheromone.distance(ant) <= radius) {
&& pheromone.distance(ant) <= radius) {
umwelt.push_back(pheromone); umwelt.push_back(pheromone);
} }
} }
@ -22,7 +21,7 @@ std::vector<Pheromone> PheromoneMap::getInVision(const Ant& ant, PheroType type,
void PheromoneMap::update() { void PheromoneMap::update() {
for (size_t i = 0; i < pheromones.size(); ++i) { for (size_t i = 0; i < pheromones.size(); ++i) {
if (pheromones[i].appearance.getFillColor().a == 0) { if (pheromones[i].appearance.getFillColor().a == 0) {
// pheromones.erase(pheromones.begin() + i); // pheromones.erase(pheromones.begin() + i);
} }
} }
} }

View File

@ -1,14 +1,13 @@
#ifndef __PHEROMONES_H_ #ifndef __PHEROMONES_H_
#define __PHEROMONES_H_ #define __PHEROMONES_H_
#include <vector>
#include <SFML/Graphics.hpp>
#include "pheromone.hpp" #include "pheromone.hpp"
#include <SFML/Graphics.hpp>
#include <vector>
class Ant; class Ant;
class PheromoneMap class PheromoneMap {
{
public: public:
std::vector<Pheromone> pheromones; std::vector<Pheromone> pheromones;

View File

@ -1,7 +1,7 @@
#include "world_object.hpp" #include "world_object.hpp"
WorldObject::WorldObject(double x, double y, unsigned short radius, sf::Color color) 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 = sf::CircleShape(radius);
appearance.setFillColor(color); appearance.setFillColor(color);
appearance.setPosition(x, y); appearance.setPosition(x, y);

View File

@ -8,11 +8,12 @@ extern const unsigned short WIDTH;
extern const unsigned short HEIGHT; extern const unsigned short HEIGHT;
enum PheroType { enum PheroType {
FOOD, HOME, NONE FOOD,
HOME,
NONE
}; };
class WorldObject class WorldObject {
{
protected: protected:
double x, y; double x, y;
const unsigned short radius; const unsigned short radius;
@ -24,15 +25,15 @@ protected:
WorldObject(double x, double y, unsigned short radius, sf::Color color); WorldObject(double x, double y, unsigned short radius, sf::Color color);
public: 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; bool collides(const WorldObject& other) const;
double distance(const WorldObject& other) const; double distance(const WorldObject& other) const;
double angle(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; virtual PheroType getPheromoneType() const = 0;
}; };
#endif // __OBJECT_H_ #endif // __OBJECT_H_