aboutsummaryrefslogtreecommitdiff
path: root/player_turn.c
blob: 62e2ff498b3a88622db096dd337fd86285e7eca6 (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
#include "7w.h"

int* data_gethand(int p);
int data_getera();
int io_getkey();
void data_endturn();
int data_numplayers();
void data_build(int p, int card);
void data_discard(int p, int card);
void data_buildwonder(int p, int card);
void data_addgold(int amnt, int p);
int data_numcards(int p);
int data_canafford(int p, int era, int card);
int data_getwonder(int p);
int data_getnextwonderstage(int p);
int data_hasbuiltname(int p, int era, int card);
int view_refresh(int focus, int cursor, int player, int mode);
int wonder_numstages(int player);
int* cards_getcost(int era, int card);
int data_numbuilt(int p);
void postmessage(char* message);
void posthelp();
void clearmessage();
int postoptions(int x, int y);
int trade_routine(int x, int y, int player);
void trade_commit(int player);
int data_spendfreebuild();
int postyn(int x, int y, char *message);
void write_trade(int player, int tradel, int trader);

void halt();

int player_build(int focus, int cursor, int player, int y)
{
 int *hand = data_gethand(player);
 int afford = data_canafford(player, data_getera(), hand[cursor]);
 if(focus == data_numplayers()) {
  if(hand[cursor] == -1) return 0;
  int choice = postoptions(61, y);
  if(choice == 0) {
   if(data_hasbuiltname(player, data_getera(), hand[cursor])) {
    postmessage("Cannot have two of the same card!");
    return 0;
   }
   if(afford == 1 || (afford == 2 && postyn(61, y, "Spend free build?"))) {
    data_build(player, hand[cursor]);
    if(afford == 2) data_spendfreebuild();
    return 1;
   }
   else postmessage("Can't afford this!");
  } else if(choice == 1) {
   data_discard(player, hand[cursor]);
   data_addgold(3, player);
   return 1;
  } else if(choice == 2) {
   if(data_getnextwonderstage(player) == -1) {
    postmessage("Wonder already complete.");
   } else if(data_canafford(player, data_getwonder(player), data_getnextwonderstage(player))) {
    data_buildwonder(player, hand[cursor]);   
    return 1;
   } else postmessage("Can't afford this!");
  }
 }
 return 0;
}

void player_turn(int player)
{
 int numcards, tradey;
 int cursor = 0; 
 int focus = data_numplayers();
 while(1) {
  numcards = data_numcards(player)+1; //include trade option in hand
  tradey = view_refresh(focus, cursor, player, 0);
  switch(io_getkey()) {
   case UP: cursor--;
    break;
   case DOWN: cursor++;
    break;
   case RIGHT: focus++;
    cursor = 0;
    break;
   case LEFT: focus--;
    cursor = 0;
    break;
   case ENTER:
    if(player_build(focus, cursor, player, tradey)) {
     trade_commit(player);
     clearmessage();
     return;
    }
    break;
   case '\t': focus = (focus+1)%(data_numplayers()+1);
    cursor = 0;
    break;
   case 'h': posthelp();
    break;
   case 'q':
    if(postyn(61, tradey, "Are you sure you want to quit?")) halt();
    break;
   default: break;
  }
  if(focus < 0) focus = data_numplayers();
  focus = focus%(data_numplayers()+1);
  if(focus == data_numplayers()) {
   if(cursor < 0) cursor = numcards-2;
   if(cursor >= numcards-1) {
    if(trade_routine(61, view_refresh(focus, cursor, player, 0), player)) cursor = 0;
    else cursor = numcards-2;
   }
  }
  else {
   if(cursor < 0) cursor = wonder_numstages((player+focus)%data_numplayers())+data_numbuilt(player+focus%data_numplayers())-1;
   cursor = cursor % (wonder_numstages((player+focus)%data_numplayers())+data_numbuilt(player+focus%data_numplayers()));
  }
 }
}
bgstack15