Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
fixer_lib.py
Go to the documentation of this file.
1 import Base
2 import Director
3 #import code #Doesn't seem to be needed...
4 import VS
5 from quest import checkSaveValue
6 global b
7 b=1
8 # To be merged with fixers.py???
9 #
10 # How to di it all:
11 # + first add the fixer to the (unique) queue, this should be the string to be
12 # executed to create the \'Fixer\' object.
13 # + when getting fixers, first get campaign fixers, then add quest fixers...
14 # print (or throw?)error if too many
15 # + each node should have seperate preconditions, each 'ref' value in iterateConversations
16 # will then be a hash seperated string of various possible nodes, with preconditions evaluated
17 # linearly until the last...which is accepted without preconditions evaluated.
18 # + The 'Root' node is in fact just the preconditions for displaying the fixer and, where
19 # the text would be, just the reference list of possible starting points for the conversation.
20 # The rest of the data in this string is ignored.
21 # Library to help in the creation of fixers
22 
23 #from fixers import Fixer
24 
25 
26 def getNewInfo(conversation, ref="Root"):
27  """Returns two strings, the first is the text for the node, the second is
28  the choices."""
29  text, choices, sprite, motext = getAllNewInfo(conversation, ref)
30  return text, choices
31 
32 def getNode(conversation, ref="Root"):
33  """Gets the node in the conversation corresponding to \'ref\'"""
34  for node in conversation:
35  if node.split('^')[0].split('|')[0] == ref:
36  return node
37  raise RuntimeError("Node with reference \'%s\' not found in conversation."%ref)
38 
39 def getPreconditions(conversation):
40  """Gets the preconditions for the conversation."""
41  pre = getNode(conversation).split('^')[0].split('|')
42  if len(pre) < 4:
43  return str()
44  else:
45  return pre[3]
46 
47 def getAllNewInfo(conversation, ref="Root"):
48  """Retrieves the suitable text and choices, as well as the sprite and
49  mouseover text."""
50  segs = getNode(conversation, ref).split('^')[1:]
51  default = segs.pop()
52  use = False
53  for seg in segs:
54  sseg = seg.split('|')
55  if evaluateConditions(sseg[0]):
56  use = sseg
57  break
58  if not use:
59  use = default.split('|')
60  while len(use) < 5:
61  use.append(str())
62  return use[1], use[2], use[3], use[4]
63 
64 def getInitialInfo(conversation):
65  """Retrieves the initial starting objects required for the CFixer class.
66  This is the starting text, choices, and preconditions. text and choices
67  are both strings, but the preconditions returned may be a list."""
68  name = getNode(conversation).split('^')[0].split('|')[1]
69  whenclicked = getNode(conversation).split('^')[0].split('|')[2]
70  text, choices, sprite, motext = getAllNewInfo(conversation)
71  precon = getPreconditions(conversation)
72  return text, whenclicked, sprite, motext, name, precon
73 
74 def evaluateCondition(condition):
75  """Takes a string representing the condition, and returns whether it is
76  met or not."""
77  if type(condition) != str:
78  raise TypeError("The condition is not a valid type.")
79  elif condition == str():
80  return 1
81  elif condition[0] == '#':
82  tempd = dict()
83  exec condition[1:] in tempd
84  print "Custom condition code, returning: " + str(tempd["result"])
85  return tempd["result"]
86  else:
87  con = condition.split('#')
88  print "Checking \'%s : %s\'"%(con[0],con[1])
89  print "Returning: " + str(checkSaveValue(VS.getCurrentPlayer(),con[0],int(con[1])))
90  return checkSaveValue(VS.getCurrentPlayer(),con[0],int(con[1]))
91 
92 def evaluateConditions(conditions):
93  """Takes a list of conditions, and evaluates them."""
94  for cond in conditions:
95  if not evaluateCondition(cond):
96  return 0
97  return 1
98 
99 def setB(value):
100  global b
101  b=value
102 
104  return "Node2|#a==2^#b==1|\"Aar! This ale is mighty fine. You there, walk the plank!\"|choices|sprite|mouseover^#b==2|\"Land Ahoy!\"|\"bases/fixers/no.spr\"~\"bases/fixers/iso_decline.py\"~\"F Off!\"^\"bases/fixers/yes.spr\"~\"bases/fixers/iso_accept.py\"~\"Sure!\"|sprite|mouseover"
105 
106 def sampleCon():
107  return ["Node2^#b==1|\"Aar! This ale is mighty fine. You there, walk the plank!\"|choices^#b==2|\"Land Ahoy!\"|choices","Root|#a==2^#b==1|\"Aar! This RootBeer is mighty fine. You are scummvm.\"|choices|sprite|mouseover^#b==2|\"Land AhoyRoot!\"|choices2|sprite2|mouseover2"]
108 
109 
110 
111 
112 
113 #Root Node:
114 # + Conversation preconditions
115 # + Text contains strin reference to possible starting nodes
116 # +
117 #Root|pirate.spr|"A Pirate"^preconditions^n1|n2|n3^
118 #
119 #
120 #Node:
121 # + Primary preconditions (is this node possible)
122 # + Secondary preconditions (what specific text makes sense...ie, no sense talking about an event that hasn't happened yet)
123 # + Each set of secondary precons has a related text node and choices set (and maybe sprite?)
124 #
125 #n1|pirate_angry.spr|"An Angry Pirate"^ ......... the segments would only occur for the topmost nodes, where it is possible to alter the initial appearance still.
126 #
127 #you would normally have:
128 #
129 #The node structure:
130 #
131 #n2|prim_preconditions^sec_precons|"Aar! This ale is mighty fine. You there, walk the plank!"|choices|sprite|mouseover^sec_precons|"Land Ahoy!"|choices|sprite|mouseover
132 #
133 #Segments are seperated by the '^' character, elements (within each segment) by the '|'
134 #The first segment has two elements, the first is the reference, the second the precondition for node activation. This precondition, if it exists, will be ignored for references not equal to 'Root'.
135 #Each additional segment (minimum of 1) contains a possible value for this node. Each (in the order the are written) have their preconditions evaluated. The first to get the 'all clear' becomes this node, the text and choices are evaluated (and if the reference of the node is 'Root', then so are the sprite and mouseover values) and used.
136 #
137 #
138 #class Conversation:
139 # """Takes a string of the content making up a fixer converstion, and
140 # provides various methods to access and display the interaction."""
141 #
142 # def __init__(self, content):
143 #do error check for type 'content' etc
144 # self.content = content.split()
145 # self.content.sort()
146 # pass
147 #
148 # def getNode(ref="Root"):
149 # """Gets the conversation node with a reference of \'ref\'."""
150 #do error check for type etc
151 #
152 # def getNextNode(ref, result):
153 # """Gets the next conversation node after \'ref\' given the result
154 # of \'result\'."""