|
6 | 6 | import net.minecraft.client.Minecraft; |
7 | 7 | import net.minecraft.entity.Entity; |
8 | 8 | import net.minecraft.init.Blocks; |
9 | | -import net.minecraft.util.BlockPos; |
10 | | -import net.minecraft.util.Tuple; |
11 | | -import net.minecraft.util.Vec3; |
| 9 | +import net.minecraft.util.*; |
12 | 10 |
|
13 | 11 | import java.util.ArrayList; |
14 | 12 | import java.util.List; |
@@ -324,4 +322,104 @@ public static int getPitchRotationTime(float target_pitch, int angle_per_unit, i |
324 | 322 | return (int) Math.max(min, Math.abs(target_pitch - mc.thePlayer.rotationPitch) / angle_per_unit * time_per_unit); |
325 | 323 | } |
326 | 324 |
|
| 325 | + public static BlockPos bresenham(Vec3 start, Vec3 end) { |
| 326 | + int x1 = MathHelper.floor_double(end.xCoord); |
| 327 | + int y1 = MathHelper.floor_double(end.yCoord); |
| 328 | + int z1 = MathHelper.floor_double(end.zCoord); |
| 329 | + int x0 = MathHelper.floor_double(start.xCoord); |
| 330 | + int y0 = MathHelper.floor_double(start.yCoord); |
| 331 | + int z0 = MathHelper.floor_double(start.zCoord); |
| 332 | + |
| 333 | + if (mc.theWorld.getBlockState(new BlockPos(x0, y0, z0)).getBlock() != Blocks.air) { |
| 334 | + return new BlockPos(x0, y0, z0); |
| 335 | + } |
| 336 | + |
| 337 | + int iterations = 200; |
| 338 | + |
| 339 | + while (iterations-- >= 0) { |
| 340 | + if (x0 == x1 && y0 == y1 && z0 == z1) { |
| 341 | + return new BlockPos(end); |
| 342 | + } |
| 343 | + |
| 344 | + boolean hasNewX = true; |
| 345 | + boolean hasNewY = true; |
| 346 | + boolean hasNewZ = true; |
| 347 | + |
| 348 | + double newX = 999.0; |
| 349 | + double newY = 999.0; |
| 350 | + double newZ = 999.0; |
| 351 | + |
| 352 | + if (x1 > x0) { |
| 353 | + newX = (double) x0 + 1.0; |
| 354 | + } else if (x1 < x0) { |
| 355 | + newX = (double) x0 + 0.0; |
| 356 | + } else { |
| 357 | + hasNewX = false; |
| 358 | + } |
| 359 | + if (y1 > y0) { |
| 360 | + newY = (double) y0 + 1.0; |
| 361 | + } else if (y1 < y0) { |
| 362 | + newY = (double) y0 + 0.0; |
| 363 | + } else { |
| 364 | + hasNewY = false; |
| 365 | + } |
| 366 | + if (z1 > z0) { |
| 367 | + newZ = (double) z0 + 1.0; |
| 368 | + } else if (z1 < z0) { |
| 369 | + newZ = (double) z0 + 0.0; |
| 370 | + } else { |
| 371 | + hasNewZ = false; |
| 372 | + } |
| 373 | + |
| 374 | + double stepX = 999.0; |
| 375 | + double stepY = 999.0; |
| 376 | + double stepZ = 999.0; |
| 377 | + |
| 378 | + |
| 379 | + double dx = end.xCoord - start.xCoord; |
| 380 | + double dy = end.yCoord - start.yCoord; |
| 381 | + double dz = end.zCoord - start.zCoord; |
| 382 | + |
| 383 | + if (hasNewX) { |
| 384 | + stepX = (newX - start.xCoord) / dx; |
| 385 | + } |
| 386 | + if (hasNewY) { |
| 387 | + stepY = (newY - start.yCoord) / dy; |
| 388 | + } |
| 389 | + if (hasNewZ) { |
| 390 | + stepZ = (newZ - start.zCoord) / dz; |
| 391 | + } |
| 392 | + if (stepX == -0.0) { |
| 393 | + stepX = -1.0E-4; |
| 394 | + } |
| 395 | + if (stepY == -0.0) { |
| 396 | + stepY = -1.0E-4; |
| 397 | + } |
| 398 | + if (stepZ == -0.0) { |
| 399 | + stepZ = -1.0E-4; |
| 400 | + } |
| 401 | + |
| 402 | + EnumFacing enumfacing; |
| 403 | + if (stepX < stepY && stepX < stepZ) { |
| 404 | + enumfacing = x1 > x0 ? EnumFacing.WEST : EnumFacing.EAST; |
| 405 | + start = new Vec3(newX, start.yCoord + dy * stepX, start.zCoord + dz * stepX); |
| 406 | + } else if (stepY < stepZ) { |
| 407 | + enumfacing = y1 > y0 ? EnumFacing.DOWN : EnumFacing.UP; |
| 408 | + start = new Vec3(start.xCoord + dx * stepY, newY, start.zCoord + dz * stepY); |
| 409 | + } else { |
| 410 | + enumfacing = z1 > z0 ? EnumFacing.NORTH : EnumFacing.SOUTH; |
| 411 | + start = new Vec3(start.xCoord + dx * stepZ, start.yCoord + dy * stepZ, newZ); |
| 412 | + } |
| 413 | + x0 = MathHelper.floor_double(start.xCoord) - (enumfacing == EnumFacing.EAST ? 1 : 0); |
| 414 | + y0 = MathHelper.floor_double(start.yCoord) - (enumfacing == EnumFacing.UP ? 1 : 0); |
| 415 | + z0 = MathHelper.floor_double(start.zCoord) - (enumfacing == EnumFacing.SOUTH ? 1 : 0); |
| 416 | + |
| 417 | + if (mc.theWorld.getBlockState(new BlockPos(x0, y0, z0)).getBlock() != Blocks.air) { |
| 418 | + return new BlockPos(x0, y0, z0); |
| 419 | + } |
| 420 | + } |
| 421 | + |
| 422 | + return null; |
| 423 | + } |
| 424 | + |
327 | 425 | } |
0 commit comments