/*
 * 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 _MUD_H
#define _MUD_H

#include "srvclimanager.H"
#include "srvclisrvsocket.H"
#include "srvgosmanager.H"
#include "srvgossrvsocket.H"
#include "srvworldmanager.H"
#include "srvworldsrvsocket.H"
#include "timekeeper.H"
#include "inetaddress.H"
#include "srvworld.H"
#include "srvgos.H"
#include "srvgeocache.H"
#include "option.H"
#include "userdb.H"

class CMud: public CTimeKeeperItem {

  COption * m_option;
  CSrvWorld * m_world;
  CUserDB * m_userDB;

  CTimeKeeper * m_timeKeeper;

  CMudSrvSocket * m_clientSocket;
  CObjectList * m_clientList;
  int m_markedClient;

  CSrvGOSSrvSocket * m_GOSSocket;
  CObjectList * m_gosList;
  int m_markedGOS;

  CSrvWorldSrvSocket * m_worldSocket;
  CObjectList * m_worldList;
  int m_markedWorld;

  CSrvGOS * m_gos;
  CSrvGEOCache * m_geoCache;

  unsigned long m_mudTime;          // Time in tics

 public:  
  CMud(COption *option);
  virtual ~CMud();

  CSrvWorld * getWorld();
  COption * getOption();
  CUserDB * getUserDB();


  void addClient(CClientManager * client);
  void removeClient(CClientManager * client);
  void markClient(){ m_markedClient = TRUE; }
  void checkMarkedClient();

  void addGOS(CSrvGOSManager * gos);
  void removeGOS(CSrvGOSManager * gos);
  void markGOS(){ m_markedGOS = TRUE; }
  void checkMarkedGOS();
  CInetAddress * getGOSAddress();
  int freeGOSAddress(CInetAddress * address);

  void addWorld(CSrvWorldManager * worldSrv);
  void removeWorld(CSrvWorldManager * worldSrv);
  void markWorld(){ m_markedWorld = TRUE; }
  void checkMarkedWorld();
  CInetAddress * getWorldSrvAddress();
  int freeWorldAddress(CInetAddress * address);

 private:
  virtual int timeKeeperHB();
  void loadWorld();

 public:
//  virtual void createOldWorld(CWorld * world);
  virtual CSrvWorld * newWorld();
  virtual void createWorld(CSrvWorld * world);
  void run();
  void stop();

  // GOS functions
  void request(int requestType,int requestId);
  void error();
  void geometry(CGeometry * geometry);
  void material(CMaterial * material);
  void texture(CTexture * texture);

  // Cache functions
  CGeometry * getGeometry(DWORD geometryId);
  void chatMessage(const char * message);
  
};

#endif /* _MUD_H */