Skip to content

Commit 0f84618

Browse files
committed
Optimise laser rifle
1 parent cd6b982 commit 0f84618

File tree

1 file changed

+76
-54
lines changed

1 file changed

+76
-54
lines changed
Lines changed: 76 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,51 @@
1+
local function InsertParticle(var, particle)
2+
var.addedParticleCount = var.addedParticleCount + 1;
3+
var.addedParticles[var.addedParticleCount] = particle;
4+
end
5+
6+
local function emitSmoke(self, particleCount)
7+
local var = self.var;
8+
local MuzzlePos = self.MuzzlePos;
9+
for i = 1, particleCount do
10+
local smoke = CreateMOSParticle("Tiny Smoke Ball 1" .. (math.random() < 0.5 and " Glow Blue" or ""), "Base.rte");
11+
smoke.Pos = MuzzlePos;
12+
smoke.Lifetime = smoke.Lifetime * RangeRand(0.5, 1.0);
13+
smoke.Vel = var.Vel * 0.5 + Vector(RangeRand(0, i), 0):RadRotate(RangeRand(-math.pi, math.pi));
14+
InsertParticle(var, smoke);
15+
end
16+
self:RequestSyncedUpdate();
17+
end
18+
119
function Create(self)
2-
self.range = math.sqrt(FrameMan.PlayerScreenWidth^2 + FrameMan.PlayerScreenHeight^2)/2;
3-
self.penetrationStrength = 170;
4-
self.strengthVariation = 5;
20+
-- Create local table to store variables for performance
21+
local var = {};
22+
23+
var.range = math.sqrt(FrameMan.PlayerScreenWidth^2 + FrameMan.PlayerScreenHeight^2)/2;
24+
var.penetrationStrength = 170;
25+
var.strengthVariation = 5;
526
--This value tracks the shots and varies the penetration strength to create a "resistance" effect on tougher materials
6-
self.shotCounter = 0; --TODO: Rename/describe this variable better
7-
self.activity = ActivityMan:GetActivity();
27+
var.shotCounter = 0; --TODO: Rename/describe this variable better
28+
var.activity = ActivityMan:GetActivity();
829

9-
self.cooldown = Timer();
10-
self.cooldownSpeed = 0.5;
30+
var.cooldown = Timer();
31+
var.cooldownSpeed = 0.5;
1132

12-
self.addedParticles = {};
33+
var.addedParticles = {};
34+
var.addedParticleCount = 0;
1335

14-
self.addedWound = nil;
15-
self.addedWoundOffset = nil;
16-
self.addedWoundToUniqueID = nil;
36+
var.addedWound = nil;
37+
var.addedWoundOffset = nil;
38+
var.addedWoundToUniqueID = nil;
39+
var.Vel = self.Vel;
1740

18-
function self.emitSmoke(particleCount)
19-
for i = 1, particleCount do
20-
local smoke = CreateMOSParticle("Tiny Smoke Ball 1" .. (math.random() < 0.5 and " Glow Blue" or ""), "Base.rte");
21-
smoke.Pos = self.MuzzlePos;
22-
smoke.Lifetime = smoke.Lifetime * RangeRand(0.5, 1.0);
23-
smoke.Vel = self.Vel * 0.5 + Vector(RangeRand(0, i), 0):RadRotate(RangeRand(-math.pi, math.pi));
24-
table.insert(self.addedParticles, smoke);
25-
end
26-
self:RequestSyncedUpdate();
27-
end
41+
self.var = var;
2842
end
2943

3044
function ThreadedUpdate(self)
45+
local var = self.var;
3146
if self.FiredFrame then
3247
local actor = self:GetRootParent();
33-
local range = self.range + math.random(8);
48+
local range = var.range + math.random(8);
3449
if IsActor(actor) then
3550
actor = ToActor(actor);
3651
range = range + actor.AimDistance;
@@ -52,12 +67,12 @@ function ThreadedUpdate(self)
5267
skipPx = 1;
5368
local shortRay = SceneMan:CastObstacleRay(gapPos, Vector(trace.X, trace.Y):SetMagnitude(range - rayLength + skipPx), hitPos, gapPos, actor.ID, self.Team, rte.airID, skipPx);
5469
gapPos = gapPos - Vector(trace.X, trace.Y):SetMagnitude(skipPx);
55-
local strengthFactor = math.max(1 - rayLength/self.range, math.random()) * (self.shotCounter + 1)/self.strengthVariation;
70+
local strengthFactor = math.max(1 - rayLength/var.range, math.random()) * (var.shotCounter + 1)/var.strengthVariation;
5671

57-
self.addedWoundToMOID = SceneMan:GetMOIDPixel(hitPos.X, hitPos.Y);
58-
if self.addedWoundToMOID ~= rte.NoMOID and self.addedWoundToMOID ~= self.ID then
59-
local mo = ToMOSRotating(MovableMan:GetMOFromID(self.addedWoundToMOID));
60-
if self.penetrationStrength * strengthFactor >= mo.Material.StructuralIntegrity then
72+
local addedWoundToMOID = SceneMan:GetMOIDPixel(hitPos.X, hitPos.Y);
73+
if addedWoundToMOID ~= rte.NoMOID and addedWoundToMOID ~= self.ID then
74+
local mo = ToMOSRotating(MovableMan:GetMOFromID(addedWoundToMOID));
75+
if var.penetrationStrength * strengthFactor >= mo.Material.StructuralIntegrity then
6176
local moAngle = -mo.RotAngle * mo.FlipFactor;
6277

6378
local woundName = mo:GetEntryWoundPresetName();
@@ -68,9 +83,9 @@ function ThreadedUpdate(self)
6883
local woundOffset = Vector(dist.X * mo.FlipFactor, dist.Y):RadRotate(moAngle):SetMagnitude(dist.Magnitude - (wound.Radius - 1) * wound.Scale);
6984
wound.InheritedRotAngleOffset = woundOffset.AbsRadAngle;
7085
woundOffset = woundOffset:RadRotate(-mo.RotAngle);
71-
self.addedWound = wound;
72-
self.addedWoundOffset = woundOffset;
73-
self.addedWoundToUniqueID = mo.UniqueID;
86+
var.addedWound = wound;
87+
var.addedWoundOffset = woundOffset;
88+
var.addedWoundToUniqueID = mo.UniqueID;
7489
self:RequestSyncedUpdate();
7590
end
7691
end
@@ -80,19 +95,19 @@ function ThreadedUpdate(self)
8095
smoke.Pos = gapPos;
8196
smoke.Vel = Vector(-trace.X, -trace.Y):SetMagnitude(math.random(3, 6)):RadRotate(RangeRand(-1.5, 1.5));
8297
smoke.Lifetime = smoke.Lifetime * strengthFactor;
83-
table.insert(self.addedParticles, smoke);
98+
InsertParticle(var, smoke);
8499

85100
local pix = CreateMOPixel("Laser Rifle Glow " .. math.floor(strengthFactor * 4 + 0.5), "Techion.rte");
86101
pix.Pos = gapPos;
87-
pix.Sharpness = self.penetrationStrength/6;
102+
pix.Sharpness = var.penetrationStrength/6;
88103
pix.Vel = Vector(trace.X, trace.Y):SetMagnitude(6);
89-
table.insert(self.addedParticles, pix);
104+
InsertParticle(var, pix);
90105
end
91106
if rayLength ~= 0 then
92107
trace = SceneMan:ShortestDistance(startPos, gapPos, SceneMan.SceneWrapsX);
93108
for player = Activity.PLAYER_1, Activity.MAXPLAYERCOUNT - 1 do
94-
local team = self.activity:GetTeamOfPlayer(player);
95-
local screen = self.activity:ScreenOfPlayer(player);
109+
local team = var.activity:GetTeamOfPlayer(player);
110+
local screen = var.activity:ScreenOfPlayer(player);
96111
if screen ~= -1 and not (SceneMan:IsUnseen(startPos.X, startPos.Y, team) or SceneMan:IsUnseen(hitPos.X, hitPos.Y, team)) then
97112
PrimitiveMan:DrawLinePrimitive(screen, startPos, startPos + trace, 254);
98113
end
@@ -101,48 +116,55 @@ function ThreadedUpdate(self)
101116
for i = 0, particleCount do
102117
local pix = CreateMOPixel("Laser Rifle Glow 0", "Techion.rte");
103118
pix.Pos = startPos + trace * i/particleCount;
104-
pix.Vel = self.Vel;
105-
table.insert(self.addedParticles, pix);
119+
pix.Vel = var.Vel;
120+
InsertParticle(var, pix);
106121
end
107122
end
108-
self.shotCounter = (self.shotCounter + 1) % self.strengthVariation;
109-
self.cooldown:Reset();
123+
var.shotCounter = (var.shotCounter + 1) % var.strengthVariation;
124+
var.cooldown:Reset();
110125
end
111126

112127
if self.Magazine and self.Magazine.RoundCount > 0 then
113128
local ammoRatio = 1 - self.Magazine.RoundCount/self.Magazine.Capacity;
114-
self.emitSmoke(math.floor(ammoRatio * RangeRand(0.5, 2.0) + RangeRand(0.25, 0.50)));
115-
self.cooldown:SetSimTimeLimitMS(self.ReloadTime * ammoRatio);
129+
emitSmoke(self, math.floor(ammoRatio * RangeRand(0.5, 2.0) + RangeRand(0.25, 0.50)));
130+
var.cooldown:SetSimTimeLimitMS(self.ReloadTime * ammoRatio);
116131

117-
local cooldownRate = math.floor(self.cooldown.ElapsedSimTimeMS/(60000/(self.RateOfFire * self.cooldownSpeed)));
132+
local cooldownRate = math.floor(var.cooldown.ElapsedSimTimeMS/(60000/(self.RateOfFire * var.cooldownSpeed)));
118133
if ammoRatio ~= 0 and cooldownRate >= 1 then
119134
self.Magazine.RoundCount = math.min(self.Magazine.RoundCount + cooldownRate, self.Magazine.Capacity);
120-
self.cooldown:Reset();
135+
var.cooldown:Reset();
121136
end
122137
self.FireSound.Pitch = (1.0 - ammoRatio * 0.1)^2;
123138
elseif self:IsReloading() then
124-
self.emitSmoke(math.floor((self.cooldown:LeftTillSimTimeLimitMS()/self.ReloadTime) * RangeRand(0.5, 2.0) + RangeRand(0.50, 0.75)));
139+
emitSmoke(self, math.floor((var.cooldown:LeftTillSimTimeLimitMS()/self.ReloadTime) * RangeRand(0.5, 2.0) + RangeRand(0.50, 0.75)));
125140
elseif self.RoundInMagCount >= 0 then
126141
self:Reload();
127142
end
128143
end
129144

145+
-- Localize function to improve performance
146+
local AddParticle = MovableMan.AddParticle;
147+
130148
function SyncedUpdate(self)
131-
if self.addedWound then
132-
local mo = MovableMan:FindObjectByUniqueID(self.addedWoundToUniqueID);
149+
local var = self.var;
150+
if var.addedWound then
151+
local mo = MovableMan:FindObjectByUniqueID(var.addedWoundToUniqueID);
133152
if mo then
134153
mo = ToMOSRotating(mo);
135-
mo:AddWound(self.addedWound, self.addedWoundOffset, true);
154+
mo:AddWound(var.addedWound, var.addedWoundOffset, true);
136155
end
137156

138-
self.addedWound = nil;
139-
self.addedWoundOffset = nil;
140-
self.addedWoundToMOID = nil;
141-
self.addedWoundToUniqueID = nil;
157+
var.addedWound = nil;
158+
var.addedWoundOffset = nil;
159+
var.addedWoundToUniqueID = nil;
142160
end
143161

144-
for i = 1, #self.addedParticles do
145-
MovableMan:AddParticle(self.addedParticles[i]);
162+
if (var.addedParticleCount > 0) then
163+
for i = 1, var.addedParticleCount do
164+
-- Remember to add function caller as first argument to localized function
165+
AddParticle(MovableMan, var.addedParticles[i]);
166+
end
167+
var.addedParticles = {};
168+
var.addedParticleCount = 0;
146169
end
147-
self.addedParticles = {};
148170
end

0 commit comments

Comments
 (0)