Exemplo n.º 1
0
        public void Run()
        {
            Thread.CurrentThread.Name = "Main thread";
            //Create ReadWrite lock for world data access
            DataLock dataLock = new DataLock();
            //Create UI Thread
            Thread uiThread = new Thread(() => _ui.StartRenderLoop(dataLock));

            uiThread.SetApartmentState(ApartmentState.STA);
            uiThread.Name = "UiThread";
            uiThread.Start();
            //Wait for the user to press on "Generate World"
            bool ready;

            do
            {
                ready = GenerateWorldEvent.WaitOne(50);
            } while (_continue && !ready);
            if (!_continue)
            {
                return;
            }
            //Generate the world
            DebugConsole.WriteLine("Generation begin");
            _world = (World)((IWorldGenerator)(new WorldGenerator(_dailyNotifications, _liveNotifications, dataLock)))
                     .Generate(s => _loadingMessage = s);
            DebugConsole.WriteLine("Generation is done");
            _world.SetNewRealmHandler(NewRealm);
            //Choose one of the realms as the UI realm
            _worldContainer.PlayerRealm = _tokens.First();
            _ais.Remove(_ais.Single(ai => ai.Realm == _tokens.First().Realm));
            _world.RegisterInteractiveRealm(_tokens.First());
            _worldContainer.World  = _world;
            _isWorldGenerationDone = true;
            _dayTimer.Start();
            while (_continue)
            {
                _dailyNotifications.Clear();
                ((IProcessableWorld)_world).Process();
                Parallel.ForEach(_ais,
                                 new ParallelOptions {
                    MaxDegreeOfParallelism = 4
                },
                                 ai => _world.RegisterCommands(ai.RealmToken, ai.Process(dataLock, _dailyNotifications)));
                DebugConsole.WriteLine("Computations done");
                _dayTimer.WaitForNextDay(_continue);
                DebugConsole.WriteLine("Day end");
            }
        }