-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.java
267 lines (224 loc) · 6.76 KB
/
World.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import libs.PerlinNoiseGenerator;
import java.util.*;
enum Direction {
NORTH, EAST, SOUTH, WEST, NONE;
private static final List<Direction> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
private static final int SIZE = VALUES.size();
private static final Random RANDOM = new Random();
public static Direction random() {
return VALUES.get(RANDOM.nextInt(SIZE));
}
}
public class World {
private final int octaves = 4;
protected int size;
protected int population;
protected Being[][] land;
protected ArrayList<Being> beings;
protected float[][] hostility;
private int turn;
private double favorability;
private double fertility;
private double intelligenceFactor;
private Random gen;
private int valences;
private int gestation;
public World() {
beings = new ArrayList<Being>();
size = 100;
}
public World(int size, int population, double fertility, double favorability,
double intelligenceFactor, int
valences) {
turn = 0;
gen = new Random();
this.valences = valences;
this.size = size;
this.favorability = favorability;
this.intelligenceFactor = intelligenceFactor;
this.fertility = fertility;
this.population = population;
beings = new ArrayList<Being>();
land = new Being[size][size];
hostility = PerlinNoiseGenerator.generateSmoothNoise
(PerlinNoiseGenerator
.generateWhiteNoise(size, size), octaves);
for (int i = 0; i < population; i++) {
int x, y;
do {
x = gen.nextInt(size);
y = gen.nextInt(size);
} while(land[x][y] != null);
land[x][y] = new Being(i % valences, x, y);
beings.add(land[x][y]);
}
}
public void move(Being b, int x, int y) {
if(Math.abs(x) + Math.abs(y) > 1) {
throw new IllegalArgumentException("Beings can only move a space at a " +
"time");
}
if (x < 0 && b.x == 0) x = 0;
if (y < 0 && b.y == 0) y = 0;
if (x > 0 && b.x == size - 1) x = 0;
if (y > 0 && b.y == size - 1) y = 0;
if (land[b.x + x][b.y + y] != null) return;
land[b.x][b.y] = null;
b.x += x;
b.y += y;
land[b.x][b.y] = b;
}
public String toString() {
StringBuilder output = new StringBuilder();
for (int i = 0; i < land.length; i++) {
for (int j = 0; j < land[i].length; j++) {
if (land[i][j] == null) {
output.append("\t-\t");
} else {
output.append(land[i][j]);
}
output.append("\t");
}
output.append("\n");
}
return output.toString();
}
public double roll(double max) {
return gen.nextDouble() * max;
}
public void kill(Being b) {
b.kill();
land[b.x][b.y] = null;
}
public synchronized Being reproduce(Being b1, Being b2) {
if (turn - b1.reproduced < gestation || turn - b2.reproduced < gestation) {
return null;
} else {
b1.reproduced = turn;
b2.reproduced = turn;
}
int x, y, counter = 0;
if (gen.nextInt(2) == 0) {
x = b1.x;
y = b1.y;
} else {
x = b2.x;
y = b2.y;
}
do {
if (counter++ > 1) return null;
Direction direction = Direction.random();
switch (direction) {
case NORTH:
if (y < size - 1) y++; break;
case SOUTH:
if (y > 0) y--; break;
case EAST:
if (x < size - 1) x++; break;
case WEST:
if (x > 0) x--; break;
}
} while (land[x][y] != null);
land[x][y] = new Being(b1, b2, x, y);
return land[x][y];
}
public Direction chooseDirection(Being b) {
if (b.dead) return Direction.NONE;
float minHostility = hostility[b.x][b.y];
Direction bestDirection = Direction.NONE;
if (b.x > 0 && hostility[b.x - 1][b.y] < minHostility) {
minHostility = hostility[b.x - 1][b.y];
bestDirection = Direction.WEST;
}
if (b.x < size - 1 && hostility[b.x + 1][b.y] < minHostility) {
minHostility = hostility[b.x + 1][b.y];
bestDirection = Direction.EAST;
}
if (b.y > 0 && hostility[b.x][b.y - 1] < minHostility) {
minHostility = hostility[b.x][b.y - 1];
bestDirection = Direction.SOUTH;
}
if (b.y < size - 1 && hostility[b.x][b.y + 1] < minHostility) {
bestDirection = Direction.NORTH;
}
return bestDirection;
}
public synchronized boolean next() {
int index = 0;
if (beings.isEmpty()) return false;
ListIterator<Being> iter = beings.listIterator();
while (iter.hasNext()) {
Being b = iter.next();
if (b.dead) {
iter.remove();
continue;
}
// handle hostility
double damage = roll(hostility[b.x][b.y] * favorability);
double resilience = roll(b.strength) + roll(b.intelligence) * intelligenceFactor;
if (damage > resilience) {
kill(b);
}
// handle neighbors
Being[] neighbors = new Being[4];
neighbors[0] = b.x - 1 > 0 ? land[b.x - 1][b.y] : null;
neighbors[1] = b.x + 1 < size ? land[b.x + 1][b.y] : null;
neighbors[2] = b.y - 1 > 0 ? land[b.x][b.y - 1] : null;
neighbors[3] = b.y + 1 < size ? land[b.x][b.y + 1] : null;
for (Being neighbor : neighbors) {
if (neighbor == null) continue;
if (neighbor.valence == b.valence) {
double pregnancy;
if (neighbor.fertility > b.fertility) {
pregnancy = roll(b.fertility);
} else {
pregnancy = roll(neighbor.fertility);
}
double intelligence;
if (neighbor.intelligence > b.intelligence) {
intelligence = roll(neighbor.intelligence);
} else {
intelligence = roll(b.intelligence);
}
if (roll(1) / fertility < pregnancy + intelligence * intelligenceFactor) {
Being newBeing = reproduce(neighbor, b);
if (newBeing != null) {
iter.add(newBeing);
}
}
if (neighbor.intelligence > b.intelligence) {
b.intelligence += (neighbor.intelligence - b.intelligence) / 10;
}
} else {
if (roll(b.strength) + roll(b.intelligence) * intelligenceFactor >
roll
(neighbor
.strength) + roll(neighbor.intelligence) * intelligenceFactor) {
kill(neighbor);
} else {
kill(b);
}
}
}
Direction d = chooseDirection(b);
switch (d) {
case NORTH:
move(b, 0, 1);
break;
case SOUTH:
move(b, 0, -1);
break;
case EAST:
move(b, 1, 0);
break;
case WEST:
move(b, -1, 0);
break;
default:
break;
}
}
turn++;
return true;
}
}