File: AltShipClassDlg.cpp

package info (click to toggle)
freespace2 3.7.4%2Brepack-1.1
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye
  • size: 22,268 kB
  • sloc: cpp: 393,535; ansic: 4,106; makefile: 1,091; xml: 181; sh: 137
file content (406 lines) | stat: -rw-r--r-- 10,664 bytes parent folder | download | duplicates (2)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Created by Hassan "Karajorma" Kazmi for the FreeSpace2 Source Code Project.
 * You may not sell or otherwise commercially exploit the source or things you
 * create based on the source.
 */

// AltShipClassDlg.cpp : implementation file
//

#include "stdafx.h"
#include "fred.h"
#include "AltShipClassDlg.h"
#include "globalincs/linklist.h"
#include "ship/ship.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// AltShipClassDlg dialog


AltShipClassDlg::AltShipClassDlg(CWnd* pParent /*=NULL*/)
	: CDialog(AltShipClassDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(AltShipClassDlg)
	m_selected_variable = -1;
	m_selected_class = -1;
	//}}AFX_DATA_INIT

	multi_edit = false;
	player_ships_only = true;
	num_string_variables = 0;

	memset(string_variable_indices, -1, sizeof (int)*MAX_SEXP_VARIABLES); 
	memset(ship_class_indices, -1, sizeof (int)*MAX_SHIP_CLASSES);
}


void AltShipClassDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(AltShipClassDlg)
	DDX_Control(pDX, IDC_DEFAULT_TO_CLASS, m_default_to_class);
	DDX_Control(pDX, IDC_ALT_CLASS_LIST, m_alt_class_list);
	DDX_Control(pDX, IDC_SET_FROM_SHIP_CLASS, m_set_from_ship_class);
	DDX_Control(pDX, IDC_SET_FROM_VARIABLES, m_set_from_variables);
	DDX_CBIndex(pDX, IDC_SET_FROM_VARIABLES, m_selected_variable);
	DDX_CBIndex(pDX, IDC_SET_FROM_SHIP_CLASS, m_selected_class);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(AltShipClassDlg, CDialog)
	//{{AFX_MSG_MAP(AltShipClassDlg)
	ON_BN_CLICKED(IDC_ALT_CLASS_ADD, OnAltClassAdd)
	ON_BN_CLICKED(IDC_ALT_CLASS_INSERT, OnAltClassInsert)
	ON_BN_CLICKED(IDC_ALT_CLASS_DELETE, OnAltClassDelete)
	ON_BN_CLICKED(IDC_ALT_CLASS_UP, OnMoveUp)
	ON_BN_CLICKED(IDC_ALT_CLASS_DOWN, OnMoveDown)
	ON_CBN_SELENDOK(IDC_SET_FROM_SHIP_CLASS, OnSelendokSetFromShipClass)
	ON_CBN_SELENDOK(IDC_SET_FROM_VARIABLES, OnSelendokSetFromVariables)
	ON_LBN_SELCHANGE(IDC_ALT_CLASS_LIST, OnSelchangeAltClassList)
	ON_BN_CLICKED(IDC_DEFAULT_TO_CLASS, OnDefaultToClass)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// AltShipClassDlg message handlers


BOOL AltShipClassDlg::OnInitDialog() 
{
	int i, count;
	char buff[TOKEN_LENGTH + TOKEN_LENGTH + 2];  // VariableName[VariableValue]

	CDialog::OnInitDialog();

	// have we got multiple selected ships?
	num_selected_ships = 0;
	object *objp;

	objp = GET_FIRST(&obj_used_list);
	while (objp != END_OF_LIST(&obj_used_list)) {
		if ((objp->type == OBJ_START) || (objp->type == OBJ_SHIP)) {
			if (objp->flags & OF_MARKED) {
				m_selected_ships[num_selected_ships++] = objp->instance;
			}
		}
		objp = GET_NEXT(objp);
	}

	Assert (num_selected_ships > 0);
	Assert (Objects[cur_object_index].flags & OF_MARKED);

	if (num_selected_ships > 1) 
	{
		multi_edit = true;
	}

	// Fill the variable combo box	
	m_set_from_variables.ResetContent();
	m_set_from_variables.AddString("Set From Ship Class");
	for (i=0; i < MAX_SEXP_VARIABLES; i++) 
	{
		if (Sexp_variables[i].type & SEXP_VARIABLE_STRING) 
		{
			sprintf(buff, "%s[%s]", Sexp_variables[i].variable_name, Sexp_variables[i].text);
			m_set_from_variables.AddString(buff);
			string_variable_indices[num_string_variables++] = i;
		}
	}
	m_set_from_variables.SetCurSel(0);
	if (!num_string_variables) {
		m_set_from_variables.EnableWindow(FALSE);
	}
	
	// Fill the ship classes combo box
	m_set_from_ship_class.ResetContent();
	// Add the default entry if we need one followed by all the ship classes
	if (num_string_variables) {
		m_set_from_ship_class.AddString("Set From Variable");
	}
	count = 0; 
	for (auto it = Ship_info.cbegin(); it != Ship_info.cend(); ++it)
	{
		if (player_ships_only && !(it->flags & SIF_PLAYER_SHIP)) {
			continue;
		}

		ship_class_indices[count++] = std::distance(Ship_info.cbegin(), it);
		m_set_from_ship_class.AddString(it->name);
	}
	m_set_from_ship_class.SetCurSel(num_string_variables?1:0); // Set to the first ship class

	// Set up the actual list of alt classes
	alt_class_pool.clear();	
	alt_class_pool = Ships[Objects[cur_object_index].instance].s_alt_classes;
	alt_class_list_rebuild(); 
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void AltShipClassDlg::alt_class_list_rebuild()
{	
	int i, j;
	char buff[TOKEN_LENGTH + TOKEN_LENGTH + 2];  // VariableName[VariableValue]

	j = m_alt_class_list.GetCount();
	for ( i=0; i < j ; i++) {
		m_alt_class_list.DeleteString(0); 
	}

	for (i = 0; i < (int)alt_class_pool.size() ; i++) {
		// Default classes should be at the end of the list
		if(alt_class_pool[i].default_to_this_class && (i < (int)alt_class_pool.size() - 1) ) {
			// Int3();
			// TO DO : we should fix this.
		}

		if (alt_class_pool[i].variable_index != -1) {
			Assert (alt_class_pool[i].variable_index > -1 && alt_class_pool[i].variable_index < MAX_SEXP_VARIABLES);
			Assert (Sexp_variables[alt_class_pool[i].variable_index].type & SEXP_VARIABLE_STRING); 
			
			sprintf(buff, "%s[%s]", Sexp_variables[alt_class_pool[i].variable_index].variable_name, Sexp_variables[alt_class_pool[i].variable_index].text);			

			// add it to the list
			m_alt_class_list.AddString(buff); 
		}
		else {
			if (alt_class_pool[i].ship_class >= 0 && alt_class_pool[i].ship_class < MAX_SHIP_CLASSES) {
				m_alt_class_list.AddString(Ship_info[alt_class_pool[i].ship_class].name);
			}
			else {
				m_alt_class_list.AddString("Invalid Ship Class");
				Int3();
			}
		}		
	}
}

void AltShipClassDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void AltShipClassDlg::OnOK() 
{
	// TODO: Add extra validation here

	for (int i=0; i < num_selected_ships; i++) {
		Ships[m_selected_ships[i]].s_alt_classes = alt_class_pool;
	}
	
	CDialog::OnOK();
}

void AltShipClassDlg::OnAltClassAdd() 
{
	alt_class new_list_item; 

	alt_class_update_entry(new_list_item); 

	alt_class_pool.push_back(new_list_item); 
	alt_class_list_rebuild(); 
}

void AltShipClassDlg::OnAltClassInsert() 
{
	int index; 
	alt_class new_list_item; 
	alt_class_update_entry(new_list_item); 

	index = m_alt_class_list.GetCurSel(); 

	// If nothing is selected just add it
	if ( index == -1) {
		alt_class_pool.push_back(new_list_item); 
	}
	// Stick it in front of the current selection
	else {
		alt_class_pool.insert(alt_class_pool.begin() + index, new_list_item );
	}

	alt_class_list_rebuild(); 
	
	// If we inserted the user will probably want to edit the selection
	if (index > -1) {
		m_alt_class_list.SetCurSel(index); 
	}
}

// Updates the currently selected list entry
void AltShipClassDlg::alt_class_update_entry(alt_class &list_item)
{
	int index; 
	
	// Add a string variable to the list
	if (num_string_variables && m_set_from_variables.GetCurSel() > 0) {
		index = string_variable_indices[m_set_from_variables.GetCurSel() -1]; 

		Assert (index >= 0); 
		list_item.variable_index = index; 
		list_item.ship_class = ship_info_lookup(Sexp_variables[index].text); 
	}
	// Add a ship class to the list
	else {
		index = m_set_from_ship_class.GetCurSel(); 

		// Correct the index if the first entry isn't actually a ship class
		if (num_string_variables) {
			Assert (index > 0);
			index--;
		}
		
		list_item.variable_index = -1;
		list_item.ship_class = ship_class_indices[index];		
	}
	
	// check the default tickbox
	list_item.default_to_this_class = m_default_to_class.GetCheck() ? true:false;
}

void AltShipClassDlg::OnAltClassDelete() 
{
	int index = m_alt_class_list.GetCurSel();
	
	// Nothing selected
	if (index == -1) {
		return;
	}

	Assert (index < (int) alt_class_pool.size());
	alt_class_pool.erase(alt_class_pool.begin()+index);  
	alt_class_list_rebuild(); 
}

void AltShipClassDlg::OnMoveUp() 
{
	int index = m_alt_class_list.GetCurSel();
	
	// Nothing selected or already at the top
	if (index < 1) {
		return;
	}

	std::swap(alt_class_pool[index], alt_class_pool[index-1]);
	alt_class_list_rebuild(); 
	m_alt_class_list.SetCurSel(index-1); 
}

void AltShipClassDlg::OnMoveDown() 
{
	int index = m_alt_class_list.GetCurSel();
	int inext = index + 1;
	
	// Nothing selected or already at the bottom
	if (index == -1 || (index >= m_alt_class_list.GetCount() - 1)  ) {
		return;
	}

	std::swap(alt_class_pool[index], alt_class_pool[inext]);
	alt_class_list_rebuild(); 
	m_alt_class_list.SetCurSel(index); 
}

void AltShipClassDlg::OnSelendokSetFromShipClass() 
{
	int current_selection; 

	if (m_set_from_ship_class.GetCurSel() == 0 && num_string_variables) {
		m_set_from_variables.SetCurSel(1);
	}
	else {
		m_set_from_variables.SetCurSel(0); 
	}

	// if a list entry is selected we should update it
	current_selection = m_alt_class_list.GetCurSel(); 
	if (current_selection >= 0) {
		alt_class_update_entry(alt_class_pool[m_alt_class_list.GetCurSel()]); 
		alt_class_list_rebuild(); 
	}
}

void AltShipClassDlg::OnSelendokSetFromVariables() 
{
	int current_selection; 

	if (m_set_from_variables.GetCurSel() == 0) {
		m_set_from_ship_class.SetCurSel(num_string_variables ? 1 : 0);
	}
	else {
		m_set_from_ship_class.SetCurSel(0); 
	}

	// if a list entry is selected we should update it
	current_selection = m_alt_class_list.GetCurSel(); 
	if (current_selection >= 0) {
		alt_class_update_entry(alt_class_pool[m_alt_class_list.GetCurSel()]); 
		alt_class_list_rebuild(); 
	}
}

void AltShipClassDlg::OnSelchangeAltClassList() 
{
	int i;
	int variable_selection = 0;
	int ship_selection = 0; 
	int index = m_alt_class_list.GetCurSel();
	
	// Selected nothing
	if (index == -1) {
		return;
	}

	// If we have a variable selected
	if (alt_class_pool[index].variable_index != -1) {
		for (i=0; i < MAX_SEXP_VARIABLES; i++) 
		{
			if (string_variable_indices[i] == alt_class_pool[index].variable_index) {
				variable_selection = i+1; 
				break;
			}
		}
	}
	// Ship selected
	else {
		for (i=0; i < MAX_SHIP_CLASSES; i++) {
			if (ship_class_indices[i] == alt_class_pool[index].ship_class) {
				ship_selection = i; 
				break;
			}
		}

		// if we have string variables in the mission the first entry on the list won't be a ship
		if (num_string_variables) {
			ship_selection++; 
		}
	}
	
	m_set_from_variables.SetCurSel(variable_selection); 
	m_set_from_ship_class.SetCurSel(ship_selection);
	m_default_to_class.SetCheck(alt_class_pool[index].default_to_this_class ? 1:0);
}

void AltShipClassDlg::OnDefaultToClass() 
{
	int index = m_alt_class_list.GetCurSel();
	
	// Nothing selected
	if (index == -1) {
		return;
	}

	alt_class_pool[index].default_to_this_class = m_default_to_class.GetCheck() ? true:false; 
}