Changeset 5f868c0 in network-game for common


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

Location:
common
Files:
3 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();
Note: See TracChangeset for help on using the changeset viewer.