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.

[Snippet] IsDestructableTree

Discussion in 'JASS Resources' started by BPower, Feb 10, 2014.

  1. BPower

    BPower

    Joined:
    Mar 18, 2012
    Messages:
    1,741
    Resources:
    21
    Spells:
    15
    Tutorials:
    1
    JASS:
    5
    Resources:
    21
    Easily distinguish between trees and other types of destructables.

    For best optimization make sure the harvester unit doesn't get indexed by
    your unit indexing system ( in case you use one in your map )
    Code (vJASS):
    library IsDestructableTree uses optional UnitIndexer /* v1.3.1
    *************************************************************************************
    *
    *   Detect whether a destructable is a tree or not.  
    *
    ***************************************************************************
    *
    *   Credits
    *
    *       To PitzerMike
    *       -----------------------
    *
    *           for IsDestructableTree
    *
    *************************************************************************************
    *
    *    Functions
    *
    *       function IsDestructableTree takes destructable d returns boolean
    *
    *       function IsDestructableAlive takes destructable d returns boolean
    *
    *       function IsDestructableDead takes destructable d returns boolean
    *
    *       function IsTreeAlive takes destructable tree returns boolean
    *           - May only return true for trees.          
    *
    *       function KillTree takes destructable tree returns boolean
    *           - May only kill trees.
    *
    */

        globals
            private constant integer HARVESTER_UNIT_ID = 'hpea'//*  human peasant
            private constant integer HARVEST_ABILITY   = 'Ahrl'//*  ghoul harvest
            private constant integer HARVEST_ORDER_ID  = 0xD0032//* harvest order ( 852018 )
            private constant player  NEUTRAL_PLAYER    = Player(PLAYER_NEUTRAL_PASSIVE)
            private unit harvester                     = null
        endglobals
        function IsDestructableTree takes destructable d returns boolean
            //*  851973 is the order id for stunned, it will interrupt the preceding harvest order.
            return (IssueTargetOrderById(harvester, HARVEST_ORDER_ID, d)) and (IssueImmediateOrderById(harvester, 851973))
        endfunction
        function IsDestructableDead takes destructable d returns boolean
            return (GetWidgetLife(d) <= 0.405)
        endfunction
        function IsDestructableAlive takes destructable d returns boolean
            return (GetWidgetLife(d) > .405)
        endfunction
        function IsTreeAlive takes destructable tree returns boolean
            return IsDestructableAlive(tree) and IsDestructableTree(tree)
        endfunction
        function KillTree takes destructable tree returns boolean
            if (IsTreeAlive(tree)) then
                call KillDestructable(tree)
                return true
            endif
            return false
        endfunction
        private function Init takes nothing returns nothing
            static if LIBRARY_UnitIndexer then//*  You may adapt this to your own indexer.
                set UnitIndexer.enabled = false
            endif
            set harvester = CreateUnit(NEUTRAL_PLAYER, HARVESTER_UNIT_ID, 0, 0, 0)
            static if LIBRARY_UnitIndexer then
                set UnitIndexer.enabled = true
            endif
            call UnitAddAbility(harvester, HARVEST_ABILITY)
            call UnitAddAbility(harvester, 'Aloc')
            call ShowUnit(harvester, false)
        endfunction
        //*  Seriously?
        private module Inits
            private static method onInit takes nothing returns nothing
                call Init()
            endmethod
        endmodule
        private struct I extends array
            implement Inits
        endstruct
       
    endlibrary
     
    Last edited: Nov 27, 2015
  2. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,659
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    The UnitIndexer optional requirement is a bit odd in my opinion.

    I also really don't like how people use modules for initializers because of the priority.

    Why not just use the standard way of initializing? It would be make the code look cleaner anyway.

    Also,
    function TreeAlive
    ->
    function IsTreeAlive


    And maybe
    PLAYER_NEUTRAL_PASSIVE
    should be configurable.

    and I don't see the point of setting the dummy's position.

    yah
     
    Last edited: Feb 10, 2014
  3. BPower

    BPower

    Joined:
    Mar 18, 2012
    Messages:
    1,741
    Resources:
    21
    Spells:
    15
    Tutorials:
    1
    JASS:
    5
    Resources:
    21
    It was IsTreeAlive in the first place, then I changed it to TreeAlive analogous to UnitAlive, but you are right it can't be compared to the native.
    I removed the UnitIndexer requirement, because it is no longer available here.

    There was no point, because the unit is hidden. I also removed that line.

    Why not ...
     
  4. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,659
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    Looks great now, I think it should be approved.
     
  5. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,427
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    Looks good. Make sure you switch TreeAlive to IsTreeAlive in the documentation. The IsTreeAlive function is a little funny logic wise (it'll return false if the destructable is not a tree). It just gave me a little chuckle to visualize it as: "Q: Are you an alive tree? A: Well... I'm alive. Q: I'll take that as a no."

    Of course, I don't mean for you to change it. It is just interesting. I guess it could make sense logically as IsTreeAndAlive, but that is so ugly that it makes my vomit want to vomit.

    Anyway, it is a good resource and definitely approvable once the documentation is fixed.
     
  6. BPower

    BPower

    Joined:
    Mar 18, 2012
    Messages:
    1,741
    Resources:
    21
    Spells:
    15
    Tutorials:
    1
    JASS:
    5
    Resources:
    21
    I now also added IsDestructableAlive. It's somehow funny how the functions works in symbiosis. KillTree --> IsTreeAlive --> IsDestructableAlive + IsDestructableTree.

    The old IsDestructableTree library had IsDestructableDead instead of IsDestructableAlive which I really dislike, because for instance
    KillTree would have to check -> not IsDestructableDead. Still it exists since 2009. Should I change it in favour of backwards-compatibility?
     
  7. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,427
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    Yes, I think you should add IsDestructableDead(). Just include it, don't replace the current one IMO. It'll all be either inlined or optimized and done away with, so there is no real disadvantage to adding it + you get backwards compatibility.
     
  8. Daffa the Mage

    Daffa the Mage

    Map Moderator

    Joined:
    Jan 30, 2013
    Messages:
    7,642
    Resources:
    27
    Packs:
    1
    Maps:
    8
    Spells:
    16
    Tutorials:
    2
    Resources:
    27
    pretty much a good one, especially to replace Pitzer's IsDestTree which is in vJass.
     
  9. Adiktuz

    Adiktuz

    Joined:
    Oct 16, 2008
    Messages:
    9,674
    Resources:
    23
    Models:
    2
    Packs:
    1
    Maps:
    1
    Spells:
    16
    Tutorials:
    1
    JASS:
    2
    Resources:
    23
    well, this is vJASS too...
     
  10. Daffa the Mage

    Daffa the Mage

    Map Moderator

    Joined:
    Jan 30, 2013
    Messages:
    7,642
    Resources:
    27
    Packs:
    1
    Maps:
    8
    Spells:
    16
    Tutorials:
    2
    Resources:
    27
    yes, but in a better way than Pitzer do it. I however, dislike adding Ghoul harvest ability since peasant always have harvest ability (I think).
     
  11. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,860
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
  12. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,427
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    Forgot Almia submitted one. I thought it was vanilla JASS, but I guess not.

    Still, this is the JASS section so I don't see any reason not to approve it. Approved.
     
  13. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,860
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    Well, yeah, he still has more functionalities :D
     
  14. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    There is a Jass one - Spell section Purge ; )
    There are tons of IDT resources already, no matter if it's GUI, vanilla Jass or vJass.
     
  15. Daffa the Mage

    Daffa the Mage

    Map Moderator

    Joined:
    Jan 30, 2013
    Messages:
    7,642
    Resources:
    27
    Packs:
    1
    Maps:
    8
    Spells:
    16
    Tutorials:
    2
    Resources:
    27
    Spinnaker, Let me help with that :
    http://www.hiveworkshop.com/forums/spells-569/isdestree-v1-2-a-224623/?prev=status=a&u=Almia
    http://www.hiveworkshop.com/forums/spells-569/gui-jass-isdesttree-v1-2-a-240149/
    http://www.hiveworkshop.com/forums/...abletree-222145/?prev=search=Tree&d=list&r=20
    http://www.hiveworkshop.com/forums/...e-gui-1-1-a-228385/?prev=d=list&r=20&u=Chaosy
    Those are IDS I found at Spell Section. Seems we got tons of variants of these ^^

    Everyone
    I guess users can have lots of fun testing each IDS and see which one suits the best for them. (Mappers varies from GUI, JASS, VJASS, CJASS, WURST Coders, when a WURST version will show up anyway?)
     
  16. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    Mine GUI-friendly TreeRevival which is simpler version of vjass TR also features the same functionality.
     
  17. BPower

    BPower

    Joined:
    Mar 18, 2012
    Messages:
    1,741
    Resources:
    21
    Spells:
    15
    Tutorials:
    1
    JASS:
    5
    Resources:
    21
    - IsDesTree v1.2 is neat. I forgot about it when I uploaded this version. However you should add the harvest ability to the unit, because users might mess around with the worker unit.
    - GUI and JASS IsDestTree is the same code as IsDesTree v1.2.

    Use hashtables if you have to store a huge amount of data, which you want to access quickly, ... but for two trees, come on.
    - IsDestructableTree may cause trouble with custom trees.
    - IsDestructableTree GUI 1.1 no comment.

    I made it, because I saw the IDS in your TR library.
    It was the "not so good" one of wc3.net in the first place, now it's equal to this one.
     
  18. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    I hope you do understand that my previous replies were not even remotely meant to offend you. Simply, I've just posted my opinion that truely there is a lot of such resources already.

    You're right, the "default" implementation used within my script is not as perfect as one implemented here. Its biggest issue is that it lacks callback for stop or stun order to prevent any suprices from occuring. However, user can use any IDT lib he wants, thus wc3c implem is not forced to be used. I'll update my lib.

    I do not think is a problem with approval of this but meaby add a "library_once" keyword to prevent any missunderstandings when multiple libraries of such are in use.

    I've coded destdex and destutils about 2years ago, meaby I'll dig to find those out, but what comes to my mind is the IsDestructableDead function - yours lack GetDestructableTypeId() == 0; I have never encountered situation where checking getwidgetlife missled me, yet if function of such designed for units checks for typeid, I think there is nothing wrong to add additional safety here too. Inline frienly +
     
  19. Daffa the Mage

    Daffa the Mage

    Map Moderator

    Joined:
    Jan 30, 2013
    Messages:
    7,642
    Resources:
    27
    Packs:
    1
    Maps:
    8
    Spells:
    16
    Tutorials:
    2
    Resources:
    27
    Sorry if I offend you in any way.
    Anyway, thanks for this information :
    I'll patch it today if I can. +Credit.
     
  20. BPower

    BPower

    Joined:
    Mar 18, 2012
    Messages:
    1,741
    Resources:
    21
    Spells:
    15
    Tutorials:
    1
    JASS:
    5
    Resources:
    21
    Haha no worries :).