summaryrefslogtreecommitdiff
path: root/ihl/flexible_cable/PowerCableNodeEntity.java
blob: 346b86cb8227a71c26d6ba93e16688fecd4649d1 (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
package ihl.flexible_cable;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ic2.core.IC2DamageSource;
import ic2.core.item.armor.ItemArmorHazmat;
import ihl.IHLMod;
import ihl.IHLModInfo;
import ihl.interfaces.IEnergyNetNode;
import ihl.utils.IHLUtils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.S29PacketSoundEffect;
import net.minecraft.network.play.server.S2APacketParticles;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;

public class PowerCableNodeEntity extends NodeEntity implements IEnergyNetNode{
	
	private Set<IHLCable> cableList;
	private IHLCable cable;
	private double soundRange=10d;
	private final static float groundConductivity=0.005f;
	private int lastCheckTimer=0;
	
	public PowerCableNodeEntity(World world) 
	{
		super(world);
	}
	
	@Override
	public boolean canBeCollidedWith()
	{
		return !worldObj.isRemote && !this.noClip;
	}
	
	@Override
	public boolean canBePushed()
	{
		return !worldObj.isRemote && !this.noClip;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onUpdate()
	{
		super.onUpdate();
		if(!worldObj.isRemote)
		{ 
			IHLGrid grid = IHLMod.enet.cablesToGrids.get(this.chainUniqueID);
			if(grid!=null && grid.energy>0d)
			{
				double voltage = grid.getSinkVoltage(this);
				if(this.onGround && voltage>=10d && this.getMaxAllowableVoltage()<voltage)
				{
	                grid.drawEnergy(voltage*voltage*groundConductivity, this);
					if(Math.abs(lastCheckTimer-checkTimer) > 10)
					{
						for(Object player:worldObj.getEntitiesWithinAABB(EntityPlayerMP.class, AxisAlignedBB.getBoundingBox(this.posX-soundRange,this.posY-soundRange,this.posZ-soundRange, this.posX+soundRange,this.posY+soundRange,this.posZ+soundRange)))
						{
							if(player instanceof EntityPlayerMP)
							{
								EntityPlayerMP playerMP = (EntityPlayerMP)player;
								playerMP.playerNetServerHandler.sendPacket(new S2APacketParticles("largesmoke",(float)this.posX,(float)this.posY,(float)this.posZ,worldObj.rand.nextFloat()*0.1f-0.05f,0.1f,worldObj.rand.nextFloat()*0.1f-0.05f,0.1f,4));
							}
						}
						lastCheckTimer=checkTimer;
					}
				}
				if(this.nextAnchorEntity!=null)
				{
					List<EntityLivingBase> entityList = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, this.boundingBox.expand(4d, 4d,4d));
					for(EntityLivingBase elb:entityList)
					{
						if(elb.boundingBox!=null)
						{
							if(IHLUtils.isSegmentInsideAABB(elb.boundingBox,this.posX,this.posY,this.posZ,this.nextAnchorEntity.posX,this.nextAnchorEntity.posY,this.nextAnchorEntity.posZ))
							{
								this.applyEntityCollision(elb);
							}
						}
					}
				}
			}
    	}
	}
	
	@Override
	public AxisAlignedBB getCollisionBox(Entity entity)
	{
		return boundingBox;
	}


	@Override
	public void applyEntityCollision(Entity entity)
	{
		super.applyEntityCollision(entity);
		IHLGrid grid = IHLMod.enet.cablesToGrids.get(this.chainUniqueID);
		if(entity instanceof EntityLivingBase && grid!=null && grid.energy>=1d)
		{
			double voltage = grid.getSinkVoltage(this);
			if(this.getMaxAllowableVoltage()<voltage)
			{
				EntityLivingBase victim=(EntityLivingBase) entity;
	            if (!ItemArmorHazmat.hasCompleteHazmat(victim))
	            {
	            	float damage=(float)Math.min(voltage*groundConductivity,grid.energy*1000d/voltage);
	                victim.attackEntityFrom(IC2DamageSource.electricity, damage);
	                grid.drawEnergy(damage*voltage/1000d, this);
	                if(voltage>1000)
	                {
	                	sendSound();
	                	IHLMod.proxy.spawnParticleFromServer(3,worldObj, (float)this.posX,(float)this.posY,(float)this.posZ,0f,0f,0f,10f);
	                }
	            }
			}
		}
	}
    
	@Override
	protected void readEntityFromNBT(NBTTagCompound nbt) 
	{
		super.readEntityFromNBT(nbt);
		if(nbt.hasKey("cable"))
		{
			this.addCable(nbt.getCompoundTag("cable"));
		}
	}

	@Override
	protected void writeEntityToNBT(NBTTagCompound nbt) 
	{
		super.writeEntityToNBT(nbt);
		if(this.cable!=null)
		{
			nbt.setTag("cable",this.cable.toNBT());
		}
	}
	
	@Override
	public IHLGrid getGrid() 
	{
		return IHLMod.enet.cablesToGrids.get(this.chainUniqueID);
	}

	@Override
	public int getGridID() 
	{
		return 0;
	}

	@Override
	public void setGrid(int newGridID) {}

	@Override
	public double getMaxAllowableVoltage() 
	{
		if(this.cable!=null)
		{
			return this.cable.maxVoltage;
		}
		else
		{
			return Integer.MAX_VALUE;
		}
	}

	@Override
	public boolean addCable(NBTTagCompound cable1) 
	{
		this.cable=IHLCable.fromNBT(cable1);
		return true;
	}

	@Override
	public Set<IHLCable> getCableList() {
		if(cableList==null)
		{
			cableList=new HashSet<IHLCable>(1);
			if(this.cable!=null)
			{
				cableList.add(this.cable);
			}
		}
		return cableList;
	}

	@Override
	public void removeAttachedChains() {}
	
	public void sendSound()
	{
		for(Object player:worldObj.getEntitiesWithinAABB(EntityPlayerMP.class, AxisAlignedBB.getBoundingBox(this.posX-soundRange,this.posY-soundRange,this.posZ-soundRange, this.posX+soundRange,this.posY+soundRange,this.posZ+soundRange)))
		{
			if(player instanceof EntityPlayerMP)
			{
				EntityPlayerMP playerMP = (EntityPlayerMP)player;
				playerMP.playerNetServerHandler.sendPacket(new S29PacketSoundEffect(IHLModInfo.MODID+":electrocution",this.posX,this.posY,this.posZ,1f,1f));
			}
		}
	}

	@Override
	public double[] getPortPos(EntityLivingBase player) 
	{
		return new double[3];
	}
	
	@Override
	public void remove(IHLCable cable) 
	{
		this.cableList.remove(cable);
	}

	@Override
	public boolean isCableRemoved(int chainUniqueID) 
	{
		return false;
	}

	@Override
	public void setCableCheck(boolean b) {}

	@Override
	public double getEnergyAmountThisNodeWant() 
	{
		return 0;
	}

	@Override
	public void injectEnergyInThisNode(double amount, double voltage) {}

	@Override
	public boolean isTileEntityBaseInvalid() {
		return this.isDead;
	}

}