summaryrefslogtreecommitdiff
path: root/src/script-squirrel.cpp
blob: 749d5b2887cdcbbe383506e162cb92c58db41453 (plain) (blame)
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
#include "script-squirrel.h"
#include <cstring>

#ifdef SCRIPT_SUPPORT

void registerStdLib(HSQUIRRELVM);

/*
 * 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
 */
bool functionCall(HSQUIRRELVM v, const char *fn, const 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);
    } else
	return false;
    sq_settop(v, top);

    va_end(arglist);
    return true;
}

/*
 * functionRegister
 * Registers a function in Squirrel VM
 */
void functionRegister(HSQUIRRELVM v, SQFUNCTION f, const char *name)
{
    sq_pushroottable(v);
    sq_pushstring(v, name, -1);
    sq_newclosure(v, f, 0);
    sq_createslot(v, -3);
    sq_pop(v, 1);
}

/*
 * Test function called from Squirrel (modified form Squirrel docs)
 * Prints the type of all arguments.
 */
int testFunc(HSQUIRRELVM v)
{
    int nargs = sq_gettop(v);

    for (int n = 1; n <= nargs; n++) {
	printf("arg: %d is ", n);
	switch (sq_gettype(v, n))
	{
	case OT_NULL:
	    printf("null");
	    break;
	case OT_INTEGER:
	    printf("integer");
	    break;
	case OT_FLOAT:
	    printf("float");
	    break;
	case OT_STRING:
	    printf("string");
	    break;
	case OT_TABLE:
	    printf("table");
	    break;
	case OT_ARRAY:
	    printf("array");
	    break;
	case OT_USERDATA:
	    printf("userdata");
	    break;
	case OT_CLOSURE:
	    printf("closure");
	    break;
	case OT_NATIVECLOSURE:
	    printf("nativeclosure");
	    break;
	case OT_GENERATOR:
	    printf("generator");
	    break;
	case OT_USERPOINTER:
	    printf("userpointer");
	    break;
	default:
	    printf("unknown");
	}
    }
    printf("\n");
    sq_pushinteger(v, nargs);
    return 1;
}

/******/

ScriptSquirrel::ScriptSquirrel(const std::string &file) :
    Script(file)
{
    vm = sq_open(1024);  //1024 byte stack
    sqstd_seterrorhandlers(vm);
    sq_setprintfunc(vm, printfunc);

    sq_pushroottable(vm);
    if (!SQ_SUCCEEDED(sqstd_dofile(vm, _SC(scriptName.c_str()), 0, 1))) {
	std::cerr << "Error: ScriptSquirrel: could not execute " <<
	    scriptName << std::endl;
    } else {
        registerStdLib(vm);

	functionCall(vm, "", NULL);
	functionCall(vm, "init", NULL);
    }
}

ScriptSquirrel::~ScriptSquirrel()
{
    functionCall(vm, "destroy", NULL);

    sq_pop(vm, 1);
    sq_close(vm);
}

void ScriptSquirrel::update()
{
    functionCall(vm, "update", NULL);
}

bool ScriptSquirrel::execute(const std::string &functionName)
{
    return functionCall(vm, functionName.c_str(), NULL);
}

void ScriptSquirrel::message(char *msg)
{
    functionCall(vm, "message", "s", msg);
}

/******************************/
/*
 * Standard Library Functions
 */
int getName(HSQUIRRELVM v)
{
    sq_pushstring(v, "no name", -1);
    return 1;
}

int getX(HSQUIRRELVM v)
{
    sq_pushinteger(v, 0);
    return 1;
}

int getY(HSQUIRRELVM v)
{
    sq_pushinteger(v, 0);
    return 1;
}

int getMap(HSQUIRRELVM v)
{
    sq_pushstring(v, "map1.map", -1);
    return 1;
}

int getLevel(HSQUIRRELVM v)
{
    sq_pushinteger(v, 0);
    return 1;
}

int getHealth(HSQUIRRELVM v)
{
    sq_pushinteger(v, 0);
    return 1;
}

int getMaxHealth(HSQUIRRELVM v)
{
    sq_pushinteger(v, 0);
    return 1;
}

int getAttack(HSQUIRRELVM v)
{
    sq_pushinteger(v, 0);
    return 1;
}

int getDefense(HSQUIRRELVM v)
{
    sq_pushinteger(v, 0);
    return 1;
}

int getLuck(HSQUIRRELVM v)
{
    sq_pushinteger(v, 0);
    return 1;
}

int getVitality(HSQUIRRELVM v)
{
    sq_pushinteger(v, 0);
    return 1;
}

/*
 *  Register standard functions for game script
 */
void registerStdLib(HSQUIRRELVM v)
{
    functionRegister(v, getName, "getName");
    functionRegister(v, getX, "getX");
    functionRegister(v, getY, "getY");
    functionRegister(v, getMap, "getMap");
    functionRegister(v, getLevel, "getLevel");
    functionRegister(v, getHealth, "getHealth");
    functionRegister(v, getMaxHealth, "getMaxHealth");
    functionRegister(v, getAttack, "getAttack");
    functionRegister(v, getDefense, "getDefense");
    functionRegister(v, getLuck, "getLuck");
    functionRegister(v, getVitality, "getVitality");
}

#endif