aboutsummaryrefslogtreecommitdiff
path: root/io.c
blob: 6fd6d89bf366425ec3514868fb2190703331027e (plain)
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
#include <curses.h>
#include <stdlib.h>
#include "7w.h"

char* cards_getname(int era, int card);
int* cards_getcost(int era, int card);
int cards_gettype(int era, int card);
int* cards_getproduction(int era, int card);
char* getname(int res);
int gettypecolor(int type);
int* cards_getcoupons(int era, int card);
int* cards_getcouponed(int era, int card);

void io_init()
{
 initscr();
 clear();
 noecho();
 cbreak();
 curs_set(0);
 keypad(stdscr, TRUE);
 start_color();
 use_default_colors();

//This may seem slightly conveluted, but it's the curses way.
 init_pair(0, A_NORMAL, -1);
 init_pair(30, COLOR_BLACK, -1);
 init_pair(31, COLOR_RED, -1);
 init_pair(32, COLOR_GREEN, -1);
 init_pair(33, COLOR_YELLOW, -1);
 init_pair(34, COLOR_BLUE, -1);
 init_pair(35, COLOR_MAGENTA, -1);
 init_pair(36, COLOR_CYAN, -1);
 init_pair(37, COLOR_WHITE, -1);
}

int io_getkey()
{
int c;
 switch(c = getch()) {
 case KEY_LEFT:
  return LEFT;
  break;
 case KEY_RIGHT:
  return RIGHT;
  break;
 case KEY_UP:
  return UP;
  break;
 case KEY_DOWN:
  return DOWN;
  break;
 case KEY_ENTER:
 case '\n':
 case '\r':
  return ENTER;
  break;
 default:
  return c;
  break;
 }
}

void io_printname(int x, int y, int era, int card)
{
 mvprintw(y, x, "# ");
 attron(COLOR_PAIR(gettypecolor(cards_gettype(era, card))));
 printw("%-24s", cards_getname(era, card));
 attrset(A_NORMAL);
 printw(" #");
}

void io_printcard(int x, int y, int era, int card)
{
 mvprintw(y++, x, "############################");
 io_printname(x, y++, era, card);
 mvprintw(y++, x, "# Costs:   | Produces:     #");
 int *costs = cards_getcost(era, card);
 int *products = cards_getproduction(era, card);
 int i, j, k;
 i = j = -1;
 while(i < NUMRESOURCES || j < NUMPRODUCTS) {
  while(i < NUMRESOURCES && costs[++i] == 0);
  while(j < NUMPRODUCTS && products[++j] == 0);
  if(i == NUMRESOURCES && j == NUMPRODUCTS) break;
  mvprintw(y++, x, "# ");
  if(i < NUMRESOURCES) printw(" %d %-6s| ", costs[i], getname(i));
  else printw("         | ");
  int isFinal = 1;
  for(k = j+1; k < NUMPRODUCTS; k++)
   if(products[k]) isFinal = 0;
  if(j < NUMPRODUCTS)
   if(isFinal) printw(" %d %-10s #", products[j], getname(j));
   else printw(" %d %-7s or #", products[j], getname(j));
  else printw("              #");
 }
 
 int* coupons = cards_getcoupons(era, card);
 if(coupons[1] || coupons[3])
 { //print the coupons!
  mvprintw(y++, x, "# Coupon for:              #");
  if(coupons[1] != 0)
   io_printname(x, y++, coupons[0], coupons[1]);
  if(coupons[3] != 0)
   io_printname(x, y++, coupons[2], coupons[3]);
 }

 coupons = cards_getcouponed(era, card);
 if(coupons[0] || coupons[2])
 { //print the coupons!
  mvprintw(y++, x, "# Free if owned:           #");
  if(coupons[0] != 0)
   io_printname(x, y++, coupons[0], coupons[1]);
  if(coupons[2] != 0)
   io_printname(x, y++, coupons[2], coupons[3]);
 } 
 
 mvprintw(y++, x, "############################");
}
bgstack15