summaryrefslogtreecommitdiff
path: root/libgame/game-match.c
blob: a5ef1049e04fce455c5b47652931532f7bfbffdb (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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 *  Copyright (C) 2006 Benjamin Otte <otte@gnome.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#include "game-private.h"
#include "game-match.h"
#include "game-portable.h"

enum {
  PROP_0,
  PROP_RUNNING,
  PROP_PAUSED,
  PROP_CAN_PAUSE,
  PROP_CAN_UNPAUSE,
  PROP_TICKING,
  PROP_TICK_RATE
};

static GameObjectClass *parent_class = NULL;

static gboolean
game_match_tick_cb (gpointer data)
{
  GameMatch *match = GAME_MATCH (data);
  GameMatchClass *klass = GAME_MATCH_GET_CLASS (match);

  g_return_val_if_fail (klass->tick != NULL, TRUE);
  g_object_ref (match);
  klass->tick (match);
  g_object_unref (match);
  
  return TRUE;
}

static void
game_match_do_tick (GameMatch *match)
{
  GTimeVal current;

  g_get_current_time (&current);
  while (game_time_val_difference (&current, &match->next_tick) >= 0 &&
         match->running) {
    game_match_tick (match);
    if (match->running) {
      g_time_val_add (&match->next_tick, 
	  GAME_OBJECT (match)->game->frame_rate * G_USEC_PER_SEC / 1000);
      g_get_current_time (&current);
    }
  }
}

void
game_match_start_ticking (GameMatch *match)
{
  glong diff;

  g_return_if_fail (GAME_IS_MATCH (match));
  if (match->tick_source != 0)
    return;
  
  diff = match->next_tick.tv_sec * G_USEC_PER_SEC + match->next_tick.tv_usec;
  g_get_current_time (&match->next_tick);
  g_time_val_add (&match->next_tick, diff);
  match->tick_source = g_timeout_add (match->tick_rate, game_match_tick_cb, match);
  g_object_notify (G_OBJECT (match), "ticking");
}

void
game_match_stop_ticking (GameMatch *match)
{
  GTimeVal current;
  glong diff;
  
  g_return_if_fail (GAME_IS_MATCH (match));
  if (match->tick_source == 0)
    return;

  g_get_current_time (&current);
  diff = game_time_val_difference (&match->next_tick, &current);
  match->next_tick.tv_sec = 0;
  match->next_tick.tv_usec = 0;
  g_time_val_add (&match->next_tick, diff);
  
  if (!g_source_remove (match->tick_source))
    g_warning ("Could not remove timeout handler %d of match %p\n", match->tick_source, match);
  match->tick_source = 0;
  g_object_notify (G_OBJECT (match), "ticking");
}

static void
game_match_get_property (GObject *obj, guint param_id, GValue *value, 
    GParamSpec * pspec)
{
  GameMatch *match = GAME_MATCH (obj);
  
  switch (param_id) {
    case PROP_RUNNING:
      g_value_set_boolean (value, match->running);
      break;
    case PROP_PAUSED:
      g_value_set_boolean (value, match->paused);
      break;
    case PROP_CAN_PAUSE:
      g_value_set_boolean (value, match->can_pause);
      break;
    case PROP_CAN_UNPAUSE:
      g_value_set_boolean (value, match->can_unpause);
      break;
    case PROP_TICKING:
      g_value_set_boolean (value, match->tick_source != 0);
      break;
    case PROP_TICK_RATE:
      g_value_set_uint (value, match->tick_rate);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (match, param_id, pspec);
      break;
  }
}

static void
game_match_set_property (GObject *obj, guint param_id, const GValue *value,
    GParamSpec *pspec)
{
  GameMatch *match = GAME_MATCH (obj);

  switch (param_id) {
    case PROP_PAUSED:
    {
      GameMatchClass *klass = GAME_MATCH_GET_CLASS (match);
      if (g_value_get_boolean (value)) {
	if (match->can_pause && !match->paused) {
	  g_return_if_fail (klass->pause);
	  klass->pause (match, TRUE);
	}
      } else {
	if (match->can_unpause && match->paused) {
	  g_return_if_fail (klass->pause);
	  klass->pause (match, FALSE);
	}
      }
      break;
    }
    case PROP_TICK_RATE:
      match->tick_rate = g_value_get_uint (value);
      if (match->tick_source != 0) {
	game_match_stop_ticking (match);
	game_match_start_ticking (match);
      }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (match, param_id, pspec);
      break;
  }
}

static void
game_match_pause (GameMatch *match, gboolean pause)
{
  if (pause == match->paused)
    return;

  if (pause)
    game_match_stop_ticking (match);
  else
    game_match_start_ticking (match);
  match->paused = pause;
  //g_object_notify (G_OBJECT (match), "paused");
}

static gboolean
unref_player (GameContainer *match, GameObject *player, gpointer unused)
{
  g_object_unref (player);
  return TRUE;
}

static char *
game_match_to_string (GameObject *object)
{
  GameMatch *match = GAME_MATCH (object);
  
  return g_strdup_printf ("%s %p with %u players%s", G_OBJECT_TYPE_NAME(object), object, 
      game_container_get_size (GAME_CONTAINER (object)), 
      match->running ? " (running)" : "");
}

static void
game_match_flush (GameObject *object)
{
  game_container_flush (GAME_CONTAINER (object));
}

static void
game_match_finalize (GObject *object)
{
  game_container_foreach (GAME_CONTAINER (object), unref_player, NULL);
  game_container_clear (GAME_CONTAINER (object));

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
game_match_start (GameMatch *match)
{
  game_match_start_ticking (match);
}

static void
game_match_stop (GameMatch *match)
{
  game_match_stop_ticking (match);
  match->next_tick.tv_sec = 0;
  match->next_tick.tv_usec = 0;
}

static GamePlayer *
game_match_do_add_player (GameMatch *match, const char *name, const GameColor *color)
{
  GameObject *player;
  
  player = game_game_add_object (GAME_OBJECT (match)->game, GAME_TYPE_PLAYER,
      NULL);
  game_container_add (GAME_CONTAINER (match), player);
  return GAME_PLAYER (player);
}

static gboolean
game_match_do_remove_player (GameMatch *match, GamePlayer *player)
{
  game_container_remove (GAME_CONTAINER (match), GAME_OBJECT (player));
  g_object_unref (player);

  return TRUE;
}

static void
game_match_class_init (gpointer g_class, gpointer class_data)
{
  GObjectClass *object_class = G_OBJECT_CLASS (g_class);
  GameObjectClass *game_object_class = GAME_OBJECT_CLASS (g_class);
  GameMatchClass *match_class = GAME_MATCH_CLASS (g_class);
  
  parent_class = g_type_class_peek_parent (g_class);

  object_class->set_property = game_match_set_property;
  object_class->get_property = game_match_get_property;
  object_class->finalize = game_match_finalize;

  g_object_class_install_property (object_class, PROP_RUNNING,
      g_param_spec_boolean ("running", _("running"), 
	  _("if this match is currently running"),
	  FALSE, G_PARAM_READABLE));
  g_object_class_install_property (object_class, PROP_PAUSED,
      g_param_spec_boolean ("paused", _("paused"), _("if the game is currently not running"),
	  FALSE, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_CAN_PAUSE,
      g_param_spec_boolean ("can-pause", _("can pause"), 
	  _("if a local playback application is allowed to pause the match"),
	  TRUE, G_PARAM_READABLE));
  g_object_class_install_property (object_class, PROP_CAN_UNPAUSE,
      g_param_spec_boolean ("can-unpause", _("can unpause"), 
	  _("if a local playback application is allowed to unpause the match"),
	  TRUE, G_PARAM_READABLE));
  g_object_class_install_property (object_class, PROP_TICKING,
      g_param_spec_boolean ("ticking", _("ticking"), 
	  _("if the match is currently actively played"), /*FIXME: improve description */
	  FALSE, G_PARAM_READABLE));
  g_object_class_install_property (object_class, PROP_TICK_RATE,
      g_param_spec_uint("tick-rate", _("tick rate"), 
	  _("how many milliseconds between calls to the tick function"),
	  0, G_MAXUINT, 5, G_PARAM_READWRITE));

  game_object_class->to_string = game_match_to_string;
  game_object_class->flush = game_match_flush;

  match_class->pause = game_match_pause;
  match_class->start = game_match_start;
  match_class->stop = game_match_stop;
  match_class->tick = game_match_do_tick;
  match_class->add_player = game_match_do_add_player;
  match_class->remove_player = game_match_do_remove_player;
}

static void
game_match_init (GTypeInstance *instance, gpointer g_class)
{
  GameMatch *match = GAME_MATCH (instance);

  match->tick_rate = 5;
  match->can_pause = TRUE;
  match->can_unpause = TRUE;
}

static void
game_match_container_init (gpointer g_iface, gpointer iface_data)
{
  GameContainerInterface *iface = g_iface;

  iface->offset = G_STRUCT_OFFSET (GameMatch, players);
}

GType
game_match_get_type ()
{
  static GType match_type = 0;
  
  if (!match_type) {
    static const GTypeInfo match_info = {
      sizeof (GameMatchClass),
      NULL,
      NULL,
      game_match_class_init,
      NULL,
      NULL,
      sizeof (GameMatch),
      0,
      game_match_init,
    };
    static const GInterfaceInfo container_info = {
      game_match_container_init,
      NULL,
      NULL,
    };
    
    match_type = g_type_register_static (GAME_TYPE_OBJECT, "GameMatch", &match_info, 0);
    g_type_add_interface_static (match_type, GAME_TYPE_CONTAINER,
        &container_info);
  }
  
  return match_type;
}

GamePlayer *
game_match_add_player (GameMatch *match, const char *name, const GameColor *color)
{
  GameMatchClass *klass;
  
  g_return_val_if_fail (GAME_IS_MATCH (match), NULL);
  g_return_val_if_fail (game_container_get_size (GAME_CONTAINER (match)) < GAME_OBJECT (match)->game->max_players, NULL);
  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (GAME_COLOR_IS_VALID (color), NULL);

  klass = GAME_MATCH_GET_CLASS (match);
  g_return_val_if_fail (klass->add_player, NULL);
  return klass->add_player (match, name, color);
}

gboolean
game_match_remove_player (GameMatch *match, GamePlayer *player)
{
  GameMatchClass *klass;
  
  g_return_val_if_fail (GAME_IS_MATCH (match), FALSE);
  g_return_val_if_fail (GAME_IS_PLAYER (player), FALSE);

  klass = GAME_MATCH_GET_CLASS (match);
  g_return_val_if_fail (klass->remove_player, FALSE);
  return klass->remove_player (match, player);
}