1 /* NetHack 3.6 macmain.c $NHDT-Date: 1432512796 2015/05/25 00:13:16 $ $NHDT-Branch: master $:$NHDT-Revision: 1.21 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /*-Copyright (c) Robert Patrick Rankin, 2009. */
4 /* NetHack may be freely redistributed. See license for details. */
6 /* main.c - Mac NetHack */
13 #if 1 /*!TARGET_API_MAC_CARBON*/
19 #include <ToolUtils.h>
20 #include <Resources.h>
28 static void finder_file_request(void);
40 boolean resuming = FALSE; /* assume new game */
43 windowprocs = mac_procs;
51 init_nhwindows(&argc, (char **) &hname);
54 * It seems you really want to play.
56 u.uhp = 1; /* prevent RIP on early quits */
58 finder_file_request();
60 dlb_init(); /* must be before newgame() */
63 * Initialize the vision system. This must be before mklev() on a
64 * new game or before a level restore on a saved game.
68 display_gamewindows();
70 set_playmode(); /* sets plname to "wizard" for wizard mode */
71 /* strip role,race,&c suffix; calls askname() if plname[] is empty
72 or holds a generic user name like "player" or "games" */
74 /* unlike Unix where the game might be invoked with a script
75 which forces a particular character name for each player
76 using a shared account, we always allow player to rename
77 the character during role/race/&c selection */
78 iflags.renameallowed = TRUE;
83 * First, try to find and restore a save file for specified character.
84 * We'll return here if new game player_selection() renames the hero.
87 if ((fd = restore_saved_game()) >= 0) {
90 display_file(NEWS, FALSE);
91 iflags.news = FALSE; /* in case dorecover() fails */
94 pline("Restoring save file...");
95 mark_synch(); /* flush output */
98 resuming = TRUE; /* not starting new game */
100 You("are in non-scoring discovery mode.");
101 if (discover || wizard) {
102 if (yn("Do you want to keep the save file?") == 'n')
103 (void) delete_savefile();
105 nh_compress(fqname(SAVEF, SAVEPREFIX, 0));
112 /* new game: start by choosing role, race, etc;
113 player might change the hero's name while doing that,
114 in which case we try to restore under the new name
115 and skip selection this time if that didn't succeed */
116 if (!iflags.renameinprogress) {
118 if (iflags.renameinprogress) {
119 /* player has renamed the hero while selecting role;
120 discard current lock file and create another for
121 the new character name */
122 delete_levelfile(0); /* remove empty lock file */
124 goto attempt_restore;
127 game_active = 1; /* done with selection, draw active game window */
130 You("are in non-scoring discovery mode.");
133 UndimMenuBar(); /* Yes, this is the place for it (!) */
143 copy_file(short src_vol, long src_dir, short dst_vol, long dst_dir,
145 pascal OSErr (*opener)(short vRefNum, long dirID,
146 ConstStr255Param fileName,
147 signed char permission, short *refNum))
149 short src_ref, dst_ref;
150 OSErr err = (*opener)(src_vol, src_dir, fName, fsRdPerm, &src_ref);
152 err = (*opener)(dst_vol, dst_dir, fName, fsWrPerm, &dst_ref);
155 err = GetEOF(src_ref, &file_len);
158 long count = MaxBlock();
159 if (count > file_len)
162 buf = NewHandle(count);
166 OSErr rd_err = FSRead(src_ref, &count, *buf);
167 err = FSWrite(dst_ref, &count, *buf);
187 force_hdelete(short vol, long dir, Str255 fName)
189 HRstFLock(vol, dir, fName);
190 HDelete(vol, dir, fName);
194 process_openfile(short src_vol, long src_dir, Str255 fName, OSType ftype)
198 if (ftype != SAVE_TYPE)
199 return; /* only deal with save files */
201 if (src_vol != theDirs.dataRefNum
202 || src_dir != theDirs.dataDirID
203 && CatMove(src_vol, src_dir, fName, theDirs.dataDirID, "\p:")
205 HCreate(theDirs.dataRefNum, theDirs.dataDirID, fName, MAC_CREATOR,
208 copy_file(src_vol, src_dir, theDirs.dataRefNum, theDirs.dataDirID,
209 fName, &HOpen); /* HOpenDF is only there under 7.0 */
211 err = copy_file(src_vol, src_dir, theDirs.dataRefNum,
212 theDirs.dataDirID, fName, &HOpenRF);
214 force_hdelete(src_vol, src_dir, fName);
216 HDelete(theDirs.dataRefNum, theDirs.dataDirID, fName);
222 ref = HOpenResFile(theDirs.dataRefNum, theDirs.dataDirID, fName,
225 Handle name = Get1Resource('STR ', PLAYER_NAME_RES_ID);
228 P2C(*(StringHandle) name, plname);
229 set_savefile_name(TRUE);
230 C2P(fqname(SAVEF, SAVEPREFIX, 0), save_f_p);
231 force_hdelete(theDirs.dataRefNum, theDirs.dataDirID,
234 if (HRename(theDirs.dataRefNum, theDirs.dataDirID, fName,
236 macFlags.gotOpen = 1;
244 finder_file_request(void)
246 if (macFlags.hasAE) {
247 /* we're capable of handling Apple Events, so let's see if we have any
250 long toWhen = TickCount()
251 + 20; /* wait a third of a second for all initial AE */
253 while (TickCount() < toWhen) {
254 if (WaitNextEvent(highLevelEventMask, &event, 3L, 0)) {
255 AEProcessAppleEvent(&event);
256 if (macFlags.gotOpen)
264 short finder_msg, file_count;
265 CountAppFiles(&finder_msg, &file_count);
266 if (finder_msg == appOpen && file_count == 1) {
271 GetAppFiles(1, &src);
272 err = FSMakeFSSpec(src.vRefNum, 0, src.fName, &filespec);
273 if (err == noErr && src.fType == SAVE_TYPE) {
274 process_openfile (filespec.vRefNum, filespec.parID, filespec.name, src.fType);
275 if (macFlags.gotOpen)
284 /* validate wizard mode if player has requested access to it */
286 authorize_wizard_mode()
288 /* other ports validate user name or character name here */