1 /* aNetHack 0.0.1 macmain.c $ANH-Date: 1432512796 2015/05/25 00:13:16 $ $ANH-Branch: master $:$ANH-Revision: 1.21 $ */
2 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3 /* aNetHack may be freely redistributed. See license for details. */
5 /* main.c - Mac aNetHack */
12 #if 1 /*!TARGET_API_MAC_CARBON*/
18 #include <ToolUtils.h>
19 #include <Resources.h>
27 static void finder_file_request(void);
39 boolean resuming
= FALSE
; /* assume new game */
42 windowprocs
= mac_procs
;
50 init_nhwindows(&argc
, (char **) &hname
);
53 * It seems you really want to play.
55 u
.uhp
= 1; /* prevent RIP on early quits */
57 finder_file_request();
59 dlb_init(); /* must be before newgame() */
62 * Initialize the vision system. This must be before mklev() on a
63 * new game or before a level restore on a saved game.
67 display_gamewindows();
69 set_playmode(); /* sets plname to "wizard" for wizard mode */
70 /* strip role,race,&c suffix; calls askname() if plname[] is empty
71 or holds a generic user name like "player" or "games" */
73 /* unlike Unix where the game might be invoked with a script
74 which forces a particular character name for each player
75 using a shared account, we always allow player to rename
76 the character during role/race/&c selection */
77 iflags
.renameallowed
= TRUE
;
82 * First, try to find and restore a save file for specified character.
83 * We'll return here if new game player_selection() renames the hero.
86 if ((fd
= restore_saved_game()) >= 0) {
89 display_file(NEWS
, FALSE
);
90 iflags
.news
= FALSE
; /* in case dorecover() fails */
93 pline("Restoring save file...");
94 mark_synch(); /* flush output */
97 resuming
= TRUE
; /* not starting new game */
99 You("are in non-scoring discovery mode.");
100 if (discover
|| wizard
) {
101 if (yn("Do you want to keep the save file?") == 'n')
102 (void) delete_savefile();
104 nh_compress(fqname(SAVEF
, SAVEPREFIX
, 0));
111 /* new game: start by choosing role, race, etc;
112 player might change the hero's name while doing that,
113 in which case we try to restore under the new name
114 and skip selection this time if that didn't succeed */
115 if (!iflags
.renameinprogress
) {
117 if (iflags
.renameinprogress
) {
118 /* player has renamed the hero while selecting role;
119 discard current lock file and create another for
120 the new character name */
121 delete_levelfile(0); /* remove empty lock file */
123 goto attempt_restore
;
126 game_active
= 1; /* done with selection, draw active game window */
129 You("are in non-scoring discovery mode.");
132 UndimMenuBar(); /* Yes, this is the place for it (!) */
142 copy_file(short src_vol
, long src_dir
, short dst_vol
, long dst_dir
,
144 pascal OSErr (*opener
)(short vRefNum
, long dirID
,
145 ConstStr255Param fileName
,
146 signed char permission
, short *refNum
))
148 short src_ref
, dst_ref
;
149 OSErr err
= (*opener
)(src_vol
, src_dir
, fName
, fsRdPerm
, &src_ref
);
151 err
= (*opener
)(dst_vol
, dst_dir
, fName
, fsWrPerm
, &dst_ref
);
154 err
= GetEOF(src_ref
, &file_len
);
157 long count
= MaxBlock();
158 if (count
> file_len
)
161 buf
= NewHandle(count
);
165 OSErr rd_err
= FSRead(src_ref
, &count
, *buf
);
166 err
= FSWrite(dst_ref
, &count
, *buf
);
186 force_hdelete(short vol
, long dir
, Str255 fName
)
188 HRstFLock(vol
, dir
, fName
);
189 HDelete(vol
, dir
, fName
);
193 process_openfile(short src_vol
, long src_dir
, Str255 fName
, OSType ftype
)
197 if (ftype
!= SAVE_TYPE
)
198 return; /* only deal with save files */
200 if (src_vol
!= theDirs
.dataRefNum
201 || src_dir
!= theDirs
.dataDirID
202 && CatMove(src_vol
, src_dir
, fName
, theDirs
.dataDirID
, "\p:")
204 HCreate(theDirs
.dataRefNum
, theDirs
.dataDirID
, fName
, MAC_CREATOR
,
207 copy_file(src_vol
, src_dir
, theDirs
.dataRefNum
, theDirs
.dataDirID
,
208 fName
, &HOpen
); /* HOpenDF is only there under 7.0 */
210 err
= copy_file(src_vol
, src_dir
, theDirs
.dataRefNum
,
211 theDirs
.dataDirID
, fName
, &HOpenRF
);
213 force_hdelete(src_vol
, src_dir
, fName
);
215 HDelete(theDirs
.dataRefNum
, theDirs
.dataDirID
, fName
);
221 ref
= HOpenResFile(theDirs
.dataRefNum
, theDirs
.dataDirID
, fName
,
224 Handle name
= Get1Resource('STR ', PLAYER_NAME_RES_ID
);
227 P2C(*(StringHandle
) name
, plname
);
228 set_savefile_name(TRUE
);
229 C2P(fqname(SAVEF
, SAVEPREFIX
, 0), save_f_p
);
230 force_hdelete(theDirs
.dataRefNum
, theDirs
.dataDirID
,
233 if (HRename(theDirs
.dataRefNum
, theDirs
.dataDirID
, fName
,
235 macFlags
.gotOpen
= 1;
243 finder_file_request(void)
245 if (macFlags
.hasAE
) {
246 /* we're capable of handling Apple Events, so let's see if we have any
249 long toWhen
= TickCount()
250 + 20; /* wait a third of a second for all initial AE */
252 while (TickCount() < toWhen
) {
253 if (WaitNextEvent(highLevelEventMask
, &event
, 3L, 0)) {
254 AEProcessAppleEvent(&event
);
255 if (macFlags
.gotOpen
)
263 short finder_msg
, file_count
;
264 CountAppFiles(&finder_msg
, &file_count
);
265 if (finder_msg
== appOpen
&& file_count
== 1) {
270 GetAppFiles(1, &src
);
271 err
= FSMakeFSSpec(src
.vRefNum
, 0, src
.fName
, &filespec
);
272 if (err
== noErr
&& src
.fType
== SAVE_TYPE
) {
273 process_openfile (filespec
.vRefNum
, filespec
.parID
, filespec
.name
, src
.fType
);
274 if (macFlags
.gotOpen
)
283 /* validate wizard mode if player has requested access to it */
285 authorize_wizard_mode()
287 /* other ports validate user name or character name here */