Add options to control the number of red/blue AI in soccer mode (#4063)

* Add options to control the number of red/blue AI in soccer mode (offline)

* fix a bug related to the upper bound when AI num=0 and trying to lower the number

* forgot to fix this

* remove the Balanced/Custom spinner

* improve the AI number spinner's behavior at lower bound

* Hide blue ai number setting if not soccer

* move getting red/blue players number to function TrackInfoScreen::getRedBluePlayerNumber()

* Tab to Space

* rename local variable m_xxx to num_xxx

* bug fix

* improve the default AI number: try the previously used one, if cannot use the previous, recalculate the AI number using balanced
This commit is contained in:
riso 2019-09-23 08:59:17 -05:00 committed by Benau
parent 1553d7b70f
commit 0547e1e469
5 changed files with 243 additions and 63 deletions

View File

@ -64,6 +64,15 @@
<label id="ai-text" proportion="3" I18N="In the track info screen" text="Number of AI karts" text_align="left" align="center"/>
</div>
<spacer width="1" height="1%"/>
<div width="100%" height="fit" layout="horizontal-row" id="ai-blue-div">
<div proportion="1" height="fit" layout="horizontal-row">
<spinner id="ai-blue-spinner" width="100%" min_value="1" max_value="20" align="center"
wrap_around="true" />
</div>
<spacer width="3%"/>
<label id="ai-blue-text" proportion="3" I18N="In the track info screen" text="Number of blue team AI karts" text_align="left" align="center"/>
</div>
<spacer width="1" height="1%"/>
<div width="100%" height="fit" layout="horizontal-row" >
<div proportion="1" height="fit" layout="horizontal-row">
<div width="100%" height="fit" text-align="center" layout="vertical-row" >

View File

@ -449,6 +449,12 @@ namespace UserConfigParams
PARAM_PREFIX StringUserConfigParam m_last_used_kart_group
PARAM_DEFAULT( StringUserConfigParam("all", "last_kart_group",
"Last selected kart group") );
PARAM_PREFIX IntUserConfigParam m_soccer_red_ai_num
PARAM_DEFAULT( IntUserConfigParam(0, "m_soccer_red_ai_num",
&m_race_setup_group, "Number of red AI karts in soccer mode.") );
PARAM_PREFIX IntUserConfigParam m_soccer_blue_ai_num
PARAM_DEFAULT( IntUserConfigParam(0, "m_soccer_blue_ai_num",
&m_race_setup_group, "Number of blue AI karts in soccer mode.") );
// ---- Wiimote data
PARAM_PREFIX GroupUserConfigParam m_wiimote_group

View File

@ -1549,37 +1549,8 @@ KartTeam World::getKartTeam(unsigned int kart_id) const
//-----------------------------------------------------------------------------
void World::setAITeam()
{
const int total_players = race_manager->getNumPlayers();
const int total_karts = race_manager->getNumberOfKarts();
// No AI
if ((total_karts - total_players) == 0) return;
int red_players = 0;
int blue_players = 0;
for (int i = 0; i < total_players; i++)
{
KartTeam team = race_manager->getKartInfo(i).getKartTeam();
// Happen in profiling mode
if (team == KART_TEAM_NONE)
{
race_manager->setKartTeam(i, KART_TEAM_BLUE);
team = KART_TEAM_BLUE;
continue; //FIXME, this is illogical
}
team == KART_TEAM_BLUE ? blue_players++ : red_players++;
}
int available_ai = total_karts - red_players - blue_players;
int additional_blue = red_players - blue_players;
m_blue_ai = (available_ai - additional_blue) / 2 + additional_blue;
m_red_ai = (available_ai - additional_blue) / 2;
if ((available_ai + additional_blue)%2 == 1)
(additional_blue < 0) ? m_red_ai++ : m_blue_ai++;
m_red_ai = UserConfigParams::m_soccer_red_ai_num;
m_blue_ai = UserConfigParams::m_soccer_blue_ai_num;
Log::debug("World", "Blue AI: %d red AI: %d", m_blue_ai, m_red_ai);

View File

@ -66,6 +66,9 @@ void TrackInfoScreen::loadedFromFile()
{
m_target_type_spinner = getWidget<SpinnerWidget>("target-type-spinner");
m_target_type_label = getWidget <LabelWidget>("target-type-text");
m_ai_blue_spinner = getWidget<SpinnerWidget>("ai-blue-spinner");
m_ai_blue_label = getWidget <LabelWidget>("ai-blue-text");
m_ai_blue_div = getWidget<Widget>("ai-blue-div");
m_target_type_div = getWidget<Widget>("target-type-div");
m_target_value_spinner = getWidget<SpinnerWidget>("target-value-spinner");
m_target_value_label = getWidget<LabelWidget>("target-value-text");
@ -102,6 +105,9 @@ void TrackInfoScreen::loadedFromFile()
m_is_soccer = false;
m_show_ffa_spinner = false;
m_red_players = 0;
m_blue_players = 0;
} // loadedFromFile
@ -116,6 +122,11 @@ void TrackInfoScreen::beforeAddingWidget()
m_target_type_div->setCollapsed(false, this);
else
m_target_type_div->setCollapsed(true, this);
if (race_manager->getMinorMode() == RaceManager::MINOR_MODE_SOCCER) // show 'Number of blue team AI karts' if soccer
m_ai_blue_div->setCollapsed(false, this);
else
m_ai_blue_div->setCollapsed(true, this);
} // beforeAddingWidget
// ----------------------------------------------------------------------------
@ -173,6 +184,10 @@ void TrackInfoScreen::init()
m_target_value_spinner->setVisible(false);
m_target_value_label->setVisible(false);
m_ai_blue_spinner->setVisible(false);
m_ai_blue_label->setVisible(false);
m_ai_blue_label->setActive(false);
// Soccer options
// -------------
if (m_is_soccer)
@ -254,39 +269,90 @@ void TrackInfoScreen::init()
if (has_AI)
{
m_ai_kart_spinner->setActive(true);
int num_ai = int(UserConfigParams::m_num_karts_per_gamemode
[race_manager->getMinorMode()]) - local_players;
// Avoid negative numbers (which can happen if e.g. the number of karts
// in a previous race was lower than the number of players now.
if (num_ai < 0) num_ai = 0;
m_ai_kart_spinner->setValue(num_ai);
race_manager->setNumKarts(num_ai + local_players);
// Set the max karts supported based on the battle arena selected
if(race_manager->getMinorMode()==RaceManager::MINOR_MODE_3_STRIKES ||
race_manager->getMinorMode()==RaceManager::MINOR_MODE_SOCCER)
if (m_is_soccer) // Soccer mode: red + blue AI
{
m_ai_kart_spinner->setMax(max_arena_players - local_players);
m_ai_blue_spinner->setVisible(true);
m_ai_blue_label->setVisible(true);
m_ai_blue_spinner->setActive(true);
m_ai_kart_label->setText(_("Number of red team AI karts"), false);
const int max_arena_players = m_track->getMaxArenaPlayers();
const int local_players = race_manager->getNumLocalPlayers();
const int num_ai = max_arena_players - local_players; // possible AI number
getRedBluePlayerNumber();
int num_blue_lower = (m_blue_players > 0) ? 0 : 1;
int num_red_lower = (m_red_players > 0) ? 0 : 1;
int num_blue_upper_hard = max_arena_players - local_players - num_red_lower; // possible upper bound
int num_red_upper_hard = max_arena_players - local_players - num_blue_lower;// possible upper bound
int num_red_ai = UserConfigParams::m_soccer_red_ai_num;
int num_blue_ai = UserConfigParams::m_soccer_blue_ai_num;
// Try the saved value, recalculate AI number (Balanced) if cannot use the saved values
if (!((num_red_ai >= num_red_lower) && (num_red_ai <= num_red_upper_hard) &&
(num_blue_ai >= num_blue_lower) && (num_blue_ai <= num_blue_upper_hard) &&
(num_red_ai + num_blue_ai <= num_ai)))
{
int additional_blue = m_red_players - m_blue_players;
num_blue_ai = (num_ai - additional_blue) / 2 + additional_blue;
num_red_ai = (num_ai - additional_blue) / 2;
if ((num_ai + additional_blue)%2 == 1)
(additional_blue < 0) ? num_red_ai++ : num_blue_ai++;
UserConfigParams::m_soccer_red_ai_num = num_red_ai;
UserConfigParams::m_soccer_blue_ai_num = num_blue_ai;
}
m_ai_kart_spinner->setMin(num_red_lower);
m_ai_blue_spinner->setMin(num_blue_lower);
m_ai_kart_spinner->setMax(std::min( num_red_ai + (num_red_ai == num_red_lower ? 0 : 1), num_red_upper_hard )); // +1 to allow adding AI
m_ai_blue_spinner->setMax(std::min( num_blue_ai + (num_blue_ai == num_blue_lower ? 0 : 1), num_blue_upper_hard )); // +1 to allow adding AI
// Set the values
m_ai_kart_spinner->setValue(UserConfigParams::m_soccer_red_ai_num);
m_ai_blue_spinner->setValue(UserConfigParams::m_soccer_blue_ai_num);
race_manager->setNumKarts(num_ai + local_players);
}
else
m_ai_kart_spinner->setMax(stk_config->m_max_karts - local_players);
// A ftl reace needs at least three karts to make any sense
if(race_manager->getMinorMode()==RaceManager::MINOR_MODE_FOLLOW_LEADER)
{
m_ai_kart_spinner->setMin(std::max(0, 3 - local_players));
}
// Make sure in battle and soccer mode at least 1 ai for single player
else if((race_manager->getMinorMode()==RaceManager::MINOR_MODE_3_STRIKES ||
race_manager->getMinorMode()==RaceManager::MINOR_MODE_SOCCER) &&
local_players == 1 &&
!UserConfigParams::m_artist_debug_mode)
m_ai_kart_spinner->setMin(1);
else
m_ai_kart_spinner->setMin(0);
m_ai_kart_label->setText(_("Number of AI karts"), false); // change to label back for other modes
int num_ai = int(UserConfigParams::m_num_karts_per_gamemode
[race_manager->getMinorMode()]) - local_players;
// Avoid negative numbers (which can happen if e.g. the number of karts
// in a previous race was lower than the number of players now.
if (num_ai < 0) num_ai = 0;
m_ai_kart_spinner->setValue(num_ai);
race_manager->setNumKarts(num_ai + local_players);
// Set the max karts supported based on the battle arena selected
if(race_manager->getMinorMode()==RaceManager::MINOR_MODE_3_STRIKES ||
race_manager->getMinorMode()==RaceManager::MINOR_MODE_SOCCER)
{
m_ai_kart_spinner->setMax(max_arena_players - local_players);
}
else
m_ai_kart_spinner->setMax(stk_config->m_max_karts - local_players);
// A ftl reace needs at least three karts to make any sense
if(race_manager->getMinorMode()==RaceManager::MINOR_MODE_FOLLOW_LEADER)
{
m_ai_kart_spinner->setMin(std::max(0, 3 - local_players));
}
// Make sure in battle and soccer mode at least 1 ai for single player
else if((race_manager->getMinorMode()==RaceManager::MINOR_MODE_3_STRIKES ||
race_manager->getMinorMode()==RaceManager::MINOR_MODE_SOCCER) &&
local_players == 1 &&
!UserConfigParams::m_artist_debug_mode)
m_ai_kart_spinner->setMin(1);
else
m_ai_kart_spinner->setMin(0);
}
} // has_AI
else
race_manager->setNumKarts(local_players);
@ -482,7 +548,16 @@ void TrackInfoScreen::onEnterPressedInternal()
int num_ai = 0;
if (has_AI)
num_ai = m_ai_kart_spinner->getValue();
{
if (m_is_soccer) // Soccer mode
{
num_ai = UserConfigParams::m_soccer_red_ai_num + UserConfigParams::m_soccer_blue_ai_num;
}
else // Other modes
{
num_ai = m_ai_kart_spinner->getValue();
}
}
const int selected_target_type = m_target_type_spinner->getValue();
const int selected_target_value = m_target_value_spinner->getValue();
@ -620,11 +695,115 @@ void TrackInfoScreen::eventCallback(Widget* widget, const std::string& name,
}
else if (name=="ai-spinner")
{
const int num_ai = m_ai_kart_spinner->getValue();
UserConfigParams::m_num_karts_per_gamemode[race_manager->getMinorMode()] = race_manager->getNumLocalPlayers() + num_ai;
updateHighScores();
if (m_is_soccer) // Soccer mode
{
const int max_arena_players = m_track->getMaxArenaPlayers();
const int local_players = race_manager->getNumLocalPlayers();
const int num_ai = max_arena_players - local_players; // possible AI number
int num_red = m_ai_kart_spinner->getValue();
int num_blue = m_ai_blue_spinner->getValue();
getRedBluePlayerNumber();
int num_blue_lower = (m_blue_players > 0) ? 0 : 1;
int num_red_lower = (m_red_players > 0) ? 0 : 1;
int num_blue_upper = num_ai - num_red; // upper bound determined by the new num_red
int num_blue_upper_hard = num_ai - num_red_lower; // possible upper bound
int num_red_upper = num_ai - num_blue; // upper bound determined by the new num_blue
int num_red_upper_hard = num_ai - num_blue_lower;// possible upper bound
// Check if need to change blue in response to the red change
if (num_blue > num_blue_upper)
{
num_blue = num_blue_upper; // change blue
num_red_upper = num_ai - num_blue; // recalculate the upper bound determined by the new num_blue
}
m_ai_kart_spinner->setMax(std::min( (num_red == num_red_lower ? num_red_upper_hard : num_red_upper + 1), num_red_upper_hard )); // cannot be higher than the hard upper limit
m_ai_blue_spinner->setMax(std::min( (num_blue == num_blue_lower ? num_blue_upper_hard : num_blue_upper + 1), num_blue_upper_hard )); // cannot be higher than the hard upper limit
m_ai_kart_spinner->setMin(num_red_lower);
m_ai_blue_spinner->setMin(num_blue_lower);
m_ai_kart_spinner->setValue(num_red);
m_ai_blue_spinner->setValue(num_blue);
UserConfigParams::m_soccer_red_ai_num = num_red;
UserConfigParams::m_soccer_blue_ai_num = num_blue;
UserConfigParams::m_num_karts_per_gamemode[race_manager->getMinorMode()] = race_manager->getNumLocalPlayers() + num_red + num_blue;
//updateHighScores();
}
else // Other modes
{
const int num_ai = m_ai_kart_spinner->getValue();
UserConfigParams::m_num_karts_per_gamemode[race_manager->getMinorMode()] = race_manager->getNumLocalPlayers() + num_ai;
updateHighScores();
}
}
else if (name == "ai-blue-spinner")
{
if (m_is_soccer) // AI distribution: Custom
{
const int max_arena_players = m_track->getMaxArenaPlayers();
const int local_players = race_manager->getNumLocalPlayers();
const int num_ai = max_arena_players - local_players; // possible AI number
int num_red = m_ai_kart_spinner->getValue();
int num_blue = m_ai_blue_spinner->getValue();
getRedBluePlayerNumber();
int num_blue_lower = (m_blue_players > 0) ? 0 : 1;
int num_red_lower = (m_red_players > 0) ? 0 : 1;
int num_blue_upper = num_ai - num_red; // upper bound determined by the new num_red
int num_blue_upper_hard = num_ai - num_red_lower; // possible upper bound
int num_red_upper = num_ai - num_blue; // upper bound determined by the new num_blue
int num_red_upper_hard = num_ai - num_blue_lower;// possible upper bound
// Check if need to change red in response to the blue change
if (num_red > num_red_upper)
{
num_red = num_red_upper; // change red
num_blue_upper = num_ai - num_red; // recalculate the upper bound determined by the new num_red
}
m_ai_kart_spinner->setMax(std::min( (num_red == num_red_lower ? num_red_upper_hard : num_red_upper + 1), num_red_upper_hard )); // cannot be higher than the hard upper limit
m_ai_blue_spinner->setMax(std::min( (num_blue == num_blue_lower ? num_blue_upper_hard : num_blue_upper + 1), num_blue_upper_hard )); // cannot be higher than the hard upper limit
m_ai_kart_spinner->setMin(num_red_lower);
m_ai_blue_spinner->setMin(num_blue_lower);
m_ai_kart_spinner->setValue(num_red);
m_ai_blue_spinner->setValue(num_blue);
UserConfigParams::m_soccer_red_ai_num = num_red;
UserConfigParams::m_soccer_blue_ai_num = num_blue;
UserConfigParams::m_num_karts_per_gamemode[race_manager->getMinorMode()] = race_manager->getNumLocalPlayers() + num_red + num_blue;
//updateHighScores();
}
}
} // eventCallback
// ----------------------------------------------------------------------------
void TrackInfoScreen::getRedBluePlayerNumber()
{
const int local_players = race_manager->getNumLocalPlayers();
int red_players = 0;
int blue_players = 0;
for (int i = 0; i < local_players; i++)
{
KartTeam team = race_manager->getKartInfo(i).getKartTeam();
// Happen in profiling mode
if (team == KART_TEAM_NONE)
{
race_manager->setKartTeam(i, KART_TEAM_BLUE);
team = KART_TEAM_BLUE;
continue; //FIXME, this is illogical
}
team == KART_TEAM_BLUE ? blue_players++ : red_players++;
}
m_red_players = red_players;
m_blue_players = blue_players;
} // getRedBluePlayerNumber
// ----------------------------------------------------------------------------

View File

@ -60,6 +60,15 @@ class TrackInfoScreen : public GUIEngine::Screen,
/** The label besides the target types spinner. */
GUIEngine::LabelWidget* m_target_type_label;
/** Spinner for number of blue AI karts. */
GUIEngine::SpinnerWidget* m_ai_blue_spinner;
/** The label besides the blue AI karts spinner. */
GUIEngine::LabelWidget* m_ai_blue_label;
/* The div that contains the blue ai spinner and label */
GUIEngine::Widget* m_ai_blue_div;
/* The div that contains the target type spinner and label */
GUIEngine::Widget* m_target_type_div;
@ -91,8 +100,14 @@ class TrackInfoScreen : public GUIEngine::Screen,
int m_icon_unknown_kart;
/* The number of red and blue players */
int m_red_players;
int m_blue_players;
void updateHighScores();
void getRedBluePlayerNumber();
public:
TrackInfoScreen();
virtual ~TrackInfoScreen();