Showcasing some more Lua stuff

Hello.
I’ve learned alot from here and there and managed to do quite a bit. This time I decided on an Unlisted YouTube Video:

You’ll notice that the camera isn’t very smooth in this recording. It seemed smoother live.
I’ll also put up the code so that people can give pointers as to how I may be able to optimize it.



-- "Proper" walking animation
-- with a surprise.
--
-- Also, noticing that Vector:Sub/Add/etc
-- are actually depricate.
-- Use the + - * / operators.
--
-- .kkapture parameters:
-- -w 1024 -h 576 +map gm_construct
--
-- By Edward

function rags_eol()
	if !npcent:IsMoving() then
		timer.Stop("constr_bones")
		timer.Stop("check_move")
		npcent:SetPos(Vector(0,0,-6000))
		for i=0,ragent:GetPhysicsObjectCount()-1 do
			local phys = ragent:GetPhysicsObjectNum(i)
			phys:EnableGravity(true)
			phys:EnableMotion(true)
			phys:Wake()
		end
		timer.Create("end_of_line",0.1,1,function()
			if(!IsValid(ragent) or !IsValid(npcent)) then
				timer.Stop("end_of_line")
				return
			end
			local explode = ents.Create("env_explosion")
			local rdpos = ragent:GetPos()
			explode:SetPos(Vector(rdpos.x+32,rdpos.y,rdpos.z))
			explode:Spawn()
			explode:SetKeyValue("iMagnitude","1")
			explode:Fire("Explode",0,0)
			explode:EmitSound("weapon_AWP.Single",400,400)
			ragphys:ApplyForceCenter(Vector(-50000,0,50000))
			timer.Create("prep_coll_test",0.5,1,function()
				if(!IsValid(ragent) or !IsValid(npcent)) then
					timer.Stop("prep_coll")
					return
				end
				b_ragcollide = true
			end)
		end)
		return
	end
end

function ragentCollide(ent,data)
	if b_ragcollide==true and data.HitEntity:GetClass()=="worldspawn" then
		b_ragcollide=false
		timer.Create("reset_npc_coll",1,1,function()
			if(!IsValid(ragent) or !IsValid(npcent)) then
				timer.Stop("reset_npc_coll")
				return
			end
			npcent:SetPos(ragent:GetPos())
			npcent:SetSchedule(SCHED_NPC_FREEZE)
			riseragdoll()
		end)
	end
end

function riseragdoll()
	-- Get Pelvis
	local risestate=0
	local cursolid = npcent:GetSolid()
	npcent:SetSolid(0)

	local PHPelvis = ragent:GetPhysicsObjectNum(0)
	local BPelvis = ragent:TranslatePhysBoneToBone(0)
	local PelvisPos,PelvisAng = npcent:GetBonePosition(BPelvis)
	local PHLeftHand = ragent:GetPhysicsObjectNum(5)
	local BLeftHand = ragent:TranslatePhysBoneToBone(5)
	local LHandPos,LHandAng = npcent:GetBonePosition(BLeftHand)
	local PHRightHand = ragent:GetPhysicsObjectNum(7)
	local BRightHand = ragent:TranslatePhysBoneToBone(7)
	local RHandPos,RHandAng = npcent:GetBonePosition(BRightHand)
	local PHHead = ragent:GetPhysicsObjectNum(10)
	local BHead = ragent:TranslatePhysBoneToBone(10)
	local HeadPos,HeadAng = npcent:GetBonePosition(BHead)

	local PHLeftFoot = ragent:GetPhysicsObjectNum(13)
	local BLeftFoot = ragent:TranslatePhysBoneToBone(13)
	local LFootPos,LFootAng = npcent:GetBonePosition(BLeftFoot)
	local PHRightFoot = ragent:GetPhysicsObjectNum(14)
	local BRightFoot = ragent:TranslatePhysBoneToBone(14)
	local RFootPos,RFootAng = npcent:GetBonePosition(BRightFoot)

	PHPelvis:EnableGravity(false)
	PHPelvis:EnableMotion(true)
	PHPelvis:Wake()
	PHLeftHand:EnableMotion(false)
	PHLeftHand:Wake()
	PHRightHand:EnableMotion(false)
	PHRightHand:Wake()
	PHLeftFoot:EnableMotion(true)
	PHLeftFoot:Wake()
	PHRightFoot:EnableMotion(true)
	PHRightFoot:Wake()
	PHHead:EnableMotion(true)
	PHHead:Wake()

	timer.Create("rstate1",1,1,function()
		if(!IsValid(ragent) or !IsValid(npcent)) then
			timer.Stop("rstate1")
			return
		end
		risestate=1
		PHLeftHand:EnableMotion(true)
		PHLeftHand:Wake()
		PHRightHand:EnableMotion(true)
		PHRightHand:Wake()
		PHLeftFoot:EnableMotion(false)
		PHLeftFoot:Wake()
		PHRightFoot:EnableMotion(false)
		PHRightFoot:Wake()
		PHHead:EnableGravity(false)
		timer.Create("rstate2",1,1,function()
			if(!IsValid(ragent) or !IsValid(npcent)) then
				timer.Stop("rstate2")
				return
			end
			risestate=2
			PHLeftFoot:EnableMotion(true)
			PHLeftFoot:Wake()
			PHRightFoot:EnableMotion(true)
			PHRightFoot:Wake()
			camstage=2
			timer.Create("rstate3",3,1,function()
				if(!IsValid(ragent) or !IsValid(npcent)) then
					timer.Stop("rstate3")
					return
				end
				timer.Stop("rise_ragdoll")
				for i=0,ragent:GetPhysicsObjectCount()-1 do
					if i != 10 then
						ragent:GetPhysicsObjectNum(i):SetVelocity(Vector(0  ,0,0))
					end
					ragent:GetPhysicsObjectNum(i):EnableMotion(false)
				end
				makeheadlookup()
			end)
		end)
	end)

	timer.Create("rise_ragdoll",0.01,0,function()
		if(!IsValid(ragent) or !IsValid(npcent)) then
			timer.Stop("rise_ragdoll")
			return
		end
		if risestate==0 then
			local move_dist = PelvisPos-PHPelvis:GetPos()
			move_dist = move_dist*5
			PHPelvis:SetVelocity(move_dist)
			move_dist = LFootPos-PHLeftFoot:GetPos()
			move_dist = move_dist*10
			PHLeftFoot:SetVelocity(move_dist)
			move_dist = RFootPos-PHRightFoot:GetPos()
			move_dist = move_dist*10
			PHRightFoot:SetVelocity(move_dist)
		elseif risestate==1 then
			local move_dist = HeadPos-PHHead:GetPos()
			move_dist = move_dist*5
			PHHead:SetVelocity(move_dist)
		elseif risestate==2 then
			for i=0,ragent:GetPhysicsObjectCount()-1 do
				local phys = ragent:GetPhysicsObjectNum(i)
				local b = ragent:TranslatePhysBoneToBone(i)
				local pos,ang = npcent:GetBonePosition(b)
				phys:EnableGravity(false)
				phys:EnableMotion(true)
				phys:Wake()
				local move_dist = pos-phys:GetPos()
				move_dist = move_dist*5
				phys:SetVelocity(move_dist)
				if i == 10 then
					phys:SetAngles(ang)
				elseif i == 5 then
					phys:SetAngles(ang)
				elseif i == 7 then
					phys:SetAngles(ang)
				elseif i == 13 then
					phys:SetAngles(ang)
				elseif i == 14 then
					phys:SetAngles(ang)
				end
			end
		end
	end)
end

function makeheadlookup()
	local PHHead = ragent:GetPhysicsObjectNum(10)
	local lookvec = PHHead:GetAngles():Right()*32
	local HeadPos = PHHead:GetPos()
	local campos = camera:GetPos()

	-- Make Point for Head
	HeadPoint = ents.Create("prop_physics")
	if(!IsValid(HeadPoint)) then
		return
	end
	HeadPoint:SetModel("models/dav0r/camera.mdl")
	HeadPoint:SetPos(HeadPos+lookvec)
	HeadPoint:SetAngles(Angle(0,0,0))
	HeadPoint:Spawn()
	cleanup.Add( player.GetByID(1), "props", HeadPoint )
		undo.Create( "prop" )
		undo.AddEntity( HeadPoint )
		undo.SetPlayer( player.GetByID(1) )
	undo.Finish()
	HPphys = HeadPoint:GetPhysicsObject()
	if(!IsValid(HPphys)) then
		HeadPoint:Remove()
		return
	end
	-- End Make Point for Head
	HeadPoint:SetNoDraw(true)
	HeadPoint:SetSolid(0)
	HPphys:EnableGravity(false)

	timer.Create("look_at_cam",0.01,0,function()
		if(!IsValid(ragent) or !IsValid(HeadPoint)) then
			timer.Stop("look_at_can")
			return
		end
		local HPpos = HeadPoint:GetPos()+Vector(0,0,0)
		local move_dist = (campos+Vector(0,0,-8))-HPpos
		move_dist = move_dist*5
		HPphys:SetVelocity(move_dist)
		local relang = (HPpos-HeadPos):Angle()
		relang:RotateAroundAxis(relang:Up(),0)
		relang:RotateAroundAxis(relang:Right(),90)
		relang:RotateAroundAxis(relang:Forward(),90)
		PHHead:SetAngles(relang)
	end)
	speaking()
	-- And so it begins...
	timer.Create("say_100",0.1,1,function()
--		ragent:EmitSound("edwards/andsoitbegins.wav",140,100,1,2)
		sound.Play("edwards/andsoitbegins.wav",ragent:GetPos(),140,100,1)
		print("And")
		nextFaceFlexes[27]=1
		nextFaceFlexes[28]=1
		nextFaceFlexes[43]=0.5
	end)
	timer.Create("say_101",0.25,1,function()
		print("aND")
		nextFaceFlexes[38]=1
	end)
	timer.Create("say_102",0.5,1,function()
		print("So")
		nextFaceFlexes[31]=1
		nextFaceFlexes[32]=1
	end)
	timer.Create("say_103",0.65,1,function()
		print("sO")
		nextFaceFlexes[38]=0
	end)
	timer.Create("say_104",1.1,1,function()
		print("It")
		nextFaceFlexes[31]=0
		nextFaceFlexes[32]=0
	end)
	timer.Create("say_105",1.2,1,function()
		print("iT")
		nextFaceFlexes[38]=1
	end)
	timer.Create("say_106",1.3,1,function()
		print("Begins")
		nextFaceFlexes[27]=0
		nextFaceFlexes[28]=0
		nextFaceFlexes[43]=0
	end)
	timer.Create("say_107",1.4,1,function()
		print("bEgins")
		nextFaceFlexes[27]=1
		nextFaceFlexes[28]=1
		nextFaceFlexes[43]=0.5
		nextFaceFlexes[42]=0.25
	end)
	timer.Create("say_108",1.5,1,function()
		print("beGins")
		nextFaceFlexes[43]=0
		nextFaceFlexes[42]=0.5
	end)
	timer.Create("say_109",1.6,1,function()
		print("begIns")
		nextFaceFlexes[43]=0.5
		nextFaceFlexes[42]=0.75
	end)
	timer.Create("say_110",1.7,1,function()
		print("begiNS")
		nextFaceFlexes[38]=1
		nextFaceFlexes[42]=1.25
	end)
	timer.Create("say_111",2,1,function()
		timer.Stop("talk_anim")
	end)
	timer.Create("clean_up",4,1,function()
		if IsValid(ragent) then ragent:Remove() end
		if IsValid(npcent) then npcent:Remove() end
		if IsValid(camera) then camera:Remove() end
		if IsValid(HeadPoint) then HeadPoint:Remove() end
	end)

end

function speaking()
	timer.Create("talk_anim",0.01,0,function()
		if(!IsValid(ragent)) then
			timer.Stop("talk_anim")
			return
		end
		for i = 0,ragent:GetFlexNum()-1 do
			local curflex = curFaceFlexes*
			local nexflex = nextFaceFlexes*
			if curflex < nexflex then
				curflex=curflex+0.1
			elseif curflex > nexflex then
				curflex=curflex-0.1
			end
			ragent:SetFlexWeight(i,curflex)
			curFaceFlexes*=curflex
		end
	end)
end

-- Make ragdoll
ragent = ents.Create("prop_ragdoll")
if(!IsValid(ragent)) then
	return
end
ragent:SetModel("models/Humans/Group02/male_09.mdl")
ragent:SetPos(Vector(0,0,-145))
ragent:SetAngles(Angle(0,0,0))
ragent:Spawn()
cleanup.Add( player.GetByID(1), "props", ragent )
	undo.Create( "prop" )
	undo.AddEntity( ragent )
	undo.SetPlayer( player.GetByID(1) )
undo.Finish()
ragphys = ragent:GetPhysicsObject()
if(!IsValid(ragphys)) then
	ragent:Remove()
	return
end
-- End Make ragdoll
ragent:AddCallback("PhysicsCollide",ragentCollide)
b_ragcollide = false


-- Make NPC
npcent = ents.Create("npc_citizen")
if(!IsValid(npcent)) then
	return
end
npcent:SetModel("models/Humans/Group02/male_09.mdl")
npcent:SetPos(Vector(0,0,-145))
npcent:SetAngles(Angle(0,0,0))
npcent:Spawn()
cleanup.Add( player.GetByID(1), "props", npcent )
	undo.Create( "prop" )
	undo.AddEntity( npcent )
	undo.SetPlayer( player.GetByID(1) )
undo.Finish()
npcphys = npcent:GetPhysicsObject()
if(!IsValid(npcphys)) then
	npcent:Remove()
	return
end
-- End Make NPC
npcent:SetNoDraw(true)

-- Make Camera
camera = ents.Create("prop_physics")
if(!IsValid(camera)) then
	return
end
camera:SetModel("models/dav0r/camera.mdl")
camera:SetPos(Vector(0,128,-80))
camera:SetAngles(Angle(0,0,0))
camera:Spawn()
cleanup.Add( player.GetByID(1), "props", camera )
	undo.Create( "prop" )
	undo.AddEntity( camera )
	undo.SetPlayer( player.GetByID(1) )
undo.Finish()
camphys = camera:GetPhysicsObject()
if(!IsValid(camphys)) then
	camera:Remove()
	return
end
-- End Make Camera
camera:SetSolid(0)
camphys:EnableGravity(false)


--for i = 0,ragent:GetBoneCount(),1 do
--	print(i..": "..ragent:GetBoneName(i))
--end

--for i = 0,ragent:GetPhysicsObjectCount()-1 do
--	local curphys = ragent:GetPhysicsObjectNum(i)
--	local bn = ragent:TranslatePhysBoneToBone(i)
--	print(i..": "..ragent:GetBoneName(bn))
--end

curFaceFlexes = {}
nextFaceFlexes = {}
for i = 0,ragent:GetFlexNum()-1 do
	table.insert(curFaceFlexes,i,ragent:GetFlexWeight(  i))
	table.insert(nextFaceFlexes,i,ragent:GetFlexWeight  (i))
--	print(i..": "..ragent:GetFlexName(i))
end
/*
0: right_lid_raiser
1: left_lid_raiser
2: right_lid_tightener
3: left_lid_tightener
4: right_lid_droop
5: left_lid_droop
6: right_lid_closer
7: left_lid_closer
8: half_closed
9: blink
10: right_inner_raiser
11: left_inner_raiser
12: right_outer_raiser
13: left_outer_raiser
14: right_lowerer
15: left_lowerer
16: right_cheek_raiser
17: left_cheek_raiser
18: wrinkler
19: dilator
20: right_upper_raiser
21: left_upper_raiser
22: right_corner_puller
23: left_corner_puller
24: right_corner_depressor
25: left_corner_depressor
26: chin_raiser
27: right_part
28: left_part
29: right_puckerer
30: left_puckerer
31: right_funneler
32: left_funneler
33: right_stretcher
34: left_stretcher
35: bite
36: presser
37: tightener
38: jaw_clencher
39: jaw_drop
40: right_mouth_drop
41: left_mouth_drop
42: smile
43: lower_lip
*/

camstage=0
player.GetByID(1):SetViewEntity(camera)
-- Hide HUD
RunConsoleCommand("cl_drawcameras", "0")
RunConsoleCommand("cl_draweffectrings", "0")
RunConsoleCommand("cl_drawhud", "0")
RunConsoleCommand("cl_drawworldtooltips", "0")
RunConsoleCommand("physgun_drawbeams", "0")
RunConsoleCommand("physgun_halo", "0")
timer.Create("cam_follow_head",0.001,0,function()
	if(!IsValid(camera)) then
		timer.Stop("cam_follow_head")
		player.GetByID(1):SetViewEntity(player.GetByID(1))
		-- Show HUD
		RunConsoleCommand("cl_drawcameras", "1")
		RunConsoleCommand("cl_draweffectrings", "1")
		RunConsoleCommand("cl_drawhud", "1")
		RunConsoleCommand("cl_drawworldtooltips", "1")
		RunConsoleCommand("physgun_drawbeams", "1")
		RunConsoleCommand("physgun_halo", "1")
		return
	end
	local ragheadpos = ragent:GetPhysicsObjectNum(10):GetPos()
	local campos = camera:GetPos()
	local relpos = ragheadpos-campos
	camera:SetAngles(relpos:Angle())
	if camstage==0 then
		if ragheadpos:Distance(campos)>300 then
			camstage=1
		end
	elseif camstage==1 then
		if ragheadpos:Distance(campos)>128 then
			local camflydir = relpos
			camphys:SetVelocity(camflydir)
		end
	elseif camstage==2 then
		if ragheadpos:Distance(campos)>32 then
			local upperpos = ragheadpos-campos
			camphys:SetVelocity(upperpos)
		end
	end
end)

timer.Create("constr_bones",0.01,0,function()
	if(!IsValid(ragent) or !IsValid(npcent)) then
		timer.Stop("constr_bones")
		timer.Stop("move_npc")
		return
	end
	for i=0,ragent:GetPhysicsObjectCount()-1 do
		local phys = ragent:GetPhysicsObjectNum(i)
		local b = ragent:TranslatePhysBoneToBone(i)
		local pos,ang = npcent:GetBonePosition(b)
		local velpos = (pos-phys:GetPos())*50
		phys:EnableMotion(true)
		phys:Wake()
		if i == 10 then
			-- Contraint Head
			phys:SetVelocity(velpos)
			phys:SetAngles(ang)
		elseif i == 0 then
			-- Contraint Pelvis
			phys:SetVelocity(velpos)
			phys:SetAngles(ang)
		elseif i == 5 then
			-- Contraint Left Hand
			phys:SetVelocity(velpos)
			phys:SetAngles(ang)
		elseif i == 7 then
			-- Contraint Right Hand
			phys:SetVelocity(velpos)
			phys:SetAngles(ang)
		elseif i == 13 then
			-- Contraint Left Foot
			phys:SetVelocity(velpos)
			phys:SetAngles(ang)
		elseif i == 14 then
			-- Contraint Right Foot
			phys:SetVelocity(velpos)
			phys:SetAngles(ang)
		end
		if string.sub(ragent:GetBoneName(b),1,4) == "prp_" then
			phys:EnableMotion(true)
			phys:Wake()
		else
			if i == 10 then
				phys:EnableGravity(false)
			elseif i == 0 then
				phys:EnableGravity(false)
			elseif i == 5 then
				phys:EnableGravity(false)
			elseif i == 7 then
				phys:EnableGravity(false)
			elseif i == 13 then
				phys:EnableGravity(false)
			elseif i == 14 then
				phys:EnableGravity(false)
			end
		end
	end
end)

timer.Create("move_npc",1,1,function()
	npcent:SetSaveValue("m_vecLastPosition", Vector(256,0,-145))
	npcent:SetSchedule(SCHED_FORCED_GO)
end)

timer.Create("check_move",2,1,function()
	timer.Adjust("check_move",0.1,0,function()
		if(!IsValid(ragent) or !IsValid(npcent)) then
			timer.Stop("check_move")
			return
		end
		rags_eol()
	end)
end)