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

Basics - Unit Groups

Status
Not open for further replies.

NEL

NEL

Level 6
Joined
Mar 6, 2017
Messages
113
Part 1

Create 200 footmen on the map at random locations. (but within playable map area)
Loop through all units on map, and kill them if they are in range of 1000 towards the center of the map.

vJASS:
//! zinc
library UnitGroups {
    private {
        group Group = CreateGroup();
        timer Timer = CreateTimer();
       
        function DeadlyCenter( unit whichUnit ) -> boolean {
            real dx = GetUnitX( whichUnit );
            real dy = GetUnitY( whichUnit );
            return SquareRoot( dx * dx + dy * dy ) <= 1000;
        }
       
        function onInit() {
            real xMax = GetRectMinX( bj_mapInitialPlayableArea );
            real xMin = GetRectMaxX( bj_mapInitialPlayableArea );
            real yMax = GetRectMinY( bj_mapInitialPlayableArea );
            real yMin = GetRectMaxY( bj_mapInitialPlayableArea );
            real x;
            real y;
            integer Index = 0;
           
            while( Index < 200 ) {
                x = GetRandomReal( xMax, xMin );
                y = GetRandomReal( yMax, yMin );
               
                GroupAddUnit( Group, 
                    CreateUnit( Player(0), 'hfoo', x, y, GetRandomReal( 0, 360 ) )
                );
               
                Index = Index + 1;
            }
           
            TimerStart( Timer, 0.0312500, true, function(){
                ForGroup( Group, function(){
                    unit u = GetEnumUnit();

                    if( DeadlyCenter( u ) ) {
                        KillUnit( u );
                        GroupRemoveUnit( Group, u );
                    }
                   
                    u = null;
                });
            });
        }
    }
}
//! endzinc





Part 2

Create a trigger that runs when a player presses the 'Esc' button (Skip Cinematic).
When the trigger runs, the amount of alive footmen should be printed on screen.
For solving this part, the "FoG Enumeration" technique must be used.

vJASS:
//! zinc
library UnitGroups {
    private {
        group Group = CreateGroup();
        timer Timer = CreateTimer();
        integer Counter = 0;
       
        function CountFootmen() -> integer {
            Counter = 0;
            ForGroup( Group, function() {
                Counter = Counter + 1;
            });
            return Counter;
        }
       
        function DeadlyCenter( unit whichUnit ) -> boolean {
            real dx = GetUnitX( whichUnit );
            real dy = GetUnitY( whichUnit );
            return SquareRoot( dx * dx + dy * dy ) <= 1000;
        }
       
        function onInit() {
            trigger trgESC = CreateTrigger();
            real xMax = GetRectMinX( bj_mapInitialPlayableArea );
            real xMin = GetRectMaxX( bj_mapInitialPlayableArea );
            real yMax = GetRectMinY( bj_mapInitialPlayableArea );
            real yMin = GetRectMaxY( bj_mapInitialPlayableArea );
            real x;
            real y;
            integer Count = 0;
            integer Index = 0;
           
            while( Count < 200 ) {
                x = GetRandomReal( xMax, xMin );
                y = GetRandomReal( yMax, yMin );
               
                GroupAddUnit( Group, 
                    CreateUnit( Player(0), 'hfoo', x, y, GetRandomReal( 0, 360 ) )
                );
               
                Count = Count + 1;
            }
           
            while( Index < bj_MAX_PLAYERS ) {
                TriggerRegisterPlayerEvent( trgESC, Player( Index ), EVENT_PLAYER_END_CINEMATIC );
                Index = Index + 1;
            }
           
            TriggerAddAction( trgESC, function(){
                ClearTextMessages();
               
                DisplayTextToPlayer( GetLocalPlayer(), 0, 0, 
                    "Alive Footmen: " + I2S( CountFootmen() )
                );
            });
           
            TimerStart( Timer, 0.0312500, true, function(){
                ForGroup( Group, function(){
                    unit u = GetEnumUnit();

                    if( DeadlyCenter( u ) ) {
                        KillUnit( u );
                        GroupRemoveUnit( Group, u );
                    }
                   
                    u = null;
                });
            });
           
            trgESC = null;
        }
    }
}
//! endzinc
 

Attachments

  • [Crash Course] Basics - Unit Groups.w3m
    24.8 KB · Views: 68
Looping doesn't imply being called periodically, so the running timer wasn't actually asked for. Likewise re-creating units.
Why the hell you do this^^:
JASS:
            real xMax = GetRectMinX( bj_mapInitialPlayableArea );
            real xMin = GetRectMaxX( bj_mapInitialPlayableArea );
            real yMax = GetRectMinY( bj_mapInitialPlayableArea );
            real yMin = GetRectMaxY( bj_mapInitialPlayableArea );
.. anways part 1 solved.

The intention of part 2 was also to introduce an other tequnique for group enumeration, FoG called on Hive. Would like to see it in use. The same thing here, that a periodic lookup isn't asked for.
 
Status
Not open for further replies.
Top