2 * $Logfile: /Freespace2/code/fred2/MissionGoalsDlg.cpp $
7 * Mission goals editor dialog box handling code
10 * Revision 1.1 2002/05/03 03:28:08 root
14 * 4 2/17/99 2:11p Dave
15 * First full run of squad war. All freespace and tracker side stuff
18 * 3 1/19/99 3:57p Andsager
19 * Round 2 of variables
21 * 2 10/07/98 6:28p Dave
22 * Initial checkin. Renamed all relevant stuff to be Fred2 instead of
23 * Fred. Globalized mission and campaign file extensions. Removed Silent
24 * Threat specific code.
26 * 1 10/07/98 3:01p Dave
28 * 1 10/07/98 3:00p Dave
30 * 47 5/23/98 12:20p Sandeep
32 * 46 5/22/98 1:06a Hoffoss
33 * Made Fred not use OLE.
35 * 45 5/20/98 1:04p Hoffoss
36 * Made credits screen use new artwork and removed rating field usage from
37 * Fred (a goal struct member).
39 * 44 3/31/98 12:23a Allender
40 * changed macro names of campaign types to be more descriptive. Added
41 * "team" to objectives dialog for team v. team missions. Added two
42 * distinct multiplayer campaign types
44 * 43 12/08/97 2:03p Hoffoss
45 * Added Fred support for MGF_NO_MUSIC flag in objectives.
47 * 42 11/11/97 4:13p Duncan
48 * changed assert to abort operation instead.
50 * 41 10/10/97 6:21p Hoffoss
51 * Put in Fred support for training object list editing.
53 * 40 10/10/97 2:53p Johnson
54 * Fixed bug with new items being selected before they are fully
55 * registered as added.
57 * 39 10/09/97 1:03p Hoffoss
58 * Renaming events or goals now updates sexp references as well.
60 * 38 9/30/97 12:30p Hoffoss
61 * Drag and drop reordering of sexp tree roots now does an insert after
62 * rather than a swap operation.
64 * 37 9/09/97 3:39p Sandeep
65 * warning level 4 bugs
67 * 36 8/12/97 3:33p Hoffoss
68 * Fixed the "press cancel to go to reference" code to work properly.
70 * 35 8/12/97 2:39p Johnson
71 * fixed bug with sexp tree goal name problem.
73 * 34 8/01/97 3:10p Hoffoss
74 * Made Sexp help hidable.
76 * 33 7/30/97 5:23p Hoffoss
77 * Removed Sexp tree verification code, since it duplicates normal sexp
78 * verification, and is just another set of code to keep maintained.
80 * 32 7/25/97 3:05p Allender
81 * added score field to goals and events editor
83 * 31 7/25/97 2:40p Hoffoss
84 * Fixed bug in sexp tree selection updating handling.
86 * 30 7/24/97 12:45p Hoffoss
87 * Added sexp help system to sexp trees and some dialog boxes.
89 * 29 7/17/97 4:10p Hoffoss
90 * Added drag and drop to sexp trees for reordering root items.
92 * 28 7/16/97 6:30p Hoffoss
93 * Added icons to sexp trees, mainly because I think they will be required
96 * 27 7/07/97 12:04p Allender
97 * mission goal validation.
99 * 26 6/02/97 8:47p Hoffoss
100 * Fixed bug with inserting an operator at root position, but under a
103 * 25 5/20/97 2:28p Hoffoss
104 * Added message box queries for close window operation on all modal
107 * 24 5/01/97 4:12p Hoffoss
108 * Added return handling to dialogs.
110 * 23 4/25/97 12:50p Allender
111 * change globals to new naming conventions
113 * 22 4/23/97 11:55a Hoffoss
114 * Fixed many bugs uncovered while trying to create Mission 6.
116 * 21 4/17/97 2:01p Hoffoss
117 * All dialog box window states are saved between sessions now.
119 * 20 4/11/97 4:22p Hoffoss
120 * Fixed bug in Sexp trees, moved Show starfield option to view menu and
121 * removed preferences dialog box.
123 * 19 4/11/97 10:11a Hoffoss
124 * Name fields supported by Fred for Events and Mission Goals.
126 * 18 4/10/97 3:20p Mike
127 * Change hull damage to be like shields.
129 * 17 4/01/97 5:15p Hoffoss
130 * Fixed errors in max length checks, renaming a wing now renames the
131 * ships in the wing as well, as it should.
133 * 16 2/21/97 5:34p Hoffoss
134 * Added extensive modification detection and fixed a bug in initial
137 * 15 2/17/97 5:28p Hoffoss
138 * Checked RCS headers, added them were missing, changing description to
139 * something better, etc where needed.
147 #include "fredview.h"
148 #include "linklist.h"
150 #include "missiongoalsdlg.h"
151 #include "management.h"
152 #include "operatorargtypeselect.h"
154 #define ID_ADD_SHIPS 9000
155 #define ID_REPLACE_SHIPS 11000
156 #define ID_ADD_WINGS 13000
157 #define ID_REPLACE_WINGS 15000
160 #define new DEBUG_NEW
162 static char THIS_FILE[] = __FILE__;
165 CMissionGoalsDlg *Goal_editor_dlg; // global reference needed by sexp_tree class
167 /////////////////////////////////////////////////////////////////////////////
168 // sexp_goal_tree class member functions
170 // determine the node number that would be allocated without actually allocating it yet.
171 int sexp_goal_tree::get_new_node_position()
175 for (i=0; i<MAX_SEXP_TREE_SIZE; i++)
176 if (nodes[i].type == SEXPT_UNUSED)
182 // construct tree nodes for an sexp, adding them to the list and returning first node
183 int sexp_goal_tree::load_sub_tree(int index)
188 cur = allocate_node(-1);
189 set_node(cur, (SEXPT_OPERATOR | SEXPT_VALID), "true"); // setup a default tree if none
193 // assumption: first token is an operator. I require this because it would cause problems
194 // with child/parent relations otherwise, and it should be this way anyway, since the
195 // return type of the whole sexp is boolean, and only operators can satisfy this.
196 Assert(Sexp_nodes[index].subtype == SEXP_ATOM_OPERATOR);
197 cur = get_new_node_position();
198 load_branch(index, -1);
202 /////////////////////////////////////////////////////////////////////////////
203 // CMissionGoalsDlg dialog class member functions
205 CMissionGoalsDlg::CMissionGoalsDlg(CWnd* pParent /*=NULL*/)
206 : CDialog(CMissionGoalsDlg::IDD, pParent)
208 //{{AFX_DATA_INIT(CMissionGoalsDlg)
209 m_goal_desc = _T("");
211 m_display_goal_types = 0;
213 m_goal_invalid = FALSE;
218 m_goals_tree.m_mode = MODE_GOALS;
220 m_goals_tree.link_modified(&modified);
222 select_sexp_node = -1;
225 BOOL CMissionGoalsDlg::OnInitDialog()
229 CDialog::OnInitDialog(); // let the base class do the default work
231 adjust = -SEXP_HELP_BOX_SIZE;
233 theApp.init_window(&Mission_goals_wnd_data, this, adjust);
234 m_goals_tree.setup((CEdit *) GetDlgItem(IDC_HELP_BOX));
237 if (m_num_goals >= MAX_GOALS)
238 GetDlgItem(IDC_BUTTON_NEW_GOAL)->EnableWindow(FALSE);
240 Goal_editor_dlg = this;
241 i = m_goals_tree.select_sexp_node;
243 GetDlgItem(IDC_GOALS_TREE) -> SetFocus();
244 m_goals_tree.hilite_item(i);
251 void CMissionGoalsDlg::DoDataExchange(CDataExchange* pDX)
253 CDialog::DoDataExchange(pDX);
254 //{{AFX_DATA_MAP(CMissionGoalsDlg)
255 DDX_Control(pDX, IDC_GOALS_TREE, m_goals_tree);
256 DDX_Text(pDX, IDC_GOAL_DESC, m_goal_desc);
257 DDX_CBIndex(pDX, IDC_GOAL_TYPE_DROP, m_goal_type);
258 DDX_CBIndex(pDX, IDC_DISPLAY_GOAL_TYPES_DROP, m_display_goal_types);
259 DDX_Text(pDX, IDC_GOAL_NAME, m_name);
260 DDX_Check(pDX, IDC_GOAL_INVALID, m_goal_invalid);
261 DDX_Text(pDX, IDC_GOAL_SCORE, m_goal_score);
262 DDX_Check(pDX, IDC_NO_MUSIC, m_no_music);
263 DDX_CBIndex(pDX, IDC_OBJ_TEAM, m_team);
265 DDV_MaxChars(pDX, m_goal_desc, MAX_GOAL_TEXT - 1);
266 DDV_MaxChars(pDX, m_name, NAME_LENGTH - 1);
269 BEGIN_MESSAGE_MAP(CMissionGoalsDlg, CDialog)
270 //{{AFX_MSG_MAP(CMissionGoalsDlg)
271 ON_CBN_SELCHANGE(IDC_DISPLAY_GOAL_TYPES_DROP, OnSelchangeDisplayGoalTypesDrop)
272 ON_NOTIFY(TVN_SELCHANGED, IDC_GOALS_TREE, OnSelchangedGoalsTree)
273 ON_NOTIFY(NM_RCLICK, IDC_GOALS_TREE, OnRclickGoalsTree)
274 ON_NOTIFY(TVN_ENDLABELEDIT, IDC_GOALS_TREE, OnEndlabeleditGoalsTree)
275 ON_NOTIFY(TVN_BEGINLABELEDIT, IDC_GOALS_TREE, OnBeginlabeleditGoalsTree)
276 ON_BN_CLICKED(IDC_BUTTON_NEW_GOAL, OnButtonNewGoal)
277 ON_EN_CHANGE(IDC_GOAL_DESC, OnChangeGoalDesc)
278 ON_EN_CHANGE(IDC_GOAL_RATING, OnChangeGoalRating)
279 ON_CBN_SELCHANGE(IDC_GOAL_TYPE_DROP, OnSelchangeGoalTypeDrop)
280 ON_EN_CHANGE(IDC_GOAL_NAME, OnChangeGoalName)
281 ON_BN_CLICKED(ID_OK, OnOk)
283 ON_BN_CLICKED(IDC_GOAL_INVALID, OnGoalInvalid)
284 ON_EN_CHANGE(IDC_GOAL_SCORE, OnChangeGoalScore)
285 ON_BN_CLICKED(IDC_NO_MUSIC, OnNoMusic)
286 ON_CBN_SELCHANGE(IDC_OBJ_TEAM, OnSelchangeTeam)
290 /////////////////////////////////////////////////////////////////////////////
291 // CMissionGoalsDlg message handlers
293 // Initialization: sets up internal working copy of mission goals and goal trees.
294 void CMissionGoalsDlg::load_tree()
298 m_goals_tree.select_sexp_node = select_sexp_node;
299 select_sexp_node = -1;
301 m_goals_tree.clear_tree();
302 m_num_goals = Num_goals;
303 for (i=0; i<Num_goals; i++) {
304 m_goals[i] = Mission_goals[i];
306 if (!(*m_goals[i].name))
307 strcpy(m_goals[i].name, "<unnamed>");
309 m_goals[i].formula = m_goals_tree.load_sub_tree(Mission_goals[i].formula);
312 m_goals_tree.post_load();
317 // create the CTreeCtrl tree from the goal tree, filtering based on m_display_goal_types
318 void CMissionGoalsDlg::create_tree()
323 m_goals_tree.DeleteAllItems();
324 m_goals_tree.reset_handles();
325 for (i=0; i<m_num_goals; i++) {
326 if ( (m_goals[i].type & GOAL_TYPE_MASK) != m_display_goal_types)
329 h = m_goals_tree.insert(m_goals[i].name);
330 m_goals_tree.SetItemData(h, m_goals[i].formula);
331 m_goals_tree.add_sub_tree(m_goals[i].formula, h);
338 // Display goal types selection changed, so update the display
339 void CMissionGoalsDlg::OnSelchangeDisplayGoalTypesDrop()
345 // New tree item selected. Because goal info is displayed for the selected tree item,
346 // we need to update the display when this occurs.
347 void CMissionGoalsDlg::OnSelchangedGoalsTree(NMHDR* pNMHDR, LRESULT* pResult)
350 NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
353 h = pNMTreeView->itemNew.hItem;
357 m_goals_tree.update_help(h);
358 while ((h2 = m_goals_tree.GetParentItem(h)) != 0)
361 z = m_goals_tree.GetItemData(h);
362 for (i=0; i<m_num_goals; i++)
363 if (m_goals[i].formula == z)
366 Assert(i < m_num_goals);
372 // update display info to reflect the currently selected goal.
373 void CMissionGoalsDlg::update_cur_goal()
377 m_goal_desc = _T("");
381 GetDlgItem(IDC_GOAL_TYPE_DROP) -> EnableWindow(FALSE);
382 GetDlgItem(IDC_GOAL_NAME) -> EnableWindow(FALSE);
383 GetDlgItem(IDC_GOAL_DESC) -> EnableWindow(FALSE);
384 GetDlgItem(IDC_GOAL_INVALID)->EnableWindow(FALSE);
385 GetDlgItem(IDC_GOAL_SCORE)->EnableWindow(FALSE);
386 GetDlgItem(IDC_NO_MUSIC)->EnableWindow(FALSE);
387 GetDlgItem(IDC_OBJ_TEAM)->EnableWindow(FALSE);
391 m_name = _T(m_goals[cur_goal].name);
392 m_goal_desc = _T(m_goals[cur_goal].message);
393 m_goal_type = m_goals[cur_goal].type & GOAL_TYPE_MASK;
394 if ( m_goals[cur_goal].type & INVALID_GOAL ){
400 if ( m_goals[cur_goal].flags & MGF_NO_MUSIC ){
406 m_goal_score = m_goals[cur_goal].score;
408 m_team = m_goals[cur_goal].team;
411 GetDlgItem(IDC_GOAL_TYPE_DROP) -> EnableWindow(TRUE);
412 GetDlgItem(IDC_GOAL_NAME) -> EnableWindow(TRUE);
413 GetDlgItem(IDC_GOAL_DESC) -> EnableWindow(TRUE);
414 // GetDlgItem(IDC_GOAL_RATING) -> EnableWindow(TRUE);
415 GetDlgItem(IDC_GOAL_INVALID)->EnableWindow(TRUE);
416 GetDlgItem(IDC_GOAL_SCORE)->EnableWindow(TRUE);
417 GetDlgItem(IDC_NO_MUSIC)->EnableWindow(TRUE);
418 GetDlgItem(IDC_OBJ_TEAM)->EnableWindow(FALSE);
419 if ( The_mission.game_type & MISSION_TYPE_MULTI_TEAMS ){
420 GetDlgItem(IDC_OBJ_TEAM)->EnableWindow(TRUE);
424 // handler for context menu (i.e. a right mouse button click).
425 void CMissionGoalsDlg::OnRclickGoalsTree(NMHDR* pNMHDR, LRESULT* pResult)
427 m_goals_tree.right_clicked(MODE_GOALS);
431 // goal tree item label editing is requested. Determine if it should be allowed.
432 void CMissionGoalsDlg::OnBeginlabeleditGoalsTree(NMHDR* pNMHDR, LRESULT* pResult)
434 TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
436 if (m_goals_tree.edit_label(pTVDispInfo->item.hItem) == 1) {
444 // Once we finish editing, we need to clean up, which we do here.
445 void CMissionGoalsDlg::OnEndlabeleditGoalsTree(NMHDR* pNMHDR, LRESULT* pResult)
447 TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;
449 *pResult = m_goals_tree.end_label_edit(pTVDispInfo->item.hItem, pTVDispInfo->item.pszText);
452 void CMissionGoalsDlg::OnOK()
460 GetDlgItem(IDC_GOALS_TREE)->SetFocus();
465 int CMissionGoalsDlg::query_modified()
472 if (Num_goals != m_num_goals)
475 for (i=0; i<Num_goals; i++) {
476 if (stricmp(Mission_goals[i].name, m_goals[i].name))
478 if (stricmp(Mission_goals[i].message, m_goals[i].message))
480 if (Mission_goals[i].type != m_goals[i].type)
482 if ( Mission_goals[i].score != m_goals[i].score )
484 if ( Mission_goals[i].team != m_goals[i].team )
491 void CMissionGoalsDlg::OnOk()
493 char buf[256], names[2][MAX_GOALS][NAME_LENGTH];
496 for (i=0; i<Num_goals; i++)
497 free_sexp2(Mission_goals[i].formula);
500 if (query_modified())
504 for (i=0; i<Num_goals; i++)
505 Mission_goals[i].satisfied = 0; // use this as a processed flag
507 // rename all sexp references to old events
508 for (i=0; i<m_num_goals; i++)
510 strcpy(names[0][count], Mission_goals[m_sig[i]].name);
511 strcpy(names[1][count], m_goals[i].name);
513 Mission_goals[m_sig[i]].satisfied = 1;
516 // invalidate all sexp references to deleted events.
517 for (i=0; i<Num_goals; i++)
518 if (!Mission_goals[i].satisfied) {
519 sprintf(buf, "<%s>", Mission_goals[i].name);
520 strcpy(buf + NAME_LENGTH - 2, ">"); // force it to be not too long
521 strcpy(names[0][count], Mission_goals[i].name);
522 strcpy(names[1][count], buf);
526 Num_goals = m_num_goals;
527 for (i=0; i<Num_goals; i++) {
528 Mission_goals[i] = m_goals[i];
529 Mission_goals[i].formula = m_goals_tree.save_tree(Mission_goals[i].formula);
530 if ( The_mission.game_type & MISSION_TYPE_MULTI_TEAMS ) {
531 Assert( Mission_goals[i].team != -1 );
535 // now update all sexp references
537 update_sexp_references(names[0][count], names[1][count], OPF_GOAL_NAME);
539 theApp.record_window_data(&Mission_goals_wnd_data, this);
543 void CMissionGoalsDlg::OnButtonNewGoal()
548 Assert(m_num_goals < MAX_GOALS);
549 m_goals[m_num_goals].type = m_display_goal_types; // this also marks the goal as valid since bit not set
550 m_sig[m_num_goals] = -1;
551 strcpy(m_goals[m_num_goals].name, "Goal name");
552 strcpy(m_goals[m_num_goals].message, "Mission goal text");
553 h = m_goals_tree.insert(m_goals[m_num_goals].name);
555 m_goals_tree.item_index = -1;
556 m_goals_tree.add_operator("true", h);
557 m_goals[m_num_goals].score = 0;
558 index = m_goals[m_num_goals].formula = m_goals_tree.item_index;
559 m_goals_tree.SetItemData(h, index);
561 // team defaults to the first team.
562 m_goals[m_num_goals].team = 0;
566 if (m_num_goals >= MAX_GOALS){
567 GetDlgItem(IDC_BUTTON_NEW_GOAL)->EnableWindow(FALSE);
570 m_goals_tree.SelectItem(h);
573 int CMissionGoalsDlg::handler(int code, int node)
579 for (goal=0; goal<m_num_goals; goal++){
580 if (m_goals[goal].formula == node){
585 Assert(goal < m_num_goals);
586 while (goal < m_num_goals - 1) {
587 m_goals[goal] = m_goals[goal + 1];
588 m_sig[goal] = m_sig[goal + 1];
592 GetDlgItem(IDC_BUTTON_NEW_GOAL)->EnableWindow(TRUE);
602 void CMissionGoalsDlg::OnChangeGoalDesc()
609 string_copy(m_goals[cur_goal].message, m_goal_desc, MAX_GOAL_TEXT);
612 void CMissionGoalsDlg::OnChangeGoalRating()
621 void CMissionGoalsDlg::OnSelchangeGoalTypeDrop()
631 UpdateData(TRUE); // doesn't seem to update unless we do it twice..
633 // change the type being sure to keep the invalid bit if set
634 otype = m_goals[cur_goal].type;
635 m_goals[cur_goal].type = m_goal_type;
636 if ( otype & INVALID_GOAL ){
637 m_goals[cur_goal].type |= INVALID_GOAL;
640 h = m_goals_tree.GetSelectedItem();
642 while ((h2 = m_goals_tree.GetParentItem(h)) != 0){
646 m_goals_tree.DeleteItem(h);
651 void CMissionGoalsDlg::OnChangeGoalName()
660 h = m_goals_tree.GetSelectedItem();
665 while ((h2 = m_goals_tree.GetParentItem(h)) != 0){
669 m_goals_tree.SetItemText(h, m_name);
670 string_copy(m_goals[cur_goal].name, m_name, NAME_LENGTH);
673 void CMissionGoalsDlg::OnCancel()
675 theApp.record_window_data(&Messages_wnd_data, this);
679 void CMissionGoalsDlg::OnClose()
683 if (query_modified()) {
684 z = MessageBox("Do you want to keep your changes?", "Close", MB_ICONQUESTION | MB_YESNOCANCEL);
697 void CMissionGoalsDlg::insert_handler(int old, int node)
701 for (i=0; i<m_num_goals; i++){
702 if (m_goals[i].formula == old){
707 Assert(i < m_num_goals);
708 m_goals[i].formula = node;
712 void CMissionGoalsDlg::OnGoalInvalid()
718 m_goal_invalid = !m_goal_invalid;
719 m_goals[cur_goal].type ^= INVALID_GOAL;
723 void CMissionGoalsDlg::OnNoMusic()
729 m_no_music = !m_no_music;
730 m_goals[cur_goal].flags ^= MGF_NO_MUSIC;
734 void CMissionGoalsDlg::swap_handler(int node1, int node2)
739 for (index1=0; index1<m_num_goals; index1++){
740 if (m_goals[index1].formula == node1){
745 Assert(index1 < m_num_goals);
746 for (index2=0; index2<m_num_goals; index2++){
747 if (m_goals[index2].formula == node2){
752 Assert(index2 < m_num_goals);
754 // m_goals[index1] = m_goals[index2];
755 while (index1 < index2) {
756 m_goals[index1] = m_goals[index1 + 1];
757 m_sig[index1] = m_sig[index1 + 1];
761 while (index1 > index2 + 1) {
762 m_goals[index1] = m_goals[index1 - 1];
763 m_sig[index1] = m_sig[index1 - 1];
770 void CMissionGoalsDlg::OnChangeGoalScore()
777 m_goals[cur_goal].score = m_goal_score;
781 // code when the "team" selection in the combo box changes
782 void CMissionGoalsDlg::OnSelchangeTeam()
789 m_goals[cur_goal].team = m_team;