Beispiel #1
0
        /// <summary>
        /// Uninitializes the player with the given index.
        /// Uninitializing a player is only allowed if players have not yet been finalized.
        /// Players can be finalized using the Scenario.FinalizePlayers method.
        /// </summary>
        /// <param name="index">The index of the player to uninitialize.</param>
        public void UninitializePlayer(int index)
        {
            if (this.playersFinalized.Read() != 0x00)
            {
                throw new InvalidOperationException("Players already finalized!");
            }
            if (index < 0 || index >= Player.MAX_PLAYERS)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            if (this.players[index].Read() == null)
            {
                throw new SimulatorException(string.Format("Player with index {0} has not yet been initialized!", index));
            }

            StartLocation startLoc = this.players[index].Read().StartLocation;

            startLoc.AttachToMap(this.map.GetQuadTile(this.players[index].Read().QuadraticStartPosition.Location));
            RCSet <Entity> entitiesOfPlayer = new RCSet <Entity>(this.players[index].Read().Entities);

            this.players[index].Read().Dispose();
            this.players[index].Write(null);

            /// Destroy entities of the player.
            foreach (Entity entity in entitiesOfPlayer)
            {
                if (entity.HasMapObject(MapObjectLayerEnum.GroundObjects, MapObjectLayerEnum.AirObjects))
                {
                    entity.DetachFromMap();
                }
                this.RemoveElementFromScenario(entity);
                entity.Dispose();
            }
        }
Beispiel #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);
        }