aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/sh_pac.lua
blob: 1d4da418239f614878e1bc2088ea320ee553aed1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
--[[
    All the functions related to networking pac's
]]
if CLIENT then
    local what
    local function applypac(who,pacname)
        local pactxt = file.Read("artery/pacs/"..pacname..".txt","DATA")
        print("got pac txt",pactxt)
        if pactxt == nil then
            print("requesting pac")
            net.Start("requestpac")
            net.WriteString(pacname)
            net.SendToServer()
            timer.Simple(5,function() --5 seconds is probably enough time to load a pac, right?
                print("trying to re-apply pac")
                applypac(who,pacname)
            end)
        else
            if who.AttachPACPart == nil then
                print("setting up ent for pac")
                pac.SetupENT(who)
            end
            print("pactxt was",pactxt)
            local pactbl = CompileString("return {"..pactxt.."}",pacname)()
            print("pactbl is")
            PrintTable(pactbl)
            print("who is",who)
            who:AttachPACPart(pactbl)
            print("Pac Equiped!")

            what = who:FindPACPart(pactbl, "ball")
            print("what was", what)
            if what ~= nil then
                print("What's position:")
                for k,v in pairs(what) do
                    print(k,":",v)
                end
                print(what:GetDrawPosition(),type(what:GetDrawPosition()))
            end
        end
    end

    local RecordTrace = false
    local swingtime
    hook.Add("Tick","weapon_trace",function()
        if what ~= nil and ART.TraceWeapon and RecordTrace then
            local pos = what:GetDrawPosition()
            local ppos = LocalPlayer():GetPos()
            for k = 1,3 do pos[k] = pos[k] - ppos[k] end -- Now is a local vector, player is the origin
            print(string.format("{%1.3f,Vector(%d,%d,%d)},",swingtime/1000,pos[1],pos[2],pos[3]-64))
            swingtime = swingtime + util.TimerCycle()
        else
            swingtime = util.TimerCycle()
        end
    end)

    concommand.Add("artery_dev_traceweapons",function(ply,cmd,args)
        RecordTrace = args[1] == "1"
    end)

    local function removepac(who,pacname)
        local pactxt = file.Read("artery/pacs/"..pacname..".txt","DATA")
        assert(pactxt ~= nil, "Attempted to remove a pac that dosn't exist")
        assert(who.RemovePACPart ~= nil,"That entity isn't set up to have pacs!")
        local pactbl = CompileString("return {" .. pactxt .. "}", pacname)()
        who:RemovePACPart(pactbl)
    end

    net.Receive("loadpac",function()
        local pacname = net.ReadString()
        local pacdata = util.Decompress(net.ReadString())
        file.Write("artery/pacs/"..pacname..".txt",pacdata)
    end)

    net.Receive("applypac",function()
        print("apply ing pac")
        local towho = net.ReadEntity()
        local pacname = net.ReadString()
        applypac(towho,pacname)
    end)

    net.Receive("removepac",function()
        local towho = net.ReadEntity()
        local pacname = net.ReadString()
        removepac(towho,pacname)
    end)
else
    for _,v in pairs({
        "applypac",
        "removepac",
        "requestpac",
        "loadpac",
    })do util.AddNetworkString(v) end

    net.Receive("requestpac",function(ln,ply)
        local pacname = net.ReadString()
        local pacdata = file.Read("artery/pacs/"..pacname..".txt","DATA")
        assert(pacdata ~= nil,string.format("Client %q requested pac that dosn't exist %q",ply:Nick(),pacname))
        net.Start("loadpac")
        net.WriteString(pacname)
        net.WriteString(util.Compress(pacdata))
        net.Send(ply)
    end)

    local playerpacs = {}
    ART.ApplyPAC = function(to,pacname)
        print("Applying pac")
        net.Start("applypac")
        net.WriteEntity(to)
        net.WriteString(pacname)
        net.Broadcast()
        playerpacs[to] = playerpacs[to]  or {}
        table.insert(playerpacs[to],pacname)
    end

    hook.Add( "PlayerInitialSpawn", "loadpacs", function(ply)
        timer.Simple(5,function() --Wait 5 seconds, hopefully they're ok to go by then
            for k,v in pairs(playerpacs) do
                for i,j in pairs(v) do
                    net.Start("applypac")
                    net.WriteEntity(k)
                    net.WriteString(j)
                    net.Send(ply)
                end
            end
        end)
    end)

    ART.RemovePAC = function(to,pacname)
        net.Start("removepac")
        net.WriteEntity(to)
        net.WriteString(pacname)
        net.Broadcast()
        table.RemoveByValue(playerpacs[to], pacname)
    end
end
local paclist