Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projectile Dodge #4694

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions LuaRules/Configs/customcmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ return {
TURN = 38530,
WANTED_SPEED = 38825,
AIR_STRAFE = 39381,
IDLE_DODGE = 39382,
MOVE_DODGE = 39383,

-- terraform
RAMP = 39734,
Expand Down
64 changes: 64 additions & 0 deletions LuaRules/Configs/projectile_dodge_defs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local TRACKED_WEAPONS = {
amphfloater_cannon = {},
amphsupport_cannon = {},
bomberheavy_arm_pidr = {},
bomberprec_bombsabot = {},
bomberriot_napalm = {},
cloakarty_hammer_weapon = {},
cloakskirm_bot_rocket = {
dynamic = true,
},
empmissile_emp_weapon = {},
gunshipassault_vtol_salvo = {},
gunshipheavyskirm_emg = {},
hoverarty_ata = {},
hoverskirm_missile = {},
jumparty_napalm_sprayer = {},
jumpblackhole_black_hole = {},
napalmmissile_weapon = {},
seismic_seismic_weapon = {},
shieldassault_thud_weapon = {},
shieldskirm_storm_rocket = {
dynamic = true,
},
shiparty_plasma = {},
shipskirm_rocket = {},
spiderassault_thud_weapon = {},
spidercrabe_arm_crabe_gauss = {},
spiderskirm_adv_rocket = {},
staticarty_plasma = {},
staticheavyarty_plasma = {},
striderarty_rocket = {},
tacnuke_weapon = {
dynamic = true,
},
tankarty_core_artillery = {},
tankassault_cor_reap = {},
tankheavyarty_plasma = {},
tankheavyassault_cor_gol = {},
turretantiheavy_ata = {},
turretgauss_gauss = {},
turretheavy_plasma = {},
turretheavylaser_laser = {},
vehassault_plasma = {},
vehheavyarty_cortruck_rocket = {
dynamic = true,
},
}

local config = {}
for projName, customData in pairs(TRACKED_WEAPONS) do
local wDef = WeaponDefNames[projName]
local wData = {
wType = wDef.type,
tracks = wDef.tracks,
maxVelocity = wDef.maxVelocity,
aoe = math.max(20, (wDef.impactOnly and 0) or wDef.damageAreaOfEffect),
dynamic = wDef.wobble ~= 0 or wDef.dance ~= 0 or wDef.tracks,
}
for key, data in pairs(customData) do
wData[key] = data
end
config[wDef.id] = wData
end
return config
2 changes: 2 additions & 0 deletions LuaRules/Configs/state_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ local stateCommands = {
[CMD_TOGGLE_DRONES] = true,
[CMD_AUTO_CALL_TRANSPORT] = true,
[CMD_SELECTION_RANK] = true,
[CMD_IDLE_DODGE] = true,
[CMD_MOVE_DODGE] = true,
}

return stateCommands
65 changes: 65 additions & 0 deletions LuaRules/Gadgets/cmd_raw_move.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ local COMMON_STOP_RADIUS_ACTIVE_DIST_SQ = 120^2 -- Commands shorter than this do
local CONSTRUCTOR_UPDATE_RATE = 30
local CONSTRUCTOR_TIMEOUT_RATE = 2

local DODGE_UPDATE_RATE = 20

local STOPPING_HAX = not Spring.Utilities.IsCurrentVersionNewerThan(104, 271)

----------------------------------------------------------------------------------------------
Expand All @@ -183,6 +185,10 @@ local constructorsPerFrame = 0
local constructorIndex = 1
local alreadyResetConstructors = false

local dodgeMoveUnit = {}
local dodgeMoveUnitByID = {}
local dodgeMoveUnitCount = 0

local checkEngineMove
local moveCommandReplacementUnits
local fastConstructorUpdate
Expand Down Expand Up @@ -258,11 +264,61 @@ local function ResetUnitData(unitData)
unitData.possiblyTurning = nil
end

----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
-- Dodge Move Handling

local function AddProjectileDodge(unitID)
if unitID and dodgeMoveUnitByID[unitID] == nil then
dodgeMoveUnitCount = dodgeMoveUnitCount + 1
dodgeMoveUnit[dodgeMoveUnitCount] = unitID
dodgeMoveUnitByID[unitID] = dodgeMoveUnitCount
end
end

local function RemoveProjectileDodge(unitID)
if unitID and dodgeMoveUnitByID[unitID] then
local index = dodgeMoveUnitByID[unitID]
dodgeMoveUnit[index] = dodgeMoveUnit[dodgeMoveUnitCount]
dodgeMoveUnitByID[dodgeMoveUnit[dodgeMoveUnitCount]] = index
dodgeMoveUnitByID[unitID] = nil
dodgeMoveUnit[dodgeMoveUnitCount] = nil
dodgeMoveUnitCount = dodgeMoveUnitCount - 1
end
end

local function UnitProjectileDodge(unitID)
if rawMoveUnit[unitID] and dodgeMoveUnitByID[unitID] then
local unitData = rawMoveUnit[unitID]
local dodgeX, dodgeZ, dodgeSize = GG.UnitDodge.Move(unitID, unitData.mx, unitData.mz, DODGE_UPDATE_RATE)
if dodgeSize > 5 then
local cmdID, _, cmdTag, _, _, _, isDodge = spGetUnitCurrentCommand(unitID)
if (cmdID == CMD_MOVE or cmdID == CMD_RAW_MOVE) and isDodge == -1 then
spGiveOrderToUnit(unitID, CMD_REMOVE, cmdTag, 0)
end
local dodgeY = Spring.GetGroundHeight(dodgeX, dodgeZ)
spGiveOrderToUnit(unitID, CMD_INSERT, {0, CMD_RAW_MOVE, 0, dodgeX, dodgeY, dodgeZ, -1}, CMD_OPT_ALT)
end
else
RemoveProjectileDodge(unitID)
end
end

local function UpdateProjectileDodge(frame)
if frame % DODGE_UPDATE_RATE == 0 then
for i = dodgeMoveUnitCount, 1, -1 do
UnitProjectileDodge(dodgeMoveUnit[i])
end
end
end

----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
-- Raw Move Handling

local function StopRawMoveUnit(unitID, stopNonRaw)
RemoveProjectileDodge(unitID)

if not rawMoveUnit[unitID] then
return
end
Expand Down Expand Up @@ -296,6 +352,10 @@ local function HandleRawMove(unitID, unitDefID, cmdParams)
end

local goalDistOverride = cmdParams[4]
if cmdParams[4] == -1 then
goalDistOverride = nil
end

local timerIncrement = cmdParams[5] or 1
if not rawMoveUnit[unitID] then
rawMoveUnit[unitID] = {}
Expand Down Expand Up @@ -459,6 +519,9 @@ function gadget:CommandFallback(unitID, unitDefID, teamID, cmdID, cmdParams, cmd
return false
end
local cmdUsed, cmdRemove = HandleRawMove(unitID, unitDefID, cmdParams)
if cmdUsed and not cmdRemove and cmdParams[4] ~= -2 then
AddProjectileDodge(unitID)
end
return cmdUsed, cmdRemove
end

Expand Down Expand Up @@ -800,6 +863,7 @@ end
function gadget:UnitDestroyed(unitID, unitDefID, teamID)
if unitID then
rawMoveUnit[unitID] = nil
RemoveProjectileDodge(unitID)
if unitDefID and constructorBuildDistDefs[unitDefID] and constructorByID[unitID] then
RemoveConstructor(unitID)
end
Expand All @@ -817,6 +881,7 @@ function gadget:GameFrame(n)
UpdateConstructors(n)
UpdateMoveReplacement()
UpdateEngineMoveCheck(n)
UpdateProjectileDodge(n)
if n%247 == 4 then
oldCommandStoppingRadius = commonStopRadius
commonStopRadius = {}
Expand Down
115 changes: 115 additions & 0 deletions LuaRules/Gadgets/map_obsticles.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
function gadget:GetInfo()
return {
name = "Map Obsticles",
desc = "tracks map obsticles",
author = "petturtle",
date = "2021",
layer = 0,
enabled = true
}
end

local DEBUG = false
local MAP_WIDTH = Game.mapSizeX
local MAP_HEIGHT = Game.mapSizeZ

if gadgetHandler:IsSyncedCode() then

local TTYPE_U = string.byte("u") -- unit
local TTYPE_G = string.byte("g") -- ground
local TTYPE_F = string.byte("f") -- feature
local TTYPE_P = string.byte('p') -- projectile

local QuadTree = VFS.Include("LuaRules/Utilities/quad_tree.lua")
local Config = VFS.Include("LuaRules/Configs/projectile_dodge_defs.lua")

local spSetWatchWeapon = Script.SetWatchWeapon
local spGetUnitPosition = Spring.GetUnitPosition
local spGetFeaturePosition = Spring.GetFeaturePosition
local spGetProjectileDefID = Spring.GetProjectileDefID
local spGetProjectileTarget = Spring.GetProjectileTarget
local spGetProjectilePosition = Spring.GetProjectilePosition

local function GetProjectileGroundTarget(tArgs)
return tArgs[1], tArgs[2], tArgs[3]
end

local ProjTTypeToPos = {
[TTYPE_U] = spGetUnitPosition,
[TTYPE_G] = GetProjectileGroundTarget,
[TTYPE_F] = spGetFeaturePosition,
[TTYPE_P] = spGetProjectilePosition,
}

local layers = {
projectiles = {
color = {1, 0, 0, 1},
obsticles = {},
quad_tree = QuadTree.New(0, 0, MAP_WIDTH, MAP_HEIGHT, 4, 4),
},
}

function gadget:ProjectileCreated(projID)
local projDefID = spGetProjectileDefID(projID)
if projDefID and Config[projDefID] then
local tType, tArgs = spGetProjectileTarget(projID)
local x, y, z = ProjTTypeToPos[tType](tArgs)
layers.projectiles.obsticles[projID] = {{x, z}, y, projID, projDefID}
layers.projectiles.quad_tree:Insert(x, z, projID)
end
end

function gadget:ProjectileDestroyed(projID)
if layers.projectiles.obsticles[projID] then
local target = layers.projectiles.obsticles[projID][1]
layers.projectiles.quad_tree:Remove(target[1], target[2], projID)
layers.projectiles.obsticles[projID] = nil
end
end

-- obsticle
-- [1] = target
-- [2] = target y
-- [3] = projID
-- [4] = projDefID

local function Query(x, z, radius, layer_names)
local results = {}
local layer, layer_results
for _, layer_name in pairs(layer_names) do
layer = layers[layer_name]
layer_results = layer.quad_tree:Query(x, z, radius)
for _, obsticle_id in pairs(layer_results) do
results[#results+1] = layer.obsticles[obsticle_id]
end
end
return results
end

function gadget:Initialize()
for projDefID, _ in pairs(Config) do
spSetWatchWeapon(projDefID, true)
end

_G.layers = layers
GG.MapObsticles = Query
end

elseif DEBUG then -- ----- Unsynced Debug -----
local SYNCED = SYNCED
local glColor = gl.Color
local glDepthTest = gl.DepthTest
local glDrawGroundCircle = gl.DrawGroundCircle

function gadget:DrawWorld()
glDepthTest(true)
for _, layer in pairs(SYNCED.layers) do
glColor(layer.color)
for _, obsticle in pairs(layer.obsticles) do
glDrawGroundCircle(obsticle[1][1], obsticle[2], obsticle[1][2], 32, 12)
end
end
glDepthTest(false)
glColor({1,1,1,1})
end
end
Loading