· 9 years ago · Nov 26, 2016, 03:50 PM
1local db
2local count = 0
3local DoorsTable = {}
4function onStart()
5 if not fileExists("objectsdb/bases.db") then
6 local h = fileCreate("objectsdb/bases.db")
7 if h then
8 fileClose(h)
9 outputServerLog("[Craft]: Database not found, creating...")
10 end
11 db = dbConnect( "sqlite", "objectsdb/bases.db" )
12 outputServerLog("[Craft]: Inserting tables into database...")
13 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, encampment VARCHAR)")
14 outputServerLog("[Craft]: Tables inserted.")
15 else
16 db = dbConnect( "sqlite", "objectsdb/bases.db" )
17 local qh = dbQuery( db, "SELECT * FROM base_objects" )
18 local result = dbPoll( qh, 10000 )
19 for i, ob in ipairs(result) do
20 local tOb = createObject(ob['model'], ob['x'], ob['y'], ob['z'], ob['rx'], ob['ry'], ob['rz'])
21 setElementData(tOb, "bc.creator", ob['owner'])
22 setElementData(tOb, "bc.ID", ob['id'])
23 local xpos,ypos,zpos = getElementPosition(tOb)
24 tObCol = createColSphere(xpos,ypos,zpos,2)
25 attachElements (tObCol, tOb, 0, 0, 0 )
26 setElementData(tObCol,"objectRust",true)
27 if ob['model'] == 1535 then
28 setupDoor(tOb,ob['encampment'])
29 end
30 count = count + 1
31 end
32 outputServerLog("[Craft]: Base objects loaded. TOTAL: "..tostring(count))
33 end
34end
35addEventHandler("onResourceStart", resourceRoot, onStart)
36
37function newObject(model,x,y,z,rx,ry,rz)
38 if model and x and y and z and rx and ry and rz then
39 local ob = createObject(model, x, y, z, rx, ry, rz)
40 obCol = createColSphere(x,y,z,2)
41 attachElements(obCol, ob, 0, 0, 0)
42 setElementData(obCol,"objectRust",true)
43 local acName = getAccountName(getPlayerAccount(client))
44 setElementData(ob, "bc.creator", acName)
45 local encampment = getElementData(client,"Group")
46 if model == 1535 then
47 setupDoor(ob,encampment)
48 end
49 if ob then
50 local x,y,z = getElementPosition(ob)
51 local rx,ry,rz = getElementRotation(ob)
52 local model = getElementModel(ob)
53 count = count+1
54 dbExec(db, "INSERT INTO base_objects VALUES (?,?,?,?,?,?,?,?,?,?)", count, model, acName, x, y, z, rx, ry, rz, tostring(encampment))
55 end
56 end
57end
58addEvent("rustCreateObjectCraft", true)
59addEventHandler("rustCreateObjectCraft", root, newObject)
60
61function onObjectDestroy(col,object,id)
62 if object then
63 dbExec(db,"DELETE FROM base_objects WHERE id=?",id)
64 if getElementData(object,"parent") then
65 destroyElement(getElementData(object,"parent"))
66 end
67 destroyElement(col)
68 destroyElement(object)
69 count = count-1
70 end
71end
72addEvent("onObjectDestroy",true)
73addEventHandler("onObjectDestroy",root,onObjectDestroy)
74
75function setupDoor(object,encampment)
76 local x,y,z = getElementPosition(object)
77 local rx,ry,rz = getElementRotation(object)
78 local col = createColSphere(x,y,z,1.5)
79 setElementData(object,"parent",col)
80 setElementData(col,"parent",object)
81 DoorsTable[col] = {pos = {x = x,y = y,z = z}, rot= {x = rx,y = ry,z = rz}, col = col, camp = encampment, door = object}
82end
83
84function closeDoorForReal(doorCol)
85 local data = DoorsTable[doorCol]
86 setElementRotation(data.door,data.rot.x,data.rot.y,data.rot.z)
87end
88
89function closeDoor(doorCol)
90 local data = DoorsTable[doorCol]
91 moveObject(data.door,800,data.pos.x,data.pos.y,data.pos.z,0,0,-90)
92 setTimer(closeDoorForReal,1050,1,doorCol)
93end
94
95function openDoor(hitElement)
96 local data = DoorsTable[source]
97 if data == nil then return end
98 local rx,ry,rz=getElementRotation(data["door"])
99 if getElementData(hitElement,"Group") == data["camp"] or data.camp == "false" or not data["camp"] then
100 moveObject(data["door"],1200,data.pos.x,data.pos.y,data.pos.z,0,0,90)
101 setTimer(closeDoor,1200,1,source)
102 end
103end
104addEventHandler("onColShapeHit",resourceRoot,openDoor)
105
106--[[
107function destroyCaminha (cama,camaCol)
108local baguizete = getElementData(camaCol,"grupodono")
109local contadestruir = getAccount(baguizete)
110local playernaconta = getAccountPlayer(contadestruir)
111 if playernaconta then
112 setAccountData(playernaconta,"resdX",nil)
113 setAccountData(playernaconta,"resdY",nil)
114 setAccountData(playernaconta,"resdZ",nil)
115 setAccountData(playernaconta,"hasrespawndd",false)
116 end
117 if contadestruir then
118 setAccountData(contadestruir,"resdX",nil)
119 setAccountData(contadestruir,"resdY",nil)
120 setAccountData(contadestruir,"resdZ",nil)
121 setAccountData(contadestruir,"hasrespawndd",false)
122 end
123 destroyElement(cama)
124 destroyElement(camaCol)
125end
126
127function caminhadored (grupo)
128local x,y,z = getElementPosition(source)
129local xr,yr,zr = getElementRotation(source)
130px, py, pz = getElementPosition(source)
131prot = getPedRotation(source)
132local offsetRot = math.rad(prot+90)
133local vx = px + 5 * math.cos(offsetRot)
134local vy = py + 5 * math.sin(offsetRot)
135local vz = pz + 2
136local vrot = prot+180
137 cama = createObject(1647,vx,vy,z-1,0,0,vrot)
138 camaCol = createColSphere(x,y,z,2)
139 attachElements (camaCol, cama, 0, 0, 0 )
140 setElementData(camaCol,"parent",cama)
141 setElementData(cama,"parent",camaCol)
142 setElementData(camaCol,"cama",true)
143 setElementData(camaCol,"grupodono",grupo)
144 setTimer(destroyCaminha,120000,1,cama,camaCol)
145end
146addEvent("criaumacaminha",true)
147addEventHandler("criaumacaminha",getRootElement(),caminhadored)
148
149function camadored ()
150local x,y,z = getElementPosition(source)
151 setElementData(source,"respawnX",x)
152 setElementData(source,"respawnY",y)
153 setElementData(source,"respawnZ",z)
154 setElementData(source,"hasrespawn",true)
155 outputChatBox("Your respawn has been set to your bed's position!",source)
156end
157addEvent("quandooursodormir",true)
158addEventHandler("quandooursodormir",getRootElement(),camadored)
159--]]
160
161function pegaitemcraft (source)
162 setElementData(source,"Wood",300)
163 setElementData(source,"Metal",300)
164 setElementData(source,"Gun Powder",300)
165 setElementData(source,"Alcohol Bottle",300)
166 setElementData(source,"Rags",300)
167 setElementData(source,"Cloch",300)
168end
169addCommandHandler("pegaritem",pegaitemcraft)