Advertisement
Guest User

Untitled

a guest
Sep 17th, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. //*** Walking System below ***
  2. void PlayerHandler::addToWalkingQueue(Player* p, int x, int y) {
  3. Pos newWaypoint;
  4. newWaypoint.x = x;
  5. newWaypoint.y = y;
  6. if(p->pathWaypoints.size() < WALKING_QUEUE_SIZE)
  7. p->pathWaypoints.push_back(newWaypoint);
  8. else
  9. printError("Walking queue is full!\n");
  10. }
  11.  
  12. void PlayerHandler::resetWalkingQueue(Player* p) {
  13. //p->waypointOffset = 0;
  14. p->pathWaypoints.clear();
  15. }
  16.  
  17. bool PlayerHandler::hasNextStep(Player* p) {
  18. return !p->pathWaypoints.empty(); //(p->waypointOffset < p->pathWaypoints.size());
  19. }
  20.  
  21. int PlayerHandler::getNextWalkingDirection(Player* p) {
  22. // returns 0-7 for next walking direction or -1, if we're not moving
  23.  
  24. // If referenced player is at the current waypoint, go to next waypoint
  25. if(cmpPos(p->pathWaypoints.front(), p->getAbsPos())) {
  26. p->pathWaypoints.erase(p->pathWaypoints.begin()); //remove first waypoint as we are standing right on it.
  27. }
  28. // Ends processing if there is no next step
  29. if(!hasNextStep(p))
  30. return -1;
  31.  
  32. int nextX = p->getAbsPos().x;
  33. int nextY = p->getAbsPos().y;
  34.  
  35. if(nextX > p->pathWaypoints.front().x)
  36. --nextX;
  37. else if(nextX < p->pathWaypoints.front().x)
  38. ++nextX;
  39.  
  40. if(nextY > p->pathWaypoints.front().y)
  41. --nextY;
  42. else if(nextY < p->pathWaypoints.front().y)
  43. ++nextY;
  44.  
  45. Pos nextPos;
  46. nextPos.x = nextX;
  47. nextPos.y = nextY;
  48.  
  49. if(!cmpPos(p->getAbsPos(), nextPos)) {
  50. Pos lastPos = p->getAbsPos();
  51. p->setAbsPos(nextPos); //current coordinates update.
  52. return getDirection(p->getAbsPos(), lastPos);
  53. }
  54. return -1;
  55. }
  56.  
  57. void PlayerHandler::getNextPlayerMovement(Player* p) {
  58. // calculates directions of player movement, or the new coordinates when teleporting
  59. p->dir1 = -1;
  60. p->dir2 = -1;
  61.  
  62. if(p->getTeleportPos().x != -1 && p->getTeleportPos().x != -1) {
  63. p->setMapRegionChange(true);
  64. if(p->getMapRegionPos().x != -1 && p->getMapRegionPos().y != -1) {
  65. // check, whether destination is within current map region
  66. int relX = p->getTeleportPos().x-p->getMapRegionPos().x*8, relY = p->getTeleportPos().y-p->getMapRegionPos().y*8;
  67. if(relX >= 2*8 && relX < 11*8 && relY >= 2*8 && relY < 11*8)
  68. p->setMapRegionChange(false);
  69. }
  70. if(p->isMapRegionChanged()) {
  71. // after map region change the relative coordinates range between 48 - 55
  72. Pos mapRegionChange;
  73. mapRegionChange.x = (p->getTeleportPos().x>>3)-6;
  74. mapRegionChange.y = (p->getTeleportPos().y>>3)-6;
  75. p->setMapRegionPos(mapRegionChange);
  76.  
  77. // completely rebuild playerList after teleport AND map region change
  78. p->playerList.erase(p->playerList.begin(), p->playerList.end());
  79. }
  80. p->setAbsPos(p->getTeleportPos());
  81. resetWalkingQueue(p);
  82. Pos tempPos;
  83. tempPos.x = -1;
  84. tempPos.y = -1;
  85. p->setTeleportPos(tempPos);
  86. p->setTeleporting(false); //not teleporting anymore to stop useless cycles.
  87. p->setHasTeleported(true); //finished with teleport (update player's new location)
  88. } else {
  89. p->dir1 = getNextWalkingDirection(p);
  90. if(p->dir1 == -1)
  91. return; // standing
  92. if(p->isRunning && hasNextStep(p))
  93. p->dir2 = getNextWalkingDirection(p);
  94.  
  95. Pos mapRegion = p->getMapRegionPos();
  96. Pos absPos = p->getAbsPos();
  97.  
  98. // check, whether destination is within current map region
  99. int relX = absPos.x-mapRegion.x*8, relY = absPos.y-mapRegion.y*8;
  100. if(relX >= 2*8 && relX < 11*8 && relY >= 2*8 && relY < 11*8) {
  101. p->setMapRegionChange(false);
  102. } else {
  103. Pos mapRegionChange;
  104. mapRegionChange.x = (p->getAbsPos().x>>3)-6;
  105. mapRegionChange.y = (p->getAbsPos().y>>3)-6;
  106. p->setMapRegionPos(mapRegionChange);
  107. p->setMapRegionChange(true);
  108. }
  109. }
  110. }
  111.  
  112. void PlayerHandler::updateThisPlayerMovement(Player* p, Stream* str) {
  113. // handles anything related to character position, i.e. walking,running and teleportation
  114. // applies only to the char and the client which is playing it
  115. if(p->isMapRegionChanged()) {
  116. str->createFrame(73);
  117. str->writeWordA(p->getMapRegionPos().x+6); // for some reason the client substracts 6 from those values
  118. str->writeWord(p->getMapRegionPos().y+6);
  119. p->setMapRegionChange(false);
  120. }
  121.  
  122. if(p->hasTeleported()) { //if teleport is completed.
  123. str->createFrameVarSizeWord(81);
  124. str->initBitAccess();
  125. str->writeBits(1, 1);
  126. str->writeBits(2, 3); // updateType
  127. str->writeBits(2, p->getHeightLevel());
  128. str->writeBits(1, 1); // set to true, if discarding (clientside) walking queue
  129. str->writeBits(1, (p->isUpdateRequired()) ? 1 : 0);
  130. str->writeBits(7, (p->getAbsPos().y - 8*p->getMapRegionPos().y));
  131. str->writeBits(7, (p->getAbsPos().x - 8*p->getMapRegionPos().x));
  132. p->setHasTeleported(false); //finished teleporting.
  133. return;
  134. }
  135.  
  136. if(p->dir1 == -1) {
  137. // don't have to update the character position, because we're just standing
  138. str->createFrameVarSizeWord(81);
  139. str->initBitAccess();
  140. if(p->isUpdateRequired()) {
  141. // tell client there's an update block appended at the end
  142. str->writeBits(1, 1);
  143. str->writeBits(2, 0);
  144. } else str->writeBits(1, 0);
  145. } else {
  146. if (p->createItems) {
  147. }
  148.  
  149. str->createFrameVarSizeWord(81);
  150. str->initBitAccess();
  151. str->writeBits(1, 1);
  152.  
  153. if(p->dir2 == -1) {
  154. // send "walking packet"
  155. str->writeBits(2, 1); // updateType
  156. str->writeBits(3, xlateDirectionToClient[p->dir1]);
  157. if(p->isUpdateRequired()) str->writeBits(1, 1); // tell client there's an update block appended at the end
  158. else str->writeBits(1, 0);
  159. } else {
  160. // send "running packet"
  161. str->writeBits(2, 2); // updateType
  162. str->writeBits(3, xlateDirectionToClient[p->dir1]);
  163. str->writeBits(3, xlateDirectionToClient[p->dir2]);
  164. if(p->isUpdateRequired()) str->writeBits(1, 1); // tell client there's an update block appended at the end
  165. else str->writeBits(1, 0);
  166. }
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement