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. 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
  4. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  5. The Secrets of Warcraft 3 have revealed interesting works. The RESULTS for Abelhawk's Mini-Mapping Contest #15 have come out!
    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. Night Rider gained several songs for his journey. The poll for the 12th Music Contest has started. Check it out!
    Dismiss Notice
  9. 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
  10. 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.

Custom JASS Natives - (Requests & Submissions)

Discussion in 'The Lab' started by TriggerHappy, Nov 24, 2013.

  1. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,654
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    I will be updating this thread with all API created for SharpCraft. Feel free to request anything you would like to see in JASS, or submit your own creations here (including the source).

    Requirements
    1. JNGP
    2. SharpCraft

    Keyboard & Mouse

    These can be used to detect mouse clicks and movement as well as detect key pressing / releasing.

    Third Person Camera (With Mouse)

    Code (vJASS):

    native GetMouseX                    takes nothing returns real
    native GetMouseY                    takes nothing returns real
    native GetMouseTerrainX             takes nothing returns real
    native GetMouseTerrainY             takes nothing returns real
    native GetMouseUIX                  takes nothing returns real
    native GetMouseUIY                  takes nothing returns real
    native GetTriggerKey                takes nothing returns integer
    native GetTriggerWheelDelta         takes nothing returns integer
    native GetTriggerKeyString          takes nothing returns string
    native IsMouseOverUI                takes nothing returns boolean
    native IsKeyDown                    takes integer vkey returns boolean
    native TriggerRegisterAnyKeyEvent   takes trigger whichTrigger, integer state returns nothing
    native TriggerRegisterAnyMouseEvent takes trigger whichTrigger, integer state returns nothing
    native TriggerRegisterKeyEvent      takes trigger whichTrigger, integer vkey, integer state returns nothing
    native TriggerRegisterMouseEvent    takes trigger whichTrigger, integer vkey, integer state returns nothing
    native TriggerRegisterMouseWheelEvent takes trigger whichTrigger returns nothing


    Mouse Example

    Code (vJASS):

    scope Mouse initializer onInit
       
        globals
            private constant integer MOUSE_LEFT  = 0
            private constant integer MOUSE_RIGHT = 2
           
            private constant integer EVENT_MOUSE_DOWN = 0
            private constant integer EVENT_MOUSE_UP   = 1
        endglobals
       
        private function LeftDown takes nothing returns nothing  
            local integer vkey = GetTriggerKey()
            if (vkey == MOUSE_LEFT) then
                call BJDebugMsg("Left mouse clicked")
            elseif (vkey == MOUSE_RIGHT) then
                call BJDebugMsg("Right mouse clicked")
            else
                call BJDebugMsg("Unhandled key " + I2S(vkey))
            endif
        endfunction
       
        private function LeftUp takes nothing returns nothing  
            local integer vkey = GetTriggerKey()
            if (vkey == MOUSE_LEFT) then
                call BJDebugMsg("Left mouse released")
            elseif (vkey == MOUSE_RIGHT) then
                call BJDebugMsg("Right mouse released")
            else
                call BJDebugMsg("Unhandled key " + I2S(vkey))
            endif
        endfunction

        //===========================================================================
        private function onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            call TriggerRegisterAnyMouseEvent(t, EVENT_MOUSE_DOWN)
            call TriggerAddAction(t, function LeftDown)
            set t = CreateTrigger()
            call TriggerRegisterMouseEvent(t, MOUSE_LEFT,  EVENT_MOUSE_UP)
            call TriggerRegisterMouseEvent(t, MOUSE_RIGHT, EVENT_MOUSE_UP)
            call TriggerAddAction(t, function LeftUp)
        endfunction

    endscope



    Keyboard Example

    Code (vJASS):

    scope Keyboard initializer onInit
       
        private function KeyDown takes nothing returns nothing  
            call BJDebugMsg(GetTriggerKeyString() + " pressed")
        endfunction

        //===========================================================================
        private function onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            call TriggerRegisterAnyKeyEvent(t, 0)
            call TriggerAddAction(t, function KeyDown)
        endfunction

    endscope
     


    Interface

    Various functions related to the game interface.

    Code (vJASS):

    native FramesPerSecond    takes nothing returns real
    native EnableChatMessages takes boolean flag returns nothing
    native InterfaceMessage   takes string message, integer area, real duration returns nothing
    native PlayerChatMessage  takes integer pid, string message, integer whichGroup, real duration returns nothing
     


    Stopwatch

    Code (vJASS):

    native StopWatchCreate  takes nothing returns integer
    native StopWatchTicks   takes integer id returns integer
    native StopWatchMark    takes integer id returns integer
    native StopWatchDestroy takes integer id returns nothing
     


    Example

    Code (vJASS):

    scope Stopwatch initializer onInit
       
        globals
            private constant integer ITERATIONS = 4000
        endglobals
       
        private function Actions takes nothing returns boolean
            local integer sw
            local integer i
            local real array ticks
            local string output

            set i  = 0
            set sw = StopWatchCreate()
               
            loop
                exitwhen i == ITERATIONS
                // TEST 1 HERE
                set i = i + 1
            endloop
               
            set ticks[0] = StopWatchTicks(sw)
            set output = I2S(ITERATIONS) + " iterations of Test #1 took " + I2S(StopWatchMark(sw)) + " milliseconds to finish.\n"
            call StopWatchDestroy(sw)
               
            set i  = 0
            set sw = StopWatchCreate()
               
            loop
                exitwhen i == ITERATIONS
                // TEST 2 HERE
                set i = i + 1
            endloop
               
            set ticks[1] = StopWatchTicks(sw)
            set output = output + I2S(ITERATIONS) + " iterations of Test #2 took " + I2S(StopWatchMark(sw)) + " milliseconds to finish.\n\n"
            call StopWatchDestroy(sw)
               
            if (ticks[0] > ticks[1]) then
                set ticks[2] = 100 - (ticks[1]/ticks[0] * 100)
                set output = output + "Test #2 was " + R2S(ticks[2]) + "% faster than Test #1\n\n"
            else
                set ticks[2] = 100 - (ticks[0]/ticks[1] * 100)
                set output = output + "Test #1 is " + R2S(ticks[2]) + "% faster than Test #2\n\n"
            endif
           
            call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, output)
           
            return false
        endfunction

        //===========================================================================
        private function onInit takes nothing returns nothing
            local trigger t = CreateTrigger()
            call TriggerRegisterPlayerEvent(t, Player(0), EVENT_PLAYER_END_CINEMATIC)
            call TriggerAddCondition(t, function Actions)
        endfunction

    endscope
     



    Http

    You can use these to communicate with websites.

    Code (vJASS):

    native HttpLastCookie      takes nothing returns string
    native HttpPendingResponse takes integer id returns string
    native HttpRequest         takes string method, string url, string cookies, string values returns string
    native StartRequest        takes string method, string url, string cookies, string values, integer id returns nothing
     


    Example

    Code (vJASS):

    struct Http
           
        string reqMethod = ""
        string url       = ""
        string cookies   = ""
        string values    = ""
           
        method BeginRequest takes nothing returns nothing
            call StartRequest(this.reqMethod, this.url, this.cookies, this.values, this)
        endmethod
           
        static method Post takes string url, string cookies, string values returns string
            return HttpRequest("POST", url, cookies, values)
        endmethod
           
        static method Get takes string url, string cookies returns string
            return HttpRequest("GET", url, cookies, "")
        endmethod
       
        static method onInit takes nothing returns nothing
            local string resp = null
            local Http req    = Http.create()
            set req.url       = "hiveworkshop.com"
            set req.reqMethod = "GET"
           
            call req.BeginRequest()

            loop
                exitwhen resp != null
                set resp = HttpPendingResponse(req)
                call TriggerSleepAction(0)
            endloop
           
            call req.destroy()
           
            call ClearTextMessages()
            call BJDebugMsg(resp)
        endmethod
       
    endstruct
     


    String

    Code (vJASS):

    native StringReplace   takes string str, string old, string new returns string
    native StringContains  takes string str, string sub returns boolean
    native StringCount     takes string str, string sub returns integer
    native StringPos       takes string str, string sub returns integer
    native StringReverse   takes string str returns string
    native StringTrim      takes string str returns string
    native StringTrimStart takes string str returns string
    native StringTrimEnd   takes string str returns string
    native StringSplit     takes string str, string sub, integer index returns string
    native StringInsert    takes string str, integer index, string val returns string
     


    Example

    Code (vJASS):

            local string str = "hello world"
            call BJDebugMsg(StringReplace(str, "hello", "hey")) // hey world
            call BJDebugMsg(StringReverse(str))                 // dlrow olleh
            call BJDebugMsg(StringInsert(str, 0, "H"))          // Hello world
            call BJDebugMsg(StringSplit(str, " ", 1))           // world
            call BJDebugMsg(StringTrim(str))                    // hello world
            call BJDebugMsg(I2S(StringPos(str, "ello")))        // 1
     

     

    Attached Files:

    Last edited: Dec 13, 2013
  2. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,427
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    Is there anyway to sync these values? Or retrieve them for particular players? e.g.:
    GetMouseX takes player p returns real


    Otherwise, I assume they work kinda like GetCameraTargetPositionX/Y(), right?
     
  3. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,654
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    AFAIK, no.

    It always detects for the local player.

    With WarCom it may be possible, but right now I don't think so.

    Yeah
     
  4. Malhorne

    Malhorne

    Joined:
    Sep 14, 2012
    Messages:
    2,340
    Resources:
    6
    Spells:
    4
    Tutorials:
    1
    JASS:
    1
    Resources:
    6
    Ohmagad ohmagad I need to install Sharpcraft :3 !
     
  5. WaterKnight

    WaterKnight

    Joined:
    Aug 18, 2009
    Messages:
    4,031
    Resources:
    5
    Maps:
    1
    Tutorials:
    4
    Resources:
    5
  6. moyackx

    moyackx

    Joined:
    Feb 15, 2006
    Messages:
    789
    Resources:
    7
    Maps:
    4
    Spells:
    2
    Tutorials:
    1
    Resources:
    7
    Why you promote this old JNGP??

    Can you use this in Bnet??
     
  7. Malhorne

    Malhorne

    Joined:
    Sep 14, 2012
    Messages:
    2,340
    Resources:
    6
    Spells:
    4
    Tutorials:
    1
    JASS:
    1
    Resources:
    6
    Yes I prefer 2.0
     
  8. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Is it possible to emulate an order given by some player, i mean just like you do with the mouse/keyboard ?
    If you can, then i have a silly awesome idea about how to use it.
     
  9. WaterKnight

    WaterKnight

    Joined:
    Aug 18, 2009
    Messages:
    4,031
    Resources:
    5
    Maps:
    1
    Tutorials:
    4
    Resources:
    5
    Queuing orders pls, unit loses order, unit starts/stops moving events.
     
  10. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,654
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    Yes but the natives won't be synced.

    Yes

    I am waiting for MindWorX to provide support for events in SharpCraft before I create any.

    Thanks for the suggestions.

    Updated with String API (unfinished)
     
  11. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Hmm i was not enough accurate, i'm talking about unit orders, still possible ?
     
  12. T. D. W.

    T. D. W.

    Joined:
    Jul 31, 2012
    Messages:
    1,505
    Resources:
    5
    Spells:
    5
    Resources:
    5
    Me too!! :ogre_hurrhurr:
     
  13. GhostWolf

    GhostWolf

    Joined:
    Jul 29, 2007
    Messages:
    4,836
    Resources:
    2
    Tools:
    1
    Tutorials:
    1
    Resources:
    2
    A string trim function generally removes whitespace from the beginning and ending of a string, not all whitespace.
     
  14. Ezekiel12

    Ezekiel12

    Joined:
    Mar 13, 2012
    Messages:
    1,053
    Resources:
    0
    Resources:
    0
  15. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Why not, Christmas day is not so far.
     
  16. T. D. W.

    T. D. W.

    Joined:
    Jul 31, 2012
    Messages:
    1,505
    Resources:
    5
    Spells:
    5
    Resources:
    5
    does the map need sharpcraft to even open in warcraft ? Oo cuz it's not opening for me
     
  17. Malhorne

    Malhorne

    Joined:
    Sep 14, 2012
    Messages:
    2,340
    Resources:
    6
    Spells:
    4
    Tutorials:
    1
    JASS:
    1
    Resources:
    6
    I think it needs.
     
  18. T. D. W.

    T. D. W.

    Joined:
    Jul 31, 2012
    Messages:
    1,505
    Resources:
    5
    Spells:
    5
    Resources:
    5
    If yes, it's kinda useless since people playing the map using this thing will need sharpcraft :/
     
  19. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,654
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    It's not useless at all especially when multiplayer is complete.

    I haven't had much time the past couple days but I will be posting more natives soon.
     
    Last edited: Nov 27, 2013
  20. T. D. W.

    T. D. W.

    Joined:
    Jul 31, 2012
    Messages:
    1,505
    Resources:
    5
    Spells:
    5
    Resources:
    5
    I wasn't able to open the test map inwarcraft if there is a bug or something.. But it doesn't open to me