summaryrefslogtreecommitdiff
path: root/ihl/enviroment/SpotlightTileEntity.java
blob: ab18ebede2817577550dfac404b0647af14cb54c (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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package ihl.enviroment;
import ic2.api.network.INetworkTileEntityEventListener;
import ic2.core.IC2;
import ihl.IHLModInfo;
import ihl.handpump.XYZ;
import ihl.utils.IHLUtils;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.Vec3;
import net.minecraftforge.common.util.ForgeDirection;

public class SpotlightTileEntity extends LightBulbTileEntity implements INetworkTileEntityEventListener
{
	Vec3 directionVector=Vec3.createVectorHelper(1, 0, 0);
	private static final int maxLightRange=128;
	private final Set<XYZ> xyzGlowningAir = new HashSet<XYZ>();
	private static final Set<Vec3> lightSphereVectors = new HashSet<Vec3>();
	public float rotationPitch=0f;
	public float rotationYaw=0f;
	public float prevRotationPitch=0f;
	public float prevRotationYaw=0f;
	boolean needLightTargetUpdate=false;
	
	public static void createLightSphereVectors()
	{
		for(double y=-1d;y<=1d;y+=0.2d)
		{
			double sqr = 1-y*y;
			double x,z;
			for(double alpha=0;alpha<Math.PI*2;alpha+=Math.PI*2d/(sqr*16d+0.1d))
			{
				x=Math.cos(alpha)*sqr;
				z=Math.sin(alpha)*sqr;
				lightSphereVectors.add(Vec3.createVectorHelper(x*0.5d, y*0.5d, z*0.5d));
			}
		}
	}
	
    @Override
	public void readFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readFromNBT(nbttagcompound);
        directionVector.xCoord = nbttagcompound.getDouble("directionVector.xCoord");
        directionVector.yCoord = nbttagcompound.getDouble("directionVector.yCoord");
        directionVector.zCoord = nbttagcompound.getDouble("directionVector.zCoord");
		this.rotationPitch=this.getVectorPitchAngle(directionVector);
		this.rotationYaw=this.getVectorYawAngle(directionVector);
		needLightTargetUpdate=true;
    }

    @Override
	public void writeToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeToNBT(nbttagcompound);
        nbttagcompound.setDouble("directionVector.xCoord", directionVector.xCoord);
        nbttagcompound.setDouble("directionVector.yCoord", directionVector.yCoord);
        nbttagcompound.setDouble("directionVector.zCoord", directionVector.zCoord);
    }
	
    @Override
	public List<String> getNetworkedFields()
    {
        List<String> list = super.getNetworkedFields();
        list.add("rotationPitch");
        list.add("rotationYaw");
        return list;
    }
	
	public void setLightOn()
	{
		Iterator<XYZ> gai = xyzGlowningAir.iterator();
		while(gai.hasNext())
		{
			XYZ xyz = gai.next();
			Block block = worldObj.getBlock(xyz.x,xyz.y,xyz.z);
			if(block==Blocks.air || block.isAir(this.worldObj, xyz.x,xyz.y,xyz.z))
			{
				worldObj.setBlock(xyz.x, xyz.y, xyz.z, LightBulbBlock.glowningAir);
			}
		}
	}
	
	public void setLightOff()
	{
		Iterator<XYZ> gai = xyzGlowningAir.iterator();
		while(gai.hasNext())
		{
			XYZ xyz = gai.next();
			Block block = worldObj.getBlock(xyz.x,xyz.y,xyz.z);
			if(block == LightBulbBlock.glowningAir)
			{
				worldObj.setBlockToAir(xyz.x, xyz.y, xyz.z);
			}
		}
	}
	
	@Override
    public void updateEntity()
    {
		super.updateEntity();
		if(needLightTargetUpdate)
		{
			this.generateGlowningAirList();
			this.updateLightState();
			needLightTargetUpdate=false;
		}
		if(this.prevRotationPitch!=this.rotationPitch)
		{
	        IC2.network.get().updateTileEntityField(this, "rotationPitch");
			this.prevRotationPitch=this.rotationPitch;
		}
		if(this.prevRotationYaw!=this.rotationYaw)
		{
	        IC2.network.get().updateTileEntityField(this, "rotationYaw");
			this.prevRotationYaw=this.rotationYaw;
		}
    }
	
	@Override
	public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
		return IHLUtils.getThisModItemStack("spotlight");
	}
	
    public void setDirectionVector(EntityLivingBase player)
	{
    	ForgeDirection dir = ForgeDirection.getOrientation(this.getFacing());
    	Vec3 lookVec = player.getLookVec();
    	if(lookVec.xCoord*dir.offsetX+lookVec.yCoord*dir.offsetY+lookVec.zCoord*dir.offsetZ<0)
    	{
    		double x=player.posX-this.xCoord-0.5D;
    		double y=player.posY+player.getEyeHeight()-this.yCoord-0.5D;
    		double z=player.posZ-this.zCoord-0.5D;
    		double d=Math.sqrt(x*x+y*y+z*z);
    		directionVector=Vec3.createVectorHelper(x/d, y/d, z/d);
    	}
    	else
    	{
    		directionVector=player.getLookVec();
    	}
		this.rotationPitch=this.getVectorPitchAngle(directionVector);
		this.rotationYaw=this.getVectorYawAngle(directionVector);
		this.needLightTargetUpdate=true;
        IC2.network.get().initiateTileEntityEvent(this, 0, true);
	}
	
	private void generateGlowningAirList()
	{
		this.setLightOff();
		xyzGlowningAir.clear();
		Iterator<Vec3> lvi = lightSphereVectors.iterator();
		while(lvi.hasNext())
		{
			traceVectorToLastAirBlock(lvi.next());
		}
	}
	
	private void traceVectorToLastAirBlock(Vec3 vector)
	{
		double dx=directionVector.xCoord;
		double dy=directionVector.yCoord;
		double dz=directionVector.zCoord;
		double dx1=vector.xCoord;
		double dy1=vector.yCoord;
		double dz1=vector.zCoord;
		if(2*dx*dx1+dx1*dx1+2*dy*dy1+dy1*dy1+2*dz*dz1+dz1*dz1>0d)
		{
			double x1=xCoord+0.5d;
			double y1=yCoord+0.5d;
			double z1=zCoord+0.5d;
			int xi1=xCoord;
			int yi1=yCoord;
			int zi1=zCoord;
			int airx=xCoord;
			int airy=yCoord;
			int airz=zCoord;
			for(int i=0;i<maxLightRange;i++)
			{
				int xi = (int)Math.floor(x1+(dx+dx1)*i*0.79);
				int yi = (int)Math.floor(y1+(dy+dy1)*i*0.79);
				int zi = (int)Math.floor(z1+(dz+dz1)*i*0.79);
				if(xi!=xi1||yi!=yi1||zi!=zi1)
				{
					Block block = worldObj.getBlock(xi,yi,zi);
					if(block!=Blocks.air && !block.isAir(worldObj, xi, yi, zi) && block.getMaterial()!=Material.glass && block.getMaterial()!=Material.air)
					{
						xyzGlowningAir.add(new XYZ(airx,airy,airz));
						break;
					}
					else
					{
						if(block.isAir(worldObj, xi, yi, zi) || block==Blocks.air)
						{
							airx=xi;
							airy=yi;
							airz=zi;
						}
						xi1=xi;
						yi1=yi;
						zi1=zi;
					}
				}
			}
		}
	}
	
	@Override
	protected void updateLightState()
    {
		if(this.getActive())
		{
    		setLightOn();
		}
		else
		{
    		setLightOff();
		}
    }
	
	private float getVectorPitchAngle(Vec3 vector)
	{
		double x=vector.xCoord;
		double y=vector.yCoord;
		double z=vector.zCoord;
		switch(this.getFacing())
		{
			case 0:
				return (float)-(Math.abs(Math.asin(z))+Math.abs(Math.asin(x)));
			case 1:
				return (float)(Math.abs(Math.asin(z))+Math.abs(Math.asin(x)));
			case 2:
				return (float)(Math.abs(Math.asin(y))+Math.abs(Math.asin(x)));
			case 3:
				return (float)(Math.abs(Math.asin(y))+Math.abs(Math.asin(x)));
			case 4:
				return (float)(Math.abs(Math.asin(y))+Math.abs(Math.asin(z)));
			case 5:
				return (float)(Math.abs(Math.asin(y))+Math.abs(Math.asin(z)));
			default:
				return (float)(-Math.asin(y));
		}
	}
	
	private float getVectorYawAngle(Vec3 vector)
	{
		double x=vector.xCoord;
		double y=vector.yCoord;
		double z=vector.zCoord;
		switch(this.getFacing())
		{
			case 0:
				if(z>=0)
				{
					return (float)(Math.acos(x/Math.sqrt(x*x+z*z))-Math.PI/2);
				}
				else
				{
					return (float)(-Math.acos(x/Math.sqrt(x*x+z*z))-Math.PI/2);
				}
			case 1:
				if(z>=0)
				{
					return (float)(Math.acos(x/Math.sqrt(x*x+z*z))-Math.PI/2);
				}
				else
				{
					return (float)(-Math.acos(x/Math.sqrt(x*x+z*z))-Math.PI/2);
				}
			case 2:
				if(y>=0)
				{
					return (float)(-Math.acos(x/Math.sqrt(x*x+y*y))-Math.PI/2);
				}
				else
				{
					return (float)(Math.acos(x/Math.sqrt(x*x+y*y))-Math.PI/2);
				}
			case 3:
				if(y>=0)
				{
					return (float)(-Math.acos(x/Math.sqrt(x*x+y*y))-Math.PI/2);
				}
				else
				{
					return (float)(Math.acos(x/Math.sqrt(x*x+y*y))-Math.PI/2);
				}
			case 4:
				if(y>=0)
				{
					return (float)(-Math.acos(z/Math.sqrt(z*z+y*y))-Math.PI/2);
				}
				else
				{
					return (float)(Math.acos(z/Math.sqrt(z*z+y*y))-Math.PI/2);
				}
			case 5:
				if(y>=0)
				{
					return (float)(-Math.acos(z/Math.sqrt(z*z+y*y))-Math.PI/2);
				}
				else
				{
					return (float)(Math.acos(z/Math.sqrt(z*z+y*y))-Math.PI/2);
				}
			default:
				return 0f;
		}
	}

	@Override
	public void onNetworkEvent(int event) 
	{
    	this.worldObj.playSound(xCoord+0.5d, yCoord+0.5d, zCoord+0.5d, IHLModInfo.MODID+":spotlightRotating", 10F, 1f, true);
	}
}


/* -Y DOWN(0, -1, 0)  0
/* +Y UP(0, 1, 0)     1
/* -Z NORTH(0, 0, -1) 2
/* +Z SOUTH(0, 0, 1)  3
/* -X WEST(-1, 0, 0), 4
/* +X EAST(1, 0, 0),  5
//VALID_DIRECTIONS = {DOWN, UP, NORTH, SOUTH, WEST, EAST};*/