2012-03-23 15:45:38 +00:00
|
|
|
/* wMUD - Yet another MUD codebase by W00d5t0ck
|
|
|
|
* Copyright (C) 2012 - Gergely POLONKAI
|
|
|
|
*
|
|
|
|
* interpreter.c: game command interpreter routines
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-03-13 01:22:22 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <gio/gio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-03-13 16:24:03 +00:00
|
|
|
#include "interpreter.h"
|
2012-03-24 23:13:37 +00:00
|
|
|
#include "game-networking.h"
|
2012-03-14 19:19:05 +00:00
|
|
|
#include "main.h"
|
2012-03-13 01:22:22 +00:00
|
|
|
|
2012-03-24 22:52:36 +00:00
|
|
|
/**
|
|
|
|
* SECTION:interpreter
|
|
|
|
* @short_description: Game command interpreter
|
|
|
|
*
|
|
|
|
* Functions to interpret and execute in-game commands
|
|
|
|
*/
|
|
|
|
|
2012-03-24 19:37:37 +00:00
|
|
|
WMUD_COMMAND(quit);
|
2012-03-14 19:06:02 +00:00
|
|
|
|
2012-03-13 16:24:03 +00:00
|
|
|
static wmudCommand command_list[] = {
|
|
|
|
{ "quit", gcmd_quit },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
2012-03-24 19:34:36 +00:00
|
|
|
/**
|
|
|
|
* destroy_string:
|
|
|
|
* @string: a GString to destroy
|
|
|
|
*
|
|
|
|
* Callback function to destroy a list of GStrings
|
|
|
|
*/
|
2012-03-13 01:22:22 +00:00
|
|
|
static void
|
2012-03-14 19:06:02 +00:00
|
|
|
destroy_string(GString *string)
|
2012-03-13 01:22:22 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
g_string_free(string, TRUE);
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 19:34:36 +00:00
|
|
|
/**
|
|
|
|
* wmud_interpret_game_command:
|
|
|
|
* @client: the wmudClient whose command should be processed
|
|
|
|
*
|
|
|
|
* Processes a wmudClient's buffer, and executes the game command if there is
|
|
|
|
* one
|
|
|
|
*/
|
2012-03-13 01:22:22 +00:00
|
|
|
void
|
|
|
|
wmud_interpret_game_command(wmudClient *client)
|
|
|
|
{
|
|
|
|
GSList *command_parts = NULL;
|
|
|
|
gchar *a,
|
2012-03-14 19:06:02 +00:00
|
|
|
*start,
|
|
|
|
*end;
|
|
|
|
gchar str_delim = 0;
|
|
|
|
wmudCommand *cmd;
|
|
|
|
int command_parts_count = 0,
|
|
|
|
match_count = 0;
|
|
|
|
GSList *matches = NULL;
|
2012-03-13 01:22:22 +00:00
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
if (strchr(client->buffer->str, '\r') || strchr(client->buffer->str, '\n'))
|
2012-03-13 01:22:22 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
/* TODO: We should NEVER reach this point! */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = client->buffer->str;
|
2012-03-13 01:22:22 +00:00
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
GString *token;
|
|
|
|
|
|
|
|
while (*a)
|
|
|
|
{
|
|
|
|
for (start = a; *start; start++)
|
2012-03-13 16:24:03 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
if (!str_delim)
|
|
|
|
{
|
|
|
|
if ((*start == '"') || (*start == '\''))
|
|
|
|
{
|
|
|
|
str_delim = *start;
|
|
|
|
start++;
|
|
|
|
break;
|
|
|
|
}
|
2012-03-24 19:37:37 +00:00
|
|
|
else if (g_ascii_isspace(*start) || (!*start))
|
2012-03-14 19:06:02 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
for (end = start; *end; end++)
|
2012-03-13 01:22:22 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
if (!str_delim && strchr("'\" \t", *end))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (str_delim && (*end == str_delim))
|
|
|
|
{
|
|
|
|
str_delim = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!*end)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
if (*start)
|
2012-03-13 16:24:03 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
token = g_string_new_len(start, end - start);
|
|
|
|
command_parts = g_slist_prepend(command_parts, token);
|
|
|
|
command_parts_count++;
|
2012-03-13 16:24:03 +00:00
|
|
|
}
|
2012-03-13 01:22:22 +00:00
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
a = end;
|
|
|
|
if (((*a == '"') || (*a == '\'')) && str_delim)
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str_delim)
|
|
|
|
{
|
|
|
|
wmud_client_send(client, "You should close quotes of any kind, like %c, shouldn't you?\r\n", str_delim);
|
|
|
|
#if GLIB_CHECK_VERSION(2, 28, 0)
|
2012-03-14 19:19:05 +00:00
|
|
|
g_slist_free_full(command_parts, (GDestroyNotify)destroy_string);
|
2012-03-14 19:06:02 +00:00
|
|
|
#else
|
|
|
|
g_slist_foreach(command_parts, (GFunc)destroy_string, NULL);
|
|
|
|
g_slist_free(command_parts);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command_parts_count == 0)
|
|
|
|
{
|
|
|
|
/* TODO: handle empty command */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_parts = g_slist_reverse(command_parts);
|
|
|
|
|
|
|
|
for (cmd = command_list; cmd->command; cmd++)
|
|
|
|
{
|
|
|
|
GString *input = (GString *)(command_parts->data);
|
|
|
|
gint cmp;
|
|
|
|
|
|
|
|
if (((cmp = g_ascii_strncasecmp(input->str, cmd->command, input->len)) == 0) && !cmd->command[input->len])
|
|
|
|
{
|
|
|
|
g_slist_free(matches);
|
|
|
|
match_count = 1;
|
|
|
|
matches = NULL;
|
|
|
|
matches = g_slist_prepend(matches, cmd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (cmp == 0)
|
2012-03-13 01:22:22 +00:00
|
|
|
{
|
2012-03-14 19:06:02 +00:00
|
|
|
matches = g_slist_prepend(matches, cmd);
|
|
|
|
match_count++;
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-14 19:19:05 +00:00
|
|
|
switch (match_count)
|
2012-03-14 19:06:02 +00:00
|
|
|
{
|
2012-03-14 19:19:05 +00:00
|
|
|
case 0:
|
|
|
|
switch (random_number(1, 3))
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
wmud_client_send(client, "Huh?\r\n");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
wmud_client_send(client, "What?\r\n");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
wmud_client_send(client, "I can hardly understand you...\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
((wmudCommand *)(matches->data))->commandFunc(client, ((GString *)(command_parts->data))->str, command_parts->next);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
wmud_client_send(client, "This command could mean several things, please try a more exact form!\r\n");
|
2012-03-13 16:24:03 +00:00
|
|
|
}
|
|
|
|
|
2012-03-14 19:06:02 +00:00
|
|
|
g_slist_free(matches);
|
2012-03-13 01:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 19:34:36 +00:00
|
|
|
/**
|
|
|
|
* gcmd_quit:
|
|
|
|
*
|
|
|
|
* The QUIT game command's handler
|
|
|
|
*/
|
|
|
|
WMUD_COMMAND(quit)
|
2012-03-14 19:06:02 +00:00
|
|
|
{
|
|
|
|
wmud_client_send(client, "Are you sure you want to get back to that freaky other reality? [y/N] ");
|
2012-03-21 15:07:02 +00:00
|
|
|
client->state = WMUD_CLIENT_STATE_QUITWAIT;
|
2012-03-14 19:06:02 +00:00
|
|
|
}
|
|
|
|
|