Пример #1
0
void command_move_to(int player_id, int unit_id, int x, int y)
{
    if (!is_passable(x, y))
        return;

    Unit * unit = UNIT(unit_id);

    if (!astar_compute(unit->x, unit->y, x, y, unit->move_path, PATH_LENGTH))
        return;

    unit->command.type = COMMAND_MOVE_TO;
    unit->moving = true;
    unit->move_target_x = x;
    unit->move_target_y = y;
    unit->offset_x = 0;
    unit->offset_y = 0;

    issue_command(unit_id, unit);
}
Пример #2
0
void CAStar::BuildPath(void *pUser)
{
	CAStar* pSelf = (CAStar*)pUser;

	{
		CServerInfo Info; pSelf->Client()->GetServerInfo(&Info);
		if(!g_Config.m_ClPathFinding || !(IsRace(&Info) || IsDDNet(&Info)))
			return;
	}

	int SolutionLength = -1;
	int *pSolution = 0;
	int Start = pSelf->GetStart();
	int Finish = pSelf->GetFinish();

/*	if(Start == -1)
		dbg_msg("path", "didn't find start tile");
	if(Finish == -1)
		dbg_msg("path", "didn't find finish tile");
*/
	if(Start >= 0 && Finish >= 0)
	{
		pSelf->FillGrid(true);
		pSolution = astar_compute((const char *)pSelf->m_pField, &SolutionLength, pSelf->Collision()->GetWidth(), pSelf->Collision()->GetHeight(), Start, Finish);
		dbg_msg("path", "start=%i, finish=%i, length=%i", Start, Finish, SolutionLength);
	}

	if(SolutionLength == -1) // try again, ignoring freeze
	{
		pSelf->FillGrid(false);
		pSolution = astar_compute((const char *)pSelf->m_pField, &SolutionLength, pSelf->Collision()->GetWidth(), pSelf->Collision()->GetHeight(), Start, Finish);
		dbg_msg("path", "ignored freeze: start=%i, finish=%i, length=%i", Start, Finish, SolutionLength);
	}

	if(g_Config.m_ClNotifications)
	{
		if(SolutionLength != -1)
		{
			char aBuf[256];
			str_format(aBuf, sizeof(aBuf), "Found path. Length: %i", SolutionLength);
			pSelf->m_pClient->m_pHud->PushNotification(aBuf);
		}
		else
			pSelf->m_pClient->m_pHud->PushNotification("No possible path found.");
	}
	
	if(pSolution)
	{
		if(SolutionLength > 0)
		{
			pSelf->m_PathFound = true;
			for(int i = SolutionLength; i >= 0 ; i--)
			{
				pSelf->m_Path.add(pSelf->Collision()->GetPos(pSolution[i]));
				thread_sleep(10);
				if(pSelf->m_ThreadShouldExit)
				{
					return;
				}
			}
		}
		free(pSolution);
	}
}