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