# Small Code Snippets

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

1. ### AGD

Joined:
Mar 29, 2016
Messages:
547
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

Joined:
Mar 29, 2016
Messages:
547
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
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
set limit = limit - 1
endif
endloop
endfunction

endlibrary

3. ### JAKEZINC

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

4. ### Wietlol

Joined:
Aug 1, 2013
Messages:
4,638
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

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

6. ### AGD

Joined:
Mar 29, 2016
Messages:
547
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

Joined:
Aug 1, 2013
Messages:
4,638
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

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

Joined:
Mar 29, 2016
Messages:
547
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

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

11. ### AGD

Joined:
Mar 29, 2016
Messages:
547
Resources:
13
Spells:
7
Tutorials:
1
JASS:
5
Resources:
13
Not so sure but just to avoid risks

12. ### Almia

Joined:
Apr 24, 2012
Messages:
4,842
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.

Joined:
Mar 29, 2016
Messages:
547
Resources:
13
Spells:
7
Tutorials:
1
JASS:
5
Resources:
13
What is it?

14. ### Kazeon

Joined:
Oct 12, 2011
Messages:
3,296
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

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

Joined:
Sep 6, 2013
Messages:
6,517
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

Joined:
Feb 27, 2007
Messages:
3,788
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
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

Joined:
Mar 29, 2016
Messages:
547
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

Joined:
Sep 6, 2013
Messages:
6,517
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

Joined:
Nov 7, 2014
Messages:
546
Resources:
6
Tools:
1
Maps:
1
Spells:
1
JASS:
3
Resources:
6
Code (vJASS):

function crash takes nothing returns nothing
call ExecuteFunc("crash")
endfunction

20. ### DracoL1ch

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