Browse Source

place troops with troops xml

Bastien Sevajol 9 months ago
parent
commit
b26758fad5
3 changed files with 54 additions and 2 deletions
  1. 1 0
      opencombat/ai/__init__.py
  2. 39 0
      opencombat/ai/placement.py
  3. 14 2
      run.py

+ 1 - 0
opencombat/ai/__init__.py View File

@@ -0,0 +1 @@
1
+# coding: utf-8

+ 39 - 0
opencombat/ai/placement.py View File

@@ -0,0 +1,39 @@
1
+# coding: utf-8
2
+import typing
3
+
4
+from synergine2.config import Config
5
+
6
+from opencombat.const import SIDE
7
+from opencombat.simulation.base import TileStrategySimulation
8
+
9
+if typing.TYPE_CHECKING:
10
+    from opencombat.simulation.subject import TileSubject
11
+
12
+
13
+class Placement(object):
14
+    """
15
+    Place troops on a map
16
+    """
17
+    def __init__(
18
+        self,
19
+        config: Config,
20
+        simulation: TileStrategySimulation,
21
+    ) -> None:
22
+        self._config = config
23
+        self._simulation = simulation
24
+
25
+    def place(self) -> None:
26
+        # For now it is an extremely simple way to do it
27
+        subject_by_sides = {}  # type: typing.Dict[str, typing.List[TileSubject]]  # nopep8
28
+        for subject in self._simulation.subjects:
29
+            subject_by_sides.setdefault(subject.properties[SIDE], []).append(
30
+                subject,
31
+            )
32
+
33
+        x, y = 0, 0
34
+        for side, subjects in subject_by_sides.items():
35
+            y += 2
36
+            for subject in subjects:
37
+                x += 2
38
+
39
+                subject.position = (x, y)

+ 14 - 2
run.py View File

@@ -11,6 +11,7 @@ from synergine2.core import Core
11 11
 from synergine2.cycle import CycleManager
12 12
 from synergine2.terminals import TerminalManager
13 13
 
14
+from opencombat.ai.placement import Placement
14 15
 from opencombat.simulation.base import TileStrategySimulation
15 16
 from opencombat.simulation.base import TileStrategySubjects
16 17
 from opencombat.state import StateConstructorBuilder
@@ -48,6 +49,7 @@ def main(
48 49
 
49 50
     simulation = TileStrategySimulation(config, map_file_path=map_file_path)
50 51
     subjects = TileStrategySubjects(simulation=simulation)
52
+    simulation.subjects = subjects
51 53
 
52 54
     if state_file_path:
53 55
         state_loader_builder = StateConstructorBuilder(config, simulation)
@@ -58,10 +60,11 @@ def main(
58 60
     elif troops_file_path:
59 61
         troop_loader_builder = TroopConstructorBuilder(config, simulation)
60 62
         troop_loader = troop_loader_builder.get_troop_loader()
63
+        placement = Placement(config, simulation)
64
+
61 65
         troops = troop_loader.get_troop(troops_file_path)
62 66
         subjects.extend(troops.subjects)
63
-
64
-    simulation.subjects = subjects
67
+        placement.place()
65 68
 
66 69
     core = Core(
67 70
         config=config,
@@ -114,6 +117,15 @@ if __name__ == '__main__':
114 117
         )
115 118
         exit(1)
116 119
 
120
+    if args.troops and not args.placement:
121
+        print(
122
+            'Cannot load troops "{}" without activate placement mode.'.format(
123
+                args.state,
124
+            ),
125
+            file=sys.stderr,
126
+        )
127
+        exit(1)
128
+
117 129
     main(
118 130
         args.map_dir_path,
119 131
         seed_value=args.seed,