Some improvements for soccer AI spinner (#4069)

* Improve code

* Bigger space in track info

* Bug fix && improvements

* Bug fix && improvements

* Bug fix && improvements
This commit is contained in:
Du maosen 2019-09-26 10:12:24 +08:00 committed by Benau
parent 351a6ed6c0
commit 8316d91469
5 changed files with 172 additions and 205 deletions

View File

@ -7,28 +7,11 @@
<spacer width="1" height="2%"/>
<div width="100%" height="73%" layout="horizontal-row">
<div width="100%" height="91%" layout="horizontal-row">
<!-- Left pane -->
<div width="30%" height="100%" layout="vertical-row">
<icon-button width="100%" height="50%" id="screenshot" custom_ratio="1.33333"/>
<spacer width="1" height="2%"/>
<!-- Misc. info box -->
<box width="100%" height="48%" padding="10" layout="vertical-row">
<spacer width="1" height="20%"/>
<label id="author" width="100%" height="30%" text_align="center" word_wrap="true"/>
<spacer width="1" height="10%"/>
<label id="max-arena-players" width="100%" height="30%" text_align="center" word_wrap="true"/>
<spacer width="1" height="20%"/>
</box>
</div>
<spacer width="2%" height="1"/>
<div width="68%" height="100%" layout="vertical-row">
<!-- Right pane -->
<box width="100%" height="50%" padding="10" layout="vertical-row">
<box width="100%" height="49%" padding="10" layout="vertical-row">
<label id="highscores" width="100%" text_align="center" text="= Highscores ="/>
<spacer width="1" height="1%"/>
<list id="highscore_entries" x="0" y="0" width="100%" proportion="1"/>
@ -36,7 +19,7 @@
<spacer width="1" height="2%"/>
<!-- Race options box -->
<box width="100%" height="48%" layout="vertical-row" id="race-options-box">
<box width="100%" height="47%" layout="vertical-row" id="race-options-box">
<div width="100%" height="fit" layout="horizontal-row" id="target-type-div">
<div proportion="3" height="fit" layout="horizontal-row">
<spinner id="target-type-spinner" width="100%" align="center"
@ -45,7 +28,9 @@
<spacer width="3%"/>
<label id="target-type-text" proportion="3" I18N="In the track info screen" text_align="left" />
</div>
<spacer width="1" height="1%"/>
<div width="100%" height="fit" layout="horizontal-row" >
<div proportion="1" height="fit" layout="horizontal-row">
<spinner id="target-value-spinner" width="100%" min_value="1" max_value="20" align="center"
@ -54,7 +39,9 @@
<spacer width="3%"/>
<label id="target-value-text" proportion="3" I18N="In the track info screen" text="Number of laps" 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">
<spinner id="ai-spinner" width="100%" min_value="1" max_value="20" align="center"
@ -63,7 +50,9 @@
<spacer width="3%"/>
<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"
@ -72,7 +61,9 @@
<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" >
@ -82,7 +73,9 @@
<spacer width="3%"/>
<label id="option-text" proportion="3" I18N="In the track info screen" 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" >
@ -93,15 +86,41 @@
<label id="record-race-text" proportion="3" I18N="In the track info screen" text="Record the race for ghost replay" text_align="left" align="center"/>
</div>
</box><!-- Race options box -->
<spacer width="1" height="2%"/>
</div>
<spacer width="2%" height="1"/>
<!-- Right pane -->
<div width="30%" height="100%" layout="vertical-row">
<icon-button width="100%" height="38%" id="screenshot" custom_ratio="1.33333"/>
<spacer width="1" height="2%"/>
<!-- Misc. info box -->
<box width="100%" height="37%" padding="10" layout="vertical-row">
<spacer width="1" height="20%"/>
<label id="author" width="100%" height="30%" text_align="center" word_wrap="true"/>
<spacer width="1" height="10%"/>
<label id="max-arena-players" width="100%" height="30%" text_align="center" word_wrap="true"/>
<spacer width="1" height="20%"/>
</box>
<spacer width="1" height="2%"/>
<buttonbar id="buttons" height="17%" width="100%" align="center">
<icon-button id="start" width="64" height="64" icon="gui/icons/green_check.png"
I18N="In the track info screen" text="Start Race"/>
</buttonbar>
</div>
<spacer width="1" height="2%"/>
</div>
<spacer width="1" height="1%"/>
<buttonbar id="buttons" height="17%" width="100%" align="center">
<icon-button id="start" width="64" height="64" icon="gui/icons/green_check.png"
I18N="In the track info screen" text="Start Race"/>
</buttonbar>
</div>
</stkgui>

View File

@ -1549,8 +1549,21 @@ KartTeam World::getKartTeam(unsigned int kart_id) const
//-----------------------------------------------------------------------------
void World::setAITeam()
{
m_red_ai = UserConfigParams::m_soccer_red_ai_num;
m_blue_ai = UserConfigParams::m_soccer_blue_ai_num;
m_red_ai = race_manager->getNumberOfRedAIKarts();
m_blue_ai = race_manager->getNumberOfBlueAIKarts();
for (int i = 0; i < (int)race_manager->getNumLocalPlayers(); 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
}
}
Log::debug("World", "Blue AI: %d red AI: %d", m_blue_ai, m_red_ai);

View File

@ -327,6 +327,8 @@ private:
GrandPrixData m_grand_prix;
SavedGrandPrix* m_saved_gp;
int m_num_karts;
unsigned int m_num_red_ai;
unsigned int m_num_blue_ai;
unsigned int m_num_ghost_karts;
unsigned int m_num_spare_tire_karts;
unsigned int m_num_finished_karts;
@ -474,6 +476,16 @@ public:
m_ai_superpower = SUPERPOWER_NONE;
} // setNumKarts
// ------------------------------------------------------------------------
void setNumRedAI(unsigned int num)
{
m_num_red_ai = num;
} // setNumRedAI
// ------------------------------------------------------------------------
void setNumBlueAI(unsigned int num)
{
m_num_blue_ai = num;
} // setNumBlueAI
// ------------------------------------------------------------------------
void setTimeTarget(float time)
{
m_goal_target = 0;
@ -505,13 +517,17 @@ public:
// ------------------------------------------------------------------------
/** Returns the selected number of karts (selected number of players and
* AI karts. */
unsigned int getNumberOfKarts() const {return m_num_karts; }
unsigned int getNumberOfKarts() const { return m_num_karts; }
// ------------------------------------------------------------------------
unsigned int getNumberOfAIKarts() const
{
return (unsigned int)m_ai_kart_list.size();
} // getNumberOfAIKarts
// ------------------------------------------------------------------------
unsigned int getNumberOfRedAIKarts() const { return m_num_red_ai; }
// ------------------------------------------------------------------------
unsigned int getNumberOfBlueAIKarts() const { return m_num_blue_ai; }
// ------------------------------------------------------------------------
unsigned int getNumNonGhostKarts() const
{ return m_num_karts - m_num_ghost_karts; }
// ------------------------------------------------------------------------

View File

@ -106,8 +106,6 @@ void TrackInfoScreen::loadedFromFile()
m_is_soccer = false;
m_show_ffa_spinner = false;
m_red_players = 0;
m_blue_players = 0;
} // loadedFromFile
@ -144,6 +142,7 @@ void TrackInfoScreen::init()
m_record_this_race = false;
const int max_arena_players = m_track->getMaxArenaPlayers();
const int local_players = race_manager->getNumLocalPlayers();
const bool has_laps = race_manager->modeHasLaps();
const bool has_highscores = race_manager->modeHasHighscores();
@ -256,7 +255,6 @@ void TrackInfoScreen::init()
// Number of AIs
// -------------
const int local_players = race_manager->getNumLocalPlayers();
const bool has_AI =
(race_manager->getMinorMode() == RaceManager::MINOR_MODE_3_STRIKES ||
race_manager->getMinorMode() == RaceManager::MINOR_MODE_FREE_FOR_ALL ||
@ -268,93 +266,90 @@ void TrackInfoScreen::init()
if (has_AI)
{
m_ai_kart_spinner->setActive(true);
if (m_is_soccer) // Soccer mode: red + blue AI
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);
// Check if there's any local players in both team
int num_blue_players = 0, num_red_players = 0;
if (race_manager->getMinorMode()==RaceManager::MINOR_MODE_SOCCER)
{
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)))
for (int i = 0; i < local_players; i++)
{
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;
KartTeam team = race_manager->getKartInfo(i).getKartTeam();
// Happen in profiling mode
if (team == KART_TEAM_NONE)
num_blue_players ++; // None team will set to blue
else
team == KART_TEAM_BLUE ? num_blue_players ++ : num_red_players ++;
}
const int max_num_ai = max_arena_players - local_players;
// Try the saved value, recalculate AI number (Balanced) if cannot use the saved values
if (UserConfigParams::m_soccer_red_ai_num + UserConfigParams::m_soccer_blue_ai_num > max_num_ai)
{
const int additional_blue = num_red_players - num_blue_players;
int num_blue_ai = (max_num_ai - additional_blue) / 2 + additional_blue;
int num_red_ai = (max_num_ai - additional_blue) / 2;
if ((num_ai + additional_blue)%2 == 1)
(additional_blue < 0) ? num_red_ai++ : num_blue_ai++;
if ((max_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;
}
int num_blue_upper = max_arena_players - local_players - num_red_ai;
int num_red_upper = max_arena_players - local_players - 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_lower ? num_red_upper_hard : num_red_upper + 1), num_red_upper_hard )); // +1 to allow adding AI
m_ai_blue_spinner->setMax(std::min( (num_blue_ai == num_blue_lower ? num_blue_upper_hard : num_blue_upper + 1), num_blue_upper_hard )); // +1 to allow adding AI
// Set the values
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);
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);
}
// 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);
m_ai_blue_spinner->setMax(max_arena_players - local_players);
if(num_blue_players == 0 && !UserConfigParams::m_artist_debug_mode)
m_ai_kart_spinner->setMax(max_arena_players - local_players - 1);
if(num_red_players == 0 && !UserConfigParams::m_artist_debug_mode)
m_ai_blue_spinner->setMax(max_arena_players - local_players - 1);
}
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_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);
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 &&
local_players == 1 &&
!UserConfigParams::m_artist_debug_mode)
m_ai_kart_spinner->setMin(1);
else if(race_manager->getMinorMode()==RaceManager::MINOR_MODE_SOCCER)
{
m_ai_blue_spinner->setMin(0);
m_ai_kart_spinner->setMin(0);
if(num_blue_players == 0 && !UserConfigParams::m_artist_debug_mode)
m_ai_blue_spinner->setMin(1);
if(num_red_players == 0 && !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);
@ -551,13 +546,11 @@ 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();
num_ai += m_ai_blue_spinner->getValue();
}
}
@ -590,6 +583,11 @@ void TrackInfoScreen::onEnterPressedInternal()
PlayerManager::getCurrentPlayer()->setCurrentChallenge("");
race_manager->setNumKarts(num_ai + local_players);
if (m_is_soccer)
{
race_manager->setNumRedAI(m_ai_kart_spinner->getValue());
race_manager->setNumBlueAI(m_ai_blue_spinner->getValue());
}
race_manager->startSingleRace(m_track->getIdent(), num_laps, false);
} // onEnterPressedInternal
@ -701,37 +699,13 @@ void TrackInfoScreen::eventCallback(Widget* widget, const std::string& name,
{
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;
const int num_ai = max_arena_players - local_players;
if(m_ai_kart_spinner->getValue() + m_ai_blue_spinner->getValue() > num_ai)
m_ai_blue_spinner->setValue(num_ai - m_ai_kart_spinner->getValue());
UserConfigParams::m_soccer_red_ai_num = m_ai_kart_spinner->getValue();
UserConfigParams::m_soccer_blue_ai_num = m_ai_blue_spinner->getValue();
//updateHighScores();
}
else // Other modes
@ -743,69 +717,20 @@ void TrackInfoScreen::eventCallback(Widget* widget, const std::string& name,
}
else if (name == "ai-blue-spinner")
{
if (m_is_soccer) // AI distribution: Custom
if (m_is_soccer)
{
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;
const int num_ai = max_arena_players - local_players;
if(m_ai_kart_spinner->getValue() + m_ai_blue_spinner->getValue() > num_ai)
m_ai_kart_spinner->setValue(num_ai - m_ai_blue_spinner->getValue());
UserConfigParams::m_soccer_red_ai_num = m_ai_kart_spinner->getValue();
UserConfigParams::m_soccer_blue_ai_num = m_ai_blue_spinner->getValue();
//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

@ -100,14 +100,8 @@ 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();