int GBLCOBGWrapper::BehaviourFunction( const CKBehaviorContext& behContext )
{
	CKBehavior	*behaviour = behContext.Behavior;
	CKContext	*context = behContext.Context;
	int			iPin, nbPin;

	CKBehavior* script = (CKBehavior*)behaviour->GetLocalParameterObject(EGBLCOBGWRAPPERPARAM_PARAMETER_SCRIPT);

	if (script == NULL)
		return CKBR_GENERICERROR;

	// Activate the right inputs
	nbPin = behaviour->GetInputCount();
	for (iPin = 0; iPin < nbPin; iPin++)
		{
		if (behaviour->IsInputActive(iPin))
			{
			script->ActivateInput(iPin, TRUE);
			behaviour->ActivateInput(iPin, FALSE);
			}
		}
	
	// Deactivate all the outputs
	nbPin = script->GetOutputCount();
	for (iPin = 0; iPin < nbPin; iPin++)
		{
		behaviour->ActivateOutput(iPin, FALSE);
		}

	// Parameter In: Set Source
	int nbPinBB = behaviour->GetInputParameterCount();
	int nbPinBG = script->GetInputParameterCount();

	if (nbPinBB != nbPinBG)
		return CKBR_GENERICERROR;

	for (iPin = 0; iPin < nbPinBB; iPin++)
	{
		CKParameterIn *pBin = behaviour->GetInputParameter(iPin);
		CKParameter* pSource = pBin->GetDirectSource();

		CKParameterIn *pSin = script->GetInputParameter(iPin);
		pSin->SetDirectSource(pSource);
	}

	// Execute the contained script
	CKERROR result = script->Execute(behContext.DeltaTime);

	// The script loop on itself too much times
	if (result == CKBR_INFINITELOOP) 
		{
		context->OutputToConsoleExBeep("Execute Script : Script %s Executed too much times",script->GetName());
		script->Activate(FALSE,FALSE);
		return CKBR_OK;
		}

	// Activate the right outputs 
	nbPin = script->GetOutputCount();
	for (iPin = 0; iPin < nbPin; iPin++)
		{
		if (script->IsOutputActive(iPin))
			{
			behaviour->ActivateOutput(iPin);
			script->ActivateOutput(iPin, FALSE);
			}
		}

	// Update Parameters Out
	nbPin = behaviour->GetOutputParameterCount();
	for (iPin = 0; iPin < nbPin; iPin++)  
		{
		CKParameterOut *pBout = behaviour->GetOutputParameter(iPin);
		CKParameterOut *pSout = script->GetOutputParameter(iPin);
		pBout->CopyValue(pSout, TRUE);
		}

	
	// Test if there are any active sub-behaviors, restart the next frame if any
	BOOL bActivateNextFrame = FALSE;
	ActivateNextFrameSubBB(script,bActivateNextFrame);
	if (bActivateNextFrame)
		return CKBR_ACTIVATENEXTFRAME;

	// return the execute value
	return result;	
}
/*
 *******************************************************************
 * Function: CKERROR BGWrapperCB(const CKBehaviorContext& behContext)
 *
 * Description : The Behavior Callback function is called by Virtools 
 *               when various events happen in the life of a BuildingBlock.
 *
 * Parameters :
 *    behaviourContext	      Behavior context reference, which gives access to 
 *                            frequently used global objects ( context, level, manager, etc... )
 *
 * Returns : CKERROR
 *
 *******************************************************************
 */
CKERROR GBLCOBGWrapper::BGWrapperCB(const CKBehaviorContext& behContext)
{
	CKERROR	result = CKBR_GENERICERROR; 

	// Initialize common pointers.	
	CKBehavior *behaviour   = behContext.Behavior;
	CKContext  *context   = behContext.Context;
	CKLevel    *level = behContext.CurrentLevel;
	CKBeObject *owner = behContext.Behavior->GetOwner();
	
	char*name = behaviour->GetName();
	
	if ( behaviour == NULL || context == NULL || level == NULL /*|| owner == NULL*/ )
		return CKBR_OK;
	
	//Get The BG Script Object if exists
	CKBehavior* newBG = NULL;
	CKBehavior* curBG = (CKBehavior*)behaviour->GetLocalParameterObject(EGBLCOBGWRAPPERPARAM_PARAMETER_SCRIPT);

	// Get the BG nms file path. GetStringValue doesn't work with setting...
	char fileName[_MAX_PATH+1];
	memset(fileName,0,_MAX_PATH+1);
	CKParameter* scriptPath  = behaviour->GetLocalParameter(EGBLCOBGWRAPPERPARAM_PARAMETER_NAME);
	if ( scriptPath == NULL )
		return CKBR_OK;
	
	int lenPath  = scriptPath->GetDataSize();
	if ( lenPath >= _MAX_PATH )
		return CKBR_OK;
	
	void*ptrPath = scriptPath->GetReadDataPtr(TRUE);
	if ( ptrPath == NULL )
		return CKBR_OK;
	
	memcpy(fileName,ptrPath,lenPath);

	CKDWORD message = behContext.CallbackMessage;

	switch (message)
		{
		case CKM_BEHAVIORLOAD :				// when the behavior is loaded
		case CKM_BEHAVIORRESET:				// when the behavior is reseted
		case CKM_BEHAVIORSETTINGSEDITED :	// when the settings are edited
			{
				if ( curBG != NULL )
				{
					DestroyCurrentBG(level,behaviour,curBG);
					curBG = NULL;
				}

				newBG = BGLoader( fileName, behContext );
				if ( newBG == NULL )
					return CKBR_OK;
				
				if ( message == CKM_BEHAVIORLOAD || message == CKM_BEHAVIORRESET )
				{
					//context->OutputToConsoleExBeep("%s : LOADED %s",behaviour->GetName(), fileName);
					if ( CheckIO(behaviour, newBG) == TRUE )
						result = CKBR_OK;
					else	
						context->OutputToConsoleExBeep("%s : Too many inputs/outputs changes in %s\r\nPlease reconstruct the wrapper.",behaviour->GetName(), fileName);
				}
				else if ( message == CKM_BEHAVIORSETTINGSEDITED )
				{
					if ( CheckIO(behaviour, newBG) == TRUE )
					{					
						result = CKBR_OK;
					}
					else if (DeleteIO(behaviour) == TRUE)
					{
						if ( CreateIO(behaviour, newBG ) == TRUE )
							result = CKBR_OK;
						else	
							context->OutputToConsoleExBeep("%s : Cannot Create Inputs/Outputs %s",behaviour->GetName(), fileName);
					}
					else
						context->OutputToConsoleExBeep("%s : Cannot Delete Inputs/Outputs %s",behaviour->GetName(), fileName);
				}

				if ( result == CKBR_OK && newBG != NULL )
					SetNewBG(behaviour,newBG);
			}
			break;
	
		case CKM_BEHAVIORDEACTIVATESCRIPT:
			{
				if ( curBG != NULL )
					DesactivateSubBB(curBG);
				result = CKBR_OK;
			}
			break;
		
		case CKM_BEHAVIORDETACH : // when the behavior is deleted
			{
				if (curBG != NULL)
				{
					DestroyCurrentBG(level,behaviour,curBG);
					curBG = NULL;
				}
				result = CKBR_OK;
			}
			break;				
		
		default: 
			{
				result = CKBR_OK;
			}
			break;

		}
		
	if (result != CKBR_OK)
		context->OutputToConsoleExBeep("%s : Problem while manipulating",behaviour->GetName());



	return result;
	}
Exemple #3
0
//************************************
// Method:    PWOverlapSphere
// FullName:  PWOverlapSphere
// Access:    public 
// Returns:   int
// Qualifier:
// Parameter: const CKBehaviorContext& behcontext
//************************************
int PWOverlapSphere(const CKBehaviorContext& behcontext)
{

	CKBehavior* beh = behcontext.Behavior;
  	CKContext* ctx = behcontext.Context;
	PhysicManager *pm = GetPMan();
	
	pFactory *pf = pFactory::Instance();

	using namespace vtTools::BehaviorTools;
	using namespace vtTools::ParameterTools;

	

	//////////////////////////////////////////////////////////////////////////
	//the object : 
	CK3dEntity *target = (CK3dEntity *) beh->GetTarget();
	if( !target ) return CKBR_OWNERERROR;


	//////////////////////////////////////////////////////////////////////////
	// the world :  
	pWorld *world=GetPMan()->getWorld(target->GetID());
	if (!world)
	{
		beh->ActivateOutput(bbOT_No);
		return 0;
	}

	NxScene *scene =  world->getScene();
	if (!scene)
	{
		beh->ActivateOutput(bbOT_No);
		return 0;
	}

	if( beh->IsInputActive(0) )
	{

		beh->ActivateInput(0,FALSE);


		CKGroup *carray = (CKGroup*)beh->GetLocalParameterObject(bbS_Result);
		if (carray)
		{
			//carray->clear();
			carray->Clear();
		}else
		{
			
			CK_OBJECTCREATION_OPTIONS creaoptions = (CK_OBJECTCREATION_OPTIONS)(CK_OBJECTCREATION_NONAMECHECK|CK_OBJECTCREATION_DYNAMIC);
			carray = (CKGroup*)ctx()->CreateObject(CKCID_GROUP,"asdasd",creaoptions);

		}
		
		beh->SetLocalParameterObject(0,carray);

		int hitIndex = 0;
		beh->SetLocalParameterValue(bbS_Index,&hitIndex);
		int hitSize = 0;
		beh->SetLocalParameterValue(bbS_Size,&hitSize);




		//////////////////////////////////////////////////////////////////////////
	
		int types  = GetInputParameterValue<int>(beh,bbI_ShapesType);
		int accurate  = GetInputParameterValue<int>(beh,bbI_Accurate);



		DWORD groupsEnabled;
		DWORD groups = 0xffffffff;
		beh->GetLocalParameterValue(bbS_Groups,&groupsEnabled);
		if (groupsEnabled)
		{
			groups = GetInputParameterValue<int>(beh,bbI_Groups);
		}

		pGroupsMask *gmask = NULL;
		DWORD mask;
		beh->GetLocalParameterValue(bbS_Mask,&mask);
		if (mask)
		{
			CKParameter *maskP = beh->GetInputParameter(bbI_Mask)->GetRealSource();
			gmask->bits0  = GetValueFromParameterStruct<int>(maskP,0);
			gmask->bits1  = GetValueFromParameterStruct<int>(maskP,1);
			gmask->bits2  = GetValueFromParameterStruct<int>(maskP,2);
			gmask->bits3  = GetValueFromParameterStruct<int>(maskP,3);

		}

		float radius = GetInputParameterValue<float>(beh,bbI_Radius);
		VxVector center = GetInputParameterValue<VxVector>(beh,bbI_Center);

		VxSphere sphere(center,radius);

		CK3dEntity *shape = (CK3dEntity*)beh->GetInputParameterObject(bbI_Ref);

		int nbShapes = world->overlapSphereShapes(sphere,shape,(pShapesType)types,carray,groups,gmask,accurate);
		if (nbShapes)
		{
			beh->ActivateOutput(bbOT_Yes);
			beh->ActivateInput(1,TRUE);

		}else{
			beh->ActivateOutput(bbOT_No);
		}
	}

	if( beh->IsInputActive(1) )
	{

		beh->ActivateInput(1,FALSE);
		CKGroup *carray = (CKGroup*)beh->GetLocalParameterObject(bbS_Result);
		
		//////////////////////////////////////////////////////////////////////////
		if (carray)
		{
			if (carray->GetObjectCount())
			{
				CKBeObject *hit = carray->GetObject(carray->GetObjectCount()-1);
				if (hit)
				{


					beh->SetOutputParameterObject(0,hit);
					carray->RemoveObject(hit);
					
					
					

					if (carray->GetObjectCount())
					{
						beh->ActivateOutput(bbOT_Next);
					}else
					{
						beh->ActivateOutput(bbOT_Finish);
						CKDestroyObject(carray);
					}

				}
			}else{
				beh->ActivateOutput(bbOT_Finish);
				CKDestroyObject(carray);
			}
		}else
		{
			beh->ActivateOutput(bbOT_Finish);
			CKDestroyObject(carray);
		}

	}

	return 0;
}