# HG changeset patch
# User Adam Kaminski <kaminskiadam9@gmail.com>
# Date 1632080909 14400
#      Sun Sep 19 15:48:29 2021 -0400
# Node ID 834659d7b7aa4877bdd056d1acd622bb06adc1a2
# Parent  e7e1ea6608a790347e17c6f23152dcb00db07076
Fixed a player's last sent chat messages being lost in a savegame.

diff -r e7e1ea6608a7 -r 834659d7b7aa src/chat.cpp
--- a/src/chat.cpp	Sat Sep 18 20:16:32 2021 -0400
+++ b/src/chat.cpp	Sun Sep 19 15:48:29 2021 -0400
@@ -78,6 +78,7 @@
 #include "sectinfo.h"
 #include "g_level.h"
 #include "p_acs.h"
+#include "farchive.h"
 
 //*****************************************************************************
 //
@@ -869,6 +870,43 @@
 
 //*****************************************************************************
 //
+void CHAT_SerializeMessages( FArchive &arc )
+{
+	FString serializedMessages[MAX_SAVED_MESSAGES];
+	unsigned int serializedPosition;
+
+	// [AK] We need to save the current position of the saved messages ring buffer
+	// so the saved game knows which entry is the oldest.
+	if ( arc.IsStoring( ))
+	{
+		serializedPosition = g_SavedChatMessages[consoleplayer].getPosition( );
+		arc << serializedPosition;
+	}
+	else
+	{
+		arc << serializedPosition;
+		g_SavedChatMessages[consoleplayer].setPosition( serializedPosition );
+	}
+
+	// [AK] We only need to save the local player's messages, as they'll be the only
+	// player left when the save is loaded. We don't need to save anybody else's.
+	for ( unsigned int i = 0; i < MAX_SAVED_MESSAGES; i++ )
+	{
+		if ( arc.IsStoring( ))
+		{
+			serializedMessages[i] = g_SavedChatMessages[consoleplayer].getOldestEntry( i );
+			arc << serializedMessages[i];
+		}
+		else
+		{
+			arc << serializedMessages[i];
+			g_SavedChatMessages[consoleplayer].put( serializedMessages[i] );
+		}
+	}
+}
+
+//*****************************************************************************
+//
 void CHAT_PrintChatString( ULONG ulPlayer, ULONG ulMode, const char *pszString )
 {
 	ULONG		ulChatLevel = 0;
diff -r e7e1ea6608a7 -r 834659d7b7aa src/chat.h
--- a/src/chat.h	Sat Sep 18 20:16:32 2021 -0400
+++ b/src/chat.h	Sun Sep 19 15:48:29 2021 -0400
@@ -88,6 +88,7 @@
 const char	*CHAT_GetChatMessage( ULONG ulPlayer, ULONG ulOffset ); // [AK]
 void		CHAT_AddChatMessage( ULONG ulPlayer, const char *pszString ); // [AK]
 void		CHAT_ClearChatMessages( ULONG ulPlayer ); // [AK]
+void		CHAT_SerializeMessages( FArchive &arc ); // [AK]
 void		CHAT_PrintChatString( ULONG ulPlayer, ULONG ulMode, const char *pszString );
 
 //*****************************************************************************
diff -r e7e1ea6608a7 -r 834659d7b7aa src/g_level.cpp
--- a/src/g_level.cpp	Sat Sep 18 20:16:32 2021 -0400
+++ b/src/g_level.cpp	Sun Sep 19 15:48:29 2021 -0400
@@ -2225,6 +2225,7 @@
 	AM_SerializeMarkers(arc);
 
 	P_SerializePlayers (arc, hubLoad);
+	CHAT_SerializeMessages (arc); // [AK]
 	P_SerializeSounds (arc);
 	if (arc.IsLoading())
 	{
diff -r e7e1ea6608a7 -r 834659d7b7aa src/networkshared.h
--- a/src/networkshared.h	Sat Sep 18 20:16:32 2021 -0400
+++ b/src/networkshared.h	Sun Sep 19 15:48:29 2021 -0400
@@ -580,6 +580,14 @@
 	{
 		return _data[ ( _position + Offset ) % Length ];
 	}
+	unsigned int getPosition( void )
+	{
+		return _position;
+	}
+	void setPosition( unsigned int pos )
+	{
+		_position = pos % Length;
+	}
 };
 
 #endif	// __NETWORKSHARED_H__