Client.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "Client.h"
  2. #include "../lib/Connection.h"
  3. #include "../StartInfo.h"
  4. #include "../map.h"
  5. #include "../CGameState.h"
  6. #include "../CGameInfo.h"
  7. #include "../mapHandler.h"
  8. #include "../CCallback.h"
  9. #include "../CPlayerInterface.h"
  10. #include "../CConsoleHandler.h"
  11. #include "../lib/NetPacks.h"
  12. #include <boost/bind.hpp>
  13. #include <boost/thread.hpp>
  14. CClient::CClient(void)
  15. {
  16. }
  17. CClient::CClient(CConnection *con, StartInfo *si)
  18. :serv(con)
  19. {
  20. timeHandler tmh;
  21. CGI->state = new CGameState();
  22. THC std::cout<<"\tGamestate: "<<tmh.getDif()<<std::endl;
  23. CConnection &c(*con);
  24. ////////////////////////////////////////////////////
  25. ui8 pom8;
  26. c << ui8(2) << ui8(1); //new game; one client
  27. c << *si;
  28. c >> pom8;
  29. if(pom8) throw "Server cannot open the map!";
  30. c << ui8(si->playerInfos.size());
  31. for(int i=0;i<si->playerInfos.size();i++)
  32. c << ui8(si->playerInfos[i].color);
  33. ui32 seed, sum;
  34. std::string mapname;
  35. c >> mapname >> sum >> seed;
  36. THC std::cout<<"\tSending/Getting info to/from the server: "<<tmh.getDif()<<std::endl;
  37. Mapa * mapa = new Mapa(mapname);
  38. THC std::cout<<"Reading and detecting map file (together): "<<tmh.getDif()<<std::endl;
  39. std::cout << "\tServer checksum for "<<mapname <<": "<<sum << std::endl;
  40. std::cout << "\tOur checksum for the map: "<< mapa->checksum << std::endl;
  41. if(mapa->checksum != sum)
  42. exit(-1);
  43. std::cout << "\tUsing random seed: "<<seed << std::endl;
  44. gs = CGI->state;
  45. gs->scenarioOps = si;
  46. gs->init(si,mapa,seed);
  47. CGI->mh = new CMapHandler();
  48. THC std::cout<<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  49. CGI->mh->map = mapa;
  50. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  51. CGI->mh->init();
  52. THC std::cout<<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  53. for (int i=0; i<CGI->state->scenarioOps->playerInfos.size();i++) //initializing interfaces
  54. {
  55. CCallback *cb = new CCallback(CGI->state,CGI->state->scenarioOps->playerInfos[i].color,this);
  56. if(!CGI->state->scenarioOps->playerInfos[i].human)
  57. CGI->playerint.push_back(static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,"EmptyAI.dll")));
  58. else
  59. {
  60. CGI->state->currentPlayer=CGI->state->scenarioOps->playerInfos[i].color;
  61. CGI->playerint.push_back(new CPlayerInterface(CGI->state->scenarioOps->playerInfos[i].color,i));
  62. ((CPlayerInterface*)(CGI->playerint[i]))->init(cb);
  63. }
  64. }
  65. CGI->consoleh->cb = new CCallback(CGI->state,-1,this);
  66. }
  67. CClient::~CClient(void)
  68. {
  69. }
  70. void CClient::process(int what)
  71. {
  72. switch (what)
  73. {
  74. case 100: //one of our interaces has turn
  75. {
  76. ui8 player;
  77. *serv >> player;//who?
  78. std::cout << "It's turn of "<<(unsigned)player<<" player."<<std::endl;
  79. boost::thread(boost::bind(&CGameInterface::yourTurn,CGI->playerint[gs->players[player].serial]));
  80. break;
  81. }
  82. case 101:
  83. {
  84. NewTurn n;
  85. *serv >> n;
  86. std::cout << "New day: "<<(unsigned)n.day<<". Applying changes... ";
  87. gs->apply(&n);
  88. std::cout << "done!"<<std::endl;
  89. break;
  90. }
  91. default:
  92. throw std::exception("Not supported server message!");
  93. break;
  94. }
  95. }
  96. void CClient::run()
  97. {
  98. try
  99. {
  100. ui16 typ;
  101. while(1)
  102. {
  103. *serv >> typ;
  104. process(typ);
  105. }
  106. } HANDLE_EXCEPTION
  107. }