Add -nocheat command line parameter to disable applying cheats from dehacked files.
[chocolate-doom.git] / src / deh_cheat.c
blob89e5d21963afd26f19337fee4a697f606b9ad37c
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 2005 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
21 //-----------------------------------------------------------------------------
23 // Parses "Cheat" sections in dehacked files
25 //-----------------------------------------------------------------------------
27 #include "doomdef.h"
28 #include "doomtype.h"
30 #include "deh_defs.h"
31 #include "deh_io.h"
32 #include "deh_main.h"
33 #include "am_map.h"
34 #include "st_stuff.h"
36 typedef struct
38 char *name;
39 cheatseq_t *seq;
40 } deh_cheat_t;
42 static deh_cheat_t allcheats[] =
44 {"Change music", &cheat_mus },
45 {"Chainsaw", &cheat_choppers },
46 {"God mode", &cheat_god },
47 {"Ammo & Keys", &cheat_ammo },
48 {"Ammo", &cheat_ammonokey },
49 {"No Clipping 1", &cheat_noclip },
50 {"No Clipping 2", &cheat_commercial_noclip },
51 {"Invincibility", &cheat_powerup[0] },
52 {"Berserk", &cheat_powerup[1] },
53 {"Invisibility", &cheat_powerup[2] },
54 {"Radiation Suit", &cheat_powerup[3] },
55 {"Auto-map", &cheat_powerup[4] },
56 {"Lite-Amp Goggles", &cheat_powerup[5] },
57 {"BEHOLD menu", &cheat_powerup[6] },
58 {"Level Warp", &cheat_clev },
59 {"Player Position", &cheat_mypos },
60 {"Map cheat", &cheat_amap },
63 static deh_cheat_t *FindCheatByName(char *name)
65 size_t i;
67 for (i=0; i<arrlen(allcheats); ++i)
69 if (!strcasecmp(allcheats[i].name, name))
70 return &allcheats[i];
73 return NULL;
76 static void *DEH_CheatStart(deh_context_t *context, char *line)
78 return NULL;
81 static void DEH_CheatParseLine(deh_context_t *context, char *line, void *tag)
83 deh_cheat_t *cheat;
84 char *variable_name;
85 char *value;
86 unsigned char *unsvalue;
87 unsigned int i;
89 if (!DEH_ParseAssignment(line, &variable_name, &value))
91 // Failed to parse
93 DEH_Warning(context, "Failed to parse assignment");
94 return;
97 unsvalue = (unsigned char *) value;
99 cheat = FindCheatByName(variable_name);
101 if (cheat == NULL)
103 DEH_Warning(context, "Unknown cheat '%s'", variable_name);
104 return;
107 // write the value into the cheat sequence
109 i = 0;
111 while (unsvalue[i] != 0 && unsvalue[i] != 0xff)
113 // If the cheat length exceeds the Vanilla limit, stop. This
114 // does not apply if we have the limit turned off.
116 if (!deh_allow_long_cheats && i >= cheat->seq->sequence_len)
118 DEH_Warning(context, "Cheat sequence longer than supported by "
119 "Vanilla dehacked");
120 break;
123 if (deh_apply_cheats)
125 cheat->seq->sequence[i] = unsvalue[i];
127 ++i;
129 // Absolute limit - don't exceed
131 if (i >= MAX_CHEAT_LEN - cheat->seq->parameter_chars)
133 DEH_Error(context, "Cheat sequence too long!");
134 return;
138 if (deh_apply_cheats)
140 cheat->seq->sequence[i] = '\0';
144 deh_section_t deh_section_cheat =
146 "Cheat",
147 NULL,
148 DEH_CheatStart,
149 DEH_CheatParseLine,
150 NULL,
151 NULL,