logoHourGlass
演示

演示

--  ***************************************************************************************************** --
--  code by http://www.Cgdev.me
--  地圖庫Map示範腳本
--                   #更新日誌#
--  TYPE ***** TIME ***** Editor ***** Text 
--  CREATE  2013/01/19     Free
--  ADD     2013/01/20     Free        針對Map庫的函數進行示範性演示,全部使用全局的Talk事件觸發

--                   #基本設置#
--  定義Lua地圖的MAPID,不可修改
local LUAMAPID = %地圖類型_LUAMAP%;
--                   #使用說明#
--  在遊戲中輸入如下命令併發出,即可觸發

--  copymap,地圖類型,地圖ID                       --> 複製指定地圖
--  mazemap,x座標範圍,y座標範圍,地圖名         --> 製作指定大小隨機地圖
--  delmap,地圖ID                             --> 刪除指定Lua地圖
--  getpos,地圖ID                             --> 獲取指定Lua生成的隨機地圖中的隨機可用的座標點
--  getimage                                    --> 獲取當前人物座標的地板圖檔號和物件圖檔號
--  setimage                                    --> 設置當前人物座標的地板/物件圖檔號
--  dumpmap,路徑,地圖ID,寫入ID                    --> 將指定Lua地圖生成成地圖文件保存在指定路徑中

--                   #注意事項#
--  生成隨機地圖時候,會暫停遊戲中本身隨機地圖的生成,如果隨機地圖設置過大,可能需要很長時間才能生成好地圖,請自行測試。
--  makeMazeMap函數中調用的Map.MakeMazeMap的各項參數請查閱相關文檔進行查看,也可在論壇/QQ羣中進行討論
--  該腳本並未經過完全的測試,如遇問題,絕對不稀奇。
--  ***************************************************************************************************** --

--註冊事件委託
Delegate.RegDelTalkEvent("MapProc_TalkEvent");

--  創建複製地圖
--  參數定義    index: 玩家對象
--              c_mapid: 要複製的地圖的Map ID
--              c_floor: 要複製的地圖的Floor ID
function makeCopyMap(index, c_mapid, c_floor)
    local newFloorID = Map.MakeCopyMap(c_mapid, c_floor);
    if(newFloorID == -1)then
        NLG.SystemMessage(index, "地圖複製失敗。");
    else
        NLG.SystemMessage(index, "地圖"..c_mapid..","..c_floor.."已經成功複製到"..LUAMAPID..","..newFloorID);
        NLG.SystemMessage(index, "使用GM命令[warp "..LUAMAPID.." "..newFloorID.." x座標 y座標] 可以移動過去看看哦");
    end
    return;
end

--  創建隨機地圖
--  參數定義    index: 玩家對象
--              xsiz: 要生成的地圖的x座標範圍
--              ysiz: 要生成的地圖的y座標範圍
--              mapName: 地圖名
--  函數說明:MakeMazeMap是需要定義回調函數的,當生成完畢後,會觸發定義的回調函數,返回生成的地圖是否完畢
function makeMazeMap(index, xsiz, ysiz, mapName)
    --Map.MakeMazeMap參數簡要說明
    --[[
        ----------------------------
        回調函數所在文件
        回調函數名
        地圖x座標最大值
        地圖y座標最大值
        地圖名
        ---------------------------- 可選參數,定義地圖調色板號
        調色板ID
        ---------------------------- 可選參數,定義隨機生成地圖的一些變量
        隨機地圖塊大小
        隨機地圖塊x座標最小值
        隨機地圖塊y座標最小值
        隨機地圖塊x座標最大值
        隨機地圖塊y座標最大值
        ---------------------------- 可選參數,定義地圖圖檔信息
        地圖地板圖檔編號
        地圖其他圖檔編號
        地圖其他物件編號
        ---------------------------- 可選參數,定義地圖牆面,如果全部寫0,則會自動生成洞窟牆面
        牆橫向圖檔編號
        牆橫向反向圖檔編號
        牆縱向圖檔編號
        牆縱向反向圖檔編號
        牆相交圖檔編號
        ----------------------------
    --]]
    local newFloorID = Map.MakeMazeMap(nil,"mazeMapDoneCall",xsiz,ysiz,mapName,0,30,30,30,30,30,9491,100,0,0,0,0,0,0);
    if(newFloorID == -1)then
        NLG.SystemMessage(index, "地圖生成失敗。");
    else
        NLG.SystemMessage(index, "地圖"..LUAMAPID..","..newFloorID.."已經開始生成了...請稍後");
    end
    return;
end

--  創建隨機地圖的回調函數
--  參數定義    floorID: 生成的地圖的Floor ID
--              doneflg: 生成結果
function mazeMapDoneCall(floorID, doneflg)
    if(doneflg == 1)then
        NLG.SystemMessage(-1,"生成地圖"..LUAMAPID..","..floorID.."成功!");
        NLG.SystemMessage(index, "可以通過使用getpos,"..floorID.."來獲取一個合法的座標點");
    else
        NLG.SystemMessage(-1,"生成地圖"..LUAMAPID..","..floorID.."失敗!");
    end
end

--  獲取隨機地圖可用的座標
--  參數定義    index: 玩家對象
--              floorID: 隨機地圖的Floor ID
function getPosition(index, floorID)
    local mapx, mapy = Map.GetAvailablePos(floorID);
    if(mapx == 0 and mapy == 0)then
        NLG.SystemMessage(index,"獲取地圖可用座標失敗,請重試");
    else
        NLG.SystemMessage(index,"獲取地圖"..LUAMAPID..","..floorID.."可用座標"..mapx..","..mapy);
        NLG.SystemMessage(index, "使用GM命令[warp "..LUAMAPID.." "..floorID.." "..mapx.." "..mapy.."] 可以移動過去看看哦");
    end
    return;
end

--  獲取當前玩家座標的地板圖檔號與物件(obj)圖檔號
--  參數定義    index: 玩家對象
function getTileandObj(index)
    local nowMap = Char.GetData(index, %對象_MAP%);
    local nowFloor = Char.GetData(index, %對象_地圖%);
    local nowXpos = Char.GetData(index, %對象_X%);
    local nowYpos = Char.GetData(index, %對象_Y%);
    local tile, obj = Map.GetImage(nowMap,nowFloor,nowXpos,nowYpos);
    NLG.SystemMessage(index,"獲取地圖"..nowMap..","..nowFloor..","..nowXpos..","..nowYpos.."的地圖元素[地板:"..tile.."],[物件:"..obj.."]");
    return;
end

--  設置當前玩家座標的地板圖檔號與物件(obj)圖檔號,函數會自動判定圖檔屬於地板還是物件
--  參數定義    index: 玩家對象
--              image: 圖檔號
function setTileandObj(index, image)
    local nowMap = Char.GetData(index, %對象_MAP%);
    local nowFloor = Char.GetData(index, %對象_地圖%);
    local nowXpos = Char.GetData(index, %對象_X%);
    local nowYpos = Char.GetData(index, %對象_Y%);
    local ori_tile, ori_obj = Map.GetImage(nowMap,nowFloor,nowXpos,nowYpos);
    Map.SetImage(nowMap,nowFloor,nowXpos,nowYpos, image);
    local now_tile, now_obj = Map.GetImage(nowMap,nowFloor,nowXpos,nowYpos);
    NLG.SystemMessage(index,"地圖"..nowMap..","..nowFloor..","..nowXpos..","..nowYpos.."的地圖元素變更[地板:"..ori_tile.."->"..now_tile.."],[物件:"..ori_obj.."->"..now_obj.."]");
    return;
end

--  輸出地圖文件到本地路徑中
--  參數定義    index: 玩家對象
--              path: 本地路徑,不包含文件名,文件夾以"\"結尾
--              floorID: Lua地圖的地圖Floor號
--              writeAsFloor: 寫出文件中定義的地圖編號
function dumpMapFile(index, path, floorID, writeAsFloor)
    local ret = Map.DumpLuaMap(path, floorID, writeAsFloor);
    if (ret == 0) then
        NLG.SystemMessage(index,"地圖"..LUAMAPID..","..floorID.."輸出至文件失敗!");
    else
        NLG.SystemMessage(index,"地圖"..LUAMAPID..","..floorID.."已經輸出至文件:["..path..LUAMAPID.."_"..floorID.."]");
    end
    return;
end

--  刪除Lua生成的地圖,釋放地圖編號
--  參數定義    floorID: Lua地圖的地圖Floor編號
function deleteLuaMap(index, floorID)
    local ret = Map.DelLuaMap(floorID);
    if (ret == 0) then
        NLG.SystemMessage(index,"地圖"..LUAMAPID..","..floorID.."刪除失敗!");
    else
        NLG.SystemMessage(index,"地圖"..LUAMAPID..","..floorID.."已經成功刪除!");
    end
    return;
end

--  NL.AllTaklEventCallBack的委託調用
--  參數定義    http://lua.cgdev.me/doku.php?id=lua:nl:callback:talkcallback
function MapProc_TalkEvent(index, msg, col, range, size)
    local token = Split(msg,",")
    if(string.find(msg,","))then
        local token = Split(msg,",")
    end
    if(string.find(msg,"copymap"))then
        local m = tonumber(token[2]);
        local f = tonumber(token[3]);
        makeCopyMap(index, m ,f);
        return 1;
    end
    if(string.find(msg,"mazemap"))then
        local xsiz = tonumber(token[2]);
        local ysiz = tonumber(token[3]);
        local MapName = token[4];
        makeMazeMap(index, xsiz, ysiz, MapName);
        return 1;
    end
    if(string.find(msg,"delmap"))then
        local f = tonumber(token[2]);
        deleteLuaMap(index, f);
        return 1;
    end
    if(string.find(msg,"getpos"))then
        local f = tonumber(token[2]);
        getPosition(index, f);
        return 1;
    end
    if(msg=="getimage")then
        getTileandObj(index);
        return 1;
    end
    if(string.find(msg,"setimage"))then
        image = tonumber(token[2]);
        setTileandObj(index, image);
        return 1;
    end
    if(string.find(msg,"dumpmap"))then
        path = token[2];
        floorID = tonumber(token[3]);
        writeAsFloor = tonumber(token[4]);
        dumpMapFile(index, path, floorID, writeAsFloor);
        return 1;
    end
end