view rogue3/daemons.c @ 280:70aa5808c782

Fix potential segfaults at restore related to ctime(). In some games, restore() passes the result of ctime() to mvprintw() or some other variadic message-formatting function. If ctime() has not been declared properly, its return type is inferred to be int instead of char *. This does not cause a warning because the compiler does not know the correct type of variadic arguments. On platforms where ints and pointers are not the same size, this can, probably depending on alignment, result in a segfault that is not easy to trace. Including time.h fixes the problem. Some games manually declared ctime() and avoided the bug. These declarations have also been replaced with the include.
author John "Elwin" Edwards
date Fri, 15 Sep 2017 20:51:10 -0400
parents 527e2150eaf0
children
line wrap: on
line source

/*
 * All the daemon and fuse functions are in here
 *
 * @(#)daemons.c	3.7 (Berkeley) 6/15/81
 *
 * Rogue: Exploring the Dungeons of Doom
 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
 * All rights reserved.
 *
 * See the file LICENSE.TXT for full copyright and licensing information.
 */

#include "curses.h"
#include "rogue.h"

/*
 * doctor:
 *	A healing daemon that restors hit points after rest
 */

void
doctor()
{
    register int lv, ohp;

    lv = pstats.s_lvl;
    ohp = pstats.s_hpt;
    quiet++;
    if (lv < 8)
    {
	if (quiet > 20 - lv*2)
	    pstats.s_hpt++;
    }
    else
	if (quiet >= 3)
	    pstats.s_hpt += rnd(lv - 7)+1;
    if (ISRING(LEFT, R_REGEN))
	pstats.s_hpt++;
    if (ISRING(RIGHT, R_REGEN))
	pstats.s_hpt++;
    if (ohp != pstats.s_hpt)
    {
	if (pstats.s_hpt > max_hp)
	    pstats.s_hpt = max_hp;
	quiet = 0;
    }
}

/*
 * Swander:
 *	Called when it is time to start rolling for wandering monsters
 */

void
swander()
{
    start_daemon(rollwand, 0, BEFORE);
}

/*
 * rollwand:
 *	Called to roll to see if a wandering monster starts up
 */

int between = 0;

void
rollwand()
{
    if (++between >= 4)
    {
	if (roll(1, 6) == 4)
	{
	    wanderer();
	    kill_daemon(rollwand);
	    fuse(swander, 0, WANDERTIME, BEFORE);
	}
	between = 0;
    }
}

/*
 * unconfuse:
 *	Release the poor player from his confusion
 */

void
unconfuse()
{
    player.t_flags &= ~ISHUH;
    msg("You feel less confused now");
}


/*
 * unsee:
 *	He lost his see invisible power
 */

void
unsee()
{
    player.t_flags &= ~CANSEE;
}

/*
 * sight:
 *	He gets his sight back
 */

void
sight()
{
    if (on(player, ISBLIND))
    {
	extinguish(sight);
	player.t_flags &= ~ISBLIND;
	light(&hero);
	msg("The veil of darkness lifts");
    }
}

/*
 * nohaste:
 *	End the hasting
 */

void
nohaste()
{
    player.t_flags &= ~ISHASTE;
    msg("You feel yourself slowing down.");
}

/*
 * digest the hero's food
 */
void
stomach()
{
    register int oldfood;

    if (food_left <= 0)
    {
	/*
	 * the hero is fainting
	 */
	if (no_command || rnd(100) > 20)
	    return;
	no_command = rnd(8)+4;
	if (!terse)
	    addmsg("You feel too weak from lack of food.  ");
	msg("You faint");
	running = FALSE;
	count = 0;
	hungry_state = 3;
    }
    else
    {
	oldfood = food_left;
	food_left -= ring_eat(LEFT) + ring_eat(RIGHT) + 1 - amulet;

	if (food_left < MORETIME && oldfood >= MORETIME)
	{
	    msg("You are starting to feel weak");
	    hungry_state = 2;
	}
	else if (food_left < 2 * MORETIME && oldfood >= 2 * MORETIME)
	{
	    if (!terse)
		msg("You are starting to get hungry");
	    else
		msg("Getting hungry");
	    hungry_state = 1;
	}
    }
}