Browse Source

place troops with troops xml

Bastien Sevajol 6 years 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

1
+# coding: utf-8

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

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
 from synergine2.cycle import CycleManager
11
 from synergine2.cycle import CycleManager
12
 from synergine2.terminals import TerminalManager
12
 from synergine2.terminals import TerminalManager
13
 
13
 
14
+from opencombat.ai.placement import Placement
14
 from opencombat.simulation.base import TileStrategySimulation
15
 from opencombat.simulation.base import TileStrategySimulation
15
 from opencombat.simulation.base import TileStrategySubjects
16
 from opencombat.simulation.base import TileStrategySubjects
16
 from opencombat.state import StateConstructorBuilder
17
 from opencombat.state import StateConstructorBuilder
48
 
49
 
49
     simulation = TileStrategySimulation(config, map_file_path=map_file_path)
50
     simulation = TileStrategySimulation(config, map_file_path=map_file_path)
50
     subjects = TileStrategySubjects(simulation=simulation)
51
     subjects = TileStrategySubjects(simulation=simulation)
52
+    simulation.subjects = subjects
51
 
53
 
52
     if state_file_path:
54
     if state_file_path:
53
         state_loader_builder = StateConstructorBuilder(config, simulation)
55
         state_loader_builder = StateConstructorBuilder(config, simulation)
58
     elif troops_file_path:
60
     elif troops_file_path:
59
         troop_loader_builder = TroopConstructorBuilder(config, simulation)
61
         troop_loader_builder = TroopConstructorBuilder(config, simulation)
60
         troop_loader = troop_loader_builder.get_troop_loader()
62
         troop_loader = troop_loader_builder.get_troop_loader()
63
+        placement = Placement(config, simulation)
64
+
61
         troops = troop_loader.get_troop(troops_file_path)
65
         troops = troop_loader.get_troop(troops_file_path)
62
         subjects.extend(troops.subjects)
66
         subjects.extend(troops.subjects)
63
-
64
-    simulation.subjects = subjects
67
+        placement.place()
65
 
68
 
66
     core = Core(
69
     core = Core(
67
         config=config,
70
         config=config,
114
         )
117
         )
115
         exit(1)
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
     main(
129
     main(
118
         args.map_dir_path,
130
         args.map_dir_path,
119
         seed_value=args.seed,
131
         seed_value=args.seed,