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
master
ryo_saeba 2010-05-30 10:11:58 +00:00
parent 33401e83d1
commit e0538867c4
5 changed files with 93 additions and 2 deletions

View File

@ -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)

View File

@ -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])

View File

@ -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

View File

@ -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

View File

@ -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