~ubuntu-branches/ubuntu/maverick/lordsawar/maverick

« back to all changes in this revision

Viewing changes to src/gui/driver.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2010-04-10 09:29:33 UTC
  • mfrom: (1.1.9 upstream) (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100410092933-23uq4dxig30kmtcw
Tags: 0.1.8-1
* New upstream release.
* Add misc:Depends for -data package.
* Bump Standards Version to 3.8.4. (No changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//  Copyright (C) 2007, 2008, Ole Laursen
2
 
//  Copyright (C) 2007, 2008, 2009 Ben Asselstine
 
2
//  Copyright (C) 2007, 2008, 2009, 2010 Ben Asselstine
3
3
//
4
4
//  This program is free software; you can redistribute it and/or modify
5
5
//  it under the terms of the GNU General Public License as published by
46
46
#include "ucompose.hpp"
47
47
#include "sound.h"
48
48
#include "timed-message-dialog.h"
49
 
#include "new-game-progress-window.h"
50
49
#include "game-preferences-dialog.h"
51
50
 
52
51
#include "game-client.h"
63
62
#include "city.h"
64
63
#include "FogMap.h"
65
64
#include "history.h"
 
65
#include "game.h"
 
66
#include "stacklist.h"
 
67
#include "smallmap.h"
 
68
#include "new-random-map-dialog.h"
66
69
 
67
70
Driver::Driver(std::string load_filename)
68
71
{
134
137
      g.map.forest = 3;
135
138
      g.map.hills = 5;
136
139
      g.map.mountains = 5;
137
 
      g.map.cities = 20;
 
140
      g.map.cities = 40;
138
141
      g.map.ruins = 25;
 
142
      g.map.temples = 3;
139
143
      g.map_path = "";
140
 
      g.play_with_quests = false;
 
144
      g.play_with_quests = GameParameters::NO_QUESTING;
141
145
      g.hidden_map = false;
142
146
      g.neutral_cities = GameParameters::STRONG;
143
147
      g.razing_cities = GameParameters::ALWAYS;
144
148
      g.diplomacy = false;
145
149
      g.random_turns = false;
146
 
      g.quick_start = false;
 
150
      g.quick_start = GameParameters::NO_QUICK_START;
147
151
      g.intense_combat = false;
148
152
      g.military_advisor = false;
149
153
      g.army_theme = "default";
280
284
  game_client->chat(message);
281
285
}
282
286
 
 
287
GameScenario *Driver::create_new_scenario(GameParameters &g, GameScenario::PlayMode m)
 
288
{
 
289
  bool update_uuid = false;
 
290
  if (g.map_path.empty()) 
 
291
    {
 
292
      // construct new random scenario if we're not going to load the game
 
293
      std::string path = 
 
294
        NewRandomMapDialog::create_and_dump_scenario("random.map", g, NULL);
 
295
      g.map_path = path;
 
296
    }
 
297
  else
 
298
    update_uuid = true;
 
299
 
 
300
  bool broken = false;
 
301
                                                 
 
302
  GameScenario* game_scenario = new GameScenario(g.map_path, broken);
 
303
 
 
304
  GameScenarioOptions::s_see_opponents_stacks = g.see_opponents_stacks;
 
305
  GameScenarioOptions::s_see_opponents_production = g.see_opponents_production;
 
306
  GameScenarioOptions::s_play_with_quests = g.play_with_quests;
 
307
  GameScenarioOptions::s_hidden_map = g.hidden_map;
 
308
  GameScenarioOptions::s_diplomacy = g.diplomacy;
 
309
  GameScenarioOptions::s_cusp_of_war = g.cusp_of_war;
 
310
  GameScenarioOptions::s_neutral_cities = g.neutral_cities;
 
311
  GameScenarioOptions::s_razing_cities = g.razing_cities;
 
312
  GameScenarioOptions::s_intense_combat = g.intense_combat;
 
313
  GameScenarioOptions::s_military_advisor = g.military_advisor;
 
314
  GameScenarioOptions::s_random_turns = g.random_turns;
 
315
 
 
316
  if (broken)
 
317
    return NULL;
 
318
 
 
319
  game_scenario->setName(g.name);
 
320
  game_scenario->setPlayMode(m);
 
321
 
 
322
  if (game_scenario->getRound() == 0)
 
323
    {
 
324
      if (update_uuid)
 
325
        game_scenario->setNewRandomId();
 
326
 
 
327
      Playerlist::getInstance()->syncPlayers(g.players);
 
328
 
 
329
      game_scenario->initialize(g);
 
330
    }
 
331
  return game_scenario;
 
332
}
 
333
 
283
334
void Driver::on_new_hosted_network_game_requested(GameParameters g, int port,
284
335
                                                  std::string nick)
285
336
{
286
337
    if (splash_window)
287
338
        splash_window->hide();
288
339
 
289
 
    NewGameProgressWindow pw(g, GameScenario::NETWORKED, 
290
 
                             Main::instance().record);
291
 
    pw.thread_worker();
292
 
    //Gtk::Main::instance()->run(pw);
293
 
    GameScenario *game_scenario = pw.getGameScenario();
 
340
    GameScenario *game_scenario = 
 
341
      create_new_scenario(g, GameScenario::NETWORKED);
294
342
 
295
343
    if (game_scenario == NULL)
296
344
      {
461
509
 
462
510
void Driver::on_new_game_requested(GameParameters g)
463
511
{
464
 
    NewGameProgressWindow pw(g, GameScenario::HOTSEAT, 
465
 
                             Main::instance().record);
466
 
    pw.thread_worker();
467
 
    //Gtk::Main::instance()->run(pw);
468
 
    GameScenario *game_scenario = pw.getGameScenario();
 
512
    GameScenario *game_scenario = create_new_scenario(g, GameScenario::HOTSEAT);
469
513
 
470
514
    if (game_scenario == NULL)
471
515
      {
589
633
 
590
634
}
591
635
 
592
 
std::string
593
 
Driver::create_and_dump_scenario(const std::string &file, const GameParameters &g)
594
 
{
595
 
    CreateScenario creator (g.map.width, g.map.height);
596
 
 
597
 
    // then fill the other players
598
 
    int c = 0;
599
 
    int army_id = Armysetlist::getInstance()->getArmyset(g.army_theme)->getId();
600
 
    Shieldsetlist *ssl = Shieldsetlist::getInstance();
601
 
    guint32 id = ssl->getShieldset(g.shield_theme)->getId();
602
 
    for (std::vector<GameParameters::Player>::const_iterator
603
 
             i = g.players.begin(), end = g.players.end();
604
 
         i != end; ++i, ++c) {
605
 
        
606
 
        if (i->type == GameParameters::Player::OFF)
607
 
        {
608
 
            fl_counter->getNextId();
609
 
            continue;
610
 
        }
611
 
        
612
 
        Player::Type type;
613
 
        if (i->type == GameParameters::Player::EASY)
614
 
            type = Player::AI_FAST;
615
 
        else if (i->type == GameParameters::Player::HARD)
616
 
            type = Player::AI_SMART;
617
 
        else
618
 
            type = Player::HUMAN;
619
 
 
620
 
        creator.addPlayer(i->name, army_id, ssl->getColor(id, c), type);
621
 
    }
622
 
 
623
 
        
624
 
    // the neutral player must come last so it has the highest id among players
625
 
    creator.addNeutral(_("Neutral"), army_id, 
626
 
                       ssl->getColor(id, MAX_PLAYERS), Player::AI_DUMMY);
627
 
 
628
 
    // now fill in some map information
629
 
    creator.setMapTiles(g.tile_theme);
630
 
    creator.setShieldset(g.shield_theme);
631
 
    creator.setCityset(g.city_theme);
632
 
    creator.setNoCities(g.map.cities);
633
 
    creator.setNoRuins(g.map.ruins);
634
 
    creator.setNoTemples(g.map.temples);
635
 
    creator.setNoSignposts(g.map.signposts);
636
 
 
637
 
    // terrain: the scenario generator also accepts input with a sum of
638
 
    // more than 100%, so the thing is rather easy here
639
 
    creator.setPercentages(g.map.grass, g.map.water, g.map.forest, g.map.swamp,
640
 
                           g.map.hills, g.map.mountains);
641
 
 
642
 
    int area = g.map.width * g.map.height;
643
 
    creator.setNoSignposts(int(area * (g.map.grass / 100.0) * 0.0030));
644
 
 
645
 
    // and tell it the turn mode
646
 
    if (g.process_armies == GameParameters::PROCESS_ARMIES_AT_PLAYERS_TURN)
647
 
        creator.setTurnmode(true);
648
 
    else
649
 
        creator.setTurnmode(false);
650
 
        
651
 
    // now create the map and dump the created map
652
 
    std::string path = File::getSavePath();
653
 
    path += file;
654
 
    
655
 
    if (NewGameProgressWindow::getInstance())
656
 
      {
657
 
        creator.progress.connect(sigc::mem_fun(NewGameProgressWindow::getInstance(), &NewGameProgressWindow::pulse));
658
 
      }
659
 
    creator.create(g);
660
 
    creator.dump(path);
661
 
    
662
 
    return path;
663
 
}
664
 
 
665
636
GameScenario *Driver::load_game(std::string file_path)
666
637
{
667
638
    bool broken = false;
675
646
        dialog.hide();
676
647
        return NULL;
677
648
      }
678
 
    if (Main::instance().record != "")
679
 
      game_scenario->startRecordingEventsToFile(Main::instance().record);
680
649
    return game_scenario;
681
650
}
682
651
 
685
654
  std::string filename;
686
655
  std::string temp_filename = File::getSavePath() + "pbmtmp.sav";
687
656
      
688
 
  NewGameProgressWindow pw(g, GameScenario::PLAY_BY_MAIL,
689
 
                           Main::instance().record);
690
 
  pw.thread_worker();
691
 
  //Gtk::Main::instance()->run(pw);
692
 
  GameScenario *game_scenario = pw.getGameScenario();
 
657
  GameScenario *game_scenario = 
 
658
    create_new_scenario(g, GameScenario::PLAY_BY_MAIL);
693
659
  if (game_scenario == NULL)
694
660
    {
695
661
      TimedMessageDialog dialog(*splash_window->get_window(),
742
708
 
743
709
void Driver::stressTestNextRound()
744
710
{
 
711
  //static time_t prev_round_start = time(NULL);
745
712
  static int count = 1;
 
713
  //if (count == 1)
 
714
    //{
 
715
      //FILE * fileptr = fopen("/tmp/crapola.csv", "w");
 
716
      //fclose(fileptr);
 
717
    //}
746
718
  count++;
 
719
  //time_t now = time(NULL);
747
720
  printf ("starting round %d!\n", count);
 
721
  /*
 
722
  FILE * fileptr = fopen("/tmp/crapola.csv", "a");
 
723
  int total_fights = Playerlist::getInstance()->countFightsThisTurn();
 
724
  int total_moves = Playerlist::getInstance()->countMovesThisTurn();
 
725
  fprintf(fileptr, "%d, %d, %d, %d, %d, %d, %d, %d", count, 
 
726
          now - prev_round_start,
 
727
          Stacklist::getNoOfStacks(), 
 
728
          Stacklist::getNoOfArmies(), 
 
729
          total_fights,
 
730
          total_moves,
 
731
          Citylist::getInstance()->countCities(Playerlist::getInstance()->getNeutral()), 
 
732
          Playerlist::getInstance()->countPlayersAlive());
 
733
  if (Playerlist::getInstance()->countPlayersAlive() == 2)
 
734
    {
 
735
      Playerlist *pl = Playerlist::getInstance();
 
736
      for (Playerlist::iterator it = pl->begin(); it != pl->end(); it++)
 
737
        {
 
738
          if ((*it)->isDead() == true)
 
739
            continue;
 
740
          if ((*it) == pl->getNeutral())
 
741
            continue;
 
742
          fprintf(fileptr,", %d (%d)", Citylist::getInstance()->countCities(*it), (*it)->getHeroes().size() + (*it)->countAllies());
 
743
        }
 
744
    }
 
745
  fprintf(fileptr,"\n");
 
746
  fclose(fileptr);
 
747
      
 
748
  prev_round_start = now;
 
749
  */
748
750
  sleep (1);
749
751
}
750
752
 
756
758
  GameParameters::Player p;
757
759
  for (unsigned int i = 0; i < MAX_PLAYERS; i++)
758
760
    {
759
 
      p.type = GameParameters::Player::EASY;
 
761
      p.type = GameParameters::Player::HARD;
760
762
      p.id = i;
761
763
      switch (p.id)
762
764
        {
779
781
  g.map.forest = 3;
780
782
  g.map.hills = 5;
781
783
  g.map.mountains = 5;
782
 
  g.map.cities = 80;
 
784
  g.map.cities = 110;
783
785
  g.map.ruins = 15;
784
 
  g.map.temples = 1;
 
786
  g.map.temples = 3;
 
787
  g.map.signposts = 10;
785
788
  g.map_path = "";
786
 
  g.play_with_quests = false;
 
789
  g.play_with_quests = GameParameters::ONE_QUEST_PER_PLAYER;
787
790
  g.hidden_map = false;
788
791
  g.neutral_cities = GameParameters::STRONG;
789
792
  g.razing_cities = GameParameters::ALWAYS;
790
793
  g.diplomacy = false;
791
794
  g.random_turns = false;
792
 
  g.quick_start = false;
 
795
  g.quick_start = GameParameters::NO_QUICK_START;
793
796
  g.intense_combat = false;
794
797
  g.military_advisor = false;
795
798
  g.army_theme = "default";
803
806
  g.see_opponents_stacks = true;
804
807
  g.see_opponents_production = true;
805
808
      
806
 
  std::string path = create_and_dump_scenario("random.map", g);
 
809
  bool broken = false;
 
810
  std::string path;
 
811
  path = NewRandomMapDialog::create_and_dump_scenario("random.map", g, NULL);
807
812
  g.map_path = path;
808
813
 
809
 
  bool broken = false;
810
814
  GameScenario* game_scenario = new GameScenario(g.map_path, broken);
811
815
 
812
816
  if (broken)
813
817
    return;
814
818
 
815
 
  if (Main::instance().record != "")
816
 
    game_scenario->startRecordingEventsToFile(Main::instance().record);
817
819
  NextTurnHotseat *nextTurn;
818
820
  nextTurn = new NextTurnHotseat(game_scenario->getTurnmode(),
819
821
                                 game_scenario->s_random_turns);
820
822
    
821
823
  nextTurn->snextRound.connect
822
824
    (sigc::mem_fun(this, &Driver::stressTestNextRound));
823
 
  nextTurn->snextRound.connect
824
 
    (sigc::mem_fun(game_scenario, &GameScenario::nextRound));
825
825
  if (game_scenario->getRound() == 0)
826
826
    {
827
827
      Playerlist::getInstance()->syncPlayers(g.players);
828
828
      game_scenario->initialize(g);
829
829
    }
830
830
 
 
831
  //this is a bit unfortunate... we have to instantiate images just to get stack positions into the stack tiles.  is there a better way?
 
832
  /*/
 
833
  GameMap::getInstance()->getTileset()->instantiateImages();
 
834
  GameMap::getInstance()->getShieldset()->instantiateImages();
 
835
  GameMap::getInstance()->getCityset()->instantiateImages();
 
836
  guint32 armyset = Playerlist::getInstance()->getNeutral()->getArmyset();
 
837
  Armysetlist::getInstance()->getArmyset(armyset)->instantiateImages();
 
838
  */
 
839
  Game game(game_scenario, nextTurn);
 
840
  game.get_smallmap().set_slide_speed(0);
 
841
  Configuration::s_displaySpeedDelay = 0;
 
842
  time_t start = time(NULL);
831
843
  nextTurn->start();
832
 
  delete nextTurn;
833
 
  delete game_scenario;
 
844
  //next turn and game_Scenario get deleted inside game.
 
845
  size_t mins = (time(NULL) - start) / 60;
 
846
  printf("duration: %d mins, turns: %d ", mins, game_scenario->getRound());
 
847
  fflush(stdout);
 
848
  printf("winner type: %s\n", Player::playerTypeToString(Player::Type(Playerlist::getInstance()->getFirstLiving()->getType())).c_str());
834
849
 
835
850
}
836
851