Beispiel #1
0
void aStarPathFinder::_readPathFromMap( StackArray<POINT, ROW*COL> &path )
{
	node = &m_Map[m_End.y][m_End.x];
	path.Push( node->pos );

	while ( !isPointEqual( node->pos, m_Start ) )
	{
		node = node->pParent;
		path.Push( node->pos );
	}
}
Beispiel #2
0
void bxBox::_processInput( MAPDATA md, const point &heroPos, char dir )
{
	// 只有被主角推到了才需要移动
	if( isPointEqual( m_Pos, heroPos ) )
	{
		m_PrevPos.x = m_Pos.x;
		m_PrevPos.y = m_Pos.y;

		switch(dir)
		{
		case 'w': case 'W':
			{
				if( md[m_Pos.y-1][m_Pos.x] != MS_WALL )
				{
					--m_Pos.y;
				}
			}
			break;

		case 's': case 'S':
			{
				if( md[m_Pos.y+1][m_Pos.x] != MS_WALL )
				{
					++m_Pos.y;
				}
			}
			break;

		case 'a': case 'A':
			{
				if( md[m_Pos.y][m_Pos.x-1] != MS_WALL )
				{
					--m_Pos.x;
				}
			}
			break;

		case 'd': case 'D':
			{
				if(	md[m_Pos.y][m_Pos.x+1] != MS_WALL )
				{
					++m_Pos.x;
				}
			}
			break;
		}
	}
}
Beispiel #3
0
void aStarPathFinder::findPath( char (*mapdata)[COL], 
							 int startX, int startY, 
							 int endX, int endY,
							 StackArray<POINT,ROW*COL> &path )
{
	path.SetEmpty();
	m_OpenList.setEmpty();
	m_CloseList.setEmpty();

	m_Start.x = startX;
	m_Start.y = startY;
	m_End.x = endX;
	m_End.y = endY;

	// 初始化地图数据
	for ( int i=0; i<ROW; ++i )
	{
		for ( int j=0; j<COL; ++j )
		{
			m_Map[i][j].pos.x = j;
			m_Map[i][j].pos.y = i;
			m_Map[i][j].mapFlag = mapdata[i][j];
			_calcH( m_Map[i][j] );
			m_Map[i][j].G = 0;
		}
	}
	node = &m_Map[startY][startX];
	node->H = 0;
	node->mapFlag = AF_INOPENLIST;
	m_OpenList.add( node );

	while ( !m_OpenList.isEmpty() )
	{
		m_OpenList.remove_Top( &node );
		m_CloseList.push( node );
		node->mapFlag = AF_INCLOSELIST;

		// 探索周围的八个方向
		m_Dir = 0;
		while ( m_Dir < DIR_NUM )
		{
			tempPos.x = node->pos.x + dirArray[m_Dir].x;
			tempPos.y = node->pos.y + dirArray[m_Dir].y;

			if ( _isPointValidAStar( tempPos ) )
			{
				temp= &m_Map[tempPos.y][tempPos.x];

				// 完成寻路
				if( isPointEqual( temp->pos, m_End ) )
				{
					temp->pParent = node;
					_readPathFromMap( path );
					return;
				}

				if ( temp->mapFlag == SPACE )
				{
					m_OpenList.add( temp );
					temp->mapFlag = AF_INOPENLIST;
					// 由于这里使用了一个方向的数组,其中斜线方向的下标
					// 刚好是一个奇数,所以判断当前的方向是否为奇数即可
					// 判断是否为斜线方向
					if( _calcF( *temp, *node, (m_Dir%2 != 0) ) )
					{
						temp->pParent = node;
					}
				}
				else
				{
					if( temp->mapFlag == AF_INOPENLIST &&
						_calcF( *temp, *node, (m_Dir%2 != 0) ) )
					{
						temp->pParent = node;
					}
					else
					if( temp->mapFlag == AF_INCLOSELIST &&
						_calcF( *temp, *node, (m_Dir%2 != 0) ) )
					{
						m_OpenList.add( temp );
						m_CloseList.remove( temp );
						temp->mapFlag = AF_INOPENLIST;
						temp->pParent = node;
					}
				}
			}

			++m_Dir;
		}
	}
}
Beispiel #4
0
bool aStarPathFinder::findPathOneStep( char (*mapdata)[COL], 
								int startX, int startY, 
								int endX, int endY,
								bool isFirst )
{
	if( isFirst )
	{
		m_OpenList.setEmpty();
		m_CloseList.setEmpty();

		m_Start.x = startX;
		m_Start.y = startY;
		m_End.x = endX;
		m_End.y = endY;

		// 初始化地图数据
		for ( int i=0; i<ROW; ++i )
		{
			for ( int j=0; j<COL; ++j )
			{
				m_Map[i][j].pos.x = j;
				m_Map[i][j].pos.y = i;
				m_Map[i][j].mapFlag = mapdata[i][j];
				_calcH( m_Map[i][j] );
				m_Map[i][j].G = 0;
			}
		}
		node = &m_Map[m_Start.y][m_Start.x];
		node->H = INT_MAX;
		mapdata[m_Start.y][m_Start.x]= AF_INOPENLIST;
		m_OpenList.add( node );

		return false;
	}

	if( !m_OpenList.isEmpty() )
	{
		m_OpenList.remove_Top( &node );
		m_CloseList.push( node );
		mapdata[node->pos.y][node->pos.x] = AF_INCLOSELIST;

		// 探索周围的八个方向
		m_Dir = 0;
		while ( m_Dir < DIR_NUM )
		{
			tempPos.x = node->pos.x + dirArray[m_Dir].x;
			tempPos.y = node->pos.y + dirArray[m_Dir].y;

			if ( isPointValid( mapdata, tempPos ) )
			{
				temp= &m_Map[tempPos.y][tempPos.x];

				// 完成寻路
				if( isPointEqual( temp->pos, m_End ) )
				{
					temp->pParent = node;
					return true;
				}

				if ( mapdata[temp->pos.y][temp->pos.x] == SPACE )
				{
					m_OpenList.add( temp );
					mapdata[temp->pos.y][temp->pos.x] = AF_INOPENLIST;
					// 由于这里使用了一个方向的数组,其中斜线方向的下标
					// 刚好是一个奇数,所以判断当前的方向是否为奇数即可
					// 判断是否为斜线方向
					if( _calcF( *temp, *node, (m_Dir%2 != 0) ) )
					{
						temp->pParent = node;
					}
				}
				else
				{
					if( mapdata[temp->pos.y][temp->pos.x] == AF_INOPENLIST &&
						_calcF( *temp, *node, (m_Dir%2 != 0) ) )
					{
						temp->pParent = node;
					}
					else
					if( mapdata[temp->pos.y][temp->pos.x] == AF_INCLOSELIST &&
						_calcF( *temp, *node, (m_Dir%2 != 0) ) )
					{
						m_OpenList.add( temp );
						m_CloseList.remove( temp );
						mapdata[temp->pos.y][temp->pos.x] = AF_INOPENLIST;
						temp->pParent = node;
					}
				}
			}

			++m_Dir;
		}
	}

	return false;
}
Beispiel #5
0
void BallMap::checkAndRemoveChain()
{
    BallSprite *ball;
    // 1. reset ingnore flag
    for (int i = 0; i < m_size.height * m_size.width; i++)
    {
        ball = m_matrix[i];
        if (!ball) {
            continue;
        }
        ball->setIgnoreCheck(false);
    }
    
    // 2. check chain
    std::list<BallSprite *> longerList;
    
    for (int i = 0; i < m_size.height * m_size.width; i++)
    {
        ball = m_matrix[i];
        if (!ball)
        {
            continue;
        }
        
        if (ball->getIsNeedRemove())
        {
            continue;// 已标记过的跳过检查
        }
        if (ball->getIgnoreCheck())
        {
//            continue;// 新变化的特殊寿司,不消除
        }
        
        // start count chain
        std::list<BallSprite *> colChainList;
        getColChain(ball, colChainList);
        
        if (colChainList.size() >= 3)
        {
            longerList.merge(colChainList);
        }
        
        std::list<BallSprite *> rowChainList;
        getRowChain(ball, rowChainList);
        
        if (rowChainList.size() >= 3)
        {
            longerList.merge(rowChainList);
        }

        if (longerList.size() < 3)
        {
            m_isNeedCheckSelf = false;
            
            TwoBallPos towBall = this->selfCheckHaveMore();

            if (isPointEqual(towBall.srcPos, towBall.destPos))
            {
                this->removeAllBall(true);
            }
            
            continue;// 小于3个不消除
        }
        
        std::list<BallSprite *>::iterator itList;
        
//        longerList.sort();  //sort the list
//        longerList.erase( unique( longerList.begin(), longerList.end() ), longerList.end());//Remove duplicate list values
        
        for (itList = longerList.begin(); itList != longerList.end(); itList++)
        {
            ball = dynamic_cast<BallSprite*>(*itList);
            
            if (!ball)
            {
                continue;
            }
        
            markRemove(ball);
        }
        
        // 如何是自由掉落产生的4消, 取最后一个变化为特殊寿司
        if (longerList.size() > 3)
        {
            ball->setIgnoreCheck(true);
            ball->setIsNeedRemove(false);
            ball->setDisplayMode(DISPLAY_MODE_FOUR);
        }
    }
    
    if (longerList.size() >= 3)
    {
        m_readyRemoveList = longerList;
    }
    // 3.消除标记了的寿司
    removeBall();
}