001 package net.minecraft.src; 002 003 import java.util.Random; 004 005 public class RandomPositionGenerator 006 { 007 /** 008 * used to store a driection when the user passes a point to move towards or away from. WARNING: NEVER THREAD SAFE. 009 * MULTIPLE findTowards and findAway calls, will share this var 010 */ 011 private static Vec3 staticVector = Vec3.createVectorHelper(0.0D, 0.0D, 0.0D); 012 013 /** 014 * finds a random target within par1(x,z) and par2 (y) blocks 015 */ 016 public static Vec3 findRandomTarget(EntityCreature par0EntityCreature, int par1, int par2) 017 { 018 return findRandomTargetBlock(par0EntityCreature, par1, par2, (Vec3)null); 019 } 020 021 /** 022 * finds a random target within par1(x,z) and par2 (y) blocks in the direction of the point par3 023 */ 024 public static Vec3 findRandomTargetBlockTowards(EntityCreature par0EntityCreature, int par1, int par2, Vec3 par3Vec3) 025 { 026 staticVector.xCoord = par3Vec3.xCoord - par0EntityCreature.posX; 027 staticVector.yCoord = par3Vec3.yCoord - par0EntityCreature.posY; 028 staticVector.zCoord = par3Vec3.zCoord - par0EntityCreature.posZ; 029 return findRandomTargetBlock(par0EntityCreature, par1, par2, staticVector); 030 } 031 032 /** 033 * finds a random target within par1(x,z) and par2 (y) blocks in the reverse direction of the point par3 034 */ 035 public static Vec3 findRandomTargetBlockAwayFrom(EntityCreature par0EntityCreature, int par1, int par2, Vec3 par3Vec3) 036 { 037 staticVector.xCoord = par0EntityCreature.posX - par3Vec3.xCoord; 038 staticVector.yCoord = par0EntityCreature.posY - par3Vec3.yCoord; 039 staticVector.zCoord = par0EntityCreature.posZ - par3Vec3.zCoord; 040 return findRandomTargetBlock(par0EntityCreature, par1, par2, staticVector); 041 } 042 043 /** 044 * searches 10 blocks at random in a within par1(x,z) and par2 (y) distance, ignores those not in the direction of 045 * par3Vec3, then points to the tile for which creature.getBlockPathWeight returns the highest number 046 */ 047 private static Vec3 findRandomTargetBlock(EntityCreature par0EntityCreature, int par1, int par2, Vec3 par3Vec3) 048 { 049 Random var4 = par0EntityCreature.getRNG(); 050 boolean var5 = false; 051 int var6 = 0; 052 int var7 = 0; 053 int var8 = 0; 054 float var9 = -99999.0F; 055 boolean var10; 056 057 if (par0EntityCreature.hasHome()) 058 { 059 double var11 = (double)(par0EntityCreature.getHomePosition().getDistanceSquared(MathHelper.floor_double(par0EntityCreature.posX), MathHelper.floor_double(par0EntityCreature.posY), MathHelper.floor_double(par0EntityCreature.posZ)) + 4.0F); 060 double var13 = (double)(par0EntityCreature.getMaximumHomeDistance() + (float)par1); 061 var10 = var11 < var13 * var13; 062 } 063 else 064 { 065 var10 = false; 066 } 067 068 for (int var16 = 0; var16 < 10; ++var16) 069 { 070 int var12 = var4.nextInt(2 * par1) - par1; 071 int var17 = var4.nextInt(2 * par2) - par2; 072 int var14 = var4.nextInt(2 * par1) - par1; 073 074 if (par3Vec3 == null || (double)var12 * par3Vec3.xCoord + (double)var14 * par3Vec3.zCoord >= 0.0D) 075 { 076 var12 += MathHelper.floor_double(par0EntityCreature.posX); 077 var17 += MathHelper.floor_double(par0EntityCreature.posY); 078 var14 += MathHelper.floor_double(par0EntityCreature.posZ); 079 080 if (!var10 || par0EntityCreature.isWithinHomeDistance(var12, var17, var14)) 081 { 082 float var15 = par0EntityCreature.getBlockPathWeight(var12, var17, var14); 083 084 if (var15 > var9) 085 { 086 var9 = var15; 087 var6 = var12; 088 var7 = var17; 089 var8 = var14; 090 var5 = true; 091 } 092 } 093 } 094 } 095 096 if (var5) 097 { 098 return par0EntityCreature.worldObj.func_82732_R().getVecFromPool((double)var6, (double)var7, (double)var8); 099 } 100 else 101 { 102 return null; 103 } 104 } 105 }