]> icculus.org git repositories - taylor/freespace2.git/blob - src/fred2/freddoc.cpp
The Great Newline Fix
[taylor/freespace2.git] / src / fred2 / freddoc.cpp
1 /*
2  * $Logfile: /Freespace2/code/Fred2/FREDDoc.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * FREDDoc.cpp : implementation of the CFREDDoc class
8  * Document class for document/view architechure, which we don't really use in
9  * Fred, but MFC forces you do use like it or not.  Handles loading/saving
10  * mainly.  Most of the MFC related stuff is handled in FredView.
11  *
12  * $Log$
13  * Revision 1.2  2002/05/07 03:16:44  theoddone33
14  * The Great Newline Fix
15  *
16  * Revision 1.1.1.1  2002/05/03 03:28:08  root
17  * Initial import.
18  *
19  * 
20  * 7     7/23/99 2:12p Jamesa
21  * fix gamepalette request
22  * 
23  * 6     5/20/99 6:59p Dave
24  * Added alternate type names for ships. Changed swarm missile table
25  * entries.
26  * 
27  * 5     10/29/98 6:49p Dave
28  * Finished up Fred support for externalizing mission and campaign files.
29  * 
30  * 4     10/29/98 12:50p Dave
31  * Intermediate checkin for fred hash table stuff.
32  * 
33  * 3     10/28/98 11:30a Dave
34  * Temporary checkin.
35  * 
36  * 2     10/07/98 6:28p Dave
37  * Initial checkin. Renamed all relevant stuff to be Fred2 instead of
38  * Fred. Globalized mission and campaign file extensions. Removed Silent
39  * Threat specific code.
40  * 
41  * 1     10/07/98 3:00p Dave
42  * 
43  * 94    9/21/98 8:46p Dave
44  * Put in special check in fred for identifying unknown ships.
45  * 
46  * 93    9/16/98 6:54p Dave
47  * Upped  max sexpression nodes to 1800 (from 1600). Changed FRED to sort
48  * the ship list box. Added code so that tracker stats are not stored with
49  * only 1 player.
50  * 
51  * 92    8/13/98 9:53a Hoffoss
52  * Fixed bug where loading mission doesn't update palette used for that
53  * mission.
54  * 
55  * 91    4/30/98 8:23p John
56  * Fixed some bugs with Fred caused by my new cfile code.
57  * 
58  * 90    4/28/98 2:13p Hoffoss
59  * Added code to help keep invalid player ship types from existing in
60  * mission.
61  * 
62  * 89    4/10/98 10:22p Mike
63  * Fix bug in parsing medals.tbl.
64  * 
65  * 88    4/10/98 4:51p Hoffoss
66  * Made several changes related to tooltips.
67  * 
68  * 87    3/25/98 4:14p Hoffoss
69  * Split ship editor up into ship editor and a misc dialog, which tracks
70  * flags and such.
71  * 
72  * 86    3/10/98 4:26p Hoffoss
73  * Changed jump node structure to include a name.  Position is now taken
74  * from the object (each jump node has an associated object now).
75  * 
76  * 85    2/26/98 4:59p Allender
77  * groundwork for team vs team briefings.  Moved weaponry pool into the
78  * Team_data structure.  Added team field into the p_info structure.
79  * Allow for mutliple structures in the briefing code.
80  * 
81  * 84    1/28/98 11:00a Johnson
82  * Fixed parse code for medals table (new field)
83  * 
84  * 83    11/06/97 4:37p Allender
85  * a ton of medal work.  Removed an uneeded element in the scoring
86  * structure.  Fix up medals screen to apprioriate display medals (after
87  * mask was changed). Fix Fred to only display medals which may actually
88  * be granted.  Added image_filename to player struct for Jason Hoffoss
89  * 
90  * 82    11/05/97 10:19p Mike
91  * Comment out call to parse_medal_tbl() to allow Fred to run.
92  * 
93  * 81    11/05/97 4:43p Allender
94  * reworked medal/rank system to read all data from tables.  Made Fred
95  * read medals.tbl.  Changed ai-warp to ai-warp-out which doesn't require
96  * waypoint for activation
97  * 
98  * 80    11/04/97 4:50p Hoffoss
99  * Made mission loading failure display an error message stating so.
100  * 
101  * 79    11/04/97 4:33p Hoffoss
102  * Made saving keep the current briefing state intact.
103  * 
104  * 78    10/20/97 1:20p Hoffoss
105  * Fixed bug loading mission file after a save.
106  * 
107  * 77    10/19/97 11:36p Hoffoss
108  * Made mission saving call reload the mission after saving it to verify
109  * it was written error free.
110  * 
111  * 76    10/08/97 11:47a Hoffoss
112  * Added better fred handling of Weaponry_pool.
113  * 
114  * 75    9/20/97 8:16a John
115  * Made .clr files go into the Cache directory. Replaced cfopen(name,NULL)
116  * to delete a file with cf_delete.
117  * 
118  * 74    9/18/97 11:52a Johnson
119  * Fixed bug with player starts count being incorrect.
120  * 
121  * 73    9/17/97 11:58a Hoffoss
122  * Fixed mission load code to properly set arrival cues to player ships.
123  * 
124  * 72    9/17/97 10:32a Allender
125  * fixup references to old "Player" ship to new real shipname
126  * 
127  * 71    9/16/97 9:41p Hoffoss
128  * Changed Fred code around to stop using Parse_player structure for
129  * player information, and use actual ships instead.
130  * 
131  * 70    9/09/97 6:50p Hoffoss
132  * Fixed bug with mission saving.
133  * 
134  * 69    9/09/97 1:55p Hoffoss
135  * Fixed bug with saving missions that are readonly.
136  * 
137  * 68    8/21/97 1:49p Hoffoss
138  * Fixed bugs with loading files.  Wasn't clearing the last mission first,
139  * and multiplayer starts in a mission caused it to think it was modified
140  * right away.
141  * 
142  * 67    8/20/97 5:45p Hoffoss
143  * Fixed bugs with mission name in sexp in non campaign mode in Fred.
144  * 
145  * 66    8/20/97 4:03p Hoffoss
146  * Fixed bug in reload current mission (revert).
147  * 
148  * 65    8/17/97 10:22p Hoffoss
149  * Fixed several bugs in Fred with Undo feature.  In the process, recoded
150  * a lot of CFile.cpp.
151  * 
152  * 64    8/16/97 9:24p Hoffoss
153  * Added support for team of players in multiplayer.
154  * 
155  * 63    8/16/97 6:44p Hoffoss
156  * Changes to allow any player to be in a wing.
157  * 
158  * 62    8/16/97 3:53p Hoffoss
159  * Added OF_NO_SHIELDS define and support in Fred and mission load/save.
160  * 
161  * 61    8/13/97 5:49p Hoffoss
162  * Fixed bugs, made additions.
163  * 
164  * 60    8/13/97 12:46p Hoffoss
165  * Added campaign error checker, accelerator table, and mission goal data
166  * listings to sexp tree right click menu.
167  * 
168  * 59    8/12/97 1:55a Hoffoss
169  * Made extensive changes to object reference checking and handling for
170  * object deletion call.
171  * 
172  * 58    7/31/97 5:55p John
173  * made so you pass flags to obj_create.
174  * Added new collision code that ignores any pairs that will never
175  * collide.
176  * 
177  * 57    7/30/97 2:10p Hoffoss
178  * Made new missions that have never been saved yet save backups.
179  * 
180  * 56    7/28/97 3:50p Hoffoss
181  * Added global error checker call to mission load and save.
182  * 
183  * 55    7/10/97 2:32p Hoffoss
184  * Made message editor dialog box modeless.
185  * 
186  * 54    7/08/97 11:35a Hoffoss
187  * Fixed bug in initial orders, and also redid how shields are handled by
188  * Fred.
189  * 
190  * 53    6/26/97 5:18p Hoffoss
191  * Major rework of briefing editor functionality.
192  * 
193  * 52    6/24/97 10:22a Hoffoss
194  * Added a forced update for briefing info prior to a mission save.
195  * 
196  * 51    6/19/97 11:34a Hoffoss
197  * Fixed bug in auto mission convertion.
198  * 
199  * 50    6/18/97 3:25p Hoffoss
200  * Added autoconverting of old ship names (for ships in wings) at mission
201  * load time, and added an hourglass cursor change when autosaving.
202  * 
203  * 49    6/17/97 11:07a Hoffoss
204  * Fixed a few bugs: revert doesn't reset mission first, and sexp tree
205  * copy doesn't act act as a single chain.
206  * 
207  * 48    6/11/97 10:02a Comet
208  * fixed a bug.
209  * 
210  * 47    6/09/97 4:57p Hoffoss
211  * Added autosave and undo to Fred.
212  * 
213  * 46    6/05/97 6:10p Hoffoss
214  * Added features: Autosaving, object hiding.  Also fixed some minor bugs.
215  * 
216  * 45    5/23/97 3:18p Hoffoss
217  * Event editor made modeless.
218  * 
219  * 44    5/21/97 5:42p Hoffoss
220  * Added features requested on Todo list.
221  * 
222  * 43    5/01/97 4:14p Hoffoss
223  * Viewer position saved to missions now for Fred restoration of state.
224  * 
225  * 42    4/25/97 3:53p Hoffoss
226  * Fixed bug in weapons save/load and moved weapon look up function to
227  * weapons.cpp.
228  * 
229  * 41    4/25/97 3:28p Mike
230  * Making shield multi-part.
231  * 
232  * 40    4/21/97 5:02p Hoffoss
233  * Player/player status editing supported, and both saved and loaded from
234  * Mission files.
235  * 
236  * 39    3/20/97 3:55p Hoffoss
237  * Major changes to how dialog boxes initialize (load) and update (save)
238  * their internal data.  This should simplify things and create less
239  * problems.
240  * 
241  * 38    3/04/97 6:27p Hoffoss
242  * Changes to Fred to handle new wing structure.
243  * 
244  * 37    2/28/97 11:31a Hoffoss
245  * Implemented modeless dialog saving and restoring, and changed some
246  * variables names.
247  * 
248  * 36    2/27/97 3:09p Allender
249  * major wing structure enhancement.  simplified wing code.  All around
250  * better wing support
251  * 
252  * 35    2/20/97 4:03p Hoffoss
253  * Several ToDo items: new reinforcement clears arrival cue, reinforcement
254  * control from ship and wing dialogs, show grid toggle.
255  * 
256  * 34    2/17/97 5:28p Hoffoss
257  * Checked RCS headers, added them were missing, changing description to
258  * something better, etc where needed.
259  * 
260  * 14    2/12/97 12:25p Hoffoss
261  * Expanded on global error checker, added initial orders conflict
262  * checking and warning, added waypoint editor dialog and code.
263  * 
264  * 13    1/30/97 2:24p Hoffoss
265  * Added remaining mission file structures and implemented load/save of
266  * them.
267  *
268  * $NoKeywords: $
269  */
270
271 #include "stdafx.h"
272 #include "fred.h"
273 #include <stdlib.h>
274
275 #include "freddoc.h"
276 #include "fredview.h"
277 #include "prefsdlg.h"
278
279 #include "3d.h"
280 #include "object.h"
281 #include "editor.h"
282 #include "ai.h"
283 #include "ailocal.h"
284 #include "cfile.h"
285 #include "ship.h"
286 #include "missionparse.h"
287 #include "missiongoals.h"
288 #include "missionsave.h"
289 #include "weapon.h"
290 #include "management.h"
291 #include "linklist.h"
292 #include "fredrender.h"
293 #include "mainfrm.h"
294 #include "eventeditor.h"
295 #include "aigoals.h"
296 #include "messageeditordlg.h"
297 #include "palman.h"
298 #include "fhash.h"
299
300 extern int num_objects;
301
302 #ifdef _DEBUG
303 #define new DEBUG_NEW
304 #undef THIS_FILE
305 static char THIS_FILE[] = __FILE__;
306 #endif
307
308 //      In editor mode, use class CFile, in game, use CFILE (our file)
309 #define XFILE CFile
310
311 /////////////////////////////////////////////////////////////////////////////
312 // CFREDDoc
313
314 IMPLEMENT_DYNCREATE(CFREDDoc, CDocument)
315
316 BEGIN_MESSAGE_MAP(CFREDDoc, CDocument)
317         //{{AFX_MSG_MAP(CFREDDoc)
318         ON_COMMAND(ID_EDIT_DELETE, OnEditDelete)
319         ON_COMMAND(ID_DUPLICATE, OnDuplicate)
320         ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
321         ON_COMMAND(ID_EDIT_CUT, OnEditCut)
322         ON_COMMAND(ID_EDIT_HOLD, OnEditHold)
323         ON_COMMAND(ID_EDIT_FETCH, OnEditFetch)
324         ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
325         ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
326         ON_COMMAND(ID_FILE_PREFERENCES, OnFilePreferences)
327         //}}AFX_MSG_MAP
328 END_MESSAGE_MAP()
329
330 /////////////////////////////////////////////////////////////////////////////
331 // CFREDDoc construction/destruction
332
333 //      Global pointer to the FREDDoc class.
334 //      Used by MK to, among other things, I hope, update the modified flag from
335 //      outside the FREDDoc class.
336 CFREDDoc *FREDDoc_ptr = NULL;
337 int Local_modified = 0;
338 int Undo_available = 0;
339 int Undo_count = 0;
340
341 extern int Fred_found_unknown_ship_during_parsing;
342
343 CFREDDoc::CFREDDoc()
344 {
345         int i;
346
347         FREDDoc_ptr = this;
348         confirm_deleting = TRUE;
349         show_capital_ships = TRUE;
350         show_elevations = TRUE;
351         show_fighters = TRUE;
352         show_grid = TRUE;
353         show_misc_objects = TRUE;
354         show_planets = TRUE;
355         show_waypoints = TRUE;
356         show_starfield = TRUE;
357
358         for (i=0; i<BACKUP_DEPTH; i++)
359                 undo_desc[i].Empty();
360 }
361
362 CFREDDoc::~CFREDDoc()
363 {
364 }
365
366 BOOL CFREDDoc::OnNewDocument()
367 {
368         if (!CDocument::OnNewDocument())
369                 return FALSE;
370
371         // TODO: add reinitialization code here
372         confirm_deleting = TRUE;
373         show_capital_ships = FALSE;
374         show_elevations = TRUE;
375         show_fighters = TRUE;
376         show_grid = FALSE;
377         show_misc_objects = TRUE;
378         show_planets = FALSE;
379         show_waypoints = TRUE;
380         show_starfield = FALSE;
381         // (SDI documents will reuse this document)
382
383         return TRUE;
384 }
385
386 // read in a new mission file from disk
387 BOOL CFREDDoc::OnOpenDocument(LPCTSTR pathname)
388 {
389         char name[1024];
390         int i, len;
391
392         if (pathname)
393                 strcpy(mission_pathname, pathname);
394
395         if (Briefing_dialog)
396                 Briefing_dialog->icon_select(-1);  // clean things up first
397
398         len = strlen(mission_pathname);
399         strcpy(name, mission_pathname);
400         if (name[len - 4] == '.')
401                 len -= 4;
402
403         name[len] = 0;  // drop extension
404         i = len;
405         while (i--)
406                 if ((name[i] == '\\') || (name[i] == ':'))
407                         break;
408
409         strcpy(Mission_filename, name + i + 1);
410 //      for (i=1; i<=BACKUP_DEPTH; i++) {
411 //              sprintf(name + len, ".%.3d", i);
412 //              unlink(name);
413 //      }
414
415         if (load_mission(mission_pathname)) {
416                 *Mission_filename = 0;
417                 return FALSE;
418         }
419
420         Fred_view_wnd->global_error_check();
421         autosave("nothing");
422         Undo_count = 0;
423         return TRUE;
424 }
425
426 // save mission to a file
427 BOOL CFREDDoc::OnSaveDocument(LPCTSTR pathname)
428 {
429         CFred_mission_save save;
430         char name[1024];
431         int len;
432         DWORD attrib;
433         FILE *fp;
434
435         len = strlen(pathname);
436         strcpy(name, pathname);
437         if (name[len - 4] == '.')
438                 len -= 4;
439
440         name[len] = 0;  // drop extension
441         while (len--)
442                 if ((name[len] == '\\') || (name[len] == ':'))
443                         break;
444
445         strcpy(Mission_filename, name + len + 1);
446         Fred_view_wnd->global_error_check();
447         if (Briefing_dialog) {
448                 Briefing_dialog->update_data(1);
449                 Briefing_dialog->save_editor_state();
450         }
451         
452         if (Event_editor_dlg)
453                 Fred_main_wnd->MessageBox("Event editor dialog is still open, so changes there won't be saved");
454
455         if (Message_editor_dlg)
456                 Fred_main_wnd->MessageBox("Message editor dialog is still open, so changes there won't be saved");
457
458         fp = fopen(pathname, "r");
459         if (fp) {
460                 fclose(fp);
461                 attrib = GetFileAttributes(pathname);
462                 if (attrib & FILE_ATTRIBUTE_READONLY) {
463                         Fred_main_wnd->MessageBox("File is read-only.  You need to check it out before saving to it");
464                         return FALSE;
465                 }
466         }       
467
468         if (save.save_mission_file((char *) pathname)) {
469                 Fred_main_wnd->MessageBox("An error occured while saving!", NULL, MB_OK | MB_ICONEXCLAMATION);
470                 return FALSE;
471         }
472
473         SetModifiedFlag(FALSE);
474         if (load_mission((char *) pathname))
475                 Error(LOCATION, "Failed attempting to reload mission after saving.  Report this bug now!");
476
477         if (Briefing_dialog) {
478                 Briefing_dialog->restore_editor_state();
479                 Briefing_dialog->update_data(1);
480         }
481
482         return TRUE;
483 //      return CDocument::OnSaveDocument(pathname);
484 }
485
486 int CFREDDoc::check_undo()
487 {
488         char name[256];
489         FILE *fp;
490
491         Undo_available = 0;
492         if (!Undo_count)
493                 return 0;
494
495         strcpy(name, MISSION_BACKUP_NAME);
496         strcat(name, ".002");
497         fp = fopen(name, "r");
498         if (!fp)
499                 return 0;
500
501         fclose(fp);
502         Undo_available = 1;
503         return 1;
504 }
505
506 int CFREDDoc::autosave(char *desc)
507 {
508         int i;
509         CFred_mission_save save;
510         CWaitCursor wait;
511
512         if (Autosave_disabled)
513                 return 0;
514
515         if (Briefing_dialog)
516                 Briefing_dialog->update_data(1);
517         
518         if (save.autosave_mission_file(MISSION_BACKUP_NAME)) {
519                 Undo_count = Undo_available = 0;
520                 return -1;
521         }
522
523         for (i=BACKUP_DEPTH; i>1; i--)
524                 undo_desc[i] = undo_desc[i - 1];
525
526         if (desc)
527                 undo_desc[1] = desc;
528         else
529                 undo_desc[1].Empty();
530
531         Undo_count++;
532         check_undo();
533         return 0;
534 }
535
536 int CFREDDoc::autoload()
537 {
538         char name[256], backup_name[256];
539         int i, r, len;
540         FILE *fp;
541
542         strcpy(name, MISSION_BACKUP_NAME);
543         strcat(name, ".002");
544         fp = fopen(name, "r");
545         if (!fp)
546                 return 0;
547
548         fclose(fp);
549         if (Briefing_dialog)
550                 Briefing_dialog->icon_select(-1);  // clean things up first
551
552 //      editor_init_mission();  
553         r = load_mission(name);
554         Update_window = 1;
555
556         strcpy(backup_name, MISSION_BACKUP_NAME);
557         len = strlen(backup_name);
558         strcat(backup_name, ".001");
559         cf_delete(backup_name, CF_TYPE_MISSIONS);
560
561         for (i=1; i<BACKUP_DEPTH; i++) {
562                 sprintf(backup_name + len, ".%.3d", i + 1);
563                 sprintf(name + len, ".%.3d", i);
564                 cf_rename(backup_name, name, CF_TYPE_MISSIONS);
565                 undo_desc[i] = undo_desc[i + 1];
566         }
567
568         Undo_count--;
569         check_undo();
570         return r;
571 }
572
573 // read in a new mission file from disk
574 int CFREDDoc::load_mission(char *pathname)
575 {
576         char name[512], *old_name;
577         int i, j, k, ob;
578         int used_pool[MAX_WEAPON_TYPES];
579         waypoint_list *wptr;
580         object *objp;
581
582         Parse_viewer_pos = view_pos;
583         Parse_viewer_orient = view_orient;
584
585         // activate the localizer hash table
586         fhash_flush();  
587
588         clear_mission();
589         Fred_found_unknown_ship_during_parsing = 0;
590         if (parse_main(pathname)) {
591                 sprintf(name, "Unable to load the file \"%s\"", pathname);
592                 Fred_view_wnd->MessageBox(name);
593                 create_new_mission();           
594                 return -1;
595         }
596
597         if(Fred_found_unknown_ship_during_parsing){
598                 Fred_view_wnd->MessageBox("Fred encountered unknown ship/weapon classes when parsing the mission file. This may be due to mission disk data you do not have");
599         }
600         Fred_found_unknown_ship_during_parsing = 0;
601
602         for (i=0; i<Num_waypoint_lists; i++) {
603                 wptr = &Waypoint_lists[i];
604                 for (j=0; j<wptr->count; j++){
605                         ob = obj_create(OBJ_WAYPOINT, -1, i * 65536 + j, NULL, &wptr->waypoints[j], 0.0f, OF_RENDERS);
606                 }
607         }
608
609         obj_merge_created_list();
610         objp = GET_FIRST(&obj_used_list);
611         while (objp != END_OF_LIST(&obj_used_list)) {
612                 if (objp->flags & OF_PLAYER_SHIP) {
613                         Assert(objp->type == OBJ_SHIP);
614                         objp->type = OBJ_START;
615 //                      Player_starts++;
616                 }
617
618                 objp = GET_NEXT(objp);
619         }
620
621         for (i=0; i<num_wings; i++) {
622                 for (j=0; j<Wings[i].wave_count; j++) {
623                         ob = Ships[Wings[i].ship_index[j]].objnum;
624                         wing_objects[i][j] = ob;
625                         Ships[Wings[i].ship_index[j]].wingnum = i;
626                         Ships[Wings[i].ship_index[j]].arrival_cue = Locked_sexp_false;
627                 }
628
629                 // fix old ship names for ships in wings if needed
630                 while (j--) {
631                         if ( (Objects[wing_objects[i][j]].type == OBJ_SHIP) || (Objects[wing_objects[i][j]].type == OBJ_START) ) {  // don't change player ship names
632                                 sprintf(name, "%s %d", Wings[i].name, j + 1);
633                                 old_name = Ships[Wings[i].ship_index[j]].ship_name;
634                                 if (stricmp(name, old_name)) {  // need to fix name
635                                         update_sexp_references(old_name, name);
636                                         ai_update_goal_references(REF_TYPE_SHIP, old_name, name);
637                                         for (k=0; k<Num_reinforcements; k++)
638                                                 if (!stricmp(old_name, Reinforcements[k].name)) {
639                                                         Assert(strlen(name) < NAME_LENGTH);
640                                                         strcpy(Reinforcements[k].name, name);
641                                                 }
642
643                                         strcpy(Ships[Wings[i].ship_index[j]].ship_name, name);
644                                 }
645                         }
646                 }
647         }
648
649         generate_weaponry_usage_list(used_pool);
650         for ( j = 0; j < Num_teams; j++ ) {
651                 for (i=0; i<Num_weapon_types; i++) {
652                         Team_data[j].weaponry_pool[i] -= used_pool[i];  // convert weaponry_pool to be extras available beyond the current ships weapons
653                         if (Team_data[j].weaponry_pool[i] < 0)
654                                 Team_data[j].weaponry_pool[i] = 0;
655                 }
656         }
657
658         Assert(Mission_palette >= 0);
659         Assert(Mission_palette <= 98);
660
661         if (The_mission.flags & MISSION_FLAG_SUBSPACE) {
662                 strcpy(name, NOX("gamepalette-subspace"));
663         } else {
664                 strcpy(name, "gamepalette1-01");
665                 // sprintf(name, NOX("gamepalette1-%02d"), Mission_palette + 1);
666         }
667
668         palette_load_table(name);
669
670         // go through all ships and translate their alternate name indices      
671         objp = GET_FIRST(&obj_used_list);
672         while (objp != END_OF_LIST(&obj_used_list)) {
673                 // if this is a ship, check it, and mark its possible alternate name down in the auxiliary array
674                 if(((objp->type == OBJ_SHIP) || (objp->type == OBJ_START)) && (objp->instance >= 0) && (Ships[objp->instance].alt_type_index >= 0)){
675                         mission_parse_lookup_alt_index(Ships[objp->instance].alt_type_index, Fred_alt_names[objp->instance]);
676
677                         // also zero it
678                         Ships[objp->instance].alt_type_index = -1;
679                 }
680
681                 objp = GET_NEXT(objp);
682         }
683
684
685         view_pos = Parse_viewer_pos;
686         view_orient = Parse_viewer_orient;
687         set_modified(0);        
688
689         return 0;
690 }
691
692 //      Editor-level interface to mission load/save.
693
694 // Does nothing now.. Handled by OnOpenDocument and OnSaveDocument.  This is because we
695 // want to avoid using the CArchive for file I/O  -JH
696 void CFREDDoc::Serialize(CArchive& ar)
697 {
698         return;
699 /*  The original Serialize code
700         int             rw_flag;
701         XFILE           *fp;
702 //      CString CSfilename;
703 //      char            filename[128], *tfilename;
704
705         fp = ar.GetFile();
706         rw_flag = ar.IsStoring();
707
708 //      CSfilename = fp->GetFileName();
709 //      tfilename = CSfilename.GetBuffer(16);
710 //      strcpy(filename, tfilename);
711 //      CSfilename.ReleaseBuffer();
712 // -- Don't close this, it gets closed by MFC --        ar.Close();
713
714         cfile_serialize(fp, rw_flag);
715         cfile_serialize_editor(fp, rw_flag);
716 */
717 }
718
719 /////////////////////////////////////////////////////////////////////////////
720 // CFREDDoc diagnostics
721
722 #ifdef _DEBUG
723 void CFREDDoc::AssertValid() const
724 {
725         CDocument::AssertValid();
726 }
727
728 void CFREDDoc::Dump(CDumpContext& dc) const
729 {
730         CDocument::Dump(dc);
731 }
732 #endif //_DEBUG
733
734
735 /////////////////////////////////////////////////////////////////////////////
736 // CFREDDoc commands
737
738 void CFREDDoc::OnEditDelete() 
739 {
740         // TODO: Add your command handler code here
741         
742 }
743
744 void CFREDDoc::OnDuplicate() 
745 {
746         // TODO: Add your command handler code here
747         
748 }
749
750 void CFREDDoc::OnEditCopy() 
751 {
752         // TODO: Add your command handler code here
753         
754 }
755
756 void CFREDDoc::OnEditCut() 
757 {
758         // TODO: Add your command handler code here
759         
760 }
761
762 void CFREDDoc::OnEditPaste() 
763 {
764         // TODO: Add your command handler code here
765         
766 }
767
768 void CFREDDoc::OnEditHold() 
769 {
770         // TODO: Add your command handler code here
771         
772 }
773
774 void CFREDDoc::OnEditFetch() 
775 {
776         // TODO: Add your command handler code here
777         
778 }
779
780 void CFREDDoc::OnEditUndo() 
781 {
782         // TODO: Add your command handler code here
783         
784 }
785
786 void CFREDDoc::OnFilePreferences() 
787 {
788         CPrefsDlg dlg;
789         // Initialize dialog data
790         dlg.m_ConfirmDeleting = confirm_deleting;
791         dlg.m_ShowCapitalShips = show_capital_ships;
792         dlg.m_ShowElevations = show_elevations;
793         dlg.m_ShowFighters = show_fighters;
794         dlg.m_ShowGrid = show_grid;
795         dlg.m_ShowMiscObjects = show_misc_objects;
796         dlg.m_ShowPlanets = show_planets;
797         dlg.m_ShowWaypoints = show_waypoints;
798         dlg.m_ShowStarfield = show_starfield;
799
800         // Invoke the dialog box
801         if (dlg.DoModal() == IDOK)
802         {
803                 // retrieve the dialog data
804                 confirm_deleting = dlg.m_ConfirmDeleting;
805                 show_capital_ships = dlg.m_ShowCapitalShips;
806                 show_elevations = dlg.m_ShowElevations;
807                 show_fighters = dlg.m_ShowFighters;
808                 show_grid = dlg.m_ShowGrid;
809                 show_misc_objects = dlg.m_ShowMiscObjects;
810                 show_planets = dlg.m_ShowPlanets;
811                 show_waypoints = dlg.m_ShowWaypoints;
812                 show_starfield = dlg.m_ShowStarfield;
813         }
814
815 }
816
817 // initialize (clear out) the mission, so it's empty and ready to use.
818 void CFREDDoc::editor_init_mission()
819 {
820         reset_mission();
821         SetModifiedFlag(FALSE);
822 }
823 /*
824 void CFREDDoc::OnFileNew() 
825 {
826         // If mission has been modified, offer to save before continuing.
827         while (IsModified()) {
828                 int     rval;
829                 
830                 rval = MessageBox(NULL,
831                         "You have not saved your work.\n(Which isn't surprising...)\nSave it now?",
832                         "Creating New Mission",
833                         MB_YESNOCANCEL + MB_ICONEXCLAMATION);
834
835                 if (rval == IDYES) {
836                         OnFileSave();
837                 } else if (rval == IDCANCEL)
838                         return;
839                 else if (rval == IDNO)
840                         break;
841         }
842
843         editor_init_mission();
844
845 }
846 */
847 void CFREDDoc::UpdateStatus(int flags)
848 {
849         if (FREDDoc_ptr)
850                 if (flags & US_WORLD_CHANGED)
851                         FREDDoc_ptr->SetModifiedFlag();
852 }
853
854 void CFREDDoc::OnEditClearAll()
855 {
856    DeleteContents();
857 }
858   
859   
860 void CFREDDoc::DeleteContents()
861 {
862         editor_init_mission();
863 }
864
865 void set_modified(BOOL arg)
866 {
867         Local_modified = arg;
868         FREDDoc_ptr->SetModifiedFlag(arg);
869 }
870
871 // call this if an unknown ship class was discovered during parsing. Sets up a warning message for players
872 void fred_notify_unknown_ship_during_parse()
873 {
874         Fred_found_unknown_ship_during_parsing = 1;
875 }
876
877  //////////////////////////////////////////////////////////////////////////
878 //
879 // Below is old, obsolete code, kept around just in case it might be found
880 // useful some time in the future for something.
881 //
882  //////////////////////////////////////////////////////////////////////////
883
884 #if 0
885 #define SerializeFloat(fp, mode, f) if (mode == 1) fp->Write(&f, sizeof(float)); else fp->Read(&f, sizeof(float))
886 #define SerializeInt(fp, mode, f) if (mode == 1) fp->Write(&f, sizeof(int)); else fp->Read(&f, sizeof(int))
887
888 void SerializeVector(XFILE *fp, int mode, vector *v)
889 {
890         SerializeFloat(fp, mode, v->x);
891         SerializeFloat(fp, mode, v->y);
892         SerializeFloat(fp, mode, v->z);
893 }
894
895 void SerializeMatrix(XFILE *fp, int mode, matrix *m)
896 {
897         SerializeVector(fp, mode, &m->rvec);
898         SerializeVector(fp, mode, &m->uvec);
899         SerializeVector(fp, mode, &m->fvec);
900 }
901
902 void SerializePhysicsInfo(XFILE *fp, int mode, physics_info *pi)
903 {
904         SerializeFloat(fp, mode, pi->mass);
905         SerializeFloat(fp, mode, pi->drag);
906         SerializeVector(fp, mode, &pi->max_thrust);
907         SerializeVector(fp, mode, &pi->max_rotthrust);
908         SerializeFloat(fp, mode, pi->turnroll);
909         SerializeInt(fp, mode, pi->flags);
910         SerializeVector(fp, mode, &pi->velocity);
911         SerializeVector(fp, mode, &pi->rotvel);
912         SerializeVector(fp, mode, &pi->thrust);
913         SerializeVector(fp, mode, &pi->rotthrust);
914 }
915
916 /////////////////////////////////////////////////////////////////////////////
917 // CFREDDoc serialization
918 void SerializeObject(XFILE *fp, int mode, object *objp)
919 {
920         SerializeInt(fp, mode, objp->signature);
921         SerializeInt(fp, mode, objp->type);
922         SerializeInt(fp, mode, objp->parent);
923         SerializeInt(fp, mode, objp->parent_sig);
924         SerializeInt(fp, mode, objp->parent_type);
925         SerializeInt(fp, mode, objp->instance);
926         SerializeInt(fp, mode, objp->flags);
927         SerializeFloat(fp, mode, objp->radius);
928 //      SerializeInt(fp, mode, objp->wing);
929         SerializePhysicsInfo(fp, mode, &objp->phys_info);
930         SerializeVector(fp, mode, &objp->pos);
931         SerializeMatrix(fp, mode, &objp->orient);
932 }
933
934 void SerializeAI(XFILE *fp, int mode, ai_info *aip)
935 {
936         SerializeInt(fp, mode, aip->shipnum);
937         SerializeInt(fp, mode, aip->type);
938         SerializeInt(fp, mode, aip->wing);
939 //MWA --        SerializeInt(fp, mode, aip->current_waypoint);
940 }
941
942 void SerializeShip(XFILE *fp, int mode, ship *shipp)
943 {
944         SerializeInt(fp, mode, shipp->objnum);
945         SerializeInt(fp, mode, shipp->ai_index);
946         SerializeInt(fp, mode, shipp->subtype);
947         SerializeInt(fp, mode, shipp->modelnum);
948         SerializeInt(fp, mode, shipp->hits);
949         SerializeInt(fp, mode, shipp->dying);
950 }
951
952 void SerializeGrid(XFILE *fp, int mode, grid *gridp)
953 {
954         int     i;
955
956         SerializeInt(fp, mode, gridp->nrows);
957         SerializeInt(fp, mode, gridp->ncols);
958         SerializeMatrix(fp, mode, &gridp->gmatrix);
959         SerializePhysicsInfo(fp, mode, &gridp->physics);
960         SerializeFloat(fp, mode, gridp->square_size);
961         SerializeFloat(fp, mode, gridp->planeD);
962         
963         for (i=0; i<MAX_GRID_POINTS; i++)
964                 SerializeVector(fp, mode, &gridp->gpoints[i]);
965
966 }
967
968 void cfile_serialize(XFILE *fp, int flag)
969 {
970         int     i;
971         int     highest_object_index = 0, highest_ship_index = 0, highest_ai_index = 0;
972
973         Assert((flag == 0) || (flag == 1));
974
975 //      fp = cfopen(filename, flag ? "wb" : "rb");
976 //      if (!fp)
977 //              MessageBox(NULL, strerror(errno), "File Open Error!", MB_ICONSTOP);
978
979         //      Find highest used object if writing.
980         if (flag == 1) {
981                 for (i=MAX_OBJECTS-1; i>0; i--)
982                         if (Objects[i].type != OBJ_NONE) {
983                                 highest_object_index = i;
984                                 break;
985                         }
986         }
987
988         if (flag == 0) {
989                 num_ships = 0;
990                 num_objects = 0;
991         }
992
993         SerializeInt(fp, flag, highest_object_index);
994
995         for (i=1; i<=highest_object_index; i++) {
996                 SerializeObject(fp, flag, &Objects[i]);
997                 if (flag == 0)
998                         if (Objects[i].type != OBJ_NONE)
999                                 num_objects++;
1000         }
1001
1002         //      Read/write ships
1003         if (flag == 1) {
1004                 for (i=MAX_SHIPS-1; i>0; i--)
1005                         if (Ships[i].objnum) {
1006                                 highest_ship_index = i;
1007                                 break;
1008                         }
1009         }
1010
1011         SerializeInt(fp, flag, highest_ship_index);
1012
1013         for (i=1; i<=highest_ship_index; i++) {
1014                 SerializeShip(fp, flag, &Ships[i]);
1015                 if (flag == 0)
1016                         if (Ships[i].objnum)
1017                                 num_ships++;
1018         }
1019
1020         // Read/write AI info
1021         if (flag == 1) {
1022                 for (i=MAX_AI_INFO-1; i>0; i--)
1023                         if (Ai_info[i].shipnum) {
1024                                 highest_ai_index = i;
1025                                 break;
1026                         }
1027         }
1028
1029         SerializeInt(fp, flag, highest_ai_index);
1030         
1031         for (i=1; i<=highest_ai_index; i++)
1032                 SerializeAI(fp, flag, &Ai_info[i]);
1033 }
1034
1035 void cfile_serialize_editor(XFILE *fp, int flag)
1036 {
1037         //      Editor only stuff
1038         SerializeMatrix(fp, flag, &view_orient);
1039         SerializeVector(fp, flag, &view_pos);
1040         
1041         SerializeInt(fp, flag, Control_mode);
1042         SerializeInt(fp, flag, cur_object_index);
1043         SerializeInt(fp, flag, cur_wing);
1044
1045         SerializeGrid(fp, flag, The_grid);
1046
1047 }
1048 #endif
1049