From 055a225d4c2a18e9aff2f0e682cf89048d1efefe Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Fri, 5 Oct 2018 22:40:50 +0300 Subject: [PATCH] System username used as player's nickname by default --- src/game/g_menu.pas | 4 ++-- src/game/g_options.pas | 21 ++++++++++++++++++--- src/shared/envvars.pas | 16 ++++++++++++++-- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/game/g_menu.pas b/src/game/g_menu.pas index ef79a76..23fbf2e 100644 --- a/src/game/g_menu.pas +++ b/src/game/g_menu.pas @@ -286,8 +286,8 @@ begin gPlayer2Settings.Color := Model.Color; end; - if gPlayer1Settings.Name = '' then gPlayer1Settings.Name := 'Player1'; - if gPlayer2Settings.Name = '' then gPlayer2Settings.Name := 'Player2'; + if gPlayer1Settings.Name = '' then gPlayer1Settings.Name := GenPlayerName(1); + if gPlayer2Settings.Name = '' then gPlayer2Settings.Name := GenPlayerName(2); if g_Game_IsServer then begin diff --git a/src/game/g_options.pas b/src/game/g_options.pas index 86761de..506fc18 100644 --- a/src/game/g_options.pas +++ b/src/game/g_options.pas @@ -61,6 +61,8 @@ type P2Control: TPlayerControl; end; +function GenPlayerName (n: Integer): String; + procedure g_Options_SetDefault(); procedure g_Options_Read(FileName: String); procedure g_Options_Write(FileName: String); @@ -137,7 +139,18 @@ uses {$INCLUDE ../nogl/noGLuses.inc} e_log, e_input, g_window, g_sound, g_gfx, g_player, Math, g_map, g_net, g_netmaster, SysUtils, CONFIG, g_game, g_main, e_texture, - g_items, wadreader, e_graphics, g_touch, SDL2; + g_items, wadreader, e_graphics, g_touch, SDL2, envvars; + + var + machine: Integer; + + function GenPlayerName (n: Integer): String; + begin + ASSERT(n >= 1); + Result := GetUserName; + if Result = '' then Result := 'Player' + IntToStr(machine MOD 10000); + if n > 1 then Result := Copy(Result, 1, 10) + (' ' + IntToStr(n)) + end; procedure g_Options_SetDefaultVideo; var @@ -259,7 +272,7 @@ begin with gPlayer1Settings do begin - Name := 'Player1'; + Name := GenPlayerName(1); Model := STD_PLAYER_MODEL; Color.R := PLAYER1_DEF_COLOR.R; Color.G := PLAYER1_DEF_COLOR.G; @@ -304,7 +317,7 @@ begin with gPlayer2Settings do begin - Name := 'Player2'; + Name := GenPlayerName(2); Model := STD_PLAYER_MODEL; Color.R := PLAYER2_DEF_COLOR.R; Color.G := PLAYER2_DEF_COLOR.G; @@ -1039,4 +1052,6 @@ begin config.Free(); end; +initialization + machine := Random(10000) end. diff --git a/src/shared/envvars.pas b/src/shared/envvars.pas index 941b8e0..3774eab 100644 --- a/src/shared/envvars.pas +++ b/src/shared/envvars.pas @@ -19,9 +19,10 @@ unit envvars; interface -uses SysUtils, CTypes; + uses SysUtils, CTypes; -function SetEnvVar(const VarName: AnsiString; const VarVal: AnsiString): Boolean; + function SetEnvVar(const VarName: AnsiString; const VarVal: AnsiString): Boolean; + function GetUserName: String; implementation @@ -46,4 +47,15 @@ begin Result := (setenv(PChar(VarName), PChar(VarVal), 1) = 0); end; + function GetUserName: String; + begin + {$IF DEFINED(WINDOWS)} + Result := SysUtils.GetEnvironmentVariable('USERNAME') + {$ELSEIF DEFINED(UNIX)} + Result := SysUtils.GetEnvironmentVariable('USER') + {$ELSE} + Result := '' + {$ENDIF} + end; + end. -- 2.29.2