diff --git a/src/date.cpp b/src/date.cpp index 4ef16aed78..9f91a2280b 100644 --- a/src/date.cpp +++ b/src/date.cpp @@ -249,7 +249,6 @@ static void OnNewMonth() static void OnNewDay() { #ifdef ENABLE_NETWORK - NetworkChatMessageDailyLoop(); if (_network_server) NetworkServerDailyLoop(); #endif /* ENABLE_NETWORK */ diff --git a/src/network/network.cpp b/src/network/network.cpp index 5631643d20..4e3fb53b4a 100644 --- a/src/network/network.cpp +++ b/src/network/network.cpp @@ -165,7 +165,7 @@ bool NetworkCompanyIsPassworded(CompanyID company_id) * If 'self_send' is true, this is the client who is sending the message */ void NetworkTextMessage(NetworkAction action, ConsoleColour colour, bool self_send, const char *name, const char *str, int64 data) { - const int duration = 10; // Game days the messages stay visible + const int duration = 20; // Seconds the messages stay visible StringID strid; switch (action) { diff --git a/src/network/network_chat_gui.cpp b/src/network/network_chat_gui.cpp index 1d929bbb63..28a3afc99e 100644 --- a/src/network/network_chat_gui.cpp +++ b/src/network/network_chat_gui.cpp @@ -40,7 +40,7 @@ static const uint NETWORK_CHAT_LINE_SPACING = 3; struct ChatMessage { char message[DRAW_STRING_BUFFER]; TextColour colour; - Date end_date; + uint32 remove_time; }; /* used for chat window */ @@ -68,10 +68,10 @@ static inline uint GetChatMessageCount() /** * Add a text message to the 'chat window' to be shown * @param colour The colour this message is to be shown in - * @param duration The duration of the chat message in game-days + * @param duration The duration of the chat message in seconds * @param message message itself in printf() style */ -void CDECL NetworkAddChatMessage(TextColour colour, uint8 duration, const char *message, ...) +void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const char *message, ...) { char buf[DRAW_STRING_BUFFER]; const char *bufp; @@ -104,7 +104,7 @@ void CDECL NetworkAddChatMessage(TextColour colour, uint8 duration, const char * /* The default colour for a message is company colour. Replace this with * white for any additional lines */ cmsg->colour = (bufp == buf && (colour & IS_PALETTE_COLOUR)) ? colour : TC_WHITE; - cmsg->end_date = _date + duration; + cmsg->remove_time = _realtime_tick + duration * 1000; bufp += strlen(bufp) + 1; // jump to 'next line' in the formatted string } @@ -181,15 +181,15 @@ void NetworkUndrawChatMessage() } } -/** Check if a message is expired every day */ -void NetworkChatMessageDailyLoop() +/** Check if a message is expired. */ +void NetworkChatMessageLoop() { for (uint i = 0; i < MAX_CHAT_MESSAGES; i++) { ChatMessage *cmsg = &_chatmsg_list[i]; if (cmsg->message[0] == '\0') continue; /* Message has expired, remove from the list */ - if (cmsg->end_date < _date) { + if (cmsg->remove_time < _realtime_tick) { /* Move the remaining messages over the current message */ if (i != MAX_CHAT_MESSAGES - 1) memmove(cmsg, cmsg + 1, sizeof(*cmsg) * (MAX_CHAT_MESSAGES - i - 1)); diff --git a/src/network/network_func.h b/src/network/network_func.h index d6fc752d8a..f9bbde778b 100644 --- a/src/network/network_func.h +++ b/src/network/network_func.h @@ -79,9 +79,9 @@ void NetworkServerKickClient(ClientID client_id); uint NetworkServerKickOrBanIP(const char *ip, bool ban); void NetworkInitChatMessage(); -void CDECL NetworkAddChatMessage(TextColour colour, uint8 duration, const char *message, ...) WARN_FORMAT(3, 4); +void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const char *message, ...) WARN_FORMAT(3, 4); void NetworkUndrawChatMessage(); -void NetworkChatMessageDailyLoop(); +void NetworkChatMessageLoop(); void NetworkAfterNewGRFScan(); diff --git a/src/openttd.cpp b/src/openttd.cpp index 1e6df5f60b..8b4e013634 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1359,6 +1359,13 @@ void GameLoop() /* Singleplayer */ StateGameLoop(); } + + /* Check chat messages roughly once a second. */ + static uint check_message = 0; + if (++check_message > 1000 / MILLISECONDS_PER_TICK) { + check_message = 0; + NetworkChatMessageLoop(); + } #else StateGameLoop(); #endif /* ENABLE_NETWORK */