示例#1
0
/*implements A* algorithm*/
tree astar(int x, int y, int d, int heuristic){
	char a = 'X';//X simbolize that no action was taken to get there
	tree t, t1, t2;
	char **mat1, **mat2;
	int i, j;
	int level;
	double f;
	queue_vector aux;
	int k;
	
	//copy the starting matrix
	mat1 = newMatrix();
	for(i = 0; i < n; i++){
		for(j = 0; j < m; j++)
			mat1[i][j] = w[i][j];
	}
	
	t1 = insertTree(NULL, d, x, y, mat1, a, 0);
	t = t1;
	
	//compute the f value according to the heuristic
	f = computeHeuristic(x, y, 0, heuristic);
	
	insertQueue(t1, f);
	
	while(n_queue > 0){
		//remove the first element of the queue
		removeQueue(&aux.t, &aux.f);
		t1 = aux.t;
		
		d = t1 -> dirt;
		mat1 = t1 -> mat;
		a = t1 -> action;
		x = t1 -> x;
		y = t1 -> y;
		level = t1 -> level;
		
		//increments the number of nodes expanded
		expanded++;
		
		//adds into the queue
		if(y != 0 && mat1[y-1][x] != '#'){
			mat2 = newMatrix();
			copyMatrix(mat1, mat2);
			
			a = mat1[y-1][x];
			
			mat2[y-1][x] = '@';
			mat2[y][x] = '_';
			
			if(a == '*')
				k = d-1;
			else
				k = d;
			
			if(checkDuplicate(t, mat2, hashFunction(x, y-1), k) == 0){
				//inserts in lowercase if there is dirt
				if(a == '*'){
					removeListDirt(x, y-1);
					t2 = insertTree(t1, d-1, x, y-1, mat2, 'n', level + 1);
					
					if(d-1 == 0)
						return t2;
				}
				else
					t2 = insertTree(t1, d, x, y-1, mat2, 'N', level + 1);
					
				t1 -> N = t2;
				
				//compute the f value according to the heuristic
				f = computeHeuristic(x, y-1, level+1, heuristic);
					
				insertQueue(t2, f);
				generated++;
			}
		}
			
		if(y != n - 1 && mat1[y+1][x] != '#'){
			mat2 = newMatrix();
			copyMatrix(mat1, mat2);
			
			a = mat1[y+1][x];
			
			mat2[y+1][x] = '@';
			mat2[y][x] = '_';
			
			if(a == '*')
				k = d-1;
			else
				k = d;
			
			if(checkDuplicate(t, mat2, hashFunction(x, y+1), k) == 0){
				//inserts in lowercase if there is dirt
				if(a == '*'){
					removeListDirt(x, y+1);
					t2 = insertTree(t1, d-1, x, y+1, mat2, 's', level + 1);
					
					if(d-1 == 0)
						return t2;
				}
				else
					t2 = insertTree(t1, d, x, y+1, mat2, 'S', level + 1);
				
				t1 -> S = t2;
				
				//compute the f value according to the heuristic
				f = computeHeuristic(x, y-1, level+1, heuristic);
				
				insertQueue(t2, f);
				generated++;
			}
		}
		
		if(x != 0 && mat1[y][x-1] != '#'){
			mat2 = newMatrix();
			copyMatrix(mat1, mat2);
			
			a = mat1[y][x-1];
			
			mat2[y][x-1] = '@';
			mat2[y][x] = '_';
			
			if(a == '*')
				k = d-1;
			else
				k = d;
			
			if(checkDuplicate(t, mat2, hashFunction(x - 1, y), k) == 0){
				//inserts in lowercase if there is dirt
				if(a == '*'){
					removeListDirt(x-1, y);
					t2 = insertTree(t1, d-1, x-1, y, mat2, 'w', level + 1);
					
					if(d-1 == 0)
						return t2;
				}
				else
					t2 = insertTree(t1, d, x-1, y, mat2, 'W', level + 1);
					
				t1 -> W = t2;
				
				//compute the f value according to the heuristic
				f = computeHeuristic(x, y-1, level+1, heuristic);
					
				insertQueue(t2, f);
				generated++;
			}
		}
		
		if(x != m - 1 && mat1[y][x+1] != '#'){
			mat2 = newMatrix();
			copyMatrix(mat1, mat2);
			
			a = mat1[y][x+1];
			
			mat2[y][x+1] = '@';
			mat2[y][x] = '_';
			
			if(a == '*')
				k = d-1;
			else
				k = d;
			
			if(checkDuplicate(t, mat2, hashFunction(x + 1, y), k) == 0){
				//inserts in lowercase if there is dirt
				if(a == '*'){
					removeListDirt(x+1, y);
					t2 = insertTree(t1, d-1, x+1, y, mat2, 'e', level + 1);
					
					if(d-1 == 0)
						return t2;
				}
				else
					t2 = insertTree(t1, d, x+1, y, mat2, 'E', level + 1);
					
				t1 -> E = t2;
				
				//compute the f value according to the heuristic
				f = computeHeuristic(x, y-1, level+1, heuristic);
				
				insertQueue(t2, f);
				generated++;
			}
		}
		
		orderQueue();
	}
	
	return NULL;
}
示例#2
0
void REV_process(tMapQ3 * map)
{
	//Variables
	f32 preWait = 0, postWait = 0;
	static u8 firstFrame = 1;
	TRACKER * auxT;
	setUp3D();
	//Wait just before drawing (instead of after), this should enhance performance
	VIDEO_Flush();
	preWait = (f32)(ticks_to_millisecs(gettime()));
 	VIDEO_WaitVSync();
	postWait = (f32)(ticks_to_millisecs(gettime()));
	GPUWaitTime = 0.001f * (postWait - preWait);
	//Update physics
	updatePhysics();
	setBGColor(SC_BLUE);
	//Clasify objects into solid or transparent queues
	//This is done before everything else because this clasification is the same for every viewport
	clasify3D(mainRoot->rootNode);
	//Now we use the clasified queues to render shadows
	//if(mainRoot->shadowCaster)
		//mainRoot->shadowScene();
	//Render each Viewport into it's texture
	GX_SetBlendMode(GX_BM_BLEND, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);
	//GX_SetZMode (GX_TRUE, GX_LEQUAL, GX_TRUE);
	std::multimap<CAMERA*, TRender2Texture*>::iterator iter = mainRoot->m_Render2Textures.begin();
	for(;iter != mainRoot->m_Render2Textures.end(); ++iter)
	{
		(*iter).second->setForRender(perspective);
		(*iter).second->getCamera()->setForRender(view);
		//Before rendering the scene, render the skyBox
		GX_SetZMode (GX_FALSE, GX_LEQUAL, GX_TRUE);
		mainRoot->skyBox.render((*iter).second->getCamera());
		GX_SetZMode (GX_TRUE, GX_LEQUAL, GX_TRUE);
		//Now render the map
		//GX_LoadPosMtxImm(view, GX_PNMTX0);
		//if(map)
			//renderQ3Map(tTex->cam->getPos(), map);
		//Now render objects
		GX_SetCullMode(GX_CULL_NONE);
		auxT = solidQueue;
		while(auxT)
		{
			render(auxT->target, (*iter).second->getCamera()->getPos());
			auxT = auxT->next;
		}
		orderQueue((*iter).second->getCamera());
		auxT = transQueue;
		while(auxT)
		{
			render(auxT->target, (*iter).second->getCamera()->getPos());
			auxT = auxT->next;
		}
		//Copy the embeded frame buffer to the texture
		(*iter).second->copyTexture();
	}
	while(solidQueue)
	{
		auxT = solidQueue;
		solidQueue = solidQueue->next;
		free(auxT);
	}
	while(transQueue)
	{
		auxT = transQueue;
		transQueue = transQueue->next;
		free(auxT);
	}
	setBGColor(SC_WHITE);
	//2D System
	//GX_SetZMode (GX_FALSE, GX_LEQUAL, GX_TRUE);
	GX_SetCopyFilter(rMode->aa,rMode->sample_pattern,GX_TRUE,rMode->vfilter);
	GX_SetViewport(0,0, w, h,0,1);
	GX_SetScissor(0,0, w, h);
	guOrtho(perspective,0,h,0,w, 1.0,100.0);
	GX_LoadProjectionMtx(perspective, GX_ORTHOGRAPHIC);
	GX_SetCullMode(GX_CULL_NONE);
	setUp2D();
	parse2D(mainRoot->rootN2D);
	order2D();
	render2D();
	GX_DrawDone();
	
	GX_CopyDisp(frameBuffer[fb],GX_TRUE);
	VIDEO_SetNextFramebuffer(frameBuffer[fb]);
	
	//Set out black screen after first frame
	if(firstFrame)
	{
		firstFrame = 0;
		VIDEO_SetBlack(FALSE);
	}
	
	fb ^= 1;
}