-
Notifications
You must be signed in to change notification settings - Fork 34
/
output.c
437 lines (382 loc) · 11.9 KB
/
output.c
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/******************************************************************************
Curse of War -- Real Time Strategy Game for Linux.
Copyright (C) 2013 Alexey Nikolaev.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "output.h"
#include "output-common.h"
#include <curses.h>
#include <string.h>
/* Macros to map tiles location (i,j) to its position on the screen */
#define POSY(ui,i,j) ((j)+1)
#define POSX(ui,i,j) ((i)*4 + (j)*2 + 1) - (ui->xskip*(CELL_STR_LEN+1))
/* Returns a color_pair number for a player p */
int player_color(int p) {
switch(p) {
case 0: return 6; // Neutral
case 1: return 4; // Green (player)
case 2: return 5; // Blue
case 3: return 3; // Red
case 4: return 6; // Yellow
case 5: return 7; // Magenta
case 6: return 8; // Cyan
case 7: return 2; // Black
default: return -1;
}
}
/* Returns output attributes for a player p */
int player_style(int p) {
if (p != NEUTRAL)
return A_BOLD | COLOR_PAIR(player_color(p));
else
return A_NORMAL | COLOR_PAIR(player_color(p));
}
/* Outputs population at the tile (i,j) */
void output_units(struct ui *ui, struct tile *t, int i, int j) {
int p;
int num = 0;
for(p=0; p<MAX_PLAYER; ++p) {
num += t->units[p][citizen];
}
move(POSY(ui,i,j), POSX(ui,i,j));
switch (pop_to_symbol(num)) {
case 8:
addstr(":::"); break;
case 7:
addstr(".::"); break;
case 6:
addstr(" ::"); break;
case 5:
addstr(".:."); break;
case 4:
addstr(".: "); break;
case 3:
addstr(" : "); break;
case 2:
addstr("..."); break;
case 1:
addstr(".. "); break;
case 0:
addstr(" . "); break;
default:
return;
}
}
/* A function to display information about the available keys that can be used by the player */
void output_key (int y, int x, char *key, int key_style, char *s, int s_style) {
int keylen = strlen(key);
attrset(key_style);
mvaddstr(y, x+1, key);
attrset(s_style);
mvaddch(y, x, '[');
mvaddch(y, x+keylen+1, ']');
mvaddstr(y, x+keylen+3, s);
}
/* the main output function */
void output_grid(struct state *st, struct ui *ui, int ktime) {
int i, j;
for (i=0; i<st->grid.width; ++i) {
for (j=0; j<st->grid.height; ++j) {
move(POSY(ui,i,j), POSX(ui,i,j)-1);
switch (st->grid.tiles[i][j].cl) {
case mountain:
attrset(A_NORMAL | COLOR_PAIR(4));
addstr(" /\\^ ");
break;
case mine:
attrset(A_NORMAL | COLOR_PAIR(4));
addstr(" /$\\ ");
move(POSY(ui,i,j), POSX(ui,i,j)+1);
if (st->grid.tiles[i][j].pl != NEUTRAL)
attrset(A_BOLD | COLOR_PAIR(6));
else
attrset(A_NORMAL | COLOR_PAIR(6));
addstr("$");
break;
/*
case abyss:
attrset(A_NORMAL | COLOR_PAIR(6));
addstr(" % ");
break;
*/
case grassland:
attrset(A_NORMAL | COLOR_PAIR(4));
addstr(" - ");
break;
case village:
attrset(player_style(st->grid.tiles[i][j].pl));
addstr(" n ");
break;
case town:
attrset(player_style(st->grid.tiles[i][j].pl));
addstr(" i=i ");
break;
case castle:
attrset(player_style(st->grid.tiles[i][j].pl));
addstr(" W#W ");
break;
default:;
}
attrset(A_NORMAL | COLOR_PAIR(1));
if (st->grid.tiles[i][j].cl == grassland) {
attrset(player_style(st->grid.tiles[i][j].pl));
output_units(ui, &st->grid.tiles[i][j], i, j);
attrset(A_NORMAL | COLOR_PAIR(1));
}
int p;
for (p=0; p<MAX_PLAYER; ++p) {
if (p != st->controlled) {
if (st->fg[p].flag[i][j] != 0 && ((ktime + p) / 5)%10 < 10) {
attrset(player_style(p));
mvaddch(POSY(ui,i,j), POSX(ui,i,j), 'x');
attrset(A_NORMAL | COLOR_PAIR(1));
}
}
}
/* for of war */
int k;
int b = 0;
for(k=0; k<DIRECTIONS; ++k) {
int di = dirs[k].i;
int dj = dirs[k].j;
if( i+di >= 0 && i+di < st->grid.width &&
j+dj >= 0 && j+dj < st->grid.height &&
(st->grid.tiles[i+di][j+dj].units[st->controlled][citizen] > 0) ) {
b = 1;
break;
}
}
if (0 && !b){
move(POSY(ui,i,j), POSX(ui,i,j)-1);
addstr(" ");
}
// player 1 flags
if (st->fg[st->controlled].flag[i][j] != 0 && (ktime / 5)%10 < 10) {
attrset(A_BOLD | COLOR_PAIR(1));
mvaddch(POSY(ui,i,j), POSX(ui,i,j)+2, 'P');
attrset(A_NORMAL | COLOR_PAIR(1));
}
}
}
i = ui->cursor.i;
j = ui->cursor.j;
attrset(A_BOLD | COLOR_PAIR(1));
mvaddch(POSY(ui,i,j), POSX(ui,i,j)-1, '(');
mvaddch(POSY(ui,i+1,j), POSX(ui,i+1,j)-1, ')');
attrset(A_NORMAL | COLOR_PAIR(1));
/* print populations at the cursor */
int p;
char buf[32];
int y = POSY(ui,0, st->grid.height) + 1;
mvaddstr(y, 0, " Gold:");
sprintf(buf, "%li ", st->country[st->controlled].gold);
attrset(player_style(st->controlled));
mvaddstr(y, 8, buf);
mvaddstr(y, 8, buf);
attrset(A_NORMAL | COLOR_PAIR(1));
mvaddstr(y+1, 0, " Prices: 160, 240, 320.");
move(y+2, 1);
addstr("Speed: ");
attrset(player_style(st->controlled));
switch(st->speed){
case sp_fastest: addstr("Fastest"); break;
case sp_faster: addstr("Faster "); break;
case sp_fast: addstr("Fast "); break;
case sp_normal: addstr("Normal "); break;
case sp_slow: addstr("Slow "); break;
case sp_slower: addstr("Slower "); break;
case sp_slowest: addstr("Slowest"); break;
case sp_pause: addstr("Pause "); break;
}
attrset(A_BOLD | COLOR_PAIR(1));
int text_style = A_NORMAL | COLOR_PAIR(1);
int key_style = player_style(st->controlled);
output_key (y+4, 1, "Space", key_style, "add/remove a flag", text_style);
output_key (y+5, 1, "R or V", key_style, "build", text_style);
output_key (y+4, 30, "X", key_style, "remove all flags", text_style);
output_key (y+5, 30, "C", key_style, "remove 50\% of flags", text_style);
output_key (y+5, 57, "S", key_style, "slow down", text_style);
output_key (y+4, 57, "F", key_style, "speed up", text_style);
output_key (y+6, 57, "P", key_style, "pause", text_style);
mvaddstr(y+1, 30, " Population at the cursor:");
for(p=0; p<MAX_PLAYER; ++p) {
if (p == NEUTRAL) continue;
attrset(player_style(p));
sprintf(buf, "%3i", st->grid.tiles[i][j].units[p][citizen]);
mvaddstr(y+2, 30 + p*5, buf);
/*
sprintf(buf, "%10li", st->country[p].gold);
mvaddstr(y+2, 10 + p*12, buf);
*/
}
attrset(A_NORMAL | COLOR_PAIR(1));
mvaddstr(y, 65, "Date:");
attrset(key_style);
int year, month, day;
time_to_ymd(st->time, &year, &month, &day);
sprintf(buf, "%i-%02i-%02i ", year, month, day);
mvaddstr(y, 72, buf);
refresh();
}
void output_dialog_quit_on(struct state *st, struct ui *ui) {
int y = POSY(ui,st->grid.width/2, st->grid.height/2) ;
int x = POSX(ui,st->grid.width/2, st->grid.height/2) - 8;
attrset(A_NORMAL | COLOR_PAIR(1));
mvaddstr(y-2, x, " ");
mvaddstr(y-1, x, " Quit? [Y/N] ");
mvaddstr(y+0, x, " [Q/Esc] ");
mvaddstr(y+1, x, " ");
int text_style = A_NORMAL | COLOR_PAIR(1);
int key_style = player_style(st->controlled);
output_key (y-1, x+9, "Y/N", key_style, "", text_style);
/* output_key (y+0, x+8, "Q/Esc", key_style, "", text_style); */
attrset(A_NORMAL | COLOR_PAIR(1));
refresh();
}
void output_dialog_quit_off(struct state *st, struct ui *ui) {
int y = POSY(ui,st->grid.width/2, st->grid.height/2);
int x = POSX(ui,st->grid.width/2, st->grid.height/2) - 8;
mvaddstr(y-1, x, " ");
mvaddstr(y+0, x, " ");
refresh();
}
#define TIMELINE_HEIGHT 5
/* helper function to output sorted values */
void insert_position(int pl[TIMELINE_HEIGHT], float val[TIMELINE_HEIGHT], int i, int p, float v) {
if (i>=TIMELINE_HEIGHT) return;
if (pl[i] == 0) {
pl[i] = p;
val[i] = v;
}
else {
if (val[i] >= v) {
insert_position(pl, val, i+1, p, v);
}
else {
int tp = pl[i]; float tv = val[i];
pl[i] = p; val[i] = v;
insert_position(pl, val, i+1, tp, tv);
}
}
}
void output_timeline(struct state *st, struct ui *ui) {
struct timeline *t = &st->timeline;
int non_zero[MAX_PLAYER];
int p, i;
int v;
for(p=0; p<MAX_PLAYER; ++p) {
non_zero[p] = 0;
for(i=0; i<=t->mark; ++i) {
v = t->data[p][i];
if (v >= 0.1) non_zero[p] = 1;
}
}
float max=0.0;
float min=0.0;
for(p=0; p<MAX_PLAYER; ++p) {
if (non_zero[p]) {
max = t->data[p][0];
min = max;
}
}
for(p=0; p<MAX_PLAYER; ++p) {
if (non_zero[p]) {
for(i=0; i<=t->mark; ++i) {
v = t->data[p][i];
if (v>max) max = v;
if (v<min) min = v;
}
}
}
/* adjust when min is close to max */
if (max - min < 0.1) max = min + 0.1;
float one_over_delta = 1.0 / (max - min);
int y0 = POSY(ui,0, st->grid.height) + 8;
int x0 = 2;
/* clean plotting area */
int j;
for(j=0; j<TIMELINE_HEIGHT+1; ++j) {
move(y0+j, x0);
for(i=0; i<MAX_TIMELINE_MARK + 15; ++i) {
addch(' ');
}
}
/*
mvaddch(y0, x0-1, '/');
mvaddch(y0+TIMELINE_HEIGHT-1, x0-1, '\\');
*/
/* add dates */
int y1, m1, d1;
int y2, m2, d2;
int x_shift_year = -1;
char buf[20];
attrset(A_NORMAL | COLOR_PAIR(1));
for(i=1-x_shift_year; i<=t->mark; ++i) {
time_to_ymd(t->time[i-1], &y1, &m1, &d1);
time_to_ymd(t->time[i], &y2, &m2, &d2);
if (y1 < y2) {
sprintf(buf, "%i", y2);
mvaddstr(y0+TIMELINE_HEIGHT-1+1, x0+i+x_shift_year, buf);
for(j=0; j<TIMELINE_HEIGHT-1; ++j) {
mvaddch(y0+j, x0+i, '.');
}
}
}
/* plotting */
int dy;
int dx;
int pp;
int store_pl_row[MAX_PLAYER];
for(i=0; i<=t->mark; ++i) {
char c = '-';
for(pp=0; pp<=MAX_PLAYER; ++pp) {
p = (pp+i) % MAX_PLAYER;
if (pp == MAX_PLAYER) {p = st->controlled; c = '*';}
if (non_zero[p]) {
attrset(player_style(p));
dx = i;
v = (int) round( (TIMELINE_HEIGHT-1) * (t->data[p][i] - min) * one_over_delta );
dy = TIMELINE_HEIGHT-1 - v;
dy = IN_SEGMENT(dy, 0, TIMELINE_HEIGHT-1);
if (i==t->mark) /* save the row for later use */
store_pl_row[p] = dy; /* 0 = maximum, TIMELINE_HEIGHT-1 = minimum */
mvaddch(y0+dy, x0+dx, c);
}
}
}
attrset(A_NORMAL | COLOR_PAIR(1));
sprintf(buf, "%g", max);
mvaddstr(y0, x0, buf);
sprintf(buf, "%g", min);
mvaddstr(y0+TIMELINE_HEIGHT-1, x0, buf);
/* add values */
int pl_arr[TIMELINE_HEIGHT];
float val_arr[TIMELINE_HEIGHT];
for(i=0; i<TIMELINE_HEIGHT; ++i) {
pl_arr[i] = 0;
val_arr[i] = 0.0;
}
for (p=1; p<MAX_PLAYER; ++p) {
if (non_zero[p])
insert_position(pl_arr, val_arr, store_pl_row[p], p, t->data[p][t->mark]);
}
for (i=0; i<TIMELINE_HEIGHT; ++i) {
if (pl_arr[i] != 0) {
dx = t->mark + 3;
dy = i;
sprintf(buf, "%g", val_arr[i]);
attrset(player_style(pl_arr[i]));
mvaddstr(y0+dy, x0+dx, buf);
}
}
attrset(A_NORMAL | COLOR_PAIR(1));
}