<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE MudletPackage>
<MudletPackage version="1.0">
    <TriggerPackage/>
    <TimerPackage/>
    <AliasPackage>
        <AliasGroup isActive="yes" isFolder="yes">
            <name>cMud Map Importer</name>
            <script></script>
            <command></command>
            <packageName></packageName>
            <regex></regex>
            <Alias isActive="yes" isFolder="no">
                <name>Create map from cmud</name>
                <script>local ok, msg = cmi:importAll()
if ok then
  echo(&quot;&lt;green&gt;cMud map successfully imported.  Use 'centerview #' in a script to orient yourself, where # is a room id\n&quot;)
else
  echo(&quot;&lt;firebrick&gt;Didn't import; &quot;..msg..&quot;.\n&quot;)
end</script>
                <command></command>
                <packageName></packageName>
                <regex>^cmud importmap$</regex>
            </Alias>
            <Alias isActive="yes" isFolder="no">
                <name>purgemap</name>
                <script>--this method is meant for a clean import from cmud
local maxid = table.maxn(getRooms())--createRoomID()
for i = 1, maxid do
  deleteRoom(i)
end
--delete labels for all areas
local areatable = getAreaTable()
for i,v in pairs(areatable) do
	local labels = getMapLabels(v)
	if type(labels) == 'table' then
		for j,k in pairs(labels) do
			deleteMapLabel(v,j)
		end
	end
	deleteArea(v)
end
cecho(&quot;&lt;green&gt;Wiped all Mudlet Mapper map data.\n&quot;)</script>
                <command></command>
                <packageName></packageName>
                <regex>^cmud purgemap$</regex>
            </Alias>
            <Alias isActive="yes" isFolder="no">
                <name>Clear sqlite connection</name>
                <script>cmi:getConnection()</script>
                <command></command>
                <packageName></packageName>
                <regex>^cmud resetConnection$</regex>
            </Alias>
            <Alias isActive="yes" isFolder="no">
                <name>Import rooms</name>
                <script>cmi:checkConnection()
cmi:addRooms(cmi.con)</script>
                <command></command>
                <packageName></packageName>
                <regex>^cmud addRooms$</regex>
            </Alias>
            <Alias isActive="yes" isFolder="no">
                <name>Color rooms</name>
                <script>cmi:checkConnection()
cmi:colorRooms(cmi.con)</script>
                <command></command>
                <packageName></packageName>
                <regex>^cmud colorRooms$</regex>
            </Alias>
            <Alias isActive="yes" isFolder="no">
                <name>Import Favorites</name>
                <script>cmi:checkConnection()
cmi:addFavorites(cmi.con)</script>
                <command></command>
                <packageName></packageName>
                <regex>^cmud addFavs$</regex>
            </Alias>
            <Alias isActive="yes" isFolder="no">
                <name>Label Zones</name>
                <script>cmi:checkConnection()
cmi:labelCmudZones(cmi.con)</script>
                <command></command>
                <packageName></packageName>
                <regex>^cmud labelZones$</regex>
            </Alias>
        </AliasGroup>
    </AliasPackage>
    <ActionPackage/>
    <ScriptPackage>
        <ScriptGroup isActive="yes" isFolder="yes">
            <name>cMud Map Importer</name>
            <packageName></packageName>
            <script>-------------------------------------------------
--         Put your Lua functions here.        --
--                                             --
-- Note that you can also use external Scripts --
-------------------------------------------------
if not cmi then
	cmi = {}
end</script>
            <eventHandlerList/>
            <Script isActive="yes" isFolder="no">
                <name>CreateMapFromCmud</name>
                <packageName></packageName>
                <script>-------------------------------------------------
--         Put your Lua functions here.        --
--                                             --
-- Note that you can also use external Scripts --
-------------------------------------------------
require &quot;luasql.sqlite3&quot;
--kept as reminders
--[[dirnums = {[1]=&quot;north&quot;, [2]=&quot;northeast&quot;, [3]=&quot;northwest&quot;,[4]=&quot;east&quot;,
[5]=&quot;west&quot;,[6]=&quot;south&quot;,[7]=&quot;southeast&quot;,[8]=&quot;southwest&quot;,[9]=&quot;up&quot;,[10]=&quot;down&quot;}
dir_list=[&quot;north&quot;,&quot;n&quot;,&quot;northeast&quot;,&quot;ne&quot;,&quot;east&quot;,&quot;e&quot;,&quot;se&quot;,&quot;southeast&quot;,
          &quot;south&quot;,&quot;s&quot;,&quot;sw&quot;,&quot;southwest&quot;,&quot;west&quot;,&quot;w&quot;,&quot;nw&quot;,&quot;northwest&quot;,
          &quot;up&quot;,&quot;u&quot;,&quot;down&quot;,&quot;d&quot;, &quot;special&quot;, &quot;special&quot;]
dirtype_list=[0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9, 10, 10]]
cmi.cmudToMudletExit = {[0]=1,[1]=2,[2]=4,[3]=7,[4]=6,[5]=8, [6]=5, [7]=3, [8]=9, [9]=10}

function cmi:colorRooms(con)
        local recs, row, roomID, colors, r,g, b, cTable
        local envsUsed = {}
        local envIndex = 500
        recs = assert(con:execute([[SELECT ObjId, Color FROM ObjectTbl]]))
        row = recs:fetch({})
        while row do
                roomID = tonumber(row[1])
                colors = cmi:decimalToRGB(tonumber(row[2]))
                r = colors['r']
                b = colors['b']
                g = colors['g']
                if envsUsed[row[10]] then
                        cTable = envsUsed[row[2]]
                else
                        envsUsed[row[2]] = envIndex
                        cTable = envIndex
                        setCustomEnvColor(cTable, r,g,b,255)
                        envIndex=envIndex+1
                end
                setRoomEnv(roomID, cTable)
                row = recs:fetch({})
        end
        recs:close()
end

function cmi:addRooms(con)
        local recs, row, roomID, roomname, roomdesc, x, y, z, cost, zone, script
        recs = assert(con:execute([[SELECT ObjId, Name, Desc, X,Y,Z, Cost, ZoneID, Script FROM ObjectTbl]]))
        row = recs:fetch({})
        while row do
                roomID = tonumber(row[1])
                roomname = row[2]
                roomdesc = row[3]
                x = tonumber(row[4])
                y = tonumber(row[5])*-1 --need to reflect on y axis
                z = tonumber(row[6])
                x = x/30
                y = y/30
                cost = tonumber(row[7])
                zone = tonumber(row[8])
                script = row[9]
                if script then
                        script = string.gsub(script, &quot;All[=]?&quot;, &quot;&quot;, 1)
                end
                addRoom( roomID )
                if roomname then
                        setRoomName( roomID, roomname )
                else
                        echo(&quot;\nnull roomname:&quot;..roomID)
                end
                if roomdesc then
                        setRoomUserData(roomID, &quot;description&quot;, roomdesc)
                else
                        echo(&quot;\nnull roomdesc:&quot;..roomID)
                end
                if script and script ~= &quot;&quot; then
                        setRoomUserData(roomID, &quot;script&quot;, script)
                end
                setRoomCoordinates(roomID, x,y,z)
                setRoomWeight(roomID, cost)
                if not zone then
                        zone = 0
                end
                setRoomArea( roomID, zone )
                row = recs:fetch({})
        end
		  centerview(roomID)
        recs:close()
end

function cmi:addAreas(con)
	local recs, row, zoneId, Name, row
	recs = assert(con:execute([[SELECT ZoneId, Name FROM ZoneTbl]]))
	row = recs:fetch({})
	while row do
		ZoneId = tonumber(row[1])
		Name = row[2]
			setAreaName(ZoneId, Name)
		row = recs:fetch({})
	end
	recs:close()
end

function cmi:addExits(con)
	local recs, row, FromID, ToID, dirtype, name
	recs = assert(con:execute([[SELECT FromID, ToID, DirType, Name FROM ExitTbl]]))
	row = recs:fetch({})
	while row do
		FromID = tonumber(row[1])
		ToID = tonumber(row[2])
		dirtype = cmi.cmudToMudletExit[tonumber(row[3])]
		name = row[4]
		if name ~= &quot;&quot; and name then
			addSpecialExit(FromID, ToID, name)
		else
			--no special exit, do it normally
			if dirtype and dirtype ~= -1 then
				setExit(FromID, ToID, dirtype)
			end
		end
		row = recs:fetch({})
	end
	recs:close()
end

function cmi:labelCmudZones(con)
	local recs, row, exitrecs, toID, zonerecs, toZone, fromZone, fromID, zonerec, zoneFetch, zoneName, input
	recs = assert(con:execute([[SELECT ObjId, X,Y,Z, ZoneID, Script FROM ObjectTbl]]))
	row = recs:fetch({})
	while row do
		fromID = tonumber(row[1])
		fromZone = tonumber(row[5])
		exitrecs = assert(con:execute([[SELECT ToID FROM ExitTbl WHERE FromID = ]]..fromID))
		toID = exitrecs:fetch({})
		while toID do
			zonerecs = assert(con:execute([[SELECT ZoneID FROM ObjectTbl WHERE ObjId = ]]..tonumber(toID[1])))
			toZone = tonumber(zonerecs:fetch({})[1])
			if toZone ~= fromZone then
				zonerec = assert(con:execute([[SELECT ZoneId, Name FROM ZoneTbl WHERE ZoneId = ]]..toZone))
				zoneFetch = zonerec:fetch({})
				zoneName = zoneFetch[2]
				input = fromID..&quot; white black &quot;..zoneName
				cmi:roomLabel(input)
			end
			toID = exitrecs:fetch({})
		end
		row = recs:fetch({})
	end
    if zonerec then
        zonerec:close()
    end
	zonerecs:close()
	exitrecs:close()
	recs:close()
end

function cmi:addFavorites(con)
	local recs, row, objId, Name
	recs = assert(con:execute([[SELECT Name, ObjID FROM FavTbl]]))
	row = recs:fetch({})
	while row do
		Name = row[1]
		objId = tonumber(row[2])
		setRoomUserData(objId, &quot;favorite&quot;, Name)
		row = recs:fetch({})
	end
	recs:close()
end

function cmi:getConnection()
	if cmi.con then
		cmi.con:close()
	end
	if cmi.env then
		cmi.env:close()
	end
	cmi.env = assert (luasql.sqlite3())
	-- connect to data source
	cmi.file = invokeFileDialog(true, &quot;Select your cMud Map&quot;)
	cmi.con = assert (cmi.env:connect(cmi.file))
end

function cmi:checkConnection()
	if not cmi.env or not cmi.file or not cmi.con then
		cmi:getConnection()
	end
end

function cmi:importAll()
	cmi:checkConnection()
	-- create environment object
    --set area names
    cmi:addAreas(cmi.con)
    cmi:addRooms(cmi.con)
    cmi:addExits(cmi.con)
    --cmi:colorRooms(cmi.con)
   cmi:addFavorites(cmi.con)
	cmi:labelCmudZones(cmi.con)
end</script>
                <eventHandlerList/>
            </Script>
            <Script isActive="yes" isFolder="no">
                <name>24bit to 8bit color converter</name>
                <packageName></packageName>
                <script>-------------------------------------------------
--         Put your Lua functions here.        --
--                                             --
-- Note that you can also use external Scripts --
-------------------------------------------------
function cmi:decimalToRGB(num)
        require(&quot;bit&quot;)
        local r,g,b
        r = bit.band(num,0x0000ff)
        g = bit.band(num, 0x00ff00)
        g = bit.rshift(g,8)
        b = bit.band(num, 0xff0000)
        b = bit.rshift(b,16)
        return {['r']=r,['g']=g,['b']=b}
end</script>
                <eventHandlerList/>
            </Script>
            <Script isActive="yes" isFolder="no">
                <name>Room Labelling</name>
                <packageName></packageName>
                <script>-------------------------------------------------
--         Put your Lua functions here.        --
--                                             --
-- Note that you can also use external Scripts --
-------------------------------------------------
-- room label the room I'm in
-- room label 342 this is a label in room 342
-- room label green this is a green label where I'm at
-- room label green black this is a green to black label where I'm at
-- room label 34 green black this is a green to black label at room 34
-- how it works: split input string into tokens by space, then determine
-- what to do by checking first few tokens, and finally call the local
-- function with the proper arguments
--taken from Vadi
--input is room number, fgcolor, bgcolor, message
function cmi:roomLabel(input)
  local tk = input:split(&quot; &quot;)
  local room, fg, bg, message

  -- input always have to be something, so tk[1] at least always exists
  if tonumber(tk[1]) then
    room = tonumber(table.remove(tk, 1)) -- remove the number, so we're left with the colors or msg
  end

  -- next: is this a foreground color?
  if tk[1] and color_table[tk[1]] then
    fg = table.remove(tk, 1)
  end

  -- next: is this a backround color?
  if tk[1] and color_table[tk[1]] then
    bg = table.remove(tk, 1)
  end

  -- the rest would be our message
  if tk[1] then
    message = table.concat(tk, &quot; &quot;)
  end

  -- if we haven't provided a room ID and we don't know where we are yet, we can't make a label
  if not room then
    echo(&quot;We don't know where we are to make a label here.&quot;) return
  end

  local x,y,z = getRoomCoordinates(room)
  local f1,f2,f3 = unpack(color_table[fg])
  local b1,b2,b3 = unpack(color_table[bg])
  local lZ, lF, lT, lS = 30, 14, true, false

  -- finally: do it :)
  if (z) then
  local lid = createMapLabel(getRoomArea(room), message, x, y, z, f1,f2,f3, b1,b2,b3, lZ,lF,lT,lS)
  --echo(string.format(&quot;Created new label #%d '%s' in %s.&quot;, lid, message, getRoomAreaName(getRoomArea(room))))
	end
end</script>
                <eventHandlerList/>
            </Script>
        </ScriptGroup>
    </ScriptPackage>
    <KeyPackage>
        <HelpPackage>
            <helpURL></helpURL>
            <helpHTML></helpHTML>
        </HelpPackage>
    </KeyPackage>
</MudletPackage>
