Files
2025-03-05 08:37:43 +01:00

141 lines
3.7 KiB
C

/*
* 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 "worldhierarchy.H"
#include "worldworld.H"
#include "wacreateobject.H"
#include "waremoveobject.H"
#include "waupdatehierarchy.H"
CWorldHierarchy::CWorldHierarchy(DWORD objectId, CWorldHierarchy * parent){
m_objectId = objectId;
m_parent = parent;
if ((m_objectId != ID_UNKNOWN) && (m_parent == NULL)){
cdebug << "ERROR: Have to have a parent!\n";
void (*a)() = NULL;
a();
}
}
CWorldHierarchy::~CWorldHierarchy(){
if (m_parent != NULL)
cdebug << "WARNING CWorldHierarchy::~CWorldHierarchy "
<< "Parent not NULL!" << endl;
if (getNumItems() > 0)
cdebug << "WARNING CWorldHierarchy::~CWorldHierarchy "
<< "Subobject not removed!" << endl;
}
DWORD CWorldHierarchy::getObjectId(){
return m_objectId;
}
CWorldHierarchy * CWorldHierarchy::getParent(){
return m_parent;
}
DWORD CWorldHierarchy::getParentId(){
if (m_parent == NULL) return ID_UNKNOWN;
return m_parent->getObjectId();
}
CWorldWorld * CWorldHierarchy::getWorld(){
return m_parent->getWorld();
}
void CWorldHierarchy::_addObject(CWorldObject * object){
addLast((CObject*)object);
}
void CWorldHierarchy::_removeObject(CWorldObject * object){
remove((CObject*)object);
}
void CWorldHierarchy::_setParent(CWorldHierarchy * parent){
// Unlink from old parent
if (m_parent != NULL){
m_parent->_removeObject((CWorldObject*)this);
}
m_parent = parent;
// Link to new parent
if (m_parent != NULL){
m_parent->_addObject((CWorldObject*)this);
}
}
void CWorldHierarchy::addObject(CWorldObject * object){
CWorldWorld * world = getWorld();
world->addAnimation(new CWACreateObject(object), this );
}
void CWorldHierarchy::removeObject(CWorldObject * object){
CWorldWorld * world = getWorld();
world->addAnimation(new CWARemoveObject(object->getObjectId(),
object->getParentId()), this );
}
void CWorldHierarchy::setParent(CWorldObject * parent){
CWorldWorld * world = getWorld();
world->addAnimation(new CWAUpdateHierarchy(getObjectId(),
parent->getObjectId()), this );
}
CWorldObject * CWorldHierarchy::getObject( DWORD objectId ){
// Check if this is the object!
if (objectId == getObjectId()) return (CWorldObject*)this;
// Check child objects
CObjectListItem * item = getFirst();
while (item != NULL){
CWorldObject * object = (CWorldObject*)item->getObject();
if ((object = object->getObject(objectId)) != NULL) return object;
item = item->getNext();
}
return NULL;
}
CWorldObject * CWorldHierarchy::getObject( char * name ){
// Check if this is the object!
if (getName() != NULL && strcmp(name,getName()) == 0) return (CWorldObject*)this;
// Check child objects
CObjectListItem * item = getFirst();
while (item != NULL){
CWorldObject * object = (CWorldObject*)item->getObject();
if ((object = object->getObject(name)) != NULL) return object;
item = item->getNext();
}
return NULL;
}