001package net.minecraft.village; 002 003import java.util.ArrayList; 004import java.util.Iterator; 005import java.util.List; 006import java.util.TreeMap; 007import net.minecraft.block.Block; 008import net.minecraft.entity.EntityLiving; 009import net.minecraft.entity.monster.EntityIronGolem; 010import net.minecraft.entity.passive.EntityVillager; 011import net.minecraft.entity.player.EntityPlayer; 012import net.minecraft.nbt.NBTTagCompound; 013import net.minecraft.nbt.NBTTagList; 014import net.minecraft.util.AxisAlignedBB; 015import net.minecraft.util.ChunkCoordinates; 016import net.minecraft.util.MathHelper; 017import net.minecraft.util.Vec3; 018import net.minecraft.world.World; 019 020public class Village 021{ 022 private World worldObj; 023 024 /** list of VillageDoorInfo objects */ 025 private final List villageDoorInfoList = new ArrayList(); 026 027 /** 028 * This is the sum of all door coordinates and used to calculate the actual village center by dividing by the number 029 * of doors. 030 */ 031 private final ChunkCoordinates centerHelper = new ChunkCoordinates(0, 0, 0); 032 033 /** This is the actual village center. */ 034 private final ChunkCoordinates center = new ChunkCoordinates(0, 0, 0); 035 private int villageRadius = 0; 036 private int lastAddDoorTimestamp = 0; 037 private int tickCounter = 0; 038 private int numVillagers = 0; 039 040 /** Timestamp of tick count when villager last bred */ 041 private int noBreedTicks; 042 043 /** List of player reputations with this village */ 044 private TreeMap playerReputation = new TreeMap(); 045 private List villageAgressors = new ArrayList(); 046 private int numIronGolems = 0; 047 048 public Village() {} 049 050 public Village(World par1World) 051 { 052 this.worldObj = par1World; 053 } 054 055 public void func_82691_a(World par1World) 056 { 057 this.worldObj = par1World; 058 } 059 060 /** 061 * Called periodically by VillageCollection 062 */ 063 public void tick(int par1) 064 { 065 this.tickCounter = par1; 066 this.removeDeadAndOutOfRangeDoors(); 067 this.removeDeadAndOldAgressors(); 068 069 if (par1 % 20 == 0) 070 { 071 this.updateNumVillagers(); 072 } 073 074 if (par1 % 30 == 0) 075 { 076 this.updateNumIronGolems(); 077 } 078 079 int j = this.numVillagers / 10; 080 081 if (this.numIronGolems < j && this.villageDoorInfoList.size() > 20 && this.worldObj.rand.nextInt(7000) == 0) 082 { 083 Vec3 vec3 = this.tryGetIronGolemSpawningLocation(MathHelper.floor_float((float)this.center.posX), MathHelper.floor_float((float)this.center.posY), MathHelper.floor_float((float)this.center.posZ), 2, 4, 2); 084 085 if (vec3 != null) 086 { 087 EntityIronGolem entityirongolem = new EntityIronGolem(this.worldObj); 088 entityirongolem.setPosition(vec3.xCoord, vec3.yCoord, vec3.zCoord); 089 this.worldObj.spawnEntityInWorld(entityirongolem); 090 ++this.numIronGolems; 091 } 092 } 093 } 094 095 /** 096 * Tries up to 10 times to get a valid spawning location before eventually failing and returning null. 097 */ 098 private Vec3 tryGetIronGolemSpawningLocation(int par1, int par2, int par3, int par4, int par5, int par6) 099 { 100 for (int k1 = 0; k1 < 10; ++k1) 101 { 102 int l1 = par1 + this.worldObj.rand.nextInt(16) - 8; 103 int i2 = par2 + this.worldObj.rand.nextInt(6) - 3; 104 int j2 = par3 + this.worldObj.rand.nextInt(16) - 8; 105 106 if (this.isInRange(l1, i2, j2) && this.isValidIronGolemSpawningLocation(l1, i2, j2, par4, par5, par6)) 107 { 108 return this.worldObj.getWorldVec3Pool().getVecFromPool((double)l1, (double)i2, (double)j2); 109 } 110 } 111 112 return null; 113 } 114 115 private boolean isValidIronGolemSpawningLocation(int par1, int par2, int par3, int par4, int par5, int par6) 116 { 117 if (!this.worldObj.doesBlockHaveSolidTopSurface(par1, par2 - 1, par3)) 118 { 119 return false; 120 } 121 else 122 { 123 int k1 = par1 - par4 / 2; 124 int l1 = par3 - par6 / 2; 125 126 for (int i2 = k1; i2 < k1 + par4; ++i2) 127 { 128 for (int j2 = par2; j2 < par2 + par5; ++j2) 129 { 130 for (int k2 = l1; k2 < l1 + par6; ++k2) 131 { 132 if (this.worldObj.isBlockNormalCube(i2, j2, k2)) 133 { 134 return false; 135 } 136 } 137 } 138 } 139 140 return true; 141 } 142 } 143 144 private void updateNumIronGolems() 145 { 146 List list = this.worldObj.getEntitiesWithinAABB(EntityIronGolem.class, AxisAlignedBB.getAABBPool().getAABB((double)(this.center.posX - this.villageRadius), (double)(this.center.posY - 4), (double)(this.center.posZ - this.villageRadius), (double)(this.center.posX + this.villageRadius), (double)(this.center.posY + 4), (double)(this.center.posZ + this.villageRadius))); 147 this.numIronGolems = list.size(); 148 } 149 150 private void updateNumVillagers() 151 { 152 List list = this.worldObj.getEntitiesWithinAABB(EntityVillager.class, AxisAlignedBB.getAABBPool().getAABB((double)(this.center.posX - this.villageRadius), (double)(this.center.posY - 4), (double)(this.center.posZ - this.villageRadius), (double)(this.center.posX + this.villageRadius), (double)(this.center.posY + 4), (double)(this.center.posZ + this.villageRadius))); 153 this.numVillagers = list.size(); 154 155 if (this.numVillagers == 0) 156 { 157 this.playerReputation.clear(); 158 } 159 } 160 161 public ChunkCoordinates getCenter() 162 { 163 return this.center; 164 } 165 166 public int getVillageRadius() 167 { 168 return this.villageRadius; 169 } 170 171 /** 172 * Actually get num village door info entries, but that boils down to number of doors. Called by 173 * EntityAIVillagerMate and VillageSiege 174 */ 175 public int getNumVillageDoors() 176 { 177 return this.villageDoorInfoList.size(); 178 } 179 180 public int getTicksSinceLastDoorAdding() 181 { 182 return this.tickCounter - this.lastAddDoorTimestamp; 183 } 184 185 public int getNumVillagers() 186 { 187 return this.numVillagers; 188 } 189 190 /** 191 * Returns true, if the given coordinates are within the bounding box of the village. 192 */ 193 public boolean isInRange(int par1, int par2, int par3) 194 { 195 return this.center.getDistanceSquared(par1, par2, par3) < (float)(this.villageRadius * this.villageRadius); 196 } 197 198 /** 199 * called only by class EntityAIMoveThroughVillage 200 */ 201 public List getVillageDoorInfoList() 202 { 203 return this.villageDoorInfoList; 204 } 205 206 public VillageDoorInfo findNearestDoor(int par1, int par2, int par3) 207 { 208 VillageDoorInfo villagedoorinfo = null; 209 int l = Integer.MAX_VALUE; 210 Iterator iterator = this.villageDoorInfoList.iterator(); 211 212 while (iterator.hasNext()) 213 { 214 VillageDoorInfo villagedoorinfo1 = (VillageDoorInfo)iterator.next(); 215 int i1 = villagedoorinfo1.getDistanceSquared(par1, par2, par3); 216 217 if (i1 < l) 218 { 219 villagedoorinfo = villagedoorinfo1; 220 l = i1; 221 } 222 } 223 224 return villagedoorinfo; 225 } 226 227 /** 228 * Find a door suitable for shelter. If there are more doors in a distance of 16 blocks, then the least restricted 229 * one (i.e. the one protecting the lowest number of villagers) of them is chosen, else the nearest one regardless 230 * of restriction. 231 */ 232 public VillageDoorInfo findNearestDoorUnrestricted(int par1, int par2, int par3) 233 { 234 VillageDoorInfo villagedoorinfo = null; 235 int l = Integer.MAX_VALUE; 236 Iterator iterator = this.villageDoorInfoList.iterator(); 237 238 while (iterator.hasNext()) 239 { 240 VillageDoorInfo villagedoorinfo1 = (VillageDoorInfo)iterator.next(); 241 int i1 = villagedoorinfo1.getDistanceSquared(par1, par2, par3); 242 243 if (i1 > 256) 244 { 245 i1 *= 1000; 246 } 247 else 248 { 249 i1 = villagedoorinfo1.getDoorOpeningRestrictionCounter(); 250 } 251 252 if (i1 < l) 253 { 254 villagedoorinfo = villagedoorinfo1; 255 l = i1; 256 } 257 } 258 259 return villagedoorinfo; 260 } 261 262 public VillageDoorInfo getVillageDoorAt(int par1, int par2, int par3) 263 { 264 if (this.center.getDistanceSquared(par1, par2, par3) > (float)(this.villageRadius * this.villageRadius)) 265 { 266 return null; 267 } 268 else 269 { 270 Iterator iterator = this.villageDoorInfoList.iterator(); 271 VillageDoorInfo villagedoorinfo; 272 273 do 274 { 275 if (!iterator.hasNext()) 276 { 277 return null; 278 } 279 280 villagedoorinfo = (VillageDoorInfo)iterator.next(); 281 } 282 while (villagedoorinfo.posX != par1 || villagedoorinfo.posZ != par3 || Math.abs(villagedoorinfo.posY - par2) > 1); 283 284 return villagedoorinfo; 285 } 286 } 287 288 public void addVillageDoorInfo(VillageDoorInfo par1VillageDoorInfo) 289 { 290 this.villageDoorInfoList.add(par1VillageDoorInfo); 291 this.centerHelper.posX += par1VillageDoorInfo.posX; 292 this.centerHelper.posY += par1VillageDoorInfo.posY; 293 this.centerHelper.posZ += par1VillageDoorInfo.posZ; 294 this.updateVillageRadiusAndCenter(); 295 this.lastAddDoorTimestamp = par1VillageDoorInfo.lastActivityTimestamp; 296 } 297 298 /** 299 * Returns true, if there is not a single village door left. Called by VillageCollection 300 */ 301 public boolean isAnnihilated() 302 { 303 return this.villageDoorInfoList.isEmpty(); 304 } 305 306 public void addOrRenewAgressor(EntityLiving par1EntityLiving) 307 { 308 Iterator iterator = this.villageAgressors.iterator(); 309 VillageAgressor villageagressor; 310 311 do 312 { 313 if (!iterator.hasNext()) 314 { 315 this.villageAgressors.add(new VillageAgressor(this, par1EntityLiving, this.tickCounter)); 316 return; 317 } 318 319 villageagressor = (VillageAgressor)iterator.next(); 320 } 321 while (villageagressor.agressor != par1EntityLiving); 322 323 villageagressor.agressionTime = this.tickCounter; 324 } 325 326 public EntityLiving findNearestVillageAggressor(EntityLiving par1EntityLiving) 327 { 328 double d0 = Double.MAX_VALUE; 329 VillageAgressor villageagressor = null; 330 331 for (int i = 0; i < this.villageAgressors.size(); ++i) 332 { 333 VillageAgressor villageagressor1 = (VillageAgressor)this.villageAgressors.get(i); 334 double d1 = villageagressor1.agressor.getDistanceSqToEntity(par1EntityLiving); 335 336 if (d1 <= d0) 337 { 338 villageagressor = villageagressor1; 339 d0 = d1; 340 } 341 } 342 343 return villageagressor != null ? villageagressor.agressor : null; 344 } 345 346 public EntityPlayer func_82685_c(EntityLiving par1EntityLiving) 347 { 348 double d0 = Double.MAX_VALUE; 349 EntityPlayer entityplayer = null; 350 Iterator iterator = this.playerReputation.keySet().iterator(); 351 352 while (iterator.hasNext()) 353 { 354 String s = (String)iterator.next(); 355 356 if (this.isPlayerReputationTooLow(s)) 357 { 358 EntityPlayer entityplayer1 = this.worldObj.getPlayerEntityByName(s); 359 360 if (entityplayer1 != null) 361 { 362 double d1 = entityplayer1.getDistanceSqToEntity(par1EntityLiving); 363 364 if (d1 <= d0) 365 { 366 entityplayer = entityplayer1; 367 d0 = d1; 368 } 369 } 370 } 371 } 372 373 return entityplayer; 374 } 375 376 private void removeDeadAndOldAgressors() 377 { 378 Iterator iterator = this.villageAgressors.iterator(); 379 380 while (iterator.hasNext()) 381 { 382 VillageAgressor villageagressor = (VillageAgressor)iterator.next(); 383 384 if (!villageagressor.agressor.isEntityAlive() || Math.abs(this.tickCounter - villageagressor.agressionTime) > 300) 385 { 386 iterator.remove(); 387 } 388 } 389 } 390 391 private void removeDeadAndOutOfRangeDoors() 392 { 393 boolean flag = false; 394 boolean flag1 = this.worldObj.rand.nextInt(50) == 0; 395 Iterator iterator = this.villageDoorInfoList.iterator(); 396 397 while (iterator.hasNext()) 398 { 399 VillageDoorInfo villagedoorinfo = (VillageDoorInfo)iterator.next(); 400 401 if (flag1) 402 { 403 villagedoorinfo.resetDoorOpeningRestrictionCounter(); 404 } 405 406 if (!this.isBlockDoor(villagedoorinfo.posX, villagedoorinfo.posY, villagedoorinfo.posZ) || Math.abs(this.tickCounter - villagedoorinfo.lastActivityTimestamp) > 1200) 407 { 408 this.centerHelper.posX -= villagedoorinfo.posX; 409 this.centerHelper.posY -= villagedoorinfo.posY; 410 this.centerHelper.posZ -= villagedoorinfo.posZ; 411 flag = true; 412 villagedoorinfo.isDetachedFromVillageFlag = true; 413 iterator.remove(); 414 } 415 } 416 417 if (flag) 418 { 419 this.updateVillageRadiusAndCenter(); 420 } 421 } 422 423 private boolean isBlockDoor(int par1, int par2, int par3) 424 { 425 int l = this.worldObj.getBlockId(par1, par2, par3); 426 return l <= 0 ? false : l == Block.doorWood.blockID; 427 } 428 429 private void updateVillageRadiusAndCenter() 430 { 431 int i = this.villageDoorInfoList.size(); 432 433 if (i == 0) 434 { 435 this.center.set(0, 0, 0); 436 this.villageRadius = 0; 437 } 438 else 439 { 440 this.center.set(this.centerHelper.posX / i, this.centerHelper.posY / i, this.centerHelper.posZ / i); 441 int j = 0; 442 VillageDoorInfo villagedoorinfo; 443 444 for (Iterator iterator = this.villageDoorInfoList.iterator(); iterator.hasNext(); j = Math.max(villagedoorinfo.getDistanceSquared(this.center.posX, this.center.posY, this.center.posZ), j)) 445 { 446 villagedoorinfo = (VillageDoorInfo)iterator.next(); 447 } 448 449 this.villageRadius = Math.max(32, (int)Math.sqrt((double)j) + 1); 450 } 451 } 452 453 /** 454 * Return the village reputation for a player 455 */ 456 public int getReputationForPlayer(String par1Str) 457 { 458 Integer integer = (Integer)this.playerReputation.get(par1Str); 459 return integer != null ? integer.intValue() : 0; 460 } 461 462 /** 463 * Set the village reputation for a player. 464 */ 465 public int setReputationForPlayer(String par1Str, int par2) 466 { 467 int j = this.getReputationForPlayer(par1Str); 468 int k = MathHelper.clamp_int(j + par2, -30, 10); 469 this.playerReputation.put(par1Str, Integer.valueOf(k)); 470 return k; 471 } 472 473 /** 474 * Return whether this player has a too low reputation with this village. 475 */ 476 public boolean isPlayerReputationTooLow(String par1Str) 477 { 478 return this.getReputationForPlayer(par1Str) <= -15; 479 } 480 481 /** 482 * Read this village's data from NBT. 483 */ 484 public void readVillageDataFromNBT(NBTTagCompound par1NBTTagCompound) 485 { 486 this.numVillagers = par1NBTTagCompound.getInteger("PopSize"); 487 this.villageRadius = par1NBTTagCompound.getInteger("Radius"); 488 this.numIronGolems = par1NBTTagCompound.getInteger("Golems"); 489 this.lastAddDoorTimestamp = par1NBTTagCompound.getInteger("Stable"); 490 this.tickCounter = par1NBTTagCompound.getInteger("Tick"); 491 this.noBreedTicks = par1NBTTagCompound.getInteger("MTick"); 492 this.center.posX = par1NBTTagCompound.getInteger("CX"); 493 this.center.posY = par1NBTTagCompound.getInteger("CY"); 494 this.center.posZ = par1NBTTagCompound.getInteger("CZ"); 495 this.centerHelper.posX = par1NBTTagCompound.getInteger("ACX"); 496 this.centerHelper.posY = par1NBTTagCompound.getInteger("ACY"); 497 this.centerHelper.posZ = par1NBTTagCompound.getInteger("ACZ"); 498 NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Doors"); 499 500 for (int i = 0; i < nbttaglist.tagCount(); ++i) 501 { 502 NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i); 503 VillageDoorInfo villagedoorinfo = new VillageDoorInfo(nbttagcompound1.getInteger("X"), nbttagcompound1.getInteger("Y"), nbttagcompound1.getInteger("Z"), nbttagcompound1.getInteger("IDX"), nbttagcompound1.getInteger("IDZ"), nbttagcompound1.getInteger("TS")); 504 this.villageDoorInfoList.add(villagedoorinfo); 505 } 506 507 NBTTagList nbttaglist1 = par1NBTTagCompound.getTagList("Players"); 508 509 for (int j = 0; j < nbttaglist1.tagCount(); ++j) 510 { 511 NBTTagCompound nbttagcompound2 = (NBTTagCompound)nbttaglist1.tagAt(j); 512 this.playerReputation.put(nbttagcompound2.getString("Name"), Integer.valueOf(nbttagcompound2.getInteger("S"))); 513 } 514 } 515 516 /** 517 * Write this village's data to NBT. 518 */ 519 public void writeVillageDataToNBT(NBTTagCompound par1NBTTagCompound) 520 { 521 par1NBTTagCompound.setInteger("PopSize", this.numVillagers); 522 par1NBTTagCompound.setInteger("Radius", this.villageRadius); 523 par1NBTTagCompound.setInteger("Golems", this.numIronGolems); 524 par1NBTTagCompound.setInteger("Stable", this.lastAddDoorTimestamp); 525 par1NBTTagCompound.setInteger("Tick", this.tickCounter); 526 par1NBTTagCompound.setInteger("MTick", this.noBreedTicks); 527 par1NBTTagCompound.setInteger("CX", this.center.posX); 528 par1NBTTagCompound.setInteger("CY", this.center.posY); 529 par1NBTTagCompound.setInteger("CZ", this.center.posZ); 530 par1NBTTagCompound.setInteger("ACX", this.centerHelper.posX); 531 par1NBTTagCompound.setInteger("ACY", this.centerHelper.posY); 532 par1NBTTagCompound.setInteger("ACZ", this.centerHelper.posZ); 533 NBTTagList nbttaglist = new NBTTagList("Doors"); 534 Iterator iterator = this.villageDoorInfoList.iterator(); 535 536 while (iterator.hasNext()) 537 { 538 VillageDoorInfo villagedoorinfo = (VillageDoorInfo)iterator.next(); 539 NBTTagCompound nbttagcompound1 = new NBTTagCompound("Door"); 540 nbttagcompound1.setInteger("X", villagedoorinfo.posX); 541 nbttagcompound1.setInteger("Y", villagedoorinfo.posY); 542 nbttagcompound1.setInteger("Z", villagedoorinfo.posZ); 543 nbttagcompound1.setInteger("IDX", villagedoorinfo.insideDirectionX); 544 nbttagcompound1.setInteger("IDZ", villagedoorinfo.insideDirectionZ); 545 nbttagcompound1.setInteger("TS", villagedoorinfo.lastActivityTimestamp); 546 nbttaglist.appendTag(nbttagcompound1); 547 } 548 549 par1NBTTagCompound.setTag("Doors", nbttaglist); 550 NBTTagList nbttaglist1 = new NBTTagList("Players"); 551 Iterator iterator1 = this.playerReputation.keySet().iterator(); 552 553 while (iterator1.hasNext()) 554 { 555 String s = (String)iterator1.next(); 556 NBTTagCompound nbttagcompound2 = new NBTTagCompound(s); 557 nbttagcompound2.setString("Name", s); 558 nbttagcompound2.setInteger("S", ((Integer)this.playerReputation.get(s)).intValue()); 559 nbttaglist1.appendTag(nbttagcompound2); 560 } 561 562 par1NBTTagCompound.setTag("Players", nbttaglist1); 563 } 564 565 /** 566 * Prevent villager breeding for a fixed interval of time 567 */ 568 public void endMatingSeason() 569 { 570 this.noBreedTicks = this.tickCounter; 571 } 572 573 /** 574 * Return whether villagers mating refractory period has passed 575 */ 576 public boolean isMatingSeason() 577 { 578 return this.noBreedTicks == 0 || this.tickCounter - this.noBreedTicks >= 3600; 579 } 580 581 public void func_82683_b(int par1) 582 { 583 Iterator iterator = this.playerReputation.keySet().iterator(); 584 585 while (iterator.hasNext()) 586 { 587 String s = (String)iterator.next(); 588 this.setReputationForPlayer(s, par1); 589 } 590 } 591}