home · contact · privacy
Add colourful intoxication beverage.
[plomrogue2] / plomrogue / things.py
1 from plomrogue.errors import GameError
2 from plomrogue.mapping import YX
3
4
5
6 class ThingBase:
7     type_ = '?'
8
9     def __init__(self, game, id_=0, position=(YX(0, 0), YX(0, 0))):
10         self.game = game
11         if id_ == 0:
12             self.id_ = self.game.new_thing_id()
13         else:
14             self.id_ = id_
15         self.position = position
16
17
18
19 class Thing(ThingBase):
20     blocking = False
21     portable = False
22     protection = '.'
23
24     def __init__(self, *args, **kwargs):
25         super().__init__(*args, **kwargs)
26
27     def proceed(self):
28         pass
29
30     @property
31     def type_(self):
32         return self.__class__.get_type()
33
34     @classmethod
35     def get_type(cls):
36         return cls.__name__[len('Thing_'):]
37
38
39
40 class Thing_Item(Thing):
41     symbol_hint = 'i'
42     portable = True
43
44
45
46 class ThingSpawner(Thing):
47     symbol_hint = 'S'
48
49     def proceed(self):
50         for t in [t for t in self.game.things
51                   if t != self and t.position == self.position]:
52             return
53         t = self.game.thing_types[self.child_type](self.game,
54                                                    position=self.position)
55         self.game.things += [t]
56         self.game.changed = True
57
58
59
60 class Thing_ItemSpawner(ThingSpawner):
61     child_type = 'Item'
62
63
64
65 class Thing_SpawnPointSpawner(ThingSpawner):
66     child_type = 'SpawnPoint'
67
68
69
70 class Thing_SpawnPoint(Thing):
71     symbol_hint = 's'
72     portable = True
73     name = 'username'
74
75
76
77 class Thing_DoorSpawner(ThingSpawner):
78     child_type = 'Door'
79
80
81
82 class Thing_Door(Thing):
83     symbol_hint = 'D'
84     blocking = False
85     portable = True
86
87     def open(self):
88         self.blocking = False
89         self.portable = True
90         del self.thing_char
91
92     def close(self):
93         self.blocking = True
94         self.portable = False
95         self.thing_char = '#'
96
97
98
99 class Thing_Consumable(Thing):
100     symbol_hint = 'B'
101     portable = True
102
103
104
105 class Thing_ConsumableSpawner(ThingSpawner):
106     child_type = 'Consumable'
107
108
109
110 class ThingAnimate(Thing):
111     blocking = True
112     drunk = 0
113
114     def __init__(self, *args, **kwargs):
115         super().__init__(*args, **kwargs)
116         self.next_tasks = []
117         self.set_task('WAIT')
118         self._fov = None
119
120     def set_task(self, task_name, args=()):
121         task_class = self.game.tasks[task_name]
122         self.task = task_class(self, args)
123         self.task.check()  # will throw GameError if necessary
124
125     def set_next_task(self, task_name, args=()):
126         task_class = self.game.tasks[task_name]
127         self.next_tasks += [task_class(self, args)]
128
129     def get_next_task(self):
130         if len(self.next_tasks) > 0:
131             task = self.next_tasks.pop(0)
132             task.check()
133             return task
134         else:
135             return None
136
137     def proceed(self):
138         self.drunk -= 1
139         if self.drunk == 0:
140             for c_id in self.game.sessions:
141                 if self.game.sessions[c_id]['thing_id'] == self.id_:
142                     self.game.io.send('DEFAULT_COLORS', c_id)
143                     self.game.io.send('CHAT "You sober up."', c_id)
144             self.game.changed = True
145         self._fov = None
146         if self.task is None:
147             self.task = self.get_next_task()
148             return
149         try:
150             self.task.check()
151         except GameError as e:
152             self.task = None
153             raise e
154         self.task.todo -= 1
155         if self.task.todo <= 0:
156             self._last_task_result = self.task.do()
157             self.game.changed = True
158             self.task = self.get_next_task()
159
160     @property
161     def fov_stencil(self):
162         if self._fov:
163             return self._fov
164         fov_map_class = self.game.map_geometry.fov_map_class
165         self._fov = fov_map_class(self.game.things, self.game.maps, self.position,
166                                   12, self.game.get_map)
167         return self._fov
168
169     def fov_test(self, big_yx, little_yx):
170         test_position = self.fov_stencil.target_yx(big_yx, little_yx)
171         if self.fov_stencil.inside(test_position):
172             if self.fov_stencil[test_position] == '.':
173                 return True
174         return False
175
176     def fov_stencil_map(self, map_type='normal'):
177         visible_terrain = ''
178         for yx in self.fov_stencil:
179             if self.fov_stencil[yx] == '.':
180                 big_yx, little_yx = self.fov_stencil.source_yxyx(yx)
181                 map_ = self.game.get_map(big_yx, map_type)
182                 visible_terrain += map_[little_yx]
183             else:
184                 visible_terrain += ' '
185         return visible_terrain
186
187
188
189 class Thing_Player(ThingAnimate):
190     symbol_hint = '@'
191
192     def __init__(self, *args, **kwargs):
193         super().__init__(*args, **kwargs)
194         self.carrying = None