| 1 |
# |
| 2 |
# $Id$ |
| 3 |
# |
| 4 |
# (C) Copyright 2002 Kai Sterker <[email protected]> |
| 5 |
# Part of the Adonthell Project http://adonthell.linuxgames.com |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License. |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY. |
| 11 |
# |
| 12 |
# See the COPYING file for more details |
| 13 |
# |
| 14 |
|
| 15 |
import sys |
| 16 |
|
| 17 |
# -- Base class for all dialogue scripts |
| 18 |
class base(object): |
| 19 |
|
| 20 |
# -- Execute one iteration of the dialogue |
| 21 |
# This consists of the following steps: |
| 22 |
# |
| 23 |
# 1) Run the code of the selected line of text |
| 24 |
# 2) Append all followers whose condition is true to the speech |
| 25 |
# to be used for the next selection. Also append the speaker |
| 26 |
# associated with each line of text. |
| 27 |
# |
| 28 |
# All data for these actions is retrieved from the derived class. |
| 29 |
def run (self, index): |
| 30 |
self.speech = [] |
| 31 |
self.speaker = [] |
| 32 |
result = 0 |
| 33 |
|
| 34 |
# -- get the code to execute and the list of following |
| 35 |
# dialogue options |
| 36 |
code, followers = self.dlg[index][1:] |
| 37 |
|
| 38 |
# -- if there is code, try to execute it |
| 39 |
if code != -1: |
| 40 |
self.execute (self.code[code], 1) |
| 41 |
|
| 42 |
# -- for each following option: |
| 43 |
for (speech, operation, cond) in followers: |
| 44 |
|
| 45 |
# -- if we're in an elif or else part of a condition |
| 46 |
# and a former condition was true already, continue |
| 47 |
if operation == 1 and result == 1: continue |
| 48 |
|
| 49 |
# -- try to ececute the condition, if there is one |
| 50 |
if cond != -1: |
| 51 |
result = self.execute (self.cond[cond], 0) |
| 52 |
else: |
| 53 |
result = 1 |
| 54 |
|
| 55 |
# -- only append the option if the condition was met |
| 56 |
if result == 1: |
| 57 |
speaker = self.dlg[speech][0] |
| 58 |
if speaker == "Default": speaker = self.the_npc.get_name () |
| 59 |
self.speaker.append (speaker) |
| 60 |
self.speech.append (speech) |
| 61 |
|
| 62 |
# -- finally, return |
| 63 |
return |
| 64 |
|
| 65 |
|
| 66 |
# -- Execute some arbitrary Python code |
| 67 |
def execute (self, statement, multiline): |
| 68 |
try: |
| 69 |
# -- this is for multiline code without retval |
| 70 |
if multiline == 1: |
| 71 |
code = compile (statement, "", "exec") |
| 72 |
return eval (code, self.namespace, locals ()) |
| 73 |
# -- this is for single line statements with retval |
| 74 |
else: |
| 75 |
return eval (statement, self.namespace, locals ()) |
| 76 |
except: |
| 77 |
# -- in case of an error, print what happened and continue |
| 78 |
err_type, value = sys.exc_info ()[:2] |
| 79 |
print ("Error in statement\n %s %s:\n '%s'" \ |
| 80 |
% (statement, str (err_type), str (value))) |
| 81 |
|
| 82 |
return 0 |
| 83 |
|
| 84 |
|
| 85 |
# -- Decide whether the dialogue engine should return control to the |
| 86 |
# player (1) or not (0) |
| 87 |
def stop (self, index): |
| 88 |
followers = self.dlg[index][2] |
| 89 |
|
| 90 |
if len (followers) > 0 and self.dlg[followers[0][0]][0] == None: return 0 |
| 91 |
else: return 1 |
| 92 |
|
| 93 |
|
| 94 |
# -- Allow usage of undeclared variables |
| 95 |
def __getattr__ (self, name): |
| 96 |
return 0 |
| 97 |
|