我的世界Minecraft源码分析(1):刷怪逻辑

发表于2017-09-07
评论1 4.9k浏览

这个系列通过对我的世界Minecraft源码进行拆分讲解,让大家可以清除的了解一款游戏是怎么一步步被实现出来的,下面就介绍Minecraft源码第一篇内容,关于刷怪逻辑。

生成循环


生物大致划分为四种:攻击型,被动型,水生型(也就是鱿鱼)和环境型(也就是蝙蝠)。攻击型生物每一个Tick(1/20秒)有一次的生成周期。被动型和水生型生物只有每400刻(20秒)一次的生成周期。因为这一点,攻击型生物可以在任何时候生成,而动物生成则非常少。另外,大部分动物在建立世界时产生的Block中生成。

生物的刷新范围通常是以玩家为中心的 15 * 15的Chunk,当有多个玩家的时候,所有玩家的附近都会刷新。当敌对怪物离玩家超过128个block的时候,就会立即把它刷掉。下图表述刷新逻辑和玩家距离的关系。



Entity种类


源码中对应的类是

EnumCreatureType.java

[java] view plain copy
  1. public enum EnumCreatureType  
  2. {  
  3.     MONSTER(IMob.class70, Material.air, falsefalse),  
  4.     CREATURE(EntityAnimal.class10, Material.air, truetrue),  
  5.     AMBIENT(EntityAmbientCreature.class15, Material.air, truefalse),  
  6.     WATER_CREATURE(EntityWaterMob.class5, Material.water, truefalse);  
  7.     /** 
  8.      * The root class of creatures associated with this EnumCreatureType (IMobs for aggressive creatures, EntityAnimals 
  9.      * for friendly ones) 
  10.      */  
  11.     private final Class creatureClass;  
  12.     private final int maxNumberOfCreature;  
  13.     private final Material creatureMaterial;  
  14.     /** A flag indicating whether this creature type is peaceful. */  
  15.     private final boolean isPeacefulCreature;  
  16.     /** Whether this creature type is an animal. */  
  17.     private final boolean isAnimal;  
  18.   
  19.     private EnumCreatureType(Class classint _maxNumberOfCreature, Material _creatureMaterial, boolean _isPeacefulCreature, boolean _isAnimal)  
  20.     {  
  21.         this.creatureClass = class;  
  22.         this.maxNumberOfCreature = _maxNumberOfCreature;  
  23.         this.creatureMaterial = _creatureMaterial;  
  24.         this.isPeacefulCreature = _isPeacefulCreature;  
  25.         this.isAnimal = _isAnimal;  
  26.     }  
  27.   
  28.     public Class getCreatureClass()  
  29.     {  
  30.         return this.creatureClass;  
  31.     }  
  32.   
  33.     public int getMaxNumberOfCreature()  
  34.     {  
  35.         return this.maxNumberOfCreature;  
  36.     }  
  37.   
  38.     /** 
  39.      * Gets whether or not this creature type is peaceful. 
  40.      */  
  41.     public boolean getPeacefulCreature()  
  42.     {  
  43.         return this.isPeacefulCreature;  
  44.     }  
  45.   
  46.     /** 
  47.      * Return whether this creature type is an animal. 
  48.      */  
  49.     public boolean getAnimal()  
  50.     {  
  51.         return this.isAnimal;  
  52.     }  
  53. }  

怪物容量(可理解为人口)与适合生成的Chunk总数直接成比例。要计算容量的话,生成区域在每一方向上均扩展一个区块,所以有17*17 BChunk的大小,然后总的ChunkCount被代入到下式中:

容量 = 常量* ChunkCount / 289
每一种生物均具有自己的容量计算和公式中不同的常量值:

攻击型 = 70
被动型 = 10
环境型(蝙蝠) = 15

水生型 = 5


在单人游戏的时候,ChunkCount 一直是289,但是在多人游戏中,每个chunk只被计算一次,所以玩家分得越开,怪物的容量也就越大。


在 Spawn开始之前,首先进行一次 mob 容量检查,如果生物的数量超过了容量,那么这个种类的生物的生成就会跳过。


服务器架构



图1. MC服务器架构


MinecraftServer作为服务器,主要负责服务端的更新,里面可以包含多个WorldServer,WorldClent作为服务端,当玩家加入一个服务器的时候,就会创建一个在本地。SpawnerAnimals作为刷怪的工具类,主要用来处理刷怪逻辑。

首先来看WorldServer的Tick

注:如无特别说明,所有tick都是一秒20次。



图2. WorldServer Tick 流程


很清晰的逻辑,这里主要看一下findChunksForSpawning的实现。

在单人游戏模式下,区块计数总为17*17=289,那么各种生物的容量也就是上面列出的数值。在多人游戏中,在多个玩家范围内的区块只被计算一次,所以玩家越分散,更多地区块会被覆盖且会有更高的生物容量。

在每次生成周期的开始都会检查一次容量。如果存活的生物数量超过它的容量,整个生成周期就会被跳过。


在每一生成周期中,会在每一个合适的区块中进行一次生成一组生物的尝试。该区块内选择一个随机地点作为这组生物的中心点。为生成这组生物,中心方块对水生生物而言必须是水方块,对所有其它生物来说则必须是空气方块。注意在后面的情形中,它一定得是空气方块。任何其它方块,哪怕是一个透明方块都会阻止整组生物的生成。


图3. 陆地怪物的生成条件



如果该组位置合适,会在以中心方块为原点41*1*41的范围(就是41*41格大小的方型,有1格高的区域)内进行12次尝试以生成多至4个的生物(狼是8个,恶魂是1个)。生物将会在这一区域生成其身体的最下部分。在每次生成尝试中,会在这一区域中随机选择一个方块的地点。尽管生成区域能扩展到中心21格之外,但是随机选出的地点强烈地向该组的中心集中。大约有85%的生成将会在该组中心的5格以内,99%会落在10格以内

组内所有的生物都是相同的种类。在该组第一次生成尝试时从该地区所适合生成的种类中随机挑选一种以决定整组的种类。

具体的种类可以参考要Minecraft的Wiki。

findChunksForSpawning函数实现的就是上面描述的逻辑。看一下SpawnerAnimals.java这个类。


[java] view plain copy
  1. public final class SpawnerAnimals  
  2. {  
  3.     private static final int MOB_COUNT_DIV = (int)Math.pow(17.0D, 2.0D);  
  4.     /** The 17x17 area around the player where mobs can spawn */  
  5.     private final Set eligibleChunksForSpawning = Sets.newHashSet();  
  6.     private static final String __OBFID = "CL_00000152";  
  7.   
  8.     /** 
  9.      * adds all chunks within the spawn radius of the players to eligibleChunksForSpawning. pars: the world, 
  10.      * hostileCreatures, passiveCreatures. returns number of eligible chunks. 
  11.      */  
  12.        
  13.        
  14.     public int findChunksForSpawning(WorldServer server, boolean spawnHostileMobs, boolean spawnPeacefulMobs, boolean isSpecialSpawnTick)  
  15.     {  
  16.         if (!spawnHostileMobs && !spawnPeacefulMobs)  
  17.         {  
  18.             return 0;  
  19.         }  
  20.         else  
  21.         {  
  22.             this.eligibleChunksForSpawning.clear();  
  23.             int chunkCount = 0;  
  24.             Iterator iterator = server.playerEntities.iterator();  
  25.             int k;  
  26.             int creatureCount;  
  27.   
  28.             while (iterator.hasNext())  
  29.             {  
  30.                 EntityPlayer entityplayer = (EntityPlayer)iterator.next();  
  31.   
  32.                 if (!entityplayer.isSpectator())  
  33.                 {  
  34.                     int chunkCoordX = Mathf.FloorToInt(entityplayer.GetPosition().x / 16.0f);  
  35.               int chunkCoordZ = Mathf.FloorToInt(entityplayer.GetPosition().z / 16.0f);  
  36.               byte chunkLength = 8;  
  37.   
  38.               for (int directionX = -chunkLength; directionX <= chunkLength;  directionX)  
  39.               {  
  40.                   for (directionZ = -chunkLength; directionZ <= chunkLength;  directionZ)  
  41.                   {  
  42.                       bool isBorderChunk = (directionX == -chunkLength || directionX == chunkLength || directionZ == -chunkLength || directionZ == chunkLength);  
  43.                       IntVector2 chunkcoordintpair = new IntVector2(directionX   chunkCoordX, directionZ   chunkCoordZ);  
  44.   
  45.                       if (!this.eligibleChunksForSpawning.Contains(chunkcoordintpair))  
  46.                       {  
  47.                            chunkCount;  
  48.                           if (!isBorderChunk && server.getWorldBorder().contains(chunkcoordintpair))  
  49.                           {  
  50.                               this.eligibleChunksForSpawning.Add(chunkcoordintpair);  
  51.                           }  
  52.                       }  
  53.                   }  
  54.               }  
  55.                 }  
  56.             }  
  57.   
  58.             int totalEntityCount = 0;  
  59.             BlockPos blockpos2 = server.getSpawnPoint();  
  60.             EnumCreatureType[] aenumcreaturetype = EnumCreatureType.values();  
  61.             k = aenumcreaturetype.length;  
  62.   
  63.             for (int i = 0; i < k;  i)  
  64.             {  
  65.                 EnumCreatureType enumcreaturetype = aenumcreaturetype[i];  
  66.   
  67.                 if ((!enumcreaturetype.getPeacefulCreature() || spawnPeacefulMobs) && (enumcreaturetype.getPeacefulCreature() || spawnHostileMobs) && (!enumcreaturetype.getAnimal() || isSpecialSpawnTick))  
  68.                 {  
  69.                     creatureCount = server.countEntities(enumcreaturetype, true);  
  70.                     int maxCreatureCount = enumcreaturetype.getMaxNumberOfCreature() * chunkCount / MOB_COUNT_DIV;  
  71.   
  72.                     if (creatureCount <= maxCreatureCount)  
  73.                     {  
  74.                         Iterator iterator1 = this.eligibleChunksForSpawning.iterator();  
  75.                         ArrayList tmp = new ArrayList(eligibleChunksForSpawning);  
  76.                         Collections.shuffle(tmp);  
  77.                         iterator1 = tmp.iterator();  
  78.                         label115:  
  79.   
  80.                         while (iterator1.hasNext())  
  81.                         {  
  82.                             ChunkCoordIntPair chunkcoordintpair1 = (ChunkCoordIntPair)iterator1.next();  
  83.                             BlockPos blockpos = getRandomChunkPosition(server, chunkcoordintpair1.chunkXPos, chunkcoordintpair1.chunkZPos);  
  84.                             int j1 = blockpos.getX();  
  85.                             int k1 = blockpos.getY();  
  86.                             int l1 = blockpos.getZ();  
  87.                             Block block = server.getBlockState(blockpos).getBlock();  
  88.   
  89.                             if (!block.isNormalCube())  
  90.                             {  
  91.                                 int entityCountOnChunk = 0;  
  92.                                 int j2 = 0;  
  93.   
  94.                                 while (j2 < 3)  
  95.                                 {  
  96.                                     int k2 = j1;  
  97.                                     int l2 = k1;  
  98.                                     int i3 = l1;  
  99.                                     byte b1 = 6;  
  100.                                     BiomeGenBase.SpawnListEntry spawnlistentry = null;  
  101.                                     IEntityLivingData ientitylivingdata = null;  
  102.                                     int j3 = 0;  
  103.   
  104.                                     while (true)  
  105.                                     {  
  106.                                         if (j3 < 4)  
  107.                                         {  
  108.                                             label108:  
  109.                                             {  
  110.                                                 k2  = server.rand.nextInt(b1) - server.rand.nextInt(b1);  
  111.                                                 l2  = server.rand.nextInt(1) - server.rand.nextInt(1);  
  112.                                                 i3  = server.rand.nextInt(b1) - server.rand.nextInt(b1);  
  113.                                                 BlockPos blockpos1 = new BlockPos(k2, l2, i3);  
  114.                                                 float f = (float)k2   0.5F;  
  115.                                                 float f1 = (float)i3   0.5F;  
  116.                                                 //Check must be away from Player by 24 block, and away from player spawn point. 576 = 24 * 24  
  117.                                                 if (!server.CheckCanSpawnHere((double)f, (double)l2, (double)f1, 24.0D) && blockpos2.distanceSq((double)f, (double)l2, (double)f1) >= 576.0D)  
  118.                                                 {  
  119.                                                     if (spawnlistentry == null)  
  120.                                                     {  
  121.                                                         spawnlistentry = server.GetSpawnListEntry(enumcreaturetype, blockpos1);  
  122.   
  123.                                                         if (spawnlistentry == null)  
  124.                                                         {  
  125.                                                             break label108;  
  126.                                                         }  
  127.                                                     }  
  128.   
  129.                                                     if (server.CheckChunkHasSpawnEntry(enumcreaturetype, spawnlistentry, blockpos1) && canCreatureTypeSpawnAtLocation(EntitySpawnPlacementRegistry.GetSpawnPointType(spawnlistentry.entityClass), server, blockpos1))  
  130.                                                     {  
  131.                                                         EntityLiving entityliving;  
  132.   
  133.                                                         try  
  134.                                                         {  
  135.                                                             entityliving = (EntityLiving)spawnlistentry.entityClass.getConstructor(new Class[] {World.class}).newInstance(new Object[] {server});  
  136.                                                         }  
  137.                                                         catch (Exception exception)  
  138.                                                         {  
  139.                                                             exception.printStackTrace();  
  140.                                                             return totalEntityCount;  
  141.                                                         }  
  142.   
  143.                                                         entityliving.setLocationAndAngles((double)f, (double)l2, (double)f1, server.rand.nextFloat() * 360.0F, 0.0F);  
  144.   
  145.                                                         Result canSpawn = ForgeEventFactory.canEntitySpawn(entityliving, server, f, l2, f1);  
  146.                                                         if (canSpawn == Result.ALLOW || (canSpawn == Result.DEFAULT && (entityliving.getCanSpawnHere() && entityliving.handleLavaMovement())))  
  147.                                                         {  
  148.                                                             if (!ForgeEventFactory.doSpecialSpawn(entityliving, server, f1, l2, f1))  
  149.                                                             ientitylivingdata = entityliving.getEntityData(server.getDifficultyForLocation(new BlockPos(entityliving)), ientitylivingdata);  
  150.   
  151.                                                             if (entityliving.handleLavaMovement())  
  152.                                                             {  
  153.                                                                  entityCountOnChunk;  
  154.                                                                 server.spawnEntityInWorld(entityliving);  
  155.                                                             }  
  156.   
  157.                                                             if (entityCountOnChunk >= ForgeEventFactory.getMaxSpawnPackSize(entityliving))  
  158.                                                             {  
  159.                                                                 continue label115;  
  160.                                                             }  
  161.                                                         }  
  162.   
  163.                                                         totalEntityCount  = entityCountOnChunk;  
  164.                                                     }  
  165.                                                 }  
  166.   
  167.                                                  j3;  
  168.                                                 continue;  
  169.                                             }  
  170.                                         }  
  171.   
  172.                                          j2;  
  173.                                         break;  
  174.                                     }  
  175.                                 }  
  176.                             }  
  177.                         }  
  178.                     }  
  179.                 }  
  180.             }  
  181.   
  182.             return totalEntityCount;  
  183.         }  
  184.     }  
  185.      
  186.   
  187.   
  188.     protected static BlockPos getRandomChunkPosition(World worldIn, int x, int z)  
  189.     {  
  190.         Chunk chunk = worldIn.getChunkFromChunkCoords(x, z);  
  191.         int k = x * 16   worldIn.rand.nextInt(16);  
  192.         int l = z * 16   worldIn.rand.nextInt(16);  
  193.         int creatureCount = MathHelper.Ceiling(chunk.getHeight(new BlockPos(k, 0, l))   116);  
  194.         int j1 = worldIn.rand.nextInt(creatureCount > 0 ? creatureCount : chunk.getTopFilledSegment()   16 - 1);  
  195.         return new BlockPos(k, j1, l);  
  196.     }  
  197.   
  198.     public static boolean canCreatureTypeSpawnAtLocation(EntityLiving.SpawnPlacementType placeType, World worldIn, BlockPos pos)  
  199.     {  
  200.         if (!worldIn.getWorldBorder().contains(pos))  
  201.         {  
  202.             return false;  
  203.         }  
  204.         else  
  205.         {  
  206.             Block block = worldIn.getBlockState(pos).getBlock();  
  207.   
  208.             if (placeType == EntityLiving.SpawnPlacementType.IN_WATER)  
  209.             {  
  210.                 return block.getMaterial().isLiquid() && worldIn.getBlockState(pos.down()).getBlock().getMaterial().isLiquid() && !worldIn.getBlockState(pos.up()).getBlock().isNormalCube();  
  211.             }  
  212.             else  
  213.             {  
  214.                 BlockPos blockpos1 = pos.down();  
  215.   
  216.                 if (!worldIn.getBlockState(blockpos1).getBlock().canCreatureSpawn(worldIn, blockpos1, placeType))  
  217.                 {  
  218.                     return false;  
  219.                 }  
  220.                 else  
  221.                 {  
  222.                     Block block1 = worldIn.getBlockState(blockpos1).getBlock();  
  223.                     boolean flag = block1 != Blocks.bedrock && block1 != Blocks.barrier;  
  224.                     return flag && !block.isNormalCube() && !block.getMaterial().isLiquid() && !worldIn.getBlockState(pos.up()).getBlock().isNormalCube();  
  225.                 }  
  226.             }  
  227.         }  
  228.     }  
  229.   
  230.     /** 
  231.      * Called during chunk generation to spawn initial creatures. 
  232.      */  
  233.     public static void performWorldGenSpawning(World worldIn, BiomeGenBase biomeGenBase, int chunkCenterX, int chunkCenterY, int rangeX, int rangeY, Random rand)  
  234.     {  
  235.         List list = biomeGenBase.getSpawnableList(EnumCreatureType.CREATURE);  
  236.   
  237.         if (!list.isEmpty())  
  238.         {  
  239.             while (rand.nextFloat() < biomeGenBase.getSpawningChance())  
  240.             {  
  241.                 BiomeGenBase.SpawnListEntry spawnlistentry = (BiomeGenBase.SpawnListEntry)WeightedRandom.getRandomItem(worldIn.rand, list);  
  242.                 int creatureCount = spawnlistentry.minGroupCount   rand.nextInt(1   spawnlistentry.maxGroupCount - spawnlistentry.minGroupCount);  
  243.                 IEntityLivingData ientitylivingdata = null;  
  244.                 int j1 = chunkCenterX   rand.nextInt(rangeX);  
  245.                 int k1 = chunkCenterY   rand.nextInt(rangeY);  
  246.                 int l1 = j1;  
  247.                 int entityCountOnChunk = k1;  
  248.   
  249.                 for (int j2 = 0; j2 < creatureCount;  j2)  
  250.                 {  
  251.                     boolean flag = false;  
  252.   
  253.                     for (int k2 = 0; !flag && k2 < 4;  k2)  
  254.                     {  
  255.                         BlockPos blockpos = worldIn.getTopSolidOrLiquidBlock(new BlockPos(j1, 0, k1));  
  256.   
  257.                         if (canCreatureTypeSpawnAtLocation(EntityLiving.SpawnPlacementType.ON_GROUND, worldIn, blockpos))  
  258.                         {  
  259.                             EntityLiving entityliving;  
  260.   
  261.                             try  
  262.                             {  
  263.                                 entityliving = (EntityLiving)spawnlistentry.entityClass.getConstructor(new Class[] {World.class}).newInstance(new Object[] {worldIn});  
  264.                             }  
  265.                             catch (Exception exception)  
  266.                             {  
  267.                                 exception.printStackTrace();  
  268.                                 continue;  
  269.                             }  
  270.   
  271.                             entityliving.setLocationAndAngles((double)((float)j1   0.5F), (double)blockpos.getY(), (double)((float)k1   0.5F), rand.nextFloat() * 360.0F, 0.0F);  
  272.                             worldIn.spawnEntityInWorld(entityliving);  
  273.                             ientitylivingdata = entityliving.func_180482_a(worldIn.getDifficultyForLocation(new BlockPos(entityliving)), ientitylivingdata);  
  274.                             flag = true;  
  275.                         }  
  276.   
  277.                         j1  = rand.nextInt(5) - rand.nextInt(5);  
  278.   
  279.                         for (k1  = rand.nextInt(5) - rand.nextInt(5); j1 < chunkCenterX || j1 >= chunkCenterX   rangeX || k1 < chunkCenterY || k1 >= chunkCenterY   rangeX; k1 = entityCountOnChunk   rand.nextInt(5) - rand.nextInt(5))  
  280.                         {  
  281.                             j1 = l1   rand.nextInt(5) - rand.nextInt(5);  
  282.                         }  
  283.                     }  
  284.                 }  
  285.             }  
  286.         }  
  287.     }  
  288. }  



生物群落的生成

在维基百科中,生物群系(Biome)在气候学和地理学上被定义为具有类似气候条件的地方,比如植物、动物和土壤生物组成的群落,它经常被称作生态系统。是Minecraft里有不同的地域特色,植物,高度,温度,湿度评级的地区。 在Minecraft中, 从万圣节更新开始,它意味着具有不同高度、温度、湿度、叶子颜色的区域。



图4. 高寒地区Biome



图5. 雪地Biome




当地图被创建时会具有雪地或草地主题。但在这个更新之后,一个世界中就可以具有所有的主题,它们的分布由生物群系图决定。

Anvil文件格式中,世界数据直接存储在生物群系中,这不同于先前的国家或地区的文件格式的格式,其中的生物群系,从种子中动态计算。


Chunk中的生物也是根据Biome来生成,这里的限制主要体现在当SpawnAnimals在Chunk中生成生物的时候,总会调用WorldServer的方法进行Check,看对应的生物种类能否生成。


[java] view plain copy
  1. public boolean CheckCreatureCanSpawn(EnumCreatureType creatureType, BiomeGenBase.SpawnListEntry spawnListEntry, BlockPos blockPos)  
  2. {  
  3.     List list = this.getChunkProvider().getSpawnableList(creatureType, blockPos);  
  4.     return list != null && !list.isEmpty() ? list.contains(spawnListEntry) : false;  
  5. }  



相关的还有ChunkProviderGenerate.getSpawnableList()
[java] view plain copy
  1. public List getSpawnableList(EnumCreatureType creatureType, BlockPos pos)  
  2. {  
  3.     BiomeGenBase biomegenbase = this.worldObj.getBiomeGenForCoords(pos);  
  4.   
  5.     if (this.mapFeaturesEnabled)  
  6.     {  
  7.         if (creatureType == EnumCreatureType.MONSTER && this.scatteredFeatureGenerator.func_175798_a(pos))  
  8.         {  
  9.             return this.scatteredFeatureGenerator.getScatteredFeatureSpawnList();  
  10.         }  
  11.   
  12.         if (creatureType == EnumCreatureType.MONSTER && this.settings.useMonuments && this.oceanMonumentGenerator.func_175796_a(this.worldObj, pos))  
  13.         {  
  14.             return this.oceanMonumentGenerator.func_175799_b();  
  15.         }  
  16.     }  
  17.   
  18.     return biomegenbase.getSpawnableList(creatureType);  
  19. }  


而所有的Biome和生物的对应关系都在BiomeGenBase这个类中定义。


在EntityLiving中定义了一个getCanSpawnHere函数,用于查询是否可以在某个位置生成

[java] view plain copy
  1. /** 
  2.  * Checks if the entity's current position is a valid location to spawn this entity. 
  3.  */  
  4. public boolean getCanSpawnHere()  
  5. {  
  6.     return true;  
  7. }  

继承EntityLiving的类就override这个函数,比如EntityAnimal

[java] view plain copy
  1. /** 
  2.    * Checks if the entity's current position is a valid location to spawn this entity. 
  3.    */  
  4.   public boolean getCanSpawnHere()  
  5.   {  
  6.       int i = MathHelper.floor_double(this.posX);  
  7.       int j = MathHelper.floor_double(this.getEntityBoundingBox().minY);  
  8.       int k = MathHelper.floor_double(this.posZ);  
  9.       BlockPos blockpos = new BlockPos(i, j, k);  
  10.       return this.worldObj.getBlockState(blockpos.down()).getBlock() == this.spawnBlock && this.worldObj.getLight(blockpos) > 8 && super.getCanSpawnHere();  
  11.   }  

史莱姆的

[java] view plain copy
  1. /** 
  2.    * Checks if the entity's current position is a valid location to spawn this entity. 
  3.    */  
  4.   public boolean getCanSpawnHere()  
  5.   {  
  6.       Chunk chunk = this.worldObj.getChunkFromBlockCoords(new BlockPos(MathHelper.floor_double(this.posX), 0, MathHelper.floor_double(this.posZ)));  
  7.   
  8.       if (this.worldObj.getWorldInfo().getTerrainType().handleSlimeSpawnReduction(rand, worldObj))  
  9.       {  
  10.           return false;  
  11.       }  
  12.       else  
  13.       {  
  14.           if (this.worldObj.getDifficulty() != EnumDifficulty.PEACEFUL)  
  15.           {  
  16.               BiomeGenBase biomegenbase = this.worldObj.getBiomeGenForCoords(new BlockPos(MathHelper.floor_double(this.posX), 0, MathHelper.floor_double(this.posZ)));  
  17.   
  18.               if (biomegenbase == BiomeGenBase.swampland && this.posY > 50.0D && this.posY < 70.0D && this.rand.nextFloat() < 0.5F && this.rand.nextFloat() < this.worldObj.getCurrentMoonPhaseFactor() && this.worldObj.getLightFromNeighbors(new BlockPos(this)) <= this.rand.nextInt(8))  
  19.               {  
  20.                   return super.getCanSpawnHere();  
  21.               }  
  22.   
  23.               if (this.rand.nextInt(10) == 0 && chunk.getRandomWithSeed(987234911L).nextInt(10) == 0 && this.posY < 40.0D)  
  24.               {  
  25.                   return super.getCanSpawnHere();  
  26.               }  
  27.           }  
  28.   
  29.           return false;  
  30.       }  
  31.   }  



参考

Minecraft Wiki

Minecraft Forge

http://blog.csdn.net/silangquan/article/details/51258052

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引