Maelstrom: Fill out the correct name based on the slot type

https://github.com/libsdl-org/Maelstrom/commit/267dce5370d42cf91c03ec3d026c862417c5b00b

From 267dce5370d42cf91c03ec3d026c862417c5b00b Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 23 Nov 2011 22:35:18 -0500
Subject: [PATCH] Fill out the correct name based on the slot type Kick players
 out of a slot if it is no longer a network slot

---
 game/gameinfo.cpp | 28 +++++++++++-----------------
 game/gameinfo.h   |  5 +++--
 game/lobby.cpp    | 23 +++++++++++++++++------
 game/main.cpp     | 10 ++++------
 4 files changed, 35 insertions(+), 31 deletions(-)

diff --git a/game/gameinfo.cpp b/game/gameinfo.cpp
index ce0f7b18..e71a0586 100644
--- a/game/gameinfo.cpp
+++ b/game/gameinfo.cpp
@@ -49,8 +49,7 @@ GameInfo::Reset()
 }
 
 void
-GameInfo::SetHost(const char *name,
-		  Uint8 wave, Uint8 lives, Uint8 turbo, Uint8 deathMatch)
+GameInfo::SetHost(Uint8 wave, Uint8 lives, Uint8 turbo, Uint8 deathMatch)
 {
 	Reset();
 
@@ -65,21 +64,20 @@ GameInfo::SetHost(const char *name,
 	assert(HOST_NODE == 0);
 	nodes[HOST_NODE].nodeID = localID;
 	numNodes = 1;
-
-	// We are the first player
-	SetPlayerSlot(0, name, CONTROL_LOCAL);
 }
 
 void
 GameInfo::SetPlayerSlot(int slot, const char *name, Uint8 controlMask)
 {
 	GameInfoPlayer *player = &players[slot];
-	if (controlMask == CONTROL_NETWORK) {
-		player->nodeID = 0;
-	} else {
+
+	SDL_strlcpy(player->name, name ? name : "", sizeof(player->name));
+
+	if (IS_LOCAL_CONTROL(controlMask) {
 		player->nodeID = localID;
+	} else {
+		player->nodeID = 0;
 	}
-	SDL_strlcpy(player->name, name ? name : "", sizeof(player->name));
 	player->controlMask = controlMask;
 
 	UpdateUI(player);
@@ -89,15 +87,9 @@ void
 GameInfo::SetPlayerName(int slot, const char *name)
 {
 	GameInfoPlayer *player = &players[slot];
+
 	SDL_strlcpy(player->name, name ? name : "", sizeof(player->name));
-	UpdateUI(player);
-}
 
-void
-GameInfo::SetPlayerControls(int slot, Uint8 controlMask)
-{
-	GameInfoPlayer *player = &players[slot];
-	player->controlMask = controlMask;
 	UpdateUI(player);
 }
 
@@ -175,8 +167,10 @@ GameInfo::CopyFrom(const GameInfo &rhs)
 			sizeof(players[i].name));
 		if (players[i].nodeID == localID) {
 			players[i].controlMask = CONTROL_LOCAL;
-		} else {
+		} else if (players[i].nodeID != 0) {
 			players[i].controlMask = CONTROL_NETWORK;
+		} else {
+			players[i].controlMask = CONTROL_NONE;
 		}
 	}
 
diff --git a/game/gameinfo.h b/game/gameinfo.h
index e0b622cd..f30f7183 100644
--- a/game/gameinfo.h
+++ b/game/gameinfo.h
@@ -47,6 +47,8 @@ enum PLAYER_CONTROL {
 #endif
 };
 
+#define IS_LOCAL_CONTROL(X)	(X != CONTROL_NONE && X != CONTROL_NETWORK && X != CONTROL_REPLAY)
+
 enum NODE_STATE_FLAG {
 	STATE_NONE	= 0x00,
 	STATE_ABORT	= 0x01,
@@ -116,11 +118,10 @@ class GameInfo
 		localID = uniqueID;
 	}
 
-	void SetHost(const char *name, Uint8 wave, Uint8 lives, Uint8 turbo, Uint8 deathMatch);
+	void SetHost(Uint8 wave, Uint8 lives, Uint8 turbo, Uint8 deathMatch);
 
 	void SetPlayerSlot(int slot, const char *name, Uint8 controlMask);
 	void SetPlayerName(int slot, const char *name);
-	void SetPlayerControls(int slot, Uint8 controlMask);
 	bool AddNetworkPlayer(Uint32 nodeID, const IPaddress &address, const char *name);
 
 	void CopyFrom(const GameInfo &rhs);
diff --git a/game/lobby.cpp b/game/lobby.cpp
index e2a42d9b..59f429fd 100644
--- a/game/lobby.cpp
+++ b/game/lobby.cpp
@@ -48,15 +48,21 @@ class SelectControlCallback : public UIClickCallback
 		m_lobby(lobby), m_dialog(dialog), m_game(game), m_index(index), m_controlType(controlType) { }
 
 	virtual void operator()() {
-		// Select the control and hide the dialog
-		if (m_controlType == CONTROL_NONE) {
+		// Kick any player that was connected
+		if (m_controlType != CONTROL_NETWORK) {
 			const GameInfoPlayer *player = m_game.GetPlayer(m_index);
 			int nodeIndex = m_game.GetNodeIndex(player->nodeID);
 			if (nodeIndex >= 0) {
 				m_lobby->SendKick(nodeIndex);
 			}
 		}
-		m_game.SetPlayerControls(m_index, m_controlType);
+
+		// Select the control and hide the dialog
+		if (IS_LOCAL_CONTROL(m_controlType)) {
+			m_game.SetPlayerSlot(m_index, prefs->GetString(PREFERENCES_HANDLE), m_controlType);
+		} else {
+			m_game.SetPlayerSlot(m_index, NULL, m_controlType);
+		}
 		m_dialog->Hide();
 	}
 
@@ -420,17 +426,22 @@ LobbyDialogDelegate::SetState(LOBBY_STATE state)
 			SendLeaveRequest();
 		}
 	} else if (state == STATE_HOSTING) {
-		m_game.SetHost(prefs->GetString(PREFERENCES_HANDLE),
-				DEFAULT_START_WAVE,
+		m_game.SetHost(DEFAULT_START_WAVE,
 				DEFAULT_START_LIVES,
 				DEFAULT_START_TURBO,
 				prefs->GetNumber(PREFERENCES_DEATHMATCH));
 
 		// Set up the controls for this game
 		for (i = 0; i < MAX_PLAYERS; ++i) {
+			Uint8 controlType;
 			char name[128];
 			SDL_snprintf(name, sizeof(name), "Player%d.Controls", i+1);
-			m_game.SetPlayerControls(i, prefs->GetNumber(name, (i == 0 ? CONTROL_LOCAL : CONTROL_NETWORK)));
+			controlType = prefs->GetNumber(name, (i == 0 ? CONTROL_LOCAL : CONTROL_NETWORK));
+			if (IS_LOCAL_CONTROL(controlType)) {
+				m_game.SetPlayerSlot(i, prefs->GetString(PREFERENCES_HANDLE), controlType);
+			} else {
+				m_game.SetPlayerSlot(i, NULL, controlType);
+			}
 		}
 	} else if (state == STATE_LISTING) {
 		ClearGameList();
diff --git a/game/main.cpp b/game/main.cpp
index 3c947c3c..6e1d670f 100644
--- a/game/main.cpp
+++ b/game/main.cpp
@@ -72,10 +72,8 @@ static void RunSinglePlayerGame()
 }
 static void RunPlayGame(void*)
 {
-	gGameInfo.SetHost(prefs->GetString(PREFERENCES_HANDLE),
-				DEFAULT_START_WAVE,
-				DEFAULT_START_LIVES,
-				DEFAULT_START_TURBO, 0);
+	gGameInfo.SetHost(DEFAULT_START_WAVE, DEFAULT_START_LIVES, DEFAULT_START_TURBO, 0);
+	gGameInfo.SetPlayerSlot(0, prefs->GetString(PREFERENCES_HANDLE), CONTROL_LOCAL);
 	RunSinglePlayerGame();
 }
 static void RunReplayGame(const char *file)
@@ -175,8 +173,8 @@ static void CheatDialogDone(UIDialog *dialog, int status)
 		Delay(SOUND_DELAY);
 		sound->PlaySound(gNewLife, 5);
 		Delay(SOUND_DELAY);
-		gGameInfo.SetHost(prefs->GetString(PREFERENCES_HANDLE),
-					wave, lives, turbo, 0);
+		gGameInfo.SetHost(wave, lives, turbo, 0);
+		gGameInfo.SetPlayerSlot(0, prefs->GetString(PREFERENCES_HANDLE), CONTROL_LOCAL);
 		RunSinglePlayerGame();
 	}
 }