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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
|
/*
* Copyright (C) 1987, 1988 Chuck Simmons
*
* See the file COPYING, distributed with empire, for restriction
* and warranty information.
*/
/*
display.c -- This file contains routines for displaying sectors and
moving the cursor about in a sector. We need to remember the following
information:
the current map portion displayed on the screen;
whether the displayed portion is from the user's or the computer's
point of view;
*/
#include <string.h>
#include <curses.h>
#include <stdarg.h>
#include "empire.h"
#include "extern.h"
static int whose_map = UNOWNED; /* user's or computer's point of view */
static int ref_row; /* map loc displayed in upper-left corner */
static int ref_col;
static int save_sector; /* the currently displayed sector */
static int save_cursor; /* currently displayed cursor position */
static bool change_ok = true; /* true if new sector may be displayed */
static void show_loc(view_map_t vmap[],loc_t loc);
static void disp_square(view_map_t *vp);
static bool on_screen(loc_t loc);
#ifdef A_COLOR
void init_colors(void)
{
start_color();
init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
attron(A_BOLD); /* otherwise we get gray for white */
keypad(stdscr, TRUE);
}
#endif /* A_COLOR */
/*
Used for win announcements
*/
void announce (char *msg)
{
(void) addstr (msg);
}
/*
* Map input character to direction offset.
* Attempts to enable arrow and keypad keys.
*/
int direction(chtype c)
{
switch (c)
{
case 'w':
case 'W':
case KEY_UP:
return 0;
case 'e':
case 'E':
case KEY_A3:
case KEY_PPAGE:
return 1;
case 'd':
case 'D':
case KEY_RIGHT:
return 2;
case 'c':
case 'C':
case KEY_C3:
case KEY_NPAGE:
return 3;
case 'x':
case 'X':
case KEY_DOWN:
return 4;
case 'z':
case 'Z':
case KEY_C1:
case KEY_END:
return 5;
case 'a':
case 'A':
case KEY_LEFT:
return 6;
case 'q':
case 'Q':
case KEY_A1:
case KEY_HOME:
return 7;
default:
return -1;
}
}
/*
This routine is called when the current display has been
trashed and no sector is shown on the screen.
*/
void kill_display (void)
{
whose_map = UNOWNED;
}
/*
This routine is called when a new sector may be displayed on the
screen even if the location to be displayed is already on the screen.
*/
void sector_change (void)
{
change_ok = true;
}
/*
Return the currently displayed user sector, if any. If a user
sector is not displayed, return -1.
*/
int cur_sector (void)
{
if (whose_map != USER)
return (-1);
return (save_sector);
}
/*
Return the current position of the cursor. If the user's map
is not on the screen, we return -1.
*/
loc_t cur_cursor (void)
{
if (whose_map != USER)
return (-1);
return (save_cursor);
}
/*
Display a location on the screen. We figure out the sector the
location is in and display that sector. The cursor is left at
the requested location.
We redisplay the sector only if we either have been requested to
redisplay the sector, or if the location is not on the screen.
*/
void
display_loc (int whose, view_map_t vmap[], loc_t loc)
/* whose is whose map to display; loc is location to display */
{
if (change_ok || whose != whose_map || !on_screen (loc))
print_sector (whose, vmap, loc_sector (loc));
show_loc (vmap, loc);
}
/*
Display a location iff the location is on the screen.
*/
void
display_locx (int whose, view_map_t vmap[], loc_t loc)
/* whose is whose map to display; loc is location to display */
{
if (whose == whose_map && on_screen (loc))
show_loc (vmap, loc);
}
/*
Display a location which exists on the screen.
*/
void
show_loc (view_map_t vmap[], loc_t loc)
{
int r, c;
r = loc_row (loc);
c = loc_col (loc);
(void) move (r-ref_row+NUMTOPS, c-ref_col);
disp_square(&vmap[loc]);
save_cursor = loc; /* remember cursor location */
(void) move (r-ref_row+NUMTOPS, c-ref_col);
}
/*
Print a sector of the user's on the screen. If it is already displayed,
we do nothing. Otherwise we redraw the screen. Someday, some intelligence
in doing this might be interesting. We heavily depend on curses to update
the screen in a reasonable fashion.
If the desired sector
is not displayed, we clear the screen. We then update the screen
to reflect the current map. We heavily depend on curses to correctly
optimize the redrawing of the screen.
When redrawing the screen, we figure out where the
center of the sector is in relation to the map. We then compute
the screen coordinates where we want to display the center of the
sector. We will remember the sector displayed, the map displayed,
and the map location that appears in the upper-left corner of the
screen.
*/
void
print_sector(int whose, view_map_t vmap[], int sector)
/* whose is USER or COMP, vmap is map to display, sector is sector to display */
{
void display_screen();
int first_row, first_col, last_row, last_col;
int display_rows, display_cols;
int r, c;
save_sector = sector; /* remember last sector displayed */
change_ok = false; /* we are displaying a new sector */
display_rows = lines - NUMTOPS - 1; /* num lines to display */
display_cols = cols - NUMSIDES;
/* compute row and column edges of sector */
first_row = sector_row (sector) * ROWS_PER_SECTOR;
first_col = sector_col (sector) * COLS_PER_SECTOR;
last_row = first_row + ROWS_PER_SECTOR - 1;
last_col = first_col + COLS_PER_SECTOR - 1;
if (!(whose == whose_map /* correct map is on screen? */
&& ref_row <= first_row /* top row on screen? */
&& ref_col <= first_col /* first col on screen? */
&& ref_row + display_rows - 1 >= last_row /* bot row on screen? */
&& ref_col + display_cols - 1 >= last_col)) /* last col on screen? */
(void) clear (); /* erase current screen */
/* figure out first row and col to print; subtract half
the extra lines from the first line */
ref_row = first_row - (display_rows - ROWS_PER_SECTOR) / 2;
ref_col = first_col - (display_cols - COLS_PER_SECTOR) / 2;
/* try not to go past bottom of map */
if (ref_row + display_rows - 1 > MAP_HEIGHT - 1)
ref_row = MAP_HEIGHT - 1 - (display_rows - 1);
/* never go past top of map */
if (ref_row < 0) ref_row = 0;
/* same with columns */
if (ref_col + display_cols - 1 > MAP_WIDTH - 1)
ref_col = MAP_WIDTH - 1 - (display_cols - 1);
if (ref_col < 0) ref_col = 0;
whose_map = whose; /* remember whose map is displayed */
display_screen (vmap);
/* print x-coordinates along bottom of screen */
for (c = ref_col; c < ref_col + display_cols && c < MAP_WIDTH; c++)
if (c % 10 == 0) {
pos_str (lines-1, c-ref_col, "%d", c);
}
/* print y-coordinates along right of screen */
for (r = ref_row; r < ref_row + display_rows && r < MAP_HEIGHT; r++) {
if (r % 2 == 0)
pos_str (r-ref_row+NUMTOPS, cols-NUMSIDES+1, "%2d", r);
else
pos_str (r-ref_row+NUMTOPS, cols-NUMSIDES+1, " ");
}
/* print round number */
(void) sprintf (jnkbuf, "Sector %d Round %ld", sector, date);
for (r = 0; jnkbuf[r] != '\0'; r++) {
if (r+NUMTOPS >= MAP_HEIGHT) break;
(void) move (r+NUMTOPS, cols-NUMSIDES+4);
(void) addch ((chtype)jnkbuf[r]);
}
}
/*
Display the contents of a single map square.
Fancy color hacks are done here. At the moment this is kind of bogus,
because the color doesn't convey any extra information, it just looks
pretty.
*/
static void disp_square(view_map_t *vp)
{
#ifdef A_COLOR
chtype attr;
switch(vp->contents)
{
case MAP_LAND:
attr = COLOR_PAIR(COLOR_GREEN);
break;
case MAP_SEA:
attr = COLOR_PAIR(COLOR_CYAN);
break;
case 'a':
case 'f':
case 'p':
case 'd':
case 'b':
case 't':
case 'c':
case 's':
case 'z':
case 'X':
attr = COLOR_PAIR(COLOR_RED);
break;
default:
attr = COLOR_PAIR(COLOR_WHITE);
break;
}
attron(attr);
#endif /* A_COLOR */
(void) addch ((chtype)vp->contents);
#ifdef A_COLOR
attroff(attr);
attron(COLOR_PAIR(COLOR_WHITE));
#endif /* A_COLOR */
}
/*
Display the portion of the map that appears on the screen.
*/
void display_screen(view_map_t vmap[])
{
int display_rows, display_cols;
int r, c;
loc_t t;
display_rows = lines - NUMTOPS - 1; /* num lines to display */
display_cols = cols - NUMSIDES;
for (r = ref_row; r < ref_row + display_rows && r < MAP_HEIGHT; r++)
for (c = ref_col; c < ref_col + display_cols && c < MAP_WIDTH; c++) {
t = row_col_loc (r, c);
(void) move (r-ref_row+NUMTOPS, c-ref_col);
disp_square(&vmap[t]);
}
}
/*
Move the cursor in a specified direction. We return true if the
cursor remains in the currently displayed screen, otherwise false.
We display the cursor on the screen, if possible.
*/
bool
move_cursor(loc_t *cursor, int offset)
/* cursor is current cursor position, offset is offset to add to cursor */
{
loc_t t;
int r, c;
t = *cursor + offset; /* proposed location */
if (!map[t].on_board) return (false); /* trying to move off map */
if (!on_screen (t)) return (false); /* loc is off screen */
*cursor = t; /* update cursor position */
save_cursor = *cursor;
r = loc_row (save_cursor);
c = loc_col (save_cursor);
(void) move (r-ref_row+NUMTOPS, c-ref_col);
return (true);
}
/*
See if a location is displayed on the screen.
*/
bool on_screen (loc_t loc)
{
int new_r, new_c;
new_r = loc_row (loc);
new_c = loc_col (loc);
if (new_r < ref_row /* past top of screen */
|| new_r - ref_row > lines - NUMTOPS - 1 /* past bot of screen? */
|| new_c < ref_col /* past left edge of screen? */
|| new_c - ref_col > cols - NUMSIDES) /* past right edge of screen? */
return (false);
return (true);
}
/* Print a view map for debugging. */
void
print_xzoom(view_map_t *vmap)
{
print_zoom (vmap);
#if 0
prompt ("Hit a key: ",0,0,0,0,0,0,0,0);
(void) get_chx (); /* wait for user */
#endif
}
/*
Print a condensed version of the map.
*/
char zoom_list[] = "XO*tcbsdpfaTCBSDPFAzZ+. ";
void
print_zoom(view_map_t *vmap)
{
void print_zoom_cell ();
int row_inc, col_inc;
int r, c;
kill_display ();
row_inc = (MAP_HEIGHT + lines - NUMTOPS - 1) / (lines - NUMTOPS);
col_inc = (MAP_WIDTH + cols - 1) / (cols - 1);
for (r = 0; r < MAP_HEIGHT; r += row_inc)
for (c = 0; c < MAP_WIDTH; c += col_inc)
print_zoom_cell (vmap, r, c, row_inc, col_inc);
pos_str (0, 0, "Round #%d", date);
(void) refresh ();
}
/*
Print a single cell in condensed format.
*/
void
print_zoom_cell(view_map_t *vmap,
int row, int col, int row_inc, int col_inc)
{
int r, c;
char cell;
cell = ' ';
for (r = row; r < row + row_inc; r++)
for (c = col; c < col + col_inc; c++)
if (strchr (zoom_list, vmap[row_col_loc(r,c)].contents)
< strchr (zoom_list, cell))
cell = vmap[row_col_loc(r,c)].contents;
(void) move (row/row_inc + NUMTOPS, col/col_inc);
(void) addch ((chtype)cell);
}
/*
Print a condensed version of a pathmap.
*/
void
print_pzoom(char *s, path_map_t *pmap, view_map_t *vmap)
{
void print_pzoom_cell();
int row_inc, col_inc;
int r, c;
kill_display ();
row_inc = (MAP_HEIGHT + lines - NUMTOPS - 1) / (lines - NUMTOPS);
col_inc = (MAP_WIDTH + cols - 1) / (cols - 1);
for (r = 0; r < MAP_HEIGHT; r += row_inc)
for (c = 0; c < MAP_WIDTH; c += col_inc)
print_pzoom_cell (pmap, vmap, r, c, row_inc, col_inc);
prompt (s,0,0,0,0,0,0,0,0);
(void) get_chx (); /* wait for user */
(void) refresh ();
}
/*
Print a single cell of a pathmap in condensed format.
We average all squares in the cell and take the mod 10 value.
Squares with a value of -1 are printed with '-', squares with
a value of INFINITY/2 are printed with 'P', and squares with
a value of INFINITY are printed with 'Z'. Squares with a value
between P and Z are printed as U.
*/
void
print_pzoom_cell(path_map_t *pmap, view_map_t *vmap,
int row, int col, int row_inc, int col_inc)
{
int r, c;
int sum, d;
char cell;
sum = 0;
d = 0; /* number of squares in cell */
for (r = row; r < row + row_inc; r++)
for (c = col; c < col + col_inc; c++) {
sum += pmap[row_col_loc(r,c)].cost;
d += 1;
}
sum /= d;
if (pmap[row_col_loc(row,col)].terrain == T_PATH) cell = '-';
else if (sum < 0) cell = '!';
else if (sum == INFINITY/2) cell = 'P';
else if (sum == INFINITY) cell = ' ';
else if (sum > INFINITY/2) cell = 'U';
else {
sum %= 36;
if (sum < 10) cell = sum + '0';
else cell = sum - 10 + 'a';
}
if (cell == ' ')
print_zoom_cell (vmap, row, col, row_inc, col_inc);
else {
(void) move (row/row_inc + NUMTOPS, col/col_inc);
(void) addch ((chtype)cell);
}
}
/*
Display the score off in the corner of the screen.
*/
void
display_score(void)
{
pos_str (1, cols-12, " User Comp");
pos_str (2, cols-12, "%5d %5d", user_score, comp_score);
}
/*
Clear the end of a specified line starting at the specified column.
*/
void
clreol(int linep, int colp)
{
(void) move (linep, colp);
(void) clrtoeol();
}
/*
Initialize the terminal.
*/
void
ttinit(void)
{
(void) initscr();
(void) noecho();
(void) crmode();
#ifdef A_COLOR
init_colors();
#endif /* A_COLOR */
lines = LINES;
cols = COLS;
if (lines > MAP_HEIGHT + NUMTOPS + 1)
lines = MAP_HEIGHT + NUMTOPS + 1;
if (cols > MAP_WIDTH + NUMSIDES)
cols = MAP_WIDTH + NUMSIDES;
}
/*
Clear the screen. We must also kill information maintained about the
display.
*/
void
clear_screen(void)
{
(void) clear ();
(void) refresh ();
kill_display ();
}
/*
Audible complaint.
*/
void
complain(void)
{
(void) beep ();
}
/*
Redraw the screen.
*/
void
redisplay(void)
{
(void) refresh ();
}
void
redraw(void)
{
(void) clearok (curscr, TRUE);
(void) refresh ();
}
/*
Wait a little bit to give user a chance to see a message. We refresh
the screen and pause for a few milliseconds.
*/
void
delay(void)
{
int t = delay_time;
int i = 500;
(void) refresh ();
if (t > i) {
(void) move (LINES - 1, 0);
}
for (; t > 0; t -= i) {
(void) napms ((t > i) ? i : t); /* pause a bit */
if (t > i) {
addstr ("*");
refresh ();
}
}
}
/*
Clean up the display. This routine gets called as we leave the game.
*/
void
close_disp(void)
{
(void) move (LINES - 1, 0);
(void) clrtoeol ();
(void) refresh ();
(void) endwin ();
}
/*
Position the cursor and output a string.
*/
void
pos_str(int row, int col, char *str, ...)
{
va_list ap;
char junkbuf[STRSIZE];
va_start(ap, str);
(void) move (row, col);
vsprintf(junkbuf, str, ap);
(void) addstr (junkbuf);
va_end(ap);
}
/*
Print a single cell in condensed format.
*/
extern char zoom_list[];
void
print_movie_cell(char *mbuf, int row, int col, int row_inc, int col_inc)
{
int r, c;
char cell;
cell = ' ';
for (r = row; r < row + row_inc; r++)
for (c = col; c < col + col_inc; c++)
if (strchr (zoom_list, mbuf[row_col_loc(r,c)])
< strchr (zoom_list, cell))
cell = mbuf[row_col_loc(r,c)];
(void) move (row/row_inc + NUMTOPS, col/col_inc);
(void) addch ((chtype)cell);
}
/* end */
|