1 
2 /* WARNING:
3  *    The conversion code is for converting pl2/plr versions 242 & 142 and cs2
4  *    version 15 ONLY!!!
5  *
6  *    Those versions are the only ones that allow for the conversion process to
7  *    run without being directly tied to the active game data.  Attempting
8  *    conversion on older versions will almost certainly result in data
9  *    corruption in the converted file!
10  */
11 
12 /* NOTE:
13  *    Lots of duplicate info here for the old pilot file format.  This is done
14  *    so that any future engine changes won't break the conversion process
15  *    or worrying about whether said changes would break anything.  It also
16  *    allows for better conversion sanity checks, and allows for the conversion
17  *    process to run totally independently of current game data.
18  */
19 
20 #define REDALERT_INTERNAL
21 
22 #include "globalincs/pstypes.h"
23 #include "cfile/cfile.h"
24 #include "stats/scoring.h"
25 #include "parse/sexp.h"
26 #include "mission/missioncampaign.h"
27 #include "controlconfig/controlsconfig.h"
28 #include "missionui/redalert.h"
29 #include "pilotfile/pilotfile.h"
30 
31 
32 static const unsigned short MAX_JOY_AXES_CONV = 5;
33 static const int MAX_WSS_SLOTS_CONV = 12;   // 3 wings * 4 slots
34 static const int MAX_SHIP_WEAPONS_CONV = 7; // 3 primary + 4 secondary
35 
36 typedef struct index_list_t {
37 	SCP_string name;
38 	int index;
39 	int val;
40 
index_list_tindex_list_t41 	index_list_t() :
42 		index(-1), val(0)
43 	{
44 	}
45 } index_list_t;
46 
47 // special stats struct, since our use here is not content specific
48 typedef struct scoring_special_t {
49 	int score;
50 	int rank;
51 	int assists;
52 	int kill_count;
53 	int kill_count_ok;
54 	int bonehead_kills;
55 
56 	unsigned int p_shots_fired;
57 	unsigned int p_shots_hit;
58 	unsigned int p_bonehead_hits;
59 
60 	unsigned int s_shots_fired;
61 	unsigned int s_shots_hit;
62 	unsigned int s_bonehead_hits;
63 
64 	unsigned int missions_flown;
65 	unsigned int flight_time;
66 	_fs_time_t last_flown;
67 	_fs_time_t last_backup;
68 
69 	SCP_vector<index_list_t> ship_kills;
70 	SCP_vector<index_list_t> medals_earned;
71 
scoring_special_tscoring_special_t72 	scoring_special_t() :
73 		score(0), rank(RANK_ENSIGN), assists(0), kill_count(0), kill_count_ok(0),
74 		bonehead_kills(0), p_shots_fired(0), p_shots_hit(0), p_bonehead_hits(0),
75 		s_shots_fired(0), s_shots_hit(0), s_bonehead_hits(0), missions_flown(0),
76 		flight_time(0), last_flown(0), last_backup(0)
77 	{
78 	}
79 } scoring_special_t;
80 
81 typedef struct cmission_conv_t {
82 	int index;			// index into Campaign.missions[]
83 	int flags;
84 
85 	SCP_vector<mgoal>	goals;
86 	SCP_vector<mevent>	events;
87 	SCP_vector<sexp_variable>	variables;
88 
89 	scoring_special_t	stats;
90 } cmission_conv_t;
91 
92 typedef struct wss_unit_conv_t {
93 	int ship_index;
94 	int wep[MAX_SHIP_WEAPONS_CONV];
95 	int wep_count[MAX_SHIP_WEAPONS_CONV];
96 
wss_unit_conv_twss_unit_conv_t97 	wss_unit_conv_t() :
98 		ship_index(-1)
99 	{
100 	}
101 } wss_unit_conv_t;
102 
103 typedef struct loadout_conv_t {
104 	SCP_string filename;
105 	SCP_string last_modified;
106 
107 	wss_unit_conv_t slot[MAX_WSS_SLOTS_CONV];
108 
109 	SCP_vector<int> weapon_pool;
110 	SCP_vector<int> ship_pool;
111 } loadout_conv_t;
112 
113 
114 struct plr_data {
115 	plr_data();
116 	~plr_data();
117 
118 	// -------------------------------------------------------------------------
119 	// NOTE: we *do not* carry over the following items because they are either
120 	//       obsolete, removed functionality, or just not needed.  But we still
121 	//       do have to take them into account during read ...
122 	//
123 	//     - stats (ver 142 only)
124 	//     - loadout (ver 142 only)
125     //     - techroom (ver 142 only)
126     //     - recent missions (ver 142 & 242)
127 	//     - red alert (ver 142 only)
128 	// -------------------------------------------------------------------------
129 
130 	// not carried over, just for reference during conversion process
131 	int version;
132 
133 	// basic flags and settings
134 	int is_multi;
135 	int tips;
136 	int rank;
137 	int skill_level;
138 	int save_flags;
139 	int readyroom_listing_mode;
140 	int voice_enabled;
141 	int auto_advance;
142 	int Use_mouse_to_fly;
143 	int Mouse_sensitivity;
144 	int Joy_sensitivity;
145 	int Dead_zone_size;
146 
147 	// multiplayer settings/options
148 	int net_protocol;
149 
150 	unsigned char multi_squad_set;
151 	unsigned char multi_endgame_set;
152 	int multi_flags;
153 	unsigned int multi_respawn;
154 	unsigned char multi_max_observers;
155 	unsigned char multi_skill_level;
156 	unsigned char multi_voice_qos;
157 	int multi_voice_token_wait;
158 	int multi_voice_record_time;
159 	int multi_time_limit;
160 	int multi_kill_limit;
161 
162 	int multi_local_flags;
163 	int multi_local_update_level;
164 
165 	// pilot info stuff
166 	char image_filename[35];
167 	char squad_name[35];
168 	char squad_filename[35];
169 	char current_campaign[35];
170 	char last_ship_flown[35];
171 
172 	// HUD config
173 	int hud_show_flags;
174 	int hud_show_flags2;
175 	int hud_popup_flags;
176 	int hud_popup_flags2;
177 	unsigned char hud_num_lines;
178 	int hud_rp_flags;
179 	int hud_rp_dist;
180 	unsigned char hud_colors[39][4];
181 
182 	// control setup
183 	SCP_vector<config_item> controls;
184 
185 	int joy_axis_map_to[5];
186 	int joy_invert_axis[5];
187 
188 	// audio
189 	float sound_volume;
190 	float music_volume;
191 	float voice_volume;
192 
193 	// detail settings
194 	int detail_setting;
195 	int detail_nebula;
196 	int detail_distance;
197 	int detail_hardware_textures;
198 	int detail_num_debris;
199 	int detail_num_particles;
200 	int detail_num_stars;
201 	int detail_shield_effects;
202 	int detail_lighting;
203 	int detail_targetview_model;
204 	int detail_planets_suns;
205 	int detail_weapon_extras;
206 
207 	// variables
208 	SCP_vector<sexp_variable> variables;
209 };
210 
211 struct csg_data {
212 	csg_data();
213 	~csg_data();
214 
215 	int sig;
216 	int cutscenes;
217 
218 	SCP_string main_hall;
219 	int prev_mission;
220 	int next_mission;
221 	int loop_reentry;
222 	int loop_enabled;
223 	int num_completed;
224 
225 	int last_ship_flown_index;
226 
227 	SCP_vector<bool> ships_allowed;
228 	SCP_vector<bool> weapons_allowed;
229 
230 	SCP_vector<bool> ships_techroom;
231 	SCP_vector<bool> weapons_techroom;
232 	SCP_vector<bool> intel_techroom;
233 
234 	SCP_vector<index_list_t> ship_list;
235 	SCP_vector<index_list_t> weapon_list;
236 	SCP_vector<index_list_t> intel_list;
237 	SCP_vector<index_list_t> medals_list;
238 
239 	SCP_vector<cmission_conv_t> missions;
240 
241 	SCP_vector<sexp_variable> variables;
242 
243 	loadout_conv_t loadout;
244 
245 	scoring_special_t stats;
246 
247 	SCP_vector<red_alert_ship_status> wingman_status;
248 	SCP_string precursor_mission;
249 };
250 
251 class pilotfile_convert {
252 	public:
253 		pilotfile_convert();
254 		~pilotfile_convert();
255 
256 		bool load();
257 		void save();
258 
259 		bool plr_convert(const char *fname, bool inferno);
260 		bool csg_convert(const char *fname, bool inferno);
261 
262 	private:
263 		CFILE *cfp;
264 		unsigned int fver;
265 
266 		scoring_special_t all_time_stats;
267 		scoring_special_t multi_stats;
268 
269 		// sections of a pilot file. includes both plr and csg sections
270 		struct Section {
271 			enum id {
272 				Flags			= 0x0001,
273 				Info			= 0x0002,
274 				Loadout			= 0x0003,
275 				Controls		= 0x0004,
276 				Multiplayer		= 0x0005,
277 				Scoring			= 0x0006,
278 				ScoringMulti	= 0x0007,
279 				Techroom		= 0x0008,
280 				HUD				= 0x0009,
281 				Settings		= 0x0010,
282 				RedAlert		= 0x0011,
283 				Variables		= 0x0012,
284 				Missions		= 0x0013
285 			};
286 		};
287 
288 		// for writing files, sets/updates section info
289 		void startSection(Section::id section_id);
290 		void endSection();
291 		// file offset of the size value for the current section (set with startSection())
292 		size_t m_size_offset;
293 
294 
295 		// --------------------------------------------------------------------
296 		// PLR specific
297 		// --------------------------------------------------------------------
298 		plr_data *plr;
299 
300 		void plr_import();
301 		void plr_import_controls();
302 		void plr_import_hud();
303 		void plr_import_detail();
304 		void plr_import_stats();
305 		void plr_import_loadout();
306 		void plr_import_multiplayer();
307 		void plr_import_red_alert();
308 		void plr_import_variables();
309 
310 		void plr_export();
311 		void plr_export_flags();
312 		void plr_export_info();
313 		void plr_export_stats();
314 		void plr_export_stats_multi();
315 		void plr_export_hud();
316 		void plr_export_variables();
317 		void plr_export_multiplayer();
318 		void plr_export_controls();
319 		void plr_export_settings();
320 
321 		// --------------------------------------------------------------------
322 		// CSG specific
323 		// --------------------------------------------------------------------
324 		csg_data *csg;
325 
326 		void csg_import(bool inferno);
327 		void csg_import_loadout();
328 		void csg_import_stats();
329 		void csg_import_techroom();
330 		void csg_import_red_alert();
331 		void csg_import_missions(bool inferno);
332 		void csg_import_ships_weapons();
333 
334 		void csg_export();
335 		void csg_export_flags();
336 		void csg_export_info();
337 		void csg_export_missions();
338 		void csg_export_techroom();
339 		void csg_export_loadout();
340 		void csg_export_stats();
341 		void csg_export_redalert();
342 		void csg_export_hud();
343 		void csg_export_variables();
344 };
345 
346