001 package net.minecraft.src;
002
003 import cpw.mods.fml.common.Side;
004 import cpw.mods.fml.common.asm.SideOnly;
005 import java.util.Iterator;
006 import java.util.List;
007
008 public abstract class EntityFireball extends Entity
009 {
010 private int xTile = -1;
011 private int yTile = -1;
012 private int zTile = -1;
013 private int inTile = 0;
014 private boolean inGround = false;
015 public EntityLiving shootingEntity;
016 private int ticksAlive;
017 private int ticksInAir = 0;
018 public double accelerationX;
019 public double accelerationY;
020 public double accelerationZ;
021
022 public EntityFireball(World par1World)
023 {
024 super(par1World);
025 this.setSize(1.0F, 1.0F);
026 }
027
028 protected void entityInit() {}
029
030 @SideOnly(Side.CLIENT)
031
032 /**
033 * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge
034 * length * 64 * renderDistanceWeight Args: distance
035 */
036 public boolean isInRangeToRenderDist(double par1)
037 {
038 double var3 = this.boundingBox.getAverageEdgeLength() * 4.0D;
039 var3 *= 64.0D;
040 return par1 < var3 * var3;
041 }
042
043 public EntityFireball(World par1World, double par2, double par4, double par6, double par8, double par10, double par12)
044 {
045 super(par1World);
046 this.setSize(1.0F, 1.0F);
047 this.setLocationAndAngles(par2, par4, par6, this.rotationYaw, this.rotationPitch);
048 this.setPosition(par2, par4, par6);
049 double var14 = (double)MathHelper.sqrt_double(par8 * par8 + par10 * par10 + par12 * par12);
050 this.accelerationX = par8 / var14 * 0.1D;
051 this.accelerationY = par10 / var14 * 0.1D;
052 this.accelerationZ = par12 / var14 * 0.1D;
053 }
054
055 public EntityFireball(World par1World, EntityLiving par2EntityLiving, double par3, double par5, double par7)
056 {
057 super(par1World);
058 this.shootingEntity = par2EntityLiving;
059 this.setSize(1.0F, 1.0F);
060 this.setLocationAndAngles(par2EntityLiving.posX, par2EntityLiving.posY, par2EntityLiving.posZ, par2EntityLiving.rotationYaw, par2EntityLiving.rotationPitch);
061 this.setPosition(this.posX, this.posY, this.posZ);
062 this.yOffset = 0.0F;
063 this.motionX = this.motionY = this.motionZ = 0.0D;
064 par3 += this.rand.nextGaussian() * 0.4D;
065 par5 += this.rand.nextGaussian() * 0.4D;
066 par7 += this.rand.nextGaussian() * 0.4D;
067 double var9 = (double)MathHelper.sqrt_double(par3 * par3 + par5 * par5 + par7 * par7);
068 this.accelerationX = par3 / var9 * 0.1D;
069 this.accelerationY = par5 / var9 * 0.1D;
070 this.accelerationZ = par7 / var9 * 0.1D;
071 }
072
073 /**
074 * Called to update the entity's position/logic.
075 */
076 public void onUpdate()
077 {
078 if (!this.worldObj.isRemote && (this.shootingEntity != null && this.shootingEntity.isDead || !this.worldObj.blockExists((int)this.posX, (int)this.posY, (int)this.posZ)))
079 {
080 this.setDead();
081 }
082 else
083 {
084 super.onUpdate();
085 this.setFire(1);
086
087 if (this.inGround)
088 {
089 int var1 = this.worldObj.getBlockId(this.xTile, this.yTile, this.zTile);
090
091 if (var1 == this.inTile)
092 {
093 ++this.ticksAlive;
094
095 if (this.ticksAlive == 600)
096 {
097 this.setDead();
098 }
099
100 return;
101 }
102
103 this.inGround = false;
104 this.motionX *= (double)(this.rand.nextFloat() * 0.2F);
105 this.motionY *= (double)(this.rand.nextFloat() * 0.2F);
106 this.motionZ *= (double)(this.rand.nextFloat() * 0.2F);
107 this.ticksAlive = 0;
108 this.ticksInAir = 0;
109 }
110 else
111 {
112 ++this.ticksInAir;
113 }
114
115 Vec3 var15 = this.worldObj.func_82732_R().getVecFromPool(this.posX, this.posY, this.posZ);
116 Vec3 var2 = this.worldObj.func_82732_R().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
117 MovingObjectPosition var3 = this.worldObj.rayTraceBlocks(var15, var2);
118 var15 = this.worldObj.func_82732_R().getVecFromPool(this.posX, this.posY, this.posZ);
119 var2 = this.worldObj.func_82732_R().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
120
121 if (var3 != null)
122 {
123 var2 = this.worldObj.func_82732_R().getVecFromPool(var3.hitVec.xCoord, var3.hitVec.yCoord, var3.hitVec.zCoord);
124 }
125
126 Entity var4 = null;
127 List var5 = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
128 double var6 = 0.0D;
129 Iterator var8 = var5.iterator();
130
131 while (var8.hasNext())
132 {
133 Entity var9 = (Entity)var8.next();
134
135 if (var9.canBeCollidedWith() && (!var9.isEntityEqual(this.shootingEntity) || this.ticksInAir >= 25))
136 {
137 float var10 = 0.3F;
138 AxisAlignedBB var11 = var9.boundingBox.expand((double)var10, (double)var10, (double)var10);
139 MovingObjectPosition var12 = var11.calculateIntercept(var15, var2);
140
141 if (var12 != null)
142 {
143 double var13 = var15.distanceTo(var12.hitVec);
144
145 if (var13 < var6 || var6 == 0.0D)
146 {
147 var4 = var9;
148 var6 = var13;
149 }
150 }
151 }
152 }
153
154 if (var4 != null)
155 {
156 var3 = new MovingObjectPosition(var4);
157 }
158
159 if (var3 != null)
160 {
161 this.onImpact(var3);
162 }
163
164 this.posX += this.motionX;
165 this.posY += this.motionY;
166 this.posZ += this.motionZ;
167 float var16 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
168 this.rotationYaw = (float)(Math.atan2(this.motionZ, this.motionX) * 180.0D / Math.PI) + 90.0F;
169
170 for (this.rotationPitch = (float)(Math.atan2((double)var16, this.motionY) * 180.0D / Math.PI) - 90.0F; this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
171 {
172 ;
173 }
174
175 while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
176 {
177 this.prevRotationPitch += 360.0F;
178 }
179
180 while (this.rotationYaw - this.prevRotationYaw < -180.0F)
181 {
182 this.prevRotationYaw -= 360.0F;
183 }
184
185 while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
186 {
187 this.prevRotationYaw += 360.0F;
188 }
189
190 this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
191 this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
192 float var17 = this.func_82341_c();
193
194 if (this.isInWater())
195 {
196 for (int var19 = 0; var19 < 4; ++var19)
197 {
198 float var18 = 0.25F;
199 this.worldObj.spawnParticle("bubble", this.posX - this.motionX * (double)var18, this.posY - this.motionY * (double)var18, this.posZ - this.motionZ * (double)var18, this.motionX, this.motionY, this.motionZ);
200 }
201
202 var17 = 0.8F;
203 }
204
205 this.motionX += this.accelerationX;
206 this.motionY += this.accelerationY;
207 this.motionZ += this.accelerationZ;
208 this.motionX *= (double)var17;
209 this.motionY *= (double)var17;
210 this.motionZ *= (double)var17;
211 this.worldObj.spawnParticle("smoke", this.posX, this.posY + 0.5D, this.posZ, 0.0D, 0.0D, 0.0D);
212 this.setPosition(this.posX, this.posY, this.posZ);
213 }
214 }
215
216 protected float func_82341_c()
217 {
218 return 0.95F;
219 }
220
221 /**
222 * Called when this EntityFireball hits a block or entity.
223 */
224 protected abstract void onImpact(MovingObjectPosition var1);
225
226 /**
227 * (abstract) Protected helper method to write subclass entity data to NBT.
228 */
229 public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
230 {
231 par1NBTTagCompound.setShort("xTile", (short)this.xTile);
232 par1NBTTagCompound.setShort("yTile", (short)this.yTile);
233 par1NBTTagCompound.setShort("zTile", (short)this.zTile);
234 par1NBTTagCompound.setByte("inTile", (byte)this.inTile);
235 par1NBTTagCompound.setByte("inGround", (byte)(this.inGround ? 1 : 0));
236 par1NBTTagCompound.setTag("direction", this.newDoubleNBTList(new double[] {this.motionX, this.motionY, this.motionZ}));
237 }
238
239 /**
240 * (abstract) Protected helper method to read subclass entity data from NBT.
241 */
242 public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
243 {
244 this.xTile = par1NBTTagCompound.getShort("xTile");
245 this.yTile = par1NBTTagCompound.getShort("yTile");
246 this.zTile = par1NBTTagCompound.getShort("zTile");
247 this.inTile = par1NBTTagCompound.getByte("inTile") & 255;
248 this.inGround = par1NBTTagCompound.getByte("inGround") == 1;
249
250 if (par1NBTTagCompound.hasKey("direction"))
251 {
252 NBTTagList var2 = par1NBTTagCompound.getTagList("direction");
253 this.motionX = ((NBTTagDouble)var2.tagAt(0)).data;
254 this.motionY = ((NBTTagDouble)var2.tagAt(1)).data;
255 this.motionZ = ((NBTTagDouble)var2.tagAt(2)).data;
256 }
257 else
258 {
259 this.setDead();
260 }
261 }
262
263 /**
264 * Returns true if other Entities should be prevented from moving through this Entity.
265 */
266 public boolean canBeCollidedWith()
267 {
268 return true;
269 }
270
271 public float getCollisionBorderSize()
272 {
273 return 1.0F;
274 }
275
276 /**
277 * Called when the entity is attacked.
278 */
279 public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
280 {
281 this.setBeenAttacked();
282
283 if (par1DamageSource.getEntity() != null)
284 {
285 Vec3 var3 = par1DamageSource.getEntity().getLookVec();
286
287 if (var3 != null)
288 {
289 this.motionX = var3.xCoord;
290 this.motionY = var3.yCoord;
291 this.motionZ = var3.zCoord;
292 this.accelerationX = this.motionX * 0.1D;
293 this.accelerationY = this.motionY * 0.1D;
294 this.accelerationZ = this.motionZ * 0.1D;
295 }
296
297 if (par1DamageSource.getEntity() instanceof EntityLiving)
298 {
299 this.shootingEntity = (EntityLiving)par1DamageSource.getEntity();
300 }
301
302 return true;
303 }
304 else
305 {
306 return false;
307 }
308 }
309
310 @SideOnly(Side.CLIENT)
311 public float getShadowSize()
312 {
313 return 0.0F;
314 }
315
316 /**
317 * Gets how bright this entity is.
318 */
319 public float getBrightness(float par1)
320 {
321 return 1.0F;
322 }
323
324 @SideOnly(Side.CLIENT)
325 public int getBrightnessForRender(float par1)
326 {
327 return 15728880;
328 }
329 }