run.py 3.7KB

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