• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Stopping a bullet

Level 6
Joined
Aug 26, 2016
Messages
109
I can’t figure out what needs to be added to remove the bullet when colliding with a destructible object.

JASS:
 local bullet A = LoadInteger( H, GetHandleId( GetExpiredTimer( ) ), 0 )
    local unit u
   
   
    set A.x = A.x + A.s * A.v.x * Cos( A.v.z )
    set A.y = A.y + A.s * A.v.y * Cos( A.v.z )
    set A.z = A.z + A.s * A.v.z
    
    set A.d = A.d - A.s
    
    call SetUnitX( A.dummy, A.x )
    call SetUnitY( A.dummy, A.y )
    call SetUnitFlyHeight( A.dummy, A.z - GetLocZPS( A.x, A.y ), 0.00 )
    
    call GroupEnumUnitsInRange( TempGroup, A.x, A.y, A.r, null )
    
    loop
        set u = FirstOfGroup( TempGroup )
        exitwhen u == null
        call GroupRemoveUnit( TempGroup, u )
        
         
     
        
        if UnitAlive( u ) and IsUnitEnemy( u, A.p ) and RAbsBJ( GetUnitFlyHeight( u ) - GetUnitFlyHeight( A.dummy ) ) <= 100.00 then
            
            call UnitDamageTarget( A.caster, u, 60.00, false, false, null, null, null )
            
             
        endif
    endloop
    
    if A.d <= 0.00 or A.z - GetLocZPS( A.x, A.y ) <= 20.00 then
        
        call FlushChildHashtable( H, GetHandleId( GetExpiredTimer( ) ) )
        call PauseTimer( GetExpiredTimer( ) )
        call DestroyTimer( GetExpiredTimer( ) )
        
        call KillUnit(A.dummy)
        call RemoveUnit( A.dummy )
        
        set A.dummy = null
        set A.caster = null
        call A.v.destroy( )
        call A.destroy( )
    endif
 

Uncle

Warcraft Moderator
Level 65
Joined
Aug 10, 2018
Messages
6,688
I can’t figure out what needs to be added to remove the bullet when colliding with a destructible object.
You use the same technique that is used to find if your bullet has collided with a unit:
  • Actions
    • Set Variable Found_Destructible = False
    • Destructible - Pick every destructible within 32.00 of (Position of YOUR_BULLET) and do (Actions)
      • Loop - Actions
        • Set Variable Found_Destructible = True
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • Found_Destructible Equal to True
      • Then - Actions
        • -------- Hit a destructible, destroy bullet --------
      • Else - Actions
Create that in GUI and convert it to code and work from there.

I imagine this is better than using the Rect method.
 

Uncle

Warcraft Moderator
Level 65
Joined
Aug 10, 2018
Messages
6,688
That's funny, been using that one for a while now.

@Nikita17
I think something like this would work?
vJASS:
library BulletHelpers initializer Init

    globals
        private rect Bullet_Rect
        private boolean Bullet_Found_Destructible = false
    endglobals

    private function BulletNearDestructibleCallback takes nothing returns nothing
        set Bullet_Found_Destructible = true
    endfunction

    function IsBulletNearDestructible takes real x, real y returns boolean
        set Bullet_Found_Destructible = false
        call MoveRectTo(Bullet_Rect, x, y)
        call EnumDestructablesInRect(Bullet_Rect, null, function BulletNearDestructibleCallback)
        return Bullet_Found_Destructible
    endfunction

    private function Init takes nothing returns nothing
        set Bullet_Rect = Rect(0, 0, 128, 128)
    endfunction

endlibrary
Then you just add this line to your Bullet code:
vJASS:
if A.d <= 0.00 or A.z - GetLocZPS( A.x, A.y ) <= 20.00 or IsBulletNearDestructible(A.x, A.y) then
 
Last edited:
Level 6
Joined
Aug 26, 2016
Messages
109
Doesn't work, tried it like this.
JASS:
library PistolLib

native UnitAlive takes unit id returns boolean
function GetLocZPS takes real x, real y returns real
    call MoveLocation( LFZ, x, y )
    return GetLocationZ( LFZ )
endfunction
 globals
        private rect Bullet_Rect
        private boolean Bullet_Found_Destructible = false
    endglobals

   private function BulletNearDestructibleCallback takes nothing returns nothing
        set Bullet_Found_Destructible = true
    endfunction

    function IsBulletNearDestructible takes real x, real y returns boolean
        set Bullet_Found_Destructible = false
        call MoveRectTo(Bullet_Rect, x, y)
        call EnumDestructablesInRect(Bullet_Rect, null, function BulletNearDestructibleCallback)
        return Bullet_Found_Destructible
    endfunction

    private function Init takes nothing returns nothing
        set Bullet_Rect = Rect(0, 0, 128, 128)
    endfunction

function BulletkillKill takes nothing returns nothing
local bullet A = LoadInteger( H, GetHandleId( GetExpiredTimer( ) ), 0 )
    local unit u
   
    loop
    call FlushChildHashtable( H, GetHandleId( GetExpiredTimer( ) ) )
        call PauseTimer( GetExpiredTimer( ) )
        call DestroyTimer( GetExpiredTimer( ) )
        
        call KillUnit(A.dummy)
        call RemoveUnit( A.dummy )
        
        set A.dummy = null
        set A.caster = null
        call A.v.destroy( )
        call A.destroy( )
    endloop
    
endfunction




function MoveP takes nothing returns nothing
   local bullet A = LoadInteger( H, GetHandleId( GetExpiredTimer( ) ), 0 )
    local unit u
    
   
    set A.x = A.x + A.s * A.v.x * Cos( A.v.z )
    set A.y = A.y + A.s * A.v.y * Cos( A.v.z )
    set A.z = A.z + A.s * A.v.z
    
    set A.d = A.d - A.s
    
    call SetUnitX( A.dummy, A.x )
    call SetUnitY( A.dummy, A.y )
    call SetUnitFlyHeight( A.dummy, A.z - GetLocZPS( A.x, A.y ), 0.00 )
    
    call GroupEnumUnitsInRange( TempGroup, A.x, A.y, A.r, null )
    
    
    loop
        set u = FirstOfGroup( TempGroup )
        exitwhen u == null
        call GroupRemoveUnit( TempGroup, u )
        
       
     
        
        if UnitAlive( u ) and IsUnitEnemy( u, A.p ) and RAbsBJ( GetUnitFlyHeight( u ) - GetUnitFlyHeight( A.dummy ) ) <= 100.00 then
            
            call UnitDamageTarget( A.caster, u, 60.00, false, false, null, null, null )
            
             
        endif
    endloop
    
    if A.d <= 0.00 or A.z - GetLocZPS( A.x, A.y ) <= 20.00 or IsBulletNearDestructible(A.x, A.y) then
        
        call FlushChildHashtable( H, GetHandleId( GetExpiredTimer( ) ) )
        call PauseTimer( GetExpiredTimer( ) )
        call DestroyTimer( GetExpiredTimer( ) )
        
        call KillUnit(A.dummy)
        call RemoveUnit( A.dummy )
        
        set A.dummy = null
        set A.caster = null
        call A.v.destroy( )
        call A.destroy( )
    endif
endfunction

function Trig_Shoot2Ak47_Copy_Copy_Copy_Actions takes nothing returns nothing
    local timer t6 = CreateTimer( )
    local unit Target = udg_Camera[GetConvertedPlayerId(GetTriggerPlayer())]
    local bullet A = bullet.create( )
    local real x = GetUnitX(Target)
    local real y = GetUnitY(Target)
    local real z = GetLocZPS( x, y ) + 90.00
    local string WeaponEffect = "WeaponsandEffect/Konstrukt_ShotgunEffektAttachment.MDX"
    local string WeaponEffect2 = "WeaponsandEffect/Pistol.mdx"
    set A.caster = udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())]
    set A.p = GetOwningPlayer( A.caster )
    call CameraSetTargetNoiseForPlayer( GetOwningPlayer(udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())]), 17.60, 3.10 )
    // for offset
    set A.x = GetUnitX( A.caster )   
    set A.y = GetUnitY( A.caster ) 
    set A.z = GetUnitFlyHeight( A.caster ) + GetLocZPS( A.x, A.y ) + 90.00 
    
    set A.v = vector.create( x - A.x, y - A.y, z - A.z )
    
    set A.x = A.x + 100.00 * A.v.x * Cos( A.v.z )
    set A.y = A.y + 100.00 * A.v.y * Cos( A.v.z )
    set A.z = GetUnitFlyHeight( A.caster ) + GetLocZPS( A.x, A.y ) + 90.00 
    
    // for move
        set A.v.x = x - A.x 
        set A.v.y = y - A.y 
        set A.v.z = z - A.z  

   
     call A.v.normalize( )
    
    set A.d = 1200.00
    set A.s = 1500.00 * 0.03125
    set A.r = 36.00
    
    
    set A.dummy = CreateUnit( A.p, 'u000', A.x, A.y, Atan2( A.v.y, A.v.x )  * bj_RADTODEG  )
    call SetUnitPathing( A.dummy, false )
    call UnitAddAbility( A.dummy, 'Arav' )
    call SetUnitX( A.dummy, A.x )
    call SetUnitY( A.dummy, A.y )
    call SetUnitFlyHeight( A.dummy, A.z - GetLocZPS( A.x, A.y ), 0.00 )
   
    call SaveInteger( H, GetHandleId( t6 ), 0, A )
    call TimerStart( t6, 0.01, true, function MoveP )
    call DestroyEffect( AddSpecialEffectTarget( WeaponEffect, A.caster, "weapon" ) )
    call DestroyEffect( AddSpecialEffectTarget( WeaponEffect2, A.caster, "chest" ) )
    call SetItemCharges( GetItemOfTypeFromUnitBJ(udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())], 'I00G'), ( GetItemCharges(GetItemOfTypeFromUnitBJ(udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())], 'I00G')) - 1 ) )
    call IssueImmediateOrderBJ( udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())], "stop" )
    set t6 = null
    set Target = null
    
    call IssueImmediateOrderBJ( udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())], "stop" )
endfunction
 
//===========================================================================
function ShootP_Conditions takes nothing returns boolean
    if ( not ( GetSpellAbilityId() == 'A012' ) ) then
        return false
    endif
    if ( not ( GetItemCharges(GetItemOfTypeFromUnitBJ(udg_UnitBody[GetConvertedPlayerId(GetTriggerPlayer())], 'I00G')) != 0 ) ) then
        return false
    endif
    return true
endfunction

function InitTrig_Shoot2Ak47_Copy_Copy_Copy takes nothing returns nothing
    local trigger t6 = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( t6, EVENT_PLAYER_UNIT_SPELL_CAST )
    call TriggerAddCondition( t6, Condition( function ShootP_Conditions ) )
    call TriggerAddAction( t6, function Trig_Shoot2Ak47_Copy_Copy_Copy_Actions )
    set t6 = null
endfunction
endlibrary
 
Top