Source:NetHack 3.1.0/allmain.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to allmain.c from the source code of NetHack 3.1.0. To link to a particular line, write [[NetHack 3.1.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.1	92/11/13	*/
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 OVL0
14.   
15.   void
16.   moveloop()
17.   {
18.   #ifdef MICRO
19.   	char ch;
20.   	int abort_lev;
21.   #endif
22.   	int moverate = 0;
23.   	boolean didmove = 0;
24.   
25.   	/* Note:  these initializers don't do anything except guarantee that
26.   		we're linked properly.
27.   	*/
28.   	decl_init();
29.   	monst_init();
30.   	monstr_init();	/* monster strengths */
31.   	objects_init();
32.   
33.   #ifdef MULDGN
34.   	quest_init();	/* set up dynamic quest data */
35.   #endif
36.   
37.   	(void) encumber_msg(); /* in case they auto-picked up something */
38.   
39.   	for(;;) {
40.   #ifdef CLIPPING
41.   		cliparound(u.ux, u.uy);
42.   #endif
43.   #if defined(MAC_MPW32) && !defined(MODEL_FAR)
44.   		UnloadAllSegments();  /* Marks non-resident segments as purgeable */
45.   #endif
46.   		get_nh_event();
47.   
48.   		didmove = flags.move;
49.   		if(flags.move) {	/* actual time passed */
50.   		    int wtcap;
51.   
52.   		    if (u.utotype) deferred_goto();
53.   		    wtcap = encumber_msg();
54.   
55.   #ifdef SOUNDS
56.   		    dosounds();
57.   #endif
58.   
59.   		    if(moverate <= 0) {
60.   			/* calculate how much time passed. */
61.   			int moveamt = 0;
62.   			if(Fast & ~INTRINSIC) moveamt = 6;
63.   			else if(Fast) moveamt = 8;
64.   			else moveamt = 12;
65.   
66.   			switch(wtcap) {
67.   			case UNENCUMBERED: break;
68.   			case SLT_ENCUMBER: moveamt = (moveamt * 4) / 3; break;
69.   			case MOD_ENCUMBER: moveamt *= 2; break;
70.   			case HVY_ENCUMBER: moveamt *= 4; break;
71.   			default: moveamt *= 12; break;
72.   			}
73.   			moverate += moveamt;
74.   			settrack();
75.   		    }
76.   
77.   		    if(moverate > 0) {
78.   			movemon();
79.   			/* a monster may have levteleported player -dlc */
80.   			if (u.utotype) deferred_goto();
81.   			if(!rn2(u.uevent.udemigod ? 25 :
82.   				(depth(&u.uz) >
83.   				 depth(&stronghold_level))
84.   				? 50 : 70))
85.   			    (void) makemon((struct permonst *)0, 0, 0);
86.   			++monstermoves;
87.   			remove_cadavers(&fobj);
88.   			remove_cadavers(&invent);
89.   			moverate -= 12;
90.   		    }
91.   		    if(Glib) glibr();
92.   		    nh_timeout();
93.   		    ++moves;
94.   		    if (u.ublesscnt)  u.ublesscnt--;
95.   		    if(flags.time) flags.botl = 1;
96.   #ifdef POLYSELF
97.   		    if (u.mtimedone && u.mh < u.mhmax) {
98.   			if (u.mh < 1) {
99.   			    rehumanize();
100.  			    moverate = 0;
101.  			} else if (Regeneration ||
102.  				 (wtcap < MOD_ENCUMBER && !(moves%20))) {
103.  			    flags.botl = 1;
104.  			    u.mh++;
105.  			}
106.  		    } else
107.  #endif
108.  		    if(u.uhp < u.uhpmax) {
109.  			if(u.ulevel > 9) {
110.  			    int heal;
111.  
112.  			    if(HRegeneration ||
113.  			       (!(moves%3) &&
114.  				(wtcap < MOD_ENCUMBER || !flags.mv))) {
115.  				flags.botl = 1;
116.  				if (ACURR(A_CON) <= 12) heal = 1;
117.  				else heal = rnd((int) ACURR(A_CON)-12);
118.  				if (heal > u.ulevel-9) heal = u.ulevel-9;
119.  				u.uhp += heal;
120.  				if(u.uhp > u.uhpmax)
121.  				    u.uhp = u.uhpmax;
122.  			    }
123.  			} else if(HRegeneration ||
124.  				  ((wtcap < MOD_ENCUMBER || !flags.mv) &&
125.  				   (!(moves%((MAXULEV+12)/(u.ulevel+2)+1))))) {
126.  			    flags.botl = 1;
127.  			    u.uhp++;
128.  			}
129.  		    }
130.  
131.  		    if (wtcap > MOD_ENCUMBER && flags.mv) {
132.  			if(!(wtcap < EXT_ENCUMBER ? moves%30 : moves%10)) {
133.  			    if(u.uhp > 1) {
134.  				u.uhp--;
135.  			    } else {
136.  				pline("You pass out from exertion!");
137.  				exercise(A_CON, FALSE);
138.  				nomul(-10);
139.  				u.usleep = 1;
140.  			    }
141.  			}
142.  		    }
143.  
144.  		    if ((u.uen < u.uenmax) &&
145.  			((wtcap < MOD_ENCUMBER &&
146.  			  (!(moves%((MAXULEV + 1 - u.ulevel) *
147.  				    (pl_character[0] == 'W' ? 3 : 4) / 2))))
148.  			 || Energy_regeneration)) {
149.  			u.uen +=
150.  			    rn1((int)(ACURR(A_WIS) + ACURR(A_INT)) / 10 + 1,1);
151.  			if (u.uen > u.uenmax)  u.uen = u.uenmax;
152.  			flags.botl = 1;
153.  		    }
154.  
155.  		    if(!u.uinvulnerable) {
156.  			if(Teleportation && !rn2(85)) tele();
157.  #ifdef POLYSELF
158.  			if(Polymorph && !rn2(100)) {
159.  			    if (multi >= 0) {
160.  				if (occupation)
161.  				    stop_occupation();
162.  				else
163.  				    nomul(0);
164.  			    }
165.  			    polyself();
166.  			    moverate = 0;
167.  			} else if (u.ulycn >= 0 && !rn2(80 - (20 * night()))) {
168.  			    if (multi >= 0) {
169.  				if (occupation)
170.  				    stop_occupation();
171.  				else
172.  				    nomul(0);
173.  			    }
174.  			    you_were();
175.  			    moverate = 0;
176.  			}
177.  #endif
178.  		    }
179.  
180.  		    if(Searching && multi >= 0) (void) dosearch0(1);
181.  		    do_storms();
182.  		    hatch_eggs();
183.  		    burn_lamps();
184.  		    gethungry();
185.  		    exerchk();
186.  		    invault();
187.  		    amulet();
188.  		    if (!rn2(40+(int)(ACURR(A_DEX)*3))) 
189.  			u_wipe_engr(rnd(3));
190.  		    if (u.uevent.udemigod && !u.uinvulnerable) {
191.  			if (u.udg_cnt) u.udg_cnt--;
192.  			if (!u.udg_cnt) {
193.  			    intervene();
194.  			    u.udg_cnt = rn1(200, 50);
195.  			}
196.  		    }
197.  		    restore_attrib();
198.  		    /* underwater and waterlevel vision are done here */
199.  		    if (Is_waterlevel(&u.uz))
200.  			movebubbles();
201.  		    else if (Underwater)
202.  			under_water(0);
203.  
204.  		}
205.  		if(multi < 0) {
206.  			if(!++multi){
207.  				pline(nomovemsg ? nomovemsg :
208.  					(const char *)"You can move again.");
209.  				nomovemsg = 0;
210.  				u.usleep = 0;
211.  				if(afternmv) (*afternmv)();
212.  				afternmv = 0;
213.  			}
214.  		}
215.  
216.  		find_ac();
217.  		if(!flags.mv || Blind) {
218.  		    /* redo monsters if hallu or wearing a helm of telepathy */
219.  		    if (Hallucination ||
220.  			(HTelepat & (WORN_HELMET|WORN_AMUL|W_ART)))
221.  			see_monsters();
222.  
223.  		    /* redo objects if hallucinating */
224.  		    if (Hallucination) see_objects();
225.  
226.  		    /* update swallowed display */
227.  		    if (Hallucination && u.uswallow) swallowed(0);
228.  
229.  		    if (vision_full_recalc) vision_recalc(0);	/* vision! */
230.  		}
231.  		if(flags.botl || flags.botlx) bot();
232.  
233.  		flags.move = 1;
234.  
235.  		if(multi >= 0 && occupation) {
236.  #ifdef MICRO
237.  			abort_lev = 0;
238.  			if (kbhit()) {
239.  				if ((ch = Getchar()) == ABORT)
240.  					abort_lev++;
241.  # ifdef REDO
242.  				else
243.  					pushch(ch);
244.  # endif /* REDO */
245.  			}
246.  			if (!abort_lev && (*occupation)() == 0)
247.  #else
248.  			if ((*occupation)() == 0)
249.  #endif
250.  				occupation = 0;
251.  			if(
252.  #ifdef MICRO
253.  			   abort_lev ||
254.  #endif
255.  			   monster_nearby()) {
256.  				stop_occupation();
257.  				reset_eat();
258.  			}
259.  #ifdef MICRO
260.  			if (!(++occtime % 7))
261.  				display_nhwindow(WIN_MAP, FALSE);
262.  #endif
263.  			continue;
264.  		}
265.  
266.  		if((u.uhave.amulet || Clairvoyant) && !In_endgame(&u.uz) &&
267.  						!(moves%15) && !rn2(2))
268.  			do_vicinity_map();
269.  
270.  		if(u.utrap && u.utraptype == TT_LAVA) {
271.  		    if(!is_lava(u.ux,u.uy))
272.  			u.utrap = 0;
273.  		    else {
274.  			u.utrap -= 1<<8;
275.  			if(u.utrap < 1<<8) {
276.  			    killer_format = KILLED_BY;
277.  			    killer = "molten lava";
278.  			    You("sink below the surface and suffocate.");
279.  			    done(DROWNING); /*whatever*/
280.  			} else if(didmove && !u.umoved) {
281.  			    Norep("You sink deeper into the lava.");
282.  			    u.utrap += rnd(4);
283.  			}
284.  		    }
285.  		}
286.  
287.  		u.umoved = FALSE;
288.  		if(!didmove || moverate <= 0) {
289.  		    if(multi > 0) {
290.  			lookaround();
291.  			if(!multi) {	/* lookaround may clear multi */
292.  				flags.move = 0;
293.  				continue;
294.  			}
295.  			if(flags.mv) {
296.  				if(multi < COLNO && !--multi)
297.  					flags.mv = flags.run = 0;
298.  				domove();
299.  			} else {
300.  				--multi;
301.  				rhack(save_cm);
302.  			}
303.  		    } else if(multi == 0) {
304.  #ifdef MAIL
305.  			ckmailstatus();
306.  #endif
307.  			rhack(NULL);
308.  		    }
309.  		}
310.  		if (vision_full_recalc) vision_recalc(0);	/* vision! */
311.  		if(multi && multi%7 == 0)
312.  			display_nhwindow(WIN_MAP, FALSE);
313.  	}
314.  }
315.  
316.  #endif /* OVL0 */
317.  #ifdef OVL1
318.  
319.  void
320.  stop_occupation()
321.  {
322.  	if(occupation) {
323.  		You("stop %s.", occtxt);
324.  		occupation = 0;
325.  /* fainting stops your occupation, there's no reason to sync.
326.  		sync_hunger();
327.  */
328.  #ifdef REDO
329.  		nomul(0);
330.  		pushch(0);
331.  #endif
332.  	}
333.  }
334.  
335.  #endif /* OVL1 */
336.  #ifdef OVLB
337.  
338.  void
339.  display_gamewindows()
340.  {
341.      WIN_MESSAGE = create_nhwindow(NHW_MESSAGE);
342.      WIN_STATUS = create_nhwindow(NHW_STATUS);
343.      WIN_MAP = create_nhwindow(NHW_MAP);
344.      WIN_INVEN = create_nhwindow(NHW_MENU);
345.  
346.  #ifdef MAC
347.      /*
348.       * This _is_ the right place for this - maybe we will
349.       * have to split display_gamewindows into create_gamewindows
350.       * and show_gamewindows to get rid of this ifdef...
351.       */
352.      SanePositions ( ) ;
353.  #endif
354.  
355.      /*
356.       * The mac port is not DEPENDENT on the order of these
357.       * displays, but it looks a lot better this way...
358.       */
359.      display_nhwindow(WIN_STATUS, FALSE);
360.      display_nhwindow(WIN_MESSAGE, FALSE);
361.      display_nhwindow(WIN_MAP, FALSE);
362.  }
363.  
364.  void
365.  newgame()
366.  {
367.  #ifdef MFLOPPY
368.  	gameDiskPrompt();
369.  #endif
370.  
371.  	fobj = invent = migrating_objs = (struct obj *)0;
372.  	fmon = migrating_mons = (struct monst *)0;
373.  	ftrap = 0;
374.  	flags.ident = 1;
375.  
376.  	if(wiz1_level.dlevel == 0) init_dungeons();
377.  	init_objects();
378.  	u_init();
379.  
380.  #ifndef NO_SIGNAL
381.  	(void) signal(SIGINT, (SIG_RET_TYPE) done1);
382.  #endif
383.  #ifdef NEWS
384.  	if(flags.news) display_file(NEWS, FALSE);
385.  #endif
386.  #ifdef MULDGN
387.  	load_qtlist();	/* load up the quest text info */
388.  	if(flags.legacy && moves == 1) com_pager(1);
389.  #endif
390.  	mklev();
391.  	u_on_upstairs();
392.  	check_special_room(FALSE);
393.  	vision_reset();		/* set up internals for level (after mklev) */
394.  
395.  	flags.botlx = 1;
396.  
397.  	/* Move the monster from under you or else
398.  	 * makedog() will fail when it calls makemon().
399.  	 * 			- ucsfcgl!kneller
400.  	 */
401.  	if(MON_AT(u.ux, u.uy)) mnexto(m_at(u.ux, u.uy));
402.  
403.  #ifdef CLIPPING
404.  	cliparound(u.ux, u.uy);
405.  #endif
406.  	(void) makedog();
407.  	docrt();
408.  
409.  #ifdef INSURANCE
410.  	save_currentstate();
411.  #endif
412.  	return;
413.  }
414.  
415.  #endif /* OVLB */
416.  
417.  /*allmain.c*/