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.1 2002/05/03 03:28:08 root
17 * 7 7/23/99 2:12p Jamesa
18 * fix gamepalette request
20 * 6 5/20/99 6:59p Dave
21 * Added alternate type names for ships. Changed swarm missile table
24 * 5 10/29/98 6:49p Dave
25 * Finished up Fred support for externalizing mission and campaign files.
27 * 4 10/29/98 12:50p Dave
28 * Intermediate checkin for fred hash table stuff.
30 * 3 10/28/98 11:30a Dave
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.
38 * 1 10/07/98 3:00p Dave
40 * 94 9/21/98 8:46p Dave
41 * Put in special check in fred for identifying unknown ships.
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
48 * 92 8/13/98 9:53a Hoffoss
49 * Fixed bug where loading mission doesn't update palette used for that
52 * 91 4/30/98 8:23p John
53 * Fixed some bugs with Fred caused by my new cfile code.
55 * 90 4/28/98 2:13p Hoffoss
56 * Added code to help keep invalid player ship types from existing in
59 * 89 4/10/98 10:22p Mike
60 * Fix bug in parsing medals.tbl.
62 * 88 4/10/98 4:51p Hoffoss
63 * Made several changes related to tooltips.
65 * 87 3/25/98 4:14p Hoffoss
66 * Split ship editor up into ship editor and a misc dialog, which tracks
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).
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.
78 * 84 1/28/98 11:00a Johnson
79 * Fixed parse code for medals table (new field)
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
87 * 82 11/05/97 10:19p Mike
88 * Comment out call to parse_medal_tbl() to allow Fred to run.
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
95 * 80 11/04/97 4:50p Hoffoss
96 * Made mission loading failure display an error message stating so.
98 * 79 11/04/97 4:33p Hoffoss
99 * Made saving keep the current briefing state intact.
101 * 78 10/20/97 1:20p Hoffoss
102 * Fixed bug loading mission file after a save.
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.
108 * 76 10/08/97 11:47a Hoffoss
109 * Added better fred handling of Weaponry_pool.
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.
115 * 74 9/18/97 11:52a Johnson
116 * Fixed bug with player starts count being incorrect.
118 * 73 9/17/97 11:58a Hoffoss
119 * Fixed mission load code to properly set arrival cues to player ships.
121 * 72 9/17/97 10:32a Allender
122 * fixup references to old "Player" ship to new real shipname
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.
128 * 70 9/09/97 6:50p Hoffoss
129 * Fixed bug with mission saving.
131 * 69 9/09/97 1:55p Hoffoss
132 * Fixed bug with saving missions that are readonly.
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
139 * 67 8/20/97 5:45p Hoffoss
140 * Fixed bugs with mission name in sexp in non campaign mode in Fred.
142 * 66 8/20/97 4:03p Hoffoss
143 * Fixed bug in reload current mission (revert).
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.
149 * 64 8/16/97 9:24p Hoffoss
150 * Added support for team of players in multiplayer.
152 * 63 8/16/97 6:44p Hoffoss
153 * Changes to allow any player to be in a wing.
155 * 62 8/16/97 3:53p Hoffoss
156 * Added OF_NO_SHIELDS define and support in Fred and mission load/save.
158 * 61 8/13/97 5:49p Hoffoss
159 * Fixed bugs, made additions.
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.
165 * 59 8/12/97 1:55a Hoffoss
166 * Made extensive changes to object reference checking and handling for
167 * object deletion call.
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
174 * 57 7/30/97 2:10p Hoffoss
175 * Made new missions that have never been saved yet save backups.
177 * 56 7/28/97 3:50p Hoffoss
178 * Added global error checker call to mission load and save.
180 * 55 7/10/97 2:32p Hoffoss
181 * Made message editor dialog box modeless.
183 * 54 7/08/97 11:35a Hoffoss
184 * Fixed bug in initial orders, and also redid how shields are handled by
187 * 53 6/26/97 5:18p Hoffoss
188 * Major rework of briefing editor functionality.
190 * 52 6/24/97 10:22a Hoffoss
191 * Added a forced update for briefing info prior to a mission save.
193 * 51 6/19/97 11:34a Hoffoss
194 * Fixed bug in auto mission convertion.
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.
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.
204 * 48 6/11/97 10:02a Comet
207 * 47 6/09/97 4:57p Hoffoss
208 * Added autosave and undo to Fred.
210 * 46 6/05/97 6:10p Hoffoss
211 * Added features: Autosaving, object hiding. Also fixed some minor bugs.
213 * 45 5/23/97 3:18p Hoffoss
214 * Event editor made modeless.
216 * 44 5/21/97 5:42p Hoffoss
217 * Added features requested on Todo list.
219 * 43 5/01/97 4:14p Hoffoss
220 * Viewer position saved to missions now for Fred restoration of state.
222 * 42 4/25/97 3:53p Hoffoss
223 * Fixed bug in weapons save/load and moved weapon look up function to
226 * 41 4/25/97 3:28p Mike
227 * Making shield multi-part.
229 * 40 4/21/97 5:02p Hoffoss
230 * Player/player status editing supported, and both saved and loaded from
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
238 * 38 3/04/97 6:27p Hoffoss
239 * Changes to Fred to handle new wing structure.
241 * 37 2/28/97 11:31a Hoffoss
242 * Implemented modeless dialog saving and restoring, and changed some
245 * 36 2/27/97 3:09p Allender
246 * major wing structure enhancement. simplified wing code. All around
247 * better wing support
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.
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.
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.
261 * 13 1/30/97 2:24p Hoffoss
262 * Added remaining mission file structures and implemented load/save of
273 #include "fredview.h"
274 #include "prefsdlg.h"
283 #include "missionparse.h"
284 #include "missiongoals.h"
285 #include "missionsave.h"
287 #include "management.h"
288 #include "linklist.h"
289 #include "fredrender.h"
291 #include "eventeditor.h"
293 #include "messageeditordlg.h"
297 extern int num_objects;
300 #define new DEBUG_NEW
302 static char THIS_FILE[] = __FILE__;
305 // In editor mode, use class CFile, in game, use CFILE (our file)
308 /////////////////////////////////////////////////////////////////////////////
311 IMPLEMENT_DYNCREATE(CFREDDoc, CDocument)
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)
327 /////////////////////////////////////////////////////////////////////////////
328 // CFREDDoc construction/destruction
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;
338 extern int Fred_found_unknown_ship_during_parsing;
345 confirm_deleting = TRUE;
346 show_capital_ships = TRUE;
347 show_elevations = TRUE;
348 show_fighters = TRUE;
350 show_misc_objects = TRUE;
352 show_waypoints = TRUE;
353 show_starfield = TRUE;
355 for (i=0; i<BACKUP_DEPTH; i++)
356 undo_desc[i].Empty();
359 CFREDDoc::~CFREDDoc()
363 BOOL CFREDDoc::OnNewDocument()
365 if (!CDocument::OnNewDocument())
368 // TODO: add reinitialization code here
369 confirm_deleting = TRUE;
370 show_capital_ships = FALSE;
371 show_elevations = TRUE;
372 show_fighters = TRUE;
374 show_misc_objects = TRUE;
375 show_planets = FALSE;
376 show_waypoints = TRUE;
377 show_starfield = FALSE;
378 // (SDI documents will reuse this document)
383 // read in a new mission file from disk
384 BOOL CFREDDoc::OnOpenDocument(LPCTSTR pathname)
390 strcpy(mission_pathname, pathname);
393 Briefing_dialog->icon_select(-1); // clean things up first
395 len = strlen(mission_pathname);
396 strcpy(name, mission_pathname);
397 if (name[len - 4] == '.')
400 name[len] = 0; // drop extension
403 if ((name[i] == '\\') || (name[i] == ':'))
406 strcpy(Mission_filename, name + i + 1);
407 // for (i=1; i<=BACKUP_DEPTH; i++) {
408 // sprintf(name + len, ".%.3d", i);
412 if (load_mission(mission_pathname)) {
413 *Mission_filename = 0;
417 Fred_view_wnd->global_error_check();
423 // save mission to a file
424 BOOL CFREDDoc::OnSaveDocument(LPCTSTR pathname)
426 CFred_mission_save save;
432 len = strlen(pathname);
433 strcpy(name, pathname);
434 if (name[len - 4] == '.')
437 name[len] = 0; // drop extension
439 if ((name[len] == '\\') || (name[len] == ':'))
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();
449 if (Event_editor_dlg)
450 Fred_main_wnd->MessageBox("Event editor dialog is still open, so changes there won't be saved");
452 if (Message_editor_dlg)
453 Fred_main_wnd->MessageBox("Message editor dialog is still open, so changes there won't be saved");
455 fp = fopen(pathname, "r");
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");
465 if (save.save_mission_file((char *) pathname)) {
466 Fred_main_wnd->MessageBox("An error occured while saving!", NULL, MB_OK | MB_ICONEXCLAMATION);
470 SetModifiedFlag(FALSE);
471 if (load_mission((char *) pathname))
472 Error(LOCATION, "Failed attempting to reload mission after saving. Report this bug now!");
474 if (Briefing_dialog) {
475 Briefing_dialog->restore_editor_state();
476 Briefing_dialog->update_data(1);
480 // return CDocument::OnSaveDocument(pathname);
483 int CFREDDoc::check_undo()
492 strcpy(name, MISSION_BACKUP_NAME);
493 strcat(name, ".002");
494 fp = fopen(name, "r");
503 int CFREDDoc::autosave(char *desc)
506 CFred_mission_save save;
509 if (Autosave_disabled)
513 Briefing_dialog->update_data(1);
515 if (save.autosave_mission_file(MISSION_BACKUP_NAME)) {
516 Undo_count = Undo_available = 0;
520 for (i=BACKUP_DEPTH; i>1; i--)
521 undo_desc[i] = undo_desc[i - 1];
526 undo_desc[1].Empty();
533 int CFREDDoc::autoload()
535 char name[256], backup_name[256];
539 strcpy(name, MISSION_BACKUP_NAME);
540 strcat(name, ".002");
541 fp = fopen(name, "r");
547 Briefing_dialog->icon_select(-1); // clean things up first
549 // editor_init_mission();
550 r = load_mission(name);
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);
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];
570 // read in a new mission file from disk
571 int CFREDDoc::load_mission(char *pathname)
573 char name[512], *old_name;
575 int used_pool[MAX_WEAPON_TYPES];
579 Parse_viewer_pos = view_pos;
580 Parse_viewer_orient = view_orient;
582 // activate the localizer hash table
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();
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");
597 Fred_found_unknown_ship_during_parsing = 0;
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);
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;
615 objp = GET_NEXT(objp);
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;
626 // fix old ship names for ships in wings if needed
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);
640 strcpy(Ships[Wings[i].ship_index[j]].ship_name, name);
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;
655 Assert(Mission_palette >= 0);
656 Assert(Mission_palette <= 98);
658 if (The_mission.flags & MISSION_FLAG_SUBSPACE) {
659 strcpy(name, NOX("gamepalette-subspace"));
661 strcpy(name, "gamepalette1-01");
662 // sprintf(name, NOX("gamepalette1-%02d"), Mission_palette + 1);
665 palette_load_table(name);
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]);
675 Ships[objp->instance].alt_type_index = -1;
678 objp = GET_NEXT(objp);
682 view_pos = Parse_viewer_pos;
683 view_orient = Parse_viewer_orient;
689 // Editor-level interface to mission load/save.
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)
696 /* The original Serialize code
699 // CString CSfilename;
700 // char filename[128], *tfilename;
703 rw_flag = ar.IsStoring();
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();
711 cfile_serialize(fp, rw_flag);
712 cfile_serialize_editor(fp, rw_flag);
716 /////////////////////////////////////////////////////////////////////////////
717 // CFREDDoc diagnostics
720 void CFREDDoc::AssertValid() const
722 CDocument::AssertValid();
725 void CFREDDoc::Dump(CDumpContext& dc) const
732 /////////////////////////////////////////////////////////////////////////////
735 void CFREDDoc::OnEditDelete()
737 // TODO: Add your command handler code here
741 void CFREDDoc::OnDuplicate()
743 // TODO: Add your command handler code here
747 void CFREDDoc::OnEditCopy()
749 // TODO: Add your command handler code here
753 void CFREDDoc::OnEditCut()
755 // TODO: Add your command handler code here
759 void CFREDDoc::OnEditPaste()
761 // TODO: Add your command handler code here
765 void CFREDDoc::OnEditHold()
767 // TODO: Add your command handler code here
771 void CFREDDoc::OnEditFetch()
773 // TODO: Add your command handler code here
777 void CFREDDoc::OnEditUndo()
779 // TODO: Add your command handler code here
783 void CFREDDoc::OnFilePreferences()
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;
797 // Invoke the dialog box
798 if (dlg.DoModal() == IDOK)
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;
814 // initialize (clear out) the mission, so it's empty and ready to use.
815 void CFREDDoc::editor_init_mission()
818 SetModifiedFlag(FALSE);
821 void CFREDDoc::OnFileNew()
823 // If mission has been modified, offer to save before continuing.
824 while (IsModified()) {
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);
834 } else if (rval == IDCANCEL)
836 else if (rval == IDNO)
840 editor_init_mission();
844 void CFREDDoc::UpdateStatus(int flags)
847 if (flags & US_WORLD_CHANGED)
848 FREDDoc_ptr->SetModifiedFlag();
851 void CFREDDoc::OnEditClearAll()
857 void CFREDDoc::DeleteContents()
859 editor_init_mission();
862 void set_modified(BOOL arg)
864 Local_modified = arg;
865 FREDDoc_ptr->SetModifiedFlag(arg);
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()
871 Fred_found_unknown_ship_during_parsing = 1;
874 //////////////////////////////////////////////////////////////////////////
876 // Below is old, obsolete code, kept around just in case it might be found
877 // useful some time in the future for something.
879 //////////////////////////////////////////////////////////////////////////
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))
885 void SerializeVector(XFILE *fp, int mode, vector *v)
887 SerializeFloat(fp, mode, v->x);
888 SerializeFloat(fp, mode, v->y);
889 SerializeFloat(fp, mode, v->z);
892 void SerializeMatrix(XFILE *fp, int mode, matrix *m)
894 SerializeVector(fp, mode, &m->rvec);
895 SerializeVector(fp, mode, &m->uvec);
896 SerializeVector(fp, mode, &m->fvec);
899 void SerializePhysicsInfo(XFILE *fp, int mode, physics_info *pi)
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);
913 /////////////////////////////////////////////////////////////////////////////
914 // CFREDDoc serialization
915 void SerializeObject(XFILE *fp, int mode, object *objp)
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);
931 void SerializeAI(XFILE *fp, int mode, ai_info *aip)
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);
939 void SerializeShip(XFILE *fp, int mode, ship *shipp)
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);
949 void SerializeGrid(XFILE *fp, int mode, grid *gridp)
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);
960 for (i=0; i<MAX_GRID_POINTS; i++)
961 SerializeVector(fp, mode, &gridp->gpoints[i]);
965 void cfile_serialize(XFILE *fp, int flag)
968 int highest_object_index = 0, highest_ship_index = 0, highest_ai_index = 0;
970 Assert((flag == 0) || (flag == 1));
972 // fp = cfopen(filename, flag ? "wb" : "rb");
974 // MessageBox(NULL, strerror(errno), "File Open Error!", MB_ICONSTOP);
976 // Find highest used object if writing.
978 for (i=MAX_OBJECTS-1; i>0; i--)
979 if (Objects[i].type != OBJ_NONE) {
980 highest_object_index = i;
990 SerializeInt(fp, flag, highest_object_index);
992 for (i=1; i<=highest_object_index; i++) {
993 SerializeObject(fp, flag, &Objects[i]);
995 if (Objects[i].type != OBJ_NONE)
1001 for (i=MAX_SHIPS-1; i>0; i--)
1002 if (Ships[i].objnum) {
1003 highest_ship_index = i;
1008 SerializeInt(fp, flag, highest_ship_index);
1010 for (i=1; i<=highest_ship_index; i++) {
1011 SerializeShip(fp, flag, &Ships[i]);
1013 if (Ships[i].objnum)
1017 // Read/write AI info
1019 for (i=MAX_AI_INFO-1; i>0; i--)
1020 if (Ai_info[i].shipnum) {
1021 highest_ai_index = i;
1026 SerializeInt(fp, flag, highest_ai_index);
1028 for (i=1; i<=highest_ai_index; i++)
1029 SerializeAI(fp, flag, &Ai_info[i]);
1032 void cfile_serialize_editor(XFILE *fp, int flag)
1034 // Editor only stuff
1035 SerializeMatrix(fp, flag, &view_orient);
1036 SerializeVector(fp, flag, &view_pos);
1038 SerializeInt(fp, flag, Control_mode);
1039 SerializeInt(fp, flag, cur_object_index);
1040 SerializeInt(fp, flag, cur_wing);
1042 SerializeGrid(fp, flag, The_grid);