From eef1caa512594383e02bfcbb962cc94627dac4bc Mon Sep 17 00:00:00 2001
From: cybersphinx <chr.ohm@gmx.net>
Date: Tue, 19 Jul 2011 11:22:28 +0200
Subject: [PATCH 3/4] Redo mapmod support.

Based on Zarel's earlier patch. Also adds support for unzipped maps in
their own subdirectory.
---
 src/init.cpp   |   11 +++++++++++
 src/levels.cpp |    4 +++-
 src/main.cpp   |   29 +++++++++++++++++++++++++++++
 src/main.h     |    3 +++
 4 files changed, 46 insertions(+), 1 deletions(-)

diff --git a/src/init.cpp b/src/init.cpp
index 0b7d7c0..11a3780 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -357,6 +357,15 @@ bool rebuildSearchPath( searchPathMode mode, bool force )
 				curSearchPath = searchPathRegistry;
 				while (curSearchPath->lowerPriority)
 					curSearchPath = curSearchPath->lowerPriority;
+				// Add the selected map first, for mapmod support
+                                while (curSearchPath)
+				{
+					addSubdirs(curSearchPath->path, "maps", PHYSFS_APPEND, current_map, false);
+					curSearchPath = curSearchPath->higherPriority;
+				}
+				curSearchPath = searchPathRegistry;
+				while (curSearchPath->lowerPriority)
+					curSearchPath = curSearchPath->lowerPriority;
 				while( curSearchPath )
 				{
 #ifdef DEBUG
@@ -908,6 +917,8 @@ bool stageOneShutDown(void)
 
 	debug(LOG_TEXTURE, "=== stageOneShutDown ===");
 	pie_TexShutDown();
+	// no map for the main menu
+	setCurrentMap((char*)"", 1);
 	// Use mod_multiplay as the default (campaign might have set it to mod_singleplayer)
 	rebuildSearchPath( mod_multiplay, true );
 	pie_TexInit(); // restart it
diff --git a/src/levels.cpp b/src/levels.cpp
index 800e810..9b30633 100644
--- a/src/levels.cpp
+++ b/src/levels.cpp
@@ -47,6 +47,7 @@
 #include "research.h"
 #include "lib/framework/lexer_input.h"
 #include "effects.h"
+#include "main.h"
 
 extern int lev_get_lineno(void);
 extern char* lev_get_text(void);
@@ -685,7 +686,8 @@ bool levLoadData(const char* name, char *pSaveName, GAME_TYPE saveType)
 		}
 	}
 
-	if (!rebuildSearchPath(psNewLevel->dataDir, false))
+	setCurrentMap(psNewLevel->pName, psNewLevel->players);
+	if (!rebuildSearchPath(psNewLevel->dataDir, true))
 	{
 		return false;
 	}
diff --git a/src/main.cpp b/src/main.cpp
index 5bab4e5..c987eee 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -119,6 +119,8 @@ char * override_mods[MAX_MODS] = { NULL };
 char * override_mod_list = NULL;
 bool use_override_mods = false;
 
+char *current_map[3] = { NULL };
+
 char * loaded_mods[MAX_MODS] = { NULL };
 char * mod_list = NULL;
 int num_loaded_mods = 0;
@@ -286,6 +288,33 @@ void clearOverrideMods(void)
 	override_mod_list = NULL;
 }
 
+void setCurrentMap(char* map, int maxPlayers)
+{
+	free(current_map[0]);
+	free(current_map[1]);
+	// Transform "Sk-Rush-T2" into "4c-Rush.wz" so it can be matched by the map loader
+	current_map[0] = (char*)malloc(strlen(map) + 1 + 7);
+	snprintf(current_map[0], 3, "%d", maxPlayers);
+	strcat(current_map[0], "c-");
+	if (strncmp(map, "Sk-", 3) == 0)
+	{
+		strcat(current_map[0], map + 3);
+	}
+	else
+	{
+		strcat(current_map[0], map);
+	}
+	if (strncmp(current_map[0] + strlen(current_map[0]) - 3, "-T", 2) == 0)
+	{
+		current_map[0][strlen(current_map[0]) - 3] = '\0';
+	}
+	current_map[1] = (char*)malloc(strlen(map) + 1 + 7);
+	strcpy(current_map[1], current_map[0]);
+	strcat(current_map[1],".wz");
+	current_map[2] = NULL;
+}
+
+
 void addLoadedMod(const char * modname)
 {
 	if (num_loaded_mods >= MAX_MODS)
diff --git a/src/main.h b/src/main.h
index 98ef70f..e39f20f 100644
--- a/src/main.h
+++ b/src/main.h
@@ -52,4 +52,7 @@ extern char * override_mods[MAX_MODS];
 extern char * override_mod_list;
 extern bool use_override_mods;
 
+void setCurrentMap(char* map, int maxPlayers);
+extern char *current_map[3];
+
 #endif // __INCLUDED_SRC_MAIN_H__
-- 
1.7.5.4


