summaryrefslogtreecommitdiff
path: root/libgame/game-object.c
blob: 25a5134f24cb94c014a099c000c7d9ef4a390061 (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
/*
 *  Copyright (C) 2005 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-object.h"

enum {
  PROP_0,
  PROP_GAME,
  PROP_NAME,
  PROP_PERMANENT,
};

static GObjectClass *parent_class = NULL;

static int
game_object_compare_tick_priority (gconstpointer p1, gconstpointer p2)
{
  if (!p1) return 0;
  if (!p2) return 0;
  /* FIXME: overflow protection? */
  return GAME_OBJECT_GET_CLASS (p1)->tick_priority - GAME_OBJECT_GET_CLASS (p2)->tick_priority;
}

static void
game_object_add_tick_func (GameObject *object)
{
  g_assert (GAME_OBJECT_GET_CLASS (object)->tick_func);
  g_assert (object->game);
  /* FIXME: not safe when called while ticking */
  object->game->tick_objects = g_list_insert_sorted (
      object->game->tick_objects, object, 
      game_object_compare_tick_priority);
}

static void
game_object_remove_tick_func (GameObject *object)
{
  GList *list;

  g_assert (object->game);
  list = g_list_find (object->game->tick_objects, object);
  g_assert (list);
  list->data = NULL;
}

static void
game_object_get_property (GObject *object_, guint param_id, GValue *value, 
    GParamSpec * pspec)
{
  GameObject *object = GAME_OBJECT (object_);
  
  switch (param_id) {
    case PROP_GAME:
      g_value_set_object (value, object->game);
      break;
    case PROP_NAME:
      g_value_set_string (value, object->name);
      break;
    case PROP_PERMANENT:
      g_value_set_boolean (value, object->permanent);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}

static void
game_object_set_property (GObject *obj, guint param_id, const GValue *value,
    GParamSpec *pspec)
{
  GameObjectClass *klass;
  GameObject *object = GAME_OBJECT (obj);

  switch (param_id) {
    case PROP_GAME:
      g_assert (object->game == NULL);
      object->game = GAME_GAME (g_value_get_object (value));
      g_assert (object->game != NULL);
      klass = GAME_OBJECT_GET_CLASS (object);
      if (klass->tick_func)
	game_object_add_tick_func (object);
      if (klass->game_set)
	klass->game_set (object);
      break;
    case PROP_NAME:
      /* may not be set before game is set */
      g_assert (object->game != NULL);
      if (object->name)
	g_hash_table_remove (object->game->named_objects, object->name);
      g_assert (object->name == NULL);
      object->name = g_strdup (g_value_get_string (value));
      if (object->name) {
	g_hash_table_insert (object->game->named_objects, object->name, object);
	g_object_ref (object);
      }
      break;
    case PROP_PERMANENT:
      if (object->permanent != g_value_get_boolean (value)) {
	object->permanent = g_value_get_boolean (value);
	if (object->permanent)
	  g_object_ref (object);
	else
	  g_object_unref (object);
      }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
  }
}

void game_game_remove_object (GameGame *game, GameObject *object);
static void
game_object_dispose (GObject *obj)
{
  GameObject *object = GAME_OBJECT (obj);

  g_assert (object->name == NULL);
  if (GAME_OBJECT_GET_CLASS (object)->tick_func)
    game_object_remove_tick_func (object);
  game_game_remove_object (object->game, object);

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

static gchar *
game_object_do_to_string (GameObject *object)
{
  return g_strdup_printf ("%s %p", G_OBJECT_TYPE_NAME(object), object);
}

static gboolean
game_object_do_copy (GameObject *dest, GameObject *src)
{
  return TRUE;
}

static gboolean
game_object_do_load (GameObject *dest, GVariantIter *iter)
{
  return TRUE;
}

static void
game_object_do_save (GameObject *dest, GVariantBuilder *builder)
{
}

static void
game_object_base_init (gpointer g_class)
{
  GameObjectClass *gameobject_class = GAME_OBJECT_CLASS (g_class);

  /* reset here so we're sure every class has its own */
  gameobject_class->copy = NULL;
}

static void
game_object_class_init (gpointer g_class, gpointer class_data)
{
  GObjectClass *object_class = G_OBJECT_CLASS (g_class);
  GameObjectClass *gameobject_class = GAME_OBJECT_CLASS (g_class);
  
  parent_class = g_type_class_peek_parent (g_class);

  object_class->set_property = game_object_set_property;
  object_class->get_property = game_object_get_property;
  object_class->dispose = game_object_dispose;
  
  g_object_class_install_property (object_class, PROP_GAME,
      g_param_spec_object ("game", _("game"), _("game this object belongs to"),
	  GAME_TYPE_GAME, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property (object_class, PROP_NAME,
      g_param_spec_string ("name", _("name"), _("unique name for easy identification"),
	  NULL, G_PARAM_READWRITE));
  g_object_class_install_property (object_class, PROP_PERMANENT,
      g_param_spec_boolean ("permanent", _("permanent"),
	  _("whether this object stays alive on level change"),
	  FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  gameobject_class->copy = game_object_do_copy; 
  gameobject_class->load = game_object_do_load; 
  gameobject_class->save = game_object_do_save; 
  gameobject_class->to_string = game_object_do_to_string; 
}

static void
game_object_init (GTypeInstance *instance, gpointer g_class)
{
}

GType
game_object_get_type ()
{
  static GType object_type = 0;
  
  if (!object_type) {
    static const GTypeInfo object_info = {
      sizeof (GameObjectClass),
      game_object_base_init,
      NULL,
      game_object_class_init,
      NULL,
      NULL,
      sizeof (GameObject),
      0,
      game_object_init,
    };
    
    object_type = g_type_register_static (G_TYPE_OBJECT, "GameObject", &object_info, 0);
  }
  
  return object_type;
}

GameObject *
game_object_copy (GameObject *object)
{
  GameObject *copy;
  GameObjectClass *klass;
  
  g_return_val_if_fail (GAME_IS_OBJECT (object), NULL);

  klass = GAME_OBJECT_GET_CLASS (object);
  if (klass->copy == NULL)
    return NULL;

  copy = game_game_add_object (object->game, G_OBJECT_TYPE (object), NULL);
  if (!klass->copy (copy, object)) {
    g_object_unref (copy);
    return NULL;
  }
  return copy;
}

GameObject *
game_object_load (GameGame  *game,
                  GVariant  *variant,
                  GError   **error)
{
  GameObject *object;
  GameObjectClass *klass;
  GVariantIter iter;
  GVariant *value;
  GType type;
  
  g_return_val_if_fail (GAME_IS_GAME (game), NULL);
  g_return_val_if_fail (variant != NULL, NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (g_variant_classify (variant) != G_VARIANT_CLASS_TUPLE)
    {
      g_set_error_literal (error, G_VARIANT_PARSE_ERROR, G_VARIANT_PARSE_ERROR_TYPE_ERROR,
          _("Expected a struct for game_object_load()"));
      return NULL;
    }

  g_variant_iter_init (&iter, variant);

  value = g_variant_iter_next_value (&iter);
  if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
    {
      const char *type_name = g_variant_get_string (value, NULL);
      
      if (type_name)
        {
          type = g_type_from_name (g_variant_get_string (value, NULL));
          if (type == 0)
              g_set_error (error, G_VARIANT_PARSE_ERROR, G_VARIANT_PARSE_ERROR_FAILED,
                  _("`%s' is not a valid type name"), type_name);
          else if (!g_type_is_a (type, GAME_TYPE_OBJECT))
            {
              g_set_error (error, G_VARIANT_PARSE_ERROR, G_VARIANT_PARSE_ERROR_FAILED,
                  _("`%s' is not a valid object type"), type_name);
              type = 0;
            }
        }
      else
        {
          g_set_error_literal (error, G_VARIANT_PARSE_ERROR, G_VARIANT_PARSE_ERROR_VALUE_EXPECTED,
              _("No type name given"));
        }
    }
  else
    {
      g_set_error_literal (error, G_VARIANT_PARSE_ERROR, G_VARIANT_PARSE_ERROR_INVALID_TYPE_STRING,
          _("No object type name string given"));
      type = 0;
    }

  g_variant_unref (value);
  if (type == 0)
    return NULL;

  object = game_game_add_object (game, type, NULL);

  klass = GAME_OBJECT_GET_CLASS (object);
  if (! klass->load (object, &iter))
    {
      g_object_unref (object);
      return NULL;
    }

  return object;
}

GVariant *
game_object_save (GameObject *object)
{
  GameObjectClass *klass;
  GVariantBuilder builder;
  GVariant *variant;

  g_return_val_if_fail (GAME_IS_OBJECT (object), NULL);

  klass = GAME_OBJECT_GET_CLASS (object);

  g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
  g_variant_builder_add (&builder, "s", G_OBJECT_TYPE_NAME (object));

  klass->save (object, &builder);

  variant = g_variant_builder_end (&builder);

  return variant;
}

gchar *
game_object_to_string (gpointer object)
{
  GameObjectClass *klass;

  g_return_val_if_fail (GAME_IS_OBJECT (object), NULL);
  klass = GAME_OBJECT_GET_CLASS (object);
  g_return_val_if_fail (klass->to_string, NULL);
  return klass->to_string (object);
}

/**
 * game_object_queue_flush:
 * @object: a #GameObject
 *
 * Requests that this object is flushed when possible.
 * "Flushing" means that the object calls internal cleanup functions, that can
 * for various reasons (the most common being speed and reentrancy problems)
 * not be called during normal processing. An object should not need to access
 * other objects during a flush. It's ok to queue multiple flushes, only one
 * flush will be executed.
 **/
void
game_object_queue_flush (GameObject *object)
{
  g_return_if_fail (GAME_IS_OBJECT (object));
  g_return_if_fail (GAME_OBJECT_GET_CLASS (object)->flush != NULL);

  if (object->needs_flush)
    return;

  object->needs_flush = TRUE;
  g_queue_push_tail (object->game->flush_requests, object);
}