1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
// console.cpp: the console buffer, its display, and command line control
#include "cube.h"
#include <ctype.h>
struct cline { char *cref; int outtime; };
vector<cline> conlines;
const int ndraw = 5;
const int WORDWRAP = 80;
int conskip = 0;
bool saycommandon = false;
string commandbuf;
void setconskip(int n)
{
conskip += n;
if(conskip<0) conskip = 0;
};
COMMANDN(conskip, setconskip, ARG_1INT);
void conline(const char *sf, bool highlight) // add a line to the console buffer
{
cline cl;
cl.cref = conlines.length()>100 ? conlines.pop().cref : newstringbuf(""); // constrain the buffer size
cl.outtime = lastmillis; // for how long to keep line on screen
conlines.insert(0,cl);
if(highlight) // show line in a different colour, for chat etc.
{
cl.cref[0] = '\f';
cl.cref[1] = 0;
strcat_s(cl.cref, sf);
}
else
{
strcpy_s(cl.cref, sf);
};
puts(cl.cref);
#ifndef _WIN32
fflush(stdout);
#endif
};
void conoutf(const char *s, ...)
{
sprintf_sdv(sf, s);
s = sf;
int n = 0;
while(strlen(s)>WORDWRAP) // cut strings to fit on screen
{
string t;
strn0cpy(t, s, WORDWRAP+1);
conline(t, n++!=0);
s += WORDWRAP;
};
conline(s, n!=0);
};
void renderconsole() // render buffer taking into account time & scrolling
{
int nd = 0;
char *refs[ndraw];
loopv(conlines) if(conskip ? i>=conskip-1 || i>=conlines.length()-ndraw : lastmillis-conlines[i].outtime<20000)
{
refs[nd++] = conlines[i].cref;
if(nd==ndraw) break;
};
loopj(nd)
{
draw_text(refs[j], FONTH/3, (FONTH/4*5)*(nd-j-1)+FONTH/3, 2);
};
};
// keymap is defined externally in keymap.cfg
struct keym { int code; char *name; char *action; } keyms[256];
int numkm = 0;
void keymap(char *code, char *key, char *action)
{
keyms[numkm].code = atoi(code);
keyms[numkm].name = newstring(key);
keyms[numkm++].action = newstringbuf(action);
};
COMMAND(keymap, ARG_3STR);
void bindkey(char *key, char *action)
{
for(char *x = key; *x; x++) *x = toupper(*x);
loopi(numkm) if(strcmp(keyms[i].name, key)==0)
{
strcpy_s(keyms[i].action, action);
return;
};
conoutf("unknown key \"%s\"", key);
};
COMMANDN(bind, bindkey, ARG_2STR);
void saycommand(char *init) // turns input to the command line on or off
{
SDL_EnableUNICODE(saycommandon = (init!=NULL));
if(!editmode) keyrepeat(saycommandon);
if(!init) init = "";
strcpy_s(commandbuf, init);
};
void mapmsg(char *s) { strn0cpy(hdr.maptitle, s, 128); };
COMMAND(saycommand, ARG_VARI);
COMMAND(mapmsg, ARG_1STR);
#ifndef _WIN32
#include <X11/Xlib.h>
#include <SDL_syswm.h>
#endif
void pasteconsole()
{
#ifdef _WIN32
if(!IsClipboardFormatAvailable(CF_TEXT)) return;
if(!OpenClipboard(NULL)) return;
char *cb = (char *)GlobalLock(GetClipboardData(CF_TEXT));
strcat_s(commandbuf, cb);
GlobalUnlock(cb);
CloseClipboard();
#else
SDL_SysWMinfo wminfo;
SDL_VERSION(&wminfo.version);
wminfo.subsystem = SDL_SYSWM_X11;
if(!SDL_GetWMInfo(&wminfo)) return;
int cbsize;
char *cb = XFetchBytes(wminfo.info.x11.display, &cbsize);
if(!cb || !cbsize) return;
int commandlen = strlen(commandbuf);
for(char *cbline = cb, *cbend; commandlen + 1 < _MAXDEFSTR && cbline < &cb[cbsize]; cbline = cbend + 1)
{
cbend = (char *)memchr(cbline, '\0', &cb[cbsize] - cbline);
if(!cbend) cbend = &cb[cbsize];
if(commandlen + cbend - cbline + 1 > _MAXDEFSTR) cbend = cbline + _MAXDEFSTR - commandlen - 1;
memcpy(&commandbuf[commandlen], cbline, cbend - cbline);
commandlen += cbend - cbline;
commandbuf[commandlen] = '\n';
if(commandlen + 1 < _MAXDEFSTR && cbend < &cb[cbsize]) ++commandlen;
commandbuf[commandlen] = '\0';
};
XFree(cb);
#endif
};
cvector vhistory;
int histpos = 0;
void history(int n)
{
static bool rec = false;
if(!rec && n>=0 && n<vhistory.length())
{
rec = true;
execute(vhistory[vhistory.length()-n-1]);
rec = false;
};
};
COMMAND(history, ARG_1INT);
void keypress(int code, bool isdown, int cooked)
{
if(saycommandon) // keystrokes go to commandline
{
if(isdown)
{
switch(code)
{
case SDLK_RETURN:
break;
case SDLK_BACKSPACE:
case SDLK_LEFT:
{
for(int i = 0; commandbuf[i]; i++) if(!commandbuf[i+1]) commandbuf[i] = 0;
resetcomplete();
break;
};
case SDLK_UP:
if(histpos) strcpy_s(commandbuf, vhistory[--histpos]);
break;
case SDLK_DOWN:
if(histpos<vhistory.length()) strcpy_s(commandbuf, vhistory[histpos++]);
break;
case SDLK_TAB:
complete(commandbuf);
break;
case SDLK_v:
if(SDL_GetModState()&(KMOD_LCTRL|KMOD_RCTRL)) { pasteconsole(); return; };
default:
resetcomplete();
if(cooked) { char add[] = { cooked, 0 }; strcat_s(commandbuf, add); };
};
}
else
{
if(code==SDLK_RETURN)
{
if(commandbuf[0])
{
if(vhistory.empty() || strcmp(vhistory.last(), commandbuf))
{
vhistory.add(newstring(commandbuf)); // cap this?
};
histpos = vhistory.length();
if(commandbuf[0]=='/') execute(commandbuf, true);
else toserver(commandbuf);
};
saycommand(NULL);
}
else if(code==SDLK_ESCAPE)
{
saycommand(NULL);
};
};
}
else if(!menukey(code, isdown)) // keystrokes go to menu
{
loopi(numkm) if(keyms[i].code==code) // keystrokes go to game, lookup in keymap and execute
{
string temp;
strcpy_s(temp, keyms[i].action);
execute(temp, isdown);
return;
};
};
};
char *getcurcommand()
{
return saycommandon ? commandbuf : NULL;
};
void writebinds(FILE *f)
{
loopi(numkm)
{
if(*keyms[i].action) fprintf(f, "bind \"%s\" [%s]\n", keyms[i].name, keyms[i].action);
};
};
|
|
>
>
>
>
|
|
>
|
>
|
|
|
>
>
|
|
|
|
|
|
|
<
|
<
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
>
|
>
|
|
>
|
|
|
|
|
>
|
|
>
>
>
>
|
>
|
|
|
|
>
|
|
>
|
|
|
|
|
|
>
|
|
>
|
>
|
|
>
>
>
|
>
<
>
>
|
|
|
>
|
>
|
|
|
|
|
|
|
|
|
>
|
|
|
>
|
>
|
<
>
|
>
|
|
>
|
|
|
|
>
|
|
|
|
>
|
|
|
<
|
|
|
|
>
|
|
|
|
<
|
<
|
|
|
|
<
|
>
>
|
|
|
|
|
>
>
|
|
|
|
>
>
|
|
|
|
|
|
|
|
>
>
>
>
|
|
>
>
|
|
|
|
<
|
<
|
<
>
>
|
<
|
>
|
|
>
|
>
|
|
|
<
|
<
|
|
|
<
|
|
>
|
>
|
|
|
|
|
|
|
>
|
|
>
|
|
|
>
|
>
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// console.cpp: the console buffer, its display, and command line control
#include "cube.h"
#include <ctype.h>
struct cline {
char *cref;
int outtime;
};
vector<cline> conlines;
const int ndraw = 5;
const int WORDWRAP = 80;
int conskip = 0;
bool saycommandon = false;
string commandbuf;
void
setconskip(int n)
{
conskip += n;
if (conskip < 0)
conskip = 0;
};
COMMANDN(conskip, setconskip, ARG_1INT);
void
conline(const char *sf, bool highlight) // add a line to the console buffer
{
cline cl;
cl.cref = conlines.length() > 100
? conlines.pop().cref
: newstringbuf(""); // constrain the buffer size
cl.outtime = lastmillis; // for how long to keep line on screen
conlines.insert(0, cl);
if (highlight) // show line in a different colour, for chat etc.
{
cl.cref[0] = '\f';
cl.cref[1] = 0;
strcat_s(cl.cref, sf);
} else {
strcpy_s(cl.cref, sf);
};
puts(cl.cref);
#ifndef _WIN32
fflush(stdout);
#endif
};
void
conoutf(const char *s, ...)
{
sprintf_sdv(sf, s);
s = sf;
int n = 0;
while (strlen(s) > WORDWRAP) // cut strings to fit on screen
{
string t;
strn0cpy(t, s, WORDWRAP + 1);
conline(t, n++ != 0);
s += WORDWRAP;
};
conline(s, n != 0);
};
void
renderconsole() // render buffer taking into account time & scrolling
{
int nd = 0;
char *refs[ndraw];
loopv(conlines) if (conskip ? i >= conskip - 1 ||
i >= conlines.length() - ndraw
: lastmillis - conlines[i].outtime < 20000)
{
refs[nd++] = conlines[i].cref;
if (nd == ndraw)
break;
};
loopj(nd)
{
draw_text(refs[j], FONTH / 3,
(FONTH / 4 * 5) * (nd - j - 1) + FONTH / 3, 2);
};
};
// keymap is defined externally in keymap.cfg
struct keym {
int code;
char *name;
char *action;
} keyms[256];
int numkm = 0;
void
keymap(char *code, char *key, char *action)
{
keyms[numkm].code = atoi(code);
keyms[numkm].name = newstring(key);
keyms[numkm++].action = newstringbuf(action);
};
COMMAND(keymap, ARG_3STR);
void
bindkey(char *key, char *action)
{
for (char *x = key; *x; x++)
*x = toupper(*x);
loopi(numkm) if (strcmp(keyms[i].name, key) == 0)
{
strcpy_s(keyms[i].action, action);
return;
};
conoutf("unknown key \"%s\"", key);
};
COMMANDN(bind, bindkey, ARG_2STR);
void
saycommand(char *init) // turns input to the command line on or off
{
SDL_EnableUNICODE(saycommandon = (init != NULL));
if (!editmode)
keyrepeat(saycommandon);
if (!init)
init = "";
strcpy_s(commandbuf, init);
};
void
mapmsg(char *s)
{
strn0cpy(hdr.maptitle, s, 128);
};
COMMAND(saycommand, ARG_VARI);
COMMAND(mapmsg, ARG_1STR);
#ifndef _WIN32
#include <SDL_syswm.h>
#include <X11/Xlib.h>
#endif
void
pasteconsole()
{
#ifdef _WIN32
if (!IsClipboardFormatAvailable(CF_TEXT))
return;
if (!OpenClipboard(NULL))
return;
char *cb = (char *)GlobalLock(GetClipboardData(CF_TEXT));
strcat_s(commandbuf, cb);
GlobalUnlock(cb);
CloseClipboard();
#else
SDL_SysWMinfo wminfo;
SDL_VERSION(&wminfo.version);
wminfo.subsystem = SDL_SYSWM_X11;
if (!SDL_GetWMInfo(&wminfo))
return;
int cbsize;
char *cb = XFetchBytes(wminfo.info.x11.display, &cbsize);
if (!cb || !cbsize)
return;
int commandlen = strlen(commandbuf);
for (char *cbline = cb, *cbend;
commandlen + 1 < _MAXDEFSTR && cbline < &cb[cbsize];
cbline = cbend + 1) {
cbend = (char *)memchr(cbline, '\0', &cb[cbsize] - cbline);
if (!cbend)
cbend = &cb[cbsize];
if (commandlen + cbend - cbline + 1 > _MAXDEFSTR)
cbend = cbline + _MAXDEFSTR - commandlen - 1;
memcpy(&commandbuf[commandlen], cbline, cbend - cbline);
commandlen += cbend - cbline;
commandbuf[commandlen] = '\n';
if (commandlen + 1 < _MAXDEFSTR && cbend < &cb[cbsize])
++commandlen;
commandbuf[commandlen] = '\0';
};
XFree(cb);
#endif
};
cvector vhistory;
int histpos = 0;
void
history(int n)
{
static bool rec = false;
if (!rec && n >= 0 && n < vhistory.length()) {
rec = true;
execute(vhistory[vhistory.length() - n - 1]);
rec = false;
};
};
COMMAND(history, ARG_1INT);
void
keypress(int code, bool isdown, int cooked)
{
if (saycommandon) // keystrokes go to commandline
{
if (isdown) {
switch (code) {
case SDLK_RETURN:
break;
case SDLK_BACKSPACE:
case SDLK_LEFT: {
for (int i = 0; commandbuf[i]; i++)
if (!commandbuf[i + 1])
commandbuf[i] = 0;
resetcomplete();
break;
};
case SDLK_UP:
if (histpos)
strcpy_s(
commandbuf, vhistory[--histpos]);
break;
case SDLK_DOWN:
if (histpos < vhistory.length())
strcpy_s(
commandbuf, vhistory[histpos++]);
break;
case SDLK_TAB:
complete(commandbuf);
break;
case SDLK_v:
if (SDL_GetModState() &
(KMOD_LCTRL | KMOD_RCTRL)) {
pasteconsole();
return;
};
default:
resetcomplete();
if (cooked) {
char add[] = {cooked, 0};
strcat_s(commandbuf, add);
};
};
} else {
if (code == SDLK_RETURN) {
if (commandbuf[0]) {
if (vhistory.empty() ||
strcmp(
vhistory.last(), commandbuf)) {
vhistory.add(newstring(
commandbuf)); // cap this?
};
histpos = vhistory.length();
if (commandbuf[0] == '/')
execute(commandbuf, true);
else
toserver(commandbuf);
};
saycommand(NULL);
} else if (code == SDLK_ESCAPE) {
saycommand(NULL);
};
};
} else if (!menukey(code, isdown)) // keystrokes go to menu
{
loopi(numkm) if (keyms[i].code ==
code) // keystrokes go to game, lookup in
// keymap and execute
{
string temp;
strcpy_s(temp, keyms[i].action);
execute(temp, isdown);
return;
};
};
};
char *
getcurcommand()
{
return saycommandon ? commandbuf : NULL;
};
void
writebinds(FILE *f)
{
loopi(numkm)
{
if (*keyms[i].action)
fprintf(f, "bind \"%s\" [%s]\n", keyms[i].name,
keyms[i].action);
};
};
|