1 #include "xrisk.h"
2 #include "xrisklibex.h"
3 #include <stdarg.h>
4 #include <ctype.h>
5
6 /* Function to jump to when writing error_msg etc., extits to unix */
risk_exit(char * format,...)7 void risk_exit(char *format,...)
8 {
9 va_list ap;
10 char *args[5];
11 int argno = 0;
12
13 va_start (ap, format);
14 while ((argno<5)&&((args[argno++] = va_arg(ap, char *)) != LAST_ARG));
15 va_end (ap);
16 fprintf(stderr,format,args[0],args[1],args[2],args[3],args[4]);
17 exit(0);
18 }
19
20 /* Read then mapnumber and return it (or -1 if none) */
mapnumber(s)21 int mapnumber(s)
22 char *s;
23 {
24 int retur;
25
26 if ((*s!='#')||(s[1]<'0')||(s[1]>'9')||(s[2]<'0')||(s[2]>'9')
27 ||(s[3]<'0')||(s[3]>'9')||(s[4]!='_')) return(-1);
28 retur = atoi(&s[1]);
29 if ( retur >= MAX_MAPS ) risk_exit
30 ("Mistake in program, to high mapnumber %d\n",s,retur,LAST_ARG);
31 return(retur);
32 }
33
34 /* Return the mapping of aa sentence ( Multi-language-support) */
mappet(s)35 char *mappet(s)
36 char *s;
37 {
38 int t;
39
40 t = mapnumber(s);
41 if ( ( t == -1) || ( ! sentencemapping[t] ) ) return(s);
42 return(sentencemapping[t]);
43 }
44
45 /* Write a line in a window */
write_line(xinfo,win,fg,bg,x,y,dx,dy,msg,flag)46 int write_line(xinfo,win,fg,bg,x,y,dx,dy,msg,flag)
47 xconp xinfo;
48 Window win;
49 int fg,bg;
50 int x,y,dx,dy;
51 char *msg;
52 int flag;
53 {
54 if (!xinfo) return(1);
55 if (dy == -1) dy=FONTH+2*FILLH;
56 if (dx == -1) dx=strlen(msg)*FONTB;
57
58 if (flag&CLR)
59 {
60 if (bg == -1) XSetForeground(xinfo->disp,xinfo->gc,xinfo->black);
61 else XSetForeground(xinfo->disp,xinfo->gc,(unsigned long)bg);
62 XFillRectangle(xinfo->disp,win,xinfo->gc,x,y-dy+FILLH,dx,dy);
63 }
64
65 if (*msg)
66 {
67 char *tmp;
68 tmp = mappet(msg);
69 if (bg == -1) XSetBackground(xinfo->disp,xinfo->gc,xinfo->black);
70 else XSetBackground(xinfo->disp,xinfo->gc,(unsigned long)bg);
71
72 if (fg == -1) XSetForeground(xinfo->disp,xinfo->gc,xinfo->white);
73 else XSetForeground(xinfo->disp,xinfo->gc,(unsigned long)fg);
74
75 if (flag&CENTER) x+=(dx-strlen(tmp)*FONTB)/2;
76
77 if (flag&CLR || (!(flag&DRAWIMAGESTRING))) XDrawString
78 (xinfo->disp,win,xinfo->gc,x,y,tmp,strlen(tmp));
79 else XDrawImageString
80 (xinfo->disp,win,xinfo->gc,x,y,tmp,strlen(tmp));
81 }
82 XFlush(xinfo->disp);
83 return(0);
84 }
85
86 /* Write msg to user */
write_msg(player,msg)87 int write_msg(player, msg)
88 playerp player;
89 char *msg;
90 {
91 if ( !player || !player->xcon ) return(1);
92 if (msg)
93 {
94 strncpy(player->pstat->string[2],msg,Min(strlen(msg),MAXLEN_MISC));
95 *(player->pstat->string[2]+Min(strlen(msg),MAXLEN_MISC)) = 0;
96 write_line(player->xcon,player->xcon->win,-1,-1,MSGX,MSGY,
97 MSGLEN,-1,player->pstat->string[2],CLR|CENTER);
98 }
99 else write_line(player->xcon,player->xcon->win,-1,-1,
100 MSGX,MSGY,MSGLEN,-1,player->pstat->string[2],CLR|CENTER);
101 return(0);
102 }
103
104 /* Map argumentlist based on format-sentence */
mappargs(format,args,argno)105 void mappargs(format, args, argno)
106 char *format, **args;
107 int argno;
108 {
109 int t,flag,ant;
110 for(t=0,flag=0,ant=0;format[t]&&ant<argno;t++)
111 if (!flag)
112 {
113 if ((format[t]=='%')&& ((!t)||(format[t-1]!='\\')) ) flag=1;
114 }
115 else
116 {
117 if (format[t]=='s') args[ant]=mappet(args[ant]);
118 if ( (isalpha(format[t])) || (format[t]=='%') )
119 {
120 flag=0;
121 ant++;
122 }
123 }
124 }
125
126 /* Write msg with arguments to user */
write_msg_arg(playerp player,char * file,...)127 void write_msg_arg(playerp player,char *file,...)
128 {
129 va_list ap;
130 char *args[5];
131 char msg[MAXLEN_MISC];
132 int argno = 0;
133 va_start (ap, file);
134 file = mappet(file);
135 while ((argno<5)&&((args[argno++] = va_arg(ap, char *)) != LAST_ARG));
136 va_end (ap);
137 mappargs(file,args,argno);
138 sprintf(msg,file,args[0],args[1],args[2],args[3],args[4]);
139 write_msg(player,msg);
140 }
141
142 /* Write a msg to all players */
write_msg_all(msg)143 void write_msg_all(msg)
144 char *msg;
145 {
146 int t,t1;
147 xconp xinfo;
148 char *temp;
149 static char *linjer[SCLINES];
150 static int init=0;
151
152 if (!init) for(init=0;init<SCLINES;init++)
153 *(linjer[init]=(char *)malloc(MAXLEN_MISC+1))=0;
154
155 if (msg)
156 {
157 sprintf((temp=linjer[0]),"> %s",mappet(msg));
158 for(t=0;t<SCLINES-1;t++) linjer[t]=linjer[t+1];
159 linjer[t]=temp;
160 }
161
162 Do_each_player(t)
163 {
164 xinfo=players[t]->xcon;
165 XSetForeground(xinfo->disp,xinfo->gc,xinfo->black);
166 XFillRectangle(xinfo->disp,xinfo->scrollwin,xinfo->gc,0,0,SCWINL,SCWINH);
167 for(t1=0;t1<SCLINES;t1++) if (*linjer[t1])
168 write_line(players[t]->xcon,players[t]->xcon->scrollwin,-1,-1,
169 FONTB/2,t1*(FONTH+FILLH)+FONTH+FILLH,-1,-1,linjer[t1],0);
170 }
171 }
172
173 /* Write a message with arguments to all players */
write_msg_all_arg(char * file,...)174 void write_msg_all_arg(char *file,...)
175 {
176 va_list ap;
177 char *args[5];
178 char msg[MAXLEN_MISC];
179 int argno = 0;
180 va_start (ap, file);
181 file = mappet(file);
182 while ((argno<5)&& ((args[argno++] = va_arg(ap, char *)) != LAST_ARG) );
183 va_end (ap);
184 mappargs(file,args,argno);
185 sprintf(msg,file,args[0],args[1],args[2],args[3],args[4]);
186 write_msg_all(msg);
187 }
188
189 /* Write name of country/continent down near south-america */
write_country(xinfo,country)190 void write_country(xinfo, country)
191 xconp xinfo;
192 countryp country;
193 {
194 static countryp last_country[MAX_PLAYERS];
195
196 if(last_country[xinfo->indexs]!=country)
197 {
198 last_country[xinfo->indexs]=country;
199 if (country)
200 {
201 write_line(xinfo,xinfo->win,-1,-1,
202 TEXTX,TEXTY,WIDTH_NMISC,-1,country->name,CLR);
203 write_line(xinfo,xinfo->win,-1,-1,
204 TEXTX,TEXTY+FONTH+2*FILLH,WIDTH_NMISC,-1,
205 country->continent->name,CLR);
206 }
207 else
208 {
209 write_line(xinfo,xinfo->win,-1,-1,TEXTX,TEXTY,WIDTH_NMISC,-1,"",CLR);
210 write_line(xinfo,xinfo->win,-1,-1,
211 TEXTX,TEXTY+FONTH+2*FILLH,WIDTH_NMISC,-1,"",CLR);
212 }
213
214 }
215 }
216