summaryrefslogtreecommitdiff
path: root/src/api.c
blob: bd8a566b5623077d19cc4ce61cb93cb979e3cfa3 (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
/* Copyright (C) 1998 Jim Hall <jhall1@isd.net>
 * Copyright (C) 2008 Bradley Smith <brad@brad-smith.co.uk>
 *
 * Robot API for the GNU Robots game
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>             /* for abs, free */

#include <glib.h>
#include <libguile.h>           /* GNU Guile high */

#include "api.h"                /* GNU Robots API */
#include "main.h"               /* for exit_nicely */
#include "grobot.h"

extern GRobot *robot;

/* Functions */

SCM
api_robot_turn (SCM s_n)
{
  g_robot_turn (robot, scm_num2int (s_n, 0, NULL));

  return SCM_BOOL (TRUE);
}

SCM
api_robot_move (SCM s_n)
{
  return SCM_BOOL (g_robot_move (robot, scm_num2int (s_n, 0, NULL)));
}

SCM
api_robot_smell (SCM s_th)
{
  gboolean ret;
  gchar *str;

  str = scm_to_locale_string (s_th);
  ret = g_robot_smell (robot, str);

  return SCM_BOOL (ret);
}

SCM
api_robot_feel (SCM s_th)
{
  gboolean ret;
  gchar *str;

  str = scm_to_locale_string (s_th);
  ret = g_robot_feel (robot, str);

  return SCM_BOOL (ret);
}

SCM
api_robot_look (SCM s_th)
{
  gboolean ret;
  gchar *str;

  str = scm_to_locale_string (s_th);
  ret = g_robot_look (robot, str);

  return SCM_BOOL (ret);
}

SCM
api_robot_grab (void)
{
  return SCM_BOOL (g_robot_grab (robot));
}

SCM
api_robot_zap (void)
{
  return SCM_BOOL (g_robot_zap (robot));
}

SCM
api_robot_stop (void)
{
  return SCM_BOOL (g_robot_stop (robot));
}

SCM
api_robot_get_shields (void)
{
  glong shields;

  g_object_get (robot, "shields", &shields, NULL);

  /* Returns the robot shields */
  return (scm_long2num (shields));
}

SCM
api_robot_get_energy (void)
{
  glong energy;

  g_object_get (robot, "energy", &energy, NULL);

  /* Returns the robot energy */
  return (scm_long2num (energy));
}

SCM
api_robot_get_score (void)
{
  glong score;

  g_object_get (robot, "score", &score, NULL);

  /* Returns the robot score */
  return (scm_long2num (score));
}

void
api_init (void)
{
  /* define some new builtins (hooks) so that they are available in
     Scheme. */

  scm_c_define_gsubr ("robot-turn", 1, 0, 0, api_robot_turn);
  scm_c_define_gsubr ("robot-move", 1, 0, 0, api_robot_move);
  scm_c_define_gsubr ("robot-smell", 1, 0, 0, api_robot_smell);
  scm_c_define_gsubr ("robot-feel", 1, 0, 0, api_robot_feel);
  scm_c_define_gsubr ("robot-look", 1, 0, 0, api_robot_look);
  scm_c_define_gsubr ("robot-grab", 0, 0, 0, api_robot_grab);
  scm_c_define_gsubr ("robot-zap", 0, 0, 0, api_robot_zap);

  scm_c_define_gsubr ("robot-get-shields", 0, 0, 0, api_robot_get_shields);
  scm_c_define_gsubr ("robot-get-energy", 0, 0, 0, api_robot_get_energy);
  scm_c_define_gsubr ("robot-get-score", 0, 0, 0, api_robot_get_score);

  scm_c_define_gsubr ("stop", 0, 0, 0, api_robot_stop);
  scm_c_define_gsubr ("quit", 0, 0, 0, api_robot_stop);
}