summaryrefslogtreecommitdiff
path: root/ihl/enviroment/LightSource.java
blob: 55f7c6cda8a67026d4f548905142a4c901c834bf (plain)
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
package ihl.enviroment;

import java.util.BitSet;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import ihl.ClientProxy;
import ihl.IHLMod;
import ihl.utils.IHLMathUtils;
import net.minecraft.world.World;

@SideOnly(value = Side.CLIENT)
public class LightSource {
	private final int centerX;
	private final int centerY;
	private final int centerZ;
	public int fromX;
	public int fromY;
	public int fromZ;
	public int toX;
	public int toY;
	public int toZ;
	private final int red;
	private final int green;
	private final int blue;
	private final int power;
	public final BitSet illuminatedBlocks = new BitSet();

	public LightSource(int centerX1, int centerY1, int centerZ1, int red1, int green1, int blue1, int power1) {
		centerX = centerX1;
		centerY = centerY1;
		centerZ = centerZ1;
		red = red1;
		green = green1;
		blue = blue1;
		power = power1;
	}

	public void setBorders(int fromX1, int fromY1, int fromZ1, int toX1, int toY1, int toZ1) {
		fromX = fromX1;
		fromY = fromY1;
		fromZ = fromZ1;
		toX = toX1;
		toY = toY1;
		toZ = toZ1;
	}

	public boolean isBlockIlluminated(int x, int y, int z) {
		if (x < fromX || x > toX || y < fromY || y > toY || z < fromZ || z > toZ) {
			return false;
		} else {
			int rx = x - centerX;
			int ry = y - centerY;
			int rz = z - centerZ;
			int l = ((ClientProxy) IHLMod.proxy).getLightHandler().encodeXYZ(rx, ry, rz);
			return illuminatedBlocks.get(l);
		}
	}

	public int[] getLightValue(int x, int y, int z, int[] normal) {
		int dx = centerX - x;
		int dy = centerY - y;
		int dz = centerZ - z;
		int d = dx * dx + dy * dy + dz * dz;
		if (d == 0) {
			return new int[] { 0xf0, this.red, this.blue, this.green };
		}
		if (normal[0] + normal[1] + normal[2] == 0) {
			dx = dx < 0 ? -dx : dx;
			dy = dy < 0 ? -dy : dy;
			dz = dz < 0 ? -dz : dz;
		} else {
			dx = normal[0] * dx;
			dy = normal[1] * dy;
			dz = normal[2] * dz;
			dx = dx > 0 ? dx : 0;
			dy = dy > 0 ? dy : 0;
			dz = dz > 0 ? dz : 0;
		}
		int r = power * (dx + dy + dz) / d;
		r = r<0?0:r;
		int brightness = r > 15 ? 15 : r;
		return new int[] { brightness << 4, this.red*r, this.green*r, this.blue*r};
	}

	@Override
	public boolean equals(Object o) {
		if (o instanceof LightSource) {
			LightSource otherLS = (LightSource) o;
			return this.centerX == otherLS.centerX && this.centerY == otherLS.centerY && this.centerZ == otherLS.centerZ
					&& this.fromX == otherLS.fromX && this.fromY == otherLS.fromY && this.fromZ == otherLS.fromZ
					&& this.toX == otherLS.toX && this.toY == otherLS.toY && this.toZ == otherLS.toZ;
		}
		return false;
	}

	public void provideLight(World world, int x, int y, int z) {
		int dx = x-centerX;//100 10 20
		int dy = y-centerY;
		int dz = z-centerZ;
		int sqd = dx*dx+dy*dy+dz*dz;
		float d = IHLMathUtils.sqrt(sqd);
		float dx1 = dx/d;
		float dy1 = dy/d;
		float dz1 = dz/d;
		float x1 = x+0.5f;
		float y1 = y+0.5f;
		float z1 = z+0.5f;
		for(int i=0;i<64;i++){
			x1+=dx1;
			y1+=dy1;
			z1+=dz1;
			int absX = (int)x1;
			int absY = (int)y1;
			int absZ = (int)z1;
			int ddx = absX-centerX;
			int ddy = absY-centerY;
			int ddz = absZ-centerZ;
			this.illuminatedBlocks.set(((ClientProxy) IHLMod.proxy).getLightHandler().encodeXYZ(ddx, ddy, ddz));
			if (absX < fromX) {
				fromX = absX;
			} else if (absY < fromY) {
				fromY = absY;
			} else if (absZ < fromZ) {
				fromZ = absZ;
			} else if (absX > toX) {
				toX = absX;
			} else if (absY > toY) {
				toY = absY;
			} else if (absZ > toZ) {
				toZ = absZ;
			}
			if(world.getBlockLightOpacity(absX, absY, absZ)>192) {
				break;
			}
		}
	}
	
	public void castShadow(World world, int x, int y, int z) {
		int dx = x-centerX;
		int dy = y-centerY;
		int dz = z-centerZ;
		int sqd = dx*dx+dy*dy+dz*dz;
		float d = IHLMathUtils.sqrt(sqd);
		float dx1 = dx/d;
		float dy1 = dy/d;
		float dz1 = dz/d;
		float x1 = x+0.5f;
		float y1 = y+0.5f;
		float z1 = z+0.5f;
		x1+=dx1;
		y1+=dy1;
		z1+=dz1;
		for(int i=0;i<64;i++){
			x1+=dx1;
			y1+=dy1;
			z1+=dz1;
			int absX = (int)x1;
			int absY = (int)y1;
			int absZ = (int)z1;
			int ddx = absX-centerX;
			int ddy = absY-centerY;
			int ddz = absZ-centerZ;
			this.illuminatedBlocks.clear(((ClientProxy) IHLMod.proxy).getLightHandler().encodeXYZ(ddx, ddy, ddz));
			if (absX < fromX) {
				fromX = absX;
			} else if (absY < fromY) {
				fromY = absY;
			} else if (absZ < fromZ) {
				fromZ = absZ;
			} else if (absX > toX) {
				toX = absX;
			} else if (absY > toY) {
				toY = absY;
			} else if (absZ > toZ) {
				toZ = absZ;
			}
			if(world.getBlockLightOpacity(absX, absY, absZ)>192) {
				break;
			}
		}
	}
}