/// <summary> /// Initializes the player with the given index at the given start location. /// Initializing 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 initialize.</param> /// <param name="startLocation">The start location of the player to initialize.</param> /// <param name="race">The race of the player to initialize.</param> public void InitializePlayer(int index, StartLocation startLocation, RaceEnum race) { if (this.playersFinalized.Read() != 0x00) { throw new InvalidOperationException("Players already finalized!"); } if (startLocation == null) { throw new ArgumentNullException("startLocation"); } if (startLocation.Scenario != this) { throw new SimulatorException("The given start location doesn't belong to the scenario!"); } 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 already been initialized!", index)); } Player newPlayer = new Player(index, startLocation); startLocation.DetachFromMap(); this.players[index].Write(newPlayer); this.elementFactory.InitializePlayer(newPlayer, race); }
/// <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(); } }
/// <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); }
/// <summary> /// Constructs a new player instance. /// </summary> /// <param name="playerIndex">The index of the player.</param> /// <param name="startLocation">The start location of the player.</param> internal Player(int playerIndex, StartLocation startLocation) { if (playerIndex < 0 || playerIndex >= Player.MAX_PLAYERS) { throw new ArgumentOutOfRangeException("playerIndex"); } if (startLocation == null) { throw new ArgumentNullException("startLocation"); } if (startLocation.Scenario == null) { throw new SimulatorException("The given start location doesn't belong to a scenario!"); } if (!startLocation.HasMapObject(MapObjectLayerEnum.GroundObjects)) { throw new SimulatorException("The given start location has already been initialized!"); } ScenarioMetadataUpgrade metadata = new ScenarioMetadataUpgrade(); metadata.AttachMetadata(ComponentManager.GetInterface <IScenarioLoader>().Metadata); this.metadata = metadata; this.metadataUpgrade = metadata; this.playerIndex = this.ConstructField <int>("playerIndex"); this.startLocation = this.ConstructField <StartLocation>("startLocation"); this.startPosition = this.ConstructField <RCNumVector>("startPosition"); this.quadraticStartPosition = this.ConstructField <RCIntRectangle>("quadraticStartPosition"); this.minerals = this.ConstructField <RCNumber>("minerals"); this.vespeneGas = this.ConstructField <RCNumber>("vespeneGas"); this.lockedSupplies = this.ConstructField <int>("lockedSupplies"); this.usedSupplyCache = new CachedValue <int>(this.CalculateUsedSupply); this.totalSupplyCache = new CachedValue <int>(this.CalculateTotalSupply); this.playerIndex.Write(playerIndex); this.startLocation.Write(startLocation); this.startPosition.Write(startLocation.MotionControl.PositionVector.Read()); this.quadraticStartPosition.Write(startLocation.MapObject.QuadraticPosition); this.buildings = new Dictionary <string, RCSet <Building> >(); this.addons = new Dictionary <string, RCSet <Addon> >(); this.units = new Dictionary <string, RCSet <Unit> >(); this.upgrades = new Dictionary <string, Upgrade>(); this.minerals.Write(INITIAL_MINERALS); this.vespeneGas.Write(INITIAL_VESPENE_GAS); this.lockedSupplies.Write(0); }