]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/common/models/BoundingBoxVillage.java
c2d218077a53c83864c0ac846a6ca77dfd274aa4
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / common / models / BoundingBoxVillage.java
1 package com.irtimaled.bbor.common.models;
2
3 import com.irtimaled.bbor.common.BoundingBoxType;
4 import com.irtimaled.bbor.common.TypeHelper;
5 import com.irtimaled.bbor.common.VillageColorCache;
6 import com.irtimaled.bbor.common.VillageHelper;
7
8 import java.awt.*;
9 import java.util.Set;
10
11 public class BoundingBoxVillage extends BoundingBoxSphere {
12     private final boolean spawnsIronGolems;
13     private final Color color;
14     private final Coords center;
15     private final Set<Coords> doors;
16     private final int villageHash;
17
18     private BoundingBoxVillage(Point point, Integer radius, Color color, boolean spawnsIronGolems, Set<Coords> doors) {
19         super(point, radius, BoundingBoxType.VillageSpheres);
20         this.center = point.getCoords();
21         this.color = color;
22         this.spawnsIronGolems = spawnsIronGolems;
23         this.doors = doors;
24         this.villageHash = VillageHelper.computeHash(this.center, radius, spawnsIronGolems, doors);
25     }
26
27     public static BoundingBoxVillage from(Coords center, Integer radius, Color color, boolean spawnsIronGolems, Set<Coords> doors) {
28         Point point = calculateCenterPoint(center, doors);
29         return new BoundingBoxVillage(point, radius, color, spawnsIronGolems, doors);
30     }
31
32     public static BoundingBoxVillage from(Coords center, Integer radius, int villageId, int population, Set<Coords> doors) {
33         boolean spawnsIronGolems = VillageHelper.shouldSpawnIronGolems(population, doors.size());
34         Color color = VillageColorCache.getColor(villageId);
35         return from(center, radius, color, spawnsIronGolems, doors);
36     }
37
38     private static Point calculateCenterPoint(Coords center, Set<Coords> doors) {
39         boolean processedFirstDoor = false;
40         int minX = 0;
41         int maxX = 0;
42         int minZ = 0;
43         int maxZ = 0;
44         for (Coords door : doors) {
45             if (!processedFirstDoor ||
46                     (minX > door.getX()))
47                 minX = door.getX();
48             if (!processedFirstDoor ||
49                     maxX < door.getX())
50                 maxX = door.getX();
51             if (!processedFirstDoor ||
52                     minZ > door.getZ())
53                 minZ = door.getZ();
54             if (!processedFirstDoor ||
55                     maxZ < door.getZ())
56                 maxZ = door.getZ();
57
58             processedFirstDoor = true;
59         }
60
61         double x = Math.abs(maxX - minX) % 2 == 0 ? 0.5 : (minX < 0 ? 0 : 1);
62         double z = Math.abs(maxZ - minZ) % 2 == 0 ? 0.5 : (minZ < 0 ? 0 : 1);
63         return new Point(center).offset(x, 0.0D, z);
64     }
65
66     public Color getColor() {
67         return color;
68     }
69
70     @Override
71     public int hashCode() {
72         return TypeHelper.combineHashCodes(super.hashCode(), villageHash);
73     }
74
75     public boolean getSpawnsIronGolems() {
76         return spawnsIronGolems;
77     }
78
79     public Set<Coords> getDoors() {
80         return doors;
81     }
82
83     public int getVillageHash() {
84         return villageHash;
85     }
86
87     public Coords getCenter() {
88         return center;
89     }
90 }