/*
 * 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
 *
 */
#include "pvvmud.H"
#include "srvobject.H"
#include "srvworldmanager.H"
#include "srvlistenerlist.H"
#include "mud.H"


CSrvObject::CSrvObject(DWORD objectId,CWorldHierarchy * parent, DWORD geometryId,const CPosition & position, const CDirection & direction )
  :CWorldObject(objectId,parent,geometryId,position,direction){

  m_geometry = NULL;

}

CSrvObject::~CSrvObject(){
}

CListener * CSrvObject::getListener(){
  return ((CSrvWorld*)getWorld())->getListenerList();
}

CGeometry * CSrvObject::getGeometry(){
  if (m_geometry == NULL){
    m_geometry = getMud()->getGeometry(getGeometryId());
  }
  return m_geometry;
}

CMud * CSrvObject::getMud(){
  return ((CSrvWorld*)getWorld())->getMud();
}

CSrvObject * CSrvObject::createObject(char * objectName, DWORD geometryId, const CPosition & position, const CDirection & direction, char * name)
{

  CSrvObject * object = (CSrvObject*)CWorldObject::createObject(
    objectName,((CSrvWorld*)getWorld())->getNewObjectId(),geometryId,
    position,direction);

  object->setName(name);
  return object;
}

// Include this in CMovingObject 
void CSrvObject::updatePosition( const CPosition & position ){

  cdebug << "CSrvObject::updatePosition " << position << endl;

  CSrvObject * masterCell = (CSrvObject*)getMasterCell();
  CPosition newpos = position;

  if ((masterCell != NULL) && (!masterCell->checkPosition(position))){
    cdebug << "Object " << getName() << " is outside boundary of " 
         << masterCell->getName() << "!\n";
    CCellPVS * cellPVS = masterCell->getPVS();
    CSrvObject * newCell;
    // Find new cell and new position. If this fails return.
    if ((newCell = (CSrvObject*)cellPVS->findCell(&newpos))== NULL) return;
    moveTo(newCell);
  } 

  CWorldObject::updatePosition(newpos);
}

/* void CSrvObject::sendCell( CSrvWorldManager * listener ){
  listener->sendAddCell( this );
  CObjectListItem * item = getFirst();
  while (item != NULL){
    CSrvObject * object = (CSrvObject*)item->getObject();
    item = item->getNext();

    object->sendObject( listener );
  }
}
*/

void CSrvObject::sendObject( CSrvWorldManager * listener ){
  listener->sendAddObject( this );
  CObjectListItem * item = getFirst();
  while (item != NULL){
    CSrvObject * object = (CSrvObject*)item->getObject();
    item = item->getNext();
    object->sendObject( listener );
  }
}


CDoubleArray * CSrvObject::getDistances(const CBeam & beam, double max, double min){
  CDoubleArray * result = new CDoubleArray();
  distances(*result,beam,max,min);

  CCellPVS * pvs = getPVS();
  CObjectListItem * item = pvs->getFirstPVCell();
  while (item != NULL){
    CPVCell * pv = (CPVCell*)item->getObject();

    int cellId = pv->getCellId();

    CSrvObject * cell = (CSrvObject*)getWorld()->getObject(cellId);
    if (cell != NULL){
      CBeam pvBeam = beam + pv->getPosition();
      cell->distances(*result,pvBeam,max,min);
    }

    item = item->getNext();
  }

  return result;
}