File: player.c

package info (click to toggle)
tuxpuck 0.8.2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 880 kB
  • ctags: 460
  • sloc: ansic: 3,319; makefile: 108
file content (274 lines) | stat: -rw-r--r-- 6,879 bytes parent folder | download | duplicates (8)
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
/* player.c - Copyright (C) 2001-2002 Jacob Kroon, see COPYING for details */

#include <stdlib.h>
#include <math.h>
#include <SDL_mouse.h>
#include "video.h"
#include "tuxpuck.h"

/* defines */
#define MIN_MOUSE_SPEED		((float)0.05)
#define MAX_MOUSE_SPEED		((float)0.50)

/* structs */
struct _HumanPlayer {
  Uint8 points;
  char *name;
  Pad *pad;
  float speed;
};

/* functions */
static float _mod(float a, float b)
{
  return (a - floor(a / b) * b);
}

/* It is not absolutely exactly (+- 1 bounce difference),
 * but I think it needn't be.
 * -- Ernst Moritz Hahn
 *
 * This function is temporarly commented since it isnt used (yet),
 * and thus gives a warning when compiling.
 * -- Jacob Kroon

static float _calc_bounces(float z, float dx, float dz)
{
  float n, x;

  if(dz==0) return 0;
  n = (35 - z) / dz;
  x = n * dx;
  return fabs(x / 40);
}
*/

static float _calc_puck_x(float x, float z, float dx, float dz, float ax)
{
  float board_w;
  float raw_x, rx, g, h, n;

  board_w = 40 - PUCK_W;
  n = (ax - z) / dz;
  raw_x = n * dx + x + board_w / 2;
  rx = _mod(raw_x, board_w);
  g = raw_x / board_w;
  h = fabs(_mod(g, 2));
  if ((h > 1))
    rx = board_w - rx;
  rx = rx - board_w / 2;
  return rx;
}

static void _dumb_really_idle(AIPlayer * player, Uint32 time)
{
  static float counter = M_PI_2;
  float dx, dz, puck_x, puck_z, pad_x, pad_z;

  counter += 0.005 * time;
  entity_get_position(player->pad, &pad_x, &pad_z);
  entity_get_position((Entity *) player->puck, &puck_x, &puck_z);
  dx = (sin(counter / 2.0) * 0.03 + (puck_x - pad_x) * 0.005) * player->speed;
  dz = (sin(counter / 4.0) * 0.01 + (35 - pad_z) * 0.005) * player->speed;
  entity_set_velocity(player->pad, dx, dz);
}

static void _smart_really_idle(AIPlayer * player, Uint32 time)
{
  static float counter = M_PI_2;
  float dx, dz, puck_x, puck_z, puck_dx, puck_dz, pad_x, pad_z, x;

  counter += 0.005 * time;
  entity_get_position(player->pad, &pad_x, &pad_z);
  entity_get_position((Entity *) player->puck, &puck_x, &puck_z);
  entity_get_velocity((Entity *) player->puck, &puck_dx, &puck_dz);
  if (puck_dz != 0) {
    x = _calc_puck_x(puck_x, puck_z, puck_dx, puck_dz, 35);
    dx =
      (sin(counter / 2.0) * 0.03 + (puck_x - pad_x) * 0.005) * player->speed;
    dz = (sin(counter / 4.0) * 0.01 + (40 - pad_z) * 0.005) * player->speed;
    if (puck_dz > 0)
      dx = ((x - pad_x) * 0.005) * player->speed;
    else
      dx = (0 - pad_x) * 0.005;
    dz = ((35 - pad_z) * 0.005) * player->speed;
    entity_set_velocity(player->pad, dx, dz);
  }
}

HumanPlayer *human_create(Pad * pad, char *name)
{
  HumanPlayer *human = NULL;

  human = malloc(sizeof(HumanPlayer));
  human->name = name;
  human->pad = pad;
  human->points = 0;
  human->speed = (MAX_MOUSE_SPEED + MIN_MOUSE_SPEED) / 2.0;
  return human;
}

void human_give_point(HumanPlayer * human)
{
  human->points++;
}

Uint8 human_get_points(HumanPlayer * human)
{
  return human->points;
}

void human_free(HumanPlayer * human)
{
  free(human);
}

void human_set_speed(HumanPlayer * human, Uint8 speed)
{
  human->speed =
    MIN_MOUSE_SPEED + (MAX_MOUSE_SPEED - MIN_MOUSE_SPEED) * speed / 10.0;
}

void human_update(HumanPlayer * human, Uint32 time)
{
  int dx, dy;

  SDL_GetRelativeMouseState(&dx, &dy);
  if (time != 0)
    entity_set_velocity(human->pad, (float) dx / time * human->speed,
			(float) -dy / time * human->speed);
  else
    entity_set_velocity(human->pad, 0, 0);
}

void aiplayer_blit(AIPlayer * player)
{
  video_blit(player->sdl_image, NULL, &player->rect);
}

void aiplayer_erase(AIPlayer * player)
{
  video_erase(&player->rect);
}

void aiplayer_set_alpha(AIPlayer * player, Uint8 alpha)
{
  video_set_alpha(player->sdl_image, alpha);
}

void aiplayer_update(AIPlayer * player, Uint32 time)
{
  switch (player->state) {
  case PLAYER_STATE_IDLE:
    player->strategy.idle(player, time);
    break;
  case PLAYER_STATE_SERVE:
    player->strategy.serve(player, time);
    break;
  case PLAYER_STATE_BACKUP:
    player->strategy.backup(player, time);
    break;
  case PLAYER_STATE_AIM:
    player->strategy.aim(player, time);
    break;
  case PLAYER_STATE_HIT:
    player->strategy.hit(player, time);
    break;
  case PLAYER_STATE_WIN_POINT:
  case PLAYER_STATE_LOOSE_POINT:
    _dumb_really_idle(player, time);
    if (player->ready() && board_get_state() == BOARD_STATE_PLAY) {
      if (board_get_turn() == 2)
	player->set_state(player, PLAYER_STATE_SERVE);
      else
	player->set_state(player, PLAYER_STATE_IDLE);
    }
    break;
  default:
    _dumb_really_idle(player, time);
    break;
  }
}

void dumb_idle(AIPlayer * player, Uint32 time)
{
  float puck_z;

  _dumb_really_idle(player, time);
  entity_get_position((Entity *) player->puck, NULL, &puck_z);
  if (puck_z > 20)
    player->set_state(player, PLAYER_STATE_BACKUP);
}

void dumb_serve(AIPlayer * player, Uint32 time)
{
  player->set_state(player, PLAYER_STATE_BACKUP);
}

void dumb_backup(AIPlayer * player, Uint32 time)
{
  float pad_x;

  entity_get_position(player->pad, &pad_x, NULL);
  if (entity_move_towards
      (player->pad, pad_x, 38, 0.03 * player->speed, time) == 0)
    player->set_state(player, PLAYER_STATE_AIM);
}

void dumb_aim(AIPlayer * player, Uint32 time)
{
  float hit_speed, dx, dz, puck_x, puck_z, pad_x, pad_z;

  entity_get_position(player->pad, &pad_x, &pad_z);
  entity_get_position((Entity *) player->puck, &puck_x, &puck_z);
  dx = (puck_x - pad_x);
  dz = (puck_z - pad_z);
  if (dz > -0.4) {
    player->set_state(player, PLAYER_STATE_BACKUP);
    return;
  }
  hit_speed = sqrt(dx * dx + dz * dz);
  dx = dx / hit_speed * player->hit_power * 0.1;
  dz = dz / hit_speed * player->hit_power * 0.1;
  player->set_state(player, PLAYER_STATE_HIT);
  entity_set_velocity(player->pad, dx, dz);
}

void dumb_hit(AIPlayer * player, Uint32 time)
{
  float pad_z;

  entity_get_position(player->pad, NULL, &pad_z);
  if (pad_z < 21)
    player->set_state(player, PLAYER_STATE_IDLE);
}

void smart_idle(AIPlayer * player, Uint32 time)
{
  float puck_z;

  _smart_really_idle(player, time);
  entity_get_position((Entity *) player->puck, NULL, &puck_z);
  if (puck_z > 20)
    player->set_state(player, PLAYER_STATE_BACKUP);
}

void smart_backup(AIPlayer * player, Uint32 time)
{
  float pad_x, puck_x, puck_z, puck_dx, puck_dz, x;

  entity_get_position(player->pad, &pad_x, NULL);
  entity_get_position((Entity *) player->puck, &puck_x, &puck_z);
  entity_get_velocity((Entity *) player->puck, &puck_dx, &puck_dz);

  if (fabs(puck_dx) > 0.15) {
    x = _calc_puck_x(puck_x, puck_z, puck_dx, puck_dz, 35);
    entity_move_towards(player->pad, x, 35, 0.03 * player->speed, time);
    player->set_state(player, PLAYER_STATE_BACKUP);
  } else {
    if (entity_move_towards
	(player->pad, pad_x, 38, 0.03 * player->speed, time) == 0) {
      player->set_state(player, PLAYER_STATE_AIM);
    }
  }
}