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

@ -78,7 +78,6 @@ 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
@ -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,14 +1,14 @@
#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]
@ -17,8 +17,7 @@ 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;

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

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

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;
@ -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,9 +2,9 @@
// 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) {
@ -33,7 +33,5 @@ void Pheromone::update() {
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;

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

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