-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpythonspot.pyx
457 lines (358 loc) · 9.57 KB
/
pythonspot.pyx
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# pythonspot.pyx
# Python scripting module
#
#
# Lord of the Rings game engine
#
# Copyright (C) 2004 Michal Benes
#
# Lord of the Rings game engine is free software;
# you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This code 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this code; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Python scripting for The Lord of the Rings game engine
"""
import sys
# system libraries
cdef extern from "Python.h":
# embedding funcs
void Py_Initialize()
void Py_Finalize()
object PyImport_Import(object)
object PyObject_GetAttr(object, object)
cdef extern from "stdlib.h":
void exit(int)
cdef extern from "string.h":
char *strncpy(char*, char*, int)
# lotr includes
cdef extern from "cartoon.h":
void cartoon_play(char*)
cdef extern from "character.h":
ctypedef struct Character:
int direction
int action
int x
int y
int life
int end
char name[20]
int pythontexts
char texts[169]
int character_get_talk_len()
cdef extern from "combat.h":
void combat_proceed_frames()
void combat_start()
cdef extern from "gui.h":
void gui_message(char*, int)
void gui_question(char*)
void gui_proceed_frames()
cdef extern from "game.h":
void game_leader_teleport(int, int, int, int, int)
Character *game_get_leader()
cdef extern from "spot.h":
ctypedef struct CommandSpot:
int flag
Character *spot_character_get(int)
int spot_question_letter(char)
cdef extern from "map.h":
void map_add_pythonspot(int, int, int, int, int)
void map_character_teleport(Character*, int, int, int, int, int)
int map_character_move(int, int, int, int)
void map_attacking_character(int)
void map_add_character(Character*)
CommandSpot* map_get_pythonspot(int)
cdef extern from "utils.h":
char *lotr_data_directory()
pythonspots = []
cdef int pythonspots_num
cdef int if_result
cdef char *pythontexts[256]
def print_error(text, ex=None):
print "lotr: %s" % (text)
if ex != None:
print ex
exit(1)
#
#initialize the system
#
cdef public void pythonspot_init():
global spots, python_spots_num, pythonspots
sys.path.append(lotr_data_directory())
try:
spots = PyImport_Import("spots")
except Exception, e:
print_error("error loading pythonmodule spots.py or spot_definitons.py", e)
try:
pythonspots_num = 0
while 1:
spot = PyObject_GetAttr(spots, "spot_%d" % pythonspots_num)
map_add_pythonspot(spot.map, spot.x, spot.y, spot.w, spot.h)
pythonspots.append(spot)
pythonspots_num = pythonspots_num + 1
except Exception:
pass
for i in range(256):
pythontexts[i] = ""
#
# shut down the sytem
#
cdef public void pythonspot_close():
pass
#
# start python spot
#
cdef public void pythonspot_start(int id):
try:
pythonspots[id].start()
except Exception, e:
print_error("error running python spot id=%d" % id, e)
#
# continue the running python spot
# return if the spot still runs
#
cdef public int pythonspot_continue(int id):
try:
return pythonspots[id].cont()
except Exception, e:
print_error("error running python spot id=%d" % id, e)
return 0
#
# return if the spot has the specified action
#
cdef public int pythonspot_has_action(int id, int type, int param, int npc):
return pythonspots[id].has_action(type, param, npc)
#
# perform the specified action
#
cdef public void pythonspot_action(int id, int type, int param, int npc):
pythonspots[id].action(type, param, npc)
#
# sets result of an if command
#
cdef public void pythonspot_if_result(int result):
global if_result
if_result = result
#
# return pythontext
#
cdef public char *pythonspot_get_text(int id):
if id < 0 or id > 255: return ""
return pythontexts[id]
#
# initialize a map
#
cdef public void pythonspot_init_map (int id):
spots.init_map(id)
#
#
# Functions which can be called from the spots
#
###########################################################################
#
# You loose the game
# no return
#
def loose():
cartoon_play("cart10")
exit(0)
#
# return character action
#
def get_character_action(who):
cdef Character *character
character = spot_character_get(who)
return character.action
#
# set character action
# no return value
#
def set_character_action(who, value):
cdef Character *character
character = spot_character_get(who)
character.action = value
#
# return character position as tuple
#
def get_character_pos(who):
cdef Character *character
character = spot_character_get(who)
return (character.x, character.y)
#
# set character position
# no return value
#
def set_character_pos(who, x=None, y=None):
cdef Character *character
character = spot_character_get(who)
if x != None: character.x = x
if y != None: character.y = y
#
# show message
# no return value
#
def message(text):
gui_message(text, 0)
gui_proceed_frames()
#
# ask a Yes/No question
# returns 1 if the answer was Yes
# returns 0 otherwise
#
def question(text):
gui_question(text)
gui_proceed_frames()
return if_result
#
# teleport character
# no return value
#
def teleport(who, x, y, relative=0, dir=None, map=None):
cdef Character *character
if map == None: map = 0xff
if dir == None: dir = 0xff
if x < 0: x = x + 0xffff
if y < 0: y = y + 0xffff
if who != 0xf0:
character = spot_character_get(who)
# 0xf0 means whole party in the original data
if who == 0xf0 or character == game_get_leader():
game_leader_teleport(relative, x, y, dir, map)
elif character != NULL:
map_character_teleport(character, relative, x, y, dir, map)
#
# move character to position (x,y) and set final direction
# this function actually makes only one step
# test character.action==STAY to check if character is at the destination
# no return value
#
def move(who, int x, int y, direction=None):
cdef Character *character
cdef int dir
character = spot_character_get(who)
if direction == None:
dir = character.direction
else:
dir = direction
if dir < 0 or dir > 3:
print_error("Wrong direction dir=%d" % dir)
map_character_move(who, x, y, dir)
#
# set character as enemy
# no return value
#
def set_enemy(who):
map_attacking_character(who)
#
# start combat
# no return value
#
def combat():
combat_start()
combat_proceed_frames()
#
# play cartoon
# no return value
#
def cartoon(name):
cartoon_play(name)
#
# add character
# if relative is set then the positions are relative to leader
# no return value
#
def add_character(who, x, y, dir, relative=0):
cdef Character *character
cdef Character *leader
character = spot_character_get(who)
leader = game_get_leader()
if relative:
character.x = leader.x + x
character.y = leader.y + y
else:
character.x = x
character.y = y
character.direction = dir
map_add_character(character)
#
# enable python spot
# no return value
#
def pythonspot_enable(id):
cdef CommandSpot *spot
spot = map_get_pythonspot(id)
spot.flag = 1
#
# disable python spot
# no return value
#
def pythonspot_disable(id):
cdef CommandSpot *spot
spot = map_get_pythonspot(id)
spot.flag = 0
#
# set character name
# no return value
#
def set_character_name(who, name):
cdef Character *character
character = spot_character_get(who)
strncpy(character.name, name, 20)
character.name[20] = 0
#
# set pythonspot texts
# no return value
#
def set_text(id, text):
cdef int n
global pythontexts
n = id
if n < 0 or n > 255: return
pythontexts[n] = text
#
# set character texts
# questions parameter is list of tuples (question, inswer_id)
# questions must be in uppercase
# no return value
#
def set_character_texts(who, greeting=None, \
questions=None, default_answer=None):
cdef Character *character
cdef int pos
cdef int textslen
character = spot_character_get(who)
character.pythontexts = 1
if greeting != None:
character.texts[0] = greeting
textslen = character_get_talk_len()
pos = 1
if questions != None:
for i in range(len(questions)):
if pos > textslen - 20:
print "lotr: too many questions in spot script"
break
(question,text) = questions[i];
for j in range(min(10, len(question))):
if not spot_question_letter(ord(question[j])):
print "lotr: invalid question character '%s'" % question[j]
break
character.texts[pos] = ord(question[j])
pos = pos + 1
character.texts[pos] = text
pos = pos + 1
if default_answer != None:
character.texts[pos] = 0
pos = pos + 1
character.texts[pos] = default_answer
pos = pos + 1
character.texts[pos] = 0xff