CConsoleHandler.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(DATADIR "Data" PATHSEPARATOR "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. int heroX;
  74. int heroY;
  75. int heroZ;
  76. switch (*cn.c_str())
  77. {
  78. case 'P':
  79. std::cout<<"Policzyc sciezke."<<std::endl;
  80. readed>>src>>dst;
  81. p = CGI->pathf->GetPath(Coordinate(src),Coordinate(dst),CGI->heroh->heroInstances[0]);
  82. LOCPLINT->adventureInt->terrain.currentPath = CGI->pathf->ConvertToOldFormat(p);
  83. //LOCPLINT->adventureInt->terrain.currentPath = CGI->pathf->getPath(src,dst,CGI->heroh->heroInstances[0]);
  84. break;
  85. case 'm': //number of heroes
  86. std::cout<<"Number of heroes: "<<CGI->heroh->heroInstances.size()<<std::endl;
  87. break;
  88. case 'H': //position of hero
  89. readed>>heronum;
  90. #ifndef __GNUC__
  91. std::cout<<"Position of hero "<<heronum<<": "<<CGI->heroh->heroInstances[heronum]->getPosition(false)<<std::endl;
  92. #else
  93. heroX = CGI->heroh->heroInstances[heronum]->getPosition(false).x;
  94. heroY = CGI->heroh->heroInstances[heronum]->getPosition(false).y;
  95. heroZ = CGI->heroh->heroInstances[heronum]->getPosition(false).z;
  96. std::cout<<"Position of hero "<<heronum<<": x:"<<heroX<<"- y:"<<heroY<<"- z:"<<heroZ<<std::endl;
  97. #endif
  98. break;
  99. case 'M': //move heroa
  100. {
  101. readed>>heronum>>dest;
  102. const CGHeroInstance * hero = cb->getHeroInfo(0,heronum,0);
  103. p = CGI->pathf->GetPath(Coordinate(hero->getPosition(false)),Coordinate(dest),hero);
  104. cb->moveHero(heronum, CGI->pathf->ConvertToOldFormat(p), 0, 0);
  105. //LOCPLINT->adventureInt->terrain.currentPath = CGI->pathf->getPath(src,dst,CGI->heroh->heroInstances[0]);
  106. break;
  107. }
  108. case 'D': //pos description
  109. readed>>src;
  110. CGI->mh->getObjDescriptions(src);
  111. break;
  112. case 'I':
  113. {
  114. SDL_Surface * temp = LOCPLINT->infoWin(NULL);
  115. blitAtWR(temp,605,389);
  116. SDL_FreeSurface(temp);
  117. break;
  118. }
  119. case 'T': //test rect
  120. readed>>src;
  121. for(int g=0; g<8; ++g)
  122. {
  123. for(int v=0; v<8; ++v)
  124. {
  125. int3 csrc = src;
  126. csrc.y+=g;
  127. csrc.x+=v;
  128. if(CGI->mh->getObjDescriptions(csrc).size())
  129. std::cout<<'x';
  130. else
  131. std::cout<<'o';
  132. }
  133. std::cout<<std::endl;
  134. }
  135. break;
  136. case 'A': //hide everything from map
  137. for(int c=0; c<CGI->objh->objInstances.size(); ++c)
  138. {
  139. CGI->mh->hideObject(CGI->objh->objInstances[c]);
  140. }
  141. break;
  142. case 'R': //restora all objects after A has been pressed
  143. for(int c=0; c<CGI->objh->objInstances.size(); ++c)
  144. {
  145. CGI->mh->printObject(CGI->objh->objInstances[c]);
  146. }
  147. break;
  148. }
  149. //SDL_Delay(100);
  150. delete p;
  151. }
  152. }
  153. return -1;
  154. }
  155. SDL_Thread * consoleReadingThread;
  156. void CConsoleHandler::runConsole()
  157. {
  158. consoleReadingThread = SDL_CreateThread(&internalFunc, cb);
  159. }