Esempio n. 1
0
        public Cluster CreateCluster(int clusterX, int clusterY)
        {
            Cluster cluster = new Cluster(ClusterType.Evergreen, clusterX, clusterY);

              for (int y = 0; y < Constants.ClusterHeight; ++y)
              {
            for (int x = 0; x < Constants.ClusterWidth; ++x)
            {
              Vector2 tilePos = new Vector2(
            x - Constants.ClusterWidth / 2,
            y - Constants.ClusterHeight / 2
              );

              Tile t = cluster.GetTileAt(x, y);
              if ((x == 0 || x == Constants.ClusterWidth - 1) ||
             (y == 0 || y == Constants.ClusterHeight - 1))
              {
            t.Type = TileType.Grass;
              }
              else
              {
            t.Type = TileType.Water;
              }
              //t.Type = (TileType)(rand.Next() % 2 + 1);
              cluster.SetTileAt(x, y, t);
            }
              }

              return cluster;
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a cluster to the Worlds cluster list and sorts the list.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void AddCluster(Cluster cluster)
        {
            if (insertIndex == clusters.Length)
              {
            GrowClusterBuffer(ref clusters, 10);
              }

              cluster.SetHashCode();
              clusters[insertIndex++] = cluster;
              isSorted = false;
        }
        public void InsertCluster(Cluster data)
        {
            long hashcode = Cluster.GetHashFromXY(data.Coordinates.X, data.Coordinates.Y);

              if (base.Select(string.Format(
            "SELECT hashcode FROM clusters WHERE hashcode={0};", hashcode
            )
              ).Rows.Count != 0)
              {
            throw new DataException(string.Format(
            "Cluster with id --> {0} <-- already exists in database.",
            hashcode
              )
            );
              }

              string sql = string.Format(
            "INSERT INTO clusters(hashcode, data) VALUES({0}, @data);",
            hashcode
              );

              AddToCommandHistory(sql);

              byte[] bytes = data.GetClusterBytes();

              //using (MemoryStream memStream = new MemoryStream())
              //{
              //  serializer.Serialize(memStream, data);
              //  memStream.Close();
              //  bytes = memStream.GetBuffer();

              //}

              base.Connection.Open();
              using (SQLiteCommand cmd = new SQLiteCommand(base.Connection))
              {
            cmd.CommandText = sql;
            cmd.Parameters.Add(
              "@data", DbType.Binary, bytes.Length
            ).Value = bytes;

            cmd.ExecuteNonQuery();
              }
              base.Connection.Close();
        }
Esempio n. 4
0
 // TODO(Martin): Move to world manager
 public EntityManager(Cluster cluster)
 {
     parent = cluster;
       entities = new List<Entity>();
 }
Esempio n. 5
0
        /// <summary>
        /// Insert a cluster at the given coordinates in cluster space.
        /// </summary>
        /// <param name="clusterCoordinates"></param>
        private static void AddCluster(World world, Coordinates clusterCoordinates)
        {
            Cluster cluster = new Cluster(ClusterType.Evergreen, clusterCoordinates);

              Matrix clusterOffset = Matrix.CreateTranslation(new Vector3(-0.5f, -0.5f, 0));

              Matrix tileTranslate =
            /*TilePositionTransform **/
            TileClusterTransform *
            clusterOffset *
            ClusterTileTransform
            ;

              for (int y = 0; y < Constants.ClusterHeight; ++y)
              {
            for (int x = 0; x < Constants.ClusterWidth; ++x)
            {
              Vector2 tilePos = Vector2.Transform(new Vector2(x, y), tileTranslate);

              Tile t = cluster.GetTileAt(x, y);
              t.Type = TileType.Grass;
              cluster.SetTileAt(x, y, t);
            }
              }

              world.AddCluster(cluster);
        }
Esempio n. 6
0
        /// <summary>
        /// Retrieves the bounding rectangle for a cluster in pixels.
        /// </summary>
        /// <param name="cluster">Cluster to get bounds from.</param>
        public static Rectangle GetClusterBounds(Cluster cluster)
        {
            // Creates cluster edges with clockwise winding.
              Vector2[] vertices = new[] {
            new Vector2(-0.5f, -0.5f), // Top Left
            new Vector2(0.5f, -0.5f), // Top Right
            new Vector2(0.5f, 0.5f), // Bottom Right
            new Vector2(-0.5f, 0.5f) // Bottom Left
              };

              // Matrix for translating into tilespace and then scaling to screen.
              // Matrix mat = ClusterTileTransform * TileScreenTransform;

              /* Translate all vertices to the correct cluster and transform
               * into pixel coordinates. */
              for (int i = 0; i < vertices.Length; ++i)
              {
            vertices[i] += cluster.Coordinates;
            vertices[i] = WorldManager.GetClusterScreenCenter(vertices[i]);

            //vertices[i] = Vector2.Transform(vertices[i], mat);
              }

              // Bounding Rectangle.
              Rectangle rect = new Rectangle(
            (int)vertices[0].X,
            (int)vertices[0].Y,
            (int)(vertices[1].X - vertices[0].X),
            (int)(vertices[2].Y - vertices[1].Y)
              );

              return rect;
        }
        public void UnloadCluster(Cluster cluster)
        {
            if(cluster == null)
              {
            throw new Exception("Unloading null cluster!");
              }

              unloadList.Add(cluster);
              if (loadList.Exists(delegate(Coordinates c) { return c == cluster.Coordinates; }))
              {
            loadList.Remove(cluster.Coordinates);
              }

              if(unloadList.Count > UnloadWaitLimit &&
            unloadThread.IsAlive == false)
              {
            StartUnloading();
              }
        }
Esempio n. 8
0
        // TEST METHOD
        private bool TestClusterRange(Cluster cluster)
        {
            Vector2 camCluster = WorldManager.TransformScreenToCluster(Camera2D.Position);

              if (Convert.ToInt32(Math.Abs(cluster.Coordinates.X - camCluster.X)) > 2 ||
              Convert.ToInt32(Math.Abs(cluster.Coordinates.Y - camCluster.Y)) > 2)
              {
            return false;
              }

              return true;
        }
Esempio n. 9
0
 public static void GrowClusterBuffer(ref Cluster[] clusters, int amount)
 {
     Array.Resize<Cluster>(ref clusters, clusters.Length + amount);
 }
Esempio n. 10
0
 /// <summary>
 /// Determines if a given cluster is in view.
 /// </summary>
 /// <param name="cluster">The cluster to investigate.</param>
 private bool IsInView(Cluster cluster)
 {
     return IsInView(cluster.Coordinates);
 }
Esempio n. 11
0
 private void ClusterUnloadedHandler(Cluster cluster)
 {
     if (GetCluster(cluster.Coordinates.X, cluster.Coordinates.Y) != null)
       {
     return;
       }
 }
Esempio n. 12
0
 private void ClusterLoadedHandler(Cluster cluster)
 {
     if (BinaryClusterSearch(clusters, 0, insertIndex, cluster.HashCode) == null)
       {
     loadedClusters.Push(cluster);
       }
 }
Esempio n. 13
0
        /// <summary>
        /// Sorts an array of cluster(ideally the buffer of a world) recursively
        /// by their hash values through the Quicksort algorithm.
        /// </summary>
        /// <param name="array">Array to sort.</param>
        /// <param name="pivot">Array index to begin lookup.</param>
        private static Cluster[] QuickSortClusters(Cluster[] array, int pivot)
        {
            /* If the input array size is two or less, the sorting is done.
               * the one exception is when an unsorted array with the size of two is
               * given as an input parameter.
               * TODO(Peter): Handle this edge-case.*/
              if (array.Length <= 2)
              {
            return array;
              }

              /* Create two arrays of max value since the worst case scenario
               * could allow for all numbers to be either greater or less than the pivot. */
              Cluster[] less = new Cluster[array.Length];
              Cluster[] more = new Cluster[array.Length];

              // Index for less array
              int lessIndex = 0;
              // Index for more array
              int moreIndex = 0;

              for (int i = 0; i < array.Length; ++i)
              {
            // Skip the pivot as it will be appended to the less array.
            if (i == pivot)
            {
              continue;
            }

            /* Null clusters are considered as 'more' than any cluster
             * that actually exists as we want them in the end of the array. */
            if (array[i] == null)
            {
              more[moreIndex++] = array[i];
            }
            // Like above, if the pivot is null, all clusters are less than the pivot.
            else if (array[pivot] == null || array[i].HashCode < array[pivot].HashCode)
            {
              less[lessIndex++] = array[i];
            }
            else if (array[i].HashCode > array[pivot].HashCode)
            {
              more[moreIndex++] = array[i];
            }
              }

              // Append the pivot to the less list.
              less[lessIndex++] = array[pivot];

              // Resize arrays to remove the extra allocated space.
              Array.Resize(ref less, lessIndex);
              Array.Resize(ref more, moreIndex);

              // Sort the less segment recursively.
              QuickSortClusters(less, lessIndex / 2);

              // Sort the more segment recursively.
              QuickSortClusters(more, moreIndex / 2);

              // Re-assign with the two now sorted segments.
              int arrIndex = 0;
              for (int i = 0; i < less.Length; ++i, arrIndex++)
              {
            array[arrIndex] = less[i];
              }
              for (int i = 0; i < more.Length; ++i, arrIndex++)
              {
            array[arrIndex] = more[i];
              }

              return array;
        }
Esempio n. 14
0
        /// <summary>
        /// Finds a cluster in the cluster buffer through binary search.
        /// </summary>
        /// <param name="clusters">The list of clusters to search.</param>
        /// <param name="start">Index to start search from.</param>
        /// <param name="length">Distance to search.</param>
        /// <param name="hashkey">The cluster hash key to look for.</param>
        /// <returns>The cluster if present.</returns>
        private static Cluster BinaryClusterSearch(Cluster[] clusters,
      int start, int length, long hashkey)
        {
            int center = start + length / 2;

              // Not found condition.
              if (clusters[center] == null || clusters[center].HashCode != hashkey && length == 1)
              {
            return null;
              }

              if (clusters[center].HashCode == hashkey)
              {
            return clusters[center];
              }
              else if (clusters[center].HashCode > hashkey)
              {
            return BinaryClusterSearch(clusters, start, length / 2, hashkey);
              }
              else
              {
            return BinaryClusterSearch(
              clusters,
              center,
              (length + start) - center,
              hashkey
            );
              }
        }