2020-09-02 22:29:17 +00:00
|
|
|
/*
|
|
|
|
** cheathandler.cpp
|
|
|
|
** Generic Cheat code.
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
2020-05-17 06:51:49 +00:00
|
|
|
// Copyright 1999-2016 Randy Heit
|
2020-09-02 22:29:17 +00:00
|
|
|
// Copyright 2002-2020 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OFf
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
2020-05-17 06:51:49 +00:00
|
|
|
|
|
|
|
#include "c_cvars.h"
|
|
|
|
#include "c_dispatch.h"
|
|
|
|
#include "d_event.h"
|
2020-07-04 08:22:20 +00:00
|
|
|
#include "cheathandler.h"
|
2020-07-04 13:51:02 +00:00
|
|
|
#include "printf.h"
|
|
|
|
#include "gamestruct.h"
|
2020-08-03 18:51:31 +00:00
|
|
|
#include "utf8.h"
|
2020-09-02 23:32:51 +00:00
|
|
|
#include "d_protocol.h"
|
|
|
|
#include "d_net.h"
|
2020-05-17 06:51:49 +00:00
|
|
|
|
2020-07-04 20:03:22 +00:00
|
|
|
static cheatseq_t *cheatlist;
|
2020-07-04 08:22:20 +00:00
|
|
|
static int numcheats;
|
2020-05-17 06:51:49 +00:00
|
|
|
|
2020-07-04 08:22:20 +00:00
|
|
|
void SetCheats(cheatseq_t *cht, int count)
|
2020-05-17 06:51:49 +00:00
|
|
|
{
|
2020-07-04 20:03:22 +00:00
|
|
|
cheatlist = cht;
|
2020-07-04 08:22:20 +00:00
|
|
|
numcheats = count;
|
2020-05-17 06:51:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 08:22:20 +00:00
|
|
|
CVAR(Bool, nocheats, false, CVAR_ARCHIVE)
|
2020-05-17 06:51:49 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// FUNC CheatAddkey
|
|
|
|
//
|
|
|
|
// Returns true if the added key completed the cheat, false otherwise.
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static bool CheatAddKey (cheatseq_t *cheat, uint8_t key, bool *eat)
|
|
|
|
{
|
|
|
|
if (cheat->Pos == NULL)
|
|
|
|
{
|
|
|
|
cheat->Pos = cheat->Sequence;
|
|
|
|
cheat->CurrentArg = 0;
|
|
|
|
}
|
2021-07-25 15:17:29 +00:00
|
|
|
if (*cheat->Pos == '#' && ((key >= '0' && key <= '9') || key == ' '))
|
2020-05-17 06:51:49 +00:00
|
|
|
{
|
|
|
|
*eat = true;
|
2021-07-25 09:59:12 +00:00
|
|
|
cheat->Args[cheat->CurrentArg++] = key == ' ' ? '0' : key;
|
2020-05-17 06:51:49 +00:00
|
|
|
cheat->Pos++;
|
|
|
|
}
|
2020-08-03 18:51:31 +00:00
|
|
|
else if (upperforlower[key] == upperforlower[*cheat->Pos])
|
2020-05-17 06:51:49 +00:00
|
|
|
{
|
|
|
|
cheat->Pos++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cheat->Pos = cheat->Sequence;
|
2020-07-04 08:22:20 +00:00
|
|
|
cheat->CurrentArg = 0;
|
2020-05-17 06:51:49 +00:00
|
|
|
}
|
|
|
|
if (*cheat->Pos == 0)
|
|
|
|
{
|
|
|
|
cheat->Pos = cheat->Sequence;
|
2020-07-04 08:22:20 +00:00
|
|
|
cheat->CurrentArg = 0;
|
2020-05-17 06:51:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-04 08:22:20 +00:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
|
2020-07-04 20:03:22 +00:00
|
|
|
auto cheats = cheatlist;
|
2020-07-04 08:22:20 +00:00
|
|
|
for (i = 0; i < numcheats; i++, cheats++)
|
|
|
|
{
|
|
|
|
if (CheatAddKey (cheats, (uint8_t)ev->data2, &eat))
|
|
|
|
{
|
2020-09-02 22:41:32 +00:00
|
|
|
if (cheats->DontCheck || !CheckCheatmode ())
|
2020-07-04 08:22:20 +00:00
|
|
|
{
|
2020-09-02 22:41:32 +00:00
|
|
|
if (cheats->Handler)
|
|
|
|
eat |= cheats->Handler (cheats);
|
|
|
|
else if (cheats->ccmd)
|
|
|
|
{
|
|
|
|
eat = true;
|
|
|
|
C_DoCommand(cheats->ccmd);
|
|
|
|
}
|
2020-07-04 08:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cheats->Pos - cheats->Sequence > 2)
|
|
|
|
{ // If more than two characters into the sequence,
|
2020-07-04 13:51:02 +00:00
|
|
|
// eat the keypress.
|
2020-07-04 08:22:20 +00:00
|
|
|
eat = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return eat;
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:32:51 +00:00
|
|
|
bool SendGenericCheat(cheatseq_t* cheat)
|
|
|
|
{
|
|
|
|
Net_WriteByte(DEM_GENERICCHEAT);
|
|
|
|
Net_WriteByte(cheat->Param);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-04 13:51:02 +00:00
|
|
|
void PlaybackCheat(const char *p)
|
|
|
|
{
|
2020-09-02 22:29:17 +00:00
|
|
|
if (!CheckCheatmode(true))
|
2020-07-04 13:51:02 +00:00
|
|
|
{
|
|
|
|
event_t ev = { EV_KeyDown, 0, 0, -1 };
|
|
|
|
Cheat_Responder(&ev); // Reset the parser by passing a non-existent key.
|
|
|
|
for (; *p; p++)
|
|
|
|
{
|
|
|
|
// just play the cheat command through the event parser
|
|
|
|
ev.data2 = *p;
|
|
|
|
Cheat_Responder(&ev);
|
|
|
|
}
|
|
|
|
ev.data2 = -1;
|
|
|
|
Cheat_Responder(&ev);
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 18:51:31 +00:00
|
|
|
|
|
|
|
CCMD(activatecheat)
|
|
|
|
{
|
2020-08-12 20:24:51 +00:00
|
|
|
if (argv.argc() < 2)
|
2020-08-03 18:51:31 +00:00
|
|
|
Printf("activatecheat <string>: activates a classic cheat code\n");
|
|
|
|
else
|
2020-08-12 20:24:51 +00:00
|
|
|
PlaybackCheat(argv[1]);
|
2020-08-03 18:51:31 +00:00
|
|
|
}
|