1 | # -*- coding: utf-8 -*- |
---|
2 | # $Id$ |
---|
3 | |
---|
4 | try: |
---|
5 | import cStringIO as _StringIO |
---|
6 | except ImportError: |
---|
7 | import StringIO as _StringIO |
---|
8 | |
---|
9 | import random |
---|
10 | import pycodebattler |
---|
11 | |
---|
12 | |
---|
13 | def pformat(warriors): |
---|
14 | order = ['NAME', 'HP', 'SP', 'STR', 'CON', 'DEF', 'AGI', 'LCK'] |
---|
15 | mmap = { |
---|
16 | 'NAME': 'name', |
---|
17 | 'HP': 'hitpoint', |
---|
18 | 'SP': 'skillpoint', |
---|
19 | 'STR': 'strength', |
---|
20 | 'CON': 'concentration', |
---|
21 | 'DEF': 'defense', |
---|
22 | 'AGI': 'agility', |
---|
23 | 'LCK': 'luck', |
---|
24 | } |
---|
25 | |
---|
26 | horiz = [[getattr(w, mmap[n])() for n in order] for w in warriors] |
---|
27 | virt = zip(*horiz) |
---|
28 | |
---|
29 | sepnum = (len(order) * 3) - 1 |
---|
30 | fmt = ['|'] |
---|
31 | for i, t in enumerate(virt): |
---|
32 | num = max(len(str(n)) for n in list(t) + [order[i]]) |
---|
33 | sepnum += num |
---|
34 | if i == 0: |
---|
35 | fmt.append(' %%-%ds |' % num) |
---|
36 | else: |
---|
37 | fmt.append(' %%%ds |' % num) |
---|
38 | |
---|
39 | fmt = ''.join(fmt + ['\n']) |
---|
40 | sep = ''.join(['+', '-' * sepnum, '+', '\n']) |
---|
41 | |
---|
42 | io = _StringIO.StringIO() |
---|
43 | io.write(sep) |
---|
44 | io.write(fmt % tuple(order)) |
---|
45 | io.write(sep) |
---|
46 | for d in horiz: |
---|
47 | io.write(fmt % tuple(d)) |
---|
48 | io.write(sep) |
---|
49 | io.flush() |
---|
50 | |
---|
51 | return io.getvalue() |
---|
52 | |
---|
53 | |
---|
54 | class Warrior: |
---|
55 | def __init__(self, name, fp, skill_list=[], luck_range=5000): |
---|
56 | scorer = pycodebattler.score.Scorer(fp.readlines()) |
---|
57 | |
---|
58 | self._luck_range = luck_range |
---|
59 | self._name = name |
---|
60 | self._skill_list = {} |
---|
61 | self._max_hitpoint = self._hitpoint = scorer.pep8_score() |
---|
62 | self._max_skillpoint = self._skillpoint = scorer.name_score() |
---|
63 | self._strength = scorer.complex_score() |
---|
64 | self._concentration = scorer.colon_score() |
---|
65 | self._defense = scorer.lines_score() |
---|
66 | self._agility = scorer.const_score() |
---|
67 | self._luck = luck_range // (self.hitpoint() + |
---|
68 | self.skillpoint() + |
---|
69 | self.strength() + |
---|
70 | self.concentration() + |
---|
71 | self.defense() + |
---|
72 | self.agility()) |
---|
73 | |
---|
74 | for sk in skill_list: |
---|
75 | self._skill_list[sk.name()] = sk |
---|
76 | |
---|
77 | def __str__(self): |
---|
78 | return self.name() |
---|
79 | |
---|
80 | def name(self): |
---|
81 | return self._name |
---|
82 | |
---|
83 | def max_hitpoint(self): |
---|
84 | return self._max_hitpoint |
---|
85 | |
---|
86 | def hitpoint(self): |
---|
87 | return self._hitpoint |
---|
88 | |
---|
89 | def max_skillpoint(self): |
---|
90 | return self._max_skillpoint |
---|
91 | |
---|
92 | def skillpoint(self): |
---|
93 | return self._skillpoint |
---|
94 | |
---|
95 | def strength(self): |
---|
96 | return self._strength |
---|
97 | |
---|
98 | def concentration(self): |
---|
99 | return self._concentration |
---|
100 | |
---|
101 | def defense(self): |
---|
102 | return self._defense |
---|
103 | |
---|
104 | def agility(self): |
---|
105 | return self._agility |
---|
106 | |
---|
107 | def luck(self): |
---|
108 | return self._luck |
---|
109 | |
---|
110 | def skill_list(self): |
---|
111 | return self._skill_list.keys() |
---|
112 | |
---|
113 | def skill(self, name): |
---|
114 | return self._skill_list[name] |
---|
115 | |
---|
116 | def lucky(self): |
---|
117 | r = random.randint(-256, self._luck_range) |
---|
118 | return r <= (self.luck() + self.agility() / 8) |
---|
119 | |
---|
120 | def is_injured(self): |
---|
121 | return self.hitpoint() <= (self.max_hitpoint() / 10) |
---|
122 | |
---|
123 | def is_dead(self): |
---|
124 | return self.hitpoint() <= 0 |
---|
125 | |
---|
126 | def can_invoke(self, name): |
---|
127 | try: |
---|
128 | return self.skill(name).point() < self.skillpoint() |
---|
129 | except KeyError: |
---|
130 | return False |
---|
131 | |
---|
132 | def invoke(self, name, enemy, warriors): |
---|
133 | try: |
---|
134 | return self.skill(name).invoke(self, enemy, warriors) |
---|
135 | except KeyError: |
---|
136 | return [] |
---|
137 | |
---|
138 | def spend(self, point): |
---|
139 | self._skillpoint -= point |
---|
140 | if self._skillpoint < 0: |
---|
141 | self._skillpoint = 0 |
---|
142 | return point |
---|
143 | |
---|
144 | def damage_from(self, enemy): |
---|
145 | miss = False |
---|
146 | critical = False |
---|
147 | |
---|
148 | rgosa = enemy.strength() / 2 |
---|
149 | gosa = random.randint(0 - rgosa, rgosa) |
---|
150 | |
---|
151 | if enemy.lucky(): |
---|
152 | critical = True |
---|
153 | damage = (enemy.strength() + gosa) * random.randint(3, 8) |
---|
154 | else: |
---|
155 | damage = enemy.strength() + gosa - self.defense() |
---|
156 | |
---|
157 | if damage <= 0: |
---|
158 | damage = 1 |
---|
159 | |
---|
160 | if self.lucky() and not critical: |
---|
161 | miss = True |
---|
162 | damage = 0 |
---|
163 | |
---|
164 | return self.damage(damage), miss, critical |
---|
165 | |
---|
166 | def damage(self, damage): |
---|
167 | self._hitpoint -= damage |
---|
168 | if self.hitpoint() < 0: |
---|
169 | self._hitpoint = 0 |
---|
170 | if self.hitpoint() > self.max_hitpoint(): |
---|
171 | self._hitpoint = self.max_hitpoint() |
---|
172 | return damage |
---|
173 | |
---|
174 | def attack_to(self, enemy): |
---|
175 | return enemy.damage_from(self) |
---|