public static void ChangeNodeType(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
      List<ISymbol> allowedSymbols = new List<ISymbol>();
      ISymbolicExpressionTreeNode parent;
      int childIndex;
      ISymbolicExpressionTreeNode child;
      // repeat until a fitting parent and child are found (MAX_TRIES times)
      int tries = 0;
      do {

#pragma warning disable 612, 618
        parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random);
#pragma warning restore 612, 618

        childIndex = random.Next(parent.SubtreeCount);

        child = parent.GetSubtree(childIndex);
        int existingSubtreeCount = child.SubtreeCount;
        allowedSymbols.Clear();
        foreach (var symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)) {
          // check basic properties that the new symbol must have
          if (symbol.Name != child.Symbol.Name &&
            symbol.InitialFrequency > 0 &&
            existingSubtreeCount <= parent.Grammar.GetMinimumSubtreeCount(symbol) &&
            existingSubtreeCount >= parent.Grammar.GetMaximumSubtreeCount(symbol)) {
            // check that all existing subtrees are also allowed for the new symbol
            bool allExistingSubtreesAllowed = true;
            for (int existingSubtreeIndex = 0; existingSubtreeIndex < existingSubtreeCount && allExistingSubtreesAllowed; existingSubtreeIndex++) {
              var existingSubtree = child.GetSubtree(existingSubtreeIndex);
              allExistingSubtreesAllowed &= parent.Grammar.IsAllowedChildSymbol(symbol, existingSubtree.Symbol, existingSubtreeIndex);
            }
            if (allExistingSubtreesAllowed) {
              allowedSymbols.Add(symbol);
            }
          }
        }
        tries++;
      } while (tries < MAX_TRIES && allowedSymbols.Count == 0);

      if (tries < MAX_TRIES) {
        var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
#pragma warning disable 612, 618
        var newSymbol = allowedSymbols.SelectRandom(weights, random);
#pragma warning restore 612, 618

        // replace the old node with the new node
        var newNode = newSymbol.CreateTreeNode();
        if (newNode.HasLocalParameters)
          newNode.ResetLocalParameters(random);
        foreach (var subtree in child.Subtrees)
          newNode.AddSubtree(subtree);
        parent.RemoveSubtree(childIndex);
        parent.InsertSubtree(childIndex, newNode);
      }
    }
    public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) {
      var allowedSymbols = new List<ISymbol>();
      ISymbolicExpressionTreeNode parent;
      int childIndex;
      int maxLength;
      int maxDepth;
      // repeat until a fitting parent and child are found (MAX_TRIES times)
      int tries = 0;
      do {
#pragma warning disable 612, 618
        parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random);
#pragma warning restore 612, 618

        childIndex = random.Next(parent.SubtreeCount);
        var child = parent.GetSubtree(childIndex);
        maxLength = maxTreeLength - symbolicExpressionTree.Length + child.GetLength();
        maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + child.GetDepth();

        allowedSymbols.Clear();
        foreach (var symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)) {
          // check basic properties that the new symbol must have
          if (symbol.Name != child.Symbol.Name &&
            symbol.InitialFrequency > 0 &&
            parent.Grammar.GetMinimumExpressionDepth(symbol) + 1 <= maxDepth &&
            parent.Grammar.GetMinimumExpressionLength(symbol) <= maxLength) {
            allowedSymbols.Add(symbol);
          }
        }
        tries++;
      } while (tries < MAX_TRIES && allowedSymbols.Count == 0);

      if (tries < MAX_TRIES) {
        var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
#pragma warning disable 612, 618
        var seedSymbol = allowedSymbols.SelectRandom(weights, random);
#pragma warning restore 612, 618

        // replace the old node with the new node
        var seedNode = seedSymbol.CreateTreeNode();
        if (seedNode.HasLocalParameters)
          seedNode.ResetLocalParameters(random);

        parent.RemoveSubtree(childIndex);
        parent.InsertSubtree(childIndex, seedNode);
        ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth);
      }
    }
Example #3
0
        private static void DoSpawn(WorldManager world, HashSet<int> chunksToSpawnIn, Mob[] mobEntities, List<WeightedValue<MobType>> mobGroup, int maximumMobs, bool inWater = false)
        {
            // Based on original server spawn logic and minecraft wiki (http://www.minecraftwiki.net/wiki/Spawn)

            // Check that we haven't already reached the maximum number of this type of mob
            if (mobGroup.Count > 0 && (mobEntities.Where(e => mobGroup.Where(mob => mob.Value == e.Type).Any()).Count() <= maximumMobs * chunksToSpawnIn.Count / 256))
            {
                foreach (var packedChunk in chunksToSpawnIn)
                {
                    MobType mobType = mobGroup.SelectRandom(world.Server.Rand);
                    UniversalCoords packSpawnPosition = GetRandomPointInChunk(world, UniversalCoords.FromPackedChunkToX(packedChunk), UniversalCoords.FromPackedChunkToZ(packedChunk));

                    byte? blockId = world.GetBlockId(packSpawnPosition);

                    if (blockId == null)
                        continue;

                    BlockBase blockClass = BlockHelper.Instance.CreateBlockInstance((byte)blockId);

                    if (!blockClass.IsOpaque && ((!inWater && blockClass.Type == BlockData.Blocks.Air) || (inWater && blockClass.IsLiquid))) // Lava is Opaque, so IsLiquid is safe to use here for water & still water
                    {
                        int spawnedCount = 0;
                        int x = packSpawnPosition.WorldX;
                        int y = packSpawnPosition.WorldY;
                        int z = packSpawnPosition.WorldZ;

                        for (int i = 0; i < 21; i++)
                        {
                            // Every 4th attempt reset the coordinates to the centre of the pack spawn
                            if (i % 4 == 0)
                            {
                                x = packSpawnPosition.WorldX;
                                y = packSpawnPosition.WorldY;
                                z = packSpawnPosition.WorldZ;
                            }

                            const int distance = 6;

                            x += world.Server.Rand.Next(distance) - world.Server.Rand.Next(distance);
                            y += world.Server.Rand.Next(1) - world.Server.Rand.Next(1);
                            z += world.Server.Rand.Next(distance) - world.Server.Rand.Next(distance);

                            if (CanMobTypeSpawnAtLocation(mobType, world, x, y, z))
                            {
                                AbsWorldCoords spawnPosition = new AbsWorldCoords(x + 0.5, y, z + 0.5);

                                // Check that no player is within a radius of 24 blocks of the spawnPosition
                                if (world.GetClosestPlayer(spawnPosition, 24.0) == null)
                                {
                                    // Check that the squared distance is more than 576 from spawn (24 blocks)
                                    if (spawnPosition.ToVector().DistanceSquared(new AbsWorldCoords(world.Spawn).ToVector()) > 576.0)
                                    {
                                        Mob newMob = MobFactory.Instance.CreateMob(world, world.Server,
                                                                          mobType) as Mob;

                                        if (newMob == null)
                                            break;

                                        newMob.Position = spawnPosition;
                                        newMob.Yaw = world.Server.Rand.NextDouble()*360.0;
                                        // Finally apply any mob specific rules about spawning here
                                        if (newMob.CanSpawnHere())
                                        {
                                            //Event
                                            EntitySpawnEventArgs e = new EntitySpawnEventArgs(newMob, newMob.Position);
                                            world.Server.PluginManager.CallEvent(Event.EntitySpawn, e);
                                            if (e.EventCanceled)
                                                continue;
                                            newMob.Position = e.Location;
                                            //End Event

                                            ++spawnedCount;
                                            MobSpecificInitialisation(newMob, world, spawnPosition);
                                            world.Server.AddEntity(newMob);

                                            if (spawnedCount >= newMob.MaxSpawnedPerGroup)
                                            {
                                                // This chunk is full - move to the next
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }