Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

feat(gamelogic): vote kick with /kick, /yes and /no in chat

Olcay Seygan27ce048bbae3
diff --git a/GeneralsMD/Code/Data/Patch.str b/GeneralsMD/Code/Data/Patch.str
index b45532913..4307e3aa3 100644
--- a/GeneralsMD/Code/Data/Patch.str
+++ b/GeneralsMD/Code/Data/Patch.str
@@ -1152,3 +1152,31 @@ END
LAN:NoMod
"none"
END
+
+GUI:KickVoteStarted
+"%ls wants %ls out of the match. Type /yes or /no in chat: every other player has 30 seconds to agree."
+END
+
+GUI:KickVoteBanner
+"Kick %ls? Yes %d of %d /yes or /no %d s"
+END
+
+GUI:KickVoteFailed
+"The vote to kick %ls failed."
+END
+
+GUI:KickVoteKicked
+"%ls was voted out of the match."
+END
+
+GUI:KickVoteTooFew
+"A vote kick needs at least two other players besides the one you want out."
+END
+
+GUI:KickVoteNoPlayer
+"No player called %ls is in the match."
+END
+
+GUI:KickVoteNone
+"No vote is open. Type /kick and a player's name to start one."
+END
diff --git a/GeneralsMD/Code/Data/Turkish/Generals.str b/GeneralsMD/Code/Data/Turkish/Generals.str
index b5d058091..4aa600b31 100644
--- a/GeneralsMD/Code/Data/Turkish/Generals.str
+++ b/GeneralsMD/Code/Data/Turkish/Generals.str
@@ -15568,3 +15568,31 @@ END
CONTROLBAR:Demo_ToolTipGLAScienceRebelAmbush
"Her yerde Asilerle ani bir pusu kurmanızı sağlar \n \n Seviye 1: 4 Asi \n Seviye 2: 6 Asi \n Seviye 3: 10 Asi \n \n Çağrıldığı yer: Komuta Merkezi"
END
+
+GUI:KickVoteStarted
+"%ls, %ls oyuncusunun maçtan atılmasını istiyor. Sohbete /yes ya da /no yazın: diğer oyuncuların kabul etmek için 30 saniyesi var."
+END
+
+GUI:KickVoteBanner
+"%ls atılsın mı? Evet %d / %d /yes ya da /no %d sn"
+END
+
+GUI:KickVoteFailed
+"%ls oyuncusunu atma oylaması geçmedi."
+END
+
+GUI:KickVoteKicked
+"%ls oylamayla maçtan atıldı."
+END
+
+GUI:KickVoteTooFew
+"Oylamayla atmak için atılacak oyuncu dışında en az iki oyuncu daha gerekir."
+END
+
+GUI:KickVoteNoPlayer
+"Maçta %ls adında bir oyuncu yok."
+END
+
+GUI:KickVoteNone
+"Açık bir oylama yok. Başlatmak için /kick ve oyuncunun adını yazın."
+END
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h b/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h
index cae1782f4..63d5be191 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/MessageStream.h
@@ -659,6 +659,7 @@ public:
MSG_QUEUE_NEXT_ORDER, ///< (Int OrderQueueMode) the order right after this one is a shift-queued one (fork)
MSG_CLEAR_RALLY_POINT, ///< (objectID) the building forgets its rally point (fork)
MSG_SET_STANCE, ///< (Int 0 defensive, 1 aggressive) how far the selected units go after what they see (fork)
+ MSG_VOTE_KICK, ///< (Int player index, Bool yes) a vote kick: opens one, or answers the open one (fork)
MSG_BEGIN_DEBUG_NETWORK_MESSAGES = 1900, ///< network messages that exist only in debug/internal builds. all grouped separately.
@@ -696,7 +697,7 @@ public:
const GameMessage *prev( void ) const { return m_prev; } ///< Return prev message in the stream
Type getType( void ) const { return m_type; } ///< Return the message type
- UnsignedByte getArgumentCount( void ) const { return (UnsignedByte)m_argList.size(); } ///< Return the number of arguments for this msg
+ Int getArgumentCount( void ) const { return (Int)m_argList.size(); } ///< Return the number of arguments for this msg; a selection of 1000 units carries 1001
AsciiString getCommandAsAsciiString( void ); ///< returns a string representation of the command type.
static AsciiString getCommandTypeAsAsciiString(GameMessage::Type t);
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index a3adb17fe..2d7e948c1 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -1386,6 +1386,7 @@ protected:
enum { HUD_CLOCK_POINT_SIZE = 7 }; ///< smaller still, and bold: the clock/rate plate is a glance, not a read
void drawHudOverlay( void ); ///< the top right info strip (ShowNetBox), peace time at its end
+ void drawKickVoteBanner( void ); ///< the open vote kick, top middle, while there is one
void drawProductionList( void ); ///< the production list, lines on the left above the control bar
SignalKind m_armedSignal; ///< the signal the next click drops; SIGNAL_KIND_COUNT for none
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h
index bccb8d7ad..fef851a9e 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/GameLogic.h
@@ -270,6 +270,15 @@ public:
/// TRUE while the truce forbids these two hurting each other; the one question both gates ask
Bool peaceTimeForbids( const Object *attacker, const Object *victim ) const;
+ /** The vote kick (MSG_VOTE_KICK, "/kick name" in chat). One vote at a time, open for 30 seconds;
+ it passes when every living human player but the one on the block has voted yes, and one no
+ ends it. It is logic state, moved only by commands, so every machine decides it alike. */
+ void processKickVote( Player *voter, Int targetIndex, Bool yes );
+ Int getKickVoteTarget( void ) const { return m_kickVoteTarget; } ///< player index on the block, -1 when no vote is open
+ UnsignedInt getKickVoteEndFrame( void ) const { return m_kickVoteEndFrame; }
+ Int countKickVoters( void ) const; ///< the players whose yes the open vote needs
+ Int countKickVotes( void ) const; ///< how many of them have given it
+
/** Each player's share of the lobby's unit limit, the same number for everybody. 0 when the
limit is off, which is every campaign mission and every game it was not ticked for. */
UnsignedInt getUnitCap( void ) const { return m_unitCap; }
@@ -415,6 +424,11 @@ private:
Int m_rankLevelLimit;
UnsignedShort m_superweaponRestriction;
UnsignedInt m_peaceTimeEndFrame; ///< logic frame the lobby's peace time runs out on, 0 = no peace time
+ Int m_kickVoteTarget; ///< player index an open vote kick is about, -1 = none
+ UnsignedInt m_kickVoteEndFrame; ///< frame that vote runs out on
+ UnsignedInt m_kickVoteYesMask; ///< player indices that have voted yes in it
+ void expireKickVote( void ); ///< closes the vote once its 30 seconds are up
+ void endKickVote( const char *label );
UnsignedInt m_unitCap; ///< units each player may have standing and queued, 0 = no limit
Bool m_proRules; ///< this match refuses what PRO-RULES.md bans
Int m_incomeSharing; ///< which earnings allies split, an IncomeSharing
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/MessageStream.cpp b/GeneralsMD/Code/GameEngine/Source/Common/MessageStream.cpp
index 3a1525630..f4cdf4aa2 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/MessageStream.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/MessageStream.cpp
@@ -665,6 +665,7 @@ AsciiString GameMessage::getCommandTypeAsAsciiString(GameMessage::Type t)
CHECK_IF(MSG_QUEUE_NEXT_ORDER)
CHECK_IF(MSG_CLEAR_RALLY_POINT)
CHECK_IF(MSG_SET_STANCE)
+ CHECK_IF(MSG_VOTE_KICK)
CHECK_IF(MSG_DO_GUARD_OBJECT)
CHECK_IF(MSG_DO_STOP)
CHECK_IF(MSG_DO_SCATTER)
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/InGameChat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/InGameChat.cpp
index 2b34de32a..aef9cceca 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/InGameChat.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/InGameChat.cpp
@@ -32,6 +32,7 @@
// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
#include "Lib/Clock.h"
+#include "Common/MessageStream.h"
#include "Common/Player.h"
#include "Common/PlayerList.h"
@@ -208,6 +209,41 @@ Bool handleInGameSlashCommands(UnicodeString uText)
return TRUE; // was a slash command
}
+ // The vote kick: "/kick name" opens one (and is a yes), "/yes" and "/no" answer the open one.
+ // This only asks; GameLogic::processKickVote decides on every machine alike whether it counts.
+ if (token == "kick" || token == "yes" || token == "no")
+ {
+ Int target = TheGameLogic->getKickVoteTarget();
+ if (token == "kick")
+ {
+ UnicodeString name(uText.str() + 5); // what follows "/kick"
+ name.trim();
+ target = -1;
+ for (Int i = 0; i < ThePlayerList->getPlayerCount(); ++i)
+ {
+ Player *player = ThePlayerList->getNthPlayer(i);
+ if (player != ThePlayerList->getLocalPlayer() && player->getPlayerType() == PLAYER_HUMAN
+ && player->isPlayerActive() && player->getPlayerDisplayName().compareNoCase(name) == 0)
+ target = i;
+ }
+ if (target < 0)
+ {
+ TheInGameUI->message("GUI:KickVoteNoPlayer", name.str());
+ return TRUE;
+ }
+ }
+ else if (target < 0)
+ {
+ TheInGameUI->message("GUI:KickVoteNone");
+ return TRUE;
+ }
+
+ GameMessage *vote = TheMessageStream->appendMessage(GameMessage::MSG_VOTE_KICK);
+ vote->appendIntegerArgument(target);
+ vote->appendBooleanArgument(token != "no");
+ return TRUE;
+ }
+
return FALSE; // not a slash command
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 8fa6f988c..da20be257 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -6442,7 +6442,9 @@ void InGameUI::postDraw( void )
{
// found one - draw it
UnicodeString line;
- Int framesLeft = TheScriptEngine->getCounter(timerName)->value;
+ // a map may show a counter its scripts never set; such a counter reads 0, as an unset one does in a condition
+ const TCounter *counter = TheScriptEngine->getCounter(timerName);
+ Int framesLeft = counter ? counter->value : 0;
UnsignedInt readyFrame = TheGameLogic->getFrame();
if (framesLeft > 0)
readyFrame += framesLeft * LOGICFRAMES_PER_RETAIL_FRAME; // script timers count retail frames
@@ -9066,6 +9068,9 @@ void InGameUI::drawHudOverlay( void )
if( TheGameLogic->getFrame() == 0 )
return;
+ // on top of the battlefield like this strip, but never behind the ShowNetBox switch
+ drawKickVoteBanner();
+
// ShowNetBox is the player's switch for the readout. Peace time is a rule of the match rather
// than a readout, so while it runs the strip stays up with only that segment in it.
const Bool showInfo = TheGlobalData->m_showNetBox;
@@ -9229,6 +9234,35 @@ void InGameUI::drawHudOverlay( void )
m_peaceTimeDisplayString->draw( x + textWidth + gap, y, peaceTimeColor( 255 ), GameMakeColor( 0, 0, 0, 255 ) );
}
+//-------------------------------------------------------------------------------------------------
+/** The open vote kick, a plate at the top middle of the battlefield: who is on the block, the yes
+ * votes against the number needed, the seconds left and how to answer. The vote is the logic's
+ * (GameLogic::processKickVote); this only shows it. */
+//-------------------------------------------------------------------------------------------------
+void InGameUI::drawKickVoteBanner( void )
+{
+ const Int target = TheGameLogic->getKickVoteTarget();
+ if( target < 0 )
+ return;
+
+ const UnsignedInt secs = ControlBar_secondsFromFrames(
+ (Real)(TheGameLogic->getKickVoteEndFrame() - TheGameLogic->getFrame()) );
+ UnicodeString text;
+ text.format( TheGameText->fetch( "GUI:KickVoteBanner" ), ThePlayerList->getNthPlayer( target )->getPlayerDisplayName().str(),
+ TheGameLogic->countKickVotes(), TheGameLogic->countKickVoters(), (Int)secs );
+ DisplayString *banner = broadcastText( "kickvote", text, m_superweaponNormalFont.str(),
+ TheGlobalLanguageData->adjustFontSize( HUD_OVERLAY_POINT_SIZE ), TRUE );
+
+ Int width = 0, height = 0;
+ banner->getSize( &width, &height );
+ const UIRect box = TheUIRect();
+ const Int pad = stripPixels( 3 );
+ const Int x = box.x + ( box.w - width ) / 2;
+ const Int y = box.y + stripPixels( 40 );
+ TheDisplay->drawFillRect( x - pad, y - pad, width + pad*2, height + pad*2, GameMakeColor( 0, 0, 0, 170 ) );
+ banner->draw( x, y, GameMakeColor( 255, 210, 90, 255 ), GameMakeColor( 0, 0, 0, 255 ) );
+}
+
//-------------------------------------------------------------------------------------------------
/** Where an item goes in the list: the soonest to finish first, ties keeping the order they were
* met in so a base of identical barracks does not shuffle frame to frame. */
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index ac05a9a79..0842252e0 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -242,6 +242,9 @@ GameLogic::GameLogic( void )
m_gameMode = GAME_NONE;
m_rankLevelLimit = 1000;
m_peaceTimeEndFrame = 0;
+ m_kickVoteTarget = -1;
+ m_kickVoteEndFrame = 0;
+ m_kickVoteYesMask = 0;
m_unitCap = 0;
m_proRules = FALSE;
m_incomeSharing = INCOME_SHARING_OFF;
@@ -1116,6 +1119,10 @@ void GameLogic::deleteLoadScreen( void )
// ------------------------------------------------------------------------------------------------
void GameLogic::startNewGame( Bool loadingSaveGame )
{
+ // a vote is about one match; none carries into the next, or into a loaded one
+ m_kickVoteTarget = -1;
+ m_kickVoteEndFrame = 0;
+ m_kickVoteYesMask = 0;
#ifdef DUMP_PERF_STATS
__int64 startTime64;
@@ -4052,6 +4059,117 @@ Bool GameLogic::peaceTimeForbids( const Object *attacker, const Object *victim )
return attacker->getRelationship( victim ) == ENEMIES;
}
+/* The vote kick. Whoever may vote is a human side still in the match, other than the one on the
+ block: not an observer, not a beaten player, not the computer. All of it is asked of the player
+ list, which is logic state, so every machine counts the same people on the same frame. A vote
+ needs two such voters at least, or in a one-on-one the one asking would decide alone, and the
+ kick there is a free win. */
+static const Int KICK_VOTE_SECONDS = 30;
+static const Int MIN_KICK_VOTERS = 2;
+
+static Bool mayVoteToKick( Player *player, Int targetIndex )
+{
+ return player->getPlayerType() == PLAYER_HUMAN && player->isPlayableSide() && player->isPlayerActive()
+ && player->getPlayerIndex() != targetIndex;
+}
+
+Int GameLogic::countKickVoters( void ) const
+{
+ Int voters = 0;
+ for( Int i = 0; i < ThePlayerList->getPlayerCount(); ++i )
+ if( mayVoteToKick( ThePlayerList->getNthPlayer( i ), m_kickVoteTarget ) )
+ ++voters;
+ return voters;
+}
+
+Int GameLogic::countKickVotes( void ) const
+{
+ Int votes = 0;
+ for( Int i = 0; i < ThePlayerList->getPlayerCount(); ++i )
+ if( ( m_kickVoteYesMask & ( 1u << i ) ) && mayVoteToKick( ThePlayerList->getNthPlayer( i ), m_kickVoteTarget ) )
+ ++votes;
+ return votes;
+}
+
+void GameLogic::endKickVote( const char *label )
+{
+ if( TheInGameUI && label )
+ TheInGameUI->message( label, ThePlayerList->getNthPlayer( m_kickVoteTarget )->getPlayerDisplayName().str() );
+ m_kickVoteTarget = -1;
+ m_kickVoteEndFrame = 0;
+ m_kickVoteYesMask = 0;
+}
+
+/* Kicked is beaten: his side is killed the way a player who quits to watch is, so he stays in the
+ match as a watcher and leaves the way anybody does. Called whenever the count may have moved,
+ since a voter who leaves can complete a vote as surely as one who says yes. */
+static Bool kickVoteAgreed( Int votes, Int voters )
+{
+ return voters >= MIN_KICK_VOTERS && votes >= voters;
+}
+
+void GameLogic::processKickVote( Player *voter, Int targetIndex, Bool yes )
+{
+ if( !mayVoteToKick( voter, targetIndex ) )
+ return;
+
+ if( m_kickVoteTarget < 0 )
+ {
+ // a no opens nothing; a yes opens a vote about a human side still playing
+ Player *target = ThePlayerList->getNthPlayer( targetIndex );
+ if( !yes || target == NULL || target->getPlayerType() != PLAYER_HUMAN || !target->isPlayableSide() || !target->isPlayerActive() )
+ return;
+
+ m_kickVoteTarget = targetIndex;
+ if( countKickVoters() < MIN_KICK_VOTERS )
+ {
+ m_kickVoteTarget = -1;
+ if( TheInGameUI && voter == ThePlayerList->getLocalPlayer() )
+ TheInGameUI->message( "GUI:KickVoteTooFew" );
+ return;
+ }
+ m_kickVoteEndFrame = m_frame + KICK_VOTE_SECONDS * LOGICFRAMES_PER_SECOND;
+ m_kickVoteYesMask = 1u << voter->getPlayerIndex();
+ if( TheInGameUI )
+ TheInGameUI->message( "GUI:KickVoteStarted", voter->getPlayerDisplayName().str(), target->getPlayerDisplayName().str() );
+ return;
+ }
+
+ // one vote at a time, and only about the one it is about
+ if( targetIndex != m_kickVoteTarget )
+ return;
+
+ if( !yes )
+ {
+ endKickVote( "GUI:KickVoteFailed" );
+ return;
+ }
+
+ m_kickVoteYesMask |= 1u << voter->getPlayerIndex();
+ if( kickVoteAgreed( countKickVotes(), countKickVoters() ) )
+ {
+ ThePlayerList->getNthPlayer( m_kickVoteTarget )->killPlayer();
+ endKickVote( "GUI:KickVoteKicked" );
+ }
+}
+
+void GameLogic::expireKickVote( void )
+{
+ if( m_kickVoteTarget < 0 )
+ return;
+
+ // he left, or lost, before the vote was done with him
+ if( !ThePlayerList->getNthPlayer( m_kickVoteTarget )->isPlayerActive() )
+ endKickVote( NULL );
+ else if( m_frame >= m_kickVoteEndFrame )
+ endKickVote( "GUI:KickVoteFailed" );
+ else if( kickVoteAgreed( countKickVotes(), countKickVoters() ) )
+ {
+ ThePlayerList->getNthPlayer( m_kickVoteTarget )->killPlayer();
+ endKickVote( "GUI:KickVoteKicked" );
+ }
+}
+
/* Peace time, the half of the rule that is not a refusal. Object::getAbleToAttackSpecificObject
and ActiveBody::attemptDamage between them stop anybody shooting anybody until the clock runs
out; without this, the truce would just be free reconnaissance and a place to park an army one
@@ -4561,6 +4679,9 @@ void GameLogic::update( void )
// the lobby's peace time, if the host set one
peaceTimeTick();
+ // a vote kick that ran out of time
+ expireKickVote();
+
// and the buildings nobody repairs
neutralBuildingRepairTick();
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
index fea5a5da3..b82cff411 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
@@ -759,6 +759,19 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, AIGroup *orderedGroup
}
+ //---------------------------------------------------------------------------------------------
+ // A vote kick (fork): (Int player index, Bool yes) from "/kick", "/yes" or "/no" in chat. The
+ // arguments came over the network, so their shape is checked; GameLogic::processKickVote
+ // decides the rest from logic state alone.
+ case GameMessage::MSG_VOTE_KICK:
+ {
+ if( msg->getArgumentCount() < 2 || msg->getArgumentDataType( 0 ) != ARGUMENTDATATYPE_INTEGER
+ || msg->getArgumentDataType( 1 ) != ARGUMENTDATATYPE_BOOLEAN )
+ break;
+ processKickVote( thisPlayer, msg->getArgument( 0 )->integer, msg->getArgument( 1 )->boolean );
+ break;
+ }
+
//---------------------------------------------------------------------------------------------
// The stance key (fork): every selected unit of the sender's takes the stance. The number came
// over the network, so only 0 and 1 mean anything