aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/combat/sv_weaponswing.lua
blob: 408e7d2622b1f64c5fdbe2d8932a53f07f0c3c25 (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
--[[
	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 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 = {}

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

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)

function ws.makeSwingable(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

local function record_animations_for(swingable, e)
	print("recording animations on", e)
	PrintTable(swingable)
	local einv = e.data.inventories[1]
	local spot = einv:FindPlaceFor(swingable)
	assert(spot != nil, "Could not equip the npc with that")
	einv:Put(spot,swingable)
	for k,v in pairs(swingable.attacks) do
		net.Start("artery_doanimation")
		net.WriteString(v.anim)
		net.WriteDouble(v.time)
		net.WriteString(swingable.Name)
		net.WriteString(swingable.pacname)
		net.WriteString(".")
		net.WriteEntity(e)
		net.Broadcast()
	end
	timer.Simple(5,function()
		--assert(IsValid(einv),"Npc's inventory is no longer valid!")
		einv:Remove(spot)
	end)
end

--Create a fake thing in front of the player, do the swing animations, and record them.
concommand.Add("artery_recordanimations",function(ply,cmd,args)
	e = ents.Create("npc_citizen")
	e:SetPos(ply:GetEyeTrace().HitPos)
	e:Spawn()
	e.data = {inventories = {}}
	local einv = inv.CreateInventory("Equipment")
	einv.id = 1
	einv.Owner = e
	e.data.inventories[einv.id] = einv
	local i = 1
	for k,v in pairs(swingable) do
		timer.Simple(5*i,function()
			record_animations_for(v,e)
		end)
		i = i + 1
	end
	
	--[[
	local animqueuetime = 0
	for k,v in pairs(swingable) do
		--equip the right item

		print("equipable inventory:")
		local eqi = ply.data.inventories[1]
		print(ply.data.inventories[1])
		local itm = eqi:Get({v.Equipable})
		if itm ~= nil then
			eqi:Remove({v.Equipable})
		end
		eqi:Put({v.Equipable},v)
		
		--queue up each attack for the player to do
		for i,j in pairs(v.attacks) do
			timer.Simple(animqueuetime,function()
				print("Doing attack:")
				print(i,":",j)
				net.Start("artery_doanimation")
				net.WriteString(j.anim) --Animation name (to return)
				net.WriteDouble(j.time) --Timeing of the animation
				net.WriteString(v.Name) --Name of the item (to return)
				net.WriteString(v.pacname) --Name of the pac for this item
				net.WriteString(i) --direction of the animation (to return)
				net.Send(ply)
			end)
			animqueuetime = animqueuetime + j.time + 1
		end
		
	end
	]]
end)



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, "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)

concommand.Add("artery_clearswingable",function(ply,cmd,args)
	swingable = {}
end)


return ws