tileset-splitter.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;
  2. ;
  3. ; $Id$
  4. ;
  5. ; SuperTux 0.3.1 tileset splitter
  6. ; Copyright (C) 2008 Christoph Sommer <christoph.sommer@2008.expires.deltadevelopment.de>
  7. ;
  8. ; This program is free software; you can redistribute it and/or
  9. ; modify it under the terms of the GNU General Public License
  10. ; as published by the Free Software Foundation; either version 2
  11. ; of the License, or (at your option) any later version.
  12. ;
  13. ; This program is distributed in the hope that it will be useful,
  14. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ; GNU General Public License for more details.
  17. ;
  18. ; You should have received a copy of the GNU General Public License
  19. ; along with this program; if not, write to the Free Software
  20. ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. ;
  22. ; ---------------------------------------------------------------------------
  23. ; Reads a SuperTux tileset on stdin, outputs a SuperTux tileset with only
  24. ; those tiles with a tile-id in the tileset's first group.
  25. ; This means that if the tileset starts with a tilegroup "snow", you
  26. ; will get a tileset with only snow tiles in the output
  27. ; ---------------------------------------------------------------------------
  28. ; return first sublist in haystack that starts with needle or #f if none is found
  29. (define (find-sublist haystack needle)
  30. (cond
  31. (
  32. (not (pair? haystack))
  33. #f
  34. )
  35. (
  36. (and (pair? (car haystack)) (eq? (caar haystack) needle))
  37. (cdar haystack)
  38. )
  39. (
  40. else
  41. (find-sublist (cdr haystack) needle)
  42. )
  43. )
  44. )
  45. ; input: (tile ... (id 1) ...) (1 2 3 4 5)
  46. ; output: #t if "id" in "valid-ids", #f otherwise
  47. (define (output-tile? children valid-ids)
  48. (let
  49. ((id (car (find-sublist children 'id))))
  50. (if (not id)
  51. #f
  52. )
  53. (if (member id valid-ids)
  54. #t
  55. #f
  56. )
  57. )
  58. )
  59. ; input: (1 7 8) (1 2 3 4 5)
  60. ; output: #t if any of "needles" in "haystack", #f otherwise
  61. (define (any-member needles haystack)
  62. (if (null? needles)
  63. #f
  64. (or
  65. (member (car needles) haystack)
  66. (any-member (cdr needles) haystack)
  67. )
  68. )
  69. )
  70. ; input: (tiles ... (ids 1 7 8) ...) (1 2 3 4 5)
  71. ; output: #t if any of "ids" in "valid-ids", #f otherwise
  72. (define (output-tiles? children valid-ids)
  73. (let
  74. ((ids (find-sublist children 'ids)))
  75. (if (not ids)
  76. #f
  77. )
  78. (if (any-member ids valid-ids)
  79. #t
  80. #f
  81. )
  82. )
  83. )
  84. ; input: ((tilegroup ...) (tilegroup ...) (tile ...) (tiles ...))
  85. ; output: ((tilegroup ...) (tile ...) (tiles ...))
  86. (define (output-and-next children valid-ids)
  87. (if (null? valid-ids)
  88. ; tilegroup not yet found
  89. (if (not (string=? (symbol->string (caar children)) "tilegroup"))
  90. ; no tilegroup: just continue
  91. (if (not (null? (cdr children))) (output-and-next (cdr children) valid-ids) '())
  92. (begin
  93. ; tilegroup: get valid-ids
  94. (set! valid-ids (find-sublist (car children) 'tiles))
  95. ; output tilegroup and continue
  96. (cons
  97. (car children)
  98. (if (not (null? (cdr children))) (output-and-next (cdr children) valid-ids) '())
  99. )
  100. )
  101. )
  102. (begin
  103. ; tilegroup already found
  104. (cond
  105. ((string=? (symbol->string (caar children)) "tile")
  106. (if (output-tile? (car children) valid-ids)
  107. (cons
  108. (car children)
  109. (if (not (null? (cdr children))) (output-and-next (cdr children) valid-ids) '())
  110. )
  111. (if (not (null? (cdr children))) (output-and-next (cdr children) valid-ids) '())
  112. )
  113. )
  114. ((string=? (symbol->string (caar children)) "tiles")
  115. (if (output-tiles? (car children) valid-ids)
  116. (cons
  117. (car children)
  118. (if (not (null? (cdr children))) (output-and-next (cdr children) valid-ids) '())
  119. )
  120. (if (not (null? (cdr children))) (output-and-next (cdr children) valid-ids) '())
  121. )
  122. )
  123. (else
  124. (if (not (null? (cdr children))) (output-and-next (cdr children) valid-ids) '())
  125. )
  126. )
  127. )
  128. )
  129. )
  130. ; input: (supertux-tiles ... (tilegroup ...) (tilegroup ...) (tile ...) (tiles ...))
  131. ; output: (supertux-tiles (tilegroup ...) (tile ...) (tiles ...))
  132. (define (clip-tileset supertux-tiles)
  133. (let ()
  134. (if (not (string=? (symbol->string (car supertux-tiles)) "supertux-tiles")) (error "not a supertux-tileset:" type))
  135. (output-and-next (cdr supertux-tiles) '())
  136. )
  137. )
  138. ; run conversion on stdin, output to stdout
  139. (write (clip-tileset (read)))
  140. (newline)
  141. (quit)