Example #1
0
void		algo_path(t_rooms *start, t_open *open_list, t_close *close_list)
{
	t_rooms		*current;
	int			weight;
	t_rooms		*connection;
	int			i;

	weight = 1;
	while (open_list->room)
	{
		i = 0;
		current = get_current(open_list);
		close_list = add_to_close(close_list, current);
		open_list = remove_from_open(open_list, current);
		while (current->nb_of_connections && current->connections[i] &&
			(connection = get_room_from_name(current->connections[i], start)))
		{
			if (is_in_close(close_list, connection) && (i = i + 1))
				continue ;
			if (!(is_in_open(open_list, connection)))
				open_list = add_to_open(open_list, connection);
			if (weight < connection->weight)
				connection->weight = weight;
			i++;
		}
		weight++;
	}
}
Example #2
0
	void Do_Astar(ZPath_Finding_Response *response)
	{
		const int ticks_until_pause = 90;
		int total_ticks;
		int pause_ticks;
		double start_time, end_time;
		//vector<pf_point> open_list;
		//vector<pf_point> closed_list;
		pf_point_array open_list;
		pf_point_array closed_list;
		pf_point cp;

		//check
		if(!response) return;

		int start_x = response->start_x / 16;
		int start_y = response->start_y / 16;
		int end_x = response->end_x / 16;
		int end_y = response->end_y / 16;

		//start and end ok?
		if(!tile_ok(start_x, start_y, start_x, start_y, response) || 
			!tile_ok(end_x, end_y, end_x, end_y, response))
		{
			response->pf_point_list.push_back(pf_point(response->end_x, response->end_y));
			return;
		}

		//printf("Do_Astar::start:[%d,%d]\n", response->start_x, response->start_y);
		//printf("Do_Astar::end:[%d,%d]\n", response->end_x, response->end_y);

		//init lists
		{
			int size;

			open_list.alloc_size = 1000;
			open_list.list = (pf_point*)malloc(open_list.alloc_size * sizeof(pf_point));
			open_list.size = 0;
			open_list.InitPointIndex(response->w, response->h);

			closed_list.alloc_size = 1000;
			closed_list.list = (pf_point*)malloc(closed_list.alloc_size * sizeof(pf_point));
			closed_list.size = 0;
			closed_list.InitPointIndex(response->w, response->h);
		}

		//open_list.push_back(pf_point(start_x, start_y));
		pf_point sp = pf_point(start_x, start_y);
		open_list.AddPoint(sp);
		//open_list.list[open_list.size++] = pf_point(start_x, start_y);

		//start_time = current_time();
		total_ticks = 0;
		pause_ticks = 0;
		while(open_list.size)
		{
			cp = lowest_f_cost(open_list);

			if(response->kill_thread)
			{
				open_list.FreeList();
				open_list.FreePointIndex();
				closed_list.FreeList();
				closed_list.FreePointIndex();
				return;
			}

			if(cp.x == end_x && cp.y == end_y)
			{
				//printf("end found\n");
				break;
			}

			remove_from_open(cp, open_list, closed_list);

			if(response->kill_thread) 
			{
				open_list.FreeList();
				open_list.FreePointIndex();
				closed_list.FreeList();
				closed_list.FreePointIndex();
				return;
			}

			push_in_neighbors(cp, open_list, closed_list, response);

			//printf("current_point:[%d,%d]\n", cp.x, cp.y);

			if(response->kill_thread)
			{
				open_list.FreeList();
				open_list.FreePointIndex();
				closed_list.FreeList();
				closed_list.FreePointIndex();
				return;
			}

			total_ticks++;
			pause_ticks++;
			if(pause_ticks >= ticks_until_pause)
			{
				pause_ticks = 0;
				SDL_Delay(10 + (ZPath_Finding_Response::existing_responses * 4));
			}

			if(response->kill_thread)
			{
				open_list.FreeList();
				open_list.FreePointIndex();
				closed_list.FreeList();
				closed_list.FreePointIndex();
				return;
			}
		}
		//end_time = current_time();

		vector<pf_point> final_path;
		if(cp.x == end_x && cp.y == end_y)
		while(1)
		{
			int i;

			final_path.insert(final_path.begin(), cp);

			//for(vector<pf_point>::iterator i=close_list.begin();;i++)
			for(i=0;;i++)
			{
				pf_point &ip = closed_list.list[i];

				if(i>=closed_list.size)
				{
					//printf("Do_Astar::could not reconstruct list\n");
					break;
				}

				if(cp.px == ip.x && cp.py == ip.y)
				{
					//printf("inserted point:[%d,%d]\n", i->x, i->y);
					cp = ip;
					break;
				}
			}

			if(i>=closed_list.size)
			{
				final_path.clear();
				break;
			}

			if(cp.x == start_x && cp.y == start_y)
				break;
		}

		//remove redundents
		int previous_dir = -1;
		int cur_dir;
		for(vector<pf_point>::iterator i=final_path.begin();i!=final_path.end();)
		{
			if(i!=final_path.begin() && i+1!=final_path.end())
			{
				previous_dir = the_dir(*(i-1), *(i));
				cur_dir = the_dir(*i, *(i+1));

				if(previous_dir == cur_dir)
					i = final_path.erase(i);
				else
					i++;
			}
			else
				i++;
		}


		//convert list to actual x,y cords
		for(vector<pf_point>::iterator i=final_path.begin();i!=final_path.end();i++)
		{
			i->x *= 16;
			i->y *= 16;

			if(response->is_robot)
			{
				i->x += 8;
				i->y += 8;
			}
			else
			{
				i->x += 16;
				i->y += 16;
			}
			//printf("Do_Astar::found point:[%d,%d]\n", i->x, i->y);
		}

		final_path.push_back(pf_point(response->end_x, response->end_y));
		
		response->pf_point_list = final_path;
		//return final_path;

		//printf("Do_Astar::finished, ticks:%d time:%lf ... [%d,%d] to [%d,%d]\n", total_ticks, end_time - start_time, response->start_x, response->start_y, response->end_x, response->end_y);

		//dealloc
		{
			open_list.FreeList();
			open_list.FreePointIndex();
			closed_list.FreeList();
			closed_list.FreePointIndex();
		}
	}