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.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ);
116 Vec3 var2 = this.worldObj.getWorldVec3Pool().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.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ);
119 var2 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
120
121 if (var3 != null)
122 {
123 var2 = this.worldObj.getWorldVec3Pool().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.getMotionFactor();
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 /**
217 * Return the motion factor for this projectile. The factor is multiplied by the original motion.
218 */
219 protected float getMotionFactor()
220 {
221 return 0.95F;
222 }
223
224 /**
225 * Called when this EntityFireball hits a block or entity.
226 */
227 protected abstract void onImpact(MovingObjectPosition var1);
228
229 /**
230 * (abstract) Protected helper method to write subclass entity data to NBT.
231 */
232 public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
233 {
234 par1NBTTagCompound.setShort("xTile", (short)this.xTile);
235 par1NBTTagCompound.setShort("yTile", (short)this.yTile);
236 par1NBTTagCompound.setShort("zTile", (short)this.zTile);
237 par1NBTTagCompound.setByte("inTile", (byte)this.inTile);
238 par1NBTTagCompound.setByte("inGround", (byte)(this.inGround ? 1 : 0));
239 par1NBTTagCompound.setTag("direction", this.newDoubleNBTList(new double[] {this.motionX, this.motionY, this.motionZ}));
240 }
241
242 /**
243 * (abstract) Protected helper method to read subclass entity data from NBT.
244 */
245 public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
246 {
247 this.xTile = par1NBTTagCompound.getShort("xTile");
248 this.yTile = par1NBTTagCompound.getShort("yTile");
249 this.zTile = par1NBTTagCompound.getShort("zTile");
250 this.inTile = par1NBTTagCompound.getByte("inTile") & 255;
251 this.inGround = par1NBTTagCompound.getByte("inGround") == 1;
252
253 if (par1NBTTagCompound.hasKey("direction"))
254 {
255 NBTTagList var2 = par1NBTTagCompound.getTagList("direction");
256 this.motionX = ((NBTTagDouble)var2.tagAt(0)).data;
257 this.motionY = ((NBTTagDouble)var2.tagAt(1)).data;
258 this.motionZ = ((NBTTagDouble)var2.tagAt(2)).data;
259 }
260 else
261 {
262 this.setDead();
263 }
264 }
265
266 /**
267 * Returns true if other Entities should be prevented from moving through this Entity.
268 */
269 public boolean canBeCollidedWith()
270 {
271 return true;
272 }
273
274 public float getCollisionBorderSize()
275 {
276 return 1.0F;
277 }
278
279 /**
280 * Called when the entity is attacked.
281 */
282 public boolean attackEntityFrom(DamageSource par1DamageSource, int par2)
283 {
284 this.setBeenAttacked();
285
286 if (par1DamageSource.getEntity() != null)
287 {
288 Vec3 var3 = par1DamageSource.getEntity().getLookVec();
289
290 if (var3 != null)
291 {
292 this.motionX = var3.xCoord;
293 this.motionY = var3.yCoord;
294 this.motionZ = var3.zCoord;
295 this.accelerationX = this.motionX * 0.1D;
296 this.accelerationY = this.motionY * 0.1D;
297 this.accelerationZ = this.motionZ * 0.1D;
298 }
299
300 if (par1DamageSource.getEntity() instanceof EntityLiving)
301 {
302 this.shootingEntity = (EntityLiving)par1DamageSource.getEntity();
303 }
304
305 return true;
306 }
307 else
308 {
309 return false;
310 }
311 }
312
313 @SideOnly(Side.CLIENT)
314 public float getShadowSize()
315 {
316 return 0.0F;
317 }
318
319 /**
320 * Gets how bright this entity is.
321 */
322 public float getBrightness(float par1)
323 {
324 return 1.0F;
325 }
326
327 @SideOnly(Side.CLIENT)
328 public int getBrightnessForRender(float par1)
329 {
330 return 15728880;
331 }
332 }