Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*** Walking System below ***
- void PlayerHandler::addToWalkingQueue(Player* p, int x, int y) {
- Pos newWaypoint;
- newWaypoint.x = x;
- newWaypoint.y = y;
- if(p->pathWaypoints.size() < WALKING_QUEUE_SIZE)
- p->pathWaypoints.push_back(newWaypoint);
- else
- printError("Walking queue is full!\n");
- }
- void PlayerHandler::resetWalkingQueue(Player* p) {
- //p->waypointOffset = 0;
- p->pathWaypoints.clear();
- }
- bool PlayerHandler::hasNextStep(Player* p) {
- return !p->pathWaypoints.empty(); //(p->waypointOffset < p->pathWaypoints.size());
- }
- int PlayerHandler::getNextWalkingDirection(Player* p) {
- // returns 0-7 for next walking direction or -1, if we're not moving
- // If referenced player is at the current waypoint, go to next waypoint
- if(cmpPos(p->pathWaypoints.front(), p->getAbsPos())) {
- p->pathWaypoints.erase(p->pathWaypoints.begin()); //remove first waypoint as we are standing right on it.
- }
- // Ends processing if there is no next step
- if(!hasNextStep(p))
- return -1;
- int nextX = p->getAbsPos().x;
- int nextY = p->getAbsPos().y;
- if(nextX > p->pathWaypoints.front().x)
- --nextX;
- else if(nextX < p->pathWaypoints.front().x)
- ++nextX;
- if(nextY > p->pathWaypoints.front().y)
- --nextY;
- else if(nextY < p->pathWaypoints.front().y)
- ++nextY;
- Pos nextPos;
- nextPos.x = nextX;
- nextPos.y = nextY;
- if(!cmpPos(p->getAbsPos(), nextPos)) {
- Pos lastPos = p->getAbsPos();
- p->setAbsPos(nextPos); //current coordinates update.
- return getDirection(p->getAbsPos(), lastPos);
- }
- return -1;
- }
- void PlayerHandler::getNextPlayerMovement(Player* p) {
- // calculates directions of player movement, or the new coordinates when teleporting
- p->dir1 = -1;
- p->dir2 = -1;
- if(p->getTeleportPos().x != -1 && p->getTeleportPos().x != -1) {
- p->setMapRegionChange(true);
- if(p->getMapRegionPos().x != -1 && p->getMapRegionPos().y != -1) {
- // check, whether destination is within current map region
- int relX = p->getTeleportPos().x-p->getMapRegionPos().x*8, relY = p->getTeleportPos().y-p->getMapRegionPos().y*8;
- if(relX >= 2*8 && relX < 11*8 && relY >= 2*8 && relY < 11*8)
- p->setMapRegionChange(false);
- }
- if(p->isMapRegionChanged()) {
- // after map region change the relative coordinates range between 48 - 55
- Pos mapRegionChange;
- mapRegionChange.x = (p->getTeleportPos().x>>3)-6;
- mapRegionChange.y = (p->getTeleportPos().y>>3)-6;
- p->setMapRegionPos(mapRegionChange);
- // completely rebuild playerList after teleport AND map region change
- p->playerList.erase(p->playerList.begin(), p->playerList.end());
- }
- p->setAbsPos(p->getTeleportPos());
- resetWalkingQueue(p);
- Pos tempPos;
- tempPos.x = -1;
- tempPos.y = -1;
- p->setTeleportPos(tempPos);
- p->setTeleporting(false); //not teleporting anymore to stop useless cycles.
- p->setHasTeleported(true); //finished with teleport (update player's new location)
- } else {
- p->dir1 = getNextWalkingDirection(p);
- if(p->dir1 == -1)
- return; // standing
- if(p->isRunning && hasNextStep(p))
- p->dir2 = getNextWalkingDirection(p);
- Pos mapRegion = p->getMapRegionPos();
- Pos absPos = p->getAbsPos();
- // check, whether destination is within current map region
- int relX = absPos.x-mapRegion.x*8, relY = absPos.y-mapRegion.y*8;
- if(relX >= 2*8 && relX < 11*8 && relY >= 2*8 && relY < 11*8) {
- p->setMapRegionChange(false);
- } else {
- Pos mapRegionChange;
- mapRegionChange.x = (p->getAbsPos().x>>3)-6;
- mapRegionChange.y = (p->getAbsPos().y>>3)-6;
- p->setMapRegionPos(mapRegionChange);
- p->setMapRegionChange(true);
- }
- }
- }
- void PlayerHandler::updateThisPlayerMovement(Player* p, Stream* str) {
- // handles anything related to character position, i.e. walking,running and teleportation
- // applies only to the char and the client which is playing it
- if(p->isMapRegionChanged()) {
- str->createFrame(73);
- str->writeWordA(p->getMapRegionPos().x+6); // for some reason the client substracts 6 from those values
- str->writeWord(p->getMapRegionPos().y+6);
- p->setMapRegionChange(false);
- }
- if(p->hasTeleported()) { //if teleport is completed.
- str->createFrameVarSizeWord(81);
- str->initBitAccess();
- str->writeBits(1, 1);
- str->writeBits(2, 3); // updateType
- str->writeBits(2, p->getHeightLevel());
- str->writeBits(1, 1); // set to true, if discarding (clientside) walking queue
- str->writeBits(1, (p->isUpdateRequired()) ? 1 : 0);
- str->writeBits(7, (p->getAbsPos().y - 8*p->getMapRegionPos().y));
- str->writeBits(7, (p->getAbsPos().x - 8*p->getMapRegionPos().x));
- p->setHasTeleported(false); //finished teleporting.
- return;
- }
- if(p->dir1 == -1) {
- // don't have to update the character position, because we're just standing
- str->createFrameVarSizeWord(81);
- str->initBitAccess();
- if(p->isUpdateRequired()) {
- // tell client there's an update block appended at the end
- str->writeBits(1, 1);
- str->writeBits(2, 0);
- } else str->writeBits(1, 0);
- } else {
- if (p->createItems) {
- }
- str->createFrameVarSizeWord(81);
- str->initBitAccess();
- str->writeBits(1, 1);
- if(p->dir2 == -1) {
- // send "walking packet"
- str->writeBits(2, 1); // updateType
- str->writeBits(3, xlateDirectionToClient[p->dir1]);
- if(p->isUpdateRequired()) str->writeBits(1, 1); // tell client there's an update block appended at the end
- else str->writeBits(1, 0);
- } else {
- // send "running packet"
- str->writeBits(2, 2); // updateType
- str->writeBits(3, xlateDirectionToClient[p->dir1]);
- str->writeBits(3, xlateDirectionToClient[p->dir2]);
- if(p->isUpdateRequired()) str->writeBits(1, 1); // tell client there's an update block appended at the end
- else str->writeBits(1, 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement