(Keeps Hive Alive)
Go Back   The Hive Workshop - A Warcraft III Modding Site > Warcraft III Resources > Submissions

Submissions Submit JASS resources! If approved, they will be moved to their proper section.
Please read me first.

Reply
 
LinkBack Thread Tools Display Modes
Old 08-05-2008, 11:36 AM   #1 (permalink)
 
Element of Water's Avatar

User
 
Join Date: Aug 2008
Posts: 252

Element of Water has little to show at this moment (29)Element of Water has little to show at this moment (29)Element of Water has little to show at this moment (29)


[vJASS] Arrow Key Detection System

This is my Arrow Key Detection system I have created as part of an arrow key movement system. I thought other people may have a use for it, so here it is!
It basically extends on Blizzard's arrow key down and up events, allowing you to query if a player is pressing an arrow key. A known bug is that it can't detect when a key is lifted while in any menu, but I can't see any way around this so it'll have to do

This system works around one function: function IsKeyDown takes integer key, player id returns boolean, and I have made constant variables to support it:

constant integer ARROW_LEFT = 0
constant integer ARROW_RIGHT = 1
constant integer ARROW_DOWN = 2
constant integer ARROW_UP = 3

I don't believe there are any leaks as I use no local handles and my only Global variable is an array whose elements are only ever set once.
Changelog

v1.0.1

This version actually works.



Anyway, the code:

Main Code

library KeyDetection initializer Init

globals
    private trigger array ArrowDetectors
    private boolean array KeyDown
    private constant integer MAX_NUMBER_OF_PLAYERS = 4

    constant integer ARROW_LEFT = 0
    constant integer ARROW_RIGHT = 1
    constant integer ARROW_DOWN = 2
    constant integer ARROW_UP = 3
endglobals

function IsKeyDown takes integer key, player id returns boolean
    return KeyDown[MAX_NUMBER_OF_PLAYERS*key+GetPlayerId(id)]
endfunction

private function GetKeyEventDown takes integer i returns playerevent
    if i == 0 then
        return EVENT_PLAYER_ARROW_LEFT_DOWN
    elseif i == 1 then
        return EVENT_PLAYER_ARROW_RIGHT_DOWN
    elseif i == 2 then
        return EVENT_PLAYER_ARROW_DOWN_DOWN
    elseif i == 3 then
        return EVENT_PLAYER_ARROW_UP_DOWN
    endif
    return null
endfunction

private function GetKeyEventUp takes integer i returns playerevent
    if i == 0 then
        return EVENT_PLAYER_ARROW_LEFT_UP
    elseif i == 1 then
        return EVENT_PLAYER_ARROW_RIGHT_UP
    elseif i == 2 then
        return EVENT_PLAYER_ARROW_DOWN_UP
    elseif i == 3 then
        return EVENT_PLAYER_ARROW_UP_UP
    endif
    return null
endfunction

private function LeftDown takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*0+GetPlayerId(GetTriggerPlayer())] = true
endfunction

private function LeftUp takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*0+GetPlayerId(GetTriggerPlayer())] = false
endfunction

private function RightDown takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*1+GetPlayerId(GetTriggerPlayer())] = true
endfunction

private function RightUp takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*1+GetPlayerId(GetTriggerPlayer())] = false
endfunction

private function DownDown takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*2+GetPlayerId(GetTriggerPlayer())] = true
endfunction

private function DownUp takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*2+GetPlayerId(GetTriggerPlayer())] = false
endfunction

private function UpDown takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*3+GetPlayerId(GetTriggerPlayer())] = true
endfunction

private function UpUp takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*3+GetPlayerId(GetTriggerPlayer())] = false
endfunction

private function GetActionFunc takes integer i returns code
    if i == 0 then
        return function LeftDown
    elseif i == 1 then
        return function RightDown
    elseif i == 2 then
        return function DownDown
    elseif i == 3 then
        return function UpDown
    elseif i == 4 then
        return function LeftUp
    elseif i == 5 then
        return function RightUp
    elseif i == 6 then
        return function DownUp
    elseif i == 7 then
        return function UpUp
    endif
    return null
endfunction

//===========================================================================
private function Init takes nothing returns nothing
    local integer i = 0
    local integer i2 = 0

    loop
        exitwhen i >= 4
        set ArrowDetectors[i] = CreateTrigger()
        set ArrowDetectors[i+4] = CreateTrigger()
        loop
            exitwhen i2 >= MAX_NUMBER_OF_PLAYERS
            call TriggerRegisterPlayerEvent(ArrowDetectors[i], Player(i2), GetKeyEventDown(i))
            call TriggerRegisterPlayerEvent(ArrowDetectors[i+4], Player(i2), GetKeyEventUp(i))
            set i2 = i2 + 1
        endloop
        set i2 = 0
        call TriggerAddAction(ArrowDetectors[i], GetActionFunc(i))
        call TriggerAddAction(ArrowDetectors[i+4], GetActionFunc(i+4))
        set i = i + 1
    endloop

endfunction

endlibrary

PS: I will convert it to normal JASS if you want, but I figured using a library with private functions was easier and most systems and spells which implement this system will probably use vJASS anyway.

Last edited by Element of Water; 11-22-2008 at 05:03 PM..
Element of Water is offline   Reply With Quote
Old 10-16-2008, 06:29 PM   #2 (permalink)
 
SerraAvenger's Avatar

How may I serve you?
 
Join Date: Apr 2007
Posts: 100

SerraAvenger has little to show at this moment (3)


private function LeftUp takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*0+GetPlayerId(GetTriggerPlayer())] = false
endfunction


2D arrays would be appreciated. You use VJass anyway ; )

    local integer i2 = 0

j would be nice instead of i2.

        endloop
        set i2 = 0

the set i2 = 0 should be infront of the loop, not behind it ; )


MAX_NUMBER_OF_PLAYERS is not counting players but keys; Also, you should use it everywhere where you can ( instead of adding +4 here and counting to 4 there )

This is what I modified your stuff into:
Code


library KeyDetector
globals
    private constant integer MAX_USER_SLOT = 12 // I don't think AI would press keys ;D
    private constant integer KEYS = 4
    private trigger array ArrowDetectors[KEYS][2]
    private boolean array KeyDown[KEYS][MAX_USER_SLOT]

    constant integer ARROW_LEFT = 0
    constant integer ARROW_RIGHT = 1
    constant integer ARROW_DOWN = 2
    constant integer ARROW_UP = 3
endglobals

function IsKeyDown takes integer key, player id returns boolean
    return KeyDown[key][ GetPlayerId(id) ]
endfunction

private function GetKeyEventDown takes integer i returns playerevent
    if i == 0 then
        return EVENT_PLAYER_ARROW_LEFT_DOWN
    elseif i == 1 then
        return EVENT_PLAYER_ARROW_RIGHT_DOWN
    elseif i == 2 then
        return EVENT_PLAYER_ARROW_DOWN_DOWN
    elseif i == 3 then
        return EVENT_PLAYER_ARROW_UP_DOWN
    endif
    return null
endfunction

private function GetKeyEventUp takes integer i returns playerevent
    if i == 0 then
        return EVENT_PLAYER_ARROW_LEFT_UP
    elseif i == 1 then
        return EVENT_PLAYER_ARROW_RIGHT_UP
    elseif i == 2 then
        return EVENT_PLAYER_ARROW_DOWN_UP
    elseif i == 3 then
        return EVENT_PLAYER_ARROW_UP_UP
    endif
    return null
endfunction

private function ArrowDown takes nothing returns nothing
    local integer i = 0
    local trigger trigTrigger = GetTriggeringTrigger()
    local integer trigId = GetPlayerId( GetTriggerPlayer() )

    loop
        exitwhen ArrowDetectors[i][0] == trigTrigger
        set i = i + 1
    endloop
    set KeyDown[i][trigId] = true
    set trigTrigger = null
endfunction

private function ArrowUp takes nothing returns nothing
    local integer i = 0
    local trigger trigTrigger = GetTriggeringTrigger()
    local integer trigId = GetPlayerId( GetTriggerPlayer() )

    loop
        exitwhen ArrowDetectors[i][1] == trigTrigger
        set i = i + 1
    endloop
    set KeyDown[i][trigId] = false
    set trigTrigger = null
endfunction

//===========================================================================
private function Init takes nothing returns nothing
    local integer i = 0
    local integer j = 0

    loop
        exitwhen i >= KEYS
        set ArrowDetectors[i][0] = CreateTrigger()
        set ArrowDetectors[i][1] = CreateTrigger()
        set j = 0
        loop
            exitwhen j >= MAX_USER_SLOT
            call TriggerRegisterPlayerEvent(ArrowDetectors[i][0], Player(j), GetKeyEventDown( i ) )
            call TriggerRegisterPlayerEvent(ArrowDetectors[i][1], Player(j), GetKeyEventUp( i ) )
            set KeyDown[i][j] = false
            set j = j + 1
        endloop
        call TriggerAddAction(ArrowDetectors[i][0], function ArrowDown )
        call TriggerAddAction(ArrowDetectors[i][1], function ArrowUp )
        set i = i + 1
    endloop
endfunction
endlibrary




Interesting system nevertheless,
Davey
__________________
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
SerraAvenger is offline   Reply With Quote
Old 10-20-2008, 08:52 PM   #3 (permalink)
 
Element of Water's Avatar

User
 
Join Date: Aug 2008
Posts: 252

Element of Water has little to show at this moment (29)Element of Water has little to show at this moment (29)Element of Water has little to show at this moment (29)


Quote:
private function LeftUp takes nothing returns nothing
    set KeyDown[MAX_NUMBER_OF_PLAYERS*0+GetPlayerId(GetTriggerPlayer())] = false
endfunction


2D arrays would be appreciated. You use VJass anyway ; )
Never even heard of 2D arrays in JASS before, that's why I didn't use them.
Quote:

local integer i2 = 0

j would be nice instead of i2.
"i" stands for index. So "i2" is index 2... you don't just carry on with the alpabet...

Quote:
        endloop
        set i2 = 0

the set i2 = 0 should be infront of the loop, not behind it ; )
Does it matter where you put it? Not really.

Quote:
MAX_NUMBER_OF_PLAYERS is not counting players but keys; Also, you should use it everywhere where you can ( instead of adding +4 here and counting to 4 there )
No it's definitely players. What's the point in setting a variable for the max number of keys anyway? It's always going to stay 4...
__________________
Element of Water is offline   Reply With Quote
Old 10-21-2008, 05:22 AM   #4 (permalink)
 
PurgeandFire111's Avatar

User Title
 
Join Date: Nov 2006
Posts: 1,029

PurgeandFire111 is a jewel in the rough (155)


Pretty cool system. I once made some sort of similar variation of this yet I don't think it functioned too well. Saves me some work if I ever want to do something that would revolve around arrow keys. =P
__________________
Vote For The Hive Workshop!
Funny but the TRUTH! :
Quote:
Originally Posted by Sopho
Unprotecting maps is not right its like crime and YOU GO MAKE YOUR OWN MAP AND BE HAPPY! Don't be gay n00b!

1. You can if it is yours but you should not if it is not. It is like stealing information and using it as if it is your own. It si gay stuff and people who unprotect maps are gay themselves.
PurgeandFire111 is offline   Reply With Quote
Old 10-23-2008, 05:06 PM   #5 (permalink)
 
SerraAvenger's Avatar

How may I serve you?
 
Join Date: Apr 2007
Posts: 100

SerraAvenger has little to show at this moment (3)


Quote:
Originally Posted by Element of Water View Post
"i" stands for index. So "i2" is index 2... you don't just carry on with the alpabet...
It's bad style.
Also, it is not index 2 but the inner index, which is normally called "j".

Quote:
Originally Posted by Element of Water View Post
Does it matter where you put it? Not really.
It's bad style.
Quote:
Originally Posted by Element of Water View Post
No it's definitely players.
There's definitely more players than 4, or 5.

Quote:
Originally Posted by Element of Water View Post
What's the point in setting a variable for the max number of keys anyway? It's always going to stay 4...
It's bad style.
__________________
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
SerraAvenger is offline   Reply With Quote
Old 10-25-2008, 10:12 AM   #6 (permalink)
 
Element of Water's Avatar

User
 
Join Date: Aug 2008
Posts: 252

Element of Water has little to show at this moment (29)Element of Water has little to show at this moment (29)Element of Water has little to show at this moment (29)


Quote:
It's bad style.
Also, it is not index 2 but the inner index, which is normally called "j".
different people have different opinions on "bad style"
Quote:
It's bad style.
as above...
Quote:
There's definitely more players than 4, or 5.
I know. I'll set that to 12 if you like, but it's there for people to edit. Some people might want maximum efficiency and only have it detect keys down for a few players...
Quote:
It's bad style.
It's not at all. Constants are there to make it easy to change elements of the script. The game can't detect more than the 4 arrow keys anyway, so what's the point in making it changeable in my script when it won't actually do anything?
__________________
Element of Water is offline   Reply With Quote
Old 10-25-2008, 12:09 PM   #7 (permalink)
 
SerraAvenger's Avatar

How may I serve you?
 
Join Date: Apr 2007
Posts: 100

SerraAvenger has little to show at this moment (3)


Quote:
Originally Posted by Element of Water View Post
different people have different opinions on "bad style"
Still there are some conventions.

Quote:
Originally Posted by Element of Water View Post
It's not at all. Constants are there to make it easy to change elements of the script. The game can't detect more than the 4 arrow keys anyway, so what's the point in making it changeable in my script when it won't actually do anything?
Have you ever heard the words "maintainability" and "readability"?
I bet an advanced programmer ( like you ) has, so you should be able to answer your question all on your own.

It is still changeable in your code. I could just go arround and change all your fours to threes, for example.

Constants are there to easily modify your code and to make it more readable. According to your argumentation: Why on earth would somebody name a constant Pi, if he can just aswell write 3.1415926..... ? It is not meant to change anyway!

local integer angle = 0
local unit trigUnit = GetTriggerUnit()

loop
    exitwhen angle > 3.1415962
    call SetUnitX( trigUnit, 10 * Cos( angle ) )
    call SetUnitY( trigUnit, 10 * Sin( angle ) )
    set angle = angle + 0.31415962
endloop



VS

local integer angle = 0
local unit trigUnit = GetTriggerUnit()

loop
    exitwhen angle > PI
    call SetUnitX( trigUnit, RADIUS * Cos( angle ) )
    call SetUnitY( trigUnit, RADIUS * Sin( angle ) )
    set angle = angle + PI/10
endloop

So, what is the point of making 3.1415926 a constant PI, that is changeable with a few clicks?

PS:Yourspacingisquite wierd.
    set KeyDown[MAX_NUMBER_OF_PLAYERS*1+GetPlayerId(GetTriggerPlayer())] = false
__________________
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
SerraAvenger is offline   Reply With Quote
Old 10-27-2008, 07:14 AM   #8 (permalink)
 
Element of Water's Avatar

User
 
Join Date: Aug 2008
Posts: 252

Element of Water has little to show at this moment (29)Element of Water has little to show at this moment (29)Element of Water has little to show at this moment (29)


About the PI thing - it's easy to remember the number 4. PI is very difficult to remember...
__________________
Element of Water is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Arrow Key Movement / Camera System OneEight7even Trigger (GUI) Editor Tutorials 28 11-04-2008 03:43 AM
[JASS] Arrow Key Movement Cov3r70ps Triggers & Scripts 2 02-17-2008 01:55 AM
[Trigger] Arrow Key Hero Selection System - IAlexN IAlexN Triggers & Scripts 2 10-10-2007 04:22 PM
Arrow Key Scroll Cosmonaut Map Development 2 10-13-2005 01:41 PM
Help with arrow key movement? creg216 Map Development 8 02-23-2005 09:32 PM

All times are GMT. The time now is 09:41 AM.






Your link here 
Mortgages | Remortgages | Problem Mortgage | Debt Consolidation | Mobile Phones
Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Copyright©Ralle