~ubuntu-branches/ubuntu/trusty/enigma/trusty-proposed

« back to all changes in this revision

Viewing changes to tools/lua2xml.lua

  • Committer: Package Import Robot
  • Author(s): Erich Schubert
  • Date: 2013-04-06 14:54:02 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130406145402-jgjrtk7hac8gtvza
Tags: 1.20-dfsg.1-1
* New upstream release (Closes: #704595)
  (Repacked: dropped zipios++ source and main menu music)
* Update watch file, sf.net again.
* Fix documentation links (Closes: #653508)
* Conflict with enigma-level-previews to encourage deinstallation
  (Pregenerated level previews were only used with version 1.01)
* Use dh7 for building instead of CDBS
* Update to policy 3.9.4.0 (no changes)
* Register documentation with doc-base

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
lua = {
3
 
    floor = floor
4
 
}
5
 
 
6
 
lua2xml = {
7
 
    datadir = "../data",
8
 
    errormsg = "",
9
 
    errors = {},
10
 
 
11
 
    w = 0,
12
 
    h = 0,
13
 
    fl = {},
14
 
    it = {},
15
 
    st = {},
16
 
    actors = {},
17
 
 
18
 
    namedobjs = {},
19
 
    oxyds = {},
20
 
    rubberbands = {},
21
 
    signals = {},
22
 
    objcnt = 0,
23
 
 
24
 
    luacode = "",
25
 
 
26
 
    -- options
27
 
    oxyd_shuffle = FALSE
28
 
}
29
 
 
30
 
function lua2xml:add_error (e)
31
 
    self.errors[e] = (self.errors[e] or 0) + 1
32
 
    self.errormsg = self.errormsg .. e .. "\n"
33
 
end
34
 
 
35
 
function lua2xml:has_error ()
36
 
    return self.errormsg ~= ""
37
 
end
38
 
 
39
 
function lua2xml:resize_world (w, h)
40
 
    self.w, self.h = w, h
41
 
    for x=0,(w-1) do
42
 
        self.fl[x] = {}
43
 
        self.it[x] = {}
44
 
        self.st[x] = {}
45
 
    end
46
 
end
47
 
 
48
 
function lua2xml:set_table  (table, x, y, o)
49
 
    x, y = lua.floor (tonumber (x)), lua.floor (tonumber (y))
50
 
    if x >= 0 and y >= 0 and x<self.w and y<self.h then
51
 
        table[x][y] = o
52
 
    end
53
 
end
54
 
 
55
 
 
56
 
function lua2xml:optimize_floor ()
57
 
    local floorcnt = {}
58
 
    for y = 0,(self.h-1) do
59
 
        for x = 0,(self.w-1) do
60
 
            local fl = self.fl[x][y]
61
 
            if fl == nil then
62
 
                fl = {name="fl-abyss"} -- replace missing floor tiles with fl-abyss
63
 
                self.fl[x][y] = fl
64
 
            end
65
 
            floorcnt[fl.name] = (floorcnt[fl.name] or 0) + 1
66
 
        end
67
 
    end
68
 
 
69
 
    local floorlist = {}
70
 
    for k,v in floorcnt do tinsert (floorlist, k) end
71
 
    sort (floorlist, function (x, y) return (%floorcnt[x] < %floorcnt[y]) end)
72
 
 
73
 
    -- Fill floor with most common floor tile
74
 
    self.fillfloor = floorlist[getn(floorlist)]
75
 
 
76
 
    -- Remove these floor tiles from the array
77
 
    for y = 0,(self.h-1) do
78
 
        for x = 0,(self.w-1) do
79
 
            local fl = self.fl[x][y].name
80
 
            if fl == self.fillfloor then
81
 
                self.fl[x][y] = nil
82
 
            end
83
 
        end
84
 
    end
85
 
end
86
 
 
87
 
 
88
 
 
89
 
------------------------------
90
 
------------------------------
91
 
 
92
 
function xmlout_info (s)
93
 
    print ("\t<info>")
94
 
    print ("\t\t<name></name>")
95
 
    print ("\t\t<author></author>")
96
 
    print ("\t\t<copyright></copyright>")
97
 
    print ("\t\t<license></license>")
98
 
    print ("\t</info>")
99
 
end
100
 
 
101
 
function xmlout_options (s)
102
 
    function yesno(x) return x==TRUE and "YES" or "NO" end
103
 
    function noyes(x) return x==TRUE and "NO" or "YES" end
104
 
    function printopt (name, val) 
105
 
        if val then
106
 
            print ('\t<option name="'..name..'" value="'..val..'" />') 
107
 
        end
108
 
    end
109
 
    
110
 
    printopt ("reset", noyes(enigma.ConserveLevel))
111
 
    printopt ("oxydflavor", oxyd_default_flavor)
112
 
    printopt ("shuffle", yesno (lua2xml.oxyd_shuffle))
113
 
    printopt ("scrolling", lua2xml.scrollmode)
114
 
    printopt ("brittleness", enigma.Brittleness)
115
 
    printopt ("slopeforce", enigma.SlopeForce)
116
 
    printopt ("flatforce", enigma.FlatForce)
117
 
    printopt ("frictionfactor", enigma.FrictionFactor)
118
 
    printopt ("electricforce", enigma.ElectricForce)
119
 
    printopt ("bumperforce", enigma.BumperForce)
120
 
    printopt ("magnetforce", enigma.MagnetForce)
121
 
    printopt ("magnetrange", enigma.MagnetRange)
122
 
    printopt ("wormholeforce", enigma.WormholeForce)
123
 
    printopt ("wormholerange", enigma.WormholeRange)
124
 
    printopt ("holeforce", enigma.HoleForce)
125
 
end
126
 
 
127
 
function string_attribs (a)
128
 
    local s = ""
129
 
    if a then
130
 
        for k,v in a do
131
 
            s = s .. k .. "='" .. v .. "',"
132
 
        end
133
 
    end
134
 
    if s ~= "" then
135
 
        return ' attribs="' .. s  .. '"'
136
 
    end
137
 
    return ""
138
 
end
139
 
 
140
 
 
141
 
function xmlout_actors (s)
142
 
    print "\t<actors>"
143
 
 
144
 
    for i=1,getn(s.actors) do
145
 
        local ac = s.actors[i]
146
 
        local o = ac.obj
147
 
        local a =string_attribs(o.attribs)
148
 
        print (format ('\t\t<actor kind="%s" x="%f" y="%f" %s />', o.name, ac.x, ac.y, a))
149
 
    end
150
 
 
151
 
    print "\t</actors>"
152
 
end
153
 
 
154
 
function xmlout_grid (s, tag, table)
155
 
    function optimize_row (r)
156
 
        -- remove trailing " -"'s and " *"'s
157
 
        r = gsub (r, "[%- ]+$", "")
158
 
 
159
 
        -- compress repeated " #"'s
160
 
        r = gsub (r, "(#[ #]+ )", function (v) 
161
 
                                     local n = (strlen(v))/2
162
 
                                     return n>1 and (n .. " ") or v
163
 
                                 end)
164
 
        r = gsub (r, "(%-[ %-]+ )", function (v) 
165
 
                                     local n = (strlen(v))/2
166
 
                                     return n>1 and ("-" .. n .. " ") or v
167
 
                                 end)
168
 
        return r
169
 
    end
170
 
 
171
 
    local attribobjs = ""
172
 
    for y=0,(s.h-1) do
173
 
        local row = ""
174
 
        local prevname = ""
175
 
        local numempty = 0
176
 
        for x=0,(s.w-1) do
177
 
            local o = table[x][y]
178
 
            if not o then
179
 
                row = row .. '- '
180
 
                numempty = numempty + 1
181
 
            else
182
 
                local a = string_attribs (o.attribs)
183
 
                if a ~= "" then
184
 
                    attribobjs = attribobjs .. '\t\t<' .. tag .. ' x="' .. x .. '" y="' .. y 
185
 
                        .. '" kind="' .. o.name .. '"' .. a .. ' />\n'
186
 
                    row = row .. '- '
187
 
                else
188
 
                    if o.name == prevname then
189
 
                        row = row .. '# '
190
 
                    else
191
 
                        row = row .. strsub(o.name, 4) .. " "
192
 
                        prevname = o.name
193
 
                    end
194
 
                end
195
 
            end
196
 
        end
197
 
        row = optimize_row (row)
198
 
        if numempty == s.w then
199
 
        elseif row ~= "" then 
200
 
            print ('\t\t<row y="' .. y .. '">' .. row  .. "</row>") 
201
 
        end
202
 
    end
203
 
    if attribobjs ~= "" then print (attribobjs) end
204
 
end
205
 
 
206
 
function xmlout_floors (s)
207
 
    print "\t<floors>"
208
 
    if s.fillfloor then
209
 
        print ('\t\t<fill kind="' .. s.fillfloor .. '" />')
210
 
    end
211
 
    xmlout_grid (s, "floor", s.fl)
212
 
    print "\t</floors>"
213
 
end
214
 
 
215
 
function xmlout_stones (s)
216
 
    print "\t<stones>"
217
 
    xmlout_grid (s, "stone", s.st)
218
 
    for i=1,getn(s.oxyds) do
219
 
        print ('\t\t<oxyd x="' .. s.oxyds[i].x .. '" y="' .. s.oxyds[i].y .. '" />')
220
 
    end
221
 
    print "\t</stones>"
222
 
end
223
 
 
224
 
function xmlout_items (s)
225
 
    print "\t<items>"
226
 
    xmlout_grid (s, "item", s.it)
227
 
    print "\t</items>"
228
 
end
229
 
 
230
 
function xmlout_signals (s)
231
 
    if getn (s.signals) == 0 then return end
232
 
    print "\t<signals>"
233
 
    print "\t</signals>"
234
 
end
235
 
 
236
 
function xmlout_rubberbands (s)
237
 
    if (getn (s.rubberbands) == 0) then return end
238
 
    print "\t<rubberbands>"
239
 
    for i=1,getn (s.rubberbands) do
240
 
        local rb = s.rubberbands[i]
241
 
        print (format ('\t\t<rubberband from="%s" to="%s" strength="%f" length="%f" minlen="%f" />', 
242
 
                        rb.o1, rb.o2, rb.force, rb.len, rb.minlen))
243
 
    end
244
 
    print "\t</rubberbands>"
245
 
end
246
 
 
247
 
function xmlout_lua (s)
248
 
    if (s.luacode ~= "") then
249
 
        print ("\t<lua>")
250
 
        print (s.luacode)
251
 
        print ("\t</lua>")
252
 
    end
253
 
end
254
 
 
255
 
 
256
 
function xmlout_level (s)
257
 
    print ("<level width=\"" .. s.w.. "\" height=\"" .. s.h.. "\">")
258
 
    xmlout_info (s)
259
 
    xmlout_options (s)
260
 
    xmlout_floors (s)
261
 
    xmlout_items (s)
262
 
    xmlout_stones (s)
263
 
    xmlout_actors (s)
264
 
    xmlout_rubberbands (s)
265
 
    xmlout_signals (s)
266
 
    xmlout_lua (s)
267
 
    print ("</level>")
268
 
end
269
 
 
270
 
 
271
 
-----------------------------------------------
272
 
-----------------------------------------------
273
 
 
274
 
options = { }
275
 
enigma = { 
276
 
    WEST  = 0,
277
 
    SOUTH = 1,
278
 
    EAST  = 2,
279
 
    NORTH = 3,
280
 
    ConserveLevel = 1,
281
 
}
282
 
world = { }
283
 
display = { 
284
 
    FOLLOW_SCROLLING = 1,
285
 
    FOLLOW_SCREEN = 2,
286
 
    FOLLOW_SCREENSCROLLING = 3,
287
 
    FOLLOW_SMOOTH = 4
288
 
}
289
 
 
290
 
options.Difficulty = 2
291
 
 
292
 
function enigma.GetAttrib (o, name)
293
 
    return o.attribs[name]
294
 
end
295
 
 
296
 
function enigma.SetAttrib (o, key, val)
297
 
    function escapestr (str)
298
 
        str = gsub (str, "'", "\\'")
299
 
        return str
300
 
    end
301
 
 
302
 
    if key == "action" and val == "callback" then
303
 
        lua2xml:add_error "- uses callbacks"
304
 
    elseif o.name == "fl-gradient" and key == "type" then
305
 
        val = tonumber (val)
306
 
        if val >= 1 and val <= 12 then
307
 
            o.name = "fl-gradient" .. val
308
 
            return
309
 
        else
310
 
            local t = {[22] = 13, [21] = 14, [24] = 15, [23] = 16}
311
 
            o.name = "fl-gradient" .. t[val]
312
 
            return
313
 
        end
314
 
    elseif o.name == "st-puzzle" and key == "connections" then
315
 
        local t = {
316
 
            "hollow", "w", "s", "sw", "e", "ew", "es", "esw", "n", 
317
 
            "nw", "ns", "nsw", "ne", "new", "nes", "nesw",
318
 
        }
319
 
        o.name = "st-puzzle-" .. t[val]
320
 
        return
321
 
    elseif o.name == "st-bolder" and key == "direction" then
322
 
        local t = { "w", "s", "e", "n" }
323
 
        o.name = "st-bolder-" .. t[val+1]
324
 
        return
325
 
    elseif o.name == "st-oneway" and key == "orientation" then
326
 
        local t = { "w", "s", "e", "n" }
327
 
        o.name = "st-oneway-" .. t[val+1]
328
 
        return
329
 
    elseif (o.name == "st-door_a" or o.name == "st-door_b") and key == "type" then
330
 
        return
331
 
    end
332
 
    o.attribs[key] = escapestr (val)
333
 
end
334
 
 
335
 
function enigma.GetKind (o)
336
 
    return o.name
337
 
end
338
 
 
339
 
function enigma.NameObject (o, name)
340
 
    o.attribs["name"] = name
341
 
    lua2xml.namedobjs[name] = o
342
 
end
343
 
 
344
 
function enigma.GetNamedObject (name)
345
 
    return lua2xml.namedobjs[name]
346
 
end
347
 
 
348
 
function world.Resize (w, h) 
349
 
    lua2xml:resize_world (w, h)
350
 
end
351
 
 
352
 
function enigma.SendMessage (o, msg, val)
353
 
    lua2xml:add_error "- uses enigma.SendMessage"
354
 
end
355
 
 
356
 
function enigma.GetTicks ()
357
 
    lua2xml:add_error "- uses enigma.GetTicks"
358
 
    return 0
359
 
end
360
 
 
361
 
function enigma.SetFloor (x, y, o) lua2xml:set_table (lua2xml.fl, x, y, o) end
362
 
function enigma.SetItem (x,y, o)   lua2xml:set_table (lua2xml.it, x, y, o) end
363
 
function enigma.SetStone (x,y, o)  lua2xml:set_table (lua2xml.st, x, y, o) end
364
 
function enigma.SetActor (x, y, o) tinsert (lua2xml.actors, {obj=o, x=x, y=y}) end
365
 
 
366
 
function enigma.KillFloor (x, y)   lua2xml:set_table (lua2xml.fl, x, y, nil) end
367
 
function enigma.KillItem (x, y)    lua2xml:set_table (lua2xml.it, x, y, nil) end
368
 
function enigma.KillStone (x, y)   lua2xml:set_table (lua2xml.st, x, y, nil) end
369
 
 
370
 
function enigma.GetItem (x, y)     return lua2xml.it[x][y] end
371
 
 
372
 
function enigma.MakeObject (name)
373
 
    return {name=name, attribs = {}}
374
 
end
375
 
 
376
 
function enigma.AddRubberBand (o1, o2, force, len, minlen)
377
 
    function add_missing_name (o)
378
 
        if o.attribs.name == nil then
379
 
            enigma.NameObject (o, "namedobj" .. lua2xml.objcnt)
380
 
            lua2xml.objcnt = lua2xml.objcnt + 1
381
 
        end
382
 
    end
383
 
    add_missing_name (o1); add_missing_name (o2)
384
 
    tinsert (lua2xml.rubberbands, 
385
 
             {o1 = o1.attribs.name, o2=o2.attribs.name, 
386
 
                 force = force, len = len, minlen = minlen or 0})
387
 
end
388
 
 
389
 
function display.SetFollowMode (m)
390
 
    lua2xml.scrollmode = m
391
 
end
392
 
 
393
 
function enigma.FindDataFile (f)
394
 
    return lua2xml.datadir .. "/" .. f
395
 
end
396
 
 
397
 
function enigma.AddConstantForce (x, y)
398
 
    lua2xml.luacode = lua2xml.luacode .. "enigma.AddConstantForce (" .. x .. ", " .. y .. ")\n"
399
 
end
400
 
 
401
 
function enigma.SetCompatibility (name)
402
 
    lua2xml.luacode = lua2xml.luacode .. "enigma.SetCompatibility (\"" .. name .. "\")\n"
403
 
end
404
 
 
405
 
function enigma.date ()
406
 
    lua2xml:add_error "- uses enigma.date"
407
 
    return "0"
408
 
end
409
 
 
410
 
function enigma.GetObjectTemplate ()
411
 
    lua2xml:add_error "- uses enigma.GetObjectTemplate"
412
 
end
413
 
 
414
 
function enigma.AddScramble ()
415
 
    lua2xml:add_error "- uses enigma.AddScramble"
416
 
end
417
 
 
418
 
function enigma.SetScrambleIntensity ()
419
 
    lua2xml:add_error "- uses enigma.SetScrambleIntensity"
420
 
end
421
 
 
422
 
function world.DefineSimpleStoneMovable ()
423
 
    lua2xml:add_error "- uses world.DefineSimpleStoneMovable"
424
 
end
425
 
 
426
 
function world.DefineSimpleStone ()
427
 
    lua2xml:add_error "- uses world.DefineSimpleStone"
428
 
end
429
 
 
430
 
function display.DefineAlias ()
431
 
    lua2xml:add_error "- uses display.DefineAlias"
432
 
end
433
 
 
434
 
function enigma.IsSolved ()
435
 
    lua2xml:add_error "- uses display.IsSolved"
436
 
end
437
 
 
438
 
 
439
 
function run_level_script (fname)
440
 
    local g = {}
441
 
    for k,v in globals() do g[k] = v end
442
 
    local oldglobals = globals(g)
443
 
 
444
 
    -- Include standard init.lua --
445
 
    dofile (enigma.FindDataFile ("init.lua"))
446
 
 
447
 
    -- Override definitions from init.lua --
448
 
    function g.oxyd (x, y)
449
 
        tinsert (lua2xml.oxyds, {x=x, y=y})
450
 
    end
451
 
    
452
 
    function g.oxyd_shuffle ()
453
 
        lua2xml.oxyd_shuffle = TRUE
454
 
    end
455
 
    
456
 
    -- Run level script --
457
 
    retval = 0
458
 
    if not dofile (luafile) then
459
 
        retval = 1
460
 
    end
461
 
 
462
 
    globals (oldglobals)
463
 
    return 0
464
 
end
465
 
 
466
 
 
467
 
 
468
 
------------------
469
 
------------------
470
 
 
471
 
if getn (arg) ~= 1 then
472
 
    print ("Usage: " .. arg[0] .. " filename.lua")
473
 
    return 1
474
 
end
475
 
 
476
 
luafile = arg[1]
477
 
 
478
 
run_level_script (luafile)
479
 
 
480
 
--------------------------------
481
 
--------------------------------
482
 
 
483
 
lua2xml:optimize_floor ()
484
 
 
485
 
 
486
 
if not lua2xml:has_error() then
487
 
    xmlout_level (lua2xml)
488
 
    exit (0)
489
 
else
490
 
    write (_STDERR,  "There were errors converting '" .. luafile .. "': \n")
491
 
    for k,v in lua2xml.errors do
492
 
        write (_STDERR, k .. ' (' .. v .. ' times) \n')
493
 
    end
494
 
    write (_STDERR, '\n')
495
 
    exit(1)
496
 
end
497