Files
pvvmud/common/lib/world/worldworld.C
2025-03-05 08:37:43 +01:00

188 lines
5.2 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 <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include "pvvmud.H"
#include "worldworld.H"
CWorldWorld::CWorldWorld():CWorldHierarchy(ID_UNKNOWN,NULL){
setName("World");
m_animationList = new CWorldAnimationList();
m_startTimeSec = time(NULL);
m_timeOffset = 0;
}
CWorldWorld::~CWorldWorld(){
delete m_animationList;
clearWorld();
}
CWorldWorld * CWorldWorld::getWorld(){
return this;
}
CWorldAnimationList * CWorldWorld::getAnimationList(){
return m_animationList;
}
/////////////////////////////////////////////////////////////
// getTime
// Return time in millisecound sins world startup
//
DWORD CWorldWorld::getTime(){
struct timeval tv;
gettimeofday(&tv,NULL);
return (tv.tv_sec - m_startTimeSec)*1000 + tv.tv_usec/1000 - m_timeOffset;
}
void CWorldWorld::fixTime(long deltaTime){
if (deltaTime < 0){
if (labs(deltaTime) > m_timeOffset){
DWORD sec = (-deltaTime)/1000 + 1;
m_startTimeSec -= sec;
deltaTime += sec*1000;
m_timeOffset += deltaTime;
} else m_timeOffset = m_timeOffset - (DWORD)labs(deltaTime);
} else m_timeOffset += deltaTime;
while (m_timeOffset >= 1000){
m_timeOffset -= 1000;
m_startTimeSec++;
}
}
void CWorldWorld::clearWorld(){
m_animationList->clearList();
deleteAll();
}
CWorldObject * CWorldWorld::createObject( DWORD parentId, char * objectName,
DWORD objectId, DWORD geometryId, const CPosition & position,
const CDirection & direction )
{
CWorldObject * object;
if (parentId == -1){
object = newObject(objectName,objectId,NULL,geometryId,position,direction);
CWorldHierarchy::addObject( object );
} else {
CWorldObject * parent = getObject( parentId );
if (parent == NULL) return NULL;
object = parent->createObject( objectName, objectId, geometryId, position, direction );
}
return object;
}
void CWorldWorld::removeObject( DWORD objectId, DWORD parentId ){
if (parentId == -1){
CWorldObject * cell = getObject( objectId );
if (cell != NULL) CWorldHierarchy::removeObject( cell );
} else {
CWorldObject * parent = getObject( parentId );
if (parent == NULL){
cdebug << "WARNING: Failed to remove object " << objectId
<< " Failed to find parent : " << parentId << endl;
return;
}
CWorldObject * object = parent->getObject( objectId );
if (object == NULL){
cdebug << "WARNING: Failed to remove object( " << objectId << ") : Did not find object in parent : " << parentId << "\n";
return;
}
parent->removeObject(object);
}
}
CPVCell * CWorldWorld::addPVCell( DWORD cellId,
DWORD PVCellId, const CPosition & position ){
CWorldObject * cell = getObject(cellId);
if (cell == NULL) return NULL;
return cell->addPVCell(PVCellId,position);
}
void CWorldWorld::updatePosition( DWORD objectId, const CPosition & position ){
CWorldObject * object = getObject(objectId);
cdebug << "CWorldWorld::updatePosition\n";
if (object != NULL) object->updatePosition(position);
}
void CWorldWorld::updateDirection( DWORD objectId,const CDirection & direction){
CWorldObject * object = getObject(objectId);
cdebug << "CWorldWorld::updateDirection\n";
if (object != NULL) object->updateDirection(direction);
}
void CWorldWorld::rotate( DWORD objectId, const CDirection & angleSpeed ){
CWorldObject * object = getObject(objectId);
cdebug << "CWorldWorld::rotate\n";
if (object != NULL) object->rotate(angleSpeed);
}
void CWorldWorld::updateHierarchy( DWORD objectId, DWORD parentId ){
CWorldObject * object = getObject(objectId);
CWorldObject * parent = getObject(parentId);
if ((object != NULL) && (parent != NULL)){
cdebug << "CWorldWorld::updateHierarchy: OK\n";
object->moveTo( parent );
} else {
cdebug << "CWorldWorld::updateHierarchy: Failed\n";
}
}
void CWorldWorld::addAnimation( CWorldAnimation * animation, CWorldHierarchy * object ){
animation->setWorld( this );
CListener * listener = object->getListener();
if (listener != NULL) listener->sendAnimation( animation );
m_animationList->add( animation , getTime() );
}
void CWorldWorld::animate(){
// cdebug << "Animate" << endl;
m_animationList->animate( getTime() );
}
void CWorldWorld::dump(){
cdebug << "World:\n";
CObjectListItem * item = getFirst();
while (item != NULL){
CWorldObject * cell = (CWorldObject*)item->getObject();
if (cell != NULL) cell->dump(1);
item = item->getNext();
}
}