## Tuesday, August 18, 2015

### Magnum Torch

A magnum torch is an Extra Utilities block which prevents the spawning of hostile mobs within a certain space around it. How? Like this:

public static boolean isInRangeOfTorch(Entity entity)
{
for (int[] coord : magnumTorchRegistry) {
if ((coord[0] == entity.field_70170_p.field_73011_w.field_76574_g) &&
(entity.field_70170_p.func_72899_e(coord[1], coord[2], coord[3])) && ((entity.field_70170_p.func_147438_o(coord[1], coord[2], coord[3]) instanceof IAntiMobTorch)))
{
TileEntity tile = entity.field_70170_p.func_147438_o(coord[1], coord[2], coord[3]);
double dx = tile.field_145851_c + 0.5F - entity.field_70165_t;
double dy = tile.field_145848_d + 0.5F - entity.field_70163_u;
double dz = tile.field_145849_e + 0.5F - entity.field_70161_v;
if ((dx * dx + dz * dz) / ((IAntiMobTorch)tile).getHorizontalTorchRangeSquared() + dy * dy / ((IAntiMobTorch)tile).getVerticalTorchRangeSquared() <= 1.0D) {
return true;
}
}
}
return false;
}

This is a method which supports both the chandelier and magnum torch. Each of those has the getHorizontalTorchRangeSquared and getVerticalTorchRangeSquared methods. It doesn't matter how this method gets called, or what the obfuscated functions are. The important bit is that line which works with dx, dy, and dz. Those are obviously the distance from the torch to the entity in question (the mob trying to spawn). The formula looks like this:

$\frac{\Delta x ^2+\Delta z^2}{h^2}+\frac{\Delta y^2}{v^2} \le 1$

Rearranging slightly, we see the formula for the surface and interior of an ellipsoid:

$\frac{\Delta x ^2}{h^2}+\frac{\Delta z^2}{h^2}+\frac{\Delta y^2}{v^2} \le 1$

The semi-axes in the horizontal direction are both $h$, while the semi-axis in the vertical direction is $v$. Looking at the implementation of these blocks we see:

public float getHorizontalTorchRangeSquared()
{
if ((func_145838_q() instanceof BlockMagnumTorch)) {
return 16384.0F;
}
if ((func_145838_q() instanceof BlockChandelier)) {
return 256.0F;
}
return -1.0F;
}

public float getVerticalTorchRangeSquared()
{
if ((func_145838_q() instanceof BlockMagnumTorch)) {
return 1024.0F;
}
if ((func_145838_q() instanceof BlockChandelier)) {
return 256.0F;
}
return -1.0F;
}

So, the magnum torch clears a spheroid around itself with an equatorial radius of 128 meters and a polar radius of 32 meters. Similarly the chandelier clears a sphere around itself of radius 16 meters.