main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "CheckTime.h"
  22. using namespace std;
  23. using namespace boost;
  24. std::string NAME = NAME_VER + std::string(" DLL runner");
  25. void mySleep(int ms)
  26. {
  27. CheckTime timer;
  28. #ifdef _WIN32
  29. Sleep(ms);
  30. #else
  31. usleep(ms * 1000);
  32. #endif
  33. tlog0 << "We were ordered to sleep for " << ms << " ms and we did for " << timer.timeSinceStart() << std::endl;
  34. }
  35. int main(int argc, char** argv)
  36. {
  37. std::string logDir = ".";
  38. if(argc >= 2)
  39. logDir = argv[1];
  40. int pid = getMyPid();
  41. initDLL(console,logfile);
  42. std::string logName = logDir + "/" + "VCMI_Runner_log_" + boost::lexical_cast<std::string>(pid) + ".txt";
  43. logfile = new std::ofstream(logName.c_str());
  44. try
  45. {
  46. string host = "127.0.0.1";
  47. string port = "3030";
  48. CConnection *serv = NULL;
  49. int i = 3;
  50. while(!serv)
  51. {
  52. try
  53. {
  54. tlog0 << "Establishing connection...\n";
  55. serv = new CConnection(host, port, NAME);
  56. }
  57. catch(...)
  58. {
  59. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" << std::endl;
  60. boost::this_thread::sleep(boost::posix_time::seconds(2));
  61. if(!--i)
  62. exit(0);
  63. }
  64. }
  65. ui8 color;
  66. StartInfo si;
  67. string battleAIName;
  68. *serv << getMyPid();
  69. *serv >> si >> battleAIName >> color;
  70. assert(si.mode == StartInfo::DUEL);
  71. tlog0 << format("Server wants us to run %s in battle %s as side %d") % battleAIName % si.mapname % (int)color;
  72. CGameState *gs = new CGameState();
  73. gs->init(&si, 0, 0);
  74. tlog0 << "Gs inited\n";
  75. CClient cl;
  76. cl.serv = serv;
  77. cl.gs = gs;
  78. tlog0 << "Cl created\n";
  79. CBattleCallback * cbc = new CBattleCallback(gs, color, &cl);
  80. tlog0 << "Cbc created\n";
  81. if(battleAIName.size())
  82. {
  83. Bomb *b = new Bomb(CONSTRUCT_TIME + 5 + HANGUP_TIME, "startup timer");
  84. CheckTime timer;
  85. //////////////////////////////////////////////////////////////////////////
  86. cl.ai = CDynLibHandler::getNewBattleAI(battleAIName);
  87. cl.color = color;
  88. tlog0 << "AI created\n";
  89. cl.ai->init(cbc);
  90. //////////////////////////////////////////////////////////////////////////
  91. postDecisionCall(timer.timeSinceStart(), "AI was being created");
  92. b->disarm();
  93. BattleStart bs;
  94. bs.info = gs->curB;
  95. bs.applyFirstCl(&cl);
  96. bs.applyCl(&cl);
  97. }
  98. else
  99. tlog0 << "Not loading AI, only simulation will be run\n";
  100. tlog0 << cbc->battleGetAllStacks().size() << std::endl;
  101. cl.run();
  102. }
  103. catch(std::exception &e)
  104. {
  105. tlog1 << "Encountered exception: " << e.what() << std::endl;
  106. }
  107. catch(...)
  108. {
  109. tlog1 << "Encountered unknown exception!" << std::endl;
  110. }
  111. return EXIT_SUCCESS;
  112. }