1 | # -*- coding: utf-8 -*- |
---|
2 | # $Id$ |
---|
3 | |
---|
4 | import sys |
---|
5 | import random |
---|
6 | |
---|
7 | try: |
---|
8 | import cStringIO as _StringIO |
---|
9 | except ImportError: |
---|
10 | import StringIO as _StringIO |
---|
11 | |
---|
12 | try: |
---|
13 | set, frozenset |
---|
14 | except NameError: |
---|
15 | from sets import Set as set, ImmutableSet as frozenset |
---|
16 | |
---|
17 | |
---|
18 | def pformat(skills): |
---|
19 | order = ['NAME', 'TYPE', 'SP', 'LV'] |
---|
20 | mmap = { |
---|
21 | 'NAME': 'name', |
---|
22 | 'TYPE': 'skilltype', |
---|
23 | 'SP': 'point', |
---|
24 | 'LV': 'level', |
---|
25 | } |
---|
26 | |
---|
27 | horiz = [[getattr(s, mmap[n])() for n in order] for s in skills] |
---|
28 | |
---|
29 | for i, t in enumerate(horiz): |
---|
30 | horiz[i][1] = str(t[1]).split('.')[-1] |
---|
31 | |
---|
32 | virt = zip(*horiz) |
---|
33 | |
---|
34 | sepnum = (len(order) * 3) - 1 |
---|
35 | fmt = ['|'] |
---|
36 | for i, t in enumerate(virt): |
---|
37 | num = max(len(str(n)) for n in list(t) + [order[i]]) |
---|
38 | sepnum += num |
---|
39 | if i < 2: |
---|
40 | fmt.append(' %%-%ds |' % num) |
---|
41 | else: |
---|
42 | fmt.append(' %%%ds |' % num) |
---|
43 | |
---|
44 | fmt = ''.join(fmt + ['\n']) |
---|
45 | sep = ''.join(['+', '-' * sepnum, '+', '\n']) |
---|
46 | |
---|
47 | io = _StringIO.StringIO() |
---|
48 | io.write(sep) |
---|
49 | io.write(fmt % tuple(order)) |
---|
50 | io.write(sep) |
---|
51 | for d in horiz: |
---|
52 | io.write(fmt % tuple(d)) |
---|
53 | io.write(sep) |
---|
54 | io.flush() |
---|
55 | |
---|
56 | return io.getvalue() |
---|
57 | |
---|
58 | |
---|
59 | class SkillType: |
---|
60 | def __init__(self, attacker): |
---|
61 | self.attacker = attacker |
---|
62 | |
---|
63 | def range(self): |
---|
64 | return 0 |
---|
65 | |
---|
66 | def selectable(self, warriors): |
---|
67 | return [] |
---|
68 | |
---|
69 | def efficiency(self): |
---|
70 | return 0 |
---|
71 | |
---|
72 | |
---|
73 | class HealType(SkillType): |
---|
74 | def range(self): |
---|
75 | return 1 |
---|
76 | |
---|
77 | def selectable(self, warriors): |
---|
78 | return [self.attacker] |
---|
79 | |
---|
80 | def efficiency(self): |
---|
81 | base = self.attacker.concentration() * 2 + 10 |
---|
82 | gosa = base / 2 |
---|
83 | return 0 - random.randint(base - gosa, base + gosa) |
---|
84 | |
---|
85 | |
---|
86 | class ResurrectionType(SkillType): |
---|
87 | def range(self): |
---|
88 | return 1 |
---|
89 | |
---|
90 | def selectable(self, warriors): |
---|
91 | loosers = [w for w in warriors if w.is_dead()] |
---|
92 | try: |
---|
93 | return [random.choice(loosers)] |
---|
94 | except IndexError: |
---|
95 | return [] |
---|
96 | |
---|
97 | def efficiency(self): |
---|
98 | base = self.attacker.concentration() + 10 |
---|
99 | gosa = base / 2 |
---|
100 | return 0 - random.randint(base - gosa, base + gosa) |
---|
101 | |
---|
102 | |
---|
103 | class SingleAttackType(SkillType): |
---|
104 | def range(self): |
---|
105 | return 1 |
---|
106 | |
---|
107 | def selectable(self, warriors): |
---|
108 | living = [w for w in warriors if not w.is_dead()] |
---|
109 | living.remove(self.attacker) |
---|
110 | return living |
---|
111 | |
---|
112 | def efficiency(self): |
---|
113 | base = int(self.attacker.concentration() * 2.5) |
---|
114 | gosa = base / 2 |
---|
115 | return random.randint(base - gosa, base + gosa) |
---|
116 | |
---|
117 | |
---|
118 | class RangeAttackType(SkillType): |
---|
119 | def range(self): |
---|
120 | return self.attacker.concentration() // 25 + 1 |
---|
121 | |
---|
122 | def selectable(self, warriors): |
---|
123 | living = [w for w in warriors if not w.is_dead()] |
---|
124 | living.remove(self.attacker) |
---|
125 | return living |
---|
126 | |
---|
127 | def efficiency(self): |
---|
128 | base = int(self.attacker.concentration() * 2.3) |
---|
129 | gosa = base / 2 |
---|
130 | return random.randint(base - gosa, base + gosa) |
---|
131 | |
---|
132 | |
---|
133 | class MultiAttackType(SkillType): |
---|
134 | def range(self): |
---|
135 | return sys.maxint |
---|
136 | |
---|
137 | def selectable(self, warriors): |
---|
138 | living = [w for w in warriors if not w.is_dead()] |
---|
139 | living.remove(self.attacker) |
---|
140 | return living |
---|
141 | |
---|
142 | def efficiency(self): |
---|
143 | base = int(self.attacker.concentration() * 1.7) |
---|
144 | gosa = base / 2 |
---|
145 | return random.randint(base - gosa, base + gosa) |
---|
146 | |
---|
147 | |
---|
148 | class SuicideAttackType(SkillType): |
---|
149 | def range(self): |
---|
150 | return sys.maxint |
---|
151 | |
---|
152 | def selectable(self, warriors): |
---|
153 | living = [w for w in warriors if not w.is_dead()] |
---|
154 | living.remove(self.attacker) |
---|
155 | return living + [self.attacker] |
---|
156 | |
---|
157 | def efficiency(self): |
---|
158 | return self.attacker.hitpoint() |
---|
159 | |
---|
160 | |
---|
161 | class Skill: |
---|
162 | def __init__(self, name, point, skilltype, level=1): |
---|
163 | self._name = name |
---|
164 | self._point = point |
---|
165 | self._skilltype = skilltype |
---|
166 | self._level = level |
---|
167 | |
---|
168 | def name(self): |
---|
169 | return self._name |
---|
170 | |
---|
171 | def point(self): |
---|
172 | return self._point |
---|
173 | |
---|
174 | def skilltype(self): |
---|
175 | return self._skilltype |
---|
176 | |
---|
177 | def level(self): |
---|
178 | return self._level |
---|
179 | |
---|
180 | def range(self, attacker): |
---|
181 | return self._skilltype(attacker).range() |
---|
182 | |
---|
183 | def selectable(self, attacker, warriors): |
---|
184 | return self._skilltype(attacker).selectable(warriors) |
---|
185 | |
---|
186 | def invoke(self, attacker, targets, warriors): |
---|
187 | type_ = self._skilltype(attacker) |
---|
188 | |
---|
189 | if not targets: |
---|
190 | return [] |
---|
191 | if not warriors: |
---|
192 | return [] |
---|
193 | if attacker.skillpoint() < self.point(): |
---|
194 | return [] |
---|
195 | if type_.range() * (self._level // 2 + 1) < len(targets): |
---|
196 | return [] |
---|
197 | if not set(targets).issubset(set(warriors)): |
---|
198 | return [] |
---|
199 | |
---|
200 | attacker.spend(self.point()) |
---|
201 | |
---|
202 | return [(t, self._invoke_one(type_, t, targets), False, False) |
---|
203 | for t in targets] |
---|
204 | |
---|
205 | def _invoke_one(self, type_, t, targets): |
---|
206 | return t.damage(int(type_.efficiency() * self.level() / len(targets))) |
---|