|
29 | 29 | _LEVELS = load_dict('levels') |
30 | 30 |
|
31 | 31 | d20 = _Die.d20() |
32 | | - |
33 | | -def get_direction(pointa: type(Vec2d), pointb: type(Vec2d)): |
| 32 | +CARDINAL_DIRECTIONS = { |
| 33 | + "East": list(range(337, 360)) + list(range(23)) + [0], |
| 34 | + "North-East": range(22, 68), |
| 35 | + "North": range(67, 113), |
| 36 | + "North-West": range(112, 158), |
| 37 | + "West": range(157, 203), |
| 38 | + "South-West": range(202, 248), |
| 39 | + "South": range(247, 293), |
| 40 | + "South-East": range(292, 338) |
| 41 | +} |
| 42 | + |
| 43 | +def get_direction(pointa: tuple[int, int] | type(Vec2d), pointb: tuple[int, int] | type(Vec2d)): |
| 44 | + if isinstance(pointa, tuple): |
| 45 | + pointa = Vec2d(*pointa) |
| 46 | + if isinstance(pointb, tuple): |
| 47 | + pointb = Vec2d(*pointb) |
34 | 48 | angle_degrees = (pointb - pointa).angle_degrees |
35 | | - angle_degrees = int(angle_degrees) |
| 49 | + angle_degrees = round(angle_degrees) |
36 | 50 | angle_degrees %= 360 |
37 | | - cardinal_directions = { |
38 | | - "E": range(337, 360) or range(23), |
39 | | - "NE": range(22, 68), |
40 | | - "N": range(67, 113), |
41 | | - "NW": range(112, 158), |
42 | | - "W": range(157, 203), |
43 | | - "SW": range(202, 248), |
44 | | - "S": range(247, 293), |
45 | | - "SE": range(292, 338) |
46 | | - } |
47 | | - return next( |
48 | | - ( |
49 | | - direction |
50 | | - for direction, angle_range in cardinal_directions.items() |
51 | | - if angle_degrees in angle_range |
52 | | - ), |
53 | | - "Invalid angle", |
54 | | - ) |
| 51 | + |
| 52 | + return [direction for direction, angle_range in CARDINAL_DIRECTIONS.items() if angle_degrees in angle_range][0] |
55 | 53 |
|
56 | 54 |
|
57 | 55 |
|
@@ -154,6 +152,7 @@ def __init__( |
154 | 152 | self.skill_points = 0 |
155 | 153 | self.armor_class = 0 |
156 | 154 | self.inventory = None |
| 155 | + self.vision = 200 |
157 | 156 | self._traveling = False |
158 | 157 | self._initialize(role, race, background, alignment, age) |
159 | 158 | self._is_turn = False |
@@ -763,28 +762,40 @@ def _is_in_pickup_range(self, other): |
763 | 762 | def _is_in_sight(self, other): |
764 | 763 | return self._get_distance(other) <= 20 |
765 | 764 |
|
766 | | - def _is_in_distant_view(self, other): |
| 765 | + def _is_in_view(self, other): |
767 | 766 | return self._get_distance(other) <= 40 |
768 | 767 |
|
769 | 768 | def _see_item(self, item): |
770 | | - if self._is_in_sight(item): |
771 | | - if self._is_in_pickup_range(item): |
772 | | - self._nearby_items[item.name] = item |
773 | | - print(f'You see a {item.name} at your feet.') |
774 | | - else: |
775 | | - direction = get_direction(Vec2d(self.position[0], self.position[1]), Vec2d(item.position[0], item.position[1])) |
776 | | - print(f'You see a {item.name} nearby. ({direction})') |
777 | | - elif self._is_in_distant_view(item): |
778 | | - direction = get_direction(Vec2d(self.position[0], self.position[1]), Vec2d(item.position[0], item.position[1])) |
779 | | - print(f'You see an object some distance to the {direction}') |
| 769 | + direction = get_direction(item.position, self.position) |
| 770 | + if self._is_in_pickup_range(item): |
| 771 | + self._nearby_items[item.name] = item |
| 772 | + print(f'You see {repr(item)} at your feet.') |
| 773 | + else: |
| 774 | + print(f'You see {repr(item)} to the {direction}') |
780 | 775 |
|
781 | 776 | def look_around(self): |
782 | | - for item in self.grid.armory._grid_instances.values(): |
783 | | - self._see_item(item) |
784 | | - for item in self.grid.goods._grid_instances.values(): |
785 | | - self._see_item(item) |
786 | | - for cell in self.grid.get_area(self.cell_name, 20): |
| 777 | + vision = self.vision//5 |
| 778 | + for cell in self.grid.get_area(self.cell_name, vision): |
| 779 | + if cell.entry_object['items'] is not None: |
| 780 | + self._nearby_items |= cell.entry_object['items'] |
787 | 781 | if cell.entry_unit['players'] is not None: |
788 | 782 | for player in cell.entry_unit['players'].values(): |
789 | 783 | if player.parent != self: |
790 | 784 | print(f'You see a {player.parent._race.title()} {player.parent._role.title()} nearby.') |
| 785 | + |
| 786 | + directions = { |
| 787 | + self.grid.get_direction(self.cell, item.cell) |
| 788 | + for name, item in self._nearby_items.items() |
| 789 | + } |
| 790 | + if len(self._nearby_items) > 8 or len(directions) > 4: |
| 791 | + print('There are several objects scattered around you,') |
| 792 | + elif len(self._nearby_items) < 4: |
| 793 | + for item in self._nearby_items.values(): |
| 794 | + self._see_item(item) |
| 795 | + |
| 796 | + |
| 797 | + # for item in self.grid.armory._grid_instances.values(): |
| 798 | + # item_count += 1 if self._is_in_sight(item) else 0 |
| 799 | + # for item in self.grid.goods._grid_instances.values(): |
| 800 | + # item_ |
| 801 | + # self._see_item(item) |
0 commit comments