|
@@ -1,13 +1,11 @@
|
1
|
1
|
from synergine.synergy.event.Action import Action
|
2
|
2
|
from intelligine.synergy.event.move.MoveEvent import MoveEvent
|
3
|
3
|
from random import randint, choice, randrange
|
|
4
|
+from synergine.synergy.event.exception.ActionAborted import ActionAborted
|
4
|
5
|
from xyzworld.cst import POSITION
|
5
|
|
-from intelligine.cst import PREVIOUS_DIRECTION, BLOCKED_SINCE, MOVE_MODE, MOVE_MODE_EXPLO, PHEROMONE_SEARCHING
|
6
|
|
-from intelligine.cst import COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING
|
7
|
|
-from intelligine.synergy.event.move.direction import directions_same_level, directions_modifiers, directions_slighty
|
|
6
|
+from intelligine.cst import PREVIOUS_DIRECTION, BLOCKED_SINCE
|
|
7
|
+from intelligine.synergy.event.move.direction import directions_same_level, directions_slighty
|
8
|
8
|
from intelligine.synergy.event.move.direction import get_position_with_direction_decal
|
9
|
|
-from intelligine.core.exceptions import NoPheromone, BestPheromoneHere
|
10
|
|
-from intelligine.simulation.pheromone.DirectionPheromone import DirectionPheromone
|
11
|
9
|
|
12
|
10
|
|
13
|
11
|
class MoveAction(Action):
|
|
@@ -21,12 +19,13 @@ class MoveAction(Action):
|
21
|
19
|
|
22
|
20
|
def prepare(self, context):
|
23
|
21
|
object_point = context.metas.value.get(POSITION, self._object_id)
|
|
22
|
+ direction = self._get_prepared_direction(context, object_point)
|
|
23
|
+ self._set_prepared_direction(context, object_point, direction)
|
24
|
24
|
|
25
|
|
- try:
|
26
|
|
- direction = self._get_direction_with_pheromones(context, object_point)
|
27
|
|
- except NoPheromone:
|
28
|
|
- direction = self._get_random_direction(context)
|
|
25
|
+ def _get_prepared_direction(self, context, object_point):
|
|
26
|
+ return self._get_random_direction(context)
|
29
|
27
|
|
|
28
|
+ def _set_prepared_direction(self, context, object_point, direction):
|
30
|
29
|
move_to_point = get_position_with_direction_decal(direction, object_point)
|
31
|
30
|
if self._direction_point_is_possible(context, move_to_point):
|
32
|
31
|
self._move_to_point = move_to_point
|
|
@@ -35,33 +34,6 @@ class MoveAction(Action):
|
35
|
34
|
# TODO: mettre self._dont_move = True ?
|
36
|
35
|
pass
|
37
|
36
|
|
38
|
|
- def _get_direction_with_pheromones(self, context, object_point):
|
39
|
|
- pheromone_type = context.metas.value.get(PHEROMONE_SEARCHING, self._object_id)
|
40
|
|
- try:
|
41
|
|
- direction = self._get_pheromone_direction_for_point(context, object_point, pheromone_type)
|
42
|
|
- except NoPheromone:
|
43
|
|
- try:
|
44
|
|
- direction = self._get_direction_of_pheromone(context, object_point, pheromone_type)
|
45
|
|
- except NoPheromone:
|
46
|
|
- raise
|
47
|
|
- return direction
|
48
|
|
-
|
49
|
|
- @staticmethod
|
50
|
|
- def _get_pheromone_direction_for_point(context, point, pheromone_type):
|
51
|
|
- return DirectionPheromone.get_direction_for_point(context, point, pheromone_type)
|
52
|
|
-
|
53
|
|
- @staticmethod
|
54
|
|
- def _get_direction_of_pheromone(context, point, pheromone_type):
|
55
|
|
- search_pheromone_in_points = context.get_arround_points_of(point, distance=1)
|
56
|
|
- try:
|
57
|
|
- best_pheromone_direction = DirectionPheromone.get_best_pheromone_direction_in(context,
|
58
|
|
- point,
|
59
|
|
- search_pheromone_in_points,
|
60
|
|
- pheromone_type)
|
61
|
|
- return best_pheromone_direction
|
62
|
|
- except NoPheromone as err:
|
63
|
|
- raise err
|
64
|
|
-
|
65
|
37
|
def _get_random_direction(self, context):
|
66
|
38
|
try:
|
67
|
39
|
blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
|
|
@@ -93,34 +65,17 @@ class MoveAction(Action):
|
93
|
65
|
return context.position_is_penetrable(direction_point)
|
94
|
66
|
|
95
|
67
|
def run(self, obj, context, synergy_manager):
|
96
|
|
- if self._move_to_point is not None and self._move_to_direction != 14: # TODO: il ne faut pas choisir une direction 14.
|
97
|
|
- obj.set_position(self._move_to_point)
|
98
|
|
- context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
|
99
|
|
- context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)
|
100
|
|
- self._appose_pheromone(obj, context)
|
101
|
|
-
|
102
|
|
- # TEST: le temps de tout tester
|
103
|
|
- if self._move_to_point == obj.get_colony().get_start_position() and obj.is_carrying():
|
104
|
|
- obj_transported = obj.get_carried()
|
105
|
|
- obj_transported.set_carried_by(None)
|
106
|
|
- obj.put_carry(obj_transported, (-1, 0, 0))
|
107
|
|
- obj.get_brain().switch_to_mode(MOVE_MODE_EXPLO)
|
108
|
|
- context.metas.collections.add_remove(obj.get_id(),
|
109
|
|
- COL_TRANSPORTER_NOT_CARRYING,
|
110
|
|
- COL_TRANSPORTER_CARRYING)
|
111
|
|
- else:
|
112
|
|
- try:
|
113
|
|
- blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id)
|
114
|
|
- except:
|
115
|
|
- blocked_since = 0
|
|
68
|
+ try:
|
|
69
|
+ self._apply_move(obj, context)
|
|
70
|
+ except ActionAborted:
|
|
71
|
+ blocked_since = context.metas.value.get(BLOCKED_SINCE, self._object_id, allow_empty=True, empty_value=0)
|
116
|
72
|
context.metas.value.set(BLOCKED_SINCE, self._object_id, blocked_since+1)
|
117
|
73
|
|
118
|
|
- @staticmethod
|
119
|
|
- def _appose_pheromone(obj, context):
|
120
|
|
- # TODO: Cette action de pheromone doit etre une surcharge de Move afin d'avoir une Action Move generique.
|
121
|
|
- try:
|
122
|
|
- DirectionPheromone.appose(context,
|
123
|
|
- obj.get_position(),
|
124
|
|
- obj.get_movement_pheromone_gland().get_movement_molecules())
|
125
|
|
- except BestPheromoneHere as best_pheromone_here:
|
126
|
|
- obj.get_brain().set_distance_from_objective(best_pheromone_here.get_best_distance())
|
|
74
|
+ def _apply_move(self, obj, context):
|
|
75
|
+ # TODO: il ne faut pas choisir une direction 14.
|
|
76
|
+ if self._move_to_point is None or self._move_to_direction == 14:
|
|
77
|
+ raise ActionAborted()
|
|
78
|
+
|
|
79
|
+ obj.set_position(self._move_to_point)
|
|
80
|
+ context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)
|
|
81
|
+ context.metas.value.set(BLOCKED_SINCE, self._object_id, 0)
|