Refactored cloaking assembly

- reduced CPU usage
- reduced network usage (redundant beams)
- added red gray color in case of low energy
- changed tier colors to green / blue, instead of green / red
- added more colors with smoother transitions
Improved packet sending debug logs
This commit is contained in:
LemADEC 2015-08-09 06:10:08 +02:00
parent 920d9521e6
commit e7d436aca3
2 changed files with 130 additions and 163 deletions

View file

@ -24,9 +24,24 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
public boolean isEnabled = false; public boolean isEnabled = false;
public byte tier = 1; // cloaking field tier, 1 or 2 public byte tier = 1; // cloaking field tier, 1 or 2
// inner coils color map
final float[] innerCoilColor_r = { 1.00f, 1.00f, 1.00f, 1.00f, 0.75f, 0.25f, 0.00f, 0.00f, 0.00f, 0.00f, 0.50f, 1.00f };
final float[] innerCoilColor_g = { 0.00f, 0.25f, 0.75f, 1.00f, 1.00f, 1.00f, 1.00f, 1.00f, 0.50f, 0.25f, 0.00f, 0.00f };
final float[] innerCoilColor_b = { 0.25f, 0.00f, 0.00f, 0.00f, 0.00f, 0.00f, 0.50f, 1.00f, 1.00f, 1.00f, 1.00f, 0.75f };
// Spatial cloaking field parameters // Spatial cloaking field parameters
public int front, back, up, down, left, right; private final int innerCoilsDistance = 2; // Step length from core block to main coils
public int minX = 0, minY = 0, minZ = 0, maxX = 0, maxY = 0, maxZ = 0; private final byte[] dx = { -1, 1, 0, 0, 0, 0 }; // validateAssembly() is coded ordering, do not change!
private final byte[] dy = { 0, 0, -1, 1, 0, 0 }; // validateAssembly() is coded ordering, do not change!
private final byte[] dz = { 0, 0, 0, 0, -1, 1 }; // validateAssembly() is coded ordering, do not change!
private int[] outerCoilsDistance = {0, 0, 0, 0, 0, 0};
public int minX = 0;
public int minY = 0;
public int minZ = 0;
public int maxX = 0;
public int maxY = 0;
public int maxZ = 0;
public boolean isValid = false; public boolean isValid = false;
public boolean isCloaking = false; public boolean isCloaking = false;
@ -51,7 +66,7 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
@Override @Override
public void updateEntity() { public void updateEntity() {
super.updateEntity(); super.updateEntity();
if (!FMLCommonHandler.instance().getEffectiveSide().isServer()) { if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
return; return;
} }
@ -64,7 +79,7 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
updateTicks--; updateTicks--;
if (updateTicks <= 0) { if (updateTicks <= 0) {
//System.out.println("" + this + " Updating cloaking state..."); WarpDrive.logger.info("" + this + " Updating cloaking state...");
updateTicks = ((tier == 1) ? 20 : (tier == 2) ? 10 : 20) * WarpDriveConfig.CD_FIELD_REFRESH_INTERVAL_SECONDS; // resetting timer updateTicks = ((tier == 1) ? 20 : (tier == 2) ? 10 : 20) * WarpDriveConfig.CD_FIELD_REFRESH_INTERVAL_SECONDS; // resetting timer
isValid = validateAssembly(); isValid = validateAssembly();
@ -93,6 +108,8 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
CloakedArea area = WarpDrive.cloaks.getCloakedArea(worldObj, xCoord, yCoord, zCoord); CloakedArea area = WarpDrive.cloaks.getCloakedArea(worldObj, xCoord, yCoord, zCoord);
if (area != null) { if (area != null) {
area.sendCloakPacketToPlayersEx(false); // recloak field area.sendCloakPacketToPlayersEx(false); // recloak field
} else {
WarpDrive.debugPrint("getCloakedArea1 returned null for " + worldObj + " " + xCoord + "," + yCoord + "," + zCoord);
} }
} else {// enabled, not cloaking but not able to } else {// enabled, not cloaking but not able to
// IDLE // IDLE
@ -109,6 +126,8 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
CloakedArea area = WarpDrive.cloaks.getCloakedArea(worldObj, xCoord, yCoord, zCoord); CloakedArea area = WarpDrive.cloaks.getCloakedArea(worldObj, xCoord, yCoord, zCoord);
if (area != null) { if (area != null) {
area.sendCloakPacketToPlayersEx(false); // recloak field area.sendCloakPacketToPlayersEx(false); // recloak field
} else {
WarpDrive.debugPrint("getCloakedArea2 returned null for " + worldObj + " " + xCoord + "," + yCoord + "," + zCoord);
} }
setCoilsState(true); setCoilsState(true);
} else {// loosing power } else {// loosing power
@ -129,104 +148,74 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
} }
} }
public void setCoilsState(boolean enabled) { private void setCoilsState(final boolean enabled) {
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, (enabled) ? 1 : 0, 2); worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, (enabled) ? 1 : 0, 2);
// Directions to check (all six directions: left, right, up, down, front, back) for (int direction = 0; direction < 6; direction++) {
byte[] dx = { 1, -1, 0, 0, 0, 0 }; setCoilState(innerCoilsDistance, direction, enabled);
byte[] dy = { 0, 0, -1, 1, 0, 0 }; setCoilState(outerCoilsDistance[direction], direction, enabled);
byte[] dz = { 0, 0, 0, 0, -1, 1 };
for (int i = 0; i < 6; i++) {
searchCoilInDirectionAndSetState(dx[i], dy[i], dz[i], enabled);
} }
} }
public void searchCoilInDirectionAndSetState(byte dx, byte dy, byte dz, boolean enabled) { private void setCoilState(final int distance, final int direction, final boolean enabled) {
int coilCount = 0; int x = xCoord + distance * dx[direction];
for (int i = 0; i < WarpDriveConfig.CD_MAX_CLOAKING_FIELD_SIDE; i++) { int y = yCoord + distance * dy[direction];
if (worldObj.getBlock(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz).isAssociatedBlock(WarpDrive.cloakCoilBlock)) { int z = zCoord + distance * dz[direction];
coilCount++; if (worldObj.getBlock(x, y, z).isAssociatedBlock(WarpDrive.cloakCoilBlock)) {
if (coilCount > 2) { worldObj.setBlockMetadataWithNotify(x, y, z, (enabled) ? 1 : 0, 2);
return;
}
worldObj.setBlockMetadataWithNotify(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz, (enabled) ? 1 : 0, 2);
}
} }
} }
public void searchCoilInDirectionAndDrawLaser(byte dx, byte dy, byte dz) { private void drawLasers() {
final int START_LENGTH = 2; float r = 0.0f;
float r = 0.0f, g = 1.0f, b = 0; float g = 1.0f;
if (tier == 1) { float b = 0.0f;
r = 0.0f; if (!isCloaking) {// out of energy
g = 1.0f; r = 0.75f;
g = 0.50f;
b = 0.50f;
} else if (tier == 1) {
r = 0.25f;
g = 1.00f;
b = 0.00f;
} else if (tier == 2) { } else if (tier == 2) {
r = 1.0f; r = 0.00f;
g = 0.0f; g = 0.25f;
b = 1.00f;
} }
for (int i = START_LENGTH + 1; i < WarpDriveConfig.CD_MAX_CLOAKING_FIELD_SIDE; i++) { // Directions to check (all six directions: left, right, up, down, front, back)
if (worldObj.getBlock(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz).isAssociatedBlock(WarpDrive.cloakCoilBlock)) { for (int direction = 0; direction < 6; direction++) {
PacketHandler.sendBeamPacketToPlayersInArea(worldObj, PacketHandler.sendBeamPacketToPlayersInArea(worldObj,
new Vector3(this).translate(0.5), new Vector3(
new Vector3(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz).translate(0.5), xCoord + innerCoilsDistance * dx[direction],
yCoord + innerCoilsDistance * dy[direction],
zCoord + innerCoilsDistance * dz[direction]).translate(0.5),
new Vector3(
xCoord + outerCoilsDistance[direction] * dx[direction],
yCoord + outerCoilsDistance[direction] * dy[direction],
zCoord + outerCoilsDistance[direction] * dz[direction]).translate(0.5),
r, g, b, 110, 0, r, g, b, 110, 0,
AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ)); AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ));
} }
}
// draw connecting coils
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 6; j++) {
// skip mirrored coils (removing the inner lines)
if (dx[i] == -dx[j] && dy[i] == -dy[j] && dz[i] == -dz[j]) {
continue;
} }
public void drawLasers() { // draw a random colored beam
final int START_LENGTH = 2; int mapIndex = worldObj.rand.nextInt(innerCoilColor_b.length);
float r = 0.0f, g = 1.0f, b = 0; r = innerCoilColor_r[mapIndex];
if (this.tier == 1) { g = innerCoilColor_g[mapIndex];
r = 0.0f; g = 1.0f; b = innerCoilColor_b[mapIndex];
} else if (this.tier == 2) {
r = 1.0f; g = 0.0f;
}
// Directions to check (all six directions: left, right, up, down, front, back)
byte[] dx = { 1, -1, 0, 0, 0, 0 };
byte[] dy = { 0, 0, -1, 1, 0, 0 };
byte[] dz = { 0, 0, 0, 0, -1, 1 };
for (int k = 0; k < 6; k++) {
searchCoilInDirectionAndDrawLaser(dx[k], dy[k], dz[k]);
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
switch (worldObj.rand.nextInt(6)) {
case 0:
r = 1.0f;
g = b = 0;
break;
case 1:
r = b = 0;
g = 1.0f;
break;
case 2:
r = g = 0;
b = 1.0f;
break;
case 3:
r = b = 0.5f;
g = 0;
break;
case 4:
r = g = 1.0f;
b = 0;
break;
case 5:
r = 1.0f;
b = 0.5f;
g = 0f;
}
PacketHandler.sendBeamPacketToPlayersInArea(worldObj, PacketHandler.sendBeamPacketToPlayersInArea(worldObj,
new Vector3(xCoord + START_LENGTH * dx[i], yCoord + START_LENGTH * dy[i], zCoord + START_LENGTH * dz[i]).translate(0.5), new Vector3(xCoord + innerCoilsDistance * dx[i], yCoord + innerCoilsDistance * dy[i], zCoord + innerCoilsDistance * dz[i]).translate(0.5),
new Vector3(xCoord + START_LENGTH * dx[j], yCoord + START_LENGTH * dy[j], zCoord + START_LENGTH * dz[j]).translate(0.5), new Vector3(xCoord + innerCoilsDistance * dx[j], yCoord + innerCoilsDistance * dy[j], zCoord + innerCoilsDistance * dz[j]).translate(0.5),
r, g, b, 110, 0, r, g, b, 110, 0,
AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ)); AxisAlignedBB.getBoundingBox(minX, minY, minZ, maxX, maxY, maxZ));
} }
@ -290,69 +279,59 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
tag.setBoolean("enabled", isEnabled); tag.setBoolean("enabled", isEnabled);
} }
public int searchCoilInDirection(byte dx, byte dy, byte dz) {
for (int i = 3; i < WarpDriveConfig.CD_MAX_CLOAKING_FIELD_SIDE; i++) {
if (worldObj.getBlock(xCoord + i * dx, yCoord + i * dy, zCoord + i * dz).isAssociatedBlock(WarpDrive.cloakCoilBlock)) {
return i;
}
}
return 0;
}
public boolean validateAssembly() { public boolean validateAssembly() {
final int START_LENGTH = 2; // Step length from core block to main coils final int maxOuterCoilDistance = WarpDriveConfig.CD_MAX_CLOAKING_FIELD_SIDE - WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS;
// Directions to check (all six directions: left, right, up, down, front, back) // Directions to check (all six directions: left, right, up, down, front, back)
byte[] dx = { 1, -1, 0, 0, 0, 0 }; for (int direction = 0; direction < 6; direction++) {
byte[] dy = { 0, 0, -1, 1, 0, 0 }; // check validity of inner coil
byte[] dz = { 0, 0, 0, 0, -1, 1 }; int x = xCoord + innerCoilsDistance * dx[direction];
int y = yCoord + innerCoilsDistance * dy[direction];
for (int i = 0; i < 6; i++) { int z = zCoord + innerCoilsDistance * dz[direction];
if (worldObj.getBlock(xCoord + START_LENGTH * dx[i], yCoord + START_LENGTH * dy[i], zCoord + START_LENGTH * dz[i]).isAssociatedBlock(WarpDrive.cloakCoilBlock)) { if (!worldObj.getBlock(x, y, z).isAssociatedBlock(WarpDrive.cloakCoilBlock)) {
return false; return false;
} }
// find closest outer coil
int newCoilDistance = 0;
for (int distance = 3; distance < maxOuterCoilDistance; distance++) {
x += dx[direction];
y += dy[direction];
z += dz[direction];
if (worldObj.getBlock(x, y, z).isAssociatedBlock(WarpDrive.cloakCoilBlock)) {
newCoilDistance = distance;
break;
}
}
// disable previous outer coil, in case a different one was found
if ( newCoilDistance != outerCoilsDistance[direction]
&& outerCoilsDistance[direction] > 0) {
int oldX = xCoord + outerCoilsDistance[direction] * dx[direction];
int oldY = yCoord + outerCoilsDistance[direction] * dy[direction];
int oldZ = zCoord + outerCoilsDistance[direction] * dz[direction];
if (worldObj.getBlock(oldX, oldY, oldZ).isAssociatedBlock(WarpDrive.cloakCoilBlock)) {
worldObj.setBlockMetadataWithNotify(oldX, oldY, oldZ, 0, 2);
}
}
// check validity and save new coil position
if (newCoilDistance <= 0) {
outerCoilsDistance[direction] = 0;
WarpDrive.debugPrint("Invalid outercoil assembly at " + direction);
return false;
}
outerCoilsDistance[direction] = newCoilDistance;
} }
// Check cloaking field parameters defining coils // Check cloaking field parameters defining coils
this.left = searchCoilInDirection((byte)1, (byte)0, (byte)0) + WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS; minX = xCoord - outerCoilsDistance[0] - WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS;
if (this.left == WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS) return false; maxX = xCoord + outerCoilsDistance[1] + WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS;
this.right = searchCoilInDirection((byte)-1, (byte)0, (byte)0) + WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS; minY = Math.max( 0, yCoord - outerCoilsDistance[2] - WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS);
if (this.right == WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS) return false; maxY = Math.min(255, yCoord + outerCoilsDistance[3] + WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS);
minZ = zCoord - outerCoilsDistance[4] - WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS;
this.up = searchCoilInDirection((byte)0, (byte)1, (byte)0) + WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS; maxZ = zCoord + outerCoilsDistance[5] + WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS;
if (this.up == WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS) return false;
this.down = searchCoilInDirection((byte)0, (byte)-1, (byte)0) + WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS;
if (this.down == WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS) return false;
this.front = searchCoilInDirection((byte)0, (byte)0, (byte)1) + WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS;
if (this.front == WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS) return false;
this.back = searchCoilInDirection((byte)0, (byte)0, (byte)-1) + WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS;
if (this.back == WarpDriveConfig.CD_COIL_CAPTURE_BLOCKS) return false;
int x1 = 0, x2 = 0, z1 = 0, z2 = 0;
z1 = zCoord - this.back;
z2 = zCoord + this.front;
x1 = xCoord - this.right;
x2 = xCoord + this.left;
if (x1 < x2) {
this.minX = x1; this.maxX = x2;
} else {
this.minX = x2; this.maxX = x1;
}
if (z1 < z2) {
this.minZ = z1; this.maxZ = z2;
} else {
this.minZ = z2; this.maxZ = z1;
}
this.minY = yCoord - this.down;
this.maxY = yCoord + this.up;
return true; return true;
} }
@ -440,16 +419,4 @@ public class TileEntityCloakingDeviceCore extends WarpEnergyTE {
public boolean canInputEnergy(ForgeDirection from) { public boolean canInputEnergy(ForgeDirection from) {
return true; return true;
} }
@Override
public int getSinkTier() {
// TODO Auto-generated method stub
return 3;
}
@Override
public int getSourceTier() {
// TODO Auto-generated method stub
return 3;
}
} }

View file

@ -81,20 +81,20 @@ public class PacketHandler {
public static void sendFreqPacket(int dimensionId, int xCoord, int yCoord, int zCoord, int frequency) { public static void sendFreqPacket(int dimensionId, int xCoord, int yCoord, int zCoord, int frequency) {
FrequencyMessage frequencyMessage = new FrequencyMessage(xCoord, yCoord, zCoord, frequency); FrequencyMessage frequencyMessage = new FrequencyMessage(xCoord, yCoord, zCoord, frequency);
simpleNetworkManager.sendToAllAround(frequencyMessage, new TargetPoint(dimensionId, xCoord, yCoord, zCoord, 100)); simpleNetworkManager.sendToAllAround(frequencyMessage, new TargetPoint(dimensionId, xCoord, yCoord, zCoord, 100));
WarpDrive.debugPrint("Packet 'frequency' sent (" + xCoord + ", " + yCoord + ", " + zCoord + ") frequency " + frequency); WarpDrive.debugPrint("Sent frequency packet (" + xCoord + ", " + yCoord + ", " + zCoord + ") frequency " + frequency);
} }
// LaserCamera shooting at target (client -> server) // LaserCamera shooting at target (client -> server)
public static void sendLaserTargetingPacket(int x, int y, int z, float yaw, float pitch) { public static void sendLaserTargetingPacket(int x, int y, int z, float yaw, float pitch) {
TargetingMessage targetingMessage = new TargetingMessage(x, y, z, yaw, pitch); TargetingMessage targetingMessage = new TargetingMessage(x, y, z, yaw, pitch);
simpleNetworkManager.sendToServer(targetingMessage); simpleNetworkManager.sendToServer(targetingMessage);
WarpDrive.debugPrint("Packet 'targeting' sent (" + x + ", " + y + ", " + z + ") yaw " + yaw + " pitch " + pitch); WarpDrive.debugPrint("Sent targeting packet (" + x + ", " + y + ", " + z + ") yaw " + yaw + " pitch " + pitch);
} }
// Sending cloaking area definition (server -> client) // Sending cloaking area definition (server -> client)
public static void sendCloakPacket(EntityPlayer player, AxisAlignedBB aabb, int tier, boolean decloak) { public static void sendCloakPacket(EntityPlayer player, AxisAlignedBB aabb, int tier, boolean decloak) {
CloakMessage cloakMessage = new CloakMessage(aabb, tier, decloak); CloakMessage cloakMessage = new CloakMessage(aabb, tier, decloak);
simpleNetworkManager.sendTo(cloakMessage, (EntityPlayerMP) player); simpleNetworkManager.sendTo(cloakMessage, (EntityPlayerMP) player);
WarpDrive.debugPrint("Packet 'cloak' sent (aabb " + aabb + ") tier " + tier + " decloak " + decloak); WarpDrive.debugPrint("Sent cloak packet (aabb " + aabb + ") tier " + tier + " decloak " + decloak);
} }
} }