Esempio n. 1
0
    public void MakeMap(int width, int height, int players, int unitCap)
    {
      UnitCap = unitCap;
      if (Map != null)
      {
        if (NaturePlayer != null)
        {
          NaturePlayer.Dispose();
        }
        Map.Dispose();
        Effects.Clear();
      }
      NaturePlayer = new Player(-1, this, "Nature");

      Map = new MapBoard(this, width, height);
      Map.Initialize();
      Map.Generate(random, players);

      NaturePlayer.Initialize();
    }
Esempio n. 2
0
    /// <summary>
    /// Method that deserializes the data from the line starting at given position in given context depending on the MessageType
    /// </summary>
    /// <param id="mt">MessageType defining the extend of deserialization which is to be performed</param>
    /// <param id="line">String array containing the serialized data</param>
    /// <param id="position">Current position in the array</param>
    /// <param id="context">Context of the serialized data, game where this INetworkSerializable object is in</param>
    public void Deserialize(MessageType mt, string[] line, ref int position, WildmenGame context)
    {
      int n;
      switch (mt)
      {
        case MessageType.GameTransfer:
          Map = new MapBoard(context, int.Parse(line[position + 0]), int.Parse(line[position + 1]));
          Map.Initialize();
          Map.Deserialize(mt, line, ref position, context);
          Map.LoadContent();

          int evCount = int.Parse(line[position++]);
          for (int i = 0; i < evCount; i++)
          {
            Effects.Add(GameEffect.Create(line, ref position, context));
          }


          UnitCap = int.Parse(line[position++]);
          n = int.Parse(line[position++]);
          for (int i = 0; i < n; i++)
          {
            var p = new Player(i, context, null);
            Players.Add(p);
            p.Initialize();
          }
          for (int i = 0; i < n; i++)
          {
            Players[i].Deserialize(mt, line, ref position, context);
          }
          NaturePlayer = new Player(-1, context, null);
          NaturePlayer.Initialize();
          NaturePlayer.Deserialize(mt, line, ref position, context);
          List<Player> tempPlayers = new List<Player>(Players);
          tempPlayers.Add(NaturePlayer);
          foreach (var player in tempPlayers)
          {
            foreach (var unit in player.Units)
            {
              unit.ResolveReferences(GetGameObjectById);
            }
            foreach (var bldg in player.Buildings)
            {
              bldg.ResolveReferences(GetGameObjectById);
            }
          }
          NextUID = int.Parse(line[position++]);
          break;
        default:
          throw new Exception("WildmenGame deserialization error");
      }
    }
Esempio n. 3
0
    // GAME-RELATED METHODS

    /// <summary>
    /// Creates a new instance of a game, sends it to all clients and start the game
    /// </summary>
    public void StartGame()
    {
      Accepting = false;
      SendToAll(Network.MakeServerMessage(MessageType.GameState, GameState.Starting));
      CurrentGameState = GameState.Starting;

      clientMessages = new Queue<string[]>();

      lock (ClientConnections)
      {

        Game = new WildmenGame();
        Game.IsServer = true;
        Game.MakeMap(MapWidth, MapHeight, ClientConnections.Count(), UnitCap);

        if (!Game.Map.Valid)
        {
          SendMessage("Unable to create map with given parameters.");
          SendToAll(Network.MakeServerMessage(MessageType.GameState, GameState.Lobby));
          CurrentGameState = GameState.Lobby;
          Accepting = true;
          return;
        }

        Game.Map.LoadContent();
        Game.SendData = SendToAll;
        Game.ReceiveData = GetClientMessage;

        for (int i = 0; i < ClientConnections.Count(); i++)
        {
          ClientConnection c = ClientConnections[i];
          c.IngamePlayerId = i;
          Player p = new Player(i, Game, c.Name);
          p.Initialize();
          Game.PlacePlayer(i, p);
        }

        string savegame = Game.SaveGame().TrimStart(':');

        for (int i = 0; i < ClientConnections.Count(); i++)
        {
          ClientConnection c = ClientConnections[i];
          Player p = Game.Players[i];

          c.StartSend(Network.MakeServerMessage(MessageType.GameTransfer, savegame));
          c.StartSend(Network.MakeServerMessage(MessageType.PlayerAssign, i));
        }

        SendToAll(Network.MakeServerMessage(MessageType.GameState, GameState.Started));
        CurrentGameState = GameState.Started;
      }
      SendMessage("Game started.");
    }