diff --git a/.clang-format b/.clang-format new file mode 100755 index 0000000..6e1c5b2 --- /dev/null +++ b/.clang-format @@ -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 + +... diff --git a/src/ant.cpp b/src/ant.cpp index ac0aa64..ab8832b 100644 --- a/src/ant.cpp +++ b/src/ant.cpp @@ -6,19 +6,19 @@ #include 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 const& obj: umwelt) { + for (std::shared_ptr const& obj : umwelt) { if (obj->collides(*this)) { pheromone_type = obj->getPheromoneType(); } diff --git a/src/ant.hpp b/src/ant.hpp index c1aa58d..9759cc3 100644 --- a/src/ant.hpp +++ b/src/ant.hpp @@ -1,29 +1,28 @@ #ifndef H_ANT #define H_ANT +#include #include #include -#include -#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> umwelt; @@ -41,7 +40,7 @@ public: void move(); void updateAppearance(); void updatePheromones(); - void dropPheromone(); // red + void dropPheromone(); // red void updateUmwelt(); }; diff --git a/src/colony.hpp b/src/colony.hpp index 049684c..526acfa 100644 --- a/src/colony.hpp +++ b/src/colony.hpp @@ -1,11 +1,10 @@ #ifndef __COLONY_H_ #define __COLONY_H_ -#include #include "world_object.hpp" +#include -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_ diff --git a/src/food.hpp b/src/food.hpp index 4cde2be..8c1822f 100644 --- a/src/food.hpp +++ b/src/food.hpp @@ -1,11 +1,10 @@ #ifndef __FOOD_H_ #define __FOOD_H_ -#include #include "world_object.hpp" +#include -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_ diff --git a/src/main.cpp b/src/main.cpp index 63372bc..925a815 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,15 @@ -#include #include -#include +#include #include +#include #include -#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> ants; // Use pointer bc we can't instatiate abstract classes + std::vector> ants; // Use pointer bc we can't instatiate abstract classes ants.reserve(ANTCOUNT); std::shared_ptr colony = std::make_shared(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 const& obj: ants) { + for (std::unique_ptr 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 const& obj: ants) { + for (std::unique_ptr const& obj : ants) { window.draw(obj->appearance); } window.draw(colony->appearance); diff --git a/src/pheromone.cpp b/src/pheromone.cpp index 597a39e..e4f183e 100644 --- a/src/pheromone.cpp +++ b/src/pheromone.cpp @@ -2,12 +2,12 @@ // Created by christoph on 11.04.21. // -#include -#include "world_object.hpp" #include "pheromone.hpp" +#include "world_object.hpp" +#include 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)); } - diff --git a/src/pheromone.hpp b/src/pheromone.hpp index 7b04524..a75c07a 100644 --- a/src/pheromone.hpp +++ b/src/pheromone.hpp @@ -5,13 +5,12 @@ #ifndef ANTSIMULATOR_PHEROMONE_HPP #define ANTSIMULATOR_PHEROMONE_HPP -#include #include "world_object.hpp" +#include 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 diff --git a/src/pheromone_map.cpp b/src/pheromone_map.cpp index 0ae349a..b3dae78 100644 --- a/src/pheromone_map.cpp +++ b/src/pheromone_map.cpp @@ -1,6 +1,6 @@ -#include #include "pheromone_map.hpp" #include "ant.hpp" +#include void PheromoneMap::place(double x, double y, PheroType type) { pheromones.emplace_back(x, y, type); @@ -10,8 +10,7 @@ std::vector PheromoneMap::getInVision(const Ant& ant, PheroType type, std::vector 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 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); } } } diff --git a/src/pheromone_map.hpp b/src/pheromone_map.hpp index d8918c2..590f8c7 100644 --- a/src/pheromone_map.hpp +++ b/src/pheromone_map.hpp @@ -1,14 +1,13 @@ #ifndef __PHEROMONES_H_ #define __PHEROMONES_H_ -#include -#include #include "pheromone.hpp" +#include +#include class Ant; -class PheromoneMap -{ +class PheromoneMap { public: std::vector pheromones; diff --git a/src/world_object.cpp b/src/world_object.cpp index 97a8fe3..d1e1a0f 100644 --- a/src/world_object.cpp +++ b/src/world_object.cpp @@ -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); diff --git a/src/world_object.hpp b/src/world_object.hpp index 54a7b68..8766ce6 100644 --- a/src/world_object.hpp +++ b/src/world_object.hpp @@ -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_