main.cpp 2.8 KB

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