· 9 years ago · Nov 29, 2016, 04:32 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 setElementData(tObCol,"objectGroup",ob['encampment'])
28 if ob['model'] == 1535 then
29 setupDoor(tOb,ob['encampment'])
30 end
31 count = count + 1
32 end
33 outputServerLog("[Craft]: Base objects loaded. TOTAL: "..tostring(count))
34 end
35end
36addEventHandler("onResourceStart", resourceRoot, onStart)
37
38function onStartTwo ()
39 db = dbConnect( "sqlite", "objectsdb/bases.db" )
40end
41
42function newObject(model,x,y,z,rx,ry,rz)
43 if model and x and y and z and rx and ry and rz then
44 local ob = createObject(model, x, y, z, rx, ry, rz)
45 obCol = createColSphere(x,y,z,2)
46 attachElements(obCol, ob, 0, 0, 0)
47 setElementData(obCol,"objectRust",true)
48 local acName = getAccountName(getPlayerAccount(client))
49 setElementData(ob, "bc.creator", acName)
50 local encampment = getElementData(client,"Group")
51 setElementData(obCol,"objectGroup",encampment)
52 if model == 1535 then
53 setupDoor(ob,encampment)
54 end
55 if ob then
56 local x,y,z = getElementPosition(ob)
57 local rx,ry,rz = getElementRotation(ob)
58 local model = getElementModel(ob)
59 count = count+1
60 dbExec(db, "INSERT INTO base_objects VALUES (?,?,?,?,?,?,?,?,?,?)", count, model, acName, x, y, z, rx, ry, rz, tostring(encampment))
61 end
62 end
63end
64addEvent("rustCreateObjectCraft", true)
65addEventHandler("rustCreateObjectCraft", root, newObject)
66
67function onObjectDestroy(col,object,id)
68 if object then
69 dbExec(db,"DELETE FROM base_objects WHERE id=(?)",id)
70 onStart()
71 if getElementData(object,"parent") then
72 destroyElement(getElementData(object,"parent"))
73 end
74 destroyElement(col)
75 destroyElement(object)
76 count = count-1
77 end
78end
79
80function setupDoor(object,encampment)
81 local x,y,z = getElementPosition(object)
82 local rx,ry,rz = getElementRotation(object)
83 local col = createColSphere(x,y,z,1.5)
84 setElementData(object,"parent",col)
85 setElementData(col,"parent",object)
86 DoorsTable[col] = {pos = {x = x,y = y,z = z}, rot= {x = rx,y = ry,z = rz}, col = col, camp = encampment, door = object}
87end
88
89function closeDoorForReal(doorCol)
90 local data = DoorsTable[doorCol]
91 setElementRotation(data.door,data.rot.x,data.rot.y,data.rot.z)
92end
93
94function closeDoor(doorCol)
95 local data = DoorsTable[doorCol]
96 moveObject(data.door,800,data.pos.x,data.pos.y,data.pos.z,0,0,-90)
97 setTimer(closeDoorForReal,1050,1,doorCol)
98end
99
100function openDoor(hitElement)
101 local data = DoorsTable[source]
102 if data == nil then return end
103 local rx,ry,rz=getElementRotation(data["door"])
104 if getElementData(hitElement,"Group") == data["camp"] or data.camp == "false" or not data["camp"] then
105 moveObject(data["door"],1200,data.pos.x,data.pos.y,data.pos.z,0,0,90)
106 setTimer(closeDoor,1200,1,source)
107 end
108end
109addEventHandler("onColShapeHit",resourceRoot,openDoor)
110
111function pegaitemcraft (source)
112 setElementData(source,"Wood",300)
113 setElementData(source,"Metal",300)
114 setElementData(source,"Gun Powder",300)
115 setElementData(source,"Alcohol Bottle",300)
116 setElementData(source,"Rags",300)
117 setElementData(source,"Cloch",300)
118 setElementData(source,"C4",300)
119end
120addCommandHandler("pegaritem",pegaitemcraft)