• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] Missile

Level 19
Joined
Mar 18, 2012
Messages
1,716
Missile

A library made to handle, optimize and ease projectile creation in your map.



Core - Missile snippet​
Tutorial - How to use Missile​
Miscellaneous​



  • Demo code
  • Disclaimer
  • Development
  • [self=http://www.hiveworkshop.com/forums/jass-resources-412/missile-265370/#post2681196]Changelog[/self]
  • [self=http://www.hiveworkshop.com/forums/jass-resources-412/missile-265370/index10.html#post2776698]Stress test[/self]

library Missile

Read and modify the configuration block within the library very carefully, otherwise it's not garantueed that
Missile is working properly. It should be mentioned that MissileRecycler is the optimal dummy recycler for Missile.
Copy 'n' paste the following code into your map:
JASS:
library Missile /* version 2.5.1
*************************************************************************************
*
*   Creating custom projectiles in Warcraft III.
*   
*   Major goal: 
*       No unessary external requirements. 
*       Implements code optional.
*   
*   Philosophy:
*       I want that feature --> Compiler writes that code into your map script.
*       I don't want that   --> Compiler ignores that code completly.
*
*   Important:
*       Take yourself 2 minutes time to setup Missile correctly. 
*       Otherwise I can't guarantee, that Missile works the way you want.
*       Once the setup is done, you can check out some examples and Missile will be easy
*       to use for everyone. I promise it. 
*
*       Do the setup at: 
*
*           1.) Import instruction  
*           2.) Global configuration
*           3.) Function configuration       
*   
*   Credits to Dirac, emjlr3, AceHart, Bribe, Wietlol, 
*              Nestharus, Maghteridon96, Vexorian and Zwiebelchen.
*
*************************************************************************************
*
*   */ requires /*
*
*       - Missile requires nothing.
*
*************************************************************************************
*
*   Optional requirements listed can reduce overall code generation,
*   add safety mechanisms, decrease overhead and optimize handle management.  
*   For a better overview I put them into blocks.
*
*   I recommend to use at least one per block in your map.
*       
*   a) For best debug results: ( Useful )
*       */ optional ErrorMessage    /* github.com/nestharus/JASS/tree/master/jass/Systems/ErrorMessage  
*   
*   b) Fatal error protection: ( Case: unit out moves of world bounds )   
*           - WorldBounds is safer than BoundSentinel.
*           - WorldBounds adds more overhead than BoundSentinel.
*           - Using none of these two forces Missile to switch from SetUnitX/Y to the SetUnitPosition native.
*       */ optional WorldBounds     /* githubusercontent.com/nestharus/JASS/master/jass/Systems/WorldBounds/script.j
*       */ optional BoundSentinel   /* wc3c.net/showthread.php?t=102576
*       
*   c) Handle recycling: ( Performace gain, memory management )
*           - uses MissileRecylcer > Dummy > xedummy. 
*       */ optional MissileRecycler /* hiveworkshop.com/forums/jass-resources-412/system-missilerecycler-206086/
*       */ optional Dummy           /* github.com/nestharus/JASS/blob/master/jass/Systems/Dummy/Dummy.w3x
*       */ optional xedummy         /* wc3c.net/showthread.php?t=101150
*
*   d) Unit indexing: ( Avoid an onIndex event )
*           - not required for Missile. Only if you use one already.
*       */ optional UnitIndexer     /* github.com/nestharus/JASS/tree/master/jass/Systems/Unit%20Indexer
*
************************************************************************************
*
*   1. Import instruction
*   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*       • Copy Missile into to your map. 
*       • You need a dummy unit, using Vexorians "dummy.mdx".
*         This unit must use the locust and crow form ability. ( Aloc & Amrf )
*                   ¯¯¯¯
*
*   2. Global configuration   
*   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*       Seven constants to setup!
*/
    globals
    
        /**
        *   Missiles are moved periodically. An interval of 1./32. is recommended.
        *       • Too short timeout intervals may cause performance issues.
        *       • Too large timeout intervals may look fishy.
        */
        public constant real TIMER_TIMEOUT                        = 1./32.
        
        /**
        *   Owner of all Missile dummies. Should be a neutral player in your map.
        */ 
        public constant player NEUTRAL_PASSIVE                    = Player(15)
        
        /**
        *   Raw code of the dummy unit. Object Editor ( F6 )
        *       • Must be correct, otherwise missile dummies can neither be recycled nor destroyed.
        *       • Units of other type ids will not be thrown into the recycler bin.
        */
        public constant integer DUMMY_UNIT_ID                     = 'dumi'
    
        /**
        *   The maximum collision size used in your map. If unsure use 197. ( Town hall collision )
        *       • Applies for all types of widgets.
        *       • A precise value can improve Missile's performance,
        *         since smaller values enumerate less widgtes per loop per missile.
        */
        public constant real MAXIMUM_COLLISION_SIZE               = 197.
        
        /**
        *   Collision types for missiles. ( Documentation only )
        *   Missile decides internally each loop which type of collision is required.
        *       • Uses circular collision dectection for speed < collision. ( Good accuracy, best performance )
        *       • Uses rectangle collision for speed >= collision. ( Best accuracy, normal performance )
        */
        public constant integer COLLISION_TYPE_CIRCLE     = 0 
        public constant integer COLLISION_TYPE_RECTANGLE  = 1 
    
        /**
        *   Determine when rectangle collision is required to detect nearby widgets. 
        *       • The smaller the factor the earlier rectangle collision is used. ( by default 1. ) 
        *       • Formula: speed >= collision*Missile_COLLISION_ACCURACY_FACTOR
        */
        public constant real    COLLISION_ACCURACY_FACTOR = 1. 
    
// Optional toogles:
// ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
//      Set booleans listed below to "true" and the compiler will write 
//      the feature into your map. Otherwise this code is completly ignored.                                        
//          • Yay, I want that           --> "true"
//          • Naah that's useless for me --> "false"
        
        /**
        *   USE_COLLISION_Z_FILTER enables z axis checks for widget collision. ( Adds minimal overhead ) 
        *   Use it when you need:
        *       • Missiles flying over or under widgets.
        *       • Determine between flying and walking units.
        */
        public constant boolean USE_COLLISION_Z_FILTER             = true
        
        /**
        *   WRITE_DELAYED_MISSILE_RECYCLING enables a delayed dummy recycling system. ( Very recommended )
        *   Use it if:
        *       • You use a dummy recycling library like MissileRecycler, Dummy or xedummy.
        *       • You want to properly display death animations of effects added to missiles.
        */
        public constant boolean WRITE_DELAYED_MISSILE_RECYCLING    = true
        
        /**
        *   DELAYED_MISSILE_DEATH_ANIMATION_TIME is the delay in seconds
        *   Missile holds back a dummy, before recycling it.
        *       • The time value does not have to be precise.
        *       • Requires WRITE_DELAYED_MISSILE_RECYCLING = true 
        */
        private constant real DELAYED_MISSILE_DEATH_ANIMATION_TIME = 2.

        /**
        *   USE_DESTRUCTABLE_FILTER and USE_ITEM_FILTER are redundant constants from previous Missile versions.
        *   They do nothing, but remain for backwards compatibilty.
        *   From Missile version 1.5 on all widget collisions are always enabled.
        */
        public constant boolean USE_DESTRUCTABLE_FILTER            = true
        public constant boolean USE_ITEM_FILTER                    = true
    endglobals
/*    
*  3. Function configuration   
*  ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*       Four functions to setup!
*/
    /**
    *   GetUnitBodySize(unit) returns a fictional value for z - axis collision.
    *   You have two options:
    *       • One constant value shared over all units. 
    *       • Dynamic values based on handle id, type id, unit user data, scaling or other parameters.
    */
    function GetUnitBodySize takes unit whichUnit returns real
        return 100.// Other example: return LoadReal(hash, GetHandleId(whichUnit), KEY_UNIT_BODY_SIZE)
    endfunction

    /**
    *   Same as GetUnitBodySize, but for destructables.
    *   Using occluder height is an idea of mine. Of course you can use your own values.
    */
    function GetDestructableHeight takes destructable d returns real
        return GetDestructableOccluderHeight(d)// Other example: return 100.
    endfunction

    /**
    *   Same as GetUnitBodySize, but for items.
    *   Again it's up to you to figure out a fictional item height.
    */
    function GetItemHeight takes item i returns real
        return 16.
    endfunction
    
    /**
    *   Unit indexers and missiles ( Only if you don't use a dummy recycling library )
    *   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
    *   It is most likely intended that projectiles don't run through a unit indexing process.
    *   ToogleUnitIndexer runs:
    *       • Directly before a dummy is created.
    *       • Directly after dummy unit creation.
    *
    *   Please return the previous setup of your indexing tool ( enabled, disabled ),
    *   so Missile can properly reset it to the original state.
    */
    private function ToogleUnitIndexer takes boolean enable returns boolean
        local boolean prev = true//UnitIndexer.enabled
        // set UnitIndexer.enabled = enable
        return prev
    endfunction
    
/**  
*   4. API
*   ¯¯¯¯¯¯
*/
//! novjass ( Disables the compiler until the next endnovjass )

        // Custom type Missile for your projectile needs.
        struct Missile extends array

        // Constants:
        // ==========
            //
            readonly static constant string ORIGIN = "origin"
            //  • Attach point name for fxs on dummies.

            readonly static constant real   HIT_BOX = (2./3.)
            //  • Fictional hit box for homing missiles.
            //    while 0 would be the toe and 1 the head of a unit.
            
        // Available creators:
        // ===================
            // 
            static method createEx takes unit missileDummy, real impactX, real impactY, real impactZ returns Missile
            //  • Core creator method.
            //  • May launches any unit. 
            //  • Units of type Missile_DUMMY_UNIT_ID get recycled in the end.
            
            static method create takes real x, real y, real z, real angleInRadians, real distanceToTravel, real endZ returns Missile
            //  • Converts arguments to fit into createEx, then calls createEx.

            static method createXYZ takes real x, real y, real z, real impactX, real impactY, real impactZ returns Missile
            //  • Converts arguments to fit into createEx, then calls createEx. 
            
        // Available destructors:
        // ======================
            //
            return true
            //  • Core destructor.
            //  • Returning true in any of the interface methods of the MissileStruct module
            //    will destroy that instance instantly.
            
            method destroy   takes nothing returns nothing
            //  • Destroys the missile during the next timer callback. 
            
            method terminate takes nothing returns nothing
            //  • Destroys the missile instantly. 
            
        // Fields you can set and read directly:
        // =====================================
            //
            unit       source
            unit       target      // For homing missiles.
            real       distance    // Distance traveled.
            player     owner       // Pseudo owner for faster onCollide evaluation. The proper dummy owner remains PLAYER_NEUTRAL_PASSIVE.
            real       speed       // Vector lenght for missile movement in plane x / y. ( DOES NOT TAKE THE TIMER TIMEOUT IN ACCOUNT )
            real       acceleration
            real       damage
            real       turn        // Set a turn rate for missiles.
            integer    data        // For data transfer set and read data.  
            boolean    recycle     // Is automatically set to true, when a Missile reaches it's destination.
            boolean    wantDestroy // Set wantDestroy to true, to destroy a missile during the next timer callback.
            
            // Neither collision nor collisionZ accept values below zero.
            real       collision   // Collision size in plane x / y.
            real       collisionZ  // Collision size in z - axis. ( deprecated )
            
        // Fields you can only read:
        // =========================      
            //
            readonly boolean         allocated        
            readonly unit            dummy// The dummy unit of this missile.
        
            // Position members for you needs.
            readonly MissilePosition origin// Grants access to readonly members of MissilePosition,
            readonly MissilePosition impact// which are "x", "y", "z", "angle", "distance", "slope" and the pitch angle "alpha". 
                                           // Furthermore method origin.move(x, y, z) and impact.move(x, y, z).
            readonly real            terrainZ
            readonly real            x
            readonly real            y
            readonly real            z
            readonly real            angle// Current angle in radians.

        // Method operators for set and read:
        // ==================================
            //
            method operator model= takes string modelFile returns nothing
            method operator model  takes nothing returns string
            //  • Adds an effect handle on a missile dummy to it's "origin".
            //  • You can read the file path.
            //  • For multiple effects access "this.dummy" in your struct.
             
            method operator scale= takes real value returns nothing
            method operator scale  takes nothing returns real
            //  • Set and read the scaling of the dummy unit.  
            
            method operator curve= takes real value returns nothing
            method operator curve  takes nothing returns real
            //  • Enables curved movement for your missile. ( Pass in radians, NOT degrees )
            //  • Do not pass in PI/2.
       
            method operator arc=   takes real value returns nothing
            method operator arc    takes nothing returns real
            //  • Enables arcing movement for your missile. ( Pass in radians, NOT degrees )
            //  • Do not pass in PI/2.
                        
        // Methods for missile movement:
        // =============================
            //
            method bounce           takes nothing returns nothing
            //  • Moves the MissilePosition "origin" to the current missile coordinates.
            //  • Resets the distance traveled to 0.
            
            method deflect          takes real tx, real ty returns nothing
            //  • Deflect the missile towards tx, ty. Then calls bounce.        
            
            method deflectEx        takes real tx, real ty, real tz returns nothing
            //  • Deflect the missile towards tx, ty, tz. Then calls bounce.
    
            method flightTime2Speed takes real duration returns nothing
            //  • Converts a fly time to a vector lenght for member "speed".
            //  • Does not take acceleration into account. ( Disclaimer )
        
            method setMovementSpeed takes real value returns nothing
            //  • Converts Warcraft III movement speed to a vector lenght for member "speed".
       
        // Methods for missile collision: ( all are hashtable entries )
        // ==============================
            // By default a widget can only be hit once per missile.
            //
            method hitWidget        takes widget w returns nothing
            //  • Saves a widget in the memory as hit by this instance.
        
            method hasHitWidget     takes widget w returns boolean
            //  • Returns true, if "w" has been hit by this instance.
        
            method removeHitWidget  takes widget w returns nothing
            //  • Removes a widget from this missile's memory for widget collision. ( Can hit "w" again )
            
            method flushHitWidgets  takes nothing returns nothing
            //  • Flushes a missile's memory for widget collision. ( All widgets can be hit again )
            
            method enableHitAfter   takes widget w, real seconds returns nothing
            //  • Automatically calls removeHitWidget(w) after "seconds" time. ( Can hit "w" again after given time )
            
        // Module MissileStruct:
        // =====================
            //
            module MissileLaunch // ( optional )
            module MissileStruct
            //  • Enables the entire missile interface for that struct.
            
        // Interface in structs: ( Must implement module MissileStruct ) 
        // =====================
            // 
            //  • Write one, many or all of the static method below into your struct.
            //  • Missiles launched in this struct will run those methods, when their events fire.
            //
            //  • All of those static methods must return a boolean. 
            //      a) return true  --> destroys the missile instance instantly.
            //      b) return false --> keep on flying.

        // Available static method:  
        // ========================
            //
            static method onCollide            takes Missile missile, unit hit returns boolean
            //  • Runs for units in collision range of a missile.

            static method onDestructable       takes Missile missile, destructable hit returns boolean
            //  • Runs for destructables in collision range of a missile.

            static method onItem               takes Missile missile, item hit returns boolean
            //  • Runs for items in collision range of a missile.
            
            static method onTerrain takes Missile missile returns boolean
            //  • Runs when a missile collides with the terrain. ( Ground and cliffs )
            
            static method onFinish  takes Missile missile returns boolean
            //  • Runs only when a missile reaches it's destination. 
            //  • However does not run, if a Missile is destroyed in another method. 
            
            static method onPeriod  takes Missile missile returns boolean
            //  • Runs every Missile_TIMER_TIMEOUT seconds.

            static method onRemove takes Missile missile returns boolean
            //  • Runs when a missile is destroyed. 
            //  • Unlike onFinish, onRemove will runs ALWAYS when a missile is destroyed!!!
            //  
            //  For onRemove the returned boolean has a unique meaning:
            //  • Return true will recycle this missile delayed. ( Only if WRITE_DELAYED_MISSILE_RECYCLING = true )
            //  • Return false will recycle this missile right away.
            
            static method launch takes Missile toLaunch returns nothing
            //  • Well ... Launches this Missile.
            //  • Missile "toLaunch" will behave as declared in the struct. ( static methods from above )
            
    // Misc: ( From the global setup )
    // =====
        //
        // Constants:
        // ==========
            //
            public constant real    TIMER_TIMEOUT                 
            public constant player  NEUTRAL_PASSIVE             
            public constant integer DUMMY_UNIT_ID                
            public constant real    MAXIMUM_COLLISION_SIZE          
            public constant boolean USE_COLLISION_Z_FILTER             
            public constant boolean WRITE_DELAYED_MISSILE_RECYCLING    
            public constant boolean USE_DESTRUCTABLE_FILTER            
            public constant boolean USE_ITEM_FILTER  
            
            readonly static constant string ORIGIN 
            readonly static constant real   HIT_BOX 
        
        // Functions:
        // ==========
            //
            public function GetLocZ               takes real x, real y returns real
                   function GetUnitBodySize       takes unit whichUnit returns real
                   function GetDestructableHeight takes destructable d returns real
                   function GetItemHeight         takes item i returns real
          
//========================================================================
// Missile system. Make changes carefully.
//========================================================================

//! endnovjass ( Enables the compiler )

// Hello and welcome to Missile.
// I wrote a guideline for every piece of code inside Missile, so you 
// can easily understand how the it gets compiled and evaluated.
//
// Let's go!
        
    globals
        // Core constant handle variables of Missile.
        private constant trigger   CORE  = CreateTrigger()
        private constant trigger   MOVE  = CreateTrigger()
        private constant timer     TMR   = CreateTimer()
        private constant location  LOC   = Location(0., 0.)
        private constant rect      RECT  = Rect(0., 0., 0., 0.)
        private constant group     GROUP = CreateGroup()
        private constant hashtable HASH  = InitHashtable()
        // For starting and stopping the timer.
        private integer active = 0
        // Arrays for data structure.
        private integer          array   instances
        private Missile          array   missileList
        private boolexpr         array   expression
        private triggercondition array   condition
        private integer          array   remove
        private boolean          array   destroying
        private boolean          array   recycling
        private integer          array   nextNode
        private integer          array   prevNode
        // Internal widget filter functions.
        private boolexpr destFilter
        private boolexpr itemFilter
        private boolexpr unitFilter
    endglobals
    
    public function GetLocZ takes real x, real y returns real
        call MoveLocation(LOC, x, y)
        return GetLocationZ(LOC)
    endfunction
    
    // For WRITE_DELAYED_MISSILE_RECYCLING = true Missile will hold back
    // dummies for DELAYED_MISSILE_DEATH_ANIMATION_TIME before they are recylced. ( Code placed in a static if )
    //
    //! runtextmacro optional WRITE_MISSILE_RECYCLE_BIN("WRITE_DELAYED_MISSILE_RECYCLING", "DELAYED_MISSILE_DEATH_ANIMATION_TIME")
    
    // The code of WRITE_MISSILE_POSITION_CODE boxes a missiles position and does the required trigonometry.
    //
    //! runtextmacro WRITE_MISSILE_POSITION_CODE()
    
    // Missiles structure works like a linked list with the folling methods:
    // allocateCollection(), allocateNode(), insertFront(node) and remove()
    //
    private keyword MissileStructure
    struct Missile extends array
        implement MissileStructure

        // Constants:
        // ==========
        //
        // Attach point name for effects created by model=. 
        readonly static constant string ORIGIN = "origin"
        
        // Set a ficitional hit box in range of 0 to 1,
        // while 0 is the toe and 1 the head of a unit.
        readonly static constant real HIT_BOX  = (2./3.)
        
        // DEBUG_MODE only members:
        // ========================
        //
        // Checks for double launching. Throws an error message. 
        debug boolean launched 

        // Private members:
        // ================
        //        
        // The position of a missile using curve= does not
        // match the position used by Missile's trigonometry.
        // Therefore we need this member two times.
        // Readable x / y / z for your needs and posX / posY for cool mathematics.
        private real posX
        private real posY
        private real dist// distance
        
        // Readonly members:
        // =================
        //
        // Prevents a double free case.
        readonly boolean allocated
        
        // The dummy unit.
        readonly unit    dummy
        
        // Position members for your needs.
        readonly MissilePosition origin// Grants access to readonly members of MissilePosition,
        readonly MissilePosition impact// which are "x", "y", "z", "angle", "distance", "slope" and "alpha". 
        readonly real            terrainZ
        readonly real            x
        readonly real            y
        readonly real            z
        readonly real            angle// Current angle
        readonly real            prevX
        readonly real            prevY
        readonly real            prevZ
        
        // Collision detection type. ( Evaluated new in each loop )
        readonly integer         collisionType// Current collision type ( circular or rectangle )
        
        // Members you can set:
        // ====================
        //
        unit       source
        unit       target      // For homing missiles.
        real       distance    // Distance traveled.
        player     owner       // Pseudo owner for faster onCollide evaluation. The proper dummy owner is PLAYER_NEUTRAL_PASSIVE.
        real       speed       // Vector lenght for missile movement in plane x / y.
        real       acceleration
        real       damage
        integer    data        // For data transfer set and read data.  
        boolean    recycle     // Is set to true, when a Missile reaches it's destination.
        real       turn        // Sets a turn rate for a missile. 
        real       collision   // Collision size in plane x / y.

        // Setting collision z is deprecated since Missile v2.5. 
        method operator collisionZ= takes real value returns nothing
        endmethod
        method operator collisionZ takes nothing returns real
            return collision
        endmethod
        
        // Operator overloading:
        // =====================
        //
        // Special effect on the missile dummy. For multiple effect attaching, access unit "dummy" directly.
        private effect sfx
        private string path
        method operator model= takes string file returns nothing
            if sfx != null then
                call DestroyEffect(sfx)
                set sfx = null
            endif
                                    // null and ""
            if StringLength(file) > 0 then
                set sfx = AddSpecialEffectTarget(file, dummy, ORIGIN)
                set path = file
            else
                set path = null
            endif
        endmethod
        method operator model takes nothing returns string
            return path
        endmethod
       
        real open
       // Enables curved movement for your missile. 
       // Remember that the result of Tan(PI/2) is infinity.
        method operator curve= takes real value returns nothing
            set open = Tan(value)*origin.distance
        endmethod
        method operator curve takes nothing returns real
            return Atan(open/origin.distance)
        endmethod
       
        real height
        // Enables arcing movement for your missile.
        method operator arc= takes real value returns nothing
            set height = Tan(value)*origin.distance/4
        endmethod
        method operator arc takes nothing returns real
            return Atan(4*height/origin.distance)
        endmethod
       
        private real scaling 
        method operator scale= takes real value returns nothing
            call SetUnitScale(dummy, value, 0., 0.)
            set scaling = value
        endmethod
        method operator scale takes nothing returns real
            return scaling
        endmethod
        
        // Methods:
        // ========
        //
        method bounce takes nothing returns nothing
            call origin.move(x, y, z)
            set dist = 0.
        endmethod
       
        method deflect takes real tx, real ty returns nothing
            local real a = 2.*Atan2(ty - y, tx - x) + bj_PI - angle
            call impact.move(x + (origin.distance - dist)*Cos(a), y + (origin.distance - dist)*Sin(a), impact.z)
            call bounce()
        endmethod
        
        method deflectEx takes real tx, real ty, real tz returns nothing
            call impact.move(impact.x, impact.y, tz)
            call deflect(tx, ty)
        endmethod
    
        method flightTime2Speed takes real duration returns nothing
            set speed = RMaxBJ(0.00000001, (origin.distance - dist)*Missile_TIMER_TIMEOUT/RMaxBJ(0.00000001, duration))
        endmethod
        
        method setMovementSpeed takes real value returns nothing
            set speed = value*Missile_TIMER_TIMEOUT
        endmethod
    
        boolean wantDestroy// For "true" a missile is destroyed on the next timer callback. 100% safe.
        method destroy takes nothing returns nothing
            set wantDestroy = true
        endmethod

        // Instantly destroys a missile.
        method terminate takes nothing returns nothing
            if allocated then
                call remove()
                call impact.destroy()
                call origin.destroy()
            
                call DestroyEffect(sfx)
                call FlushChildHashtable(HASH, this)
                if GetUnitTypeId(dummy) == Missile_DUMMY_UNIT_ID then
                    // MissileRecycler > Dummy > xe.
                    static if LIBRARY_MissileRecycler then
                        call RecycleMissile(dummy)
                    elseif LIBRARY_Dummy and Dummy.create.exists then
                        call Dummy[dummy].destroy()
                    elseif LIBRARY_xedummy and xedummy.release.exists then
                        call xedummy.release(dummy)
                    else
                        call RemoveUnit(dummy)
                    endif
                endif
                set sfx = null
                set source = null
                set target = null
                set dummy = null
                set owner = null
            endif
        endmethod
        
        // Runs in createEx.
        //! textmacro MISSILE_RESET_ALL_MEMBERS
            set path = null
            set speed = 0.
            set acceleration = 0.
            set distance = 0.
            set dist = 0
            set dist = 0.
            set height = 0.
            set turn = 0.
            set open = 0.
            set collision = 0.
            set collisionType = 0
            set stackSize = 0
            set scaling = 1.
            set wantDestroy = false
            set recycle = false
        //! endtextmacro
    
        // Launches a dummy of your choice.
        static method createEx takes unit missileDummy, real impactX, real impactY, real impactZ returns thistype
            local thistype this = thistype.allocateNode()
            local real originX  = GetUnitX(missileDummy)
            local real originY  = GetUnitY(missileDummy)
            local real originZ  = GetUnitFlyHeight(missileDummy)
            //
            //! runtextmacro MISSILE_RESET_ALL_MEMBERS()
            //
            set origin = MissilePosition.create(originX, originY, originZ)
            set impact = MissilePosition.create(impactX, impactY, impactZ)
            call MissilePosition.link(origin, impact)
                        
            set posX      = originX
            set posY      = originY
            set x         = originX
            set y         = originY
            set z         = originZ
            set angle     = origin.angle
            set dummy     = missileDummy
            
            call SetUnitFlyHeight(missileDummy, originZ, 0.)   
            call SaveUnitHandle(HASH, this, GetHandleId(missileDummy), missileDummy)
            //
            static if LIBRARY_ErrorMessage then
                debug call ThrowWarning(GetUnitTypeId(missileDummy) == 0, "Missile", "createEx", "missileDummy", this, "Invalid missile dummy unit ( null )!")
            endif
            debug set launched = false
            return this
        endmethod
    
        // Freaky static if ensures these libraries really don't exist.
        static if not LIBRARY_MissileRecycler and not LIBRARY_Dummy and not Dummy.create.exists and not LIBRARY_xe_dummy and not xe_dummy.new.exists then
            private static method newMissileUnit takes real x, real y, real z, real face returns unit
                local boolean prev = ToogleUnitIndexer(false)
                set bj_lastCreatedUnit = CreateUnit(Missile_NEUTRAL_PASSIVE, Missile_DUMMY_UNIT_ID , x, y, face)
                call ToogleUnitIndexer(prev)
                call SetUnitX(bj_lastCreatedUnit, x)
                call SetUnitY(bj_lastCreatedUnit, y)
                call UnitAddAbility(bj_lastCreatedUnit, 'Amrf')
                call SetUnitFlyHeight(bj_lastCreatedUnit, z, 0.)
                call PauseUnit(bj_lastCreatedUnit, true)
                return bj_lastCreatedUnit
            endmethod
        endif
    
        // MissileRecylcer > Dummy > xe > Missile.
        //! textmacro MISSILE_GET_DUMMY_FROM_LIBRARY
            static if LIBRARY_MissileRecycler then
                return createEx(GetRecycledMissile(x, y, z, angle*bj_RADTODEG), impactX, impactY, impactZ)
        
            elseif LIBRARY_Dummy and Dummy.create.exists then
                local Dummy dummy = Dummy.create(x, y, angle*bj_RADTODEG)
                call SetUnitFlyHeight(dummy.unit, z, 0.)
                return createEx(dummy.unit, impactX, impactY, impactZ)
        
            elseif LIBRARY_xedummy and xedummy.new.exists then
                set bj_lastCreatedUnit = xedummy.new(Missile_NEUTRAL_PASSIVE, x, y, angle*bj_RADTODEG)
                call SetUnitFlyHeight(bj_lastCreatedUnit, z, 0.)
                return createEx(bj_lastCreatedUnit, impactX, impactY, impactZ)
            
            else
                return createEx(Missile.newMissileUnit(x, y, z, angle*bj_RADTODEG), impactX, impactY, impactZ)
            endif
        //! endtextmacro
    
        // Wrapper to createEx.
        static method create takes real x, real y, real z, real angle, real distance, real impactZ returns thistype
            local real impactX = x + distance*Cos(angle)
            local real impactY = y + distance*Sin(angle)
            // Get the dummy unit.
            //! runtextmacro MISSILE_GET_DUMMY_FROM_LIBRARY()
        endmethod
        
        // Wrapper to createEx.
        static method createXYZ takes real x, real y, real z, real impactX, real impactY, real impactZ returns thistype
            local real angle = Atan2(impactY - y, impactX - x)
            // Get the dummy unit.
            //! runtextmacro MISSILE_GET_DUMMY_FROM_LIBRARY()
        endmethod
    
        // Missile motion takes place every Missile_TIMER_TIMEOUT
        // before accessing each active struct.
        static Missile temp = 0
        static method move takes nothing returns boolean
            local integer loops = 0   // Current iteration.
            local integer limit = 150 // Set iteration border per trigger evaluation to avoid hitting the operation limit.
            local thistype this = thistype.temp
            
            local MissilePosition p
            local real a 
            local real d
            local unit u
            local real newX
            local real newY
            local real vel
            local real point
            local real pitch
            loop
                exitwhen 0 == this or loops == limit
                set p = origin
                
                // Save previous, respectively current missile position.
                set prevX = x
                set prevY = y
                set prevZ = z
                
                // Evaluate the collision type.
                set vel = speed
                set speed = vel + acceleration
                if vel < collision*Missile_COLLISION_ACCURACY_FACTOR then
                    set collisionType = Missile_COLLISION_TYPE_CIRCLE
                else
                    set collisionType = Missile_COLLISION_TYPE_RECTANGLE
                endif
                
                // Update missile guidance to its intended target.
                set u = target
                if u != null then
                    if 0 == GetUnitTypeId(u) then
                        set target = null 
                    else
                        call origin.move(x, y, z)
                        call impact.move(GetUnitX(u), GetUnitY(u), GetUnitFlyHeight(u) + GetUnitBodySize(u)*Missile.HIT_BOX)
                        set dist = 0
                        set height = 0
                        set curve = 0
                    endif
                endif
                set a = p.angle
                
                // Update the missile facing angle depending on the turn ratio.
                if 0. != turn and Cos(angle - a) < Cos(turn) then
                    if 0. > Sin(a - angle) then
                        set angle = angle - turn
                    else
                        set angle = angle + turn
                    endif
                else
                    set angle = a
                endif
                
                // Update the missile position on the parabola.
                set d = p.distance// origin - impact distance.
                
                set recycle = dist + vel >= d 
                if recycle then// Maximum distance reached. 
                    set point = d
                    set distance = distance + d - dist
                else
                    set distance = distance + vel
                    set point = dist + vel
                endif
                set dist = point
                
                set newX  = posX + vel*Cos(angle)
                set newY  = posY + vel*Sin(angle)
                set posX  = newX
                set posY  = newY
                
                // Update point(x/y) if a curving trajectory is defined.
                set u = dummy
                if 0. != open and target == null then
                    set vel = 4*open*point*(d - point)/p.square
                    set a = angle + bj_PI/2
                    set newX = newX + vel*Cos(a)
                    set newY = newY + vel*Sin(a)
                    set a = angle + Atan(-((4*open)*(2*point - d))/p.square)
                else
                    set a = angle
                endif
                set x = newX
                set y = newY
                
                // Update pos z if an arc or height is set.
                call MoveLocation(LOC, newX, newY)
                set terrainZ = GetLocationZ(LOC)
                set pitch = p.alpha
                if 0. == height and 0. == pitch then
                    set z = p.z - terrainZ
                else
                    set z = p.z - terrainZ + p.slope*point 
                    if 0. != height and target == null then
                        set z = z + (4*height*point*(d - point)/p.square)
                        set pitch = pitch - Atan(((4*height)*(2*point - d))/p.square)*bj_RADTODEG
                    endif
                endif
                // Update the pitch angle of the dummy unit. 
                if GetUnitTypeId(u) == Missile_DUMMY_UNIT_ID then
                    call SetUnitAnimationByIndex(u, R2I(pitch + 90.5))
                endif
                
                // Move the missile dummy via native.
                call SetUnitFlyHeight(u, z, 0.)
                call SetUnitFacing(u, a*bj_RADTODEG)
                
                // WorldBounds > BoundSentinel.
                static if not LIBRARY_BoundSentinel and not LIBRARY_WorldBounds then
                    if RectContainsCoords(bj_mapInitialPlayableArea, newX, newY) then
                        call SetUnitX(u, newX)
                        call SetUnitY(u, newY)
                    endif                    
                elseif LIBRARY_WorldBounds then
                    if newX < WorldBounds.maxX and newX > WorldBounds.minX and newY < WorldBounds.maxY and newY > WorldBounds.minY then
                        call SetUnitX(u, newX)
                        call SetUnitY(u, newY)
                    endif                
                else
                    call SetUnitX(u, newX)
                    call SetUnitY(u, newY)
                endif
                
                set loops = loops + 1
                set this = nextNode[this]
            endloop
            
            set u = null
            set thistype.temp = this
            return this == 0
        endmethod
            
        // Widget collision API:
        // =====================
        //
        // Runs automatically on widget collision.
        method hitWidget takes widget w returns nothing
            if w != null then
                call SaveWidgetHandle(HASH, this, GetHandleId(w), w)
            endif
        endmethod
        
        // All widget which have been hit return true.
        method hasHitWidget takes widget w returns boolean
            return HaveSavedHandle(HASH, this, GetHandleId(w))
        endmethod
        
        // Removes a widget from the missile's memory of hit widgets. ( This widget can be hit again )
        method removeHitWidget takes widget w returns nothing
            if w != null then
                call RemoveSavedHandle(HASH, this, GetHandleId(w))
            endif
        endmethod
        
        // Flushes a missile's memory for collision. ( All widgets can be hit again )
        method flushHitWidgets takes nothing returns nothing
            call FlushChildHashtable(HASH, this)
            call hitWidget(dummy)
        endmethod
        
        // Tells missile to call removeHitWidget(w) after "seconds" time.
        // Does not apply to widgets, which are already hit by this missile.
        readonly integer stackSize
        method enableHitAfter takes widget w, real seconds returns nothing
            local integer id = GetHandleId(w)
            if w != null then
                if not HaveSavedInteger(HASH, this, id) then
                    call SaveInteger(HASH, this, id, stackSize)
                    call SaveInteger(HASH, this, stackSize, id)
                    set stackSize = stackSize + 1
                endif
                call SaveReal(HASH, this, id, seconds)// Set time.
            endif
        endmethod
        
        method updateStack takes nothing returns nothing
            local integer dex = 0
            local integer id
            local real time
            loop
                exitwhen dex == stackSize
                set id = LoadInteger(HASH, this, dex)
                set time = LoadReal(HASH, this, id) - Missile_TIMER_TIMEOUT
                if time <= 0. or not HaveSavedHandle(HASH, this, id) then
                    set stackSize = stackSize - 1
                    set id = LoadInteger(HASH, this, stackSize)
                    call SaveInteger(HASH, this, dex, id)
                    call SaveInteger(HASH, this, id, dex)
                    // Enables hit.
                    call RemoveSavedHandle(HASH, this, id)
                    // Remove data from stack.
                    call RemoveSavedReal(HASH, this, id)
                    call RemoveSavedInteger(HASH, this, id)
                    call RemoveSavedInteger(HASH, this, stackSize)
                else
                    call SaveReal(HASH, this, id, time)
                    set dex = dex + 1
                endif
            endloop
        endmethod
    
        // Widget collision code:
        // ======================
        //
        private static boolean circle = true
        //
        // 1.) Rectangle collision for fast moving missiles with small collision radius.
        //        
        // Runs for destructables and items in a rectangle.
        // Checks if widget w is in collision range of a missile.
        // Is not precise in z - axis collision.
        private method isWidgetInRectangle takes widget w, real wz, real distance returns boolean
            local real wx = GetWidgetX(w)
            local real wy = GetWidgetY(w)
            local real dz = Missile_GetLocZ(wx, wy) - terrainZ
            local real dx = x - prevX
            local real dy = y - prevY
            local real s  = (dx*(wx - prevX) + dy*(wy - prevY))/(dx*dx + dy*dy)  
            
            if s < 0. then
                set s = 0.
            elseif s > 1 then
                set s = 1.
            endif
            set dx = (prevX + s*dx) - wx
            set dy = (prevY + s*dy) - wy

            return dx*dx + dy*dy <= distance*distance and dz + wz >= z - distance and dz <= z + distance 
        endmethod        
        //
        // 2.) Circular collision detection for all other missiles.
        //
        // Returns true for widgets in a xyz collision range. 
        private method isWidgetInRange takes widget w, real wz, real distance returns boolean
            local real wx = GetWidgetX(w) 
            local real wy = GetWidgetY(w) 
            local real dz = Missile_GetLocZ(wx, wy) - terrainZ
            //     collision in plane x and y,            collision in z axis.
            return IsUnitInRangeXY(dummy, wx, wy, distance) and dz + wz >= z - distance and dz <= z + distance 
        endmethod        
        //
        //  3.) Action functions inside the widget enumeration thread.
        //
        // Runs for every enumerated destructable.
        //  • Directly filters out already hit destructables.
        //  • Distance formula based on the Pythagorean theorem.
        //                         
        private static method filterDests takes nothing returns boolean
            local destructable d = GetFilterDestructable()
            local boolean b = false
            if not HaveSavedHandle(HASH, temp, GetHandleId(d)) then
                if circle then                          
                    set b = temp.isWidgetInRange(d, GetDestructableHeight(d), temp.collision)
                else
                    set b = temp.isWidgetInRectangle(d, GetDestructableHeight(d), temp.collision) 
                endif
            endif
            set d = null
            return b
        endmethod
        //
        // Runs for every enumerated item.
        //  • Directly filters out already hit items.
        //  • Distance formula based on the Pythagorean theorem.
        //  • Items have a fix collision size of 16.
        // 
        private static method filterItems takes nothing returns boolean
            local item i = GetFilterItem()
            local boolean b = false
            if not HaveSavedHandle(HASH, temp, GetHandleId(i)) then
                if circle then                                        // Item in missile collision size or item pathing in missile range.
                    set b = temp.isWidgetInRange(i, GetItemHeight(i), RMaxBJ(temp.collision, 16.))
                else
                    set b = temp.isWidgetInRectangle(i, GetItemHeight(i), RMaxBJ(temp.collision, 16.)) 
                endif
            endif
            set i = null
            return b
        endmethod
        //
        //  4.) Filter function for rectangle unit collision.
        //
        // Runs for every enumerated units.
        //  • Filters out units which are not in collision range in plane x / y.
        //  • Inlined and therefore a bit faster than item and destructable collision.
        //
        private static method filterUnits takes nothing returns boolean
            local thistype this = thistype.temp
            local unit u = GetFilterUnit()
            local real dx 
            local real dy
            local real s 
            local boolean is = false
            
            if not HaveSavedHandle(HASH, this, GetHandleId(u)) then
                set dx = x - prevX
                set dy = y - prevY
                set s = (dx*(GetUnitX(u) - prevX) + dy*(GetUnitY(u)- prevY))/(dx*dx + dy*dy)
                if s < 0. then
                    set s = 0.
                elseif s > 1. then
                    set s = 1.
                endif
                set is = IsUnitInRangeXY(u, prevX + s*dx, prevY + s*dy, collision) 
            endif
            
            set u = null
            return is
        endmethod
        //
        // 5.) Proper rect preparation.
        //
        // For rectangle.
        private method prepareRectRectangle takes nothing returns nothing
            local real x1 = prevX 
            local real y1 = prevY 
            local real x2 = x
            local real y2 = y
            local real d = collision + Missile_MAXIMUM_COLLISION_SIZE
            // What is min, what is max ...
            if x1 < x2 then
                if y1 < y2 then
                    call SetRect(RECT, x1 - d, y1 - d, x2 + d, y2 + d)
                else
                    call SetRect(RECT, x1 - d, y2 - d, x2 + d, y1 + d)
                endif
            elseif y1 < y2 then
                call SetRect(RECT, x2 - d, y1 - d, x1 + d, y2 + d)
            else
                call SetRect(RECT, x2 - d, y2 - d, x1 + d, y1 + d)
            endif 
        endmethod
        //
        // For circular.
        private method prepareRectCircle takes nothing returns nothing
            local real d = collision + Missile_MAXIMUM_COLLISION_SIZE
            call SetRect(RECT, x - d, y - d, x + d, y + d)
        endmethod
        //
        // 5.) API for the MissileStruct iteration.
        //
        method groupEnumUnitsRectangle takes nothing returns nothing
            call prepareRectRectangle()
            set thistype.temp = this
            call GroupEnumUnitsInRect(GROUP, RECT, unitFilter)
        endmethod
        //
        // Prepares destructable enumeration, then runs enumDests.
        method checkDestCollision takes code func returns nothing
            set circle = collisionType == Missile_COLLISION_TYPE_CIRCLE
            if circle then
                call prepareRectCircle()
            else
                call prepareRectRectangle()
            endif
            //
            set thistype.temp = this
            call EnumDestructablesInRect(RECT, destFilter, func)
        endmethod
        //
        // Prepares item enumeration, then runs enumItems.
        method checkItemCollision takes code func returns nothing
            set circle = collisionType == Missile_COLLISION_TYPE_CIRCLE 
            if circle then
                call prepareRectCircle()
            else
                call prepareRectRectangle()
            endif
            //
            set thistype.temp = this
            call EnumItemsInRect(RECT, itemFilter, func)
        endmethod

static if Missile_WRITE_DELAYED_MISSILE_RECYCLING then
    method nullBefore takes nothing returns nothing
        set dummy = null
    endmethod
endif

// Does not check for 'Aloc' and 'Amrf' as they could be customized.
        private static method onInit takes nothing returns nothing
            static if LIBRARY_ErrorMessage then
                debug local boolean prev = ToogleUnitIndexer(false)
                debug local unit dummy = CreateUnit(Missile_NEUTRAL_PASSIVE, Missile_DUMMY_UNIT_ID, 0., 0., 0.)
                debug call ToogleUnitIndexer(prev)
                //
                debug call ThrowError((GetUnitTypeId(dummy) != Missile_DUMMY_UNIT_ID), "Missile", "DEBUG_MISSILE", "type id",    0, "Global setup for public integer DUMMY_UNIT_ID is incorrect! This map currently can't use Missile!")
                debug call ThrowError((Missile_MAXIMUM_COLLISION_SIZE < 0),            "Missile", "DEBUG_MISSILE", "collision",  0, "Global setup for public real MAXIMUM_COLLISION_SIZE is incorrect, below zero! This map currently can't use Missile!")
                debug call RemoveUnit(dummy)
                debug set dummy = null
            endif
            //
            set unitFilter = Filter(function thistype.filterUnits) 
            set destFilter = Filter(function thistype.filterDests) 
            set itemFilter = Filter(function thistype.filterItems) 
            call TriggerAddCondition(MOVE, Condition(function thistype.move))
        endmethod
        
    endstruct
    // You made it to the end of Missile, but we are not finished.
    // Do you remember about the data structure, the delayed recycler
    // and of course our interface module "MissileStruct"
    //
    // This comes now!
    
    // Debug code taken from List ( full credits to Nestharus )
    private module MissileStructure
        private static thistype collectionCount = 0
        private static thistype nodeCount       = 0
        
        static if LIBRARY_ErrorMessage then
            debug private boolean isNode
            debug private boolean isCollection
        endif
        
        private thistype _list
        method operator list takes nothing returns thistype
            static if LIBRARY_ErrorMessage then
                debug call ThrowError(this == 0,    "MissileStructure", "list", "thistype", this, "Attempted To Read Null Node.")
                debug call ThrowError(not isNode,   "MissileStructure", "list", "thistype", this, "Attempted To Read Invalid Node.")
            endif
            return _list
        endmethod
        
        private thistype _next
        method operator next takes nothing returns thistype
            static if LIBRARY_ErrorMessage then
                debug call ThrowError(this == 0,    "MissileStructure", "next", "thistype", this, "Attempted To Go Out Of Bounds.")
                debug call ThrowError(not isNode,   "MissileStructure", "next", "thistype", this, "Attempted To Read Invalid Node.")
            endif
            return _next
        endmethod
        
        private thistype _prev
        method operator prev takes nothing returns thistype
            static if LIBRARY_ErrorMessage then
                debug call ThrowError(this == 0,    "MissileStructure", "prev", "thistype", this, "Attempted To Go Out Of Bounds.")
                debug call ThrowError(not isNode,   "MissileStructure", "prev", "thistype", this, "Attempted To Read Invalid Node.")
            endif
            return _prev
        endmethod
        
        private thistype _first
        method operator first takes nothing returns thistype
            static if LIBRARY_ErrorMessage then
                debug call ThrowError(this == 0,        "MissileStructure", "first", "thistype", this, "Attempted To Read Null List.")
                debug call ThrowError(not isCollection, "MissileStructure", "first", "thistype", this, "Attempted To Read Invalid List.")
            endif
            return _first
        endmethod
        
        private thistype _last
        method operator last takes nothing returns thistype
            static if LIBRARY_ErrorMessage then
                debug call ThrowError(this == 0,        "MissileStructure", "last", "thistype", this, "Attempted To Read Null List.")
                debug call ThrowError(not isCollection, "MissileStructure", "last", "thistype", this, "Attempted To Read Invalid List.")
            endif
            return _last
        endmethod
        
        static method allocateCollection takes nothing returns thistype
            local thistype this = thistype(0)._first
            
            if (0 == this) then
                static if LIBRARY_ErrorMessage then
                    debug call ThrowError(collectionCount == 8191, "MissileStructure", "allocateCollection", "thistype", 0, "Overflow.")
                endif
                
                set this = collectionCount + 1
                set collectionCount = this
            else
                set thistype(0)._first = _first
            endif
            
            static if LIBRARY_ErrorMessage then
                debug set isCollection = true
            endif
            
            set _first = 0
            
            return this
        endmethod
        
        static method allocateNode takes nothing returns thistype
            local thistype this = thistype(0)._next
            
            if (0 == this) then
                static if LIBRARY_ErrorMessage then
                    debug call ThrowError(nodeCount == 8191, "MissileStructure", "allocateNode", "thistype", 0, "Overflow.")
                endif
                
                set this = nodeCount + 1
                set nodeCount = this
            else
                set thistype(0)._next = _next
            endif
            
            set allocated = true
            
            return this
        endmethod
        
        method insertFront takes thistype node returns thistype
            
            // Extra static unique list for missile motion.
            set nextNode[node] = 0
            set prevNode[node] = prevNode[0]
            set nextNode[prevNode[0]] = node
            set prevNode[0] = node
            
            static if LIBRARY_ErrorMessage then
                debug call ThrowError(this == 0,        "List", "push", "thistype", this, "Attempted To Push On To Null List.")
                debug call ThrowError(not isCollection, "List", "push", "thistype", this, "Attempted To Push On To Invalid List.")
                debug set node.isNode = true
            endif
            
            set node._list = this
        
            if (_first == 0) then
                set _first = node
                set _last = node
                set node._next = 0
            else
                set _first._prev = node
                set node._next = _first
                set _first = node
            endif
            
            set node._prev = 0
            
            return node
        endmethod
      
        method remove takes nothing returns nothing
            local thistype node = this
            set this = node._list
            
            static if LIBRARY_ErrorMessage then
                debug call ThrowError(node == 0,        "MissileStructure", "remove", "thistype", this, "Attempted To Remove Null Node.")
                debug call ThrowError(not node.isNode,  "MissileStructure", "remove", "thistype", this, "Attempted To Remove Invalid Node (" + I2S(node) + ").")
                debug set node.isNode = false
            endif
            
            set node._list = 0
        
            if (0 == node._prev) then
                set _first = node._next
            else
                set node._prev._next = node._next
            endif
            if (0 == node._next) then
                set _last = node._prev
            else
                set node._next._prev = node._prev
            endif
            
            set node._next = thistype(0)._next
            set thistype(0)._next = node
            
            set node.allocated = false
            
            // Static unique list for missile motion.
            set nextNode[prevNode[node]] = nextNode[node]
            set prevNode[nextNode[node]] = prevNode[node]
        endmethod
    endmodule

    // Boolean expressions per struct:
    // ===============================
    //     
    // Condition function for the core trigger evaluation. ( Runs for all struct using module MissileStruct )
    private function MissileCreateExpression takes integer structId, code func returns nothing
        set expression[structId] = Condition(func)
    endfunction
    
    // Creates a collection for a struct. ( Runs for all struct using module MissileStruct )
    private function MissileCreateCollection takes integer structId returns nothing
        set missileList[structId] = Missile.allocateCollection()
    endfunction
    
    // Core:
    // =====
    //
    // Fires every Missile_TIMER_TIMEOUT.
    private function Fire takes nothing returns nothing
        local integer i = remove[0]
        set remove[0] = 0
        loop
            exitwhen 0 == i
            if recycling[i] then
                call TriggerRemoveCondition(CORE, condition[i])
                set condition[i] = null
                set active = active - 1
            endif
            set destroying[i] = false
            set recycling[i] = false
            set i = remove[i]
        endloop
        
        if 0 == active then
            call PauseTimer(TMR)
        else
            // Move all launched missiles.
            set Missile.temp = nextNode[0]
            set i = 0
            loop
                exitwhen TriggerEvaluate(MOVE)
                exitwhen i == 60// Moved over 8910 missiles, something buggy happened.
                set i = i + 1   // This case is impossible, hence never happens. But if I'm wrong, which also never happens 
            endloop             // then the map 100% crashes. i == 66 will prevent the game crash and Missile will start to 
                                // to debug itself over the next iterations.
        
            // Access all structs using module MissileStruct. 
            static if DEBUG_MODE and LIBRARY_ErrorMesssage then
                if not TriggerEvaluate(CORE) then
                    call ThrowWarning(true, "Missile", "Fire", "op limit", 0, /*
*/"You just ran into a op limit!
The op limit protection of Missile is insufficient for your map.
The code of the missile module can't run in one thread and must be splitted.
If unsure make a post in the official 'The Hive Workshop' forum thread of Missile!")
                endif
            else
                call TriggerEvaluate(CORE)
            endif
        endif
    endfunction
    
    // Conditionally starts the timer.
    private function StartPeriodic takes integer structId returns nothing
        if 0 == instances[structId] or destroying[structId] then            
            if destroying[structId] then
                set recycling[structId] = false
            else
                if 0 == active then
                    call TimerStart(TMR, Missile_TIMER_TIMEOUT, true, function Fire)
                endif
                set active = active + 1
                set condition[structId] = TriggerAddCondition(CORE, expression[structId])
            endif
        endif
        set instances[structId] = instances[structId] + 1
    endfunction
    
    // Conditionally stops the timer in the next callback.
    private function StopPeriodic takes integer structId returns nothing
        set instances[structId] = instances[structId] - 1
        if 0 == instances[structId] and condition[structId] != null then
            if not destroying[structId] and not recycling[structId] then
                set destroying[structId] = true
                set recycling[structId] = true
                set remove[structId] = remove[0]
                set remove[0] = structId
            endif
        endif
    endfunction
    
    // Modules:
    // ========
    //
    // You want to place module MissileLaunch at the very top of your struct.
    module MissileLaunch
        static method launch takes Missile missile returns nothing
            static if LIBRARY_ErrorMessage then
                debug call ThrowError(missile.launched, "thistype", "launch", "missile.launched", missile, "This missile was already launched before!")
            endif
            debug set missile.launched = true
            //
            call missileList[thistype.typeid].insertFront(missile)
            call StartPeriodic(thistype.typeid)
        endmethod
    endmodule
    
    module MissileTerminate
        // Called from missileIterate. "P" to avoid code collision.
        static method missileTerminateP takes Missile node returns nothing
            if node.allocated then
                static if thistype.onRemove.exists then
                    static if Missile_WRITE_DELAYED_MISSILE_RECYCLING and RecycleBin.recycle.exists then
                        if thistype.onRemove(node) and GetUnitTypeId(node.dummy) == Missile_DUMMY_UNIT_ID then
                            call RecycleBin.recycle(node.dummy)
                            call node.nullBefore()
                        endif
                    else
                        call thistype.onRemove(node)
                    endif
                endif
            
                call node.terminate()
                call StopPeriodic(thistype.typeid)
            endif
        endmethod
    endmodule
    
    // Allows you to inject missile in certain stages of the motion process.
    module MissileAction

static if DEBUG_MODE then
    // Runs check during compile time.
    static if thistype.onMissile.exists then
        Error Message from library Missile in struct thistype !
        thistype.onMissile is a reserved name for Missile, once you implemented MissileStruct.
        thistype.onMissile is currently not supported by library Missile. 
        Please delete or re-name that method.
    endif
endif

    static if thistype.onItem.exists then
        private static method missileActionItem takes nothing returns nothing
            local item i = GetEnumItem()
            local Missile this = Missile.temp
            if this.allocated then
                call SaveItemHandle(HASH, this, GetHandleId(i), i) 
                if thistype.onItem(this, i) then
                    call missileTerminateP(this)
                endif
            endif
            set i = null
        endmethod
    endif
        
    static if thistype.onDestructable.exists then
        private static method missileActionDest takes nothing returns nothing
            local destructable d = GetEnumDestructable()
            local Missile this = Missile.temp
            if this.allocated then
                call SaveDestructableHandle(HASH, this, GetHandleId(d), d) 
                if thistype.onDestructable(this, d) then
                    call missileTerminateP(this)
                endif
            endif
            set d = null
        endmethod
    endif
    
        // Runs every Missile_TIMER_TIMEOUT for this struct.
        static method missileIterateP takes nothing returns boolean
            local Missile this = missileList[thistype.typeid].first 
            local Missile node
            local real collideZ
            local boolean b
            local unit u
            
            loop
                exitwhen 0 == this
                set node = this.next// The linked list should not lose the next node.
               
                if this.wantDestroy then
                    call thistype.missileTerminateP(this)
                else
                    if this.stackSize > 0 then
                        call this.updateStack()
                    endif
                    
                    // Runs unit collision.
                    static if thistype.onCollide.exists then
                        if this.allocated and 0. < this.collision then
                            set b = this.collisionType == Missile_COLLISION_TYPE_RECTANGLE  
                            if b then
                                call this.groupEnumUnitsRectangle()
                            else
                                call GroupEnumUnitsInRange(GROUP, this.x, this.y, this.collision + Missile_MAXIMUM_COLLISION_SIZE, null)
                            endif
                            loop
                                set u = FirstOfGroup(GROUP)
                                exitwhen u == null
                                call GroupRemoveUnit(GROUP, u)
                                if not HaveSavedHandle(HASH, this, GetHandleId(u)) then
                                    if IsUnitInRange(u, this.dummy, this.collision) or b then  
                                        // Eventually run z collision checks.
                                        static if Missile_USE_COLLISION_Z_FILTER then
                                            set collideZ = Missile_GetLocZ(GetUnitX(u), GetUnitY(u)) + GetUnitFlyHeight(u) - this.terrainZ
                                            if (collideZ + GetUnitBodySize(u) >= this.z - this.collisionZ) and (collideZ <= this.z + this.collisionZ) then
                                                // Mark as hit.
                                                call SaveUnitHandle(HASH, this, GetHandleId(u), u)
                                                if thistype.onCollide(this, u) then
                                                    call thistype.missileTerminateP(this)
                                                    exitwhen true
                                                endif
                                            endif
    
                                        else
                                            // Runs unit collision without z collision checks.
                                            call SaveUnitHandle(HASH, this, GetHandleId(u), u)
                                            if thistype.onCollide(this, u) then
                                                call thistype.missileTerminateP(this)
                                                exitwhen true
                                            endif
                                        endif
                                    endif                                    
                                endif
                            endloop
                        endif
                    endif
                    
                    // Runs destructable collision.
                    static if thistype.onDestructable.exists then
                        // Check if the missile is not terminated.
                        if this.allocated and 0. < this.collision then
                            call this.checkDestCollision(function thistype.missileActionDest)
                        endif
                    endif
                    
                    // Runs item collision.
                    static if thistype.onItem.exists then
                        //  Check if the missile is not terminated.
                        if this.allocated and 0. < this.collision then
                            call this.checkItemCollision(function thistype.missileActionItem)
                        endif
                    endif
                
                    // Runs when the destination is reached.
                    if this.recycle and this.allocated then
                        static if thistype.onFinish.exists then
                            if thistype.onFinish(this) then
                                call thistype.missileTerminateP(this)
                            endif
                        else
                            call thistype.missileTerminateP(this)
                        endif
                    endif
                
                    // Runs on terrian collision.
                    static if thistype.onTerrain.exists then
                        if this.allocated and 0. > this.z and thistype.onTerrain(this) then
                            call missileTerminateP(this)
                        endif
                    endif
                
                    // Runs every Missile_TIMER_TIMEOUT.
                    static if thistype.onPeriod.exists then
                        if this.allocated and thistype.onPeriod(this) then
                            call missileTerminateP(this)
                        endif
                    endif
                endif
                set this = node
            endloop
            
            set u = null
            static if DEBUG_MODE and LIBRARY_ErrorMessage then
                return true
            else
                return false
            endif
        endmethod
    endmodule
    
    module MissileStruct
        implement MissileLaunch
        implement MissileTerminate
        implement MissileAction
        
        private static method onInit takes nothing returns nothing
            call MissileCreateCollection(thistype.typeid)
            call MissileCreateExpression(thistype.typeid, function thistype.missileIterateP)
        endmethod
    endmodule
    
    // Missile position:
    // =================
    //
    // Simple trigonometry.
    //! textmacro WRITE_MISSILE_POSITION_CODE 
        struct MissilePosition extends array
            private static integer array recycler
            private static integer alloc = 0
            
            // Readonly members you can access.
            readonly real x
            readonly real y
            readonly real z
            readonly real angle
            readonly real distance
            readonly real square
            readonly real slope
            readonly real alpha
            
            // Creates an origin - impact link.
            private thistype ref
            
            private static method math takes thistype a, thistype b returns nothing
                local real dx 
                local real dy 
                loop
                    set dx = b.x - a.x
                    set dy = b.y - a.y
                    set dx = dx*dx + dy*dy
                    set dy = SquareRoot(dx)
                    exitwhen dx != 0. and dy != 0.
                    set b.x = b.x + .01
                    set b.z = b.z - Missile_GetLocZ(b.x -.01, b.y) + Missile_GetLocZ(b.x, b.y)
                endloop
                
                set a.square   = dx
                set a.distance = dy
                set a.angle    = Atan2(b.y - a.y, b.x - a.x)
                set a.slope    = (b.z - a.z)/dy
                set a.alpha    = Atan(a.slope)*bj_RADTODEG
                // Set b.
                if b.ref == a then
                    set b.angle     = a.angle + bj_PI
                    set b.distance  = dy
                    set b.slope     = -a.slope
                    set b.alpha     = -a.alpha
                    set b.square    = dx
                endif
            endmethod
            
            static method link takes thistype a, thistype b returns nothing
                set a.ref = b
                set b.ref = a
                call math(a, b)
            endmethod
    
            method move takes real toX, real toY, real toZ returns nothing
                set x = toX
                set y = toY
                set z = toZ + Missile_GetLocZ(toX, toY)
                if ref != this then
                    call math(this, ref)
                endif
            endmethod
    
            method destroy takes nothing returns nothing
                set recycler[this] = recycler[0]
                set recycler[0]    = this
            endmethod
    
            static method create takes real x, real y, real z returns MissilePosition
                local thistype this = recycler[0]
                 if 0 == this then
                    set alloc = alloc + 1
                    set this  = alloc
                else
                    set recycler[0] = recycler[this]
                endif
                set ref = this
                call move(x, y, z)
                return this
            endmethod
    
        endstruct
    //! endtextmacro 
    
    // Delayed dummy recycling:
    // ========================
    //
    // Ensures proper fx death animations.
    //! textmacro WRITE_MISSILE_RECYCLE_BIN takes DO_THIS, AFTER_TIME 
    static if $DO_THIS$ then

        private struct RecycleBin extends array
            private static constant timer t = CreateTimer()
            private static integer max = 0
            private static unit array dummy
            private static real array time
     
            private static method onPeriodic takes nothing returns nothing
                local integer dex = 0
                loop
                    exitwhen dex == thistype.max
                    set thistype.time[dex] = thistype.time[dex] - 1
                    if 0 >= thistype.time[dex] then
                    
                        static if LIBRARY_MissileRecycler then
                            call RecycleMissile(thistype.dummy[dex])
                    
                        elseif Dummy.create.exists and LIBRARY_Dummy then
                            call Dummy[thistype.dummy[dex]].destroy()
                        
                        elseif LIBRARY_xedummy and xedummy.release.exists then
                            call xedummy.release(thistype.dummy[dex])
                    
                        else
                            call RemoveUnit(thistype.dummy[dex])
                        endif
                    
                        set thistype.dummy[dex]          = null
                        set thistype.max                 = thistype.max - 1
                        set thistype.dummy[dex]          = thistype.dummy[thistype.max]
                        set thistype.time[dex]           = thistype.time[thistype.max]
                        set thistype.dummy[thistype.max] = null
                        set dex                          = dex - 1
                    
                        if 0 == thistype.max then
                            call PauseTimer(thistype.t)
                        endif
                    
                    endif
                    set dex = dex + 1
                endloop
            endmethod
    
            static method recycle takes unit toRecycle returns nothing
                if 0 == thistype.max then
                    call TimerStart(thistype.t, 1., true, function thistype.onPeriodic)
                endif
                set thistype.dummy[max] = toRecycle
                set thistype.time[max]  = $AFTER_TIME$ + TimerGetRemaining(thistype.t)
                set thistype.max        = thistype.max + 1
            endmethod
    
        endstruct
    endif
    //! endtextmacro
    
// The end!
endlibrary

Requirements

Important: Missile requires basic knowledge about structs, hence it is not GUI-friendly at all.
Furthermore in order to compile library Missile, you'll need JPNG.

By default library Missile has no external requirements. This is intended by me, as I don't want to enforce you as map maker
to import a specific i.e. dummy recycler just because of library Missile. That way Missile can be imported very easily into any map.


Optional requirements

Missile can use a few optional requirements ( JassHelper link ), once they exists in your map.
Each optional requirement has been chosen carefully. It's recommended to consider using one of each block listed in the table below.

Important: Using more than one requirement of each block has no benefit at all! It would be counterproductive for your map!


Dummy recycler​
Safety​
Debugging​





Creators and destructors

A new Missile instance can be allocated via following static methods:
JASS:
////
//
// Available creators:
// ===================
    // 
    static method createEx takes unit missileDummy, real impactX, real impactY, real impactZ returns Missile
    //  • Core creator method.
    //  • May launches any unit. 
    //  • Units of type Missile_DUMMY_UNIT_ID get recycled in the end.
        
    static method create takes real x, real y, real z, real angleInRadians, real distanceToTravel, real endZ returns Missile
    //  • Converts arguments to fit into createEx, then calls createEx.
    
    static method createXYZ takes real x, real y, real z, real impactX, real impactY, real impactZ returns Missile
    //  • Converts arguments to fit into createEx, then calls createEx. 
            
    // Available destructors:
    // ======================
        //
        return true
        //  • Core destructor.
        //  • Returning true in any of the interface methods of the MissileStruct module
        //    will destroy that instance instantly.
            
        method destroy   takes nothing returns nothing
        //  • Destroys the missile during the next timer callback. 
        
        method terminate takes nothing returns nothing
        //  • Destroys the missile instantly.
Each struct allocator is discussed in detail in this tutorial.

module MissileStruct

In order to use Missile in a struct, simply implement Module MissileStruct into it.
JASS:
scope HowToUse

    struct HowToUse
        
        // MissileStruct has to be implemented below all declared methods
        // For better understanding check: Declaration
        implement MissileStruct// Adds required code to struct HowToUse

    endstruct

endscope
For more information look into this tutorial.

Static methods in your struct

Once module MissileStruct is implemented, you can declare various static methods in that struct.
Later the compiler will check, if these methods exist and write the required code into your struct.

Each method returns boolean ( true/false )
return true - destroy this Missile instance.​
return false - keep on moving this Missile instance.​

JASS:
////
//
//  Interface in structs: ( Must implement module MissileStruct ) 
//  =====================
    // 
    //  • Write one, many or all of the static method below into your struct.
    //  • Missiles launched in this struct will run those methods, when their events fire.
    //
    //  • All of those static methods must return a boolean. 
    //      a) return true  --> destroys the missile instance instantly.
    //      b) return false --> keep on flying.

    // Available static method:  
    // ========================
        //
        static method onCollide            takes Missile missile, unit hit returns boolean
        //  • Runs for units in collision range of a missile.

        static method onDestructable       takes Missile missile, destructable hit returns boolean
        //  • Runs for destructables in collision range of a missile.
        //  • Runs after onDestructableFilter ( if a filter is declared )

        static method onDestructableFilter takes nothing returns boolean
        //  • Runs before onDestructable as filter function.
        //  • Runs for destructables in collision range + Missile_MAX_COLLISION_RANGE.
        //  • Get the filter destructable via "GetFilterDestructable()" 
        //  • Designed only for improving code read-ability.

        static method onItem               takes Missile missile, item hit returns boolean
        //  • Runs for items in collision range of a missile.
        //  • Runs after onItemFilter ( if a filter is declared )

        static method onDestructableFilter takes nothing returns boolean
        //  • Runs before onItem as filter function.
        //  • Runs for items in collision range + Missile_MAX_COLLISION_RANGE.
        //  • Get the filter item via "GetFilterItem()" 
        //  • Designed only for improving code read-ability.            
            
        static method onTerrain takes Missile missile returns boolean
        //  • Runs when a missile collides with the terrain. ( Ground and cliffs )
            
        static method onFinish  takes Missile missile returns boolean
        //  • Runs only when a missile reaches it's destination. 
        //  • However does not run, if a Missile is destroyed in another method. 
            
        static method onPeriod  takes Missile missile returns boolean
        //  • Runs every Missile_TIMER_TIMEOUT seconds.

        static method onRemove takes Missile missile returns boolean
        //  • Runs when a missile is destroyed. 
        //  • Unlike onFinish, onRemove will runs ALWAYS when a missile is destroyed!!!
        //  
        //  For onRemove the returned boolean has a unique meaning:
        //
        //  • Return true will recycle this missile delayed. ( only if WRITE_DELAYED_MISSILE_RECYCLING = true )
        //  • Return false will recycle this missile right away.
            
        static method launch takes Missile toLaunch returns nothing
        //  • Well ... Launches this Missile.
        //  • Missile "toLaunch" will behave as declared in the struct. ( static methods from above )

JASS:
scope HowToUse

    struct HowToUse
        
        static method onCollide takes Missile missile, unit hit returns boolean
            if UnitAlive(hit) then
                return true// Destroy this Missile instance
            endif
            return false// Keep on flying
        endmethod

        // MissileStruct has to be implemented below all declared methods
        // For better understanding check: Declaration
        implement MissileStruct// Adds required code to struct HowToUse

    endstruct

endscope

Missile members and methods

Each Missile instance has fields you can set and / or read.
JASS:
////
//
//  Fields you can set and read directly:
//  =====================================
        //
        unit       source
        unit       target      // For homing missiles.
        real       distance    // Distance traveled.
        player     owner       // Pseudo owner for faster onCollide evaluation. The proper dummy owner remains PLAYER_NEUTRAL_PASSIVE.
        real       speed       // Vector lenght for missile movement in plane x / y. ( DOES NOT TAKE THE TIMER TIMEOUT IN ACCOUNT )
        real       acceleration
        real       damage
        integer    data        // For data transfer set and read data.  
        boolean    recycle     // Is automatically set to true, when a Missile reaches it's destination.
        real       turn        // Set a turn rate for a missile.
            
        // Neither collision nor collisionZ accept values below zero.
        real       collision   // Collision size in plane x / y.
        real       collisionZ  // Collision size in z - axis.
            
    // Fields you can only read:
    // =========================      
        //
        readonly boolean         allocated        
        readonly unit            dummy// The dummy unit of this missile.
        
        // Position members for you needs.
        readonly MissilePosition origin// Grants access to readonly members of MissilePosition,
        readonly MissilePosition impact// which are "x", "y", "z", "angle", "distance", "slope" and the pitch angle "alpha". 
        readonly real            terrainZ
        readonly real            x
        readonly real            y
        readonly real            z
        readonly real            angle// Current angle in radians.

    // Method operators for set and read:
    // ==================================
        //
        method operator model= takes string modelFile returns nothing
        method operator model  takes nothing returns string
        //  • Adds an effect handle on a missile dummy to it's "origin".
        //  • You can read the file path.
        //  • For multiple effects access "this.dummy" in your struct.
            
        method operator scale= takes real value returns nothing
        method operator scale  takes nothing returns real
        //  • Set and read the scaling of the dummy unit.  
            
        method operator curve= takes real value returns nothing
        method operator curve  takes nothing returns real
        //  • Enables curved movement for your missile. ( Pass in radians, NOT degrees )
        //  • Do not pass in PI/2.
       
        method operator arc=   takes real value returns nothing
        method operator arc    takes nothing returns real
        //  • Enables arcing movement for your missile. ( Pass in radians, NOT degrees )
        //  • Do not pass in PI/2.
                        
    // Methods for missile movement:
    // =============================
        //
        method bounce           takes nothing returns nothing
        //  • Moves the MissilePosition "origin" to the current missile coordinates.
        //  • Resets the distance traveled to 0.
            
        method deflect          takes real tx, real ty returns nothing
        //  • Deflect the missile towards tx, ty. Then calls bounce.        
        
        method deflectEx        takes real tx, real ty, real tz returns nothing
        //  • Deflect the missile towards tx, ty, tz. Then calls bounce.
    
        method flightTime2Speed takes real duration returns nothing
        //  • Converts a fly time to a vector lenght for member "speed".
        //  • Does not take acceleration into account. ( Disclaimer )
        
        method setMovementSpeed takes real value returns nothing
        //  • Converts Warcraft III movement speed to a vector lenght for member "speed".
JASS:
scope HowToUse

    struct HowToUse

        private static method onCollide takes Missile missile, unit hit returns boolean 
            if UnitAlive(hit) and IsUnitEnemy(hit, missile.owner) then
                call UnitDamageTarget(missile.source, hit, missile.damage, false, false, null, null, null)
                return true
            endif
            return false
        endmethod

        // MissileStruct has to be implemented below all declared methods
        // For better understanding check: Declaration
        implement MissileStruct// Adds required code to struct HowToUse

        static method onCast takes nothing returns nothing
            local Missile m =  Missile.create(-505, -670, 50, 90*bj_DEGTORAD, 1000, 50)
            set m.source    = GetTriggerUnit()
            set m.speed     = 15.
            set m.model     = "Abilities\\Weapons\\Arrow\\ArrowMissile.mdl"
            set m.arc       = GetRandomReal(0, bj_PI/4)
            set m.curve     = GetRandomReal(-bj_PI/4, bj_PI/4) 
            set m.owner     = GetTriggerPlayer()
            call thistype.launch(m)// Launch is implemented via module MissileStruct
        endmethod

    endstruct

endscope

Demo Code

I attached a demo map, which demonstrates how to use Missile.

Public resources using Missile.

Disclaimer

Known issues
  • parabolla function, makes speed setup hard for small distances.
  • Meteor like systems are hard to design with Missile, as x/y velocity of 0 is not allowed in the parabolla function.
  • Non-square destructables are difficult to detect, as it is not supported by Blizzard.
  • Destructables placed with a z offset or created via CreateDestructableZ will not be detected properly.
  • onMissile method is not supported. ( I'm on it )
  • Bad collision detection for high speed, while using a low collision member ( I'm on it )

Development

All versions from 1.4 to 1.5 of Missile seems to be stable and working. If you experience any bugs/error
please report them in the thread. I'm going to fix them as soon as possible.

To-do List:
  • Rectangle collision.

Changelog:
See comment #2
 
Last edited:
Level 12
Joined
Mar 13, 2012
Messages
1,121
If this is serious better call the thread and testmap "Missile 2.0" :).

Anyways, do you have any benchmarks?
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
Changelog

Version 1.0 - Version 1.4
  • Release ( undocumentated )
Version 1.4.1 ( 13.01.2016 )
  • Added filter so only units of type Missile_DUMMY_UNIT_ID will set their pitch angle via SetUnitAnimationIndex()
Version 1.4.2 ( 14.01.2016 )
  • Added static method onItem and static method onItemFilter.
  • Optimized the code a bit
  • Fixed widget collision ( item and destructable ). It works now properly.
  • Better safety for removing nodes from the core trigger.
  • Added method flyTime2Speed to covert for example 2 seconds into the proper missile speed.
Version 1.5 ( 16.01.2016 )
  • real speed does now accept also 0 and negative values.
    ---
  • real turn is working properly.
    ---
  • Filter functions for widget collision are now created in one array inside Missile and not in each struct.
    ---
  • Item and destructable collision are now much more methodologically sound.
    Therefore they are no longer optional toogles of Missile, but permanent enabled in Missile.
    ---
  • Widget collision is now handled via one global hashtable instead of a unit group array.
    This was inevitable for item and destructable collision.
    ---
  • createEx is again the main creator function. create and createXYZ are now wrappers to createEx.
    This slightly slows down create, but reduces overall code and speeds up createXYZ on the other hand.
    ---
  • added method setMovementSpeed takes real value returns nothing. Coverts movement speed to missile speed.
    ---
  • Debug options in DEBUG_MODE are better than ever.
    ---
  • Added method flyTime2Speed to covert for example 2 seconds into the proper missile speed.
    ---
  • I restructured the code to be more read-able.
    ---
  • The code documentation is now epic ( my opinion ).
Add stress test can be found here ( link )
Manual about collision types for missiles can be found here.

Version 2.0
  • [self=http://www.hiveworkshop.com/forums/jass-resources-412/missile-265370/index11.html#post2778028]Changelog[/self]
Version 2.0.1
  • Hotfix. You're losing now the origin location when using target=
    That was important, because otherwise all pitch angle and z calculation go nuts.
Version 2.0.2
  • Hotfix. Code was not compiling with library ErrorMessage, this is now fixed.
Version 2.0.2.1
  • Corrected some grammar mistakes.
  • Optimized & shortened to code minimal ( 3 - 5 lines )
  • Not using WorldBounds or BoundSentinel makes Missile switch from SetUnitX/Y to SetUnitPosition ( safety first ).
  • flightTime2Speed does no longer crash the thread if the passed in value is 0.
Version 2.0.2.2
  • Not longer uses SetUnitPosition in abscence of WorldBounds or BoundSentinel. Instead uses RectContainsCoords.
Version 2.0.3
  • Improved the protection of internal linked list of the Missile core trigger, to not fail in very seldom corner cases.
Version 2.1
  • Kicked out onDestructableFilter / onItemFilter from the Missile interface.
  • onItem / onDestructable are now even better and similar in mechanics to onCollide.
Version 2.3
  • Added more solidity against an OP limit case.
  • Missile shouts at you with an error message if you run into an OP limit situation.
Version 2.4
  • Fixed all flaws concerning target= ( hopefully )
Version 2.4.1
  • Hotfix in MissilePosition ensuring to never hit an OP limit.
Version 2.5
  • Previously enqueued, now new nodes are now pushed to the list of missiles to ensure better system stability.
  • Optimized widget collision a bit.
  • Removed collisionZ as stand-alone member. Considering the resolution of Warcraft III, it's fair enough to use one collision size for all three dimensions.
  • Missile motion runs now over a static unique list. It's faster and more methodologically sound than executing projectile motion per struct.
  • module MissileStruct has been split into module MissileLaunch, module MissileTerminate, module MissileAction, module MissileStruct.
    Nothing changes as module MissileStruct implements all, however you can now implement module MissileLaunch at the very top to prevent pseudo code generation.
Version 2.5.1
  • Previously private, real prevX, prevY and prevZ are now read-only.
  • terminate() has now a build in double free safety, hence is always safe to use.
  • Optimized the missile motion loop as good as possible.
  • Missiles can no longer collide with themself.
  • Added safety to missile motion.
 
Last edited:

Kazeon

Hosted Project: EC
Level 33
Joined
Oct 12, 2011
Messages
3,449
End-user optional method
JASS:
static method isCollide takes real xyDiff, real zDiff returns boolean
// xyDiff is distance between missile and the target
// zDiff is difference between missile's and target's height z

If the method exists, evaluate that method, if not, use that GetUnitBodySize.

Using the method then I can choose to have box or spherical collision or anything using the passed data.
JASS:
// Box collision
static method isCollide takes real xyDiff, real zDiff returns boolean
    return xyDiff < 100 and zDiff < 100
endmethod
or
JASS:
// Spherical collision
static method isCollide takes real xyDiff, real zDiff returns boolean
    return xyDiff*xyDiff+zDiff*zDiff < 10000
endmethod

And ultimately, I can have my own value for collision sizes. :)


onTerrain => onGround (just sounds better)
createXY => createXYZ (?)


JASS:
        // Maintain Dirac's API. ... Seriously this method is crap.
        static method createLoc takes AdvLoc originP, AdvLoc impactP returns thistype
            //! runtextmacro CREATE_MISSILE_DUMMY_UNIT("originP.x", "originP.y", "originP.z")
        endmethod
You are not obligated to serve that ultimate crap dinner right?


More things:
They are non constant
JASS:
        private integer             array   @INSTANCES@
        private TimerGroup32        array   @TIMER@
        private Missile             array   @STACK@
Just that actually.


JASS:
        static if LIBRARY_MissileRecycler then
            return createEx(GetRecycledMissile($X$, $Y$, $Z$, originP.angle*bj_RADTODEG), originP, impactP)
        
        elseif LIBRARY_Dummy and Dummy.create.exists then
            return createEx(Dummy.create($X$, $Y$, originP.angle*bj_RADTODEG).unit, originP, impactP)
        
        else
            return createEx(Missile.newMissileUnit($X$, $Y$, originP.angle*bj_RADTODEG), originP, impactP)
        endif
I didn't know elseif works for static if, does it? :/
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
- I like the isCollide idea. I'll add it as soon as possible. You wish to run that when a Missile is actually in collision range or on every loop ?
There are two members for collision which is collision and collsionZ.

- Yeah maybe onGround, onSurface, ... sounds better. Basically a check which can also be done in onPeriod. I will think about the best name.

- Yes createXYZ would be more precise.

- I keep createLoc, doesn't hurt too much.... sounds like location??...
some people maybe used it, so they don't have to change their code after c'n'p the new Missile library.

More things:
They are non constant
JASS:
        private integer             array   @INSTANCES@
        private TimerGroup32        array   @TIMER@
        private Missile             array   @STACK@
Just that actually.

- Why should they be constant? I don't think you can assign a value to a constant variable onInit.

- Yes I tested the elseif behaviour with static ifs.
 

Kazeon

Hosted Project: EC
Level 33
Joined
Oct 12, 2011
Messages
3,449
- I like the isCollide idea. I'll add it as soon as possible. You wish to run that when a Missile is actually in collision range or on every loop ?
There are two members for collision which is collision and collsionZ.
Evaluating the method only if there happens collision will just causing a couple more of evaluations (2x collision checks). And moreover, it bounds the user to only have maximum collision size of that default value. So, no, you better run it on every iterated widget on every tick.

- Why should they be constant? I don't think you can assign a value to a constant variable onInit.
No, I meant, capitalized names are for constant only, whereas those variables are not constant but you capitalized them all.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
isCollide can only run, if there is an unit set to target.

GetUnitBodySize is there to figure out the z height of a unit from toe to head,
otherwise collisionZ does not work properly. I think there is no data for exact values,
you have to make up your own (test to get appropriate values)
Furthermore you may want to have different value for different units (mountain giant, peon, ...)
... Or maybe have scaled units in your map.

Update 1.1
- changed XY to XYZ
- changed variables names according to the JPAG. --> camelCase.

Funny fact: KillUnit on units having locust, will make that unit be considered by GroupEnumUnit functions.
That's why I'm using ExecuteFunc and TriggerSleepAction to remove dummy units.

I agree that ground is a much more used word than terrain in english language.
However in wc3/editor/forum we use words like terrain board, terrain pallete, terraining, ....
that's why I think onTerrain is an adequate name to describe that type of collision.

There are some disclaimers with onDestructable.
  • does not work good for destructables created via CreateDestructableZ
  • does not work for destructables with no square pathing block (gates)
--> as destructables are not as good supported by natives like units,
the only way to evaluate a destructable collision is to check the
distance between the missile and widget. However this distance is always based on
the center of that widget and center of the missile.
If really required, you could detect a gate in case you know it's facing angle and the size of each pathing block (64*64).
--> using is Missile in RectAngle()
Another way would be to normalize coordinates, store them into a hashtable and define a collision there ( takes time).
Basically you check for the collision per tile.
 
Last edited:

Kazeon

Hosted Project: EC
Level 33
Joined
Oct 12, 2011
Messages
3,449
I think I understood the way you deal with collision a bit wrong. I thought you always set it to somekind of default value, but then I see you are allowing user to modify the collision size anytime without limitation, so there is no really something called maximum size. My bad.

So then, you can evaluate the method whenever a unit is in range (to every first of group).

if thistype.isCollide(range between missile and fog, Abs(missile z - fog z)) and thistype.onCollide(this, fog) then
...

isCollide can only run, if there is an unit set to target.
Hm? Are you sure? Why so?

You can evaluate it anytime if one or some units are in range from missile. Must be somewhere around this part:
JASS:
                                    call GroupEnumUnitsInRange(bj_lastCreatedGroup, this.x, this.y, this.collision + Missile_MAXIMUM_COLLISION_SIZE, null)
                                    loop
                                        set u = FirstOfGroup(bj_lastCreatedGroup)
                                        exitwhen u == null
                                        call GroupRemoveUnit(bj_lastCreatedGroup, u)
                                        if IsUnitInRange(u, this.dummy, this.collision) then 
                                            set collideZ = Missile_GetLocZ(GetUnitX(u), GetUnitY(u)) + GetUnitFlyHeight(u) - Missile_GetLocZ(this.x, this.y)
                                            if (collideZ + GetUnitBodySize(u) >= this.z - this.collisionZ) and (collideZ <= this.z + this.collisionZ) then
                                               if not (IsUnitInGroup(u, this.unitsHit)) and (u != this.target) and thistype.onCollide(this, u) then
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
1. onDestructableFilter was not recognized, because of a missing .exists
fixed.

2. Add a demo for detecting destructable with non square collision. However this method is not recommened.
Best would be to store these coordinated on normalizedXY values in a hashtable and check if the missile is within such a box.

Currently Missiles can't jump on one point, due to a flaw in Dirac's AdvLoc.
He didn't cover the case that two locations can have the same coordinates and therefore divides with 0.
You can work around this by changing x or y or one AdvLoc by a very small value.

Update:

I found two critical issues and will fix them as soon as possible
- Double free error, if using onDestructable and onCollide, while both return true during the same loop.
- recycle never runs, if onTerrain is declared and returns true before.

So I have to reconsider the priority of each method (which should run first)
 
Last edited:
Level 19
Joined
Mar 18, 2012
Messages
1,716
I updated the code, because I found another mistake in the original Missile which is now fixed.
  • Double Free Protection, when using various combinations of missile methods in one struct.
  • Theoretically you can now shoot any unit as Missile, it will then not be recycled. Just unliked once the Missile instance gets destroyed
  • Collision should now work even with curved Missiles, I will write a small snippet to check hit boxes with lightnings to see if it's working as intended. :)
 
Level 8
Joined
Feb 3, 2013
Messages
277
JASS:
// bpower
set m.speed = 15.625
set m.arc = .25
call launch(m)

// vexorian
call .launch(500, .25)

missile.gif


your pitch and arc calculation are a little weird..
i've tweaked with your demo a bit, and the pitch only gets considered when the arc becomes extreme. like +.5 arc or something

yours is the first, where the pitch is straight and vex is the one where the pitch is dynamic and smooth
final note, very nice demo! :>
 
Last edited:

Zwiebelchen

Hosted Project GR
Level 35
Joined
Sep 17, 2009
Messages
7,236
Thanks for all your work on missile!

Finally a bug-free version of missile is there for the masses.

I really wonder why we didn't get this before 2015, with all those great coders around here... maybe because all were busy coding stuff nobody needs? Dat Hive-style... ;)

Good job on keeping the API compatible to Dirac's Missile.
AdvLoc is a pain in the ass and I'd definitely support getting rid of it, but unfortunately, it would require to change all spells using the old missile to be changed. And that is probably a no-go. We want to encourage people to use this with minimal extra effort.

Funny fact: KillUnit on units having locust, will make that unit be considered by GroupEnumUnit functions.
That's why I'm using ExecuteFunc and TriggerSleepAction to remove dummy units.
Didn't you find a more elegant way? Both ExecuteFunc and TriggerSleepAction are terrible. ShowUnit(false) doesn't work either?
And why actually kill the missile? Just play the death animation and then remove it.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
Didn't you find a more elegant way? Both ExecuteFunc and TriggerSleepAction are terrible. ShowUnit(false) doesn't work either?
And why actually kill the missile? Just play the death animation and then remove it.

I didn't experiment so much with: how to remove a non recycle-able missile in the best way.
I though most people use either Dummy recycler, Dummy or xe(not supported since:( )

- Just removing will kill the death animation ( dirac did this)
- KillUnit will make it beeing enumerated by GroupEnumUnits
- AddTimedLife( I didn't try that one )
- Keep on iterating without running extra code, until the death animation is over ( could be best solution )
- deallocate + ExecuteFunc + TriggerSleepAction + RemoveUnit ( I did this )

There are still some issues in this version of Missile:

- pitch angle thing
- correct order of how the method are called. (onCollide before onDestructable? .. etc)
- onDestructable, onCollide do not run on the last tick ( onFinsh ) ingame that can look really strange.
- i didn't look into deflect and bounce yet, maybe they need a fix aswell

These are small things but I will try to fix them aswell

Edit:

I wrote a recycler for destroyed dummy units. I think it's the best solution.
 
Last edited:
Level 19
Joined
Mar 18, 2012
Messages
1,716
TO DO:

- RecycleBin for delayed dummy removal

- Integrate more dummy recycling resources.
--> Currently supported: Dummy, DummyRecycler, xedummy
--> write if you wish your system to be integrated.
--> Requirements: public approved, angle sorting

- Correct pitching angle
--> Pitch angles may freak out over uneven terrain (I'm on it)

- Best method order for the MissileStruct
--> Currently there are some flaws

- Better destructable detection
--> Quite hard, as destructables are shitty supported by blizzard

- Jump in place to allow for example Meteors
--> Here i have to work around AdvLoc and Loc from dirac ...

- Make Missile Popular
i've tweaked with your demo a bit, and the pitch only gets considered when the arc becomes extreme. like +.5 arc or something
xe has the same behaviour on uneven terrain

arc should take values in range of -PI/2 and PI/2. Everything in Missile works with radians
 
Last edited:
Level 19
Joined
Mar 18, 2012
Messages
1,716
It will start to follow the target to its new position.
target= make a Missile a homing missile.

I will add a demo for that aswell soon.

EDIT:

First of all I will remove Loc and AdvLoc from Missile, just because they suck.
Sorry for those who create Missiles via Missile.createLoc/Ex(unit, loc1, loc2)
It breaks the thread, if distance between two Locs is 0. (Can happen)
I will simply integrate a small MissilePosition struct into the Missile library.
Don't think I'm not a fan of high modularity ... modules, macros, sharing code all over a map. But these two
finally make more trouble than they help.

I added the options to set speed via fly duration. The function is not working properly yet when
you also add an acceleration, but I will fix that soon.

I would like to change module MissileStruct in that way, that ...
- onCollide
- onDestructable/Filter
... are called even when this.recycle is set to true. Dirac did not call onCollide in this case.

Quick variable/case explanation:
Missile(this).recycle is set to true, when a missiles fly distance is bigger or equal
to the total distance it should fly. Basically on the tick it finishes its course.
Since Missile works with a 1/32 timeout and a (constant) speed (not vector), it is
inaccurate to not call onCollide and onDestructable on the last tick.

That is why I want to move onCollide and onDestructable to the top (below wantDestroy) and call them
before onFinish is called. onPeriod and onTerrain still stay exluded if recycle is set to true.

onPeriod is not important in an onFinish case and onTerrain most times will be true in case of onFinish....

I always wish to write an algorithm to easily detect non square destructables.... :)
 
Last edited:

Zwiebelchen

Hosted Project GR
Level 35
Joined
Sep 17, 2009
Messages
7,236
Try not to convolute the system too much by adding too much new variables and methods. The cool thing about the original Missile was that it's API was rather basic.

I didn't experiment so much with: how to remove a non recycle-able missile in the best way.
I though most people use either Dummy recycler, Dummy or xe(not supported since:( )

- Just removing will kill the death animation ( dirac did this)
- KillUnit will make it beeing enumerated by GroupEnumUnits
- AddTimedLife( I didn't try that one )
- Keep on iterating without running extra code, until the death animation is over ( could be best solution )
- deallocate + ExecuteFunc + TriggerSleepAction + RemoveUnit ( I did this )
After thinking about this again, actually, I prefer Dirac's implementation of instant removal of the missile.

Instant removal without death animation gives us the power to customize the death animation of our missile. What if I want my shadowbolt spell not to use the default death animation of deathcoil?
Thinking about timed removal is a waste of effort. People can just play a special effect onCollide or onDestroy, like they did with the original system.


Beware of the destructable thing, though: make the whole destructable add-in optional. I assume that most people won't ever use the destructable feature, so make sure it doesn't slow the system down by adding enough static ifs.


You have my vote for removing AdvLoc, though. I also think that fewer dependencies are better.
Actually, I would remove CTL aswell. CTL kind of fixes a problem that never existed in the first place.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
Hey, is it safe to use this now? I hope you will finish (fix all major issues) soon

Maybe after today.

After thinking about this again, actually, I prefer Dirac's implementation of instant removal of the missile.
I think I can include it into onRemove with a static if.

JASS:
static if thistype.onRemove.exists then
    static if ENABLE_RECYCLE_DUMMY_DELAYED then// global setup, so the whole timed recycling can be exluded.
        if thistype.onRemove(Missile(this)) then// if true
            call RecycleDelayed(dummy, CONSTANT_TIME)
        else
            call RemoveUnit(dummy)// RecycleMissile(dummy) 
        endif
    else
        call RemoveUnit(dummy)// No delayed recycling code at all in your map.
    endif
endif
Would that be better?

---
Actually, I would remove CTL aswell. CTL kind of fixes a problem that never existed in the first place.
I like CTL, but we can argue about that, once everything else is working correctly.

I'm not adding this, but with small adaptations you can do things like:
GetMissilesOfType(Fireball) returns stack[Fireball.typeid].first :p

What is currently not possible is:
GetAllMissiles() returns stack[].first
 

Kazeon

Hosted Project: EC
Level 33
Joined
Oct 12, 2011
Messages
3,449
It's so excellent that it only requires CTL. But please consider this, TimerUtils is more widely used, since it's more versatile and can be used in more various cases, e.g. non-periodic timer. Means there is higher chance that users (map makers) already have/use TimerUtils in their maps. I highly recommend you to replace CTL, that doesn't mean that CTL is worse or bad. And that's personal opinion after all.

This should have been done years ago imho. Good luck in finishing this BPower.

EDIT:
JASS:
implement optional MissileCollisionZConditionHead
I'm curious how the module looks like? Do you give example in the demo code or something?
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
You can't do
JASS:
if hi then 
    static if hi2 then
        if
    endif
        if hi3 then
        endif
    static if hi2 then
        endif
    endif
endif
So I have to use either a macro or a module.
JASS:
    // I can't include a single endif via static if
    // So I'm using a high level macro/module.
    static if Missile_USE_COLLISION_Z_FILTER then
        module MissileCollisionZConditionHead
            set collideZ = Missile_GetLocZ(GetUnitX(u), GetUnitY(u)) + GetUnitFlyHeight(u) - Missile_GetLocZ(this.x, this.y)
            if (collideZ + GetUnitBodySize(u) >= this.z - this.collisionZ) and (collideZ <= this.z + this.collisionZ) then
        endmodule
       
        module MissileCollisionZConditionEnd
            endif
        endmodule
    endif

TimeUtils is very nice for not too many simultaneous instances.
However CTL outperforms TimerUtils, when you have let say 150 instances running.
If you increase the number TimerUtils will run at 5-10 fps while CTL is at let say 38-45
Numbers are made up, but you understand what I mean.
!! Does not mean CTL is better, it works just different and has its own benefits !!

What I could do is write a CTL like timer system just for Missile. That would actually be the best,
because now CTL evaluates a triggercondition per registered struct and each struct
calls move() for their own Missile stack. So we move Missile stacks instead of all Missiles at once.

Having an integrated own timer system would allow to move all missiles first and the evaluate every struct.
Also as triggercondition afterwards.
Once you start the first active instance of a struct, add the triggercondition to the trigger and fire it periodically.
Once the last active instances is destroyed remove that triggercondition.
 
Level 3
Joined
Jun 25, 2011
Messages
36
And can't you just use 1 single timer for all instances? So the library requires TimerUtils and the performances are ok. It's not like there will be 150 ressources like that in a map. And the performances aren't the most important thing when you map.
Personally I will never use that ressource if it requires useless ressources like CTL (and I think I am not the only one on the hive).
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
And can't you just use 1 single timer for all instances?
CTL is just running with one timer. So Missile also runs on a single timer
It's not like there will be 150 ressources like that in a map.
You miss the point of TimerUtils here. You use TimerUtils, if you wish
A) attach data to a timer and B) use one timer per instance.
If you want static timer, just declare it directly in your library/struct.
You can decrease the initial timer stack size of TimerUtils by 1, if you worry about handle management :p.

The only other opion I have is, to write a CTL like system directly into Missile.
I will do this, once I decided if I want to give the option to users for a onMissile( Missile collides Missile) method. If there is onMissile then there will be that timer system.

Problems to think about, when doing that.
  • Requires additional data structure. (One extra struct for all Missiles)
  • Will be faster than CTL, when different typeid of Missiles are in the air.
  • The faster, the more missiles are in air during the same time.
  • Will be slower, if only one typeid onf Missile is used in your map
  • Allows onMissile
  • Allows GetAllMissiles() and GetAllMissilesOfType(Fireball.typeid), ForMissilesInRangeDo()// Maybe the last one.
  • Finally: How to detect Missile-Missile collision. (Big ?)

Looks like this:
1. Create boolexpr onInit for each struct. --> boolexpr[thistype.typeid]
2. Fire first missile of typeid --> TriggerAddCondition(Missile_trigger, boolexpr[typeid]) returns triggerconidtion (will be safed)
3. Destroy last missile of typeid --> TriggerRemoveCondition(Missile_trigger, condition[typeid]
4. onPeriod --> iterate over all Missiles, then fire Missile_trigger
5. would look like moveMissile(a, b, c, ...), fireTrigger(a, b, c, ...

Currently Missile iterates seperate for each struct and then fires a trigger just for that struct.
Move(a) fire(a), move(b) fire(b)
 

Zwiebelchen

Hosted Project GR
Level 35
Joined
Sep 17, 2009
Messages
7,236
I support the idea of hardcoding the timers into the system.

I don't see the need for anyone to ever use a timer system. They are mostly dead weight. If you want timer attachment, just use a single global hashtable for your entire map and you're done.
If you need a periodic timer, there's no reason not to have one per system.
And the fact that there are dozens of timer systems out there doesn't really support my idea of modular code either.

About onMissile:
I see the reason why you want that... but it also adds a lot of headache. Make sure it does not artifically slow down the rest of the system if it is not used.

I prefer a clean and simple missile system over one obstructed with too many features any day. I have no problem having this as an optional toggle, but only if it doesn't have an impact on system performance.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
Conclusion first (for lazy people): I won't do a change in the data structure, until there
is a valid argument why I should do so. I can replace CTL if people want me to.

First of all: Any changes I do, won't change the system from the user aspect.
User code evaluates the same way, as written in the first place and will always do so.
There will be no change in the API (maybe static if optional additions)

About onMissile:
I see the reason why you want that... but it also adds a lot of headache. Make sure it does not artifically slow down the rest of the system if it is not used.

The only difference is the data structure. I can't tell which is better (perfomance wise)
I quickly demonstrate what I mean and also point out how Dirac solved it.

Dirac:
1. Each struct has a local LinkedList and Missile has a global LinkedList (extra struct called Missiles).
2. Missile.create() <-- new Missile instance.
3. add that instance to the local list and the global list
4. iterate over the struct list, while calling moveMissile()
5. iterate again, evaluating the methods declared inside the struct.
6. destroy removes the node from both List (global and local)

7) onMissile itereates then over the global LnkedList ( no other use for that list)

Me:
1. Same as Dirac but no Global LinkedList, so we don't need that extra logic which only onMissile needs.

My idea with integrated timer system:
1. local and global LinkedList (more overhead, coz of twice calling enqueue()/remove()
2. iterating over the global list -> "active" structs - 1 times less function calls of moveMissle()
3. fire trigger (like CTL)
4. iterate over the local lists.
5. destroy also requires the removal from local and global list

From an user aspect nothing changes. Your code works the same as before.
But from coders side, there are some facts to think about.

Pro/Contra:
- Does not need CTL (pro)
- Can support onMissile (pro?), in general more features. (pro?)
- May be faster, but only if many missiles are allocated (not sure, can't benchmark, pro?)
- More logical, as we move all missiles first and the evaluate declared struct methods. (pro)
- But slower on allocate/deallocate (contra)
- Extra code, extra logic (contra)

I can also remove CTL and write something similar into the Missile script (little bit less code maybe).

I'm not a timer system expert, so I don't know if this code is fail save.
If yes that's the alternative for CTL
JASS:
    private function Fire takes nothing returns nothing
        call TriggerEvaluate(core)
    endfunction
    
    private function MissileCreateExpression takes integer structId, code c returns nothing
        set expression[structId] = Condition(c)
    endfunction
    
    // Start timer 1/32 if not already running.
    private function StartPeriodic takes integer structId returns nothing
        if (instances[structId] == 0) then
            set condition[structId] = TriggerAddCondition(core, expression[structId])
            set enabled[structId]   = true
            if (active == 0) then
                call TimerStart(clock, 0.031250000, true, function Fire)
            endif
            set active    = active + 1
        endif
        set instances[structId] = instances[structId] + 1
    endfunction
    
    // And stops it.
    private function StopPeriodic takes integer structId returns nothing
        set instances[structId] = instances[structId] - 1
        if (instances[structId] == 0) and enabled[structId] then
            set active = active - 1
            if (active == 0) then
                call PauseTimer(clock)
            endif
            call TriggerRemoveCondition(core, condition[structId])
            set condition[structId] = null
            set enabled[structId]   = false
        endif
    endfunction
 
Last edited:
Level 19
Joined
Mar 18, 2012
Messages
1,716
I replaced CTL with an internal timer. The test map still has both (with CTL and without) libraries.
You now can do one more global setup, which is TIMER_TIMEOUT (by default 0.03125 == 1/32)
JASS:
/*************************************************************************************
*
*   */ requires /*
*
*       - Missile requires nothing
*
***********************************************************************/

I should work bug free.
However I'm not a timer expert, so I would like to consult Nestharus and ask if there
is something which has to be changed.

The future of the Missile data structure and onMissile method is unknown (even to me :D)
 

Zwiebelchen

Hosted Project GR
Level 35
Joined
Sep 17, 2009
Messages
7,236
About your code changes:
Remember that you are creating, moving and removing units.
This basicly overshadows any performance changes in data structure like a hundred times. I'd say, go with what you would regard as the "cleanest" or "most elegant" code.
Optimizing missile systems for speed is a pointless effort. They are always slow.

... unless your optimization changes performance drastically (like O(n) operation to O(log n)), there's no reason to do it.
Clean code trumps performance.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
I think the same way. For now I will leave it as it is, just because it works as intended.
-----------------

turn= didn't work ( also not in Dirac's ), because he set the first facing angle ( cA in Missile ) to the angle between origin and impact ( called fly angle below).
turn can't be applied when Cos(Radians(unit facing) - fly angle) is 1, Cos(0).

Therefore I added 2 methods to create Missiles with different first facing angle.
--> static method createFacing(x, y, z, angle, distanceToFly, z1, facingAngle)
--> static method createFacingXYZ(x, y, z , x1, y1, z1, facingAngle)

You can now create a Missile with a fly angle of let's say 0, but emerges from the caster in a 45* degree angle.
--> call createFacing(10, 10, 50, 0, 1000, 50, 45)

Missile will throw an DEBUG_ONLY eror onInit if your setup for the DUMMY_UNIT_ID is incorrect.
Missile will throw an DEBUG_ONLY error onInit if your setup for MAXIMUM_COLLISION_SIZE is below zero.
 

Zwiebelchen

Hosted Project GR
Level 35
Joined
Sep 17, 2009
Messages
7,236
I think the same way. For now I will leave it as it is, just because it works as intended.
-----------------

turn= didn't work ( also not in Dirac's ), because he set the first facing angle ( cA in Missile ) to the angle between origin and impact ( called fly angle below).
turn can't be applied when Cos(Radians(unit facing) - fly angle) is 1, Cos(0).

Therefore I added 2 methods to create Missiles with different first facing angle.
--> static method createFacing(x, y, z, angle, distanceToFly, z1, facingAngle)
--> static method createFacingXYZ(x, y, z , x1, y1, z1, facingAngle)

You can now create a Missile with a fly angle of let's say 0, but emerges from the caster in a 45* degree angle.
--> call createFacing(10, 10, 50, 0, 1000, 50, 45)

Missile will throw an DEBUG_ONLY eror onInit if your setup for the DUMMY_UNIT_ID is incorrect.
Missile will throw an DEBUG_ONLY error onInit if your setup for MAXIMUM_COLLISION_SIZE is below zero.

Not sure if I understood correctly... will this create a curved missile trajectory?
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
The most generic method creating a Missile is Missile.create
--> Creates the dummy at point origin facing point impact.
--> Dummy facing angle equals angle between impact and origin

The problem is that a missile already facing it's impact, can't use turn= as argument.
So whatever you enter for turn= , nothing will happen.
(Cos(cA - a) < Cos(turn)) Can't be true as Cos(cA - a) is 1, because cA - a = 0.

To use turn= you need a dummy, which is not facing it's impact, so it can "turn" to it.
Like emerging from the side of the caster, than from the front.

CreateFacing will create a dummy which may have in the beginning a different facing than it's fly direction.
The slowly changes it's facing and, using turn=
JASS:
                if (0 != turn) and (Cos(cA - a) < Cos(turn)) then// cA is the dummy facing in radians, a angle between origin and impact
                    if (Sin(a - cA) >= 0) then
                        set cA = cA + turn
                    else
                        set cA = cA - turn
                    endif
                else
                    set cA = a
                endif
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
Well Dirac's Missile supports turn=, so I also use it to ensure backwards compatibility.
But no matter what you do in Dirac's, you can't use turn=.

Every period for every Missile it checks for (turn!=0), but since it does nothing until now,
this was just unnessesary overhead.

I just gave the option to use it.

I saw a spell in the spell section ( Icewave ) by Quilnez. The eyecandy is nice and he did what turn= is supposed to do.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
TO DO:

1. Going to check, if the missile fly height is set 100% correct ( I have some doubts here )

2. I want to reconsider turn= and open=, because in it's current state it's too difficult to set up.

3. Math optimizing, however I will stay with the parabola function.
The benefit of less +×/ operators within the loop doesn't improve so much the overall performance. Don't change a working system.
I'm still unsure if it's better to apply a fix speed to a missile or a given time to travel a distance.
The second is much more handy, when dealing with very short distances.
Another math may allow dummies to jump in place and the creation of meteor like projectiles.
I'm on it!!

4. Erase all the grammar mistakes I made in the comments ( quite alot )

I want to publish a demo map in form of a spell submission in the spell section to adress a bigger part of the
community. This also helps me to improve Missile in terms of potential critical errors ( nothing known )
and making the application interface as generic and easy for users as possible.
Demo code is always very helpful to understand the very basics of a ( new ) system.

Feedback is always very welcome.
 
Level 11
Joined
Dec 3, 2011
Messages
366
TO DO:

1. Going to check, if the missile fly height is set 100% correct ( I have some doubts here )

2. I want to reconsider turn= and open=, because in it's current state it's too difficult to set up.

3. Math optimizing, however I will stay with the parabola function.
The benefit of less +×/ operators within the loop doesn't improve so much the overall performance. Don't change a working system.
I'm still unsure if it's better to apply a fix speed to a missile or a given time to travel a distance.
The second is much more handy, when dealing with very short distances.
Another math may allow dummies to jump in place and the creation of meteor like projectiles.
I'm on it!!

4. Erase all the grammar mistakes I made in the comments ( quite alot )

I want to publish a demo map in form of a spell submission in the spell section to adress a bigger part of the
community. This also helps me to improve Missile in terms of potential critical errors ( nothing known )
and making the application interface as generic and easy for users as possible.
Demo code is always very helpful to understand the very basics of a ( new ) system.

Feedback is always very welcome.
Does the missle's fly height work correctly when it's flying over a flatform or oversea, cliff, I wonder. I think you can stick this system with GetZ Library.
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
Yes it does maintain the correct fly height over platforms etc.
Shallow and deep water does not look smooth. There might be a fix, but it costs
unwanted overhead. Water created via raise/decrease looks smooth though.

Why do I need GetZ library, when Missile already covers everything that library offers?
You clearly didn't look into my code :)

What I ment is that if the max z to reach is for instance 350, the missile may only get to e.x 287.
 
Level 11
Joined
Dec 3, 2011
Messages
366
Yes it does maintain the correct fly height over platforms etc.
Shallow and deep water does not look smooth. There might be a fix, but it costs
unwanted overhead. Water created via raise/decrease looks smooth though.

Why do I need GetZ library, when Missile already covers everything that library offers?
You clearly didn't look into my code :)

What I ment is that if the max z to reach is for instance 350, the missile may only get to e.x 287.

I cant test map.

I cant save with my JNG and test it.

When I save, it returns
Syntax Errors:
Function passed to Filter or Condition must return a boolean.

That's why I can't know exactly. I've use Dirac's missle, It cause bug with the flatform (sometimes).
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
I check later if the testmap isn't up to date.

I know which line causes the error for you.
It origins from the periodic function of the MissileStruct module,
as it is registered as boolexpr to the core trigger of Missile.
In a previous version it had to return false to compile properly,
but I worked around by passing it to another function first.

As mentioned I gonna check it later and fix what has to be fixed.

Edit: For you it doesn't compile, because you have Cohadars JassHelper checked instead of
Vexorians.


There are two possibilties for you:
1. Switch to Vexorians JassHelper, which is nowadays recommended by THW. ( I would do that )

2. Go to library Missile --> scroll down until the module MissileStruct --> find static method missileIterateP takes nothing returns nothing
--> change it to static method missileIterateP takes nothing returns boolean --> go to the last line of that method ( after set u = null )
write and return false there.
 
Last edited:
Level 11
Joined
Dec 3, 2011
Messages
366
I check later if the testmap isn't up to date.

I know which line causes the error for you.
It origins from the periodic function of the MissileStruct module,
as it is registered as boolexpr to the core trigger of Missile.
In a previous version it had to return false to compile properly,
but I worked around by passing it to another function first.

As mentioned I gonna check it later and fix what has to be fixed.

Edit: For you it doesn't compile, because you have Cohadars JassHelper checked instead of
Vexorians.


There are two possibilties for you:
1. Switch to Vexorians JassHelper, which is nowadays recommended by THW. ( I would do that )

2. Go to library Missile --> scroll down until the module MissileStruct --> find static method missileIterateP takes nothing returns nothing
--> change it to static method missileIterateP takes nothing returns boolean --> go to the last line of that method ( after set u = null )
write and return false there.

I use JH 0.A.2.B ...

I think the second solution is fine :ogre_haosis:
 
Top