Skip to content

Commit

Permalink
Fix Kennel lag and animation
Browse files Browse the repository at this point in the history
Issue was, if you build more than 30 kennels and upgrade them at the same time, it was freezing the game for 2-3 seconds when new drones spawns.
The lag came from a table.deepcopy function that was making a deepcopy of the drone data.
The deepcopy was not only making a copy of the unit, it also did a copy of platoondata, Brain and all other tables that where only present by a pointer.

Also fixed a minor issue with the door animation. They only opens if a real buildcommand was issued.
Selecting the drone and just let them start was not opening the doors.
Now doors stay open if the first drone is detached and will close if all drones return.
  • Loading branch information
Uveso authored and shalkya committed Feb 29, 2020
1 parent 819dd6f commit ccd5a0c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
74 changes: 43 additions & 31 deletions lua/terranunits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,13 @@ TPodTowerUnit = Class(TStructureUnit) {
if not self.PodData then
self.PodData = {}
end
self.PodData[pod.PodName] = table.deepcopy(podData)
self.PodData[pod.PodName] = {}
self.PodData[pod.PodName].PodHandle = pod
self.PodData[pod.PodName].PodUnitID = podData.PodUnitID
self.PodData[pod.PodName].PodName = podData.PodName
self.PodData[pod.PodName].Active = podData.Active
self.PodData[pod.PodName].PodAttachpoint = podData.PodAttachpoint
self.PodData[pod.PodName].CreateWithUnit = podData.CreateWithUnit
pod:SetParent(self, pod.PodName)
end
end,
Expand Down Expand Up @@ -443,33 +448,6 @@ TPodTowerUnit = Class(TStructureUnit) {
self.PodData[podName].Active = false
end,

NotifyOfPodStartBuild = function(self)
if not self.OpeningAnimationStarted then
self.OpeningAnimationStarted = true
local bp = self:GetBlueprint()
if not bp.Display.AnimationOpen then return end
if not self.OpenAnim then
self.OpenAnim = CreateAnimator(self)
self.Trash:Add(self.OpenAnim)
end
self.OpenAnim:PlayAnim(bp.Display.AnimationOpen, false):SetRate(2.0)
WaitSeconds(0.5)
if not self.NowUpgrading then
self.OpenAnim:SetRate(0)
end
end
end,

NotifyOfPodStopBuild = function(self)
if self.OpeningAnimationStarted then
local bp = self:GetBlueprint()
if not bp.Display.AnimationOpen then return end
if not self.OpenAnim then return end
self.OpenAnim:SetRate(1.5)
self.OpeningAnimationStarted = false
end
end,

SetPodConsumptionRebuildRate = function(self, podData)
local bp = self:GetBlueprint()
-- Get build rate of tower
Expand All @@ -493,16 +471,50 @@ TPodTowerUnit = Class(TStructureUnit) {

OnTransportAttach = function(self, bone, attachee)
attachee:SetDoNotTarget(true)
self:PlayUnitSound('Load')
self:RequestRefreshUI()
local PodPresent = 0
for _, v in self.PodData or {} do
if v.Active then
PodPresent = PodPresent + 1
end
end
local PodAttached = 0
for _, v in self:GetCargo() do
PodAttached = PodAttached + 1
end
if PodAttached == PodPresent and self.OpeningAnimationStarted then
local bp = self:GetBlueprint()
if not self.OpenAnim then return end
self.OpenAnim:SetRate(1.5)
self.OpeningAnimationStarted = false
end
end,

OnTransportDetach = function(self, bone, attachee)
attachee:SetDoNotTarget(false)
self:PlayUnitSound('Unload')
self:RequestRefreshUI()
if not self.OpeningAnimationStarted then
self.OpeningAnimationStarted = true
local bp = self:GetBlueprint()
if not self.OpenAnim then
self.OpenAnim = CreateAnimator(self)
self.Trash:Add(self.OpenAnim)
end
self.OpenAnim:PlayAnim(bp.Display.AnimationOpen, false):SetRate(2.0)
-- wait 5 ticks and stop the animation so that the doors stay open
ForkThread(function ()
coroutine.yield(5)
self.OpenAnim:SetRate(0)
end)
end
end,

FinishedBeingBuilt = State {
Main = function(self)
-- Wait one tick to make sure this wasn't captured and we don't create an extra pod
WaitSeconds(0.1)
coroutine.yield(1)

-- Create the pod for the kennel. DO NOT ADD TO TRASH.
-- This pod may have to be passed to another unit after it upgrades. We cannot let the trash clean it up
Expand All @@ -514,7 +526,7 @@ TPodTowerUnit = Class(TStructureUnit) {
if not self.PodData then
self.PodData = {}
end
self.PodData[v.PodName] = table.deepcopy(v)
self.PodData[v.PodName] = table.copy(v)
self:CreatePod(v.PodName)
end
end
Expand Down Expand Up @@ -554,7 +566,7 @@ TPodTowerUnit = Class(TStructureUnit) {
ChangeState(self, self.RebuildingPodState)
end
end
WaitSeconds(1)
coroutine.yield(1)
end
end,

Expand Down
21 changes: 0 additions & 21 deletions units/XEA3204/XEA3204_script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ XEA3204 = Class(TConstructionUnit) {
OnCreate = function(self)
TConstructionUnit.OnCreate(self)
self.docked = true
self.returning = false
end,

SetParent = function(self, parent, podName)
Expand All @@ -30,34 +29,14 @@ XEA3204 = Class(TConstructionUnit) {

OnStartBuild = function(self, unitBeingBuilt, order)
TConstructionUnit.OnStartBuild(self, unitBeingBuilt, order)
self.returning = false
end,

OnStopBuild = function(self, unitBuilding)
TConstructionUnit.OnStopBuild(self, unitBuilding)
self.returning = true
end,

OnFailedToBuild = function(self)
TConstructionUnit.OnFailedToBuild(self)
self.returning = true
end,

OnMotionHorzEventChange = function(self, new, old)
if self and not self.Dead then
if self.Parent and not self.Parent.Dead then
local myPosition = self:GetPosition()
local parentPosition = self.Parent:GetPosition(self.Parent.PodData[self.PodName].PodAttachpoint)
local distSq = VDist2Sq(myPosition[1], myPosition[3], parentPosition[1], parentPosition[3])
if self.docked and distSq > 0 and not self.returning then
self.docked = false
self.Parent:ForkThread(self.Parent.NotifyOfPodStartBuild)
elseif not self.docked and distSq < 1 and self.returning then
self.docked = true
self.Parent:ForkThread(self.Parent.NotifyOfPodStopBuild)
end
end
end
end,

-- Don't make wreckage
Expand Down

0 comments on commit ccd5a0c

Please sign in to comment.