CConsoleHandler.cpp 3.9 KB

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