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

Bound Coordinates

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
Bound Coordinates v1.01

This simple snippet helps you implement map boundary with convenience when setting units' coordinates. It helps in other cases as well.

Script
JASS:
library BoundCoordinates /*v1.01


  */requires WorldBounds/*
    - https://github.com/nestharus/JASS/blob/master/jass/Systems/WorldBounds/script.j

*///! novjass
     _________________
    |                 |
    | WRITTEN BY: AGD |
    |_________________|


    |=====|
    | API |
    |=====|

        function SafeSetX takes unit u, real x returns boolean/*
        - Sets the x-coordinate of a unit while implementing map bounds
        - Returns true if the taken <x> value is already within the map bounds
          and returns false if not

      */function SafeSetY takes unit u, real y returns boolean/*
        - Sets the y-coordinate of a unit while implementing map bounds
        - Returns true if the taken <y> value is already within the map bounds
          and returns false if not

      */function SafeSetXY takes unit u, real x, real y returns boolean/*
        - Sets the x-coordinate and y-coordinate of a unit while implementing map bounds
        - Returns true if both taken coordinates are already within the map bounds
          and returns false if not

      */function BoundX takes real x returns real/*
        - Returns the bounded value of x: if x is outside the map coordinates,
          x is set to x of the map boundary

      */function BoundY takes real y returns real/*
        - Returns the bounded value of y: if y is outside the map coordinates,
          y is set to y of the map boundary

*///! endnovjass


    function SafeSetX takes unit u, real x returns boolean
        local boolean check = true
        if x < WorldBounds.minX then
            set x = I2R(WorldBounds.minX)
            set check = false
        elseif x > WorldBounds.maxX then
            set x = I2R(WorldBounds.maxX)
            set check = false
        endif
        call SetUnitX(u, x)
        return check
    endfunction

    function SafeSetY takes unit u, real y returns boolean
        local boolean check = true
        if y < WorldBounds.minY then
            set y = I2R(WorldBounds.minY)
            set check = false
        elseif y > WorldBounds.maxY then
            set y = I2R(WorldBounds.maxY)
            set check = false
        endif
        call SetUnitY(u, y)
        return check
    endfunction

    function SafeSetXY takes unit u, real x, real y returns boolean
        return SafeSetX(u, x) and SafeSetY(u, y)
    endfunction

    function BoundX takes real x returns real
        if x < WorldBounds.minX then
            return I2R(WorldBounds.minX)
        elseif x > WorldBounds.maxX then
            return I2R(WorldBounds.maxX)
        endif
        return x
    endfunction

    function BoundY takes real y returns real
        if y < WorldBounds.minY then
            return I2R(WorldBounds.minY)
        elseif y > WorldBounds.maxY then
            return I2R(WorldBounds.maxY)
        endif
        return y
    endfunction


endlibrary


v1.01
- Added WorldBounds as a requirement
- Changed some function naming
v1.00
- First version
 
Last edited:

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
Oh there is that already :( and it does it all already. Hmm, should I continue this?

EDIT:
Updated to v1.01

- Added WorldBounds as a requirement
- Changed some function naming

EDIT:
Guess I'll continue. This can do something not included in BoundSentinel, for example
in spells with circular effects (those with circular borders) and other:
JASS:
// instead of doing it like this (I often do things like these in my spells)
set x = centerX + dist*Cos( angle )
set y = centerY + dist*Sin( angle )
if x < MAPMINX then
    set x = MAPMINX
elseif x > MAPMAXX then
    set x = MAPMAXX
endif
if y < MAPMINY then
    set y = MAPMINY
elseif y > MAPMAXY then
    set y = MAPMAXX
endif
call AddSpecialEffect( model, x, y )

// you can just do it like
call AddSpecialEffect( model, BoundX( centerX + dist*Cos( angle ) ), BoundY( centerY + dist*Sin( angle ) ) )
 
Last edited:
The save unit movement functions is extra sugar, but the real usage is BoundX/Y.
It is pretty easy for the user to write SetUnitX(u, Bound(x)) instead of SafeSetX(u, x).

Don't understand me wrong, these BoundX/Y functions might be useful. But it is too easy to write your own conditon-check when WorldBounds is already required.
It can't be approved I think. :/
 
Top