diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/connectionhandler.cpp | 7 | ||||
-rw-r--r-- | src/main.cpp | 24 | ||||
-rw-r--r-- | src/object.h | 77 | ||||
-rw-r--r-- | src/script-sq.cpp | 103 | ||||
-rw-r--r-- | src/script-sq.h | 47 | ||||
-rw-r--r-- | src/script.cpp | 4 | ||||
-rw-r--r-- | src/script.h | 47 |
7 files changed, 309 insertions, 0 deletions
diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp index 953f5c15..c3090bbe 100644 --- a/src/connectionhandler.cpp +++ b/src/connectionhandler.cpp @@ -24,6 +24,10 @@ #include "connectionhandler.h" #include "netsession.h" +#ifdef SCRIPT_SUPPORT +#include "script.h" +#endif + #define MAX_CLIENTS 1024 ConnectionHandler::ConnectionHandler() @@ -102,6 +106,9 @@ void ConnectionHandler::startListen(ListenThreadData *ltd) { buffer[result] = 0; printf("Received %s\n", buffer); +#ifdef SCRIPT_SUPPORT + script->message(buffer); +#endif } } diff --git a/src/main.cpp b/src/main.cpp index 19e95520..56e0d693 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,12 @@ #include <SDL.h> #include <SDL_net.h> +#ifdef SCRIPT_SUPPORT +#define SCRIPT_SQUIRREL +#include "script.h" +#include "script-sq.h" +#endif + #define TMW_WORLD_TICK SDL_USEREVENT #define SERVER_PORT 9601 @@ -70,6 +76,16 @@ void initialize() // Initialize world timer at 10 times per second worldTimerID = SDL_AddTimer(100, worldTick, NULL); + + // Initialize scripting subsystem +#ifdef SCRIPT_SUPPORT +#ifdef SCRIPT_SQUIRREL + script = new ScriptSquirrel(); +#else +#error Scripting enabled, but no language defined. +#endif + script->init(); +#endif } /** @@ -82,6 +98,11 @@ void deinitialize() // Quit SDL_net SDLNet_Quit(); + + // Destroy scripting subsystem +#ifdef SCRIPT_SUPPORT + delete script; +#endif } /** @@ -124,6 +145,9 @@ int main(int argc, char *argv[]) // - Handle all messages that are in the message queue // - Update all active objects/beings +#ifdef SCRIPT_SUPPORT + script->update(); +#endif } else if (event.type == SDL_QUIT) { diff --git a/src/object.h b/src/object.h new file mode 100644 index 00000000..1135ef9a --- /dev/null +++ b/src/object.h @@ -0,0 +1,77 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 + * any later version. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include <iostream> + +/* + * Generic In-Game Object Definition + */ +class Object +{ + int x; + int y; + public: + ~Object() { } + virtual void update() = 0; +}; + +/* + * Generic Living Object + */ +class LivingObject : public Object +{ + //Object name + std::string name; + + //Object level + unsigned int level; + + //Object money + unsigned int money; + + //Object statistics + unsigned int strength; + unsigned int agility; + unsigned int vitality; + unsigned int intelligence; + unsigned int dexterity; + unsigned int luck; + + //Object inventory/equiped items + //Inventory inventory; + //Equipment equipment; + + public: + ~LivingObject() { }; + void update() { }; +}; + +/* + * Player object + */ +class Player : public LivingObject +{ + //Player gender (male = true, female = false) + bool gender; + public: +}; diff --git a/src/script-sq.cpp b/src/script-sq.cpp new file mode 100644 index 00000000..fc02616e --- /dev/null +++ b/src/script-sq.cpp @@ -0,0 +1,103 @@ +#include "script-sq.h" +#include <cstring> + +/* + * printfunc - Print function for Squirrel + */ +void printfunc(HSQUIRRELVM v, const SQChar *s, ...) +{ + va_list arglist; + va_start(arglist, s); + vprintf(s, arglist); + va_end(arglist); +} + +/* + * functionCall - Call function with arguments + * fn - name of function + * args - string with argument types. + * 's' = String + * 'i' = Integer + * 'f' = Float + */ +void functionCall(HSQUIRRELVM v, char *fn, char *args, ...) +{ + int argCount = 0; + va_list arglist; + va_start(arglist, args); + + int top = sq_gettop(v); //save stack + sq_pushroottable(v); //pushes global table + sq_pushstring(v, _SC(fn), -1); + if (SQ_SUCCEEDED(sq_get(v, -2))) + { + sq_pushroottable(v); //push 'this' + + if (args != NULL) + { + for (int i = 0; i < strlen(args); i++) + { + switch (args[i]) + { + case 'S': + case 's': + //string + argCount++; + sq_pushstring(v, va_arg(arglist, char*), -1); + break; + case 'I': + case 'i': + //integer + argCount++; + sq_pushinteger(v, va_arg(arglist, int)); + break; + case 'F': + case 'f': + //float + argCount++; + sq_pushfloat(v, va_arg(arglist, float)); + break; + } + } + } + + sq_call(v, argCount + 1, 0); + } + sq_settop(v, top); + + va_end(arglist); +} + +ScriptSquirrel::~ScriptSquirrel() +{ + sq_pop(vm, 1); + sq_close(vm); +} + +void ScriptSquirrel::init() +{ + vm = sq_open(2048); + sqstd_seterrorhandlers(vm); + sq_setprintfunc(vm, printfunc); + + sq_pushroottable(vm); + sqstd_dofile(vm, _SC("test.nut"), 0, 1); + if (SQ_SUCCEEDED(sqstd_dofile(vm, _SC("test.nut"), 0, 1))) + functionCall(vm, "init", NULL); +} + +void ScriptSquirrel::destroy() +{ + functionCall(vm, "destroy", NULL); +} + +void ScriptSquirrel::update() +{ + functionCall(vm, "update", NULL); +} + +void ScriptSquirrel::message(char *msg) +{ + functionCall(vm, "message", "s", msg); +} + diff --git a/src/script-sq.h b/src/script-sq.h new file mode 100644 index 00000000..688c6230 --- /dev/null +++ b/src/script-sq.h @@ -0,0 +1,47 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 + * any later version. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef SCRIPT_SQ_H +#define SCRIPT_SQ_H + +#include "script.h" +#include <cstdio> +#include <cstdlib> +#include <cstdarg> +#include <squirrel.h> +#include <sqstdio.h> +#include <sqstdaux.h> + +class ScriptSquirrel : public ScriptingInterface +{ + HSQUIRRELVM vm; + + public: + ~ScriptSquirrel(); + void init(); + void destroy(); + void update(); + void message(char *); +}; + +#endif diff --git a/src/script.cpp b/src/script.cpp new file mode 100644 index 00000000..0f51d6cc --- /dev/null +++ b/src/script.cpp @@ -0,0 +1,4 @@ +#include "script.h" + +ScriptingInterface *script; + diff --git a/src/script.h b/src/script.h new file mode 100644 index 00000000..32f675a0 --- /dev/null +++ b/src/script.h @@ -0,0 +1,47 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World 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 + * any later version. + * + * The Mana World 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 The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef SCRIPT_H +#define SCRIPT_H + +/* + * ScriptingInterface provides a simple class which is a simple interface + * for defining a scripting backend. + */ +class ScriptingInterface +{ + public: + virtual ~ScriptingInterface() { }; + //Initialization + virtual void init() = 0; + //Destruction + virtual void destroy() = 0; + //State update + virtual void update() = 0; + //Script sent raw message + virtual void message(char *) = 0; +}; + +extern ScriptingInterface *script; + +#endif |