forked from lastleon/EvolutionSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathField.pde
More file actions
146 lines (132 loc) · 4.06 KB
/
Field.pde
File metadata and controls
146 lines (132 loc) · 4.06 KB
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
class Field {
//// Positionswerte
float posX;
float posY;
float fieldWidth;
float noiseHeight;
int arrayPosX;
int arrayPosY;
//// Energie- & Wachstumswerte
final public static float maxOverallEnergy = 200;
float energyValue = 0;
float maxEnergyValue;
float regenerationrate;
float maxRegenerationrate = maxOverallEnergy/500;
float[] influencingValues;
boolean influenceable;
// h: noise Höhe, fW: Feldbreite, aX,aY: array Position
Field(float x, float y, float h, float fW, int aX, int aY) {
posX = x;
posY = y;
noiseHeight = h;
fieldWidth = fW;
arrayPosX = aX;
arrayPosY = aY;
influencingValues = new float[4];
maxEnergyValue = maxOverallEnergy;
if (this.isLand()) {
influenceable = true;
} else {
influenceable = false;
}
}
// Wachstumsalgorithmus
public void grow() {
if (noiseHeight>oceanLevel) {
if (influenceable) {
regenerationrate = 0;
float rest = maxRegenerationrate;
influencingValues = sort(influencingValues);
for (int i = 3; i >= 0; i--) {
if (influencingValues[i] > random(0.5, 0.9)) {
regenerationrate += influencingValues[i] * rest * 0.9;
}
rest = maxRegenerationrate - regenerationrate;
}
}
regenerationrate *= sq(oceanLevel/noiseHeight);
if (regenerationrate>maxRegenerationrate)regenerationrate = maxRegenerationrate;
energyValue += regenerationrate;
if (energyValue > maxEnergyValue)energyValue = maxEnergyValue;
} else energyValue = 0;
}
public boolean isLand() {
return noiseHeight>oceanLevel;
}
public int isLandInt() {
if (noiseHeight>oceanLevel) {
return 1;
} else return 0;
}
public void drawField() {
noStroke();
if (noiseHeight>oceanLevel) {
fill(map(energyValue, 0, maxEnergyValue+1, 255, 80), map(energyValue, 0, maxEnergyValue+1, 210, 140), 20);
} else {
fill(0, 0, map(noiseHeight, 0, oceanLevel, 0, 140));
if (energyValue > 0) {
energyValue = 0;
}
}
rect(posX, posY, fieldWidth, fieldWidth);
}
public void setEnergy(int x) {
energyValue = x;
}
// getter(bisher)
public float getEnergy() {
return energyValue;
}
public float getMaxEnergy() {
return maxEnergyValue;
}
public float getGrown() {
return energyValue/maxOverallEnergy;
}
// Wachstumsalgorithmus
public void influenceByWater() {
if (isLand()) {
boolean water = false;
if (arrayPosX > 0) water = !map.getFieldInArray(arrayPosX-1, arrayPosY).isLand();
if (!water && arrayPosY > 0) water = !map.getFieldInArray(arrayPosX, arrayPosY-1).isLand();
if (!water && arrayPosX < worldSize -1 ) water = !map.getFieldInArray(arrayPosX+1, arrayPosY).isLand();
if (!water && arrayPosY < worldSize -1) water = !map.getFieldInArray(arrayPosX, arrayPosY+1).isLand();
if (water) {
regenerationrate = maxRegenerationrate;
influenceable = false;
} else {
regenerationrate = 0;
influenceable = true;
}
}
}
// Wachstumsalgorithmus
public void influenceNeighbours() {
if (isLand()) {
if (arrayPosX > 0) {
Field f = map.getFieldInArray(arrayPosX-1, arrayPosY);
if (f.influenceable) {
f.influencingValues[0] = getGrown();
}
}
if (arrayPosY > 0) {
Field f = map.getFieldInArray(arrayPosX, arrayPosY-1);
if (f.influenceable) {
f.influencingValues[1] = getGrown();
}
}
if (arrayPosX < worldSize -1) {
Field f = map.getFieldInArray(arrayPosX+1, arrayPosY);
if (f.influenceable) {
f.influencingValues[2] = getGrown();
}
}
if (arrayPosY < worldSize -1) {
Field f = map.getFieldInArray(arrayPosX, arrayPosY+1);
if (f.influenceable) {
f.influencingValues[3] = getGrown();
}
}
}
}
}