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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gamenetwork): ack frame resend replies to their sender in 3+ player rooms
Olcay Seygan0f00be346977
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8227ccc03..9116d5702 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -678,6 +678,7 @@ found and fixed — EA's own, not port damage.**- Your clicks are answered sooner online. The game worked out how far ahead to schedule orders from the two slowest connections in the room and counted the host as one of them, so a two-player game waited out a whole round trip where half of one is the wire. At a 200 ms ping the delay between a click and the units moving drops from about 310 ms to 215; three players at 250 ms go from 500 to 370. Pings up to 300 ms and more are measured as they are now instead of being cut off at half a second, which left those links a window too short for them.- The readout in the top right corner now carries the frame number, and in a network game how many frames the room can still play before it has to wait on somebody, out of the input delay it is running with, plus the frame rate the slowest machine has set for everyone. "ready 4/7 room 29fps" is a healthy match; ready sitting at 0 is the stall you are about to feel.- Two players who each lost the other's last packet no longer freeze until the disconnect screen. Each asked the other to send it again, and each only resent what it had already played, which was nothing, so on a link with a tenth of a second of delay and one packet in twenty lost the match stopped for good one second in. Now the packet goes again and the match carries on.+- A match of three or more players no longer clogs up after a lost packet. When one player asked another, not the host, for a frame he had missed, the answer was acknowledged to the host instead of to the player who sent it, so that player never heard back and resent the same frame every two seconds for the rest of the match. A copy of someone else's frame could also carry the number of an order that player still had on the way, and the acknowledgement for the copy cancelled the order's own resend. Answers are acknowledged to whoever sent them now, under the number their author gave them, and a player who misses a frame asks for it on the host's measured timing instead of waiting two seconds first. Two-player games are unchanged.- A desynced match stops at once instead of playing on as two games.- Two players on different factions can play a LAN or VPN match again. 3.0.0 and 3.0.1 put every USA against GLA, China against USA and China against GLA game out of sync in its first second, while two players of the same side played on: the mismatch check had started counting the map scripts' timers and flags, and some of those scripts follow each player's own faction, so the two machines never agreed on them. The check leaves them out again, as Zero Hour always did.- A single lost packet no longer freezes the match for twenty seconds. The game now asks for thediff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.hindex 590521d63..6ff3fea8b 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/Connection.h@@ -143,6 +143,8 @@ public:/** This connection's measured retry timeout, in milliseconds. How long a packet on this link isgiven before it is worth sending again - see FrameResendPolicy.h. */UnsignedInt getRetryTimeMS( void ) const { return (UnsignedInt)m_retryTime; }+ /** FALSE while the retry timeout is still the unmeasured ceiling. */+ Bool hasLatencySample( void ) const { return m_smoothedLatency >= 0.0f; }#if defined(_DEBUG) || defined(_INTERNAL)void debugPrintCommands();diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.hindex 687a4abf6..6bc5078e3 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/ConnectionManager.h@@ -166,7 +166,7 @@ private:void doRelay();void doKeepAlive();void sendRemoteCommand(NetCommandRef *msg);- void ackCommand(NetCommandRef *ref, UnsignedInt localSlot);+ void ackCommand(NetCommandRef *ref, UnsignedInt localSlot, UnsignedInt senderSlot = MAX_SLOTS); ///< senderSlot: whose connection the copy came in on, MAX_SLOTS when unknownBool processNetCommand(NetCommandRef *ref);void processAckStage1(NetCommandMsg *msg);diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameData.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameData.hindex 48ee8c6b9..42344de70 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameData.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameData.h@@ -52,6 +52,8 @@ public:NetCommandList * getCommandList();void setFrameCommandCount(UnsignedInt totalCommandCount);UnsignedInt getFrameCommandCount();+ void setFrameInfoID(UnsignedShort id);+ Int getFrameInfoID(); ///< id of the FRAMEINFO that set the count, -1 when none camevoid addCommand(NetCommandMsg *msg);UnsignedInt getCommandCount();void zeroFrame();@@ -60,6 +62,7 @@ public:protected:UnsignedInt m_frame;UnsignedInt m_frameCommandCount;+ Int m_frameInfoID;UnsignedInt m_commandCount;NetCommandList *m_commandList;UnsignedInt m_lastFailedCC;diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameDataManager.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameDataManager.hindex e300e144b..128ba1b7f 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameDataManager.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameDataManager.h@@ -47,8 +47,9 @@ public:FrameDataReturnType allCommandsReady(UnsignedInt frame, Bool debugSpewage);NetCommandList * getFrameCommandList(UnsignedInt frame);UnsignedInt getCommandCount(UnsignedInt frame);- void setFrameCommandCount(UnsignedInt frame, UnsignedInt commandCount);+ void setFrameCommandCount(UnsignedInt frame, UnsignedInt commandCount, UnsignedShort frameInfoID);UnsignedInt getFrameCommandCount(UnsignedInt frame);+ Int getFrameInfoID(UnsignedInt frame);void zeroFrames(UnsignedInt startingFrame, UnsignedInt numFrames);void destroyGameMessages();void resetFrame(UnsignedInt frame, Bool isAdvancing = TRUE);diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cppindex 30fc6f44a..a4e9f3e91 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Connection.cpp@@ -275,8 +275,12 @@ void Connection::sendNetCommandMsg(NetCommandMsg *msg, UnsignedByte relay) {NetCommandList *list = tempPacket->getCommandList();NetCommandRef *ref1 = list->getFirstMessage();while (ref1 != NULL) {+ // NULL when the queue already holds a command with this author and id: a relayed+ // command is wrapped under our own id counter (NetPacket.cpp), which can match one.NetCommandRef *ref2 = m_netCommandList->addMessage(ref1->getCommand());- ref2->setRelay(relay);+ if (ref2 != NULL) {+ ref2->setRelay(relay);+ }ref1 = ref1->getNext();}diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cppindex 455fe93a8..7782b6de5 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp@@ -405,6 +405,20 @@ void ConnectionManager::doRelay() {NetCommandList *cmdList = packet->getCommandList();NetCommandRef *cmd = cmdList->getFirstMessage();+ /* Who put this packet on the wire, as opposed to who wrote the commands in it. An ack has+ to reach the queue that keeps resending the copy, and that queue is the sender's. A+ packet from an address no connection owns leaves it MAX_SLOTS, and ackCommand then routes+ the way it always did. */+ UnsignedInt senderSlot = MAX_SLOTS;+ for (Int s = 0; s < MAX_SLOTS; ++s) {+ if ((m_connections[s] != NULL) &&+ (m_connections[s]->getUser()->GetIPAddr() == m_transport->m_inBuffer[i].addr) &&+ (m_connections[s]->getUser()->GetPort() == m_transport->m_inBuffer[i].port)) {+ senderSlot = s;+ break;+ }+ }+// Iterate through the commands in this packet and send them to the proper connections.while (cmd != NULL) {//DEBUG_LOG(("ConnectionManager::doRelay() - Looking at a command of type %s\n",@@ -418,7 +432,7 @@ void ConnectionManager::doRelay() {}if (CommandRequiresAck(cmd->getCommand())) {- ackCommand(cmd, m_localSlot);+ ackCommand(cmd, m_localSlot, senderSlot);}if (!processNetCommand(cmd)) {sendRemoteCommand(cmd);@@ -915,7 +929,7 @@ void ConnectionManager::processFrameInfo(NetFrameCommandMsg *msg) {if ((playerID >= 0) && (playerID < MAX_SLOTS)) {if (m_frameData[playerID] != NULL) {// DEBUG_LOG(("ConnectionManager::processFrameInfo - player %d, frame %d, command count %d, received on frame %d\n", playerID, msg->getExecutionFrame(), msg->getCommandCount(), TheGameLogic->getFrame()));- m_frameData[playerID]->setFrameCommandCount(msg->getExecutionFrame(), msg->getCommandCount());+ m_frameData[playerID]->setFrameCommandCount(msg->getExecutionFrame(), msg->getCommandCount(), msg->getID());}}}@@ -947,7 +961,13 @@ void ConnectionManager::processAckStage1(NetCommandMsg *msg) {}if (ref != NULL) {- if (ref->getCommand()->getNetCommandType() == NETCOMMANDTYPE_FRAMEINFO) {+ /* The run-ahead's latency is the round trip of our own frame info through the packet router.+ An ack from another player, or for another slot's frame info we passed on, is for a frame+ resend answer (see ackCommand), whose frame went out long before the stopwatch this would+ read, so it is no sample. A non-router's queue to the router only ever held its own. */+ if ((ref->getCommand()->getNetCommandType() == NETCOMMANDTYPE_FRAMEINFO) &&+ ((m_localSlot == m_packetRouterSlot) ||+ ((playerID == m_packetRouterSlot) && (ref->getCommand()->getPlayerID() == m_localSlot)))) {m_frameMetrics.processLatencyResponse(((NetFrameCommandMsg *)(ref->getCommand()))->getExecutionFrame());}@@ -1118,13 +1138,25 @@ void ConnectionManager::handleLocalPlayerLeaving(UnsignedInt frame) {/*** We just got a message that needs to be ack'd, so ack it!*/-void ConnectionManager::ackCommand(NetCommandRef *ref, UnsignedInt localSlot) {+void ConnectionManager::ackCommand(NetCommandRef *ref, UnsignedInt localSlot, UnsignedInt senderSlot) {NetCommandMsg *msg = ref->getCommand();NetCommandMsg *ackmsg;UnsignedShort commandID;UnsignedByte originalPlayerID;UnsignedByte sendRelay = 0;+ /* A copy that a player other than the packet router sent us straight, which is how a frame+ resend request is answered: the asked player's own commands and the frames it holds for every+ other slot. That copy sits in the sender's queue to us. EA acked it to the packet router,+ which only clears the router's own queue, or, on the router, to the command's author, so in a+ room of three or more the sender never heard back and resent the copy every retry timeout for+ the rest of the match. It goes back to the sender now, as a stage 1 ack only: we have it, but+ only the router can say everyone has. Two players never take this branch - one of the two is+ the router, and the router's own traffic and its author's sends route as before. */+ const Bool directReply = (CommandRequiresDirectSend(msg) == FALSE) && (senderSlot < MAX_SLOTS) &&+ (senderSlot != m_packetRouterSlot) && (m_connections[senderSlot] != NULL) &&+ !((m_localSlot == m_packetRouterSlot) && (senderSlot == msg->getPlayerID()));+// Make send relay a bitmask for the connections that the relay will actually be sent to. This is// necessary to determine whether or not we have to wait to send a stage 2 ack.for (Int i = 0; i < MAX_SLOTS; ++i) {@@ -1138,7 +1170,7 @@ void ConnectionManager::ackCommand(NetCommandRef *ref, UnsignedInt localSlot) {#endifsendRelay = sendRelay & ref->getRelay();- if (sendRelay == 0) {+ if ((sendRelay == 0) && (directReply == FALSE)) {NetAckBothCommandMsg *bothmsg = newInstance(NetAckBothCommandMsg)(ref->getCommand());ackmsg = bothmsg;commandID = bothmsg->getCommandID();@@ -1169,6 +1201,8 @@ void ConnectionManager::ackCommand(NetCommandRef *ref, UnsignedInt localSlot) {m_connections[msg->getPlayerID()]->sendNetCommandMsg(ackmsg, 1 << msg->getPlayerID());}}+ } else if (directReply) {+ m_connections[senderSlot]->sendNetCommandMsg(ackmsg, 1 << senderSlot);} else {// The local connection may be the packet router, in that case, the connection would be NULL. So do something about it!if ((m_packetRouterSlot >= 0) && (m_packetRouterSlot < MAX_SLOTS)) {@@ -1721,9 +1755,21 @@ Bool ConnectionManager::allCommandsReady(UnsignedInt frame, Bool justTesting /*? stalledMS: (UnsignedInt)(now - m_resendLastRequest);+ /* The frame comes the way every frame comes, through the packet router unless we are the+ router, so that link's timeout is the wait. The request and its answer go straight between+ us and the player we wait on, though, so once that link has been measured a slower one+ stretches the wait, or we ask again before the answer can land. Unmeasured, it sits at its+ 2000 ms ceiling, which made every such stall wait two seconds before asking. With two+ players the router is the player we wait on, so nothing changes there. */UnsignedInt retryTimeoutMS = FRAME_RESEND_MIN_TIMEOUT_MS;- if (m_connections[notReadyPlayer] != NULL) {- retryTimeoutMS = m_connections[notReadyPlayer]->getRetryTimeMS();+ Connection *direct = m_connections[notReadyPlayer];+ if ((m_packetRouterSlot < MAX_SLOTS) && (m_connections[m_packetRouterSlot] != NULL)) {+ retryTimeoutMS = m_connections[m_packetRouterSlot]->getRetryTimeMS();+ if ((direct != NULL) && direct->hasLatencySample() && (direct->getRetryTimeMS() > retryTimeoutMS)) {+ retryTimeoutMS = direct->getRetryTimeMS();+ }+ } else if (direct != NULL) {+ retryTimeoutMS = direct->getRetryTimeMS();}if (shouldRequestFrameResend(stalledMS, sinceLastRequestMS, retryTimeoutMS)) {@@ -2641,13 +2687,21 @@ void ConnectionManager::sendSlotFrameToPlayer(UnsignedInt playerID, UnsignedIntref = ref->getNext();}}+ /* A command is known by its author and id together, and ours is the only id counter we own. EA+ stamped another slot's frame info with an id from that counter, and the counters run nearly in+ step, so the copy could carry the very id of a command that slot really had in flight: the+ receiver's ack then cleared that command from a queue still owing it, and a packet holding both+ kept one. Another slot's frame info goes out under the id its author gave it, and not at all+ when it never reached us, since then we have no count to vouch for either. */+ Int frameInfoID = (slot == m_localSlot) ? (Int)GenerateNextCommandID() : m_frameData[slot]->getFrameInfoID(frame);+ if (frameInfoID < 0) {+ return;+ }UnsignedInt frameCommandCount = m_frameData[slot]->getFrameCommandCount(frame);NetFrameCommandMsg *msg = newInstance(NetFrameCommandMsg);msg->setExecutionFrame(frame);msg->setCommandCount(frameCommandCount);- if (DoesCommandRequireACommandID(msg->getNetCommandType())) {- msg->setID(GenerateNextCommandID());- }+ msg->setID((UnsignedShort)frameInfoID);msg->setPlayerID(slot);DEBUG_LOG(("ConnectionManager::sendFrameDataToPlayer - sending frame info from player %d to player %d for frame %d with command count %d and ID %d and relay %d\n", slot, playerID, msg->getExecutionFrame(), msg->getCommandCount(), msg->getID(), relay));sendLocalCommandDirect(msg, relay);diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameData.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameData.cppindex 9209ff068..ae35b12d2 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameData.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameData.cpp@@ -37,6 +37,7 @@ FrameData::FrameData()m_commandList = NULL;m_commandCount = 0;m_frameCommandCount = -1;+ m_frameInfoID = -1;//Added By Sadullah Nader//Initializations missing and neededm_lastFailedCC = 0;@@ -68,6 +69,7 @@ void FrameData::init()m_commandList->reset();m_frameCommandCount = -1;+ m_frameInfoID = -1;//DEBUG_LOG(("FrameData::init\n"));m_commandCount = 0;m_lastFailedCC = -2;@@ -150,6 +152,17 @@ UnsignedInt FrameData::getFrameCommandCount() {return m_frameCommandCount;}+/**+ * Remember the id the author gave the frame info, so a copy we pass on is the same command.+ */+void FrameData::setFrameInfoID(UnsignedShort id) {+ m_frameInfoID = id;+}++Int FrameData::getFrameInfoID() {+ return m_frameInfoID;+}+/*** return the number of commands received so far.*/diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameDataManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameDataManager.cppindex 20e726995..cdf5f680e 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameDataManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameDataManager.cpp@@ -147,10 +147,20 @@ UnsignedInt FrameDataManager::getCommandCount(UnsignedInt frame) {/*** Set the frame command count for the given frame.*/-void FrameDataManager::setFrameCommandCount(UnsignedInt frame, UnsignedInt commandCount) {+void FrameDataManager::setFrameCommandCount(UnsignedInt frame, UnsignedInt commandCount, UnsignedShort frameInfoID) {UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;m_frameData[frameindex].setFrameCommandCount(commandCount);+ m_frameData[frameindex].setFrameInfoID(frameInfoID);+}++/**+ * The id of the frame info that announced the given frame, -1 if none has.+ */+Int FrameDataManager::getFrameInfoID(UnsignedInt frame) {+ UnsignedInt frameindex = frame % FRAME_DATA_LENGTH;++ return m_frameData[frameindex].getFrameInfoID();}/**diff --git a/GeneralsMD/Code/Tools/net_check.py b/GeneralsMD/Code/Tools/net_check.pyindex ce8b261e8..c6cff1b3a 100644--- a/GeneralsMD/Code/Tools/net_check.py+++ b/GeneralsMD/Code/Tools/net_check.py@@ -29,7 +29,7 @@ from control_client import Control, ControlErrorRUN_DIRECTORY = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..", "Run"))EXE_NAME = "generals.exe"FIRST_CONTROL_PORT = 8787-MAXIMUM_COPIES = 3 # four copies stall at frame 61 in the disconnect keepalive, not chased+MAXIMUM_COPIES = 8 # eight ran clean on 2026-10-24; the map needs a start spot per copy (Iron Dragon has 8)CONNECT_TIMEOUT_SECONDS = 120.0MATCH_START_TIMEOUT_SECONDS = 180.0PRESS_INTERVAL_SECONDS = 0.5@@ -129,7 +129,7 @@ def play(arguments: argparse.Namespace, games: list) -> list:def main() -> int:arguments = parse_arguments()if not 2 <= arguments.copies <= MAXIMUM_COPIES:- raise SystemExit("--copies is 2 or 3, got %d; four stall at frame 61" % arguments.copies)+ raise SystemExit("--copies is 2 to %d, got %d" % (MAXIMUM_COPIES, arguments.copies))processes = [subprocess.Popen(command_line(arguments, slot), cwd=RUN_DIRECTORY)for slot in range(arguments.copies)]