-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.pde
218 lines (183 loc) · 6.05 KB
/
World.pde
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
import java.util.*;
public static class World {
public static float matingDivisor;
public static float offspringDivisor;
public static boolean canJump;
public static boolean dieOnTrapped;
public static float trappedDivisor;
public static float zombifyDivisor;
public static float zombifiedDivisor;
public static float zombieBrains;
public static float spawnChance;
public static float zombieChance;
public static float dragonChance;
public static float fireDivisor;
public static float healDivisor;
public static int turn = 0;
public static int total = 0;
public static int alive = 0;
public static int zombies = 0;
public static int dragons = 0;
public static Spark[] population;
public static ArrayList<Spark> living;
public static void reset() {
matingDivisor = 4;
offspringDivisor = 2;
canJump = false;
dieOnTrapped = true;
trappedDivisor = 6;
zombifyDivisor = 1.2;
zombifiedDivisor = 1.2;
zombieBrains = 200;
spawnChance = 0.2;
zombieChance = 0.5;
dragonChance = 0.05;
fireDivisor = 15;
healDivisor = 20;
print("RESET! ");
printWorldSettings();
}
public static void setup() {
reset();
population = new Spark[Config.WIDTH*Config.HEIGHT];
for (int i=0; i<population.length; i++) {
population[i] = null;
}
living = new ArrayList<Spark>();
}
public static boolean add(Spark c) {
if (population[c.pos] == null) {
population[c.pos] = c;
alive++;
if (c instanceof Zombie) zombies++;
if (c instanceof Dragon) dragons++;
living.add(c);
return true;
}
throw new RuntimeException("Couldn't add " + c.number + " because " + population[c.pos].number + " occupies that spot.");
}
public static boolean kill(Spark c) {
if (c == null) {
println("Cannot kill null");
return false;
}
if (c.isDead) {
// println(c.number + " is already dead.");
// throw new RuntimeException();
return true;
}
if (population[c.pos] != c) {
int pos = find(c);
if (pos >= 0) {
println("Corrected " + c.number + " from " + c.pos + " to " + c.pos + ", will kill.");
c.pos = pos;
}
else {
println("Cannot kill " + c.number + " because is not where expected: " + c.pos + "=" + population[c.pos]);
return false;
}
}
c.dad = null;
c.mom = null;
population[c.pos] = null;
living.remove(c);
for (Spark child : population) {
if (child != null) {
if (child.dad == c) child.dad = null;
if (child.mom == c) child.mom = null;
}
}
c.isDead = true;
alive--;
if (c instanceof Zombie) zombies--;
if (c instanceof Dragon) dragons--;
//println("Killed " + c.number + " now " + alive + " alive");
return true;
}
public static int find(Spark target) {
for (int i=0; i<population.length; i++) {
if (population[i] == target) return i;
}
return -1;
}
public static int bound(int i) {
while(i>=population.length) i-= population.length;
while(i<0) i+= population.length;
return i;
}
public static Spark get(int i) {
return population[World.bound(i)];
}
public static int move(Spark c, int delta) {
int newpos = bound(c.pos+delta);
if (get(newpos) == null) {
population[c.pos] = null;
population[newpos] = c;
return newpos;
}
throw new RuntimeException("Cannot move " + c.number + " to " + newpos + " because " + population[newpos].number + " is there");
}
public static void swap(int l, int r) {
Spark tmp = population[l];
population[l] = population[r];
population[r] = tmp;
}
public static void act() {
Collections.shuffle(living,new Random(System.nanoTime()));
for (int i=0; i<living.size(); i++) {
Spark c = living.get(i);
if (!c.isDead) c.act();
}
turn++;
if (turn % 100 == 0)
println("Turn #"+turn + " " + total+" born " + alive+" alive " + zombies + " zombies " + dragons + " dragons");
}
public static void draw() {
for (Spark c : population)
if (c != null && !c.isDead)
c.draw();
}
public static int makeNumber() {
return total++;
}
public static void despawn() {
Spark c = living.get(0);
living.remove(0);
World.kill(c);
}
public static float random(float n) {
return (float)(Math.random() * n);
}
public static void entropy() {
World.matingDivisor = World.matingDivisor * (0.9 + random(0.2));
World.offspringDivisor = World.offspringDivisor * (0.9 + random(0.2));
World.canJump = random(1) < 0.2 ? !World.canJump : World.canJump;
World.dieOnTrapped = random(1) < 0.2 ? !World.dieOnTrapped : World.dieOnTrapped;
World.trappedDivisor = World.trappedDivisor * (0.9 + random(0.2));
World.zombifyDivisor = World.zombifyDivisor * (0.9 + random(0.2));
World.zombifiedDivisor = World.zombifiedDivisor * (0.9 + random(0.2));
World.zombieChance = World.zombieChance * (0.9 + random(0.2));
World.zombieBrains = World.zombieBrains * (0.9 + random(0.2));
World.dragonChance = World.dragonChance * (0.9 + random(0.2));
World.fireDivisor = World.fireDivisor * (0.9 + random(0.2));
World.healDivisor = World.healDivisor * (0.9 + random(0.2));
World.fireDivisor = World.fireDivisor * (0.9 + random(0.2));
print("Entropy! ");
printWorldSettings();
}
public static void printWorldSettings() {
println("matingDivisor=" + World.matingDivisor +
" offSpringDivisor=" + World.offspringDivisor +
" canJump=" + World.canJump +
" dieOnTrapped=" + World.dieOnTrapped +
" trappedDivisor=" + World.trappedDivisor +
" zombifyDivisor=" + World.zombifyDivisor +
" zombifiedDivisor=" + World.zombifiedDivisor +
" zombieChance=" + World.zombieChance +
" zombieBrains=" + World.zombieBrains +
" dragonChance=" + World.dragonChance +
" fireDivisor=" + World.fireDivisor +
" healDivisor=" + World.healDivisor
);
}
}