public TilemapModifyCommand(string title, Tilemap changedTilemap, TileBlock.StateData oldState, TileBlock.StateData newState)
     : base(title)
 {
     this.changedTilemap = changedTilemap;
     this.oldState = oldState;
     this.newState = newState;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Check a tile block for nonexistent tile ids.
 /// </summary>
 /// <param name="tiles">TileBlock to check</param>
 /// <param name="Tileset">Tileset where the ids should be defined</param>
 /// <returns>List of invalid tile ids.</returns>
 public static List<int> CheckIds(TileBlock tiles, Tileset Tileset)
 {
     List<int> invalidTiles = new List<int>();
     for (int y = 0; y < tiles.Height; ++y) {
         for (int x = 0; x < tiles.Width; ++x) {
             int TileId = tiles[x, y];
             if (!Tileset.IsValid(TileId)) {
                 if (invalidTiles.IndexOf(TileId) == -1)
                     invalidTiles.Add(TileId);
             }
         }
     }
     return invalidTiles;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Clone Subset of other TileBlock
 /// </summary>
 public TileBlock(TileBlock other, int startX, int startY, int width, int height)
 {
     field = new Field<int>(other.field, startX, startY, width, height, 0);
 }
Ejemplo n.º 4
0
 public bool EqualContents(TileBlock rhs)
 {
     return field.EqualContents(rhs.field);
 }
Ejemplo n.º 5
0
    /// <summary>
    /// Return a measure of how similar fields t1 and t2 are.
    /// </summary>
    protected float calculateSimilarity(TileBlock t1, TileBlock t2)
    {
        float sim = 0;

        // make sure both fields have equal dimensions
        if (t1.Width != t2.Width) throw new ArgumentException("TileBlocks had different widths");
        if (t1.Height != t2.Height) throw new ArgumentException("TileBlocks had different heights");

        // compare each tile in the fields
        for (int px = 0; px < t1.Width; px++) {
            for (int py = 0; py < t1.Height; py++) {
                int id1 = t1[px, py];
                int id2 = t2[px, py];
                Tile tile1 = tileset.Get(id1);
                Tile tile2 = tileset.Get(id2);

                // if any of the tile ids is invalid, make this a really bad match
                if (tile1 == null) return -1;
                if (tile2 == null) return -1;

                bool solid1 = ((tile1.Attributes & Tile.Attribute.SOLID) != 0);
                bool solid2 = ((tile2.Attributes & Tile.Attribute.SOLID) != 0);

                // reward identical tiles...
                if (id1 == id2) sim += 1;

                // ...but not changing solid to non-solid (and vice versa) is even better
                if (solid1 == solid2) sim += 75;

                // Also reward attributes that are completely alike
                if(tile1.Attributes == tile2.Attributes) sim += 25;
            }
        }
        return sim;
    }
Ejemplo n.º 6
0
 /// <summary>
 /// Add all tiles of the given tileBlock to the list of valid patterns
 /// </summary>
 /// <param name="tileBlock">tileBlock that specifies the patterns to add</param>
 public void LearnPatterns(TileBlock tileBlock)
 {
     for (int tx = 0; tx <= (int)tileBlock.Width - (int)width; tx++) {
         for (int ty = 0; ty <= (int)tileBlock.Height - (int)height; ty++) {
             LearnPattern(tileBlock, tx, ty);
         }
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Add the tiles of the given tileBlock as a valid pattern
 /// </summary>
 /// <param name="tileBlock">tileBlock that specifies the pattern to add</param>
 /// <param name="startX">horizontal offset (in tileBlock) of the pattern to add</param>
 /// <param name="startY">vertical offset (in tileBlock) of the pattern to add</param>
 public void LearnPattern(TileBlock tileBlock, int startX, int startY)
 {
     TileBlock tb = new TileBlock(tileBlock, startX, startY, width, height);
     if (FindPattern(tb) == null) patterns.Add(tb);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Remove the tiles of the given tileBlock from the list of valid patterns
 /// </summary>
 /// <param name="tileBlock">tileBlock that specifies the pattern to remove</param>
 /// <param name="startX">horizontal offset (in tileBlock) of the pattern to remove</param>
 /// <param name="startY">vertical offset (in tileBlock) of the pattern to remove</param>
 public void ForgetPattern(TileBlock tileBlock, int startX, int startY)
 {
     TileBlock tb = new TileBlock(tileBlock, startX, startY, width, height);
     TileBlock pattern = FindPattern(tb);
     if (pattern != null) patterns.Remove(pattern);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Return the known pattern that has identical contents as the given TileBlock, or null if none is found
 /// </summary>
 /// <param name="tileBlock">tileBlock to search for</param>
 public TileBlock FindPattern(TileBlock tileBlock)
 {
     foreach (TileBlock pattern in patterns) {
         if (pattern.EqualContents(tileBlock)) return pattern;
     }
     return null;
 }
Ejemplo n.º 10
0
    /// <summary>
    /// Find the best pattern to use when changing tiles to one of the stored patterns (with topright position arguments).
    /// </summary>
    /// <param name="px">The starting X position at the <paramref name="tilemap"/> to look at.</param>
    /// <param name="py">The starting Y position at the <paramref name="tilemap"/> to look at.</param>
    /// <param name="tilemap">The tilemap to look at.</param>
    /// <param name="bestPattern">A tileblock that will replace the area.</param>
    /// <returns>
    /// True if <paramref name="bestPattern"/> is different from the calculated
    /// pattern in <paramref name="tilemap"/>, otherwise false.
    /// </returns>
    public bool FindBestPattern(int px, int py, Tilemap tilemap, ref TileBlock bestPattern)
    {
        // store subset of tilemap where brush will be applied as a reference pattern
        TileBlock tb = new TileBlock(tilemap, px, py, width, height);

        if(cache.ContainsKey(tb))
            bestPattern = (TileBlock) cache[tb];
        else {
            // find the stored pattern that matches this reference pattern best
            float bestSimilarity = 0;
            bestPattern = null;
            foreach (TileBlock pattern in patterns) {
                float sim = calculateSimilarity(pattern, tb);
                if (sim > bestSimilarity) {
                    bestSimilarity = sim;
                    bestPattern = pattern;
                }
            }
            if(cache.Count < 16)
                cache[tb] = bestPattern;
        }
        return ! ((bestPattern == null) || (bestPattern.EqualContents(tb)));
    }
Ejemplo n.º 11
0
    /// <summary>
    /// Find the best pattern to use when changing tiles around the given position to one of the stored patterns.
    /// </summary>
    /// <param name="pos">The (center) position at the <paramref name="tilemap"/> to look at.</param>
    /// <param name="tilemap">The tilemap to look at.</param>
    /// <param name="bestPattern">A tileblock that will replace the area.</param>
    /// <returns>
    /// True if <paramref name="bestPattern"/> is different from the calculated
    /// pattern in <paramref name="tilemap"/>, otherwise false.
    /// </returns>
    public bool FindBestPattern(FieldPos pos, Tilemap tilemap, ref TileBlock bestPattern)
    {
        // find upper-left corner of where to apply brush
        int px = pos.X - (int) (width / 2);
        int py = pos.Y - (int) (height / 2);

        return FindBestPattern(px, py, tilemap, ref bestPattern);
    }
Ejemplo n.º 12
0
 /// <summary>
 /// Replace deprecated tiles in tileblocks.
 /// </summary>
 /// <param name="tiles">The tileblock</param>
 /// <param name="TilesetFile">The TileSet file.</param>
 private static void ReplaceDeprecatedTiles(TileBlock tiles, string TilesetFile)
 {
     // We don't have any worldmap one currently
     if (TilesetFile != "images/tiles.strf")
         return;
     for (int y = 0; y < tiles.Height; ++y) {
         for (int x = 0; x < tiles.Width; ++x) {
             int TileId = tiles[x, y];
             if (LevelReplaceMap.ContainsKey(TileId)) {
                 tiles[x, y] = LevelReplaceMap[TileId];
                 LogManager.Log(LogLevel.Info, "Replaced deprecated tile {0} with {1}", TileId, LevelReplaceMap[TileId]);
             }
         }
     }
 }
Ejemplo n.º 13
0
    public void CreateSchematicTileBlocks(TileMap givenMap)
    {
        for (int x = 0; x < givenMap.xSize; x++)
        {
            for (int y = 0; y < givenMap.ySize; y++)
            {
                Tile       linkedTile = givenMap.GetTileAt(x, y);
                GameObject holder     = TileMapController.Instance.tileMapPlacer.GetHolderForTileMap(givenMap);
                bool       tileDrawn  = false;
                EcoBlock   linkedEB   = linkedTile.linkedEcoBlock;
                TileBlock  tileBlock  = Instantiate(tileBlockPrefab);
                GameObject blockObj   = tileBlock.gameObject;
                blockObj.transform.SetParent(holder.transform);
                tileBlock.xCoord     = x;
                tileBlock.yCoord     = y;
                tileBlock.linkedTile = linkedTile;
                blockObj.name        = "TileBlock X" + x + " Y" + y;
                blockObj.GetComponent <SpriteRenderer>().sortingLayerName = "TileBlockAboveUI";
                if (givenMap.onlyUsedInUICanvas == true)
                {
                    blockObj.transform.localPosition = new Vector3(x / mainCanvas.transform.localScale.x, y / mainCanvas.transform.localScale.y, 0);
                    blockObj.transform.localScale    = new Vector3(20 / holder.transform.localScale.x, 20 / holder.transform.localScale.y, 0);
                }
                else
                {
                    blockObj.transform.localPosition = new Vector3(x, y, 0);
                }

                TileMapController.Instance.activeSchematicTileMapBlocksList.Add(blockObj);

                foreach (World.WorldElement type in worldElementSpritePairings.Keys)
                {
                    if (type != World.WorldElement.Unassigned && tileDrawn == false && linkedTile.tileWorldElementType == type)
                    {
                        if (type == World.WorldElement.Location)
                        {
                            if (SpriteCollection.Instance.locationSubTypePairings.ContainsKey(linkedTile.linkedLocation.GetLocationSubType()))
                            {
                                blockObj.GetComponent <SpriteRenderer>().sprite = SpriteCollection.Instance.locationSubTypePairings[linkedTile.linkedLocation.GetLocationSubType()];
                            }
                            else
                            {
                                blockObj.GetComponent <SpriteRenderer>().sprite = SpriteCollection.Instance.locationTypePairings[linkedTile.linkedLocation.GetLocationType()];
                            }

                            blockObj.transform.localScale *= 3;
                        }
                        else
                        {
                            blockObj.GetComponent <SpriteRenderer>().sprite = worldElementSpritePairings[type];
                        }
                        tileDrawn = true;
                    }
                }

                foreach (EcoBlock.BlockType type in ecoBlockSpritePairings.Keys)
                {
                    if (type != EcoBlock.BlockType.Unassigned && tileDrawn == false && linkedTile.tileEcoBlockType == type)
                    {
                        blockObj.GetComponent <SpriteRenderer>().sprite = ecoBlockSpritePairings[type];
                        tileDrawn = true;
                    }
                }
                if (linkedTile.tileEcoBlockType == EcoBlock.BlockType.Ruler)
                {
                    blockObj.GetComponent <SpriteRenderer>().sprite = ecoBlockHierarchySpritePairings[EconomyController.Instance.GetRulerFromID(linkedEB.blockID).rulerHierarchy];
                }
            }
        }
    }
Ejemplo n.º 14
0
        public List <TileSet> ConvertLegacy(string fileName)
        {
            try
            {
                var data     = File.ReadAllBytes(fileName);
                var tileSets = new List <TileSet>();

                for (int i = 0; i < 16; i++)
                {
                    int bankOffset = i * 0x400;
                    var tileSet    = new TileSet();

                    for (int j = 0; j < 256; j++)
                    {
                        TileBlock tile = new TileBlock();

                        tile.UpperLeft  = data[bankOffset + j];
                        tile.LowerLeft  = data[bankOffset + 0x100 + j];
                        tile.UpperRight = data[bankOffset + 0x200 + j];
                        tile.LowerRight = data[bankOffset + 0x300 + j];

                        tileSet.TileBlocks[j] = tile;
                    }

                    tileSets.Add(tileSet);
                }

                var propertyOffset = 0x4000;
                for (int i = 0; i < 16; i++)
                {
                    for (int j = 0; j < 256; j++)
                    {
                        tileSets[i].TileBlocks[j].Property = data[propertyOffset++];
                    }
                }

                for (int i = 0; i < 16; i++)
                {
                    for (int k = 0; k < 8; k++)
                    {
                        tileSets[i].FireBallInteractions.Add(data[propertyOffset++]);
                    }

                    for (int k = 0; k < 8; k++)
                    {
                        tileSets[i].IceBallInteractions.Add(data[propertyOffset++]);
                    }

                    for (int k = 0; k < 8; k++)
                    {
                        tileSets[i].PSwitchAlterations.Add(new PSwitchAlteration(data[propertyOffset++], data[propertyOffset++]));
                    }
                }

                return(tileSets);
            }
            catch (Exception e)
            {
                _errorService.LogError(e);
                return(null);
            }
        }