-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsh_boneanimlib.lua
More file actions
282 lines (249 loc) · 6.95 KB
/
sh_boneanimlib.lua
File metadata and controls
282 lines (249 loc) · 6.95 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
--[[
Bone Animations Library
Created by William "JetBoom" Moodhe (williammoodhe@gmail.com / www.noxiousnet.com)
Because I wanted custom, dynamic animations.
Give credit or reference if used in your creations.
]]
if GetLuaAnimations ~= nil then return end
TYPE_GESTURE = 0 -- Gestures are keyframed animations that use the current position and angles of the bones. They play once and then stop automatically.
TYPE_POSTURE = 1 -- Postures are static animations that use the current position and angles of the bones. They stay that way until manually stopped. Use TimeToArrive if you want to have a posture lerp.
TYPE_STANCE = 2 -- Stances are keyframed animations that use the current position and angles of the bones. They play forever until manually stopped. Use RestartFrame to specify a frame to go to if the animation ends (instead of frame 1).
TYPE_SEQUENCE = 3 -- Sequences are keyframed animations that use the reference pose. They play forever until manually stopped. Use RestartFrame to specify a frame to go to if the animation ends (instead of frame 1).
-- You can also use StartFrame to specify a starting frame for the first loop.
INTERP_LINEAR = 0 -- Straight linear interp.
INTERP_COSINE = 1 -- Best compatability / quality balance.
INTERP_CUBIC = 2 -- Overall best quality blending but may cause animation frames to go 'over the top'.
INTERP_DEFAULT = INTERP_COSINE
local Animations = {}
function GetLuaAnimations()
return Animations
end
function RegisterLuaAnimation(sName, tInfo)
if tInfo.FrameData then
local BonesUsed = {}
for iFrame, tFrame in ipairs(tInfo.FrameData) do
for iBoneID, tBoneTable in pairs(tFrame.BoneInfo) do
BonesUsed[iBoneID] = (BonesUsed[iBoneID] or 0) + 1
tBoneTable.MU = tBoneTable.MU or 0
tBoneTable.MF = tBoneTable.MF or 0
tBoneTable.MR = tBoneTable.MR or 0
tBoneTable.RU = tBoneTable.RU or 0
tBoneTable.RF = tBoneTable.RF or 0
tBoneTable.RR = tBoneTable.RR or 0
end
end
if #tInfo.FrameData > 1 then
for iBoneUsed, iTimesUsed in pairs(BonesUsed) do
for iFrame, tFrame in ipairs(tInfo.FrameData) do
if not tFrame.BoneInfo[iBoneUsed] then
tFrame.BoneInfo[iBoneUsed] = {MU = 0, MF = 0, MR = 0, RU = 0, RF = 0, RR = 0}
end
end
end
end
end
Animations[sName] = tInfo
end
-----------------------------
-- Deserialize / Serialize --
-----------------------------
local sandbox_env = {Vector = Vector, Angle = Angle}
function Deserialize(sIn)
local out = {}
if #sIn == 0 or string.sub(sIn, -1) ~= "}" then return out end
if string.sub(sIn, 1, 4) ~= "SRL=" then sIn = "SRL="..sIn end
if string.sub(sIn, 5, 5) ~= "{" then return out end
sIn = sIn.." return SRL"
local func = CompileString(sIn, "deserialize", false)
if type(func) == "string" then
print("Deserialization error: "..func)
else
setfenv(func, sandbox_env)
out = func() or out
end
return out
end
local allowedtypes = {}
allowedtypes["string"] = true
allowedtypes["number"] = true
allowedtypes["table"] = true
allowedtypes["Vector"] = true
allowedtypes["Angle"] = true
allowedtypes["boolean"] = true
local function MakeTable(tab, done)
local str = ""
local done = done or {}
local sequential = table.IsSequential(tab)
for key, value in pairs(tab) do
local keytype = type(key)
local valuetype = type(value)
if allowedtypes[keytype] and allowedtypes[valuetype] then
if sequential then
key = ""
else
if keytype == "number" or keytype == "boolean" then
key ="["..tostring(key).."]="
else
key = "["..string.format("%q", tostring(key)).."]="
end
end
if valuetype == "table" and not done[value] then
done[value] = true
if type(value._serialize) == "function" then
str = str..key..value:_serialize()..","
else
str = str..key.."{"..MakeTable(value, done).."},"
end
else
if valuetype == "string" then
value = string.format("%q", value)
elseif valuetype == "Vector" then
value = "Vector("..value.x..","..value.y..","..value.z..")"
elseif valuetype == "Angle" then
value = "Angle("..value.pitch..","..value.yaw..","..value.roll..")"
else
value = tostring(value)
end
str = str .. key .. value .. ","
end
end
end
if string.sub(str, -1) == "," then
return string.sub(str, 1, #str - 1)
else
return str
end
end
function Serialize(tIn, bRaw)
if #tIn == 0 then
local empty = true
for k in pairs(tIn) do
empty = false
break
end
if empty then
return ""
end
end
if bRaw then
return "{"..MakeTable(tIn).."}"
end
return "SRL={"..MakeTable(tIn).."}"
end
---------------------------------
-- End Deserialize / Serialize --
---------------------------------
/* EXAMPLES!
-- If your animation is only used on one model, use numbers instead of bone names (cache the lookup).
-- If it's being used on a wide array of models (including default player models) then you should use bone names.
-- You can use Callback as a function instead of MU, RR, etc. which will allow you to do some interesting things.
-- See cl_boneanimlib.lua for the full format.
STANCE: stancetest
A simple looping stance that stretches the model's spine up and down until stopped.
RegisterLuaAnimation("stancetest", {
FrameData = {
{
BoneInfo = {
["ValveBiped.Bip01_Spine"] = {
MU = 64
}
},
FrameRate = 0.25
},
{
BoneInfo = {
["ValveBiped.Bip01_Spine"] = {
MU = -32
}
},
FrameRate = 1.5
},
{
BoneInfo = {
["ValveBiped.Bip01_Spine"] = {
MU = 32
}
},
FrameRate = 4
}
},
RestartFrame = 2,
Type = TYPE_STANCE
})
--[[
STANCE: staffholdspell
To be used with the ACT_HL2MP_IDLE_MELEE2 animation.
Player holds the staff so that their left hand is over the top of it.
]]
RegisterLuaAnimation("staffholdspell", {
FrameData = {
{
BoneInfo = {
["ValveBiped.Bip01_R_Forearm"] = {
RU = 40,
RF = -40
},
["ValveBiped.Bip01_R_Upperarm"] = {
RU = 40
},
["ValveBiped.Bip01_R_Hand"] = {
RU = -40
},
["ValveBiped.Bip01_L_Forearm"] = {
RU = 40
},
["ValveBiped.Bip01_L_Hand"] = {
RU = -40
}
},
FrameRate = 6
},
{
BoneInfo = {
["ValveBiped.Bip01_R_Forearm"] = {
RU = 2,
},
["ValveBiped.Bip01_R_Upperarm"] = {
RU = 1
},
["ValveBiped.Bip01_R_Hand"] = {
RU = -10
},
["ValveBiped.Bip01_L_Forearm"] = {
RU = 8
},
["ValveBiped.Bip01_L_Hand"] = {
RU = -12
}
},
FrameRate = 0.4
},
{
BoneInfo = {
["ValveBiped.Bip01_R_Forearm"] = {
RU = -2,
},
["ValveBiped.Bip01_R_Upperarm"] = {
RU = -1
},
["ValveBiped.Bip01_R_Hand"] = {
RU = 10
},
["ValveBiped.Bip01_L_Forearm"] = {
RU = -8
},
["ValveBiped.Bip01_L_Hand"] = {
RU = 12
}
},
FrameRate = 0.1
}
},
RestartFrame = 2,
Type = TYPE_STANCE,
ShouldPlay = function(pl, sGestureName, tGestureTable, iCurFrame, tFrameData)
local wepstatus = pl.WeaponStatus
return wepstatus and wepstatus:IsValid() and wepstatus:GetSkin() == 1 and wepstatus.IsStaff
end
})
*/