aboutsummaryrefslogtreecommitdiff
path: root/gamemode/ents/art_npc/sv_art_npc.lua
blob: 9c24e9d57090150aba73cb3ff78f4f44a40972da (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
local ENT = nrequire("sh_art_npc.lua")
local log = nrequire("log.lua")

util.AddNetworkString( "art_opennpcdialog" )
util.AddNetworkString( "art_closenpcdialog" )
util.AddNetworkString( "art_updatenpcdialog")

local oinit = ENT.Initalize
function ENT:Initialize()
	--self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_BBOX)
	self:SetCollisionGroup(COLLISION_GROUP_NPC)

	self:SetHealth(50)

	-- self.loco:SetDeathDropHeight(500)	--default 200
	-- self.loco:SetAcceleration(400)		--default 400
	-- self.loco:SetDeceleration(400)		--default 400
	-- self.loco:SetStepHeight(18)			--default 18
	-- self.loco:SetJumpHeight(25)		--default 58
	-- self.loco:SetDesiredSpeed( 50 )

	self.nodereached = false

	self.lastdooropen = CurTime()

	if self.Model then self:SetModel(self.Model)
	else log.error("NPC created without model, this might be a bug!") end

	if self.Pos then self:SetPos(self.Pos)
	else log.error("NPC created without a position, this might be a bug!") end

	self.talking = false

	if self.Name then self:SetName(self.Name)
	else log.error("NPC created without a name! They won't be able to open doors!") end

	if self.Ang then self:SetAngles(self.Ang) end

	if self.OnSpawn then self.OnSpawn(self) end

	self.allowedNodes = {}
	self.NavNodes = self.NavNodes or {}

	for k,v in pairs(ents.FindByClass( "info_townienode" ) ) do
		if self.NavNodes[v.Name] then
			table.insert(self.allowedNodes,v)
		end
	end

	self:SetUseType( SIMPLE_USE )

	self.DialogCursors = {}
	if oinit then
		oinit(self)
	end
end

function ENT:OnInjured(dmg)
	if self.OnDammage ~= nil then self:OnDammage(dmg) end
end

local removeblock = {
	["prop_door_rotating"] = function(bot,ent)
		ent:Fire("OpenAwayFrom",bot:GetName())
		timer.Simple(3,function()
			ent:Fire("Close")
		end)
	end
}

function ENT:BehaveUpdate(num)
	if not self.BehaveThread then return end
	if self.curnode and self.curnode:IsValid() and self.curnode:GetPos():Distance(self:GetPos()) < 5 then
		if self.nodereached == false then
			self.curnode.OnReached(self)
			self.nodereached = true
		else
			if self.curnode.IsDone(self) then
				--error("Finished action")
				self.curnode = self:selectNewNode()
				self.nodereached = false
			end
		end
	end
	local trdata = {
		["start"] = self:GetPos() + (self:GetUp() * 72),
		["endpos"] = self:GetPos() + (self:GetUp() * 72) + (self:GetForward() * 32),
		["filter"] = self
	}
	local tr = util.TraceLine(trdata)
	local ecl
	if (tr ~= nil) and (tr.Entity ~= nil) and tr.Entity:IsValid() then
		ecl = tr.Entity:GetClass()
	end
	if tr.Hit and removeblock[ecl] ~= nil then
		removeblock[ecl](self,tr.Entity)
	end
	local ok, message = coroutine.resume( self.BehaveThread )
	if not ok then
		self.BehaveThread = nil
		Msg( self, "error: ", message, "\n" );
	end

end

function ENT:OnKilled(dmg)
	if not self.Drops then return end
	for k,v in pairs(self.Drops) do
		local rng = math.random(0,100)
		local itemname = self.Drops[k][1]
		local itemchance = self.Drops[k][2]
		local heightoffset = 10
		if rng < itemchance then
			local drop = ents.Create("ws_item")
			drop.Item = GetItemByName(itemname)
			drop:SetModel(drop.Item.Model)
			drop:SetPos(self:GetPos() + (self:GetUp() * heightoffset))
			drop:Spawn()
			heightoffset = heightoffset + 10
		end
	end
	self:BecomeRagdoll( dmg )
end

local devs = {}
concommand.Add("artery_develop",function(ply,cmd,args)
	if not ply:IsAdmin() then return end
	if devs[ply] then
		devs[ply] = nil
	else
		devs[ply] = true
	end
end)

--Fix the dialog so we aren't sending functions
local function SendableDialog(tbl)
	local ntbl = table.Copy(tbl)
	ntbl["Options"] = table.GetKeys(tbl["Options"])
	return ntbl
end

function ENT:Use(ply)
	if devs[ply] then

	else

		self.DialogCursors[ply] = self.getDialogFor(ply)
		--
		-- if self.oyaw ~= 0 and self.oacc ~= 0 then
		-- 	self.oyaw, self.oacc,self.onod = self.loco:GetMaxYawRate(), self.loco:GetAcceleration(), self.curnode
		-- end
		-- self.loco:SetAcceleration(0)
		-- self.loco:SetVelocity(Vector(0,0,0))
		-- self.loco:SetMaxYawRate(9999990)
		-- self.loco:FaceTowards(ply:GetPos())
		-- self.loco:SetMaxYawRate(0)
		timer.Simple(0.5,function()
			--self.loco:FaceTowards(ply:GetPos())


		end)
		net.Start("art_opennpcdialog")
		net.WriteEntity(self)
		net.WriteTable(SendableDialog(self.DialogCursors[ply]))
		net.Send(ply)
	end
end

net.Receive("art_updatenpcdialog",function(len,ply)
	local npc = net.ReadEntity()
	local option = net.ReadString()
	npc.DialogCursors[ply] = npc.DialogCursors[ply]["Options"][option](ply)
	net.Start("art_opennpcdialog")
	net.WriteEntity(npc)
	net.WriteTable(SendableDialog(npc.DialogCursors[ply]))
	net.Send(ply)
end)

scripted_ents.Register(ENT, "art_npc")