Source:NetHack 3.2.0/allmain.c

From NetHackWiki
Revision as of 08:03, 4 March 2008 by Kernigh bot (talk | contribs) (NetHack 3.2.0/allmain.c moved to Source:NetHack 3.2.0/allmain.c: Robot: moved page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Below is the full text to allmain.c from the source code of NetHack 3.2.0. To link to a particular line, write [[NetHack 3.2.0/allmain.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1.    /*	SCCS Id: @(#)allmain.c	3.2	96/03/28	*/
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* NetHack may be freely redistributed.  See license for details. */
4.    
5.    /* various code that was replicated in *main.c */
6.    
7.    #include "hack.h"
8.    
9.    #ifndef NO_SIGNAL
10.   #include <signal.h>
11.   #endif
12.   
13.   #ifdef POSITIONBAR
14.   STATIC_DCL void NDECL(do_postionbar);
15.   #endif
16.   
17.   #ifdef OVL0
18.   
19.   void
20.   moveloop()
21.   {
22.   #ifdef MICRO
23.   	char ch;
24.   	int abort_lev;
25.   #endif
26.   	int moverate = 0;
27.   	boolean didmove = 0;
28.   
29.   	flags.moonphase = phase_of_the_moon();
30.   	if(flags.moonphase == FULL_MOON) {
31.   		You("are lucky!  Full moon tonight.");
32.   		change_luck(1);
33.   	} else if(flags.moonphase == NEW_MOON) {
34.   		pline("Be careful!  New moon tonight.");
35.   	}
36.   	flags.friday13 = friday_13th();
37.   	if (flags.friday13) {
38.   		pline("Watch out!  Bad things can happen on Friday the 13th.");
39.   		change_luck(-1);
40.   	}
41.   
42.   	initrack();
43.   
44.   
45.   	/* Note:  these initializers don't do anything except guarantee that
46.   		we're linked properly.
47.   	*/
48.   	decl_init();
49.   	monst_init();
50.   	monstr_init();	/* monster strengths */
51.   	objects_init();
52.   
53.   #ifdef WIZARD
54.   	if (wizard) add_debug_extended_commands();
55.   #endif
56.   
57.   	(void) encumber_msg(); /* in case they auto-picked up something */
58.   
59.   	u.uz0.dlevel = u.uz.dlevel;
60.   
61.   	for(;;) {
62.   #ifdef CLIPPING
63.   		cliparound(u.ux, u.uy);
64.   #endif
65.   #if defined(MAC68K) && defined(MAC_MPW32) && !defined(MODEL_FAR)
66.   		UnloadAllSegments();  /* Marks non-resident segments as purgeable */
67.   #endif
68.   		get_nh_event();
69.   #ifdef POSITIONBAR
70.   		do_positionbar();
71.   #endif
72.   
73.   		didmove = flags.move;
74.   		if(flags.move) {	/* actual time passed */
75.   		    int oldmtimedone;
76.   		    int wtcap;
77.   
78.   		    if (u.utotype) deferred_goto();
79.   		    wtcap = encumber_msg();
80.   		    oldmtimedone = u.mtimedone;
81.   		    dosounds();
82.   
83.   		    if(moverate <= 0) {
84.   			/* calculate how much time passed. */
85.   			int moveamt = 0;
86.   			if(Fast & ~INTRINSIC) moveamt = 6;
87.   			else if(Fast) moveamt = 8;
88.   			else moveamt = 12;
89.   
90.   			switch(wtcap) {
91.   			case UNENCUMBERED: break;
92.   			case SLT_ENCUMBER: moveamt = (moveamt * 4) / 3; break;
93.   			case MOD_ENCUMBER: moveamt *= 2; break;
94.   			case HVY_ENCUMBER: moveamt *= 4; break;
95.   			default: moveamt *= 12; break;
96.   			}
97.   			moverate += moveamt;
98.   			settrack();
99.   		    }
100.  
101.  		    if(moverate > 0) {
102.  			flags.mon_moving = TRUE;
103.  			movemon();
104.  			flags.mon_moving = FALSE;
105.  			/* a monster may have levteleported player -dlc */
106.  			if (u.utotype) deferred_goto();
107.  			if(!rn2(u.uevent.udemigod ? 25 :
108.  				(depth(&u.uz) >
109.  				 depth(&stronghold_level))
110.  				? 50 : 70))
111.  			    (void) makemon((struct permonst *)0, 0, 0);
112.  			++monstermoves;
113.  			moverate -= 12;
114.  		    }
115.  		    if(Glib) glibr();
116.  		    nh_timeout();
117.  		    ++moves;
118.  		    if (u.ublesscnt)  u.ublesscnt--;
119.  		    if(flags.time && !flags.run)
120.  			flags.botl = 1;
121.  
122.  		    /* One possible result of prayer is healing.  Whether or
123.  		     * not you get healed depends on your current hit points.
124.  		     * If you are allowed to regenerate during the prayer, the
125.  		     * end-of-prayer calculation messes up on this.
126.  		     * Another possible result is rehumanization, which requires
127.  		     * that encumbrance and movement rate be recalculated.
128.  		     */
129.  		    if (u.uinvulnerable) {
130.  			/* for the moment at least, you're in tiptop shape */
131.  			wtcap = UNENCUMBERED;
132.  			moverate = 0;
133.  		    } else if (u.mtimedone && u.mh < u.mhmax) {
134.  			if (u.mh < 1) {
135.  			    rehumanize();
136.  			    moverate = 0;
137.  			} else if (Regeneration ||
138.  				 (wtcap < MOD_ENCUMBER && !(moves%20))) {
139.  			    flags.botl = 1;
140.  			    u.mh++;
141.  			}
142.  		    } else if (u.uhp < u.uhpmax) {
143.  			if(u.ulevel > 9) {
144.  			    int heal;
145.  
146.  			    if(HRegeneration ||
147.  			       (!(moves%3) &&
148.  				(wtcap < MOD_ENCUMBER || !flags.mv))) {
149.  				flags.botl = 1;
150.  				if (ACURR(A_CON) <= 12) heal = 1;
151.  				else heal = rnd((int) ACURR(A_CON)-12);
152.  				if (heal > u.ulevel-9) heal = u.ulevel-9;
153.  				u.uhp += heal;
154.  				if(u.uhp > u.uhpmax)
155.  				    u.uhp = u.uhpmax;
156.  			    }
157.  			} else if(HRegeneration ||
158.  				  ((wtcap < MOD_ENCUMBER || !flags.mv) &&
159.  				   (!(moves%((MAXULEV+12)/(u.ulevel+2)+1))))) {
160.  			    flags.botl = 1;
161.  			    u.uhp++;
162.  			}
163.  		    }
164.  
165.  		    if (wtcap > MOD_ENCUMBER && flags.mv) {
166.  			if(!(wtcap < EXT_ENCUMBER ? moves%30 : moves%10)) {
167.  			    if(u.uhp > 1) {
168.  				u.uhp--;
169.  			    } else {
170.  				You("pass out from exertion!");
171.  				exercise(A_CON, FALSE);
172.  				fall_asleep(-10, FALSE);
173.  			    }
174.  			}
175.  		    }
176.  
177.  		    if ((u.uen < u.uenmax) &&
178.  			((wtcap < MOD_ENCUMBER &&
179.  			  (!(moves%((MAXULEV + 8 - u.ulevel) *
180.  				    (Role_is('W') ? 3 : 4) / 6))))
181.  			 || Energy_regeneration)) {
182.  			u.uen +=
183.  			    rn1((int)(ACURR(A_WIS) + ACURR(A_INT)) / 10 + 1,1);
184.  			if (u.uen > u.uenmax)  u.uen = u.uenmax;
185.  			flags.botl = 1;
186.  		    }
187.  
188.  		    if(!u.uinvulnerable) {
189.  			if(Teleportation && !rn2(85)) {
190.  #ifdef REDO
191.  			    xchar old_ux = u.ux, old_uy = u.uy;
192.  #endif
193.  			    tele();
194.  #ifdef REDO
195.  			    if (u.ux != old_ux || u.uy != old_uy) {
196.  				/* clear doagain keystrokes */
197.  				pushch(0);
198.  				savech(0);
199.  			    }
200.  #endif
201.  			}
202.  			if(Polymorph && !rn2(100)) {
203.  			    if (multi >= 0) {
204.  				if (occupation)
205.  				    stop_occupation();
206.  				else
207.  				    nomul(0);
208.  			    }
209.  			    polyself();
210.  			    moverate = 0;
211.  			} else if (u.ulycn >= LOW_PM &&
212.  				   !rn2(80 - (20 * night()))) {
213.  			    if (multi >= 0) {
214.  				if (occupation)
215.  				    stop_occupation();
216.  				else
217.  				    nomul(0);
218.  			    }
219.  			    you_were();
220.  			    moverate = 0;
221.  			}
222.  		    }
223.  
224.  		    if(Searching && multi >= 0) (void) dosearch0(1);
225.  		    do_storms();
226.  		    gethungry();
227.  		    exerchk();
228.  		    invault();
229.  		    if (u.uhave.amulet) amulet();
230.  		    if (!rn2(40+(int)(ACURR(A_DEX)*3)))
231.  			u_wipe_engr(rnd(3));
232.  		    if (u.uevent.udemigod && !u.uinvulnerable) {
233.  			if (u.udg_cnt) u.udg_cnt--;
234.  			if (!u.udg_cnt) {
235.  			    intervene();
236.  			    u.udg_cnt = rn1(200, 50);
237.  			}
238.  		    }
239.  		    restore_attrib();
240.  		    /* underwater and waterlevel vision are done here */
241.  		    if (Is_waterlevel(&u.uz))
242.  			movebubbles();
243.  		    else if (Underwater) under_water(0);
244.  		    /* vision while buried done here */
245.  		    else if (u.uburied) under_ground(0);
246.  
247.  		    if ((oldmtimedone && !u.mtimedone) ||
248.  			(!oldmtimedone && u.mtimedone)) moverate = 0;
249.  		}
250.  		if(multi < 0) {
251.  			if (++multi == 0)	/* finished yet? */
252.  				unmul((char *)0);
253.  		}
254.  
255.  		find_ac();
256.  		if(!flags.mv || Blind) {
257.  		    /* redo monsters if hallu or wearing a helm of telepathy */
258.  		    if (Hallucination ||
259.  			(HTelepat & (WORN_HELMET|WORN_AMUL|W_ART)))
260.  			see_monsters();
261.  
262.  		    /* redo objects if hallucinating */
263.  		    if (Hallucination) see_objects();
264.  
265.  		    /* update swallowed display */
266.  		    if (Hallucination && u.uswallow) swallowed(0);
267.  
268.  		    if (vision_full_recalc) vision_recalc(0);	/* vision! */
269.  		}
270.  		if(flags.botl || flags.botlx) bot();
271.  
272.  		flags.move = 1;
273.  
274.  		if(multi >= 0 && occupation) {
275.  #ifdef MICRO
276.  			abort_lev = 0;
277.  			if (kbhit()) {
278.  				if ((ch = Getchar()) == ABORT)
279.  					abort_lev++;
280.  # ifdef REDO
281.  				else
282.  					pushch(ch);
283.  # endif /* REDO */
284.  			}
285.  			if (!abort_lev && (*occupation)() == 0)
286.  #else
287.  			if ((*occupation)() == 0)
288.  #endif
289.  				occupation = 0;
290.  			if(
291.  #ifdef MICRO
292.  			   abort_lev ||
293.  #endif
294.  			   monster_nearby()) {
295.  				stop_occupation();
296.  				reset_eat();
297.  			}
298.  #ifdef MICRO
299.  			if (!(++occtime % 7))
300.  				display_nhwindow(WIN_MAP, FALSE);
301.  #endif
302.  			continue;
303.  		}
304.  
305.  		if ((u.uhave.amulet || Clairvoyant) &&
306.  		    !(In_endgame(&u.uz) || (HClairvoyant & I_BLOCKED)) &&
307.  		    !(moves % 15) && !rn2(2))
308.  			do_vicinity_map();
309.  
310.  		if(u.utrap && u.utraptype == TT_LAVA) {
311.  		    if(!is_lava(u.ux,u.uy))
312.  			u.utrap = 0;
313.  		    else {
314.  			u.utrap -= 1<<8;
315.  			if(u.utrap < 1<<8) {
316.  			    killer_format = KILLED_BY;
317.  			    killer = "molten lava";
318.  			    You("sink below the surface and die.");
319.  			    done(DISSOLVED);
320.  			} else if(didmove && !u.umoved) {
321.  			    Norep("You sink deeper into the lava.");
322.  			    u.utrap += rnd(4);
323.  			}
324.  		    }
325.  		}
326.  
327.  #ifdef WIZARD
328.  		if (flags.sanity_check)
329.  		    sanity_check();
330.  #endif
331.  
332.  		u.umoved = FALSE;
333.  		if(!didmove || moverate <= 0) {
334.  		    if(multi > 0) {
335.  			lookaround();
336.  			if(!multi)	/* lookaround may clear multi */
337.  				flags.move = 0;
338.  			else if(flags.mv) {
339.  				if(multi < COLNO && !--multi)
340.  					flags.mv = flags.run = 0;
341.  				domove();
342.  			} else {
343.  				--multi;
344.  				rhack(save_cm);
345.  			}
346.  		    } else if(multi == 0) {
347.  #ifdef MAIL
348.  			ckmailstatus();
349.  #endif
350.  			rhack((char *)0);
351.  		    }
352.  		    /* !flags.move here: multiple movement command stopped */
353.  		    if (flags.time && (!flags.move || !flags.mv)) flags.botl=1;
354.  		}
355.  		if (vision_full_recalc) vision_recalc(0);	/* vision! */
356.  		if(multi && multi%7 == 0)
357.  			display_nhwindow(WIN_MAP, FALSE);
358.  	}
359.  }
360.  
361.  #endif /* OVL0 */
362.  #ifdef OVL1
363.  
364.  void
365.  stop_occupation()
366.  {
367.  	if(occupation) {
368.  		You("stop %s.", occtxt);
369.  		occupation = 0;
370.  /* fainting stops your occupation, there's no reason to sync.
371.  		sync_hunger();
372.  */
373.  #ifdef REDO
374.  		nomul(0);
375.  		pushch(0);
376.  #endif
377.  	}
378.  }
379.  
380.  #endif /* OVL1 */
381.  #ifdef OVLB
382.  
383.  void
384.  display_gamewindows()
385.  {
386.      WIN_MESSAGE = create_nhwindow(NHW_MESSAGE);
387.      WIN_STATUS = create_nhwindow(NHW_STATUS);
388.      WIN_MAP = create_nhwindow(NHW_MAP);
389.      WIN_INVEN = create_nhwindow(NHW_MENU);
390.  
391.  #ifdef MAC
392.      /*
393.       * This _is_ the right place for this - maybe we will
394.       * have to split display_gamewindows into create_gamewindows
395.       * and show_gamewindows to get rid of this ifdef...
396.       */
397.  	if ( ! strcmp ( windowprocs . name , "mac" ) ) {
398.  	    SanePositions ( ) ;
399.  	}
400.  #endif
401.  
402.      /*
403.       * The mac port is not DEPENDENT on the order of these
404.       * displays, but it looks a lot better this way...
405.       */
406.      display_nhwindow(WIN_STATUS, FALSE);
407.      display_nhwindow(WIN_MESSAGE, FALSE);
408.      clear_glyph_buffer();
409.      display_nhwindow(WIN_MAP, FALSE);
410.  }
411.  
412.  void
413.  newgame()
414.  {
415.  	int i;
416.  
417.  #ifdef MFLOPPY
418.  	gameDiskPrompt();
419.  #endif
420.  
421.  	fobj = invent = level.buriedobjlist = migrating_objs = (struct obj *)0;
422.  	fmon = migrating_mons = (struct monst *)0;
423.  	ftrap = 0;
424.  	flags.ident = 1;
425.  
426.  	if(wiz1_level.dlevel == 0) init_dungeons();
427.  
428.  	for (i = 0; i < NUMMONS; i++)
429.  		mvitals[i].mvflags = mons[i].geno & G_NOCORPSE;
430.  
431.  	init_objects();		/* must be before u_init() */
432.  	u_init();
433.  	init_artifacts();	/* must be after u_init() */
434.  
435.  #ifndef NO_SIGNAL
436.  	(void) signal(SIGINT, (SIG_RET_TYPE) done1);
437.  #endif
438.  #ifdef NEWS
439.  	if(flags.news) display_file(NEWS, FALSE);
440.  #endif
441.  	load_qtlist();	/* load up the quest text info */
442.  	quest_init();
443.  
444.  	mklev();
445.  	u_on_upstairs();
446.  #ifdef CLIPPING
447.  	/* pline() (hence You()) will call flush_screen() if u.ux is set,
448.  	 * which will be confused if clipping is not set up.
449.  	 * this is the equivalent of the restgamestate() call for new games.
450.  	 */
451.  	cliparound(u.ux, u.uy);
452.  #endif
453.  	check_special_room(FALSE);
454.  	vision_reset();		/* set up internals for level (after mklev) */
455.  
456.  	flags.botlx = 1;
457.  
458.  	/* Move the monster from under you or else
459.  	 * makedog() will fail when it calls makemon().
460.  	 *			- ucsfcgl!kneller
461.  	 */
462.  	if(MON_AT(u.ux, u.uy)) mnexto(m_at(u.ux, u.uy));
463.  	(void) makedog();
464.  	docrt();
465.  
466.  	if(flags.legacy && moves == 1) {
467.  		flush_screen(1);
468.  		com_pager(1);
469.  	}
470.  
471.  #ifdef INSURANCE
472.  	save_currentstate();
473.  #endif
474.  	program_state.something_worth_saving++;	/* useful data now exists */
475.  	return;
476.  }
477.  
478.  #ifdef POSITIONBAR
479.  do_positionbar()
480.  {
481.  	static char pbar[COLNO];
482.  	char *p;
483.  	
484.  	p = pbar;
485.  	/* up stairway */
486.  	if (upstair.sx &&
487.  	   (glyph_to_cmap(level.locations[upstair.sx][upstair.sy].glyph) ==
488.  	    S_upstair ||
489.   	    glyph_to_cmap(level.locations[upstair.sx][upstair.sy].glyph) == 
490.  	    S_upladder)) {
491.  		*p++ = '<';
492.  		*p++ = upstair.sx;
493.  	}
494.  	if (sstairs.sx &&
495.  	   (glyph_to_cmap(level.locations[sstairs.sx][sstairs.sy].glyph) ==
496.  	    S_upstair ||
497.   	    glyph_to_cmap(level.locations[sstairs.sx][sstairs.sy].glyph) == 
498.  	    S_upladder)) {
499.  		*p++ = '<';
500.  		*p++ = sstairs.sx;
501.  	}
502.  
503.  	/* down stairway */
504.  	if (dnstair.sx &&
505.  	   (glyph_to_cmap(level.locations[dnstair.sx][dnstair.sy].glyph) ==
506.  	    S_dnstair ||
507.   	    glyph_to_cmap(level.locations[dnstair.sx][dnstair.sy].glyph) ==
508.  	    S_dnladder)) {
509.  		*p++ = '>';
510.  		*p++ = dnstair.sx;
511.  	}
512.  	if (sstairs.sx &&
513.  	   (glyph_to_cmap(level.locations[sstairs.sx][sstairs.sy].glyph) ==
514.  	    S_dnstair ||
515.   	    glyph_to_cmap(level.locations[sstairs.sx][sstairs.sy].glyph) == 
516.  	    S_dnladder)) {
517.  		*p++ = '>';
518.  		*p++ = sstairs.sx;
519.  	}
520.  
521.  	/* hero location */
522.  	if (u.ux) {
523.  		*p++ = '@';
524.  		*p++ = u.ux;
525.  	}
526.  	/* fence post */
527.  	*p = 0;
528.  
529.  	update_positionbar(pbar);
530.  }
531.  #endif
532.  
533.  #endif /* OVLB */
534.  
535.  /*allmain.c*/