void PlayerInterface::netMessageAllianceRequest( NetMessage *message )
{
    PlayerAllianceRequest *allie_request;
    PlayerState *player_state;

    allie_request = (PlayerAllianceRequest *) message;

    if ( allie_request->alliance_request_type == _player_make_alliance ) {
        setAlliance( allie_request->allie_by_player_index, allie_request->allie_with_player_index );

        if( (allie_request->allie_by_player_index == local_player_index) ) {
            player_state = getPlayerState( allie_request->allie_with_player_index );
            ConsoleInterface::postMessage( "Alliance created with %s.",
                    player_state->getName().c_str() );
        } else
            if( (allie_request->allie_with_player_index == local_player_index) ) {
                player_state = getPlayerState( allie_request->allie_by_player_index );
                ConsoleInterface::postMessage( "%s has allied with you.",
                        player_state->getName().c_str() );
            }
    } else {
        clearAlliance( allie_request->allie_by_player_index, allie_request->allie_with_player_index );

        if( (allie_request->allie_by_player_index == local_player_index) ) {
            player_state = getPlayerState( allie_request->allie_with_player_index );
            ConsoleInterface::postMessage( "Alliance broken with %s.",
                    player_state->getName().c_str() );
        } else
            if( (allie_request->allie_with_player_index == local_player_index) ) {
                player_state = getPlayerState( allie_request->allie_by_player_index );
                ConsoleInterface::postMessage(
                        "%s has broken their alliance with you.",
                        player_state->getName().c_str() );
            }

    }

    PlayerAllianceUpdate allie_update;

    allie_update.allie_by_player_index   = allie_request->allie_by_player_index;
    allie_update.allie_with_player_index = allie_request->allie_with_player_index;
    allie_update.alliance_update_type = allie_request->alliance_request_type;

    SERVER->sendMessage( &allie_update, sizeof( PlayerAllianceUpdate ), 0 );
}
void PlayerInterface::netMessageAllianceUpdate( NetMessage *message )
{
    PlayerAllianceUpdate *allie_update;
    PlayerState *player_state;

    allie_update = (PlayerAllianceUpdate *) message;

    if ( allie_update->alliance_update_type == _player_make_alliance ) {
        setAlliance( allie_update->allie_by_player_index, allie_update->allie_with_player_index );

        if( (allie_update->allie_by_player_index == local_player_index) ) {
            player_state = getPlayerState( allie_update->allie_with_player_index );
            ConsoleInterface::postMessage( "Alliance created with %s.",
                    player_state->getName().c_str() );
        } else
            if( (allie_update->allie_with_player_index == local_player_index) ) {
                player_state = getPlayerState( allie_update->allie_by_player_index );
                ConsoleInterface::postMessage( "%s has allied with you.",
                        player_state->getName().c_str() );
            }
    } else {
        clearAlliance( allie_update->allie_by_player_index, allie_update->allie_with_player_index );

        if( (allie_update->allie_by_player_index == local_player_index) ) {
            player_state = getPlayerState( allie_update->allie_with_player_index );
            ConsoleInterface::postMessage( "Alliance broken with %s.",
                    player_state->getName().c_str() );
        } else
            if( (allie_update->allie_with_player_index == local_player_index) ) {
                player_state = getPlayerState( allie_update->allie_by_player_index );
                ConsoleInterface::postMessage(
                        "%s has broken their alliance with you.",
                        player_state->getName().c_str() );
            }
    }
}
static void handleAllianceMessage(const int type,
                                  const int by_player,
                                  const int with_player)
{
    const int local_player = PlayerInterface::getLocalPlayerIndex();
    PlayerState *player_state;
    if ( type == _player_make_alliance )
    {
        setAlliance(by_player, with_player);

        if ( by_player == local_player )
        {
            player_state = PlayerInterface::getPlayer(with_player);
            if ( PlayerInterface::isSingleAllied(player_state->getID(), local_player) )
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "You accepted %s alliance request.",
                                              player_state->getName().c_str());
            }
            else
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "You request alliance with %s.",
                                              player_state->getName().c_str());
            }
        }
        else if ( with_player == local_player )
        {
            player_state = PlayerInterface::getPlayer(by_player);
            if ( PlayerInterface::isSingleAllied( local_player, player_state->getID()) )
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                           "%s accepted your alliance request.",
                                           player_state->getName().c_str());
            }
            else
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "%s request to ally with you.",
                                              player_state->getName().c_str());
            }
        }
    }
    else
    {
        // break alliance cancels both alliances
        clearAlliance(by_player, with_player);
        if ( by_player == local_player )
        {
            player_state = PlayerInterface::getPlayer(with_player);
            if ( PlayerInterface::isSingleAllied(player_state->getID(), local_player) )
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "You broke the alliance with %s.",
                                              player_state->getName().c_str());
            }
            else
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "You cancel your alliance request with %s.",
                                              player_state->getName().c_str());
            }
        }
        else if ( with_player == local_player )
        {
            player_state = PlayerInterface::getPlayer(by_player);
            if ( PlayerInterface::isSingleAllied( local_player, player_state->getID()) )
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "%s broke the alliance with you.",
                                              player_state->getName().c_str());
            }
            else
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "%s cancelled the alliance request with you.",
                                              player_state->getName().c_str());
            }
        }

        clearAlliance(with_player, by_player);
    }
}
void PlayerInterface::netMessageAllianceUpdate(const NetMessage* message)
{
    PlayerState *player_state;

    const PlayerAllianceUpdate* allie_update
        = (const PlayerAllianceUpdate *) message;

    if(allie_update->getAllieByPlayerIndex() >= max_players
       || allie_update->getAllieWithPlayerIndex() >= max_players) {
        LOGGER.warning("Invalid alliance update message");
        return;
    }

    SDL_mutexP(mutex);
    if (allie_update->alliance_update_type == _player_make_alliance)
    {
        setAlliance(  allie_update->getAllieByPlayerIndex(),
					  allie_update->getAllieWithPlayerIndex());

        if ( allie_update->getAllieByPlayerIndex() == local_player_index )
		{
            player_state = getPlayer( allie_update->getAllieWithPlayerIndex() );
			if ( isSingleAllied( player_state->getID(), local_player_index) )
			{
				ConsoleInterface::postMessage( Color::yellow, false, 0, "You accepted %s alliance request.",
												player_state->getName().c_str() );
			}
			else
			{
				ConsoleInterface::postMessage( Color::yellow, false, 0, "You request alliance with %s.",
												player_state->getName().c_str() );
			}
        }
		else if ( allie_update->getAllieWithPlayerIndex() == local_player_index )
		{
			player_state = getPlayer( allie_update->getAllieByPlayerIndex() );
			if ( isSingleAllied( local_player_index, player_state->getID() ) )
			{
				ConsoleInterface::postMessage( Color::yellow, false, 0, "%s accepted your alliance request.",
												player_state->getName().c_str() );
			}
			else
			{
				ConsoleInterface::postMessage( Color::yellow, false, 0, "%s request to ally with you.",
												player_state->getName().c_str() );
			}
        }
    }
    else
    {
		// break alliance cancels both alliances
		clearAlliance( allie_update->getAllieByPlayerIndex(),
                       allie_update->getAllieWithPlayerIndex());

        if ( allie_update->getAllieByPlayerIndex() == local_player_index )
		{
            player_state = getPlayer( allie_update->getAllieWithPlayerIndex() );
			if ( isSingleAllied( player_state->getID(), local_player_index) )
			{
				ConsoleInterface::postMessage( Color::yellow, false, 0, "You broke the alliance with %s.",
												player_state->getName().c_str() );
			}
			else
			{
				ConsoleInterface::postMessage( Color::yellow, false, 0, "You cancel your alliance request with %s.",
												player_state->getName().c_str() );
			}
        }
		else if ( allie_update->getAllieWithPlayerIndex() == local_player_index )
		{
			player_state = getPlayer( allie_update->getAllieByPlayerIndex() );
			if ( isSingleAllied( local_player_index, player_state->getID()) )
			{
				ConsoleInterface::postMessage( Color::yellow, false, 0, "%s broke the alliance with you.",
												player_state->getName().c_str() );
			}
			else
			{
				ConsoleInterface::postMessage( Color::yellow, false, 0, "%s cancelled the alliance request with you.",
												player_state->getName().c_str() );
			}
		}
		
		clearAlliance( allie_update->getAllieWithPlayerIndex(),
						allie_update->getAllieByPlayerIndex() );
    }
    SDL_mutexV(mutex);
}
void PlayerInterface::netMessageAllianceRequest(const NetMessage *message)
{
    if ( gameconfig->allowallies == false )
    {
        return;
    }
	
    PlayerState *player_state;

    const PlayerAllianceRequest *allie_request
        = (const PlayerAllianceRequest *) message;

    if(allie_request->getAllieByPlayerIndex() >= max_players
       || allie_request->getAllieWithPlayerIndex() >= max_players)
    {
        LOGGER.warning("Invalid alliance request message");
        return;                                                       
    }

    SDL_mutexP(mutex);
    if ( allie_request->alliance_request_type == _player_make_alliance )
    {
        setAlliance(allie_request->getAllieByPlayerIndex(),
                    allie_request->getAllieWithPlayerIndex());

        if ( allie_request->getAllieByPlayerIndex() == local_player_index )
        {
            player_state = getPlayer(allie_request->getAllieWithPlayerIndex());
            if ( isSingleAllied( player_state->getID(), local_player_index) )
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "You accepted %s alliance request.",
                                              player_state->getName().c_str());
            }
            else
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "You request alliance with %s.",
                                              player_state->getName().c_str());
            }
        }
        else if ( allie_request->getAllieWithPlayerIndex() == local_player_index )
        {
            player_state = getPlayer( allie_request->getAllieByPlayerIndex() );
            if ( isSingleAllied( local_player_index, player_state->getID()) )
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                           "%s accepted your alliance request.",
                                           player_state->getName().c_str());
            }
            else
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "%s request to ally with you.",
                                              player_state->getName().c_str());
            }
        }
    }
    else
    {
        // break alliance cancels both alliances
        clearAlliance( allie_request->getAllieByPlayerIndex(),
                       allie_request->getAllieWithPlayerIndex());

        if ( allie_request->getAllieByPlayerIndex() == local_player_index )
        {
            player_state = getPlayer( allie_request->getAllieWithPlayerIndex() );
            if ( isSingleAllied( player_state->getID(), local_player_index) )
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "You broke the alliance with %s.",
                                              player_state->getName().c_str());
            }
            else
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "You cancel your alliance request with %s.",
                                              player_state->getName().c_str());
            }
        }
        else if ( allie_request->getAllieWithPlayerIndex() == local_player_index )
        {
            player_state = getPlayer( allie_request->getAllieByPlayerIndex() );
            if ( isSingleAllied( local_player_index, player_state->getID()) )
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "%s broke the alliance with you.",
                                              player_state->getName().c_str());
            }
            else
            {
                ConsoleInterface::postMessage(Color::yellow, false, 0,
                                              "%s cancelled the alliance request with you.",
                                              player_state->getName().c_str());
            }
        }

        clearAlliance(allie_request->getAllieWithPlayerIndex(),
                      allie_request->getAllieByPlayerIndex());
    }
    SDL_mutexV(mutex);

    PlayerAllianceUpdate allie_update;
    allie_update.set(allie_request->getAllieByPlayerIndex(),
                     allie_request->getAllieWithPlayerIndex(),
                     allie_request->alliance_request_type);
    //NetworkServer::broadcastMessage(&allie_update, sizeof(PlayerAllianceUpdate));
    if ( allie_request->getAllieByPlayerIndex() != local_player_index )
    {
        NetworkServer::sendMessage(allie_request->getAllieByPlayerIndex(),
                                   &allie_update, sizeof(PlayerAllianceUpdate));
    }
    if ( allie_request->getAllieWithPlayerIndex() != local_player_index )
    {
        NetworkServer::sendMessage(allie_request->getAllieWithPlayerIndex(),
                                   &allie_update, sizeof(PlayerAllianceUpdate));
    }
}
Exemplo n.º 6
0
void DriverStation::setAlliance (int alliance)
{
    setAlliance ((DS_Alliance) alliance);
}
Exemplo n.º 7
0
int main(int argc, char* argv[]) {
	printf("SimpleDS\n");
	printf("Build: " VERSION_STR " (" SYSTEM_NAME " " SYSTEM_PROCESSOR ")");
#ifdef SDL_HAPTIC_DISABLED
	printf(" - No Haptic");
#endif
	printf("\nAuthors: " VERSION_AUTHORS "\n");

	args = std::vector<const char*>(argv, argv + argc);
	int offset = 1;

	std::string configFile = "./simpleds.conf";
	if (hasOpt("-c") || hasOpt("--config")) {
		configFile = getOpt("-c");
		if (configFile.size() == 0) {
			configFile = getOpt("--config");
		}
		offset += 2;
	}
	config = new Config(configFile);
	if (config->loaded) {
		printf("Config: %s\n", configFile.c_str());
		if (config->has("DS.foo")) {
			printf("Foo: %s\n", config->getString("DS.foo").c_str());
		}
	} else {
		printf("Config file couldn't be loaded, won't be able to save\n");
	}

	uint16_t teamNum = (uint16_t)config->getInt32("DS.team");

	verbose = (hasOpt("-v") || hasOpt("--verbose"));

	if (hasOpt("-V") || hasOpt("--version")) {
		return 0;
	}

	if (hasOpt("-h") || hasOpt("--help")) {
		printUsage();
		printf("Options:\n");
		printf(" -h, --help      Prints this message\n");
		printf(" -v, --verbose   Output debugging information\n");
		printf(" -V, --version   Prints version info and exits\n");
		printf(" -c, --config    Sets the config file to use [default: ./simpleds.conf]\n");
		printf(" teamNum         The team number to use, must be provided here or in configuration file\n");
		return 0;
	}

	if (teamNum == 0 && args.begin() + offset == args.end()) { // We're out of arguments
		printUsage();
		return 1;
	} else {
		char* endptr;
		uint16_t argTeamNum = (uint16_t)std::strtol(args[offset], &endptr, 10);

		if (endptr == args[offset] || *endptr != '\0') { // No team number in config, and none provided
			if (teamNum == 0) {
				printUsage();
				return 1;
			}
		} else {
			teamNum = argTeamNum;
		}
	}

	printf("Team Number: %d\n", teamNum);

	bool quit = false;
	SDL_Event e;

	auto gui = new GUI();
	if (!gui->isValid()) {
		return 1;
	}

	config->setInt32("DS.team", teamNum);
	config->initInt32("DS.alliance", Alliance::RED);
	config->initInt32("DS.position", 1);

	DS::initialize(teamNum);
	auto ds = DS::getInstance();

	enum GUIMode { MAIN, INFO, JOYSTICKS, CONTROL, HELP, COUNT };
	GUIMode mode = GUIMode::MAIN;

	auto runner = std::async(std::launch::async, &DS::run, ds);
	std::map<GUIMode, Screen*> screens;
	screens[MAIN] = new ScreenMain();
	screens[INFO] = new ScreenInfo();
	screens[JOYSTICKS] = new ScreenJoysticks();
	screens[CONTROL] = new ScreenControl();
	screens[HELP] = new ScreenHelp();

	while (!quit) {
		while (gui->pollEvent(&e) != 0) {
			if (e.type == SDL_QUIT) {
				quit = true;
			} else if (e.type == SDL_JOYDEVICEREMOVED || e.type == SDL_JOYDEVICEADDED) {
				ds->setEnable(false);
				ds->loadJoysticks();
			} else if (e.type == SDL_KEYDOWN && e.key.repeat == 0) {
				auto key = e.key.keysym;
				if (key.sym == SDLK_e) {
					ds->toggleEnable();
				} else if (key.sym == SDLK_SPACE) {
					ds->setEnable(false);
				} else if (key.sym == SDLK_0) {
					ds->setEStop();
				} else if (key.sym == SDLK_q) {
					quit = true;
				} else if (key.sym == SDLK_r) {
					if (key.mod & KMOD_SHIFT) {
						ds->reboot();
					} else {
						ds->restartCode();
					}
				} else if (key.sym == SDLK_BACKQUOTE) {
					ds->setAlliance(ds->getAlliance() == Alliance::BLUE ? Alliance::RED : Alliance::BLUE);
				} else if (key.sym >= SDLK_1 && key.sym <= SDLK_9) {
					uint8_t keyNum = (uint8_t)(1 + key.sym - SDLK_1);
					if (key.mod & KMOD_CTRL && keyNum >= 1 && keyNum <= 3) {
						ds->setPosition(keyNum);
					} else if (key.mod & KMOD_SHIFT && keyNum >= 1 && keyNum <= 3) {
						ds->setEnable(false);
						ds->setMode((Mode)(keyNum - 1));
					} else if (keyNum >= 1 && keyNum <= GUIMode::COUNT) {
						mode = (GUIMode)(keyNum - 1);
					}
				}
			}
			screens[mode]->update(e);
		}
		if (gui->readyToDraw()) {
			gui->setOffset(10, 10);
			gui->clear();
			gui->drawScreen(screens[mode]);

			gui->setOffset(10, gui->getHeight() - gui->getCharSize().y);
			gui->drawText(0, 0, "1: Main", mode == GUIMode::MAIN ? Colors::BLACK : Colors::DISABLED);
			gui->drawTextRel(9, 0, "2: Info", mode == GUIMode::INFO ? Colors::BLACK : Colors::DISABLED);
			gui->drawTextRel(9, 0, "3: Joysticks", mode == GUIMode::JOYSTICKS ? Colors::BLACK : Colors::DISABLED);
			gui->drawTextRel(14, 0, "4: Control", mode == GUIMode::CONTROL ? Colors::BLACK : Colors::DISABLED);
			gui->drawTextRel(12, 0, "5: Help", mode == GUIMode::HELP ? Colors::BLACK : Colors::DISABLED);

			gui->render();
		}

		std::string s = narf::util::format("Team %d", teamNum);
		if (ds->isConnected()) {
			auto rio = ds->getRoboRIO();
			s += " - ";
			if (rio->getEStop()) {
				s += "E-Stopped";
			} else {
				s += narf::util::format("%s %s", modeNames[rio->getMode()].c_str(), rio->getEnable() ? "Enabled" : "Disabled");
			}
			s += narf::util::format(" - %s %d", allianceNames[ds->getAlliance()].c_str(), ds->getPosition());
			if (!rio->getCode()) {
				s += " - No Code";
			}
		} else {
			s += " - No Comms";
		}
		gui->setTitle(s);

		ds->updateJoysticks();
		SDL_Delay(25);
	}

	ds->saveJoysticks();
	ds->stop();
	SDL_Quit();
	return 0;
}