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 "cfile/cfile.h"
23 #include "controlconfig/controlsconfig.h"
24 #include "globalincs/pstypes.h"
25 #include "localization/localize.h"
26 #include "mission/missioncampaign.h"
27 #include "missionui/redalert.h"
28 #include "parse/sexp.h"
29 #include "pilotfile/pilotfile.h"
30 #include "stats/scoring.h"
31 
32 
33 static const unsigned short MAX_JOY_AXES_CONV = 5;
34 static const int MAX_WSS_SLOTS_CONV = 12;   // 3 wings * 4 slots
35 static const int MAX_SHIP_WEAPONS_CONV = 7; // 3 primary + 4 secondary
36 
37 typedef struct cmission_conv_t {
38 	int index;			// index into Campaign.missions[]
39 	int flags;
40 
41 	SCP_vector<mgoal>	goals;
42 	SCP_vector<mevent>	events;
43 	SCP_vector<sexp_variable>	variables;
44 
45 	scoring_special_t	stats;
46 } cmission_conv_t;
47 
48 typedef struct wss_unit_conv_t {
49 	int ship_index;
50 	int wep[MAX_SHIP_WEAPONS_CONV];
51 	int wep_count[MAX_SHIP_WEAPONS_CONV];
52 
wss_unit_conv_twss_unit_conv_t53 	wss_unit_conv_t() :
54 		ship_index(-1)
55 	{
56 	}
57 } wss_unit_conv_t;
58 
59 typedef struct loadout_conv_t {
60 	SCP_string filename;
61 	SCP_string last_modified;
62 
63 	wss_unit_conv_t slot[MAX_WSS_SLOTS_CONV];
64 
65 	SCP_vector<int> weapon_pool;
66 	SCP_vector<int> ship_pool;
67 } loadout_conv_t;
68 
69 
70 struct plr_data {
71 	plr_data();
72 	~plr_data();
73 
74 	// -------------------------------------------------------------------------
75 	// NOTE: we *do not* carry over the following items because they are either
76 	//       obsolete, removed functionality, or just not needed.  But we still
77 	//       do have to take them into account during read ...
78 	//
79 	//     - stats (ver 142 only)
80 	//     - loadout (ver 142 only)
81     //     - techroom (ver 142 only)
82     //     - recent missions (ver 142 & 242)
83 	//     - red alert (ver 142 only)
84 	// -------------------------------------------------------------------------
85 
86 	// not carried over, just for reference during conversion process
87 	int version;
88 
89 	// basic flags and settings
90 	int is_multi;
91 	char language[LCL_LANG_NAME_LEN+1];
92 	int tips;
93 	int rank;
94 	int skill_level;
95 	int save_flags;
96 	int readyroom_listing_mode;
97 	int voice_enabled;
98 	int auto_advance;
99 	int Use_mouse_to_fly;
100 	int Mouse_sensitivity;
101 	int Joy_sensitivity;
102 	int Dead_zone_size;
103 
104 	// multiplayer settings/options
105 	int net_protocol;
106 
107 	unsigned char multi_squad_set;
108 	unsigned char multi_endgame_set;
109 	int multi_flags;
110 	unsigned int multi_respawn;
111 	unsigned char multi_max_observers;
112 	unsigned char multi_skill_level;
113 	unsigned char multi_voice_qos;
114 	int multi_voice_token_wait;
115 	int multi_voice_record_time;
116 	int multi_time_limit;
117 	int multi_kill_limit;
118 
119 	int multi_local_flags;
120 	int multi_local_update_level;
121 
122 	// pilot info stuff
123 	char image_filename[35];
124 	char squad_name[35];
125 	char squad_filename[35];
126 	char current_campaign[35];
127 	char last_ship_flown[35];
128 
129 	// HUD config
130 	int hud_show_flags;
131 	int hud_show_flags2;
132 	int hud_popup_flags;
133 	int hud_popup_flags2;
134 	unsigned char hud_num_lines;
135 	int hud_rp_flags;
136 	int hud_rp_dist;
137 	unsigned char hud_colors[39][4];
138 
139 	// control setup
140 	SCP_vector<CCI> controls;
141 
142 	int joy_axis_map_to[5];
143 	int joy_invert_axis[5];
144 
145 	// audio
146 	float sound_volume;
147 	float music_volume;
148 	float voice_volume;
149 
150 	// detail settings
151 	int detail_setting;
152 	int detail_nebula;
153 	int detail_distance;
154 	int detail_hardware_textures;
155 	int detail_num_debris;
156 	int detail_num_particles;
157 	int detail_num_stars;
158 	int detail_shield_effects;
159 	int detail_lighting;
160 	int detail_targetview_model;
161 	int detail_planets_suns;
162 	int detail_weapon_extras;
163 
164 	// variables
165 	SCP_vector<sexp_variable> variables;
166 };
167 
168 struct csg_data {
169 	csg_data();
170 	~csg_data();
171 
172 	int sig;
173 	int cutscenes;
174 
175 	SCP_string main_hall;
176 	int prev_mission;
177 	int next_mission;
178 	int loop_reentry;
179 	int loop_enabled;
180 	int num_completed;
181 
182 	int last_ship_flown_index;
183 
184 	SCP_vector<bool> ships_allowed;
185 	SCP_vector<bool> weapons_allowed;
186 
187 	SCP_vector<bool> ships_techroom;
188 	SCP_vector<bool> weapons_techroom;
189 	SCP_vector<bool> intel_techroom;
190 
191 	SCP_vector<index_list_t> ship_list;
192 	SCP_vector<index_list_t> weapon_list;
193 	SCP_vector<index_list_t> intel_list;
194 	SCP_vector<index_list_t> medals_list;
195 
196 	SCP_vector<cmission_conv_t> missions;
197 
198 	SCP_vector<sexp_variable> variables;
199 
200 	loadout_conv_t loadout;
201 
202 	scoring_special_t stats;
203 
204 	SCP_vector<red_alert_ship_status> wingman_status;
205 	SCP_string precursor_mission;
206 };
207 
208 class pilotfile_convert {
209 	public:
210 		pilotfile_convert();
211 		~pilotfile_convert();
212 
213 		bool load();
214 		void save();
215 
216 		bool plr_convert(const char *fname, bool inferno);
217 		bool csg_convert(const char *fname, bool inferno);
218 
219 	private:
220 		CFILE *cfp;
221 		unsigned int fver;
222 
223 		scoring_special_t all_time_stats;
224 		scoring_special_t multi_stats;
225 
226 		// sections of a pilot file. includes both plr and csg sections
227 		enum class Section {
228 			Flags			= 0x0001,
229 			Info			= 0x0002,
230 			Loadout			= 0x0003,
231 			Controls		= 0x0004,
232 			Multiplayer		= 0x0005,
233 			Scoring			= 0x0006,
234 			ScoringMulti	= 0x0007,
235 			Techroom		= 0x0008,
236 			HUD				= 0x0009,
237 			Settings		= 0x0010,
238 			RedAlert		= 0x0011,
239 			Variables		= 0x0012,
240 			Missions		= 0x0013,
241 			Cutscenes		= 0x0014,
242 		};
243 
244 		// for writing files, sets/updates section info
245 		void startSection(Section section_id);
246 		void endSection();
247 		// file offset of the size value for the current section (set with startSection())
248 		size_t m_size_offset;
249 
250 
251 		// --------------------------------------------------------------------
252 		// PLR specific
253 		// --------------------------------------------------------------------
254 		plr_data *plr;
255 
256 		void plr_import();
257 
258 		/**
259 		 * @brief Reads in the player controls from the playerfile.  Assumes controls are written in the order they are hardcoded.
260 		 */
261 		void plr_import_controls();
262 		void plr_import_hud();
263 		void plr_import_detail();
264 		void plr_import_stats();
265 		void plr_import_loadout();
266 		void plr_import_multiplayer();
267 		void plr_import_red_alert();
268 		void plr_import_variables();
269 
270 		void plr_export();
271 		void plr_export_flags();
272 		void plr_export_info();
273 		void plr_export_stats();
274 		void plr_export_stats_multi();
275 		void plr_export_hud();
276 		void plr_export_variables();
277 		void plr_export_multiplayer();
278 		void plr_export_controls();
279 		void plr_export_settings();
280 
281 		// --------------------------------------------------------------------
282 		// CSG specific
283 		// --------------------------------------------------------------------
284 		csg_data *csg;
285 
286 		void csg_import(bool inferno);
287 		void csg_import_loadout();
288 		void csg_import_stats();
289 		void csg_import_techroom();
290 		void csg_import_red_alert();
291 		void csg_import_missions(bool inferno);
292 		void csg_import_ships_weapons();
293 
294 		void csg_export();
295 		void csg_export_flags();
296 		void csg_export_info();
297 		void csg_export_missions();
298 		void csg_export_techroom();
299 		void csg_export_loadout();
300 		void csg_export_stats();
301 		void csg_export_redalert();
302 		void csg_export_hud();
303 		void csg_export_variables();
304 		void csg_export_cutscenes();
305 };
306 
307