Crossfire Server, Trunk
npc_dialog.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # npc_dialog.py - Dialog helper class
3 #
4 # Copyright (C) 2007 David Delbecq
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #
20 #
21 # This is a simple script that makes use of CFNPCDialog.py and that receives
22 # parameters from a JSON inside the event message.
23 #
24 # An example of a map file entry is:
25 #
26 # arch guildmaster
27 # name Sigmund
28 # msg
29 #
30 # endmsg
31 # x 11
32 # y 7
33 # arch event_say
34 # name start/sigmund.msg
35 # title Python
36 # slaying /python/dialog/npc_dialog.py
37 # end
38 # end
39 #
40 # see http://wiki.metalforge.net/doku.php/user:cavesomething:guide_to_quest_dialogs
41 # for lots of detail on how to use this, and look at examples in test/quest_handling
42 
43 import Crossfire
44 import os
45 from CFDialog import DialogRule, Dialog, IncludeRule
46 import cjson
47 import random
48 
49 location = "defaultdialognamespace"
50 
51 def parseJSON(filename, relpath):
52  global location
53  parameters = []
54  for filenm in filename:
55  if filenm[0] == "/":
56  filepath = os.path.join(Crossfire.DataDirectory(),
57  Crossfire.MapDirectory(), filenm[1:])
58  elif relpath != '':
59  filepath = os.path.join(relpath, filenm)
60  else:
61  filepath = os.path.join(Crossfire.DataDirectory(),
62  Crossfire.MapDirectory(), relpath, filenm)
63  try:
64  f = open(filepath,'rb')
65  except:
66  Crossfire.Log(Crossfire.LogDebug, "Error loading NPC dialog %s" % filepath)
67  raise
68  else:
69  Crossfire.Log(Crossfire.LogDebug, "Loading NPC dialog %s" % filepath)
70  params = cjson.decode(f.read())
71  f.close()
72  if "location" in params:
73  location = params["location"]
74  for jsonRule in params["rules"]:
75  if "include" in jsonRule:
76  shouldinclude = 0
77  if "pre" in jsonRule:
78  incldialog = Dialog(player, npc, location)
79  inclrule = IncludeRule(jsonRule["pre"])
80  # There will only ever be one 'pre' block for an include
81  shouldinclude = incldialog.matchConditions(inclrule)
82  else:
83  shouldinclude =1
84  newfiles = jsonRule["include"]
85  if shouldinclude == 1:
86  # this isn't a 'real' rule, so we don't include it, but we do
87  # include the results of parsing it.
88  parameters.extend(parseJSON(newfiles, os.path.dirname(filepath)))
89  else:
90  Crossfire.Log(Crossfire.LogDebug, "Ignoring NPC dialog from %s, conditions not met" % newfiles)
91  else:
92  parameters.append(jsonRule)
93  return parameters
94 
95 npc = Crossfire.WhoAmI()
96 #event = Crossfire.WhatIsEvent()
97 player = Crossfire.WhoIsActivator()
98 if (Crossfire.ScriptParameters() != None):
99  filename = Crossfire.ScriptParameters()
100  dialogs = parseJSON([filename], '')
101 speech = Dialog(player, npc, location)
102 index = 0
103 
104 for jsonRule in dialogs:
105  replies = None
106  if 'replies' in jsonRule:
107  replies = jsonRule['replies']
108  rule = DialogRule(jsonRule["match"],
109  jsonRule["pre"],
110  jsonRule["msg"],
111  jsonRule["post"],
112  replies)
113  speech.addRule(rule, index)
114  index = index + 1
115 
116 if speech.speak(Crossfire.WhatIsMessage()) == 0:
117  # block the NPC for some time
118  Crossfire.WhoAmI().WriteKey('talked_to', str(random.randint(3, 8)), 1)
119  Crossfire.SetReturnValue(1)
CFBank.open
def open()
Definition: CFBank.py:69
CFDialog.IncludeRule
Definition: CFDialog.py:179
npc_dialog.parseJSON
def parseJSON(filename, relpath)
Definition: npc_dialog.py:51
make_face_from_files.str
str
Definition: make_face_from_files.py:30
CFDialog.DialogRule
Definition: CFDialog.py:128
CFDialog.Dialog
Definition: CFDialog.py:183