diff --git a/source/duke3d/src/config.cpp b/source/duke3d/src/config.cpp index 4fb1bf77a..5314d83a7 100644 --- a/source/duke3d/src/config.cpp +++ b/source/duke3d/src/config.cpp @@ -116,24 +116,42 @@ const char *CONFIG_AnalogNumToName(int32_t func) } -void CONFIG_SetDefaultKeys(const char (*keyptr)[MAXGAMEFUNCLEN]) +void CONFIG_SetDefaultKeys(const char (*keyptr)[MAXGAMEFUNCLEN], bool lazy/*=false*/) { - Bmemset(ud.config.KeyboardKeys, 0xff, sizeof(ud.config.KeyboardKeys)); + if (!lazy) + { + Bmemset(ud.config.KeyboardKeys, 0xff, sizeof(ud.config.KeyboardKeys)); + CONTROL_ClearAllBinds(); + } - CONTROL_ClearAllBinds(); - - for (size_t i=0; i < ARRAY_SIZE(gamefunctions); ++i) + for (int i=0; i < ARRAY_SSIZE(gamefunctions); ++i) { if (gamefunctions[i][0] == '\0') continue; - ud.config.KeyboardKeys[i][0] = KB_StringToScanCode(keyptr[i<<1]); - ud.config.KeyboardKeys[i][1] = KB_StringToScanCode(keyptr[(i<<1)+1]); + auto &key = ud.config.KeyboardKeys[i]; + + int const default0 = KB_StringToScanCode(keyptr[i<<1]); + int const default1 = KB_StringToScanCode(keyptr[(i<<1)+1]); + + // skip the function if the default key is already used + // or the function is assigned to another key + if (lazy && (CONTROL_KeyIsBound(default0) || key[0] != 0xff)) + continue; + + key[0] = default0; + key[1] = default1; + + if (key[0]) + CONTROL_FreeKeyBind(key[0]); + + if (key[1]) + CONTROL_FreeKeyBind(key[1]); if (i == gamefunc_Show_Console) - OSD_CaptureKey(ud.config.KeyboardKeys[i][0]); + OSD_CaptureKey(key[0]); else - CONFIG_MapKey(i, ud.config.KeyboardKeys[i][0], 0, ud.config.KeyboardKeys[i][1], 0); + CONFIG_MapKey(i, key[0], 0, key[1], 0); } } diff --git a/source/duke3d/src/config.h b/source/duke3d/src/config.h index 0a4f52f8f..2ec9691ac 100644 --- a/source/duke3d/src/config.h +++ b/source/duke3d/src/config.h @@ -30,7 +30,7 @@ void CONFIG_WriteSetup(uint32_t flags); void CONFIG_SetDefaults(void); void CONFIG_SetupMouse(void); void CONFIG_SetupJoystick(void); -void CONFIG_SetDefaultKeys(const char (*keyptr)[MAXGAMEFUNCLEN]); +void CONFIG_SetDefaultKeys(const char (*keyptr)[MAXGAMEFUNCLEN], bool lazy=false); int32_t CONFIG_GetMapBestTime(char const *mapname, uint8_t const *mapmd4); int CONFIG_SetMapBestTime(uint8_t const *mapmd4, int32_t tm); diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index ca0bb94c7..ca244df26 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -6519,6 +6519,8 @@ int app_main(int argc, char const * const * argv) OSD_Exec(tempbuf); OSD_Exec("autoexec.cfg"); + CONFIG_SetDefaultKeys(keydefaults, true); + system_getcvars(); if (g_networkMode != NET_DEDICATED_SERVER)