Esempio n. 1
0
        public GroupSumWindow(GroupSumSettings settings)
            : base(settings.ScreenWidth,
			settings.ScreenHeight, GraphicsMode.Default, "Group Sum",
			settings.Fullscreen ? GameWindowFlags.Fullscreen : 0)
        {
            logger.Info("Initializing Group Sum game window");
            this.Settings = settings;

            Keyboard.KeyDown += Keyboard_KeyDown;

            plotSurface = new GroupSumPlotSurface(Settings.TargetNumber);

            maximumPossibleSum = Settings.Players * (GroupSumSettings.PlayerButtonCount - 1);
            plotSurface.SetPlotAxes(0, 1, 0, maximumPossibleSum + 1);

            CalculateSizesAndLoadFonts();
            SetStatusText(string.Format("Waiting for guesses ({0})", Settings.FirstRoundSeconds));

            // Open data file
            if (!string.IsNullOrEmpty(Settings.DataFilePath))
            {
                logger.DebugFormat("Opening data file at {0}", Settings.DataFilePath);
                dataWriter = new YAMLOutputStream(Settings.DataFilePath);
            }

            // Validate number of players
            var textureBasePath = Path.Combine(basePath, Path.Combine("etc", "player_images"));
            var playerImageCount = Directory.GetFiles(textureBasePath, "*.png").Length;

            if (Settings.Players > playerImageCount)
            {
                logger.WarnFormat("Too few player images are present, only allowing {0} players", playerImageCount);
                Settings.Players = playerImageCount;
            }

            players = Enumerable.Range(0, Settings.Players).Select(i => new Player(i)).ToArray();

            // Load textures
            logger.DebugFormat("Loading textures from {0}", textureBasePath);

            playerTextureIds = Textures.LoadPlayers(textureBasePath, Settings.Players).ToArray();

            // Output settings and initial player positions
            if (dataWriter != null)
            {
                dataWriter.WriteStartDocument();
                dataWriter.WriteHashSingle("Version", GroupSumSettings.FileVersion);

                dataWriter.WriteLine();
                dataWriter.WriteStartList("Settings");
                dataWriter.WriteHashSingle("Description", Settings.GameDescription);
                dataWriter.WriteHashSingle("Players", Settings.Players);
                dataWriter.WriteHashSingle("First Round Duration", Settings.FirstRoundSeconds);
                dataWriter.WriteHashSingle("Round Duration", Settings.RoundSeconds);
                dataWriter.WriteHashSingle("Target Number", Settings.TargetNumber);
                dataWriter.WriteHashSingle("Range", string.Format("{0} - {1}",
                    settings.MinNumber, settings.MaxNumber));

                dataWriter.WriteHashSingle("Show Numeric Feedback", Settings.ShowNumericFeedback);
                dataWriter.WriteHashSingle("Use Previous Input", Settings.UsePreviousRoundInput);

                dataWriter.WriteEndList();

                dataWriter.WriteLine();
                dataWriter.WriteStartList("Rounds");
                dataWriter.WriteComment("round, player, guess");
            }

            // Connect to input server
            if (Settings.Port > 0)
            {
                try
                {
                    inputClient.Connect(IPAddress.Loopback, Settings.Port);
                    inputClient.Client.BeginReceive(inputBuffer, 0, 2, SocketFlags.None,
                        inputClient_DataReceived, null);
                }
                catch (Exception ex)
                {
                    logger.Error("Failed to connect to input server", ex);
                }
            }

            secondsLeftInRound = Settings.FirstRoundSeconds;
            roundTimer = new System.Timers.Timer(1000);
            roundTimer.Elapsed += roundTimer_Elapsed;
            roundTimer.Start();
        }
Esempio n. 2
0
        public FlatlandWindow(FlatlandSettings settings)
            : base(settings.ScreenWidth,
      settings.ScreenHeight, GraphicsMode.Default, "Flatland",
      settings.Fullscreen ? GameWindowFlags.Fullscreen : 0)
        {
            logger.Info("Initializing Flatland game window");
              this.Settings = settings;

              Keyboard.KeyDown += Keyboard_KeyDown;

              // Open data file
              if (!string.IsNullOrEmpty(Settings.DataFilePath))
              {
            logger.DebugFormat("Opening data file at {0}", Settings.DataFilePath);
            dataWriter = new YAMLOutputStream(Settings.DataFilePath);
              }

              // Validate number of players
              var textureBasePath = Path.Combine(basePath, Path.Combine("etc", "player_images"));
              var playerImageCount = Directory.GetFiles(textureBasePath, "*.png").Length;

              if (Settings.Players > playerImageCount)
              {
            logger.WarnFormat("Too few player images are present, only allowing {0} players", playerImageCount);
            Settings.Players = playerImageCount;
              }

              pixelCount = Settings.Tiles * Settings.Tiles;

              // Calculate pixel and board sizes
              CalculateSizesAndLoadFonts();

              // Load textures
              logger.DebugFormat("Loading textures from {0}", textureBasePath);
              playerTextureIds = Textures.LoadPlayers(textureBasePath, Settings.Players).ToArray();

              // Create players
              players.AddRange(Enumerable.Range(0, Settings.Players)
            .Select(i => new Player(i))
              );

              bool assignRandomTeams = true;

              if (Settings.GameType == FlatlandGameType.OnePrey)
              {
            bool preyAssigned = false;
            var preyTeam = Settings.Teams.FirstOrDefault(t => t.Kind == FlatlandTeamKind.Prey);
            var predatorTeam = Settings.Teams.FirstOrDefault(t => t.Kind == FlatlandTeamKind.Predator);

            if ((preyTeam == null) || (predatorTeam == null))
            {
              logger.Error("Prey and predator teams are required for One Prey game. Assigning teams randomly...");
              assignRandomTeams = true;
            }
            else
            {
              // Choose a random person to be the prey
              foreach (var player in players.OrderBy(p => rand.Next()))
              {
            if (!preyAssigned)
            {
              player.Team = preyTeam;
              player.SetBoxTheme(preyTeam.Theme);
              player.IsPredator = false;
              player.IsPrey = true;
              preyAssigned = true;
            }
            else
            {
              player.Team = predatorTeam;
              player.SetBoxTheme(predatorTeam.Theme);
              player.IsPredator = true;
              player.IsPrey = false;
              preyAssigned = true;
            }
              } // for each player

              assignRandomTeams = false;
            }

              } // if game type is One Prey

              if (assignRandomTeams)
              {
            // Assign teams in order (players order will be random, but first teams get priority)
            var teamPlayerQueue = new Queue<Player>(players.OrderBy(p => rand.Next()));
            var orderedTeams = Settings.Teams.OrderBy(t => t.TeamIndex).ToList();
            int teamIndex = 0;

            while (teamPlayerQueue.Count > 0)
            {
              var team = orderedTeams[teamIndex];
              var player = teamPlayerQueue.Dequeue();

              player.Team = team;
              player.SetBoxTheme(team.Theme);
              player.IsPredator = (team.Kind == FlatlandTeamKind.Predator);
              player.IsPrey = (team.Kind == FlatlandTeamKind.Prey);

              teamIndex = (teamIndex + 1) % orderedTeams.Count;
            }
              }

              // Only show team names if there's more than one
              Settings.ShowTeamNames = (Settings.Teams.Count > 1);

              // Place players on the board
              SetupPlayerSort();

              // Output settings and initial player positions
              if (dataWriter != null)
              {
            dataWriter.WriteStartDocument();
            dataWriter.WriteHashSingle("Version", FlatlandSettings.FileVersion);

            dataWriter.WriteLine();
            dataWriter.WriteStartList("Settings");
            dataWriter.WriteHashSingle("Description", Settings.GameDescription);
            dataWriter.WriteHashSingle("Players", players.Count);
            dataWriter.WriteHashSingle("Size", string.Format("{0}x{0}", Settings.Tiles));
            dataWriter.WriteHashSingle("Collision Behavior", Settings.CollisionBehavior);
            dataWriter.WriteHashSingle("Game Type", Settings.GameType);

            if (Settings.Teams.Count > 0)
            {
              dataWriter.WriteStartList("Teams");
              dataWriter.WriteComment("team number, theme, move seconds, kind, wrap, scoring system");

              foreach (var team in Settings.Teams)
              {
            dataWriter.WriteText
              (string.Format("{0}, {1}, {2}, {3}, {4}, {5}",
                             team.TeamIndex, team.ThemeName,
                             team.MoveSeconds, team.Kind,
                             team.WrappedMovement, team.ScoringSystem));
              }

              dataWriter.WriteEndList();
            }

            dataWriter.WriteEndList();

            dataWriter.WriteLine();
            dataWriter.WriteStartList("Player Information");
            dataWriter.WriteComment("player, x, y, team");

            foreach (var player in players)
            {
              dataWriter.WriteText(string.Format("{0}, {1}, {2}, {3}",
            player.Id, player.TileLeft, player.TileTop, player.Team.TeamName));
            }

            dataWriter.WriteEndList();

            dataWriter.WriteLine();
            dataWriter.WriteStartList("Moves");
            dataWriter.WriteComment("time in milliseconds, Move, player, x, y");
            dataWriter.WriteComment("time in milliseconds, Eat, predator, prey");
              }

              // Connect to input server
              if (Settings.Port > 0)
              {
            try
            {
              inputClient.Connect(IPAddress.Loopback, Settings.Port);
              inputClient.Client.BeginReceive(inputBuffer, 0, 2, SocketFlags.None,
            inputClient_DataReceived, null);
            }
            catch (Exception ex)
            {
              logger.Error("Failed to connect to input server", ex);
            }
              }

              inputTimer.Start();
        }
Esempio n. 3
0
        public ForagerWindow(ForagerSettings settings)
            : base(settings.ScreenWidth,
      settings.ScreenHeight, GraphicsMode.Default, "Forager",
      settings.Fullscreen ? GameWindowFlags.Fullscreen : 0)
        {
            logger.Info("Initializing Forager game window");
              this.Settings = settings;

              Keyboard.KeyDown += Keyboard_KeyDown;

              // Open data file
              if (!string.IsNullOrEmpty(Settings.DataFilePath))
              {
            logger.DebugFormat("Opening data file at {0}", Settings.DataFilePath);
            dataWriter = new YAMLOutputStream(Settings.DataFilePath);
              }

              // Validate number of players
              var playerTextureBasePath = Path.Combine(basePath, Path.Combine("etc", "player_images"));
              var playerImageCount = Directory.GetFiles(playerTextureBasePath, "*.png").Length;

              if (Settings.Players > playerImageCount)
              {
            logger.WarnFormat("Too few player images are present, only allowing {0} players", playerImageCount);
            Settings.Players = playerImageCount;
              }

              players = Enumerable.Range(0, Settings.Players).Select(i => new Player(i)).ToArray();
              playerShadows = players.Select(p => new PlayerShadow(p.Id)).ToArray();

              // Load player textures
              logger.DebugFormat("Loading player textures from {0}", playerTextureBasePath);

              playerTextureIds = Textures.LoadPlayers(playerTextureBasePath, Settings.Players).ToArray();
              playerGrayTextureIds = Textures.LoadPlayers(playerTextureBasePath, Settings.Players,
            (image) => Textures.MakeGrayScale(image)).ToArray();

              // Load food textures
              var foodTextureBasePath = Path.Combine(basePath, Path.Combine("etc", "food_images"));
              var foodImageCount = Directory.GetFiles(foodTextureBasePath, "*.png").Length;

              logger.DebugFormat("Loading food textures from {0}", foodTextureBasePath);
              foodTextureIds = Textures.Load(foodTextureBasePath, "food", foodImageCount, x => x).ToArray();

              plots = Enumerable.Range(0, Settings.Plots).Select(i => RectangleF.Empty).ToArray();
              plotNumberOffsets = Enumerable.Range(0, Settings.Plots).Select(i => PointF.Empty).ToArray();
              emptyPlotFood = Enumerable.Range(0, Settings.Plots).Select(i => new EmptyPlotFood()).ToArray();

              CalculateSizesAndLoadFonts();
              NormalizeProbabilities();

              AssignPlayerPositions();

              // Output settings
              if (dataWriter != null)
              {
            dataWriter.WriteStartDocument();
            dataWriter.WriteHashSingle("Version", ForagerSettings.FileVersion);

            dataWriter.WriteLine();
            dataWriter.WriteStartList("Settings");
            dataWriter.WriteHashSingle("Description", Settings.GameDescription);
            dataWriter.WriteHashSingle("Players", Settings.Players);

            dataWriter.WriteStartList("Player Plots");
            dataWriter.WriteComment("player, plot");

            foreach (var player in players)
            {
              dataWriter.WriteText(string.Format("{0}, {1}", player.Id, player.PlotNumber + 1));
            }

            // Player plots
            dataWriter.WriteEndList();

            dataWriter.WriteHashSingle("Game Duration", Settings.GameSeconds);
            dataWriter.WriteHashSingle("Travel Time", Settings.TravelTime);
            dataWriter.WriteHashSingle("Food Rate", Settings.FoodRate);
            dataWriter.WriteHashSingle("Plots", Settings.Plots);

            if (Settings.ProbabilityShiftTimes.Count > 0)
            {
              dataWriter.WriteStartList("Probability Shift Times");
              dataWriter.WriteComment("set, seconds from last shift");

              int setIndex = 1;

              foreach (var shiftTime in settings.ProbabilityShiftTimes)
              {
            dataWriter.WriteText(string.Format("{0}, {1}", setIndex, shiftTime));
            setIndex++;
              }

              // Probability Shift Times
              dataWriter.WriteEndList();
            }

            dataWriter.WriteStartList("Plot Probabilities");
            dataWriter.WriteComment("set, plot #, probability");

            int probabilitySet = 1;
            double probabilitySum = 0;

            foreach (var probabilities in Settings.PlotProbabilities)
            {
              for (int plotIndex = 0; plotIndex < probabilities.Count; plotIndex++)
              {
            var plotProbability = probabilities[plotIndex] - probabilitySum;
            probabilitySum += plotProbability;

            dataWriter.WriteText(string.Format("{0}, {1}, {2:N2}", probabilitySet,
                                               plotIndex + 1, plotProbability));
              }

              probabilitySet++;
              probabilitySum = 0;
            }

            // Plot probabilities
            dataWriter.WriteEndList();

            // Settings
            dataWriter.WriteEndList();

            dataWriter.WriteLine();
            dataWriter.WriteStartList("Actions");
            dataWriter.WriteComment("time in milliseconds, action, player, plot number");
              }

              inputTimer.Start();

              // Connect to input server
              if (Settings.Port > 0)
              {
            try
            {
              inputClient.Connect(IPAddress.Loopback, Settings.Port);
              inputClient.Client.BeginReceive(inputBuffer, 0, 2, SocketFlags.None,
            inputClient_DataReceived, null);
            }
            catch (Exception ex)
            {
              logger.Error("Failed to connect to input server", ex);
            }
              }

              gameSecondsLeft = Settings.GameSeconds;
              probabilitySecondsLeft = Settings.ProbabilityShiftTimes.FirstOrDefault();
              UpdateStatusText();

              roundTimer = new System.Timers.Timer(1000);
              roundTimer.Elapsed += roundTimer_Elapsed;
              roundTimer.Start();
        }
Esempio n. 4
0
        public PixelWindow(PixelSettings settings)
            : base(settings.ScreenWidth,
			settings.ScreenHeight, GraphicsMode.Default, "Pixel",
			settings.Fullscreen ? GameWindowFlags.Fullscreen : 0)
        {
            logger.Info("Initializing Pixel game window");
            this.Settings = settings;

            Keyboard.KeyDown += Keyboard_KeyDown;

            // Open data file
            if (!string.IsNullOrEmpty(Settings.DataFilePath))
            {
                logger.DebugFormat("Opening data file at {0}", Settings.DataFilePath);
                dataWriter = new YAMLOutputStream(Settings.DataFilePath);
            }

            // Calculate number of pixels
            var possiblePixels = settings.Players * PixelSettings.PlayerButtonCount;
            var maxPixels = Settings.MaxSize * Settings.MaxSize;

            if ((maxPixels > 0) && (possiblePixels > maxPixels))
            {
                tiles = Settings.MaxSize;
            }
            else
            {
                tiles = (int)Math.Floor(Math.Sqrt(possiblePixels));
            }

            pixelCount = tiles * tiles;

            // Calculate pixel and board sizes
            CalculateSizesAndLoadFonts();

            // Load textures
            var textureBasePath = Path.Combine(basePath, Path.Combine("etc", "player_images"));
            logger.DebugFormat("Loading textures from {0}", textureBasePath);

            playerTextureIds = Textures.LoadPlayers(textureBasePath, Settings.Players).ToArray();

            // Create pixels
            pixels.AddRange(Enumerable.Range(0, pixelCount)
                .Select(i => new PlayerPixel(i % tiles, i / tiles,
                    GetInitialPixelState()))
            );

            SetupPlayerSort();

            // Output settings and player pixels
            if (dataWriter != null)
            {
                dataWriter.WriteStartDocument();
                dataWriter.WriteHashSingle("Version", PixelSettings.FileVersion);

                dataWriter.WriteLine();
                dataWriter.WriteStartList("Settings");
            dataWriter.WriteHashSingle("Description", Settings.GameDescription);
                dataWriter.WriteHashSingle("Players", Settings.Players);
                dataWriter.WriteHashSingle("Size", string.Format("{0}x{0}", tiles));
                dataWriter.WriteHashSingle("Initial State", Settings.InitialState);
                dataWriter.WriteHashSingle("Player Sort", Settings.PlayerSort);
                dataWriter.WriteEndList();

                dataWriter.WriteLine();
                dataWriter.WriteStartList("Player Pixels");
                dataWriter.WriteComment("x, y, state, player");

                foreach (var pixel in pixels)
                {
                    if (pixel.State == PixelState.Fixed)
                    {
                        dataWriter.WriteText(string.Format("{0}, {1}, Fixed", pixel.TileLeft, pixel.TileTop));
                    }
                    else
                    {
                        dataWriter.WriteText(string.Format("{0}, {1}, {2}, {3}",
                            pixel.TileLeft, pixel.TileTop, pixel.State, pixel.PlayerId));
                    }
                }

                dataWriter.WriteEndList();

                dataWriter.WriteLine();
                dataWriter.WriteStartList("Moves");
                dataWriter.WriteComment("time in milliseconds, player, x, y, On/Off");
            }

            // Connect to input server
            if (Settings.Port > 0)
            {
                try
                {
                    inputClient.Connect(IPAddress.Loopback, Settings.Port);
                    inputClient.Client.BeginReceive(inputBuffer, 0, 2, SocketFlags.None,
                        inputClient_DataReceived, null);
                }
                catch (Exception ex)
                {
                    logger.Error("Failed to connect to input server", ex);
                }
            }

            inputTimer.Start();
        }