Sergey_Walter Posted April 16, 2013 Share Posted April 16, 2013 Как сделать в луа двумерный массив на подобие павновского: new OnZONE[150][24]; например чтоб использовать его в цикле: onZone[i][faction] = onZone[i][faction] + 1 вместо павновского: OnZONE[i][faction] ++; Помогите плиз, всю голову сломал, раньше как-то получалось, нечаянно скрипт удалил и всё забыл =( Link to comment
Sergey_Walter Posted April 16, 2013 Author Share Posted April 16, 2013 Вот мой скрипт системы гангзон.. mysql = exports.mysql local null = mysql_null() local toLoad = { } local threads = { } local zoneInfo = { } local frakCD = { } local onZone = { } function resume() for key, value in ipairs(threads) do coroutine.resume(value) end end function loadAllGangZones() local result = mysql:query("SELECT id FROM gangzones ORDER BY `id` ASC") if result then while true do local row = mysql:fetch_assoc(result) if not row then break end toLoad[tonumber(row["id"])] = true end mysql:free_result(result) for id in pairs( toLoad ) do local co = coroutine.create(loadOneGangZone) coroutine.resume(co, id, true) table.insert(threads, co) end setTimer(resume, 1000, 4) setTimer(gzCheck, 1000, 0) for i = 1, 4 do frakCD[i] = 0 end else outputDebugString( "loadAllGangZones failed" ) end end addEventHandler("onResourceStart", getResourceRootElement(), loadAllGangZones) function loadOneGangZone(id, hasCoroutine) if (hasCoroutine==nil) then hasCoroutine = false end local row = mysql:query_fetch_assoc("SELECT * FROM gangzones WHERE id = " .. mysql:escape_string(id) .. " LIMIT 1" ) if row then if (hasCoroutine) then coroutine.yield() end for k, v in pairs( row ) do if v == null then row[k] = nil else row[k] = tonumber(row[k]) or row[k] end end zoneInfo[row.id] = { ["x"] = row.x, ["y"] = row.y, ["zoneBattle"] = false, ["safeTime"] = 0, ["zoneTimer"] = 0, ["gangNapad"] = 0 } zoneInfo[row.id]["colShape"] = createColRectangle(zoneInfo[row.id]["x"], zoneInfo[row.id]["y"], 100.0, 100.0) if row.faction == 1 then zoneInfo[row.id]["radarArea"] = createRadarArea(zoneInfo[row.id]["x"], zoneInfo[row.id]["y"], 100.0, 100.0, 0, 255, 255, 175) elseif row.faction == 2 then zoneInfo[row.id]["radarArea"] = createRadarArea(zoneInfo[row.id]["x"], zoneInfo[row.id]["y"], 100.0, 100.0, 52, 201, 36, 175) elseif row.faction == 3 then zoneInfo[row.id]["radarArea"] = createRadarArea(zoneInfo[row.id]["x"], zoneInfo[row.id]["y"], 100.0, 100.0, 255, 0, 255, 175) elseif row.faction == 4 then zoneInfo[row.id]["radarArea"] = createRadarArea(zoneInfo[row.id]["x"], zoneInfo[row.id]["y"], 100.0, 100.0, 255, 165, 0, 175) end setElementData ( zoneInfo[row.id]["colShape"], "faction", row.faction ) setElementData ( zoneInfo[row.id]["colShape"], "type", "turf" ) setElementData ( zoneInfo[row.id]["colShape"], "ID", row.id) addEventHandler("onColShapeHit", getRootElement(), function(player) if(getElementData(source, "type") == "turf") then local id = getElementData(source, "ID") if(getPlayerTurf(player) == id) then setElementData(player, "turfID", id) end end end) end end function capture( thePlayer ) local gangid = getElementData(thePlayer, "turfID") if isInAllowed(thePlayer) ~= 0 then outputChatBox ("#ff0500x #ffffffВы не можете атаковать банды", thePlayer, 0,0,0, true) return true end --if frakCD[getElementData( thePlayer, "faction" )] > 0 then --outputChatBox ("#ff0500x #ffffffВаша банда уже атакует одну гангзону, подождите ".. math.floor(frakCD[getElementData( thePlayer, "faction" )] / 60) .." минут", thePlayer, 0,0,0, true) --return true --end if(getPlayerTurf(thePlayer) ~= gangid) then outputChatBox ("#ff0500x #ffffffВы не находитесь на територии банды", thePlayer, 0,0,0, true) return true end --if isPlayerInBandOnline(gangid) then -- outputChatBox ("#ff0500x #ffffffЗону банды которую вы хотите атаковать нет в онлайне", thePlayer, 0,0,0, true) -- return true -- end outputChatBox ("#ff0500x #ffffffВы начали захват гангзоны", thePlayer, 0,0,0, true) setRadarAreaFlashing ( zoneInfo[gangid]["radarArea"], true ) frakCD[getElementData( thePlayer, "faction" )] = 1800 zoneInfo[gangid]["zoneBattle"] = true zoneInfo[gangid]["safeTime"] = 10 zoneInfo[gangid]["zoneTimer"] = 200 zoneInfo[gangid]["gangNapad"] = getElementData(thePlayer, "faction") triggerClientEvent(thePlayer, "showGangText", getRootElement(), true) end addCommandHandler ( "capture", capture ) function gzcolor( thePlayer, command, gangid ) destroyElement(zoneInfo[getPlayerTurf(thePlayer)]["radarArea"]) mysql:query_free("UPDATE gangzones SET `faction` = '".. gangid .."' WHERE id='" .. getPlayerTurf(thePlayer) .. "'") loadOneGangZone(getPlayerTurf(thePlayer)) end addCommandHandler ( "gzcolor", gzcolor, gangid ) function isInAllowed( thePlayer ) local theTeam = getPlayerTeam( thePlayer ) local factionType = getElementData( theTeam, "type" ) return factionType end function isPlayerInBandOnline(gangid) local players = exports.pool:getPoolElementsByType("player") for k, arrayPlayer in ipairs(players) do if getElementData( arrayPlayer, "faction" ) == getElementData( zoneInfo[gangid]["colShape"], "faction" ) then return false end end return true end function getPlayerTurf(player) local x, y, _ = getElementPosition(player) for i, _ in ipairs(zoneInfo) do if(x >= zoneInfo[i]["x"] and y >= zoneInfo[i]["y"] and x <= (zoneInfo[i]["x"]+100.0) and y <= (zoneInfo[i]["y"]+100.0)) then return i end end return -1 end function gzCheck() local players = exports.pool:getPoolElementsByType("player") local kol1 = 0 local kol2 = 0 for x,_ in ipairs(frakCD) do if frakCD[x] > 0 then frakCD[x] = frakCD[x] - 1 end end for i,_ in pairs(zoneInfo) do if zoneInfo[i]["safeTime"] > 0 then zoneInfo[i]["safeTime"] = zoneInfo[i]["safeTime"] - 1 end if zoneInfo[i]["zoneBattle"] == true and zoneInfo[i]["safeTime"] == 0 then zoneInfo[i]["zoneTimer"] = zoneInfo[i]["zoneTimer"] - 5 if zoneInfo[i]["zoneTimer"] <= 0 then sendFamilyMessage (zoneInfo[i]["gangNapad"], getElementData( zoneInfo[i]["colShape"], "faction" ), "#ff0500x #ffffffВремя на захват зоны вышел. Ничья" ) zoneInfo[i]["zoneBattle"] = false setRadarAreaFlashing ( zoneInfo[i]["radarArea"], false ) for k, arrayPlayer in ipairs(players) do local faction = getElementData(arrayPlayer, "faction") if faction == getElementData( zoneInfo[i]["colShape"], "faction" ) or faction == zoneInfo[i]["gangNapad"] then triggerClientEvent(arrayPlayer, "showGangText", getRootElement(), false) end end end for k, arrayPlayer in ipairs(players) do local faction = getElementData(arrayPlayer, "faction") if getPlayerTurf(arrayPlayer) == i and isInAllowed(arrayPlayer) == 0 and faction == getElementData( zoneInfo[i]["colShape"], "faction" ) or faction == zoneInfo[i]["gangNapad"] then local faction = getElementData(arrayPlayer, "faction") onZone[i][faction] = onZone[i][faction] + 1 end end local asd = 0 for x,_ in ipairs(onZone) do if onZone[i][x] > 0 then asd = asd + 1 end end if asd == 1 then for x,_ in ipairs(onZone) do if onZone[i][x] > 0 then if getElementData( zoneInfo[i]["colShape"], "faction" ) == x then sendFamilyMessage (zoneInfo[i]["gangNapad"], getElementData( zoneInfo[i]["colShape"], "faction" ), "#ff0500x #ffffffБанда ".. GetGangName(getElementData( zoneInfo[i]["colShape"], "faction" )) .." отстояла свою территорию" ) else sendFamilyMessage (zoneInfo[i]["gangNapad"], getElementData( zoneInfo[i]["colShape"], "faction" ), "#ff0500x #ffffffБанда ".. GetGangName(zoneInfo[i]["gangNapad"]) .." выигрывает войну за территорию" ) end for k, arrayPlayer in ipairs(players) do local faction = getElementData(arrayPlayer, "faction") if faction == getElementData( zoneInfo[i]["colShape"], "faction" ) or faction == zoneInfo[i]["gangNapad"] then triggerClientEvent(arrayPlayer, "showGangText", getRootElement(), false) end end zoneInfo[i]["zoneBattle"] = false zoneInfo[i]["zoneTimer"] = 0 onZone[i][x] = 0 mysql:query_free("UPDATE gangzones SET `faction` = '".. x .."' WHERE id='" .. i .. "'") setRadarAreaFlashing ( zoneInfo[i]["radarArea"], false ) destroyElement(zoneInfo[i]["radarArea"]) loadOneGangZone(i) break end end end end if zoneInfo[i]["zoneBattle"] == true then for k, arrayPlayer in ipairs(players) do local faction = getElementData(arrayPlayer, "faction") if faction == getElementData( zoneInfo[i]["colShape"], "faction" ) or faction == zoneInfo[i]["gangNapad"] then triggerClientEvent(arrayPlayer, "showGangText", getRootElement(), true) if faction == getElementData( zoneInfo[i]["colShape"], "faction" ) and getPlayerTurf(arrayPlayer) == i then kol1 = kol1 + 1 end if faction == zoneInfo[i]["gangNapad"] and getPlayerTurf(arrayPlayer) == i then kol2 = kol2 + 1 end triggerClientEvent(arrayPlayer, "varText", getRootElement(), kol1, kol2, GetGangName(getElementData( zoneInfo[i]["colShape"], "faction" )), GetGangName(zoneInfo[i]["gangNapad"]), zoneInfo[i]["safeTime"]) end end end end end function sendFamilyMessage(gangnapad, gangvlad, text) local players = exports.pool:getPoolElementsByType("player") for k, arrayPlayer in ipairs(players) do if getElementData(arrayPlayer, "faction") == gangnapad or getElementData(arrayPlayer, "faction") == gangvlad then outputChatBox (text, arrayPlayer, 0,0,0, true) end end end function GetGangName(gangid) local string if gangid == 1 then string = "Los Santos Aztecaz" end if gangid == 2 then string = "Groove Street" end if gangid == 3 then string = "The Ballas Gang" end if gangid == 4 then string = "Los Santos Vagos Gang" end return string end Link to comment
Kernell Posted April 16, 2013 Share Posted April 16, 2013 http://ilovelua.narod.ru/about_lua.html#Tables Link to comment
Kenix Posted April 16, 2013 Share Posted April 16, 2013 local a = {}; a[ 1 ] = {}; a[ 1 ][ 1 ] = '1) text'; a[ 1 ][ 2 ] = '1) text2'; a[ 2 ] = {}; a[ 2 ][ 1 ] = '2) text'; a[ 2 ][ 2 ] = '2) text2'; print( a[ 1 ][ 1 ], a[ 2 ][ 2 ] ); --[[ Соответственно выведет: 1) text 2) text2 ]] Памоему тут всё просто. А вообще нужно документацию читать, ссылка выше. Link to comment
Sergey_Walter Posted April 16, 2013 Author Share Posted April 16, 2013 for i = 1, 150 do for x = 1, 4 do onZone = { } onZone[i] = { } onZone[i][x] = 0 end end Так можно делать? У меня как никрути вылазит эти ошибки.. http://screenshot.su/show.php?img=de6e3 ... 917873.jpg Мне просто нужно записать в эту строку два значения, onZone[faction] = onZone[faction] + 1 там где i индекс гангзоны, там где faction номер банды, и к этому нужно прибавить единицу в зависимости сколько там стоит человек.. Link to comment
Kenix Posted April 16, 2013 Share Posted April 16, 2013 local onZone = {}; for i = 1, 50 do onZone[ i ] = {}; for i2 = 1, 4 do onZone[ i ][ i2 ] = 0; end end И вообще вы можете и не использовать эти таблицы. Создайте элемент GangZone, создаёте кулшейп и сделайте его предком этого элемента, и также ввиде элемент даты вы можете хронить игроков и т.д. Функции для работы createElement setElementParent getElementParent getElementChildren destroyElement MTA это не SAMP, не стоит недооценивать дерево элементов, она очень развита. Разве не удобно, когда вы создаёте один элемент, а он уже содержит все данные? Кулшейп, радарарену, блип и т.д И если вы уничтожите этот элемент, то все эти элементы также будут уничтожены. Если вам нужны примеры, то пишите. Link to comment
Sergey_Walter Posted April 17, 2013 Author Share Posted April 17, 2013 Ну вот, если я например сделаю так, local interiorElement = createElement("interior", "int"..tostring(row.id)) setElementDataEx(interiorElement, "dbid", row.id, true) setElementDataEx(interiorElement, "entrance", { row.x, row.y, row.z, row.interiorwithin, row.dimensionwithin, row.angle, row.fee }, true ) setElementDataEx(interiorElement, "exit", { row.interiorx, row.interiory, row.interiorz, row.interior, row.id, row.angleexit, 0 }, true ) setElementDataEx(interiorElement, "status", { row.type, row.disabled == 1, row.locked == 1, row.owner, row.cost, row.supplies}, true ) setElementDataEx(interiorElement, "name", row.name, true ) if row.owner == -1 then local blip = createBlip(row.x, row.y, 10, 31, 2, 255, 0, 0, 255, 0, 180) setElementData ( interiorElement, "blip", blip ) elseif row.owner > 0 then local blip = createBlip(row.x, row.y, 10, 32, 2, 255, 0, 0, 255, 0, 180) setElementData ( interiorElement, "blip", blip ) end и удалю этот элемент, destroyElement(interiorElement) то всё что в нём хранилось, данные и иконка на радаре удалится? Link to comment
Kenix Posted April 17, 2013 Share Posted April 17, 2013 Вам нужно использовать setElementParent Link to comment
Sergey_Walter Posted April 20, 2013 Author Share Posted April 20, 2013 Вам нужно использовать setElementParent Да действительно эта функция помогает удалять и пикапы и иконки, буду изучать эти древа элементов =) Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now