1 /* robotfindskitten for maemo
2 * original by Leonard Richardson, 1997
3 * ported to maemo by Thomas Thurman, 2009
6 * gcc -Wall -g rfk.c -o rfk `pkg-config --cflags --libs gtk+-2.0 hildon-1 dbus-glib-1 dbus-1`
9 #include <dbus/dbus-glib.h>
13 #include <hildon/hildon.h>
18 #define ARENA_WIDTH 25
19 #define ARENA_HEIGHT 12
21 const int amount_of_random_stuff = 1;
30 StateOfPlay current_state = STATE_LAST;
31 GtkWidget* state_widget[STATE_LAST];
36 GtkWidget *arena[ARENA_WIDTH][ARENA_HEIGHT];
37 GtkWidget *window, *robot, *kitten;
39 gboolean *used = NULL;
41 GdkPixbuf *robot_pic, *love_pic, *kitten_pic;
43 const GdkColor black = { 0, };
45 /****************************************************************/
46 /* Random object descriptions. */
47 /****************************************************************/
56 r = random() % nki_count;
61 return g_slist_nth_data (nki, r);
64 /****************************************************************/
65 /* Placing objects. */
66 /****************************************************************/
69 place_in_arena_at_xy (GtkWidget *item, int x, int y)
73 gtk_table_attach_defaults (GTK_TABLE (state_widget[STATE_PLAYING]),
86 place_in_arena_randomly (GtkWidget *item)
92 x = random() % ARENA_WIDTH;
93 y = random() % ARENA_HEIGHT;
97 place_in_arena_at_xy (item, x, y);
100 /****************************************************************/
101 /* Labels representing things the robot might find. */
102 /****************************************************************/
105 random_character (gchar *description)
107 gchar character[2] = { random() % ('~'-'!') + '!', 0 };
108 gchar *escaped_character = g_markup_escape_text (character, -1);
109 gchar *markup = g_strdup_printf ("<span color=\"#%02x%02x%02x\">%s</span>",
110 (int) (random() % 0x7F)+0x80,
111 (int) (random() % 0x7F)+0x80,
112 (int) (random() % 0x7F)+0x80,
114 GtkWidget *result = gtk_label_new (NULL);
115 gtk_label_set_markup (GTK_LABEL (result), markup);
117 g_free (escaped_character);
119 g_object_set_data (G_OBJECT (result), "examine", description);
124 /****************************************************************/
125 /* Talking back to the user. */
126 /****************************************************************/
129 show_message (const char *message)
131 HildonNote* note = HILDON_NOTE
132 (hildon_note_new_information (GTK_WINDOW (window),
134 "Some message was supposed to be here."));
135 gtk_dialog_run (GTK_DIALOG (note));
136 gtk_widget_destroy (GTK_WIDGET (note));
139 /****************************************************************/
140 /* Loading the non-kitten objects. */
141 /****************************************************************/
143 ensure_messages_loaded (void)
145 FILE *nki_file = NULL;
147 gboolean headers = TRUE;
152 nki_file = fopen ("/usr/share/rfk/non-kitten-items.rfk", "r");
156 show_message ("Could not open list of non-kitten items! Must quit.");
160 while (!feof (nki_file))
163 if (fscanf (nki_file, "%a[^\n]%c", &line, &newline) == EOF)
168 if (strcmp(line, "")==0)
171 fscanf (nki_file, "%c", &newline);
176 /* we ignore all the headers for now */
181 nki = g_slist_prepend (nki, line);
188 used = g_malloc0 (nki_count);
194 robot_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-robot.png", NULL);
195 love_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-love.png", NULL);
196 kitten_pic = gdk_pixbuf_new_from_file ("/usr/share/rfk/rfk-kitten.png", NULL);
199 /****************************************************************/
200 /* Stop doing that, and do something else. */
201 /****************************************************************/
203 switch_state (StateOfPlay new_state)
205 if (current_state != STATE_LAST)
207 gtk_container_remove (GTK_CONTAINER (window), state_widget[current_state]);
209 gtk_container_add (GTK_CONTAINER (window), state_widget[new_state]);
211 gtk_widget_show_all (window);
212 gdk_window_set_events (GTK_WIDGET (window)->window,
213 gdk_window_get_events(GTK_WIDGET (window)->window) | GDK_BUTTON_PRESS_MASK);
215 current_state = new_state;
218 /****************************************************************/
219 /* The ending animation. */
220 /****************************************************************/
222 gboolean animation_running = FALSE;
225 ending_animation_quit (gpointer data)
227 switch_state (STATE_PROLOGUE);
232 ending_animation_draw (GtkWidget *widget, GdkEventExpose *event, gpointer data)
234 static int cycle_count = 0;
236 static int robot_x = 0;
237 static int robot_stop = 0;
238 static int kitten_x = 0;
239 static int all_y = 0;
240 static GdkGC *gc = NULL;
242 const int stepsize = 3;
246 gc = gdk_gc_new (GDK_DRAWABLE (widget->window));
248 all_y = (event->area.height - gdk_pixbuf_get_height (love_pic)) / 2;
250 robot_stop = gdk_pixbuf_get_width (robot_pic) + gdk_pixbuf_get_width (love_pic);
251 kitten_x = event->area.width - (cycle_count*stepsize + gdk_pixbuf_get_width (kitten_pic));
254 gdk_gc_set_foreground (gc, &black);
256 gdk_draw_rectangle (GDK_DRAWABLE (widget->window),
259 0, 0, event->area.width, event->area.height);
261 gdk_draw_pixbuf (GDK_DRAWABLE (widget->window),
266 GDK_RGB_DITHER_NONE, 0, 0);
268 gdk_draw_pixbuf (GDK_DRAWABLE (widget->window),
273 GDK_RGB_DITHER_NONE, 0, 0);
277 kitten_x -= stepsize;
279 if (robot_x+robot_stop >= kitten_x)
281 gdk_draw_pixbuf (GDK_DRAWABLE(widget->window),
284 robot_x + gdk_pixbuf_get_width (robot_pic), all_y,
286 GDK_RGB_DITHER_NONE, 0, 0);
288 animation_running = FALSE;
290 g_timeout_add (2000, ending_animation_quit, NULL);
305 ending_animation_step (gpointer data)
307 if (animation_running)
309 gdk_window_invalidate_rect (state_widget[STATE_EPILOGUE]->window,
321 if (current_state!=STATE_EPILOGUE)
323 animation_running = TRUE;
324 g_timeout_add (10, ending_animation_step, NULL);
328 /****************************************************************/
329 /* Moving the robot. Way to go, robot! */
330 /****************************************************************/
334 gchar vi_key; /* or nethack equivalent */
339 direction directions[] = {
340 { GDK_Home, 'y', -1, -1 },
341 { GDK_Left, 'h', -1, 0 },
342 { GDK_End, 'b', -1, 1 },
343 { GDK_Down, 'j', 0, 1 },
344 { GDK_Page_Down, 'n', 1, 1 },
345 { GDK_Right, 'l', 1, 0 },
346 { GDK_Page_Up, 'u', 1, -1 },
347 { GDK_Up, 'k', 0, -1 }
351 move_robot (guint8 whichway)
353 GtkWidget *new_space;
354 gint8 dx = directions[whichway].move_x;
355 gint8 dy = directions[whichway].move_y;
361 robot_x+dx>=ARENA_WIDTH ||
362 robot_y+dy>=ARENA_HEIGHT)
365 new_space = arena[robot_x+dx][robot_y+dy];
366 found = g_object_get_data (G_OBJECT (new_space), "examine");
370 show_message (found);
372 if (new_space == kitten)
374 switch_state (STATE_EPILOGUE);
381 /* just an ordinary move into an empty space */
383 g_object_ref (new_space);
385 gtk_container_remove (GTK_CONTAINER (state_widget[STATE_PLAYING]), robot);
386 gtk_container_remove (GTK_CONTAINER (state_widget[STATE_PLAYING]), new_space);
388 place_in_arena_at_xy (new_space, robot_x, robot_y);
389 place_in_arena_at_xy (robot, robot_x+dx, robot_y+dy);
391 g_object_unref (new_space);
397 /****************************************************************/
398 /* Event handlers. */
399 /****************************************************************/
402 on_window_clicked (GtkWidget *widget,
403 GdkEventButton *event,
406 /** Centre point of robot's representation on screen */
410 if (current_state!=STATE_PLAYING)
415 rx = (robot->allocation.x+robot->allocation.width/2);
416 ry = (robot->allocation.y+robot->allocation.height/2);
418 angle = atan2(event->x - rx,
422 move_robot (((int) (angle / (M_PI/4))) % 8);
428 on_key_pressed (GtkWidget *widget,
433 guint keyval = event->keyval;
435 if (current_state!=STATE_PLAYING)
440 if (keyval>='A' && keyval<='Z')
445 for (i=0; i<G_N_ELEMENTS(directions); i++)
447 if (keyval==directions[i].gdk_key ||
448 keyval==directions[i].vi_key)
450 if (event->state & GDK_SHIFT_MASK)
452 while (!move_robot (i))
454 /* keep going, robot! */
468 /****************************************************************/
470 /****************************************************************/
472 get_help (gpointer button, gpointer data)
474 DBusGConnection *connection;
475 GError *error = NULL;
479 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
481 if (connection == NULL)
483 show_message (error->message);
484 g_error_free (error);
488 proxy = dbus_g_proxy_new_for_name (connection,
489 "com.nokia.osso_browser",
490 "/com/nokia/osso_browser/request",
491 "com.nokia.osso_browser");
494 if (!dbus_g_proxy_call (proxy, "load_url", &error,
495 G_TYPE_STRING, "/usr/share/rfk/help.html",
499 show_message (error->message);
500 g_error_free (error);
507 play_game (gpointer button, gpointer data)
509 switch_state (STATE_PLAYING);
517 if (current_state==STATE_PLAYING)
519 /* end of the game; clean up */
521 for (x=0; x < ARENA_WIDTH; x++)
522 for (y=0; y < ARENA_HEIGHT; y++)
525 gtk_container_remove (GTK_CONTAINER (state_widget[STATE_PLAYING]),
530 g_object_unref (robot);
531 g_object_unref (kitten);
535 /* make everything new */
537 robot = gtk_label_new ("#");
538 g_object_ref (robot);
539 kitten = random_character ("You found kitten! Way to go, robot!");
540 g_object_ref (kitten);
542 place_in_arena_randomly (robot);
543 place_in_arena_randomly (kitten);
545 if (nki_count < amount_of_random_stuff)
547 /* sanity check failed */
548 show_message ("There are too few non-kitten items to play a meaningful game.");
552 for (x=0; x < amount_of_random_stuff; x++)
553 place_in_arena_randomly (random_character (description ()));
555 for (x=0; x < ARENA_WIDTH; x++)
556 for (y=0; y < ARENA_HEIGHT; y++)
558 place_in_arena_at_xy (gtk_label_new (NULL), x, y);
563 set_up_widgets (void)
565 GtkWidget *middle = gtk_hbox_new (FALSE, 0);
566 GtkWidget *buttons = gtk_hbox_new (TRUE, 0);
567 GtkWidget *explain = NULL, *help_button, *play_button, *intro;
568 const char *explanation =
569 "In this game, you are robot (#). "
570 "Your job is to find kitten. This task is complicated "
571 "by the existence of various things which are not kitten. "
572 "Robot must touch items to determine if they are kitten or "
573 "not. The game ends when robotfindskitten. You may move "
574 "robot about by tapping on any side of robot, or with the "
576 GKeyFile *desktop = g_key_file_new ();
582 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
583 gtk_window_set_title (GTK_WINDOW (window), "robotfindskitten");
584 gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &black);
585 g_signal_connect (G_OBJECT (window), "button-press-event", G_CALLBACK (on_window_clicked), NULL);
586 g_signal_connect (G_OBJECT (window), "key-press-event", G_CALLBACK (on_key_pressed), NULL);
587 g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
591 if (g_key_file_load_from_file (desktop,
592 "/usr/share/applications/hildon/rfk.desktop",
596 version = g_strdup_printf("v%s.%d",
597 g_key_file_get_value (desktop, "Desktop Entry", "Version", NULL),
599 g_key_file_free (desktop);
603 version = g_strdup("");
606 help_button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_THUMB_HEIGHT,
607 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
609 g_signal_connect (help_button, "clicked", G_CALLBACK (get_help), NULL);
611 play_button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_THUMB_HEIGHT,
612 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
614 g_signal_connect (play_button, "clicked", G_CALLBACK (play_game), NULL);
616 gtk_box_pack_end (GTK_BOX (buttons), play_button, TRUE, TRUE, 0);
617 gtk_box_pack_end (GTK_BOX (buttons), help_button, TRUE, TRUE, 0);
619 explain = gtk_label_new (explanation);
620 gtk_label_set_line_wrap (GTK_LABEL (explain), TRUE);
622 gtk_box_pack_end (GTK_BOX (middle), explain, TRUE, TRUE, 0);
623 gtk_box_pack_end (GTK_BOX (middle), gtk_image_new_from_pixbuf (robot_pic), FALSE, FALSE, 0);
625 intro = gtk_vbox_new (FALSE, 0);
626 gtk_box_pack_end (GTK_BOX (intro), buttons, FALSE, FALSE, 0);
627 gtk_box_pack_end (GTK_BOX (intro), middle, TRUE, TRUE, 0);
628 gtk_box_pack_end (GTK_BOX (intro), gtk_label_new (version), FALSE, FALSE, 0);
631 state_widget[STATE_PROLOGUE] = intro;
633 /* The game itself */
635 state_widget[STATE_PLAYING] = gtk_table_new (ARENA_HEIGHT, ARENA_WIDTH, TRUE);
636 g_signal_connect (state_widget[STATE_PLAYING], "parent-set", G_CALLBACK (set_up_board), NULL);
638 for (x=0; x < ARENA_WIDTH; x++)
639 for (y=0; y < ARENA_HEIGHT; y++)
643 state_widget[STATE_EPILOGUE] = gtk_drawing_area_new ();
644 g_signal_connect (state_widget[STATE_EPILOGUE], "parent-set", G_CALLBACK (ending_animation), NULL);
645 g_signal_connect (G_OBJECT (state_widget[STATE_EPILOGUE]),
646 "expose_event", G_CALLBACK (ending_animation_draw), NULL);
648 for (x=0; x<STATE_LAST; x++)
650 /* so we don't lose them when we take them offscreen */
651 g_object_ref (state_widget[x]);
655 /****************************************************************/
656 /* Let's kick the whole thing off... */
657 /****************************************************************/
663 gtk_init (&argc, &argv);
664 g_set_application_name ("robotfindskitten");
667 ensure_messages_loaded ();
671 switch_state (STATE_PROLOGUE);