Skip to content

Commit

Permalink
game: add map vote auto filtering by players count
Browse files Browse the repository at this point in the history
By declaring a map with minimum and maximum required player in "mapvoteplayerscount.cfg", the map vote list will be automaticly filtered depending of players count. (Including specs and bots)
  • Loading branch information
Aranud committed Nov 25, 2020
1 parent d3b2cbc commit 128b9a8
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
14 changes: 14 additions & 0 deletions misc/etmain/mapvoteplayerscount.cfg
@@ -0,0 +1,14 @@
// Map Vote Player Count
// config file used for listing maps depending of players number in map vote mod
// if the config file doesn't exist, all the map are listed
// if the map doesn't exist in the config file, the map will appear in the map vote list
// if the minimum / maximum values is < 0, one or both condition is not take into account
// there is no limit for minimum / maximum values
//
// map min max
"oasis" -1 -1
"battery" -1 -1
"goldrush" -1 -1
"radar" -1 -1
"railgun" -1 -1
"fueldump" -1 -1
21 changes: 20 additions & 1 deletion src/game/g_local.h
Expand Up @@ -1298,6 +1298,7 @@ typedef struct level_locals_s
mapVoteInfo_t mapvoteinfo[MAX_VOTE_MAPS];
int mapVoteNumMaps;
int mapsSinceLastXPReset;
qboolean mapVotePlayersCount;

// sv_cvars
svCvar_t svCvars[MAX_SVCVARS];
Expand Down Expand Up @@ -1481,6 +1482,24 @@ team_t G_GetTeamFromEntity(gentity_t *ent);
const char *G_StringContains(const char *str1, const char *str2, int casesensitive);
qboolean G_MatchString(const char *filter, const char *name, int casesensitive);

qboolean CG_ParseMapVotePlayersCountConfig(void);

/**
* @struct mapVotePlayersCount_s
* @typedef mapVotePlayersCount_t
* @brief
*/
typedef struct mapVotePlayersCount_s
{
char map[MAX_QPATH];
int min;
int max;

} mapVotePlayersCount_t;

#define MAX_MAPVOTEPLAYERCOUNT 256
extern mapVotePlayersCount_t mapVotePlayersCount[MAX_MAPVOTEPLAYERCOUNT];

/**
* @struct grefEntity_t
* @brief cut down refEntity_t w/ only stuff needed for player bone calculation
Expand Down Expand Up @@ -2793,7 +2812,7 @@ void G_RailBox(vec_t *origin, vec_t *mins, vec_t *maxs, vec_t *color, int index)
typedef struct weapFireTable_t
{
weapon_t weapon;
gentity_t * (*fire)(gentity_t * ent); ///< -
gentity_t *(*fire)(gentity_t * ent); ///< -
void (*think)(gentity_t *ent); ///< -
void (*free)(gentity_t *ent); ///< -
int eType; ///< -
Expand Down
29 changes: 29 additions & 0 deletions src/game/g_main.c
Expand Up @@ -2573,6 +2573,8 @@ void G_InitGame(int levelTime, int randomSeed, int restart, int etLegacyServer,

trap_SendConsoleCommand(EXEC_APPEND, mapConfig);
}

level.mapVotePlayersCount = CG_ParseMapVotePlayersCountConfig();
}

// Clear out spawn target config strings
Expand Down Expand Up @@ -3395,6 +3397,33 @@ void BeginIntermission(void)
bspptrTmp += strlen(bspptrTmp) + 1;
continue;
}

// check maps depending of players count
if (level.mapVotePlayersCount)
{
int k;
int isValid = qtrue;

for (k = 0; mapVotePlayersCount[k].map[0]; k++)
{
if (!Q_stricmp(mapVotePlayersCount[k].map, str))
{
if ((mapVotePlayersCount[k].min >= 0 && mapVotePlayersCount[k].min > level.numConnectedClients) ||
(mapVotePlayersCount[k].max >= 0 && mapVotePlayersCount[k].max < level.numConnectedClients))
{
isValid = qfalse;
}
break;
}
}

if (!isValid)
{
bspptrTmp += strlen(bspptrTmp) + 1;
continue;
}
}

Q_strncpyz(shuffledNames[j], str, sizeof(shuffledNames[j]));
shuffled[j] = qfalse;
++j;
Expand Down
77 changes: 77 additions & 0 deletions src/game/g_utils.c
Expand Up @@ -2063,3 +2063,80 @@ qboolean G_MatchString(const char *filter, const char *name, int casesensitive)
}
return qtrue;
}

mapVotePlayersCount_t mapVotePlayersCount[MAX_MAPVOTEPLAYERCOUNT];

/**
* @brief CG_ParsePatriclesConfig
*/
qboolean CG_ParseMapVotePlayersCountConfig(void)
{
static const char *filename = "mapvoteplayerscount.cfg";
char *text_p;
int len;
int i;
char *token;
char text[2048];
fileHandle_t f;

// load the file
len = trap_FS_FOpenFile("mapvoteplayerscount.cfg", &f, FS_READ);

if (len <= 0)
{
G_Printf("G_ParseMapVotePlayersCountConfig: File not found: %s\n", filename);
return qfalse;
}

if (len >= sizeof(text) - 1)
{
G_Printf("G_ParseMapVotePlayersCountConfig: File %s too long\n", filename);
trap_FS_FCloseFile(f);
return qfalse;
}

trap_FS_Read(text, len, f);
text[len] = 0;
trap_FS_FCloseFile(f);

// parse the text
text_p = text;

COM_BeginParseSession("G_ParseMapVotePlayersCountConfig");

Com_Memset(mapVotePlayersCount, 0, sizeof(mapVotePlayersCount));

for (i = 0 ; i < MAX_MAPVOTEPLAYERCOUNT; i++)
{
// map name
token = COM_Parse(&text_p);
if (!token[0])
{
break;
}
Q_strncpyz(mapVotePlayersCount[i].map, token, MAX_QPATH);

// map min players
token = COM_Parse(&text_p);
if (!token[0])
{
break;
}
mapVotePlayersCount[i].min = atoi(token);

// map max players
token = COM_Parse(&text_p);
if (!token[0])
{
break;
}
mapVotePlayersCount[i].max = atoi(token);
}

if (i == MAX_MAPVOTEPLAYERCOUNT)
{
G_Printf("G_ParseMapVotePlayersCountConfig: Too much map registered in file %s, max is %d\n", filename, MAX_MAPVOTEPLAYERCOUNT);
}

return qtrue;
}

2 comments on commit 128b9a8

@rmarquis
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refs #1318

@Aranud
Copy link
Contributor Author

@Aranud Aranud commented on 128b9a8 Nov 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouah, I didn't even know a ticket was existing for that. Thanks you very much !

Please sign in to comment.