forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnhanceTask.lua
More file actions
91 lines (78 loc) · 3.18 KB
/
EnhanceTask.lua
File metadata and controls
91 lines (78 loc) · 3.18 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
#*****************************************************************************
#* File: lua/modules/EnhanceTask.lua
#*
#* Copyright Š 2008 Gas Powered Games, Inc. All rights reserved.
#*****************************************************************************
local ScriptTask = import('/lua/sim/ScriptTask.lua').ScriptTask
local TASKSTATUS = import('/lua/sim/ScriptTask.lua').TASKSTATUS
local AIRESULT = import('/lua/sim/ScriptTask.lua').AIRESULT
EnhanceTask = Class(ScriptTask) {
OnCreate = function(self,commandData)
ScriptTask.OnCreate(self,commandData)
self:GetUnit():SetWorkProgress(0.0)
self:GetUnit():SetUnitState('Enhancing',true)
self:GetUnit():SetUnitState('Upgrading',true)
self.LastProgress = 0
ChangeState(self, self.Stopping)
end,
OnDestroy = function(self)
self:GetUnit():SetUnitState('Enhancing',false)
self:GetUnit():SetUnitState('Upgrading',false)
self:GetUnit():SetWorkProgress(0.0)
if self.Success then
self:SetAIResult(AIRESULT.Success)
else
self:SetAIResult(AIRESULT.Fail)
self:GetUnit():OnWorkFail(self.CommandData.Enhancement)
end
end,
Stopping = State {
TaskTick = function(self)
local unit = self:GetUnit()
if unit:IsMobile() and unit:IsMoving() then
unit:GetNavigator():AbortMove()
return TASKSTATUS.Wait
else
-- check if enhancement was started (not restricted and met prerequisite)
local workStarted = unit:OnWorkBegin(self.CommandData.Enhancement)
if not workStarted then
self.Success = false -- required for AI notification
return TASKSTATUS.Done -- not using Abort because it will freeze the unit
else
ChangeState(self, self.Enhancing)
return TASKSTATUS.Repeat
end
end
end,
},
Enhancing = State {
TaskTick = function(self)
local unit = self:GetUnit()
local current = unit.WorkProgress
if not unit:IsPaused() then
local obtained = unit:GetResourceConsumed()
if obtained > 0 then
local frac = (1 / (unit.WorkItemBuildTime / unit:GetBuildRate())) * obtained * SecondsPerTick()
current = current + frac
unit.WorkProgress = current
end
end
if((self.LastProgress < 0.25 and current >= 0.25) or
(self.LastProgress < 0.50 and current >= 0.50) or
(self.LastProgress < 0.75 and current >= 0.75)) then
unit:OnBuildProgress(self.LastProgress,current)
end
self.LastProgress = current
unit:SetWorkProgress(current)
if(current < 1.0) then
return TASKSTATUS.Wait
end
unit:OnWorkEnd(self.CommandData.Enhancement)
if unit:IsPaused() then
unit:SetPaused(false)
end
self.Success = true
return TASKSTATUS.Done
end,
},
}