source: pycodebattler/trunk/pycodebattler/warrior.py @ 71

Revision 71, 4.9 KB checked in by atzm, 13 years ago (diff)

add preamble

  • Property svn:keywords set to Id
Line 
1# -*- coding: utf-8 -*-
2#
3#  Copyright (C) 2010 by Atzm WATANABE <atzm@atzm.org>
4#
5#  This program is free software; you can redistribute it and/or modify it
6#  under the terms of the GNU General Public License (version 2) as
7#  published by the Free Software Foundation.  It is distributed in the
8#  hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
9#  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10#  PURPOSE.  See the GNU General Public License for more details.
11#
12# $Id$
13#
14
15try:
16    import cStringIO as _StringIO
17except ImportError:
18    import StringIO as _StringIO
19
20import random
21import pycodebattler
22
23
24def pformat(warriors):
25    order = ['NAME', 'HP', 'SP', 'STR', 'CON', 'DEF', 'AGI', 'LCK']
26    mmap = {
27        'NAME': 'name',
28        'HP':   'hitpoint',
29        'SP':   'skillpoint',
30        'STR':  'strength',
31        'CON':  'concentration',
32        'DEF':  'defense',
33        'AGI':  'agility',
34        'LCK':  'luck',
35    }
36
37    horiz = [[getattr(w, mmap[n])() for n in order] for w in warriors]
38    virt = zip(*horiz)
39
40    sepnum = (len(order) * 3) - 1
41    fmt = ['|']
42    for i, t in enumerate(virt):
43        num = max(len(str(n)) for n in list(t) + [order[i]])
44        sepnum += num
45        if i == 0:
46            fmt.append(' %%-%ds |' % num)
47        else:
48            fmt.append(' %%%ds |' % num)
49
50    fmt = ''.join(fmt + ['\n'])
51    sep = ''.join(['+', '-' * sepnum, '+', '\n'])
52
53    io = _StringIO.StringIO()
54    io.write(sep)
55    io.write(fmt % tuple(order))
56    io.write(sep)
57    for d in horiz:
58        io.write(fmt % tuple(d))
59    io.write(sep)
60    io.flush()
61
62    return io.getvalue()
63
64
65class Warrior:
66    def __init__(self, name, fp, skill_list=[], luck_range=5000):
67        scorer = pycodebattler.score.Scorer(fp.readlines())
68
69        self._luck_range = luck_range
70        self._name = name
71        self._skill_list = {}
72        self._max_hitpoint = self._hitpoint = scorer.style_score()
73        self._max_skillpoint = self._skillpoint = scorer.name_score()
74        self._strength = scorer.complex_score()
75        self._concentration = scorer.colon_score()
76        self._defense = scorer.lines_score()
77        self._agility = scorer.const_score()
78        self._luck = luck_range // (self.hitpoint() +
79                                    self.skillpoint() +
80                                    self.strength() +
81                                    self.concentration() +
82                                    self.defense() +
83                                    self.agility())
84
85        for sk in skill_list:
86            self._skill_list[sk.name()] = sk
87
88    def __str__(self):
89        return self.name()
90
91    def name(self):
92        return self._name
93
94    def max_hitpoint(self):
95        return self._max_hitpoint
96
97    def hitpoint(self):
98        return self._hitpoint
99
100    def max_skillpoint(self):
101        return self._max_skillpoint
102
103    def skillpoint(self):
104        return self._skillpoint
105
106    def strength(self):
107        return self._strength
108
109    def concentration(self):
110        return self._concentration
111
112    def defense(self):
113        return self._defense
114
115    def agility(self):
116        return self._agility
117
118    def luck(self):
119        return self._luck
120
121    def skill_list(self):
122        return self._skill_list.keys()
123
124    def skill(self, name):
125        return self._skill_list[name]
126
127    def lucky(self):
128        r = random.randint(-256, self._luck_range)
129        return r <= (self.luck() + self.agility() / 8)
130
131    def is_injured(self):
132        return self.hitpoint() <= (self.max_hitpoint() / 10)
133
134    def is_dead(self):
135        return self.hitpoint() <= 0
136
137    def can_invoke(self, name):
138        try:
139            return self.skill(name).point() < self.skillpoint()
140        except KeyError:
141            return False
142
143    def invoke(self, name, enemy, warriors):
144        try:
145            return self.skill(name).invoke(self, enemy, warriors)
146        except KeyError:
147            return []
148
149    def spend(self, point):
150        self._skillpoint -= point
151        if self._skillpoint < 0:
152            self._skillpoint = 0
153        return point
154
155    def damage_from(self, enemy):
156        miss = False
157        critical = False
158
159        rgosa = enemy.strength() / 2
160        gosa = random.randint(0 - rgosa, rgosa)
161
162        if enemy.lucky():
163            critical = True
164            damage = (enemy.strength() + gosa) * random.randint(3, 8)
165        else:
166            damage = enemy.strength() + gosa - self.defense()
167
168        if damage <= 0:
169            damage = 1
170
171        if self.lucky() and not critical:
172            miss = True
173            damage = 0
174
175        return self.damage(damage), miss, critical
176
177    def damage(self, damage):
178        self._hitpoint -= damage
179        if self.hitpoint() < 0:
180            self._hitpoint = 0
181        if self.hitpoint() > self.max_hitpoint():
182            self._hitpoint = self.max_hitpoint()
183        return damage
184
185    def attack_to(self, enemy):
186        return enemy.damage_from(self)
Note: See TracBrowser for help on using the repository browser.