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 parseJSONstr(s, relpath=''):
52  global location
53  params = cjson.decode(s)
54  parameters = []
55  if "location" in params:
56  location = params["location"]
57  for jsonRule in params["rules"]:
58  if "include" in jsonRule:
59  shouldinclude = 0
60  if "pre" in jsonRule:
61  incldialog = Dialog(player, npc, location)
62  inclrule = IncludeRule(jsonRule["pre"])
63  # There will only ever be one 'pre' block for an include
64  shouldinclude = incldialog.matchConditions(inclrule)
65  else:
66  shouldinclude =1
67  newfiles = jsonRule["include"]
68  if shouldinclude == 1:
69  # this isn't a 'real' rule, so we don't include it, but we do
70  # include the results of parsing it.
71  parameters.extend(parseJSON(newfiles, relpath))
72  else:
73  Crossfire.Log(Crossfire.LogDebug, "Ignoring NPC dialog from %s, conditions not met" % newfiles)
74  else:
75  parameters.append(jsonRule)
76  return parameters
77 
78 def parseJSON(filename, relpath):
79  for filenm in filename:
80  if filenm[0] == "/":
81  filepath = os.path.join(Crossfire.DataDirectory(), Crossfire.MapDirectory(), filenm[1:])
82  elif relpath != '':
83  filepath = os.path.join(relpath, filenm)
84  else:
85  filepath = os.path.join(Crossfire.DataDirectory(), Crossfire.MapDirectory(), filenm)
86 
87  try:
88  f = open(filepath,'rb')
89  except:
90  Crossfire.Log(Crossfire.LogDebug, "Error loading NPC dialog %s" % filepath)
91  raise
92  else:
93  Crossfire.Log(Crossfire.LogDebug, "Loading NPC dialog %s" % filepath)
94  params = parseJSONstr(f.read(), relpath=os.path.dirname(filepath))
95  f.close()
96  return params
97 
98 npc = Crossfire.WhoAmI()
99 #event = Crossfire.WhatIsEvent()
100 player = Crossfire.WhoIsActivator()
101 params = Crossfire.ScriptParameters()
102 if params != 'npc_dialog': # when script option is blank, it takes the arch name
103  # parse dialog from file path
104  dialogs = parseJSON([params], '')
105 else:
106  # parse dialog from message field
107  dialogs = parseJSONstr(npc.Message)
108 speech = Dialog(player, npc, location)
109 index = 0
110 
111 for jsonRule in dialogs:
112  rule = DialogRule(jsonRule["match"],
113  jsonRule.get("pre", []),
114  jsonRule["msg"],
115  jsonRule.get("post", []),
116  jsonRule.get("replies", None))
117  speech.addRule(rule, index)
118  index = index + 1
119 
120 if speech.speak(Crossfire.WhatIsMessage()) == 0:
121  # block the NPC for some time
122  Crossfire.WhoAmI().WriteKey('talked_to', str(random.randint(3, 8)), 1)
123  Crossfire.SetReturnValue(1)
npc_dialog.parseJSONstr
def parseJSONstr(s, relpath='')
Definition: npc_dialog.py:51
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:78
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