Changeset ce2bb87 in network-game


Ignore:
Timestamp:
Dec 20, 2013, 3:42:30 AM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
b73bc28
Parents:
6c9bcdd
Message:

A player can pick up an opponent's flag in an individual game

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • common/Game.cpp

    r6c9bcdd rce2bb87  
    11#include "Game.h"
     2
     3#include "Common.h"
    24
    35using namespace std;
     
    112114}
    113115
     116// returns the id of the picked-up flag or -1 if none was picked up
     117int Game::processFlagPickupRequest(Player* p) {
     118   vector<WorldMap::Object>* vctObjects = this->worldMap->getObjects();
     119   vector<WorldMap::Object>::iterator it;
     120   int playerId = -1;
     121
     122   for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
     123      if (posDistance(p->pos, it->pos.toFloat()) < 10) {
     124         switch (it->type) {
     125            case WorldMap::OBJECT_BLUE_FLAG:
     126               if (p->team == 1) {
     127                  p->hasBlueFlag = true;
     128                  playerId = it->id;
     129               }
     130               break;
     131            case WorldMap::OBJECT_RED_FLAG:
     132               if (p->team == 0) {
     133                  p->hasRedFlag = true;
     134                  playerId = it->id;
     135               }
     136               break;
     137         }
     138
     139         if (playerId > -1) {
     140            vctObjects->erase(it);
     141            return playerId;
     142         }
     143      }
     144   }
     145
     146   return playerId;
     147}
     148
    114149void Game::setRedScore(int score) {
    115150   this->redScore = score;
  • common/Game.h

    r6c9bcdd rce2bb87  
    4343   bool startPlayerMovement(unsigned int id, int x, int y);
    4444   bool processPlayerMovement(Player* p, FLOAT_POSITION oldPos);
     45   int processFlagPickupRequest(Player* p);
    4546
    4647   void setBlueScore(int score);
  • server/server.cpp

    r6c9bcdd rce2bb87  
    153153
    154154         map<unsigned int, Player*>::iterator it;
     155
     156         cout << "Updating player targets and respawning dead players" << endl;
    155157
    156158         // set targets for all chasing players (or make them attack if they're close enough)
     
    216218         }
    217219
     220         cout << "Processing players in a game" << endl;
     221
    218222         // process players currently in a game
    219223         FLOAT_POSITION oldPos;
    220224         for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    221225         {
     226            if (it->second->currentGame == NULL)
     227               continue;
     228
     229            cout << "moving player" << endl;
    222230            bool broadcastMove = false;
    223231
     
    226234            if (it->second->move(it->second->currentGame->getMap())) {
    227235
     236               cout << "player moved" << endl;
    228237               if (it->second->currentGame->processPlayerMovement(it->second, oldPos))
    229238                   broadcastMove = true;
     239               cout << "player move processed" << endl;
    230240
    231241               /*
     
    386396               if (broadcastMove)
    387397               {
     398                  cout << "broadcasting player move" << endl;
    388399                  serverMsg.type = MSG_TYPE_PLAYER;
    389400                  it->second->serialize(serverMsg.buffer);
     
    394405                  map<unsigned int, Player*> playersInGame = it->second->currentGame->getPlayers();
    395406                  map<unsigned int, Player*>::iterator it2;
    396                   for (it2 = playersInGame.begin(); it2 != playersInGamePlayers.end(); it2++)
     407                  for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
    397408                  {
    398409                     if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
    399410                        error("sendMessage");
    400411                  }
    401                }
    402             }
     412                  cout << "done broadcasting player move" << endl;
     413               }
     414            }
     415
     416            cout << "processing player attack" << endl;
    403417
    404418            // check if the player's attack animation is complete
     
    476490            }
    477491         }
     492
     493         cout << "Processing projectiles"  << endl;
    478494
    479495         // move all projectiles
     
    540556            cout << "Should be broadcasting the message" << endl;
    541557
     558            // needs to be updated to use the players from the game
    542559            map<unsigned int, Player*>::iterator it;
    543560            for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
     
    555572               error("sendMessage");
    556573         }
     574
     575         cout << "Finished processing the message" << endl;
    557576      }
    558577   }
     
    651670            cout << "new player id: " << p->id << endl;
    652671            p->setAddr(from);
     672            p->currentGame = NULL;
    653673
    654674            // choose a random team (either 0 or 1)
     
    835855
    836856         Player* p = mapPlayers[id];
    837 
    838          vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
    839          vector<WorldMap::Object>::iterator itObjects;
    840 
    841          for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
    842             POSITION pos = itObjects->pos;
    843             bool gotFlag = false;
    844 
    845             if (posDistance(p->pos, pos.toFloat()) < 10) {
    846                switch (itObjects->type) {
    847                   case WorldMap::OBJECT_BLUE_FLAG:
    848                      if (p->team == 1) {
    849                         gotFlag = true;
    850                         p->hasBlueFlag = true;
    851                         broadcastResponse = true;
    852                      }
    853                      break;
    854                   case WorldMap::OBJECT_RED_FLAG:
    855                      if (p->team == 0) {
    856                         gotFlag = true;
    857                         p->hasRedFlag = true;
    858                         broadcastResponse = true;
    859                      }
    860                      break;
    861                }
    862 
    863                if (gotFlag) {
    864                   serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
    865                   memcpy(serverMsg.buffer, &itObjects->id, 4);
    866 
    867                   map<unsigned int, Player*>::iterator it;
    868                   for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    869                   {
    870                      if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
    871                         error("sendMessage");
    872                   }
    873 
    874                   // remove the object from the server-side map
    875                   cout << "size before: " << gameMap->getObjects()->size() << endl;
    876                   itObjects = vctObjects->erase(itObjects);
    877                   cout << "size after: " << gameMap->getObjects()->size() << endl;
    878                }
    879             }
    880 
    881             if (!gotFlag)
    882                itObjects++;
    883          }
    884 
     857         int objectId = p->currentGame->processFlagPickupRequest(p);
     858
     859         if (objectId >= 0) {
     860            serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
     861            memcpy(serverMsg.buffer, &objectId, 4);
     862
     863            map<unsigned int, Player*> players = p->currentGame->getPlayers();
     864            map<unsigned int, Player*>::iterator it;
     865            for (it = players.begin(); it != players.end(); it++)
     866            {
     867               if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
     868                  error("sendMessage");
     869            }
     870
     871         }
     872
     873         // if there was no flag to pickup, we really don't need to send a message
    885874         serverMsg.type = MSG_TYPE_PLAYER;
    886875         p->serialize(serverMsg.buffer);
Note: See TracChangeset for help on using the changeset viewer.