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>
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();
}

View File

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

View File

@ -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_

View File

@ -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_

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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_