Browse Source

attack event and action

Bastien Sevajol 9 years ago
parent
commit
feb855fb94

+ 9 - 1
config.py View File

@@ -4,6 +4,14 @@ from intelligine.display.Pygame import Pygame
4 4
 from intelligine.display.pygame.visualisation import visualisation as pygame_visualisation
5 5
 from intelligine.sandbox.redblue.red_blue_colonys import collections
6 6
 
7
+"""
8
+ TODO:
9
+ * AttackAction :: comment choisir entre les actions ?
10
+ * TakeAction, PutAction, Object Egg
11
+ * Plusieurs objets au mm endroit; Cinq oeuf => dessein de cinq oeuf; etc (image dynamique (param max_supperposer ?)
12
+ * 3d
13
+"""
14
+
7 15
 config = {
8 16
     'app': {
9 17
         'name': 'StigEngine',
@@ -12,7 +20,7 @@ config = {
12 20
         }
13 21
     },
14 22
     'engine': {
15
-        'fpsmax': 5,
23
+        'fpsmax': 225,
16 24
         'debug': {
17 25
             'mainprocess': True,
18 26
             'cycles': -1

+ 4 - 1
intelligine/cst.py View File

@@ -3,4 +3,7 @@ from synergine.lib.eint import IncrementedNamedInt
3 3
 ALIVE = IncrementedNamedInt.get('intelligine.alive')
4 4
 IMPENETRABLE = IncrementedNamedInt.get('intelligine.impenetrable')
5 5
 PREVIOUS_DIRECTION = IncrementedNamedInt.get('intelligine.previous_direction')
6
-BLOCKED_SINCE = IncrementedNamedInt.get('intelligine.blocked_since')
6
+BLOCKED_SINCE = IncrementedNamedInt.get('intelligine.blocked_since')
7
+ATTACKABLE = IncrementedNamedInt.get('intelligine.attackable')
8
+ATTACKER = IncrementedNamedInt.get('intelligine.attacker')
9
+COLONY = IncrementedNamedInt.get('intelligine.colony')

+ 2 - 1
intelligine/synergy/Colony.py View File

@@ -1,5 +1,6 @@
1 1
 from synergine.synergy.collection.SynergyCollection import SynergyCollection
2 2
 from intelligine.synergy.event.move.MoveAction import MoveAction
3
+from intelligine.synergy.event.attack.NearAttackableAction import NearAttackableAction
3 4
 
4 5
 
5 6
 class Colony(SynergyCollection):
@@ -7,4 +8,4 @@ class Colony(SynergyCollection):
7 8
 
8 9
     def __init__(self, configuration):
9 10
         super().__init__(configuration)
10
-        self._actions = [MoveAction]
11
+        self._actions = [MoveAction, NearAttackableAction]

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

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

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

@@ -7,7 +7,7 @@ from synergine.synergy.Simulation import Simulation
7 7
 
8 8
 class RocksConfiguration(Configuration):
9 9
 
10
-    def get_start_objects(self):
10
+    def get_start_objects(self, collection):
11 11
 
12 12
       rocks = []
13 13
 

+ 28 - 0
intelligine/synergy/event/attack/NearAttackableAction.py View File

@@ -0,0 +1,28 @@
1
+from synergine.synergy.event.Action import Action
2
+from intelligine.synergy.event.attack.NearAttackableEvent import NearAttackableEvent
3
+from random import randint
4
+from intelligine.synergy.Simulation import Simulation
5
+from intelligine.cst import ALIVE, ATTACKABLE
6
+
7
+
8
+class NearAttackableAction(Action):
9
+
10
+    _listen = NearAttackableEvent
11
+
12
+    def __init__(self, object_id, parameters):
13
+        super().__init__(object_id, parameters)
14
+
15
+    def prepare(self, context):
16
+        pass
17
+
18
+    def run(self, obj, collection, context, synergy_manager):
19
+        # TODO: TEST
20
+        for obj_id_attackable in self._parameters['objects_ids_attackable']:
21
+            obj_attackable = synergy_manager.get_map().get_object(obj_id_attackable)
22
+            obj_attackable.hurted(randint(0, 2))
23
+            if obj_attackable.get_life_points() <= 0:
24
+                try:
25
+                    context.metas.list.remove(Simulation.STATE, obj_id_attackable, ALIVE)
26
+                    context.metas.list.remove(Simulation.STATE, obj_id_attackable, ATTACKABLE)
27
+                except ValueError:  # Ant maybed killed by other ant at same turn
28
+                    pass

+ 28 - 0
intelligine/synergy/event/attack/NearAttackableEvent.py View File

@@ -0,0 +1,28 @@
1
+from synergine.synergy.event.Event import Event
2
+from xyzworld.mechanism.ArroundMechanism import ArroundMechanism
3
+from synergine.synergy.Simulation import Simulation
4
+from intelligine.cst import ATTACKER, ATTACKABLE, COLONY, ALIVE
5
+
6
+
7
+class NearAttackableEvent(Event):
8
+
9
+    def concern(self, object_id, context):
10
+        return context.metas.list.have(Simulation.STATE, object_id, ATTACKER) and \
11
+               context.metas.list.have(Simulation.STATE, object_id, ALIVE)
12
+
13
+    def __init__(self, actions):
14
+        super().__init__(actions)
15
+        self._mechanism = ArroundMechanism
16
+
17
+    def _object_match(self, object_id, context, parameters={}):
18
+      # TODO: nettoyer
19
+        obj_colony_id = context.metas.value.get(COLONY, object_id)
20
+        for obj_near_id in parameters['objects_ids_near']:
21
+            if context.metas.list.have(Simulation.STATE, obj_near_id, ATTACKABLE):
22
+                if obj_colony_id != context.metas.value.get(COLONY, obj_near_id):
23
+                    if 'objects_ids_attackable' not in parameters:
24
+                        parameters['objects_ids_attackable'] = []
25
+                    parameters['objects_ids_attackable'].append(obj_near_id)
26
+        if 'objects_ids_attackable' not in parameters:
27
+            return False
28
+        return True

+ 1 - 1
intelligine/synergy/event/move/MoveAction.py View File

@@ -58,7 +58,7 @@ class MoveAction(Action):
58 58
             return False
59 59
         return True
60 60
 
61
-    def run(self, obj, collection, context):
61
+    def run(self, obj, collection, context, synergy_manager):
62 62
         if self._move_to_point is not None:
63 63
             obj.set_position(self._move_to_point)
64 64
             context.metas.value.set(PREVIOUS_DIRECTION, self._object_id, self._move_to_direction)

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

@@ -1,11 +1,13 @@
1 1
 from synergine.synergy.event.Event import Event
2 2
 from synergine.core.simulation.mechanism.Mechanism import Mechanism
3
+from intelligine.synergy.Simulation import Simulation
4
+from intelligine.cst import ALIVE
3 5
 
4 6
 
5 7
 class MoveEvent(Event):
6 8
 
7 9
     def concern(self, object_id, context):
8
-        return True
10
+        return context.metas.list.have(Simulation.STATE, object_id, ALIVE)
9 11
 
10 12
     def __init__(self, actions):
11 13
         super().__init__(actions)

+ 17 - 1
intelligine/synergy/object/Bug.py View File

@@ -1,4 +1,20 @@
1 1
 from xyzworld.SynergyObject import SynergyObject as XyzSynergyObject
2
+from synergine.metas import metas
3
+from intelligine.cst import ALIVE, ATTACKABLE, ATTACKER
4
+from synergine.synergy.Simulation import Simulation
5
+
2 6
 
3 7
 class Bug(XyzSynergyObject):
4
-  pass
8
+
9
+    def __init__(self):
10
+        super().__init__()
11
+        metas.list.add(Simulation.STATE, self.get_id(), ALIVE)
12
+        metas.list.add(Simulation.STATE, self.get_id(), ATTACKER)
13
+        metas.list.add(Simulation.STATE, self.get_id(), ATTACKABLE)
14
+        self._life_points = 10
15
+
16
+    def hurted(self, points):
17
+        self._life_points -= points
18
+
19
+    def get_life_points(self):
20
+        return self._life_points