Client.cpp 3.5 KB

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