Example #1
0
        /// <summary>
        /// Initializes the isometric tiles of the map structure.
        /// </summary>
        /// <param name="isotileListPackage">The package that contains the isometric tile informations.</param>
        private void LoadIsoTiles(RCPackage isotileListPackage)
        {
            string[] terrainIndexTable = isotileListPackage.ReadStringArray(0);
            byte[]   isotileInfoBytes  = isotileListPackage.ReadByteArray(1);

            int offset = 0;

            while (offset < isotileInfoBytes.Length)
            {
                int       parsedBytes;
                RCPackage package = RCPackage.Parse(isotileInfoBytes, offset, isotileInfoBytes.Length - offset, out parsedBytes);
                if (package == null || !package.IsCommitted)
                {
                    throw new MapException("Syntax error!");
                }
                offset += parsedBytes;
                if (package.PackageFormat.ID == MapFileFormat.ISOTILE)
                {
                    RCIntVector        quadCoords   = new RCIntVector(package.ReadShort(0), package.ReadShort(1));
                    TerrainCombination terrainCombo = (TerrainCombination)package.ReadByte(4);
                    string             terrainA     = terrainIndexTable[package.ReadByte(2)];
                    string             terrainB     = terrainCombo != TerrainCombination.Simple ? terrainIndexTable[package.ReadByte(3)] : null;
                    int variantIdx = package.ReadByte(5);

                    this.mapStructure.InitIsoTile(quadCoords,
                                                  terrainCombo == TerrainCombination.Simple ?
                                                  this.mapStructure.Tileset.GetIsoTileType(terrainA) :
                                                  this.mapStructure.Tileset.GetIsoTileType(terrainA, terrainB, terrainCombo),
                                                  variantIdx);
                }
            }
        }
Example #2
0
        /// <see cref="IScenarioLoader.LoadScenario"/>
        public Scenario LoadScenario(IMapAccess map, byte[] data)
        {
            if (map == null)
            {
                throw new ArgumentNullException("map");
            }
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            /// Load the packages from the byte array.
            int      offset   = 0;
            Scenario scenario = new Scenario(map);

            while (offset < data.Length)
            {
                int       parsedBytes;
                RCPackage package = RCPackage.Parse(data, offset, data.Length - offset, out parsedBytes);
                if (package == null || !package.IsCommitted)
                {
                    throw new SimulatorException("Syntax error!");
                }
                offset += parsedBytes;
                if (package.PackageFormat.ID == ScenarioFileFormat.MINERAL_FIELD)
                {
                    IQuadTile    quadTile     = map.GetQuadTile(new RCIntVector(package.ReadShort(0), package.ReadShort(1)));
                    MineralField mineralField = new MineralField();
                    mineralField.ResourceAmount.Write(package.ReadInt(2));
                    scenario.AddElementToScenario(mineralField);
                    mineralField.AttachToMap(quadTile);
                }
                else if (package.PackageFormat.ID == ScenarioFileFormat.VESPENE_GEYSER)
                {
                    IQuadTile     quadTile      = map.GetQuadTile(new RCIntVector(package.ReadShort(0), package.ReadShort(1)));
                    VespeneGeyser vespeneGeyser = new VespeneGeyser();
                    vespeneGeyser.ResourceAmount.Write(package.ReadInt(2));
                    scenario.AddElementToScenario(vespeneGeyser);
                    vespeneGeyser.AttachToMap(quadTile);
                }
                else if (package.PackageFormat.ID == ScenarioFileFormat.START_LOCATION)
                {
                    IQuadTile     quadTile      = map.GetQuadTile(new RCIntVector(package.ReadShort(0), package.ReadShort(1)));
                    StartLocation startLocation = new StartLocation(package.ReadByte(2));
                    scenario.AddElementToScenario(startLocation);
                    startLocation.AttachToMap(quadTile);
                }
            }

            /// Check the constraints of the visible entities.
            foreach (Entity entity in scenario.GetElementsOnMap <Entity>(MapObjectLayerEnum.GroundObjects, MapObjectLayerEnum.AirObjects))
            {
                if (entity.CheckPlacementConstraints(entity.MapObject.QuadraticPosition.Location, new RCSet <Entity>()).Count != 0)
                {
                    throw new MapException(string.Format("Entity at {0} is voilating its placement constraints!", entity.MapObject.QuadraticPosition.Location));
                }
            }
            return(scenario);
        }
Example #3
0
 public void GuestCommand(int guestIndex, RCPackage command)
 {
     if (command.PackageFormat.ID == TestClientMessages.COMMAND)
     {
         PlayerDirection dir = (PlayerDirection)command.ReadByte(0);
         this.simulator.GetPlayer(guestIndex + 1).Direction = dir;
     }
 }
Example #4
0
        /// <summary>
        /// Creates a MapHeader structure from the given RCPackage.
        /// </summary>
        /// <param name="package">The RCPackage that contains the map header informations.</param>
        /// <returns>The created MapHeader structure.</returns>
        public static MapHeader FromPackage(RCPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }
            if (!package.IsCommitted)
            {
                throw new ArgumentException("The header package is not committed!", "package");
            }
            if (package.PackageType != RCPackageType.CUSTOM_DATA_PACKAGE)
            {
                throw new ArgumentException("Invalid package type!", "package");
            }
            if (package.PackageFormat.ID != MapFileFormat.MAP_HEADER)
            {
                throw new ArgumentException("Invalid package format!", "package");
            }

            MapHeader header = new MapHeader();

            header.appVersion   = new Version(package.ReadInt(0), package.ReadInt(1), package.ReadInt(2), package.ReadInt(3));
            header.mapName      = package.ReadString(4);
            header.tilesetName  = package.ReadString(5);
            header.mapSize      = new RCIntVector(package.ReadShort(6), package.ReadShort(7));
            header.maxPlayers   = package.ReadByte(8);
            header.checksumList = new List <int>(package.ReadIntArray(9));

            if (header.mapName == null)
            {
                throw new MapException("Map name information is missing!");
            }
            if (header.tilesetName == null)
            {
                throw new MapException("Tileset name information is missing!");
            }
            if (header.mapSize.X <= 0 || header.mapSize.Y <= 0)
            {
                throw new MapException("Map size cannot be negative or 0!");
            }
            if (header.maxPlayers <= 0)
            {
                throw new MapException("Maximum number of players cannot be negative or 0!");
            }
            return(header);
        }
Example #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="package"></param>
        private void CreateExceptionRow(RCPackage package)
        {
            string threadName = package.ReadString(1);
            long   timestamp  = package.ReadLong(2);
            bool   isFatal    = (package.ReadByte(3) == (byte)0x00) ? false : true;
            string ex         = package.ReadString(4);

            if (!this.threadsToColumns.ContainsKey(threadName))
            {
                int idxOfCol = this.gridLog.Columns.Add(threadName, threadName);
                this.threadsToColumns.Add(threadName, idxOfCol);
            }

            object[] rowContent = new object[this.threadsToColumns.Count];
            rowContent[this.threadsToColumns[threadName]] = isFatal ? "FATAL_EXCEPTION" : "EXCEPTION";

            int idxOfRow = this.gridLog.Rows.Add(rowContent);

            this.gridLog.Rows[idxOfRow].HeaderCell.Value = timestamp.ToString();

            DataGridViewCell cellOfException = this.gridLog[this.threadsToColumns[threadName], idxOfRow];

            cellOfException.Style.BackColor = isFatal ? Color.Red : Color.Yellow;

            if (isFatal)
            {
                if (!this.fatalExceptions.ContainsKey(cellOfException))
                {
                    this.fatalExceptions.Add(cellOfException, ex);
                }
            }
            else
            {
                if (!this.normalExceptions.ContainsKey(cellOfException))
                {
                    this.normalExceptions.Add(cellOfException, ex);
                }
            }
        }
Example #6
0
        /// <summary>
        /// Initializes the terrain objects of the map.
        /// </summary>
        /// <param name="terrainObjListPackage">The package that contains the terrain object informations.</param>
        /// <param name="map">Reference to the map.</param>
        private void LoadTerrainObjects(RCPackage terrainObjListPackage, IMapAccess map)
        {
            /// TODO: Avoid this downcast!
            MapAccess mapObj = map as MapAccess;

            if (mapObj == null)
            {
                throw new ArgumentException("The given map cannot be handled by the MapEditor!", "map");
            }

            string[] terrainObjIndexTable = terrainObjListPackage.ReadStringArray(0);
            byte[]   terrainObjInfoBytes  = terrainObjListPackage.ReadByteArray(1);

            int offset = 0;

            while (offset < terrainObjInfoBytes.Length)
            {
                int       parsedBytes;
                RCPackage package = RCPackage.Parse(terrainObjInfoBytes, offset, terrainObjInfoBytes.Length - offset, out parsedBytes);
                if (package == null || !package.IsCommitted)
                {
                    throw new MapException("Syntax error!");
                }
                offset += parsedBytes;
                if (package.PackageFormat.ID == MapFileFormat.TERRAINOBJ)
                {
                    RCIntVector        quadCoords     = new RCIntVector(package.ReadShort(0), package.ReadShort(1));
                    ITerrainObjectType terrainObjType = this.mapStructure.Tileset.GetTerrainObjectType(terrainObjIndexTable[package.ReadByte(2)]);

                    /// TODO: Might be better to create the TerrainObject with a factory?
                    ITerrainObject newObj = new TerrainObject(map, terrainObjType, quadCoords);
                    foreach (ICellDataChangeSet changeset in newObj.Type.CellDataChangesets)
                    {
                        changeset.Apply(newObj);
                    }
                    mapObj.AttachTerrainObject(newObj);
                }
            }

            /// Check the constraints of the terrain objects.
            List <ITerrainObject> terrainObjects = new List <ITerrainObject>(map.TerrainObjects);

            foreach (ITerrainObject terrainObj in terrainObjects)
            {
                mapObj.DetachTerrainObject(terrainObj);
                if (terrainObj.Type.CheckConstraints(map, terrainObj.MapCoords).Count != 0)
                {
                    throw new MapException(string.Format("Terrain object at {0} is voilating the tileset constraints!", terrainObj.MapCoords));
                }
                if (terrainObj.Type.CheckTerrainObjectIntersections(map, terrainObj.MapCoords).Count != 0)
                {
                    throw new MapException(string.Format("Terrain object at {0} intersects other terrain objects!", terrainObj.MapCoords));
                }
                mapObj.AttachTerrainObject(terrainObj);
            }
        }