-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.c
289 lines (236 loc) · 6.49 KB
/
player.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
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include "player.h"
#include "common.h"
/* score constants */
#define PLAYER_BOX_X 600
#define USER_BOX_Y 470
#define COMPUTER_BOX_Y 40
#define PLAYER_BOX_Y(type) (type == USER)? USER_BOX_Y:COMPUTER_BOX_Y
#define USER_SCORE_Y 485
#define COMPUTER_SCORE_Y 55
#define PLAYER_SCORE_Y(type) (type == USER)? USER_SCORE_Y:COMPUTER_SCORE_Y
#define PLAYER_SCORE_X 620
/* bonus coordinates and constants */
#define USER_BONUS_Y 420
#define COMPUTER_BONUS_Y 150
#define PLAYER_BONUS_Y(type) (type == USER)? USER_BONUS_Y:COMPUTER_BONUS_Y
#define PLAYER_BONUS_X 600
/* file paths for fonts and images */
#define SCOREBOX_FILE GFX_DIR "scorebox.png"
#define GEORGIA_I_FILE FONTS_DIR "georgiai.ttf"
#define BONUS_SHOW_TIME 100
static void bonus_init(bonus_t *bonus, bonus_type_t type, short c)
{
bonus->type = type;
bonus->bonus_card = c;
bonus->tex = NULL;
bonus->bonus_shown = 1;
bonus->show_frames = 0;
}
inline static void bonus_init_empty(bonus_t *bonus)
{
bonus_init(bonus, NONE, -1);
}
player* player_init(type_t t, SDL_Renderer *renderer)
{
unsigned short i;
player *p = try_malloc(sizeof(player));
if (TTF_Init() == -1)
sdl_ttf_error("Initialisation failed");
for (i = 0; i < MAX_NB_CARDS_HAND; i++) {
p->hand[i].value = EMPTY;
p->hand[i].tex = NULL;
p->hand[i].position = NULL;
}
p->nb_cards_in_hand = 0;
p->score.gained_cards = 0;
p->score.points = 0;
p->type = t;
p->pos_score_box.x = PLAYER_BOX_X;
p->pos_score_box.y = PLAYER_BOX_Y(t);
p->sel_hand = -1;
p->sel_table = -1;
bonus_init_empty(&p->card_bonus);
for (i = 0; i < MAX_BONUS; i++)
p->action_bonus[i] = NULL;
p->score_box = load_image(SCOREBOX_FILE, renderer);
return p;
}
void player_distribute(card_num card_list[],player *p, unsigned short *nb_cards_remaining)
{
unsigned short i;
unsigned short posx, posy;
bool back;
for (i=0; i<MAX_NB_CARDS_HAND;i++) {
posx = PLAYER_XPOS(i);
if (p->type == USER) {
posy = 450;
back = 0;
} else {
posy = 15;
back = 1;
}
set_card(&p->hand[i], card_list[*nb_cards_remaining-1-i], posx, posy, back);
p->nb_cards_in_hand++;
}
*nb_cards_remaining -= i;
}
void add_bonus(player *p, bonus_type_t type, short c)
{
if (type == RONDA || type == TRINGLA) {
bonus_t b;
bonus_init(&b, type, c);
b.bonus_shown = 0;
p->card_bonus = b;
} else if(type == ESTE || type == MESA) {
int i;
for (i = 0; i < MAX_BONUS && p->action_bonus[i] != NULL ; i++);
p->action_bonus[i] = try_malloc(sizeof(bonus_t));
bonus_init(p->action_bonus[i], type, c);
p->action_bonus[i]->bonus_shown = 0;
}
}
void player_update_bonus(player *p)
{
int i;
for (i = 0; i < MAX_BONUS ; i++) {
if (p->action_bonus[i] != NULL)
if (p->action_bonus[i]->bonus_shown){
try_free(p->action_bonus[i]);
p->action_bonus[i] = NULL;
}
}
}
static void player_show_score(player *p, SDL_Renderer *renderer)
{
char s[13] = "";
int y = PLAYER_SCORE_Y(p->type);
/* gained cards number */
sprintf(s, "cards : %2d", p->score.gained_cards);
show_white_text(GEORGIA_I_FILE, 18, s, PLAYER_SCORE_X, y, renderer);
/* points */
y += 35;
sprintf(s, "points : %2d", p->score.points);
show_white_text(GEORGIA_I_FILE, 18, s, PLAYER_SCORE_X, y, renderer);
}
static void display_bonus(bonus_t *b, char *s, short ypos, SDL_Renderer *renderer)
{
if (!passed(BONUS_SHOW_TIME, &b->show_frames)) {
if (s != NULL)
b->tex = set_text_surf(GEORGIA_I_FILE, 20, s, 255, 255, 255, renderer);
short xpos = PLAYER_BONUS_X + (130 - tex_w(b->tex))/2;
renderTexture(b->tex, renderer, xpos, ypos, NULL);
} else {
b->bonus_shown = 1;
SDL_DestroyTexture(b->tex);
b->tex = NULL;
}
}
static void player_show_bonus(player *p, SDL_Renderer *renderer)
{
int y = PLAYER_BONUS_Y(p->type);
int diff = (p->type == USER)? -30 : 45;
if (!p->card_bonus.bonus_shown) {
char *s = NULL;
if (!p->card_bonus.tex) {
s = try_calloc(8, sizeof(char));
if (p->card_bonus.type == RONDA)
strcat(s, "Ronda");
else if (p->card_bonus.type == TRINGLA)
strcat(s, "Tringla");
}
display_bonus(&p->card_bonus, s, y, renderer);
y += diff;
if (s == NULL)
free(s);
}
int i;
for (i = 0; i < MAX_BONUS; i++, y += diff) {
if (p->action_bonus[i] == NULL)
continue;
if (!p->action_bonus[i]->bonus_shown) {
char *s = NULL;
if (!p->action_bonus[i]->tex) {
s = try_calloc(6, sizeof(char));
if (p->action_bonus[i]->type == ESTE)
strcat(s, "Este");
else if (p->action_bonus[i]->type == MESA)
strcat(s, "Mesa");
}
display_bonus(p->action_bonus[i], s, y, renderer);
try_free(s);
}
}
}
void player_render(player *p, SDL_Renderer *renderer)
{
unsigned short i;
for (i=0; i < MAX_NB_CARDS_HAND; i++)
renderTexture(p->hand[i].tex, renderer, p->hand[i].position->x, p->hand[i].position->y, NULL);
renderTexture(p->score_box, renderer, p->pos_score_box.x, p->pos_score_box.y, NULL);
player_show_score(p, renderer);
player_show_bonus(p, renderer);
}
void player_free(player *p)
{
if (!p)
return;
unsigned short i = 0;
for (i = 0; i < p->nb_cards_in_hand; i++) {
if ((p->hand[i].value != EMPTY) && (p->type != COMPUTER))
SDL_DestroyTexture(p->hand[i].tex);
try_free(p->hand[i].position);
}
SDL_DestroyTexture(p->score_box);
try_free(p);
TTF_Quit();
}
card_num get_sel_hand_val(player p)
{
if (p.sel_hand != -1)
return p.hand[p.sel_hand].value;
else
return -1;
}
inline SDL_Texture* get_sel_hand_tex(player p)
{
if (p.sel_hand != -1)
return p.hand[p.sel_hand].tex;
else
return NULL;
}
bool ronda(player *p, card_num *c)
{
if (equal(p->hand[0].value, p->hand[1].value))
*c = p->hand[0].value % 10;
else if (equal(p->hand[0].value, p->hand[2].value))
*c = p->hand[0].value % 10;
else if (equal(p->hand[1].value, p->hand[2].value))
*c = p->hand[1].value % 10;
return equal(p->hand[0].value, p->hand[1].value) ||
equal(p->hand[0].value, p->hand[2].value) ||
equal(p->hand[1].value, p->hand[2].value);
}
bool tringla(player *p, card_num *c)
{
if (equal(p->hand[0].value, p->hand[1].value) &&
equal(p->hand[0].value, p->hand[2].value))
*c = p->hand[0].value % 10;
return equal(p->hand[0].value, p->hand[1].value) &&
equal(p->hand[0].value, p->hand[2].value);
}
void set_card_bonus(player *p)
{
card_num c = EMPTY;
if (tringla(p, &c))
add_bonus(p, TRINGLA, c);
else if (ronda(p, &c))
add_bonus(p, RONDA, c);
}
void set_final_score(player *p)
{
if (p->score.gained_cards > 20)
p->score.points += p->score.gained_cards - 20;
}