コード例 #1
0
ファイル: reduceFunction.cpp プロジェクト: Jiaran/MapReduce
void reduceFunction::getInfo(int fd, rio_t client, int id){
	char buf1[MAXLINE];
	int numBytes;
  	char * saveptr=NULL;
	
	while(true){
  		numBytes = Rio_readlineb(&client, buf1, MAXLINE);
		if(numBytes==0){
    		break;
  		}
		printf("buf1 is %s\n", buf1);
		char* d1 = strtok_r(buf1, "*",&saveptr);
		char ip[20];
		strcpy(ip,d1);
		char* d2 = strtok_r(NULL, "*",&saveptr);
		int port=atoi(d2);
		char* d3 = strtok_r(NULL, "* \r\n",&saveptr);
		int num=atoi(d3);
		mapNode mapWorker;
		strcpy(mapWorker.mapIp,ip);
		mapWorker.mapPort=port;
		mapAddress[num]=mapWorker;
	}
	getMapInfo();
	sendInfo();

}
コード例 #2
0
bool Pacman::checkCollision(unsigned int cellIndex, ViewDirection direction, bool isPlayer)
{
	int mapInfo = getMapInfo(cellIndex, direction);

	if(mapInfo == CellType::Wall || (isPlayer ? mapInfo == CellType::GhostDoor : false))
	{
		return true;
	}

	return false;
}
コード例 #3
0
/* WadArchive::detectMaps
 * Searches for any maps in the wad and adds them to the map list
 *******************************************************************/
vector<Archive::mapdesc_t> WadArchive::detectMaps()
{
	vector<mapdesc_t> maps;

	// Go through all lumps
	ArchiveEntry* entry = getEntry(0);
	bool lastentryismapentry = false;
	while (entry)
	{
		// UDMF format map check ********************************************************

		// Check for UDMF format map lump (TEXTMAP lump)
		if (entry->getName() == "TEXTMAP" && entry->prevEntry())
		{
			// Get map info
			mapdesc_t md = getMapInfo(entry->prevEntry());

			// Add to map list
			if (md.head != NULL)
			{
				entry = md.end;
				maps.push_back(md);
			}

			// Current index is ENDMAP, we don't want to check for a doom/hexen format
			// map so just go to the next index and continue the loop
			entry = entry->nextEntry();
			continue;
		}

		// Doom/Hexen format map check **************************************************
		// TODO maybe get rid of code duplication by calling getMapInfo() here too?

		// Array to keep track of what doom/hexen map lumps have been found
		uint8_t existing_map_lumps[NUMMAPLUMPS];
		memset(existing_map_lumps, 0, NUMMAPLUMPS);

		// Check if the current lump is a doom/hexen map lump
		bool maplump_found = false;
		for (int a = 0; a < 5; a++)
		{
			// Compare with all base map lump names
			if (S_CMP(entry->getName(), map_lumps[a]))
			{
				maplump_found = true;
				existing_map_lumps[a] = 1;
				break;
			}
		}

		// If we've found what might be a map
		if (maplump_found && entry->prevEntry())
		{
			// Save map header entry
			ArchiveEntry* header_entry = entry->prevEntry();

			// Check off map lumps until we find a non-map lump
			bool done = false;
			while (!done)
			{
				// Loop will end if no map lump is found
				done = true;

				// Compare with all map lump names
				for (int a = 0; a < NUMMAPLUMPS; a++)
				{
					// Compare with all base map lump names
					if (S_CMP(entry->getName(), map_lumps[a]))
					{
						existing_map_lumps[a] = 1;
						done = false;
						break;
					}
				}

				// If we're at the end of the wad, exit the loop
				if (!entry->nextEntry())
				{
					lastentryismapentry = true;
					break;
				}

				// Go to next lump if there is one
				if (!lastentryismapentry) entry = entry->nextEntry();
			}

			// Go back to the lump just after the last map lump found, but only if we actually moved
			if (!lastentryismapentry) entry = entry->prevEntry();

			// Check that we have all the required map lumps: VERTEXES, LINEDEFS, SIDEDEFS, THINGS & SECTORS
			if (!memchr(existing_map_lumps, 0, 5))
			{
				// Get map info
				mapdesc_t md;
				md.head = header_entry;				// Header lump
				md.name = header_entry->getName();	// Map title
				md.end = lastentryismapentry ?		// End lump
				         entry : entry->prevEntry();

				// If BEHAVIOR lump exists, it's a hexen format map
				if (existing_map_lumps[LUMP_BEHAVIOR])
					md.format = MAP_HEXEN;
				// If LEAFS, LIGHTS and MACROS exist, it's a doom 64 format map
				else if (existing_map_lumps[LUMP_LEAFS] && existing_map_lumps[LUMP_LIGHTS]
				         && existing_map_lumps[LUMP_MACROS])
					md.format = MAP_DOOM64;
				// Otherwise it's doom format
				else
					md.format = MAP_DOOM;

				// Add map info to the maps list
				maps.push_back(md);
			}
		}

		// Embedded WAD check (for Doom 64)
		if (entry->getType()->getFormat() == "archive_wad")
		{
			// Detect map format (probably kinda slow but whatever, no better way to do it really)
			Archive* tempwad = new WadArchive();
			tempwad->open(entry);
			vector<mapdesc_t> emaps = tempwad->detectMaps();
			if (emaps.size() > 0)
			{
				mapdesc_t md;
				md.head = entry;
				md.end = entry;
				md.archive = true;
				md.name = entry->getName(true).Upper();
				md.format = emaps[0].format;
				maps.push_back(md);
			}
			delete tempwad;
			entry->unlock();
		}

		// Not a UDMF or Doom/Hexen map lump, go to next lump
		entry = entry->nextEntry();
	}

	// Set all map header entries to ETYPE_MAP type
	for (size_t a = 0; a < maps.size(); a++)
		if (!maps[a].archive)
			maps[a].head->setType(EntryType::mapMarkerType());

	// Update entry map format hints
	for (unsigned a = 0; a < maps.size(); a++)
	{
		string format;
		if (maps[a].format == MAP_DOOM)
			format = "doom";
		else if (maps[a].format == MAP_DOOM64)
			format = "doom64";
		else if (maps[a].format == MAP_HEXEN)
			format = "hexen";
		else
			format = "udmf";

		ArchiveEntry* entry = maps[a].head;
		while (entry && entry != maps[a].end->nextEntry())
		{
			entry->exProp("MapFormat") = format;
			entry = entry->nextEntry();
		}
	}

	return maps;
}
コード例 #4
0
Pacman::ViewDirection Pacman::getRandomDirection(unsigned int cellIndex, unsigned int ignoreCell)
{
	std::vector<ViewDirection> randomDirection;
	ViewDirection ignoreDirection = ViewDirection::Up; // Inicializamos su valor para que no se queje el compilador

	if(ignoreCell != m_mapNullCell)
	{
		if(ignoreCell == getMapIndex(cellIndex, ViewDirection::Right))
		{
			ignoreDirection = ViewDirection::Right;
		}
		else if(ignoreCell == getMapIndex(cellIndex, ViewDirection::Left))
		{
			ignoreDirection = ViewDirection::Left;
		}
		else if(ignoreCell == getMapIndex(cellIndex, ViewDirection::Down))
		{
			ignoreDirection = ViewDirection::Down;
		}
		else
		{
			ignoreDirection = ViewDirection::Up;
		}

		if(getMapInfo(cellIndex, ViewDirection::Right) != CellType::Wall)
		{
			if(ignoreDirection != ViewDirection::Right)
			{
				randomDirection.push_back(ViewDirection::Right);
			}
		}

		if(getMapInfo(cellIndex, ViewDirection::Left) != CellType::Wall)
		{
			if(ignoreDirection != ViewDirection::Left)
			{
				randomDirection.push_back(ViewDirection::Left);
			}
		}

		if(getMapInfo(cellIndex, ViewDirection::Up) != CellType::Wall)
		{
			if(ignoreDirection != ViewDirection::Up)
			{
				randomDirection.push_back(ViewDirection::Up);
			}
		}

		if(getMapInfo(cellIndex, ViewDirection::Down) != CellType::Wall)
		{
			if(ignoreDirection != ViewDirection::Down)
			{
				randomDirection.push_back(ViewDirection::Down);
			}
		}
	}
	else
	{
		if(getMapInfo(cellIndex, ViewDirection::Right) != CellType::Wall)
		{
			randomDirection.push_back(ViewDirection::Right);
		}

		if(getMapInfo(cellIndex, ViewDirection::Left) != CellType::Wall)
		{
			randomDirection.push_back(ViewDirection::Left);
		}

		if(getMapInfo(cellIndex, ViewDirection::Up) != CellType::Wall)
		{
			randomDirection.push_back(ViewDirection::Up);
		}

		if(getMapInfo(cellIndex, ViewDirection::Down) != CellType::Wall)
		{
			randomDirection.push_back(ViewDirection::Down);
		}
	}

	if(randomDirection.empty())
	{
		return ignoreDirection;
	}
	else
	{
		return randomDirection[rand() % randomDirection.size()];
	}
}
コード例 #5
0
void Pacman::onTick()
{
	// Cambiar dirección deseada de Pacman
	if(m_input->getKeyPress()->Up.value)
	{
		m_player.DesiredDirection = ViewDirection::Up;
	}
	else if(m_input->getKeyPress()->Right.value)
	{
		m_player.DesiredDirection = ViewDirection::Right;
	}
	else if(m_input->getKeyPress()->Left.value)
	{
		m_player.DesiredDirection = ViewDirection::Left;
	}
	else if(m_input->getKeyPress()->Down.value)
	{
		m_player.DesiredDirection = ViewDirection::Down;
	}

	if(m_readyTimer.getTicks() > 0)
	{
		if(m_readyTimer.getTicks() >= 2000)
		{
			m_readyTimer.stop();
			m_gameTimer.start();
			m_frameTimer.start();
		}

		return;
	}

	if(m_pauseTimer.getTicks() > 0)
	{
		if(m_pauseTimer.getTicks() >= 500)
		{
			m_pauseTimer.stop();

			m_gameTimer.resume();
			m_frameTimer.resume();
			if(m_ghostKillTimer.isPaused())
			{
				m_ghostKillTimer.resume();
			}

			if(m_player.IsDead)
			{
				resetGame();
			}
		}

		return;
	}

	// Animar Pacman y Fantasmas (Cambio de Frames)
	if(m_frameTimer.getTicks() > 50)
	{
		if(m_player.Moving)
		{
			m_player.CurrentFrame++;

			if(m_player.CurrentFrame > 3)
			{
				m_player.CurrentFrame = 0;
			}
		}
		else
		{
			m_player.CurrentFrame = 1;
		}

		for(unsigned int x = 0; x < m_ghostCount; ++x)
		{
			m_ghost[x].CurrentFrame++;

			if(m_ghost[x].CurrentFrame > 1)
			{
				m_ghost[x].CurrentFrame = 0;
			}
		}

		m_frameTimer.start();
	}

	// Mover Pacman
	moveEntity(&m_player.CurrentCellIndex, &m_player.PositionOffset, &m_player.Direction, &m_player.DesiredDirection, &m_player.Moving, m_player.MoveSpeed, true);

	bool ghostForceRefresh = false;

	sf::Vector2i findCoin = sf::Vector2i(m_player.CurrentCellIndex % m_mapSize.x, floor(m_player.CurrentCellIndex / m_mapSize.x));

	// Comer bolas (Pacman) y actualizar mapa
	for(unsigned int x = 0; x < m_ballPosition.size(); ++x)
	{
		if(m_ballPosition[x] == findCoin)
		{
			mapSetCell(m_player.CurrentCellIndex, CellType::Nothing);

			m_ballPosition.erase(m_ballPosition.begin() + x);

			addScore(10);

			m_sound.setBuffer(m_ballSound);
			m_sound.play();

			if(m_ballPosition.empty() && m_ballBigPosition.empty())
			{
				resetGame();

				return;
			}
		}
	}

	for(unsigned int x = 0; x < m_ballBigPosition.size(); ++x)
	{
		if(m_ballBigPosition[x] == findCoin)
		{
			mapSetCell(m_player.CurrentCellIndex, CellType::Nothing);

			m_ballBigPosition.erase(m_ballBigPosition.begin() + x);

			m_powerupKillCounter = 0;
			m_ghostKillTimer.start();
			ghostForceRefresh = true;

			m_sound.setBuffer(m_powerupSound);
			m_sound.play();

			for(unsigned int x = 0; x < m_ghostCount; ++x)
			{
				m_ghost[x].Invulnerable = false;
			}

			addScore(50);

			if(m_ballPosition.empty() && m_ballBigPosition.empty())
			{
				resetGame();

				return;
			}
		}
	}

	if(m_ghostKillTimer.getTicks() > 7000)
	{
		m_ghostKillTimer.stop();

		for(unsigned int x = 0; x < m_ghostCount; ++x)
		{
			m_ghost[x].Invulnerable = false;

			m_ghost[x].LastStateChange = m_gameTimer.getTicks();
			m_ghost[x].StateDuration = 3000;
			m_ghost[x].IsInChase = true;
		}

		ghostForceRefresh = true;
	}

	// IA Fantasmas
	for(unsigned int x = 0; x < m_ghostCount; ++x)
	{
		if(m_ghost[x].TimeToRelease >= m_gameTimer.getTicks())
		{
			break;
		}

		unsigned int ignoreCell;

		if(m_ghost[x].Direction == ViewDirection::Left)
		{
			ignoreCell = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Right);
		}
		else if(m_ghost[x].Direction == ViewDirection::Right)
		{
			ignoreCell = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Left);
		}
		else if(m_ghost[x].Direction == ViewDirection::Up)
		{
			ignoreCell = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Down);
		}
		else
		{
			ignoreCell = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Up);
		}

		float ghostMoveSpeed = m_ghostKillTimer.isStarted() && (m_ghost[x].IsDead ? false : !m_ghost[x].Invulnerable) ?
			(m_ghost[x].MoveSpeed / 2) :
			(m_ghost[x].IsDead ? m_ghost[x].MoveSpeed * 1.5: m_ghost[x].MoveSpeed);

		if(m_ghost[x].CurrentCellIndex == m_ghost[x].HomeIndex)
		{
			m_ghost[x].IsDead = false;

			if(m_ghostKillTimer.isStarted())
			{
				m_ghost[x].Invulnerable = true;
			}

			m_ghost[x].LastStateChange = m_gameTimer.getTicks();
			m_ghost[x].StateDuration = 1000;
			m_ghost[x].IsInChase = true;
		}

		if(m_gameTimer.getTicks() >= (m_ghost[x].LastStateChange + m_ghost[x].StateDuration))
		{
			if(!m_ghostKillTimer.isStarted())
			{
				ghostForceRefresh = true;
			}

			m_ghost[x].LastStateChange = m_gameTimer.getTicks();

			if(m_ghost[x].IsInChase)
			{
				m_ghost[x].IsInChase = false;
				m_ghost[x].StateDuration = (3000 + (rand() % 3000));
			}
			else
			{
				m_ghost[x].IsInChase = true;
				m_ghost[x].StateDuration = (5000 + (rand() % 3000));
			}
		}

		if(m_ghost[x].LastCellChecked != m_ghost[x].CurrentCellIndex || ghostForceRefresh)
		{
			if(m_ghost[x].IsDead)
			{
				m_ghost[x].DesiredDirection = getNextDirection(m_ghost[x].CurrentCellIndex, m_ghost[x].HomeIndex, ignoreCell);

			}
			else if(m_ghost[x].IsInChase && (!m_ghostKillTimer.isStarted() || m_ghost[x].Invulnerable))
			{
				if(m_ghost[x].CurrentCellIndex != m_player.CurrentCellIndex)
				{
					m_ghost[x].DesiredDirection = getNextDirection(m_ghost[x].CurrentCellIndex, m_player.CurrentCellIndex, ignoreCell);
				}
			}
			else
			{
				unsigned int tempIndex;
				std::vector<unsigned int> possibleCells;

				if(m_ghost[x].Direction != ViewDirection::Left)
				{
					tempIndex = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Left);

					if(tempIndex != ignoreCell && m_mapInfo[tempIndex] != CellType::Wall)
					{
						possibleCells.push_back(tempIndex);
					}
				}

				if(m_ghost[x].Direction != ViewDirection::Right)
				{
					tempIndex = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Right);

					if(tempIndex != ignoreCell && m_mapInfo[tempIndex] != CellType::Wall)
					{
						possibleCells.push_back(tempIndex);
					}
				}

				if(m_ghost[x].Direction != ViewDirection::Up)
				{
					tempIndex = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Up);

					if(tempIndex != ignoreCell && m_mapInfo[tempIndex] != CellType::Wall)
					{
						possibleCells.push_back(tempIndex);
					}
				}

				if(m_ghost[x].Direction != ViewDirection::Down)
				{
					tempIndex = getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Down);

					if(tempIndex != ignoreCell && m_mapInfo[tempIndex] != CellType::Wall)
					{
						possibleCells.push_back(tempIndex);
					}
				}

				// Si hay caminos alternativos a seguir (Que no sea ni a donde se dirige ni la casilla anterior) o es un callejón sin salida...
				if(possibleCells.size() > 0 || getMapInfo(m_ghost[x].CurrentCellIndex, m_ghost[x].Direction) == CellType::Wall)
				{
					m_ghost[x].DesiredDirection = getRandomDirection(m_ghost[x].CurrentCellIndex, ignoreCell);
				}
			}

			m_ghost[x].LastCellChecked = m_ghost[x].CurrentCellIndex;
		}

		moveEntity(&m_ghost[x].CurrentCellIndex, &m_ghost[x].PositionOffset, &m_ghost[x].Direction, &m_ghost[x].DesiredDirection, &m_ghost[x].Moving, ghostMoveSpeed, false);

		if(!m_ghost[x].IsDead)
		{
			// Calcular colisión con Pacman
			if(m_ghost[x].CurrentCellIndex == m_player.CurrentCellIndex)
			{
				ghostCollide(x);
			}
			else if(getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Left) == m_player.CurrentCellIndex)
			{
				if((m_player.PositionOffset.x + (m_tileSize.x / 2)) - (m_ghost[x].PositionOffset.x + (m_tileSize.x / 2)) >= 0)
				{
					ghostCollide(x);
				}
			}
			else if(getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Right) == m_player.CurrentCellIndex)
			{
				if((m_ghost[x].PositionOffset.x + (m_tileSize.x / 2)) - (m_player.PositionOffset.x + (m_tileSize.x / 2)) >= 0)
				{
					ghostCollide(x);
				}
			}
			else if(getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Up) == m_player.CurrentCellIndex)
			{
				if((m_player.PositionOffset.y + (m_tileSize.y / 2)) - (m_ghost[x].PositionOffset.y + (m_tileSize.y / 2)) >= 0)
				{
					ghostCollide(x);
				}
			}
			else if(getMapIndex(m_ghost[x].CurrentCellIndex, ViewDirection::Down) == m_player.CurrentCellIndex)
			{
				if((m_ghost[x].PositionOffset.y + (m_tileSize.y / 2)) - (m_player.PositionOffset.y + (m_tileSize.y / 2)) >= 0)
				{
					ghostCollide(x);
				}
			}
		}
	}
}