2 * $Logfile: /Freespace2/code/Fred2/FREDDoc.cpp $
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.
13 * Revision 1.2 2002/05/07 03:16:44 theoddone33
14 * The Great Newline Fix
16 * Revision 1.1.1.1 2002/05/03 03:28:08 root
20 * 7 7/23/99 2:12p Jamesa
21 * fix gamepalette request
23 * 6 5/20/99 6:59p Dave
24 * Added alternate type names for ships. Changed swarm missile table
27 * 5 10/29/98 6:49p Dave
28 * Finished up Fred support for externalizing mission and campaign files.
30 * 4 10/29/98 12:50p Dave
31 * Intermediate checkin for fred hash table stuff.
33 * 3 10/28/98 11:30a Dave
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.
41 * 1 10/07/98 3:00p Dave
43 * 94 9/21/98 8:46p Dave
44 * Put in special check in fred for identifying unknown ships.
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
51 * 92 8/13/98 9:53a Hoffoss
52 * Fixed bug where loading mission doesn't update palette used for that
55 * 91 4/30/98 8:23p John
56 * Fixed some bugs with Fred caused by my new cfile code.
58 * 90 4/28/98 2:13p Hoffoss
59 * Added code to help keep invalid player ship types from existing in
62 * 89 4/10/98 10:22p Mike
63 * Fix bug in parsing medals.tbl.
65 * 88 4/10/98 4:51p Hoffoss
66 * Made several changes related to tooltips.
68 * 87 3/25/98 4:14p Hoffoss
69 * Split ship editor up into ship editor and a misc dialog, which tracks
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).
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.
81 * 84 1/28/98 11:00a Johnson
82 * Fixed parse code for medals table (new field)
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
90 * 82 11/05/97 10:19p Mike
91 * Comment out call to parse_medal_tbl() to allow Fred to run.
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
98 * 80 11/04/97 4:50p Hoffoss
99 * Made mission loading failure display an error message stating so.
101 * 79 11/04/97 4:33p Hoffoss
102 * Made saving keep the current briefing state intact.
104 * 78 10/20/97 1:20p Hoffoss
105 * Fixed bug loading mission file after a save.
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.
111 * 76 10/08/97 11:47a Hoffoss
112 * Added better fred handling of Weaponry_pool.
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.
118 * 74 9/18/97 11:52a Johnson
119 * Fixed bug with player starts count being incorrect.
121 * 73 9/17/97 11:58a Hoffoss
122 * Fixed mission load code to properly set arrival cues to player ships.
124 * 72 9/17/97 10:32a Allender
125 * fixup references to old "Player" ship to new real shipname
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.
131 * 70 9/09/97 6:50p Hoffoss
132 * Fixed bug with mission saving.
134 * 69 9/09/97 1:55p Hoffoss
135 * Fixed bug with saving missions that are readonly.
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
142 * 67 8/20/97 5:45p Hoffoss
143 * Fixed bugs with mission name in sexp in non campaign mode in Fred.
145 * 66 8/20/97 4:03p Hoffoss
146 * Fixed bug in reload current mission (revert).
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.
152 * 64 8/16/97 9:24p Hoffoss
153 * Added support for team of players in multiplayer.
155 * 63 8/16/97 6:44p Hoffoss
156 * Changes to allow any player to be in a wing.
158 * 62 8/16/97 3:53p Hoffoss
159 * Added OF_NO_SHIELDS define and support in Fred and mission load/save.
161 * 61 8/13/97 5:49p Hoffoss
162 * Fixed bugs, made additions.
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.
168 * 59 8/12/97 1:55a Hoffoss
169 * Made extensive changes to object reference checking and handling for
170 * object deletion call.
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
177 * 57 7/30/97 2:10p Hoffoss
178 * Made new missions that have never been saved yet save backups.
180 * 56 7/28/97 3:50p Hoffoss
181 * Added global error checker call to mission load and save.
183 * 55 7/10/97 2:32p Hoffoss
184 * Made message editor dialog box modeless.
186 * 54 7/08/97 11:35a Hoffoss
187 * Fixed bug in initial orders, and also redid how shields are handled by
190 * 53 6/26/97 5:18p Hoffoss
191 * Major rework of briefing editor functionality.
193 * 52 6/24/97 10:22a Hoffoss
194 * Added a forced update for briefing info prior to a mission save.
196 * 51 6/19/97 11:34a Hoffoss
197 * Fixed bug in auto mission convertion.
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.
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.
207 * 48 6/11/97 10:02a Comet
210 * 47 6/09/97 4:57p Hoffoss
211 * Added autosave and undo to Fred.
213 * 46 6/05/97 6:10p Hoffoss
214 * Added features: Autosaving, object hiding. Also fixed some minor bugs.
216 * 45 5/23/97 3:18p Hoffoss
217 * Event editor made modeless.
219 * 44 5/21/97 5:42p Hoffoss
220 * Added features requested on Todo list.
222 * 43 5/01/97 4:14p Hoffoss
223 * Viewer position saved to missions now for Fred restoration of state.
225 * 42 4/25/97 3:53p Hoffoss
226 * Fixed bug in weapons save/load and moved weapon look up function to
229 * 41 4/25/97 3:28p Mike
230 * Making shield multi-part.
232 * 40 4/21/97 5:02p Hoffoss
233 * Player/player status editing supported, and both saved and loaded from
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
241 * 38 3/04/97 6:27p Hoffoss
242 * Changes to Fred to handle new wing structure.
244 * 37 2/28/97 11:31a Hoffoss
245 * Implemented modeless dialog saving and restoring, and changed some
248 * 36 2/27/97 3:09p Allender
249 * major wing structure enhancement. simplified wing code. All around
250 * better wing support
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.
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.
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.
264 * 13 1/30/97 2:24p Hoffoss
265 * Added remaining mission file structures and implemented load/save of
276 #include "fredview.h"
277 #include "prefsdlg.h"
286 #include "missionparse.h"
287 #include "missiongoals.h"
288 #include "missionsave.h"
290 #include "management.h"
291 #include "linklist.h"
292 #include "fredrender.h"
294 #include "eventeditor.h"
296 #include "messageeditordlg.h"
300 extern int num_objects;
303 #define new DEBUG_NEW
305 static char THIS_FILE[] = __FILE__;
308 // In editor mode, use class CFile, in game, use CFILE (our file)
311 /////////////////////////////////////////////////////////////////////////////
314 IMPLEMENT_DYNCREATE(CFREDDoc, CDocument)
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)
330 /////////////////////////////////////////////////////////////////////////////
331 // CFREDDoc construction/destruction
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;
341 extern int Fred_found_unknown_ship_during_parsing;
348 confirm_deleting = TRUE;
349 show_capital_ships = TRUE;
350 show_elevations = TRUE;
351 show_fighters = TRUE;
353 show_misc_objects = TRUE;
355 show_waypoints = TRUE;
356 show_starfield = TRUE;
358 for (i=0; i<BACKUP_DEPTH; i++)
359 undo_desc[i].Empty();
362 CFREDDoc::~CFREDDoc()
366 BOOL CFREDDoc::OnNewDocument()
368 if (!CDocument::OnNewDocument())
371 // TODO: add reinitialization code here
372 confirm_deleting = TRUE;
373 show_capital_ships = FALSE;
374 show_elevations = TRUE;
375 show_fighters = TRUE;
377 show_misc_objects = TRUE;
378 show_planets = FALSE;
379 show_waypoints = TRUE;
380 show_starfield = FALSE;
381 // (SDI documents will reuse this document)
386 // read in a new mission file from disk
387 BOOL CFREDDoc::OnOpenDocument(LPCTSTR pathname)
393 strcpy(mission_pathname, pathname);
396 Briefing_dialog->icon_select(-1); // clean things up first
398 len = strlen(mission_pathname);
399 strcpy(name, mission_pathname);
400 if (name[len - 4] == '.')
403 name[len] = 0; // drop extension
406 if ((name[i] == '\\') || (name[i] == ':'))
409 strcpy(Mission_filename, name + i + 1);
410 // for (i=1; i<=BACKUP_DEPTH; i++) {
411 // sprintf(name + len, ".%.3d", i);
415 if (load_mission(mission_pathname)) {
416 *Mission_filename = 0;
420 Fred_view_wnd->global_error_check();
426 // save mission to a file
427 BOOL CFREDDoc::OnSaveDocument(LPCTSTR pathname)
429 CFred_mission_save save;
435 len = strlen(pathname);
436 strcpy(name, pathname);
437 if (name[len - 4] == '.')
440 name[len] = 0; // drop extension
442 if ((name[len] == '\\') || (name[len] == ':'))
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();
452 if (Event_editor_dlg)
453 Fred_main_wnd->MessageBox("Event editor dialog is still open, so changes there won't be saved");
455 if (Message_editor_dlg)
456 Fred_main_wnd->MessageBox("Message editor dialog is still open, so changes there won't be saved");
458 fp = fopen(pathname, "r");
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");
468 if (save.save_mission_file((char *) pathname)) {
469 Fred_main_wnd->MessageBox("An error occured while saving!", NULL, MB_OK | MB_ICONEXCLAMATION);
473 SetModifiedFlag(FALSE);
474 if (load_mission((char *) pathname))
475 Error(LOCATION, "Failed attempting to reload mission after saving. Report this bug now!");
477 if (Briefing_dialog) {
478 Briefing_dialog->restore_editor_state();
479 Briefing_dialog->update_data(1);
483 // return CDocument::OnSaveDocument(pathname);
486 int CFREDDoc::check_undo()
495 strcpy(name, MISSION_BACKUP_NAME);
496 strcat(name, ".002");
497 fp = fopen(name, "r");
506 int CFREDDoc::autosave(char *desc)
509 CFred_mission_save save;
512 if (Autosave_disabled)
516 Briefing_dialog->update_data(1);
518 if (save.autosave_mission_file(MISSION_BACKUP_NAME)) {
519 Undo_count = Undo_available = 0;
523 for (i=BACKUP_DEPTH; i>1; i--)
524 undo_desc[i] = undo_desc[i - 1];
529 undo_desc[1].Empty();
536 int CFREDDoc::autoload()
538 char name[256], backup_name[256];
542 strcpy(name, MISSION_BACKUP_NAME);
543 strcat(name, ".002");
544 fp = fopen(name, "r");
550 Briefing_dialog->icon_select(-1); // clean things up first
552 // editor_init_mission();
553 r = load_mission(name);
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);
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];
573 // read in a new mission file from disk
574 int CFREDDoc::load_mission(char *pathname)
576 char name[512], *old_name;
578 int used_pool[MAX_WEAPON_TYPES];
582 Parse_viewer_pos = view_pos;
583 Parse_viewer_orient = view_orient;
585 // activate the localizer hash table
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();
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");
600 Fred_found_unknown_ship_during_parsing = 0;
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);
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;
618 objp = GET_NEXT(objp);
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;
629 // fix old ship names for ships in wings if needed
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);
643 strcpy(Ships[Wings[i].ship_index[j]].ship_name, name);
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;
658 Assert(Mission_palette >= 0);
659 Assert(Mission_palette <= 98);
661 if (The_mission.flags & MISSION_FLAG_SUBSPACE) {
662 strcpy(name, NOX("gamepalette-subspace"));
664 strcpy(name, "gamepalette1-01");
665 // sprintf(name, NOX("gamepalette1-%02d"), Mission_palette + 1);
668 palette_load_table(name);
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]);
678 Ships[objp->instance].alt_type_index = -1;
681 objp = GET_NEXT(objp);
685 view_pos = Parse_viewer_pos;
686 view_orient = Parse_viewer_orient;
692 // Editor-level interface to mission load/save.
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)
699 /* The original Serialize code
702 // CString CSfilename;
703 // char filename[128], *tfilename;
706 rw_flag = ar.IsStoring();
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();
714 cfile_serialize(fp, rw_flag);
715 cfile_serialize_editor(fp, rw_flag);
719 /////////////////////////////////////////////////////////////////////////////
720 // CFREDDoc diagnostics
723 void CFREDDoc::AssertValid() const
725 CDocument::AssertValid();
728 void CFREDDoc::Dump(CDumpContext& dc) const
735 /////////////////////////////////////////////////////////////////////////////
738 void CFREDDoc::OnEditDelete()
740 // TODO: Add your command handler code here
744 void CFREDDoc::OnDuplicate()
746 // TODO: Add your command handler code here
750 void CFREDDoc::OnEditCopy()
752 // TODO: Add your command handler code here
756 void CFREDDoc::OnEditCut()
758 // TODO: Add your command handler code here
762 void CFREDDoc::OnEditPaste()
764 // TODO: Add your command handler code here
768 void CFREDDoc::OnEditHold()
770 // TODO: Add your command handler code here
774 void CFREDDoc::OnEditFetch()
776 // TODO: Add your command handler code here
780 void CFREDDoc::OnEditUndo()
782 // TODO: Add your command handler code here
786 void CFREDDoc::OnFilePreferences()
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;
800 // Invoke the dialog box
801 if (dlg.DoModal() == IDOK)
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;
817 // initialize (clear out) the mission, so it's empty and ready to use.
818 void CFREDDoc::editor_init_mission()
821 SetModifiedFlag(FALSE);
824 void CFREDDoc::OnFileNew()
826 // If mission has been modified, offer to save before continuing.
827 while (IsModified()) {
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);
837 } else if (rval == IDCANCEL)
839 else if (rval == IDNO)
843 editor_init_mission();
847 void CFREDDoc::UpdateStatus(int flags)
850 if (flags & US_WORLD_CHANGED)
851 FREDDoc_ptr->SetModifiedFlag();
854 void CFREDDoc::OnEditClearAll()
860 void CFREDDoc::DeleteContents()
862 editor_init_mission();
865 void set_modified(BOOL arg)
867 Local_modified = arg;
868 FREDDoc_ptr->SetModifiedFlag(arg);
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()
874 Fred_found_unknown_ship_during_parsing = 1;
877 //////////////////////////////////////////////////////////////////////////
879 // Below is old, obsolete code, kept around just in case it might be found
880 // useful some time in the future for something.
882 //////////////////////////////////////////////////////////////////////////
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))
888 void SerializeVector(XFILE *fp, int mode, vector *v)
890 SerializeFloat(fp, mode, v->x);
891 SerializeFloat(fp, mode, v->y);
892 SerializeFloat(fp, mode, v->z);
895 void SerializeMatrix(XFILE *fp, int mode, matrix *m)
897 SerializeVector(fp, mode, &m->rvec);
898 SerializeVector(fp, mode, &m->uvec);
899 SerializeVector(fp, mode, &m->fvec);
902 void SerializePhysicsInfo(XFILE *fp, int mode, physics_info *pi)
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);
916 /////////////////////////////////////////////////////////////////////////////
917 // CFREDDoc serialization
918 void SerializeObject(XFILE *fp, int mode, object *objp)
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);
934 void SerializeAI(XFILE *fp, int mode, ai_info *aip)
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);
942 void SerializeShip(XFILE *fp, int mode, ship *shipp)
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);
952 void SerializeGrid(XFILE *fp, int mode, grid *gridp)
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);
963 for (i=0; i<MAX_GRID_POINTS; i++)
964 SerializeVector(fp, mode, &gridp->gpoints[i]);
968 void cfile_serialize(XFILE *fp, int flag)
971 int highest_object_index = 0, highest_ship_index = 0, highest_ai_index = 0;
973 Assert((flag == 0) || (flag == 1));
975 // fp = cfopen(filename, flag ? "wb" : "rb");
977 // MessageBox(NULL, strerror(errno), "File Open Error!", MB_ICONSTOP);
979 // Find highest used object if writing.
981 for (i=MAX_OBJECTS-1; i>0; i--)
982 if (Objects[i].type != OBJ_NONE) {
983 highest_object_index = i;
993 SerializeInt(fp, flag, highest_object_index);
995 for (i=1; i<=highest_object_index; i++) {
996 SerializeObject(fp, flag, &Objects[i]);
998 if (Objects[i].type != OBJ_NONE)
1004 for (i=MAX_SHIPS-1; i>0; i--)
1005 if (Ships[i].objnum) {
1006 highest_ship_index = i;
1011 SerializeInt(fp, flag, highest_ship_index);
1013 for (i=1; i<=highest_ship_index; i++) {
1014 SerializeShip(fp, flag, &Ships[i]);
1016 if (Ships[i].objnum)
1020 // Read/write AI info
1022 for (i=MAX_AI_INFO-1; i>0; i--)
1023 if (Ai_info[i].shipnum) {
1024 highest_ai_index = i;
1029 SerializeInt(fp, flag, highest_ai_index);
1031 for (i=1; i<=highest_ai_index; i++)
1032 SerializeAI(fp, flag, &Ai_info[i]);
1035 void cfile_serialize_editor(XFILE *fp, int flag)
1037 // Editor only stuff
1038 SerializeMatrix(fp, flag, &view_orient);
1039 SerializeVector(fp, flag, &view_pos);
1041 SerializeInt(fp, flag, Control_mode);
1042 SerializeInt(fp, flag, cur_object_index);
1043 SerializeInt(fp, flag, cur_wing);
1045 SerializeGrid(fp, flag, The_grid);