Changeset 5f868c0 in network-game


Ignore:
Timestamp:
May 22, 2013, 10:34:42 PM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
45b2750
Parents:
6e66ffd
Message:

Added partial server support for new messages for sending item info

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • common/Message.h

    r6e66ffd r5f868c0  
    22#define _MESSAGE_H
    33
    4 #define MSG_TYPE_REGISTER     1
    5 #define MSG_TYPE_LOGIN        2
    6 #define MSG_TYPE_LOGOUT       3
    7 #define MSG_TYPE_CHAT         4
    8 #define MSG_TYPE_PLAYER       5  // server sends this to update player positions
    9 #define MSG_TYPE_PLAYER_MOVE  6  // client sends this when a player wants to move
     4#define MSG_TYPE_REGISTER      1
     5#define MSG_TYPE_LOGIN         2
     6#define MSG_TYPE_LOGOUT        3
     7#define MSG_TYPE_CHAT          4
     8#define MSG_TYPE_PLAYER        5  // server sends this to update player positions
     9#define MSG_TYPE_PLAYER_MOVE   6  // client sends this when a player wants to move
     10#define MSG_TYPE_OBJECT        7
     11#define MSG_TYPE_REMOVE_OBJECT 8
    1012
    1113typedef struct
  • common/WorldMap.cpp

    r6e66ffd r5f868c0  
    66#include <sstream>
    77#include <cstdlib>
     8#include <cstring>
    89
    910using namespace std;
     
    6465}
    6566
     67vector<WorldMap::Object> WorldMap::getObjects() {
     68   return *vctObjects;
     69}
    6670vector<WorldMap::Object> WorldMap::getObjects(int x, int y) {
    6771   vector<WorldMap::Object> vctObjectsInRegion;
     
    7882// used by the server to create new objects
    7983void WorldMap::addObject(WorldMap::ObjectType t, int x, int y) {
    80    WorldMap::Object o(t, vctObjects->size(), x, y);
     84   WorldMap::Object o(vctObjects->size(), t, x, y);
    8185   vctObjects->push_back(o);
    8286}
     
    96100
    97101   if (!foundObject) {
    98       WorldMap::Object o(t, id, x, y);
     102      WorldMap::Object o(id, t, x, y);
    99103      vctObjects->push_back(o);
    100104   }
     105}
     106
     107bool WorldMap::removeObject(int id) {
     108   vector<WorldMap::Object>::iterator it;
     109
     110   for (it = vctObjects->begin(); it != vctObjects->end(); it++) {
     111      if (it->id == id) {
     112         vctObjects->erase(it);
     113         return true;
     114      }
     115   }
     116
     117   return false;  // no object with that id was found
    101118}
    102119
     
    240257/*** Functions for Object ***/
    241258
    242 WorldMap::Object::Object(ObjectType type, int id, int x, int y) {
     259WorldMap::Object::Object(int id, ObjectType type, int x, int y) {
    243260   this->type = type;
    244261   this->id = id;
     
    247264}
    248265
    249 WorldMap::Object::Object(ObjectType type, int id, POSITION pos) {
     266WorldMap::Object::Object(int id, ObjectType type, POSITION pos) {
    250267   this->type = type;
    251268   this->id = id;
     
    255272WorldMap::Object::~Object() {
    256273}
     274
     275void WorldMap::Object::serialize(char* buffer) {
     276   memcpy(buffer, &this->type, 4);
     277   memcpy(buffer+4, &this->id, 4);
     278   memcpy(buffer+8, &this->pos.x, 4);
     279   memcpy(buffer+12, &this->pos.y, 4);
     280}
     281
     282void WorldMap::Object::deserialize(char* buffer) {
     283   memcpy(&this->type, buffer, 4);
     284   memcpy(&this->id, buffer+4, 4);
     285   memcpy(&this->pos.x, buffer+8, 4);
     286   memcpy(&this->pos.y, buffer+12, 4);
     287}
  • common/WorldMap.h

    r6e66ffd r5f868c0  
    3737      POSITION pos;
    3838
    39       Object(ObjectType type, int id, int x, int y);
    40       Object(ObjectType type, int id, POSITION pos);
     39      Object(int id, ObjectType type, int x, int y);
     40      Object(int id, ObjectType type, POSITION pos);
    4141
    4242      ~Object();
     43
     44      void serialize(char* buffer);
     45      void deserialize(char* buffer);
    4346   };
    4447
     
    5861   void setStructure(int x, int y, StructureType type);
    5962
     63   vector<Object> getObjects();
    6064   vector<Object> getObjects(int x, int y);
     65
    6166   void addObject(ObjectType type, int x, int y);
    6267   void updateObject(int id, WorldMap::ObjectType t, int x, int y);
     68   bool removeObject(int id);
    6369
    6470   static WorldMap* createDefaultMap();
  • server/server.cpp

    r6e66ffd r5f868c0  
    9898   // add some items to the map. They will be sent out
    9999   // to players when they login
    100    // m->addObject(x*25+12, y*25+12, OBJECT_BLUE_FLAG);
    101    // m->addObject(x*25+12, y*25+12, OBJECT_RED_FLAG);
     100   for (int y=0; y<gameMap->height; y++) {
     101      for (int x=0; x<gameMap->width; x++) {
     102         switch (gameMap->getStructure(x, y)) {
     103            case WorldMap::STRUCTURE_BLUE_FLAG:
     104               gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
     105               break;
     106            case WorldMap::STRUCTURE_RED_FLAG:
     107               gameMap->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
     108               break;
     109         }
     110      }
     111   }
    102112
    103113   sock = socket(AF_INET, SOCK_DGRAM, 0);
     
    259269
    260270               cout << "sending info about " << it->second.name  << endl;
    261                cout << "sending ind " << it->second.id  << endl;
     271               cout << "sending id " << it->second.id  << endl;
     272               if ( sendMessage(&serverMsg, sock, &from) < 0 )
     273                  error("sendMessage");
     274            }
     275
     276            // tell the new player about all map objects
     277            // (currently just the flags)
     278            serverMsg.type = MSG_TYPE_OBJECT;
     279            vector<WorldMap::Object> vctObjects = gameMap->getObjects();
     280            vector<WorldMap::Object>::iterator itObjects;
     281            cout << "sending items" << endl;
     282            for (itObjects = vctObjects.begin(); itObjects != vctObjects.end(); itObjects++) {
     283               itObjects->serialize(serverMsg.buffer);
     284               cout << "sending item id " << itObjects->id  << endl;
    262285               if ( sendMessage(&serverMsg, sock, &from) < 0 )
    263286                  error("sendMessage");
Note: See TracChangeset for help on using the changeset viewer.