-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecautomata.hpp
160 lines (123 loc) · 4.72 KB
/
ecautomata.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef EC_AUTOMATA_HPP
#define EC_AUTOMATA_HPP
#include "./util/grid.hpp"
#include "./util/seeder.hpp"
#include <map>
#include <random>
#include <set>
struct NeighborSpecification {
int start;
int end;
NeighborSpecification(int start = 0, int end = 0) {
this->start = start;
this->end = end;
};
};
// Given a state maps to a number of neighboring cell states.
typedef std::map<unsigned int, unsigned int> NeighboringCellStateCount;
template <typename T = int>
struct BasePosition {
T top;
T topLeft;
T topRight;
T left;
T right;
T bottom;
T bottomLeft;
T bottomRight;
};
typedef BasePosition<int> NeighborhoodPositionOffset;
/*
This object is used to indicate what a "neighborhood" is to be considered in your automaton.
For example: A neighbor on the top is anything between the next cell and 5 cells upwards.
*/
typedef BasePosition<NeighborSpecification> NeighborhoodPosition;
/*
Abstract struct that contains an overridable "apply" method for applying custom rulesets.
@template @typedef T = Dynamic dependency that MAY be needed in order to smoothly implement the apply method.
*/
template <typename T>
class CustomGridRuleset {
public:
CustomGridRuleset<T>() {
this->dependency = nullptr;
this->distribution = nullptr;
};
/*
Injects an address to a single dependency into the object.
*/
void inject(T *depen) {
this->dependency = depen;
};
/*
It may be necessary to set a INT distribution when dealing with percent and chance.
This I believe to be something common in the implementation of rules, so if we need to set a range for a given number
we can simply pass a distribution into this setter.
*/
void setDistribution(std::uniform_int_distribution<int> *distribution) {
this->distribution = distribution;
};
/*
Apply is a method used to "apply" different rules to the grid. The user is provided 3 parameters, which be filled
when the "pass" method is called.
@param current - The cell in the LAST updated grid.
@param update - The cell that is meant to be updated within the NEW grid.
@param stateCount - This is a map that uses the given provided states as keys, and the values are the computed
neighbors that contain that state.
*/
virtual void apply(int currentCell, int *cellToUpdate, NeighboringCellStateCount &stateCount) = 0;
protected:
T *dependency;
std::uniform_int_distribution<int> *distribution;
};
/*
ECAutomata is a extendable framework meant to be used by no one other than me (though others can lol). Its supposed to
speed the development of two dimensional cellular automata by offering ways to initialize the grid, set different states,
and create new functionality without all the annoying boilerplate.
*/
class ECAutomata {
public:
void virtual execute() = 0;
void initGrid(int rowSize, int colSize, unsigned int popValue = 0);
/*
Adjustes grid and states based on a scale factor.
*/
void setScaleFactor(float rfactor, float cfactor = 1.0f, std::set<unsigned int> *rareStateSet = nullptr);
protected:
Grid grid;
Seeder seeder;
NeighboringCellStateCount neighborStateCountMap;
void setState(unsigned int state);
/*
Sets the object that contains information on what a neighborhood should be considered.
*/
void setNeighborhoodPosition(NeighborhoodPosition np);
/*
Method that performs a pass through the grid
*/
template <typename T> void pass(CustomGridRuleset<T> &ruleset) {
std::vector<std::vector<int>> *freshGrid = this->grid.getContent();
for (int row = 0; row < this->grid.getRows(); ++row) {
for (int col = 0; col < this->grid.getCols(); ++col) {
this->computeNeighbors({row, col}, this->neighborStateCountMap);
int current = this->grid.getContent()->at(row).at(col);
int *update = &freshGrid->at(row).at(col);
ruleset.apply(current, update, this->neighborStateCountMap);
this->resetNeighborStateMap();
};
};
this->grid.setContent(*freshGrid);
};
private:
NeighborhoodPosition neighborhoodPosition;
void resetNeighborStateMap();
/*
Method that calculates the states of neighboring cells, from the provided row and col position in the grid.
@param pos - Positional data of the current cell in the grid.
@param neighborStateCount - Unordered map that contains a count of the nearby neighbor cell states.
*/
void computeNeighbors(Position pos, NeighboringCellStateCount &neighborStateCount);
int computePositionalOffset(NeighborSpecification &spec);
NeighborhoodPositionOffset computeNeighborhoodOffsets();
};
#endif