Updated addon repository URL and improved debug output on download
[supertux.git] / tools / levelconverter-0.1.3_0.2.0.scm
1 ;
2 ;
3 ; $Id$
4 ;
5 ; SuperTux 0.1.3 to SuperTux 0.2.x level conversion helper
6 ; Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.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 ; ---------------------------------------------------------------------------
24
25 ;
26 ; The rest of this file may seem like a Long Irritating Series of Parentheses,
27 ; but it's actually a program. Install a Scheme interpreter, e.g. scm, to run
28 ; it.
29 ;
30 ; This program aids in the conversion of SuperTux levels from 0.1.3 level
31 ; format to the one used by SuperTux 0.2.x.
32 ;
33 ; Usage:
34 ;   levelconverter-0.1.3_0.2.0.scm < oldformat.stl > newformat.stl
35 ;
36 ; Bugs:
37 ;   Some things (like what background image to use) are not converted:
38 ;   they will need manual adjustment afterwards.
39
40 ; ---------------------------------------------------------------------------
41
42 ; return first sublist in haystack that starts with needle or #f if none is found
43 (define (find-sublist haystack needle)
44   (cond
45     (
46      (not (pair? haystack))
47      #f
48      )
49     (
50      (and (pair? (car haystack)) (eq? (caar haystack) needle))
51      (cdar haystack)
52      )
53     (
54      else
55      (find-sublist (cdr haystack) needle)
56      )
57     )
58   )
59
60 ; return SuperTux 0.1.3 object in SuperTux 0.2.x form
61 (define (convert-object object)
62   (cond
63     (
64      (eq? (car object) 'money)
65      (append '(jumpy) (cdr object))
66      )
67     (
68      else
69      object
70      )
71     )
72   )
73
74 ; return SuperTux 0.1.3 level in SuperTux 0.2.x form
75 (define (convert-level level)
76   (let
77     (
78      (type (car level))
79      (version (find-sublist level 'version))
80      (author (find-sublist level 'author))
81      (name (find-sublist level 'name))
82      (width (find-sublist level 'width))
83      (height (find-sublist level 'height))
84      (start_pos_x (find-sublist level 'start_pos_x))
85      (start_pos_y (find-sublist level 'start_pos_y))
86      (interactive-tm (find-sublist level 'interactive-tm))
87      (background-tm (find-sublist level 'background-tm))
88      (foreground-tm (find-sublist level 'foreground-tm))
89      (objects (find-sublist level 'objects))
90      )
91     (if (not (string=? (symbol->string type) "supertux-level")) (error "not a supertux-level:" type))
92     (if (not (= (car version) 1)) (error "not a version 1 level"))
93     (if (not author) (set! author '("Anonymous")))
94     (if (not name) (set! name '("Unnamed Level")))
95     (if (not width) (error "No level width given"))
96     (if (not height) (set! height '(15)))
97     (if (not start_pos_x) (set! start_pos_x '(100)))
98     (if (not start_pos_y) (set! start_pos_y '(170)))
99     (if (not interactive-tm) (error "No interactive tilemap given"))
100     (if (not background-tm) (error "No background tilemap given"))
101     (if (not foreground-tm) (error "No foreground tilemap given"))
102     (if (not objects) (error "No objects list given"))
103     (quasiquote
104       (supertux-level
105         (version 2)
106         (name (_ ,(car name)))
107         (author ,(car author))
108         ,(append
109            (quasiquote
110              (sector
111                (name "main")
112                (gradient
113                  (top_color 0 0 0.2)
114                  (bottom_color 0 0 0.6)
115                  )
116                (tilemap
117                  (z-pos -100)
118                  (solid #f)
119                  (speed 1)
120                  (width ,(car width))
121                  (height ,(car height))
122                  ,(append '(tiles) background-tm)
123                  )
124                (tilemap
125                  (z-pos 0)
126                  (solid #t)
127                  (speed 1)
128                  (width ,(car width))
129                  (height ,(car height))
130                  ,(append '(tiles) interactive-tm)
131                  )
132                (tilemap
133                  (z-pos 100)
134                  (solid #f)
135                  (speed 1)
136                  (width ,(car width))
137                  (height ,(car height))
138                  ,(append '(tiles) foreground-tm)
139                  )
140                (spawnpoint
141                  (name "main")
142                  (x ,(car start_pos_x))
143                  (y ,(car start_pos_y))
144                  )
145                )
146              )
147            (map convert-object objects)
148            )
149         )
150       )
151     )
152   )
153
154 ; run conversion on stdin, output to stdout
155 (write (convert-level (read)))
156 (newline)
157 (quit)
158