/*****************************************************************************
Name            :   sclist.cpp 
Title           :   Scene List
C Author        :   Stel Michael
Created         :   9 Sept 1993

Copyright       :   1992 by VideoLogic Limited. All rights reserved.
		    No part of this software, either material or conceptual
		    may be copied or distributed, transmitted, transcribed,
		    stored in a retrieval system or translated into any
		    human or computer language in any form by any means,
		    electronic, mechanical, manual or other-wise, or
		    disclosed to third parties without the express written
		    permission of VideoLogic Limited, HomePark Estate,
		    King's Langley, Hertfordshire, WD4 8LZ, U.K.

Description     :   Scene list operations
Program Type    :   Microsoft Visual C++ V2
*****************************************************************************/

#define Cplusplus 

#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include "sgl.h"
#include "math.h"   
#include "sclist.h"      


BasicObject::BasicObject(int trm,int grp)
{                  
	id=BASOBJ;
	tran = trm;
	group=grp;
	status=ALIVE;                 
	
	pos[0]=0.0f;
	pos[1]=0.0f;
	pos[2]=0.0f;
}
      
                
void BasicObject::setPos(float xp,float yp,float zp)
{
	pos[0]=xp;
	pos[1]=yp;
	pos[2]=zp;
}                
                
                   
void BasicObject::Process()
{   
	if (status==HITIT)
		status=DEAD;
                        
	sgl_modify_transform(tran, TRUE);	
	sgl_translate(pos[0],pos[1],pos[2]);
}


SceneList::SceneList()
{                    
	SceneNode *SN;
	SN=new SceneNode;
	SN->SceneItem=NULL;
	SN->Next=NULL;
	Scene=NULL;
}


SceneList::~SceneList()
{
	while (Scene !=NULL)
	{
		SceneNode *SN = Scene;
		delete(SN->SceneItem);
		Scene = SN->Next;
		delete SN;
	}     
}    


void SceneList::AddObject(BasicObject *NewObject)
{
	SceneNode *sN;   
	
	sN = new SceneNode;
	
	sN->SceneItem=NewObject;
	sN->Next=Scene;
	Scene= sN;
}	


void SceneList::RemoveObjects()
{
	int	ObStat;
	SceneNode *CurObject=Scene;	
	SceneNode *ptrLast=Scene;
		
	while (CurObject!=NULL)
	{
	    ObStat=CurObject->SceneItem->GetStatus();
	
	    if (ObStat==DEAD)                   
	    {	 
	    	sgl_delete_list(CurObject->SceneItem->GetGroup());
            
	    	delete(CurObject->SceneItem); 
	    	
	    	if (CurObject==Scene)   
	    	{
	    		Scene=CurObject->Next;  
	    		ptrLast=Scene;
	    		delete(CurObject);
	    		CurObject=Scene;
	    	}	
	    	else         
	    	{
	    		ptrLast->Next=CurObject->Next;	    		
	    		delete(CurObject);                
	    		CurObject=ptrLast->Next;
	    	}
	    }         
	    else  
	    {
	 		ptrLast=CurObject;	    
			CurObject=CurObject->Next;  
		}                   
		
	}
}


void  SceneList::ProcessScene()    
{        
	int	ObStat;
	SceneNode *CurObject=Scene;
	
	while (CurObject!=NULL)
	{
	    ObStat=CurObject->SceneItem->GetStatus();
	
	    if (ObStat!=DORMANT)                   
	    {	   
		 	CurObject->SceneItem->Process();
	    }
		CurObject=CurObject->Next;
	}
}	


int  SceneList::SphereTest(float range,float pnt[3])    
{        
	SceneNode *CurObject=Scene;               
	float xd,yd,zd,dd;
	int ahit=0;
	
	while (CurObject!=NULL)
	{
	    xd=CurObject->SceneItem->pos[0]-pnt[0];
	    yd=CurObject->SceneItem->pos[1]-pnt[1];
		zd=CurObject->SceneItem->pos[2]-pnt[2];
        
        dd= (float) sqrt(xd*xd+yd*yd+zd*zd);
        
        if (dd<range)   
        {
        	CurObject->SceneItem->SetStatus(HITIT);  
        	ahit=1;
        }
		CurObject=CurObject->Next;
	}    
	
	return ahit;
}	
 
