1 /* grrobot - Ricochet Robot using GTK+ and Xr
3 * Copyright © 2003 Carl Worth
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without
7 * fee, provided that the above copyright notice appear in all copies
8 * and that both that copyright notice and this permission notice
9 * appear in supporting documentation, and that the name of Carl Worth
10 * not be used in advertising or publicity pertaining to distribution
11 * of the software without specific, written prior permission.
12 * Carl Worth makes no representations about the suitability of this
13 * software for any purpose. It is provided "as is" without express
14 * or implied warranty.
16 * CARL WORTH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
18 * NO EVENT SHALL CARL WORTH BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
20 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
21 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * Author: Carl Worth <carl@theworths.org>
37 #include "grr_board_view.h"
41 typedef struct grr_game {
46 GtkWidget *board_view;
47 GtkWidget *command_entry;
49 GtkTextBuffer *message_buffer;
50 GtkWidget *message_view;
53 rr_robot_t last_move_robot;
57 grr_game_printf (grr_game_t *game, const char *fmt, ...);
60 grr_game_vprintf (grr_game_t *game, const char *fmt, va_list ap);
63 grr_game_print (grr_game_t *game, const char *msg);
66 grr_game_start_gui (grr_game_t *game);
69 grr_game_read_notices (grr_game_t *game);
72 grr_game_notices_source_new (grr_game_t *game);
75 main (int argc, char **argv)
84 gtk_init (&argc, &argv);
86 args_parse (&args, argc, argv);
88 game.client = rr_client_create (args.host, args.port, args.user);
89 if (game.client == NULL) {
90 fprintf (stderr, "Failed connecting to %s:%s as %s\n",
91 args.host, args.port, args.user);
95 status = rr_client_join (game.client, args.game);
96 if (status == RR_STATUS_NO_GAME) {
97 status = rr_client_new (game.client, args.game);
100 game.board = rr_board_create (16, 16);
102 game.last_move_msg_id = 0;
103 game.last_move_robot = RR_ROBOT_NONE;
105 source = grr_game_notices_source_new (&game);
106 g_source_set_priority (source, GDK_PRIORITY_EVENTS);
107 g_source_attach (source, NULL);
108 g_source_unref (source);
110 rr_client_show (game.client, &diagram);
111 rr_board_parse (game.board, diagram);
114 return grr_game_start_gui (&game);
117 typedef struct _GrrNoticeSource {
123 grr_game_notices_prepare (GSource *source,
126 GrrNoticeSource *src = (GrrNoticeSource*)source;
127 grr_game_t *game = src->game;
130 GDK_THREADS_ENTER ();
132 retval = rr_client_notice_pending (game->client);
133 GDK_THREADS_LEAVE ();
139 grr_game_notices_check (GSource *source)
141 GrrNoticeSource *src = (GrrNoticeSource*)source;
142 grr_game_t *game = src->game;
145 GDK_THREADS_ENTER ();
146 retval = rr_client_notice_pending (game->client);
147 GDK_THREADS_LEAVE ();
153 grr_game_notices_dispatch (GSource *source,
154 GSourceFunc callback,
157 GrrNoticeSource *src = (GrrNoticeSource*)source;
158 grr_game_t *game = src->game;
160 grr_game_read_notices (game);
165 static GSourceFuncs game_notices_funcs = {
166 grr_game_notices_prepare,
167 grr_game_notices_check,
168 grr_game_notices_dispatch,
173 grr_game_notices_source_new (grr_game_t *game)
175 GSource *source = g_source_new (&game_notices_funcs, sizeof (GrrNoticeSource));
176 GrrNoticeSource *src = (GrrNoticeSource*)source;
182 grr_game_read_notices (grr_game_t *game)
184 rr_board_t *board = game->board;
188 while (rr_client_notice_pending (game->client)) {
189 status = rr_client_next_notice (game->client, ¬ice);
191 fprintf (stderr, "Error during rr_client_next_notice: %s\n",
192 rr_status_str (status));
197 switch (notice->type) {
199 grr_game_printf (game,
200 "\nGLOBAL: %s has connected.", notice->u.string);
203 grr_game_printf (game,
204 "\nGLOBAL: %s has disconnected.", notice->u.string);
207 grr_game_printf (game,
208 "\nGLOBAL: New game available: %s.", notice->u.string);
210 case RR_NOTICE_DISPOSE:
211 grr_game_printf (game,
212 "\nGLOBAL: Game %s has been disposed.", notice->u.string);
214 case RR_NOTICE_MESSAGE:
215 grr_game_printf (game, "\n%s> %s",
216 notice->u.message.username,
217 notice->u.message.text);
219 case RR_NOTICE_GAMESTATE:
220 grr_game_printf (game, "\nGame state changed to: %s.",
221 rr_gamestate_str (notice->u.gamestate));
224 grr_game_printf (game, "\nUser %s has joined the game.",
227 case RR_NOTICE_WATCH:
228 grr_game_printf (game, "\nUser %s has started watching the game.",
232 grr_game_printf (game, "\nUser %s has left the game.",
236 grr_game_printf (game, "\nUser %s bids %d.",
237 notice->u.bid.username,
238 notice->u.bid.number);
240 case RR_NOTICE_REVOKE:
241 grr_game_printf (game, "\nUser %s revokes previous bid.",
244 case RR_NOTICE_ABANDON:
245 grr_game_printf (game, "\nUser %s is willing to abandon this turn.",
248 case RR_NOTICE_NOBID:
249 grr_game_printf (game, "\nUser %s sees no point in bidding further this round",
253 if (game->msg_id == game->last_move_msg_id
254 && game->last_move_robot == notice->u.move.robot) {
255 game->last_move_msg_id = grr_game_printf (game, " %s",
256 rr_direction_str (notice->u.move.direction));
258 game->last_move_msg_id = grr_game_printf (game, "\nMove #%d: %s %s",
259 notice->u.move.count,
260 rr_robot_str (notice->u.move.robot),
261 rr_direction_str (notice->u.move.direction));
262 game->last_move_robot = notice->u.move.robot;
266 grr_game_print (game, "\nUNDO");
268 case RR_NOTICE_RESET:
269 grr_game_print (game, "\nRESET");
271 case RR_NOTICE_SCORE:
272 grr_game_printf (game, "\nScore for %s: %d.",
273 notice->u.bid.username,
274 notice->u.bid.number);
276 case RR_NOTICE_ACTIVATE:
277 grr_game_printf (game, "\nYour turn.",
280 case RR_NOTICE_ACTIVE:
281 grr_game_printf (game, "\nUser %s now active to demonstrate solution in %d moves.",
282 notice->u.bid.username,
283 notice->u.bid.number);
285 case RR_NOTICE_TIMER:
286 grr_game_printf (game, "\nTime remaining: %d seconds.",
289 case RR_NOTICE_POSITION:
290 rr_board_position (board, notice->u.position.robot,
291 notice->u.position.x, notice->u.position.y);
292 gtk_widget_queue_draw (GTK_WIDGET (game->window));
295 grr_game_print (game, "\nNew round!");
296 rr_board_set_goal_target (board, notice->u.target);
297 gtk_widget_queue_draw (GTK_WIDGET (game->window));
305 grr_game_entry_callback (GtkWidget *widget,
309 const gchar *entry_text;
313 entry_text = gtk_entry_get_text (GTK_ENTRY (game->command_entry));
315 status = rr_client_request (game->client, entry_text, &response);
317 grr_game_printf (game, "\nERROR: %s.", rr_status_str (status));
320 grr_game_print (game, "\n");
321 for (i=0; response[i]; i++)
322 grr_game_printf (game, "%s ", response[i]);
326 gtk_entry_set_text (GTK_ENTRY (game->command_entry), "");
328 /* XXX: Huh? Why is this triggering valgrind?
335 grr_game_printf (grr_game_t *game, const char *fmt, ...)
342 msg_id = grr_game_vprintf (game, fmt, ap);
349 grr_game_vprintf (grr_game_t *game, const char *fmt, va_list ap)
354 grr_sprintf_alloc_va (&msg, fmt, ap);
358 msg_id = grr_game_print (game, msg);
366 grr_game_print (grr_game_t *game, const char *msg)
371 /* There might be a lighter way to do this, but this seems to
374 gtk_text_buffer_get_end_iter (game->message_buffer, &iter);
376 mark = gtk_text_buffer_get_mark (game->message_buffer, "end");
378 gtk_text_buffer_move_mark (game->message_buffer, mark, &iter);
380 gtk_text_buffer_insert (game->message_buffer, &iter, msg, -1);
382 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (game->message_view),
383 mark, 0.0, TRUE, 0.0, 1.0);
385 return ++game->msg_id;
389 grr_game_start_gui (grr_game_t *game)
391 GtkWidget *board_frame;
396 GtkWidget *message_view;
397 GtkWidget *command_entry;
399 game->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
400 window = game->window;
402 gtk_window_set_title (GTK_WINDOW (window), "Ricochet Robot");
403 gtk_window_set_default_size (GTK_WINDOW (window), 512, 512);
405 g_signal_connect (G_OBJECT (window), "destroy",
406 G_CALLBACK (exit), NULL);
407 gtk_container_set_border_width (GTK_CONTAINER (window), 0);
409 vbox = gtk_vbox_new (FALSE, 1);
410 gtk_container_add (GTK_CONTAINER (window), vbox);
413 game->board_view = grr_board_view_new (game->board);
414 grr_board_view_set_client (GRR_BOARD_VIEW (game->board_view), game->client);
415 game->message_buffer = gtk_text_buffer_new (NULL);
416 gtk_text_buffer_get_iter_at_offset (game->message_buffer,
418 gtk_text_buffer_create_mark (game->message_buffer,
419 "end", &iter, FALSE);
420 game->message_view = gtk_text_view_new_with_buffer (game->message_buffer);
421 game->command_entry = gtk_entry_new ();
423 vpaned = gtk_vpaned_new ();
424 gtk_container_set_border_width (GTK_CONTAINER (vpaned), 0);
425 gtk_box_pack_start (GTK_BOX (vbox), vpaned,
428 board_frame = gtk_aspect_frame_new (NULL, 0.5, 0.5, 1.0, FALSE);
429 gtk_paned_pack1 (GTK_PANED (vpaned), board_frame, TRUE, TRUE);
431 gtk_container_add (GTK_CONTAINER (board_frame), game->board_view);
432 gtk_widget_show (game->board_view);
434 gtk_widget_show (board_frame);
436 sw = gtk_scrolled_window_new (NULL, NULL);
437 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
439 GTK_POLICY_AUTOMATIC);
440 gtk_paned_pack2 (GTK_PANED (vpaned), sw, FALSE, TRUE);
442 message_view = game->message_view;
443 gtk_text_view_set_editable (GTK_TEXT_VIEW (message_view), FALSE);
444 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (message_view), FALSE);
445 gtk_container_add (GTK_CONTAINER (sw), message_view);
446 gtk_widget_show (message_view);
448 gtk_widget_show (sw);
450 gtk_widget_show (vpaned);
452 command_entry = game->command_entry;
453 gtk_box_pack_end (GTK_BOX (vbox), command_entry,
455 gtk_widget_show (command_entry);
456 g_signal_connect (G_OBJECT (command_entry), "activate",
457 G_CALLBACK (grr_game_entry_callback),
460 gtk_widget_show (vbox);
462 gtk_widget_show (window);