コード例 #1
0
void LocalTeamsSelectionBox::ValidTeamsSelection()
{
  uint nb_teams=0;
  for (uint i=0; i < teams_selections.size(); i++) {
    if (teams_selections.at(i)->GetTeam() != NULL) {
      nb_teams++;
    }
  }

  if (nb_teams >= 2) {
    std::list<uint> selection;

    for (uint i=0; i < teams_selections.size(); i++) {

      if (teams_selections.at(i)->GetTeam() != NULL) {

        int index = -1;
        teams_selections.at(i)->ValidOptions();
        GetTeamsList().FindById(teams_selections.at(i)->GetTeam()->GetId(), index);
        if (index > -1)
        {
          selection.push_back(uint(index));

        }
      }
    }

    GetTeamsList().ChangeSelection(selection);
  }
}
コード例 #2
0
ファイル: replay.cpp プロジェクト: fluxer/warmux
void Replay::StopPlaying()
{
  ASSERT(!is_recorder);

  if (replay_state != PLAYING)
    return;

  // Only replay seems to use this, so we can quit it now
  replay_state = NOTHING;

  // Restore game mode
  GameMode * game_mode = GameMode::GetInstance();
  game_mode->allow_character_selection = (GameMode::manual_change_character_t)
    mode_info.allow_character_selection;
  game_mode->duration_turn = mode_info.turn_duration;
  game_mode->damage_per_turn_during_death_mode = mode_info.damage_per_turn_during_death_mode;
  game_mode->duration_before_death_mode = mode_info.duration_before_death_mode;
  game_mode->character.init_energy = mode_info.init_energy;
  game_mode->character.max_energy = mode_info.max_energy;
  game_mode->gravity = mode_info.gravity;

  // Restore also volume
  JukeBox::GetInstance()->SetMusicVolume(Config::GetInstance()->GetVolumeMusic());

  // Restore playing list
  GetTeamsList().SetPlayingList(backup_list);
}
コード例 #3
0
ファイル: replay_info.cpp プロジェクト: Arnaud474/Warmux
ReplayInfo *ReplayInfo::ReplayInfoFromCurrent(uint32_t duration, const char* comment)
{
  ReplayInfo *info    = new ReplayInfo(time(NULL), duration);
  
  info->version = Constants::WARMUX_VERSION; // Copy ?
  info->comment = (comment) ? comment : _("No comment.");
  info->map_id  = ActiveMap()->GetRawName();

  //Teams
  const std::vector<Team*>& plist = GetTeamsList().playing_list;
  for (uint i=0; i<plist.size(); i++) {
    ConfigTeam team_cfg = { plist[i]->GetId(), plist[i]->GetPlayerName(),
                            plist[i]->GetNbCharacters(), plist[i]->GetAIName() };
    info->teams.push_back(team_cfg);
  }

  // Game mode
  const GameMode * game_mode = GameMode::GetInstance();
  info->mode_info.allow_character_selection = game_mode->allow_character_selection;
  info->mode_info.turn_duration = game_mode->duration_turn;
  info->mode_info.duration_before_death_mode = game_mode->duration_before_death_mode;
  info->mode_info.damage_per_turn_during_death_mode = game_mode->damage_per_turn_during_death_mode;
  info->mode_info.init_energy = game_mode->character.init_energy;
  info->mode_info.max_energy = game_mode->character.max_energy;
  info->mode_info.gravity = (int)game_mode->gravity;

  // Everything ready
  info->valid   = true;
  return info;
}
コード例 #4
0
LocalTeamsSelectionBox::LocalTeamsSelectionBox(const Point2i &size, bool border) :
  TeamsSelectionBox(size, false, border)
{
  GetTeamsList().InitList(Config::GetInstance()->AccessTeamList());

  TeamsList::iterator it  = GetTeamsList().playing_list.begin(),
    end = GetTeamsList().playing_list.end();

  uint j=0;
  for (; it != end && j<teams_selections.size(); ++it, j++) {
    teams_selections.at(j)->SetTeam((**it), true);
  }

  // we need at least 2 teams
  if (j < 2) {
    SetNbTeams(2);
    local_teams_nb->SetValue(2);
    teams_selections.at(1)->SetAIName(DEFAULT_AI_NAME);
  } else {
    SetNbTeams(j);
    local_teams_nb->SetValue(j);
  }
}
コード例 #5
0
void LocalTeamsSelectionBox::NextTeam(int i)
{
  if (!teams_selections.at(i)->GetTeam())
    return;

  bool to_continue;
  Team* tmp;
  int previous_index = -1, index;

  GetTeamsList().FindById(teams_selections.at(i)->GetTeam()->GetId(), previous_index);

  index = previous_index+1;

  do {
    to_continue = false;

    // select the first team if we are outside list
    if (index >= int(GetTeamsList().full_list.size()))
      index = 0;

    // Get the team at current index
    tmp = GetTeamsList().FindByIndex(index);

    // Check if that team is already selected
    for (int j = 0; j < local_teams_nb->GetValue(); j++) {
      if (j!= i && tmp == teams_selections.at(j)->GetTeam()) {
        index++;
        to_continue = true;
        break;
      }
    }

    // We have found a team which is not selected
    if (tmp != NULL && !to_continue)
      teams_selections.at(i)->SetTeam(*tmp);
  } while (index != previous_index && to_continue);
}
コード例 #6
0
void LocalTeamsSelectionBox::PrevTeam(int i)
{
  if (!teams_selections.at(i)->GetTeam())
    return;

  bool stop;
  int  previous_index = -1, index;

  GetTeamsList().FindById(teams_selections.at(i)->GetTeam()->GetId(), previous_index);

  index = previous_index-1;

  do {
    stop = true;

    // select the last team if we are outside list
    if (index < 0)
      index = int(GetTeamsList().full_list.size())-1;

    // Get the team at current index
    Team *tmp = GetTeamsList().FindByIndex(index);

    // Check if that team is already selected
    for (int j = 0; j < local_teams_nb->GetValue(); j++) {
      if (j!= i && tmp == teams_selections.at(j)->GetTeam()) {
        index--;
        stop = false;
        break;
      }
    }

    // We have found a team which is not selected
    if (tmp && stop)
      teams_selections.at(i)->SetTeam(*tmp);
  } while (index != previous_index && !stop);
}
コード例 #7
0
void LocalTeamsSelectionBox::SetNbTeams(uint nb_teams)
{
  // we hide the useless teams selector
  for (uint i=nb_teams; i<teams_selections.size(); i++) {
    teams_selections[i]->ClearTeam();
  }

  for (uint i=0; i<nb_teams;i++) {
    if (!teams_selections.at(i)->GetTeam()) {
      // we should find an available team
      teams_selections.at(i)->SetTeam(*(GetTeamsList().FindByIndex(i)));
      NextTeam(i);
    }
  }

  if (list_box)
    list_box->SetNbTeams(nb_teams);
}
コード例 #8
0
ファイル: network.cpp プロジェクト: fluxer/warmux
void Network::CheckOneHostTeams(Player& player, DistantComputer* new_host,
                                const std::vector<Team*>& local_list,
                                const std::vector<uint>& common_list)
{
  const std::list<ConfigTeam>& configs         = player.GetTeams();
  std::list<ConfigTeam>::const_iterator config = configs.begin();

  // Check current player list of teams
  while (config != configs.end()) {

    // Search if the common list holds current team
    bool found = false;
    for (uint i=0; i<common_list.size(); i++) {
      if (config->id == local_list[common_list[i]]->GetId()) {
        found = true;
        break;
      }
    }

    // If not found, remove corresponding team
    if (!found) {
      std::string id = config->id;
      MSG_DEBUG("action_handler.team", "Deleting team %s!\n", id.c_str());

      Action *a = new Action(Action::ACTION_GAME_DEL_TEAM);
      a->Push(int(player.GetId()));
      a->Push(id);

      SendActionToAllExceptOne(*a, new_host, false);

      // We need to immediately remove the team, but this invalidates
      // the current iterator, so we increment it before the config is removed
      ++config;
      player.RemoveTeam(id);
      GetTeamsList().DelTeam(id);
      if (network_menu)
        network_menu->DelTeamCallback(id);
    } else
      ++config;
  }
}
コード例 #9
0
ファイル: game_classic.cpp プロジェクト: fluxer/warmux
// Beginning of a new turn
void GameClassic::__SetState_PLAYING()
{
  MSG_DEBUG("game.statechange", "Playing");

  // initialize counter
  duration = GameMode::GetInstance()->duration_turn;
  Interface::GetInstance()->UpdateTimer(duration, false, true);
  Interface::GetInstance()->EnableDisplayTimer(true);
  last_clock_update = GameTime::GetInstance()->Read();

  Wind::GetRef().UpdateStrength();

  SetCharacterChosen(false);

  // Prepare each character for a new turn
  FOR_ALL_LIVING_CHARACTERS(team,character)
    character->PrepareTurn();

  // Select the next team
  ASSERT (!IsGameFinished());
  GetTeamsList().NextTeam();

  give_objbox = true; //hack: make it so that there is no more than one objbox per turn
}
コード例 #10
0
bool NetworkConnectionMenu::signal_ok()
{
  bool r = false;
  std::list<GameServerInfo> lst;

  // Hack: force loading of teams before creating threads.
  GetTeamsList();

  // Which tab is displayed ?
  std::string id = tabs->GetCurrentTabId();

  if (id == TAB_SERVER_ID) {
    // Hosting your own server
    r = HostingServer(srv_port_number->GetText(),
                      srv_game_name->GetText(),
                      srv_game_pwd->GetPassword(),
                      srv_internet_server->GetValue());
    if (!r)
      goto out;

    // Remember the parameters
    Config::GetInstance()->SetNetworkServerPort(srv_port_number->GetText());
    Config::GetInstance()->SetNetworkServerGameName(srv_game_name->GetText());
    Config::GetInstance()->SetNetworkServerPublic(srv_internet_server->GetValue());

  } else if (id == TAB_CLIENT_ID) { // Direct connexion to a server

    if (cl_net_games_lst->GetSelectedItem() != -1) {
      // Connect to an internet game!
      r = ConnectToClient(cl_net_games_lst->GetAddress(),
                          cl_net_games_lst->GetPort(),
                          cl_net_server_pwd->GetPassword());
      if (!r)
        goto out;

    } else if (!cl_server_address->GetText().empty()) {
      r = ConnectToClient(cl_server_address->GetText(),
                          cl_port_number->GetText(),
                          cl_server_pwd->GetPassword());
      if (!r)
        goto out;

      // Remember the parameters
      Config::GetInstance()->SetNetworkClientHost(cl_server_address->GetText());
      Config::GetInstance()->SetNetworkClientPort(cl_port_number->GetText());
    } else
      goto out;
  } else if (id == TAB_MANUAL_ID) {
    if (!cl_server_address->GetText().empty()) {
      r = ConnectToClient(cl_server_address->GetText(),
                          cl_port_number->GetText(),
                          cl_server_pwd->GetPassword());
      if (!r)
        goto out;

      // Remember the parameters
      Config::GetInstance()->SetNetworkClientHost(cl_server_address->GetText());
      Config::GetInstance()->SetNetworkClientPort(cl_port_number->GetText());
    }
  }

  if (Network::IsConnected()) {
    play_ok_sound();

    // run the network menu ! :-)
    NetworkMenu nm;
    Network::GetInstance()->network_menu = &nm;
    nm.Run();
    Network::GetInstance()->network_menu = NULL;
    IndexServer::GetInstance()->Disconnect();

    Network::Disconnect();

    // Don't go back to main menu after playing
    Menu::RedrawMenu();
    ThreadRefreshList();

    return false;
  }

 out:
  Network::Disconnect();
  return r;
}
コード例 #11
0
ファイル: replay.cpp プロジェクト: fluxer/warmux
bool Replay::LoadReplay(const std::string& name)
{
#define TEMP_SIZE 256
  char           temp[TEMP_SIZE];
  bool           status     = false;
  std::streampos pos;
  ReplayInfo     *info      = NULL;
  GameMode       *game_mode = GameMode::GetInstance();
  int            map_id, val;

  ASSERT(!is_recorder);
  old_time = 0;

  FILE *in = fopen(name.c_str(), "rb");
  if (!in) {
    Error(Format(_("Couldn't open %s\n"), name.c_str()));
    goto done;
  }

  info = ReplayInfo::ReplayInfoFromFile(in);
  if (!info->IsValid())
    goto done;

  if (info->GetVersion() != Constants::WARMUX_VERSION) {
    Error(Format(_("Bad version: %s != %s"),
                 info->GetVersion().c_str(),
                 Constants::WARMUX_VERSION.c_str()));
    goto done;
  }
  goto ok;

err:
  Error(Format(_("Warning, malformed replay with data of size %u"), bufsize));

done:
  fclose(in);
  if (info) delete info;
  return status;

ok:
  // duration
  old_time = info->GetMillisecondsDuration();

  // map ID
  map_id = MapsList::GetInstance()->FindMapById(info->GetMapId());
  if (map_id == -1) {
    Error(Format(_("Couldn't find map %s"), temp));
    return false;
  }
  MapsList::GetInstance()->SelectMapByIndex(map_id);

  // Backup playing list
  TeamsList& teams_list = GetTeamsList();
  backup_list = teams_list.playing_list;
  teams_list.playing_list.clear();

  // Teams
  for (uint i = 0; i<info->GetTeams().size(); i++) {
    ConfigTeam team_cfg;
    teams_list.AddTeam(info->GetTeams()[i], true);
  }

  // Game mode
  memcpy(&mode_info, info->GetGameModeInfo(), sizeof(GameModeInfo));

  // Set GameMode
  val = mode_info.allow_character_selection;
  mode_info.allow_character_selection = game_mode->allow_character_selection;
  game_mode->allow_character_selection = (GameMode::manual_change_character_t)val;
#define SWAP(a, b) val = a; a = b; b = val
  SWAP(mode_info.turn_duration, game_mode->duration_turn);
  SWAP(mode_info.duration_before_death_mode, game_mode->duration_before_death_mode);
  SWAP(mode_info.damage_per_turn_during_death_mode, game_mode->damage_per_turn_during_death_mode);
  SWAP(mode_info.init_energy, game_mode->character.init_energy);
  SWAP(mode_info.max_energy, game_mode->character.max_energy);
  SWAP(mode_info.gravity, game_mode->gravity);

  MSG_DEBUG("replay", "Game mode: turn=%us move_player=%u max_energy=%u init_energy=%u\n",
            game_mode->duration_turn, game_mode->duration_move_player,
            game_mode->character.max_energy, game_mode->character.init_energy);

  // All of the above could be avoided through a GameMode::Load
  config_loaded = true;

  seed = Read32(in);

  // Get remaining data
  pos = ftell(in);
  fseek(in, 0, SEEK_END);
  uint size = ftell(in)-pos;
  fseek(in, pos, SEEK_SET);
  MSG_DEBUG("replay", "Actions found at %u on %uB, seed=%08X\n", (uint)pos, size, seed);

  // Explicit buffer change to avoid garbage
  if (buf)
    free(buf);
  buf = (uint8_t*)malloc(size);
  ptr = buf;
  bufsize = size;

  if (fread(buf, size, 1, in)!=1 || ferror(in))
    goto err;

  size = SDLNet_Read16(ptr); ptr += 2;
  std::string mode_name((char*)ptr, size); ptr += size;

  size = SDLNet_Read16(ptr); ptr += 2;
  std::string mode((char*)ptr, size); ptr += size;

  size = SDLNet_Read16(ptr); ptr += 2;
  std::string mode_objects((char*)ptr, size); ptr += size;

  game_mode->LoadFromString(mode_name, mode, mode_objects);
  status = true;

  goto done;
}
コード例 #12
0
ファイル: benchmark_menu.cpp プロジェクト: fluxer/warmux
bool BenchmarkMenu::Launch(BenchItem *b)
{
  if (!b)
    return false;

  bool        ok    = false;
  float       score = 0.0f;
  const char *fmt   = "";

  switch (b->type) {
  case BENCH_MENU:
    {
      Stopwatch   clock;
      delete (new OptionMenu());
      score = 1000.0f / clock.GetValue();
      fmt   = "%.3f";
      ok    = true;
      break;
    }
  case BENCH_GRAPHICS:
    {
      graph->UnsetResults();

      // Backup and set playing teams
      std::vector<Team*> list_bak = GetTeamsList().GetPlayingList();
      std::list<uint> sel; sel.push_back(1); sel.push_back(2);
      GetTeamsList().ChangeSelection(sel);

      // Backup and set team configuration - make it quick ;)
      std::vector<Team*>& list = GetTeamsList().GetPlayingList();
      for (uint i=0; i<list.size(); i++) {
        printf("Setting %s\n", list[i]->GetName().c_str());
        list[i]->SetPlayerName("CPU");
        list[i]->SetNbCharacters(10);
        list[i]->SetAIName(STRONG_AI_NAME);
        list[i]->SetGroup(i);
      }

      // Backup and set game mode
      Config *cfg = Config::GetInstance();
      std::string game_mode = cfg->GetGameMode();
      cfg->SetGameMode("benchmark");
      GameMode::GetInstance()->Load();

      // Backup and set some config options
      uint wind_particles = cfg->GetWindParticlesPercentage(); cfg->SetWindParticlesPercentage(100);
      bool display_energy = cfg->GetDisplayEnergyCharacter(); cfg->SetDisplayEnergyCharacter(true);
      bool display_multisky = cfg->GetDisplayMultiLayerSky(); cfg->SetDisplayMultiLayerSky(false);

      // Mute all sounds
      JukeBox *jbox = JukeBox::GetInstance();
      bool music = cfg->GetSoundMusic(); jbox->ActiveMusic(false);
      bool sfx = cfg->GetSoundEffects(); cfg->SetSoundEffects(false);

      // Backup and set default map - should I save the config?
      std::string map_name = cfg->GetMapName();
      MapsList *maps = MapsList::GetInstance();
      int map_id_bak = maps->GetActiveMapIndex(); maps->SelectMapByName("banquise");
      if (!maps->lst[maps->GetActiveMapIndex()]->LoadBasicInfo()) {
        fmt = "Error!";
        break;
      }

      // Set max FPS
      Video *video = AppWarmux::GetInstance()->video;
      int fps = video->GetMaxFps(); video->SetMaxFps(60);

      // Set seeds - we'll set random ones afterwards
      RandomLocal().SetSeed(0xABADCAFE);
      RandomSync().SetSeed(0xABADCAFE);

      // All set, run the game!
      float num  = Game::UpdateGameRules()->Start(true);
      if (num) {
        GraphCanvas::Result res;
        res.list = Game::GetInstance()->GetBenchResults();
        GraphCanvas::FindMax(res);

        float  time = res.xmax - res.list[0].first;
        score = (num * video->window.GetWidth()*video->window.GetHeight())
              / (1000.0f * time);
        fmt = "%.0f";

        res.item = NULL;
        res.color = primary_red_color;
        graph->AddResult(res);
      } else {
        fmt = "Aborted";
      }
      graph->NeedRedrawing();

      // Restore all!
      video->SetMaxFps(fps);
      jbox->ActiveMusic(music);
      cfg->SetSoundEffects(sfx);
      maps->SelectMapByIndex(map_id_bak);
      cfg->SetMapName(map_name);
      cfg->SetDisplayMultiLayerSky(display_multisky);
      cfg->SetDisplayEnergyCharacter(display_energy);
      cfg->SetWindParticlesPercentage(wind_particles);
      cfg->SetGameMode(game_mode);
      GetTeamsList().SetPlayingList(list_bak);

      ok = num == 0;
      break;
    }
  default: break;
  }

  b->SetScore(fmt, score);
  return ok;
}
コード例 #13
0
TeamsSelectionBox::TeamsSelectionBox(const Point2i &_size, bool network, bool w_border) :
  HBox(_size.y, w_border, false)
{
  if (!w_border)
    SetNoBorder();
  SetMargin(0);

  // How many teams ?
  VBox *tmp = new VBox(120, false, false, true);
  if (network) {
    local_teams_nb =
      new SpinButtonWithPicture(_("Local teams:"), "menu/team_number",
                                Point2i(100, 130), 0, 1, 0, MAX_NB_TEAMS);
  } else {
    local_teams_nb =
      new SpinButtonWithPicture(_("Number of teams:"), "menu/team_number",
                                Point2i(100, 130), 2, 1, 2, MAX_NB_TEAMS);
  }
  tmp->AddWidget(local_teams_nb);
  //tmp->AddWidget(new NullWidget(Point2i(120, 120)));
  AddWidget(tmp);

  uint box_w = _size.x - local_teams_nb->GetSizeX() - 10;
  Point2i grid_size = Point2i(box_w, _size.y);
  Point2i grid_dim = grid_size / Point2i(300 + 10, 130 + 10);
  Point2i box_size;
  bool use_list;
  if (grid_dim.x*grid_dim.y < (int)MAX_NB_TEAMS) {
    use_list = true;
    box_size.SetValues(box_w - 40, 120);
  } else {
    use_list = false;
    box_size.SetValues((grid_size / grid_dim) - 10);
  }

  for (uint i=0; i < MAX_NB_TEAMS; i++) {
    std::string player_name = _("Player") ;
    char num_player[4];
    sprintf(num_player, " %d", i+1);
    player_name += num_player;
    teams_selections.push_back(new TeamBox(player_name, box_size));
  }

  // If the intended gridbox would be too big for the intended size,
  // instead create a listbox
  if (use_list) {
    // Warning: this box takes the ownership of the widgets in teams_selections:
    // while any other Box will delete the ones it contains, TeamScrollBox
    // doesn't really contain them as widgets. They therefore aren't released
    // through this mechanism, but with a manual one. This manual mechanism
    // requires we have a *real* copy of the vector for when it is destroyed.
    list_box = new TeamScrollBox(teams_selections, Point2i(box_w-20, _size.y-10));
    list_box->SetNbTeams(0);

    AddWidget(list_box);
  } else {
    list_box = NULL;
    Box * teams_grid_box = new GridBox(grid_dim.y, grid_dim.x, 10, false);
    teams_grid_box->SetNoBorder();

    for (uint i=0; i<MAX_NB_TEAMS; i++)
      teams_grid_box->AddWidget(teams_selections[i]);

    AddWidget(teams_grid_box);
  }

  // Load Teams' list
  GetTeamsList().full_list.sort(compareTeams);
}