forked from LostSavage/SecretHitlerCE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ttslua
More file actions
451 lines (396 loc) · 12.4 KB
/
common.ttslua
File metadata and controls
451 lines (396 loc) · 12.4 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
--Static
ALL_PLAYABLE_COLORS = {"White", "Brown", "Red", "Orange", "Yellow", "Green", "Teal", "Blue", "Purple", "Pink", "Tan", "Maroon"}
MAIN_PLAYABLE_COLORS = {"White", "Brown", "Red", "Orange", "Yellow", "Green", "Teal", "Blue", "Purple", "Pink"}
GREY_PLAYABLE_COLORS = {"Tan", "Maroon"}
GREY_PLAYABLE_COLORS_RGB = {Tan = {r = 210/255, g = 180/255, b = 140/255}, Maroon = {r = 128/255, g = 0/255, b = 0/255}}
GREY_AVATAR_POS = {Tan = {29.30, 9.4, -54}, Maroon = {0, 9.4, -54}}
GREY_HAND_ZONE_GUIDS = {Maroon = "b9a8d0", Tan = "409da9"}
GREY_TEXT_GUIDS = {Tan = "37d7b5", Maroon = "13f08c"}
GREY_VOTE_POS = {Tan = {29.30, 1.05, -35.50}, Maroon = {0, 1.05, -35.50}}
-- FIXME don't hardcode GREY_ABILITY_POS
GREY_ABILITY_POS = {Tan = {37, 1.2, -41}, Maroon = {12.60, 1.2, -41}}
GREY_FORWARD = 0
GREY_RIGHT = 0
GREY_UP = 0.3
NO_ROT = {x = 0, y = 0, z = 0}
FACE_UP_ROT = {x = 0, y = 180, z = 0}
FACE_DOWN_ROT = {x = 0, y = 180, z = 180}
function getPlayerPosRotVectors(playerColor)
local returnVal = {}
if greyPlayer(playerColor) then
local ph = getObjectFromGUID(greyPlayerHandGuids[playerColor])
if ph then
local pos = ph.getPosition()
returnVal.pos = {x = pos["x"], y = pos["y"], z = pos["z"] - 2.26}
returnVal.rot = ph.getRotation()
returnVal.vForward = ph.getTransformForward()
returnVal.vRight = ph.getTransformRight()
returnVal.vUp = ph.getTransformUp()
end
else
local ph = Player[playerColor].getPlayerHand();
if ph then
returnVal.pos = {x = ph["pos_x"], y = ph["pos_y"], z = ph["pos_z"]}
returnVal.rot = {x = ph["rot_x"], y = ph["rot_y"], z = ph["rot_z"]}
returnVal.vForward = {x = ph["trigger_forward_x"], y = ph["trigger_forward_y"], z = ph["trigger_forward_z"]}
returnVal.vRight = {x = ph["trigger_right_x"], y = ph["trigger_right_y"], z = ph["trigger_right_z"]}
returnVal.vUp = {x = ph["trigger_up_x"], y = ph["trigger_up_y"], z = ph["trigger_up_z"]}
end
end
return returnVal
end
--function giveObjectToPlayer(object, playerColor, posAdd, rotAdd, collide, fast)
function giveObjectToPlayer(object, playerColor, posAdd, rotAdd, ...)
local info = getPlayerPosRotVectors(playerColor)
if info then
if rotAdd["exactRot"] then
object.setRotationSmooth({rotAdd["x"], rotAdd["y"], rotAdd["z"]}, ...)
else
object.setRotationSmooth({info.rot["x"] + rotAdd["x"], info.rot["y"] + rotAdd["y"], info.rot["z"] + rotAdd["z"]}, ...)
end
if posAdd["forceHeight"] then
object.setPositionSmooth({info.pos["x"] + info.vForward["x"] * posAdd["forward"] + info.vRight["x"] * posAdd["right"] + info.vUp["x"] * posAdd["up"],
posAdd["forceHeight"],
info.pos["z"] + info.vForward["z"] * posAdd["forward"] + info.vRight["z"] * posAdd["right"] + info.vUp["z"] * posAdd["up"]}, ...)
else
object.setPositionSmooth({info.pos["x"] + info.vForward["x"] * posAdd["forward"] + info.vRight["x"] * posAdd["right"] + info.vUp["x"] * posAdd["up"],
info.pos["y"] + info.vForward["y"] * posAdd["forward"] + info.vRight["y"] * posAdd["right"] + info.vUp["y"] * posAdd["up"],
info.pos["z"] + info.vForward["z"] * posAdd["forward"] + info.vRight["z"] * posAdd["right"] + info.vUp["z"] * posAdd["up"]}, ...)
end
end
end
function forceObjectToPlayer(object, playerColor, posAdd, rotAdd)
local info = getPlayerPosRotVectors(playerColor)
if info then
if rotAdd["exactRot"] then
object.setRotation({rotAdd["x"], rotAdd["y"], rotAdd["z"]})
else
object.setRotation({info.rot["x"] + rotAdd["x"], info.rot["y"] + rotAdd["y"], info.rot["z"] + rotAdd["z"]})
end
if posAdd["forceHeight"] then
object.setPosition({info.pos["x"] + info.vForward["x"] * posAdd["forward"] + info.vRight["x"] * posAdd["right"] + info.vUp["x"] * posAdd["up"],
posAdd["forceHeight"],
info.pos["z"] + info.vForward["z"] * posAdd["forward"] + info.vRight["z"] * posAdd["right"] + info.vUp["z"] * posAdd["up"]})
else
object.setPosition({info.pos["x"] + info.vForward["x"] * posAdd["forward"] + info.vRight["x"] * posAdd["right"] + info.vUp["x"] * posAdd["up"],
info.pos["y"] + info.vForward["y"] * posAdd["forward"] + info.vRight["y"] * posAdd["right"] + info.vUp["y"] * posAdd["up"],
info.pos["z"] + info.vForward["z"] * posAdd["forward"] + info.vRight["z"] * posAdd["right"] + info.vUp["z"] * posAdd["up"]})
end
end
end
--tableIn = {guid = , max = , forward = , height = }
function moveObjectToPlayerByGUID(tableIn)
local object = getObjectFromGUID(tableIn.guid)
if object then
local playerColor = closestPlayer(object, players, tableIn.max)
if playerColor then
giveObjectToPlayer(object, playerColor, {forward = tableIn.forward, right = 0, up = 0, forceHeight = tableIn.height}, NO_ROT, false, true)
end
end
end
function findDistance(posA, posB)
return math.sqrt((posA["x"] - posB["x"])^2 +
(posA["y"] - posB["y"])^2 +
(posA["z"] - posB["z"])^2)
end
function closestPlayer(objectIn, playerListIn, maxIn)
local playerColorOut = nil
local pos = nil
local lastDistance = maxIn
if objectIn then
local tempPos = objectIn.getPosition()
for i, playerColor in ipairs(playerListIn) do
if greyPlayer(playerColor) then
ph = getObjectFromGUID(greyPlayerHandGuids[playerColor])
if ph then
pos = ph.getPosition()
pos = {x = pos["x"], y = pos["y"], z = pos["z"] - 2.26}
end
else
local ph = Player[playerColor].getPlayerHand()
if ph then
pos = {x = ph["pos_x"], y = ph["pos_y"], z = ph["pos_z"]}
end
end
if pos then
local distance = findDistance(tempPos, pos);
if distance < lastDistance then
lastDistance = distance
playerColorOut = playerColor
end
end
end
end
return playerColorOut
end
function sleep(numSeconds)
local t0 = os.clock()
while os.clock() - t0 <= numSeconds do coroutine.yield(0) end
end
function wait(numFrames)
for i=1,numFrames,1 do coroutine.yield(0) end
end
function destroyObjectByGUID(guidIn)
local dObject = getObjectFromGUID(guidIn)
if dObject then destroyObject(dObject) end
end
function rgbToHex(c)
return string.format("%02x%02x%02x", c["r"] * 255 , c["g"] * 255, c["b"] * 255)
end
function stringColorToHex(color)
return rgbToHex(stringColorToRGBExtra(color))
end
function stringColorToRGBExtra(color)
if greyPlayer(color) then
return GREY_PLAYABLE_COLORS_RGB[color]
else
return stringColorToRGB(color)
end
end
function removeBBCode(stringIn)
local out = ""
local formating = false
for i = 1, string.len(stringIn) do
local tmpChar = string.sub(stringIn, i, i)
if tmpChar == "[" then
formating = true
elseif tmpChar == "]" then
formating = false
else
if not formating then out = out .. tmpChar end
end
end
return out
end
function getPositionByGUID(guidIn)
local tmpObj = getObjectFromGUID(guidIn)
return tmpObj.getPosition()
end
function getPositionByGUIDOffsetZ(guidIn, offsetZ)
local tmpObj = getObjectFromGUID(guidIn)
local pos = tmpObj.getPosition()
pos["z"] = pos["z"] + offsetZ
return pos
end
function getDeckFromZoneByGUID(guidIn)
local deck = nil
local deck_ct = 0
local zone = getObjectFromGUID(guidIn)
local object
if zone then
local inZone = zone.getObjects()
for _, object in ipairs(inZone) do
if object.tag == "Card" then
deck_ct = 2
elseif object.tag == "Deck" then
deck = object
deck_ct = deck_ct + 1
end
end
end
if deck_ct == 1 then
return deck
end
return nil
end
function inTable(tableIn, valueIn)
local value
if tableIn then
for _, value in pairs(tableIn) do
if value == valueIn then
return true
end
end
end
return false
end
function smartTableInsert(tableIn, valueIn)
if not inTable(tableIn, valueIn) then
table.insert(tableIn, valueIn)
end
end
function versionInfo()
local msg
msg = _VERSION -- Lua info
if MOD_NAME then
msg = msg .. "\nMOD_NAME = " .. MOD_NAME
else
msg = msg .. "\nMOD_NAME = nil"
end
if UPDATE_VERSION then
msg = msg .. "\nUPDATE_VERSION = " .. UPDATE_VERSION
else
msg = msg .. "\nUPDATE_VERSION = nil"
end
msg = msg .. "\nGlobal Lua length " .. string.len(Global.getLuaScript())
return msg
end
function nullFunction()
--nothing here
end
-- This allows outside objects to call any function from this code.
-- Global.call("callFunction", { fcn = "drawCards", params = {amountToDraw, color} })
function callFunction(packet)
assert(type(_G[packet.fcn]) == "function", "No function named " .. packet.fcn .. " exists!")
return table.pack(_G[packet.fcn](unpack(packet.params or {})))
end
function tableToString(tbl, indent)
if not indent then indent = 0 end
local out = ""
for k, v in pairs(tbl) do
formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
out = out .. formatting .. "\n" .. tableToString(v, indent + 1)
elseif type(v) == "boolean" then
out = out .. formatting .. tostring(v) .. "\n"
else
out = out .. formatting .. v .. "\n"
end
end
return out
end
function generateAvatarImageUrl(steamId)
return string.format("http://steam-tts.bclass.info/avatar/?i=%s&s=l", steamId)
end
function greyPlayer(color)
if type(color) == "table" then
color = color[1]
end
if inTable(GREY_PLAYABLE_COLORS, color) then
return true
else
return false
end
end
function getPlayerObj(color)
if greyPlayer(color) then
local playerFound = nil
steamId = greyPlayerSteamIds[color]
for _, player in ipairs(Player.getSpectators()) do
if steamId == player.steam_id then
playerFound = player
break
end
end
return playerFound
else
return Player[color]
end
end
function getGreyColor(steamId)
local colorFound = nil
for testColor, testSteamId in pairs(greyPlayerSteamIds) do
if steamId == testSteamId then
colorFound = testColor
break
end
end
return colorFound
end
--Returns a simple table with all objects and their container GUIDs
function getObjsFromZone(zoneGUID)
local zoneObj = getObjectFromGUID(zoneGUID)
local objs = zoneObj.getObjects()
local returnTable = {}
local deckTab = {}
local deckGUID
for k1,v1 in pairs(objs) do
if v1.tag == "Card" then
table.insert(returnTable, {
name = v1.getName(),
description = v1.getDescription(),
deck_guid = nil,
index = nil,
guid = v1.getGUID()
})
elseif v1.tag == "Deck" then
deckGuid = v1.getGUID()
deckTab = v1.getObjects()
for k2, v2 in pairs(deckTab) do
table.insert(returnTable, {
name = v2.nickname,
description = v2.description,
deck_guid = deckGuid,
index = v2.index,
guid = v2.guid
})
end
end
end
return returnTable
end
function shufflePosition(objects)
local positionA
local positionB
for i = 1, #objects * 5 do
local a = math.random(#objects)
local b = math.random(#objects)
positionA = objects[a].getPosition()
positionB = objects[b].getPosition()
objects[b].setPosition(positionA)
objects[a].setPosition(positionB)
end
end
function shuffleTable(objects)
for i = 1, #objects * 5 do
local a = math.random(#objects)
local b = math.random(#objects)
objects[a], objects[b] = objects[b], objects[a]
end
end
function string:tokenize(sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
function string:titlecase()
return string.upper(string.sub(self, 1, 1)) .. string.lower(string.sub(self, 2))
end
function bigBroadcast(msgIn, playerObjIn)
local messageTable = string.tokenize(msgIn, "\n")
for i=#messageTable, 1, -1 do
playerObjIn:broadcast(messageTable[i])
end
end
function getPlayerByName(playerName, playerList)
local amountOfPlayersFound = 0
local playerToReturn = nil
if playerName then
for index, name in pairs(playerList) do
if string.match(string.lower(playerList[index].steam_name), string.lower(playerName)) then
playerToReturn = playerList[index]
amountOfPlayersFound = amountOfPlayersFound + 1
end
end
if amountOfPlayersFound == 1 then
return playerToReturn
end
end
return nil
end
function getPlayerByNameSteamID(playerNameSteamID, playerList)
if playerNameSteamID then
local playerFound = getPlayerByName(playerNameSteamID, playerList)
if playerFound then
return playerFound
else
for _, p in pairs(playerList) do
if p.steam_id == playerNameSteamID then
return p
end
end
end
end
return nil
end
function smartBroadcastToColor(msg, playerColor, msgColor)
if greyPlayer(playerColor) then
local player = getPlayerObj(playerColor)
if player then
player.broadcast(msg, msgColor)
end
else
broadcastToColor(msg, playerColor, msgColor)
end
end