forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteViewing.lua
162 lines (147 loc) · 7.38 KB
/
RemoteViewing.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
--****************************************************************************
--**
--** File : /lua/RemoteViewing.lua
--** Author(s): Dru Staltman
--**
--** Summary : File that creates in units ability to create Remote Entities
--**
--** Copyright © 2007 Gas Powered Games, Inc. All rights reserved.
--****************************************************************************
local VizMarker = import("/lua/sim/vizmarker.lua").VizMarker
-- TODO: make sure each new instance is using a previous metatable
function RemoteViewing(SuperClass)
return Class(SuperClass) {
OnCreate = function(self)
SuperClass.OnCreate(self)
self.RemoteViewingData = {}
self.RemoteViewingData.RemoteViewingFunctions = {}
self.RemoteViewingData.DisableCounter = 0
self.RemoteViewingData.IntelButton = true
end,
OnStopBeingBuilt = function(self,builder,layer)
self.Sync.Abilities = self:GetBlueprint().Abilities
self:SetMaintenanceConsumptionInactive()
SuperClass.OnStopBeingBuilt(self,builder,layer)
end,
OnKilled = function(self, instigator, type, overkillRatio)
SuperClass.OnKilled(self, instigator, type, overkillRatio)
if self.RemoteViewingData.Satellite then
self.RemoteViewingData.Satellite:Destroy()
end
self:SetMaintenanceConsumptionInactive()
end,
DisableRemoteViewingButtons = function(self)
self.Sync.Abilities = self:GetBlueprint().Abilities
self.Sync.Abilities.TargetLocation.Active = false
self:RemoveToggleCap('RULEUTC_IntelToggle')
end,
EnableRemoteViewingButtons = function(self)
self.Sync.Abilities = self:GetBlueprint().Abilities
self.Sync.Abilities.TargetLocation.Active = true
self:AddToggleCap('RULEUTC_IntelToggle')
end,
OnTargetLocation = function(self, location)
-- Initial energy drain here - we drain resources instantly when an eye is relocated (including initial move)
local aiBrain = self:GetAIBrain()
local bp = self:GetBlueprint()
local have = aiBrain:GetEconomyStored('ENERGY')
local need = bp.Economy.InitialRemoteViewingEnergyDrain
if not ( have > need ) then
return
end
-- Drain economy here
aiBrain:TakeResource( 'ENERGY', bp.Economy.InitialRemoteViewingEnergyDrain )
self.RemoteViewingData.VisibleLocation = location
self:CreateVisibleEntity()
end,
CreateVisibleEntity = function(self)
-- Only give a visible area if we have a location and intel button enabled
if not self.RemoteViewingData.VisibleLocation then
self:SetMaintenanceConsumptionInactive()
return
end
if self.RemoteViewingData.VisibleLocation and self.RemoteViewingData.DisableCounter == 0 and self.RemoteViewingData.IntelButton then
local bp = self:GetBlueprint()
self:SetMaintenanceConsumptionActive()
-- Create new visible area
if not self.RemoteViewingData.Satellite then
local spec = {
X = self.RemoteViewingData.VisibleLocation[1],
Z = self.RemoteViewingData.VisibleLocation[3],
Radius = bp.Intel.RemoteViewingRadius,
LifeTime = -1,
Omni = false,
Radar = false,
Vision = true,
WaterVision = true,
Army = self.Army,
}
self.RemoteViewingData.Satellite = VizMarker(spec)
self.Trash:Add(self.RemoteViewingData.Satellite)
else
-- Move and reactivate old visible area
if not self.RemoteViewingData.Satellite:BeenDestroyed() then
Warp( self.RemoteViewingData.Satellite, self.RemoteViewingData.VisibleLocation )
self.RemoteViewingData.Satellite:EnableIntel('Omni')
self.RemoteViewingData.Satellite:EnableIntel('Radar')
self.RemoteViewingData.Satellite:EnableIntel('Vision')
self.RemoteViewingData.Satellite:EnableIntel('WaterVision')
end
end
-- monitor resources
if self.RemoteViewingData.ResourceThread then
self.RemoteViewingData.ResourceThread:Destroy()
end
self.RemoteViewingData.ResourceThread = self:ForkThread(self.DisableResourceMonitor)
end
end,
DisableVisibleEntity = function(self)
-- visible entity already off
if self.RemoteViewingData.DisableCounter > 1 then return end
-- disable vis entity and monitor resources
if not self.Dead and self.RemoteViewingData.Satellite then
self.RemoteViewingData.Satellite:DisableIntel('Omni')
self.RemoteViewingData.Satellite:DisableIntel('Radar')
self.RemoteViewingData.Satellite:DisableIntel('Vision')
self.RemoteViewingData.Satellite:DisableIntel('WaterVision')
end
end,
OnIntelEnabled = function(self, intel)
-- Make sure the button is only calculated once rather than once per possible intel type
if not self.RemoteViewingData.IntelButton then
self.RemoteViewingData.IntelButton = true
self.RemoteViewingData.DisableCounter = self.RemoteViewingData.DisableCounter - 1
self:CreateVisibleEntity()
end
SuperClass.OnIntelEnabled(self, intel)
end,
OnIntelDisabled = function(self, intel)
-- make sure button is only calculated once rather than once per possible intel type
if self.RemoteViewingData.IntelButton then
self.RemoteViewingData.IntelButton = false
self.RemoteViewingData.DisableCounter = self.RemoteViewingData.DisableCounter + 1
self:DisableVisibleEntity()
end
SuperClass.OnIntelDisabled(self, intel)
end,
DisableResourceMonitor = function(self)
WaitSeconds(0.5)
local fraction = self:GetResourceConsumed()
while fraction == 1 do
WaitSeconds(0.5)
fraction = self:GetResourceConsumed()
end
if self.RemoteViewingData.IntelButton then
self:DisableVisibleEntity()
self.RemoteViewingData.DisableCounter = self.RemoteViewingData.DisableCounter + 1
self.RemoteViewingData.ResourceThread = self:ForkThread(self.EnableResourceMonitor)
end
end,
EnableResourceMonitor = function(self)
local recharge = self:GetBlueprint().Intel.ReactivateTime or 10
WaitSeconds(recharge)
self.RemoteViewingData.DisableCounter = self.RemoteViewingData.DisableCounter - 1
self:CreateVisibleEntity()
end,
}
end