Adedd some comments to schemeintegration.c
This commit is contained in:
parent
6b01917c11
commit
4696a1ec32
|
@ -3,6 +3,7 @@
|
||||||
#include <libguile.h>
|
#include <libguile.h>
|
||||||
#include "schemeintegration.h"
|
#include "schemeintegration.h"
|
||||||
|
|
||||||
|
// Create a player skill and add it to a given skill list from Scheme:
|
||||||
SCM scheme_create_skill(SCM string, SCM skilllist)
|
SCM scheme_create_skill(SCM string, SCM skilllist)
|
||||||
{
|
{
|
||||||
size_t skillNameLength = 0;
|
size_t skillNameLength = 0;
|
||||||
|
@ -12,33 +13,56 @@ SCM scheme_create_skill(SCM string, SCM skilllist)
|
||||||
return SCM_BOOL_T;
|
return SCM_BOOL_T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Message every currently connected player from Scheme:
|
||||||
SCM scheme_message_everyone(SCM sendername, SCM messagecontent, SCM outputqueue)
|
SCM scheme_message_everyone(SCM sendername, SCM messagecontent, SCM outputqueue)
|
||||||
{
|
{
|
||||||
|
// Allocate the memory for the needed data structures:
|
||||||
outputMessage * newOutputMessage = calloc(1, sizeof(userMessage));
|
outputMessage * newOutputMessage = calloc(1, sizeof(userMessage));
|
||||||
userMessage * newMessage = calloc(1, sizeof(userMessage));
|
userMessage * newMessage = calloc(1, sizeof(userMessage));
|
||||||
|
|
||||||
|
// Set some basic information for the output message, allowing it to be sent to everyone:
|
||||||
newOutputMessage->content = newMessage;
|
newOutputMessage->content = newMessage;
|
||||||
newOutputMessage->recipientsCount = 0;
|
newOutputMessage->recipientsCount = 0;
|
||||||
newOutputMessage->recipients = NULL;
|
newOutputMessage->recipients = NULL;
|
||||||
|
|
||||||
|
// Convert the Scheme strings to C strings, and ensure they're NULL terminated:
|
||||||
scm_to_locale_stringbuf(sendername, newMessage->senderName, 31);
|
scm_to_locale_stringbuf(sendername, newMessage->senderName, 31);
|
||||||
newMessage->senderName[31] = '\0';
|
newMessage->senderName[31] = '\0';
|
||||||
scm_to_locale_stringbuf(messagecontent, newMessage->messageContent, MAX - 1);
|
scm_to_locale_stringbuf(messagecontent, newMessage->messageContent, MAX - 1);
|
||||||
newMessage->messageContent[MAX - 1] = '\0';
|
newMessage->messageContent[MAX - 1] = '\0';
|
||||||
|
|
||||||
|
// Clean up the message contents to ensure they're safe to send and display correctly:
|
||||||
userNameSanatize(newMessage->senderName, 32);
|
userNameSanatize(newMessage->senderName, 32);
|
||||||
userInputSanatize(newMessage->messageContent, MAX);
|
userInputSanatize(newMessage->messageContent, MAX);
|
||||||
|
|
||||||
|
// Push it to the queue, where it will be handled and de-allocated:
|
||||||
pushQueue(scm_to_pointer(outputqueue), newOutputMessage, OUTPUT_MESSAGE);
|
pushQueue(scm_to_pointer(outputqueue), newOutputMessage, OUTPUT_MESSAGE);
|
||||||
|
|
||||||
|
// Return a Scheme #t value:
|
||||||
return SCM_BOOL_T;
|
return SCM_BOOL_T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The function ran by the Scheme thread which handles all game-master and interpreter interaction:
|
||||||
void * schemeHandler(void * parameters)
|
void * schemeHandler(void * parameters)
|
||||||
{
|
{
|
||||||
|
// Take in the needed values from the main thread and make it back into the struct:
|
||||||
SchemeThreadParameters * schemeThreadParameters = parameters;
|
SchemeThreadParameters * schemeThreadParameters = parameters;
|
||||||
|
|
||||||
|
// Initialize GNU Guile:
|
||||||
scm_init_guile();
|
scm_init_guile();
|
||||||
|
|
||||||
|
// Register the various functions:
|
||||||
scm_c_define_gsubr("create-skill", 2, 0, 0, &scheme_create_skill);
|
scm_c_define_gsubr("create-skill", 2, 0, 0, &scheme_create_skill);
|
||||||
scm_c_define_gsubr("message-everyone", 3, 0, 0, &scheme_message_everyone);
|
scm_c_define_gsubr("message-everyone", 3, 0, 0, &scheme_message_everyone);
|
||||||
|
|
||||||
|
// Define the various game state pointers as Scheme objects:
|
||||||
scm_c_define("skill-list", scm_from_pointer(schemeThreadParameters->skillList, NULL));
|
scm_c_define("skill-list", scm_from_pointer(schemeThreadParameters->skillList, NULL));
|
||||||
scm_c_define("output-queue", scm_from_pointer(schemeThreadParameters->outputQueue, NULL));
|
scm_c_define("output-queue", scm_from_pointer(schemeThreadParameters->outputQueue, NULL));
|
||||||
|
|
||||||
|
// Drop into the Scheme interpreter:
|
||||||
scm_shell(0, NULL);
|
scm_shell(0, NULL);
|
||||||
|
|
||||||
|
// Return NULL. This should be unreachable.
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue