run.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # coding: utf-8
  2. import argparse
  3. import logging
  4. import sys
  5. from random import seed
  6. from synergine2.log import get_default_logger
  7. from synergine2.config import Config
  8. from synergine2_cocos2d.util import get_map_file_path_from_dir
  9. from synergine2.core import Core
  10. from synergine2.cycle import CycleManager
  11. from synergine2.terminals import TerminalManager
  12. from opencombat.ai.placement import Placement
  13. from opencombat.simulation.base import TileStrategySimulation
  14. from opencombat.simulation.base import TileStrategySubjects
  15. from opencombat.state import StateConstructorBuilder
  16. from opencombat.strategy.troops import TroopConstructorBuilder
  17. from opencombat.terminal.base import CocosTerminal
  18. def main(
  19. map_dir_path: str,
  20. seed_value: int=None,
  21. state_file_path: str=None,
  22. troops_file_path: str=None,
  23. state_save_dir: str='.',
  24. placement_mode: bool = False,
  25. ):
  26. assert not (state_file_path and troops_file_path),\
  27. 'Do not provide troops file when state file given'
  28. if seed_value is not None:
  29. seed(seed_value)
  30. config = Config()
  31. config.load_yaml('config.yaml')
  32. # Runtime config
  33. config['_runtime'] = {}
  34. config['_runtime']['state_save_dir'] = state_save_dir
  35. config['_runtime']['placement_mode'] = placement_mode
  36. config['_runtime']['map_dir_path'] = map_dir_path
  37. level = logging.getLevelName(config.resolve('global.logging_level', 'ERROR'))
  38. logger = get_default_logger(level=level)
  39. map_file_path = get_map_file_path_from_dir(map_dir_path)
  40. simulation = TileStrategySimulation(config, map_file_path=map_file_path)
  41. subjects = TileStrategySubjects(simulation=simulation)
  42. simulation.subjects = subjects
  43. if state_file_path:
  44. state_loader_builder = StateConstructorBuilder(config, simulation)
  45. state_loader = state_loader_builder.get_state_loader()
  46. state = state_loader.get_state(state_file_path)
  47. subjects.extend(state.subjects)
  48. elif troops_file_path:
  49. troop_loader_builder = TroopConstructorBuilder(config, simulation)
  50. troop_loader = troop_loader_builder.get_troop_loader()
  51. placement = Placement(config, simulation)
  52. troops = troop_loader.get_troop(troops_file_path)
  53. subjects.extend(troops.subjects)
  54. placement.place()
  55. core = Core(
  56. config=config,
  57. simulation=simulation,
  58. cycle_manager=CycleManager(
  59. config=config,
  60. simulation=simulation,
  61. ),
  62. terminal_manager=TerminalManager(
  63. config=config,
  64. terminals=[CocosTerminal(
  65. config,
  66. asynchronous=False,
  67. map_dir_path=map_dir_path,
  68. )]
  69. ),
  70. cycles_per_seconds=1 / config.resolve('core.cycle_duration'),
  71. )
  72. core.run()
  73. if __name__ == '__main__':
  74. parser = argparse.ArgumentParser(
  75. description='Run a map'
  76. )
  77. parser.add_argument('map_dir_path', help='map directory path')
  78. parser.add_argument('--seed', dest='seed', default=None)
  79. parser.add_argument('--troops', dest='troops', default=None)
  80. parser.add_argument('--state', dest='state', default=None)
  81. parser.add_argument(
  82. '--state-save-dir',
  83. dest='state_save_dir',
  84. default='.',
  85. )
  86. parser.add_argument(
  87. '--placement',
  88. dest='placement',
  89. action='store_true',
  90. )
  91. args = parser.parse_args()
  92. if args.troops and args.state:
  93. print(
  94. 'Cannot load state "{}" because you provide troops file "{}". '
  95. 'You must provide only one of them.'.format(
  96. args.state,
  97. args.troops,
  98. ),
  99. file=sys.stderr,
  100. )
  101. exit(1)
  102. if args.troops and not args.placement:
  103. print(
  104. 'Cannot load troops "{}" without activate placement mode.'.format(
  105. args.state,
  106. ),
  107. file=sys.stderr,
  108. )
  109. exit(1)
  110. main(
  111. args.map_dir_path,
  112. seed_value=args.seed,
  113. state_file_path=args.state,
  114. troops_file_path=args.troops,
  115. state_save_dir=args.state_save_dir,
  116. placement_mode=args.placement,
  117. )