Jump to content

open door group


No4NaMe

Recommended Posts

local count = 0
local DoorsTable = {}
function onStart()
	if not fileExists("db/bases.db") then
		local h = fileCreate("db/bases.db")
		if h then
		fileClose(h)
		outputDebugString("[DayZ] Bases database not found, creating...")
		end
		db = dbConnect( "sqlite", "db/bases.db" )
		outputDebugString("[DayZ] Inserting tables into database...")
		dbExec(db, "CREATE TABLE IF NOT EXISTS base_objects(id INT AUTO_INCREMENT, model INT, owner VARCHAR, x FLOAT, y FLOAT, z FLOAT, rx FLOAT, ry FLOAT, rz FLOAT, health FLOAT, encampment VARCHAR)")
		outputDebugString("[DayZ] Tables inserted.")
	else
		db = dbConnect( "sqlite", "db/bases.db" )
		local qh = dbQuery( db, "SELECT * FROM base_objects" )
		local result = dbPoll( qh, 10000 )
			for i, ob in ipairs(result) do
				local tOb = createObject(ob['model'], ob['x'], ob['y'], ob['z'], ob['rx'], ob['ry'], ob['rz'])
				setTimer(function()
					if ob['health'] > 0 then
						triggerClientEvent("setTheObjectUnbreakable",root,tOb)
					end
				end,1000,1,tOb)
				setElementData(tOb, "bc.creator", ob['owner'])
				setElementData(tOb, "bc.ID", ob['id'])
				if ob['model'] == 1502 or ob['model'] == 1495 then
					setupDoor(tOb,ob['encampment'])
				end
				setElementData(tOb,"object.health",ob['health'])
				if getElementData(tOb,"object.health") <= 0 then
					if getElementData(tOb,"parent") then
						destroyElement(getElementData(tOb,"parent"))
					end
					destroyElement(tOb)
				end
				count = count + 1
			end
			outputDebugString("[DayZ] Base objects loaded. TOTAL: "..tostring(count))
	end
end
addEventHandler("onResourceStart", resourceRoot, onStart)

function newObject(model,x,y,z,rx,ry,rz,health)
	if model and x and y and z and rx and ry and rz then
		local ob = createObject(model, x, y, z, rx, ry, rz)
		local acName = getAccountName(getPlayerAccount(client))
		setElementData(ob, "bc.creator", acName)
		setElementData(ob,"object.health",health)
		triggerClientEvent("setTheObjectUnbreakable",root,ob)
		local encampment = getElementData(client,"Group")
		if model == 3093 or model == 3029 then
			setupDoor(ob,encampment)
		end
		if ob then
			local x,y,z = getElementPosition(ob)
			local rx,ry,rz = getElementRotation(ob)
			local model = getElementModel(ob)
			count = count+1
			dbExec(db, "INSERT INTO base_objects VALUES (?,?,?,?,?,?,?,?,?,?,?)", count, model, acName, x, y, z, rx, ry, rz, health, tostring(encampment))
		end
	end
end
addEvent("addon.basecreator:newObject", true)
addEventHandler("addon.basecreator:newObject", root, newObject)

function onObjectDamage(object,health,id)
	if object then
		dbExec(db,'UPDATE base_objects SET health=? WHERE id=?',health,id)
	end
end
addEvent("onObjectDamage",true)
addEventHandler("onObjectDamage",root,onObjectDamage)

function onObjectDestroy(object,id)
	if object then
		dbExec(db,"DELETE FROM base_objects WHERE id=?",id)
		if getElementData(object,"parent") then
			destroyElement(getElementData(object,"parent"))
		end
		destroyElement(object)
		count = count-1
	end
end
addEvent("onObjectDestroy",true)
addEventHandler("onObjectDestroy",root,onObjectDestroy)

function setupDoor(object,encampment)
	local x,y,z = getElementPosition(object)
	local rx,ry,rz = getElementRotation(object)
	local col = createColSphere(x,y,z,2)
	setElementData(object,"parent",col)
	setElementData(col,"parent",object)
	DoorsTable[col] = {pos = {x = x,y = y,z = z}, rot= {x = rx,y = ry,z = rz}, col = col, camp = encampment, door = object}
end

function closeDoor(player,colshape)
	local data = DoorsTable[colshape]
	if data == nil then return end
	if getElementData(data["door"],"object.moved") == "moved" then
		if moveObject(data["door"],2000,data.pos.x,data.pos.y,data.pos.z,0,0,-90) then
			outputChatBox("Дверь успешно открыта")
		end
		setElementRotation(data["door"],data.rot.x,data.rot.y,data.rot.z - -90) -- checar isso
		-- ^ A rotação já é definida em moveObject, e menos com menos vai ser uma adição na rotZ
		setElementData(data["door"],"object.moved","notmoved") 
	end
end

function toggleDoorOpen(player, key, keyState, colshape)
	if not isElementWithinColShape( player, colshape ) then return end;
	--[[ A bind só vai funcionar se tiver dentro do colshape,
		se quiser desativar só remover essa linha ]]
	
	local data = DoorsTable[colshape]
	if data == nil then return end
	local rx,ry,rz=getElementRotation(data["door"]) -- se não for usar isso pode remover
	local doorState = getElementData(data["door"], "object.moved")
	if doorState and doorState == "moved" and (getElementData(player,"Group") == data["camp"] or data.camp == "false" or not data["camp"]) then
		closeDoor( player, colshape )
		outputChatBox("Дверь открыта")
		return
	end
	if doorState and doorState == "notmoved" and (getElementData(player,"Group") == data["camp"] or data.camp == "false" or not data["camp"]) then
		moveObject(data["door"],2000,data.pos.x,data.pos.y,data.pos.z,0,0,90)
		setElementData(data["door"],"object.moved","moved") 
		outputChatBox("Дверь закрыта")
	end
end

function bind(hitPlayer, matchingDimension) 
	if getElementType(hitPlayer) == "player" then
		if type(getElementData( DoorsTable[source]["door"], "object.moved" )) ~= "string" then
			setElementData( DoorsTable[source]["door"], "object.moved", "notmoved" )
		end
		if isKeyBound( hitPlayer, "E", "down", toggleDoorOpen ) ~= true then
			bindKey(hitPlayer, "E", "down", toggleDoorOpen, source)
		end
		outputChatBox("Дверь может открыть участник группировки!")
	end
end 
addEventHandler("onColShapeHit", resourceRoot, bind)

how to do what the door could open members player of the group?   now the open door to all players server

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...