CConsoleHandler.cpp 3.5 KB

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