-
Notifications
You must be signed in to change notification settings - Fork 63
/
MixinFogHandler.java
156 lines (128 loc) · 6.47 KB
/
MixinFogHandler.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
package com.mitchej123.hodgepodge.mixins.biomesoplenty;
import biomesoplenty.client.fog.FogHandler;
import biomesoplenty.client.fog.IBiomeFog;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import java.util.concurrent.atomic.AtomicInteger;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.MathHelper;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.client.event.EntityViewRenderEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
@Mixin(FogHandler.class)
public class MixinFogHandler {
@Shadow(remap = false)
private static double fogX, fogZ;
@Shadow(remap = false)
private static boolean fogInit;
@Shadow(remap = false)
private static float fogFarPlaneDistance;
private static float farPlaneDistanceScale = 0.75f;
private static float farPlaneDistanceM;
/**
* @author glowredman, TataTawa
* @reason It is not useful to calculate 1600 times per frame only to get newest {@link #fogX} and {@link #fogZ}
*/
@Overwrite(remap = false)
@SubscribeEvent
public void onRenderFog(EntityViewRenderEvent.RenderFogEvent event) {
EntityLivingBase entity = event.entity;
int playerX = MathHelper.floor_double(entity.posX);
int playerZ = MathHelper.floor_double(entity.posZ);
if (playerX == fogX && playerZ == fogZ && fogInit) renderFog(event.fogMode, fogFarPlaneDistance, 0.75f);
farPlaneDistanceM = event.farPlaneDistance;
if (ticks.get() < -50) {
new ClientTickThread().start();
ticks.set(0);
}
if (Math.random() < 0.1) ticks.incrementAndGet();
renderFog(event.fogMode, fogFarPlaneDistance, farPlaneDistanceScale);
}
private static AtomicInteger ticks = new AtomicInteger(-100);
private class ClientTickThread extends Thread {
@Override
public void run() {
while (true) {
if (Minecraft.getMinecraft().theWorld == null) {
ticks.set(0);
try {
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
if (ticks.get() > 0) {
try {
int distance = 20;
float fpDistanceBiomeFog = 0.0f;
float weightBiomeFog = 0.0f;
for (int x = -distance; x <= distance; x++) {
for (int z = -distance; z <= distance; z++) {
EntityClientPlayerMP player = Minecraft.getMinecraft().thePlayer;
int playerX = MathHelper.floor_double(player.posX);
int playerY = MathHelper.floor_double(player.posY);
int playerZ = MathHelper.floor_double(player.posZ);
BiomeGenBase biome = Minecraft.getMinecraft()
.theWorld
.getBiomeGenForCoords(playerX + x, playerZ + z);
if (biome instanceof IBiomeFog) {
float distancePart =
((IBiomeFog) biome).getFogDensity(playerX + x, playerY, playerZ + z);
float weightPart = 1.0f;
if (x == -distance) {
double xDiff = 1.0 - (player.posX - playerX);
distancePart *= xDiff;
weightPart *= xDiff;
} else if (x == distance) {
double xDiff = player.posX - playerX;
distancePart *= xDiff;
weightPart *= xDiff;
}
if (z == -distance) {
double zDiff = 1.0 - (player.posZ - playerZ);
distancePart *= zDiff;
weightPart *= zDiff;
} else if (z == distance) {
double zDiff = player.posZ - playerZ;
distancePart *= zDiff;
weightPart *= zDiff;
}
fpDistanceBiomeFog += distancePart;
weightBiomeFog += weightPart;
}
}
}
float weightMixed = distance * distance * 4.0f;
float weightDefault = weightMixed - weightBiomeFog;
float fpDistanceBiomeFogAvg =
weightBiomeFog == 0.0f ? 0.0f : fpDistanceBiomeFog / weightBiomeFog;
float farPlaneDistance =
(fpDistanceBiomeFog * 240.0f + farPlaneDistanceM * weightDefault) / weightMixed;
float farPlaneDistanceScaleBiome =
(0.1f * (1.0f - fpDistanceBiomeFogAvg) + 0.75f * fpDistanceBiomeFogAvg);
fogX = Minecraft.getMinecraft().thePlayer.posX;
fogZ = Minecraft.getMinecraft().thePlayer.posZ;
farPlaneDistanceScale =
(farPlaneDistanceScaleBiome * weightBiomeFog + 0.75f * weightDefault) / weightMixed;
fogFarPlaneDistance = Math.min(farPlaneDistance, farPlaneDistanceM);
fogInit = true;
} catch (Exception e) {
e.printStackTrace();
}
ticks.decrementAndGet();
} else {
try {
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
@Shadow(remap = false)
private static void renderFog(int fogMode, float farPlaneDistance, float farPlaneDistanceScale) {}
}