Example #1
0
        /// <summary>
        /// Reads a map and syncronizes it with the construction grid
        /// </summary>
        /// <param name="mapData"></param>
        public void parseMapData(Texture2D mapData)
        {
            Color[] pixels = mapData.GetPixels();


            Vector2 center = ai.race == Races.ELVES ? new Vector2(mapData.width / 2 + 0.5f, mapData.height / 2 - 0.5f) : new Vector2(mapData.width / 2 - 0.5f, mapData.height / 2 - 1f);

            // Math Facts:
            // The equation to find te position of something is
            // Offset = (i , j) - Center
            // centerPos + GridSize * Offset

            Vector3 processingPos    = Vector3.zero;
            Vector2 processingOffset = Vector2.zero;

            processingPos.y = ai.rootBasePosition.y;

            for (int i = 0; i < mapData.height; i++)
            {
                for (int j = 0; j < mapData.width; j++)
                {
                    Color pixel = pixels[j + i * mapData.width];
                    processingOffset = new Vector2(i, j) - center;
                    processingPos.x  = ai.rootBasePosition.x + constructionGrid.getDimensions().x *processingOffset.x;
                    processingPos.z  = ai.rootBasePosition.z + constructionGrid.getDimensions().y *processingOffset.y;
                    processingPos    = constructionGrid.discretizeMapCoords(processingPos);

                    //In order to be more flexible we need to check if we can construct
                    if (!constructionGrid.isNewPositionAbleForConstrucction(processingPos, false))
                    {
                        continue;
                    }

                    if (CompareColors(pixel, stronghold))
                    {
                        Debug.Log("Stronghold");
                    }
                    else if (CompareColors(pixel, militaryBuilding))
                    {
                        avaliablePositions[StructureType.MILITARY_BUILDING].Add(processingPos);
                    }
                    else if (CompareColors(pixel, resourcesBuilding))
                    {
                        avaliablePositions[StructureType.RESOURCE_BUILDING].Add(processingPos);
                    }
                    else if (CompareColors(pixel, tower))
                    {
                        avaliablePositions[StructureType.TOWER].Add(processingPos);
                    }
                    else if (CompareColors(pixel, horizontallWall))
                    {
                        avaliablePositions[StructureType.HORIZONTALL_WALL].Add(processingPos);
                    }
                    else if (CompareColors(pixel, verticallWall))
                    {
                        avaliablePositions[StructureType.VERTICALL_WALL].Add(processingPos);
                    }
                    else if (CompareColors(pixel, cornerWall))
                    {
                        avaliablePositions[StructureType.CORNER_WALL].Add(processingPos);
                    }
                    else if (CompareColors(pixel, defenceZone))
                    {
                        baseCriticPoints.Add(processingPos);
                        Debug.Log(baseCriticPoints.Count);
                    }
                    else if (CompareColors(pixel, emptySpace))
                    {
                        continue;
                    }
                    else if (CompareColors(pixel, ignorePixel))
                    {
                        continue;
                    }
                    else
                    {
                        Debug.Log("Unknown Structure" + pixel.ToString());
                    }
                }
            }
            Debug.Log("Reading Complete");
        }