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