CConsoleHandler.cpp 4.7 KB

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