forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimSync.lua
206 lines (175 loc) · 5.85 KB
/
SimSync.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
---@declare-global
-- The global sync table is copied to the user layer every time the main and sim threads are
-- synchronized on the sim beat (which is like a tick but happens even when the game is paused)
Sync = {}
-- UnitData that has been synced. We keep a separate copy of this so when we change
-- focus army we can resync the data.
UnitData = {}
SimUnitEnhancements = {}
function ResetSyncTable()
Sync = {
-- A list of camera control operations that we'd like the user layer to perform.
CameraRequests = {},
Sounds = {},
Voice = {},
AIChat = {},
-- Table of army indices set to "victory" or "defeat".
-- It's the user layer's job to determine if any UI needs to be shown
-- for the focus army.
GameResult = {},
-- Player to player queries that can affect the Sim
PlayerQueries = {},
QueryResults = {},
-- Contain operation data when op is complete
OperationComplete = nil,
UnitData = {},
ReleaseIds = {},
-- contains the current score for each army
Score = {},
ScoreAccum = {},
Reclaim = {}
}
end
function AddUnitEnhancement(unit, enhancement, slot)
if not slot then return end
local id = unit.EntityId
SimUnitEnhancements[id] = SimUnitEnhancements[id] or {}
SimUnitEnhancements[id][slot] = enhancement
SyncUnitEnhancements()
end
function RemoveUnitEnhancement(unit, enhancement)
if not unit or unit.Dead then return end
local id = unit.EntityId
local slots = SimUnitEnhancements[id]
if not slots then return end
local key = nil
for k, v in slots do
if v == enhancement then
key = k
break
end
end
if not key then return end
SimUnitEnhancements[id][key] = nil
if table.empty(slots) then
SimUnitEnhancements[id] = nil
end
SyncUnitEnhancements()
end
function RemoveAllUnitEnhancements(unit)
local id = unit.EntityId
if not SimUnitEnhancements[id] then return end
SimUnitEnhancements[id] = nil
SyncUnitEnhancements()
end
function SyncUnitEnhancements()
import('/lua/enhancementcommon.lua').SetEnhancementTable(SimUnitEnhancements)
local sync = {}
for id, slots in SimUnitEnhancements do
local unit = GetEntityById(id)
local me = GetFocusArmy()
if unit and (me == -1 or IsAlly(me, unit.Army)) then
sync[id] = slots
end
end
Sync.UserUnitEnhancements = sync
end
function DebugMoveCamera(x0,y0,x1,y1)
local Camera = import('/lua/SimCamera.lua').SimCamera
local cam = Camera("WorldCamera")
-- cam:ScaleMoveVelocity(0.02)
cam:MoveTo(Rect(x0,y0,x1,y1),5.0)
end
function SyncPlayableRect(rect)
local Camera = import('/lua/SimCamera.lua').SimCamera
local cam = Camera("WorldCamera")
cam:SyncPlayableRect(rect)
end
function LockInput()
Sync.LockInput = true
end
function UnlockInput()
Sync.UnlockInput = true
end
function OnPostLoad()
local focus = GetFocusArmy()
for entityID, data in UnitData do
if data.OwnerArmy == focus or focus == -1 then
Sync.UnitData[entityID] = data.Data
end
end
Sync.IsSavedGame = true
end
function NoteFocusArmyChanged(new, old)
--LOG('NoteFocusArmyChanged(new=' .. repr(new) .. ', old=' .. repr(old) .. ')')
import('/lua/SimPing.lua').OnArmyChange()
for entityID, data in UnitData do
if new == -1 or data.OwnerArmy == new then
Sync.UnitData[entityID] = data.Data
elseif old == -1 or data.OwnerArmy == old then
Sync.ReleaseIds[entityID] = true
else
end
end
SyncUnitEnhancements()
Sync.FocusArmyChanged = {new = new, old = old}
end
function FloatingEntityText(entityId, text)
if not entityId and text then
WARN('Trying to float entity text with no entityId or no text.')
return false
else
if GetEntityById(entityId).Army == GetFocusArmy() then
if not Sync.FloatingEntityText then Sync.FloatingEntityText = {} end
table.insert(Sync.FloatingEntityText, {entity = entityId, text = text})
end
end
end
function StartCountdown(entityId, duration)
cdDuration = duration or 5
if not entityId then
WARN('Trying to start countdown text with no entityId.')
return false
else
if GetEntityById(entityId).Army == GetFocusArmy() then
if not Sync.StartCountdown then Sync.StartCountdown = {} end
table.insert(Sync.StartCountdown, {entity = entityId, duration = cdDuration})
end
end
end
function CancelCountdown(entityId)
if not entityId then
WARN('Trying to Cancel Countdown text with no entityId.')
return false
else
if GetEntityById(entityId).Army == GetFocusArmy() then
if not Sync.CancelCountdown then Sync.CancelCountdown = {} end
table.insert(Sync.CancelCountdown, {entity = entityId})
end
end
end
function HighlightUIPanel(panel)
if not Sync.HighlightUIPanel then Sync.HighlightUIPanel = {} end
table.insert(Sync.HighlightUIPanel, panel)
end
function ChangeCameraZoom(newMult)
Sync.ChangeCameraZoom = newMult
end
function CreateCameraMarker(position)
return import('/lua/simcameramarkers.lua').AddCameraMarker(position)
end
function EndDemo()
Sync.EndDemo = true
end
function PrintText(text, fontSize, fontColor, duration, location)
if not text and location then
WARN('Trying to print text with no string or no location.')
return false
else
if not Sync.PrintText then Sync.PrintText = {} end
table.insert(Sync.PrintText, {text = text, size = fontSize, color = fontColor, duration = duration, location = location})
end
end
function CreateDialogue(text, buttonText, position)
return import('/lua/SimDialogue.lua').Create(text, buttonText, position)
end