main.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "../global.h"
  2. #include "../lib/Connection.h"
  3. #include <boost/lexical_cast.hpp>
  4. #include <boost/thread.hpp>
  5. #include <fstream>
  6. #include "../StartInfo.h"
  7. #ifdef _WIN32
  8. #include <Windows.h>
  9. #else
  10. #include <unistd.h>
  11. #endif
  12. #include "../lib/CGameState.h"
  13. #include "../CCallback.h"
  14. #include "../lib/CGameInterface.h"
  15. #include <boost/format.hpp>
  16. #include "Client.h"
  17. #include "../lib/VCMI_Lib.h"
  18. #include "../lib/BattleState.h"
  19. #include "../lib/NetPacks.h"
  20. #include "../lib/CThreadHelper.h"
  21. using namespace std;
  22. using namespace boost;
  23. std::string NAME = NAME_VER + std::string(" DLL runner");
  24. int main(int argc, char** argv)
  25. {
  26. int pid = getMyPid();
  27. initDLL(console,logfile);
  28. logfile = new std::ofstream(("VCMI_Runner_log_" + boost::lexical_cast<std::string>(pid) + ".txt").c_str());
  29. try
  30. {
  31. string host = "127.0.0.1";
  32. string port = "3030";
  33. CConnection *serv = NULL;
  34. int i = 3;
  35. while(!serv)
  36. {
  37. try
  38. {
  39. tlog0 << "Establishing connection...\n";
  40. serv = new CConnection(host, port, NAME);
  41. }
  42. catch(...)
  43. {
  44. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" << std::endl;
  45. boost::this_thread::sleep(boost::posix_time::seconds(2));
  46. if(!--i)
  47. exit(0);
  48. }
  49. }
  50. ui8 color;
  51. StartInfo si;
  52. string battleAIName;
  53. *serv << getMyPid();
  54. *serv >> si >> battleAIName >> color;
  55. assert(si.mode == StartInfo::DUEL);
  56. tlog0 << format("Server wants us to run %s in battle %s as side %d") % battleAIName % si.mapname % (int)color;
  57. CGameState *gs = new CGameState();
  58. gs->init(&si, 0, 0);
  59. tlog0 << "Gs inited\n";
  60. CClient cl;
  61. cl.serv = serv;
  62. cl.gs = gs;
  63. tlog0 << "Cl created\n";
  64. CBattleCallback * cbc = new CBattleCallback(gs, color, &cl);
  65. tlog0 << "Cbc created\n";
  66. if(battleAIName.size())
  67. {
  68. cl.ai = CDynLibHandler::getNewBattleAI(battleAIName);
  69. cl.color = color;
  70. tlog0 << "AI created\n";
  71. cl.ai->init(cbc);
  72. BattleStart bs;
  73. bs.info = gs->curB;
  74. bs.applyFirstCl(&cl);
  75. bs.applyCl(&cl);
  76. }
  77. else
  78. tlog0 << "Not loading AI, only simulation will be run\n";
  79. tlog0 << cbc->battleGetAllStacks().size() << std::endl;
  80. cl.run();
  81. }
  82. catch(std::exception &e)
  83. {
  84. tlog1 << "Encountered exception: " << e.what() << std::endl;
  85. }
  86. catch(...)
  87. {
  88. tlog1 << "Encountered unknown exception!" << std::endl;
  89. }
  90. return EXIT_SUCCESS;
  91. }