• 🏆 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!

Small Code Snippets

It's not about adding zeroes, you add any digit (maybe).
With more digits, the Jass Interpreter would probably assume that accuracy is more important for a certain real. I don't know why it doesn't assume that 4 or 5 digit places indicates the need for accuracy. Why 9? Maybe it was that n00b coder's lucky number or something ;o (lol)
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
But how can you prove that? How do we know that our timer is really off by 0.0000001 seconds? How do you know timers even maintain a strict accuracy based on the value of the real? If the coders were noob I'd assume that they would assume that all timers should have the same accuracy, because that would be easiest.
 
I don't know, that's why I would test things out ;/
I wonder if timers just execute things multiple times at once when the given interval is less than a certain value :/ (meaning that it would execute a code twice every 0.001 seconds instead of doing it once every 0.0005 seconds.)
I can't think of ways to test that >:O
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
It just seems rather gimmicky that some reals are more precise than others. I'm pretty sure they're all either half-precision or single-precision (floats) values.

That's exactly how it works >.>

.031250000 or w/e => .03125.
.03125 => .03125001 or something crazy like that.


I have tested it and if you add 0s to the end, it will keep its accuracy. Don't ask me why, JASS is just weird. Reals are not floats, they are crappy tripped out floats ;o.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
Under the decimal you mean?

If you have 10 numbers under the decimal you've destroyed your precision. Even if they are 0s. A number like 0.0123456789 printed out as 0.018 or something weird like that. For 0.0123000000 it came out to a similar result, 0.018 or something weird like that.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Under the decimal you mean?

Yep, fixed.

If you have 10 numbers under the decimal you've destroyed your precision. Even if they are 0s. A number like 0.0123456789 printed out as 0.018 or something weird like that. For 0.0123000000 it came out to a similar result, 0.018 or something weird like that.

I'm not only talking about reals in general, but the period of timers which seem to don't have the same behavior (8 numbers)
 
Level 7
Joined
Apr 27, 2011
Messages
272
just a simple function that checks how far a point is from a line segment

JASS:
	function DistanceFromLineSegment takes real px, real py, real x1, real y1, real x2, real y2 returns real
		local real r =SquareRoot((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
		local real a1=Atan2(y2-y1,x2-x1)
		local real a2=Atan2(px-x1,py-y1)
        if(a1>a2)then
            return Sin(a1-a2)*r
        endif
		return Sin(a2-a1)*r
	endfunction
 
I'm going to save this thread now.

JASS:
library IsUnitDead

    function IsUnitDead takes unit u returns boolean
        return IsUnitType(u, UNIT_TYPE_DEAD) and GetUnitTypeId(u) != 0
    endfunction

    function UnitAlive takes unit u returns boolean
        return not IsUnitType(u, UNIT_TYPE_DEAD) and GetUnitTypeId(u) != 0
    endfunction

endlibrary

This is a solid library.
No one should ever make the mistake of only using IsUnitType(u, UNIT_TYPE_DEAD) anymore.
We really need a standard way of checking whether a unit is dead or not, so why not have a library for it?
 
Level 7
Joined
Apr 30, 2011
Messages
359
@mag:

should be like this:
JASS:
library IsUnitAlive
    
    function IsUnitAlive takes unit u returns boolean
        return not IsUnitType(u, UNIT_TYPE_DEAD) and GetUnitTypeId(u) != 0
    endfunction
    
    function IsUnitDead takes unit u returns boolean
        return IsUnitType(u, UNIT_TYPE_DEAD) and GetUnitTypeId(u) != 0
    endfunction
    
    function IsUnitDeadEx takes unit u returns boolean
        return not IsUnitAlive(u)
    endfunction
    
    function IsUnitRemoved takes unit u returns boolean
        return GetUnitTypeId(u) == 0
    endfunction
endlibrary

or just simply:
JASS:
library IsUnitAlive
    
    function IsUnitAlive takes unit u returns boolean
        return not IsUnitType(u, UNIT_TYPE_DEAD) and GetUnitTypeId(u) != 0
    endfunction
    
    function IsUnitDead takes unit u returns boolean
        return not IsUnitAlive(u)
    endfunction
endlibrary
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Considering approving:

DistanceFromLineSegment: http://www.hiveworkshop.com/forums/2098605-post716.html

The reason why I am considering it still is because the description could be more clear (variable names for example). There is also similar functionality in the AdvancedLoc library by Dirac.

UnitPropWindow: http://www.hiveworkshop.com/forums/2052422-post676.html

The reason why I am still considering it is because it's my resource and there's been no comment on it. I don't approve just because I like it.
 
JASS:
function Xor takes boolean a, boolean b returns boolean
    return a != b
endfunction

function Nand takes boolean a, boolean b returns boolean
    return not (a and b)
endfunction

function Xnor takes boolean a, boolean b returns boolean
    return a == b
endfunction

function Nor takes boolean a, boolean b returns boolean
    return not (a or b)
endfunction

Wrappers and functions that will never be used by any sane man are what makes the world go around :3
 
Last edited:
JASS:
/*******************************************
*
*   UnitRallyPoint
*   v1.0.0.0
*   By Magtheridon96
*
*   - Provides GetUnitRallyPointX and GetUnitRallyPointY
*
*   API:
*   ----
*
*       - function GetUnitRallyPointX takes unit u returns real
*           - Returns the x coordinate of the rally point for a given unit
*       - function GetUnitRallyPointY takes unit u returns real
*           - Returns the y coordinate of the rally point for a given unit
*
*******************************************/
library UnitRallyPoint
    
    globals
        private location loc
    endglobals
    
    function GetUnitRallyPointX takes unit u returns real
        local real x
        set loc = GetUnitRallyPoint(u)
        set x = GetLocationX(loc)
        call RemoveLocation(loc)
        return x
    endfunction
    
    function GetUnitRallyPointY takes unit u returns real
        local real y
        set loc = GetUnitRallyPoint(u)
        set y = GetLocationY(loc)
        call RemoveLocation(loc)
        return y
    endfunction
    
endlibrary

Moved from the Jass section.
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
The one Magtheridon posted is fine, check all the cases yourself.
No, Maghtheridon's is right.

His yields the same results yours does.


Are you kidding me?

Magtheridon's code will return the wrong answer if both a and b are false.
(False != False) = true.

However, False Xor False should return false. Xor stands for Exclusive Or. It returns true iff one of the arguments are true.


Edit: See below post :D
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
Wait, wat, nvm :D

I mixed up == and != (or something. I dunno :>)

I think my brain tried to compensate for "a != b" being too easy. Kinda made Xor seem redundant. (Edit: It is redundant in fact)

Edit: However that means Mag's code snippet is useless, and using it is not recommended at all, as it will degrade the performance of your code.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
It gets compressed again when going through optimizer. Having shorter code does not mean that it would be better readable or structured. If it was for the ultimate performance, I would not use any foreign code. I see the posting of snippets more like a basic reference how things can be solved, you just learned how xor can be written for example.
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
It gets compressed again when going through optimizer.
The compiler I spoke of also includes the optimizer. It sucks.

For example, the following code:
JASS:
function Xor takes boolean a, boolean b returns boolean
    return a != b
endfunction
    
function HaiThar takes nothing returns nothing
    if Xor(true, false) then
        call BJDebugMsg("WOOO")
    endif
endfunction
Is turned into this:
JASS:
function Xor takes boolean a,boolean b returns boolean
    return a != b //No, this function is not removed
endfunction
    
function HaiThar takes nothing returns nothing
    if ((true ) != ( false)) then // INLINED!!
        call BJDebugMsg("WOOO")
    endif
endfunction
Which is much longer (and also slower) than only having this:
JASS:
function HaiThar takes nothing returns nothing
    if true != false then
        call BJDebugMsg("WOOO")
    endif
endfunction

Having shorter code does not mean that it would be better readable or structured.
It could be confusing. In this case it would confuse some people who don't know what Xor is, and they would have to look up the function.

If it was for the ultimate performance, I would not use any foreign code.
Neither would I, but that is personal preferences. I prefer making everything myself, but some people will just include shit they see being posted into their own code.

you just learned how xor can be written for example.
I already knew what Xor was and what it meant, but I was made aware of it being utterly redundant when dealing with booleans.

Unnecessarily long post is long.
 
Top