level.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Flexlay - A Generic 2D Game Editor
  2. # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from typing import Any, IO, Optional
  17. import os
  18. import logging
  19. from flexlay.util.config import Config
  20. from flexlay.util.sexpr_reader import get_value_from_tree, sexpr_filter
  21. from flexlay.util.sexpr_writer import SExprWriter
  22. from supertux.sector import Sector
  23. from supertux.util import load_lisp
  24. class Level:
  25. @staticmethod
  26. def from_file(filename: str) -> 'Level':
  27. level = Level()
  28. if filename.endswith(".stwm"):
  29. level.is_worldmap = True
  30. else:
  31. level.is_worldmap = False
  32. level.filename = filename
  33. tree: Any = load_lisp(level.filename, "supertux-level")
  34. data = tree[1:]
  35. level.version = get_value_from_tree(["version", "_"], data, 0)
  36. # print("VERSION:", level.filename, " ", level.version)
  37. if level.version == 1:
  38. raise Exception("version 1 levels not supported at the moment")
  39. else:
  40. level.parse_v2(data)
  41. return level
  42. @staticmethod
  43. def from_size(width: int, height: int) -> 'Level':
  44. result = Level()
  45. result.current_sector = Sector(result)
  46. result.current_sector.new_from_size("main", width, height)
  47. result.sectors.append(result.current_sector)
  48. return result
  49. def __init__(self) -> None:
  50. self.version: int = 2
  51. self.filename: Optional[str] = None
  52. self.name: str = "No Name"
  53. self.author: str = "No Author"
  54. self.contact: str = ""
  55. self.license = "GPL 2+ / CC-by-sa 3.0"
  56. self.target_time: int = 0
  57. self.current_sector: Optional[Sector] = None
  58. self.sectors: list[Sector] = []
  59. self.tileset_path: str = os.path.join("images", "tiles.strf")
  60. self.is_worldmap: bool = False
  61. self.music: str
  62. self.background: str
  63. self.width: int
  64. self.height: int
  65. def parse_v2(self, data: Any) -> None:
  66. self.name = get_value_from_tree(["name", "_"], data, "")
  67. self.author = get_value_from_tree(["author", "_"], data, "")
  68. self.contact = get_value_from_tree(["contact", "_"], data, "")
  69. self.license = get_value_from_tree(["license", "_"], data, "")
  70. self.target_time = get_value_from_tree(["target-time", "_"], data, 0)
  71. self.tileset_path = get_value_from_tree(["tileset", "_"], data, os.path.join("images", "tiles.strf"))
  72. # self.tileset_path = get_value_from_tree(["tileset", "_"], data, "images/tiles.strf")
  73. # self.tileset_path = os.path.join(Config.current.datadir, self.tileset_path)
  74. # Check tileset path is somewhat valid
  75. if len(self.tileset_path) < 1:
  76. self.tileset_path = os.path.join("images", "tiles.strf")
  77. # Sort out tileset path beginning with os.sep
  78. if self.tileset_path[0] == os.sep:
  79. if len(self.tileset_path) > 1:
  80. self.tileset_path = self.tileset_path[1:] # Remove os.sep
  81. else:
  82. self.tileset_path = os.path.join("images", "tiles.strf") # Reset to default
  83. self.current_sector = None
  84. self.sectors = []
  85. for sec in sexpr_filter("sector", data):
  86. sector = Sector(self)
  87. sector.load_v2(sec)
  88. self.sectors.append(sector)
  89. if sector.name == "main":
  90. self.current_sector = sector
  91. if self.current_sector is None:
  92. logging.error("Error: No main sector defined: " + str(self.sectors))
  93. self.current_sector = self.sectors[0]
  94. def save(self, filename: str) -> None:
  95. with open(filename, "w") as f:
  96. self.save_io(f)
  97. def save_io(self, f: IO[str]) -> None:
  98. writer = SExprWriter(f)
  99. # writer.write_comment("Generated by Flexlay Level Editor (Development Version)")
  100. writer.begin_list("supertux-level")
  101. writer.write_int("version", 2)
  102. writer.write_tr_string("name", self.name)
  103. writer.write_string("author", self.author)
  104. if self.contact:
  105. writer.write_string("contact", self.contact)
  106. if self.license:
  107. writer.write_string("license", self.license)
  108. if self.target_time != 0:
  109. writer.write_int("target-time", self.target_time)
  110. for sector in self.sectors:
  111. writer.begin_list("sector")
  112. sector.write(writer)
  113. writer.end_list()
  114. tileset_path = self.tileset_path
  115. assert Config.current is not None
  116. if tileset_path[:len(Config.current.datadir)] == Config.current.datadir:
  117. tileset_path = tileset_path.replace(os.path.join(Config.current.datadir, "/"), "", 1)
  118. if tileset_path is not None:
  119. if tileset_path != "images/tiles.strf":
  120. writer.write_string("tileset", tileset_path)
  121. writer.end_list()
  122. def add_sector(self, sector: Sector) -> None:
  123. self.sectors.append(sector)
  124. def remove_sector(self, name: str) -> None:
  125. if len(self.sectors) > 1:
  126. self.sectors = [sec for sec in self.sectors if sec.name != name]
  127. else:
  128. logging.warning("Only one sector left, can't delete it")
  129. def get_sectors(self) -> list[str]:
  130. return [sec.name for sec in self.sectors]
  131. # EOF #