Sources/CryGame C++/Solution1/CryGame/ScriptObjectWeaponClass.cpp
/*! Traces a ray to determine what and where an instant weapon hits
@param pObj ray-description-table (shooter, pos, angles, dir, distance)
@return target-description-table (objtype (0=entity, 1=stat-obj, 2=terrain), pos, normal, dir, target (nil if objtype!=0))
*/
Traces a ray to determine what and where an instant weapon hits
Traces a ray to determine what and where an instant weapon hits
int CScriptObjectWeaponClass::GetInstantHit(IFunctionHandler *pH)
{
	CHECK_PARAMETERS(1);
	_SmartScriptObject pObj(m_pScriptSystem,true);
	_SmartScriptObject pTempObj(m_pScriptSystem,true);
	CScriptObjectVector oVec(m_pScriptSystem,true);
	CScriptObjectVector oVec1(m_pScriptSystem,false);
	CScriptObjectVector oVec2(m_pScriptSystem,false);
	CScriptObjectVector oVec3(m_pScriptSystem,false);
	IEntity *shooter;
	int nID;
	Vec3d pos, angles, dir;
	float fDistance;
	int res;
	pH->GetParam(1,*pObj);
	pObj->GetValue( "shooter",*pTempObj );
	pTempObj->GetValue("id",nID);
	shooter= m_pSystem->GetIEntitySystem()->GetEntity(nID);
	if(shooter==NULL)
	{
		TRACE("CScriptObjectWeapon::GetInstantHit() shooter in nil");
		return pH->EndFunctionNull();
	}
	pObj->GetValue( "pos", *oVec );
	pos=oVec.Get();
	pObj->GetValue( "angles", *oVec );
	angles=oVec.Get();
	pObj->GetValue( "dir", *oVec );
	dir=oVec.Get();
	pObj->GetValue( "distance", fDistance );
	dir*=fDistance;
	IPhysicalEntity *skip=shooter->GetPhysics();
	ray_hit hits[MAX_HITS];
	res=m_pSystem->GetIPhysicalWorld()->RayWorldIntersection(pos,dir, ent_all, 0,hits,MAX_HITS, skip);			
	if(res){
		_SmartScriptObject pTable(m_pScriptSystem);
		for(int nCount=0;nCount<MAX_HITS;nCount++)
		{
			_SmartScriptObject pOut(m_pScriptSystem);
			CScriptObjectVector oVec1(m_pScriptSystem);
			CScriptObjectVector oVec2(m_pScriptSystem);
			CScriptObjectVector oVec3(m_pScriptSystem);
			int objecttype;
			if(hits[nCount].dist<=0)
				continue;
			IEntity *centycontact = NULL;
			if (res && hits[nCount].dist>0 && hits[nCount].pCollider)
			{
				centycontact = (IEntity *)hits[nCount].pCollider->GetForeignData();
				if (centycontact && centycontact->IsGarbage())
				{
					res = 0;
				}
				if (centycontact && (!centycontact->IsGarbage()))
					objecttype = OT_ENTITY;
				else 
					objecttype = OT_STAT_OBJ;
			}
			if (hits[nCount].pCollider)
			{
				if (centycontact)
				{
					objecttype=0;	
					pOut->SetValue("target", centycontact->GetId());
				}
				else
				{
					objecttype=1;	
					pOut->SetToNull("target");
				}
			}else
			{
				objecttype=2; 
				pOut->SetToNull("target");
			}
			pOut->SetValue("objtype", objecttype);
			oVec1=(Vec3d)hits[nCount].pt;
			pOut->SetValue("pos", *oVec1);
			oVec2=(Vec3d)hits[nCount].n;
			pOut->SetValue("normal", *oVec2);
			oVec3=GetNormalized(dir);
			pOut->SetValue( "dir", *oVec3 );
			pOut->SetValue( "surfaceid", hits[nCount].surface_idx );
			pOut->SetValue( "partid", hits[nCount].partid);
			pOut->SetValue( "shooter",nID);
			pTable->SetAt(nCount,pOut);
			if (res && hits[0].dist>0) 
				break;       
		}
		return pH->EndFunction(pTable);
	}
	else{
		return pH->EndFunctionNull();
	}
}
int CScriptObjectWeaponClass :: GetInstantHit ( IFunctionHandler * pH ) { CHECK_PARAMETERS ( 1 ) ; _SmartScriptObject pObj ( m_pScriptSystem , true ) ; _SmartScriptObject pTempObj ( m_pScriptSystem , true ) ; CScriptObjectVector oVec ( m_pScriptSystem , true ) ; CScriptObjectVector oVec1 ( m_pScriptSystem , false ) ; CScriptObjectVector oVec2 ( m_pScriptSystem , false ) ; CScriptObjectVector oVec3 ( m_pScriptSystem , false ) ; IEntity * shooter ; int nID ; Vec3d pos , angles , dir ; float fDistance ; int res ; pH -> GetParam ( 1 , * pObj ) ; pObj -> GetValue ( " " , * pTempObj ) ; pTempObj -> GetValue ( " " , nID ) ; shooter = m_pSystem -> GetIEntitySystem ( ) -> GetEntity ( nID ) ; if ( shooter == NULL ) { TRACE ( " " ) ; return pH -> EndFunctionNull ( ) ; } pObj -> GetValue ( " " , * oVec ) ; pos = oVec . Get ( ) ; pObj -> GetValue ( " " , * oVec ) ; angles = oVec . Get ( ) ; pObj -> GetValue ( " " , * oVec ) ; dir = oVec . Get ( ) ; pObj -> GetValue ( " " , fDistance ) ; dir *= fDistance ; IPhysicalEntity * skip = shooter -> GetPhysics ( ) ; ray_hit hits [ MAX_HITS ] ; res = m_pSystem -> GetIPhysicalWorld ( ) -> RayWorldIntersection ( pos , dir , ent_all , 0 , hits , MAX_HITS , skip ) ; if ( res ) { _SmartScriptObject pTable ( m_pScriptSystem ) ; for ( int nCount = 0 ; nCount < MAX_HITS ; nCount ++ ) { _SmartScriptObject pOut ( m_pScriptSystem ) ; CScriptObjectVector oVec1 ( m_pScriptSystem ) ; CScriptObjectVector oVec2 ( m_pScriptSystem ) ; CScriptObjectVector oVec3 ( m_pScriptSystem ) ; int objecttype ; if ( hits [ nCount ] . dist <= 0 ) continue ; IEntity * centycontact = NULL ; if ( res && hits [ nCount ] . dist > 0 && hits [ nCount ] . pCollider ) { centycontact = ( IEntity * ) hits [ nCount ] . pCollider -> GetForeignData ( ) ; if ( centycontact && centycontact -> IsGarbage ( ) ) { res = 0 ; } if ( centycontact && ( ! centycontact -> IsGarbage ( ) ) ) objecttype = OT_ENTITY ; else objecttype = OT_STAT_OBJ ; } if ( hits [ nCount ] . pCollider ) { if ( centycontact ) { objecttype = 0 ; pOut -> SetValue ( " " , centycontact -> GetId ( ) ) ; } else { objecttype = 1 ; pOut -> SetToNull ( " " ) ; } } else { objecttype = 2 ; pOut -> SetToNull ( " " ) ; } pOut -> SetValue ( " " , objecttype ) ; oVec1 = ( Vec3d ) hits [ nCount ] . pt ; pOut -> SetValue ( " " , * oVec1 ) ; oVec2 = ( Vec3d ) hits [ nCount ] . n ; pOut -> SetValue ( " " , * oVec2 ) ; oVec3 = GetNormalized ( dir ) ; pOut -> SetValue ( " " , * oVec3 ) ; pOut -> SetValue ( " " , hits [ nCount ] . surface_idx ) ; pOut -> SetValue ( " " , hits [ nCount ] . partid ) ; pOut -> SetValue ( " " , nID ) ; pTable -> SetAt ( nCount , pOut ) ; if ( res && hits [ 0 ] . dist > 0 ) break ; } return pH -> EndFunction ( pTable ) ; } else { return pH -> EndFunctionNull ( ) ; } }
Traces a ray to determine what and where an instant weapon hits
Traces a ray to determine what and where an instant weapon hits
// entity
 // stat obj
 // terrain
