<?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="Bodyguard" el:subtitle="In the line of thieves" el:id="20060730ral012"/>
      <el:version el:score="1" el:release="1" el:revision="$Revision: 41 $" 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:modes el:easy="true" el:single="true" el:network="false"/>
      <el:score el:easy="3:44" el:difficult="9:40"/>
    </el:info>
    <el:luamain><![CDATA[
enigma.ConserveLevel = FALSE

set_oxyd = oxyd

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

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

function horizontal_bolder(x,y,tiles)
    if random() >= 0.5 then
        return set_stone( "st-bolder-w", x, y )
    else
    	return set_stone( "st-bolder-e", x, y )
    end
end

function vertical_bolder(x,y,tiles)
    if random() >= 0.5 then
        return set_stone( "st-bolder-n", x, y )
    else
    	return set_stone( "st-bolder-s", x, y )
    end
end

function checkerboard_floor( type1, type2, attribs1, attribs2 )
    return function( x, y, tiles )
        if mod(x,2) == mod(y,2) then
            return set_floor( type1, x, y, attribs1 or {} )
        else
            return set_floor( type2, x, y, attribs2 or {} )
        end
    end
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 gradient( gradient_type )
    return function( x, y, tiles )
        return set_floor( "fl-gradient", x, y, {type=gradient_type} )
    end
end

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

function group( array, constructor )
    return function( x, y, tiles )
        object = constructor( x, y, tiles )
    	tinsert( array, object )
    	return object
    end
end

function difficulty( d, constructor )
    if options.Difficulty == d then
    	return constructor
    else
    	return nothing
    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-bluegray")}
tiles["."] = {floor("fl-thief")}
tiles["#"] = {inherit("."), stone("st-glass")}
tiles["B"] = {inherit(" "), stone("st-black4")}
tiles["W"] = {inherit(" "), stone("st-white4")}
tiles["b"] = {inherit("B"), item("it-yinyang")}
tiles["w"] = {inherit("W"), item("it-yinyang")}
tiles["c"] = {inherit(" "), item("it-cross")}

tiles["k"] = {inherit("B"), item("it-key", {keycode=0})}
tiles["l"] = {inherit("B"), item("it-key", {keycode=1})}
tiles["m"] = {inherit("W"), item("it-key", {keycode=2})}
tiles["n"] = {inherit("W"), item("it-key", {keycode=3})}

tiles["1"] = {inherit("."), actor("ac-blackball", {player=0})}
tiles["2"] = {inherit("."), actor("ac-whiteball", {player=1, controllers=2})}
tiles["H"] = {inherit("."), actor("ac-horse", {force=1, target1="4 3", target2="4 9", target3="15 9", target4="15 3" })}
tiles["h"] = {inherit("."), actor("ac-horse", {force=1, target1="13 5", target2="13 7", target3="6 7", target4="6 5" })}

tiles["T"] = {inherit(" "), item("it-document",{text="help_keys"})}

if difficult then
    tiles[","] = tiles["."]
    tiles["K"] = tiles["#"]
    tiles["L"] = tiles["#"]
    tiles["M"] = tiles["#"]
    tiles["N"] = tiles["#"]
    tiles["P"] = {inherit(" "), stone("st-key", {action="callback", keycode=0, target="open_0"})}
    tiles["Q"] = {inherit(" "), stone("st-key", {action="callback", keycode=1, target="open_1"})}
    tiles["R"] = {inherit(" "), stone("st-key", {action="callback", keycode=2, target="open_2"})}
    tiles["S"] = {inherit(" "), stone("st-key", {action="callback", keycode=3, target="open_3"})}
else
    tiles[","] = tiles[" "]
    tiles["K"] = {inherit(" "), stone("st-key", {action="callback", keycode=0, target="open_0"})}
    tiles["L"] = {inherit(" "), stone("st-key", {action="callback", keycode=1, target="open_1"})}
    tiles["M"] = {inherit(" "), stone("st-key", {action="callback", keycode=2, target="open_2"})}
    tiles["N"] = {inherit(" "), stone("st-key", {action="callback", keycode=3, target="open_3"})}
    tiles["P"] = tiles["#"]
    tiles["Q"] = tiles["#"]
    tiles["R"] = tiles["#"]
    tiles["S"] = tiles["#"]
end

tiles["x"] = {inherit(" "), oxyd}

function open_0()
    kill_stone(19, 1)
    kill_stone(18, 0)
end
function open_1()
    kill_stone(19, 11)
    kill_stone(18, 12)
end
function open_2()
    kill_stone(0, 1)
    kill_stone(1, 0)
end
function open_3()
    kill_stone(0, 11)
    kill_stone(1, 12)
end

create_world_from_map( tiles, {
"x####R########P####x",
"#.....        ....2#",
"#..##############..#",
"#..#,,....H...,,#..#",
"M..#,####..####,#..K",
"#..#.#.c.h..c.#.#..#",
"#.Tk.#.m#bw#l.#.n .#",
"#..#.#........#.#..#",
"N..#,##########,#..L",
"#..#,,........,,#..#",
"#..##############..#",
"#1....        .....#",
"x####S########Q####x"
})
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:translation el:lang="de">In der Schusslinie von Dieben</el:translation>
      </el:string>
      <el:string el:key="help_keys">
        <el:english>Keys from left to right are NE, NW, SE, SW.</el:english>
        <el:translation el:lang="de">Die Schlüssel von links nach rechts sind NO, NW, SO, SW.</el:translation>
      </el:string>
    </el:i18n>
  </el:protected>
</el:level>

