simplify matters in cheat code handling, again (#768)

One less level of function pointer indirection, still full clang-15 compatible.
This commit is contained in:
Fabian Greffrath 2022-10-14 21:28:49 +02:00 committed by GitHub
parent 004cd3928d
commit e413bb866a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 51 deletions

View File

@ -52,16 +52,16 @@
//
//-----------------------------------------------------------------------------
static void cheat_mus(cheatarg_t arg);
static void cheat_mus(char *buf);
static void cheat_choppers();
static void cheat_god();
static void cheat_fa();
static void cheat_k();
static void cheat_kfa();
static void cheat_noclip();
static void cheat_pw(cheatarg_t arg);
static void cheat_pw(int pw);
static void cheat_behold();
static void cheat_clev(cheatarg_t arg);
static void cheat_clev(char *buf);
static void cheat_clev0();
static void cheat_mypos();
static void cheat_comp();
@ -74,11 +74,11 @@ static void cheat_hom();
static void cheat_fast();
static void cheat_key();
static void cheat_keyx();
static void cheat_keyxx(cheatarg_t arg);
static void cheat_keyxx(int key);
static void cheat_weap();
static void cheat_weapx(cheatarg_t arg);
static void cheat_weapx(char *buf);
static void cheat_ammo();
static void cheat_ammox(cheatarg_t arg);
static void cheat_ammox(char *buf);
static void cheat_smart();
static void cheat_pitch();
static void cheat_nuke();
@ -87,7 +87,7 @@ static void cheat_buddha();
static void cheat_spechits();
static void cheat_notarget();
static void cheat_autoaim(); // killough 7/19/98
static void cheat_autoaim(); // killough 7/19/98
static void cheat_tst();
static void cheat_showfps(); // [FG] FPS counter widget
@ -114,7 +114,7 @@ static void cheat_showfps(); // [FG] FPS counter widget
struct cheat_s cheat[] = {
{"idmus", "Change music", always,
{cheat_mus}, {-2} },
{cheat_mus}, -2 },
{"idchoppers", "Chainsaw", not_net | not_demo,
{cheat_choppers} },
@ -141,31 +141,31 @@ struct cheat_s cheat[] = {
{cheat_noclip} },
{"idbeholdo", NULL, not_net | not_demo | not_deh,
{cheat_pw}, {NUMPOWERS} }, // [FG] disable all powerups at once
{cheat_pw}, NUMPOWERS }, // [FG] disable all powerups at once
{"idbeholdv", "Invincibility", not_net | not_demo,
{cheat_pw}, {pw_invulnerability} },
{cheat_pw}, pw_invulnerability },
{"idbeholds", "Berserk", not_net | not_demo,
{cheat_pw}, {pw_strength} },
{cheat_pw}, pw_strength },
{"idbeholdi", "Invisibility", not_net | not_demo,
{cheat_pw}, {pw_invisibility} },
{cheat_pw}, pw_invisibility },
{"idbeholdr", "Radiation Suit", not_net | not_demo,
{cheat_pw}, {pw_ironfeet} },
{cheat_pw}, pw_ironfeet },
{"idbeholda", "Auto-map", not_net | not_demo,
{cheat_pw}, {pw_allmap} },
{cheat_pw}, pw_allmap },
{"idbeholdl", "Lite-Amp Goggles", not_net | not_demo,
{cheat_pw}, {pw_infrared} },
{cheat_pw}, pw_infrared },
{"idbehold", "BEHOLD menu", not_net | not_demo,
{cheat_behold} },
{"idclev", "Level Warp", not_net | not_demo | not_menu,
{cheat_clev}, {-2} },
{cheat_clev}, -2 },
{"idclev", "Level Warp", not_net | not_demo | not_menu,
{cheat_clev0} },
@ -204,34 +204,34 @@ struct cheat_s cheat[] = {
{cheat_keyx} },
{"keyrc", NULL, not_net | not_demo,
{cheat_keyxx}, {it_redcard} },
{cheat_keyxx}, it_redcard },
{"keyyc", NULL, not_net | not_demo,
{cheat_keyxx}, {it_yellowcard} },
{cheat_keyxx}, it_yellowcard },
{"keybc", NULL, not_net | not_demo,
{cheat_keyxx}, {it_bluecard} },
{cheat_keyxx}, it_bluecard },
{"keyrs", NULL, not_net | not_demo,
{cheat_keyxx}, {it_redskull} },
{cheat_keyxx}, it_redskull },
{"keyys", NULL, not_net | not_demo,
{cheat_keyxx}, {it_yellowskull} },
{cheat_keyxx}, it_yellowskull },
{"keybs", NULL, not_net | not_demo,
{cheat_keyxx}, {it_blueskull} }, // killough 2/16/98: end generalized keys
{cheat_keyxx}, it_blueskull }, // killough 2/16/98: end generalized keys
{"weap", NULL, not_net | not_demo,
{cheat_weap} }, // killough 2/16/98: generalized weapon cheats
{"weap", NULL, not_net | not_demo,
{cheat_weapx}, {-1} },
{cheat_weapx}, -1 },
{"ammo", NULL, not_net | not_demo,
{cheat_ammo} },
{"ammo", NULL, not_net | not_demo,
{cheat_ammox}, {-1} }, // killough 2/16/98: end generalized weapons
{cheat_ammox}, -1 }, // killough 2/16/98: end generalized weapons
{"tran", NULL, always,
{cheat_tran} }, // invoke translucency // phares
@ -300,9 +300,8 @@ static void cheat_autoaim()
"Projectile autoaiming off";
}
static void cheat_mus(cheatarg_t arg)
static void cheat_mus(char *buf)
{
char *buf = arg.s;
int musnum;
//jff 3/20/98 note: this cheat allowed in netgame/demorecord
@ -464,9 +463,8 @@ static void cheat_noclip()
}
// 'behold?' power-up cheats (modified for infinite duration -- killough)
static void cheat_pw(cheatarg_t arg)
static void cheat_pw(int pw)
{
int pw = arg.i;
if (pw == NUMPOWERS)
{
memset(plyr->powers, 0, sizeof(plyr->powers));
@ -510,9 +508,8 @@ static void cheat_clev0()
free(cur);
}
static void cheat_clev(cheatarg_t arg)
static void cheat_clev(char *buf)
{
char *buf = arg.s;
int epsd, map;
mapentry_t* entry;
@ -623,7 +620,7 @@ static void cheat_tran()
D_SetPredefinedTranslucency();
}
static void cheat_massacre() // jff 2/01/98 kill all monsters
static void cheat_massacre() // jff 2/01/98 kill all monsters
{
// jff 02/01/98 'em' cheat - kill all monsters
// partially taken from Chi's .46 port
@ -844,9 +841,8 @@ static void cheat_keyx()
plyr->message = "Card, Skull"; // Ty 03/27/98 - *not* externalized
}
static void cheat_keyxx(cheatarg_t arg)
static void cheat_keyxx(int key)
{
int key = arg.i;
plyr->message = (plyr->cards[key] = !plyr->cards[key]) ?
"Key Added" : "Key Removed"; // Ty 03/27/98 - *not* externalized
}
@ -859,9 +855,8 @@ static void cheat_weap()
"Weapon number 1-9" : "Weapon number 1-8";
}
static void cheat_weapx(cheatarg_t arg)
static void cheat_weapx(char *buf)
{
char *buf = arg.s;
int w = *buf - '1';
if ((w==wp_supershotgun && gamemode!=commercial) || // killough 2/28/98
@ -869,7 +864,7 @@ static void cheat_weapx(cheatarg_t arg)
return;
if (w==wp_fist) // make '1' apply beserker strength toggle
cheat_pw((cheatarg_t){.i = pw_strength});
cheat_pw(pw_strength);
else
if (w >= 0 && w < NUMWEAPONS)
{
@ -890,9 +885,8 @@ static void cheat_ammo()
plyr->message = "Ammo 1-4, Backpack"; // Ty 03/27/98 - *not* externalized
}
static void cheat_ammox(cheatarg_t arg)
static void cheat_ammox(char *buf)
{
char *buf = arg.s;
int a = *buf - '1';
if (*buf == 'b') // Ty 03/27/98 - strings *not* externalized
if ((plyr->backpack = !plyr->backpack))
@ -960,7 +954,7 @@ boolean M_FindCheats(int key)
{
*arg++ = tolower(key); // store key in arg buffer
if (!--argsleft) // if last key in arg list,
cheat[cht].func.p1((cheatarg_t){.s = argbuf}); // process the arg buffer
cheat[cht].func.s(argbuf); // process the arg buffer
return 1; // affirmative response
}
@ -1006,18 +1000,18 @@ boolean M_FindCheats(int key)
!(cheat[i].when & beta_only && !beta_emulation) &&
!(cheat[i].when & not_deh && cheat[i].deh_modified))
{
if (cheat[i].arg.i < 0) // if additional args are required
if (cheat[i].arg < 0) // if additional args are required
{
cht = i; // remember this cheat code
arg = argbuf; // point to start of arg buffer
argsleft = -cheat[i].arg.i; // number of args expected
argsleft = -cheat[i].arg; // number of args expected
ret = 1; // responder has eaten key
}
else
if (!matchedbefore) // allow only one cheat at a time
{
matchedbefore = ret = 1; // responder has eaten key
cheat[i].func.p1(cheat[i].arg); // call cheat handler
cheat[i].func.i(cheat[i].arg); // call cheat handler
}
}
return ret;

View File

@ -31,19 +31,15 @@
#include "doomtype.h"
typedef union cheatarg_u
{
int i;
char *s;
} cheatarg_t;
typedef void (*cheatf_v)();
typedef void (*cheatf_p1)(cheatarg_t);
typedef void (*cheatf_i)(int i);
typedef void (*cheatf_s)(char *s);
typedef union
{
cheatf_v v;
cheatf_p1 p1;
cheatf_i i;
cheatf_s s;
} cheatf_t;
// killough 4/16/98: Cheat table structure
@ -62,7 +58,7 @@ extern struct cheat_s {
not_net = not_dm | not_coop
} const when;
const cheatf_t func;
const cheatarg_t arg;
const int arg;
uint64_t code, mask;
boolean deh_modified; // killough 9/12/98
} cheat[];