aboutsummaryrefslogtreecommitdiff
path: root/data.c
blob: 8be97a993f30412a47c28cbc9230038388053c9e (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
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
#include "7w.h"
#include <time.h>

int* getdeck(int era, int numplayers);
int* cards_getproduction(int era, int card);
int cards_gettype(int era, int card);
int* get_intarray(int size);
void array_cpy(int *a, int *b, int len); //copies b to a
void shuffle(int *deck, int n);

#define MISC 3
#define DATAGOLD 0

static int decks[3][49];
static int player[7][4][7]; //3 eras and extra stuff (wonder, wonder side, wonder stages completed, gold, military wins, defeats)
static int hands[7][7];
static int numplayers;
static int era;

void data_nextera()
{
 if(era == 2) return;
 era++;
 int i, j, k;
 k = 0;
 for(i = 0; i < numplayers; i++)
  for(j = 0; j < 7; j++)
   hands[i][j] = decks[era][k++];
}

void data_distributewonders()
{
 int i;
 int wonders[7];
 for(i = 0; i < 7; i++)
  wonders[i] = i+3; //wonders are numbered 3-9
 shuffle(wonders, 7);
 for(i = 0; i < numplayers; i++) {
  player[i][3][0] = wonders[i];
  player[i][3][1] = rand()%2;
 }
}

void data_init(int n)
{
 numplayers = n;
 int i, j, k, *deck;
 for(i = 0; i < 3; i++) {
  deck = getdeck(i, numplayers);
  for(j = 0; j < numplayers*7; j++)
   decks[i][j] = deck[j];
 }
 era = -1;
 data_nextera();
 data_distributewonders(n);
 for(i = 0; i < n; i++)
  for(j = 0; j < 3; j++)
   for(k = 0; k < 7; k++)
    player[i][j][k] = -1;
}

void data_passturn()
{ //remember, player numbers are arranged clockwise
 int buffer[7], i;
 if(era == 0 || era == 2) { //pass to the left
  array_cpy(buffer, hands[numplayers-1], 7);
  for(i = numplayers-1; i > 0; i--) {
   array_cpy(hands[i], hands[i-1], 7);
  }
  array_cpy(hands[0], buffer, 7);
 } else { //pass to the right
  array_cpy(buffer, hands[0], 7);
  for(i = 0; i < numplayers-1; i++) {
   array_cpy(hands[i], hands[i+1], 7);
  }
  array_cpy(hands[numplayers-1], buffer, 7);
 }
}

int* data_gethand(int p)
{
 return hands[p];
}

int data_getera()
{
 return era;
}

int data_getwonder(int p)
{
 return player[p][3][0];
}

int data_getwonderside(int p)
{
 return player[p][3][1];
}

int data_getwonderstages(int p)
{
 return player[p][3][2];
}

int data_numplayers()
{
 return numplayers;
}

void data_build(int p, int card)
{
 int i;
 for(i = 0; hands[p][i] != card; i++);
 for(; i < 7; i++) hands[p][i] = hands[p][i+1];
 hands[p][7] = -1;
 for(i = 0; player[p][era][i] != -1; i++);
 player[p][era][i] = card;
}

//0 is non producing, 1 produces one kind of resource, 2 produces multiple resources
int data_productiontype(int e, int card)
{
 int *prod = cards_getproduction(e, card);
 int i;
 int type = 0;
 for(i = 0; i < GOLD; i++) {
  if(prod[i]) {
   if(type == 1) type = 2;
   if(type == 0) type = 1;
  }
 }
 return type;
}

void data_removedefinites(int p, int *cost)
{
 int i, j, k, *prod;
 for(i = 0; i < 3; i++) {
  for(j = 0; j < 7; j++) {
   if(data_productiontype(i, player[p][p][j]) == 1) {
    prod = cards_getproduction(i, player[p][i][j]);
    for(k = 0; k < GOLD; k++) {
     if(prod[k] && cost[k]) {
      cost[k] -= prod[k];
      if(cost[k] < 0) cost[k] = 0;
     }
    }
   }
  }
 }
}

#define INDEF 16

int* data_getindefinites(int p)
{
 int *ret = get_intarray(INDEF);
 int i, j, k;
 k = 0;
 for(i = 0; i < INDEF; i++) ret[i] = 0;
 for(i = 0; i < 2; i++) {
  for(j = 0; j < 7; j++) {
   if(data_productiontype(i, player[p][i][j]) == 2) {
    ret[k++] = i;
    ret[k++] = player[p][i][j];
   }
  }
 }
 return ret;
}

int data_iszerocost(int *cost)
{
 int i;
 int free = 1;
 for(i = 0; i < GOLD; i++)
  if(cost[i] > 0) free = 0;
 return free;
}

static int recurse(int *cost, int *indef)
{
 int i;
 int ncost[GOLD], nindef[INDEF], *prod;
 for(i = 0; i < GOLD; i++) ncost[i] = cost[i];
 for(i = 0; i < INDEF; i++) nindef[i] = indef[i];
 for(i = 0; i < INDEF && nindef[i] != 0; i += 2);
 if(i < INDEF) {
  prod = cards_getproduction(nindef[i-1], nindef[i]);
  nindef[i-1] = nindef[i] = 0;
  for(i = 0; i < GOLD; i++) {
   if(prod[i] && ncost[i]) {
    ncost[i] -= prod[i];
    if(data_iszerocost(ncost)) return 1;
    if(recurse(ncost, nindef)) return 1;
    ncost[i] += prod[i];
   }
  }
 }
 return 0;
}

//return 0 if can't afford, 1 if must buy stuff from neighbors, 2 if have all resources.
int data_canafford(int p, int *cost)
{
 if(cost[GOLD] > player[p][MISC][DATAGOLD]) return 0;
 int i, j, k;
 data_removedefinites(p, cost);
 if(data_iszerocost(cost)) return 2;
 return recurse(cost, data_getindefinites(p));
}

int* data_getbuilt(int p)
{
 int *ret = get_intarray(42);
 int type, e, c;
 int i = 0;
 for(type = 0; type <= 7; type++)
  for(e = 0; e < 3; e++)
   for(c = 0; c < 7; c++)
    if(cards_gettype(e, player[p][e][c]) == type) {
     ret[i++] = e;
     ret[i++] = player[p][e][c];
    }
 return ret;
}
bgstack15