aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/combat/sv_weaponswing.lua
blob: 69bfab48dec72fb8717f3ad5a9782af32570013b (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
---Automatically re-calculates swing arcs for weapons.
-- This files helps you create mele weapons that need to be swung
--@server sv_weaponswing.lua
--@alias ws

--[[
	This file tells you what weapons need their swings recalculated
]]

-- local itm = nrequire("core/inventory/item.lua")
-- local inv = nrequire("core/inventory/inventory.lua")
local wep = nrequire("core/inventory/common/weapons.lua")
local ws = {}

--Cache swing hits, if we used one once, we'll probably use it again soon
-- local swingcache_size = 30
-- local swingcache = {} --Swing arc cache

--A table of all the items that are swingable
local swingable = {}

--A queue of {pac, animation_name, animation_time, mapping,item_tbl}
local recordqueue = {}

util.AddNetworkString("artery_notifyserverofswing")
util.AddNetworkString("artery_doanimation")
util.AddNetworkString("artery_weaponswing")
util.AddNetworkString("artery_setswingdebug")

net.Receive("artery_notifyserverofswing",function()
	local weapon = net.ReadString()
	local anim = net.ReadString()
	local data = net.ReadTable()

	--print("Got swing data for ",weapon,anim)
	--PrintTable(data)

	--Get the data that already exists for this weapon
	local olddata = file.Read("artery/dynamic/swingdata/" .. weapon .. ".txt","DATA")
	if olddata == nil then olddata = {}
	else olddata = util.JSONToTable(util.Decompress(olddata)) end

	--Add our new data
	olddata[anim] = data

	--And save back
	file.Write("artery/dynamic/swingdata/" .. weapon .. ".txt",util.Compress(util.TableToJSON(olddata)))
end)

---Make a weapons swingable.
-- Make a new weapon that needs swing arcs, this function checks to make sure the table has a .Name, .attacks, and .pacname
--@tparam swingitem tbl A swingable item.
function ws.makeSwingable(tbl)
	--print("Makeing a swingable out of:")
	--PrintTable(tbl)
	assert(tbl.Name,"Tried to make a swingable weapon out of an item with no name!")
	assert(tbl.attacks,"Tried to make a swingable weapon out of an item with no attacks! See rustyaxe.lua for example")
	assert(tbl.pacname,"Tried to make a swingable weapon without a pac name! see rustyaxe.lua for example")
	swingable[tbl.Name] = tbl
end

---Actually swings a weapon.
-- Makes a player swing their weapon, and dammages anything in the swing arc
--@tparam swingitem The item the player swung
--@tparam player ply The player swinging the weapon
--@tparam function callback The function to call when the swing arc hit something.
function ws.doSwing(weapon,ply,callback)
	local dir = wep.playermovedir(ply)
	local fdata = file.Read("artery/dynamic/swingdata/" .. weapon.Name .. ".txt")
	local swingtbl = util.JSONToTable(util.Decompress(fdata))
	if swingtbl[dir] == nil then return end
	local tswing = swingtbl[dir]
	for k,v in pairs(tswing) do
		v:Rotate(ply:EyeAngles())
	end
	local timedelay = weapon.attacks[dir].waittime
	local hitthings = {}
	net.Start("artery_weaponswing")
	net.WriteString(weapon.attacks[dir].anim)
	net.WriteEntity(ply)
	net.WriteDouble(weapon.attacks[dir].time)
	net.Broadcast()
	timer.Simple(timedelay,function()
		net.Start("artery_setswingdebug")
		net.WriteTable(tswing)
		net.Broadcast()
		local pp = ply:GetPos()
		for i = 2, #tswing do
			local s = tswing[i] + pp
			local e = tswing[i-1] + pp
			local tr = util.TraceLine({
				start = s,
				endpos = e,
				filter = ply
			})
			if tr.Hit then
				hitthings[#hitthings + 1] = tr.Entity
			end
		end
		for k,v in pairs(hitthings) do
			callback(v)
		end
		if #hitthings == 0 and weapon.onMiss then
			weapon:onMiss(ply)
		end
	end)
end

---Records animations for swingables.
-- Records the player doing swings 
--@concommand artery_Recordanimations
concommand.Add("artery_recordanimations",function(ply,cmd,args)
	if not ply:IsAdmin() then return end
	local e = ply
	e:SetAngles(Angle(0,0,0))
	e:SetEyeAngles(Angle(0,0,0))

	--Add everything in swingables to a queue to be recorded
	for k,v in pairs(swingable) do
		for i,j in pairs(v.attacks) do
			local tn = {
				v.pacname,
				j.anim,
				j.time,
				i,
				v,
				j.waittime
			}
			recordqueue[#recordqueue + 1] = tn
		end
	end

	--Create timers for everything
	local timefromnow = 0
	for k,v in ipairs(recordqueue) do
		timefromnow = timefromnow + 1
		print("timefromnow 1 is",timefromnow)
		--Put the item into the inventory
		local tfunc = function(nowtime)
			--local whentoremove = nowtime + 1 + v[3] + 1
			timer.Simple(nowtime,function()
				print("timefromnow 2 is",nowtime)
				local einv = e.data.inventories[1]
				local spot = einv:FindPlaceFor(v[5])
				assert(spot != nil, "Could not equip the npc with " .. v[5].Name)
				einv:Put(spot,v[5])
				print("Placed item in inventory")
				--Wait 1 second to de-apply the pac and remove the item
				print("I will remove the item in", v[3])
				timer.Simple(v[3],function()
					print("removeing equiped...")
					einv:Remove(spot)
				end)
			end)
		end
		tfunc(timefromnow)
		print("timefromnow 3 is",timefromnow)
		local tfunc2 = function(nowtime)
			timer.Simple(nowtime,function()
				print("timefromnow 4 is",nowtime)
				net.Start("artery_doanimation")
				net.WriteString(v[2])
				net.WriteDouble(v[3])
				net.WriteString(v[5].Name)
				net.WriteString(v[1])
				net.WriteString(v[4])
				net.WriteEntity(e)
				net.WriteDouble(v[6])
				net.Broadcast()
			end)
		end
		--Wait 1 second for the pac to apply
		timefromnow = timefromnow + 1
		tfunc2(timefromnow)
		timefromnow = timefromnow + 1 + v[3] + 1 --Wait for anim to be recorded, and the thing to be removed
	end
	recordqueue = {}
end)


---Command to check that all swingable weapons have been recorded.
-- Check the console for output
--@concommand artery_checkSwingable
concommand.Add("artery_checkSwingable",function(ply,cmd,args)
	local nitms = 0
	local noswingdata = {}
	for k,v in pairs(swingable) do
		--Make sure we have a weapon path for everything
		if file.Exists( "artery/dynamic/swingdata/" .. k .. ".txt", "DATA" ) then
			MsgC(Color(0,255,0),"Found data for " .. k .. "\n")
		else
			MsgC(Color(255,0,0),"Couldn't find data for " .. k .. "\n")
			noswingdata[#noswingdata + 1] = k
		end
		nitms = nitms + 1
	end

	print("Scanned ",nitms,"swingable items, could not find data for:\n\t",table.concat(noswingdata,"\n\t"))
end)

---Clears the swingable cache, usefull for developers.
-- Clears the list of items the gamemode knows about that are swingable 
--@concommand artery_clearswingable
concommand.Add("artery_clearswingable",function(ply,cmd,args)
	if not ply:IsAdmin() then return end
	swingable = {}
end)

return ws