CBattleCallback.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #define VCMI_DLL
  2. #include "CBattleCallback.h"
  3. #include "NetPacks.h"
  4. #include "Connection.h"
  5. #include "CGameState.h"
  6. #include <boost/thread/shared_mutex.hpp>
  7. #include "BattleState.h"
  8. CBattleCallback::CBattleCallback(CGameState *GS, int Player, IConnectionHandler *C )
  9. {
  10. gs = GS;
  11. player = Player;
  12. connHandler = C;
  13. }
  14. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  15. {
  16. assert(gs->curB->tacticDistance);
  17. if(action->actionType == BattleAction::WALK)
  18. {
  19. if(!gs->curB->isInTacticRange(action->destinationTile))
  20. {
  21. tlog0 << "Requesting movement to tile that is not in tactics range? Illegal!\n";
  22. if(!action->destinationTile.isValid())
  23. tlog0 << "Moreover the hex is invalid!!!\n";
  24. return false;
  25. }
  26. }
  27. MakeAction ma;
  28. ma.ba = *action;
  29. sendRequest(&ma);
  30. return true;
  31. }
  32. int CBattleCallback::battleMakeAction(BattleAction* action)
  33. {
  34. assert(action->actionType == BattleAction::HERO_SPELL);
  35. MakeCustomAction mca(*action);
  36. sendRequest(&mca);
  37. return 0;
  38. }
  39. void CBattleCallback::sendRequest(const CPack* request)
  40. {
  41. //TODO should be part of CClient (client owns connection, not CB)
  42. //but it would have to be very tricky cause template/serialization issues
  43. if(waitTillRealize)
  44. connHandler->waitingRequest.set(typeList.getTypeID(request));
  45. connHandler->serv->sendPack(*request);
  46. if(waitTillRealize)
  47. {
  48. if(unlockGsWhenWaiting)
  49. gs->mx->unlock_shared();
  50. connHandler->waitingRequest.waitWhileTrue();
  51. if(unlockGsWhenWaiting)
  52. gs->mx->lock_shared();
  53. }
  54. }
  55. IConnectionHandler::IConnectionHandler()
  56. : waitingRequest(0)
  57. {
  58. }
  59. IConnectionHandler::~IConnectionHandler()
  60. {
  61. }