run.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # coding: utf-8
  2. import argparse
  3. import logging
  4. from random import seed
  5. from synergine2.log import get_default_logger
  6. from synergine2.config import Config
  7. from synergine2_cocos2d.util import get_map_file_path_from_dir
  8. from synergine2.core import Core
  9. from synergine2.cycle import CycleManager
  10. from synergine2.terminals import TerminalManager
  11. from opencombat.simulation.base import TileStrategySimulation
  12. from opencombat.simulation.base import TileStrategySubjects
  13. from opencombat.state import StateConstructorBuilder
  14. from opencombat.terminal.base import CocosTerminal
  15. def main(
  16. map_dir_path: str,
  17. seed_value: int=None,
  18. state_file_path: str=None,
  19. state_save_dir: str='.',
  20. placement_mode: bool = False,
  21. ):
  22. if seed_value is not None:
  23. seed(seed_value)
  24. config = Config()
  25. config.load_yaml('config.yaml')
  26. # Runtime config
  27. config['_runtime'] = {}
  28. config['_runtime']['state_save_dir'] = state_save_dir
  29. config['_runtime']['placement_mode'] = placement_mode
  30. level = logging.getLevelName(config.resolve('global.logging_level', 'ERROR'))
  31. logger = get_default_logger(level=level)
  32. map_file_path = get_map_file_path_from_dir(map_dir_path)
  33. simulation = TileStrategySimulation(config, map_file_path=map_file_path)
  34. subjects = TileStrategySubjects(simulation=simulation)
  35. if state_file_path:
  36. state_loader_builder = StateConstructorBuilder(config, simulation)
  37. state_loader = state_loader_builder.get_state_loader()
  38. state = state_loader.get_state(state_file_path)
  39. subjects.extend(state.subjects)
  40. simulation.subjects = subjects
  41. core = Core(
  42. config=config,
  43. simulation=simulation,
  44. cycle_manager=CycleManager(
  45. config=config,
  46. simulation=simulation,
  47. ),
  48. terminal_manager=TerminalManager(
  49. config=config,
  50. terminals=[CocosTerminal(
  51. config,
  52. asynchronous=False,
  53. map_dir_path=map_dir_path,
  54. )]
  55. ),
  56. cycles_per_seconds=1 / config.resolve('core.cycle_duration'),
  57. )
  58. core.run()
  59. if __name__ == '__main__':
  60. parser = argparse.ArgumentParser(description='Run TileStrategy')
  61. parser.add_argument('map_dir_path', help='map directory path')
  62. parser.add_argument('--seed', dest='seed', default=None)
  63. parser.add_argument('--state', dest='state', default=None)
  64. parser.add_argument(
  65. '--state-save-dir',
  66. dest='state_save_dir',
  67. default='.',
  68. )
  69. parser.add_argument(
  70. '--placement',
  71. dest='placement',
  72. action='store_true',
  73. )
  74. args = parser.parse_args()
  75. main(
  76. args.map_dir_path,
  77. seed_value=args.seed,
  78. state_file_path=args.state,
  79. state_save_dir=args.state_save_dir,
  80. placement_mode=args.placement,
  81. )