Пример #1
0
        public IEnumerable <IStaticEntity> Decode(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return(Enumerable.Empty <IStaticEntity>());
            }

            string[] parts = input.Split("!");

            if (parts.Length == 2)
            {
                _gameBoard.TerrainSeed = int.Parse(parts[0]);
                input = parts[1];
            }
            else if (parts.Length > 2)
            {
                throw new InvalidOperationException("You are using a share code from the future, and this is the past. Please stop.");
            }

            var staticEntities = new List <IStaticEntity>();

            var br = new BitReader(Convert.FromBase64String(input));

            int col   = 0;
            int row   = 0;
            int count = 1;

            while (!br.EndOfBytes())
            {
                if (br.ReadBit() == 0)
                {
                    // Base64 likes to pad extra 0's, so just to be safe!
                    if (br.EndOfBytes())
                    {
                        break;
                    }

                    // Track
                    if (br.ReadBit() == 0)
                    {
                        // Empty
                        col += count;
                    }
                    else
                    {
                        // Track Piece
                        var dir = (TrackEncoding)br.Read4BitInt();

                        for (int i = 0; i < count; i++)
                        {
                            staticEntities.Add(new Track()
                            {
                                Column    = col++,
                                Row       = row,
                                Direction = s_directionMap[dir]
                            });
                        }
                    }
                    count = 1;
                }
                else
                {
                    // Control
                    if (br.ReadBit() == 0)
                    {
                        // Repeat
                        count = br.Read3BitInt();
                    }
                    else
                    {
                        // Misc
                        if (br.ReadBit() == 0)
                        {
                            // End Of Line
                            col  = 0;
                            row += count;
                        }
                        else
                        {
                            // Experimental Tree
                            for (int i = 0; i < count; i++)
                            {
                                staticEntities.Add(new Tree()
                                {
                                    Column = col++,
                                    Row    = row
                                });
                            }
                        }
                        count = 1;
                    }
                }
            }
            return(staticEntities);
        }
Пример #2
0
        public IEnumerable <Track> Decode(string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return(Enumerable.Empty <Track>());
            }

            var tracks = new List <Track>();

            var br = new BitReader(Convert.FromBase64String(input));

            int col   = 0;
            int row   = 0;
            int count = 1;

            while (!br.EndOfBytes())
            {
                if (br.ReadBit() == 0)
                {
                    // Base64 likes to pad extra 0's, so just to be safe!
                    if (br.EndOfBytes())
                    {
                        break;
                    }

                    // Track
                    if (br.ReadBit() == 0)
                    {
                        // Empty
                        col += count;
                    }
                    else
                    {
                        // Track Piece
                        var dir = (TrackEncoding)br.Read4BitInt();

                        for (int i = 0; i < count; i++)
                        {
                            tracks.Add(new Track(null)
                            {
                                Column    = col++,
                                Row       = row,
                                Direction = s_directionMap[dir]
                            });
                        }
                    }
                    count = 1;
                }
                else
                {
                    // Control
                    if (br.ReadBit() == 0)
                    {
                        // Repeat
                        count = br.Read3BitInt();
                    }
                    else
                    {
                        // Misc
                        if (br.ReadBit() == 0)
                        {
                            // End Of Line
                            col   = 0;
                            row  += count;
                            count = 1;
                        }
                        else
                        {
                            // Experimental
                            throw new Exception("Potato is a fruit, implement this please <3");
                        }
                    }
                }
            }
            return(tracks);
        }