-
Notifications
You must be signed in to change notification settings - Fork 0
/
story.py
295 lines (260 loc) · 9.69 KB
/
story.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
"""
@author: zx013
"""
from setting import MazeBase
from cache import Config
from tools import Tools
from g import gmaze, gtask
import operator
import random
class Scene:
def __init__(self, name='', help='', pos=(0, 0, 0), dialog=[], task=[], repeat=False):
self.active = False #是否激活过对话
self.name = name
self.help = help
self.pos = pos
#人物,1表示英雄,2表示对方,其他数字表示其他人物
self.dialog = dialog #对话
self.task = task
self.repeat = repeat #是否重复触发,前置中不能设置该参数
self.forward = []
self.backward = []
def isdialog(self):
if self.dialog:
return True
return False
def run(self):
self.active = True
task = []
for b in self.backward:
if not sum([not f.active for f in b.forward]):
task.append(b)
return task
class Story:
def __init__(self, maze):
self.maze = maze
self.task = {} #任务指引
self.state = {}
self.repeat = {}
def create_scene(self, **kwargs):
scene = Scene(**kwargs)
self.add_scene(scene)
return scene
def add_scene(self, scene, task_id=None):
for s in Tools.object_list(scene):
if s.isdialog():
if s.repeat:
self.add_repeat(s)
else:
self.add_dialog(s, task_id)
else:
self.add_task(s, task_id)
def clean_scene(self, scene):
for s in Tools.object_list(scene):
if s.isdialog():
self.clean_dialog(s)
else:
self.clean_task(s)
def del_scene(self, scene, immediate=False):
for s in Tools.object_list(scene):
if s.isdialog():
self.del_dialog(s, immediate)
else:
self.del_task(s, immediate)
self.clean_scene(s)
def next_scene(self, scene):
task = scene.run()
if len(task) > 0:
self.clean_scene(scene)
self.add_scene(task, scene.task_id)
else:
self.del_scene(scene)
def add_repeat(self, scene):
for s in Tools.object_list(scene):
pos = s.pos
if pos not in self.repeat:
self.repeat[pos] = []
self.repeat[pos].append(s)
def create_dialog_name(self, pos):
pos_data = gmaze.get_data(pos)
pos_key = MazeBase.get_key(pos_data)
pos_name = Config.config[pos_key].get('name', '未知')
floor = pos[0]
name = '找到{}层的{}'.format(floor, pos_name)
return name
def add_dialog(self, scene, task_id=None):
for s in Tools.object_list(scene):
if task_id is None:
s.task_id = gtask.insert()
else:
s.task_id = task_id
gtask.update(s.task_id, s.name, s.help)
pos = s.pos
if pos not in self.task:
self.task[pos] = []
self.task[pos].append(s)
def clean_dialog(self, scene):
for s in Tools.object_list(scene):
pos = s.pos
if s in self.task[pos]:
self.task[pos].remove(s)
if not self.task[pos]:
del self.task[pos]
def del_dialog(self, scene, immediate=False):
for s in Tools.object_list(scene):
gtask.remove(s.task_id, immediate)
def split_task(self, task, ops):
name, goal = task.split(ops)
goal = int(goal) if goal.isdigit() else 0
return name, goal
def create_task_name(self, name, ops, goal):
name_dict = {
'health': ('生命', '恢复', '损失'),
'attack': ('攻击', '提高', '降低'),
'defence': ('防御', '提高', '降低'),
'gold': ('金钱', '获得', '消耗'),
'experience': ('经验', '获得', '消耗'),
}
task_name, task_add, task_del = name_dict.get(name)
if ops == '+':
name = '{}{}{}'.format(task_add, goal, task_name)
elif ops == '-':
name = '{}{}{}'.format(task_del, goal, task_name)
elif ops == '*':
name = '{}{}倍的{}'.format(task_add, goal, task_name)
elif ops == '/':
name = '{}{}倍的{}'.format(task_del, goal, task_name)
elif ops == '<=':
name = '{}降低至{}'.format(task_name, goal)
elif ops == '<':
name = '{}低于{}'.format(task_name, goal)
elif ops == '>=':
name = '{}到达{}'.format(task_name, goal)
elif ops == '>':
name = '{}超过{}'.format(task_name, goal)
elif ops == '==':
name = '{}恰好为{}'.format(task_name, goal)
elif ops == '!=':
name = '{}不为{}'.format(task_name, goal)
else:
name = ''
return name
def create_task(self, task):
operate_dict = {
'+': operator.add,
'-': operator.sub,
'*': operator.mul,
'/': operator.floordiv
} #乘除的显示上应该需要另外的逻辑
compare_dict = {
'<=': operator.le,
'<': operator.lt,
'>=': operator.ge,
'>': operator.gt,
'==': operator.eq,
'!=': operator.ne
}
task = task.replace(' ', '')
for ops in compare_dict.keys():
if ops not in task:
continue
name, goal = self.split_task(task, ops)
op = compare_dict.get(ops)
func = lambda x, y: op(x, goal)
return name, func, ops, goal, True
for ops in operate_dict.keys():
if ops not in task:
continue
if '+' in task:
name, goal = self.split_task(task, '+')
func = lambda x, y: x >= y + goal
elif '-' in task:
name, goal = self.split_task(task, '-')
func = lambda x, y: x <= y - goal
elif '*' in task:
name, goal = self.split_task(task, '*')
func = lambda x, y: x >= y * goal
elif '/' in task:
name, goal = self.split_task(task, '/')
func = lambda x, y: x <= y / goal
return name, func, ops, goal, False
return None
def add_task(self, scene, task_id=None):
for s in Tools.object_list(scene):
name_list = []
op_list = []
for task in Tools.object_list(s.task)[::-1]:
name, op, ops, goal, achieve = self.create_task(task)
if name not in name_list:
name_list.insert(0, name)
op_list.insert(0, (name, op))
if task_id is None:
s.task_id = gtask.insert()
else:
s.task_id = task_id
if not s.name:
s.name = self.create_task_name(name, ops, goal)
s.attr = dict([(name, getattr(gmaze.herostate, name)) for name in name_list])
s.goal = goal
if achieve:
achieve = s.attr[name]
s.attr[name] = 0
else:
achieve = 0
gtask.update(s.task_id, s.name, s.help, s.goal, achieve)
for name in name_list:
if name not in self.state:
self.state[name] = {}
self.state[name][s.task_id] = {'name': name_list, 'op': op_list, 'scene': s}
def clean_task(self, scene):
for s in Tools.object_list(scene):
for task in self.state.values():
if s.task_id in task:
del task[s.task_id]
def del_task(self, scene, immediate=False):
for s in Tools.object_list(scene):
gtask.remove(s.task_id, immediate)
def add_challenge(self):
self.create_scene(task='health >= 1000')
def del_challenge(self):
pass
def connect(self, fscene, bscene):
for f in Tools.object_list(fscene):
for b in Tools.object_list(bscene):
f.backward.append(b)
b.forward.append(f)
self.del_scene(b, immediate=True)
def check_state(self, state, name):
if name not in self.state:
return None
for task in list(self.state[name].values()):
scene = task['scene']
attr = dict([(name, getattr(gmaze.herostate, name)) for name in task['name']])
if name == task['name'][0]:
gtask.achieve(scene.task_id, attr[name] - scene.attr[name])
for name, op in task['op']:
if not op(attr[name], scene.attr[name]):
break
else:
self.next_scene(scene)
return scene
return None
def check_pos(self, pos):
scene_task = [scene for scene in self.task.get(pos, []) if not scene.active]
if scene_task:
scene = scene_task.pop()
self.next_scene(scene)
return scene
scene_repeat = [scene for scene in self.repeat.get(pos, [])]
random.shuffle(scene_repeat)
if scene_repeat:
scene = scene_repeat.pop()
task = scene.run()
print('repeat', task)
return scene
return None
def save(self):
pass
def load(self):
pass