/// <summary> /// Erstellt eine Kopie der Konfiguration /// </summary> /// <returns>Kopie der aktuellen Konfiguration</returns> public object Clone() { // Kopie erstellen und Spielerliste kopieren SimulatorConfiguration output = (SimulatorConfiguration)MemberwiseClone(); output.teams = new List <TeamInfo>(teams.Count); foreach (TeamInfo team in teams) { output.teams.Add((TeamInfo)team.Clone()); } return(output); }
/// <summary> /// Initialisiert die Simulation um mit Runde 1 zu beginnen /// </summary> /// <param name="configuration">Konfiguration der Simulation</param> /// <throws><see cref="ArgumentException"/></throws> /// <throws><see cref="ArgumentNullException"/></throws> /// <throws><see cref="PathTooLongException"/></throws> /// <throws><see cref="DirectoryNotFoundException"/></throws> /// <throws><see cref="IOException"/></throws> /// <throws><see cref="UnauthorizedAccessException"/></throws> /// <throws><see cref="FileNotFoundException"/></throws> /// <throws><see cref="NotSupportedException"/></throws> /// <throws><see cref="SecurityException"/></throws> /// <throws><see cref="FileLoadException"/></throws> /// <throws><see cref="BadImageFormatException"/></throws> /// <throws><see cref="RuleViolationException"/></throws> /// <throws><see cref="TypeLoadException"/></throws> public void Init(SimulatorConfiguration configuration) { // Init some varialbes currentRound = 0; if (configuration.MapInitialValue != 0) { random = new Random(configuration.MapInitialValue); } else { random = new Random(); } // count players playerCount = 0; foreach (TeamInfo team in configuration.Teams) { playerCount += team.Player.Count; } // Sugar-relevant stuff sugarDelay = 0; sugarCountDown = (int)(SimulationSettings.Custom.SugarTotalCount * (1 + (SimulationSettings.Custom.SugarTotalCountPlayerMultiplier * playerCount))); sugarLimit = (int)(SimulationSettings.Custom.SugarSimultaneousCount * (1 + (SimulationSettings.Custom.SugarCountPlayerMultiplier * playerCount))); // Fruit-relevant stuff fruitDelay = 0; fruitCountDown = (int)(SimulationSettings.Custom.FruitTotalCount * (1 + (SimulationSettings.Custom.FruitTotalCountPlayerMultiplier * playerCount))); fruitLimit = (int)(SimulationSettings.Custom.FruitSimultaneousCount * (1 + (SimulationSettings.Custom.FruitCountPlayerMultiplier * playerCount))); // Ant-relevant stuff int antCountDown = (int)(SimulationSettings.Custom.AntTotalCount * (1 + (SimulationSettings.Custom.AntTotalCountPlayerMultiplier * playerCount))); antLimit = (int)(SimulationSettings.Custom.AntSimultaneousCount * (1 + (SimulationSettings.Custom.AntCountPlayerMultiplier * playerCount))); // Spielfeld erzeugen float area = SimulationSettings.Custom.PlayGroundBaseSize; area *= 1 + (playerCount * SimulationSettings.Custom.PlayGroundSizePlayerMultiplier); int playgroundWidth = (int)Math.Round(Math.Sqrt(area * 4 / 3)); int playgroundHeight = (int)Math.Round(Math.Sqrt(area * 3 / 4)); Playground = new CorePlayground(playgroundWidth, playgroundHeight, random, playerCount); // Bugs-relevant stuff Bugs = new CoreColony(Playground); Bugs.insectCountDown = (int)(SimulationSettings.Custom.BugTotalCount * (1 + (SimulationSettings.Custom.BugTotalCountPlayerMultiplier * playerCount))); bugLimit = (int)(SimulationSettings.Custom.BugSimultaneousCount * (1 + (SimulationSettings.Custom.BugCountPlayerMultiplier * playerCount))); // Völker erzeugen Teams = new CoreTeam[configuration.Teams.Count]; for (int i = 0; i < configuration.Teams.Count; i++) { TeamInfo team = configuration.Teams[i]; Teams[i] = new CoreTeam(i, team.Guid, team.Name); Teams[i].Colonies = new CoreColony[team.Player.Count]; // Völker erstellen for (int j = 0; j < team.Player.Count; j++) { PlayerInfo player = team.Player[j]; CoreColony colony = new CoreColony(Playground, player, Teams[i]); Teams[i].Colonies[j] = colony; colony.AntHills.Add(Playground.NeuerBau(colony.Id)); colony.insectCountDown = antCountDown; } } }
/// <summary> /// Creates an Instance of simulator. Should be called only from factory. /// </summary> /// <param name="configuration">configuration</param> public Simulator(SimulatorConfiguration configuration) { // Leere Konfiguration prüfen if (configuration == null) { throw new ArgumentNullException("configuration", Resource.SimulationCoreFactoryConfigIsNull); } // Copy config this.configuration = (SimulatorConfiguration)configuration.Clone(); // Reload PlayerInfo if (this.configuration.Teams != null) { foreach (TeamInfo team in this.configuration.Teams) { if (team.Player != null) { for (int i = 0; i < team.Player.Count; i++) { PlayerInfo player = team.Player[i]; if (player is PlayerInfoFiledump) { team.Player[i] = AiAnalysis.FindPlayerInformation( ((PlayerInfoFiledump)player).File, player.ClassName); } else if (player is PlayerInfoFilename) { team.Player[i] = AiAnalysis.FindPlayerInformation( ((PlayerInfoFilename)player).File, player.ClassName); } else { // TODO: Throw unknown type... } team.Player[i].Guid = player.Guid; } } } } // Regelprüfung der Konfig anwerfen configuration.Rulecheck(); // Init values currentLoop = 0; currentRound = 0; roundTime = 0; loopTime = 0; totalTime = 0; // Reset totalPlayerTime = new Dictionary <Guid, long>(); for (int i = 0; i < configuration.Teams.Count; i++) { for (int j = 0; j < configuration.Teams[i].Player.Count; j++) { Guid guid = configuration.Teams[i].Player[j].Guid; if (!totalPlayerTime.ContainsKey(guid)) { totalPlayerTime.Add(guid, 0); } } } // Set initial state state = SimulatorState.Ready; }
/// <summary> /// Initialize the simulation-environment. /// </summary> /// <param name="config">configuration</param> public bool Init(SimulatorConfiguration config) { // Prepare values configuration = config; SimulationSettings.SetCustomSettings(config.Settings); environment = null; // Prepare time-counting playerTimes = new Dictionary <PlayerInfo, long>(); currentPlayer = null; currentArea = Area.Unknown; // Load Playerfiles foreach (TeamInfo team in configuration.Teams) { foreach (PlayerInfo spieler in team.Player) { if (spieler is PlayerInfoFiledump) { // Try, to load filedump try { spieler.assembly = Assembly.Load(((PlayerInfoFiledump)spieler).File); } catch (Exception ex) { exception = ex; return(false); } } else if (spieler is PlayerInfoFilename) { // Try, to load filename try { spieler.assembly = Assembly.LoadFile(((PlayerInfoFilename)spieler).File); } catch (Exception ex) { exception = ex; return(false); } } else { exception = new InvalidOperationException( Resource.SimulationCoreHostWrongPlayerInfo); return(false); } // Add player to counter-list // TODO: Need another key for times // playerTimes.Add(spieler, 0); } } // Init environment try { environment = new SimulationEnvironment(); environment.AreaChange += umgebung_Verantwortungswechsel; environment.Init(configuration); } catch (Exception ex) { exception = ex; return(false); } // Everything nice... return(true); }
/// <summary> /// Initialisierung der Simulation /// </summary> /// <param name="configuration">Simulationskonfiguration</param> public void Init(SimulatorConfiguration configuration) { // unload possible appdomains. Unload(); // load involved ai's for (int team = 0; team < configuration.Teams.Count; team++) { for (int i = 0; i < configuration.Teams[team].Player.Count; i++) { // externe Guid-Info speichern Guid guid = configuration.Teams[team].Player[i].Guid; if (configuration.Teams[team].Player[i] is PlayerInfoFiledump) { // use filedump configuration.Teams[team].Player[i] = AiAnalysis.FindPlayerInformation( ((PlayerInfoFiledump)configuration.Teams[team].Player[i]).File, configuration.Teams[team].Player[i].ClassName); } else if (configuration.Teams[team].Player[i] is PlayerInfoFilename) { // use filename configuration.Teams[team].Player[i] = AiAnalysis.FindPlayerInformation( ((PlayerInfoFilename)configuration.Teams[team].Player[i]).File, configuration.Teams[team].Player[i].ClassName); } else { // not supported PlayerInfo-type throw new InvalidOperationException( Resource.SimulationCoreProxyWrongPlayerInfo); } // Rückspeicherung der externen Guid configuration.Teams[team].Player[i].Guid = guid; } } // setup appDomain AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; setup.ShadowCopyDirectories = AppDomain.CurrentDomain.SetupInformation.ShadowCopyDirectories; setup.ShadowCopyFiles = "false"; setup.PrivateBinPath = ""; // setup some access-rights für appdomain PermissionSet rechte = new PermissionSet(PermissionState.None); rechte.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); rechte.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess)); bool IoAccess = false; bool UiAccess = false; bool DbAccess = false; bool NetAccess = false; // allow access to the needed ai-files foreach (TeamInfo team in configuration.Teams) { foreach (PlayerInfo info in team.Player) { if (info is PlayerInfoFilename) { rechte.AddPermission( new FileIOPermission( FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, ((PlayerInfoFilename)info).File)); } if (info.RequestDatabaseAccess) { DbAccess = true; } if (info.RequestFileAccess) { IoAccess = true; } if (info.RequestNetworkAccess) { NetAccess = true; } if (info.RequestUserInterfaceAccess) { UiAccess = true; } } } // Grand special rights if (IoAccess) { if (configuration.AllowFileAccess) { rechte.AddPermission(new FileIOPermission(PermissionState.Unrestricted)); } else { throw new ConfigurationErrorsException(Resource.SimulationCoreRightsConflictIo); } } if (UiAccess) { if (configuration.AllowUserinterfaceAccess) { rechte.AddPermission(new UIPermission(PermissionState.Unrestricted)); } else { throw new ConfigurationErrorsException(Resource.SimulationCoreRightsConflictUi); } } if (DbAccess) { if (configuration.AllowDatabaseAccess) { // TODO: Grand rights } else { throw new ConfigurationErrorsException(Resource.SimulationCoreRightsConflictDb); } } if (NetAccess) { if (configuration.AllowNetworkAccess) { rechte.AddPermission(new System.Net.WebPermission(PermissionState.Unrestricted)); rechte.AddPermission(new System.Net.SocketPermission(PermissionState.Unrestricted)); rechte.AddPermission(new System.Net.DnsPermission(PermissionState.Unrestricted)); } else { throw new ConfigurationErrorsException(Resource.SimulationCoreRightsConflictNet); } } // create appdomain and load simulation-host appDomain = AppDomain.CreateDomain("Simulation", AppDomain.CurrentDomain.Evidence, setup, rechte); host = (SimulatorHost) appDomain.CreateInstanceAndUnwrap( Assembly.GetExecutingAssembly().FullName, "AntMe.Simulation.SimulatorHost"); // initialize host if (!host.Init(configuration)) { // got some exceptions - unload appdomain und throw exception Exception ex = host.Exception; Unload(); throw ex; } }