mirror of
https://github.com/KartKrewDev/RingRacers.git
synced 2025-12-23 00:12:27 +00:00
Move choose and chooseweighted commands to command.c
Fixes disabled under dedicated and it's the appropriate
place for these. Effectively cherry pick of 35b82b6dd9
This commit is contained in:
parent
3f3e3ffdf6
commit
7a56e5ade6
2 changed files with 80 additions and 78 deletions
|
|
@ -36,6 +36,7 @@
|
|||
#include "d_netfil.h" // findfile
|
||||
#include "r_data.h" // Color_cons_t
|
||||
#include "r_skins.h"
|
||||
#include "m_random.h"
|
||||
|
||||
//========
|
||||
// protos.
|
||||
|
|
@ -53,6 +54,8 @@ static void COM_Wait_f(void);
|
|||
static void COM_Help_f(void);
|
||||
static void COM_Toggle_f(void);
|
||||
static void COM_Add_f(void);
|
||||
static void COM_Choose_f(void);
|
||||
static void COM_ChooseWeighted_f(void);
|
||||
|
||||
static void CV_EnforceExecVersion(void);
|
||||
static boolean CV_FilterVarByVersion(consvar_t *v, const char *valstr);
|
||||
|
|
@ -361,6 +364,8 @@ void COM_Init(void)
|
|||
COM_AddCommand("help", COM_Help_f);
|
||||
COM_AddCommand("toggle", COM_Toggle_f);
|
||||
COM_AddCommand("add", COM_Add_f);
|
||||
COM_AddCommand("choose", COM_Choose_f);
|
||||
COM_AddCommand("chooseweighted", COM_ChooseWeighted_f);
|
||||
RegisterNetXCmd(XD_NETVAR, Got_NetVar);
|
||||
}
|
||||
|
||||
|
|
@ -1075,6 +1080,81 @@ static void COM_Add_f(void)
|
|||
CV_AddValue(cvar, atoi(COM_Argv(2)));
|
||||
}
|
||||
|
||||
static void COM_Choose_f(void)
|
||||
{
|
||||
size_t na = COM_Argc();
|
||||
|
||||
if (na < 2)
|
||||
{
|
||||
CONS_Printf(M_GetText("choose <option1> [<option2>] [<option3>] [...]: Picks a command at random\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
COM_BufAddText(COM_Argv(M_RandomKey(na - 1) + 1));
|
||||
COM_BufAddText("\n");
|
||||
}
|
||||
|
||||
static void COM_ChooseWeighted_f(void)
|
||||
{
|
||||
size_t na = COM_Argc();
|
||||
size_t i, cmd;
|
||||
const char *commands[40];
|
||||
INT32 weights[40];
|
||||
INT32 totalWeight = 0;
|
||||
INT32 roll;
|
||||
|
||||
if (na < 3)
|
||||
{
|
||||
CONS_Printf(M_GetText("chooseweighted <option1> <weight1> [<option2> <weight2>] [<option3> <weight3>] [...]: Picks a command with weighted randomization\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
memset(weights, 0, sizeof(weights));
|
||||
|
||||
i = 1;
|
||||
cmd = 0;
|
||||
while (i < na)
|
||||
{
|
||||
commands[cmd] = COM_Argv(i);
|
||||
|
||||
i++;
|
||||
if (i >= na)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
weights[cmd] = atoi(COM_Argv(i));
|
||||
totalWeight += weights[cmd];
|
||||
|
||||
i++;
|
||||
cmd++;
|
||||
}
|
||||
|
||||
if (cmd == 0 || totalWeight <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
roll = M_RandomRange(1, totalWeight);
|
||||
|
||||
for (i = 0; i < cmd; i++)
|
||||
{
|
||||
if (roll <= weights[i])
|
||||
{
|
||||
if (commands[i] == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
COM_BufAddText(commands[i]);
|
||||
COM_BufAddText("\n");
|
||||
break;
|
||||
}
|
||||
|
||||
roll -= weights[i];
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// VARIABLE SIZE BUFFERS
|
||||
// =========================================================================
|
||||
|
|
|
|||
|
|
@ -34,7 +34,6 @@
|
|||
#include "k_menu.h"
|
||||
#include "filesrch.h"
|
||||
#include "m_misc.h"
|
||||
#include "m_random.h"
|
||||
|
||||
#ifdef _WINDOWS
|
||||
#include "win32/win_main.h"
|
||||
|
|
@ -244,81 +243,6 @@ static void CONS_Bind_f(void)
|
|||
bindtable[key] = Z_StrDup(COM_Argv(2));
|
||||
}
|
||||
|
||||
static void CONS_Choose_f(void)
|
||||
{
|
||||
size_t na = COM_Argc();
|
||||
|
||||
if (na < 2)
|
||||
{
|
||||
CONS_Printf(M_GetText("choose <option1> [<option2>] [<option3>] [...]: Picks a command at random\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
COM_BufAddText(COM_Argv(M_RandomKey(na - 1) + 1));
|
||||
COM_BufAddText("\n");
|
||||
}
|
||||
|
||||
static void CONS_ChooseWeighted_f(void)
|
||||
{
|
||||
size_t na = COM_Argc();
|
||||
size_t i, cmd;
|
||||
const char *commands[40];
|
||||
INT32 weights[40];
|
||||
INT32 totalWeight = 0;
|
||||
INT32 roll;
|
||||
|
||||
if (na < 3)
|
||||
{
|
||||
CONS_Printf(M_GetText("chooseweighted <option1> <weight1> [<option2> <weight2>] [<option3> <weight3>] [...]: Picks a command with weighted randomization\n"));
|
||||
return;
|
||||
}
|
||||
|
||||
memset(weights, 0, sizeof(weights));
|
||||
|
||||
i = 1;
|
||||
cmd = 0;
|
||||
while (i < na)
|
||||
{
|
||||
commands[cmd] = COM_Argv(i);
|
||||
|
||||
i++;
|
||||
if (i >= na)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
weights[cmd] = atoi(COM_Argv(i));
|
||||
totalWeight += weights[cmd];
|
||||
|
||||
i++;
|
||||
cmd++;
|
||||
}
|
||||
|
||||
if (cmd == 0 || totalWeight <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
roll = M_RandomRange(1, totalWeight);
|
||||
|
||||
for (i = 0; i < cmd; i++)
|
||||
{
|
||||
if (roll <= weights[i])
|
||||
{
|
||||
if (commands[i] == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
COM_BufAddText(commands[i]);
|
||||
COM_BufAddText("\n");
|
||||
break;
|
||||
}
|
||||
|
||||
roll -= weights[i];
|
||||
}
|
||||
}
|
||||
|
||||
//======================================================================
|
||||
// CONSOLE SETUP
|
||||
//======================================================================
|
||||
|
|
@ -521,8 +445,6 @@ void CON_Init(void)
|
|||
CV_RegisterVar(&cons_backpic);
|
||||
CV_RegisterVar(&cons_backcolor);
|
||||
COM_AddCommand("bind", CONS_Bind_f);
|
||||
COM_AddCommand("choose", CONS_Choose_f);
|
||||
COM_AddCommand("chooseweighted", CONS_ChooseWeighted_f);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue