-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsh_roles.lua
283 lines (231 loc) · 6.14 KB
/
sh_roles.lua
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
283
local ttt_traitor_max = CreateConVar("ttt_traitor_max", "32", FCVAR_REPLICATED, "The max amount of traitors in a round.")
local ttt_traitor_pct = CreateConVar("ttt_traitor_pct", "0.25", FCVAR_REPLICATED, "The percent of players that will be traitors.")
local ttt_detective_pct = CreateConVar("ttt_detective_pct", "0.13", FCVAR_REPLICATED, "The percent of players that will be detectives.")
local ttt_detective_max = CreateConVar("ttt_detective_max", "32", FCVAR_REPLICATED, "The max amount of detectives in a round.")
local ttt_detective_min_players = CreateConVar("ttt_detective_min_players", "10", FCVAR_REPLICATED, "The amount of players needed before detectives spawn.")
local TEAM = {}
function ttt.CanPlayerSeePlayersRole(looker, ply)
local SeenBy = ply:GetRoleData().CanBeSeenBy
local TeamSeenBy = ply:GetRoleTeamData().CanBeSeenBy
if (SeenBy) then
if (SeenBy[looker:GetRole()] or SeenBy[looker:GetRoleTeam()] or SeenBy["*"]) then
return true
end
end
if (TeamSeenBy and TeamSeenBy ~= SeenBy) then
if (TeamSeenBy[looker:GetRole()] or TeamSeenBy[looker:GetRoleTeam()] or TeamSeenBy["*"]) then
return true
end
end
return false
end
local function AccessorFunc(self, name, fnname)
fnname = fnname or name
self["Get" .. fnname] = function(self)
return self[name]
end
self["Set" .. fnname] = function(self, v)
self[name] = v
return self
end
end
function TEAM:SetColor(col, g, b, a)
if (type(col) == "number") then
col = Color(col, g, b, a)
end
self.Color = col
return self
end
local SEEN_BY_ALL = {
__index = function() return true end,
__newindex = function() end,
__eq = function() return true end
}
function TEAM:SeenByAll()
self.CanBeSeenBy = setmetatable({}, SEEN_BY_ALL)
return self
end
function TEAM:SeenBy(what)
if (not self.CanBeSeenBy) then
self.CanBeSeenBy = {}
end
if (type(what) ~= "table") then
what = {what}
end
for _, what in ipairs(what) do
self.CanBeSeenBy[what] = true
end
return self
end
function TEAM:TeamChatSeenBy(what)
if (not self.TeamChatCanBeSeenBy) then
self.TeamChatCanBeSeenBy = {}
end
if (type(what) ~= "table") then
what = {what}
end
for _, what in ipairs(what) do
self.TeamChatCanBeSeenBy[what] = true
end
return self
end
function TEAM:CreditOnRoleDeath(func)
self.OnRoleDeath = func
return self
end
AccessorFunc(TEAM, "ModifyTickets", "ModifyTicketsFunc")
AccessorFunc(TEAM, "Evil")
AccessorFunc(TEAM, "CanUseBuyMenu")
AccessorFunc(TEAM, "VoiceChannel")
AccessorFunc(TEAM, "DeathIcon")
AccessorFunc(TEAM, "DefaultCredits")
AccessorFunc(TEAM, "CalculateAmount", "CalculateAmountFunc")
AccessorFunc(TEAM, "NoIgnoreZ", "CanSeeThroughWalls")
setmetatable(SEEN_BY_ALL, SEEN_BY_ALL)
local ROLE = {
SetColor = TEAM.SetColor,
SeenBy = TEAM.CanBeSeenBy,
SeenByAll = TEAM.CanBeSeenByAll
}
local TEAM_MT = {
__index = TEAM
}
local function Team(name)
local t = ttt.teams[name] or setmetatable({
Name = name,
Speed = 260,
--[[
RunSpeed = 400,
RunTime = 3, -- seconds
RunRecovery = 0.5, -- per second
RunMinimum = 0.5 -- before running you have to have this amount of a fraction
]]
}, TEAM_MT)
ttt.teams[name] = t
return t
end
local ROLE_MT = {
__index = function(self, index)
local Team = rawget(self, "Team")
if (Team and Team[index]) then
return Team[index]
end
return ROLE[index]
end
}
local function Role(name, team)
local role = ttt.roles[name] or setmetatable({
Name = name,
Team = Team(team)
}, ROLE_MT)
ttt.roles[name] = role
return role
end
function GM:TTTPrepareRoles(Team, Role)
Team "innocent"
:SetColor(19, 130, 77)
:SetDeathIcon "materials/tttrw/roles/innocent.png"
Team "traitor"
:SeenBy {"traitor"}
:SetColor(136, 21, 22)
:SetDefaultCredits(2)
:CreditOnRoleDeath(function(roles, deathrole)
if (deathrole.Team == "traitor") then
return 0
end
local amt = 0
for role, i in pairs(roles) do
if (role.Team.Name ~= "traitor") then
amt = amt + i
end
end
return amt % 4 == 0 and 1 or 0
end)
:TeamChatSeenBy "traitor"
:SetVoiceChannel "traitor"
:SetEvil(true)
:SetCanUseBuyMenu(true)
:SetDeathIcon "materials/tttrw/tbutton.png"
:SetModifyTicketsFunc(function(tickets)
return tickets / 2
end)
Team "spectator"
:SeenByAll()
:SetColor(Color(51, 54, 56))
Role("Innocent", "innocent")
Role("Spectator", "spectator")
Role("Detective", "innocent")
:SeenByAll()
:SetCalculateAmountFunc(function(total_players)
return math.floor(math.Clamp(total_players * ttt_detective_pct:GetFloat(), 0, ttt_detective_max:GetInt()))
end)
:SetColor(5, 75, 153)
:TeamChatSeenBy "Detective"
:SetVoiceChannel "Detective"
:SetCanUseBuyMenu(true)
:SetModifyTicketsFunc(function(tickets)
return tickets - 1
end)
:SetDeathIcon "materials/tttrw/roles/detective.png"
:SetDefaultCredits(2)
:CreditOnRoleDeath(function(roles, deathrole)
if (not deathrole.Evil) then
return 0
end
local amt = 0
for role, i in pairs(roles) do
if (role.Evil) then
amt = amt + i
end
end
return amt % 3 == 0 and 1 or 0
end)
Role("Traitor", "traitor")
:SetCalculateAmountFunc(function(total_players)
return math.floor(math.Clamp(total_players * ttt_traitor_pct:GetFloat(), 1, ttt_traitor_max:GetInt()))
end)
end
function GM:SetupRoles()
ttt.roles = {}
ttt.teams = {}
hook.Run("TTTPrepareRoles", Team, Role)
for name, role in pairs(ttt.roles) do
local team = role.Team
if (team.CanBeSeenBy) then
if (not role.CanBeSeenBy or team.CanBeSeenBy == SEEN_BY_ALL) then
role.CanBeSeenBy = team.CanBeSeenBy
else
for what in pairs(team.CanBeSeenBy) do
role.CanBeSeenBy[what] = true
end
end
end
if (not role.Color) then
role.Color = color_black
end
end
end
hook.Add("TTTGetHiddenPlayerVariables", "Roles", function(vars)
table.insert(vars, {
Name = "Role",
Type = "String",
Default = "Spectator",
Enums = {}
})
table.insert(vars, {
Name = "Credits",
Type = "Int",
Default = 0,
Enums = {}
})
end)
local PLY = FindMetaTable "Player"
function PLY:GetRoleData()
return ttt.roles[self:GetRole()]
end
function PLY:GetRoleTeamData()
return ttt.roles[self:GetRole()].Team
end
function PLY:GetRoleTeam()
return ttt.roles[self:GetRole()].Team.Name
end