CConsoleHandler.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "stdafx.h"
  2. #include "CConsoleHandler.h"
  3. #include "CAdvmapInterface.h"
  4. #include "CPlayerInterface.h"
  5. #include "SDL.h"
  6. #include "SDL_thread.h"
  7. #include "CGameInfo.h"
  8. #include "global.h"
  9. #include "CGameState.h"
  10. #include "CCallback.h"
  11. #include "CPathfinder.h"
  12. #include "mapHandler.h"
  13. #include <sstream>
  14. #include "SDL_Extensions.h"
  15. #include "hch/CHeroHandler.h"
  16. #include "hch/CLodHandler.h"
  17. #include "boost/filesystem/operations.hpp"
  18. #include <boost/algorithm/string.hpp>
  19. #ifdef WIN32
  20. #include <conio.h>
  21. #else
  22. #endif
  23. bool continueReadingConsole = true;
  24. int internalFunc(void * callback)
  25. {
  26. CCallback * cb = (CCallback*)callback;
  27. char * usersMessage = new char[500];
  28. std::string readed;
  29. while(true)
  30. {
  31. #ifdef WIN32
  32. if(continueReadingConsole && kbhit())
  33. #else
  34. #endif
  35. {
  36. std::cin.getline(usersMessage, 500);
  37. std::istringstream readed;
  38. std::string pom(usersMessage);
  39. readed.str(pom);
  40. std::string cn; //command name
  41. readed >> cn;
  42. int3 src, dst;
  43. int heronum;
  44. int3 dest;
  45. if(pom==std::string("die, fool"))
  46. exit(0);
  47. else if(pom==std::string("get txt"))
  48. {
  49. boost::filesystem::create_directory("Extracted_txts");
  50. std::cout<<"Command accepted. Opening .lod file...\t";
  51. CLodHandler * txth = new CLodHandler;
  52. txth->init(std::string("Data\\H3bitmap.lod"));
  53. std::cout<<"done.\nScanning .lod file\n";
  54. int curp=0;
  55. std::string pattern = ".TXT";
  56. for(int i=0;i<txth->entries.size(); i++)
  57. {
  58. std::string pom = txth->entries[i].nameStr;
  59. if(boost::algorithm::find_last(pom,pattern))
  60. {
  61. txth->extractFile(std::string("Extracted_txts\\")+pom,pom);
  62. }
  63. int p2 = ((float)i/(float)txth->entries.size())*(float)100;
  64. if(p2!=curp)
  65. {
  66. curp = p2;
  67. std::cout<<"\r"<<curp<<"%";
  68. }
  69. }
  70. std::cout<<"\rExtracting done :)\n";
  71. }
  72. vector<Coordinate>* p;
  73. switch (*cn.c_str())
  74. {
  75. case 'P':
  76. std::cout<<"Policzyc sciezke."<<std::endl;
  77. readed>>src>>dst;
  78. p = CGI->pathf->GetPath(Coordinate(src),Coordinate(dst),CGI->heroh->heroInstances[0]);
  79. LOCPLINT->adventureInt->terrain.currentPath = CGI->pathf->ConvertToOldFormat(p);
  80. //LOCPLINT->adventureInt->terrain.currentPath = CGI->pathf->getPath(src,dst,CGI->heroh->heroInstances[0]);
  81. break;
  82. case 'm': //number of heroes
  83. std::cout<<"Number of heroes: "<<CGI->heroh->heroInstances.size()<<std::endl;
  84. break;
  85. case 'H': //position of hero
  86. readed>>heronum;
  87. std::cout<<"Position of hero "<<heronum<<": "<<CGI->heroh->heroInstances[heronum]->getPosition(false)<<std::endl;
  88. break;
  89. case 'M': //move heroa
  90. {
  91. readed>>heronum>>dest;
  92. const CGHeroInstance * hero = cb->getHeroInfo(0,heronum,0);
  93. p = CGI->pathf->GetPath(Coordinate(hero->getPosition(false)),Coordinate(dest),hero);
  94. cb->moveHero(heronum, CGI->pathf->ConvertToOldFormat(p), 0, 0);
  95. //LOCPLINT->adventureInt->terrain.currentPath = CGI->pathf->getPath(src,dst,CGI->heroh->heroInstances[0]);
  96. break;
  97. }
  98. case 'D': //pos description
  99. readed>>src;
  100. CGI->mh->getObjDescriptions(src);
  101. break;
  102. case 'I':
  103. {
  104. SDL_Surface * temp = LOCPLINT->infoWin(NULL);
  105. blitAtWR(temp,605,389);
  106. SDL_FreeSurface(temp);
  107. break;
  108. }
  109. case 'T': //test rect
  110. readed>>src;
  111. for(int g=0; g<8; ++g)
  112. {
  113. for(int v=0; v<8; ++v)
  114. {
  115. int3 csrc = src;
  116. csrc.y+=g;
  117. csrc.x+=v;
  118. if(CGI->mh->getObjDescriptions(csrc).size())
  119. std::cout<<'x';
  120. else
  121. std::cout<<'o';
  122. }
  123. std::cout<<std::endl;
  124. }
  125. break;
  126. case 'A': //hide everything from map
  127. for(int c=0; c<CGI->objh->objInstances.size(); ++c)
  128. {
  129. CGI->mh->hideObject(CGI->objh->objInstances[c]);
  130. }
  131. break;
  132. case 'R': //restora all objects after A has been pressed
  133. for(int c=0; c<CGI->objh->objInstances.size(); ++c)
  134. {
  135. CGI->mh->printObject(CGI->objh->objInstances[c]);
  136. }
  137. break;
  138. }
  139. //SDL_Delay(100);
  140. delete p;
  141. }
  142. }
  143. return -1;
  144. }
  145. SDL_Thread * consoleReadingThread;
  146. void CConsoleHandler::runConsole()
  147. {
  148. consoleReadingThread = SDL_CreateThread(&internalFunc, cb);
  149. }