Advertisement
Arctic_Wolfy

BindingDungeon

Feb 19th, 2020
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.20 KB | None | 0 0
  1. package com.blizzard.bos.world.feature.dungeon;
  2.  
  3. import com.blizzard.bos.utils.Rect;
  4. import com.google.common.collect.Lists;
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockState;
  7. import net.minecraft.block.Blocks;
  8. import net.minecraft.network.PacketBuffer;
  9. import net.minecraft.util.Direction;
  10. import net.minecraft.util.math.BlockPos;
  11. import net.minecraft.util.math.Vec3i;
  12. import net.minecraft.world.World;
  13. import net.minecraftforge.common.util.Size2i;
  14.  
  15. import java.util.*;
  16. import java.util.function.BiFunction;
  17.  
  18.  
  19. public final class BindingDungeon {
  20.     private static List<BiFunction<Level, PacketBuffer, Level.Room>> ROOMS = Lists.newArrayList(
  21.             Level.Room::new
  22.     );
  23.  
  24.     private static Level.Room get(Level level, PacketBuffer buf) {
  25.         return ROOMS.get(buf.readInt()).apply(level, buf);
  26.     }
  27.  
  28.     private final Size2i UNIT = new Size2i(19, 5);
  29.     //private final Vec3i UNIT = new Vec3i(19, 5, 19);
  30.     private final World world;
  31.     private final BlockPos start;
  32.     private final Level[] levels;
  33.  
  34.     public BindingDungeon(World world, BlockPos start) {
  35.         this.world = world;
  36.         this.start = start;
  37.         Arrays.setAll(levels = new Level[3], y -> new Level(this, y));
  38.     }
  39.  
  40.     public BindingDungeon(World world, PacketBuffer buf) {
  41.         this.world = world;
  42.         start = buf.readBlockPos();
  43.         levels = new Level[buf.readInt()];
  44.         Arrays.setAll(levels, i -> new Level(this, buf));
  45.     }
  46.  
  47.     public void save(PacketBuffer buf) {
  48.         buf.writeBlockPos(start);
  49.         buf.writeInt(levels.length);
  50.         for (Level level : levels) level.save(buf);
  51.     }
  52.  
  53.     public void spawn() { for (Level level : levels) level.spawn(); }
  54.  
  55.     public final static class Level {
  56.         private final BindingDungeon dungeon;
  57.         private final HashMap<Vec3i, Room> rooms = new HashMap<>();
  58.         private final int y;
  59.  
  60.         public Level(BindingDungeon dungeon, int y) {
  61.             this.dungeon = dungeon;
  62.             this.y = y;
  63.             for (int x = -1; x <= 1; x++) {
  64.                 for (int z = -1; z <= 1; z++) {
  65.                     add(new Room(this, x, z, 1, 1));
  66.                 }
  67.             }
  68.             add(new Room(this, -1, 2, y + 1, y + 1));
  69.         }
  70.  
  71.         private Level(BindingDungeon dungeon, PacketBuffer buf) {
  72.             this.dungeon = dungeon;
  73.             y = buf.readInt();
  74.             int count = buf.readInt();
  75.             for (int i = 0; i < count; i++) {
  76.                 add(get(this, buf));
  77.             }
  78.         }
  79.  
  80.         public void save(PacketBuffer buf) {
  81.             buf.writeInt(y);
  82.             Room[] rooms = this.rooms.values().stream().sorted()
  83.                     .distinct().toArray(Room[]::new);
  84.             buf.writeInt(rooms.length);
  85.             for (Room room : rooms) room.save(buf);
  86.         }
  87.  
  88.         private void add(Room room) {
  89.             Rect size = new Rect(room.x, room.z, room.sizeX, room.sizeZ);
  90.             for (int x = size.getMinX(); x < size.getMaxX(); x++) {
  91.                 for (int z = size.getMinZ(); z < size.getMaxZ(); z++) {
  92.                     Vec3i pos = new Vec3i(x, 0, z);
  93.                     rooms.computeIfPresent(pos, (p, v) -> {
  94.                         throw new IllegalStateException(room+" overlaps with "+v);
  95.                     });
  96.                     rooms.put(pos, room);
  97.                 }
  98.             }
  99.         }
  100.  
  101.         private void spawn() { rooms.values().stream().sorted().distinct().forEach(Room::spawn); }
  102.  
  103.         public static class Room {
  104.             protected final Level level;
  105.             protected final int x, z, sizeX, sizeZ;
  106.  
  107.             public Room(Level level, int x, int z, int sizeX, int sizeZ) {
  108.                 this.level = level;
  109.                 this.x = x;
  110.                 this.z = z;
  111.                 this.sizeX = sizeX;
  112.                 this.sizeZ = sizeZ;
  113.             }
  114.  
  115.             protected Room(Level level, PacketBuffer buf) {
  116.                 this.level = level;
  117.                 x = buf.readInt();
  118.                 z = buf.readInt();
  119.                 sizeX = buf.readInt();
  120.                 sizeZ = buf.readInt();
  121.             }
  122.  
  123.             public void save(PacketBuffer buf) {
  124.                 buf.writeInt(getType());
  125.                 buf.writeInt(x);
  126.                 buf.writeInt(z);
  127.                 buf.writeInt(sizeX);
  128.                 buf.writeInt(sizeZ);
  129.             }
  130.  
  131.             protected int getType() { return 0; }
  132.  
  133.             protected final Size2i getUNIT() { return level.dungeon.UNIT; }
  134.             protected final BlockPos getStart() { return level.dungeon.start; }
  135.             protected final int getY() { return level.y; }
  136.             protected final World getWorld() { return level.dungeon.world; }
  137.  
  138.             public BlockPos getCenter() {
  139.                 BlockPos.Mutable center = new BlockPos.Mutable(getStart());
  140.                 center.move(
  141.                         x *  (getUNIT().width),
  142.                         getY() * -(getUNIT().height),
  143.                         z *  (getUNIT().width)
  144.                 );
  145.                 return center.toImmutable();
  146.             }
  147.  
  148.             protected Rect getSize() {
  149.                 return new Rect(
  150.                         -(getUNIT().width / 2), -(getUNIT().width / 2),
  151.                         (sizeX * (getUNIT().width)) - 1,
  152.                         (sizeZ * (getUNIT().width)) - 1
  153.                 );
  154.             }
  155.  
  156.             protected void spawn() {
  157.                 BlockPos center = getCenter();
  158.                 setBlockState(center.add(0, getUNIT().height - 1, 0), Color.GRAY);
  159.                 Rect size = getSize();
  160.                 for (int x = size.getMinX(); x <= size.getMaxX(); x++) {
  161.                     for (int z = size.getMinZ(); z <= size.getMaxZ(); z++) {
  162.                         Color color = Color.WHITE;
  163.                         boolean sX = size.isSideX(x);
  164.                         boolean sZ = size.isSideZ(z);
  165.                         if (x == 0 && z == 0) color = Color.GRAY;
  166.                         else if (sX && sZ) color = Color.PURPLE;
  167.                         else if (sX) color = Color.RED;
  168.                         else if (sZ) color = Color.BLUE;
  169.                         setBlockState(center.add(x, 0, z), color);
  170.                     }
  171.                 }
  172.                 for (int x = 0; x < sizeX; x++) {
  173.                     setBlockState(getDoor(Direction.EAST, x), Color.BLACK);
  174.                     //int px = x * (UNIT.getX());
  175.                     //world.setBlockState(center.add(px, 0, size.getMinZ()), getMarker(Color.BLACK), 0);
  176.                     //world.setBlockState(center.add(px, 0, size.getMaxZ()), getMarker(Color.BLACK), 0);
  177.                 }
  178.                 for (int z = 0; z < sizeZ; z++) {
  179.                     //int pz = z * (UNIT.getZ());
  180.                     //world.setBlockState(center.add(size.getMinX(), 0, pz), getMarker(Color.BLACK), 0);
  181.                     //world.setBlockState(center.add(size.getMaxX(), 0, pz), getMarker(Color.BLACK), 0);
  182.                 }
  183.                 /*for (int y = 1; y < UNIT.getY() - 1; y++)
  184.                     world.setBlockState(center.up(y), Blocks.LADDER.getDefaultState(), 0);*/
  185.             }
  186.  
  187.             protected boolean setBlockState(BlockPos pos, Color color) {
  188.                 return setBlockState(pos, getMarker(color));
  189.             }
  190.  
  191.             protected boolean setBlockState(BlockPos pos, Block block) {
  192.                 return setBlockState(pos, block.getDefaultState());
  193.             }
  194.  
  195.             protected boolean setBlockState(BlockPos pos, BlockState state) {
  196.                 return getWorld().setBlockState(pos, state, 0);
  197.             }
  198.  
  199.             private IndexOutOfBoundsException error(Direction.Axis axis, int expected, int supplied) {
  200.                 return new IndexOutOfBoundsException(String.format(
  201.                         "%s index must be less then %d got %d", axis, expected, supplied
  202.                 ));
  203.             }
  204.  
  205.             private IndexOutOfBoundsException error(int supplied) {
  206.                 return new IndexOutOfBoundsException(String.format(
  207.                         "Index must be non-negative got %d", supplied
  208.                 ));
  209.             }
  210.  
  211.             public BlockPos getDoor(Direction side, int doorIndex) {
  212.                 if (doorIndex < 0) throw error(doorIndex);
  213.                 BlockPos center = getCenter();
  214.                 Vec3i a, b1, b2;
  215.                 Rect size = getSize();
  216.                 int pos = doorIndex * getUNIT().width;
  217.                 switch (side.getAxis()) {
  218.                     case X:
  219.                         if (doorIndex >= sizeZ) throw error(side.getAxis(), sizeZ, doorIndex);
  220.                         a = new Vec3i(0, 0, pos);
  221.                         b1 = new Vec3i(size.getMinX(), 0, 0);
  222.                         b2 = new Vec3i(size.getMaxX(), 0, 0);
  223.                         break;
  224.                     case Z:
  225.                         if (doorIndex >= sizeX) throw error(side.getAxis(), sizeX, doorIndex);
  226.                         a = new Vec3i(pos, 0, 0);
  227.                         b1 = new Vec3i(0, 0, size.getMinZ());
  228.                         b2 = new Vec3i(0, 0, size.getMaxZ());
  229.                         break;
  230.                     default: throw new IllegalArgumentException("Expected horizontal axis!");
  231.                 }
  232.                 if (side.getAxisDirection() == Direction.AxisDirection.NEGATIVE)
  233.                     return center.add(a).add(b1);
  234.                 else return center.add(a).add(b2);
  235.             }
  236.  
  237.             @Override
  238.             public String toString() {
  239.                 return "Room{" + "x=" + x + ", z=" + z +
  240.                         ", sizeX=" + sizeX + ", sizeZ=" + sizeZ + '}';
  241.             }
  242.  
  243.             @Override
  244.             public boolean equals(Object o) {
  245.                 if (this == o) return true;
  246.                 if (o == null || getClass() != o.getClass()) return false;
  247.                 Room room = (Room) o;
  248.                 return x == room.x && z == room.z &&
  249.                         sizeX == room.sizeX && sizeZ == room.sizeZ;
  250.             }
  251.  
  252.             @Override
  253.             public int hashCode() {
  254.                 return Objects.hash(x, z, sizeX, sizeZ);
  255.             }
  256.         }
  257.     }
  258.  
  259.     enum Color {
  260.         WHITE,ORANGE,MAGENTA,LIGHT_BLUE,
  261.         YELLOW,LIME,PINK,GRAY,
  262.         LIGHT_GRAY,CYAN,PURPLE,
  263.         BLUE,BROWN,GREEN,RED, BLACK
  264.     }
  265.  
  266.     private static BlockState getMarker(Color color) {
  267.         switch (color) {
  268.             case WHITE: return Blocks.WHITE_WOOL.getDefaultState();
  269.             case ORANGE: return Blocks.ORANGE_WOOL.getDefaultState();
  270.             case MAGENTA: return Blocks.MAGENTA_WOOL.getDefaultState();
  271.             case LIGHT_BLUE: return Blocks.LIGHT_BLUE_WOOL.getDefaultState();
  272.             case YELLOW: return Blocks.YELLOW_WOOL.getDefaultState();
  273.             case LIME: return Blocks.LIME_WOOL.getDefaultState();
  274.             case PINK: return Blocks.PINK_WOOL.getDefaultState();
  275.             case GRAY: return Blocks.GRAY_WOOL.getDefaultState();
  276.             case LIGHT_GRAY: return Blocks.LIGHT_GRAY_WOOL.getDefaultState();
  277.             case CYAN: return Blocks.CYAN_WOOL.getDefaultState();
  278.             case PURPLE: return Blocks.PURPLE_WOOL.getDefaultState();
  279.             case BLUE: return Blocks.BLUE_WOOL.getDefaultState();
  280.             case BROWN: return Blocks.BROWN_WOOL.getDefaultState();
  281.             case GREEN: return Blocks.GREEN_WOOL.getDefaultState();
  282.             case RED: return Blocks.RED_WOOL.getDefaultState();
  283.             case BLACK: return Blocks.BLACK_WOOL.getDefaultState();
  284.             default: return Blocks.AIR.getDefaultState();
  285.         }
  286.     }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement