1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  4. Travel to distant realms and encounter scenes unknown to the common folk. The Greatest of Adventures is upon us with the 8th Cinematic Contest. Join in on a fun ride.
    Dismiss Notice
  5. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  6. Contestants are to create a scene set in the Stone Age. Come and see what you can come up with. We wish you the best of luck!
    Dismiss Notice
  7. Colour outside the lines! Techtree Contest #13 is a go. The contest is optionally paired.
    Dismiss Notice
  8. Greetings cerebrates, our Swarm needs new spawners that will have numerous children. Join the HIVE's 31st Modeling Contest - Spawners and Spawned! The contest is optionally paired.
    Dismiss Notice
  9. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

Small Code Snippets

Discussion in 'Triggers & Scripts' started by PurplePoot, Sep 29, 2007.

  1. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    397
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Yes I havent thought about that. I'll be posting it once I'm on my PC. I'm currently on mobile.
     
  2. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    397
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Counted Random Units Iteration

    Code (vJASS):

    library RandomIteration

        globals
            private group tempGroup = CreateGroup()
            private integer unitCount = 0
        endglobals

        private function EnumUnits takes nothing returns boolean
            call GroupAddUnit(tempGroup, GetFilterUnit())
            set unitCount = unitCount + 1
            return false
        endfunction

        function EnumRandomUnitsInRangeCounted takes group g, real x, real y, real radius, integer limit returns nothing
            local real chance
            local unit u
            call GroupEnumUnitsInRange(g, x, y, radius, Filter(function EnumUnits))
            set chance = I2R(limit)/I2R(unitCount)
            loop
                set u = FirstOfGroup(tempGroup)
                exitwhen u == null or limit == 0
                call GroupRemoveUnit(tempGroup, u)
                if GetRandomReal(0, 1) <= chance then
                    call GroupAddUnit(g, u)
                    set limit = limit - 1
                endif
            endloop
        endfunction

    endlibrary
     
     
  3. jakeZinc

    jakeZinc

    Joined:
    Aug 13, 2013
    Messages:
    1,373
    Resources:
    20
    Spells:
    20
    Resources:
    20
    Must take a boolean parameter to add from filtering that prevents dead units or somewhat to be picked.
     
  4. Wietlol

    Wietlol

    Joined:
    Aug 1, 2013
    Messages:
    4,653
    Resources:
    3
    Spells:
    3
    Resources:
    3
    Why?
    Just why?

    function FilterRandomUnitsCounted takes group g, integer count returns group (optional)
    (Remove units from the given group and return the group.)

    I mean, if I wanted to get the units from a rect, or filter them first by something else, or even if I already have an existing group that has units.
    In any of those cases, your function is pretty much useless. So you have to separate the two functionalities,
    1 pick units in range
    2 filter random units

    The first one, we already have, the second one, that is what you have to create.

    Also, you leak a unit handle "exitwhen limit == 0"

    You should have that check after lowering the limit:
    if limit == 0 then
    set u = null
    exitwhen true
     
  5. Aniki

    Aniki

    Joined:
    Nov 7, 2014
    Messages:
    517
    Resources:
    4
    Spells:
    1
    JASS:
    3
    Resources:
    4
    @Wietlol, @AGD it would be nice if the function would work correctly as well =), there is a working one in this thread.
     
  6. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    397
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Okay, I'm too lazy to update that randomiterator now, for now lets have this.

    Code (vJASS):

    //Requires WorldBounds
    //! textmacro IMPLEMENT_BOUNDS takes X, Y
    if $X$ < WorldBounds.minX then
        set $X$ = WorldBounds.minX
    elseif $X$ > WorldBounds.maxX then
        set $X$ = WorldBounds.maxX
    endif
    if $Y$ < WorldBounds.minY then
        set $Y$ = WorldBounds.minY
    elseif $Y$ > WorldBounds.maxY then
        set $Y$ = WorldBounds.maxY
    endif
    //! endtextmacro
     


    Doesn't need an extra function call but creates spaghetti code =P

    EDIT: It doesn't have to be in a library after all
     
    Last edited: Sep 8, 2016
  7. Wietlol

    Wietlol

    Joined:
    Aug 1, 2013
    Messages:
    4,653
    Resources:
    3
    Spells:
    3
    Resources:
    3
    Textmacros are ugly though.
    In theory, everything can be done with textmacros, but it is horrible to do so.
     
  8. lang_dye

    lang_dye

    Joined:
    Mar 7, 2016
    Messages:
    5
    Resources:
    0
    Resources:
    0
    I agree with it,but the main question is that it is a library,many codes will quote it,then if different code have different boolean,what boolean should I to add from fitering?
     
  9. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    397
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Credits goes to Vexorian. I just rewrote his script and made it shorter using zinc.
    Code (vJASS):
    //! zinc
    library BoolexprUtils {

        public boolexpr BOOLEXPR_TRUE, BOOLEXPR_FALSE;

        module Init {
            static method onInit() {
                BOOLEXPR_TRUE = Filter(function() -> boolean {return true;});
                BOOLEXPR_FALSE = Filter(function() -> boolean {return false;});
            }
        }

        struct S extends array {module Init;}

    }
    //! endzinc
     
    Last edited: Nov 8, 2016
  10. Wietlol

    Wietlol

    Joined:
    Aug 1, 2013
    Messages:
    4,653
    Resources:
    3
    Spells:
    3
    Resources:
    3
    why the hell does it have to be inside a module?
     
  11. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    397
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Not so sure but just to avoid risks ;)
     
  12. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,860
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    LMAO why is it inside a module.

    If you are going to use a faster initialization, there is another way to do that.
     
  13. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    397
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    What is it?
     
  14. Quilnez

    Quilnez

    Joined:
    Oct 12, 2011
    Messages:
    3,250
    Resources:
    37
    Icons:
    2
    Tools:
    1
    Maps:
    7
    Spells:
    21
    Tutorials:
    2
    JASS:
    4
    Resources:
    37
    Most people do not realize amphibious units are not the same as ground unit. This is the condition to check it:

    1. Checks if unit is amphibious
    Code (vJASS):
    function IsUnitAmphibious takes unit u returns boolean
        return not (IsUnitType(u, UNIT_TYPE_GROUND) or IsUnitType(u, UNIT_TYPE_FLYING))
    endfunction


    2. Checks if unit is ground or amphibious:
    Code (vJASS):
    function IsUnitGroundOrAmphibiousFail takes unit u returns boolean
        @ return IsUnitAmphibious(u) or IsUnitType(u, UNIT_TYPE_GROUND) <= not nerdly efficient@
    endfunction

    use this instead
    Code (vJASS):
    function IsUnitGroundOrAmphibious takes unit u returns boolean
        return IsUnitType(u, UNIT_TYPE_GROUND) or not IsUnitType(u, UNIT_TYPE_FLYING)
    endfunction


    3. Checks if unit is flying or amphibious (will be rarely useful):
    Code (vJASS):
    function IsUnitFlyingOrAmphibious takes unit u returns boolean
        return IsUnitType(u, UNIT_TYPE_FLYING) or not IsUnitType(u, UNIT_TYPE_GROUND)
    endfunction
     
    Last edited: Nov 24, 2016
  15. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,165
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Maybe someone will find a use, as well:

    Code (vJASS):

    struct Repair extends array
        public static constant integer HUMAN_SPELL           = 'Ahrp'
        public static constant integer HUMAN_ORDER           = 852024
        public static constant integer HUMAN_ORDER_ON        = 852025
        public static constant integer HUMAN_ORDER_OFF       = 852026
       
        public static constant integer ORC_SPELL             = 'Arep'
        public static constant integer ORC_ORDER             = 852024
        public static constant integer ORC_ORDER_ON          = 852025
        public static constant integer ORC_ORDER_OFF         = 852026
       
        public static constant integer NIGHT_ELVES_SPELL     = 'Aren'
        public static constant integer NIGHT_ELVES_ORDER     = 852161
        public static constant integer NIGHT_ELVES_ORDER_ON  = 852162
        public static constant integer NIGHT_ELVES_ORDER_OFF = 852163
       
        public static constant integer UNDEAD_SPELL          = 'Arst'
        public static constant integer UNDEAD_ORDER          = 852202
        public static constant integer UNDEAD_ORDER_ON       = 852203
        public static constant integer UNDEAD_ORDER_OFF      = 852204
       
        public static method IsRepairAbility takes integer id returns boolean
            return id == HUMAN_SPELL or id == ORC_SPELL or id == NIGHT_ELVES_SPELL or id == UNDEAD_SPELL
        endmethod
       
        public static method IsRepairOrder takes integer id returns boolean
            return id == HUMAN_ORDER or id == ORC_ORDER or id == NIGHT_ELVES_ORDER or id == UNDEAD_ORDER
        endmethod
       
        public static method IsRepairOrderOn takes integer id returns boolean
            return id == HUMAN_ORDER_ON or id == ORC_ORDER_ON or id == NIGHT_ELVES_ORDER_ON or id == UNDEAD_ORDER_ON
        endmethod
       
        public static method IsRepairOrderOff takes integer id returns boolean
            return id == HUMAN_ORDER_OFF or id == ORC_ORDER_OFF or id == NIGHT_ELVES_ORDER_OFF or id == UNDEAD_ORDER_OFF
        endmethod
    endstruct
     
  16. Pyrogasm

    Pyrogasm

    Joined:
    Feb 27, 2007
    Messages:
    2,794
    Resources:
    1
    Spells:
    1
    Resources:
    1
    For getting locusted units in range/of player

    Code (vJASS):
    library LocustUnits initializer init //has optional GroupUtils
        globals
            private boolexpr filt
            private integer ID = 'Aloc'
        endglobals
       
        private function IsLocust takes nothing returns boolean
            return GetUnitAbilityLevel(GetFilterUnit(), ID) > 0
        endfunction
       
        function GroupAddLocustUnitsOfPlayer takes group TarG, player TarP returns nothing
            call GroupEnumUnitsOfPlayer(TarG, TarP, filt)
        endfunction
       
        function GroupAddLocustUnitsInRange takes group TarG, real TarX, real TarY, real R, boolexpr someFilter returns nothing
            local integer i = -1
            local unit u
            local group g
            local real x
            local real y
            set R = R*R
           
            static if LIBRARY_GroupUtils then
                set g = NewGroup()
            else
                set g = CreateGroup()
            endif
           
            loop
                set i = i+1
                exitwhen i > 15
                call GroupEnumUnitsOfPlayer(g,Player(i),And(filt, someFilter))
               
                loop
                    set u = FirstOfGroup(g)
                    exitwhen u == null
                    call GroupRemoveUnit(g,u)
                   
                    set x = TarX-GetUnitX(u)
                    set y = TarY-GetUnitY(u)
                    if x*x+y*y <= R then
                        call GroupAddUnit(TarG,u)
                    endif
                endloop
            endloop

            static if LIBRARY_GroupUtils then
                call ReleaseGroup(g)
            else
                call DestroyGroup(g)
            endif
            set g = null
            set u = null
        endfunction
       
        private function init takes nothing returns nothing
            set filt = Filter(function IsLocust)
        endfunction
    endlibrary
     
  17. AGD

    AGD

    Joined:
    Mar 29, 2016
    Messages:
    397
    Resources:
    13
    Spells:
    7
    Tutorials:
    1
    JASS:
    5
    Resources:
    13
    Maybe this can be useful =)
    Code (vJASS):
    //! zinc
    library ExitWarcraft { //Credits: Aniki

        //! novjass
        /*=========== API ===========*/
            function ExitWarcraft()
        /* Exits the Warcraft 3 Game */
        /*===========================*/
        //! endnovjass

        public function ExitWarcraft() {ExecuteFunc("ExitWarcraft");}

    }
    //! endzinc
     
     
    Last edited: Dec 19, 2016
  18. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,165
    Resources:
    22
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    4
    JASS:
    3
    Resources:
    22
    Submission: ExitWarcraft
    Date: 18 December 2016
    Status: Approved
    Note:

    Nic3, intuitive AP1. Condition over 4ction us3d, pr0! ThoUgh, maYbe GUI support can be added to run the trigger. >_>


    I actually wanted to be cool, and make a crash without too much repetive actions, but failed horribly.
     
  19. Aniki

    Aniki

    Joined:
    Nov 7, 2014
    Messages:
    517
    Resources:
    4
    Spells:
    1
    JASS:
    3
    Resources:
    4
    Recursion can be deadly =):
    Code (vJASS):

    function crash takes nothing returns nothing
       call ExecuteFunc("crash")
    endfunction
     
     
  20. DracoL1ch

    DracoL1ch

    Joined:
    Dec 12, 2010
    Messages:
    1,758
    Resources:
    2
    Tutorials:
    2
    Resources:
    2
    Why not Player(-1)?