summaryrefslogtreecommitdiff
path: root/libgame/game-game.c
blob: f0f3e75273967a7f3bc76bf3e5bc9f8325438f1f (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
/*
 *  Copyright (C) 2003 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-game.h"
#include "game-marshal.h"
#include "game-match.h"
#include "game-resource.h"
#include "game-types.h"

#include <math.h>
#include <string.h>
#include <gobject/gvaluecollector.h>

/* define this to get useful text dumps during replays */
//#define REPLAY_DEBUG
#ifdef REPLAY_DEBUG
#define REPLAY_DEBUG_PRINT(game, ...) if (game->match) \
    g_printerr (__VA_ARGS__)
#else
#define REPLAY_DEBUG_PRINT(game, ...)
#endif

static void	game_game_class_init		(gpointer		g_class,
                                                 gpointer		class_data);
static void	game_game_init			(GTypeInstance *	instance,
                                                 gpointer		g_class);
static void	game_game_dispose		(GObject *		object);
static void	game_game_finalize		(GObject *		object);
static void	game_game_get_property		(GObject *		object,
						 guint			param_id,
						 GValue *		value,
						 GParamSpec *		pspec);
static void	game_game_set_property		(GObject *		object,
						 guint			param_id,
						 const GValue *		value,
						 GParamSpec *		pspec);

static void	game_game_tick			(GameGame *		game,
						 guint			tick);
  
enum {
  TICK,
  START_GAME,
  END_GAME,
  LOAD_RESOURCE,
  /* FILL ME */
  LAST_SIGNAL
};

enum {
  PROP_0,
  PROP_FRAME_RATE,
  PROP_MATCH,
  PROP_MAX_PLAYERS,
  PROP_NAME,
  PROP_NICK,
  PROP_DESCRIPTION,
  PROP_VERSION,
  PROP_SEED,
  PROP_PLAYER_SCORES,
  PROP_SYSTEM_SCORES,
  PROP_RECORD,
  PROP_RECORD_DATE,
  PROP_ICON,
  PROP_VIEWPORT,
  PROP_TRANSLATION_DOMAIN
};

static GObject *parent_class = NULL;
static guint game_game_signals[LAST_SIGNAL] = { 0 };

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

  iface->offset = G_STRUCT_OFFSET (GameGame, objects);
}

static void
game_game_do_load_resource (GameGame *game, const gchar *resource_name,
    guchar **resource, gsize *length, GError **error)
{
  char *filename;
  GMappedFile *file;

  /* we only load here if the data has not been provided yet by a signal 
   * handler. Such a signal handler for example exists in replay playback */
  if (*resource != NULL || *error != NULL)
    return;

  if (game->resources)
    file = g_hash_table_lookup (game->resources, resource_name);
  else 
    file = NULL;
  if (!file) {
    filename = g_strconcat (game_get_data_dir (), G_DIR_SEPARATOR_S, game_game_get_name (game),
	G_DIR_SEPARATOR_S, "resources", G_DIR_SEPARATOR_S, resource_name, NULL);
    file = g_mapped_file_new (filename, FALSE, error);
    g_free (filename);
    if (!file)
      return;
    if (game->resources)
      g_hash_table_insert (game->resources, g_strdup (resource_name), file);
  }
  *resource = (guchar *) g_mapped_file_get_contents (file);
  *length = g_mapped_file_get_length (file);
}

GType
game_game_get_type ()
{
  static GType game_type = 0;
  
  if (!game_type) {
    static const GTypeInfo game_info = {
      sizeof (GameGameClass),
      NULL,
      NULL,
      game_game_class_init,
      NULL,
      NULL,
      sizeof (GameGame),
      0,
      game_game_init,
    };
    static const GInterfaceInfo container_info = {
      game_game_container_init,
      NULL,
      NULL,
    };
    
    game_type = g_type_register_static (G_TYPE_OBJECT, "GameGame", &game_info, 0);
    g_type_add_interface_static (game_type, GAME_TYPE_CONTAINER,
        &container_info);
  }
  
  return game_type;
}
static void
game_game_class_init (gpointer g_class, gpointer class_data)
{
  GObjectClass *object_class = G_OBJECT_CLASS (g_class);
  GameGameClass *game_class = GAME_GAME_CLASS (g_class);
  
  parent_class = g_type_class_ref (G_TYPE_OBJECT);
  
  object_class->finalize = game_game_finalize;
  object_class->dispose = game_game_dispose;
  object_class->get_property = game_game_get_property;
  object_class->set_property = game_game_set_property;
  
  game_game_signals[TICK] = g_signal_new ("tick",
	  G_TYPE_FROM_CLASS (g_class), G_SIGNAL_RUN_LAST,
	  G_STRUCT_OFFSET (GameGameClass, tick), NULL, NULL,
	  game_marshal_VOID__UINT, G_TYPE_NONE, 1, G_TYPE_UINT);
  game_game_signals[START_GAME] = g_signal_new ("start-game",
	  G_TYPE_FROM_CLASS (g_class), G_SIGNAL_RUN_LAST,
	  G_STRUCT_OFFSET (GameGameClass, start), NULL, NULL,
	  game_marshal_VOID__VOID, G_TYPE_NONE, 0);
  game_game_signals[END_GAME] = g_signal_new ("end-game",
	  G_TYPE_FROM_CLASS (g_class), G_SIGNAL_RUN_LAST,
	  G_STRUCT_OFFSET (GameGameClass, end), NULL, NULL,
	  game_marshal_VOID__VOID, G_TYPE_NONE, 0);
  game_game_signals[LOAD_RESOURCE] = g_signal_new ("load-resource",
	  G_TYPE_FROM_CLASS (g_class), G_SIGNAL_RUN_LAST,
	  G_STRUCT_OFFSET (GameGameClass, load_resource), NULL, NULL,
	  game_marshal_VOID__STRING_POINTER_POINTER_POINTER, 
	  G_TYPE_NONE, 4, G_TYPE_STRING,
	  G_TYPE_POINTER | G_SIGNAL_TYPE_STATIC_SCOPE,
	  G_TYPE_POINTER | G_SIGNAL_TYPE_STATIC_SCOPE,
	  G_TYPE_POINTER | G_SIGNAL_TYPE_STATIC_SCOPE);
  
  g_object_class_install_property (object_class, PROP_FRAME_RATE,
	  g_param_spec_uint ("frame-rate", _("frame rate"), _("miliseconds between ticks"),
			    1, G_MAXUINT, 25, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_MATCH,
      g_param_spec_object ("match", _("match"), _("the match that is currently played"),
	  GAME_TYPE_MATCH, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_MAX_PLAYERS,
	  g_param_spec_uint ("max-players", _("max players"), _("maximum number of players"),
			    1, G_MAXUINT, 1, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_NAME,
	  g_param_spec_string ("name", _("name"), _("name of the game"),
			       NULL, G_PARAM_READABLE));
  g_object_class_install_property (object_class, PROP_NICK,
	  g_param_spec_string ("nick", _("nick"), _("translated name of the game for display"),
			       NULL, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_DESCRIPTION,
	  g_param_spec_string ("description", _("description"), _("short description of what the game does"),
			       NULL, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_VERSION,
	  g_param_spec_string ("version", _("version"), _("version of the game"),
			       NULL, G_PARAM_READABLE));
  g_object_class_install_property (object_class, PROP_SEED,
	  g_param_spec_uint ("seed", _("seed"), _("seed for random number generation"),
			     0, 0xFFFFFFFF, 0, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_PLAYER_SCORES,
      g_param_spec_object ("player-scores", _("player scores"), 
	  _("high score table for current player"), 
	  GAME_TYPE_HIGH_SCORE, G_PARAM_READABLE));
  g_object_class_install_property (object_class, PROP_SYSTEM_SCORES,
      g_param_spec_object ("system-scores", _("system scores"), 
	  _("high score table on current machine"), 
	  GAME_TYPE_HIGH_SCORE, G_PARAM_READABLE));
  g_object_class_install_property (object_class, PROP_RECORD,
      g_param_spec_boolean ("record", _("record"), 
	  _("TRUE to record the game"), 
	  TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
  g_object_class_install_property (object_class, PROP_RECORD_DATE,
      g_param_spec_ulong ("record-date", _("record date"), 
	  _("unix timestamp of last recorded game"), 
	  0, G_MAXULONG, 0, G_PARAM_READABLE));
  g_object_class_install_property (object_class, PROP_VIEWPORT,
      g_param_spec_object ("viewport", _("viewport"), 
	  _("default viewport"), 
	  GAME_TYPE_GRAPHIC, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_ICON,
      g_param_spec_object ("icon", _("icon"), 
	  _("icon to use for representing game"), 
	  GAME_TYPE_GRAPHIC, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_TRANSLATION_DOMAIN,
      g_param_spec_string ("translation-domain", _("Translation Domain"), 
	  _("gettext domain for translating"), 
	  NULL, G_PARAM_READWRITE));

  game_class->tick = game_game_tick;
  game_class->load_resource = game_game_do_load_resource;
}

static gboolean
always_relevant (GameFilter *filter, GameObject *object)
{
  return TRUE;
}

static void
game_game_init (GTypeInstance *instance, gpointer g_class)
{
  GameGame *game = (GameGame *) instance;

  game->frame_rate = 25;
  game->max_players = 1;
    
  game->rand = g_rand_new ();

  game->flush_requests = g_queue_new ();

  game->players = game_filter_new (game, NULL, always_relevant, NULL);
}

static gboolean
game_game_score_player (GameContainer *container, GameObject *object, gpointer gamep)
{
  GameGame *game = GAME_GAME (gamep);
  GamePlayer *player = GAME_PLAYER (object);

  if (!player->score)
    return TRUE;
  if (player->scores == NULL)
    return TRUE;
  game_high_score_add_entry (game->player_scores, player->name, player->scores, game->recorder);
  game_high_score_add_entry (game->system_scores, player->name, player->scores, game->recorder);

  return TRUE;
}

static void
game_game_unset_match (GameGame *game)
{
  GameMatchClass *klass;
  GameMatch *match = game->match;
  
  if (game->match == NULL)
    return;

  game->seed = 0;
  g_hash_table_destroy (game->resources);
  game->resources = NULL;
  g_signal_emit (game, game_game_signals[END_GAME], 0);
  game_container_foreach (GAME_CONTAINER (game->match), game_game_score_player, game);
  game->tick = 0;
  game_game_flush (game);
  game->match = NULL;
  g_object_set (game->players, "container", NULL, NULL);
  klass = GAME_MATCH_GET_CLASS (match);
  if (klass->stop)
    klass->stop (match);
  match->running = FALSE;
  g_object_notify (G_OBJECT (match), "running");
  g_object_unref (match);
  g_object_notify (G_OBJECT (game), "match");
}

static void
game_game_set_match (GameGame *game, GameMatch *match)
{
  GameMatchClass *klass;
  
  game_game_unset_match (game);
  if (match == NULL)
    return;
  
  g_return_if_fail (!match->running);
  g_object_ref (match);
  /* FIXME: put this at the end (it starts ticking after all) */
  klass = GAME_MATCH_GET_CLASS (match);
  if (klass->start)
    klass->start (match);
  
  if (game->seed == 0) {
    guint32 seed = 0;
    do {
      seed = g_random_int ();
    } while (seed == 0);
    game_game_set_seed (game, seed);
  }
  g_rand_set_seed (game->rand, game->seed);
  g_assert (game->resources == NULL);
  game->resources = g_hash_table_new_full (g_str_hash, g_str_equal, 
      g_free, (GDestroyNotify) g_mapped_file_unref);
  game->match = match;
  g_object_set (game->players, "container", match, NULL);
  REPLAY_DEBUG_PRINT (game, "<<< EMIT START-GAME >>>\n");
  REPLAY_DEBUG_PRINT (game, "rand is %u\n", (guint) game->seed);
  REPLAY_DEBUG_PRINT (game, "%u objects in game\n", 
      game_container_get_size (GAME_CONTAINER (game)));
  g_signal_emit (game, game_game_signals[START_GAME], 0);
  REPLAY_DEBUG_PRINT (game, "<<< EMIT START-GAME <<<\n");
  match->running = TRUE;
  g_object_notify (G_OBJECT (match), "running");
  g_object_notify (G_OBJECT (game), "record-date");
  g_object_notify (G_OBJECT (game), "match");
}

static void
game_game_finalize (GObject *object)
{
  GameGame *game = GAME_GAME (object);

  game_game_set_translate_func (game, NULL, NULL, NULL);
  
  g_assert (g_queue_get_length (game->flush_requests) == 0);
  g_queue_free (game->flush_requests);
  g_free (game->name);
  g_free (game->nick);
  g_free (game->description);

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

static void
game_game_dispose (GObject *object)
{
  GameContainerIter iter;
  GameObject *o;
  GameGame *game = GAME_GAME (object);

  game_game_unset_match (game);
  
  if (game->default_resources)
    {
      g_hash_table_destroy (game->default_resources);
      game->default_resources = NULL;
    }
  game_container_iter_init (&iter, GAME_CONTAINER (game));
  while ((o = game_container_iter_next (&iter))) {
    if (o->permanent) {
      /* FIXME: make this an explicit set_object? */
      o->permanent = FALSE;
      g_object_unref (o);
    }
  }
  g_object_unref (game->players);
  game_game_flush (game);
  if (game_container_get_size (GAME_CONTAINER (game)) > 0) {
    g_warning ("objects still existing on game exit:");
    game_game_print (game);
  }
  
  if (game->rand)
    g_rand_free (game->rand);
  game->rand = NULL;

  if (game->system_scores) {
    g_object_unref (game->system_scores);
    game->system_scores = NULL;
  }
  if (game->player_scores) {
    g_object_unref (game->player_scores);
    game->player_scores = NULL;
  }
  if (game->recorder) {
    g_object_unref (game->recorder);
    game->recorder = NULL;
  }

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

static const char *
dgettext_swapped (const gchar *msgid, const gpointer domainname)
{
  return dgettext (domainname, msgid);
}

static void
game_game_get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec)
{
  GameGame *game;
  
  g_return_if_fail (GAME_IS_GAME (object));
  
  game = GAME_GAME (object);
  
  switch (param_id) {
    case PROP_FRAME_RATE:
      g_value_set_uint (value, game_game_get_frame_rate (game));
      break;
    case PROP_MATCH:
      g_value_set_object (value, game->match);
      break;
    case PROP_MAX_PLAYERS:
      g_value_set_uint (value, game_game_get_max_players (game));
      break;
    case PROP_NAME:
      g_value_set_string (value, game_game_get_name (game));
      break;
    case PROP_NICK:
      g_value_set_string (value, game->nick ? game_game_translate (game, game->nick) 
	  : game->name);
      break;
    case PROP_DESCRIPTION:
      g_value_set_string (value, game->description ? 
	  game_game_translate (game, game->description) : 
	  (game->nick ? game_game_translate (game, game->nick) : game->name));
      break;
    case PROP_VERSION:
      g_value_set_string (value, game_game_get_version (game));
      break;
    case PROP_SEED:
      g_value_set_uint (value, game->seed);
      break;
    case PROP_PLAYER_SCORES:
      g_value_set_object (value, game->player_scores);
      break;
    case PROP_SYSTEM_SCORES:
      g_value_set_object (value, game->system_scores);
      break;
    case PROP_RECORD:
      g_value_set_boolean (value, game->recorder != NULL);
      break;
    case PROP_RECORD_DATE:
      if (game->recorder)
	g_object_get_property (G_OBJECT (game->recorder), "date", value);
      else
	g_value_set_ulong (value, 0);
      break;
    case PROP_ICON:
      g_value_set_object (value, game->icon);
      break;
    case PROP_VIEWPORT:
      g_value_set_object (value, game->viewport);
      break;
    case PROP_TRANSLATION_DOMAIN:
      if (game->translate_func == dgettext_swapped)
	g_value_set_string (value, game->translate_data);
      else
	g_value_set_string (value, NULL);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}
static void
game_game_set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec)
{
  GameGame *game;
  
  g_return_if_fail (GAME_IS_GAME (object));
  
  game = GAME_GAME (object);
  
  switch (param_id) {
    case PROP_NICK:
      g_free (game->nick);
      game->nick = g_value_dup_string (value);
      break;
    case PROP_DESCRIPTION:
      g_free (game->description);
      game->description = g_value_dup_string (value);
      break;
    case PROP_FRAME_RATE:
      game_game_set_frame_rate (game, g_value_get_uint (value));
      break;
    case PROP_MATCH:
      game_game_set_match (game, GAME_MATCH (g_value_get_object (value)));
      break;
    case PROP_MAX_PLAYERS:
      game_game_set_max_players (game, g_value_get_uint (value));
      break;
    case PROP_SEED:
      game_game_set_seed (game, g_value_get_uint (value));
      break;
    case PROP_RECORD:
      if (g_value_get_boolean (value) && game->recorder == NULL) {
	game->recorder = game_recorder_new ();
	game_recorder_set_game (game->recorder, game);
      } else if (!g_value_get_boolean (value) && game->recorder != NULL) {
	g_object_unref (game->recorder);
	game->recorder = NULL;
      }
      break;
    case PROP_ICON:
      game->icon = GAME_GRAPHIC (g_value_get_object (value));
      break;
    case PROP_VIEWPORT:
      game->viewport = GAME_GRAPHIC (g_value_get_object (value));
      break;
    case PROP_TRANSLATION_DOMAIN:
      if (g_value_get_string (value))
	game_game_set_translate_func (game, 
	    dgettext_swapped, g_value_dup_string (value),
	    g_free);
      else
	game_game_set_translate_func (game, NULL, NULL, NULL);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}

const char *
game_game_translate (GameGame *game, const char *string)
{
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  g_return_val_if_fail (string != NULL, NULL);
  
  if (game->translate_func == NULL)
    return string;

  return game->translate_func (string, game->translate_data);
}

void
game_game_set_translate_func (GameGame *game, GameTranslateFunc func,
    gpointer data, GDestroyNotify notify)
{
  g_return_if_fail (GAME_IS_GAME (game));

  if (game->translate_free)
    game->translate_free (game->translate_data);

  game->translate_func = func;
  game->translate_data = data;
  game->translate_free = notify;
}

GameGame *
game_game_new (const gchar *name, const gchar *version, 
    const GameInfoKey *keys, const GameInfoScore *scores)
{
  GameGame *game;

  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (version != NULL, NULL);
 
  game = g_object_new (GAME_TYPE_GAME, NULL);

  /* FIXME: make this work with g_object_new */
  game->name = g_strdup (name);
  game->version = g_strdup (version);
  game->keys = keys;
  game->scores = scores;

  game->system_scores = game_high_score_new (scores,
      name, version, TRUE);
  game->player_scores = game_high_score_new (scores,
      name, version, FALSE);
  
  return game;
}

void
game_game_set_frame_rate (GameGame *game, guint milis_per_frame)
{
  g_return_if_fail (GAME_IS_GAME (game));
 
  if (game->frame_rate == milis_per_frame) return;
  
  game->frame_rate = milis_per_frame;
  g_object_notify (G_OBJECT (game), "frame-rate");
}
guint
game_game_get_frame_rate (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), 25);

  return game->frame_rate;
}

static gboolean
game_game_find_alive (GameContainer *container, GameObject *player, gpointer unused)
{
  return GAME_PLAYER (player)->dead;
}

void
game_match_tick (GameMatch *match)
{
  GameGame *game;
  g_return_if_fail (GAME_IS_MATCH (match));
  g_return_if_fail (match->running);
  game = GAME_OBJECT (match)->game;
  g_assert (game->match == match);

  g_return_if_fail (game->ticking == FALSE);
  game->ticking = TRUE;
  REPLAY_DEBUG_PRINT (game, ">>> TICK %u >>>\n", game->tick);
  g_signal_emit (game, game_game_signals[TICK], 0, game->tick);
  REPLAY_DEBUG_PRINT (game, "<<< TICK %u <<<\n", game->tick);
  game->ticking = FALSE;
  if (game_container_foreach (game_game_get_players (game),
      game_game_find_alive, NULL))
    game_game_unset_match (game);
  if (game->match == match) {
    game->tick++;
    /* FIXME: put in idle handler */
    game_game_flush (game);
  }
}

static void
game_game_tick (GameGame *game, guint tick)
{
  GList *walk;
  
  REPLAY_DEBUG_PRINT (game, "ticking objects\n");
  for (walk = game->tick_objects; walk; walk = g_list_next (walk)) {
    GameObject *object = walk->data;
    if (object)
      GAME_OBJECT_GET_CLASS (object)->tick_func (object);
  }
  REPLAY_DEBUG_PRINT (game, "done ticking objects\n");
}

void
game_game_set_max_players (GameGame *game, guint max_players)
{
  g_return_if_fail (GAME_IS_GAME (game));
  g_return_if_fail (max_players != 0);
  g_return_if_fail (game_game_get_player_count (game) < max_players);
  
  game->max_players = max_players;
  g_object_notify (G_OBJECT (game), "max_players");
}
guint
game_game_get_max_players (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), 1);

  return game->max_players;
}

guint
game_game_get_player_count (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), 0);

  return game_container_get_size (game->players);
}

GameContainer *
game_game_get_players (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);

  return game->players;
}

void
game_game_set_name_func (GameGame *game, const gchar *name)
{
  g_object_notify (G_OBJECT (game), "name");
}
void
game_game_set_version (GameGame *game, const gchar *version)
{
  g_object_notify (G_OBJECT (game), "version");
}
void
game_game_set_name (GameGame *game, const gchar *name, const gchar *version)
{
  g_return_if_fail (GAME_IS_GAME (game));
  g_return_if_fail (name == NULL);
  g_return_if_fail (version == NULL);
  
  g_object_freeze_notify (G_OBJECT (game));
  game_game_set_name_func (game, name);
  game_game_set_version (game, version);
  g_object_thaw_notify (G_OBJECT (game));
}
const gchar *
game_game_get_name (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);

  return game->name;
}
const gchar *
game_game_get_version (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);

  return game->version;
}
guint
game_game_get_tick (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), 0);

  return game->tick;
}
void
game_game_set_seed (GameGame *game, guint32 seed)
{
  g_return_if_fail (GAME_IS_GAME (game));
  g_return_if_fail (game->match == NULL);

  game->seed = seed;
  g_object_notify (G_OBJECT (game), "seed");
}

gboolean
game_random_boolean (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), g_random_boolean ());

  return g_rand_boolean (game->rand);
}
gint32
game_random_int (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), g_random_int ());

  return g_rand_int (game->rand);
}
gint32
game_random_int_range (GameGame *game, gint32 begin, gint32 end)
{
  gint32 ret;
  
  g_return_val_if_fail (GAME_IS_GAME (game), g_random_int_range (begin, end));

  ret = g_rand_int_range (game->rand, begin, end);
  REPLAY_DEBUG_PRINT (game, "random int in [%d, %d] = %d\n", 
      (int) begin, (int) end, (int) ret);

  return ret;
}
double
game_random_double (GameGame *game)
{
  g_return_val_if_fail (GAME_IS_GAME (game), g_random_double ());

  return g_rand_double (game->rand);
}
double
game_random_double_range  (GameGame *game, double begin, double end)
{
  double ret;
  
  g_return_val_if_fail (GAME_IS_GAME (game), g_random_double_range (begin, end));

  ret = g_rand_double_range (game->rand, begin, end);
  REPLAY_DEBUG_PRINT (game, "random int in [%g, %g] = %g\n", 
      begin, end, ret);

  return ret;
}

GameObject *
game_game_add_object (GameGame *game, GType object_type, ...)
{
  va_list args;
  GameObject *object;
  
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  g_return_val_if_fail (g_type_is_a (object_type, GAME_TYPE_OBJECT), NULL);

  va_start (args, object_type);
  object = game_game_add_object_valist (game, object_type, args);
  va_end (args);
  
  return object;
}

GameObject *
game_game_add_object_valist (GameGame *game, GType object_type,
    va_list varargs)
{
  GObjectClass *class;
  GParameter *params;
  const gchar *name;
  GameObject *object;
  guint n_params = 0, n_alloced_params = 16;
  
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  g_return_val_if_fail (g_type_is_a (object_type, GAME_TYPE_OBJECT), NULL);

  class = g_type_class_ref (object_type);
  
  name = va_arg (varargs, gchar*);
  if (name) {
    params = g_new (GParameter, n_alloced_params);
    while (name) {
      gchar *error = NULL;
      GParamSpec *pspec = g_object_class_find_property (class, name);
      if (!pspec) {
	g_warning ("%s: object class `%s' has no property named `%s'", G_STRFUNC,
	    g_type_name (object_type), name);
	break;
      }
      if (n_params >= n_alloced_params) {
	n_alloced_params += 16;
	params = g_renew (GParameter, params, n_alloced_params);
      }
      params[n_params].name = name;
      params[n_params].value.g_type = 0;
      g_value_init (&params[n_params].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
      G_VALUE_COLLECT (&params[n_params].value, varargs, 0, &error);
      if (error) {
	g_warning ("%s: %s", G_STRFUNC, error);
	g_free (error);
	g_value_unset (&params[n_params].value);
	break;
      }
      n_params++;
      name = va_arg (varargs, gchar*);
    }
  } else {
    params = NULL;
    n_params = 0;
  }
  
  object = game_game_add_objectv (game, object_type, n_params, params);

  while (n_params--)
    g_value_unset (&params[n_params].value);
  g_free (params);

  g_type_class_unref (class);

  return object;
}

GameObject *
game_game_add_objectv (GameGame *game, GType object_type, guint n_parameters, 
    GParameter *parameters)
{
  GameObject *object;
  GParameter all_parameters[n_parameters + 1];

  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  g_return_val_if_fail (g_type_is_a (object_type, GAME_TYPE_OBJECT), NULL);
  g_return_val_if_fail (parameters != NULL || n_parameters == 0, NULL);

  if (n_parameters)
    memcpy (all_parameters + 1, parameters, sizeof (GParameter) * n_parameters);
  all_parameters[0].name = "game";
  all_parameters[0].value.g_type = 0;
  g_value_init (&all_parameters[0].value, GAME_TYPE_GAME);
  g_value_set_object (&all_parameters[0].value, game);
  
  object = GAME_OBJECT (g_object_newv (object_type, n_parameters + 1, all_parameters));
  g_value_unset (&all_parameters[0].value);
  game_container_add (GAME_CONTAINER (game), object);
#ifdef REPLAY_DEBUG
  {
    char *s = game_object_to_string (object);
    REPLAY_DEBUG_PRINT (game, "added object %s\n", s);
    g_free (s);
  }
#endif

  return object;
}

static gboolean
game_game_flush_one (GameGame *game)
{
  GameObject *object;
  GameObjectClass *klass;
  
  if (g_queue_get_length (game->flush_requests) == 0)
    return FALSE;
  
  object = g_queue_pop_head (game->flush_requests);
  klass = GAME_OBJECT_GET_CLASS (object);
  klass->flush (object);
  object->needs_flush = FALSE;
  return TRUE;
}

void
game_game_remove_object (GameGame *game, GameObject *object)
{
  
  g_return_if_fail (GAME_IS_GAME (game));
  g_return_if_fail (GAME_IS_OBJECT (object));

  if (object->needs_flush) {
    g_queue_remove (game->flush_requests, object);
    g_queue_push_head (game->flush_requests, object);
    if (!game_game_flush_one (game)) {
      g_assert_not_reached ();
    }
    g_assert (!object->needs_flush);
  }
  game_container_remove (GAME_CONTAINER (game), object);
}

void
game_game_flush (GameGame *game)
{
  g_return_if_fail (GAME_IS_GAME (game));

  game_container_flush (GAME_CONTAINER (game));
  game->tick_objects = g_list_remove_all (game->tick_objects, NULL);
  while (game_game_flush_one (game));
}

gboolean
game_game_save (GameGame *game, const char *filename)
{
  g_return_val_if_fail (GAME_IS_GAME (game), FALSE);
  g_return_val_if_fail (filename != NULL, FALSE);

  if (game->recorder == NULL)
    return FALSE;

  return game_recorder_save (game->recorder, filename);
}

void
game_game_print (GameGame *game)
{
  GameContainerIter iter;
  GameObject *o;
  gchar *str;

  g_return_if_fail (GAME_IS_GAME (game));

  g_printerr ("Game @ %p\n", game);
  g_printerr ("active objects:\n");
  game_container_iter_init (&iter, GAME_CONTAINER (game));
  while ((o = game_container_iter_next (&iter))) {
    str = game_object_to_string (o);
    g_printerr ("  %c%c %u %s\n", o->permanent ? 'P' : ' ', 
	GAME_OBJECT_GET_CLASS (o)->tick_func ? 'T' : ' ', 
	G_OBJECT (o)->ref_count, str);
    g_free (str);
  }
  g_printerr ("\n");
}

/**
 * game_game_load_old_resource:
 * @game: a #GameGame
 * @resource_name: identifying name of the resource
 * @length: an optional pointer to take the length of the returned data
 * @error: pointer to location to store the error in or NULL
 *
 * Loads data from an external resource. Use this function if you want to load
 * game-relevant data from somewhere. Loading the same resource twice will 
 * result in the same data. The default implementation looks for it
 * in the file $datadir/libgame/$gamename/resources/$resource_name.
 * <note>Data loaded with this function will be saved in replays. This ensures
 * that replays will keep working even when this data is not available or has 
 * changed when the replay is watched. Of course it also means that loading huge
 * amounts of data this way will create huge replay files.</note>
 *
 * Returns: The loaded resource, or NULL if the resource could not be found. You
 *	    must not modify or free the returned data. The data may only be used
 *	    until the game ends, after that it will be freed automatically.
 **/
const guchar *
game_game_load_old_resource (GameGame *game, const char *resource_name, gsize *length,
    GError **error)
{
  guchar *ret = NULL;
  gsize len = 0;
  GError *err = NULL;
  
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  g_return_val_if_fail (resource_name != NULL, NULL);

  g_signal_emit (game, game_game_signals[LOAD_RESOURCE], 0, resource_name, 
      &ret, &len, &err);
  if (ret && length)
    *length = len;
  if (err)
    g_propagate_error (error, err);
  return ret;
}

GameGame *
game_game_load (const char *name, GError **error)
{
  char *module;
  GameGame *game;
  
  g_return_val_if_fail (name != NULL, NULL);

  module = g_module_build_path (game_get_plugin_dir (), name);
  if (!g_file_test (module, G_FILE_TEST_EXISTS)) {
    g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_EXIST, 
	_("The plugin for the game '%s' wasn't found at the expected location \"%s\""),
	name, module);
    g_free (module);
    return NULL;
  }
  game = game_game_load_from_file (module, error);
  g_free (module);
  return game;
}

GameGame *
game_game_load_from_file (const char *name, GError **error)
{
  GModule *module;
  gpointer symbol;
  GameGame * (* new_game) (GError **);
  GameGame *game;
  GError *err = NULL;
  
  g_return_val_if_fail (name != NULL, NULL);

  module = g_module_open (name, G_MODULE_BIND_LOCAL);
  if (!module)
    goto err;
  if (!g_module_symbol (module, "new_game", &symbol))
    goto err;
  new_game = symbol;
  game = new_game (&err);
  if (game) {
    g_module_make_resident (module);
  } else {
    if (err == NULL) {
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, 
	  _("Plugin failed to create a game."));
    } else {
      g_propagate_error (error, err);
    }
  }
  return game;

err:
  if (error)
    *error = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_INVAL, g_module_error ());
  if (module)
    g_module_close (module);
  return NULL;
}

static void
game_game_add_resource (GameGame *    game,
                        const char *  name,
                        GameResource *resource)
{
  g_hash_table_insert (game->default_resources, g_strdup (name), g_object_ref (resource));
}

static void
game_game_load_default_resources (GameGame *game)
{
  GameDataResource *data_resource;
  char *s;
  const guchar *data;
  GVariant *variant, *v;
  GVariantIter iter;
  GError *error = NULL;
  GameResource *resource;

  if (G_LIKELY (game->default_resources))
    return;

  game->default_resources = g_hash_table_new_full (g_str_hash, g_str_equal,
      g_free, g_object_unref);

  s = g_strconcat (game->name, ".resources", NULL);
  data_resource = game_game_load_resource (game, s);
  g_free (s);
  
  data = game_data_resource_get_data (data_resource);
  if (data == NULL)
    return;

  variant = g_variant_parse (G_VARIANT_TYPE ("a{sv}"),
                             (char *) data,
                             (char *) data + game_data_resource_get_size (data_resource),
                             NULL,
                             &error);
  if (variant == NULL)
    {
      g_warning ("Could not load game resources: %s", error->message);
      g_error_free (error);
      return;
    }

  g_variant_iter_init (&iter, variant);

  while (g_variant_iter_loop (&iter, "{sv}", &s, &v))
    {
      resource = game_resource_load (game, v);
      game_game_add_resource (game, s, resource);
      g_object_unref (resource);
    }

  g_variant_unref (variant);
  g_object_unref (data_resource);
}

GameResource *
game_game_get_resource (GameGame *game, const char *name)
{
  GameResource *resource;

  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  game_game_load_default_resources (game);

  resource = g_hash_table_lookup (game->default_resources, name);
  if (resource == NULL)
    {
      g_critical ("No resources named `%s'", name);
      return NULL;
    }

  return resource;
}

GameObject *
game_game_spawn_object (GameGame *  game,
                        const char *name)
{
  GameResource *resource;

  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  resource = game_game_get_resource (game, name);
  return game_resource_spawn (resource);
}

GameDataResource *
game_game_load_resource (GameGame *  game,
                         const char *filename)
{
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  g_return_val_if_fail (filename != NULL, NULL);

  return GAME_DATA_RESOURCE (game_game_add_object (game,
                                                   GAME_TYPE_FILE_RESOURCE,
                                                   "filename", filename,
                                                   NULL));
}