From e0538867c4f5cffe959561ca0b91dfffbbe25903 Mon Sep 17 00:00:00 2001 From: ryo_saeba Date: Sun, 30 May 2010 10:11:58 +0000 Subject: [PATCH] Add 'npctoken' and 'setnpctoken' pre and post conditions, to keep data in NPC instead of player. git-svn-id: svn://svn.code.sf.net/p/crossfire/code/maps/trunk@13336 282e977c-c81d-0410-88c4-b93c2d0d6712 --- python/CFDialog.py | 47 +++++++++++++++++++++++++++++-- python/dialog/post/setnpctoken.py | 17 +++++++++++ python/dialog/post/settoken.py | 2 ++ python/dialog/pre/npctoken.py | 27 ++++++++++++++++++ python/dialog/pre/token.py | 2 ++ 5 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 python/dialog/post/setnpctoken.py create mode 100644 python/dialog/pre/npctoken.py diff --git a/python/CFDialog.py b/python/CFDialog.py index be70c85ae..b1e74e050 100644 --- a/python/CFDialog.py +++ b/python/CFDialog.py @@ -297,8 +297,8 @@ class Dialog: if os.path.isfile(path): try: exec(open(path).read()) - except: - Crossfire.Log(Crossfire.LogError, "CFDialog: Failed to set post-condition %s." % condition) + except Exception as error: + Crossfire.Log(Crossfire.LogError, "CFDialog: Failed to set post-condition %s:%s." %(condition, error)) else: Crossfire.Log(Crossfire.LogError, "CFDialog: Post Block called with unknown action %s." % action) @@ -344,3 +344,46 @@ class Dialog: finished = finished + ";" finished = finished + key + ":" + value self.__character.WriteKey("dialog_" + self.__location, finished, 1) + + # Search the NPC for a particular flag, and if it exists, return + # its value. Flag names are combined with the unique dialog "location" + # identifier and the player's name, and are therefore are not required + # to be unique. This also prevents flags from conflicting with other + # non-dialog-related contents in the NPC. + def getNPCStatus(self, key): + npc_status=self.__speaker.ReadKey("dialog_"+self.__location + "_" + self.__character.Name); + if npc_status == "": + return "0" + pairs=npc_status.split(";") + for i in pairs: + subpair=i.split(":") + if subpair[0] == key: + return subpair[1] + return "0" + + # Store a flag in the NPC and set it to the specified value. Flag + # names are combined with the unique dialog "location" identifier + # and the player's name, and are therefore are not required to be unique. + # This also prevents flags from conflicting with other non-dialog-related + # contents in the player file. + def setNPCStatus(self, key, value): + if value == "*": + return + ishere = 0 + finished = "" + npc_status = self.__speaker.ReadKey("dialog_"+self.__location + "_" + self.__character.Name); + if npc_status != "": + pairs = npc_status.split(";") + for i in pairs: + subpair = i.split(":") + if subpair[0] == key: + subpair[1] = value + ishere = 1 + if finished != "": + finished = finished+";" + finished = finished + subpair[0] + ":" + subpair[1] + if ishere == 0: + if finished != "": + finished = finished + ";" + finished = finished + key + ":" + value + self.__speaker.WriteKey("dialog_" + self.__location + "_" + self.__character.Name, finished, 1) diff --git a/python/dialog/post/setnpctoken.py b/python/dialog/post/setnpctoken.py new file mode 100644 index 000000000..dd73843eb --- /dev/null +++ b/python/dialog/post/setnpctoken.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# setnpctoken.py +# This is one of the files that can be called by an npc_dialog, +# The following code runs when a dialog has a post rule of 'settoken' +# The syntax is ["setlocaltoken", "tokenname", "valuetosetto"] +# this can then be checked by a token condition that looks for the +# value of the token +# The token is kept in the NPC's data, and will be lost if the +# map containing the NPC resets. +## DIALOGCHECK +## MINARGS 2 +## MAXARGS 2 +## .* +## .* +## ENDDIALOGCHECK + +self.setNPCStatus(args[0],args[1]) \ No newline at end of file diff --git a/python/dialog/post/settoken.py b/python/dialog/post/settoken.py index 3f366e7b2..419323d63 100644 --- a/python/dialog/post/settoken.py +++ b/python/dialog/post/settoken.py @@ -5,6 +5,8 @@ # The syntax is ["settoken", "tokenname", "valuetosetto"] # this can then be checked by a token condition that looks for the # value of the token +# The token will be kept with the player's data, and survive the reset +# of the map containing the NPC. ## DIALOGCHECK ## MINARGS 2 ## MAXARGS 2 diff --git a/python/dialog/pre/npctoken.py b/python/dialog/pre/npctoken.py new file mode 100644 index 000000000..39323340c --- /dev/null +++ b/python/dialog/pre/npctoken.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +#npctoken.py +# This is one of the files that can be called by an npc_dialog, +# The following code runs when a dialog has a pre rule of 'token' +# The syntax is +# ["token", "tokenname", "possiblevalue1", "possiblevalue2", etc] +# To deliver a True verdict, the token tokenname must be set to one of the +# 'possiblevalue' arguments. This will normally have been done +# with a previous use of settoken +# The token is kept in the NPC's data, and will be lost if the +# map containing the NPC resets. +## DIALOGCHECK +## MINARGS 2 +## MAXARGS 0 +## .* +## .* +## ENDDIALOGCHECK + +verdict = False +status = self.getNPCStatus(args[0]) +for value in args[1:]: + if (status == value) or (value == "*"): + verdict = True + break + else: + pass + diff --git a/python/dialog/pre/token.py b/python/dialog/pre/token.py index d57cb6689..c8332dc8d 100644 --- a/python/dialog/pre/token.py +++ b/python/dialog/pre/token.py @@ -7,6 +7,8 @@ # To deliver a True verdict, the token tokenname must be set to one of the # 'possiblevalue' arguments. This will normally have been done # with a previous use of settoken +# The token is be kept with the player's data, and survive the reset +# of the map containing the NPC. ## DIALOGCHECK ## MINARGS 2 ## MAXARGS 0