rogue

Ken Arnold's Rogue
git clone https://git.woozle.org/neale/rogue.git

Neale Pickett  ·  2011-10-13

edit.c

  1/*
  2 * All sorts of miscellaneous routines
  3 *
  4 * @(#)edit.c	4.66 (Berkeley) 08/06/83
  5 */
  6
  7#include <curses.h>
  8#include "netprot.h"
  9#include <ctype.h>
 10
 11/*
 12 * look:
 13 *	A quick glance all around the player
 14 */
 15# undef	DEBUG
 16
 17void
 18look(bool wakeup)
 19{
 20    int x, y;
 21    int ch;
 22    THING *tp;
 23    PLACE *pp;
 24    struct room *rp;
 25    int ey, ex;
 26    int passcount;
 27    char pfl, *fp, pch;
 28    int sy, sx, sumhero, diffhero;
 29# ifdef DEBUG
 30    static bool done = FALSE;
 31
 32    if (done)
 33	return;
 34    done = TRUE;
 35# endif DEBUG
 36    passcount = 0;
 37    rp = Proom;
 38    if (!ce(Oldpos, Hero))
 39    {
 40	erase_lamp(&Oldpos, Oldrp);
 41	Oldpos = Hero;
 42	Oldrp = rp;
 43    }
 44    ey = Hero.y + 1;
 45    ex = Hero.x + 1;
 46    sx = Hero.x - 1;
 47    sy = Hero.y - 1;
 48    if (Door_stop && !Firstmove && Running)
 49    {
 50	sumhero = Hero.y + Hero.x;
 51	diffhero = Hero.y - Hero.x;
 52    }
 53    pp = INDEX(Hero.y, Hero.x);
 54    pch = pp->p_ch;
 55    pfl = pp->p_flags;
 56
 57    for (y = sy; y <= ey; y++)
 58	if (y > 0 && y < NUMLINES - 1) for (x = sx; x <= ex; x++)
 59	{
 60	    if (x < 0 || x >= NUMCOLS)
 61		continue;
 62	    if (!on(Player, ISBLIND))
 63	    {
 64		if (y == Hero.y && x == Hero.x)
 65		    continue;
 66	    }
 67
 68	    pp = INDEX(y, x);
 69	    ch = pp->p_ch;
 70	    if (ch == ' ')		/* nothing need be done with a ' ' */
 71		    continue;
 72	    fp = &pp->p_flags;
 73	    if (pch != DOOR && ch != DOOR)
 74		if ((pfl & F_PASS) != (*fp & F_PASS))
 75		    continue;
 76	    if (((*fp & F_PASS) || ch == DOOR) && 
 77		 ((pfl & F_PASS) || pch == DOOR))
 78	    {
 79		if (Hero.x != x && Hero.y != y &&
 80		    !step_ok(chat(y, Hero.x)) && !step_ok(chat(Hero.y, x)))
 81			continue;
 82	    }
 83
 84	    if ((tp = pp->p_monst) == NULL)
 85		ch = trip_ch(y, x, ch);
 86	    else
 87		if (on(Player, SEEMONST) && on(*tp, ISINVIS))
 88		{
 89		    if (Door_stop && !Firstmove)
 90			Running = FALSE;
 91		    continue;
 92		}
 93		else
 94		{
 95		    if (wakeup)
 96			wake_monster(y, x);
 97		    if (see_monst(tp))
 98			if (on(Player, ISHALU))
 99			    ch = rnd(26) + 'A';
100			else
101			    ch = tp->t_disguise;
102		}
103	    if (on(Player, ISBLIND) && (y != Hero.y || x != Hero.x))
104		continue;
105
106	    move(y, x);
107
108	    if ((Proom->r_flags & ISDARK) && !See_floor && ch == FLOOR)
109		ch = ' ';
110
111	    if (tp != NULL || ch != inch())
112		addch(ch);
113
114	    if (Door_stop && !Firstmove && Running)
115	    {
116		switch (Runch)
117		{
118		    when 'h':
119			if (x == ex)
120			    continue;
121		    when 'j':
122			if (y == sy)
123			    continue;
124		    when 'k':
125			if (y == ey)
126			    continue;
127		    when 'l':
128			if (x == sx)
129			    continue;
130		    when 'y':
131			if ((y + x) - sumhero >= 1)
132			    continue;
133		    when 'u':
134			if ((y - x) - diffhero >= 1)
135			    continue;
136		    when 'n':
137			if ((y + x) - sumhero <= -1)
138			    continue;
139		    when 'b':
140			if ((y - x) - diffhero <= -1)
141			    continue;
142		}
143		switch (ch)
144		{
145		    case DOOR:
146			if (x == Hero.x || y == Hero.y)
147			    Running = FALSE;
148			break;
149		    case PASSAGE:
150			if (x == Hero.x || y == Hero.y)
151			    passcount++;
152			break;
153		    case FLOOR:
154		    case '|':
155		    case '-':
156		    case ' ':
157			break;
158		    default:
159			Running = FALSE;
160			break;
161		}
162	    }
163	}
164    if (Door_stop && !Firstmove && passcount > 1)
165	Running = FALSE;
166    if (!Running || !Jump)
167	mvaddch(Hero.y, Hero.x, PLAYER);
168# ifdef DEBUG
169    done = FALSE;
170# endif DEBUG
171}
172
173/*
174 * trip_ch:
175 *	Return the character appropriate for this space, taking into
176 *	account whether or not the player is tripping.
177 */
178int
179trip_ch(int y, int x, char ch)
180{
181    if (on(Player, ISHALU) && After)
182	switch (ch)
183	{
184	    case FLOOR:
185	    case ' ':
186	    case PASSAGE:
187	    case '-':
188	    case '|':
189	    case DOOR:
190	    case TRAP:
191		break;
192	    default:
193		if (y != Stairs.y || x != Stairs.x || !Seenstairs)
194		    ch = rnd_thing();
195		break;
196	}
197    return ch;
198}
199
200/*
201 * erase_lamp:
202 *	Erase the area shown by a lamp in a dark room.
203 */
204void
205erase_lamp(coord *pos, struct room *rp)
206{
207    int y, x, ey, sy, ex;
208
209    if (!(See_floor && (rp->r_flags & (ISGONE|ISDARK)) == ISDARK
210	&& !on(Player,ISBLIND)))
211	    return;
212
213    ey = pos->y + 1;
214    ex = pos->x + 1;
215    sy = pos->y - 1;
216    for (x = pos->x - 1; x <= ex; x++)
217	for (y = sy; y <= ey; y++)
218	{
219	    if (y == Hero.y && x == Hero.x)
220		continue;
221	    move(y, x);
222	    if (inch() == FLOOR)
223		addch(' ');
224	}
225}
226
227/*
228 * show_floor:
229 *	Should we show the floor in her room at this time?
230 */
231bool
232show_floor(void)
233{
234    if ((Proom->r_flags & (ISGONE|ISDARK)) == ISDARK && !on(Player, ISBLIND))
235	return See_floor;
236    else
237	return TRUE;
238}
239
240/*
241 * find_obj:
242 *	Find the unclaimed object at y, x
243 */
244THING *
245find_obj(int y, int x)
246{
247    THING *obj;
248
249    for (obj = Lvl_obj; obj != NULL; obj = next(obj))
250    {
251	if (obj->o_pos.y == y && obj->o_pos.x == x)
252		return obj;
253    }
254#ifdef MASTER
255    msg(sprintf(Prbuf, "Non-object %d,%d", y, x));
256    return NULL;
257#else
258    /* NOTREACHED */
259#endif
260}
261
262/*
263 * eat:
264 *	She wants to eat something, so let her try
265 */
266void
267eat(void)
268{
269    THING *obj;
270
271    if ((obj = get_item("eat", FOOD)) == NULL)
272	return;
273    if (obj->o_type != FOOD)
274    {
275	if (!Terse)
276	    msg("ugh, you would get ill if you ate that");
277	else
278	    msg("that's Inedible!");
279	return;
280    }
281    if (Food_left < 0)
282	Food_left = 0;
283    if ((Food_left += HUNGERTIME - 200 + rnd(400)) > STOMACHSIZE)
284	Food_left = STOMACHSIZE;
285    Hungry_state = 0;
286    if (obj == Cur_weapon)
287	Cur_weapon = NULL;
288    leave_pack(obj, FALSE, FALSE);
289    if (obj->o_which == 1)
290	msg("my, that was a yummy %s", Fruit);
291    else
292	if (rnd(100) > 70)
293	{
294	    Pstats.s_exp++;
295	    msg("%s, this food tastes awful", choose_str("bummer", "yuk"));
296	    check_level();
297	}
298	else
299	    msg("%s, that tasted good", choose_str("oh, wow", "yum"));
300}
301
302/*
303 * check_level:
304 *	Check to see if the guy has gone up a level.
305 */
306void
307check_level(void)
308{
309    int i, add, olevel;
310
311    for (i = 0; E_levels[i] != 0; i++)
312	if (E_levels[i] > Pstats.s_exp)
313	    break;
314    i++;
315    olevel = Pstats.s_lvl;
316    Pstats.s_lvl = i;
317    if (i > olevel)
318    {
319	add = roll(i - olevel, 10);
320	Max_hp += add;
321	Pstats.s_hpt += add;
322	msg("welcome to level %d", i);
323    }
324}
325
326/*
327 * chg_str:
328 *	Used to modify the playes strength.  It keeps track of the
329 *	highest it has been, just in case
330 */
331void
332chg_str(int amt)
333{
334    auto str_t comp;
335
336    if (amt == 0)
337	return;
338    add_str(&Pstats.s_str, amt);
339    comp = Pstats.s_str;
340    if (ISRING(LEFT, R_ADDSTR))
341	add_str(&comp, -Cur_ring[LEFT]->o_arm);
342    if (ISRING(RIGHT, R_ADDSTR))
343	add_str(&comp, -Cur_ring[RIGHT]->o_arm);
344    if (comp > Max_stats.s_str)
345	Max_stats.s_str = comp;
346}
347
348/*
349 * add_str:
350 *	Perform the actual add, checking upper and lower bound limits
351 */
352add_str(str_t *sp, int amt)
353{
354    if ((*sp += amt) < 3)
355	*sp = 3;
356    else if (*sp > 31)
357	*sp = 31;
358}
359
360/*
361 * add_haste:
362 *	Add a haste to the player
363 */
364bool
365add_haste(bool potion)
366{
367    if (on(Player, ISHASTE))
368    {
369	No_command += rnd(8);
370	Player.t_flags &= ~(ISRUN|ISHASTE);
371	extinguish(nohaste);
372	msg("you faint from exhaustion");
373	return FALSE;
374    }
375    else
376    {
377	Player.t_flags |= ISHASTE;
378	if (potion)
379	    fuse(nohaste, 0, rnd(4)+4, AFTER);
380	return TRUE;
381    }
382}
383
384/*
385 * aggravate:
386 *	Aggravate all the monsters on this level
387 */
388void
389aggravate(void)
390{
391    THING *mp;
392
393    for (mp = Mlist; mp != NULL; mp = next(mp))
394	runto(&mp->t_pos);
395}
396
397/*
398 * vowelstr:
399 *      For printfs: if string starts with a vowel, return "n" for an
400 *	"an".
401 */
402char *
403vowelstr(char *str)
404{
405    switch (*str)
406    {
407	case 'a': case 'A':
408	case 'e': case 'E':
409	case 'i': case 'I':
410	case 'o': case 'O':
411	case 'u': case 'U':
412	    return "n";
413	default:
414	    return "";
415    }
416}
417
418/* 
419 * is_current:
420 *	See if the object is one of the currently used items
421 */
422bool
423is_current(THING *obj)
424{
425    if (obj == NULL)
426	return FALSE;
427    if (obj == Cur_armor || obj == Cur_weapon || obj == Cur_ring[LEFT]
428	|| obj == Cur_ring[RIGHT])
429    {
430	if (!Terse)
431	    addmsg("That's already ");
432	msg("in use");
433	return TRUE;
434    }
435    return FALSE;
436}
437
438/*
439 * get_dir:
440 *      Set up the direction co_ordinate for use in varios "prefix"
441 *	commands
442 */
443bool
444get_dir(void)
445{
446    char *prompt;
447    bool gotit;
448    static coord last_delt;
449
450    if (Again && Last_dir != '\0')
451    {
452	Delta.y = last_delt.y;
453	Delta.x = last_delt.x;
454	Dir_ch = Last_dir;
455    }
456    else
457    {
458	if (!Terse)
459	    msg(prompt = "which direction? ");
460	else
461	    prompt = "direction: ";
462	do
463	{
464	    gotit = TRUE;
465	    switch (Dir_ch = readchar())
466	    {
467		when 'h': case'H': Delta.y =  0; Delta.x = -1;
468		when 'j': case'J': Delta.y =  1; Delta.x =  0;
469		when 'k': case'K': Delta.y = -1; Delta.x =  0;
470		when 'l': case'L': Delta.y =  0; Delta.x =  1;
471		when 'y': case'Y': Delta.y = -1; Delta.x = -1;
472		when 'u': case'U': Delta.y = -1; Delta.x =  1;
473		when 'b': case'B': Delta.y =  1; Delta.x = -1;
474		when 'n': case'N': Delta.y =  1; Delta.x =  1;
475		when ESCAPE: Last_dir = '\0'; reset_last(); return FALSE;
476		otherwise:
477		    Mpos = 0;
478		    msg(prompt);
479		    gotit = FALSE;
480	    }
481	} until (gotit);
482	if (isupper(Dir_ch))
483	    Dir_ch = tolower(Dir_ch);
484	Last_dir = Dir_ch;
485	last_delt.y = Delta.y;
486	last_delt.x = Delta.x;
487    }
488    if (on(Player, ISHUH) && rnd(5) == 0)
489	do
490	{
491	    Delta.y = rnd(3) - 1;
492	    Delta.x = rnd(3) - 1;
493	} while (Delta.y == 0 && Delta.x == 0);
494    Mpos = 0;
495    return TRUE;
496}
497
498/*
499 * sign:
500 *	Return the sign of the number
501 */
502int
503sign(int nm)
504{
505    if (nm < 0)
506	return -1;
507    else
508	return (nm > 0);
509}
510
511/*
512 * spread:
513 *	Give a spread around a given number (+/- 20%)
514 */
515int
516spread(int nm)
517{
518    return nm - nm / 20 + rnd(nm / 10);
519}
520
521/*
522 * call_it:
523 *	Call an object something after use.
524 */
525void
526call_it(struct obj_info *info)
527{
528    if (info->oi_know)
529    {
530	if (info->oi_guess)
531	{
532	    free(info->oi_guess);
533	    info->oi_guess = NULL;
534	}
535    }
536    else if (!info->oi_guess)
537    {
538	msg(Terse ? "call it: " : "what do you want to call it? ");
539	if (get_str(Prbuf, stdscr) == NORM)
540	{
541	    if (info->oi_guess != NULL)
542		free(info->oi_guess);
543	    info->oi_guess = malloc((unsigned int) strlen(Prbuf) + 1);
544	    strcpy(info->oi_guess, Prbuf);
545	}
546    }
547}
548
549/*
550 * rnd_thing:
551 *	Pick a random thing appropriate for this level
552 */
553int
554rnd_thing(void)
555{
556    int i;
557    static char thing_list[] = {
558	POTION, SCROLL, RING, STICK, FOOD, WEAPON, ARMOR, STAIRS, GOLD, AMULET
559    };
560
561    if (Level >= AMULETLEVEL)
562        i = rnd(sizeof thing_list / sizeof (char));
563    else
564        i = rnd(sizeof thing_list / sizeof (char) - 1);
565    return thing_list[i];
566}
567
568/*
569 str str:
570 *	Choose the first or second string depending on whether it the
571 *	player is tripping
572 */
573char *
574choose_str(char *ts, char *ns)
575{
576	return (on(Player, ISHALU) ? ts : ns);
577}