Skip to content

Commit 6411223

Browse files
authored
Optimize sector_fire custom weapons behavior gadget entry (#4232)
There's leftover expensive code from when the tremor had its firestate toggle removed. The GDT wants these buttons gone, so it's a safe bet it isn't coming back. This is a partial code cleanup and bigtime optimization that cuts performance cpu load in half and eliminates almost all memory usage.. I don't have time for a full refactor/cleanup. This makes it performant enough to where IMO it can be used for shotguns and machine guns where spread is desired over cone random accuracy
1 parent 46c1460 commit 6411223

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

luarules/gadgets/unit_custom_weapons_behaviours.lua

+24-29
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ local SpSetProjectileTarget = Spring.SetProjectileTarget
1717

1818
local SpGetProjectileVelocity = Spring.GetProjectileVelocity
1919
local SpGetProjectileOwnerID = Spring.GetProjectileOwnerID
20-
local SpGetUnitStates = Spring.GetUnitStates
2120
local SpGetProjectileTimeToLive = Spring.GetProjectileTimeToLive
2221
local SpGetUnitWeaponTarget = Spring.GetUnitWeaponTarget
2322
local SpGetProjectileTarget = Spring.GetProjectileTarget
@@ -30,6 +29,9 @@ if gadgetHandler:IsSyncedCode() then
3029
local checkingFunctions = {}
3130
local applyingFunctions = {}
3231
local math_sqrt = math.sqrt
32+
local mathCos = math.cos
33+
local mathSin = math.sin
34+
local mathPi = math.pi
3335

3436
local specialWeaponCustomDefs = {}
3537
local weaponDefNamesID = {}
@@ -106,33 +108,27 @@ if gadgetHandler:IsSyncedCode() then
106108
end
107109

108110
applyingFunctions.sector_fire = function (proID)
109-
local ownerID = SpGetProjectileOwnerID(proID)
110-
local ownerState = SpGetUnitStates(ownerID)
111-
--if ownerState.active == true then
112111
local infos = projectiles[proID]
113-
--x' = x cos θ − y sin θ
114-
--y' = x sin θ + y cos θ
115112
local vx, vy, vz = SpGetProjectileVelocity(proID)
116-
117-
angle_factor = tonumber(infos.spread_angle)*random()-tonumber(infos.spread_angle)*0.5
118-
angle_factor = angle_factor*math.pi/180
119-
vx_new = vx*math.cos(angle_factor) - vz*math.sin(angle_factor)
120-
vz_new = vx*math.sin(angle_factor) + vz*math.cos(angle_factor)
121-
122-
--vx_new = vx
123-
--vz_new = vz
124-
--velocity_reduction = 1-math.sqrt(1-tonumber(infos.max_range_reduction))
125-
--velocity_floor = (1-velocity_reduction)^2
126-
--velocity_factor = random()*(1-velocity_floor)
127-
--velocity_factor = math.sqrt(velocity_floor+velocity_factor)
128-
velocity_factor = 1-(random()) ^(1+tonumber(infos.max_range_reduction))*tonumber(infos.max_range_reduction)
129-
vx = vx_new*velocity_factor
130-
--vy = vy*velocity_factor
131-
vz = vz_new*velocity_factor
132-
133-
SpSetProjectileVelocity(proID,vx,vy,vz)
134-
--end
135-
end
113+
114+
local spread_angle = tonumber(infos.spread_angle)
115+
local max_range_reduction = tonumber(infos.max_range_reduction)
116+
117+
local angle_factor = (spread_angle * (random() - 0.5)) * mathPi / 180
118+
local cos_angle = mathCos(angle_factor)
119+
local sin_angle = mathSin(angle_factor)
120+
121+
local vx_new = vx * cos_angle - vz * sin_angle
122+
local vz_new = vx * sin_angle + vz * cos_angle
123+
124+
local velocity_factor = 1 - (random() ^ (1 + max_range_reduction)) * max_range_reduction
125+
126+
vx = vx_new * velocity_factor
127+
vz = vz_new * velocity_factor
128+
129+
SpSetProjectileVelocity(proID, vx, vy, vz)
130+
end
131+
136132

137133
checkingFunctions.retarget = {}
138134
checkingFunctions.retarget["always"] = function (proID)
@@ -314,9 +310,8 @@ if gadgetHandler:IsSyncedCode() then
314310
end
315311

316312
function gadget:ProjectileCreated(proID, proOwnerID, weaponDefID)
317-
local wDefID = Spring.GetProjectileDefID(proID)
318-
if specialWeaponCustomDefs[wDefID] then
319-
projectiles[proID] = specialWeaponCustomDefs[wDefID]
313+
if specialWeaponCustomDefs[weaponDefID] then
314+
projectiles[proID] = specialWeaponCustomDefs[weaponDefID]
320315
active_projectiles[proID] = nil
321316
end
322317
end

0 commit comments

Comments
 (0)