more typographical niceties
[rfk] / rfk.c
1 /*  robotfindskitten for maemo
2  *  original by Leonard Richardson, 1997
3  *  ported to maemo by Thomas Thurman, 2009
4  *  suggestions welcome
5  *  Compile with:
6  *  gcc -Wall -g rfk.c -o rfk `pkg-config --cflags --libs gtk+-2.0 hildon-1`
7  */
8
9 #include <gtk/gtk.h>
10 #include <stdlib.h>
11 #include <glib.h>
12 #include <hildon/hildon.h>
13 #include <math.h>
14 #include <stdio.h>
15 #include <string.h>
16
17 #define ARENA_WIDTH 25
18 #define ARENA_HEIGHT 12
19
20 const int amount_of_random_stuff = 15;
21
22 const char *explanation =
23   "In this game, you are robot (#). "
24   "Your job is to find kitten. This task is complicated "
25   "by the existence of various things which are not kitten. "
26   "Robot must touch items to determine if they are kitten or "
27   "not. The game ends when robotfindskitten. You may move "
28   "robot about by tapping on any side of robot, or with the "
29   "cursor keys.";
30
31 GSList *nki = NULL;
32 guint nki_count = 0;
33
34 GtkWidget *arena[ARENA_WIDTH][ARENA_HEIGHT];
35 GtkWidget *table, *window, *robot, *kitten;
36 int robot_x, robot_y;
37 gboolean *used = NULL;
38
39 GdkPixbuf *robot_pic, *love_pic, *kitten_pic;
40 GtkWidget *animation_area;
41
42 const GdkColor black = { 0, };
43
44 /****************************************************************/
45 /* Random object descriptions.                                  */
46 /****************************************************************/
47
48 char *
49 description (void)
50 {
51   int r;
52    
53   do
54     {
55       r = random() % nki_count;
56     }
57   while (used[r]);
58
59   used[r] = TRUE;
60   return g_slist_nth_data (nki, r);
61 }
62
63 /****************************************************************/
64 /* Placing objects.                                             */
65 /****************************************************************/
66
67 void
68 place_in_arena_at_xy (GtkWidget *item, int x, int y)
69 {
70   arena[x][y] = item;
71
72   gtk_table_attach_defaults (GTK_TABLE (table),
73                              item,
74                              x, x+1,
75                              y, y+1);
76
77   if (item==robot)
78     {
79       robot_x = x;
80       robot_y = y;
81     }
82 }
83
84 void
85 place_in_arena_randomly (GtkWidget *item)
86 {
87   int x, y;
88    
89   do
90     {
91       x = random() % ARENA_WIDTH;
92       y = random() % ARENA_HEIGHT;
93     }
94   while (arena[x][y]);
95
96   place_in_arena_at_xy (item, x, y);
97 }
98
99 /****************************************************************/
100 /* Labels representing things the robot might find.             */
101 /****************************************************************/
102
103 GtkWidget *
104 random_character (gchar *description)
105 {
106   gchar character[2] = { random() % ('~'-'!') + '!', 0 };
107   gchar *escaped_character = g_markup_escape_text (character, -1);
108   gchar *markup = g_strdup_printf ("<span color=\"#%02x%02x%02x\">%s</span>",
109                                    (int) (random() % 0x7F)+0x80,
110                                    (int) (random() % 0x7F)+0x80,
111                                    (int) (random() % 0x7F)+0x80,
112                                    escaped_character);
113   GtkWidget *result = gtk_label_new (NULL);
114   gtk_label_set_markup (GTK_LABEL (result), markup);
115   g_free (markup);
116   g_free (escaped_character);
117
118   g_object_set_data (G_OBJECT (result), "examine", description);
119
120   return result;
121 }
122
123 /****************************************************************/
124 /* Talking back to the user.                                    */
125 /****************************************************************/
126
127 void
128 show_message (const char *message)
129 {
130   HildonNote* note = HILDON_NOTE
131     (hildon_note_new_information (GTK_WINDOW (window),
132                                   message));
133   gtk_dialog_run (GTK_DIALOG (note));
134   gtk_widget_destroy (GTK_WIDGET (note));
135 }
136
137 /****************************************************************/
138 /* Loading the non-kitten objects.                              */
139 /****************************************************************/
140 void
141 ensure_messages_loaded (void)
142 {
143   FILE *nki_file = NULL;
144   gchar *line = NULL;
145   gboolean headers = TRUE;
146
147   if (nki_count)
148     return;
149
150   nki_file = fopen ("/usr/share/rfk/non-kitten-items.rfk", "r");
151
152   if (!nki_file)
153     {
154       show_message ("Could not open list of non-kitten items!  Must quit.");
155       exit (EXIT_FAILURE);
156     }
157
158   while (!feof (nki_file))
159     {
160       char newline;
161       fscanf (nki_file, "%a[^\n]%c", &line, &newline);
162
163       if (strcmp(line, "")==0)
164         {
165           headers = FALSE;
166           fscanf (nki_file, "%c", &newline); 
167           free (line);
168         }
169       else if (headers)
170         {
171           /* we ignore all the headers for now */
172           free (line);
173         }
174       else
175         {
176           nki = g_slist_prepend (nki, line);
177           nki_count++;
178         }
179     }
180
181   fclose (nki_file);
182
183   used = g_malloc0 (nki_count);
184 }
185
186 /****************************************************************/
187 /* The ending animation.                                        */
188 /****************************************************************/
189
190 static gboolean
191 ending_animation_quit (gpointer data)
192 {
193   gtk_main_quit ();
194   return FALSE;
195 }
196
197 static gboolean
198 ending_animation_draw (GtkWidget *widget, GdkEventExpose *event, gpointer data)
199 {
200   /* We only run through once, so just make it static. */
201   static int cycle_count = 0;
202
203   static int robot_x = 0;
204   static int robot_stop = 0;
205   static int kitten_x = 0;
206   static int all_y = 0;
207
208   const int stepsize = 3;
209
210   if (!kitten_x)
211     {
212       all_y = (event->area.height - gdk_pixbuf_get_height (love_pic)) / 2;
213
214       robot_stop = gdk_pixbuf_get_width (robot_pic) + gdk_pixbuf_get_width (love_pic);
215       kitten_x = event->area.width - (cycle_count*stepsize + gdk_pixbuf_get_width (kitten_pic));
216     }
217
218   gdk_gc_set_foreground (widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
219                          &black);
220
221   gdk_draw_rectangle (GDK_DRAWABLE(widget->window),
222                       widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
223                       TRUE,
224                       0, 0, event->area.width, event->area.height);
225
226   gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
227                    widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
228                    robot_pic, 0, 0,
229                    robot_x, all_y,
230                    -1, -1,
231                    GDK_RGB_DITHER_NONE, 0, 0);
232
233   gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
234                    widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
235                    kitten_pic, 0, 0,
236                    kitten_x, all_y,
237                    -1, -1,
238                    GDK_RGB_DITHER_NONE, 0, 0);
239
240   cycle_count++;
241   robot_x += stepsize;
242   kitten_x -= stepsize;
243
244   if (robot_x+robot_stop >= kitten_x)
245     {
246       gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
247                        widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
248                        love_pic, 0, 0,
249                        robot_x + gdk_pixbuf_get_width (robot_pic), all_y,
250                        -1, -1,
251                        GDK_RGB_DITHER_NONE, 0, 0);
252
253       g_object_unref (love_pic);
254       love_pic = NULL;
255
256       g_timeout_add (2000, ending_animation_quit, NULL);
257     }
258
259   return TRUE;
260 }
261
262 static gboolean
263 ending_animation_step (gpointer data)
264 {
265   if (love_pic)
266     {
267       gdk_window_invalidate_rect (animation_area->window,
268                                   NULL, TRUE);
269
270       return TRUE;
271     }
272   else
273     return FALSE;
274 }
275
276 static void
277 ending_animation ()
278 {
279   robot_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-robot.png", NULL);
280   love_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-love.png", NULL);
281   kitten_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-kitten.png", NULL);
282   animation_area =  gtk_drawing_area_new ();
283
284   gtk_container_remove (GTK_CONTAINER (window), GTK_WIDGET (table));
285   gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (animation_area));
286   gtk_widget_show_all (window);
287
288   g_signal_connect (G_OBJECT (animation_area),
289                     "expose_event", G_CALLBACK (ending_animation_draw), NULL);
290   g_timeout_add (10, ending_animation_step, NULL);
291 }
292
293 /****************************************************************/
294 /* Moving the robot.  Way to go, robot!                         */
295 /****************************************************************/
296
297 typedef struct {
298   guint gdk_key;
299   gchar vi_key; /* or nethack equivalent */
300   guint8 move_x;
301   guint8 move_y;
302 } direction;
303
304 direction directions[] = {
305   { GDK_Home,      'y', -1, -1 },
306   { GDK_Left,      'h', -1,  0 },
307   { GDK_End,       'b', -1,  1 },
308   { GDK_Down,      'j',  0,  1 },
309   { GDK_Page_Down, 'n',  1,  1 },
310   { GDK_Right,     'l',  1,  0 },
311   { GDK_Page_Up,   'u',  1, -1 },
312   { GDK_Up,        'k',  0, -1 }
313 };
314
315 gboolean
316 move_robot (guint8 whichway)
317 {
318   GtkWidget *new_space;
319   gint8 dx = directions[whichway].move_x;
320   gint8 dy = directions[whichway].move_y;
321
322   const char *found;
323
324   if (robot_x+dx<0 ||
325       robot_y+dy<0 ||
326       robot_x+dx>=ARENA_WIDTH ||
327       robot_y+dy>=ARENA_HEIGHT)
328     return TRUE;
329
330   new_space = arena[robot_x+dx][robot_y+dy];
331   found = g_object_get_data (G_OBJECT (new_space), "examine");
332
333   if (found && *found)
334     {
335       show_message (found);
336
337       if (new_space == kitten)
338         {
339           ending_animation ();
340         }
341
342       return TRUE;
343     }
344   else
345     {
346       /* just an ordinary move into an empty space */
347
348       g_object_ref (new_space);
349
350       gtk_container_remove (GTK_CONTAINER (table), robot);
351       gtk_container_remove (GTK_CONTAINER (table), new_space);
352
353       place_in_arena_at_xy (new_space, robot_x, robot_y);
354       place_in_arena_at_xy (robot, robot_x+dx, robot_y+dy);
355
356       g_object_unref (new_space);
357
358       return FALSE;
359     }
360 }
361
362 /****************************************************************/
363 /* Event handlers.                                              */
364 /****************************************************************/
365
366 gboolean
367 on_window_clicked (GtkWidget      *widget,
368                    GdkEventButton *event,
369                    gpointer        user_data)
370 {
371   /** Centre point of robot's representation on screen */
372   int rx, ry;
373   double angle;
374
375   rx = (robot->allocation.x+robot->allocation.width/2);
376   ry = (robot->allocation.y+robot->allocation.height/2);
377
378   angle = atan2(event->x - rx,
379                 event->y - ry) +
380     M_PI +
381     M_PI/8;
382
383   move_robot (((int) (angle / (M_PI/4)))-1);
384
385   return TRUE;
386 }
387
388 gboolean
389 on_key_pressed (GtkWidget      *widget,
390                 GdkEventKey    *event,
391                 gpointer        user_data)
392 {
393   gint i;
394   guint keyval = event->keyval;
395
396   if (keyval>='A' && keyval<='Z')
397     {
398       keyval += ('a'-'A');
399     }
400
401   for (i=0; i<G_N_ELEMENTS(directions); i++)
402     {
403       if (keyval==directions[i].gdk_key ||
404           keyval==directions[i].vi_key)
405         {
406           if (event->state & GDK_SHIFT_MASK)
407             {
408               while (!move_robot (i))
409                 {
410                   /* keep going, robot! */
411                 }
412             }
413           else
414             {
415               move_robot (i);
416             }
417           return FALSE;
418         }
419     }
420
421   return FALSE;
422 }
423
424 /****************************************************************/
425 /* Let's kick the whole thing off...                            */
426 /****************************************************************/
427
428 int
429 main (gint argc,
430       gchar **argv)
431 {
432   int x, y;
433
434   gtk_init (&argc, &argv);
435   g_set_application_name ("robotfindskitten");
436   srandom (time(0));
437
438   ensure_messages_loaded ();
439
440   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
441   gtk_window_set_title (GTK_WINDOW (window), "robotfindskitten");
442   gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &black);
443   g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK (on_window_clicked), NULL);
444   g_signal_connect (G_OBJECT (window), "key-press-event", G_CALLBACK (on_key_pressed), NULL);
445   g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
446         
447   table = gtk_table_new (ARENA_HEIGHT, ARENA_WIDTH, TRUE);
448   gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (table));
449
450   robot = gtk_label_new ("#");
451   g_object_ref (robot);
452   kitten = random_character ("You found kitten!  Way to go, robot!");
453
454   place_in_arena_randomly (robot);
455   place_in_arena_randomly (kitten);
456
457   for (x=0; x < amount_of_random_stuff; x++)
458     place_in_arena_randomly (random_character (description ()));
459
460   for (x=0; x < ARENA_WIDTH; x++)
461     for (y=0; y < ARENA_HEIGHT; y++)
462       if (!arena[x][y])
463         place_in_arena_at_xy (gtk_label_new (NULL), x, y);
464
465   gtk_widget_show_all (window);
466
467   gdk_window_set_events (GTK_WIDGET (window)->window,
468                          gdk_window_get_events(GTK_WIDGET (window)->window) | GDK_BUTTON_PRESS_MASK);
469
470         
471   show_message (explanation);
472
473   gtk_main ();
474
475   return EXIT_SUCCESS;
476 }