]> git.uio.no Git - private-olgabo.git/commitdiff
changed to dumb client
authorolgabo <olgabo@ifi.uio.no>
Thu, 18 Oct 2012 19:32:05 +0000 (21:32 +0200)
committerolgabo <olgabo@ifi.uio.no>
Thu, 18 Oct 2012 19:32:05 +0000 (21:32 +0200)
include/Player.h
projects/dumbclient/Game.cpp
projects/dumbclient/Game.h
src/PredictedState.cpp

index 080f6390be87d256681cc0abcf9378d1fb498e70..1c8f359d071f2190ecf41d7c8d4b3cf8d01ef0bb 100644 (file)
@@ -58,7 +58,7 @@ public:
 
     //score
     int HandshakesDone;
-       int HandshakesReceived;
+    int HandshakesReceived;
        int RPSDone;
        int RPSReceived;
        int PunchesDone;
@@ -66,6 +66,15 @@ public:
        int WavesDone;
        int WavesReceived;
 
+       int HandshakesDoneAck;
+    int HandshakesReceivedAck;
+       int RPSDoneAck;
+       int RPSReceivedAck;
+       int PunchesDoneAck;
+       int PunchesReceivedAck;
+       int WavesDoneAck;
+       int WavesReceivedAck;
+
        int stepsLeft;
 
     bool cameraChanged;
@@ -76,6 +85,8 @@ public:
        Vector3 moveCamera;
        Real distanceToOtherPlayer;
 
+       Vector3 lastActionLocation;
+
 
 
 protected:
index ba7816c97f4e6009c8254a17dffb45031a8fb91d..83a663ffadcd799249e7ea8bb4b712ee0757a294 100644 (file)
@@ -42,7 +42,7 @@ void Game::init()
     cameraTimerOn = false;
     gameOver = false;
     cameraRotProgress = 0;
-    nextWave.tv_sec = 0;
+    
 
     if (gameServer != "offline")
     {  
@@ -583,7 +583,7 @@ bool Game::frameRenderingQueued(const FrameEvent &evt)
         {
             restoreCamera = true;
             if (!localPlayer->dead)
-                processAction("idle", localPlayer);
+                processAction("idle", localPlayer, false);
         }
 
         // set idle animation immediately if stopped walking 
@@ -593,7 +593,7 @@ bool Game::frameRenderingQueued(const FrameEvent &evt)
                 !mKeyboard->isKeyDown(OIS::KC_DOWN) && 
                 !mKeyboard->isKeyDown(OIS::KC_LEFT) && 
                 !mKeyboard->isKeyDown(OIS::KC_RIGHT))
-                processAction("idle", localPlayer);
+                processAction("idle", localPlayer, false);
         }
 
         
@@ -700,7 +700,7 @@ bool Game::processUnbufferedInput(const FrameEvent& evt)
     {   
         mToggle  = 0.1;
         if (localPlayer->anState->getAnimationName() != "walk" || !localPlayer->anState->getEnabled())
-            canWalk = processAction("walk", localPlayer);
+            canWalk = processAction("walk", localPlayer, false);
         transVector.z += mMove;
 
     }
@@ -709,7 +709,7 @@ bool Game::processUnbufferedInput(const FrameEvent& evt)
     {   
         mToggle  = 0.1;
         if (localPlayer->anState->getAnimationName() != "walk" || !localPlayer->anState->getEnabled())
-            canWalk = processAction("walk", localPlayer);
+            canWalk = processAction("walk", localPlayer, false);
         transVector.z -= mMove;  
     }
 
@@ -717,7 +717,7 @@ bool Game::processUnbufferedInput(const FrameEvent& evt)
     {
         mToggle  = 0.1;
         if (localPlayer->anState->getAnimationName() != "walk" || !localPlayer->anState->getEnabled())
-            canWalk = processAction("walk", localPlayer);
+            canWalk = processAction("walk", localPlayer, false);
         
         mRotate += DEGREES_PER_SECOND;
     }
@@ -726,7 +726,7 @@ bool Game::processUnbufferedInput(const FrameEvent& evt)
     {
         mToggle  = 0.1;
         if (localPlayer->anState->getAnimationName() != "walk" || !localPlayer->anState->getEnabled())
-            canWalk = processAction("walk", localPlayer);
+            canWalk = processAction("walk", localPlayer, false);
         
         mRotate -= DEGREES_PER_SECOND;
     }
@@ -735,27 +735,27 @@ bool Game::processUnbufferedInput(const FrameEvent& evt)
     {
         mToggle  = 0.1;
         if (mKeyboard->isKeyDown(OIS::KC_W))
-            processAction("wave", localPlayer);
+            processAction("wave", localPlayer, true);
         else if (mKeyboard->isKeyDown(OIS::KC_N)) //nod
-            processAction("nod", localPlayer);
+            processAction("nod", localPlayer, false);
         else if (mKeyboard->isKeyDown(OIS::KC_P)) // punch
-            processAction("punch", localPlayer);
+            processAction("punch", localPlayer, true);
         else if (mKeyboard->isKeyDown(OIS::KC_3)) // headshake
-            processAction("headshake", localPlayer);
+            processAction("headshake", localPlayer, false);
         else if (mKeyboard->isKeyDown(OIS::KC_4)) // headshake
-            processAction("rock", localPlayer);
+            processAction("rock", localPlayer, false);
         else if (mKeyboard->isKeyDown(OIS::KC_5)) // headshake
-            processAction("paper", localPlayer);
+            processAction("paper", localPlayer, false);
         else if (mKeyboard->isKeyDown(OIS::KC_6)) // headshake
-            processAction("scissors", localPlayer);
+            processAction("scissors", localPlayer, false);
         else if (mKeyboard->isKeyDown(OIS::KC_H)) // handshake
         {
-            processAction("handshake", localPlayer);
+            processAction("handshake", localPlayer, false);
             mToggle  = 0.5;
         }
         else if (mKeyboard->isKeyDown(OIS::KC_R))
         {
-            processAction("rockpaperscissors", localPlayer);
+            processAction("rockpaperscissors", localPlayer, true);
             mToggle  = 0.9;
         }
         /*    
@@ -797,15 +797,9 @@ bool Game::processUnbufferedInput(const FrameEvent& evt)
     return true;
 }
 //-------------------------------------------------------------------------------------
-bool Game::processAction(const String &an, class Player* player)
+bool Game::processAction(const String &an, class Player* player, bool scoreAction)
 {
-    String curAnimation = player->anState->getAnimationName();
-    /*if (curAnimation == an && !player->anState->getLoop())
-        return false;*/
-    /*if ((player == localPlayer && !player->anState->getLoop() && !player->anState->hasEnded()) || localPlayer->cameraChanged)
-        return false;*/
-
+    
     if (player == localPlayer && localPlayer->cameraChanged)
     {
         cout << "local player and camera changed\n";
@@ -813,7 +807,13 @@ bool Game::processAction(const String &an, class Player* player)
     }
        
    
-    
+    if (player == localPlayer && localPlayer->stepsLeft > 0 && scoreAction)
+    {
+        msgBoard->setCaption("Go " + StringConverter::toString(localPlayer->stepsLeft) + 
+            " steps away from last action location to perform next action");
+        gettimeofday(&msgTime, NULL);                
+        return true;
+    }
 
     if (an == "handshake")
     {
@@ -842,27 +842,12 @@ bool Game::processAction(const String &an, class Player* player)
                         adjustCamera = true;
                     }
                     if (player == localPlayer)
-                        updateActionInfo(an);
+                        updateActionInfo(an, current->id);
                     return true;    
                 } 
             }
         }
 
-        for (current = localPlayer; current != NULL; current = current->next)
-        {
-            if (current != player && predictionHandler->HandshakePosition(player, current) == 2)
-            {
-                if (player == localPlayer)
-                {
-                    msgBoard->setCaption("Go " + 
-                          StringConverter::toString(current->stepsLeft) +
-                            " steps away from the player to perform next action");
-                        gettimeofday(&msgTime, NULL);
-                        return true;
-                }
-            }
-        }
-
         for (current = localPlayer; current != NULL; current = current->next)
         {
             // requestor is local player and can see another player 
@@ -944,28 +929,12 @@ bool Game::processAction(const String &an, class Player* player)
                     adjustCamera = true;
                 }
                 if (player == localPlayer)
-                    updateActionInfo(an);
+                    updateActionInfo(an, current->id);
            
                 return true;    
             }
         }
 
-        for (current = localPlayer; current != NULL; current = current->next)
-        {  
-            if (current != player && predictionHandler->RockPaperScissorsPosition(player, current) == 2)
-            {
-                if (player == localPlayer)
-                {
-                    msgBoard->setCaption("Go " + 
-                        StringConverter::toString(current->stepsLeft) +
-                        " steps away from the player to perform next action");
-                    gettimeofday(&msgTime, NULL);
-                    return true;
-                }
-            }
-        }
-
-
         for (current = localPlayer; current != NULL; current = current->next)
         {        
             // requestor is local player and can see another player 
@@ -1012,27 +981,11 @@ bool Game::processAction(const String &an, class Player* player)
                     adjustCamera = true;
                 }
                 if (player == localPlayer)
-                    updateActionInfo(an);
+                    updateActionInfo(an, current->id);
                 return true;     
             }
         }
         
-        for (current = localPlayer; current != NULL; current = current->next)
-        {
-
-            if (current != player && predictionHandler->PunchPosition(player, current) == 2)
-            {
-                if (player == localPlayer)
-                {
-                    msgBoard->setCaption("Go " + 
-                        StringConverter::toString(current->stepsLeft) +
-                        " steps away from the player to perform next action");
-                    gettimeofday(&msgTime, NULL);
-                    return true; 
-                }
-            }
-        }
-
         for (current = localPlayer; current != NULL; current = current->next)
         {                
             // requestor is local player and can see another player
@@ -1058,24 +1011,8 @@ bool Game::processAction(const String &an, class Player* player)
     }
     else if (an == "wave")
     {
-        if (!canWave())
-        {
-            if (player == localPlayer)
-            {
-                msgBoard->setCaption("You cannot wave more often than once in 3 seconds!");
-                gettimeofday(&msgTime, NULL);
-            }
-            return true;
-        }
-        
         player->playAnimation("wave");
-        struct timeval now;
-        gettimeofday(&now, NULL); 
-        struct timeval delay;
-        delay.tv_sec = 3;
-        delay.tv_usec = 0;
-        timeradd(&now, &delay, &nextWave);
-       
+              
         
         int i;
         Player *current;
@@ -1095,7 +1032,7 @@ bool Game::processAction(const String &an, class Player* player)
                         msgBoard->setCaption("Player " + StringConverter::toString(current->id) + " waved at you");   
                     registerScore(player, current, WAVE);
                     if (player == localPlayer)
-                        updateActionInfo(an);
+                        updateActionInfo(an, current->id);
                     return true;    
                 }
             }
@@ -1112,7 +1049,7 @@ bool Game::processAction(const String &an, class Player* player)
     {
         player->playAnimation(an);
         if (player == localPlayer)
-            updateActionInfo(an);
+            updateActionInfo(an, -1);
     }
         
     
@@ -1120,20 +1057,20 @@ bool Game::processAction(const String &an, class Player* player)
 }
 
 //-------------------------------------------------------------------------------------
-void Game::processActionByPlayerID(const String &an, int id)
+void Game::processActionByPlayerID(const String &an, int id, bool scoreAction)
 {
     Player *current;
     for (current = localPlayer->next; current != NULL; current = current->next)
     {
         if (current->id == id)
         {
-            processAction(an, current);
+            processAction(an, current, scoreAction);
             break;
         }
     }
 }
 //-------------------------------------------------------------------------------------
-void Game::updateActionInfo(const String &an)
+void Game::updateActionInfo(const String &an, int otherplayer)
 {
     int animation_id;
     if (an == "idle")
@@ -1155,6 +1092,7 @@ void Game::updateActionInfo(const String &an)
    
     pthread_mutex_lock(&an_mutex);
     animation = animation_id;
+    otherPlayer = otherplayer;
     pthread_mutex_unlock(&an_mutex);
     pthread_cond_signal(&time2send);
 }
@@ -1216,7 +1154,10 @@ void Game::sendUpdate()
         memcpy(&sendbuffer[size],(void*)&animation, sizeof(int));
         if (animation == 7 || animation == 8 || animation == 5)
             animation = 0;
+        
       //  cout << "sent animation " << send << "\n\n\n\n\n\n";
+        size += sizeof(int);
+        memcpy(&sendbuffer[size],(void*)&otherPlayer, sizeof(int));
         pthread_mutex_unlock(&an_mutex);
         size += sizeof(int);        
         client->sendMessage(sendbuffer, size);
@@ -1264,10 +1205,15 @@ void Game::update()
             }
 
             memcpy(&tp,&recvbuffer[sizeread], sizeof(struct timeval));
-            sizeread += sizeof(struct timeval);    
-            memcpy(&pos, &recvbuffer[sizeread], sizeof(Vector3));
-            //cout << pos << "\n";
-            sizeread += sizeof(Vector3);
+            sizeread += sizeof(struct timeval);  
+            int x,z;
+            memcpy(&x,&recvbuffer[sizeread], sizeof(int));
+            sizeread += sizeof(int);
+            memcpy(&z,&recvbuffer[sizeread], sizeof(int));
+            sizeread += sizeof(int);
+            pos.x = x;
+            pos.y = 100.5;
+            pos.z = z;    
             memcpy(&orient, &recvbuffer[sizeread], sizeof(Quaternion));
             //cout << orient << "\n";
             sizeread += sizeof(Quaternion);
@@ -1275,30 +1221,30 @@ void Game::update()
             sizeread += sizeof(int);
             //cout << an << "\n";
             
-              class update newUpd;
-              newUpd.id = pid;
-              newUpd.pos = pos;
-              newUpd.orient = orient;
-           newUpd.timestamp = tp;
-           newUpd.an = an;
+               class update newUpd;
+               newUpd.id = pid;
+               newUpd.pos = pos;
+               newUpd.orient = orient;
+            newUpd.timestamp = tp;
+            newUpd.an = an;
            
-           struct timeval now;
-           gettimeofday(&now, NULL); 
-           struct timeval delay;
-           delay.tv_sec = delay_ms/1000;
-           delay.tv_usec = (delay_ms % 1000) *1000;
+            struct timeval now;
+            gettimeofday(&now, NULL); 
+            struct timeval delay;
+            delay.tv_sec = delay_ms/1000;
+            delay.tv_usec = (delay_ms % 1000) *1000;
           /* cout << "now secs before: " << now.tv_sec << "\n";
            cout << "now usecs before: " << now.tv_usec << "\n";
            cout << delay.tv_usec << ": delay microseconds\n";*/
-           timeradd(&now, &delay, &newUpd.delayed);
+            timeradd(&now, &delay, &newUpd.delayed);
           /* cout << "delayed secs after: " << newUpd.delayed.tv_sec << "\n";
            cout << "delayed usecs after: " << newUpd.delayed.tv_usec << "\n";*/
-           pthread_mutex_lock(&updatesqueue_mutex);    
-           updates.push_back(newUpd);  
-              pthread_mutex_unlock(&updatesqueue_mutex);
-           pthread_cond_signal(&delay_start);
+            pthread_mutex_lock(&updatesqueue_mutex);   
+            updates.push_back(newUpd); 
+               pthread_mutex_unlock(&updatesqueue_mutex);
+            pthread_cond_signal(&delay_start);
                       
-       }
+        }
        
     }
 }
@@ -1487,17 +1433,7 @@ bool Game::isNewPlayer(int id)
     return true;
 }
 //-------------------------------------------------------------------------------------
-bool Game::canWave()
-{
-    if (nextWave.tv_sec == 0)
-        return true;
-    struct timeval now;
-    gettimeofday(&now, NULL);
-    if (timercmp(&nextWave, &now, >))
-        return false;
-    else
-        return true;
-}
+
 void Game::setPlayerStateUpdate()
 {
     pthread_mutex_lock(&players_mutex); 
@@ -1563,41 +1499,41 @@ void Game::setPlayerStateUpdate()
         {
             case 1:
                // cout << "idle\n";
-                processActionByPlayerID("idle", newUpdate.id);
+                processActionByPlayerID("idle", newUpdate.id, false);
                 break;
 
             case 2:
                // cout << "walk\n";
-                processActionByPlayerID("walk", newUpdate.id);
+                processActionByPlayerID("walk", newUpdate.id, false);
                 break;
 
             case 3:
                 cout << "wave\n";
-                processActionByPlayerID("wave", newUpdate.id);
+                processActionByPlayerID("wave", newUpdate.id, true);
                 break;
 
             case 4:
                 cout << "nod\n";
-                processActionByPlayerID("nod", newUpdate.id);
+                processActionByPlayerID("nod", newUpdate.id, false);
                 break;
 
             case 5:
                 cout << "punch\n";
-                processActionByPlayerID("punch", newUpdate.id);
+                processActionByPlayerID("punch", newUpdate.id, true);
                 break;
 
             case 6:
                 cout << "headshake\n";
-                processActionByPlayerID("headshake", newUpdate.id);
+                processActionByPlayerID("headshake", newUpdate.id, false);
                 break;
 
             case 7:
                 cout << "handshake\n";
-                processActionByPlayerID("handshake", newUpdate.id);
+                processActionByPlayerID("handshake", newUpdate.id, true);
                 break;
             case 8:
                 cout << "rps\n";
-                processActionByPlayerID("rockpaperscissors", newUpdate.id);
+                processActionByPlayerID("rockpaperscissors", newUpdate.id, true);
 
             default:
                 break;
@@ -1760,27 +1696,12 @@ void Game::printPlayerScore()
 
 void Game::printStepsLeft()
 {
-    std::string caption = "Steps left:";
-
-    Player *current;
-    for (current = localPlayer->next; current != NULL; current = current->next)
-    { 
-        if (current->stepsLeft > 0)
-        {
-            Real distance = localPlayer->playerNode->_getDerivedPosition().distance(
-                current->playerNode->_getDerivedPosition());
-            if (distance < STEPS)
-                if (distance < current->stepsLeft)
-                    current->stepsLeft = STEPS - distance;
-            else
-                current->stepsLeft = 0;
-            if (current->stepsLeft < 50)
-                current->stepsLeft = 0;
+    std::string caption = "Steps left: ";
 
-        }
-        caption = caption + " P" + StringConverter::toString(current->id) + 
-            ":" + StringConverter::toString(current->stepsLeft);
-    }
+    Real distance = localPlayer->lastActionLocation.distance(localPlayer->playerNode->_getDerivedPosition());
+    localPlayer->stepsLeft = STEPS - distance;
+    caption = caption + StringConverter::toString(localPlayer->stepsLeft);
+            
     stepsBoard->setCaption(caption);
 
 }
@@ -1801,21 +1722,21 @@ void Game::registerScore(Player* playerA, Player* playerB, int action)
 
         playerB->dead = true;
     }
-    if (action == 8)
+    
+   
+    if (playerA == localPlayer)
     {
-        if (playerA == localPlayer)
-            playerB->stepsLeft = STEPS+500;
-        else
-            playerA->stepsLeft = STEPS+500; 
+        playerA->lastActionLocation = localPlayer->playerNode->_getDerivedPosition();
+        playerA->stepsLeft = STEPS;
     }
-    else
+       
+    else if (playerB == localPlayer)
     {
-        if (playerA == localPlayer)
-            playerB->stepsLeft = STEPS;
-        else
-            playerA->stepsLeft = STEPS;
+        playerB->lastActionLocation = localPlayer->playerNode->_getDerivedPosition();
+        playerB->stepsLeft = STEPS;
     }
-    
+     
+        
     printPlayerScore();
 
 }
index 7c08d3e3de6b0ba0442e41a568ba7c8ace19844e..319b98fd50a6ce2dd9aa0a841a20fba27d786f24 100644 (file)
@@ -81,16 +81,15 @@ protected:
     virtual bool keyReleased(const OIS::KeyEvent &arg);
 
     void init();
-    void processActionByPlayerID(const String &an, int id);
-    bool processAction(const String &an, Player* player);
-    void updateActionInfo(const String &an);
+    void processActionByPlayerID(const String &an, int id, bool scoreAction);
+    bool processAction(const String &an, Player* player, bool scoreAction);
+    void updateActionInfo(const String &an, int otherplayer);
     void sendUpdate();
     void update();
     void delay();
     void createPlayer(int id);
     void deletePlayer(int id, bool dead);
     bool isNewPlayer(int id);
-    bool canWave();
     void setPlayerStateUpdate();
     Player* getPlayer(int id);
     void restoreCameraPosition(const FrameEvent &evt);
@@ -141,6 +140,7 @@ protected:
     Vector3 position;
     Quaternion orientation;
     int animation;
+    int otherPlayer;
        
     char recvbuffer[BUFFERSIZE]; 
     char sendbuffer[BUFFERSIZE]; 
@@ -151,9 +151,7 @@ protected:
     Vector3 cameraPosNew;    
     Quaternion cameraOrientNew;
 
-    struct timeval nextWave;
-
-
     bool restoreCamera;
     bool adjustCamera;
     bool cameraTimerOn;
index 835c4438bc9d53e82b39b31a78bbce45e4de7ed7..66c5b2ff17b3dfe17579200b598da1241b99d297 100644 (file)
@@ -124,10 +124,6 @@ int PredictedState::getDirection(Vector3 curPos)
 
 int PredictedState::HandshakePosition(Player* player1, Player* player2)
 {
-    if ((player1 == localPlayer && player2->stepsLeft > 0) ||
-        (player2 == localPlayer && player1->stepsLeft > 0))
-        return 2;
-
     Ogre::Real distance = getDistance(player1,player2);
     if (player1 == localPlayer && distance > (HANDSHAKE_ALLOWED_DISTANCE - 50))
         return 0;
@@ -154,10 +150,6 @@ int PredictedState::HandshakePosition(Player* player1, Player* player2)
 }
 int PredictedState::RockPaperScissorsPosition(Player* player1, Player* player2)
 {
-    if ((player1 == localPlayer && player2->stepsLeft > 0) ||
-        (player2 == localPlayer && player1->stepsLeft > 0))
-        return 2;
-
     Ogre::Real distance = getDistance(player1,player2);
      
     if (player1 == localPlayer && distance > (RPS_ALLOWED_DISTANCE - 50))
@@ -194,10 +186,6 @@ int PredictedState::RockPaperScissorsPosition(Player* player1, Player* player2)
 }
 int PredictedState::PunchPosition(Player* player1, Player* player2)
 {
-    if ((player1 == localPlayer && player2->stepsLeft > 50) ||
-        (player2 == localPlayer && player1->stepsLeft > 50))
-        return 2;
-    
     Ogre::Real distance = getDistance(player1,player2);
     if (player1 == localPlayer && distance > (PUNCH_ALLOWED_DISTANCE - 50))
         return 0;