- simplified the generic cheat code.

This is both closer to ZDoom and more robust.
This commit is contained in:
Christoph Oelckers 2020-07-04 10:22:20 +02:00
parent c9d3a383a4
commit dbd3202433
3 changed files with 70 additions and 81 deletions

View file

@ -783,10 +783,10 @@ set (PCH_SOURCES
build/src/timer.cpp
build/src/voxmodel.cpp
core/cheathandler.cpp
core/mathutil.cpp
core/rts.cpp
core/ct_chat.cpp
core/entercheat.cpp
core/gameconfigfile.cpp
core/gamecvars.cpp
core/gamecontrol.cpp

View file

@ -1,9 +1,7 @@
//-----------------------------------------------------------------------------
//
// Copyright 1993-1996 id Software
// Copyright 1994-1996 Raven Software
// Copyright 1999-2016 Randy Heit
// Copyright 2002-2020 Christoph Oelckers
// Copyright 2002-2016 Christoph Oelckers
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@ -21,90 +19,26 @@
//-----------------------------------------------------------------------------
//
// DESCRIPTION:
// Cheat code. See *_sbar.cpp for status bars.
// Generic Cheat code.
//
//-----------------------------------------------------------------------------
#include "gstrings.h"
#include "c_cvars.h"
#include "c_dispatch.h"
#include "d_event.h"
#include "baselayer.h"
#include "gamecontrol.h"
#include "cheathandler.h"
struct cheatseq_t
static cheatseq_t *cheats;
static int numcheats;
void SetCheats(cheatseq_t *cht, int count)
{
const uint8_t *Sequence;
const uint8_t *Pos;
uint8_t CurrentArg;
uint8_t Args[5];
};
cheats = cht;
numcheats = count;
}
static TArray<cheatseq_t> cheats;
static bool CheatCheckList (event_t *ev);
static bool CheatAddKey (cheatseq_t *cheat, uint8_t key, bool *eat);
CVAR(Bool, allcheats, false, CVAR_ARCHIVE)
CVAR(Bool, nocheats, false, CVAR_ARCHIVE)
// Respond to keyboard input events, intercept cheats.
// [RH] Cheats eat the last keypress used to trigger them
bool Cheat_Responder (event_t *ev)
{
bool eat = false;
if (cheats.Size() == 0)
{
#if 0
auto gcheats = gi->GetCheats();
if (gcheats)
{
for (int i = 0; gcheats[i]; i++)
{
cheatseq_t cht = { (const uint8_t*)gcheats[i], nullptr };
cheats.Push(cht);
}
}
#endif
}
if (nocheats)
{
return false;
}
else
{
return CheatCheckList(ev);
}
return false;
}
static bool CheatCheckList (event_t *ev)
{
bool eat = false;
if (ev->type == EV_KeyDown)
{
for (auto &cht :cheats)
{
if (CheatAddKey (&cht, (uint8_t)ev->data2, &eat))
{
int processed = gi->CheckCheat((const char*)cht.Sequence, (const char*)cht.Args);
if (processed = 1) cht.Pos = nullptr;
eat |= processed != 0;
}
else if (cht.Pos - cht.Sequence > 2)
{ // If more than two characters into the sequence,
// eat the keypress, to reduce interference with game controls.
eat = true;
}
}
}
return eat;
}
//--------------------------------------------------------------------------
//
// FUNC CheatAddkey
@ -119,15 +53,12 @@ static bool CheatAddKey (cheatseq_t *cheat, uint8_t key, bool *eat)
{
cheat->Pos = cheat->Sequence;
cheat->CurrentArg = 0;
cheat->Args[0] = 0;
}
if (*cheat->Pos == '#' && key >= '0' && key <= '9')
if (*cheat->Pos == '#')
{
*eat = true;
cheat->Args[cheat->CurrentArg++] = key;
cheat->Args[cheat->CurrentArg] = 0;
cheat->Pos++;
return true;
}
else if (key == *cheat->Pos)
{
@ -136,12 +67,53 @@ static bool CheatAddKey (cheatseq_t *cheat, uint8_t key, bool *eat)
else
{
cheat->Pos = cheat->Sequence;
cheat->CurrentArg = 0;
}
if (*cheat->Pos == 0)
{
cheat->Pos = cheat->Sequence;
cheat->CurrentArg = 0;
return true;
}
return false;
}
//--------------------------------------------------------------------------
//
// Respond to keyboard input events, intercept cheats.
// [RH] Cheats eat the last keypress used to trigger them
//
//--------------------------------------------------------------------------
bool Cheat_Responder (event_t *ev)
{
bool eat = false;
if (nocheats)
{
return false;
}
if (ev->type == EV_KeyDown)
{
int i;
for (i = 0; i < numcheats; i++, cheats++)
{
if (CheatAddKey (cheats, (uint8_t)ev->data2, &eat))
{
if (cheats->DontCheck || !CheckCheatmode ())
{
eat |= cheats->Handler (cheats);
}
}
else if (cheats->Pos - cheats->Sequence > 2)
{ // If more than two characters into the sequence,
// eat the keypress, just so that the Hexen cheats
// with T in them will work without unbinding T.
eat = true;
}
}
}
return eat;
}

View file

@ -0,0 +1,17 @@
#pragma once
struct cheatseq_t
{
const char *Sequence;
const char *Pos;
bool (*Handler)(cheatseq_t *);
uint8_t DontCheck;
// This is working data for processing the cheat
uint8_t CurrentArg;
uint8_t Args[6];
};
struct event_t;
bool Cheat_Responder(event_t* ev);
void SetCheats(cheatseq_t *cht, int count);