Nano v1.0.0
Simulating Natural Selection
simulation.h
Go to the documentation of this file.
1#pragma once
2// clang-format off
3#include "pch_bll.h"
4#include "simulation_data.h"
5#include "statistics_store.h"
6// clang-format on
7
9{
13struct Food
14{
15 types::fVec2 pos = {0, 0};
16 bool isEaten = false;
17};
18
22enum class DirectionsDeg
23{
24 LEFT = 0,
25 UP = 90,
26 RIGHT = 180,
27 DOWN = 270,
28};
29
34{
35 ZERO_FOOD, // Entity has not found food yet. Seeking for food.
36 ONE_FOOD, // Entity has found 1 piece of food. Going home. Could take more food on the way.
37 TWO_FOOD // Entity has found 2 pieces of food. Going home. No more detours for food.
38};
39
44class Entity
45{
46 public:
47 // ^ remove me (Debug only)
49 std::vector<Food> *m_foods = nullptr;
50
51 size_t m_id;
53 size_t m_cyclesLived = 0;
54 size_t m_cycleDiedAt = 0;
55
56 types::fVec2 m_pos = {0.0f, 0.0f};
57 /* const */ float m_turnRate = 70.0f;
58 /* const */ float m_maxTurnAngle = 100.0f;
59 float m_facingAngle = 0.0f;
60 float m_turningAngle = 0.0f;
61 float m_timeSinceLastTurn = 0.0f;
62
63 Food *m_targetFood = nullptr;
64
67
69 constexpr inline static float traitPadding = 0.8f;
70
71 bool m_isAlive = true;
72 bool m_isDoneWithCycle = false;
73 bool m_shouldReproduce = false;
74
76
77 public:
78 Entity(size_t t_id, const int t_worldSize, const types::Trait &t_traits, std::vector<Food> *t_foods,
79 size_t t_cycleBornAt);
80 // Entity& operator=(const Entity&) = default;
81
82 private:
83 void update(const float elapsedTime);
84
85 std::optional<float> getAngleToClosestFoodInRange();
87
89 bool isOutOfBounds() const;
91 bool handleFoodCollision(const float elapsedTime);
92
93 void reset();
94
95 void hanldeEnergy(const float elapsedTime);
96 void walk(float elapsedTime);
97 void move(float elapsedTime);
98
99 public:
101 [[nodiscard]] const types::fVec2 &getPos() const;
102 [[nodiscard]] float getFacingAngle() const;
103
104 friend class Cycle;
105 friend class Simulation;
106};
107
112class Cycle
113{
114 std::vector<Entity> *m_entities;
115 std::vector<Entity>::iterator *m_entitiesEndIter;
116 std::span<Entity> activeEntities;
118
119 std::vector<Food> *m_foods;
120
122
123 size_t m_cycleId;
124 bool m_isCycleDone = false;
125
126 public:
127 Cycle();
128 Cycle(std::vector<Entity> *t_entities, std::vector<Entity>::iterator *t_entitiesEndIter, size_t t_worldSize,
129 std::vector<Food> *t_foods, size_t t_cycleId, size_t *t_lastEntityId);
130
131 void CycleEnd();
132 // ~Cycle();
133
134 static void reproduceEntities(std::vector<Entity> &entities, std::vector<Entity>::iterator &entitiesEndIt,
135 size_t *lastEntityId, size_t cycleId, std::vector<Food> *foods);
136
137 static void equalEntitiesDistribution(std::span<Entity> entities, size_t wallSize);
138 static void randomEntitiesDistribution(std::span<Entity> entities, size_t wallSize);
139
140 static void distributeEntities(std::span<Entity> entities, size_t wallSize);
141
142 static void handleFoodCount(std::vector<Food> &foods, int changeBy);
143 static void randomizeFoodPositions(std::span<Food> foods, size_t worldSize);
144
145 public:
146 void update(float elapsedTime);
147
148 friend class Simulation;
149};
150
157{
158 public:
160
161 std::vector<Entity> m_entities;
162 std::vector<Entity>::iterator m_entitiesEndIt;
163 std::vector<Food> m_foods;
164 size_t m_lastEntityId = 1;
165
168
169 public:
170 bool isSimulationDone = false;
171
172 private:
173 void cleanEntities();
174
175 public:
176 // Make sure that all the active entities are included. (especially the last active one)
177 [[nodiscard]] static std::span<Entity> getActiveEntities(std::vector<Entity> &entities,
178 std::vector<Entity>::iterator &iter);
179 const std::vector<Food> &getFoods() const;
180 static void repositionEntitiesIter(std::vector<Entity> &entities, std::vector<Entity>::iterator &iter);
181 const Entity *getEntityById(size_t id);
182 void setEntityTraits(size_t id, const types::Trait &traits);
183
184 public:
186 size_t getTotalAliveEntities() const;
187 size_t getTotalDiedEntities() const;
189 void saveSimulationInfo(std::optional<std::string> fileName) const;
190 void update(float elapsedTime);
191 void setFoodChange(int foodChange);
192 friend class Cycle;
193};
194} // namespace ss::bll::simulation
A class representing a single cycle in the simulation.
Definition: simulation.h:113
static void randomizeFoodPositions(std::span< Food > foods, size_t worldSize)
Redistributes the food particles on the board.
Definition: simulation.cpp:544
std::span< Entity > activeEntities
Definition: simulation.h:116
std::vector< Entity >::iterator * m_entitiesEndIter
Definition: simulation.h:115
size_t m_cycleId
Definition: simulation.h:123
size_t m_worldSize
Definition: simulation.h:121
static void handleFoodCount(std::vector< Food > &foods, int changeBy)
Definition: simulation.cpp:532
static void distributeEntities(std::span< Entity > entities, size_t wallSize)
Determines if there is enough space for equal distribution and calls the appropriate function.
Definition: simulation.cpp:520
static void randomEntitiesDistribution(std::span< Entity > entities, size_t wallSize)
Distributes entities randomly on the edges of the board.
Definition: simulation.cpp:475
static void reproduceEntities(std::vector< Entity > &entities, std::vector< Entity >::iterator &entitiesEndIt, size_t *lastEntityId, size_t cycleId, std::vector< Food > *foods)
Reproduces all entities that should reproduce with random traits inherited from their parent.
Definition: simulation.cpp:379
bool m_isCycleDone
Definition: simulation.h:124
size_t * m_lastEntityId
Definition: simulation.h:117
void update(float elapsedTime)
Updates the current cycle.
Definition: simulation.cpp:558
Cycle()
Necessary default constructor.
Definition: simulation.cpp:327
static void equalEntitiesDistribution(std::span< Entity > entities, size_t wallSize)
Distributes entities with equal spacing on the board.
Definition: simulation.cpp:417
std::vector< Entity > * m_entities
Definition: simulation.h:114
std::vector< Food > * m_foods
Definition: simulation.h:119
void CycleEnd()
Prepares the entities for the next cycle.
Definition: simulation.cpp:345
A class representing a single entity in the simulation.
Definition: simulation.h:45
void walk(float elapsedTime)
Handles special turning logic.
Definition: simulation.cpp:226
Entity(size_t t_id, const int t_worldSize, const types::Trait &t_traits, std::vector< Food > *t_foods, size_t t_cycleBornAt)
Definition: simulation.cpp:9
float getFacingAngle() const
Getter for facing angle.
Definition: simulation.cpp:317
int m_worldSize
Definition: simulation.h:48
void update(const float elapsedTime)
Update the entity.
Definition: simulation.cpp:22
size_t m_cycleDiedAt
Definition: simulation.h:54
float m_timeSinceLastTurn
Definition: simulation.h:61
float m_turningAngle
Definition: simulation.h:60
size_t m_cycleBornAt
Definition: simulation.h:52
void hanldeEnergy(const float elapsedTime)
Handles the energy depletion of the entity.
Definition: simulation.cpp:212
const types::fVec2 & getPos() const
Getter for position.
Definition: simulation.cpp:307
void reset()
Prepares the entity for the next cycle.
Definition: simulation.cpp:198
float m_energyMax
Definition: simulation.h:65
types::fVec2 m_pos
Definition: simulation.h:56
bool handleFoodCollision(const float elapsedTime)
Handles everything regarding food collision.
Definition: simulation.cpp:179
float getAngleToClosestWall()
Returns the angle to the closest wall to the entity.
Definition: simulation.cpp:122
types::EntityTarget getBrain() const
Peek into an entity's thoughts.
Definition: simulation.cpp:282
bool m_isDoneWithCycle
Definition: simulation.h:72
static constexpr float traitPadding
Definition: simulation.h:69
bool isOutOfBounds() const
Checks if the entity is outside the bounds of the board.
Definition: simulation.cpp:160
float m_maxTurnAngle
Definition: simulation.h:58
void generateNewTurningAngle()
Sets the turning angle to a new random value that's less than the max turning angle.
Definition: simulation.cpp:152
float m_turnRate
Definition: simulation.h:57
std::vector< Food > * m_foods
Definition: simulation.h:49
void move(float elapsedTime)
Moves the entity in it's facing direction.
Definition: simulation.cpp:260
Food * m_targetFood
Definition: simulation.h:63
bool m_shouldReproduce
Definition: simulation.h:73
size_t m_cyclesLived
Definition: simulation.h:53
float m_facingAngle
Definition: simulation.h:59
bool m_isAlive
Definition: simulation.h:71
types::Trait m_traits
Definition: simulation.h:68
size_t m_id
Definition: simulation.h:51
std::optional< float > getAngleToClosestFoodInRange()
Returns the angle to the closest food in range (if there is any).
Definition: simulation.cpp:85
void clampEntityPosToBoard()
Limits the position of the entity to the board.
Definition: simulation.cpp:170
EntityFoodStage m_foodStage
Definition: simulation.h:75
float m_currentEnergy
Definition: simulation.h:66
A class representing the simulation.
Definition: simulation.h:157
std::vector< Entity >::iterator m_entitiesEndIt
Definition: simulation.h:162
Simulation(types::SimulationInfo t_simInfo)
Constructor for the Simulation class.
Definition: simulation.cpp:688
types::Trait getAvgTraits() const
Calculates the average traits of all entities for the whole simulation.
Definition: simulation.cpp:743
bool isSimulationDone
Definition: simulation.h:170
Cycle m_currentCycle
Definition: simulation.h:167
std::vector< Entity > m_entities
Definition: simulation.h:161
void update(float elapsedTime)
Updates the simulation.
Definition: simulation.cpp:808
size_t m_currentCycle_n
Definition: simulation.h:166
void setFoodChange(int foodChange)
Definition: simulation.cpp:836
void setEntityTraits(size_t id, const types::Trait &traits)
Sets the traits of an entity.
Definition: simulation.cpp:671
const std::vector< Food > & getFoods() const
Getter for all the food particles.
Definition: simulation.cpp:626
static std::span< Entity > getActiveEntities(std::vector< Entity > &entities, std::vector< Entity >::iterator &iter)
Returns only the alive entities in the current cycle.
Definition: simulation.cpp:615
void cleanEntities()
Cleans up unused entities after the simulation has finished.
Definition: simulation.cpp:582
types::SimulationInfo m_simInfo
Definition: simulation.h:159
size_t m_lastEntityId
Definition: simulation.h:164
static void repositionEntitiesIter(std::vector< Entity > &entities, std::vector< Entity >::iterator &iter)
Sets an appropriate position for the iterator used to mark the point between the alive and dead entit...
Definition: simulation.cpp:635
size_t getTotalDiedEntities() const
Calculates the amount of the total died entities.
Definition: simulation.cpp:723
const Entity * getEntityById(size_t id)
Getter for entity by id.
Definition: simulation.cpp:653
void saveSimulationInfo(std::optional< std::string > fileName) const
Generates and attempts to save all of the information gathered throughout the simulation.
Definition: simulation.cpp:766
size_t getTotalAliveEntities() const
Calculates the amount of the total alive entities.
Definition: simulation.cpp:713
std::vector< Food > m_foods
Definition: simulation.h:163
Definition: simulation.h:9
DirectionsDeg
An enumeration type describing the directions of the board.
Definition: simulation.h:23
EntityFoodStage
An enumeration type describing the three states of an Entity.
Definition: simulation.h:34
EntityTarget
Definition: simulation_data.h:9
An enumeration type representing a single food particle.
Definition: simulation.h:14
bool isEaten
Definition: simulation.h:16
types::fVec2 pos
Definition: simulation.h:15
A struct holding the data for a simulation.
Definition: simulation_data.h:35
A struct holding the data for a trait.
Definition: simulation_data.h:26
Definition: simulation_data.h:17