File: cheats_table.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (186 lines) | stat: -rw-r--r-- 5,641 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "cheats_table.h"
#include "parse/parselo.h"

void cheat_table_init() {
	customCheats.clear();

	if (cf_exists_full("cheats.tbl", CF_TYPE_TABLES)) {
		parse_cheat_table("cheats.tbl");
	}
	parse_modular_table("*-cht.tbm", parse_cheat_table);
}

bool CustomCheat::canUseCheat() {
	return !requireCheatsEnabled || Cheats_enabled;
}

void CustomCheat::runCheat() {
	if (!canUseCheat()) return;

	//Only try to send the message if we actually have one to send and are in a mission
	if (!cheatMsg.empty() && (Game_mode & GM_IN_MISSION))
		HUD_printf("%s", cheatMsg.c_str());

	scripting::hooks::OnCheat->run(scripting::hook_param_list(scripting::hook_param("Cheat", 's', cheatCode)));
	CheatUsed = cheatCode;
}

void SpawnShipCheat::runCheat() {
		CustomCheat::runCheat();

		if (canUseCheat() && (Game_mode & GM_IN_MISSION) && Player_obj != nullptr) {
			extern void prevent_spawning_collision(object *new_obj);
			ship_subsys *ptr;
			char name[NAME_LENGTH];
			int ship_idx, ship_class; 

			// if not found, then don't create it :(
			ship_class = ship_info_lookup(shipClassName.c_str());
			if (ship_class < 0)
				return;

			vec3d pos = Player_obj->pos;
			matrix orient = Player_obj->orient;
			pos.xyz.x += frand_range(-700.0f, 700.0f);
			pos.xyz.y += frand_range(-700.0f, 700.0f);
			pos.xyz.z += frand_range(-700.0f, 700.0f);

			int objnum = ship_create(&orient, &pos, ship_class);
			if (objnum < 0)
				return;
			
			ship *shipp = &Ships[Objects[objnum].instance];
			shipp->ship_name[0] = '\0';
			shipp->display_name.clear();
			for (size_t j = 0; j < Player_orders.size(); j++)
				shipp->orders_accepted.insert(j);

			// Goober5000 - stolen from support ship creation
			// create a name for the ship with a number. look for collisions until one isn't found anymore
			ship_idx = 1;
			do {
				sprintf(name, "%s %d", shipName.c_str(), ship_idx);
				if ( (ship_name_lookup(name) == -1) && (ship_find_exited_ship_by_name(name) == -1) )
				{
					strcpy_s(shipp->ship_name, name);
					break;
				}

				ship_idx++;
			} while(1);

			shipp->flags.set(Ship::Ship_Flags::Escort);
			shipp->escort_priority = 1000 - ship_idx;

			// now make sure we're not colliding with anyone
			prevent_spawning_collision(&Objects[objnum]);

			// Goober5000 - beam free
			for (ptr = GET_FIRST(&shipp->subsys_list); ptr != END_OF_LIST(&shipp->subsys_list); ptr = GET_NEXT(ptr))
			{
				// mark all turrets as beam free
				if (ptr->system_info->type == SUBSYSTEM_TURRET)
				{
					ptr->weapons.flags.set(Ship::Weapon_Flags::Beam_Free);
					ptr->turret_next_fire_stamp = timestamp(Random::next(50, 4000));
				}
			}

			// Cyborg17 to prevent a nullptr...
			ship_set_warp_effects(&Objects[objnum]);
			// warpin
			shipfx_warpin_start(&Objects[objnum]);
		}
}

void parse_cheat_table(const char* filename) {
	try {
		read_file_text(filename, CF_TYPE_TABLES);
		reset_parse();
		optional_string("#CUSTOM CHEATS");

		while (optional_string("$Cheat:")) {
			SCP_string code, msg, shipClass, shipName;
			bool requireCheats = false;
			bool shipSpawn = false;
			required_string("+Code:");
			stuff_string(code, F_RAW);

			//We are limited to the buffer size used for the traditional cheats. We can still use short cheats, though.
			if (code.length() > CHEAT_BUFFER_LEN) {
				Warning(LOCATION, "Cheat code %s is too long. It will be cut off to maximum length (%i characters).", code.c_str(), CHEAT_BUFFER_LEN);
				code = code.substr(0, CHEAT_BUFFER_LEN);
			}

			if (optional_string("+Message:")) {
				stuff_string(msg, F_MESSAGE);
			}

			if (optional_string("+RequireCheats:")) {
				stuff_boolean(&requireCheats);
			}

			if (optional_string("+SpawnShip:")) {
				shipSpawn = true;
				required_string("+Name:");
				stuff_string(shipName, F_NAME);
				required_string("+Class:");
				stuff_string(shipClass, F_NAME);

				int shipClassInfo = ship_info_lookup(shipClass.c_str());
				if (shipClassInfo < 0) {
					shipSpawn = false;
					Warning(LOCATION, "There is no ship class named '%s'. The '%s' cheat won't summon a ship.", shipClass.c_str(), code.c_str());
				}
			}
			
			if (shipSpawn) {
				std::unique_ptr<CustomCheat> shipCheat(new SpawnShipCheat(code, msg, requireCheats, shipClass, shipName));

				if(customCheats.count(code) == 1) {
					Warning(LOCATION, "A cheat for code '%s' already exists. It will be replaced.", code.c_str());
					customCheats[code] = std::move(shipCheat);
				} else {
					customCheats.emplace(code, std::move(shipCheat));
				}
			} else {
				std::unique_ptr<CustomCheat> cheat(new CustomCheat(code, msg, requireCheats));

				if(customCheats.count(code) == 1) {
					Warning(LOCATION, "A cheat for code '%s' already exists. It will be replaced.", code.c_str());
					customCheats[code] = std::move(cheat);
				} else {
					customCheats.emplace(code, std::move(cheat));
				}
			}
		}
		required_string("#END");
	}
	catch (const parse::ParseException& e) {
		mprintf(("TABLES: Unable to parse '%s'!  Error message = %s.\n", filename, e.what()));
		return;
	}
}

bool checkForCustomCheats(const char* buffer, int buffer_length) {
	const char* check_buffer = buffer;
	for (int i = 0; i < buffer_length; i++) {
		if (*check_buffer == '\0') {
			check_buffer++;
		}
		else {
			break;
		}
	}

	// Loop through our map of custom cheats and check if the code is within the buffer.
	// We don't just check for equality, as the player may have typed other characters before the cheat.
	for (auto &ccheat : customCheats) {
		auto found = std::strstr(check_buffer, ccheat.first.c_str());
		if (found != nullptr) {
			ccheat.second->runCheat();
			return true;
		}
	}
	return false;
}