forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimPing.lua
183 lines (165 loc) · 6 KB
/
SimPing.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
local SUtils = import("/lua/ai/sorianutilities.lua")
local PingLimit = 1 -- Maximum number of pings per army, within PingTimeout seconds
local PingTimeout = 1 -- How many seconds a ping counts toward an army's limit
local PingsRemaining = {}
local PingMarkers = {}
MaxPingMarkers = 15
--On first ping, send data to the user layer telling it the maximum allowable markers per army
Sync.MaxPingMarkers = MaxPingMarkers
function AnimatePingMesh(entity)
local time = 0
local ascending = true
while entity do
entity:SetScale(MATH_Lerp(math.sin(time), -.5, 0.5, .3, .5))
time = time + .3
WaitSeconds(.001)
end
end
function SpawnPing(data)
if not PingsRemaining[data.Owner] then
PingsRemaining[data.Owner] = PingLimit
end
if PingsRemaining[data.Owner] > 0 then
if data.Marker and PingMarkers[data.Owner] and table.getsize(PingMarkers[data.Owner]) >= MaxPingMarkers then
return
elseif data.Marker and not PingMarkers[data.Owner] then
PingMarkers[data.Owner] = {}
end
PingsRemaining[data.Owner] = PingsRemaining[data.Owner] - 1
if data.Marker and GetPingID(data.Owner) then
data.ID = GetPingID(data.Owner)
PingMarkers[data.Owner][data.ID] = data
else
local Entity = import("/lua/sim/entity.lua").Entity
data.Location[2] = data.Location[2]+2
local pingSpec = {Owner = data.Owner, Location = data.Location}
local ping = Entity(pingSpec)
Warp(ping, Vector(data.Location[1], data.Location[2], data.Location[3]))
ping:SetVizToFocusPlayer('Always')
ping:SetVizToEnemies('Never')
ping:SetVizToAllies('Always')
ping:SetVizToNeutrals('Never')
ping:SetMesh('/meshes/game/ping_'..data.Mesh)
local animThread = ForkThread(AnimatePingMesh, ping)
ForkThread(function()
WaitSeconds(data.Lifetime)
KillThread(animThread)
ping:Destroy()
end)
end
SendData(data)
-- Callbacks to allied brains
for num,brain in ArmyBrains do
if data.Owner + 1 ~= num and IsAlly(num, data.Owner + 1) then
ArmyBrains[num]:DoPingCallbacks(data)
if not SUtils.IsAIArmy(data.Owner + 1) then
ArmyBrains[num]:DoAIPing(data)
end
end
end
ForkThread(function(owner) WaitSeconds(PingTimeout) PingsRemaining[owner] = PingsRemaining[owner] + 1 end, data.Owner)
end
end
function SpawnSpecialPing(data)
--This function is used to generate automatic nuke pings
local Entity = import("/lua/sim/entity.lua").Entity
data.Location[2] = data.Location[2]+2
local pingSpec = {Owner = data.Owner, Location = data.Location}
local ping = Entity(pingSpec)
Warp(ping, Vector(data.Location[1], data.Location[2], data.Location[3]))
ping:SetVizToFocusPlayer('Always')
ping:SetVizToEnemies('Never')
ping:SetVizToAllies('Always')
ping:SetVizToNeutrals('Never')
ping:SetMesh('/meshes/game/ping_'..data.Mesh)
local animThread = ForkThread(AnimatePingMesh, ping)
ForkThread(function()
WaitSeconds(data.Lifetime)
KillThread(animThread)
ping:Destroy()
end)
SendData(data)
-- Callbacks to allied brains
for num,brain in ArmyBrains do
if data.Owner + 1 ~= num and IsAlly(num, data.Owner + 1) then
ArmyBrains[num]:DoPingCallbacks(data)
if not SUtils.IsAIArmy(data.Owner + 1) then
ArmyBrains[num]:DoAIPing(data)
end
end
end
end
function GetPingID(owner)
for i = 1, MaxPingMarkers do
if not PingMarkers[owner][i] then
return i
end
end
return false
end
function OnArmyDefeat(armyID)
armyID = armyID - 1
if PingMarkers[armyID] then
for i, v in PingMarkers[armyID] do
UpdateMarker({Action = 'delete', ID = i, Owner = v.Spec.Owner})
end
end
end
function OnArmyChange()
Sync.MaxPingMarkers = MaxPingMarkers
LOG('syncing max ping markers: ', MaxPingMarkers)
--Flush all of the current markers on the UI side
if not Sync.Ping then Sync.Ping = {} end
table.insert(Sync.Ping, {Action = 'flush'})
--Add All of the relevant marker data on the next sync
local focus = GetFocusArmy()
if focus ~= -1 then
ForkThread(function()
for ownerID, pingTable in PingMarkers do
if IsAlly(ownerID+1, focus) then
for pingID, ping in pingTable do
ping.Renew = true
SendData(ping)
end
end
end
end)
end
end
function OnAllianceChange()
OnArmyChange()
end
function UpdateMarker(data)
if PingMarkers[data.Owner][data.ID] or data.Action == 'renew' then
if data.Action == 'delete' then
PingMarkers[data.Owner][data.ID] = nil
elseif data.Action == 'move' then
PingMarkers[data.Owner][data.ID].Location = data.Location
elseif data.Action == 'rename' then
PingMarkers[data.Owner][data.ID].Name = data.Name
elseif data.Action == 'renew' then
local focus = GetFocusArmy()
if focus ~= -1 then
ForkThread(function()
for ownerID, pingTable in PingMarkers do
if IsAlly(ownerID+1, focus) then
for pingID, ping in pingTable do
ping.Renew = true
SendData(ping)
end
end
end
end)
end
return
end
SendData(data)
end
end
function SendData(data)
local focus = GetFocusArmy()
if focus ~= -1 and IsAlly(data.Owner+1, focus) then
if not Sync.Ping then Sync.Ping = {} end
table.insert(Sync.Ping, data)
end
end