Browse Source

COL system compatibility

Bastien Sevajol 9 years ago
parent
commit
5c3ff17601

+ 8 - 1
intelligine/cst.py View File

@@ -12,4 +12,11 @@ TRANSPORTABLE = IncrementedNamedInt.get('intelligine.transportable')
12 12
 TRANSPORTER = IncrementedNamedInt.get('intelligine.transporter')
13 13
 CARRYING = IncrementedNamedInt.get('intelligine.carrying')
14 14
 CANT_CARRY_STILL = IncrementedNamedInt.get('intelligine.cantcarry.still')
15
-CANT_PUT_STILL = IncrementedNamedInt.get('intelligine.cantput.still')
15
+CANT_PUT_STILL = IncrementedNamedInt.get('intelligine.cantput.still')
16
+ACTION_DIE = IncrementedNamedInt.get('intelligine.basebug.action.die')
17
+
18
+COL_ALIVE = IncrementedNamedInt.get('intelligine.col.alive')
19
+COL_WALKER = IncrementedNamedInt.get('intelligine.col.walker')
20
+COL_FIGHTER = IncrementedNamedInt.get('intelligine.col.walker')
21
+COL_TRANSPORTER_CARRYING = IncrementedNamedInt.get('intelligine.col.transporter_carrying')
22
+COL_TRANSPORTER_NOT_CARRYING = IncrementedNamedInt.get('intelligine.col.transporter_not_carrying')

+ 10 - 9
intelligine/display/pygame/visualisation.py View File

@@ -8,7 +8,6 @@ from intelligine.sandbox.colored.GreenAnt import GreenAnt
8 8
 from intelligine.synergy.object.Rock import Rock
9 9
 from intelligine.synergy.object.ant.Egg import Egg
10 10
 from os import getcwd
11
-from synergine.metas import metas
12 11
 from intelligine.cst import PREVIOUS_DIRECTION
13 12
 
14 13
 # TODO: Analyser les procedes ici pour proposer des outils dans le framework
@@ -30,38 +29,40 @@ directions_red_ant = DirectionnedImage(red_ant)
30 29
 directions_blue_ant = DirectionnedImage(blue_ant)
31 30
 directions_green_ant = DirectionnedImage(green_ant)
32 31
 
33
-def bug_direction(bug):
32
+# TODO: context donne au callbacks
33
+
34
+def bug_direction(bug, context):
34 35
     if bug.get_life_points() <= 0:
35 36
         return dead_ant
36 37
     try:
37
-        previous_direction = metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
38
+        previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
38 39
     except KeyError:
39 40
         previous_direction = 14
40 41
     return directions_ant.get_for_direction(previous_direction)
41 42
 
42
-def red_ant_direction(bug):
43
+def red_ant_direction(bug, context):
43 44
     if bug.get_life_points() <= 0:
44 45
         return dead_red_ant
45 46
     try:
46
-        previous_direction = metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
47
+        previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
47 48
     except KeyError:
48 49
         previous_direction = 14
49 50
     return directions_red_ant.get_for_direction(previous_direction)
50 51
 
51
-def blue_ant_direction(bug):
52
+def blue_ant_direction(bug, context):
52 53
     if bug.get_life_points() <= 0:
53 54
         return dead_blue_ant
54 55
     try:
55
-        previous_direction = metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
56
+        previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
56 57
     except KeyError:
57 58
         previous_direction = 14
58 59
     return directions_blue_ant.get_for_direction(previous_direction)
59 60
 
60
-def green_ant_direction(bug):
61
+def green_ant_direction(bug, context):
61 62
     if bug.get_life_points() <= 0:
62 63
         return dead_green_ant
63 64
     try:
64
-        previous_direction = metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
65
+        previous_direction = context.metas.value.get(PREVIOUS_DIRECTION, bug.get_id())
65 66
     except KeyError:
66 67
         previous_direction = 14
67 68
     return directions_green_ant.get_for_direction(previous_direction)

+ 4 - 5
intelligine/sandbox/colored/RedColonyConfiguration.py View File

@@ -1,7 +1,6 @@
1 1
 from intelligine.synergy.ColonyConfiguration import ColonyConfiguration
2 2
 from intelligine.sandbox.colored.RedAnt import RedAnt
3 3
 from intelligine.synergy.object.ant.Egg import Egg
4
-from synergine.metas import metas
5 4
 from intelligine.cst import COLONY
6 5
 
7 6
 
@@ -10,16 +9,16 @@ class RedColonyConfiguration(ColonyConfiguration):
10 9
     _start_position = (0, 20, 70)
11 10
     _ant_class = RedAnt
12 11
 
13
-    def get_start_objects(self, collection):
14
-        objects = super().get_start_objects(collection)
12
+    def get_start_objects(self, collection, context):
13
+        objects = super().get_start_objects(collection, context)
15 14
 
16 15
         for x in range(50):
17 16
           for y in range(1, 50):
18 17
             if x % 3 == 0 and y % 3 == 0:
19
-              egg = Egg()
18
+              egg = Egg(context)
20 19
               egg.set_position((0, 1+x, 50+y))
21 20
               # TODO: Ce COLONY doit devenir un truc automatise au niveau de la collection (qd get_object)
22
-              metas.value.set(COLONY, egg.get_id(), collection.get_id())
21
+              context.metas.value.set(COLONY, egg.get_id(), collection.get_id())
23 22
               objects.append(egg)
24 23
 
25 24
         return objects

+ 3 - 4
intelligine/synergy/ColonyConfiguration.py View File

@@ -1,6 +1,5 @@
1 1
 from synergine.synergy.collection.Configuration import Configuration
2 2
 from intelligine.synergy.object.ant.Ant import Ant
3
-from synergine.metas import metas
4 3
 from intelligine.cst import ALIVE, COLONY
5 4
 
6 5
 
@@ -9,12 +8,12 @@ class ColonyConfiguration(Configuration):
9 8
     _start_position = (0, 20, 20)
10 9
     _ant_class = Ant
11 10
 
12
-    def get_start_objects(self, collection):
11
+    def get_start_objects(self, collection, context):
13 12
 
14 13
       ants = []
15 14
       for i in range(20):
16
-          ant = self._ant_class()
17
-          metas.value.set(COLONY, ant.get_id(), collection.get_id())
15
+          ant = self._ant_class(context)
16
+          context.metas.value.set(COLONY, ant.get_id(), collection.get_id())
18 17
           ant.set_position(self._start_position)
19 18
           ants.append(ant)
20 19
 

+ 2 - 2
intelligine/synergy/RocksConfiguration.py View File

@@ -4,7 +4,7 @@ from intelligine.synergy.object.Rock import Rock
4 4
 
5 5
 class RocksConfiguration(Configuration):
6 6
 
7
-    def get_start_objects(self, collection):
7
+    def get_start_objects(self, collection, context):
8 8
 
9 9
       rocks = []
10 10
       rocks_positions = []
@@ -29,7 +29,7 @@ class RocksConfiguration(Configuration):
29 29
       rocks_positions.append((0, 100, 100))
30 30
 
31 31
       for rock_position in rocks_positions:
32
-          rock = Rock()
32
+          rock = Rock(context)
33 33
           rock.set_position(rock_position)
34 34
           rocks.append(rock)
35 35
 

+ 23 - 0
intelligine/synergy/Simulation.py View File

@@ -1,9 +1,32 @@
1 1
 from synergine.synergy.Simulation import Simulation as BaseSimulation
2 2
 from xyzworld.cst import POSITION, POSITIONS
3
+#from intelligine.synergy.event.attack.NearAttackableAction import NearAttackableAction
4
+from intelligine.synergy.event.transport.PutableAction import PutableAction
5
+from intelligine.synergy.event.transport.TakeableAction import TakeableAction
6
+from intelligine.synergy.event.move.MoveAction import MoveAction
7
+from intelligine.synergy.event.CycleAction import CycleAction
8
+from intelligine.cst import COL_TRANSPORTER_CARRYING, COL_TRANSPORTER_NOT_CARRYING, \
9
+    COL_WALKER, ACTION_DIE, COL_ALIVE
3 10
 
11
+# TODO: Mettre ailleurs ?
12
+def bug_die(bug, context):
13
+    context.metas.collections.remove_list(bug.get_id(),
14
+                                          [COL_TRANSPORTER_CARRYING, \
15
+                                           COL_TRANSPORTER_NOT_CARRYING, \
16
+                                           COL_WALKER, \
17
+                                           COL_ALIVE],
18
+                                          allow_not_in=True)
19
+    context.metas.states.remove_list(bug.get_id(), [ALIVE, ATTACKABLE])
4 20
 
5 21
 class Simulation(BaseSimulation):
6 22
 
23
+    def connect_actions_signals(self, Signals):
24
+        Signals.signal(PutableAction).connect(lambda obj, context: \
25
+            context.metas.collections.add_remove(obj.get_id(), COL_TRANSPORTER_NOT_CARRYING, COL_TRANSPORTER_CARRYING))
26
+        Signals.signal(TakeableAction).connect(lambda obj, context: \
27
+            context.metas.collections.add_remove(obj.get_id(), COL_TRANSPORTER_CARRYING, COL_TRANSPORTER_NOT_CARRYING))
28
+        Signals.signal(ACTION_DIE).connect(lambda obj, context: bug_die(obj, context))
29
+
7 30
     def end_cycle(self, context):
8 31
         if context.get_cycle() % 100 is 0:
9 32
             context.metas.list.clean(POSITIONS)

+ 2 - 3
intelligine/synergy/event/CycleEvent.py View File

@@ -1,11 +1,10 @@
1 1
 from synergine.synergy.event.Event import Event
2
-from intelligine.cst import CANT_CARRY_STILL, TRANSPORTER, ALIVE
2
+from intelligine.cst import COL_ALIVE # TODO: Crer une col TRANPORTER ?
3 3
 
4 4
 
5 5
 class CycleEvent(Event):
6 6
 
7
-    def concern(self, object_id, context):
8
-        return context.metas.states.have_list(object_id, [TRANSPORTER, ALIVE])
7
+    concern = COL_ALIVE
9 8
 
10 9
     def _object_match(self, object_id, context, parameters):
11 10
         return True

+ 4 - 7
intelligine/synergy/event/attack/NearAttackableAction.py View File

@@ -1,7 +1,8 @@
1 1
 from synergine.synergy.event.Action import Action
2 2
 from intelligine.synergy.event.attack.NearAttackableEvent import NearAttackableEvent
3 3
 from random import randint
4
-from intelligine.cst import ALIVE, ATTACKABLE
4
+from intelligine.cst import ALIVE, ATTACKABLE, ACTION_DIE
5
+from synergine.core.Signals import Signals
5 6
 
6 7
 
7 8
 class NearAttackableAction(Action):
@@ -15,13 +16,9 @@ class NearAttackableAction(Action):
15 16
         pass
16 17
 
17 18
     def run(self, obj, collection, context, synergy_manager):
18
-        # TODO: TEST
19
+        # TODO: reprendre ?
19 20
         for obj_id_attackable in self._parameters['objects_ids_attackable']:
20 21
             obj_attackable = synergy_manager.get_map().get_object(obj_id_attackable)
21 22
             obj_attackable.hurted(randint(0, 2))
22 23
             if obj_attackable.get_life_points() <= 0:
23
-                try:
24
-                    # TODO: Doit etre dans l'objet, lui sais cz qu'il perd comme etat
25
-                    context.metas.states.remove_list(obj_id_attackable, [ALIVE, ATTACKABLE])
26
-                except ValueError:  # Ant maybed killed by other ant at same turn
27
-                    pass
24
+                Signals.signal(ACTION_DIE).send(obj=obj_attackable, context=context)

+ 2 - 3
intelligine/synergy/event/attack/NearAttackableEvent.py View File

@@ -1,12 +1,11 @@
1 1
 from synergine.synergy.event.Event import Event
2 2
 from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
3
-from intelligine.cst import ATTACKER, ATTACKABLE, COLONY, ALIVE
3
+from intelligine.cst import ATTACKABLE, COLONY, COL_FIGHTER
4 4
 
5 5
 
6 6
 class NearAttackableEvent(Event):
7 7
 
8
-    def concern(self, object_id, context):
9
-        return context.metas.states.have_list(object_id, [ATTACKER, ALIVE])
8
+    concern = COL_FIGHTER
10 9
 
11 10
     def __init__(self, actions):
12 11
         super().__init__(actions)

+ 2 - 3
intelligine/synergy/event/move/MoveEvent.py View File

@@ -1,12 +1,11 @@
1 1
 from synergine.synergy.event.Event import Event
2 2
 from synergine.core.simulation.mechanism.Mechanism import Mechanism
3
-from intelligine.cst import ALIVE, WALKER
3
+from intelligine.cst import COL_WALKER
4 4
 
5 5
 
6 6
 class MoveEvent(Event):
7 7
 
8
-    def concern(self, object_id, context):
9
-        return context.metas.states.have_list(object_id, [ALIVE, WALKER])
8
+    concern = COL_WALKER
10 9
 
11 10
     def __init__(self, actions):
12 11
         super().__init__(actions)

+ 7 - 4
intelligine/synergy/event/transport/PutableEvent.py View File

@@ -1,10 +1,13 @@
1 1
 from intelligine.synergy.event.transport.TakeableEvent import TakeableEvent
2 2
 from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
3
-from intelligine.cst import TRANSPORTER, ALIVE, CARRYING, CANT_PUT_STILL
3
+from intelligine.cst import CANT_PUT_STILL, COL_TRANSPORTER_CARRYING
4 4
 
5 5
 
6 6
 class PutableEvent(TakeableEvent):
7 7
 
8
-    def concern(self, object_id, context):
9
-        return context.metas.states.have_list(object_id, [TRANSPORTER, ALIVE, CARRYING]) \
10
-               and not context.metas.value.get(CANT_PUT_STILL, object_id, allow_empty=True)
8
+    concern = COL_TRANSPORTER_CARRYING
9
+
10
+    def _object_match(self, object_id, context, parameters={}):
11
+        if context.metas.value.get(CANT_PUT_STILL, object_id, allow_empty=True):
12
+            return False
13
+        return super()._object_match(object_id, context, parameters)

+ 10 - 8
intelligine/synergy/event/transport/TakeableAction.py View File

@@ -1,6 +1,7 @@
1 1
 from synergine.synergy.event.Action import Action
2 2
 from intelligine.synergy.event.transport.TakeableEvent import TakeableEvent
3 3
 from intelligine.cst import CANT_PUT_STILL
4
+from synergine.synergy.event.exception.ActionAborted import ActionAborted
4 5
 
5 6
 
6 7
 class TakeableAction(Action):
@@ -19,11 +20,12 @@ class TakeableAction(Action):
19 20
         # ?! Comment gerer lorsque deux obj vont vouloir transporter le meme objet ? process !
20 21
         obj_id_transportable = self._parameters['objects_ids_transportable'][0]
21 22
         obj_transportable = synergy_manager.get_map().get_object(obj_id_transportable)
22
-        if not obj_transportable.is_carried():
23
-            try:
24
-                obj_transportable.set_carried_by(obj)
25
-                obj.carry(obj_transportable)
26
-                context.metas.value.set(CANT_PUT_STILL, obj.get_id(), 5)
27
-            except ValueError: # Une NotCarriableError serais pus approprie
28
-                # TODO: tmp? Si on as pas pu le porter c'est qu'il vient d'etre porte par une autre.
29
-                pass
23
+        if obj_transportable.is_carried():
24
+            raise ActionAborted()
25
+        try:
26
+            obj_transportable.set_carried_by(obj)
27
+            obj.carry(obj_transportable)
28
+            context.metas.value.set(CANT_PUT_STILL, obj.get_id(), 5)
29
+        except ValueError: # Une NotCarriableError serais pus approprie
30
+            # TODO: tmp? Si on as pas pu le porter c'est qu'il vient d'etre porte par une autre.
31
+            pass

+ 4 - 5
intelligine/synergy/event/transport/TakeableEvent.py View File

@@ -1,20 +1,19 @@
1 1
 from synergine.synergy.event.Event import Event
2 2
 from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
3
-from intelligine.cst import TRANSPORTABLE, TRANSPORTER, ALIVE, CARRYING, CANT_CARRY_STILL
3
+from intelligine.cst import TRANSPORTABLE, CANT_CARRY_STILL, COL_TRANSPORTER_NOT_CARRYING
4 4
 
5 5
 
6 6
 class TakeableEvent(Event):
7 7
 
8
-    def concern(self, object_id, context):
9
-        return context.metas.states.have_list(object_id, [TRANSPORTER, ALIVE]) and \
10
-               context.metas.states.dont_have(object_id, CARRYING) and not \
11
-               context.metas.value.get(CANT_CARRY_STILL, object_id, allow_empty=True) # TODO: si COLS: ca plus dans concern mais dans object_match
8
+    concern = COL_TRANSPORTER_NOT_CARRYING
12 9
 
13 10
     def __init__(self, actions):
14 11
         super().__init__(actions)
15 12
         self._mechanism = ArroundMechanism
16 13
 
17 14
     def _object_match(self, object_id, context, parameters={}):
15
+        if context.metas.value.get(CANT_CARRY_STILL, object_id, allow_empty=True):
16
+            return False
18 17
         # TODO: Nettoyer (refact possible sur ces objets ont tel states, comme dans concern)
19 18
         for obj_near_id in parameters['objects_ids_near']:
20 19
             if context.metas.states.have(obj_near_id, TRANSPORTABLE):

+ 7 - 7
intelligine/synergy/object/BaseBug.py View File

@@ -1,13 +1,13 @@
1 1
 from xyzworld.SynergyObject import SynergyObject as XyzSynergyObject
2
-from synergine.metas import metas
3
-from intelligine.cst import ALIVE, ATTACKABLE, TRANSPORTABLE
2
+from intelligine.cst import ALIVE, ATTACKABLE, TRANSPORTABLE, COL_ALIVE
4 3
 
5 4
 
6 5
 class BaseBug(XyzSynergyObject):
7 6
 
8
-    def __init__(self):
9
-        super().__init__()
10
-        metas.states.add_list(self.get_id(), [ALIVE, ATTACKABLE])
7
+    def __init__(self, context):
8
+        super().__init__(context)
9
+        context.metas.states.add_list(self.get_id(), [ALIVE, ATTACKABLE])
10
+        context.metas.collections.add(self.get_id(), COL_ALIVE)
11 11
         self._life_points = 10
12 12
         self._carried_by = None
13 13
 
@@ -21,11 +21,11 @@ class BaseBug(XyzSynergyObject):
21 21
         if obj is not None:
22 22
             assert self._carried_by is None
23 23
             self._carried_by = obj
24
-            metas.states.remove(self.get_id(), TRANSPORTABLE)
24
+            self._context.metas.states.remove(self.get_id(), TRANSPORTABLE)
25 25
         else:
26 26
             assert self._carried_by is not None
27 27
             self._carried_by = None
28
-            metas.states.add(self.get_id(), TRANSPORTABLE)
28
+            self._context.metas.states.add(self.get_id(), TRANSPORTABLE)
29 29
 
30 30
     def is_carried(self):
31 31
         if self._carried_by:

+ 5 - 5
intelligine/synergy/object/Bug.py View File

@@ -1,10 +1,10 @@
1 1
 from intelligine.synergy.object.BaseBug import BaseBug
2
-from synergine.metas import metas
3
-from intelligine.cst import WALKER
2
+from intelligine.cst import WALKER, COL_WALKER
4 3
 
5 4
 
6 5
 class Bug(BaseBug):
7 6
 
8
-    def __init__(self):
9
-        super().__init__()
10
-        metas.states.add(self.get_id(), WALKER)
7
+    def __init__(self, context):
8
+        super().__init__(context)
9
+        context.metas.states.add(self.get_id(), WALKER)
10
+        context.metas.collections.add(self.get_id(), COL_WALKER)

+ 4 - 5
intelligine/synergy/object/Egg.py View File

@@ -1,11 +1,10 @@
1 1
 from intelligine.synergy.object.BaseBug import BaseBug
2
-from synergine.metas import metas
3
-from intelligine.cst import ALIVE, TRANSPORTABLE
2
+from intelligine.cst import TRANSPORTABLE
4 3
 
5 4
 
6 5
 class Egg(BaseBug):
7 6
 
8
-    def __init__(self):
9
-        super().__init__()
10
-        metas.states.add(self.get_id(), TRANSPORTABLE)
7
+    def __init__(self, context):
8
+        super().__init__(context)
9
+        context.metas.states.add(self.get_id(), TRANSPORTABLE)
11 10
         self._life_points = 1

+ 3 - 4
intelligine/synergy/object/Rock.py View File

@@ -1,10 +1,9 @@
1 1
 from xyzworld.SynergyObject import SynergyObject as XyzSynergyObject
2
-from synergine.metas import metas
3 2
 from intelligine.cst import IMPENETRABLE
4 3
 
5 4
 
6 5
 class Rock(XyzSynergyObject):
7 6
 
8
-    def __init__(self):
9
-        super().__init__()
10
-        metas.states.add(self.get_id(), IMPENETRABLE)
7
+    def __init__(self, context):
8
+        super().__init__(context)
9
+        context.metas.states.add(self.get_id(), IMPENETRABLE)

+ 8 - 7
intelligine/synergy/object/ant/Ant.py View File

@@ -1,19 +1,20 @@
1 1
 from intelligine.synergy.object.Bug import Bug
2
-from synergine.metas import metas
3
-from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER
2
+from intelligine.cst import CARRYING, TRANSPORTER, ATTACKER, \
3
+    COL_TRANSPORTER_NOT_CARRYING, COL_FIGHTER
4 4
 
5 5
 
6 6
 class Ant(Bug):
7 7
 
8
-    def __init__(self):
9
-        super().__init__()
10
-        metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
8
+    def __init__(self, context):
9
+        super().__init__(context)
10
+        context.metas.states.add_list(self.get_id(), [TRANSPORTER, ATTACKER])
11
+        context.metas.collections.add_list(self.get_id(), [COL_TRANSPORTER_NOT_CARRYING, COL_FIGHTER])
11 12
         self._carried = []
12 13
 
13 14
     def put_carry(self, obj):
14 15
         self._carried.remove(obj)
15 16
         obj.set_position(self.get_position())
16
-        metas.states.remove(self.get_id(), CARRYING)
17
+        self._context.metas.states.remove(self.get_id(), CARRYING)
17 18
 
18 19
     def get_carried(self):
19 20
         # TODO: cas ou plusieurs ?
@@ -21,7 +22,7 @@ class Ant(Bug):
21 22
 
22 23
     def carry(self, obj):
23 24
         self._carried.append(obj)
24
-        metas.states.add(self.get_id(), CARRYING)
25
+        self._context.metas.states.add(self.get_id(), CARRYING)
25 26
 
26 27
     def is_carrying(self):
27 28
         if len(self._carried):