summaryrefslogtreecommitdiff
path: root/lib/textplugin.c
blob: 32a5a564bf5fcd27e435d730d1f6f5391c6041e0 (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
/* $Id: textplugin.c,v 1.1 2004/10/21 19:24:30 zeenix Exp $ */

/* GNU Robots game engine. */

/* Copyright (C) 1998 Jim Hall, jhall1@isd.net */

/*
   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 of the License, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glib.h>
#include <stdio.h>
#include "configs.h"
#include "textplugin.h"

enum
{
  ARG_0,
  ARG_MAP 
};

GType _text_plugin_type = 0;
static GType _parent_type = 0;

static void text_plugin_class_init (TextPluginClass * klass);
static void text_plugin_init (GObject * object);
static void text_plugin_interface_init (gpointer g_iface, gpointer iface_data);

static GObject * text_plugin_constructor (GType type, 
    guint n_construct_properties, 
    GObjectConstructParam *construct_properties);

static void text_plugin_finalize (GObject * object);
static void text_plugin_dispose (GObject * object);

static void text_plugin_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);

static void text_plugin_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

inline void text_plugin_update_status (TextPlugin *text,
				   const gchar *s, 
				   glong energy, 
				   glong score, 
				   glong shields);

static GObjectClass *parent_class = NULL;

/* For translation of directions to strings */
static gchar *str_dir[] = { "North", "East", "South", "West" };

GType
text_plugin_get_type (void)
{
  if (!_text_plugin_type) {
    static const GTypeInfo object_info = {
      sizeof (TextPluginClass),
      NULL,
      NULL,
      (GClassInitFunc) text_plugin_class_init,
      NULL,
      NULL,
      sizeof (TextPlugin),
      0,
      (GInstanceInitFunc) text_plugin_init,
      NULL
    };

    static const GInterfaceInfo interface_info = {
      (GInterfaceInitFunc) text_plugin_interface_init,
      NULL,
      NULL
    };

    _text_plugin_type =
        g_type_register_static (G_TYPE_OBJECT, 
			"TextPlugin", 
			&object_info,
			0);
    
    g_type_add_interface_static (_text_plugin_type,
                                 _parent_type,
                                 &interface_info);
  }

  return _text_plugin_type;
}

static void
text_plugin_class_init (TextPluginClass * klass)
{
  GObjectClass *gobject_class;

  gobject_class = (GObjectClass *) klass;

  parent_class = g_type_class_ref (G_TYPE_OBJECT);

  gobject_class->constructor = text_plugin_constructor;
  gobject_class->set_property = text_plugin_set_property;
  gobject_class->get_property = text_plugin_get_property;
  gobject_class->dispose = text_plugin_dispose;
  gobject_class->finalize = text_plugin_finalize;

  g_object_class_override_property (gobject_class, ARG_MAP, "map");
}

static void
text_plugin_init (GObject * object)
{
  TextPlugin *text = TEXT_PLUGIN (object);

  text->map = NULL;
  text->map_size = NULL;
}

static GObject *
text_plugin_constructor (GType type, 
    guint n_construct_properties, 
    GObjectConstructParam *construct_properties)
{
  GObject *object;

  /* Chain up to the parent first */
  object = parent_class->constructor (type, n_construct_properties, construct_properties);
  
  g_printf ("GNU Robots starting..\n");
  
  return object;
}

static void 
text_plugin_dispose (GObject * object)
{
  TextPlugin *text = TEXT_PLUGIN (object);
 
  if (text->map != NULL) {
    g_object_unref (G_OBJECT (text->map));

    if (text->map_size != NULL) {
      g_free (text->map_size);
    }
  }

  parent_class->dispose (object);
}

/* finalize is called when the object has to free its resources */
static void
text_plugin_finalize (GObject * object)
{
  g_printf ("GNU Robots done.\n");
  
  parent_class->finalize (object);
}

static void
text_plugin_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  TextPlugin *text;
  GObject *obj;

  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (G_IS_TEXT_PLUGIN (object));

  text = TEXT_PLUGIN (object);

  switch (prop_id) {
    case ARG_MAP:
      obj = g_value_get_object (value);
      g_return_if_fail (obj != NULL);
      
      if (text->map != NULL) {
	g_object_unref (text->map);
      }

      text->map = MAP (g_object_ref (obj));

      if (text->map_size != NULL) {
        g_free (text->map_size);
      }

      g_object_get (G_OBJECT (text->map),
		"size", &text->map_size,
		NULL);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
text_plugin_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  TextPlugin *text;

  /* it's not null if we got it, but it might not be ours */
  g_return_if_fail (G_IS_TEXT_PLUGIN (object));

  text = TEXT_PLUGIN (object);

  switch (prop_id) {
    case ARG_MAP:
      g_value_set_object (value, g_object_ref (G_OBJECT (text->map)));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

UserInterface *
user_interface_new (Map *map, GType parent_type)
{
  g_return_val_if_fail (map != NULL, NULL);
  g_return_val_if_fail (_parent_type != 0 || parent_type != 0, NULL);

  if (!_parent_type) {
     _parent_type = parent_type;
  }

  TextPlugin *text =
  	TEXT_PLUGIN (g_object_new (text_plugin_get_type (),
				  "map", map,
				  NULL));
  if (text->errors) {
    g_object_unref (G_OBJECT (text));
    return NULL;
  }

  return USER_INTERFACE (text);
}

inline void text_plugin_add_thing (TextPlugin *text,
				      gint x, 
				      gint y, 
				      gint thing)
{
  g_printf ("thing `%c' added at %d,%d\n", thing, x, y);
}

inline void text_plugin_draw (TextPlugin *text)
{
  gint i, j;

  for (j = 0; j < text->map_size->num_rows; j++) {
    for (i = 0; i < text->map_size->num_cols; i++) {
      g_printf ("thing `%c' added at %d,%d\n", MAP_GET_OBJECT (text->map, i, j), i, j);
    }
  }
}

inline void text_plugin_move_robot (TextPlugin *text,
				       gint from_x, 
				       gint from_y, 
				       gint x, 
				       gint y, 
				       gint cdir,
				       glong energy,
				       glong score, 
				       glong shields)
{
  g_printf ("robot now at %d,%d facing %s\n", x, y, str_dir[cdir]);
}

/* function to animate the robot */
inline void text_plugin_robot_smell (TextPlugin *text,
					gint x, 
					gint y, 
					gint cdir,
				        glong energy,
				        glong score, 
				        glong shields)
{
  g_printf ("the robot sniffs..\n");
}

inline void text_plugin_robot_zap (TextPlugin *text,
				      gint x, 
				      gint y, 
				      gint cdir, 
				      gint x_to, 
				      gint y_to,
				      glong energy,
				      glong score, 
				      glong shields)
{
  g_printf ("robot fires gun at space %d,%d\n", x_to, y_to);
}

inline void text_plugin_robot_feel (TextPlugin *text,
				       gint x, 
				       gint y, 
				       gint cdir, 
				       gint x_to, 
				       gint y_to,
				       glong energy,
				       glong score, 
				       glong shields)
{
  g_printf ("robot feels space %d,%d\n", x_to, y_to);
}

inline void text_plugin_robot_grab (TextPlugin *text,
				       gint x, 
				       gint y, 
				       gint cdir, 
				       gint x_to, 
				       gint y_to,
				       glong energy,
				       glong score, 
				       glong shields)
{
  g_printf ("robot grabs for space %d,%d\n", x_to, y_to);
}

inline void text_plugin_robot_look (TextPlugin *text,
				       gint x, 
				       gint y, 
				       gint cdir, 
				       gint x_to, 
				       gint y_to,
				       glong energy,
				       glong score, 
				       glong shields)
{
  g_printf ("robot looks towards %s from %d,%d\n", str_dir[cdir], x, y);
}

/* function to get data from user */
inline void text_plugin_get_string (TextPlugin *text,
				       gchar *prompt, 
				       gchar *buff, 
				       gint len)
{
  fputs (prompt, stdout);
  fgets (buff, len, stdin);
}

inline void text_plugin_update_status (TextPlugin *text,
				   const gchar *s, 
				   glong energy, 
				   glong score, 
				   glong shields)
{
  puts (s);
}

static void text_plugin_interface_init (gpointer g_iface, gpointer iface_data)
{
  UserInterfaceClass *klass = (UserInterfaceClass *)g_iface;

  klass->user_interface_add_thing = (void (*) (UserInterface *ui,
				      	       gint x, 
				               gint y, 
				               gint thing)) 
	  			    text_plugin_add_thing;

  klass->user_interface_draw = (void (*) (UserInterface *ui)) text_plugin_draw;
  klass->user_interface_update_status = (void (*) (UserInterface *ui,
				  	 	   const gchar *s, 
				  	 	   glong energy,
				  	 	   glong score, 
				  	 	   glong shields)) 
	  				text_plugin_update_status;
  klass->user_interface_move_robot = (void (*) (UserInterface *ui, 
					 	gint from_x, 
					 	gint from_y, 
					 	gint to_x, 
					 	gint to_y, 
					 	gint cdir,
		 			 	glong energy, 
					 	glong score, 
					 	glong shields))
	  			     text_plugin_move_robot;
  klass->user_interface_robot_smell = (void (*) (UserInterface *ui, 
					 	 gint x, 
					 	 gint y, 
					 	 gint cdir,
		 			 	 glong energy, 
					 	 glong score, 
					 	 glong shields))
	  			      text_plugin_robot_smell;
  klass->user_interface_robot_zap = (void (*) (UserInterface *ui,
			  		       gint x, 
					       gint y, 
					       gint cdir, 
			       		       gint x_to, 
					       gint y_to,
		 			       glong energy, 
					       glong score, 
					       glong shields))
	  			    text_plugin_robot_zap;
  klass->user_interface_robot_feel = (void (*) (UserInterface *ui, 
					 gint x, 
					 gint y, 
					 gint cdir, 
					 gint x_to, 
					 gint y_to,
		 			 glong energy, 
					 glong score, 
					 glong shields))
	  			     text_plugin_robot_grab;
  klass->user_interface_robot_grab = (void (*) (UserInterface *ui,
				         	gint x, 
				         	gint y, 
				         	gint cdir, 
				         	gint x_to, 
				         	gint y_to,
				         	glong energy,
				         	glong score, 
				         	glong shields))
	  	                     text_plugin_robot_grab;
  klass->user_interface_robot_look = (void (*) (UserInterface *ui,
					 	gint x, 
					 	gint y, 
					 	gint cdir, 
					 	gint x_to, 
					 	gint y_to,
		 			 	glong energy, 
					 	glong score, 
					 	glong shields))
	  			     text_plugin_robot_look;
  klass->user_interface_get_string = (void (*) (UserInterface *ui,
					 gchar *prompt, 
					 gchar *buff, 
					 gint len))
	  			     text_plugin_get_string;
}