/*
 * PVVMUD a 3D MUD
 * Copyright (C) 1998-1999  Programvareverkstedet (pvv@pvv.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#ifndef _MESSAGE_H
#define _MESSAGE_H

#include "object.H"
#include "stream.H"
#include "position.H"
#include "direction.H"

#define MSGCURRPOS                 -1024

// Server client messages
#define MSG_LOGIN                      0
#define MSG_HELLO                      1
#define MSG_BYE                        2
#define MSG_GETWORLD                   3
#define MSG_GEOMETRYOBJECT             4
#define MSG_UPDATEPOSITION             5
#define MSG_UPDATEDIRECTION            6
#define MSG_COMMAND                    7
#define MSG_VIEWPOINT                  8
#define MSG_SELECTION                  9
#define MSG_ACTION                     10
#define MSG_ACTIONLIST                 11
#define MSG_MSG                        12
#define MSG_GEOOBJREMOVE               13
#define MSG_SERVERINFO                 14
#define MSG_PLAYERINFO                 15
#define MSG_PING                       16
#define MSG_PONG                       17

// Server messages
#define MSG_SERVERLOGIN                50

// GOS client messages
#define MSG_GOSREQUEST                100
#define MSG_GOSERROR                  101
#define MSG_GEOMETRY                  110
#define MSG_MATERIAL                  111
#define MSG_TEXTURE                   112

// Server Worldsrv messages 
//#define MSG_WORLDLOGIN              150
#define MSG_GOSINFO                   151
//#define MSG_SECTION                   152
//#define MSG_CELL                      153
//#define MSG_REMOVECELL                154
#define MSG_OBJECT                    155
#define MSG_REMOVEOBJECT              156
#define MSG_CHANGEMASTERCELL          157
#define MSG_PVCELL                    158
#define MSG_UPDATEHIERARCHY           159
#define MSG_ROTATE                    160


#define MESSAGE_OK                    1
#define MESSAGE_BLOCKING              2

class CManager;

// Hope this to constant isn't used any more. 19990317
// #define MESSAGE_MINLENGTH       0
// #define MESSAGE_MAXLENGTH       128

class CMessage: public CObject {
  long length;    // Message length or Header length when variable length message.
  CManager * manager;
  long currpos;   // Current position for writing and  reading.
  BYTE * message;
  long msglength; // Length of message buffer. 
  long writeIndex; // Current write index in message buffer
  long readIndex; // Current read index in message buffer
protected:
  void reallocMessage(long size);
 
  // Write functions
  void writeByte(long index,BYTE byte);
  void writeWord(long index,WORD word);
  void writeDWord(long index,DWORD dword);
  void writeDouble(long index,double value);
  void writeBuf(long index,BYTE * buf,long size);
  void writeString(long index,char * str);
  void writePosition(long index,const CPosition & position);
  void writeDirection(long index,const CDirection & direction);

  // Read functions
  BYTE readByte(long index);
  WORD readWord(long index);
  DWORD readDWord(long index);
  double readDouble(long index);
  void readBuf(long index,BYTE * buf,long size);
  char * readString(long index);
  CPosition readPosition(long index);
  CDirection readDirection(long index);
  int reciveMessage(CStream * stream,long index, long size);
  int sendMessage(CStream * stream,long index, long size);
  long getWriteIndex();
  long getReadIndex();
  virtual int reciveHead(CStream * stream);
  virtual int isVarLength() { return FALSE; }
  CManager * getManager(){ return manager; }
public:
  CMessage(BYTE id, long length);
  ~CMessage();
  static CMessage * createMsg();
  virtual int recive(CStream * stream);
  virtual void prepareSend(){ readIndex = 0; }
  virtual int send(CStream * stream);
  virtual int executeMsg(){ return FALSE; }
  void setManager(CManager * manager){this->manager = manager;}
  virtual BYTE getId();
  virtual long getLength();
};

#endif // _MESSAGE_H