<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<el:level xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://enigma-game.org/schema/level/1 level.xsd" xmlns:el="http://enigma-game.org/schema/level/1">
  <el:protected >
    <el:info el:type="level">
      <el:identity el:title="Firefox" el:subtitle="Fire or be fired" el:id="20060213ral003"/>
      <el:version el:score="2" el:release="2" el:revision="$Revision: 32 $" el:status="released"/>
      <el:author  el:name="Ronald Lamprecht" el:email="ral@users.berlios.de"/>
      <el:copyright>Copyright © 2006 Ronald Lamprecht</el:copyright>
      <el:license el:type="GPL v2.0 or above" el:open="true"/>
      <el:compatibility el:enigma="1.00">
        <el:dependency el:path="lib/natmaze" el:id="lib/natmaze" el:release="1" el:preload="true"/>
      </el:compatibility>
      <el:modes el:easy="true" el:single="true" el:network="false"/>
      <el:comments>
        <el:credits el:showinfo="true" el:showstart="false">Thanks to Nat Pryce for his libs and code examples</el:credits>
        <el:dedication el:showinfo="true" el:showstart="false">To Daniel Heck for his great work on the engine</el:dedication>
        <el:code>Lua 5.1</el:code>
      </el:comments>
      <el:score el:easy="6:17" el:difficult="11:35"/>
    </el:info>
    <el:luamain><![CDATA[
-- Replaced st-wood by st-flrock to support new fire system. -- Andreas 27.9.06

enigma.ConserveLevel = FALSE

set_oxyd = oxyd
math_floor = floor

function oxyd( x, y, tiles )
    return set_oxyd( x, y )
end

function nothing(x, y, tiles)
    -- create nothing
end

function floor( floor_type, attribs )
    return function( x, y, tiles )
        return set_floor( floor_type, x, y, attribs or {} )
    end
end

function stone( stone_type, attribs )
    return function( x, y, tiles )
        return set_stone( stone_type, x, y, attribs or {} )
    end
end

function item( item_type, attribs )
    return function( x, y, tiles )
        return set_item( item_type, x, y, attribs or {} )
    end
end

function actor( actor_type, attribs )
    return function( x, y, tiles )
        return set_actor( actor_type, x+0.5, y+0.5, attribs or {} )
    end
end

function inherit(tile_type)
    return function( x, y, tiles )
        return create_tile( tiles, x, y, tile_type )
    end
end

function create_world_from_map( tiles, map )
    create_world( strlen(map[1]), getn(map) )
    
    for y = 1,getn(map) do
        local line = map[y]
        for x = 1,strlen(line) do
            local tile = strchar(strbyte(line,x))
            create_tile( tiles, x-1, y-1, tile )
        end
    end
end

function create_tile( tiles, x, y, tile_type )
    local constructors = tiles[tile_type]

    if constructors then
        for i = 1,getn(constructors) do
            constructors[i](x,y,tiles)
        end
    else
        error("invalid tile identifier " .. tile_type )
    end
end

oxyd_default_flavor = "a"

tiles = {}
tiles["-"] = {floor("fl-rock", {secure = TRUE})}
tiles["."] = {floor("fl-hay", {secure = TRUE})}
tiles[","] = {floor("fl-wood", {secure = TRUE})}
tiles["i"] = {floor("fl-ice", {secure = TRUE})}
tiles[" "] = {inherit("."), item("it-burnable")}
tiles[";"] = {inherit(","), item("it-burnable")}
tiles["d"] = {inherit("."), item("it-dynamite")}
tiles["h"] = {inherit("."), item("it-weight")}
tiles["L"] = {inherit("."), item("it-extralife")}
tiles["a"] = {inherit("."), item("it-booze")}
tiles["e"] = {inherit("."), item("it-extinguisher_empty",{name="extinguisher1"})}
tiles["f"] = {inherit("."), item("it-extinguisher")}
tiles["T"] = {inherit("."), item("it-document",{text="Do you think a bushman would start to search an extinguisher in the cellar in case of a wall of fire is approaching? Be a Firefox - you have the first 5 items to deal with, no more no less!"})}
tiles["I0"] = {inherit("i"), item("it-crack0")}
tiles["="] = {inherit("."), stone("st-wood")}
tiles["I"] =  tiles["I0"]
tiles["I3"] = {inherit("i"), item("it-crack3")}
tiles["I1"] = {inherit("i"), item("it-crack1")}
tiles["I0"] = {inherit("."), item("it-crack0")}
tiles["G"] = {inherit("."), stone("st-grate1")}
tiles["#"] = {inherit("."), stone("st-marble")}
tiles["$"] = {inherit(" "), stone("st-marble")}
tiles["N1"] = {inherit("-"), item("it-trigger", {invisible=1})} -- anti cracking
tiles["N"] = {inherit("."), stone("st-oneway_black-n")}
tiles["S1"] = {inherit("-"), item("it-trigger", {invisible=1, action="callback", target="helpbrokenmaze"})}
tiles["S"] = {inherit("S1"), stone("st-oneway_black-s")}
tiles["t"] = {inherit("."), stone("st-laser-n",{on=1})}
tiles["n"] = {inherit("."), stone("st-laser-n",{name="laser1",on=0})}
tiles["s"] = {inherit("."), stone("st-laser-s",{on=1})}
tiles["b"] = {inherit("."), item("it-blackbomb")}
tiles["x"] = {inherit("."), oxyd}
tiles["~"] = {floor("fl-water")}

tiles["1"] = {inherit(" "), actor("ac-blackball", {player=0})}
tiles["2"] = {inherit("."), item("it-trigger", {invisible=1, action="on", target="laser1"})}
tiles["3"] = {inherit("."), stone("st-switch", {action="callback", target="refill"})}
tiles["maze_wall"] = { inherit(","), item("it-burnable") }

if difficult then
    tiles["5"] =  tiles["h"]
    tiles["6"] =  tiles["h"]
    tiles["7"] =  tiles["e"]
    tiles["8"] =  tiles["a"]
    tiles["9"] =  tiles["d"]
    tiles["%"] = {inherit("I3"), stone("st-flhay")}
    tiles["C"] = {inherit(","), item("it-cross")}
    tiles["c"] =  tiles["C"]
    tiles["B"] =  tiles["b"]
    tiles["w"] = {inherit("."), stone("st-flrock")}
    tiles["W"] =  tiles[","]
else
    tiles["5"] =  tiles["d"]
    tiles["6"] =  tiles["a"]
    tiles["7"] =  tiles["h"]
    tiles["8"] =  tiles["e"]
    tiles["9"] =  tiles["h"]
    tiles["%"] = {inherit("I1"), stone("st-flhay")}
    tiles["C"] =  tiles[","]
    tiles["c"] =  tiles["."]
    tiles["B"] =  tiles["."]
    tiles["w"] = {inherit("."), stone("st-wood")}
    tiles["W"] =  tiles["."]
end


function refill()
    local water = 0
    local floor = enigma.GetFloor(18, 28)
    local water1 = false
    local water2 = false
    local water3 = false
    if floor ~= nil then
         if GetKind(floor) == "fl-water" then
             water = water + 1
             water1 = true
         end
    end
    floor = enigma.GetFloor(18, 27)
    if floor ~= nil then
         if GetKind(floor) == "fl-water" then
             water = water + 1
             water2 = true
         end
    end
    floor = enigma.GetFloor(18, 25)
    if floor ~= nil then
         if GetKind(floor) == "fl-water" then
             water = water + 1
             water3 = true
         end
    end

    if water > 0 then
        local obj = enigma.GetItem(18, 26)
        local didFill = 0
        if obj == nil then
            if water == 3 then
                set_item("it-document", 18, 26, {text="You can extinguish fire with water. This is a good place to refill your extinguisher. Be aware, water is rare!"})
            end
        else
            if GetKind(obj) == "it-extinguisher_empty" then
                set_item("it-extinguisher_medium", 18, 26, {})
                water = water -1
                didFill = 1
            else
                if GetKind(obj) == "it-extinguisher_medium" and water > 1 then
                    set_item("it-extinguisher", 18, 26, {})
                    water = water - 2
                    didFill = 2
                end
            end
        end
        if didFill >= 1 and water1 then
            set_floor("fl-swamp", 18, 28, {})
            didFill = didFill - 1
        end
        if didFill >= 1 and water2 then
            set_floor("fl-swamp", 18, 27, {})
            didFill = didFill - 1
        end
        if didFill >= 1 and water3 then
            set_floor("fl-swamp", 18, 25, {})
        end
    end
end

create_world_from_map( tiles, {
"###########x###############x###########",
"#         ===      d      ===         #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#=                                   =#",
"x=                                   =x",
"#=                                   =#",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"#=                                   =#",
"x=                                   =x",
"#=                                   =#",
"#                                     #",
"#                                     #",
"#                                     #",
"#                                     #",
"# ===       w           ===           #",
"###S##s##N###############x#############",
"#  -  . WCcaw    3~#     #   #   #    #",
"#     . B         -# ### # # # # # #22#",
"#     .   B       ~#Tfx#   #   #   # 1#",
"#     .           ~#################22#",
"#     .B     ;;;G###           .   ;  #",
"#     .      ;x;%  #           .   ;7 #",
"#     .      ;;;% x#           .   ;  #",
"#;;;###IIIIII#######    b      . 56;8 #",
"#           d           .      .   ;  #",
"#                       .      .   ;9 #",
"#                       .      .   ;  #",
"########################n######t#######"
})

function helpbrokenmaze()
    set_item("it-document", 20, 27, {text="OOPS! The maze is broken! Prepare to repair the maze before you destroy it."})
end

max_depth = 0
brake_x = -1
brake_y = -1
brake_dir = WEST
-- Brake a maze in the middle of the longest path from an arbitrary given
-- root node.
-- Recursive preorder depth first search algorithm.
function brake_maze(maze, path, depth, x, y, dir)
    local endpoint = true
    if dir ~= NORTH and  maze:contains_cell(x, y-1) and
            maze:can_go_south(x, y-1)  then
        endpoint = false
        path[depth] = {cellx=x, celly=y-1, dir=SOUTH}
        brake_maze(maze, path, depth+1, x, y-1, SOUTH)
    end
    if dir ~= SOUTH and  maze:contains_cell(x, y+1) and
            maze:can_go_south(x, y)  then
        endpoint = false
        path[depth] = {cellx=x, celly=y, dir=SOUTH}
        brake_maze(maze, path, depth+1, x, y+1, NORTH)
    end
    if dir ~= WEST and  maze:contains_cell(x-1, y) and
            maze:can_go_east(x-1, y)  then
        endpoint = false
        path[depth] = {cellx=x-1, celly=y, dir=EAST}
        brake_maze(maze, path, depth+1, x-1, y, EAST)
    end
    if dir ~= EAST and  maze:contains_cell(x, y) and
            maze:can_go_east(x, y)  then
        endpoint = false
        path[depth] = {cellx=x, celly=y, dir=EAST}
        brake_maze(maze, path, depth+1, x+1, y, WEST)
    end
    
    if endpoint == true then
        if max_depth < depth then     
            max_depth = depth
            local infn =   path[math_floor(depth/2)]
            brake_x = infn.cellx                      
            brake_y = infn.celly
            brake_dir = infn.dir
        end
    end
end

function render_broken_maze()
    local perfect_maze = new_kruskal_maze(19,12)
    render_maze(perfect_maze, 
            function(maze,cellx,celly)
                local x = 1 + cellx*2 
                local y = 1 + celly*2
        
                if not maze:can_go_east(cellx,celly) and maze:contains_cell(cellx+1,celly) then
                    create_tile( tiles, x+1, y, "maze_wall" )
                end
                
                if not maze:can_go_south(cellx,celly) and maze:contains_cell(cellx,celly+1) then
                    create_tile( tiles, x, y+1, "maze_wall" )
                end
                
                if maze:contains_cell(cellx+1,celly+1) then
                    create_tile( tiles, x+1, y+1, "maze_wall" )
                end
            end )
    local path = {}
    brake_maze(perfect_maze, path, 0, 0, 0, WEST)
    if brake_dir == EAST then
        create_tile( tiles, 2+brake_x*2, 1+brake_y*2, "maze_wall" )
        if not difficult then
            set_item("it-pin", 2+brake_x*2, 1+brake_y*2, {})
        end
    elseif brake_dir == SOUTH then
        create_tile( tiles, 1+brake_x*2, 2+brake_y*2, "maze_wall" )
        if not difficult then
            set_item("it-pin", 1+brake_x*2, 2+brake_y*2, {})
        end
    end
end

render_broken_maze()
oxyd_shuffle()
    ]]></el:luamain>
    <el:i18n>
      <el:string el:key="title">
        <el:english el:translate="false"/>
      </el:string>
      <el:string el:key="subtitle">
        <el:english el:translate="true" el:comment="Set fire or die"/>
        <el:translation el:lang="de">Brenne oder verbrenne</el:translation>
      </el:string>
      <el:string el:key="Do you think a bushman would start to search an extinguisher in the cellar in case of a wall of fire is approaching? Be a Firefox - you have the first 5 items to deal with, no more no less!">
        <el:english el:comment="Firefox in sense of clever in handling fire">Do you think a bushman would start to search an extinguisher in the cellar in case of a wall of fire is approaching? Be a Firefox - you have the first 5 items to deal with, no more no less!</el:english>
        <el:translation el:lang="de">Würde ein Buschmann anfangen einen Feuerlöscher im Keller zu suchen, wenn eine Feuerwalze naht? Sei ein Fuchs im Feuer - es gibt nur die ersten 5 Gegenstände die zur Lösung beitragen können!</el:translation>
      </el:string>
      <el:string el:key="You can extinguish fire with water. This is a good place to refill your extinguisher. Be aware, water is rare!">
        <el:english>You can extinguish fire with water. This is a good place to refill your extinguisher. Be aware, water is rare!</el:english>
        <el:translation el:lang="de">Man kann Feuer mit Wasser löschen! Hier ist ein guter Ort um Feuerlöscher wieder aufzufüllen. Wer Wasser verschwendet, jämmerlich verendet!</el:translation>
      </el:string>
      <el:string el:key="OOPS! The maze is broken! Prepare to repair the maze before you destroy it.">
        <el:english>OOPS! The maze is broken! Prepare to repair the maze before you destroy it.</el:english>
        <el:translation el:lang="de">Oh! Das Labyrinth ist unvollständig! Unternimm etwas um das Labyrinth zu reparieren bevor Du es zerstörst.</el:translation>
      </el:string>
    </el:i18n>
  </el:protected>
</el:level>

