<commit_before>// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
// 
// Copyright 2015 <NAME>, All Rights Reserved.
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
// 
// Perception Neuron (TM) is a trademark of Beijing Noitom Technology Ltd.
// 
// Description:
// This code implements the Controller functions that are used to connect to the Perception Neuron (TM) Axis Player.
// 

#include "PerceptionNeuronPrivatePCH.h"
#include "PerceptionNeuronController.h"


// Sets default values
APerceptionNeuronController::APerceptionNeuronController()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void APerceptionNeuronController::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void APerceptionNeuronController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	// Read each tick a new motionline. Additionally motionlines are just discarded.
	if ((bConnected == true) || (bPlay == true))
	{
		uint32 BytesPending = 0;

		if (bConnected == true)
			if (ReceiverSocket->HasPendingData(BytesPending) != true)
				BytesPending = 0;

		if ((BytesPending) || (bPlay == true))
		{
#define SIZEOFDATA 10000
			uint8 Data[SIZEOFDATA];
			int32 BytesRead = 0;
			bool bMotionLineBeginFound = false;
			bool bMotionLineEndFound = false;

			if (bConnected == true) // Network connection?
			{
				if (ReceiverSocket->Recv(Data, SIZEOFDATA, BytesRead) != true)
					BytesRead = 0;
			}
			else if (bPlay == true)	// Player connected ?
			{				
				DeltaTimeAdded += DeltaTime;
				if (FrameTime < DeltaTimeAdded) // Synchronize to FrameTime
				{
					DeltaTimeAdded -= FrameTime;
					while (DeltaTimeAdded > FrameTime) // We are too slow => Skip frames
					{						
						DeltaTimeAdded -= FrameTime;						
						MotionLinePointer++;
						if ((MotionLinePointer >= (MotionLineOffset + Frames)) && (bEndless == true))
							MotionLinePointer = MotionLineOffset;				
					}

					if (MotionLinePointer < (MotionLineOffset + Frames))
					{
						if (SIZEOFDATA >= PlayerMotionLines[MotionLinePointer].Len() + 1) // Sanity check
						{
							// Convert string based motion line back to an array of chars to pump the line through the parser (Little bit ugly...)
							memcpy(Data, TCHAR_TO_ANSI(*PlayerMotionLines[MotionLinePointer]), PlayerMotionLines[MotionLinePointer].Len());
							Data[PlayerMotionLines[MotionLinePointer].Len()] = '\n';
							BytesRead = PlayerMotionLines[MotionLinePointer].Len() + 1;
						}
						MotionLinePointer++; // Increment line for next time;
						if ((MotionLinePointer >= (MotionLineOffset + Frames)) && (bEndless == true))
							MotionLinePointer = MotionLineOffset;						
					}					
				}
			}

			if (BytesRead)
			{
				int32 i, j, k;

				if (MotionLineFormat == Neuron)
				{
					// Scan for the beginning of a motion line and synchronize to Avatarname
					// Neuron BVH output format:
					// 0 Avatarname 0.00 0.00 ... 0.00 ||
					for (i = j = k = 0; i < BytesRead - 2; i++)
					{
						if ((Data[i] == '0') && (Data[i + 1] == ' ') && ((Data[i + 2] < '0') || (Data[i + 2] > '9') || (Data[i + 2] == '-')))
						{
							bMotionLineBeginFound = true;
							// Skip Avatarname<commit_msg>Fix a synchronization bug when using Neuron format<commit_after>// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
// 
// Copyright 2015 <NAME>, All Rights Reserved.
//
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
// 
// Perception Neuron (TM) is a trademark of Beijing Noitom Technology Ltd.
// 
// Description:
// This code implements the Controller functions that are used to connect to the Perception Neuron (TM) Axis Player.
// 

#include "PerceptionNeuronPrivatePCH.h"
#include "PerceptionNeuronController.h"


// Sets default values
APerceptionNeuronController::APerceptionNeuronController()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void APerceptionNeuronController::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void APerceptionNeuronController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	// Read each tick a new motionline. Additionally motionlines are just discarded.
	if ((bConnected == true) || (bPlay == true))
	{
		uint32 BytesPending = 0;

		if (bConnected == true)
			if (ReceiverSocket->HasPendingData(BytesPending) != true)
				BytesPending = 0;

		if ((BytesPending) || (bPlay == true))
		{
#define SIZEOFDATA 10000
			uint8 Data[SIZEOFDATA];
			int32 BytesRead = 0;
			bool bMotionLineBeginFound = false;
			bool bMotionLineEndFound = false;

			if (bConnected == true) // Network connection?
			{
				if (ReceiverSocket->Recv(Data, SIZEOFDATA, BytesRead) != true)
					BytesRead = 0;
			}
			else if (bPlay == true)	// Player connected ?
			{				
				DeltaTimeAdded += DeltaTime;
				if (FrameTime < DeltaTimeAdded) // Synchronize to FrameTime
				{
					DeltaTimeAdded -= FrameTime;
					while (DeltaTimeAdded > FrameTime) // We are too slow => Skip frames
					{						
						DeltaTimeAdded -= FrameTime;						
						MotionLinePointer++;
						if ((MotionLinePointer >= (MotionLineOffset + Frames)) && (bEndless == true))
							MotionLinePointer = MotionLineOffset;				
					}

					if (MotionLinePointer < (MotionLineOffset + Frames))
					{
						if (SIZEOFDATA >= PlayerMotionLines[MotionLinePointer].Len() + 1) // Sanity check
						{
							// Convert string based motion line back to an array of chars to pump the line through the parser (Little bit ugly...)
							memcpy(Data, TCHAR_TO_ANSI(*PlayerMotionLines[MotionLinePointer]), PlayerMotionLines[MotionLinePointer].Len());
							Data[PlayerMotionLines[MotionLinePointer].Len()] = '\n';
							BytesRead = PlayerMotionLines[MotionLinePointer].Len() + 1;
						}
						MotionLinePointer++; // Increment line for next time;
						if ((MotionLinePointer >= (MotionLineOffset + Frames)) && (bEndless == true))
							MotionLinePointer = MotionLineOffset;						
					}					
				}
			}

			if (BytesRead)
			{
				int32 i, j, k;

				if (MotionLineFormat == Neuron)
				{
					// Scan for the beginning of a motion line and synchronize to Avatarname
					// Neuron BVH output format:
					// 0 Avatarname 0.00 0.00 ... 0.00 ||
					for (i = j = k = 0; i < BytesRead - 2; i++)
					{
						if ((Data[i] == '0') && (Data[i + 1] == ' ') && ((Data[i + 2] < '0') || (Data[i + 2] > '9')) && (Data[i + 2] != '-'))
						{
							bMotionLineBeginFound = true;
							// Skip Avatarname
{'fraction_non_alphanumeric': 0.12041502265088412, 'fraction_numerical': 0.014321204150226509, 'mean_word_length': 3.0258823529411765, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '144217', 'n_tokens_mistral': 2690, 'n_tokens_neox': 2039, 'n_words': 640}