Skip to content

Commit 7461285

Browse files
YJack0000claude
andcommitted
docs: restore Doxygen comments and add inline documentation
Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 6fd8f8c commit 7461285

8 files changed

Lines changed: 487 additions & 49 deletions

File tree

include/core/Environment.hpp

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,122 @@
1010
#include "Organism.hpp"
1111
#include "index/ISpatialIndex.hpp"
1212

13+
/**
14+
* @brief The 2D simulation world that owns all objects and drives the tick loop.
15+
*
16+
* Environment manages a spatial index for efficient proximity queries and an
17+
* object map keyed by UUID. Each simulation tick runs three phases in order:
18+
* 1. **Interactions** -- close-range actions (eating, fighting) within size radius.
19+
* 2. **Reactions** -- movement decisions based on objects within reaction radius.
20+
* 3. **Post-iteration** -- life-span deduction, movement, and spatial-index sync.
21+
*
22+
* After the requested iterations complete, a cleanup pass removes dead organisms
23+
* and consumed food from the world.
24+
*
25+
* Both handleInteractions() and handleReactions() run single-threaded so that
26+
* Python strategy callbacks can safely acquire the GIL without deadlocks.
27+
*/
1328
class Environment {
1429
public:
30+
/**
31+
* @brief Construct an environment with the given dimensions.
32+
* @param width Horizontal extent of the world (x in [0, width]).
33+
* @param height Vertical extent of the world (y in [0, height]).
34+
* @param type Spatial-index implementation: "default" (brute-force) or "optimized" (quadtree).
35+
* @param numThreads Reserved for future multi-threaded support (currently unused).
36+
*/
1537
Environment(int width, int height, std::string type = "default", int numThreads = 1);
38+
39+
/** @brief Horizontal extent of the simulation area. */
1640
int getWidth() const { return width; }
41+
/** @brief Vertical extent of the simulation area. */
1742
int getHeight() const { return height; }
1843

44+
/**
45+
* @brief Place an organism into the environment at the given coordinates.
46+
* @param organism Shared pointer to the organism.
47+
* @param x X-coordinate (must be within bounds).
48+
* @param y Y-coordinate (must be within bounds).
49+
* @throws std::out_of_range If (x, y) is outside the environment bounds.
50+
*/
1951
void add(const std::shared_ptr<Organism>& organism, float x, float y);
52+
53+
/**
54+
* @brief Place a food item into the environment at the given coordinates.
55+
* @param food Shared pointer to the food.
56+
* @param x X-coordinate (must be within bounds).
57+
* @param y Y-coordinate (must be within bounds).
58+
* @throws std::out_of_range If (x, y) is outside the environment bounds.
59+
*/
2060
void add(const std::shared_ptr<Food>& food, float x, float y);
2161

62+
/**
63+
* @brief Remove an organism from the environment and spatial index.
64+
* @param organism The organism to remove.
65+
* @throws std::runtime_error If the organism is not found.
66+
*/
2267
void remove(const std::shared_ptr<Organism>& organism);
68+
69+
/**
70+
* @brief Remove a food item from the environment and spatial index.
71+
* @param food The food to remove.
72+
* @throws std::runtime_error If the food is not found.
73+
*/
2374
void remove(const std::shared_ptr<Food>& food);
2475

76+
/** @brief Remove all objects and reset statistics (dead organisms, food consumption). */
2577
void reset();
2678

27-
void simulateIteration(int,
79+
/**
80+
* @brief Run the simulation for a number of iterations.
81+
* @param iterations Number of ticks to simulate; stops early if the world is empty.
82+
* @param on_each_iteration Optional callback invoked after every tick (useful for rendering).
83+
*/
84+
void simulateIteration(int iterations,
2885
std::function<void(const Environment&)> on_each_iteration = nullptr);
2986

87+
/** @brief Snapshot of all living organisms currently in the environment. */
3088
std::vector<std::shared_ptr<Organism>> getAllOrganisms() const;
89+
/** @brief Snapshot of all remaining (uneaten) food items. */
3190
std::vector<std::shared_ptr<Food>> getAllFoods() const;
91+
/** @brief Snapshot of every environment object (organisms + food). */
3292
std::vector<std::shared_ptr<EnvironmentObject>> getAllObjects() const;
93+
/** @brief Organisms that have died during the simulation (accumulated across ticks). */
3394
std::vector<std::shared_ptr<Organism>> getDeadOrganisms() const;
95+
/** @brief Total number of food items consumed since the last reset(). */
3496
unsigned long getFoodConsumptionInIteration() const;
3597

3698
private:
3799
int width, height;
38-
std::string type;
100+
std::string type; ///< Spatial-index variant identifier ("default" or "optimized").
39101
std::unique_ptr<ISpatialIndex<boost::uuids::uuid>> spatialIndex;
102+
/// Maps object UUID -> shared_ptr; serves as the authoritative object store.
40103
std::unordered_map<boost::uuids::uuid, std::shared_ptr<EnvironmentObject>> objectsMapper;
41104

42-
std::vector<std::shared_ptr<Organism>> deadOrganisms;
43-
unsigned long foodConsumption = 0;
105+
std::vector<std::shared_ptr<Organism>> deadOrganisms; ///< Accumulated dead organisms.
106+
unsigned long foodConsumption = 0; ///< Eaten-food counter.
44107

45-
int numThreads = 1;
108+
int numThreads = 1; ///< Reserved for future multi-threaded tick processing.
46109

110+
/**
111+
* @brief Validate that coordinates lie within the environment bounds.
112+
* @throws std::out_of_range If out of bounds.
113+
*/
47114
void checkBounds(float x, float y) const;
115+
116+
/** @brief Synchronise organism positions with the spatial index after movement. */
48117
void updatePositionsInSpatialIndex();
118+
119+
/** @brief Run close-range interaction phase for all living organisms. */
49120
void handleInteractions();
121+
122+
/** @brief Run reaction (movement-decision) phase for all living organisms. */
50123
void handleReactions();
124+
125+
/** @brief Invoke postIteration() on every object and sync positions. */
51126
void postIteration();
127+
128+
/** @brief Remove dead organisms and eaten food from the world. */
52129
void cleanUp();
53130
};
54131

include/core/EnvironmentObject.hpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,48 @@
44
#include <boost/uuid/uuid.hpp>
55
#include <boost/uuid/uuid_generators.hpp>
66

7+
/**
8+
* @brief Base class for all objects that can exist within the simulation environment.
9+
*
10+
* Every environment object has a unique UUID and a 2D position. Subclasses include
11+
* Organism and Food. The UUID is used as the key in the spatial index and object map.
12+
*/
713
class EnvironmentObject {
814
public:
15+
/**
16+
* @brief Construct an environment object at the given coordinates.
17+
* @param x Initial x-coordinate.
18+
* @param y Initial y-coordinate.
19+
*/
920
EnvironmentObject(float x, float y)
1021
: id(boost::uuids::random_generator()()), position(std::make_pair(x, y)) {}
1122

23+
/** @brief Return the unique identifier for this object. */
1224
boost::uuids::uuid getId() const { return id; }
1325

1426
virtual ~EnvironmentObject() = default;
27+
28+
/**
29+
* @brief Called once per simulation tick after interactions and reactions.
30+
*
31+
* Subclasses override this to perform per-tick bookkeeping (e.g. life-span
32+
* deduction, movement).
33+
*/
1534
virtual void postIteration() {};
1635

17-
// [TODO] change this - very bad implementation in order to make organism
18-
// move
36+
/** @brief Get the current (x, y) position. */
1937
virtual std::pair<float, float> getPosition() const { return position; }
38+
39+
/**
40+
* @brief Set the position to new coordinates.
41+
* @param x New x-coordinate.
42+
* @param y New y-coordinate.
43+
*/
2044
virtual void setPosition(float x, float y) { position = std::make_pair(x, y); }
2145

2246
private:
23-
boost::uuids::uuid id;
24-
std::pair<float, float> position;
47+
boost::uuids::uuid id; ///< Unique identifier for spatial-index lookups.
48+
std::pair<float, float> position; ///< Current (x, y) position in the environment.
2549
};
2650

2751
#endif

include/core/Food.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,31 @@
33

44
#include "EnvironmentObject.hpp"
55

6+
/** @brief Lifecycle state of a Food item. */
67
enum class FoodState { FRESH, EATEN };
78

9+
/**
10+
* @brief A consumable food item placed in the environment.
11+
*
12+
* Food starts in the FRESH state and transitions to EATEN once an organism
13+
* consumes it. Eaten food is removed during the environment's cleanup phase.
14+
*/
815
class Food : public EnvironmentObject {
916
public:
17+
/** @brief Construct a fresh food item at origin (0,0). Position is set later via setPosition(). */
1018
Food() : EnvironmentObject(0, 0) {}
19+
20+
/** @brief Check whether this food is still available for consumption. */
1121
bool canBeEaten() { return state == FoodState::FRESH; }
22+
23+
/** @brief Mark this food as consumed. Subsequent canBeEaten() calls return false. */
1224
void eaten() { state = FoodState::EATEN; }
25+
26+
/** @brief Energy value awarded to the organism that eats this food. */
1327
int getEnergy() const { return 500; }
1428

1529
private:
16-
FoodState state = FoodState::FRESH;
30+
FoodState state = FoodState::FRESH; ///< Tracks whether the food has been consumed.
1731
};
1832

1933
#endif

include/core/Genes.hpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,49 @@
33

44
#include <functional>
55

6+
/**
7+
* @brief Encapsulates a 4-byte DNA sequence and its mutation logic.
8+
*
9+
* Each byte in the DNA array encodes a different organism trait (speed, size,
10+
* awareness, and a reserved gene). Mutation can be customised by injecting a
11+
* MutationFunction, which is also propagated to offspring during reproduction.
12+
*/
613
class Genes {
714
public:
15+
/** @brief Callable that mutates a 4-byte DNA array in place. */
816
using MutationFunction = std::function<void(char[4])>;
917

18+
/**
19+
* @brief Construct genes from a raw DNA string using the default mutation logic.
20+
* @param dnaStr Pointer to at least 4 bytes representing the gene values.
21+
*/
1022
Genes(const char *dnaStr);
23+
24+
/**
25+
* @brief Construct genes with a custom mutation function.
26+
* @param dnaStr Pointer to at least 4 bytes representing the gene values.
27+
* @param customMutationLogic Callable applied during mutate(); nullptr uses the default.
28+
*/
1129
Genes(const char *dnaStr, MutationFunction customMutationLogic);
1230

31+
/** @brief Apply the mutation function to the DNA array. */
1332
void mutate();
33+
34+
/**
35+
* @brief Retrieve a single gene value.
36+
* @param index Gene index (0 = speed, 1 = size, 2 = awareness, 3 = reserved).
37+
* @return The raw gene byte value.
38+
*/
1439
char getDNA(int index) const;
1540

1641
private:
17-
char dna[4];
18-
MutationFunction mutationLogic;
42+
char dna[4]; ///< Raw gene data; each byte maps to one trait.
43+
MutationFunction mutationLogic; ///< Mutation strategy applied during mutate().
44+
45+
/**
46+
* @brief Built-in mutation that adds a small random delta ([-3, 3]) to each gene.
47+
* @param dna The 4-byte DNA array to mutate in place.
48+
*/
1949
static void defaultMutationLogic(char dna[4]);
2050
};
2151

0 commit comments

Comments
 (0)