Source:NetHack 5.0.0/src/allmain.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to allmain.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/allmain.c#line12]], for example.

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. /* NetHack 5.0	allmain.c	$NHDT-Date: 1771213100 2026/02/15 19:38:20 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.286 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2012. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. /* various code that was replicated in *main.c */
7. 
8. #include "hack.h"
9. 
10. #ifndef NO_SIGNAL
11. #include <signal.h>
12. #endif
13. 
14. staticfn void moveloop_preamble(boolean);
15. staticfn void u_calc_moveamt(int);
16. staticfn void maybe_generate_rnd_mon(void);
17. staticfn void maybe_do_tutorial(void);
18. #ifdef POSITIONBAR
19. staticfn void do_positionbar(void);
20. #endif
21. staticfn void regen_pw(int);
22. staticfn void regen_hp(int);
23. staticfn void interrupt_multi(const char *);
24. 
25. #ifdef CRASHREPORT
26. #define USED_FOR_CRASHREPORT
27. #else
28. #define USED_FOR_CRASHREPORT UNUSED
29. #endif
30. 
31. /*ARGSUSED*/
32. void
33. early_init(int argc USED_FOR_CRASHREPORT, char *argv[] USED_FOR_CRASHREPORT)
34. {
35.     program_state_init();
36. #ifdef CRASHREPORT
37.     /* Do this as early as possible, but let ports do other things first. */
38.     crashreport_init(argc, argv);
39. #endif
40.     decl_globals_init();
41.     objects_globals_init();
42.     monst_globals_init();
43.     sys_early_init();
44.     runtime_info_init();
45. }
46. 
47. staticfn void
48. moveloop_preamble(boolean resuming)
49. {
50.     /* if a save file created in normal mode is now being restored in
51.        explore mode, treat it as normal restore followed by 'X' command
52.        to use up the save file and require confirmation for explore mode */
53.     if (resuming && iflags.deferred_X)
54.         (void) enter_explore_mode();
55. 
56.     /* side-effects from the real world */
57.     flags.moonphase = phase_of_the_moon();
58.     if (flags.moonphase == FULL_MOON) {
59.         You("are lucky!  Full moon tonight.");
60.         change_luck(1);
61.     } else if (flags.moonphase == NEW_MOON) {
62.         pline("Be careful!  New moon tonight.");
63.     }
64.     flags.friday13 = friday_13th();
65.     if (flags.friday13) {
66.         pline("Watch out!  Bad things can happen on Friday the 13th.");
67.         change_luck(-1);
68.     }
69. 
70.     if (!resuming) { /* new game */
71.         program_state.beyond_savefile_load = 1; /* for TTY_PERM_INVENT */
72.         svc.context.rndencode = rnd(9000);
73.         set_wear((struct obj *) 0); /* for side-effects of starting gear */
74.         reset_justpicked(gi.invent);
75.         (void) pickup(1);      /* autopickup at initial location */
76.         /* only matters if someday a character is able to start with
77.            clairvoyance (wizard with cornuthaum perhaps?); without this,
78.            first "random" occurrence would always kick in on turn 1 */
79.         svc.context.seer_turn = (long) rnd(30);
80.         /* give hero initial movement points; new game only--for restore,
81.            pending movement points were included in the save file */
82.         u.umovement = NORMAL_SPEED;
83.         initrack();
84.     }
85.     disp.botlx = TRUE; /* for STATUS_HILITES */
86.     if (resuming) { /* restoring old game */
87.         read_engr_at(u.ux, u.uy); /* subset of pickup() */
88.         fix_shop_damage();
89.     }
90. 
91.     encumber_msg(); /* in case they auto-picked up something */
92.     if (gd.defer_see_monsters) {
93.         gd.defer_see_monsters = FALSE;
94.         see_monsters();
95.     }
96. 
97.     u.uz0.dlevel = u.uz.dlevel;
98.     svc.context.move = 0;
99. 
100.     /* finish processing "--debug:fuzzer" from the command line */
101.     if (iflags.fuzzerpending) {
102.         iflags.debug_fuzzer = fuzzer_impossible_panic;
103.         iflags.fuzzerpending = FALSE;
104.     }
105. 
106.     program_state.in_moveloop = 1;
107.     /* for perm_invent preset at startup, display persistent inventory after
108.        invent is fully populated and the in_moveloop flag has been set */
109.     if (iflags.perm_invent)
110.         update_inventory();
111. }
112. 
113. staticfn void
114. u_calc_moveamt(int wtcap)
115. {
116.     int moveamt = 0;
117. 
118.     /* calculate how much time passed. */
119.     if (u.usteed && u.umoved) {
120.         /* your speed doesn't augment steed's speed */
121.         moveamt = mcalcmove(u.usteed, TRUE);
122.     } else {
123.         moveamt = gy.youmonst.data->mmove;
124. 
125.         if (Very_fast) { /* speed boots, potion, or spell */
126.             /* gain a free action on 2/3 of turns */
127.             if (rn2(3) != 0)
128.                 moveamt += NORMAL_SPEED;
129.         } else if (Fast) { /* intrinsic */
130.             /* gain a free action on 1/3 of turns */
131.             if (rn2(3) == 0)
132.                 moveamt += NORMAL_SPEED;
133.         }
134.     }
135. 
136.     switch (wtcap) {
137.     case UNENCUMBERED:
138.         break;
139.     case SLT_ENCUMBER:
140.         moveamt -= (moveamt / 4);
141.         break;
142.     case MOD_ENCUMBER:
143.         moveamt -= (moveamt / 2);
144.         break;
145.     case HVY_ENCUMBER:
146.         moveamt -= ((moveamt * 3) / 4);
147.         break;
148.     case EXT_ENCUMBER:
149.         moveamt -= ((moveamt * 7) / 8);
150.         break;
151.     default:
152.         break;
153.     }
154. 
155.     u.umovement += moveamt;
156.     if (u.umovement < 0)
157.         u.umovement = 0;
158. }
159. 
160. /* small chance of generating a new random monster */
161. staticfn void
162. maybe_generate_rnd_mon(void)
163. {
164.     if (!rn2(u.uevent.udemigod ? 25
165.              : (depth(&u.uz) > depth(&stronghold_level)) ? 50
166.              : 70))
167.         (void) makemon((struct permonst *) 0, 0, 0, NO_MM_FLAGS);
168. }
169. 
170. #if defined(MICRO) || defined(WIN32)
171. static int mvl_abort_lev;
172. #endif
173. static int mvl_wtcap = 0;
174. static int mvl_change = 0;
175. 
176. void
177. moveloop_core(void)
178. {
179.     boolean monscanmove = FALSE;
180. 
181. #ifdef SAFERHANGUP
182.     if (program_state.done_hup)
183.         end_of_input();
184. #endif
185.     get_nh_event();
186. #ifdef POSITIONBAR
187.     do_positionbar();
188. #endif
189.     if (iflags.pending_customizations)
190.         maybe_shuffle_customizations();
191. 
192.     dobjsfree();
193. 
194.     if (svc.context.bypasses)
195.         clear_bypasses();
196. 
197.     if (iflags.sanity_check || iflags.debug_fuzzer)
198.         sanity_check();
199. 
200.     if (svc.context.resume_wish)
201.         makewish(); /* clears resume_wish */
202. 
203.     if (svc.context.move) {
204.         /* actual time passed */
205.         u.umovement -= NORMAL_SPEED;
206. 
207.         do { /* hero can't move this turn loop */
208.             encumber_msg();
209. 
210.             svc.context.mon_moving = TRUE;
211.             do {
212.                 monscanmove = movemon();
213.                 if (u.umovement >= NORMAL_SPEED)
214.                     break; /* it's now your turn */
215.             } while (monscanmove);
216.             svc.context.mon_moving = FALSE;
217. 
218.             /* this needs to be after the monster movement loop in
219.                case monster actions affected burden, e.g. rehumanize */
220.             mvl_wtcap = near_capacity();
221. 
222.             if (!monscanmove && u.umovement < NORMAL_SPEED) {
223.                 /* both hero and monsters are out of steam this round */
224.                 struct monst *mtmp;
225. 
226.                 /* set up for a new turn */
227.                 gw.were_changes = 0L;
228.                 mcalcdistress(); /* adjust monsters' trap, blind, etc */
229. 
230.                 /* reallocate movement rations to monsters; don't need
231.                    to skip dead monsters here because they will have
232.                    been purged at end of their previous round of moving */
233.                 for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
234.                     mtmp->movement += mcalcmove(mtmp, TRUE);
235. 
236.                 /* occasionally add another monster; since this takes
237.                    place after movement has been allotted, the new
238.                    monster effectively loses its first turn */
239.                 maybe_generate_rnd_mon();
240. 
241.                 u_calc_moveamt(mvl_wtcap);
242.                 settrack();
243. 
244.                 svm.moves++;
245.                 /*
246.                  * Never allow 'moves' to grow big enough to wrap.
247.                  * We don't care what the maximum possible 'long int'
248.                  * is for the current configuration, we want a value
249.                  * that is the same for all viable configurations.
250.                  * When imposing the limit, use a mystic decimal value
251.                  * instead of a magic binary one such as 0x7fffffffL.
252.                  */
253.                 if (svm.moves >= 1000000000L) {
254.                     display_nhwindow(WIN_MESSAGE, TRUE);
255.                     urgent_pline("The dungeon capitulates.");
256.                     done(ESCAPED);
257.                 }
258.                 /* 'moves' is misnamed; it represents turns; hero_seq is
259.                    a value that is distinct every time the hero moves */
260.                 gh.hero_seq = svm.moves << 3;
261. 
262.                 if (flags.time && !svc.context.run)
263.                     disp.time_botl = TRUE; /* 'moves' just changed */
264. 
265.                 /********************************/
266.                 /* once-per-turn things go here */
267.                 /********************************/
268. 
269.                 l_nhcore_call(NHCORE_MOVELOOP_TURN);
270. 
271.                 if (Glib)
272.                     glibr();
273.                 nh_timeout();
274.                 run_regions();
275. 
276.                 if (u.ublesscnt)
277.                     u.ublesscnt--;
278. 
279.                 /* One possible result of prayer is healing.  Whether or
280.                  * not you get healed depends on your current hit points.
281.                  * If you are allowed to regenerate during the prayer,
282.                  * the end-of-prayer calculation messes up on this.
283.                  * Another possible result is rehumanization, which
284.                  * requires that encumbrance and movement rate be
285.                  * recalculated.
286.                  */
287.                 if (u.uinvulnerable) {
288.                     /* for the moment at least, you're in tiptop shape */
289.                     mvl_wtcap = UNENCUMBERED;
290.                 } else if (!Upolyd ? (u.uhp < u.uhpmax)
291.                            : (u.mh < u.mhmax
292.                               || gy.youmonst.data->mlet == S_EEL)) {
293.                     /* maybe heal */
294.                     regen_hp(mvl_wtcap);
295.                 }
296. 
297.                 /* moving around while encumbered is hard work */
298.                 if (mvl_wtcap > MOD_ENCUMBER && u.umoved) {
299.                     if (!(mvl_wtcap < EXT_ENCUMBER ? svm.moves % 30
300.                           : svm.moves % 10)) {
301.                         overexert_hp();
302.                     }
303.                 }
304. 
305.                 regen_pw(mvl_wtcap);
306. 
307.                 if (!u.uinvulnerable) {
308.                     if (Teleportation && !rn2(85)) {
309.                         coordxy old_ux = u.ux, old_uy = u.uy;
310. 
311.                         tele();
312.                         if (u.ux != old_ux || u.uy != old_uy) {
313.                             if (!next_to_u()) {
314.                                 check_leash(old_ux, old_uy);
315.                             }
316.                             /* clear doagain keystrokes */
317.                             cmdq_clear(CQ_CANNED);
318.                             cmdq_clear(CQ_REPEAT);
319.                         }
320.                     }
321.                     /* delayed change may not be valid anymore */
322.                     if ((mvl_change == 1 && !Polymorph)
323.                         || (mvl_change == 2 && u.ulycn == NON_PM))
324.                         mvl_change = 0;
325.                     if (Polymorph && !rn2(100))
326.                         mvl_change = 1;
327.                     else if (ismnum(u.ulycn) && !Upolyd
328.                              && !rn2(80 - (20 * night())))
329.                         mvl_change = 2;
330.                     if (mvl_change && !Unchanging) {
331.                         if (gm.multi >= 0) {
332.                             stop_occupation();
333.                             if (mvl_change == 1)
334.                                 polyself(POLY_NOFLAGS);
335.                             else
336.                                 you_were();
337.                             mvl_change = 0;
338.                         }
339.                     }
340.                 }
341. 
342.                 if (Searching && !svl.level.flags.noautosearch
343.                     && gm.multi >= 0)
344.                     (void) dosearch0(1);
345.                 if (Warning)
346.                     warnreveal();
347.                 if (gw.were_changes) {
348.                     /* update innate intrinsics (mainly Drain_resistance) */
349.                     set_uasmon();
350.                 }
351.                 mkot_trap_warn();
352.                 dosounds();
353.                 do_storms();
354.                 gethungry();
355.                 age_spells();
356.                 exerchk();
357.                 invault();
358.                 if (u.uhave.amulet)
359.                     amulet();
360.                 if (!rn2(40 + (int) (ACURR(A_DEX) * 3)))
361.                     u_wipe_engr(rnd(3));
362.                 if (u.uevent.udemigod && !u.uinvulnerable) {
363.                     if (u.udg_cnt)
364.                         u.udg_cnt--;
365.                     if (!u.udg_cnt) {
366.                         intervene();
367.                         u.udg_cnt = rn1(200, 50);
368.                     }
369.                 }
370. /* XXX This should be recoded to use something like regions - a list of
371.  * things that are active and need to be handled that is dynamically
372.  * maintained and not a list of special cases. */
373.                 /* vision will be updated as bubbles move */
374.                 if (Is_waterlevel(&u.uz) || Is_airlevel(&u.uz))
375.                     movebubbles();
376.                 else if (svl.level.flags.fumaroles)
377.                     fumaroles();
378. 
379.                 /* when immobile, count is in turns */
380.                 if (gm.multi < 0) {
381.                     runmode_delay_output();
382.                     if (++gm.multi == 0) { /* finished yet? */
383.                         unmul((char *) 0);
384.                         /* if unmul caused a level change, take it now */
385.                         if (u.utotype)
386.                             deferred_goto();
387.                     }
388.                 }
389.             }
390.         } while (u.umovement < NORMAL_SPEED); /* hero can't move */
391. 
392.         /******************************************/
393.         /* once-per-hero-took-time things go here */
394.         /******************************************/
395. 
396.         gh.hero_seq++; /* moves*8 + n for n == 1..7 */
397. 
398.         /* although we checked for encumbrance above, we need to
399.            check again for message purposes, as the weight of
400.            inventory may have changed in, e.g., nh_timeout(); we do
401.            need two checks here so that the player gets feedback
402.            immediately if their own action encumbered them */
403.         encumber_msg();
404. 
405. #ifdef STATUS_HILITES
406.         if (iflags.hilite_delta)
407.             status_eval_next_unhilite();
408. #endif
409.         if (svm.moves >= svc.context.seer_turn) {
410.             if ((u.uhave.amulet || Clairvoyant) && !In_endgame(&u.uz)
411.                 && !BClairvoyant)
412.                 do_vicinity_map((struct obj *) 0);
413.             /* we maintain this counter even when clairvoyance isn't
414.                taking place; on average, go again 30 turns from now */
415.             svc.context.seer_turn = svm.moves + (long) rn1(31, 15); /*15..45*/
416.             /* [it used to be that on every 15th turn, there was a 50%
417.                chance of farsight, so it could happen as often as every
418.                15 turns or theoretically never happen at all; but when
419.                a fast hero got multiple moves on that 15th turn, it
420.                could actually happen more than once on the same turn!] */
421.         }
422.         /* [fast hero who gets multiple moves per turn ends up sinking
423.            multiple times per turn; is that what we really want?] */
424.         if (u.utrap && u.utraptype == TT_LAVA)
425.             sink_into_lava();
426.         /* when/if hero escapes from lava, he can't just stay there */
427.         else if (!u.umoved)
428.             (void) pooleffects(FALSE);
429. 
430.         /* vision while buried or underwater is updated here */
431.         if (Underwater)
432.             under_water(0);
433.         else if (u.uburied)
434.             under_ground(0);
435. 
436.         see_nearby_monsters();
437.     } /* actual time passed */
438. 
439.     /****************************************/
440.     /* once-per-player-input things go here */
441.     /****************************************/
442. 
443.     clear_splitobjs();
444. 
445.     /* the Amulet of Yendor gives a wish when initially picked up */
446.     if (u.uhave.amulet && !u.uevent.amulet_wish) {
447.         u.uevent.amulet_wish = 1;
448.         display_nhwindow(WIN_MESSAGE, TRUE);
449.         urgent_pline("The Amulet is bestowing a wish upon you!");
450.         makewish();
451.     }
452. 
453.     find_ac();
454.     if (!svc.context.mv || Blind) {
455.         /* redo monsters if hallu or wearing a helm of telepathy */
456.         if (Hallucination) { /* update screen randomly */
457.             see_monsters();
458.             see_objects();
459.             see_traps();
460.             if (u.uswallow)
461.                 swallowed(0);
462.         } else if (Unblind_telepat || Warning || Warn_of_mon
463.                    /* this is needed for the case where you saw a monster
464.                       due to being next to it while it's in a gas cloud
465.                       and then you moved away; it should no longer be seen
466.                       when that happens, even if it hasn't moved */
467.                    || any_visible_region()) { /* TODO: optimize this */
468.             see_monsters();
469.         }
470.         if (gv.vision_full_recalc)
471.             vision_recalc(0); /* vision! */
472.     }
473.     if (disp.botl || disp.botlx) {
474.         bot();
475.         curs_on_u();
476.     } else if (disp.time_botl) {
477.         timebot();
478.         curs_on_u();
479.     }
480. 
481.     m_everyturn_effect(&gy.youmonst);
482. 
483.     svc.context.move = 1;
484. 
485.     if (gm.multi >= 0 && go.occupation) {
486. #if defined(MICRO) || defined(WIN32CON)
487.         mvl_abort_lev = 0;
488.         if (kbhit()) {
489.             char ch;
490. 
491.             if ((ch = pgetchar()) == ABORT)
492.                 mvl_abort_lev++;
493.             else
494.                 cmdq_add_key(CQ_CANNED, ch);
495.         }
496.         if (!mvl_abort_lev && (*go.occupation)() == 0)
497. #else
498.             if ((*go.occupation)() == 0)
499. #endif
500.                 go.occupation = 0;
501.         if (
502. #if defined(MICRO) || defined(WIN32)
503.             mvl_abort_lev ||
504. #endif
505.             monster_nearby()) {
506.             stop_occupation();
507.             reset_eat();
508.         }
509.         runmode_delay_output();
510.         return;
511.     }
512. 
513.     u.umoved = FALSE;
514. 
515.     if (gm.multi > 0) {
516.         lookaround();
517.         runmode_delay_output();
518.         if (!gm.multi) {
519.             /* lookaround may clear multi */
520.             svc.context.move = 0;
521.             return;
522.         }
523.         if (svc.context.mv) {
524.             if (gm.multi < COLNO && !--gm.multi)
525.                 end_running(TRUE);
526.             domove();
527.         } else {
528.             --gm.multi;
529.             nhassert(gc.command_count != 0);
530.             rhack(gc.cmd_key);
531.         }
532.     } else if (gm.multi == 0) {
533. #ifdef MAIL
534.         ckmailstatus();
535. #endif
536.         rhack(0);
537.     }
538.     if (u.utotype)       /* change dungeon level */
539.         deferred_goto(); /* after rhack() */
540. 
541.     if (gv.vision_full_recalc)
542.         vision_recalc(0); /* vision! */
543. #ifdef CLIPPING
544.     /* after rhack() and vision_recalc() so that the map is redrawn
545.        once with correct vision data, not twice (overshoot+correct) */
546.     cliparound(u.ux, u.uy);
547. #endif
548.     /* when running in non-tport mode, this gets done through domove() */
549.     if ((!svc.context.run || flags.runmode == RUN_TPORT)
550.         && (gm.multi && (!svc.context.travel ? !(gm.multi % 7)
551.                         : !(svm.moves % 7L)))) {
552.         if (flags.time && svc.context.run)
553.             disp.botl = TRUE;
554.         /* [should this be flush_screen() instead?] */
555.         display_nhwindow(WIN_MAP, FALSE);
556.     }
557. 
558.     if (gl.luacore && nhcb_counts[NHCB_END_TURN]) {
559.         lua_getglobal(gl.luacore, "nh_callback_run");
560.         lua_pushstring(gl.luacore, nhcb_name[NHCB_END_TURN]);
561.         nhl_pcall_handle(gl.luacore, 1, 0, "moveloop_core", NHLpa_panic);
562.         lua_settop(gl.luacore, 0);
563.     }
564. }
565. 
566. staticfn void
567. maybe_do_tutorial(void)
568. {
569.     s_level *sp = find_level("tut-1");
570. 
571.     if (!sp)
572.         return;
573. 
574.     if (ask_do_tutorial()) {
575.         assign_level(&u.ucamefrom, &u.uz);
576.         iflags.nofollowers = TRUE;
577.         schedule_goto(&sp->dlevel, UTOTYPE_NONE,
578.                       "Entering the tutorial.", (char *) 0);
579.         deferred_goto();
580.         vision_recalc(0);
581.         docrt();
582.         iflags.nofollowers = FALSE;
583.     }
584. }
585. 
586. void
587. moveloop(boolean resuming)
588. {
589.     moveloop_preamble(resuming);
590. 
591.     if (!resuming)
592.         maybe_do_tutorial();
593. 
594.     for (;;) {
595.         moveloop_core();
596.     }
597. }
598. 
599. staticfn void
600. regen_pw(int wtcap)
601. {
602.     if (u.uen < u.uenmax
603.         && ((wtcap < MOD_ENCUMBER
604.              && (!(svm.moves % ((MAXULEV + 8 - u.ulevel)
605.                               * (Role_if(PM_WIZARD) ? 3 : 4)
606.                               / 6)))) || Energy_regeneration)) {
607.         int upper = (int) (ACURR(A_WIS) + ACURR(A_INT)) / 15 + 1;
608. 
609.         if (EMagical_breathing)
610.             upper += 2;
611. 
612.         u.uen += rn1(upper, 1);
613.         if (u.uen > u.uenmax)
614.             u.uen = u.uenmax;
615.         disp.botl = TRUE;
616.         if (u.uen == u.uenmax)
617.             interrupt_multi("You feel full of energy.");
618.     }
619. }
620. 
621. #define U_CAN_REGEN() (Regeneration || (Sleepy && u.usleep))
622. 
623. /* maybe recover some lost health (or lose some when an eel out of water) */
624. staticfn void
625. regen_hp(int wtcap)
626. {
627.     int heal = 0;
628.     boolean reached_full = FALSE,
629.             encumbrance_ok = (wtcap < MOD_ENCUMBER || !u.umoved);
630. 
631.     if (Upolyd) {
632.         if (u.mh < 1) { /* shouldn't happen... */
633.             rehumanize();
634.         } else if (gy.youmonst.data->mlet == S_EEL
635.                    && !is_pool(u.ux, u.uy) && !Is_waterlevel(&u.uz)
636.                    && !Breathless) {
637.             /* eel out of water loses hp, similar to monster eels;
638.                as hp gets lower, rate of further loss slows down */
639.             if (u.mh > 1 && !Regeneration && rn2(u.mh) > rn2(8)
640.                 && (!Half_physical_damage || !(svm.moves % 2L)))
641.                 heal = -1;
642.         } else if (u.mh < u.mhmax) {
643.             if (U_CAN_REGEN() || (encumbrance_ok && !(svm.moves % 20L)))
644.                 heal = 1;
645.         }
646.         if (heal) {
647.             disp.botl = TRUE;
648.             u.mh += heal;
649.             reached_full = (u.mh == u.mhmax);
650.         }
651. 
652.     /* !Upolyd */
653.     } else {
654.         /* [when this code was in-line within moveloop(), there was
655.            no !Upolyd check here, so poly'd hero recovered lost u.uhp
656.            once u.mh reached u.mhmax; that may have been convenient
657.            for the player, but it didn't make sense for gameplay...] */
658.         if (u.uhp < u.uhpmax && (encumbrance_ok || U_CAN_REGEN())) {
659.             heal = (u.ulevel + (int)ACURR(A_CON)) > rn2(100);
660. 
661.             if (U_CAN_REGEN())
662.                 heal += 1;
663.             if (Sleepy && u.usleep)
664.                 heal++;
665. 
666.             if (heal) {
667.                 disp.botl = TRUE;
668.                 u.uhp += heal;
669.                 if (u.uhp > u.uhpmax)
670.                     u.uhp = u.uhpmax;
671.                 /* stop voluntary multi-turn activity if now fully healed */
672.                 reached_full = (u.uhp == u.uhpmax);
673.             }
674.         }
675.     }
676. 
677.     if (reached_full)
678.         interrupt_multi("You are in full health.");
679. }
680. 
681. #undef U_CAN_REGEN
682. 
683. void
684. stop_occupation(void)
685. {
686.     if (go.occupation) {
687.         if (!maybe_finished_meal(TRUE))
688.             You("stop %s.", go.occtxt);
689.         go.occupation = (int (*)(void)) 0;
690.         disp.botl = TRUE; /* in case u.uhs changed */
691.         nomul(0);
692.     } else if (gm.multi >= 0) {
693.         nomul(0);
694.     }
695.     cmdq_clear(CQ_CANNED);
696. }
697. 
698. void
699. init_sound_disp_gamewindows(void)
700. {
701.     int menu_behavior = MENU_BEHAVE_STANDARD;
702. 
703.     activate_chosen_soundlib();
704. 
705.     if (iflags.wc_splash_screen && !flags.randomall) {
706.         SoundAchievement(0, sa2_splashscreen, 0);
707.         /* ToDo: new splash screen invocation will go here */
708.     } else {
709.         SoundAchievement(0, sa2_newgame_nosplash, 0);
710.     }
711. 
712. #ifdef CHANGE_COLOR
713.     /* init_nhwindows() has already been called, so before
714.        creating the windows, check to see if there are any
715.        palette entries to alter */
716.     change_palette();
717. #endif
718. 
719.     WIN_MESSAGE = create_nhwindow(NHW_MESSAGE);
720.     if (VIA_WINDOWPORT()) {
721.         status_initialize(FALSE);
722.     } else {
723.         WIN_STATUS = create_nhwindow(NHW_STATUS);
724.     }
725.     WIN_MAP = create_nhwindow(NHW_MAP);
726.     WIN_INVEN = create_nhwindow(NHW_MENU);
727.     if (WIN_INVEN != WIN_ERR)
728.         adjust_menu_promptstyle(WIN_INVEN, &iflags.menu_headings);
729. 
730. #ifdef TTY_PERM_INVENT
731.     if (WINDOWPORT(tty) && WIN_INVEN != WIN_ERR) {
732.         menu_behavior = MENU_BEHAVE_PERMINV;
733.         prepare_perminvent(WIN_INVEN);
734.     }
735. #endif
736.     /* in case of early quit where WIN_INVEN could be destroyed before
737.        ever having been used, use it here to pacify the Qt interface */
738.     start_menu(WIN_INVEN, menu_behavior), end_menu(WIN_INVEN, (char *) 0);
739. 
740. #ifdef MACOS9
741.     /* This _is_ the right place for this - maybe we will
742.      * have to split init_sound_disp_gamewindows into
743.      * create_gamewindows and show_gamewindows to get rid of this ifdef...
744.      */
745.     if (!strcmp(windowprocs.name, "mac"))
746.         SanePositions();
747. #endif
748. 
749.     /*
750.      * The mac port is not DEPENDENT on the order of these
751.      * displays, but it looks a lot better this way...
752.      */
753. #ifndef STATUS_HILITES
754.     display_nhwindow(WIN_STATUS, FALSE);
755. #endif
756.     display_nhwindow(WIN_MESSAGE, FALSE);
757.     clear_glyph_buffer();
758.     display_nhwindow(WIN_MAP, FALSE);
759. #ifdef TTY_PERM_INVENT
760.     if (iflags.perm_invent_pending)
761.         check_perm_invent_again();
762. #endif
763. }
764. 
765. void
766. newgame(void)
767. {
768.     int i;
769. 
770.     /* make sure welcome messages are given before noticing monsters */
771.     notice_mon_off();
772.     disp.botlx = TRUE;
773.     svc.context.ident = 2;  /* id 1 is reserved for gy.youmonst */
774.     svc.context.warnlevel = 1;
775.     svc.context.next_attrib_check = 600L; /* arbitrary first setting */
776.     svc.context.tribute.enabled = TRUE;   /* turn on 3.6 tributes    */
777.     svc.context.tribute.tributesz = sizeof(struct tribute_info);
778.     get_nhuuid();
779. 
780.     for (i = LOW_PM; i < NUMMONS; i++)
781.         svm.mvitals[i].mvflags = mons[i].geno & G_NOCORPSE;
782. 
783.     init_objects(); /* must be before u_init() */
784. 
785.     flags.pantheon = -1; /* role_init() will reset this */
786.     role_init();         /* must be before init_dungeons(), u_init(),
787.                           * and init_artifacts() */
788. 
789.     init_dungeons();  /* must be before u_init() to avoid rndmonst()
790.                        * creating odd monsters for any tins and eggs
791.                        * in hero's initial inventory */
792.     init_artifacts(); /* before u_init() in case $WIZKIT specifies
793.                        * any artifacts */
794.     u_init_misc();
795. 
796.     l_nhcore_init();  /* create a Lua state that lasts until end of game */
797.     reset_glyphmap(gm_newgame);
798. #ifndef NO_SIGNAL
799.     (void) signal(SIGINT, (SIG_RET_TYPE) done1);
800. #endif
801. #ifdef NEWS
802.     if (iflags.news)
803.         display_file(NEWS, FALSE);
804. #endif
805.     /* quest_init();  --  Now part of role_init() */
806. 
807.     mklev();
808.     u_on_upstairs();
809.     vision_reset();          /* set up internals for level (after mklev) */
810.     check_special_room(FALSE);
811. 
812.     if (MON_AT(u.ux, u.uy))
813.         mnexto(m_at(u.ux, u.uy), RLOC_NOMSG);
814.     (void) makedog();
815. 
816.     u_init_inventory_attrs();
817.     docrt();
818.     flush_screen(1);
819.     bot();
820.     while (u.uroleplay.reroll && reroll_menu()) {
821.         u_init_inventory_attrs();
822.         bot();
823.     }
824.     u_init_skills_discoveries();
825. 
826.     if (wizard) {
827.         read_wizkit();
828.         obj_delivery(FALSE); /* finish wizkit */
829.     }
830. 
831.     if (flags.legacy) {
832.         com_pager(u.uroleplay.pauper ? "pauper_legacy" : "legacy");
833.     }
834. 
835.     urealtime.realtime = 0L;
836.     urealtime.start_timing = getnow();
837. #ifdef INSURANCE
838.     save_currentstate();
839. #endif
840.     program_state.something_worth_saving++; /* useful data now exists */
841. 
842.     /* Success! */
843.     welcome(TRUE);
844.     notice_mon_on(); /* now we can notice monsters */
845.     if (a11y.glyph_updates)
846.         (void) dolookaround();
847.     else
848.         notice_all_mons(TRUE);
849.     return;
850. }
851. 
852. /* show "welcome [back] to NetHack" message at program startup */
853. void
854. welcome(boolean new_game) /* false => restoring an old game */
855. {
856.     char buf[BUFSZ];
857.     boolean currentgend = Upolyd ? u.mfemale : flags.female,
858.             adrift = (u.ualign.type != u.ualignbase[A_CURRENT]);
859. 
860.     l_nhcore_call(new_game ? NHCORE_START_NEW_GAME : NHCORE_RESTORE_OLD_GAME);
861. 
862.     /* skip "welcome back" if restoring a doomed character */
863.     if (!new_game && Upolyd && ugenocided()) {
864.         /* death via self-genocide is pending */
865.         pline("You're back, but you still feel %s inside.", udeadinside());
866.         return;
867.     }
868. 
869.     if (Hallucination)
870.         pline("NetHack is filmed in front of an undead studio audience.");
871. 
872.     /*
873.      * The "welcome back" message always describes your innate form
874.      * even when polymorphed or wearing a helm of opposite alignment.
875.      * Alignment is shown unconditionally for new games; for restores
876.      * it's only shown if it has changed from its original value.
877.      * Sex is shown for new games except when it is redundant; for
878.      * restores it's only shown if different from its original value.
879.      */
880.     *buf = '\0';
881. #if 0
882.     if (new_game || u.ualignbase[A_ORIGINAL] != u.ualignbase[A_CURRENT])
883.         Sprintf(eos(buf), " %s", align_str(u.ualignbase[A_ORIGINAL]));
884. #else
885.     /*
886.      * 2026-04-24
887.      * GitHub issue https://github.com/NetHack/NetHack/issues/537
888.      * "Judging by the comment above, it should display your new alignment
889.      *  if it was changed, so align_str(u.ualignbase[A_CURRENT]) would
890.      *  probably be more appropriate. This won't affect the new game message."
891.      *
892.      * That is followed by a suggestion to revisit the matter (paraphrased):
893.      * "That's actually intentional; the comment oversimplifies.
894.      *  When it was implemented, it may have been the only way to tell that
895.      *  you had converted alignment. Now ^X mentions your starting alignment
896.      *  if base alignment has been changed, so revisiting this welcome back
897.      *  message."
898.      */
899.     if (new_game || u.ualignbase[A_ORIGINAL] != u.ualignbase[A_CURRENT] || adrift)
900.         Sprintf(eos(buf), " %s%s",
901.                 adrift ? "adrift " : "",
902.                 adrift ? align_str(u.ualign.type)
903.                        : align_str(u.ualignbase[A_CURRENT]));
904. #endif
905.     if (!gu.urole.name.f
906.         && (new_game
907.             ? (gu.urole.allow & ROLE_GENDMASK) == (ROLE_MALE | ROLE_FEMALE)
908.             : currentgend != flags.initgend))
909.         Sprintf(eos(buf), " %s", genders[currentgend].adj);
910.     Sprintf(eos(buf), " %s %s", gu.urace.adj,
911.             (currentgend && gu.urole.name.f) ? gu.urole.name.f
912.                                              : gu.urole.name.m);
913. 
914.     pline(new_game ? "%s %s, welcome to NetHack!  You are a%s."
915.                    : "%s %s, the%s, welcome back to NetHack!",
916.           Hello((struct monst *) 0), svp.plname, buf);
917. 
918.     if (new_game) {
919.         /* guarantee that 'major' event category is never empty */
920.         livelog_printf(LL_ACHIEVE, "%s the%s entered the dungeon",
921.                        svp.plname, buf);
922.     } else {
923.         /* if restoring in Gehennom, give same hot/smoky message as when
924.            first entering it */
925.         hellish_smoke_mesg();
926.         /* remind player of the level annotation, like in goto_level() */
927.         print_level_annotation();
928.     }
929. }
930. 
931. #ifdef POSITIONBAR
932. staticfn void
933. do_positionbar(void)
934. {
935.     /* FIXME: this will break if any coordinate is too big for (char);
936.        the sys/msdos/vid*.c code uses (unsigned char) which is less
937.        vulnerable but not guaranteed to be able to hold coordxy values;
938.        also, there doesn't appear to be any need for this to be static,
939.        nor to contain pairs of (> or <) and x; it could just be a full
940.        line of spaces and > or < characters with update_positionbar()
941.        revised to reconstruct the x values for non-space characters */
942.     static char pbar[COLNO];
943.     char *p;
944.     stairway *stway;
945.     coordxy x, y;
946.     int glyph, symbol;
947. 
948.     p = pbar;
949.     /* TODO: use the same method as getpos() so objects don't cover stairs */
950.     /* FIXME: traversing 'stairs' list ignores mimics that pose as stairs */
951.     for (stway = gs.stairs; stway; stway = stway->next) {
952.         x = stway->sx;
953.         y = stway->sy;
954.         glyph = levl[x][y].glyph;
955.         symbol = glyph_to_cmap(glyph);
956. 
957.         if (is_cmap_stairs(symbol)) {
958.             *p++ = (stway->up ? '<' : '>');
959.             *p++ = (char) x;
960.         }
961.      }
962. 
963.     /* hero location */
964.     if (u.ux) {
965.         *p++ = '@';
966.         *p++ = u.ux;
967.     }
968.     /* fence post */
969.     *p = 0;
970. 
971.     update_positionbar(pbar);
972. }
973. #endif
974. 
975. staticfn void
976. interrupt_multi(const char *msg)
977. {
978.     if (gm.multi > 0 && !svc.context.travel && !svc.context.run) {
979.         nomul(0);
980.         if (flags.verbose && msg)
981.             Norep("%s", msg);
982.     }
983. }
984. 
985. /* convert from time_t to number of seconds */
986. long
987. timet_to_seconds(time_t ttim)
988. {
989.     /* for Unix-based and Posix-compliant systems, a cast to 'long' would
990.        suffice but the C Standard doesn't require time_t to be that simple */
991.     return timet_delta(ttim, (time_t) 0);
992. }
993. 
994. /* calculate the difference in seconds between two time_t values */
995. long
996. timet_delta(time_t etim, time_t stim) /* end and start times */
997. {
998.     /* difftime() is a STDC routine which returns the number of seconds
999.        between two time_t values as a 'double' */
1000.     return (long) difftime(etim, stim);
1001. }
1002. 
1003. /*allmain.c*/