-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.py
149 lines (131 loc) · 5.68 KB
/
map.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
from __future__ import annotations
from core import Modifier, Action, Attack, Defense, Sound
from words import EventMessages, Format, ExitDescription
from things import Object
from containers import ObjectGroup
from typing import Set, Dict, Tuple
from dataclasses import dataclass, field
from copy import copy
@dataclass
class Place:
tag: str
description: str
category: str
variant: str
exits: Set[Exit]
height: int = 0
soundproof: int = 0
contained: ObjectGroup = field(default_factory=ObjectGroup)
actions: List[Action] = field(default_factory=list)
attacks: List[Attack] = field(default_factory=list)
defenses: List[Defense] = field(default_factory=list)
@property
def objects_description(self) -> str:
if bigs := [o.description.far for o in self.contained if o.visible]:
return f', verso {Format.quantify(bigs)}'
return ''
@property
def modifier(self) -> Modifier:
return Modifier(visibility=self.height)
@property
def actions(self) -> List[Action]:
return self._user_actions + \
[a for exit in self.exits for a in exit.actions] + \
self.contained.actions + \
[Action(
(f'{obj.pick_verb} {obj.description.oneof}').capitalize(),
EventMessages(
subject=f'{obj.pick_verb} {obj.description.oneof}',
close=f'{{subject}} prende {obj.description.oneof}',
far=f'{{subject}} prende in mano qualcosa',
),
(lambda obj: lambda char: char.inventory.pick(obj, char, self.contained))(obj),
condition=(lambda obj: lambda char: char.inventory.pick_message(obj))(obj)
) for obj in self.contained if isinstance(obj, Object) and not obj.visible] + \
[a for obj in self.contained for a in getattr(obj, 'ground_actions', []) if isinstance(obj, Object)]
@actions.setter
def actions(self, user_actions: List[Action]):
self._user_actions = user_actions if isinstance(user_actions, list) else []
def objects_around(self, visibility: int, done=(), all=True, dirs=(), d=None):
d = d or {}
if visibility >= 1:
for exit in self.exits - set(done):
exit.direction.objects_around(visibility-1, done + (self,), all, dirs+(exit,), d)
for obj in (self.contained.all if all else self.contained):
if obj.visible or not dirs:
d[obj] = dirs
return d
"""def objects_around(self, visibility: int, done=(), all=True) -> Dict[Object, Tuple[Place]]:
around = {}
if visibility >= 1:
around.update({obj: loc+(exit,) for exit in self.exits - set(done)
for obj, loc in exit.direction.objects_around(visibility-1, done + (self,), all).items()
if obj.visible})
around.update({obj: () for obj in (self.contained.all if all else self.contained)})
return around"""
def get_exit(self, tag):
return next(exit for exit in self.exits if exit.tag == tag)
def deltaObject(self, before, after, obj) -> Dict[str, List[Tuple[Object, Exit]]]:
if obj in before and obj not in after:
if before[obj]: return 'far_to_none', before[obj][-1]
else: return 'close_to_none', None
elif obj in after and obj not in before:
if after[obj]: return 'none_to_far', after[obj][-1]
else: return 'none_to_close', None
elif obj in after and obj in before:
if len(before[obj]) > len(after[obj]):
if after[obj]: return 'far_to_closer', after[obj][-1]
else: return 'far_to_close', None
elif len(before[obj]) < len(after[obj]):
if before[obj]: return 'far_to_farer', after[obj][-1]
else: return 'close_to_far', after[obj][-1]
else:
if before[obj]: return 'far', after[obj][-1]
else: return 'close', None
def __eq__(self, other): self is other
def __hash__(self): return id(self)
@dataclass
class Exit:
description: ExitDescription
tag: str
distance: int = 5
direction: Optional[Place] = None
parent: Optional[Place] = None
inverse: Optional[Exit] = None
@property
def actions(self) -> List[Action]:
a = Action(
time = self.distance,
fatigue = self.distance,
sound = Sound(event=EventMessages(far='{direction} senti qualcuno che cammina',)),
name = f'Vai {self.description}{self.direction.objects_description}',
event = EventMessages(),
visibility_event = EventMessages(
close_to_far = 'dietro di te vedi ancora {object}',
close_to_none = 'dietro di te non vedi più {object}',
far_to_close = 'arrivi da {object}',
far_to_closer = 'ti avvicini verso {object}, che vedi {direction}',
far_to_farer = 'dietro di te vedi ancora {object}',
far_to_none = 'dietro di te non vedi più {object}',
none_to_close = 'come arrivi vedi {object}',
none_to_far = 'ora {direction} vedi {object}'),
post_event = EventMessages(
subject = f'arrivi {self.direction.description}',
close=f'{{subject}} si allontana {self.description} ma la strada curva e riporta da te',
close_to_far = f'{{subject}} si allontana {self.description}',
close_to_none = f'{{subject}} si allontana {self.description} e scompare dalla tua vista',
far = 'distante, {direction}, vedi {subject} spostarsi',
far_to_close = f'{self.inverse.description} da te arriva {{subject}}',
far_to_closer = 'ancora distante, {direction} vedi avvicinarsi {subject}',
far_to_farer = 'già distante, {direction} vedi allontanarsi {subject}',
far_to_none = '{direction} vedi {subject} allontanarsi e scomparire dalla tua vista',
none_to_close = f'improvvisamente {{subject}} arriva da te {self.description.inverse}',
none_to_far = 'improvvisamente vedi {subject} {direction}'),
do = lambda c: c.move_to(self),
condition = lambda c: c.last_exit is not self)
b = copy(a) # Same, but with 'Torna verso' instead of 'Vai verso'
b.name = f'Torna {self.description}{self.direction.objects_description}'
b.condition = lambda c: c.last_exit is self
return [a, b]
def __eq__(self, other): self is other
def __hash__(self): return id(self)