summaryrefslogtreecommitdiff
path: root/src/main.c
blob: f12b3e964684495f49ba2b073f1aa7926dc4d25b (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* Copyright (C) 1998 Jim Hall <jhall@freedos.org>
 * Copyright (C) 2008 Bradley Smith <brad@brad-smith.co.uk>
 *
 * GNU Robots game engine. This is the main() program, using GNU
 * Guile as the backend to handle the language.
 *
 * GNU Robots 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 3 of the License, or
 * (at your option) any later version.
 *
 * GNU Robots 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 GNU Robots.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <unistd.h>			/* for getopt */
#include <string.h>			/* for strdup */

#include <glib.h>
#include <glib/gprintf.h>

#include <libguile.h>

#include <getopt.h>			/* for GNU getopt_long */

#include "grobot.h"			/* the robot structure, and robot manipulation
								   routines */

#include "api.h"			/* robot API, as Scheme functions */
#include "ui-cmdwin.h"
#include "ui-arena.h"
#include "ui-window.h"
#include "ui-window-private.h"
#include "config.h"
#include "configs.h"
#include "map.h"			/* Game Map */
#include "main.h"			/* for this source file */

#define BUFF_LEN 1024

/* Globals (share with api.c) */
GList *robots = NULL;
GRobot *robot = NULL;		/* The current robot */
UIWindow *ui;
UIArena *arena;
Map *map = NULL;

gpointer callback_scm(gpointer data);
void gui_init();
void gui_main();
SCM catch_handler(void *data, SCM tag, SCM throw_args);
gint is_file_readable(const gchar * filename);

/************************************************************************
 * gint main(gint argc, gchar *argv[])                                  *
 *                                                                      *
 * Entry point                                                          *
 ************************************************************************/
gint main(gint argc, gchar *argv[])
{
	gint opt;				/* the option read from getopt */
	gint flag;				/* flag passed back from getopt - NOT USED */

	gchar maps_path[MAX_PATH], scripts_path[MAX_PATH];

	static
	gchar *main_argv[5] = { "GNU Robots",
		NULL,
		NULL,
		NULL,
		NULL
	};

	struct option long_opts[] = {
		{"version", 0, NULL, 'V'},
		{"help", 0, NULL, 'h'},
		{"map-file", 1, NULL, 'f'},
		{"shields", 1, NULL, 's'},
		{"energy", 1, NULL, 'e'},
		{NULL, 0, NULL, 0}
	};

	/* Check command line */

	/* Create a robot Object */
	robot = g_robot_new(1, 1, EAST, 0, DEFAULT_ENERGY, DEFAULT_SHIELDS, 0, 0,
		NULL, NULL);

	g_assert(robot != NULL);

	/* And add to to the list of robots */
	robots = g_list_append(robots, robot);

	while ((opt = getopt_long(argc, argv, "Vhf:s:e:p:", long_opts,
				&flag)) != EOF)
	{
		switch (opt)
		{
		case 'V':
			/* Display version, then quit */
			g_printf("%s\n\n", PKGINFO);
			g_printf("%s\n\n", COPYRIGHT);
			g_printf
("GNU Robots is free software: you can redistribute it and/or modify\n"
 "it under the terms of the GNU General Public License as published by\n"
 "the Free Software Foundation, either version 3 of the License, or\n"
 "(at your option) any later version.\n");
			g_printf
("\nGNU Robots is distributed in the hope that it will be useful,\n"
 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
 "GNU General Public License for more details.\n");
			g_printf
("\nYou should have received a copy of the GNU General Public License\n"
 "along with GNU Robots.  If not, see <http://www.gnu.org/licenses/>.\n");

			exit(0);
			break;

		case 'h':
			/* Display help, then quit. */
			usage(argv[0]);
			exit(0);
			break;

		case 'f':
			/* Set map file */
			main_argv[1] = optarg;	/* pointer assignment */
			break;

		case 's':
			/* Set shields */
			robot->shields = (glong) atol(optarg);
			break;

		case 'e':
			/* Set energy */
			robot->energy = (glong) atol(optarg);
			break;

		default:
			/* invalid option */
			usage(argv[0]);
			exit(1);
			break;
		}						/* switch */
	}							/* while */

	/* Extra arg is the Scheme file */

	if (optind < argc)
	{
		/* Set Scheme file */
		main_argv[2] = argv[optind];	/* pointer assignment */
	}

	/* Check that files have been given */
	if (main_argv[1] == NULL)
	{
		main_argv[1] = g_malloc(MAX_PATH);
		g_strlcpy(main_argv[1], DEFAULT_MAP, MAX_PATH);
		g_fprintf(stderr, "map file not specified, trying default: %s\n",
			main_argv[1]);
	}

	/* Check that files exist */
	g_strlcpy(maps_path, main_argv[1], MAX_PATH);

	if (!is_file_readable(maps_path))
	{
		g_strlcpy(maps_path, MAPS_PATH, MAX_PATH);
		g_strlcat(maps_path, "/", MAX_PATH);
		g_strlcat(maps_path, main_argv[1], MAX_PATH);

		if (!is_file_readable(maps_path))
		{
			gchar *env = getenv(MAPS_PATH_ENV);

			if (env != NULL)
			{
				g_strlcpy(maps_path, env, MAX_PATH);
				g_strlcat(maps_path, "/", MAX_PATH);
				g_strlcat(maps_path, main_argv[1], MAX_PATH);

				if (!is_file_readable(maps_path))
				{
					g_fprintf(stderr,
				"%s: %s: game map file does not exist or is not readable\n",
						argv[0], main_argv[1]);
					exit(1);
				}
			}
			else
			{
				g_fprintf(stderr,
				"%s: %s: game map file does not exist or is not readable\n",
					argv[0], main_argv[1]);
				exit(1);
			}
		}
	}

	main_argv[1] = maps_path;

	/* Now the Scheme file */
	if (main_argv[2] != NULL)
	{
		g_strlcpy(scripts_path, main_argv[2], MAX_PATH);

		if (!is_file_readable(scripts_path))
		{
			g_strlcpy(scripts_path, SCRIPTS_PATH, MAX_PATH);
			g_strlcat(scripts_path, "/", MAX_PATH);
			g_strlcat(scripts_path, main_argv[2], MAX_PATH);

			if (!is_file_readable(scripts_path))
			{
				gchar *env = getenv(SCRIPTS_PATH_ENV);

				if (env != NULL)
				{
					g_strlcpy(scripts_path, env, MAX_PATH);
					g_strlcat(scripts_path, "/", MAX_PATH);
					g_strlcat(scripts_path, main_argv[2], MAX_PATH);

					if (!is_file_readable(scripts_path))
					{
						g_fprintf(stderr,
				"%s: %s: Scheme file does not exist or is not readable\n",
							argv[0], main_argv[2]);
						exit(1);
					}
				}
				else
				{
					g_fprintf(stderr,
				"%s: %s: Scheme file does not exist or is not readable\n",
						argv[0], main_argv[2]);
					exit(1);
				}
			}
		}

		main_argv[2] = scripts_path;
	}
	else
	{
		/* argv[2] can't be NULL as argv[3] may also be NULL */
		main_argv[2] = "";
	}

	gui_init();

	g_thread_unref(g_thread_new(NULL, callback_scm, main_argv));

	gui_main();

	return 0;				/* never gets here, but keeps compiler happy */
}

gpointer callback_scm(gpointer data)
{
	/* Start Guile environment.  Does not exit */
	char **main_argv = data;

	g_printf("%s\n", PKGINFO);
	g_printf("%s\n", COPYRIGHT);
	g_printf("GNU Robots comes with ABSOLUTELY NO WARRANTY\n");
	g_printf
		("This is free software, and you are welcome to redistribute it\n");
	g_printf
		("under certain conditions; see the file `COPYING' for details.\n");
	g_printf("Loading Guile ... Please wait\n\n");

	scm_boot_guile(3, main_argv, main_prog, NULL);

	return NULL; /* never gets here, but keeps compiler happy */
}

/************************************************************************
 * void main_prog(void *closure, gint argc, gchar *argv[])              *
 *                                                                      *
 * The main program code that is executed after Guile starts up. Pass   *
 * the Scheme program as argv[1] and the map file as argv[2]. The       *
 * program name is still argv[0].                                       *
 ************************************************************************/
void main_prog(void *closure, gint argc, gchar *argv[])
{
	gchar *map_file = argv[1];
	gchar *robot_program = argv[2];

	api_init();

	g_printf("Map file: %s\n", map_file);

	map = map_new_from_file(map_file, DEFAULT_MAP_ROWS,
		DEFAULT_MAP_COLUMNS);

	if (map == NULL)
	{
		exit (1);
	}

	/* ensure the robot is placed properly */
	MAP_SET_OBJECT(map, G_ROBOT_POSITION_Y(robot),
		G_ROBOT_POSITION_X(robot), ROBOT);

	/* Now initialize the rest of the Robot properties */
	g_object_set(G_OBJECT(robot),
		"user-interface", G_OBJECT(arena), "map", G_OBJECT(map), NULL);

	g_signal_connect(G_OBJECT(robot), "death", G_CALLBACK(death), NULL);

	if (strlen(robot_program) != 0)
	{
		/* execute a Scheme file */
		g_printf("Robot program: %s\n", robot_program);
		scm_c_primitive_load(robot_program);
	}
	else
	{
		g_printf
			("Robot program not specified. Entering interactive mode..\n");

		scm_internal_catch(SCM_BOOL_T,
		                   (scm_t_catch_body) scm_c_use_module,
		                   "ice-9 readline",
		                   catch_handler, NULL);
		scm_internal_catch(SCM_BOOL_T,
		                   (scm_t_catch_body) scm_c_eval_string,
		                   "(activate-readline)",
		                   catch_handler, NULL);
		scm_internal_catch(SCM_BOOL_T,
		                   (scm_t_catch_body) scm_c_use_module,
		                   "system repl repl",
		                   catch_handler, NULL);
		scm_internal_catch(SCM_BOOL_T,
		                   (scm_t_catch_body) scm_c_eval_string,
		                   "(start-repl)",
		                   catch_handler, NULL);
	}

	/* done */
	exit_nicely();
}

void gui_init()
{
	g_printf("Loading GTK Interface ... Please wait\n\n");

	gdk_threads_init();
	gdk_threads_enter();
	gtk_init(0, NULL);

	ui = UI_WINDOW(ui_window_new());
	arena = UI_ARENA(ui->priv->arena);
	gdk_threads_leave();
}

void gui_main()
{
	gdk_threads_enter();
	ui_window_postinit(ui, &map); /* waits for guile to load map... */

	/* draw the map */
	ui_arena_draw(arena);
	ui_arena_update_status(arena, "Welcome to GNU Robots", NULL,
	                       robot->energy, robot->score, robot->shields);

	gtk_main();
	gdk_threads_leave();
}

/************************************************************************
 * SCM catch_handler (void *data, SCM tag, SCM throw_args);             *
 *                                                                      *
 * Responsible for handling errors                                      *
 ************************************************************************/
SCM catch_handler(void *data, SCM tag, SCM throw_args)
{

  gchar *message = NULL;

  if (scm_ilength (throw_args) > 1
      && SCM_NFALSEP (scm_string_p (SCM_CADR (throw_args))))
  {
		message = scm_to_locale_string (scm_simple_format (SCM_BOOL_F,
		                                                   SCM_CADR (throw_args),
		                                                   SCM_CADDR (throw_args)));
  }

  else if (SCM_NFALSEP (scm_symbol_p (tag)))
  {
		message = scm_to_locale_string (scm_symbol_to_string (tag));
  }

/*
  user_interface_update_status (ui, message, -1, -1, -1);
*/
	if (message) {
		g_printf("Invalid Instruction: %s\n", message);
		free (message);
	} else {
		g_printf("Couldn't get error message\n");
	}

	return SCM_BOOL_F;
}

void death(GRobot *robot)
{
	/* We get a ref increment on a signal */
	g_object_unref(G_OBJECT(robot));

	exit_nicely();
}

/************************************************************************
 * void exit_nicely()                                                   *
 *                                                                      *
 * A function that allows the program to exit nicely, after freeing all *
 * memory pointers, etc.                                                *
 ************************************************************************/
void exit_nicely()
{
	glong score, energy, shields, shots, units;

	/* Show statistics */
	g_object_get(G_OBJECT(robot),
		"shields", &shields,
		"energy", &energy,
		"units", &units, "shots", &shots, "score", &score, NULL);

	g_printf(
		"\n-----------------------STATISTICS-----------------------\n");
	g_printf("Shields: %ld\n", (shields < 0 ? 0 : shields));
	g_printf("Energy: %ld\n", (energy < 0 ? 0 : energy));
	g_printf("Units walked: %ld\n", (units < 0 ? 0 : units));
	g_printf("Shots: %ld\n", (shots < 0 ? 0 : shots));
	g_printf("Score: %ld\n", score);

	/* Show results, if any */
	if (shields < 1)
	{
		g_printf("** Robot took too much damage, and died.\n");
	}

	else if (energy < 1)
	{
		g_printf("** Robot ran out of energy.\n");
	}

	g_thread_exit(0);
}

/************************************************************************
 * void usage(const gchar *argv0)                                       *
 *                                                                      *
 * A function that prints the usage of GNU Robots to the user. Assume   *
 * text mode for this function. We have not initialized X Windows yet.  *
 ************************************************************************/
void usage(const gchar *argv0)
{
	g_printf("%s\n", PKGINFO);
	g_printf("%s\n", COPYRIGHT);
	g_printf
	("Game/diversion where you construct a program for a little robot\n");
	g_printf
	("then set him loose and watch him explore a world on his own.\n\n");

	g_printf("Usage: %s [OPTION]... [FILE]\n\n", argv0);
	g_printf("  -f, --map-file=FILE    Load map file\n");
	g_printf("  -s, --shields=N        Set initial shields to N\n");
	g_printf("  -e, --energy=N         Set initial energy to N\n");
	g_printf
		("  -V, --version          Output version information and exit\n");
	g_printf("  -h, --help             Display this help and exit\n");
	g_printf("\nNote: FILE refers to a scheme file and %s enters into \n",
		argv0);
	g_printf("      interactive mode if it is not specified.\n");

	g_printf("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
}

/************************************************************************
 * gint is_file_readable (const gchar *filename)                        *
 *                                                                      *
 * Checks if a file is a readable file. We will use this function as    *
 * part of a sanity check, before we get anywhere near having to open   *
 * files. This will save on error checking later on, when we may have   *
 * already initialized another environment (Curses, X Windows, ...)     *
 ************************************************************************/
gint is_file_readable(const gchar *filename)
{
	FILE *stream;

	stream = fopen(filename, "r");
	if (stream == NULL)
	{
		/* Failed */
		return (0);
	}

	/* Success */
	fclose(stream);
	return (1);
}