Source:NetHack 1.3d/topl.c

From NetHackWiki
Jump to: navigation, search

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

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

Screenshots and source code from Hack are used under the CWI license.

1.    /*	SCCS Id: @(#)topl.c	1.3	87/07/14
2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3.    /* topl.c - version 1.0.2 */
4.    
5.    #include <stdio.h>
6.    #include "hack.h"
7.    extern char *eos();
8.    extern int CO;
9.    
10.   char toplines[BUFSZ];
11.   xchar tlx, tly;			/* set by pline; used by addtopl */
12.   
13.   struct topl {
14.   	struct topl *next_topl;
15.   	char *topl_text;
16.   } *old_toplines, *last_redone_topl;
17.   #define	OTLMAX	20		/* max nr of old toplines remembered */
18.   
19.   doredotopl(){
20.   	if(last_redone_topl)
21.   		last_redone_topl = last_redone_topl->next_topl;
22.   	if(!last_redone_topl)
23.   		last_redone_topl = old_toplines;
24.   	if(last_redone_topl){
25.   		(void) strcpy(toplines, last_redone_topl->topl_text);
26.   	}
27.   	redotoplin();
28.   	return(0);
29.   }
30.   
31.   redotoplin() {
32.   	home();
33.   	if(index(toplines, '\n')) cl_end();
34.   	putstr(toplines);
35.   	cl_end();
36.   	tlx = curx;
37.   	tly = cury;
38.   	flags.toplin = 1;
39.   	if(tly > 1)
40.   		more();
41.   }
42.   
43.   remember_topl() {
44.   register struct topl *tl;
45.   register int cnt = OTLMAX;
46.   	if(last_redone_topl &&
47.   	   !strcmp(toplines, last_redone_topl->topl_text)) return;
48.   	if(old_toplines &&
49.   	   !strcmp(toplines, old_toplines->topl_text)) return;
50.   	last_redone_topl = 0;
51.   	tl = (struct topl *)
52.   		alloc((unsigned)(strlen(toplines) + sizeof(struct topl) + 1));
53.   	tl->next_topl = old_toplines;
54.   	tl->topl_text = (char *)(tl + 1);
55.   	(void) strcpy(tl->topl_text, toplines);
56.   	old_toplines = tl;
57.   	while(cnt && tl){
58.   		cnt--;
59.   		tl = tl->next_topl;
60.   	}
61.   	if(tl && tl->next_topl){
62.   		free((char *) tl->next_topl);
63.   		tl->next_topl = 0;
64.   	}
65.   }
66.   
67.   addtopl(s) char *s; {
68.   	curs(tlx,tly);
69.   	if(tlx + strlen(s) > CO) putsym('\n');
70.   	putstr(s);
71.   	tlx = curx;
72.   	tly = cury;
73.   	flags.toplin = 1;
74.   }
75.   
76.   xmore(s)
77.   char *s;	/* allowed chars besides space/return */
78.   {
79.   	if(flags.toplin) {
80.   		curs(tlx, tly);
81.   		if(tlx + 8 > CO) putsym('\n'), tly++;
82.   	}
83.   
84.   	if(flags.standout)
85.   		standoutbeg();
86.   	putstr("--More--");
87.   	if(flags.standout)
88.   		standoutend();
89.   
90.   	xwaitforspace(s);
91.   	if(flags.toplin && tly > 1) {
92.   		home();
93.   		cl_end();
94.   		docorner(1, tly-1);
95.   	}
96.   	flags.toplin = 0;
97.   }
98.   
99.   more(){
100.  	xmore("");
101.  }
102.  
103.  cmore(s)
104.  register char *s;
105.  {
106.  	xmore(s);
107.  }
108.  
109.  clrlin(){
110.  	if(flags.toplin) {
111.  		home();
112.  		cl_end();
113.  		if(tly > 1) docorner(1, tly-1);
114.  		remember_topl();
115.  	}
116.  	flags.toplin = 0;
117.  }
118.  
119.  /*VARARGS1*/
120.  /* Because the modified mstatusline has 9 arguments KAA */
121.  pline(line,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
122.  register char *line,*arg1,*arg2,*arg3,*arg4,*arg5,*arg6,*arg7,*arg8,*arg9;
123.  {
124.  	char pbuf[BUFSZ];
125.  	register char *bp = pbuf, *tl;
126.  	register int n,n0;
127.  
128.  	if(!line || !*line) return;
129.  	if(!index(line, '%')) (void) strcpy(pbuf,line); else
130.  	(void) sprintf(pbuf,line,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9);
131.  	if(flags.toplin == 1 && !strcmp(pbuf, toplines)) return;
132.  	nscr();		/* %% */
133.  
134.  	/* If there is room on the line, print message on same line */
135.  	/* But messages like "You die..." deserve their own line */
136.  	n0 = strlen(bp);
137.  	if(flags.toplin == 1 && tly == 1 &&
138.  	    n0 + strlen(toplines) + 3 < CO-8 &&  /* leave room for --More-- */
139.  	    strncmp(bp, "You ", 4)) {
140.  		(void) strcat(toplines, "  ");
141.  		(void) strcat(toplines, bp);
142.  		tlx += 2;
143.  		addtopl(bp);
144.  		return;
145.  	}
146.  	if(flags.toplin == 1) more();
147.  	remember_topl();
148.  	toplines[0] = 0;
149.  	while(n0){
150.  		if(n0 >= CO){
151.  			/* look for appropriate cut point */
152.  			n0 = 0;
153.  			for(n = 0; n < CO; n++) if(bp[n] == ' ')
154.  				n0 = n;
155.  			if(!n0) for(n = 0; n < CO-1; n++)
156.  				if(!letter(bp[n])) n0 = n;
157.  			if(!n0) n0 = CO-2;
158.  		}
159.  		(void) strncpy((tl = eos(toplines)), bp, n0);
160.  		tl[n0] = 0;
161.  		bp += n0;
162.  
163.  		/* remove trailing spaces, but leave one */
164.  		while(n0 > 1 && tl[n0-1] == ' ' && tl[n0-2] == ' ')
165.  			tl[--n0] = 0;
166.  
167.  		n0 = strlen(bp);
168.  		if(n0 && tl[0]) (void) strcat(tl, "\n");
169.  	}
170.  	redotoplin();
171.  }
172.  
173.  putsym(c) char c; {
174.  	switch(c) {
175.  	case '\b':
176.  		backsp();
177.  		return;
178.  	case '\n':
179.  		curx = 1;
180.  		cury++;
181.  		if(cury > tly) tly = cury;
182.  		break;
183.  	default:
184.  		if(curx == CO)
185.  			putsym('\n');	/* 1 <= curx <= CO; avoid CO */
186.  		else
187.  			curx++;
188.  	}
189.  	(void) putchar(c);
190.  }
191.  
192.  putstr(s) register char *s; {
193.  	while(*s) putsym(*s++);
194.  }