Jump to content

Problem with dxRectangle


xeon17

Recommended Posts

Hello there

I'm trying to do dxRectangle, work with the server side SetTimer, as follows, where every 1 second it adds +1 dxRectangle size, and when the dxrectangle reach 250 size it disappears and starts the attack on the territory. Actually the problem is , when i get in the area , the dxRectangle apper and stay on 0 , it doesn't start.

Server

local _setElementData = setElementData 
function setElementData ( element, group, value ) 
    return _setElementData ( element, group, value, true ) 
end 
  
local turf = { } 
function createArea( x, y, w, h, owner, forcedId ) 
    local owner = tostring( owner ) or "server" 
    if not r then r = 255 end 
    if not g then g = 255 end 
    if not b then b = 255 end 
     
    if ( owner == "server" ) then 
        r, g, b = 255, 255, 255 
    end 
     
    local radar = createRadarArea ( x, y, w, h, r, g, b, 200 ) 
    local col = createColRectangle ( x, y, w, h ) 
    if ( not forcedId or turf[ id ] ) then 
        id = 0 
        while ( turf[id] ) do 
            id = id + 1000 
        end 
    else 
        id = forcedId 
    end 
     
    turf[id] = { } 
    turf[id].col = col 
    turf[id].area = radar 
    turf[id].owner = owner or "server" 
    turf[id].attackers = nil 
    turf[id].seg = 0 
    setElementData ( turf[id].col, "turfId", id ) 
    setElementData ( turf[id].col, "TurffingTable", turf[id] ) 
    addEventHandler ( "onColShapeHit", turf[id].col, onColShapeHit ) 
    addEventHandler ( "onColShapeLeave", turf[id].col, onColShapeLeave ) 
    return turf[id] 
  
end 
  
function onColShapeHit (player) 
    if ( player and isElement( player ) and getElementType ( player ) == "player" and not isPedInVehicle( player ) ) then 
        local gang = getPlayerTeam ( player ) 
         
        if ( not gang ) then 
            return outputChatBox ( "você não está em uma gang. Não pode dominar", player, 255, 255, 0 ) 
        end 
         
        local id = tonumber ( getElementData ( source, "turfId") ) 
        if ( turf[id].owner == gang ) then 
            return 
        end 
         
        if ( turf[id].attackers and turf[id].attackers ~= gang ) then 
            return outputChatBox ( " A gang "..getTeamName( turf[id].attackers).." ja está dominando este territorio", player, 255, 0, 0 ) 
        end 
         
        if ( not turf[id].attackers ) then 
            if ( not turf[id].owner == "server" ) then 
                outputChatBox ( "você está preparando - se para dominar esse territorio!", player, 255, 255, 0 ) 
                local x, y, z = getElementPosition( source ) 
                for i, v in pairs ( getPlayersInTeam(getPlayerTeam(player))) do 
                    if ( player ~= v ) then 
                        outputChatBox( getPlayerName( player ).. " está dominando o territorio "..getZoneName(x, y, z).." !", v, 255, 255, 0 ) 
                    end 
                end 
            else 
                outputChatBox ( "Você está preparando uma guerra no territorio. Se esconda, chame reforço e espere começar o ataque!", player, 255, 255, 0 ) 
                local x, y, z = getElementPosition ( source ) 
                for i, v in ipairs (getPlayersInTeam(getPlayerTeam(player))) do 
                    if ( player ~= v ) then 
                        outputChatBox ( getPlayerName( player ).." está preparando um ataque no territorio "..getZoneName(x,y,z)..", "..getZoneName(x,y,z,true).."! venham ajudar, o ataque começa em 2 minutos!", v, 255, 255, 0 ) 
                    end 
                end 
            end 
        end      
    end 
end 
  
function onColShapeLeave( player ) 
    if ( player and getElementType ( player ) == "player" ) then 
    triggerClientEvent ( player, "dominar_stop", player, turf[id] ) 
    end 
end 
  
setTimer( function ( ) 
for id, data in pairs ( turf ) do 
    if ( data.attackers ) then 
        local players = { attackers = { }, owners = { } } 
        local isGangInTurf = false 
        local isOwnerInTurf = false 
        for i, v in pairs ( getElementsWithinColShape ( data.col, "player") ) do 
            local g = getPlayerTeam ( v ) 
            if ( g == data.attackers ) then 
                isGangInTurf = true 
                table.insert ( players.attackers, v) 
            elseif ( g == data.owner ) then 
                isOwnerInTurf = true 
                table.insert( players.owners, v ) 
            end 
        end 
        local x, y, z = getElementPosition( data.col ) 
            if ( isGangInTurf ) then 
                if ( turf[id].owner == "server" ) then 
                    turf[id].seg = turf[id].seg + 1 
                    if ( turf[id].seg >= 250 ) then 
                        outputChatBox( "A gang "..getTeamName(turf[id].attackers).." dominou o territorio "..getZoneName( x, y, z).."!.", root, 0, 255, 0) 
                        setTurfOwner( id, turf[id].attackers ) 
                    end 
                end 
            end 
            for i, v in pairs ( players ) do 
                for k, p in pairs ( v ) do 
                end 
            end 
    end 
end 
end, 1000, 0) 
addEvent( "onTurfProgress", true) 
  
function table.len ( tb ) 
        local c = 0 
        for i, v in pairs ( tb ) do 
                c = c + 1 
        end 
        return c 
end 
  
function beginTurfWarOnTurf ( id ) 
        local d = turf[id] 
        local x, y, z = getElementPosition ( d.col ) 
        if ( d.owner == "server" ) then 
            return  
        end 
        outputChatBox ( "A gang "..getTeamName(d.attackers).." está atacando o territorio "..getZoneName ( x, y, z).." da gang "..getTeamName(d.owner).."!.", root, 255, 255, 0)  
        setRadarAreaColor ( d.radar, 255, 0, 0, 100 ) 
        setRadarAreaFlashing( d.radar, true) 
end 
  
function setTurfOwner ( id, owner ) 
        setRadarAreaFlashing ( turf[id].radar, false ) 
        turf[id].owner = owner 
        turf[id].attackers = nil 
        turf[id].attackProg = 0 
        local r, g, b = getTeamColor ( owner ) 
        setRadarAreaColor ( turf[id].radar, r, g, b, 180 ) 
        saveTurfs ( ) 
end 
  
function getTurfs ( ) 
        return turf 
end 
  
function saveTurfs ( ) 
        for id, data in pairs ( turf ) do 
                exports.NGSQL:db_exec ( "UPDATE areas SET owner=? WHERE id=?", data.owner, id ) 
        end 
        return true 
end 
  
addEventHandler( "onResourceStart", getResourceRootElement( getThisResource ( ) ), function ( ) 
        exports.NGSQL:db_exec ( "CREATE TABLE IF NOT EXISTS areas ( id INT, owner VARCHAR(50), x FLOAT, y FLOAT, z FLOAT, width INT, height INT )" ) 
        local query = exports.NGSQL:db_query ( "SELECT * FROM areas" ) 
        outputChatBox( tostring( query) ) 
        if #query == 0 then 
                outputChatBox('soherre') 
                local query = { } 
                local data = { 
                        { 232.160, -153.421, 400, 400 } 
                } 
  
                for i, v in pairs ( data ) do 
                        query[i] = { 
                                ['x'] = v[1], 
                                ['y'] = v[2], 
                                ['width'] = v[3], 
                                ['height'] = v[4], 
                                ['owner'] = "server" 
                        } 
                        local v = query[ i ] 
                        local owner, x, y, width, height = v['owner'], tonumber ( v['x'] ), tonumber ( v['y'] ), tonumber ( v['width'] ), tonumber ( v['height'] ) 
                        outputChatBox( tostring(owner) ) 
                        local turf = createArea ( x, y, width, height, owner ) 
                        outputChatBox( tostring( turf ) ) 
                        outputChatBox( 'tap' ) 
                end 
        else 
                outputChatBox('atap2') 
                for i=1, #query do 
                        local v = query[ i ] 
                        outputChatBox(" kya ") 
                        local id, owner, x, y, z, width, height = tonumber ( v['id'] ), v['owner'], tonumber ( v['x'] ), tonumber ( v['y'] ), tonumber ( v['width'] ), tonumber ( v['height'] ) 
                        outputChatBox( tostring(owner) ) 
                        createArea ( x, y, width, height, owner, id ) 
                end 
        end 
end ) 

Client

  
function dominar_start ( ) 
    addEventHandler ( "onClientRender", root, dominar_render ) 
end 
addEvent ( "dominar_start", true ) 
addEventHandler ( "dominar_start", root, dominar_start ) 
  
function dominar_stop ( ) 
    removeEventHandler ( "onClientRender", root, dominar_render ) 
end 
addEvent ( "dominar_stop", true ) 
addEventHandler ( "dominar_stop", root, dominar_stop ) 
  
function dominar_render () 
    local seg = 0 
    dxDrawRectangle(55, 478, seg, 20, tocolor(255, 100, 0, 255), false) 
    dxDrawLine(54, 478, 304, 478, tocolor(255, 255, 255, 255), 1, false) 
    dxDrawLine(304, 478, 304, 498, tocolor(255, 255, 255, 255), 1, false) 
    dxDrawLine(54, 498, 304, 498, tocolor(255, 255, 255, 255), 1, false) 
    dxDrawLine(54, 478, 54, 498, tocolor(255, 255, 255, 255), 1, false) 
    dxDrawText("atacando territorio!", 53, 478, 304, 498, tocolor(255, 255, 255, 255), 0.65, "bankgothic", "center", "center", false, false, false, false, false) 
end 

Link to comment

It's very easy all what you need to do is :

function dominar_render () 
 local seg = 0 
  
dxDrawRectangle(55, 478, seg, 20, tocolor(255, 100, 0, 255), false) 
dxDrawLine(54, 478, 304, 478, tocolor(255, 255, 255, 255), 1, false) 
dxDrawLine(304, 478, 304, 498, tocolor(255, 255, 255, 255), 1, false) 
dxDrawLine(54, 498, 304, 498, tocolor(255, 255, 255, 255), 1, false) 
dxDrawLine(54, 478, 54, 498, tocolor(255, 255, 255, 255), 1, false) 
dxDrawText("atacando territorio!", 53, 478, 304, 498, tocolor(255, 255, 255, 255), 0.65, "bankgothic", "center", "center", false, false, false, false, false) 
  
seg = seg + 1 
if ( seg >= 250 ) then 
seg = 0 
-- and if you want to remove the whole dominar_render function use removeEventHandler() 
 removeEventHandler ( "onClientRender", root, dominar_render ) 
end  
end 

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...