Пример #1
0
void GameState::serverLoop(bool generateMap, size_t player, std::string username) {

	readyClients = 1;
	totalClients = stillConnected = player;

	if (generateMap)
		map = generator->generate(32, 25, 2);
	else
		readMapFromFile();

	mapData = map->printTiles();

	auto pos = findFreePosition();

	compressedMap = "";
	for (int i = 0; i < map->getHeight(); i++)
	{
		std::string line = "";
		for (int j = 0; j < map->getWidth(); j++)
		{
			line += std::to_string( (unsigned int)mapData[i][j] );
		}
		compressedMap += line;
	}

	nameList = new std::set<std::string>();
	endpoint = new ip::tcp::endpoint(ip::tcp::v4(), GameState::tcpPort);
	acceptor = new ip::tcp::acceptor(service, *endpoint);
	socket = new ip::tcp::socket(service);
	sessions = new std::vector<std::shared_ptr<ServerSession>>();
	entities = new std::vector<std::shared_ptr<Entity>>();
	entities->resize(player*3);
	
	{
		auto position = findFreePosition();
		reserveMap(position.x, position.y);
		character = std::make_shared<Entity>(position.x, position.y, this, true, 0);
		(*entities)[0] = character;
	}

	for (auto i = player; i < player * 3; i++)
	{
		auto position = findFreePosition();
		reserveMap(position.x, position.y);
		(*entities)[i] = std::make_shared<Entity>(position.x, position.y, this, false, i);
	}

	sessions->push_back(nullptr);
	registerName(username);

	vacantPlayers = player - 1;
	if (vacantPlayers == 0)
		inGame = true;

	accept();

	service.run();
	std::cout << "Finished server thread" << std::endl;
}
Пример #2
0
void GameState::serverLoop(bool generateMap, size_t player, std::string username) {
	yourID = 0;
	readyClients = 1;
	totalClients = stillConnected = player;

	if (generateMap)
		map = generator->generate(32, 25, 2);
	else
		readMapFromFile();

	mapData = map->printTiles();

	auto pos = findFreePosition();

	compressMap();

	nameList = new std::set<std::string>();
	endpoint = new ip::tcp::endpoint(ip::tcp::v4(), GameState::tcpPort);
	acceptor = new ip::tcp::acceptor(service, *endpoint);
	socket = new ip::tcp::socket(service);
	sessions = new std::vector<std::shared_ptr<ServerSession>>();
	entities = new std::vector<std::shared_ptr<Entity>>();
	entities->resize( Universal::ENTITY_COUNT );
	
	{
		auto position = findFreePosition();
		reserveMap(position.x, position.y);
		character = std::make_shared<Entity>(position.x, position.y, this, true, 0);
		(*entities)[0] = character;
	}

	for (auto i = player; i < Universal::ENTITY_COUNT; i++)
	{
		auto position = findFreePosition();
		reserveMap(position.x, position.y);
		(*entities)[i] = std::make_shared<Entity>(position.x, position.y, this, false, i);
	}

	sessions->push_back(nullptr);
	registerName(username);

	vacantPlayers = player - 1;
	if (vacantPlayers == 0)
		inGame = true;

	accept();

	service.run();
}