fix cr/lfs and remove trailing whitespaces...
[supertux.git] / tools / levelconverter-0.1.3_0.2.0.scm
1 #!/usr/bin/scm
2 ; !#
3 ;
4 ; $Id$
5 ;
6 ; SuperTux 0.1.3 to SuperTux 0.2.x level conversion helper
7 ; Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
8 ;
9 ; This program is free software; you can redistribute it and/or
10 ; modify it under the terms of the GNU General Public License
11 ; as published by the Free Software Foundation; either version 2
12 ; of the License, or (at your option) any later version.
13 ;
14 ; This program is distributed in the hope that it will be useful,
15 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ; GNU General Public License for more details.
18 ;
19 ; You should have received a copy of the GNU General Public License
20 ; along with this program; if not, write to the Free Software
21 ; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 ;
23
24 ; ---------------------------------------------------------------------------
25
26 ;
27 ; The rest of this file may seem like a Long Irritating Series of Parentheses,
28 ; but it's actually a program. Install a Scheme interpreter, e.g. scm, to run 
29 ; it.
30 ;
31 ; This program aids in the conversion of SuperTux levels from 0.1.3 level 
32 ; format to the one used by SuperTux 0.2.x.
33 ;
34 ; Usage:
35 ;   levelconverter-0.1.3_0.2.0.scm < oldformat.stl > newformat.stl
36 ;
37
38 ; ---------------------------------------------------------------------------
39
40 ; return first sublist in haystack that starts with needle or #f if none is found
41 (define (find-sublist haystack needle)
42   (cond
43     (
44      (not (pair? haystack))
45      #f
46      )
47     (
48      (and (pair? (car haystack)) (eq? (caar haystack) needle))
49      (cdar haystack)
50      )
51     (
52      else
53      (find-sublist (cdr haystack) needle)
54      )
55     )
56   )
57
58 ; return SuperTux 0.1.3 object in SuperTux 0.2.x form
59 (define (convert-object object)
60   (cond
61     (
62      (eq? (car object) 'money)
63      (append '(jumpy) (cdr object))
64      )
65     (
66      else
67      object
68      )
69     )
70   )
71
72 ; return SuperTux 0.1.3 level in SuperTux 0.2.x form
73 (define (convert-level level)
74   (let 
75     (
76      (type (car level))
77      (version (find-sublist level 'version))
78      (author (find-sublist level 'author))
79      (name (find-sublist level 'name))
80      (width (find-sublist level 'width))
81      (height (find-sublist level 'height))
82      (start_pos_x (find-sublist level 'start_pos_x))
83      (start_pos_y (find-sublist level 'start_pos_y))
84      (interactive-tm (find-sublist level 'interactive-tm))
85      (background-tm (find-sublist level 'background-tm))
86      (foreground-tm (find-sublist level 'foreground-tm))
87      (objects (find-sublist level 'objects))
88      )
89     (if (not (string=? (symbol->string type) "supertux-level")) (error "not a supertux-level:" type))
90     (if (not (= (car version) 1)) (error "not a version 1 level"))
91     (if (not author) (set! author '("Anonymous")))
92     (if (not name) (set! name '("Unnamed Level")))
93     (if (not width) (error "No level width given"))
94     (if (not height) (set! height '(15)))
95     (if (not start_pos_x) (set! start_pos_x '(100)))
96     (if (not start_pos_y) (set! start_pos_y '(170)))
97     (if (not interactive-tm) (error "No interactive tilemap given"))
98     (if (not background-tm) (error "No background tilemap given"))
99     (if (not foreground-tm) (error "No foreground tilemap given"))
100     (if (not objects) (error "No objects list given"))
101     (quasiquote 
102       (supertux-level
103         (version 2)
104         (name (_ ,(car name)))
105         (author ,(car author))
106         ,(append 
107            (quasiquote
108              (sector
109                (name "main")
110                (gradient
111                  (top_color 0 0 0.2)
112                  (bottom_color 0 0 0.6)
113                  )
114                (tilemap
115                  (z-pos -100)
116                  (solid #f)
117                  (speed 1)
118                  (width ,(car width))
119                  (height ,(car height))
120                  ,(append '(tiles) background-tm)
121                  )
122                (tilemap
123                  (z-pos 0)
124                  (solid #t)
125                  (speed 1)
126                  (width ,(car width))
127                  (height ,(car height))
128                  ,(append '(tiles) interactive-tm)
129                  )
130                (tilemap
131                  (z-pos 100)
132                  (solid #f)
133                  (speed 1)
134                  (width ,(car width))
135                  (height ,(car height))
136                  ,(append '(tiles) foreground-tm)
137                  )
138                (spawnpoint
139                  (name "main")
140                  (x ,(car start_pos_x))
141                  (y ,(car start_pos_y))
142                  )
143                )
144              )
145            (map convert-object objects)
146            )
147         )
148       )
149     )
150   )
151   
152 ; run conversion on stdin, output to stdout
153 (write (convert-level (read)))
154 (newline)
155 (quit)
156