-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchSpawnerAnimals.java
223 lines (207 loc) · 7.93 KB
/
PatchSpawnerAnimals.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
package me.nallar.patched;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
import me.nallar.collections.LongList;
import me.nallar.collections.LongSet;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.patcher.Declare;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.profiler.Profiler;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.SpawnerAnimals;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.biome.SpawnListEntry;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.event.Event;
import net.minecraftforge.event.ForgeEventFactory;
public abstract class PatchSpawnerAnimals extends SpawnerAnimals {
private static long hash(int x, int z) {
return (((long) x) << 32) | (z & 0xffffffffL);
}
private static final int closeRange = 1;
private static final int farRange = 5;
private static final int spawnVariance = 6;
private static final int clumping = 4;
private static final int maxChunksPerPlayer = square(farRange * 2) - square(closeRange * 2);
private static int surfaceChance;
private static int gapChance;
private static int square(int a) {
return a * a;
}
// Spigot compatibility
@SuppressWarnings ("UnusedDeclaration")
public static int getEntityCount(WorldServer server, Class oClass) {
return server.countEntities(oClass);
}
private static int getPseudoRandomHeightValue(int wX, int wZ, WorldServer worldServer, boolean surface, int gapChance) {
Chunk chunk = worldServer.getChunkIfExists(wX >> 4, wZ >> 4);
if (chunk == null) {
return -1;
}
int x = wX & 15;
int z = wZ & 15;
int height = chunk.getHeightValue(x, z);
if (surface) {
return height;
}
boolean inGap = false;
int lastGap = 0;
for (int y = 1; y < height; y++) {
int blockId = chunk.getBlockID(x, y, z);
if (blockId == 0) {
if (!inGap) {
inGap = true;
if (gapChance++ % 3 == 0) {
return y;
}
lastGap = y;
}
} else {
inGap = false;
}
}
return lastGap == 0 ? height : lastGap;
}
@Declare
public static int spawnMobsQuickly(WorldServer worldServer, boolean peaceful, boolean hostile, boolean animal) {
if (worldServer.tickCount % clumping != 0) {
return 0;
}
final Profiler profiler = worldServer.theProfiler;
profiler.startSection("creatureTypes");
float loadFactor = 1 - (float) (MinecraftServer.getTickTime() / MinecraftServer.getTargetTickTime());
if (loadFactor < 0.2f || loadFactor > 1f) {
loadFactor = 0.2f;
}
float entityMultiplier = worldServer.playerEntities.size() * loadFactor; // TODO: Make this configurable
if (entityMultiplier == 0) {
profiler.endSection();
return 0;
}
boolean dayTime = worldServer.isDaytime();
float mobMultiplier = entityMultiplier * (dayTime ? 1 : 2);
Map<EnumCreatureType, Integer> requiredSpawns = new EnumMap<EnumCreatureType, Integer>(EnumCreatureType.class);
for (EnumCreatureType creatureType : EnumCreatureType.values()) {
int count = (int) ((creatureType.getPeacefulCreature() ? entityMultiplier : mobMultiplier) * creatureType.getMaxNumberOfCreature());
if (!(creatureType.getPeacefulCreature() && !peaceful || creatureType.getAnimal() && !animal || !creatureType.getPeacefulCreature() && !hostile) && count > worldServer.countEntities(creatureType.getCreatureClass())) {
requiredSpawns.put(creatureType, count);
}
}
profiler.endSection();
if (requiredSpawns.isEmpty()) {
return 0;
}
profiler.startSection("spawnableChunks");
int attemptedSpawnedMobs = 0;
LongSet closeChunks = new LongSet();
Collection<EntityPlayer> entityPlayers = worldServer.playerEntities;
LongList spawnableChunks = new LongList(entityPlayers.size() * maxChunksPerPlayer);
for (EntityPlayer entityPlayer : entityPlayers) {
int pX = entityPlayer.chunkCoordX;
int pZ = entityPlayer.chunkCoordZ;
int x = pX - closeRange;
int maxX = pX + closeRange;
int startZ = pZ - closeRange;
int maxZ = pZ + closeRange;
for (; x <= maxX; x++) {
for (int z = startZ; z <= maxZ; z++) {
closeChunks.add(hash(x, z));
}
}
}
for (EntityPlayer entityPlayer : entityPlayers) {
int pX = entityPlayer.chunkCoordX;
int pZ = entityPlayer.chunkCoordZ;
int x = pX - farRange;
int maxX = pX + farRange;
int startZ = pZ - farRange;
int maxZ = pZ + farRange;
for (; x <= maxX; x++) {
for (int z = startZ; z <= maxZ; z++) {
long hash = hash(x, z);
if (!closeChunks.contains(hash)) {
spawnableChunks.add(hash);
}
}
}
}
profiler.endStartSection("spawnMobs");
int size = spawnableChunks.size;
if (size < 1) {
return 0;
}
SpawnLoop:
for (Map.Entry<EnumCreatureType, Integer> entry : requiredSpawns.entrySet()) {
EnumCreatureType creatureType = entry.getKey();
long hash = spawnableChunks.get(worldServer.rand.nextInt(size));
int x = (int) (hash >> 32);
int z = (int) hash;
int sX = x * 16 + worldServer.rand.nextInt(16);
int sZ = z * 16 + worldServer.rand.nextInt(16);
boolean surface = creatureType.getPeacefulCreature() || (dayTime ? surfaceChance++ % 5 != 0 : surfaceChance++ % 5 == 0);
int gap = gapChance++;
int sY;
if (creatureType == EnumCreatureType.waterCreature) {
String biomeName = worldServer.getBiomeGenForCoords(sX, sZ).biomeName;
if (!"Ocean".equals(biomeName) && !"River".equals(biomeName)) {
continue;
}
sY = getPseudoRandomHeightValue(sX, sZ, worldServer, true, gap) - 2;
} else {
sY = getPseudoRandomHeightValue(sX, sZ, worldServer, surface, gap);
}
if (sY < 0) {
continue;
}
if (worldServer.getBlockMaterial(sX, sY, sZ) == creatureType.getCreatureMaterial()) {
for (int i = 0; i < ((clumping * 3) / 2); i++) {
int ssX = sX + (worldServer.rand.nextInt(spawnVariance) - spawnVariance / 2);
int ssZ = sZ + (worldServer.rand.nextInt(spawnVariance) - spawnVariance / 2);
int ssY;
if (creatureType == EnumCreatureType.waterCreature) {
ssY = sY;
} else if (creatureType == EnumCreatureType.ambient) {
ssY = worldServer.rand.nextInt(63) + 1;
} else {
ssY = getPseudoRandomHeightValue(ssX, ssZ, worldServer, surface, gap);
if (ssY == -1 ||
!worldServer.getBlockMaterial(ssX, ssY - 1, ssZ).isOpaque() ||
!Block.blocksList[worldServer.getBlockId(ssX, ssY - 1, ssZ)].canCreatureSpawn(creatureType, worldServer, ssX, ssY - 1, ssZ)) {
continue;
}
}
if (creatureType == EnumCreatureType.waterCreature || (!worldServer.getBlockMaterial(ssX, ssY - 1, ssZ).isLiquid())) {
SpawnListEntry creatureClass = worldServer.spawnRandomCreature(creatureType, ssX, ssY, ssZ);
if (creatureClass == null) {
break;
}
EntityLiving spawnedEntity;
try {
spawnedEntity = (EntityLiving) creatureClass.entityClass.getConstructor(World.class).newInstance(worldServer);
spawnedEntity.setLocationAndAngles((double) ssX, (double) ssY, (double) ssZ, worldServer.rand.nextFloat() * 360.0F, 0.0F);
Event.Result canSpawn = ForgeEventFactory.canEntitySpawn(spawnedEntity, worldServer, ssX, ssY, ssZ);
if (canSpawn == Event.Result.ALLOW || (canSpawn == Event.Result.DEFAULT && spawnedEntity.getCanSpawnHere())) {
worldServer.spawnEntityInWorld(spawnedEntity);
creatureSpecificInit(spawnedEntity, worldServer, ssX, ssY, ssZ);
}
attemptedSpawnedMobs++;
} catch (Exception e) {
Log.warning("Failed to spawn entity " + creatureClass, e);
break SpawnLoop;
}
}
}
}
if (attemptedSpawnedMobs >= 24) {
break;
}
}
profiler.endSection();
return attemptedSpawnedMobs;
}
}