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

WASD movement help

Status
Not open for further replies.
Level 7
Joined
Apr 18, 2010
Messages
102
Hi.. Ive been trying to make this script work..

I cant make it so that game displays "hero move" I dont know why....

Right now it detects whenever i press W, A, S, D
detects whether my selected hero is alive..
assigns 1 hero per player (when selected)



Terrain pathability Trigger:
JASS:
library TerrainPathability initializer Init
//******************************************************************************
//* BY: Rising_Dusk
//* 
//* This script can be used to detect the type of pathing at a specific point.
//* It is valuable to do it this way because the IsTerrainPathable is very
//* counterintuitive and returns in odd ways and aren't always as you would
//* expect. This library, however, facilitates detecting those things reliably
//* and easily.
//* 
//******************************************************************************
//* 
//*    > function IsTerrainDeepWater    takes real x, real y returns boolean
//*    > function IsTerrainShallowWater takes real x, real y returns boolean
//*    > function IsTerrainLand         takes real x, real y returns boolean
//*    > function IsTerrainPlatform     takes real x, real y returns boolean
//*    > function IsTerrainWalkable     takes real x, real y returns boolean
//* 
//* These functions return true if the given point is of the type specified
//* in the function's name and false if it is not. For the IsTerrainWalkable
//* function, the MAX_RANGE constant below is the maximum deviation range from
//* the supplied coordinates that will still return true.
//* 
//* The IsTerrainPlatform works for any preplaced walkable destructable. It will
//* return true over bridges, destructable ramps, elevators, and invisible
//* platforms. Walkable destructables created at runtime do not create the same
//* pathing hole as preplaced ones do, so this will return false for them. All
//* other functions except IsTerrainWalkable return false for platforms, because
//* the platform itself erases their pathing when the map is saved.
//* 
//* After calling IsTerrainWalkable(x, y), the following two global variables
//* gain meaning. They return the X and Y coordinates of the nearest walkable
//* point to the specified coordinates. These will only deviate from the
//* IsTerrainWalkable function arguments if the function returned false.
//* 
//* Variables that can be used from the library:
//*     [real]    TerrainPathability_X
//*     [real]    TerrainPathability_Y
//* 
globals
    private constant real    MAX_RANGE     = 10.
    private constant integer DUMMY_ITEM_ID = 'wolg'
endglobals
globals   
    private item       Item   = null
    private rect       Find   = null
    private item array Hid
    private integer    HidMax = 0
    public  real       X      = 0.
    public  real       Y      = 0.
endglobals
function IsTerrainDeepWater takes real x, real y returns boolean
    return not IsTerrainPathable(x, y, PATHING_TYPE_FLOATABILITY) and IsTerrainPathable(x, y, PATHING_TYPE_WALKABILITY)
endfunction
function IsTerrainShallowWater takes real x, real y returns boolean
    return not IsTerrainPathable(x, y, PATHING_TYPE_FLOATABILITY) and not IsTerrainPathable(x, y, PATHING_TYPE_WALKABILITY) and IsTerrainPathable(x, y, PATHING_TYPE_BUILDABILITY)
endfunction
function IsTerrainLand takes real x, real y returns boolean
    return IsTerrainPathable(x, y, PATHING_TYPE_FLOATABILITY)
endfunction
function IsTerrainPlatform takes real x, real y returns boolean
    return not IsTerrainPathable(x, y, PATHING_TYPE_FLOATABILITY) and not IsTerrainPathable(x, y, PATHING_TYPE_WALKABILITY) and not IsTerrainPathable(x, y, PATHING_TYPE_BUILDABILITY)
endfunction
private function HideItem takes nothing returns nothing
    if IsItemVisible(GetEnumItem()) then
        set Hid[HidMax] = GetEnumItem()
        call SetItemVisible(Hid[HidMax], false)
        set HidMax = HidMax + 1
    endif
endfunction
function IsTerrainWalkable takes real x, real y returns boolean
    //Hide any items in the area to avoid conflicts with our item
    call MoveRectTo(Find, x, y)
    call EnumItemsInRect(Find ,null, function HideItem)
    //Try to move the test item and get its coords
    call SetItemPosition(Item, x, y) //Unhides the item
    set X = GetItemX(Item)
    set Y = GetItemY(Item)
    static if LIBRARY_IsTerrainWalkable then
        //This is for compatibility with the IsTerrainWalkable library
        set IsTerrainWalkable_X = X
        set IsTerrainWalkable_Y = Y
    endif
    call SetItemVisible(Item, false)//Hide it again
    //Unhide any items hidden at the start
    loop
        exitwhen HidMax <= 0
        set HidMax = HidMax - 1
        call SetItemVisible(Hid[HidMax], true)
        set Hid[HidMax] = null
    endloop
    //Return walkability
    return (X-x)*(X-x)+(Y-y)*(Y-y) <= MAX_RANGE*MAX_RANGE and not IsTerrainPathable(x, y, PATHING_TYPE_WALKABILITY)
endfunction
private function Init takes nothing returns nothing
    set Find = Rect(0., 0., 128., 128.)
    set Item = CreateItem(DUMMY_ITEM_ID, 0, 0)
    call SetItemVisible(Item, false)
endfunction
endlibrary


JASS:
library WASDmovement initializer OnInit requires TerrainPathability 
    globals
        unit array WASD_Hero
       
        boolean array WASD_IsKeyDown_W
        boolean array WASD_IsKeyDown_A
        boolean array WASD_IsKeyDown_S
        boolean array WASD_IsKeyDown_D
       
        constant oskeytype WASD_KEY_W = ConvertOsKeyType($57)      //Wkey
        //constant oskeytype WASD_KEY_W = ConvertOsKeyType($26) //upkey
        constant oskeytype WASD_KEY_A = ConvertOsKeyType($41)      //Akey
        //constant oskeytype WASD_KEY_A = ConvertOsKeyType($25) //leftkey
        constant oskeytype WASD_KEY_S = ConvertOsKeyType($53)      //Skey
        //constant oskeytype WASD_KEY_S = ConvertOsKeyType($28) //downkey
        constant oskeytype WASD_KEY_D = ConvertOsKeyType($44)      //Dkey
        //constant oskeytype WASD_KEY_D = ConvertOsKeyType($27) //rightkey
       
        private constant real WASD_camera = 1800
        private constant real WASD_fps = 1 / 33.0
        private constant real WASD_agi = 3.0
        private constant real WASD_aspd = 0.03
        private boolean WASD_willmove = false
        private timer WASD_timer = CreateTimer()
   
        group Iswalking = CreateGroup()
        group Inbackswing = CreateGroup()
        group Cannotturn = CreateGroup()
        group Cannotwalk = CreateGroup()
        group Cannotmove = CreateGroup()
        real array WASD_angle
   
    endglobals
   
   
    function MoveUnit takes unit unitu, real pointx, real pointy returns nothing
        if IsTerrainWalkable(pointx, pointy) == true then
        call SetUnitX(unitu, pointx)
        call SetUnitY(unitu, pointy)
        endif
    endfunction
   
    private function WASD_move takes nothing returns nothing
        local integer i = 0
        local real WASD_ms
        local real x1
        local real x2
        local real y1
        local real y2
        local real dx
        local real dy
        local real dr
        loop
            exitwhen i > 24
            set dx = 0.0
            set dy = 0.0
            set WASD_willmove = false
           
            if WASD_IsKeyDown_W[i] == true then
                set dy = dy + 1.0
                set WASD_willmove = true
            endif
           
            if WASD_IsKeyDown_A[i] == true then
                set dx = dx - 1.0
                set WASD_willmove = true
                //call DisplayTextToForce( GetPlayersAll(), "A key is down")
            endif
           
            if WASD_IsKeyDown_S[i] == true then
                set dy = dy - 1.0
                set WASD_willmove = true
                //call DisplayTextToForce( GetPlayersAll(), "S key is down")
            endif
           
            if WASD_IsKeyDown_D[i] == true then
                set dx = dx + 1.0
                set WASD_willmove = true   
                //call DisplayTextToForce( GetPlayersAll(), "D key is down")               
            endif
           
            if dx != 0 and dy != 0 then
                set dx = dx/2
                set dy = dy/2
            endif
           
            if WASD_willmove == true then
                call DisplayTextToPlayer( Player(i),0,0, R2S(dx))
                call DisplayTextToPlayer( Player(i),0,0, R2S(dy))
                set WASD_angle[i] = Atan2(dy, dx)
            endif
           
            //moving hero 
            if GetWidgetLife( WASD_Hero[i]) >= 0.405 then //checks if alive
                //call DisplayTextToForce( GetPlayersAll(), "Hero alive")
           
                if IsUnitInGroup( WASD_Hero[i], Cannotmove) == false and  WASD_willmove == true then
                    set WASD_ms = ( 300 + (WASD_agi * GetHeroAgi(WASD_Hero[i], true) ) )  /  ( 1 / WASD_fps)
                    set x1 = GetUnitX( WASD_Hero[i] )
                    set y1 = GetUnitY( WASD_Hero[i] )
                    set x2 = x1 + WASD_ms*Cos(WASD_angle[i])
                    set y2 = y1 + WASD_ms*Sin(WASD_angle[i])
                    call MoveUnit( WASD_Hero[i], x2, y2)
                    call DisplayTextToForce( GetPlayersAll(), "Hero move")
                   
                endif
               
                //changing angle (movement)
               
                if IsUnitInGroup( WASD_Hero[i], Cannotturn) == false and WASD_willmove == true then
                    call SetUnitFacing( WASD_Hero[i], Rad2Deg( WASD_angle[i]) )
                endif
               
                //walking animation
               
                if IsUnitInGroup( WASD_Hero[i], Cannotwalk) == false and WASD_willmove == true and IsUnitInGroup(WASD_Hero[i], Inbackswing) == false then
                //checked if Unit can walk, will walk, alive, in backswing
               
                //check if its already walking
                    if IsUnitInGroup( WASD_Hero[i], Iswalking) == false then
                        call SetUnitAnimationByIndex( WASD_Hero[i] , 1 )
                        call SetUnitTimeScale( WASD_Hero[i], 1 + WASD_aspd*GetHeroAgi( WASD_Hero[i], true) )
                        call GroupAddUnit(Iswalking, WASD_Hero[i])
                    endif
                endif
               
                //turn off walking animation
                if IsUnitInGroup( WASD_Hero[i] , Cannotwalk) == false and WASD_willmove == false and IsUnitInGroup(WASD_Hero[i], Iswalking) == true and IsUnitPaused(WASD_Hero[i]) == false then
                    call GroupRemoveUnit(Iswalking, WASD_Hero[i])
                    call SetUnitAnimation( WASD_Hero[i], "stand")
                    call SetUnitTimeScale( WASD_Hero[i], 1.0)
                endif
           
            endif
           
            set i = i + 1
        endloop
   
    endfunction
   
   
   
    private function WASD_WP takes nothing returns nothing
        set WASD_IsKeyDown_W[GetPlayerId(GetTriggerPlayer())] = true
        call DisplayTextToForce( GetPlayersAll(), "W key is pressed")
    endfunction
    private function WASD_WR takes nothing returns nothing
        set WASD_IsKeyDown_W[GetPlayerId(GetTriggerPlayer())] = false
        call DisplayTextToForce( GetPlayersAll(), "W key is released")
    endfunction
    private function WASD_AP takes nothing returns nothing
        set WASD_IsKeyDown_A[GetPlayerId(GetTriggerPlayer())] = true
        call DisplayTextToForce( GetPlayersAll(), "A key is pressed")
    endfunction
    private function WASD_AR takes nothing returns nothing
        set WASD_IsKeyDown_A[GetPlayerId(GetTriggerPlayer())] = false
        call DisplayTextToForce( GetPlayersAll(), "A key is released")
    endfunction
    private function WASD_SR takes nothing returns nothing
        set WASD_IsKeyDown_S[GetPlayerId(GetTriggerPlayer())] = false
        call DisplayTextToForce( GetPlayersAll(), "S key is released")
    endfunction
    private function WASD_SP takes nothing returns nothing
        set WASD_IsKeyDown_S[GetPlayerId(GetTriggerPlayer())] = true
        call DisplayTextToForce( GetPlayersAll(), "S key is pressed")
    endfunction
    private function WASD_DP takes nothing returns nothing
        set WASD_IsKeyDown_D[GetPlayerId(GetTriggerPlayer())] = true
        call DisplayTextToForce( GetPlayersAll(), "D key is pressed")
    endfunction
    private function WASD_DR takes nothing returns nothing
        set WASD_IsKeyDown_D[GetPlayerId(GetTriggerPlayer())] = false
        call DisplayTextToForce( GetPlayersAll(), "D key is released")
    endfunction
   
    private function WASD_hero takes nothing returns nothing
        if GetOwningPlayer(GetTriggerUnit()) == GetTriggerPlayer() and IsUnitType(GetTriggerUnit(), UNIT_TYPE_HERO) == true  then
        set WASD_Hero[GetConvertedPlayerId(GetTriggerPlayer())] = GetTriggerUnit()
        call DisplayTextToForce( GetPlayersAll(), GetUnitName(WASD_Hero[GetConvertedPlayerId(GetTriggerPlayer())]))
        //call UnitRemoveAbility(GetTriggerUnit(), 'Amov')
        call BlzUnitHideAbility( GetTriggerUnit(), 'Amov', true )
        call SetCameraTargetControllerNoZForPlayer(GetTriggerPlayer(), GetTriggerUnit(), 0, 0 , false)
        call SetCameraFieldForPlayer( GetTriggerPlayer(), CAMERA_FIELD_TARGET_DISTANCE, WASD_camera, 0 )
        endif
    endfunction
   
    private function OnInit takes nothing returns nothing
        local trigger wp = CreateTrigger()
        local trigger wr = CreateTrigger()
        local trigger ap = CreateTrigger()
        local trigger ar = CreateTrigger()
        local trigger sp = CreateTrigger()
        local trigger sr = CreateTrigger()
        local trigger dp = CreateTrigger()
        local trigger dr = CreateTrigger()
        local integer i = 0
        local trigger hs = CreateTrigger()
       
        loop
            exitwhen i > 24
            call BlzTriggerRegisterPlayerKeyEvent( wp, Player(i), WASD_KEY_W, 0, true) //for detecting press
            call BlzTriggerRegisterPlayerKeyEvent( wr, Player(i), WASD_KEY_W, 0, false) 
            call DisplayTextToForce( GetPlayersAll(), "W Registed")
           
           
            call BlzTriggerRegisterPlayerKeyEvent( dp, Player(i), WASD_KEY_D, 0, true)
            call BlzTriggerRegisterPlayerKeyEvent( dr, Player(i), WASD_KEY_D, 0, false)
           
            call BlzTriggerRegisterPlayerKeyEvent( ap, Player(i), WASD_KEY_A, 0, true)
            call BlzTriggerRegisterPlayerKeyEvent( ar, Player(i), WASD_KEY_A, 0, false)
           
            call BlzTriggerRegisterPlayerKeyEvent( sp, Player(i), WASD_KEY_S, 0, true)
            call BlzTriggerRegisterPlayerKeyEvent( sr, Player(i), WASD_KEY_S, 0, false)
           
            call TriggerRegisterPlayerSelectionEventBJ( hs, Player(i), true )
            set i = i + 1
        endloop
       
        call TriggerAddAction(wp, function WASD_WP)
        call TriggerAddAction(wr, function WASD_WR)
        call TriggerAddAction(ap, function WASD_AP)
        call TriggerAddAction(ar, function WASD_AR)
        call TriggerAddAction(sp, function WASD_SP)
        call TriggerAddAction(sr, function WASD_SR)
        call TriggerAddAction(dp, function WASD_DP)
        call TriggerAddAction(dr, function WASD_DR)
       
        call TriggerAddAction(hs, function WASD_hero)
       
        set wp = null
        set wr = null
        set ap = null
        set ar = null
        set dp = null
        set dr = null
        set sp = null
        set sr = null
        set hs = null
   
        call TimerStart( WASD_timer, WASD_fps, true, function WASD_move)
    endfunction
endlibrary
 
Level 39
Joined
Feb 27, 2007
Messages
5,023
  • You're using GetConvertedPlayerId() in WASD_hero but GetPlayerId() in all the other functions.

  • You should only set WASD_willmove to true inside the if dx != 0 and dy != 0 block; if both A and D are pressed dx will be 0 so the unit shouldn't move, but your system will think it should since willmove was set to true.
 
Level 7
Joined
Apr 18, 2010
Messages
102
  • You're using GetConvertedPlayerId() in WASD_hero but GetPlayerId() in all the other functions.

  • You should only set WASD_willmove to true inside the if dx != 0 and dy != 0 block; if both A and D are pressed dx will be 0 so the unit shouldn't move, but your system will think it should since willmove was set to true.

ooohhh.. man such a small mistake haha. kept me bothered for awhile... will test later :goblin_cry: thank you sir!!!! +rep
 
Status
Not open for further replies.
Top