示例#1
0
PathNode* MicroPather::NewPathNode( void* state, float costFromStart, float estToEnd, PathNode* parent )
{
	// Try to find an existing node for this state.
	unsigned key = Hash( state );   //(HASH_SIZE-1) & ( (unsigned)state + (((unsigned)state)>>8) + (((unsigned)state)>>16) + (((unsigned)state)>>24) );

	if ( !hashTable[key] ) {
		// There isn't even a hashtable yet - create and initialize the PathNode.
		hashTable[key] = AllocatePathNode();
		hashTable[key]->Init( frame, state, costFromStart, estToEnd, parent );
		return hashTable[key];
	}

	PathNode* root = hashTable[key];
	PathNode* up = 0;
	while ( root ) {
		up = root;
		if ( root->state == state ) {
			root->Reuse( frame, costFromStart, estToEnd, parent );
			assert( root->state == state );
			return root;
		}
		#ifdef USE_BINARY_HASH
		else if ( state > root->state ) {
			root = root->right;
		}
		#endif
		else {
			#ifdef USE_BINARY_HASH
			assert( state < root->state );
			#endif
			root = root->left;
		}
	}

	assert( up );
	PathNode* pNode = AllocatePathNode();
	pNode->Init( frame, state, costFromStart, estToEnd, parent );
	#ifdef USE_BINARY_HASH
	if ( state > up->state ) {
		assert( up->right == 0 );
		up->right = pNode;
	}
	else {
		assert( up->left == 0 );
		up->left = pNode;
	}
	#else
	up->left = pNode;
	#endif
	return pNode;
}